0000: 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
0010: 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
0020: 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
0030: 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
0040: 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
0050: 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
0060: 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
0070: 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
0080: 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
0090: 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
00a0: 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
00b0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
00c0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
00d0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
00e0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
00f0: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
0100: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
0110: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
0120: 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
0180: 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 ile contains C c
0190: 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 ode routines tha
01a0: 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 t are called by
01b0: 74 68 65 20 53 51 4c 69 74 65 20 70 61 72 73 65 the SQLite parse
01c0: 72 0a 2a 2a 20 77 68 65 6e 20 73 79 6e 74 61 78 r.** when syntax
01d0: 20 72 75 6c 65 73 20 61 72 65 20 72 65 64 75 63 rules are reduc
01e0: 65 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 ed. The routine
01f0: 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 68 s in this file h
0200: 61 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 66 6f 6c andle the.** fol
0210: 6c 6f 77 69 6e 67 20 6b 69 6e 64 73 20 6f 66 20 lowing kinds of
0220: 53 51 4c 20 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a SQL syntax:.**.*
0230: 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 * CREATE TAB
0240: 4c 45 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 54 LE.** DROP T
0250: 41 42 4c 45 0a 2a 2a 20 20 20 20 20 43 52 45 41 ABLE.** CREA
0260: 54 45 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 TE INDEX.**
0270: 44 52 4f 50 20 49 4e 44 45 58 0a 2a 2a 20 20 20 DROP INDEX.**
0280: 20 20 63 72 65 61 74 69 6e 67 20 49 44 20 6c 69 creating ID li
0290: 73 74 73 0a 2a 2a 20 20 20 20 20 42 45 47 49 4e sts.** BEGIN
02a0: 20 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 TRANSACTION.**
02b0: 20 20 20 20 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 COMMIT.**
02c0: 20 20 52 4f 4c 4c 42 41 43 4b 0a 2a 2f 0a 23 69 ROLLBACK.*/.#i
02d0: 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 6e nclude "sqliteIn
02e0: 74 2e 68 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 t.h"../*.** This
02f0: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
0300: 65 64 20 77 68 65 6e 20 61 20 6e 65 77 20 53 51 ed when a new SQ
0310: 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 L statement is b
0320: 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 62 eginning to.** b
0330: 65 20 70 61 72 73 65 64 2e 20 20 49 6e 69 74 69 e parsed. Initi
0340: 61 6c 69 7a 65 20 74 68 65 20 70 50 61 72 73 65 alize the pParse
0350: 20 73 74 72 75 63 74 75 72 65 20 61 73 20 6e 65 structure as ne
0360: 65 64 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 eded..*/.void sq
0370: 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 lite3BeginParse(
0380: 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
0390: 6e 74 20 65 78 70 6c 61 69 6e 46 6c 61 67 29 7b nt explainFlag){
03a0: 0a 20 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 . pParse->expla
03b0: 69 6e 20 3d 20 28 75 38 29 65 78 70 6c 61 69 6e in = (u8)explain
03c0: 46 6c 61 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e Flag;. pParse->
03d0: 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 nVar = 0;.}..#if
03e0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
03f0: 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a _SHARED_CACHE./*
0400: 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 4c 6f 63 .** The TableLoc
0410: 6b 20 73 74 72 75 63 74 75 72 65 20 69 73 20 6f k structure is o
0420: 6e 6c 79 20 75 73 65 64 20 62 79 20 74 68 65 20 nly used by the
0430: 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
0440: 28 29 20 61 6e 64 0a 2a 2a 20 63 6f 64 65 54 61 () and.** codeTa
0450: 62 6c 65 4c 6f 63 6b 73 28 29 20 66 75 6e 63 74 bleLocks() funct
0460: 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ions..*/.struct
0470: 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e TableLock {. in
0480: 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 t iDb;
0490: 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
04a0: 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 se containing th
04b0: 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f e table to be lo
04c0: 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 cked */. int iT
04d0: 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ab; /
04e0: 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 * The root page
04f0: 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 of the table to
0500: 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 be locked */. u
0510: 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 20 20 8 isWriteLock;
0520: 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 /* True for
0530: 77 72 69 74 65 20 6c 6f 63 6b 2e 20 20 46 61 6c write lock. Fal
0540: 73 65 20 66 6f 72 20 61 20 72 65 61 64 20 6c 6f se for a read lo
0550: 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ck */. const ch
0560: 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 ar *zName; /*
0570: 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c Name of the tabl
0580: 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 e */.};../*.** R
0590: 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 ecord the fact t
05a0: 68 61 74 20 77 65 20 77 61 6e 74 20 74 6f 20 6c hat we want to l
05b0: 6f 63 6b 20 61 20 74 61 62 6c 65 20 61 74 20 72 ock a table at r
05c0: 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a 2a 0a 2a 2a un-time. .**.**
05d0: 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 The table to be
05e0: 20 6c 6f 63 6b 65 64 20 68 61 73 20 72 6f 6f 74 locked has root
05f0: 20 70 61 67 65 20 69 54 61 62 20 61 6e 64 20 69 page iTab and i
0600: 73 20 66 6f 75 6e 64 20 69 6e 20 64 61 74 61 62 s found in datab
0610: 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 20 72 65 ase iDb..** A re
0620: 61 64 20 6f 72 20 61 20 77 72 69 74 65 20 6c 6f ad or a write lo
0630: 63 6b 20 63 61 6e 20 62 65 20 74 61 6b 65 6e 20 ck can be taken
0640: 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 69 73 57 depending on isW
0650: 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 ritelock..**.**
0660: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 This routine jus
0670: 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 66 61 t records the fa
0680: 63 74 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b ct that the lock
0690: 20 69 73 20 64 65 73 69 72 65 64 2e 20 20 54 68 is desired. Th
06a0: 65 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 6d 61 6b e.** code to mak
06b0: 65 20 74 68 65 20 6c 6f 63 6b 20 6f 63 63 75 72 e the lock occur
06c0: 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 is generated by
06d0: 20 61 20 6c 61 74 65 72 20 63 61 6c 6c 20 74 6f a later call to
06e0: 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 .** codeTableLoc
06f0: 6b 73 28 29 20 77 68 69 63 68 20 6f 63 63 75 72 ks() which occur
0700: 73 20 64 75 72 69 6e 67 20 73 71 6c 69 74 65 33 s during sqlite3
0710: 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a FinishCoding()..
0720: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 54 */.void sqlite3T
0730: 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50 61 72 73 ableLock(. Pars
0740: 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f e *pParse, /
0750: 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
0760: 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 t */. int iDb,
0770: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
0780: 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ex of the databa
0790: 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 se containing th
07a0: 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 20 e table to lock
07b0: 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20 */. int iTab,
07c0: 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
07d0: 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
07e0: 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c he table to be l
07f0: 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 ocked */. u8 is
0800: 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20 20 2f 2a WriteLock, /*
0810: 20 54 72 75 65 20 66 6f 72 20 61 20 77 72 69 74 True for a writ
0820: 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 e lock */. cons
0830: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 2f t char *zName /
0840: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 * Name of the ta
0850: 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 ble to be locked
0860: 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a */.){. Parse *
0870: 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 pToplevel = sqli
0880: 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c te3ParseToplevel
0890: 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 (pParse);. int
08a0: 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b i;. int nBytes;
08b0: 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b . TableLock *p;
08c0: 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d . assert( iDb>=
08d0: 30 20 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 0 );.. for(i=0;
08e0: 20 69 3c 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 i<pToplevel->nT
08f0: 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a ableLock; i++){.
0900: 20 20 20 20 70 20 3d 20 26 70 54 6f 70 6c 65 76 p = &pToplev
0910: 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 el->aTableLock[i
0920: 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 44 ];. if( p->iD
0930: 62 3d 3d 69 44 62 20 26 26 20 70 2d 3e 69 54 61 b==iDb && p->iTa
0940: 62 3d 3d 69 54 61 62 20 29 7b 0a 20 20 20 20 20 b==iTab ){.
0950: 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 p->isWriteLock
0960: 3d 20 28 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 = (p->isWriteLoc
0970: 6b 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b k || isWriteLock
0980: 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b );. return;
0990: 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 . }. }.. nB
09a0: 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 54 61 ytes = sizeof(Ta
09b0: 62 6c 65 4c 6f 63 6b 29 20 2a 20 28 70 54 6f 70 bleLock) * (pTop
09c0: 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 level->nTableLoc
09d0: 6b 2b 31 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 k+1);. pTopleve
09e0: 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 0a l->aTableLock =.
09f0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 sqlite3DbR
0a00: 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 54 6f eallocOrFree(pTo
0a10: 70 6c 65 76 65 6c 2d 3e 64 62 2c 20 70 54 6f 70 plevel->db, pTop
0a20: 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 level->aTableLoc
0a30: 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 k, nBytes);. if
0a40: 28 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 ( pToplevel->aTa
0a50: 62 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 bleLock ){. p
0a60: 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 = &pToplevel->a
0a70: 54 61 62 6c 65 4c 6f 63 6b 5b 70 54 6f 70 6c 65 TableLock[pTople
0a80: 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b vel->nTableLock+
0a90: 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d +];. p->iDb =
0aa0: 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 iDb;. p->iTa
0ab0: 62 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 70 2d b = iTab;. p-
0ac0: 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69 >isWriteLock = i
0ad0: 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 sWriteLock;.
0ae0: 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 p->zName = zName
0af0: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
0b00: 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 Toplevel->nTable
0b10: 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 54 Lock = 0;. pT
0b20: 6f 70 6c 65 76 65 6c 2d 3e 64 62 2d 3e 6d 61 6c oplevel->db->mal
0b30: 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
0b40: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 }.}../*.** Code
0b50: 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b an OP_TableLock
0b60: 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 instruction for
0b70: 20 65 61 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b each table lock
0b80: 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 ed by the.** sta
0b90: 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 tement (configur
0ba0: 65 64 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 ed by calls to s
0bb0: 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 qlite3TableLock(
0bc0: 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ))..*/.static vo
0bd0: 69 64 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b id codeTableLock
0be0: 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 s(Parse *pParse)
0bf0: 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 {. int i;. Vdb
0c00: 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20 20 70 56 e *pVdbe; .. pV
0c10: 64 62 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 dbe = sqlite3Get
0c20: 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
0c30: 61 73 73 65 72 74 28 20 70 56 64 62 65 21 3d 30 assert( pVdbe!=0
0c40: 20 29 3b 20 2f 2a 20 73 71 6c 69 74 65 33 47 65 ); /* sqlite3Ge
0c50: 74 56 64 62 65 20 63 61 6e 6e 6f 74 20 66 61 69 tVdbe cannot fai
0c60: 6c 3a 20 56 44 42 45 20 61 6c 72 65 61 64 79 20 l: VDBE already
0c70: 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 allocated */..
0c80: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 for(i=0; i<pPars
0c90: 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 e->nTableLock; i
0ca0: 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f ++){. TableLo
0cb0: 63 6b 20 2a 70 20 3d 20 26 70 50 61 72 73 65 2d ck *p = &pParse-
0cc0: 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a >aTableLock[i];.
0cd0: 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e int p1 = p->
0ce0: 69 44 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 iDb;. sqlite3
0cf0: 56 64 62 65 41 64 64 4f 70 34 28 70 56 64 62 65 VdbeAddOp4(pVdbe
0d00: 2c 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 , OP_TableLock,
0d10: 70 31 2c 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e p1, p->iTab, p->
0d20: 69 73 57 72 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 isWriteLock,.
0d30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0d40: 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f p->zName, P4_
0d50: 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 STATIC);. }.}.#
0d60: 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 63 else. #define c
0d70: 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 odeTableLocks(x)
0d80: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
0d90: 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 eturn TRUE if th
0da0: 65 20 67 69 76 65 6e 20 79 44 62 4d 61 73 6b 20 e given yDbMask
0db0: 6f 62 6a 65 63 74 20 69 73 20 65 6d 70 74 79 20 object is empty
0dc0: 2d 20 69 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 - if it contains
0dd0: 20 6e 6f 0a 2a 2a 20 31 20 62 69 74 73 2e 20 20 no.** 1 bits.
0de0: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
0df0: 75 73 65 64 20 62 79 20 74 68 65 20 44 62 4d 61 used by the DbMa
0e00: 73 6b 41 6c 6c 5a 65 72 6f 28 29 20 61 6e 64 20 skAllZero() and
0e10: 44 62 4d 61 73 6b 4e 6f 74 5a 65 72 6f 28 29 0a DbMaskNotZero().
0e20: 2a 2a 20 6d 61 63 72 6f 73 20 77 68 65 6e 20 53 ** macros when S
0e30: 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 QLITE_MAX_ATTACH
0e40: 45 44 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ED is greater th
0e50: 61 6e 20 33 30 2e 0a 2a 2f 0a 23 69 66 20 53 51 an 30..*/.#if SQ
0e60: 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 LITE_MAX_ATTACHE
0e70: 44 3e 33 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 D>30.int sqlite3
0e80: 44 62 4d 61 73 6b 41 6c 6c 5a 65 72 6f 28 79 44 DbMaskAllZero(yD
0e90: 62 4d 61 73 6b 20 6d 29 7b 0a 20 20 69 6e 74 20 bMask m){. int
0ea0: 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
0eb0: 73 69 7a 65 6f 66 28 79 44 62 4d 61 73 6b 29 3b sizeof(yDbMask);
0ec0: 20 69 2b 2b 29 20 69 66 28 20 6d 5b 69 5d 20 29 i++) if( m[i] )
0ed0: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 return 0;. ret
0ee0: 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn 1;.}.#endif.
0ef0: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
0f00: 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 ine is called af
0f10: 74 65 72 20 61 20 73 69 6e 67 6c 65 20 53 51 4c ter a single SQL
0f20: 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 statement has b
0f30: 65 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61 6e een.** parsed an
0f40: 64 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d d a VDBE program
0f50: 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 61 74 to execute that
0f60: 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 statement has b
0f70: 65 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64 2e een.** prepared.
0f80: 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 This routine p
0f90: 75 74 73 20 74 68 65 20 66 69 6e 69 73 68 69 6e uts the finishin
0fa0: 67 20 74 6f 75 63 68 65 73 20 6f 6e 20 74 68 65 g touches on the
0fb0: 0a 2a 2a 20 56 44 42 45 20 70 72 6f 67 72 61 6d .** VDBE program
0fc0: 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65 20 and resets the
0fd0: 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 pParse structure
0fe0: 20 66 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a 2a for the next.**
0ff0: 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f parse..**.** No
1000: 74 65 20 74 68 61 74 20 69 66 20 61 6e 20 65 72 te that if an er
1010: 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 69 74 ror occurred, it
1020: 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 61 might be the ca
1030: 73 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 44 se that.** no VD
1040: 42 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e 65 BE code was gene
1050: 72 61 74 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 rated..*/.void s
1060: 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 qlite3FinishCodi
1070: 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ng(Parse *pParse
1080: 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1090: 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 ;. Vdbe *v;..
10a0: 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
10b0: 70 54 6f 70 6c 65 76 65 6c 3d 3d 30 20 29 3b 0a pToplevel==0 );.
10c0: 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
10d0: 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d b;. if( pParse-
10e0: 3e 6e 65 73 74 65 64 20 29 20 72 65 74 75 72 6e >nested ) return
10f0: 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c ;. if( db->mall
1100: 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 ocFailed || pPar
1110: 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 se->nErr ){.
1120: 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d if( pParse->rc==
1130: 53 51 4c 49 54 45 5f 4f 4b 20 29 20 70 50 61 72 SQLITE_OK ) pPar
1140: 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
1150: 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 ERROR;. retur
1160: 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 n;. }.. /* Beg
1170: 69 6e 20 62 79 20 67 65 6e 65 72 61 74 69 6e 67 in by generating
1180: 20 73 6f 6d 65 20 74 65 72 6d 69 6e 61 74 69 6f some terminatio
1190: 6e 20 63 6f 64 65 20 61 74 20 74 68 65 20 65 6e n code at the en
11a0: 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 76 64 d of the. ** vd
11b0: 62 65 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2f 0a be program. */.
11c0: 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 v = sqlite3Get
11d0: 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
11e0: 61 73 73 65 72 74 28 20 21 70 50 61 72 73 65 2d assert( !pParse-
11f0: 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 0a 20 >isMultiWrite .
1200: 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
1210: 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f VdbeAssertMayAbo
1220: 72 74 28 76 2c 20 70 50 61 72 73 65 2d 3e 6d 61 rt(v, pParse->ma
1230: 79 41 62 6f 72 74 29 29 3b 0a 20 20 69 66 28 20 yAbort));. if(
1240: 76 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 v ){. while(
1250: 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
1260: 65 50 72 69 6f 72 4f 70 63 6f 64 65 28 76 2c 20 ePriorOpcode(v,
1270: 4f 50 5f 43 6c 6f 73 65 29 20 29 7b 7d 0a 20 20 OP_Close) ){}.
1280: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1290: 4f 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b Op0(v, OP_Halt);
12a0: 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 55 53 45 ..#if SQLITE_USE
12b0: 52 5f 41 55 54 48 45 4e 54 49 43 41 54 49 4f 4e R_AUTHENTICATION
12c0: 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d . if( pParse-
12d0: 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3e 30 20 26 26 >nTableLock>0 &&
12e0: 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d db->init.busy==
12f0: 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1300: 65 33 55 73 65 72 41 75 74 68 49 6e 69 74 28 64 e3UserAuthInit(d
1310: 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 b);. if( db
1320: 2d 3e 61 75 74 68 2e 61 75 74 68 4c 65 76 65 6c ->auth.authLevel
1330: 3c 55 41 55 54 48 5f 55 73 65 72 20 29 7b 0a 20 <UAUTH_User ){.
1340: 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 pParse->r
1350: 63 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 5f c = SQLITE_AUTH_
1360: 55 53 45 52 3b 0a 20 20 20 20 20 20 20 20 73 71 USER;. sq
1370: 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1380: 61 72 73 65 2c 20 22 75 73 65 72 20 6e 6f 74 20 arse, "user not
1390: 61 75 74 68 65 6e 74 69 63 61 74 65 64 22 29 3b authenticated");
13a0: 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b . return;
13b0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
13c0: 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 endif.. /* Th
13d0: 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f e cookie mask co
13e0: 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 ntains one bit f
13f0: 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65 or each database
1400: 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 file open..
1410: 2a 2a 20 28 42 69 74 20 30 20 69 73 20 66 6f 72 ** (Bit 0 is for
1420: 20 6d 61 69 6e 2c 20 62 69 74 20 31 20 69 73 20 main, bit 1 is
1430: 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f for temp, and so
1440: 20 66 6f 72 74 68 2e 29 20 20 42 69 74 73 20 61 forth.) Bits a
1450: 72 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 66 6f re. ** set fo
1460: 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 r each database
1470: 74 68 61 74 20 69 73 20 75 73 65 64 2e 20 20 47 that is used. G
1480: 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
1490: 73 74 61 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 start a. ** t
14a0: 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 ransaction on ea
14b0: 63 68 20 75 73 65 64 20 64 61 74 61 62 61 73 65 ch used database
14c0: 20 61 6e 64 20 74 6f 20 76 65 72 69 66 79 20 74 and to verify t
14d0: 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 he schema cookie
14e0: 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 . ** on each
14f0: 75 73 65 64 20 64 61 74 61 62 61 73 65 2e 0a 20 used database..
1500: 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 */. if( db
1510: 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d ->mallocFailed==
1520: 30 20 0a 20 20 20 20 20 26 26 20 28 44 62 4d 61 0 . && (DbMa
1530: 73 6b 4e 6f 6e 5a 65 72 6f 28 70 50 61 72 73 65 skNonZero(pParse
1540: 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 29 20 7c 7c ->cookieMask) ||
1550: 20 70 50 61 72 73 65 2d 3e 70 43 6f 6e 73 74 45 pParse->pConstE
1560: 78 70 72 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 xpr). ){.
1570: 20 20 69 6e 74 20 69 44 62 2c 20 69 3b 0a 20 20 int iDb, i;.
1580: 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
1590: 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 te3VdbeGetOp(v,
15a0: 30 29 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 0)->opcode==OP_I
15b0: 6e 69 74 20 29 3b 0a 20 20 20 20 20 20 73 71 6c nit );. sql
15c0: 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
15d0: 28 76 2c 20 30 29 3b 0a 20 20 20 20 20 20 66 6f (v, 0);. fo
15e0: 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64 62 2d r(iDb=0; iDb<db-
15f0: 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20 20 >nDb; iDb++){.
1600: 20 20 20 20 20 20 69 66 28 20 44 62 4d 61 73 6b if( DbMask
1610: 54 65 73 74 28 70 50 61 72 73 65 2d 3e 63 6f 6f Test(pParse->coo
1620: 6b 69 65 4d 61 73 6b 2c 20 69 44 62 29 3d 3d 30 kieMask, iDb)==0
1630: 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
1640: 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1650: 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 UsesBtree(v, iDb
1660: 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1670: 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 e3VdbeAddOp4Int(
1680: 76 2c 0a 20 20 20 20 20 20 20 20 20 20 4f 50 5f v,. OP_
1690: 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 Transaction,
16a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b0: 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 /* Opcode */.
16c0: 20 20 20 20 20 20 20 69 44 62 2c 20 20 20 20 20 iDb,
16d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16e0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20 /* P1
16f0: 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 44 62 4d */. DbM
1700: 61 73 6b 54 65 73 74 28 70 50 61 72 73 65 2d 3e askTest(pParse->
1710: 77 72 69 74 65 4d 61 73 6b 2c 69 44 62 29 2c 20 writeMask,iDb),
1720: 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 20 20 20 /* P2 */.
1730: 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 pParse->cooki
1740: 65 56 61 6c 75 65 5b 69 44 62 5d 2c 20 20 20 20 eValue[iDb],
1750: 20 20 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a 20 /* P3 */.
1760: 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 db->aDb
1770: 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 [iDb].pSchema->i
1780: 47 65 6e 65 72 61 74 69 6f 6e 20 20 2f 2a 20 50 Generation /* P
1790: 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 29 3b 0a 4 */. );.
17a0: 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e if( db->
17b0: 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 20 73 init.busy==0 ) s
17c0: 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
17d0: 50 35 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 P5(v, 1);.
17e0: 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
17f0: 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1800: 4c 45 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 LE. for(i=0
1810: 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 74 61 ; i<pParse->nVta
1820: 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 bLock; i++){.
1830: 20 20 20 20 20 63 68 61 72 20 2a 76 74 61 62 20 char *vtab
1840: 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
1850: 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 3GetVTable(db, p
1860: 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 Parse->apVtabLoc
1870: 6b 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 73 k[i]);. s
1880: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1890: 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 (v, OP_VBegin, 0
18a0: 2c 20 30 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 , 0, 0, vtab, P4
18b0: 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20 7d 0a _VTAB);. }.
18c0: 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 pParse->nV
18d0: 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 23 65 6e tabLock = 0;.#en
18e0: 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 6e dif.. /* On
18f0: 63 65 20 61 6c 6c 20 74 68 65 20 63 6f 6f 6b 69 ce all the cooki
1900: 65 73 20 68 61 76 65 20 62 65 65 6e 20 76 65 72 es have been ver
1910: 69 66 69 65 64 20 61 6e 64 20 74 72 61 6e 73 61 ified and transa
1920: 63 74 69 6f 6e 73 20 6f 70 65 6e 65 64 2c 20 0a ctions opened, .
1930: 20 20 20 20 20 20 2a 2a 20 6f 62 74 61 69 6e 20 ** obtain
1940: 74 68 65 20 72 65 71 75 69 72 65 64 20 74 61 62 the required tab
1950: 6c 65 2d 6c 6f 63 6b 73 2e 20 54 68 69 73 20 69 le-locks. This i
1960: 73 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 s a no-op unless
1970: 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 the . ** s
1980: 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 hared-cache feat
1990: 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a ure is enabled..
19a0: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 */. c
19b0: 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 50 odeTableLocks(pP
19c0: 61 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a arse);.. /*
19d0: 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 79 20 Initialize any
19e0: 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 64 61 AUTOINCREMENT da
19f0: 74 61 20 73 74 72 75 63 74 75 72 65 73 20 72 65 ta structures re
1a00: 71 75 69 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f quired.. */
1a10: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 75 . sqlite3Au
1a20: 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e toincrementBegin
1a30: 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 20 (pParse);..
1a40: 20 2f 2a 20 43 6f 64 65 20 63 6f 6e 73 74 61 6e /* Code constan
1a50: 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 t expressions th
1a60: 61 74 20 77 68 65 72 65 20 66 61 63 74 6f 72 65 at where factore
1a70: 64 20 6f 75 74 20 6f 66 20 69 6e 6e 65 72 20 6c d out of inner l
1a80: 6f 6f 70 73 20 2a 2f 0a 20 20 20 20 20 20 69 66 oops */. if
1a90: 28 20 70 50 61 72 73 65 2d 3e 70 43 6f 6e 73 74 ( pParse->pConst
1aa0: 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 Expr ){.
1ab0: 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 20 3d 20 ExprList *pEL =
1ac0: 70 50 61 72 73 65 2d 3e 70 43 6f 6e 73 74 45 78 pParse->pConstEx
1ad0: 70 72 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 pr;. pPar
1ae0: 73 65 2d 3e 6f 6b 43 6f 6e 73 74 46 61 63 74 6f se->okConstFacto
1af0: 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 r = 0;. f
1b00: 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 2d 3e 6e or(i=0; i<pEL->n
1b10: 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
1b20: 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1b30: 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 rCode(pParse, pE
1b40: 4c 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 L->a[i].pExpr, p
1b50: 45 4c 2d 3e 61 5b 69 5d 2e 75 2e 69 43 6f 6e 73 EL->a[i].u.iCons
1b60: 74 45 78 70 72 52 65 67 29 3b 0a 20 20 20 20 20 tExprReg);.
1b70: 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 }. }..
1b80: 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 /* Finally,
1b90: 6a 75 6d 70 20 62 61 63 6b 20 74 6f 20 74 68 65 jump back to the
1ba0: 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
1bb0: 65 20 65 78 65 63 75 74 61 62 6c 65 20 63 6f 64 e executable cod
1bc0: 65 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 e. */. sqli
1bd0: 74 65 33 56 64 62 65 47 6f 74 6f 28 76 2c 20 31 te3VdbeGoto(v, 1
1be0: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 );. }. }...
1bf0: 20 2f 2a 20 47 65 74 20 74 68 65 20 56 44 42 45 /* Get the VDBE
1c00: 20 70 72 6f 67 72 61 6d 20 72 65 61 64 79 20 66 program ready f
1c10: 6f 72 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a or execution. *
1c20: 2f 0a 20 20 69 66 28 20 76 20 26 26 20 70 50 61 /. if( v && pPa
1c30: 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 rse->nErr==0 &&
1c40: 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 !db->mallocFaile
1c50: 64 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 d ){. assert(
1c60: 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c pParse->iCacheL
1c70: 65 76 65 6c 3d 3d 30 20 29 3b 20 20 2f 2a 20 44 evel==0 ); /* D
1c80: 69 73 61 62 6c 65 73 20 61 6e 64 20 72 65 2d 65 isables and re-e
1c90: 6e 61 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f 0a nables match */.
1ca0: 20 20 20 20 2f 2a 20 41 20 6d 69 6e 69 6d 75 6d /* A minimum
1cb0: 20 6f 66 20 6f 6e 65 20 63 75 72 73 6f 72 20 69 of one cursor i
1cc0: 73 20 72 65 71 75 69 72 65 64 20 69 66 20 61 75 s required if au
1cd0: 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 toincrement is u
1ce0: 73 65 64 0a 20 20 20 20 2a 20 20 53 65 65 20 74 sed. * See t
1cf0: 69 63 6b 65 74 20 5b 61 36 39 36 33 37 39 63 31 icket [a696379c1
1d00: 66 30 38 38 36 36 5d 20 2a 2f 0a 20 20 20 20 69 f08866] */. i
1d10: 66 28 20 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 f( pParse->pAinc
1d20: 21 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 6e !=0 && pParse->n
1d30: 54 61 62 3d 3d 30 20 29 20 70 50 61 72 73 65 2d Tab==0 ) pParse-
1d40: 3e 6e 54 61 62 20 3d 20 31 3b 0a 20 20 20 20 73 >nTab = 1;. s
1d50: 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 qlite3VdbeMakeRe
1d60: 61 64 79 28 76 2c 20 70 50 61 72 73 65 29 3b 0a ady(v, pParse);.
1d70: 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d pParse->rc =
1d80: 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
1d90: 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d pParse->colNam
1da0: 65 73 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c esSet = 0;. }el
1db0: 73 65 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e se{. pParse->
1dc0: 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1dd0: 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d R;. }. pParse-
1de0: 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50 61 >nTab = 0;. pPa
1df0: 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20 rse->nMem = 0;.
1e00: 20 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20 pParse->nSet =
1e10: 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 0;. pParse->nVa
1e20: 72 20 3d 20 30 3b 0a 20 20 44 62 4d 61 73 6b 5a r = 0;. DbMaskZ
1e30: 65 72 6f 28 70 50 61 72 73 65 2d 3e 63 6f 6f 6b ero(pParse->cook
1e40: 69 65 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ieMask);.}../*.*
1e50: 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65 72 * Run the parser
1e60: 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 and code genera
1e70: 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c 79 20 tor recursively
1e80: 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 in order to gene
1e90: 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 rate.** code for
1ea0: 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
1eb0: 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20 74 68 nt given onto th
1ec0: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 50 61 e end of the pPa
1ed0: 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 63 rse context.** c
1ee0: 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 urrently under c
1ef0: 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 57 68 onstruction. Wh
1f00: 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 69 73 en the parser is
1f10: 20 72 75 6e 20 72 65 63 75 72 73 69 76 65 6c 79 run recursively
1f20: 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20 74 68 .** this way, th
1f30: 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 e final OP_Halt
1f40: 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65 64 20 is not appended
1f50: 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74 69 61 and other initia
1f60: 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 lization.** and
1f70: 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73 74 65 finalization ste
1f80: 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 62 ps are omitted b
1f90: 65 63 61 75 73 65 20 74 68 6f 73 65 20 61 72 65 ecause those are
1fa0: 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 68 65 handling by the
1fb0: 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20 70 61 .** outermost pa
1fc0: 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 rser..**.** Not
1fd0: 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 6e 65 everything is ne
1fe0: 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20 66 61 stable. This fa
1ff0: 63 69 6c 69 74 79 20 69 73 20 64 65 73 69 67 6e cility is design
2000: 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a 2a 20 ed to permit.**
2010: 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 INSERT, UPDATE,
2020: 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 72 61 and DELETE opera
2030: 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 53 51 tions against SQ
2040: 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 55 73 LITE_MASTER. Us
2050: 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 6f 75 e.** care if you
2060: 20 64 65 63 69 64 65 20 74 6f 20 74 72 79 20 74 decide to try t
2070: 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 69 o use this routi
2080: 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 ne for some othe
2090: 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a 76 r purposes..*/.v
20a0: 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 oid sqlite3Neste
20b0: 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 dParse(Parse *pP
20c0: 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 arse, const char
20d0: 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
20e0: 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
20f0: 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 63 char *zSql;. c
2100: 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 har *zErrMsg = 0
2110: 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
2120: 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 20 = pParse->db;.#
2130: 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20 20 define SAVE_SZ
2140: 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20 2d (sizeof(Parse) -
2150: 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65 2c offsetof(Parse,
2160: 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73 61 nVar)). char sa
2170: 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b 0a veBuf[SAVE_SZ];.
2180: 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e . if( pParse->n
2190: 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 Err ) return;.
21a0: 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
21b0: 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f 2a nested<10 ); /*
21c0: 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64 20 Nesting should
21d0: 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69 74 only be of limit
21e0: 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76 61 ed depth */. va
21f0: 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
2200: 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 at);. zSql = sq
2210: 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
2220: 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
2230: 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
2240: 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 if( zSql==0 ){.
2250: 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a 20 return; /*
2260: 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 A malloc must ha
2270: 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d ve failed */. }
2280: 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 . pParse->neste
2290: 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73 61 d++;. memcpy(sa
22a0: 76 65 42 75 66 2c 20 26 70 50 61 72 73 65 2d 3e veBuf, &pParse->
22b0: 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b 0a nVar, SAVE_SZ);.
22c0: 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 memset(&pParse
22d0: 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56 45 5f ->nVar, 0, SAVE_
22e0: 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 75 SZ);. sqlite3Ru
22f0: 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20 nParser(pParse,
2300: 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b zSql, &zErrMsg);
2310: 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
2320: 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 (db, zErrMsg);.
2330: 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
2340: 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d 63 b, zSql);. memc
2350: 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 py(&pParse->nVar
2360: 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45 5f , saveBuf, SAVE_
2370: 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e SZ);. pParse->n
2380: 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 23 69 66 20 ested--;.}..#if
2390: 53 51 4c 49 54 45 5f 55 53 45 52 5f 41 55 54 48 SQLITE_USER_AUTH
23a0: 45 4e 54 49 43 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a ENTICATION./*.**
23b0: 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
23c0: 7a 54 61 62 6c 65 20 69 73 20 74 68 65 20 6e 61 zTable is the na
23d0: 6d 65 20 6f 66 20 74 68 65 20 73 79 73 74 65 6d me of the system
23e0: 20 74 61 62 6c 65 20 74 68 61 74 20 73 74 6f 72 table that stor
23f0: 65 73 20 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f es the.** list o
2400: 66 20 75 73 65 72 73 20 61 6e 64 20 74 68 65 69 f users and thei
2410: 72 20 61 63 63 65 73 73 20 63 72 65 64 65 6e 74 r access credent
2420: 69 61 6c 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ials..*/.int sql
2430: 69 74 65 33 55 73 65 72 41 75 74 68 54 61 62 6c ite3UserAuthTabl
2440: 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 e(const char *zT
2450: 61 62 6c 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 able){. return
2460: 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 sqlite3_stricmp(
2470: 7a 54 61 62 6c 65 2c 20 22 73 71 6c 69 74 65 5f zTable, "sqlite_
2480: 75 73 65 72 22 29 3d 3d 30 3b 0a 7d 0a 23 65 6e user")==0;.}.#en
2490: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 dif../*.** Locat
24a0: 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 e the in-memory
24b0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 structure that d
24c0: 65 73 63 72 69 62 65 73 20 61 20 70 61 72 74 69 escribes a parti
24d0: 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a cular database.*
24e0: 2a 20 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68 * table given th
24f0: 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74 e name of that t
2500: 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e able and (option
2510: 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f ally) the name o
2520: 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 f the.** databas
2530: 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
2540: 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 table. Return
2550: 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e NULL if not foun
2560: 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 d..**.** If zDat
2570: 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c 6c 20 abase is 0, all
2580: 64 61 74 61 62 61 73 65 73 20 61 72 65 20 73 65 databases are se
2590: 61 72 63 68 65 64 20 66 6f 72 20 74 68 65 20 74 arched for the t
25a0: 61 62 6c 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 able and the.**
25b0: 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 74 first matching t
25c0: 61 62 6c 65 20 69 73 20 72 65 74 75 72 6e 65 64 able is returned
25d0: 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e 67 20 . (No checking
25e0: 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20 74 61 for duplicate ta
25f0: 62 6c 65 0a 2a 2a 20 6e 61 6d 65 73 20 69 73 20 ble.** names is
2600: 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65 61 72 done.) The sear
2610: 63 68 20 6f 72 64 65 72 20 69 73 20 54 45 4d 50 ch order is TEMP
2620: 20 66 69 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 first, then MAI
2630: 4e 2c 20 74 68 65 6e 20 61 6e 79 0a 2a 2a 20 61 N, then any.** a
2640: 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
2650: 65 73 20 61 64 64 65 64 20 75 73 69 6e 67 20 74 es added using t
2660: 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e he ATTACH comman
2670: 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 d..**.** See als
2680: 6f 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 o sqlite3LocateT
2690: 61 62 6c 65 28 29 2e 0a 2a 2f 0a 54 61 62 6c 65 able()..*/.Table
26a0: 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 *sqlite3FindTab
26b0: 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 le(sqlite3 *db,
26c0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
26d0: 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
26e0: 44 61 74 61 62 61 73 65 29 7b 0a 20 20 54 61 62 Database){. Tab
26f0: 6c 65 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 le *p = 0;. int
2700: 20 69 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 20 6d 75 i;.. /* All mu
2710: 74 65 78 65 73 20 61 72 65 20 72 65 71 75 69 72 texes are requir
2720: 65 64 20 66 6f 72 20 73 63 68 65 6d 61 20 61 63 ed for schema ac
2730: 63 65 73 73 2e 20 20 4d 61 6b 65 20 73 75 72 65 cess. Make sure
2740: 20 77 65 20 68 6f 6c 64 20 74 68 65 6d 2e 20 2a we hold them. *
2750: 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 44 61 74 /. assert( zDat
2760: 61 62 61 73 65 21 3d 30 20 7c 7c 20 73 71 6c 69 abase!=0 || sqli
2770: 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c te3BtreeHoldsAll
2780: 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 23 Mutexes(db) );.#
2790: 69 66 20 53 51 4c 49 54 45 5f 55 53 45 52 5f 41 if SQLITE_USER_A
27a0: 55 54 48 45 4e 54 49 43 41 54 49 4f 4e 0a 20 20 UTHENTICATION.
27b0: 2f 2a 20 4f 6e 6c 79 20 74 68 65 20 61 64 6d 69 /* Only the admi
27c0: 6e 20 75 73 65 72 20 69 73 20 61 6c 6c 6f 77 65 n user is allowe
27d0: 64 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 74 d to know that t
27e0: 68 65 20 73 71 6c 69 74 65 5f 75 73 65 72 20 74 he sqlite_user t
27f0: 61 62 6c 65 0a 20 20 2a 2a 20 65 78 69 73 74 73 able. ** exists
2800: 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 */. if( db->au
2810: 74 68 2e 61 75 74 68 4c 65 76 65 6c 3c 55 41 55 th.authLevel<UAU
2820: 54 48 5f 41 64 6d 69 6e 20 26 26 20 73 71 6c 69 TH_Admin && sqli
2830: 74 65 33 55 73 65 72 41 75 74 68 54 61 62 6c 65 te3UserAuthTable
2840: 28 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 (zName)!=0 ){.
2850: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
2860: 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 4f #endif. for(i=O
2870: 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 MIT_TEMPDB; i<db
2880: 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
2890: 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f int j = (i<2) ?
28a0: 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53 i^1 : i; /* S
28b0: 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 earch TEMP befor
28c0: 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 e MAIN */. if
28d0: 28 20 7a 44 61 74 61 62 61 73 65 21 3d 30 20 26 ( zDatabase!=0 &
28e0: 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 & sqlite3StrICmp
28f0: 28 7a 44 61 74 61 62 61 73 65 2c 20 64 62 2d 3e (zDatabase, db->
2900: 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 aDb[j].zName) )
2910: 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 continue;. as
2920: 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 sert( sqlite3Sch
2930: 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c emaMutexHeld(db,
2940: 20 6a 2c 20 30 29 20 29 3b 0a 20 20 20 20 70 20 j, 0) );. p
2950: 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e = sqlite3HashFin
2960: 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 d(&db->aDb[j].pS
2970: 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 chema->tblHash,
2980: 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 zName);. if(
2990: 70 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 p ) break;. }.
29a0: 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
29b0: 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 .** Locate the i
29c0: 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 n-memory structu
29d0: 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 re that describe
29e0: 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 s a particular d
29f0: 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 atabase.** table
2a00: 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 given the name
2a10: 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e of that table an
2a20: 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 d (optionally) t
2a30: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a he name of the.*
2a40: 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 * database conta
2a50: 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e ining the table.
2a60: 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 Return NULL if
2a70: 20 6e 6f 74 20 66 6f 75 6e 64 2e 20 20 41 6c 73 not found. Als
2a80: 6f 20 6c 65 61 76 65 20 61 6e 0a 2a 2a 20 65 72 o leave an.** er
2a90: 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 ror message in p
2aa0: 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a Parse->zErrMsg..
2ab0: 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 **.** The differ
2ac0: 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 ence between thi
2ad0: 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 73 71 s routine and sq
2ae0: 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29 lite3FindTable()
2af0: 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a is that this.**
2b00: 20 72 6f 75 74 69 6e 65 20 6c 65 61 76 65 73 20 routine leaves
2b10: 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
2b20: 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 in pParse->zErr
2b30: 4d 73 67 20 77 68 65 72 65 0a 2a 2a 20 73 71 6c Msg where.** sql
2b40: 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29 20 ite3FindTable()
2b50: 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 54 61 62 does not..*/.Tab
2b60: 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 le *sqlite3Locat
2b70: 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 eTable(. Parse
2b80: 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
2b90: 20 2f 2a 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 /* context in w
2ba0: 68 69 63 68 20 74 6f 20 72 65 70 6f 72 74 20 65 hich to report e
2bb0: 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 rrors */. int i
2bc0: 73 56 69 65 77 2c 20 20 20 20 20 20 20 20 20 20 sView,
2bd0: 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 6f 6f /* True if loo
2be0: 6b 69 6e 67 20 66 6f 72 20 61 20 56 49 45 57 20 king for a VIEW
2bf0: 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 54 41 rather than a TA
2c00: 42 4c 45 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 BLE */. const c
2c10: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 har *zName,
2c20: 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 /* Name of the t
2c30: 61 62 6c 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b able we are look
2c40: 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e ing for */. con
2c50: 73 74 20 63 68 61 72 20 2a 7a 44 62 61 73 65 20 st char *zDbase
2c60: 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
2c70: 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 69 he database. Mi
2c80: 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 ght be NULL */.)
2c90: 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a 20 {. Table *p;..
2ca0: 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 /* Read the dat
2cb0: 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 abase schema. If
2cc0: 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
2cd0: 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 , leave an error
2ce0: 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e message. ** an
2cf0: 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 d code in pParse
2d00: 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c and return NULL
2d10: 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 . */. if( SQLIT
2d20: 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 E_OK!=sqlite3Rea
2d30: 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 dSchema(pParse)
2d40: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
2d50: 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69 . }.. p = sqli
2d60: 74 65 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61 te3FindTable(pPa
2d70: 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20 rse->db, zName,
2d80: 7a 44 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 zDbase);. if( p
2d90: 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 ==0 ){. const
2da0: 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 69 73 char *zMsg = is
2db0: 56 69 65 77 20 3f 20 22 6e 6f 20 73 75 63 68 20 View ? "no such
2dc0: 76 69 65 77 22 20 3a 20 22 6e 6f 20 73 75 63 68 view" : "no such
2dd0: 20 74 61 62 6c 65 22 3b 0a 23 69 66 6e 64 65 66 table";.#ifndef
2de0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
2df0: 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 TUALTABLE. if
2e00: 28 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e ( sqlite3FindDbN
2e10: 61 6d 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ame(pParse->db,
2e20: 7a 44 62 61 73 65 29 3c 31 20 29 7b 0a 20 20 20 zDbase)<1 ){.
2e30: 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d 65 20 69 /* If zName i
2e40: 73 20 74 68 65 20 6e 6f 74 20 74 68 65 20 6e 61 s the not the na
2e50: 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20 69 6e me of a table in
2e60: 20 74 68 65 20 73 63 68 65 6d 61 20 63 72 65 61 the schema crea
2e70: 74 65 64 20 75 73 69 6e 67 0a 20 20 20 20 20 20 ted using.
2e80: 2a 2a 20 43 52 45 41 54 45 2c 20 74 68 65 6e 20 ** CREATE, then
2e90: 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 check to see if
2ea0: 69 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f it is the name o
2eb0: 66 20 61 6e 20 76 69 72 74 75 61 6c 20 74 61 62 f an virtual tab
2ec0: 6c 65 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a le that. **
2ed0: 20 63 61 6e 20 62 65 20 61 6e 20 65 70 6f 6e 79 can be an epony
2ee0: 6d 6f 75 73 20 76 69 72 74 75 61 6c 20 74 61 62 mous virtual tab
2ef0: 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 4d 6f 64 le. */. Mod
2f00: 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d 6f 64 ule *pMod = (Mod
2f10: 75 6c 65 2a 29 73 71 6c 69 74 65 33 48 61 73 68 ule*)sqlite3Hash
2f20: 46 69 6e 64 28 26 70 50 61 72 73 65 2d 3e 64 62 Find(&pParse->db
2f30: 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4e 61 6d 65 ->aModule, zName
2f40: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 6f );. if( pMo
2f50: 64 20 26 26 20 73 71 6c 69 74 65 33 56 74 61 62 d && sqlite3Vtab
2f60: 45 70 6f 6e 79 6d 6f 75 73 54 61 62 6c 65 49 6e EponymousTableIn
2f70: 69 74 28 70 50 61 72 73 65 2c 20 70 4d 6f 64 29 it(pParse, pMod)
2f80: 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
2f90: 72 6e 20 70 4d 6f 64 2d 3e 70 45 70 6f 54 61 62 rn pMod->pEpoTab
2fa0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
2fb0: 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 7a #endif. if( z
2fc0: 44 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 73 Dbase ){. s
2fd0: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
2fe0: 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 Parse, "%s: %s.%
2ff0: 73 22 2c 20 7a 4d 73 67 2c 20 7a 44 62 61 73 65 s", zMsg, zDbase
3000: 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 , zName);. }e
3010: 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
3020: 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
3030: 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 4d 73 e, "%s: %s", zMs
3040: 67 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d g, zName);. }
3050: 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65 . pParse->che
3060: 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20 ckSchema = 1;.
3070: 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 55 53 45 }.#if SQLITE_USE
3080: 52 5f 41 55 54 48 45 4e 54 49 43 41 54 49 4f 4e R_AUTHENTICATION
3090: 0a 20 20 65 6c 73 65 20 69 66 28 20 70 50 61 72 . else if( pPar
30a0: 73 65 2d 3e 64 62 2d 3e 61 75 74 68 2e 61 75 74 se->db->auth.aut
30b0: 68 4c 65 76 65 6c 3c 55 41 55 54 48 5f 55 73 65 hLevel<UAUTH_Use
30c0: 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
30d0: 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
30e0: 20 22 75 73 65 72 20 6e 6f 74 20 61 75 74 68 65 "user not authe
30f0: 6e 74 69 63 61 74 65 64 22 29 3b 0a 20 20 20 20 nticated");.
3100: 70 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 p = 0;. }.#endi
3110: 66 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a f. return p;.}.
3120: 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 ./*.** Locate th
3130: 65 20 74 61 62 6c 65 20 69 64 65 6e 74 69 66 69 e table identifi
3140: 65 64 20 62 79 20 2a 70 2e 0a 2a 2a 0a 2a 2a 20 ed by *p..**.**
3150: 54 68 69 73 20 69 73 20 61 20 77 72 61 70 70 65 This is a wrappe
3160: 72 20 61 72 6f 75 6e 64 20 73 71 6c 69 74 65 33 r around sqlite3
3170: 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 20 54 LocateTable(). T
3180: 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 he difference be
3190: 74 77 65 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 tween.** sqlite3
31a0: 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 20 61 6e LocateTable() an
31b0: 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 d this function
31c0: 69 73 20 74 68 61 74 20 74 68 69 73 20 66 75 6e is that this fun
31d0: 63 74 69 6f 6e 20 72 65 73 74 72 69 63 74 73 0a ction restricts.
31e0: 2a 2a 20 74 68 65 20 73 65 61 72 63 68 20 74 6f ** the search to
31f0: 20 73 63 68 65 6d 61 20 28 70 2d 3e 70 53 63 68 schema (p->pSch
3200: 65 6d 61 29 20 69 66 20 69 74 20 69 73 20 6e 6f ema) if it is no
3210: 74 20 4e 55 4c 4c 2e 20 70 2d 3e 70 53 63 68 65 t NULL. p->pSche
3220: 6d 61 20 6d 61 79 20 62 65 0a 2a 2a 20 6e 6f 6e ma may be.** non
3230: 2d 4e 55 4c 4c 20 69 66 20 69 74 20 69 73 20 70 -NULL if it is p
3240: 61 72 74 20 6f 66 20 61 20 76 69 65 77 20 6f 72 art of a view or
3250: 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d trigger program
3260: 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 53 65 65 definition. See
3270: 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69 78 53 72 .** sqlite3FixSr
3280: 63 4c 69 73 74 28 29 20 66 6f 72 20 64 65 74 61 cList() for deta
3290: 69 6c 73 2e 0a 2a 2f 0a 54 61 62 6c 65 20 2a 73 ils..*/.Table *s
32a0: 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c qlite3LocateTabl
32b0: 65 49 74 65 6d 28 0a 20 20 50 61 72 73 65 20 2a eItem(. Parse *
32c0: 70 50 61 72 73 65 2c 20 0a 20 20 69 6e 74 20 69 pParse, . int i
32d0: 73 56 69 65 77 2c 20 0a 20 20 73 74 72 75 63 74 sView, . struct
32e0: 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
32f0: 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 .){. const char
3300: 20 2a 7a 44 62 3b 0a 20 20 61 73 73 65 72 74 28 *zDb;. assert(
3310: 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 30 20 7c p->pSchema==0 |
3320: 7c 20 70 2d 3e 7a 44 61 74 61 62 61 73 65 3d 3d | p->zDatabase==
3330: 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53 0 );. if( p->pS
3340: 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 69 6e 74 chema ){. int
3350: 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
3360: 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 hemaToIndex(pPar
3370: 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 53 63 68 65 se->db, p->pSche
3380: 6d 61 29 3b 0a 20 20 20 20 7a 44 62 20 3d 20 70 ma);. zDb = p
3390: 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 Parse->db->aDb[i
33a0: 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c Db].zName;. }el
33b0: 73 65 7b 0a 20 20 20 20 7a 44 62 20 3d 20 70 2d se{. zDb = p-
33c0: 3e 7a 44 61 74 61 62 61 73 65 3b 0a 20 20 7d 0a >zDatabase;. }.
33d0: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
33e0: 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 LocateTable(pPar
33f0: 73 65 2c 20 69 73 56 69 65 77 2c 20 70 2d 3e 7a se, isView, p->z
3400: 4e 61 6d 65 2c 20 7a 44 62 29 3b 0a 7d 0a 0a 2f Name, zDb);.}../
3410: 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 *.** Locate the
3420: 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 in-memory struct
3430: 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 ure that describ
3440: 65 73 20 0a 2a 2a 20 61 20 70 61 72 74 69 63 75 es .** a particu
3450: 6c 61 72 20 69 6e 64 65 78 20 67 69 76 65 6e 20 lar index given
3460: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 the name of that
3470: 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 index.** and th
3480: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 e name of the da
3490: 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74 tabase that cont
34a0: 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 2e 0a ains the index..
34b0: 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 ** Return NULL i
34c0: 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a f not found..**.
34d0: 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 ** If zDatabase
34e0: 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 is 0, all databa
34f0: 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64 ses are searched
3500: 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c for the.** tabl
3510: 65 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 e and the first
3520: 6d 61 74 63 68 69 6e 67 20 69 6e 64 65 78 20 69 matching index i
3530: 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f s returned. (No
3540: 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 checking.** for
3550: 20 64 75 70 6c 69 63 61 74 65 20 69 6e 64 65 78 duplicate index
3560: 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 names is done.)
3570: 20 20 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 The search ord
3580: 65 72 20 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69 er is.** TEMP fi
3590: 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 rst, then MAIN,
35a0: 74 68 65 6e 20 61 6e 79 20 61 75 78 69 6c 69 61 then any auxilia
35b0: 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64 64 ry databases add
35c0: 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 ed.** using the
35d0: 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a ATTACH command..
35e0: 2a 2f 0a 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 */.Index *sqlite
35f0: 33 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 3FindIndex(sqlit
3600: 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
3610: 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 ar *zName, const
3620: 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 49 char *zDb){. I
3630: 6e 64 65 78 20 2a 70 20 3d 20 30 3b 0a 20 20 69 ndex *p = 0;. i
3640: 6e 74 20 69 3b 0a 20 20 2f 2a 20 41 6c 6c 20 6d nt i;. /* All m
3650: 75 74 65 78 65 73 20 61 72 65 20 72 65 71 75 69 utexes are requi
3660: 72 65 64 20 66 6f 72 20 73 63 68 65 6d 61 20 61 red for schema a
3670: 63 63 65 73 73 2e 20 20 4d 61 6b 65 20 73 75 72 ccess. Make sur
3680: 65 20 77 65 20 68 6f 6c 64 20 74 68 65 6d 2e 20 e we hold them.
3690: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 44 62 */. assert( zDb
36a0: 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 !=0 || sqlite3Bt
36b0: 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 reeHoldsAllMutex
36c0: 65 73 28 64 62 29 20 29 3b 0a 20 20 66 6f 72 28 es(db) );. for(
36d0: 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 i=OMIT_TEMPDB; i
36e0: 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
36f0: 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 int j = (i<2
3700: 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a ) ? i^1 : i; /*
3710: 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 Search TEMP bef
3720: 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 ore MAIN */.
3730: 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 Schema *pSchema
3740: 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 = db->aDb[j].pSc
3750: 68 65 6d 61 3b 0a 20 20 20 20 61 73 73 65 72 74 hema;. assert
3760: 28 20 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 ( pSchema );.
3770: 20 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69 if( zDb && sqli
3780: 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62 2c 20 te3StrICmp(zDb,
3790: 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 db->aDb[j].zName
37a0: 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 ) ) continue;.
37b0: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
37c0: 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 3SchemaMutexHeld
37d0: 28 64 62 2c 20 6a 2c 20 30 29 20 29 3b 0a 20 20 (db, j, 0) );.
37e0: 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 p = sqlite3Has
37f0: 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e hFind(&pSchema->
3800: 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 29 3b idxHash, zName);
3810: 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72 65 . if( p ) bre
3820: 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ak;. }. return
3830: 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 p;.}../*.** Rec
3840: 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20 laim the memory
3850: 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78 used by an index
3860: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
3870: 66 72 65 65 49 6e 64 65 78 28 73 71 6c 69 74 65 freeIndex(sqlite
3880: 33 20 2a 64 62 2c 20 49 6e 64 65 78 20 2a 70 29 3 *db, Index *p)
3890: 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
38a0: 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 20 20 _OMIT_ANALYZE.
38b0: 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 sqlite3DeleteInd
38c0: 65 78 53 61 6d 70 6c 65 73 28 64 62 2c 20 70 29 exSamples(db, p)
38d0: 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 ;.#endif. sqlit
38e0: 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
38f0: 20 70 2d 3e 70 50 61 72 74 49 64 78 57 68 65 72 p->pPartIdxWher
3900: 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 e);. sqlite3Exp
3910: 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 rListDelete(db,
3920: 70 2d 3e 61 43 6f 6c 45 78 70 72 29 3b 0a 20 20 p->aColExpr);.
3930: 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
3940: 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 , p->zColAff);.
3950: 20 69 66 28 20 70 2d 3e 69 73 52 65 73 69 7a 65 if( p->isResize
3960: 64 20 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 d ) sqlite3DbFre
3970: 65 28 64 62 2c 20 70 2d 3e 61 7a 43 6f 6c 6c 29 e(db, p->azColl)
3980: 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
3990: 45 4e 41 42 4c 45 5f 53 54 41 54 33 5f 4f 52 5f ENABLE_STAT3_OR_
39a0: 53 54 41 54 34 0a 20 20 73 71 6c 69 74 65 33 5f STAT4. sqlite3_
39b0: 66 72 65 65 28 70 2d 3e 61 69 52 6f 77 45 73 74 free(p->aiRowEst
39c0: 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 );.#endif. sqli
39d0: 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 te3DbFree(db, p)
39e0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 ;.}../*.** For t
39f0: 68 65 20 69 6e 64 65 78 20 63 61 6c 6c 65 64 20 he index called
3a00: 7a 49 64 78 4e 61 6d 65 20 77 68 69 63 68 20 69 zIdxName which i
3a10: 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 s found in the d
3a20: 61 74 61 62 61 73 65 20 69 44 62 2c 0a 2a 2a 20 atabase iDb,.**
3a30: 75 6e 6c 69 6b 65 20 74 68 61 74 20 69 6e 64 65 unlike that inde
3a40: 78 20 66 72 6f 6d 20 69 74 73 20 54 61 62 6c 65 x from its Table
3a50: 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 then remove the
3a60: 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 index from.** t
3a70: 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 he index hash ta
3a80: 62 6c 65 20 61 6e 64 20 66 72 65 65 20 61 6c 6c ble and free all
3a90: 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 memory structur
3aa0: 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a es associated.**
3ab0: 20 77 69 74 68 20 74 68 65 20 69 6e 64 65 78 2e with the index.
3ac0: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
3ad0: 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 UnlinkAndDeleteI
3ae0: 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 ndex(sqlite3 *db
3af0: 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 , int iDb, const
3b00: 20 63 68 61 72 20 2a 7a 49 64 78 4e 61 6d 65 29 char *zIdxName)
3b10: 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 {. Index *pInde
3b20: 78 3b 0a 20 20 48 61 73 68 20 2a 70 48 61 73 68 x;. Hash *pHash
3b30: 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
3b40: 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 ite3SchemaMutexH
3b50: 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 eld(db, iDb, 0)
3b60: 29 3b 0a 20 20 70 48 61 73 68 20 3d 20 26 64 62 );. pHash = &db
3b70: 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 ->aDb[iDb].pSche
3b80: 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20 70 ma->idxHash;. p
3b90: 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 48 Index = sqlite3H
3ba0: 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c ashInsert(pHash,
3bb0: 20 7a 49 64 78 4e 61 6d 65 2c 20 30 29 3b 0a 20 zIdxName, 0);.
3bc0: 20 69 66 28 20 41 4c 57 41 59 53 28 70 49 6e 64 if( ALWAYS(pInd
3bd0: 65 78 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ex) ){. if( p
3be0: 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 Index->pTable->p
3bf0: 49 6e 64 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b Index==pIndex ){
3c00: 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 . pIndex->p
3c10: 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 Table->pIndex =
3c20: 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 pIndex->pNext;.
3c30: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
3c40: 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20 20 20 Index *p;.
3c50: 2f 2a 20 4a 75 73 74 69 66 69 63 61 74 69 6f 6e /* Justification
3c60: 20 6f 66 20 41 4c 57 41 59 53 28 29 3b 20 20 54 of ALWAYS(); T
3c70: 68 65 20 69 6e 64 65 78 20 6d 75 73 74 20 62 65 he index must be
3c80: 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 0a on the list of.
3c90: 20 20 20 20 20 20 2a 2a 20 69 6e 64 69 63 65 73 ** indices
3ca0: 2e 20 2a 2f 0a 20 20 20 20 20 20 70 20 3d 20 70 . */. p = p
3cb0: 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 Index->pTable->p
3cc0: 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 77 68 69 Index;. whi
3cd0: 6c 65 28 20 41 4c 57 41 59 53 28 70 29 20 26 26 le( ALWAYS(p) &&
3ce0: 20 70 2d 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 p->pNext!=pInde
3cf0: 78 20 29 7b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 x ){ p = p->pNex
3d00: 74 3b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 t; }. if( A
3d10: 4c 57 41 59 53 28 70 20 26 26 20 70 2d 3e 70 4e LWAYS(p && p->pN
3d20: 65 78 74 3d 3d 70 49 6e 64 65 78 29 20 29 7b 0a ext==pIndex) ){.
3d30: 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 p->pNext
3d40: 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 = pIndex->pNext
3d50: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
3d60: 20 20 20 20 66 72 65 65 49 6e 64 65 78 28 64 62 freeIndex(db
3d70: 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 , pIndex);. }.
3d80: 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 db->flags |= SQ
3d90: 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 LITE_InternChang
3da0: 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f es;.}../*.** Loo
3db0: 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 69 k through the li
3dc0: 73 74 20 6f 66 20 6f 70 65 6e 20 64 61 74 61 62 st of open datab
3dd0: 61 73 65 20 66 69 6c 65 73 20 69 6e 20 64 62 2d ase files in db-
3de0: 3e 61 44 62 5b 5d 20 61 6e 64 20 69 66 0a 2a 2a >aDb[] and if.**
3df0: 20 61 6e 79 20 68 61 76 65 20 62 65 65 6e 20 63 any have been c
3e00: 6c 6f 73 65 64 2c 20 72 65 6d 6f 76 65 20 74 68 losed, remove th
3e10: 65 6d 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 em from the list
3e20: 2e 20 20 52 65 61 6c 6c 6f 63 61 74 65 20 74 68 . Reallocate th
3e30: 65 0a 2a 2a 20 64 62 2d 3e 61 44 62 5b 5d 20 73 e.** db->aDb[] s
3e40: 74 72 75 63 74 75 72 65 20 74 6f 20 61 20 73 6d tructure to a sm
3e50: 61 6c 6c 65 72 20 73 69 7a 65 2c 20 69 66 20 70 aller size, if p
3e60: 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 45 ossible..**.** E
3e70: 6e 74 72 79 20 30 20 28 74 68 65 20 22 6d 61 69 ntry 0 (the "mai
3e80: 6e 22 20 64 61 74 61 62 61 73 65 29 20 61 6e 64 n" database) and
3e90: 20 65 6e 74 72 79 20 31 20 28 74 68 65 20 22 74 entry 1 (the "t
3ea0: 65 6d 70 22 20 64 61 74 61 62 61 73 65 29 0a 2a emp" database).*
3eb0: 2a 20 61 72 65 20 6e 65 76 65 72 20 63 61 6e 64 * are never cand
3ec0: 69 64 61 74 65 73 20 66 6f 72 20 62 65 69 6e 67 idates for being
3ed0: 20 63 6f 6c 6c 61 70 73 65 64 2e 0a 2a 2f 0a 76 collapsed..*/.v
3ee0: 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 6c 61 oid sqlite3Colla
3ef0: 70 73 65 44 61 74 61 62 61 73 65 41 72 72 61 79 pseDatabaseArray
3f00: 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
3f10: 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 int i, j;. for
3f20: 28 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e 6e 44 (i=j=2; i<db->nD
3f30: 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 b; i++){. str
3f40: 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 uct Db *pDb = &d
3f50: 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 b->aDb[i];. i
3f60: 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 f( pDb->pBt==0 )
3f70: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
3f80: 62 46 72 65 65 28 64 62 2c 20 70 44 62 2d 3e 7a bFree(db, pDb->z
3f90: 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 44 62 Name);. pDb
3fa0: 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 ->zName = 0;.
3fb0: 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 continue;.
3fc0: 20 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69 20 29 }. if( j<i )
3fd0: 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b {. db->aDb[
3fe0: 6a 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 3b j] = db->aDb[i];
3ff0: 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a . }. j++;.
4000: 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 64 62 }. memset(&db
4010: 2d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28 64 62 ->aDb[j], 0, (db
4020: 2d 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f 66 28 ->nDb-j)*sizeof(
4030: 64 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a 20 20 db->aDb[j]));.
4040: 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 69 db->nDb = j;. i
4050: 66 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 26 f( db->nDb<=2 &&
4060: 20 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 44 db->aDb!=db->aD
4070: 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 6d bStatic ){. m
4080: 65 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 61 emcpy(db->aDbSta
4090: 74 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 2a tic, db->aDb, 2*
40a0: 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 sizeof(db->aDb[0
40b0: 5d 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ]));. sqlite3
40c0: 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 DbFree(db, db->a
40d0: 44 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 Db);. db->aDb
40e0: 20 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 = db->aDbStatic
40f0: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
4100: 65 73 65 74 20 74 68 65 20 73 63 68 65 6d 61 20 eset the schema
4110: 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 for the database
4120: 20 61 74 20 69 6e 64 65 78 20 69 44 62 2e 20 20 at index iDb.
4130: 41 6c 73 6f 20 72 65 73 65 74 20 74 68 65 0a 2a Also reset the.*
4140: 2a 20 54 45 4d 50 20 73 63 68 65 6d 61 2e 0a 2a * TEMP schema..*
4150: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 /.void sqlite3Re
4160: 73 65 74 4f 6e 65 53 63 68 65 6d 61 28 73 71 6c setOneSchema(sql
4170: 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 ite3 *db, int iD
4180: 62 29 7b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 b){. Db *pDb;.
4190: 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d assert( iDb<db-
41a0: 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 >nDb );.. /* Ca
41b0: 73 65 20 31 3a 20 20 52 65 73 65 74 20 74 68 65 se 1: Reset the
41c0: 20 73 69 6e 67 6c 65 20 73 63 68 65 6d 61 20 69 single schema i
41d0: 64 65 6e 74 69 66 69 65 64 20 62 79 20 69 44 62 dentified by iDb
41e0: 20 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62 2d */. pDb = &db-
41f0: 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 61 73 73 >aDb[iDb];. ass
4200: 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 ert( sqlite3Sche
4210: 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 maMutexHeld(db,
4220: 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 61 73 73 iDb, 0) );. ass
4230: 65 72 74 28 20 70 44 62 2d 3e 70 53 63 68 65 6d ert( pDb->pSchem
4240: 61 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 a!=0 );. sqlite
4250: 33 53 63 68 65 6d 61 43 6c 65 61 72 28 70 44 62 3SchemaClear(pDb
4260: 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f ->pSchema);.. /
4270: 2a 20 49 66 20 61 6e 79 20 64 61 74 61 62 61 73 * If any databas
4280: 65 20 6f 74 68 65 72 20 74 68 61 6e 20 54 45 4d e other than TEM
4290: 50 20 69 73 20 72 65 73 65 74 2c 20 74 68 65 6e P is reset, then
42a0: 20 61 6c 73 6f 20 72 65 73 65 74 20 54 45 4d 50 also reset TEMP
42b0: 0a 20 20 2a 2a 20 73 69 6e 63 65 20 54 45 4d 50 . ** since TEMP
42c0: 20 6d 69 67 68 74 20 62 65 20 68 6f 6c 64 69 6e might be holdin
42d0: 67 20 74 72 69 67 67 65 72 73 20 74 68 61 74 20 g triggers that
42e0: 72 65 66 65 72 65 6e 63 65 20 74 61 62 6c 65 73 reference tables
42f0: 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 in the. ** oth
4300: 65 72 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a er database.. *
4310: 2f 0a 20 20 69 66 28 20 69 44 62 21 3d 31 20 29 /. if( iDb!=1 )
4320: 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d {. pDb = &db-
4330: 3e 61 44 62 5b 31 5d 3b 0a 20 20 20 20 61 73 73 >aDb[1];. ass
4340: 65 72 74 28 20 70 44 62 2d 3e 70 53 63 68 65 6d ert( pDb->pSchem
4350: 61 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 a!=0 );. sqli
4360: 74 65 33 53 63 68 65 6d 61 43 6c 65 61 72 28 70 te3SchemaClear(p
4370: 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 Db->pSchema);.
4380: 7d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f }. return;.}../
4390: 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 *.** Erase all s
43a0: 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f chema informatio
43b0: 6e 20 66 72 6f 6d 20 61 6c 6c 20 61 74 74 61 63 n from all attac
43c0: 68 65 64 20 64 61 74 61 62 61 73 65 73 20 28 69 hed databases (i
43d0: 6e 63 6c 75 64 69 6e 67 0a 2a 2a 20 22 6d 61 69 ncluding.** "mai
43e0: 6e 22 20 61 6e 64 20 22 74 65 6d 70 22 29 20 66 n" and "temp") f
43f0: 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 or a single data
4400: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
4410: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
4420: 52 65 73 65 74 41 6c 6c 53 63 68 65 6d 61 73 4f ResetAllSchemasO
4430: 66 43 6f 6e 6e 65 63 74 69 6f 6e 28 73 71 6c 69 fConnection(sqli
4440: 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 te3 *db){. int
4450: 69 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 i;. sqlite3Btre
4460: 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 eEnterAll(db);.
4470: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
4480: 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44 nDb; i++){. D
4490: 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 b *pDb = &db->aD
44a0: 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 b[i];. if( pD
44b0: 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 b->pSchema ){.
44c0: 20 20 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d sqlite3Schem
44d0: 61 43 6c 65 61 72 28 70 44 62 2d 3e 70 53 63 68 aClear(pDb->pSch
44e0: 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ema);. }. }.
44f0: 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e db->flags &= ~
4500: 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
4510: 6e 67 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 56 nges;. sqlite3V
4520: 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 tabUnlockList(db
4530: 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
4540: 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 eLeaveAll(db);.
4550: 20 73 71 6c 69 74 65 33 43 6f 6c 6c 61 70 73 65 sqlite3Collapse
4560: 44 61 74 61 62 61 73 65 41 72 72 61 79 28 64 62 DatabaseArray(db
4570: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 );.}../*.** This
4580: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
4590: 65 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74 ed when a commit
45a0: 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 76 6f 69 64 occurs..*/.void
45b0: 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e sqlite3CommitIn
45c0: 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 ternalChanges(sq
45d0: 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64 62 lite3 *db){. db
45e0: 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 ->flags &= ~SQLI
45f0: 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 TE_InternChanges
4600: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
4610: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
4620: 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d ed for the colum
4630: 6e 20 6e 61 6d 65 73 20 6f 66 20 61 20 74 61 62 n names of a tab
4640: 6c 65 20 6f 72 20 76 69 65 77 20 28 74 68 65 0a le or view (the.
4650: 2a 2a 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 ** Table.aCol[]
4660: 61 72 72 61 79 29 2e 0a 2a 2f 0a 76 6f 69 64 20 array)..*/.void
4670: 73 71 6c 69 74 65 33 44 65 6c 65 74 65 43 6f 6c sqlite3DeleteCol
4680: 75 6d 6e 4e 61 6d 65 73 28 73 71 6c 69 74 65 33 umnNames(sqlite3
4690: 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 *db, Table *pTa
46a0: 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ble){. int i;.
46b0: 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 Column *pCol;.
46c0: 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21 assert( pTable!
46d0: 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 6f =0 );. if( (pCo
46e0: 6c 20 3d 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c l = pTable->aCol
46f0: 29 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 )!=0 ){. for(
4700: 69 3d 30 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e i=0; i<pTable->n
4710: 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b Col; i++, pCol++
4720: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
4730: 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d DbFree(db, pCol-
4740: 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 >zName);. s
4750: 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
4760: 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 (db, pCol->pDflt
4770: 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
4780: 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d DbFree(db, pCol-
4790: 3e 7a 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 73 >zDflt);. s
47a0: 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
47b0: 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 pCol->zType);.
47c0: 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
47d0: 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 43 6f ee(db, pCol->zCo
47e0: 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ll);. }. s
47f0: 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
4800: 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a pTable->aCol);.
4810: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d }.}../*.** Rem
4820: 6f 76 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 ove the memory d
4830: 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 61 ata structures a
4840: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
4850: 68 65 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c he given.** Tabl
4860: 65 2e 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 e. No changes a
4870: 72 65 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 re made to disk
4880: 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e by this routine.
4890: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
48a0: 69 6e 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73 ine just deletes
48b0: 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 the data struct
48c0: 75 72 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f ure. It does no
48d0: 74 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 t unlink.** the
48e0: 74 61 62 6c 65 20 64 61 74 61 20 73 74 72 75 63 table data struc
48f0: 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61 ture from the ha
4900: 73 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69 sh table. But i
4910: 74 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a t does destroy.*
4920: 2a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 * memory structu
4930: 72 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63 res of the indic
4940: 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b es and foreign k
4950: 65 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 eys associated w
4960: 69 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c ith .** the tabl
4970: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 62 20 e..**.** The db
4980: 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f 70 74 parameter is opt
4990: 69 6f 6e 61 6c 2e 20 20 49 74 20 69 73 20 6e 65 ional. It is ne
49a0: 65 64 65 64 20 69 66 20 74 68 65 20 54 61 62 6c eded if the Tabl
49b0: 65 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 63 6f 6e e object .** con
49c0: 74 61 69 6e 73 20 6c 6f 6f 6b 61 73 69 64 65 20 tains lookaside
49d0: 6d 65 6d 6f 72 79 2e 20 20 28 54 61 62 6c 65 20 memory. (Table
49e0: 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73 objects in the s
49f0: 63 68 65 6d 61 20 64 6f 20 6e 6f 74 20 75 73 65 chema do not use
4a00: 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 .** lookaside me
4a10: 6d 6f 72 79 2c 20 62 75 74 20 73 6f 6d 65 20 65 mory, but some e
4a20: 70 68 65 6d 65 72 61 6c 20 54 61 62 6c 65 20 6f phemeral Table o
4a30: 62 6a 65 63 74 73 20 64 6f 2e 29 20 20 4f 72 20 bjects do.) Or
4a40: 74 68 65 0a 2a 2a 20 64 62 20 70 61 72 61 6d 65 the.** db parame
4a50: 74 65 72 20 63 61 6e 20 62 65 20 75 73 65 64 20 ter can be used
4a60: 77 69 74 68 20 64 62 2d 3e 70 6e 42 79 74 65 73 with db->pnBytes
4a70: 46 72 65 65 64 20 74 6f 20 6d 65 61 73 75 72 65 Freed to measure
4a80: 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 the memory.** u
4a90: 73 65 64 20 62 79 20 74 68 65 20 54 61 62 6c 65 sed by the Table
4aa0: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 76 6f 69 64 object..*/.void
4ab0: 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 sqlite3DeleteTa
4ac0: 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ble(sqlite3 *db,
4ad0: 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b Table *pTable){
4ae0: 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
4af0: 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 54 45 53 54 , *pNext;. TEST
4b00: 4f 4e 4c 59 28 20 69 6e 74 20 6e 4c 6f 6f 6b 61 ONLY( int nLooka
4b10: 73 69 64 65 3b 20 29 20 2f 2a 20 55 73 65 64 20 side; ) /* Used
4b20: 74 6f 20 76 65 72 69 66 79 20 6c 6f 6f 6b 61 73 to verify lookas
4b30: 69 64 65 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 ide not used for
4b40: 20 73 63 68 65 6d 61 20 2a 2f 0a 0a 20 20 61 73 schema */.. as
4b50: 73 65 72 74 28 20 21 70 54 61 62 6c 65 20 7c 7c sert( !pTable ||
4b60: 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e 30 20 pTable->nRef>0
4b70: 29 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 );.. /* Do not
4b80: 64 65 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65 delete the table
4b90: 20 75 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72 until the refer
4ba0: 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 ence count reach
4bb0: 65 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 es zero. */. if
4bc0: 28 20 21 70 54 61 62 6c 65 20 29 20 72 65 74 75 ( !pTable ) retu
4bd0: 72 6e 3b 0a 20 20 69 66 28 20 28 28 21 64 62 20 rn;. if( ((!db
4be0: 7c 7c 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 || db->pnBytesFr
4bf0: 65 65 64 3d 3d 30 29 20 26 26 20 28 2d 2d 70 54 eed==0) && (--pT
4c00: 61 62 6c 65 2d 3e 6e 52 65 66 29 3e 30 29 20 29 able->nRef)>0) )
4c10: 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 52 return;.. /* R
4c20: 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 ecord the number
4c30: 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 of outstanding
4c40: 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 lookaside alloca
4c50: 74 69 6f 6e 73 20 69 6e 20 73 63 68 65 6d 61 20 tions in schema
4c60: 54 61 62 6c 65 73 0a 20 20 2a 2a 20 70 72 69 6f Tables. ** prio
4c70: 72 20 74 6f 20 64 6f 69 6e 67 20 61 6e 79 20 66 r to doing any f
4c80: 72 65 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 73 ree() operations
4c90: 2e 20 20 53 69 6e 63 65 20 73 63 68 65 6d 61 20 . Since schema
4ca0: 54 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 75 73 Tables do not us
4cb0: 65 0a 20 20 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 e. ** lookaside
4cc0: 2c 20 74 68 69 73 20 6e 75 6d 62 65 72 20 73 68 , this number sh
4cd0: 6f 75 6c 64 20 6e 6f 74 20 63 68 61 6e 67 65 2e ould not change.
4ce0: 20 2a 2f 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 */. TESTONLY(
4cf0: 6e 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 28 64 62 nLookaside = (db
4d00: 20 26 26 20 28 70 54 61 62 6c 65 2d 3e 74 61 62 && (pTable->tab
4d10: 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d Flags & TF_Ephem
4d20: 65 72 61 6c 29 3d 3d 30 29 20 3f 0a 20 20 20 20 eral)==0) ?.
4d30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d40: 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 db->lookasi
4d50: 64 65 2e 6e 4f 75 74 20 3a 20 30 20 29 3b 0a 0a de.nOut : 0 );..
4d60: 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 /* Delete all
4d70: 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 indices associat
4d80: 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 ed with this tab
4d90: 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 49 6e le. */. for(pIn
4da0: 64 65 78 20 3d 20 70 54 61 62 6c 65 2d 3e 70 49 dex = pTable->pI
4db0: 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 ndex; pIndex; pI
4dc0: 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0a 20 20 20 ndex=pNext){.
4dd0: 20 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 2d pNext = pIndex-
4de0: 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61 73 73 65 >pNext;. asse
4df0: 72 74 28 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 rt( pIndex->pSch
4e00: 65 6d 61 3d 3d 70 54 61 62 6c 65 2d 3e 70 53 63 ema==pTable->pSc
4e10: 68 65 6d 61 20 29 3b 0a 20 20 20 20 69 66 28 20 hema );. if(
4e20: 21 64 62 20 7c 7c 20 64 62 2d 3e 70 6e 42 79 74 !db || db->pnByt
4e30: 65 73 46 72 65 65 64 3d 3d 30 20 29 7b 0a 20 20 esFreed==0 ){.
4e40: 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 char *zName
4e50: 3d 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 3b = pIndex->zName;
4e60: 20 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 . TESTONLY
4e70: 20 28 20 49 6e 64 65 78 20 2a 70 4f 6c 64 20 3d ( Index *pOld =
4e80: 20 29 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e ) sqlite3HashIn
4e90: 73 65 72 74 28 0a 20 20 20 20 20 20 20 20 20 26 sert(. &
4ea0: 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d pIndex->pSchema-
4eb0: 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c >idxHash, zName,
4ec0: 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 0. );.
4ed0: 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 assert( db==0
4ee0: 7c 7c 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 || sqlite3Schema
4ef0: 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c MutexHeld(db, 0,
4f00: 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 pIndex->pSchema
4f10: 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ) );. asser
4f20: 74 28 20 70 4f 6c 64 3d 3d 70 49 6e 64 65 78 20 t( pOld==pIndex
4f30: 7c 7c 20 70 4f 6c 64 3d 3d 30 20 29 3b 0a 20 20 || pOld==0 );.
4f40: 20 20 7d 0a 20 20 20 20 66 72 65 65 49 6e 64 65 }. freeInde
4f50: 78 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0a 20 x(db, pIndex);.
4f60: 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 }.. /* Delete
4f70: 61 6e 79 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 any foreign keys
4f80: 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 69 attached to thi
4f90: 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 73 71 s table. */. sq
4fa0: 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 64 62 lite3FkDelete(db
4fb0: 2c 20 70 54 61 62 6c 65 29 3b 0a 0a 20 20 2f 2a , pTable);.. /*
4fc0: 20 44 65 6c 65 74 65 20 74 68 65 20 54 61 62 6c Delete the Tabl
4fd0: 65 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 e structure itse
4fe0: 6c 66 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 lf.. */. sqlit
4ff0: 65 33 44 65 6c 65 74 65 43 6f 6c 75 6d 6e 4e 61 e3DeleteColumnNa
5000: 6d 65 73 28 64 62 2c 20 70 54 61 62 6c 65 29 3b mes(db, pTable);
5010: 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
5020: 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 (db, pTable->zNa
5030: 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 me);. sqlite3Db
5040: 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d Free(db, pTable-
5050: 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c >zColAff);. sql
5060: 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 ite3SelectDelete
5070: 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 65 (db, pTable->pSe
5080: 6c 65 63 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 lect);. sqlite3
5090: 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
50a0: 62 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 b, pTable->pChec
50b0: 6b 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 k);.#ifndef SQLI
50c0: 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
50d0: 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 56 74 ABLE. sqlite3Vt
50e0: 61 62 43 6c 65 61 72 28 64 62 2c 20 70 54 61 62 abClear(db, pTab
50f0: 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 le);.#endif. sq
5100: 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
5110: 70 54 61 62 6c 65 29 3b 0a 0a 20 20 2f 2a 20 56 pTable);.. /* V
5120: 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20 6c 6f erify that no lo
5130: 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 77 okaside memory w
5140: 61 73 20 75 73 65 64 20 62 79 20 73 63 68 65 6d as used by schem
5150: 61 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 61 73 a tables */. as
5160: 73 65 72 74 28 20 6e 4c 6f 6f 6b 61 73 69 64 65 sert( nLookaside
5170: 3d 3d 30 20 7c 7c 20 6e 4c 6f 6f 6b 61 73 69 64 ==0 || nLookasid
5180: 65 3d 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 e==db->lookaside
5190: 2e 6e 4f 75 74 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a .nOut );.}../*.*
51a0: 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 67 69 76 * Unlink the giv
51b0: 65 6e 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 en table from th
51c0: 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e e hash tables an
51d0: 64 20 74 68 65 20 64 65 6c 65 74 65 20 74 68 65 d the delete the
51e0: 0a 2a 2a 20 74 61 62 6c 65 20 73 74 72 75 63 74 .** table struct
51f0: 75 72 65 20 77 69 74 68 20 61 6c 6c 20 69 74 73 ure with all its
5200: 20 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 indices and for
5210: 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2f 0a 76 6f eign keys..*/.vo
5220: 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b id sqlite3Unlink
5230: 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 AndDeleteTable(s
5240: 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
5250: 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 iDb, const char
5260: 2a 7a 54 61 62 4e 61 6d 65 29 7b 0a 20 20 54 61 *zTabName){. Ta
5270: 62 6c 65 20 2a 70 3b 0a 20 20 44 62 20 2a 70 44 ble *p;. Db *pD
5280: 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 b;.. assert( db
5290: 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
52a0: 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 iDb>=0 && iDb<d
52b0: 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 b->nDb );. asse
52c0: 72 74 28 20 7a 54 61 62 4e 61 6d 65 20 29 3b 0a rt( zTabName );.
52d0: 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
52e0: 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 3SchemaMutexHeld
52f0: 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a (db, iDb, 0) );.
5300: 20 20 74 65 73 74 63 61 73 65 28 20 7a 54 61 62 testcase( zTab
5310: 4e 61 6d 65 5b 30 5d 3d 3d 30 20 29 3b 20 20 2f Name[0]==0 ); /
5320: 2a 20 5a 65 72 6f 2d 6c 65 6e 67 74 68 20 74 61 * Zero-length ta
5330: 62 6c 65 20 6e 61 6d 65 73 20 61 72 65 20 61 6c ble names are al
5340: 6c 6f 77 65 64 20 2a 2f 0a 20 20 70 44 62 20 3d lowed */. pDb =
5350: 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a &db->aDb[iDb];.
5360: 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 p = sqlite3Has
5370: 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 hInsert(&pDb->pS
5380: 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 chema->tblHash,
5390: 7a 54 61 62 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 zTabName, 0);.
53a0: 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 sqlite3DeleteTab
53b0: 6c 65 28 64 62 2c 20 70 29 3b 0a 20 20 64 62 2d le(db, p);. db-
53c0: 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 >flags |= SQLITE
53d0: 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a _InternChanges;.
53e0: 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 }../*.** Given a
53f0: 20 74 6f 6b 65 6e 2c 20 72 65 74 75 72 6e 20 61 token, return a
5400: 20 73 74 72 69 6e 67 20 74 68 61 74 20 63 6f 6e string that con
5410: 73 69 73 74 73 20 6f 66 20 74 68 65 20 74 65 78 sists of the tex
5420: 74 20 6f 66 20 74 68 61 74 0a 2a 2a 20 74 6f 6b t of that.** tok
5430: 65 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f en. Space to ho
5440: 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 ld the returned
5450: 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 6f 62 74 string.** is obt
5460: 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
5470: 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 eMalloc() and mu
5480: 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 st be freed by t
5490: 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 he calling.** fu
54a0: 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e nction..**.** An
54b0: 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61 72 6b y quotation mark
54c0: 73 20 28 65 78 3a 20 20 22 6e 61 6d 65 22 2c 20 s (ex: "name",
54d0: 27 6e 61 6d 65 27 2c 20 5b 6e 61 6d 65 5d 2c 20 'name', [name],
54e0: 6f 72 20 60 6e 61 6d 65 60 29 20 74 68 61 74 0a or `name`) that.
54f0: 2a 2a 20 73 75 72 72 6f 75 6e 64 20 74 68 65 20 ** surround the
5500: 62 6f 64 79 20 6f 66 20 74 68 65 20 74 6f 6b 65 body of the toke
5510: 6e 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a n are removed..*
5520: 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 *.** Tokens are
5530: 6f 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74 often just point
5540: 65 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 ers into the ori
5550: 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61 ginal SQL text a
5560: 6e 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 nd so.** are not
5570: 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 \000 terminated
5580: 20 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72 and are not per
5590: 73 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65 sistent. The re
55a0: 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a turned string.**
55b0: 20 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 is \000 termina
55c0: 74 65 64 20 61 6e 64 20 69 73 20 70 65 72 73 69 ted and is persi
55d0: 73 74 65 6e 74 2e 0a 2a 2f 0a 63 68 61 72 20 2a stent..*/.char *
55e0: 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
55f0: 6f 6b 65 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 oken(sqlite3 *db
5600: 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b , Token *pName){
5610: 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a . char *zName;.
5620: 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 if( pName ){.
5630: 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 zName = sqlit
5640: 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 e3DbStrNDup(db,
5650: 28 63 68 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c (char*)pName->z,
5660: 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 pName->n);.
5670: 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a sqlite3Dequote(z
5680: 4e 61 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Name);. }else{.
5690: 20 20 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 zName = 0;.
56a0: 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d }. return zNam
56b0: 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e e;.}../*.** Open
56c0: 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 the sqlite_mast
56d0: 65 72 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 er table stored
56e0: 69 6e 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 in database numb
56f0: 65 72 20 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 er iDb for.** wr
5700: 69 74 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 iting. The table
5710: 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 is opened using
5720: 20 63 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a 76 6f cursor 0..*/.vo
5730: 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 id sqlite3OpenMa
5740: 73 74 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 sterTable(Parse
5750: 2a 70 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 *p, int iDb){.
5760: 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 Vdbe *v = sqlite
5770: 33 47 65 74 56 64 62 65 28 70 29 3b 0a 20 20 73 3GetVdbe(p);. s
5780: 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 qlite3TableLock(
5790: 70 2c 20 69 44 62 2c 20 4d 41 53 54 45 52 5f 52 p, iDb, MASTER_R
57a0: 4f 4f 54 2c 20 31 2c 20 53 43 48 45 4d 41 5f 54 OOT, 1, SCHEMA_T
57b0: 41 42 4c 45 28 69 44 62 29 29 3b 0a 20 20 73 71 ABLE(iDb));. sq
57c0: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 lite3VdbeAddOp4I
57d0: 6e 74 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 nt(v, OP_OpenWri
57e0: 74 65 2c 20 30 2c 20 4d 41 53 54 45 52 5f 52 4f te, 0, MASTER_RO
57f0: 4f 54 2c 20 69 44 62 2c 20 35 29 3b 0a 20 20 69 OT, iDb, 5);. i
5800: 66 28 20 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b f( p->nTab==0 ){
5810: 0a 20 20 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31 . p->nTab = 1
5820: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ;. }.}../*.** P
5830: 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 arameter zName p
5840: 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74 oints to a nul-t
5850: 65 72 6d 69 6e 61 74 65 64 20 62 75 66 66 65 72 erminated buffer
5860: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
5870: 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74 name.** of a dat
5880: 61 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 abase ("main", "
5890: 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d temp" or the nam
58a0: 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 e of an attached
58b0: 20 64 62 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75 db). This.** fu
58c0: 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
58d0: 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
58e0: 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 69 named database i
58f0: 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a n db->aDb[], or.
5900: 2a 2a 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d ** -1 if the nam
5910: 65 64 20 64 62 20 63 61 6e 6e 6f 74 20 62 65 20 ed db cannot be
5920: 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 found..*/.int sq
5930: 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 lite3FindDbName(
5940: 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
5950: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b st char *zName){
5960: 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31 3b 20 20 . int i = -1;
5970: 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
5980: 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 se number */. i
5990: 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 f( zName ){.
59a0: 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 69 6e 74 Db *pDb;. int
59b0: 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
59c0: 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 en30(zName);.
59d0: 20 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d for(i=(db->nDb-
59e0: 31 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 1), pDb=&db->aDb
59f0: 5b 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 [i]; i>=0; i--,
5a00: 70 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 pDb--){. if
5a10: 28 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 ( (!OMIT_TEMPDB
5a20: 7c 7c 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d || i!=1 ) && n==
5a30: 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
5a40: 70 44 62 2d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a pDb->zName) && .
5a50: 20 20 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c 0==sql
5a60: 69 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d ite3StrICmp(pDb-
5a70: 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 >zName, zName) )
5a80: 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b {. break;
5a90: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
5aa0: 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d }. return i;.}
5ab0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 ../*.** The toke
5ac0: 6e 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 61 69 6e n *pName contain
5ad0: 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 s the name of a
5ae0: 64 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 database (either
5af0: 20 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 "main" or.** "t
5b00: 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 emp" or the name
5b10: 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 of an attached
5b20: 64 62 29 2e 20 54 68 69 73 20 72 6f 75 74 69 6e db). This routin
5b30: 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a e returns the.**
5b40: 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 index of the na
5b50: 6d 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 med database in
5b60: 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 db->aDb[], or -1
5b70: 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62 if the named db
5b80: 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 .** does not ex
5b90: 69 73 74 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ist..*/.int sqli
5ba0: 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 te3FindDb(sqlite
5bb0: 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 3 *db, Token *pN
5bc0: 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 ame){. int i;
5bd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5be0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5bf0: 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 Database number
5c00: 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 */. char *zName
5c10: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5c20: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
5c30: 65 20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 e we are searchi
5c40: 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 7a 4e 61 6d ng for */. zNam
5c50: 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 e = sqlite3NameF
5c60: 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 romToken(db, pNa
5c70: 6d 65 29 3b 0a 20 20 69 20 3d 20 73 71 6c 69 74 me);. i = sqlit
5c80: 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 64 62 2c e3FindDbName(db,
5c90: 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 zName);. sqlit
5ca0: 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 e3DbFree(db, zNa
5cb0: 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b me);. return i;
5cc0: 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c 65 .}../* The table
5cd0: 20 6f 72 20 76 69 65 77 20 6f 72 20 74 72 69 67 or view or trig
5ce0: 67 65 72 20 6e 61 6d 65 20 69 73 20 70 61 73 73 ger name is pass
5cf0: 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 ed to this routi
5d00: 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a 2a ne via tokens.**
5d10: 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d pName1 and pNam
5d20: 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 e2. If the table
5d30: 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 name was fully
5d40: 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 qualified, for e
5d50: 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 xample:.**.** CR
5d60: 45 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 EATE TABLE xxx.y
5d70: 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a yy (...);.** .**
5d80: 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 Then pName1 is
5d90: 73 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 set to "xxx" and
5da0: 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f pName2 "yyy". O
5db0: 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 n the other hand
5dc0: 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 if.** the table
5dd0: 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c name is not ful
5de0: 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e ly qualified, i.
5df0: 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 e.:.**.** CREATE
5e00: 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b TABLE yyy(...);
5e10: 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d .**.** Then pNam
5e20: 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79 79 e1 is set to "yy
5e30: 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 y" and pName2 is
5e40: 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ""..**.** This
5e50: 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 routine sets the
5e60: 20 2a 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e 74 *ppUnqual point
5e70: 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 er to point at t
5e80: 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 he token (pName1
5e90: 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20 74 or.** pName2) t
5ea0: 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 75 hat stores the u
5eb0: 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 nqualified table
5ec0: 20 6e 61 6d 65 2e 20 20 54 68 65 20 69 6e 64 65 name. The inde
5ed0: 78 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 x of the.** data
5ee0: 62 61 73 65 20 22 78 78 78 22 20 69 73 20 72 65 base "xxx" is re
5ef0: 74 75 72 6e 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 turned..*/.int s
5f00: 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d qlite3TwoPartNam
5f10: 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
5f20: 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 se, /* Pars
5f30: 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e ing and code gen
5f40: 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 erating context
5f50: 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d */. Token *pNam
5f60: 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 e1, /* The
5f70: 22 78 78 78 22 20 69 6e 20 74 68 65 20 6e 61 6d "xxx" in the nam
5f80: 65 20 22 78 78 78 2e 79 79 79 22 20 6f 72 20 22 e "xxx.yyy" or "
5f90: 78 78 78 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 xxx" */. Token
5fa0: 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a *pName2, /*
5fb0: 20 54 68 65 20 22 79 79 79 22 20 69 6e 20 74 68 The "yyy" in th
5fc0: 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 e name "xxx.yyy"
5fd0: 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55 */. Token **pU
5fe0: 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 nqual /* Wri
5ff0: 74 65 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 te the unqualifi
6000: 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68 ed object name h
6010: 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
6020: 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 iDb;
6030: 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
6040: 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 ase holding the
6050: 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 object */. sqli
6060: 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
6070: 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 41 4c 57 ->db;.. if( ALW
6080: 41 59 53 28 70 4e 61 6d 65 32 21 3d 30 29 20 26 AYS(pName2!=0) &
6090: 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b & pName2->n>0 ){
60a0: 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 . if( db->ini
60b0: 74 2e 62 75 73 79 20 29 20 7b 0a 20 20 20 20 20 t.busy ) {.
60c0: 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
60d0: 28 70 50 61 72 73 65 2c 20 22 63 6f 72 72 75 70 (pParse, "corrup
60e0: 74 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 t database");.
60f0: 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 return -1;.
6100: 20 20 20 7d 0a 20 20 20 20 2a 70 55 6e 71 75 61 }. *pUnqua
6110: 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 20 20 20 l = pName2;.
6120: 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e iDb = sqlite3Fin
6130: 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b dDb(db, pName1);
6140: 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29 . if( iDb<0 )
6150: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
6160: 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
6170: 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 "unknown databas
6180: 65 20 25 54 22 2c 20 70 4e 61 6d 65 31 29 3b 0a e %T", pName1);.
6190: 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b return -1;
61a0: 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
61b0: 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
61c0: 69 6e 69 74 2e 69 44 62 3d 3d 30 20 7c 7c 20 64 init.iDb==0 || d
61d0: 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a b->init.busy );.
61e0: 20 20 20 20 69 44 62 20 3d 20 64 62 2d 3e 69 6e iDb = db->in
61f0: 69 74 2e 69 44 62 3b 0a 20 20 20 20 2a 70 55 6e it.iDb;. *pUn
6200: 71 75 61 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 qual = pName1;.
6210: 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 44 62 3b }. return iDb;
6220: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
6230: 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
6240: 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 55 o check if the U
6250: 54 46 2d 38 20 73 74 72 69 6e 67 20 7a 4e 61 6d TF-8 string zNam
6260: 65 20 69 73 20 61 20 6c 65 67 61 6c 0a 2a 2a 20 e is a legal.**
6270: 75 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 unqualified name
6280: 20 66 6f 72 20 61 20 6e 65 77 20 73 63 68 65 6d for a new schem
6290: 61 20 6f 62 6a 65 63 74 20 28 74 61 62 6c 65 2c a object (table,
62a0: 20 69 6e 64 65 78 2c 20 76 69 65 77 20 6f 72 0a index, view or.
62b0: 2a 2a 20 74 72 69 67 67 65 72 29 2e 20 41 6c 6c ** trigger). All
62c0: 20 6e 61 6d 65 73 20 61 72 65 20 6c 65 67 61 6c names are legal
62d0: 20 65 78 63 65 70 74 20 74 68 6f 73 65 20 74 68 except those th
62e0: 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 at begin with th
62f0: 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 73 71 6c e string.** "sql
6300: 69 74 65 5f 22 20 28 69 6e 20 75 70 70 65 72 2c ite_" (in upper,
6310: 20 6c 6f 77 65 72 20 6f 72 20 6d 69 78 65 64 20 lower or mixed
6320: 63 61 73 65 29 2e 20 54 68 69 73 20 70 6f 72 74 case). This port
6330: 69 6f 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65 73 ion of the names
6340: 70 61 63 65 0a 2a 2a 20 69 73 20 72 65 73 65 72 pace.** is reser
6350: 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c ved for internal
6360: 20 75 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c use..*/.int sql
6370: 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e ite3CheckObjectN
6380: 61 6d 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ame(Parse *pPars
6390: 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
63a0: 4e 61 6d 65 29 7b 0a 20 20 69 66 28 20 21 70 50 Name){. if( !pP
63b0: 61 72 73 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 arse->db->init.b
63c0: 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e usy && pParse->n
63d0: 65 73 74 65 64 3d 3d 30 20 0a 20 20 20 20 20 20 ested==0 .
63e0: 20 20 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e && (pParse->
63f0: 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
6400: 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d TE_WriteSchema)=
6410: 3d 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 =0. &&
6420: 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 0==sqlite3StrNIC
6430: 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 mp(zName, "sqlit
6440: 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20 20 20 73 e_", 7) ){. s
6450: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
6460: 50 61 72 73 65 2c 20 22 6f 62 6a 65 63 74 20 6e Parse, "object n
6470: 61 6d 65 20 72 65 73 65 72 76 65 64 20 66 6f 72 ame reserved for
6480: 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 3a 20 25 internal use: %
6490: 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 s", zName);.
64a0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
64b0: 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ROR;. }. retur
64c0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
64d0: 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
64e0: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 64 PRIMARY KEY ind
64f0: 65 78 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a 2f ex of a table.*/
6500: 0a 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 50 .Index *sqlite3P
6510: 72 69 6d 61 72 79 4b 65 79 49 6e 64 65 78 28 54 rimaryKeyIndex(T
6520: 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 49 able *pTab){. I
6530: 6e 64 65 78 20 2a 70 3b 0a 20 20 66 6f 72 28 70 ndex *p;. for(p
6540: 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
6550: 20 26 26 20 21 49 73 50 72 69 6d 61 72 79 4b 65 && !IsPrimaryKe
6560: 79 49 6e 64 65 78 28 70 29 3b 20 70 3d 70 2d 3e yIndex(p); p=p->
6570: 70 4e 65 78 74 29 7b 7d 0a 20 20 72 65 74 75 72 pNext){}. retur
6580: 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n p;.}../*.** Re
6590: 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 turn the column
65a0: 6f 66 20 69 6e 64 65 78 20 70 49 64 78 20 74 68 of index pIdx th
65b0: 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 at corresponds t
65c0: 6f 20 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d o table.** colum
65d0: 6e 20 69 43 6f 6c 2e 20 20 52 65 74 75 72 6e 20 n iCol. Return
65e0: 2d 31 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e -1 if not found.
65f0: 0a 2a 2f 0a 69 31 36 20 73 71 6c 69 74 65 33 43 .*/.i16 sqlite3C
6600: 6f 6c 75 6d 6e 4f 66 49 6e 64 65 78 28 49 6e 64 olumnOfIndex(Ind
6610: 65 78 20 2a 70 49 64 78 2c 20 69 31 36 20 69 43 ex *pIdx, i16 iC
6620: 6f 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 ol){. int i;.
6630: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d for(i=0; i<pIdx-
6640: 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a >nColumn; i++){.
6650: 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 49 if( iCol==pI
6660: 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 dx->aiColumn[i]
6670: 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 7d 0a ) return i;. }.
6680: 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a return -1;.}..
6690: 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 63 6f 6e 73 /*.** Begin cons
66a0: 74 72 75 63 74 69 6e 67 20 61 20 6e 65 77 20 74 tructing a new t
66b0: 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 able representat
66c0: 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 ion in memory.
66d0: 54 68 69 73 20 69 73 0a 2a 2a 20 74 68 65 20 66 This is.** the f
66e0: 69 72 73 74 20 6f 66 20 73 65 76 65 72 61 6c 20 irst of several
66f0: 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 action routines
6700: 74 68 61 74 20 67 65 74 20 63 61 6c 6c 65 64 20 that get called
6710: 69 6e 20 72 65 73 70 6f 6e 73 65 0a 2a 2a 20 74 in response.** t
6720: 6f 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 o a CREATE TABLE
6730: 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 statement. In
6740: 70 61 72 74 69 63 75 6c 61 72 2c 20 74 68 69 73 particular, this
6750: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
6760: 65 64 0a 2a 2a 20 61 66 74 65 72 20 73 65 65 69 ed.** after seei
6770: 6e 67 20 74 6f 6b 65 6e 73 20 22 43 52 45 41 54 ng tokens "CREAT
6780: 45 22 20 61 6e 64 20 22 54 41 42 4c 45 22 20 61 E" and "TABLE" a
6790: 6e 64 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d nd the table nam
67a0: 65 2e 20 54 68 65 20 69 73 54 65 6d 70 0a 2a 2a e. The isTemp.**
67b0: 20 66 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 flag is true if
67c0: 20 74 68 65 20 74 61 62 6c 65 20 73 68 6f 75 6c the table shoul
67d0: 64 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 d be stored in t
67e0: 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 he auxiliary dat
67f0: 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 6e abase.** file in
6800: 73 74 65 61 64 20 6f 66 20 69 6e 20 74 68 65 20 stead of in the
6810: 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
6820: 6c 65 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 le. This is nor
6830: 6d 61 6c 6c 79 20 74 68 65 20 63 61 73 65 0a 2a mally the case.*
6840: 2a 20 77 68 65 6e 20 74 68 65 20 22 54 45 4d 50 * when the "TEMP
6850: 22 20 6f 72 20 22 54 45 4d 50 4f 52 41 52 59 22 " or "TEMPORARY"
6860: 20 6b 65 79 77 6f 72 64 20 6f 63 63 75 72 73 20 keyword occurs
6870: 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 43 52 in between.** CR
6880: 45 41 54 45 20 61 6e 64 20 54 41 42 4c 45 2e 0a EATE and TABLE..
6890: 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 74 61 **.** The new ta
68a0: 62 6c 65 20 72 65 63 6f 72 64 20 69 73 20 69 6e ble record is in
68b0: 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 20 70 75 itialized and pu
68c0: 74 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 t in pParse->pNe
68d0: 77 54 61 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f wTable..** As mo
68e0: 72 65 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 re of the CREATE
68f0: 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
6900: 20 69 73 20 70 61 72 73 65 64 2c 20 61 64 64 69 is parsed, addi
6910: 74 69 6f 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a tional action.**
6920: 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20 62 routines will b
6930: 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 64 64 20 e called to add
6940: 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e more information
6950: 20 74 6f 20 74 68 69 73 20 72 65 63 6f 72 64 2e to this record.
6960: 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f .** At the end o
6970: 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 f the CREATE TAB
6980: 4c 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 LE statement, th
6990: 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c e sqlite3EndTabl
69a0: 65 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 e() routine.** i
69b0: 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 s called to comp
69c0: 6c 65 74 65 20 74 68 65 20 63 6f 6e 73 74 72 75 lete the constru
69d0: 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 ction of the new
69e0: 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a table record..*
69f0: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 /.void sqlite3St
6a00: 61 72 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73 artTable(. Pars
6a10: 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 e *pParse, /*
6a20: 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a Parser context *
6a30: 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
6a40: 31 2c 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 1, /* First pa
6a50: 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f rt of the name o
6a60: 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 f the table or v
6a70: 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a iew */. Token *
6a80: 70 4e 61 6d 65 32 2c 20 20 20 2f 2a 20 53 65 63 pName2, /* Sec
6a90: 6f 6e 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 ond part of the
6aa0: 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
6ab0: 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 e or view */. i
6ac0: 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 nt isTemp,
6ad0: 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
6ae0: 69 73 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 is a TEMP table
6af0: 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c */. int isView,
6b00: 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
6b10: 20 74 68 69 73 20 69 73 20 61 20 56 49 45 57 20 this is a VIEW
6b20: 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 72 74 75 */. int isVirtu
6b30: 61 6c 2c 20 20 20 2f 2a 20 54 72 75 65 20 69 66 al, /* True if
6b40: 20 74 68 69 73 20 69 73 20 61 20 56 49 52 54 55 this is a VIRTU
6b50: 41 4c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e AL table */. in
6b60: 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 2f t noErr /
6b70: 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 * Do nothing if
6b80: 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 table already ex
6b90: 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 ists */.){. Tab
6ba0: 6c 65 20 2a 70 54 61 62 6c 65 3b 0a 20 20 63 68 le *pTable;. ch
6bb0: 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f ar *zName = 0; /
6bc0: 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
6bd0: 65 20 6e 65 77 20 74 61 62 6c 65 20 2a 2f 0a 20 e new table */.
6be0: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
6bf0: 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 Parse->db;. Vdb
6c00: 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b e *v;. int iDb;
6c10: 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
6c20: 62 61 73 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 base number to c
6c30: 72 65 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 reate the table
6c40: 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 in */. Token *p
6c50: 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 55 6e 71 75 Name; /* Unqu
6c60: 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 alified name of
6c70: 74 68 65 20 74 61 62 6c 65 20 74 6f 20 63 72 65 the table to cre
6c80: 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 ate */.. /* The
6c90: 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6e table or view n
6ca0: 61 6d 65 20 74 6f 20 63 72 65 61 74 65 20 69 73 ame to create is
6cb0: 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 passed to this
6cc0: 72 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 routine via toke
6cd0: 6e 73 0a 20 20 2a 2a 20 70 4e 61 6d 65 31 20 61 ns. ** pName1 a
6ce0: 6e 64 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 nd pName2. If th
6cf0: 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 e table name was
6d00: 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 fully qualified
6d10: 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 , for example:.
6d20: 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 **. ** CREATE
6d30: 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e TABLE xxx.yyy (.
6d40: 2e 2e 29 3b 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 ..);. ** . **
6d50: 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 Then pName1 is s
6d60: 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20 et to "xxx" and
6d70: 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e pName2 "yyy". On
6d80: 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20 the other hand
6d90: 69 66 0a 20 20 2a 2a 20 74 68 65 20 74 61 62 6c if. ** the tabl
6da0: 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 e name is not fu
6db0: 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69 lly qualified, i
6dc0: 2e 65 2e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 .e.:. **. ** C
6dd0: 52 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 REATE TABLE yyy(
6de0: 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ...);. **. **
6df0: 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 Then pName1 is s
6e00: 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e 64 20 et to "yyy" and
6e10: 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a 20 20 pName2 is ""..
6e20: 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c **. ** The call
6e30: 20 62 65 6c 6f 77 20 73 65 74 73 20 74 68 65 20 below sets the
6e40: 70 4e 61 6d 65 20 70 6f 69 6e 74 65 72 20 74 6f pName pointer to
6e50: 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f point at the to
6e60: 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 20 ken (pName1 or.
6e70: 20 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 ** pName2) that
6e80: 20 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 stores the unqu
6e90: 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 alified table na
6ea0: 6d 65 2e 20 54 68 65 20 76 61 72 69 61 62 6c 65 me. The variable
6eb0: 20 69 44 62 20 69 73 0a 20 20 2a 2a 20 73 65 74 iDb is. ** set
6ec0: 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 to the index of
6ed0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 the database th
6ee0: 61 74 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 at the table or
6ef0: 76 69 65 77 20 69 73 20 74 6f 20 62 65 0a 20 20 view is to be.
6f00: 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 2e 0a 20 ** created in..
6f10: 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 */. iDb = sqli
6f20: 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 te3TwoPartName(p
6f30: 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 Parse, pName1, p
6f40: 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a Name2, &pName);.
6f50: 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 if( iDb<0 ) re
6f60: 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 4f 4d 49 turn;. if( !OMI
6f70: 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 T_TEMPDB && isTe
6f80: 6d 70 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e mp && pName2->n>
6f90: 30 20 26 26 20 69 44 62 21 3d 31 20 29 7b 0a 20 0 && iDb!=1 ){.
6fa0: 20 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69 6e /* If creatin
6fb0: 67 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2c 20 g a temp table,
6fc0: 74 68 65 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 the name may not
6fd0: 20 62 65 20 71 75 61 6c 69 66 69 65 64 2e 20 55 be qualified. U
6fe0: 6e 6c 65 73 73 20 0a 20 20 20 20 2a 2a 20 74 68 nless . ** th
6ff0: 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 e database name
7000: 69 73 20 22 74 65 6d 70 22 20 61 6e 79 77 61 79 is "temp" anyway
7010: 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 . */. sqlite
7020: 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
7030: 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74 61 62 , "temporary tab
7040: 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 le name must be
7050: 75 6e 71 75 61 6c 69 66 69 65 64 22 29 3b 0a 20 unqualified");.
7060: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
7070: 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 if( !OMIT_TEMPD
7080: 42 20 26 26 20 69 73 54 65 6d 70 20 29 20 69 44 B && isTemp ) iD
7090: 62 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72 73 65 b = 1;.. pParse
70a0: 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a ->sNameToken = *
70b0: 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d pName;. zName =
70c0: 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
70d0: 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 Token(db, pName)
70e0: 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 ;. if( zName==0
70f0: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
7100: 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 SQLITE_OK!=sqli
7110: 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 te3CheckObjectNa
7120: 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 me(pParse, zName
7130: 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 65 ) ){. goto be
7140: 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b gin_table_error;
7150: 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 69 . }. if( db->i
7160: 6e 69 74 2e 69 44 62 3d 3d 31 20 29 20 69 73 54 nit.iDb==1 ) isT
7170: 65 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 emp = 1;.#ifndef
7180: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
7190: 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 HORIZATION. ass
71a0: 65 72 74 28 20 28 69 73 54 65 6d 70 20 26 20 31 ert( (isTemp & 1
71b0: 29 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20 20 7b )==isTemp );. {
71c0: 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 . int code;.
71d0: 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 char *zDb = d
71e0: 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
71f0: 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 e;. if( sqlit
7200: 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
7210: 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 se, SQLITE_INSER
7220: 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 T, SCHEMA_TABLE(
7230: 69 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29 isTemp), 0, zDb)
7240: 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 ){. goto b
7250: 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 egin_table_error
7260: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
7270: 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 isView ){.
7280: 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 if( !OMIT_TEMPDB
7290: 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 && isTemp ){.
72a0: 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c code = SQL
72b0: 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
72c0: 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 VIEW;. }els
72d0: 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 e{. code
72e0: 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f = SQLITE_CREATE_
72f0: 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 VIEW;. }.
7300: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
7310: 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 f( !OMIT_TEMPDB
7320: 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 && isTemp ){.
7330: 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 code = SQLI
7340: 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 TE_CREATE_TEMP_T
7350: 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ABLE;. }els
7360: 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 e{. code
7370: 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f = SQLITE_CREATE_
7380: 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 TABLE;. }.
7390: 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 }. if( !is
73a0: 56 69 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74 Virtual && sqlit
73b0: 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
73c0: 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c se, code, zName,
73d0: 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 0, zDb) ){.
73e0: 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 goto begin_tab
73f0: 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a le_error;. }.
7400: 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
7410: 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e Make sure the n
7420: 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f ew table name do
7430: 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 es not collide w
7440: 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a ith an existing.
7450: 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 ** index or ta
7460: 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 ble name in the
7470: 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20 20 same database.
7480: 49 73 73 75 65 20 61 6e 20 65 72 72 6f 72 20 6d Issue an error m
7490: 65 73 73 61 67 65 20 69 66 0a 20 20 2a 2a 20 69 essage if. ** i
74a0: 74 20 64 6f 65 73 2e 20 54 68 65 20 65 78 63 65 t does. The exce
74b0: 70 74 69 6f 6e 20 69 73 20 69 66 20 74 68 65 20 ption is if the
74c0: 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 statement being
74d0: 70 61 72 73 65 64 20 77 61 73 20 70 61 73 73 65 parsed was passe
74e0: 64 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c d. ** to an sql
74f0: 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 ite3_declare_vta
7500: 62 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61 b() call. In tha
7510: 74 20 63 61 73 65 20 6f 6e 6c 79 20 74 68 65 20 t case only the
7520: 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a column names. *
7530: 2a 20 61 6e 64 20 74 79 70 65 73 20 77 69 6c 6c * and types will
7540: 20 62 65 20 75 73 65 64 2c 20 73 6f 20 74 68 65 be used, so the
7550: 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f re is no need to
7560: 20 74 65 73 74 20 66 6f 72 20 6e 61 6d 65 73 70 test for namesp
7570: 61 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69 ace. ** collisi
7580: 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ons.. */. if(
7590: 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 !IN_DECLARE_VTAB
75a0: 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 ){. char *zD
75b0: 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d b = db->aDb[iDb]
75c0: 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 .zName;. if(
75d0: 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 SQLITE_OK!=sqlit
75e0: 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 e3ReadSchema(pPa
75f0: 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f rse) ){. go
7600: 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 to begin_table_e
7610: 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rror;. }.
7620: 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 pTable = sqlite3
7630: 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e FindTable(db, zN
7640: 61 6d 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 ame, zDb);. i
7650: 66 28 20 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 f( pTable ){.
7660: 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b if( !noErr ){
7670: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
7680: 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
7690: 20 22 74 61 62 6c 65 20 25 54 20 61 6c 72 65 61 "table %T alrea
76a0: 64 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d dy exists", pNam
76b0: 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b e);. }else{
76c0: 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
76d0: 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 !db->init.busy
76e0: 7c 7c 20 43 4f 52 52 55 50 54 5f 44 42 20 29 3b || CORRUPT_DB );
76f0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
7700: 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 CodeVerifySchema
7710: 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 (pParse, iDb);.
7720: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 }. got
7730: 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 o begin_table_er
7740: 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ror;. }. i
7750: 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e f( sqlite3FindIn
7760: 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a dex(db, zName, z
7770: 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 Db)!=0 ){.
7780: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
7790: 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 pParse, "there i
77a0: 73 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 s already an ind
77b0: 65 78 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e ex named %s", zN
77c0: 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f ame);. goto
77d0: 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
77e0: 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 or;. }. }..
77f0: 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 pTable = sqlite
7800: 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
7810: 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 , sizeof(Table))
7820: 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d ;. if( pTable==
7830: 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 0 ){. db->mal
7840: 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
7850: 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 pParse->rc =
7860: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
7870: 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b pParse->nErr++
7880: 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e ;. goto begin
7890: 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 _table_error;.
78a0: 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d }. pTable->zNam
78b0: 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 e = zName;. pTa
78c0: 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b ble->iPKey = -1;
78d0: 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 . pTable->pSche
78e0: 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 ma = db->aDb[iDb
78f0: 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 ].pSchema;. pTa
7900: 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 ble->nRef = 1;.
7910: 20 70 54 61 62 6c 65 2d 3e 6e 52 6f 77 4c 6f 67 pTable->nRowLog
7920: 45 73 74 20 3d 20 32 30 30 3b 20 61 73 73 65 72 Est = 200; asser
7930: 74 28 20 32 30 30 3d 3d 73 71 6c 69 74 65 33 4c t( 200==sqlite3L
7940: 6f 67 45 73 74 28 31 30 34 38 35 37 36 29 20 29 ogEst(1048576) )
7950: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 ;. assert( pPar
7960: 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 se->pNewTable==0
7970: 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e );. pParse->pN
7980: 65 77 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 ewTable = pTable
7990: 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 ;.. /* If this
79a0: 69 73 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c is the magic sql
79b0: 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 ite_sequence tab
79c0: 6c 65 20 75 73 65 64 20 62 79 20 61 75 74 6f 69 le used by autoi
79d0: 6e 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 ncrement,. ** t
79e0: 68 65 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69 hen record a poi
79f0: 6e 74 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 nter to this tab
7a00: 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 le in the main d
7a10: 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 atabase structur
7a20: 65 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 e. ** so that I
7a30: 4e 53 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74 NSERT can find t
7a40: 68 65 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e he table easily.
7a50: 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 . */.#ifndef SQ
7a60: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
7a70: 43 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 CREMENT. if( !p
7a80: 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 Parse->nested &&
7a90: 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 strcmp(zName, "
7aa0: 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 sqlite_sequence"
7ab0: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 )==0 ){. asse
7ac0: 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d rt( sqlite3Schem
7ad0: 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 aMutexHeld(db, i
7ae0: 44 62 2c 20 30 29 20 29 3b 0a 20 20 20 20 70 54 Db, 0) );. pT
7af0: 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 able->pSchema->p
7b00: 53 65 71 54 61 62 20 3d 20 70 54 61 62 6c 65 3b SeqTab = pTable;
7b10: 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
7b20: 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 * Begin generati
7b30: 6e 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 ng the code that
7b40: 20 77 69 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 will insert the
7b50: 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 69 6e table record in
7b60: 74 6f 0a 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 to. ** the SQLI
7b70: 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e TE_MASTER table.
7b80: 20 20 4e 6f 74 65 20 69 6e 20 70 61 72 74 69 63 Note in partic
7b90: 75 6c 61 72 20 74 68 61 74 20 77 65 20 6d 75 73 ular that we mus
7ba0: 74 20 67 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 t go ahead. **
7bb0: 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 and allocate the
7bc0: 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 record number f
7bd0: 6f 72 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 or the table ent
7be0: 72 79 20 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 ry now. Before
7bf0: 61 6e 79 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 any. ** PRIMARY
7c00: 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 6b KEY or UNIQUE k
7c10: 65 79 77 6f 72 64 73 20 61 72 65 20 70 61 72 73 eywords are pars
7c20: 65 64 2e 20 20 54 68 6f 73 65 20 6b 65 79 77 6f ed. Those keywo
7c30: 72 64 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 rds will cause.
7c40: 20 2a 2a 20 69 6e 64 69 63 65 73 20 74 6f 20 62 ** indices to b
7c50: 65 20 63 72 65 61 74 65 64 20 61 6e 64 20 74 68 e created and th
7c60: 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 6d e table record m
7c70: 75 73 74 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 ust come before
7c80: 74 68 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 the . ** indice
7c90: 73 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 72 s. Hence, the r
7ca0: 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 ecord number for
7cb0: 20 74 68 65 20 74 61 62 6c 65 20 6d 75 73 74 20 the table must
7cc0: 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a be allocated. *
7cd0: 2a 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 * now.. */. if
7ce0: 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 ( !db->init.busy
7cf0: 20 26 26 20 28 76 20 3d 20 73 71 6c 69 74 65 33 && (v = sqlite3
7d00: 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29 GetVdbe(pParse))
7d10: 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a !=0 ){. int j
7d20: 31 3b 0a 20 20 20 20 69 6e 74 20 66 69 6c 65 46 1;. int fileF
7d30: 6f 72 6d 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 ormat;. int r
7d40: 65 67 31 2c 20 72 65 67 32 2c 20 72 65 67 33 3b eg1, reg2, reg3;
7d50: 0a 20 20 20 20 2f 2a 20 6e 75 6c 6c 52 6f 77 5b . /* nullRow[
7d60: 5d 20 69 73 20 61 6e 20 4f 50 5f 52 65 63 6f 72 ] is an OP_Recor
7d70: 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 d encoding of a
7d80: 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 35 row containing 5
7d90: 20 4e 55 4c 4c 73 20 2a 2f 0a 20 20 20 20 73 74 NULLs */. st
7da0: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
7db0: 6e 75 6c 6c 52 6f 77 5b 5d 20 3d 20 7b 20 36 2c nullRow[] = { 6,
7dc0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 0, 0, 0, 0, 0 }
7dd0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 ;. sqlite3Beg
7de0: 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e inWriteOperation
7df0: 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 (pParse, 1, iDb)
7e00: 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
7e10: 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
7e20: 42 4c 45 0a 20 20 20 20 69 66 28 20 69 73 56 69 BLE. if( isVi
7e30: 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 rtual ){. s
7e40: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 qlite3VdbeAddOp0
7e50: 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a (v, OP_VBegin);.
7e60: 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
7e70: 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 /* If the file
7e80: 20 66 6f 72 6d 61 74 20 61 6e 64 20 65 6e 63 6f format and enco
7e90: 64 69 6e 67 20 69 6e 20 74 68 65 20 64 61 74 61 ding in the data
7ea0: 62 61 73 65 20 68 61 76 65 20 6e 6f 74 20 62 65 base have not be
7eb0: 65 6e 20 73 65 74 2c 20 0a 20 20 20 20 2a 2a 20 en set, . **
7ec0: 73 65 74 20 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 set them now..
7ed0: 20 20 2a 2f 0a 20 20 20 20 72 65 67 31 20 3d 20 */. reg1 =
7ee0: 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 pParse->regRowid
7ef0: 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 = ++pParse->nMe
7f00: 6d 3b 0a 20 20 20 20 72 65 67 32 20 3d 20 70 50 m;. reg2 = pP
7f10: 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 arse->regRoot =
7f20: 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
7f30: 20 20 20 20 72 65 67 33 20 3d 20 2b 2b 70 50 61 reg3 = ++pPa
7f40: 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 rse->nMem;. s
7f50: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
7f60: 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 (v, OP_ReadCooki
7f70: 65 2c 20 69 44 62 2c 20 72 65 67 33 2c 20 42 54 e, iDb, reg3, BT
7f80: 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 29 REE_FILE_FORMAT)
7f90: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
7fa0: 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 eUsesBtree(v, iD
7fb0: 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c b);. j1 = sql
7fc0: 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
7fd0: 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b 20 , OP_If, reg3);
7fe0: 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b VdbeCoverage(v);
7ff0: 0a 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 74 20 . fileFormat
8000: 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 = (db->flags & S
8010: 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 QLITE_LegacyFile
8020: 46 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 Fmt)!=0 ?.
8030: 20 20 20 20 20 20 20 20 20 20 20 20 31 20 3a 20 1 :
8040: 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f SQLITE_MAX_FILE_
8050: 46 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69 FORMAT;. sqli
8060: 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
8070: 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c OP_Integer, fil
8080: 65 46 6f 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a eFormat, reg3);.
8090: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
80a0: 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 ddOp3(v, OP_SetC
80b0: 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45 ookie, iDb, BTRE
80c0: 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2c 20 72 E_FILE_FORMAT, r
80d0: 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 eg3);. sqlite
80e0: 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
80f0: 50 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 P_Integer, ENC(d
8100: 62 29 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 b), reg3);. s
8110: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
8120: 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 (v, OP_SetCookie
8130: 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 54 45 58 , iDb, BTREE_TEX
8140: 54 5f 45 4e 43 4f 44 49 4e 47 2c 20 72 65 67 33 T_ENCODING, reg3
8150: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
8160: 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 beJumpHere(v, j1
8170: 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 );.. /* This
8180: 6a 75 73 74 20 63 72 65 61 74 65 73 20 61 20 70 just creates a p
8190: 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f lace-holder reco
81a0: 72 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 rd in the sqlite
81b0: 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 _master table..
81c0: 20 20 20 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 ** The record
81d0: 20 63 72 65 61 74 65 64 20 64 6f 65 73 20 6e 6f created does no
81e0: 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69 t contain anythi
81f0: 6e 67 20 79 65 74 2e 20 20 49 74 20 77 69 6c 6c ng yet. It will
8200: 20 62 65 20 72 65 70 6c 61 63 65 64 0a 20 20 20 be replaced.
8210: 20 2a 2a 20 62 79 20 74 68 65 20 72 65 61 6c 20 ** by the real
8220: 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 20 67 65 entry in code ge
8230: 6e 65 72 61 74 65 64 20 61 74 20 73 71 6c 69 74 nerated at sqlit
8240: 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20 e3EndTable()..
8250: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
8260: 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 6e 65 rowid for the ne
8270: 77 20 65 6e 74 72 79 20 69 73 20 6c 65 66 74 20 w entry is left
8280: 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72 in register pPar
8290: 73 65 2d 3e 72 65 67 52 6f 77 69 64 2e 0a 20 20 se->regRowid..
82a0: 20 20 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 ** The root pa
82b0: 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
82c0: 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20 6c 65 new table is le
82d0: 66 74 20 69 6e 20 72 65 67 20 70 50 61 72 73 65 ft in reg pParse
82e0: 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a ->regRoot.. *
82f0: 2a 20 54 68 65 20 72 6f 77 69 64 20 61 6e 64 20 * The rowid and
8300: 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
8310: 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 65 64 values are need
8320: 65 64 20 62 79 20 74 68 65 20 63 6f 64 65 20 74 ed by the code t
8330: 68 61 74 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 hat. ** sqlit
8340: 65 33 45 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20 e3EndTable will
8350: 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f generate.. */
8360: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
8370: 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
8380: 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
8390: 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
83a0: 41 42 4c 45 29 0a 20 20 20 20 69 66 28 20 69 73 ABLE). if( is
83b0: 56 69 65 77 20 7c 7c 20 69 73 56 69 72 74 75 61 View || isVirtua
83c0: 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 l ){. sqlit
83d0: 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
83e0: 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 OP_Integer, 0, r
83f0: 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a eg2);. }else.
8400: 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 #endif. {.
8410: 20 20 20 70 50 61 72 73 65 2d 3e 61 64 64 72 43 pParse->addrC
8420: 72 54 61 62 20 3d 20 73 71 6c 69 74 65 33 56 64 rTab = sqlite3Vd
8430: 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 beAddOp2(v, OP_C
8440: 72 65 61 74 65 54 61 62 6c 65 2c 20 69 44 62 2c reateTable, iDb,
8450: 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 0a 20 20 reg2);. }.
8460: 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 sqlite3OpenMas
8470: 74 65 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c terTable(pParse,
8480: 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 iDb);. sqlit
8490: 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
84a0: 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20 OP_NewRowid, 0,
84b0: 72 65 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 reg1);. sqlit
84c0: 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
84d0: 4f 50 5f 42 6c 6f 62 2c 20 36 2c 20 72 65 67 33 OP_Blob, 6, reg3
84e0: 2c 20 30 2c 20 6e 75 6c 6c 52 6f 77 2c 20 50 34 , 0, nullRow, P4
84f0: 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 _STATIC);. sq
8500: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
8510: 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c v, OP_Insert, 0,
8520: 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0a 20 20 reg3, reg1);.
8530: 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
8540: 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f ngeP5(v, OPFLAG_
8550: 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c APPEND);. sql
8560: 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 ite3VdbeAddOp0(v
8570: 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d , OP_Close);. }
8580: 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e .. /* Normal (n
8590: 6f 6e 2d 65 72 72 6f 72 29 20 72 65 74 75 72 6e on-error) return
85a0: 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a . */. return;..
85b0: 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 /* If an error
85c0: 20 6f 63 63 75 72 73 2c 20 77 65 20 6a 75 6d 70 occurs, we jump
85d0: 20 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 here */.begin_t
85e0: 61 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71 able_error:. sq
85f0: 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
8600: 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e zName);. return
8610: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
8620: 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 6f macro is used to
8630: 20 63 6f 6d 70 61 72 65 20 74 77 6f 20 73 74 72 compare two str
8640: 69 6e 67 73 20 69 6e 20 61 20 63 61 73 65 2d 69 ings in a case-i
8650: 6e 73 65 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65 nsensitive manne
8660: 72 2e 0a 2a 2a 20 49 74 20 69 73 20 73 6c 69 67 r..** It is slig
8670: 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 61 6e htly faster than
8680: 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 calling sqlite3
8690: 53 74 72 49 43 6d 70 28 29 20 64 69 72 65 63 74 StrICmp() direct
86a0: 6c 79 2c 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75 ly, but.** produ
86b0: 63 65 73 20 6c 61 72 67 65 72 20 63 6f 64 65 2e ces larger code.
86c0: 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 .**.** WARNING:
86d0: 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f This macro is no
86e0: 74 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 t compatible wit
86f0: 68 20 74 68 65 20 73 74 72 63 6d 70 28 29 20 66 h the strcmp() f
8700: 61 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74 amily. It.** ret
8710: 75 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 urns true if the
8720: 20 74 77 6f 20 73 74 72 69 6e 67 73 20 61 72 65 two strings are
8730: 20 65 71 75 61 6c 2c 20 6f 74 68 65 72 77 69 73 equal, otherwis
8740: 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 e false..*/.#def
8750: 69 6e 65 20 53 54 52 49 43 4d 50 28 78 2c 20 79 ine STRICMP(x, y
8760: 29 20 28 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 ) (\.sqlite3Uppe
8770: 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 rToLower[*(unsig
8780: 6e 65 64 20 63 68 61 72 20 2a 29 28 78 29 5d 3d ned char *)(x)]=
8790: 3d 20 20 20 5c 0a 73 71 6c 69 74 65 33 55 70 70 = \.sqlite3Upp
87a0: 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 erToLower[*(unsi
87b0: 67 6e 65 64 20 63 68 61 72 20 2a 29 28 79 29 5d gned char *)(y)]
87c0: 20 20 20 20 20 5c 0a 26 26 20 73 71 6c 69 74 65 \.&& sqlite
87d0: 33 53 74 72 49 43 6d 70 28 28 78 29 2b 31 2c 28 3StrICmp((x)+1,(
87e0: 79 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a y)+1)==0 )../*.*
87f0: 2a 20 41 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 * Add a new colu
8800: 6d 6e 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 mn to the table
8810: 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 currently being
8820: 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a constructed..**.
8830: 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 ** The parser ca
8840: 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 lls this routine
8850: 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 once for each c
8860: 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f olumn declaratio
8870: 6e 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41 54 45 n.** in a CREATE
8880: 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
8890: 2e 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 . sqlite3StartT
88a0: 61 62 6c 65 28 29 20 67 65 74 73 20 63 61 6c 6c able() gets call
88b0: 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 6f 20 67 ed.** first to g
88c0: 65 74 20 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e et things going.
88d0: 20 20 54 68 65 6e 20 74 68 69 73 20 72 6f 75 74 Then this rout
88e0: 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f ine is called fo
88f0: 72 20 65 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e r each.** column
8900: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
8910: 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 3AddColumn(Parse
8920: 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 *pParse, Token
8930: 2a 70 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 *pName){. Table
8940: 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 *p;. int i;.
8950: 63 68 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d char *z;. Colum
8960: 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 n *pCol;. sqlit
8970: 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
8980: 3e 64 62 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 >db;. if( (p =
8990: 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
89a0: 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a e)==0 ) return;.
89b0: 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 #if SQLITE_MAX_C
89c0: 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 6e OLUMN. if( p->n
89d0: 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 Col+1>db->aLimit
89e0: 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f [SQLITE_LIMIT_CO
89f0: 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c LUMN] ){. sql
8a00: 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
8a10: 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 rse, "too many c
8a20: 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73 22 2c 20 70 olumns on %s", p
8a30: 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 ->zName);. re
8a40: 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 turn;. }.#endif
8a50: 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 . z = sqlite3Na
8a60: 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 meFromToken(db,
8a70: 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d pName);. if( z=
8a80: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 =0 ) return;. f
8a90: 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f or(i=0; i<p->nCo
8aa0: 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 l; i++){. if(
8ab0: 20 53 54 52 49 43 4d 50 28 7a 2c 20 70 2d 3e 61 STRICMP(z, p->a
8ac0: 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b Col[i].zName) ){
8ad0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
8ae0: 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
8af0: 64 75 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d 6e duplicate column
8b00: 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b 0a name: %s", z);.
8b10: 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
8b20: 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 ree(db, z);.
8b30: 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
8b40: 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e 43 }. if( (p->nC
8b50: 6f 6c 20 26 20 30 78 37 29 3d 3d 30 20 29 7b 0a ol & 0x7)==0 ){.
8b60: 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 Column *aNew
8b70: 3b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c ;. aNew = sql
8b80: 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 ite3DbRealloc(db
8b90: 2c 70 2d 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f ,p->aCol,(p->nCo
8ba0: 6c 2b 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 l+8)*sizeof(p->a
8bb0: 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 Col[0]));. if
8bc0: 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 ( aNew==0 ){.
8bd0: 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
8be0: 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 (db, z);. r
8bf0: 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 eturn;. }.
8c00: 20 70 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77 3b p->aCol = aNew;
8c10: 0a 20 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26 70 . }. pCol = &p
8c20: 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b ->aCol[p->nCol];
8c30: 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c 20 . memset(pCol,
8c40: 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 0, sizeof(p->aCo
8c50: 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e l[0]));. pCol->
8c60: 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f zName = z;. . /
8c70: 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f * If there is no
8c80: 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64 2c type specified,
8c90: 20 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74 68 columns have th
8ca0: 65 20 64 65 66 61 75 6c 74 20 61 66 66 69 6e 69 e default affini
8cb0: 74 79 0a 20 20 2a 2a 20 27 42 4c 4f 42 27 2e 20 ty. ** 'BLOB'.
8cc0: 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74 79 If there is a ty
8cd0: 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 pe specified, th
8ce0: 65 6e 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c en sqlite3AddCol
8cf0: 75 6d 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a 20 umnType() will.
8d00: 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e 65 ** be called ne
8d10: 78 74 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d 3e xt to set pCol->
8d20: 61 66 66 69 6e 69 74 79 20 63 6f 72 72 65 63 74 affinity correct
8d30: 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d ly.. */. pCol-
8d40: 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 >affinity = SQLI
8d50: 54 45 5f 41 46 46 5f 42 4c 4f 42 3b 0a 20 20 70 TE_AFF_BLOB;. p
8d60: 43 6f 6c 2d 3e 73 7a 45 73 74 20 3d 20 31 3b 0a Col->szEst = 1;.
8d70: 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a p->nCol++;.}..
8d80: 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
8d90: 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 ne is called by
8da0: 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 the parser while
8db0: 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
8dc0: 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 f.** parsing a C
8dd0: 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
8de0: 65 6d 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20 4e ement. A "NOT N
8df0: 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 ULL" constraint
8e00: 68 61 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65 6e has.** been seen
8e10: 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 54 on a column. T
8e20: 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 his routine sets
8e30: 20 74 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c 61 the notNull fla
8e40: 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 g on.** the colu
8e50: 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 mn currently und
8e60: 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e er construction.
8e70: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
8e80: 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 AddNotNull(Parse
8e90: 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e *pParse, int on
8ea0: 45 72 72 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20 Error){. Table
8eb0: 2a 70 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 *p;. p = pParse
8ec0: 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 ->pNewTable;. i
8ed0: 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 f( p==0 || NEVER
8ee0: 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 72 65 (p->nCol<1) ) re
8ef0: 74 75 72 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 5b turn;. p->aCol[
8f00: 70 2d 3e 6e 43 6f 6c 2d 31 5d 2e 6e 6f 74 4e 75 p->nCol-1].notNu
8f10: 6c 6c 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 ll = (u8)onError
8f20: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 ;.}../*.** Scan
8f30: 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 the column type
8f40: 6e 61 6d 65 20 7a 54 79 70 65 20 28 6c 65 6e 67 name zType (leng
8f50: 74 68 20 6e 54 79 70 65 29 20 61 6e 64 20 72 65 th nType) and re
8f60: 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 73 73 6f turn the.** asso
8f70: 63 69 61 74 65 64 20 61 66 66 69 6e 69 74 79 20 ciated affinity
8f80: 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 type..**.** This
8f90: 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20 routine does a
8fa0: 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 case-independent
8fb0: 20 73 65 61 72 63 68 20 6f 66 20 7a 54 79 70 65 search of zType
8fc0: 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 73 75 62 for the .** sub
8fd0: 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 66 strings in the f
8fe0: 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 2e 20 ollowing table.
8ff0: 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 If one of the su
9000: 62 73 74 72 69 6e 67 73 20 69 73 0a 2a 2a 20 66 bstrings is.** f
9010: 6f 75 6e 64 2c 20 74 68 65 20 63 6f 72 72 65 73 ound, the corres
9020: 70 6f 6e 64 69 6e 67 20 61 66 66 69 6e 69 74 79 ponding affinity
9030: 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
9040: 20 7a 54 79 70 65 20 63 6f 6e 74 61 69 6e 73 0a zType contains.
9050: 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 ** more than one
9060: 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e of the substrin
9070: 67 73 2c 20 65 6e 74 72 69 65 73 20 74 6f 77 61 gs, entries towa
9080: 72 64 20 74 68 65 20 74 6f 70 20 6f 66 20 0a 2a rd the top of .*
9090: 2a 20 74 68 65 20 74 61 62 6c 65 20 74 61 6b 65 * the table take
90a0: 20 70 72 69 6f 72 69 74 79 2e 20 46 6f 72 20 65 priority. For e
90b0: 78 61 6d 70 6c 65 2c 20 69 66 20 7a 54 79 70 65 xample, if zType
90c0: 20 69 73 20 27 42 4c 4f 42 49 4e 54 27 2c 20 0a is 'BLOBINT', .
90d0: 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e ** SQLITE_AFF_IN
90e0: 54 45 47 45 52 20 69 73 20 72 65 74 75 72 6e 65 TEGER is returne
90f0: 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 73 74 72 69 d..**.** Substri
9100: 6e 67 20 20 20 20 20 7c 20 41 66 66 69 6e 69 74 ng | Affinit
9110: 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d y.** -----------
9120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
9130: 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 -----.** 'INT'
9140: 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f | SQLITE_
9150: 41 46 46 5f 49 4e 54 45 47 45 52 0a 2a 2a 20 27 AFF_INTEGER.** '
9160: 43 48 41 52 27 20 20 20 20 20 20 20 20 7c 20 53 CHAR' | S
9170: 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a QLITE_AFF_TEXT.*
9180: 2a 20 27 43 4c 4f 42 27 20 20 20 20 20 20 20 20 * 'CLOB'
9190: 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 | SQLITE_AFF_TEX
91a0: 54 0a 2a 2a 20 27 54 45 58 54 27 20 20 20 20 20 T.** 'TEXT'
91b0: 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f | SQLITE_AFF_
91c0: 54 45 58 54 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 TEXT.** 'BLOB'
91d0: 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 | SQLITE_A
91e0: 46 46 5f 42 4c 4f 42 0a 2a 2a 20 27 52 45 41 4c FF_BLOB.** 'REAL
91f0: 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 ' | SQLIT
9200: 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 E_AFF_REAL.** 'F
9210: 4c 4f 41 27 20 20 20 20 20 20 20 20 7c 20 53 51 LOA' | SQ
9220: 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a LITE_AFF_REAL.**
9230: 20 27 44 4f 55 42 27 20 20 20 20 20 20 20 20 7c 'DOUB' |
9240: 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
9250: 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f .**.** If none o
9260: 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 f the substrings
9270: 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 74 61 in the above ta
9280: 62 6c 65 20 61 72 65 20 66 6f 75 6e 64 2c 0a 2a ble are found,.*
9290: 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d * SQLITE_AFF_NUM
92a0: 45 52 49 43 20 69 73 20 72 65 74 75 72 6e 65 64 ERIC is returned
92b0: 2e 0a 2a 2f 0a 63 68 61 72 20 73 71 6c 69 74 65 ..*/.char sqlite
92c0: 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 63 6f 3AffinityType(co
92d0: 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 75 nst char *zIn, u
92e0: 38 20 2a 70 73 7a 45 73 74 29 7b 0a 20 20 75 33 8 *pszEst){. u3
92f0: 32 20 68 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2 h = 0;. char
9300: 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 aff = SQLITE_AFF
9310: 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 63 6f 6e 73 _NUMERIC;. cons
9320: 74 20 63 68 61 72 20 2a 7a 43 68 61 72 20 3d 20 t char *zChar =
9330: 30 3b 0a 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 0;.. if( zIn==0
9340: 20 29 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 20 ) return aff;.
9350: 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 while( zIn[0] )
9360: 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 38 29 {. h = (h<<8)
9370: 20 2b 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 + sqlite3UpperT
9380: 6f 4c 6f 77 65 72 5b 28 2a 7a 49 6e 29 26 30 78 oLower[(*zIn)&0x
9390: 66 66 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b 3b 0a ff];. zIn++;.
93a0: 20 20 20 20 69 66 28 20 68 3d 3d 28 28 27 63 27 if( h==(('c'
93b0: 3c 3c 32 34 29 2b 28 27 68 27 3c 3c 31 36 29 2b <<24)+('h'<<16)+
93c0: 28 27 61 27 3c 3c 38 29 2b 27 72 27 29 20 29 7b ('a'<<8)+'r') ){
93d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
93e0: 43 48 41 52 20 2a 2f 0a 20 20 20 20 20 20 61 66 CHAR */. af
93f0: 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 f = SQLITE_AFF_T
9400: 45 58 54 3b 0a 20 20 20 20 20 20 7a 43 68 61 72 EXT;. zChar
9410: 20 3d 20 7a 49 6e 3b 0a 20 20 20 20 7d 65 6c 73 = zIn;. }els
9420: 65 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c e if( h==(('c'<<
9430: 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 24)+('l'<<16)+('
9440: 6f 27 3c 3c 38 29 2b 27 62 27 29 20 29 7b 20 20 o'<<8)+'b') ){
9450: 20 20 20 20 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a /* CLOB */.
9460: 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 aff = SQLI
9470: 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 TE_AFF_TEXT;.
9480: 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 }else if( h==((
9490: 27 74 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 't'<<24)+('e'<<1
94a0: 36 29 2b 28 27 78 27 3c 3c 38 29 2b 27 74 27 29 6)+('x'<<8)+'t')
94b0: 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 54 45 58 ){ /* TEX
94c0: 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d T */. aff =
94d0: 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
94e0: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
94f0: 68 3d 3d 28 28 27 62 27 3c 3c 32 34 29 2b 28 27 h==(('b'<<24)+('
9500: 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 l'<<16)+('o'<<8)
9510: 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 20 2f +'b') /
9520: 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 * BLOB */.
9530: 20 20 26 26 20 28 61 66 66 3d 3d 53 51 4c 49 54 && (aff==SQLIT
9540: 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 7c E_AFF_NUMERIC ||
9550: 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 aff==SQLITE_AFF
9560: 5f 52 45 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 _REAL) ){.
9570: 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 aff = SQLITE_AFF
9580: 5f 42 4c 4f 42 3b 0a 20 20 20 20 20 20 69 66 28 _BLOB;. if(
9590: 20 7a 49 6e 5b 30 5d 3d 3d 27 28 27 20 29 20 7a zIn[0]=='(' ) z
95a0: 43 68 61 72 20 3d 20 7a 49 6e 3b 0a 23 69 66 6e Char = zIn;.#ifn
95b0: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
95c0: 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 FLOATING_POINT.
95d0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d }else if( h==
95e0: 28 28 27 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c (('r'<<24)+('e'<
95f0: 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c <16)+('a'<<8)+'l
9600: 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 ') /* R
9610: 45 41 4c 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 EAL */. &
9620: 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 & aff==SQLITE_AF
9630: 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 F_NUMERIC ){.
9640: 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f aff = SQLITE_
9650: 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 AFF_REAL;. }e
9660: 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 66 27 lse if( h==(('f'
9670: 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b <<24)+('l'<<16)+
9680: 28 27 6f 27 3c 3c 38 29 2b 27 61 27 29 20 20 20 ('o'<<8)+'a')
9690: 20 20 20 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a /* FLOA *
96a0: 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 /. && aff
96b0: 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d ==SQLITE_AFF_NUM
96c0: 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 ERIC ){. af
96d0: 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 f = SQLITE_AFF_R
96e0: 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 EAL;. }else i
96f0: 66 28 20 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29 f( h==(('d'<<24)
9700: 2b 28 27 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c +('o'<<16)+('u'<
9710: 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20 <8)+'b')
9720: 20 20 2f 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20 /* DOUB */.
9730: 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c && aff==SQL
9740: 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 ITE_AFF_NUMERIC
9750: 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 ){. aff = S
9760: 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a QLITE_AFF_REAL;.
9770: 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 #endif. }else
9780: 20 69 66 28 20 28 68 26 30 78 30 30 46 46 46 46 if( (h&0x00FFFF
9790: 46 46 29 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b FF)==(('i'<<16)+
97a0: 28 27 6e 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b ('n'<<8)+'t') ){
97b0: 20 20 20 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20 /* INT */.
97c0: 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
97d0: 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 _AFF_INTEGER;.
97e0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
97f0: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 70 73 . }.. /* If ps
9800: 7a 45 73 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c zEst is not NULL
9810: 2c 20 73 74 6f 72 65 20 61 6e 20 65 73 74 69 6d , store an estim
9820: 61 74 65 20 6f 66 20 74 68 65 20 66 69 65 6c 64 ate of the field
9830: 20 73 69 7a 65 2e 20 20 54 68 65 0a 20 20 2a 2a size. The. **
9840: 20 65 73 74 69 6d 61 74 65 20 69 73 20 73 63 61 estimate is sca
9850: 6c 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 led so that the
9860: 73 69 7a 65 20 6f 66 20 61 6e 20 69 6e 74 65 67 size of an integ
9870: 65 72 20 69 73 20 31 2e 20 20 2a 2f 0a 20 20 69 er is 1. */. i
9880: 66 28 20 70 73 7a 45 73 74 20 29 7b 0a 20 20 20 f( pszEst ){.
9890: 20 2a 70 73 7a 45 73 74 20 3d 20 31 3b 20 20 20 *pszEst = 1;
98a0: 2f 2a 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 /* default size
98b0: 69 73 20 61 70 70 72 6f 78 20 34 20 62 79 74 65 is approx 4 byte
98c0: 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 66 66 s */. if( aff
98d0: 3c 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 <SQLITE_AFF_NUME
98e0: 52 49 43 20 29 7b 0a 20 20 20 20 20 20 69 66 28 RIC ){. if(
98f0: 20 7a 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 zChar ){.
9900: 20 20 77 68 69 6c 65 28 20 7a 43 68 61 72 5b 30 while( zChar[0
9910: 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ] ){. i
9920: 66 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 f( sqlite3Isdigi
9930: 74 28 7a 43 68 61 72 5b 30 5d 29 20 29 7b 0a 20 t(zChar[0]) ){.
9940: 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 76 int v
9950: 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
9960: 20 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 sqlite3GetInt3
9970: 32 28 7a 43 68 61 72 2c 20 26 76 29 3b 0a 20 20 2(zChar, &v);.
9980: 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 2f v = v/
9990: 34 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 20 4 + 1;.
99a0: 20 20 20 69 66 28 20 76 3e 32 35 35 20 29 20 76 if( v>255 ) v
99b0: 20 3d 20 32 35 35 3b 0a 20 20 20 20 20 20 20 20 = 255;.
99c0: 20 20 20 20 2a 70 73 7a 45 73 74 20 3d 20 76 3b *pszEst = v;
99d0: 20 2f 2a 20 42 4c 4f 42 28 6b 29 2c 20 56 41 52 /* BLOB(k), VAR
99e0: 43 48 41 52 28 6b 29 2c 20 43 48 41 52 28 6b 29 CHAR(k), CHAR(k)
99f0: 20 2d 3e 20 72 3d 28 6b 2f 34 2b 31 29 20 2a 2f -> r=(k/4+1) */
9a00: 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 . bre
9a10: 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ak;. }.
9a20: 20 20 20 20 20 20 20 20 20 20 7a 43 68 61 72 2b zChar+
9a30: 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 +;. }.
9a40: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
9a50: 20 20 2a 70 73 7a 45 73 74 20 3d 20 35 3b 20 20 *pszEst = 5;
9a60: 20 2f 2a 20 42 4c 4f 42 2c 20 54 45 58 54 2c 20 /* BLOB, TEXT,
9a70: 43 4c 4f 42 20 2d 3e 20 72 3d 35 20 20 28 61 70 CLOB -> r=5 (ap
9a80: 70 72 6f 78 20 32 30 20 62 79 74 65 73 29 2a 2f prox 20 bytes)*/
9a90: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
9aa0: 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b }. return aff;
9ab0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
9ac0: 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
9ad0: 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 by the parser w
9ae0: 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 hile in the midd
9af0: 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 le of.** parsing
9b00: 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 a CREATE TABLE
9b10: 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 statement. The
9b20: 70 46 69 72 73 74 20 74 6f 6b 65 6e 20 69 73 20 pFirst token is
9b30: 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b the first.** tok
9b40: 65 6e 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e en in the sequen
9b50: 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 74 68 61 ce of tokens tha
9b60: 74 20 64 65 73 63 72 69 62 65 20 74 68 65 20 74 t describe the t
9b70: 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f ype of the.** co
9b80: 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 lumn currently u
9b90: 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
9ba0: 6e 2e 20 20 20 70 4c 61 73 74 20 69 73 20 74 68 n. pLast is th
9bb0: 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 e last token.**
9bc0: 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65 2e in the sequence.
9bd0: 20 20 55 73 65 20 74 68 69 73 20 69 6e 66 6f 72 Use this infor
9be0: 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 74 72 mation to constr
9bf0: 75 63 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 uct a string.**
9c00: 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
9c10: 65 20 74 79 70 65 6e 61 6d 65 20 6f 66 20 74 68 e typename of th
9c20: 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 74 6f e column and sto
9c30: 72 65 20 74 68 61 74 20 73 74 72 69 6e 67 0a 2a re that string.*
9c40: 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a * in zType..*/ .
9c50: 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 void sqlite3AddC
9c60: 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 20 olumnType(Parse
9c70: 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a *pParse, Token *
9c80: 70 54 79 70 65 29 7b 0a 20 20 54 61 62 6c 65 20 pType){. Table
9c90: 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 *p;. Column *pC
9ca0: 6f 6c 3b 0a 0a 20 20 70 20 3d 20 70 50 61 72 73 ol;.. p = pPars
9cb0: 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 e->pNewTable;.
9cc0: 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 if( p==0 || NEVE
9cd0: 52 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 72 R(p->nCol<1) ) r
9ce0: 65 74 75 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 eturn;. pCol =
9cf0: 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c &p->aCol[p->nCol
9d00: 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 -1];. assert( p
9d10: 43 6f 6c 2d 3e 7a 54 79 70 65 3d 3d 30 20 7c 7c Col->zType==0 ||
9d20: 20 43 4f 52 52 55 50 54 5f 44 42 20 29 3b 0a 20 CORRUPT_DB );.
9d30: 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
9d40: 50 61 72 73 65 2d 3e 64 62 2c 20 70 43 6f 6c 2d Parse->db, pCol-
9d50: 3e 7a 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d >zType);. pCol-
9d60: 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 >zType = sqlite3
9d70: 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 NameFromToken(pP
9d80: 61 72 73 65 2d 3e 64 62 2c 20 70 54 79 70 65 29 arse->db, pType)
9d90: 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 ;. pCol->affini
9da0: 74 79 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69 ty = sqlite3Affi
9db0: 6e 69 74 79 54 79 70 65 28 70 43 6f 6c 2d 3e 7a nityType(pCol->z
9dc0: 54 79 70 65 2c 20 26 70 43 6f 6c 2d 3e 73 7a 45 Type, &pCol->szE
9dd0: 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 st);.}../*.** Th
9de0: 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
9df0: 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 the default valu
9e00: 65 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 e for the most r
9e10: 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 63 6f ecently added co
9e20: 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 74 lumn.** of the t
9e30: 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 able currently u
9e40: 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
9e50: 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 n..**.** Default
9e60: 20 76 61 6c 75 65 20 65 78 70 72 65 73 73 69 6f value expressio
9e70: 6e 73 20 6d 75 73 74 20 62 65 20 63 6f 6e 73 74 ns must be const
9e80: 61 6e 74 2e 20 20 52 61 69 73 65 20 61 6e 20 65 ant. Raise an e
9e90: 78 63 65 70 74 69 6f 6e 20 69 66 20 74 68 69 73 xception if this
9ea0: 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65 20 63 .** is not the c
9eb0: 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ase..**.** This
9ec0: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
9ed0: 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 d by the parser
9ee0: 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 while in the mid
9ef0: 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e dle of.** parsin
9f00: 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 g a CREATE TABLE
9f10: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 76 statement..*/.v
9f20: 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 oid sqlite3AddDe
9f30: 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 faultValue(Parse
9f40: 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 53 70 *pParse, ExprSp
9f50: 61 6e 20 2a 70 53 70 61 6e 29 7b 0a 20 20 54 61 an *pSpan){. Ta
9f60: 62 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e ble *p;. Column
9f70: 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 *pCol;. sqlite
9f80: 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
9f90: 64 62 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 db;. p = pParse
9fa0: 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 ->pNewTable;. i
9fb0: 66 28 20 70 21 3d 30 20 29 7b 0a 20 20 20 20 70 f( p!=0 ){. p
9fc0: 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 5b Col = &(p->aCol[
9fd0: 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a 20 20 20 p->nCol-1]);.
9fe0: 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 if( !sqlite3Exp
9ff0: 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e rIsConstantOrFun
a000: 63 74 69 6f 6e 28 70 53 70 61 6e 2d 3e 70 45 78 ction(pSpan->pEx
a010: 70 72 2c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 pr, db->init.bus
a020: 79 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 y) ){. sqli
a030: 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
a040: 73 65 2c 20 22 64 65 66 61 75 6c 74 20 76 61 6c se, "default val
a050: 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 ue of column [%s
a060: 5d 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e ] is not constan
a070: 74 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 t",. pC
a080: 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ol->zName);.
a090: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
a0a0: 41 20 63 6f 70 79 20 6f 66 20 70 45 78 70 72 20 A copy of pExpr
a0b0: 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 is used instead
a0c0: 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 2c of the original,
a0d0: 20 61 73 20 70 45 78 70 72 20 63 6f 6e 74 61 69 as pExpr contai
a0e0: 6e 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 ns. ** toke
a0f0: 6e 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f ns that point to
a100: 20 76 6f 6c 61 74 69 6c 65 20 6d 65 6d 6f 72 79 volatile memory
a110: 2e 20 54 68 65 20 27 73 70 61 6e 27 20 6f 66 20 . The 'span' of
a120: 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 the expression.
a130: 20 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 ** is requi
a140: 72 65 64 20 62 79 20 70 72 61 67 6d 61 20 74 61 red by pragma ta
a150: 62 6c 65 5f 69 6e 66 6f 2e 0a 20 20 20 20 20 20 ble_info..
a160: 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
a170: 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
a180: 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 Col->pDflt);.
a190: 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d pCol->pDflt =
a1a0: 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 sqlite3ExprDup(
a1b0: 64 62 2c 20 70 53 70 61 6e 2d 3e 70 45 78 70 72 db, pSpan->pExpr
a1c0: 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 , EXPRDUP_REDUCE
a1d0: 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
a1e0: 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d DbFree(db, pCol-
a1f0: 3e 7a 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 >zDflt);. p
a200: 43 6f 6c 2d 3e 7a 44 66 6c 74 20 3d 20 73 71 6c Col->zDflt = sql
a210: 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 ite3DbStrNDup(db
a220: 2c 20 28 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e , (char*)pSpan->
a230: 7a 53 74 61 72 74 2c 0a 20 20 20 20 20 20 20 20 zStart,.
a240: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a250: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e (in
a260: 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d t)(pSpan->zEnd -
a270: 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 29 29 pSpan->zStart))
a280: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
a290: 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
a2a0: 64 62 2c 20 70 53 70 61 6e 2d 3e 70 45 78 70 72 db, pSpan->pExpr
a2b0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 69 );.}../*.** Desi
a2c0: 67 6e 61 74 65 20 74 68 65 20 50 52 49 4d 41 52 gnate the PRIMAR
a2d0: 59 20 4b 45 59 20 66 6f 72 20 74 68 65 20 74 61 Y KEY for the ta
a2e0: 62 6c 65 2e 20 20 70 4c 69 73 74 20 69 73 20 61 ble. pList is a
a2f0: 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 73 20 0a list of names .
a300: 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 68 ** of columns th
a310: 61 74 20 66 6f 72 6d 20 74 68 65 20 70 72 69 6d at form the prim
a320: 61 72 79 20 6b 65 79 2e 20 20 49 66 20 70 4c 69 ary key. If pLi
a330: 73 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e st is NULL, then
a340: 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 the.** most rec
a350: 65 6e 74 6c 79 20 61 64 64 65 64 20 63 6f 6c 75 ently added colu
a360: 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 mn of the table
a370: 69 73 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b is the primary k
a380: 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 61 62 6c ey..**.** A tabl
a390: 65 20 63 61 6e 20 68 61 76 65 20 61 74 20 6d 6f e can have at mo
a3a0: 73 74 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b st one primary k
a3b0: 65 79 2e 20 20 49 66 20 74 68 65 20 74 61 62 6c ey. If the tabl
a3c0: 65 20 61 6c 72 65 61 64 79 20 68 61 73 0a 2a 2a e already has.**
a3d0: 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 28 a primary key (
a3e0: 61 6e 64 20 74 68 69 73 20 69 73 20 74 68 65 20 and this is the
a3f0: 73 65 63 6f 6e 64 20 70 72 69 6d 61 72 79 20 6b second primary k
a400: 65 79 29 20 74 68 65 6e 20 63 72 65 61 74 65 20 ey) then create
a410: 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2a 0a an.** error..**.
a420: 2a 2a 20 49 66 20 74 68 65 20 50 52 49 4d 41 52 ** If the PRIMAR
a430: 59 20 4b 45 59 20 69 73 20 6f 6e 20 61 20 73 69 Y KEY is on a si
a440: 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 ngle column whos
a450: 65 20 64 61 74 61 74 79 70 65 20 69 73 20 49 4e e datatype is IN
a460: 54 45 47 45 52 2c 0a 2a 2a 20 74 68 65 6e 20 77 TEGER,.** then w
a470: 65 20 77 69 6c 6c 20 74 72 79 20 74 6f 20 75 73 e will try to us
a480: 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 61 73 e that column as
a490: 20 74 68 65 20 72 6f 77 69 64 2e 20 20 53 65 74 the rowid. Set
a4a0: 20 74 68 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 the Table.iPKey
a4b0: 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 .** field of the
a4c0: 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e table under con
a4d0: 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 struction to be
a4e0: 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
a4f0: 0a 2a 2a 20 49 4e 54 45 47 45 52 20 50 52 49 4d .** INTEGER PRIM
a500: 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2e 20 ARY KEY column.
a510: 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 Table.iPKey is
a520: 73 65 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65 set to -1 if the
a530: 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 49 4e 54 45 re is.** no INTE
a540: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2e GER PRIMARY KEY.
a550: 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6b 65 .**.** If the ke
a560: 79 20 69 73 20 6e 6f 74 20 61 6e 20 49 4e 54 45 y is not an INTE
a570: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c GER PRIMARY KEY,
a580: 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 75 then create a u
a590: 6e 69 71 75 65 0a 2a 2a 20 69 6e 64 65 78 20 66 nique.** index f
a5a0: 6f 72 20 74 68 65 20 6b 65 79 2e 20 20 4e 6f 20 or the key. No
a5b0: 69 6e 64 65 78 20 69 73 20 63 72 65 61 74 65 64 index is created
a5c0: 20 66 6f 72 20 49 4e 54 45 47 45 52 20 50 52 49 for INTEGER PRI
a5d0: 4d 41 52 59 20 4b 45 59 73 2e 0a 2a 2f 0a 76 6f MARY KEYs..*/.vo
a5e0: 69 64 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 id sqlite3AddPri
a5f0: 6d 61 72 79 4b 65 79 28 0a 20 20 50 61 72 73 65 maryKey(. Parse
a600: 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 *pParse, /*
a610: 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
a620: 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
a630: 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f List, /* List o
a640: 66 20 66 69 65 6c 64 20 6e 61 6d 65 73 20 74 6f f field names to
a650: 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 be indexed */.
a660: 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 int onError,
a670: 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f /* What to do
a680: 20 77 69 74 68 20 61 20 75 6e 69 71 75 65 6e 65 with a uniquene
a690: 73 73 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 ss conflict */.
a6a0: 20 69 6e 74 20 61 75 74 6f 49 6e 63 2c 20 20 20 int autoInc,
a6b0: 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
a6c0: 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 e AUTOINCREMENT
a6d0: 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 keyword is prese
a6e0: 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 nt */. int sort
a6f0: 4f 72 64 65 72 20 20 20 20 20 2f 2a 20 53 51 4c Order /* SQL
a700: 49 54 45 5f 53 4f 5f 41 53 43 20 6f 72 20 53 51 ITE_SO_ASC or SQ
a710: 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 2a 2f 0a LITE_SO_DESC */.
a720: 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 ){. Table *pTab
a730: 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 = pParse->pNewT
a740: 61 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 54 able;. char *zT
a750: 79 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 ype = 0;. int i
a760: 43 6f 6c 20 3d 20 2d 31 2c 20 69 3b 0a 20 20 69 Col = -1, i;. i
a770: 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 69 66 28 20 nt nTerm;. if(
a780: 70 54 61 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 pTab==0 || IN_DE
a790: 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74 CLARE_VTAB ) got
a7a0: 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 o primary_key_ex
a7b0: 69 74 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e it;. if( pTab->
a7c0: 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 48 61 tabFlags & TF_Ha
a7d0: 73 50 72 69 6d 61 72 79 4b 65 79 20 29 7b 0a 20 sPrimaryKey ){.
a7e0: 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
a7f0: 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 sg(pParse, .
a800: 20 20 22 74 61 62 6c 65 20 5c 22 25 73 5c 22 20 "table \"%s\"
a810: 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e has more than on
a820: 65 20 70 72 69 6d 61 72 79 20 6b 65 79 22 2c 20 e primary key",
a830: 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pTab->zName);.
a840: 20 20 67 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b goto primary_k
a850: 65 79 5f 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 ey_exit;. }. p
a860: 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d Tab->tabFlags |=
a870: 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 TF_HasPrimaryKe
a880: 79 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d y;. if( pList==
a890: 30 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 0 ){. iCol =
a8a0: 70 54 61 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a pTab->nCol - 1;.
a8b0: 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 pTab->aCol[i
a8c0: 43 6f 6c 5d 2e 63 6f 6c 46 6c 61 67 73 20 7c 3d Col].colFlags |=
a8d0: 20 43 4f 4c 46 4c 41 47 5f 50 52 49 4d 4b 45 59 COLFLAG_PRIMKEY
a8e0: 3b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54 ;. zType = pT
a8f0: 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a ab->aCol[iCol].z
a900: 54 79 70 65 3b 0a 20 20 20 20 6e 54 65 72 6d 20 Type;. nTerm
a910: 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 1;. }else{.
a920: 20 20 6e 54 65 72 6d 20 3d 20 70 4c 69 73 74 2d nTerm = pList-
a930: 3e 6e 45 78 70 72 3b 0a 20 20 20 20 66 6f 72 28 >nExpr;. for(
a940: 69 3d 30 3b 20 69 3c 6e 54 65 72 6d 3b 20 69 2b i=0; i<nTerm; i+
a950: 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a +){. Expr *
a960: 70 43 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 pCExpr = sqlite3
a970: 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 74 65 28 ExprSkipCollate(
a980: 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 pList->a[i].pExp
a990: 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 r);. assert
a9a0: 28 20 70 43 45 78 70 72 21 3d 30 20 29 3b 0a 20 ( pCExpr!=0 );.
a9b0: 20 20 20 20 20 69 66 28 20 70 43 45 78 70 72 2d if( pCExpr-
a9c0: 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 >op==TK_ID ){.
a9d0: 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
a9e0: 20 2a 7a 43 4e 61 6d 65 20 3d 20 70 43 45 78 70 *zCName = pCExp
a9f0: 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 r->u.zToken;.
aa00: 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b for(iCol=0;
aa10: 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c iCol<pTab->nCol
aa20: 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 ; iCol++){.
aa30: 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
aa40: 53 74 72 49 43 6d 70 28 7a 43 4e 61 6d 65 2c 20 StrICmp(zCName,
aa50: 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d pTab->aCol[iCol]
aa60: 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 .zName)==0 ){.
aa70: 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e pTab->
aa80: 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 63 6f 6c 46 6c aCol[iCol].colFl
aa90: 61 67 73 20 7c 3d 20 43 4f 4c 46 4c 41 47 5f 50 ags |= COLFLAG_P
aaa0: 52 49 4d 4b 45 59 3b 0a 20 20 20 20 20 20 20 20 RIMKEY;.
aab0: 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54 61 62 zType = pTab
aac0: 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 ->aCol[iCol].zTy
aad0: 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 pe;.
aae0: 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
aaf0: 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
ab00: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
ab10: 20 69 66 28 20 6e 54 65 72 6d 3d 3d 31 0a 20 20 if( nTerm==1.
ab20: 20 26 26 20 7a 54 79 70 65 20 26 26 20 73 71 6c && zType && sql
ab30: 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79 70 ite3StrICmp(zTyp
ab40: 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d 30 e, "INTEGER")==0
ab50: 0a 20 20 20 26 26 20 73 6f 72 74 4f 72 64 65 72 . && sortOrder
ab60: 21 3d 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 !=SQLITE_SO_DESC
ab70: 0a 20 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e . ){. pTab->
ab80: 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20 20 iPKey = iCol;.
ab90: 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 20 pTab->keyConf
aba0: 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 = (u8)onError;.
abb0: 20 20 20 61 73 73 65 72 74 28 20 61 75 74 6f 49 assert( autoI
abc0: 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63 nc==0 || autoInc
abd0: 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d ==1 );. pTab-
abe0: 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74 >tabFlags |= aut
abf0: 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72 oInc*TF_Autoincr
ac00: 65 6d 65 6e 74 3b 0a 20 20 20 20 69 66 28 20 70 ement;. if( p
ac10: 4c 69 73 74 20 29 20 70 50 61 72 73 65 2d 3e 69 List ) pParse->i
ac20: 50 6b 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4c PkSortOrder = pL
ac30: 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 ist->a[0].sortOr
ac40: 64 65 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 der;. }else if(
ac50: 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69 66 6e autoInc ){.#ifn
ac60: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
ac70: 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 AUTOINCREMENT.
ac80: 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
ac90: 67 28 70 50 61 72 73 65 2c 20 22 41 55 54 4f 49 g(pParse, "AUTOI
aca0: 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e 6c 79 NCREMENT is only
acb0: 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e 20 22 allowed on an "
acc0: 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47 45 52 . "INTEGER
acd0: 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29 3b 0a PRIMARY KEY");.
ace0: 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a #endif. }else{.
acf0: 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 Index *p;.
ad00: 20 20 70 20 3d 20 73 71 6c 69 74 65 33 43 72 65 p = sqlite3Cre
ad10: 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c ateIndex(pParse,
ad20: 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c 0, 0, 0, pList,
ad30: 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 0a 20 20 20 onError, 0,.
ad40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad50: 20 20 20 20 20 20 20 20 30 2c 20 73 6f 72 74 4f 0, sortO
ad60: 72 64 65 72 2c 20 30 29 3b 0a 20 20 20 20 69 66 rder, 0);. if
ad70: 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ( p ){. p->
ad80: 69 64 78 54 79 70 65 20 3d 20 53 51 4c 49 54 45 idxType = SQLITE
ad90: 5f 49 44 58 54 59 50 45 5f 50 52 49 4d 41 52 59 _IDXTYPE_PRIMARY
ada0: 4b 45 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 KEY;. }. p
adb0: 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 70 List = 0;. }..p
adc0: 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3a rimary_key_exit:
add0: 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 . sqlite3ExprLi
ade0: 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d stDelete(pParse-
adf0: 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 >db, pList);. r
ae00: 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eturn;.}../*.**
ae10: 41 64 64 20 61 20 6e 65 77 20 43 48 45 43 4b 20 Add a new CHECK
ae20: 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 74 68 constraint to th
ae30: 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c e table currentl
ae40: 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 y under construc
ae50: 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 tion..*/.void sq
ae60: 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e lite3AddCheckCon
ae70: 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72 73 65 straint(. Parse
ae80: 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 *pParse, /*
ae90: 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
aea0: 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 68 65 63 */. Expr *pChec
aeb0: 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20 63 68 kExpr /* The ch
aec0: 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e 20 2a eck expression *
aed0: 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c /.){.#ifndef SQL
aee0: 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 ITE_OMIT_CHECK.
aef0: 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 Table *pTab = p
af00: 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
af10: 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
af20: 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
af30: 69 66 28 20 70 54 61 62 20 26 26 20 21 49 4e 5f if( pTab && !IN_
af40: 44 45 43 4c 41 52 45 5f 56 54 41 42 0a 20 20 20 DECLARE_VTAB.
af50: 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 && !sqlite3Btree
af60: 49 73 52 65 61 64 6f 6e 6c 79 28 64 62 2d 3e 61 IsReadonly(db->a
af70: 44 62 5b 64 62 2d 3e 69 6e 69 74 2e 69 44 62 5d Db[db->init.iDb]
af80: 2e 70 42 74 29 0a 20 20 29 7b 0a 20 20 20 20 70 .pBt). ){. p
af90: 54 61 62 2d 3e 70 43 68 65 63 6b 20 3d 20 73 71 Tab->pCheck = sq
afa0: 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 lite3ExprListApp
afb0: 65 6e 64 28 70 50 61 72 73 65 2c 20 70 54 61 62 end(pParse, pTab
afc0: 2d 3e 70 43 68 65 63 6b 2c 20 70 43 68 65 63 6b ->pCheck, pCheck
afd0: 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20 70 Expr);. if( p
afe0: 50 61 72 73 65 2d 3e 63 6f 6e 73 74 72 61 69 6e Parse->constrain
aff0: 74 4e 61 6d 65 2e 6e 20 29 7b 0a 20 20 20 20 20 tName.n ){.
b000: 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
b010: 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 SetName(pParse,
b020: 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 26 70 pTab->pCheck, &p
b030: 50 61 72 73 65 2d 3e 63 6f 6e 73 74 72 61 69 6e Parse->constrain
b040: 74 4e 61 6d 65 2c 20 31 29 3b 0a 20 20 20 20 7d tName, 1);. }
b050: 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a . }else.#endif.
b060: 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
b070: 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 xprDelete(pParse
b080: 2d 3e 64 62 2c 20 70 43 68 65 63 6b 45 78 70 72 ->db, pCheckExpr
b090: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
b0a0: 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f Set the collatio
b0b0: 6e 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 74 68 n function of th
b0c0: 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
b0d0: 70 61 72 73 65 64 20 74 61 62 6c 65 20 63 6f 6c parsed table col
b0e0: 75 6d 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 43 6f umn.** to the Co
b0f0: 6c 6c 53 65 71 20 67 69 76 65 6e 2e 0a 2a 2f 0a llSeq given..*/.
b100: 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 void sqlite3AddC
b110: 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 72 73 65 ollateType(Parse
b120: 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 *pParse, Token
b130: 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 54 61 62 6c *pToken){. Tabl
b140: 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 e *p;. int i;.
b150: 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 char *zColl;
b160: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
b170: 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 quoted name of c
b180: 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
b190: 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a e */. sqlite3 *
b1a0: 64 62 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d 20 db;.. if( (p =
b1b0: 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
b1c0: 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a e)==0 ) return;.
b1d0: 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b i = p->nCol-1;
b1e0: 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e . db = pParse->
b1f0: 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 db;. zColl = sq
b200: 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
b210: 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a en(db, pToken);.
b220: 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72 if( !zColl ) r
b230: 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 73 71 eturn;.. if( sq
b240: 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 lite3LocateCollS
b250: 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c eq(pParse, zColl
b260: 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a ) ){. Index *
b270: 70 49 64 78 3b 0a 20 20 20 20 73 71 6c 69 74 65 pIdx;. sqlite
b280: 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 3DbFree(db, p->a
b290: 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 29 3b 0a 20 Col[i].zColl);.
b2a0: 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 p->aCol[i].zC
b2b0: 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 0a oll = zColl;. .
b2c0: 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f /* If the co
b2d0: 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 64 lumn is declared
b2e0: 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d as "<name> PRIM
b2f0: 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20 ARY KEY COLLATE
b300: 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a 20 <type>",. **
b310: 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d 61 then an index ma
b320: 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65 61 y have been crea
b330: 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c 75 ted on this colu
b340: 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20 mn before the.
b350: 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 ** collation t
b360: 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20 43 ype was added. C
b370: 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20 69 orrect this if i
b380: 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 t is the case..
b390: 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 49 */. for(pI
b3a0: 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49 dx=p->pIndex; pI
b3b0: 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 dx; pIdx=pIdx->p
b3c0: 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 Next){. ass
b3d0: 65 72 74 28 20 70 49 64 78 2d 3e 6e 4b 65 79 43 ert( pIdx->nKeyC
b3e0: 6f 6c 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 ol==1 );. i
b3f0: 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d f( pIdx->aiColum
b400: 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 20 n[0]==i ){.
b410: 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b pIdx->azColl[
b420: 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 0] = p->aCol[i].
b430: 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 zColl;. }.
b440: 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
b450: 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
b460: 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a db, zColl);. }.
b470: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
b480: 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
b490: 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 he collation seq
b4a0: 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 61 uence for databa
b4b0: 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a 2a se native text.*
b4c0: 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e 74 * encoding ident
b4d0: 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74 72 ified by the str
b4e0: 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 ing zName, lengt
b4f0: 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 h nName..**.** I
b500: 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
b510: 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
b520: 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 ce is not availa
b530: 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61 69 ble, or not avai
b540: 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 lable.** in the
b550: 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 database native
b560: 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63 6f encoding, the co
b570: 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 llation factory
b580: 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a is invoked to.**
b590: 20 72 65 71 75 65 73 74 20 69 74 2e 20 49 66 20 request it. If
b5a0: 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 the collation fa
b5b0: 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 73 ctory does not s
b5c0: 75 70 70 6c 79 20 73 75 63 68 20 61 20 73 65 71 upply such a seq
b5d0: 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74 68 uence,.** and th
b5e0: 65 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 76 e sequence is av
b5f0: 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 ailable in anoth
b600: 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 er text encoding
b610: 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a 2a , then that is.*
b620: 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 * returned inste
b630: 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 ad..**.** If no
b640: 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 versions of the
b650: 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 requested collat
b660: 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61 72 ions sequence ar
b670: 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 0a e available, or.
b680: 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f 72 ** another error
b690: 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 occurs, NULL is
b6a0: 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e returned and an
b6b0: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 error message w
b6c0: 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 70 ritten into.** p
b6d0: 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 Parse..**.** Thi
b6e0: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 77 s routine is a w
b6f0: 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 rapper around sq
b700: 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 lite3FindCollSeq
b710: 28 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e (). This routin
b720: 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 65 e.** invokes the
b730: 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f collation facto
b740: 72 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 ry if the named
b750: 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 collation cannot
b760: 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e 64 be found.** and
b770: 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 generates an er
b780: 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a ror message..**.
b790: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c ** See also: sql
b7a0: 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 ite3FindCollSeq(
b7b0: 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c ), sqlite3GetCol
b7c0: 6c 53 65 71 28 29 0a 2a 2f 0a 43 6f 6c 6c 53 65 lSeq().*/.CollSe
b7d0: 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 q *sqlite3Locate
b7e0: 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 CollSeq(Parse *p
b7f0: 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 Parse, const cha
b800: 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 73 71 6c r *zName){. sql
b810: 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
b820: 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 20 e->db;. u8 enc
b830: 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 20 = ENC(db);. u8
b840: 69 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e 69 initbusy = db->i
b850: 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c 6c nit.busy;. Coll
b860: 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 70 Seq *pColl;.. p
b870: 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 Coll = sqlite3Fi
b880: 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e ndCollSeq(db, en
b890: 63 2c 20 7a 4e 61 6d 65 2c 20 69 6e 69 74 62 75 c, zName, initbu
b8a0: 73 79 29 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 sy);. if( !init
b8b0: 62 75 73 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 busy && (!pColl
b8c0: 7c 7c 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 || !pColl->xCmp)
b8d0: 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 ){. pColl =
b8e0: 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 sqlite3GetCollSe
b8f0: 71 28 70 50 61 72 73 65 2c 20 65 6e 63 2c 20 70 q(pParse, enc, p
b900: 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 Coll, zName);.
b910: 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c }.. return pCol
b920: 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e l;.}.../*.** Gen
b930: 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 erate code that
b940: 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20 74 will increment t
b950: 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 he schema cookie
b960: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 68 65 ..**.** The sche
b970: 6d 61 20 63 6f 6f 6b 69 65 20 69 73 20 75 73 65 ma cookie is use
b980: 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 d to determine w
b990: 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20 66 hen the schema f
b9a0: 6f 72 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 or the.** databa
b9b0: 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41 66 74 se changes. Aft
b9c0: 65 72 20 65 61 63 68 20 73 63 68 65 6d 61 20 63 er each schema c
b9d0: 68 61 6e 67 65 2c 20 74 68 65 20 63 6f 6f 6b 69 hange, the cooki
b9e0: 65 20 76 61 6c 75 65 0a 2a 2a 20 63 68 61 6e 67 e value.** chang
b9f0: 65 73 2e 20 20 57 68 65 6e 20 61 20 70 72 6f 63 es. When a proc
ba00: 65 73 73 20 66 69 72 73 74 20 72 65 61 64 73 20 ess first reads
ba10: 74 68 65 20 73 63 68 65 6d 61 20 69 74 20 72 65 the schema it re
ba20: 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6f cords the.** coo
ba30: 6b 69 65 2e 20 20 54 68 65 72 65 61 66 74 65 72 kie. Thereafter
ba40: 2c 20 77 68 65 6e 65 76 65 72 20 69 74 20 67 6f , whenever it go
ba50: 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 es to access the
ba60: 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 69 74 database,.** it
ba70: 20 63 68 65 63 6b 73 20 74 68 65 20 63 6f 6f 6b checks the cook
ba80: 69 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ie to make sure
ba90: 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 6e the schema has n
baa0: 6f 74 20 63 68 61 6e 67 65 64 0a 2a 2a 20 73 69 ot changed.** si
bab0: 6e 63 65 20 69 74 20 77 61 73 20 6c 61 73 74 20 nce it was last
bac0: 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 read..**.** This
bad0: 20 70 6c 61 6e 20 69 73 20 6e 6f 74 20 63 6f 6d plan is not com
bae0: 70 6c 65 74 65 6c 79 20 62 75 6c 6c 65 74 2d 70 pletely bullet-p
baf0: 72 6f 6f 66 2e 20 20 49 74 20 69 73 20 70 6f 73 roof. It is pos
bb00: 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 68 65 sible for.** the
bb10: 20 73 63 68 65 6d 61 20 74 6f 20 63 68 61 6e 67 schema to chang
bb20: 65 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 e multiple times
bb30: 20 61 6e 64 20 66 6f 72 20 74 68 65 20 63 6f 6f and for the coo
bb40: 6b 69 65 20 74 6f 20 62 65 0a 2a 2a 20 73 65 74 kie to be.** set
bb50: 20 62 61 63 6b 20 74 6f 20 70 72 69 6f 72 20 76 back to prior v
bb60: 61 6c 75 65 2e 20 20 42 75 74 20 73 63 68 65 6d alue. But schem
bb70: 61 20 63 68 61 6e 67 65 73 20 61 72 65 20 69 6e a changes are in
bb80: 66 72 65 71 75 65 6e 74 0a 2a 2a 20 61 6e 64 20 frequent.** and
bb90: 74 68 65 20 70 72 6f 62 61 62 69 6c 69 74 79 20 the probability
bba0: 6f 66 20 68 69 74 74 69 6e 67 20 74 68 65 20 73 of hitting the s
bbb0: 61 6d 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 ame cookie value
bbc0: 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 31 20 63 68 is only.** 1 ch
bbd0: 61 6e 63 65 20 69 6e 20 32 5e 33 32 2e 20 20 53 ance in 2^32. S
bbe0: 6f 20 77 65 27 72 65 20 73 61 66 65 20 65 6e 6f o we're safe eno
bbf0: 75 67 68 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ugh..*/.void sql
bc00: 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 ite3ChangeCookie
bc10: 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
bc20: 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74 20 int iDb){. int
bc30: 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r1 = sqlite3GetT
bc40: 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
bc50: 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
bc60: 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56 64 pParse->db;. Vd
bc70: 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e be *v = pParse->
bc80: 70 56 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 pVdbe;. assert(
bc90: 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 sqlite3SchemaMu
bca0: 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c texHeld(db, iDb,
bcb0: 20 30 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 0) );. sqlite3
bcc0: 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
bcd0: 5f 49 6e 74 65 67 65 72 2c 20 64 62 2d 3e 61 44 _Integer, db->aD
bce0: 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e b[iDb].pSchema->
bcf0: 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 2b 31 2c schema_cookie+1,
bd00: 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 r1);. sqlite3V
bd10: 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
bd20: 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 SetCookie, iDb,
bd30: 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 BTREE_SCHEMA_VER
bd40: 53 49 4f 4e 2c 20 72 31 29 3b 0a 20 20 73 71 6c SION, r1);. sql
bd50: 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
bd60: 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a eg(pParse, r1);.
bd70: 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 75 72 65 }../*.** Measure
bd80: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
bd90: 68 61 72 61 63 74 65 72 73 20 6e 65 65 64 65 64 haracters needed
bda0: 20 74 6f 20 6f 75 74 70 75 74 20 74 68 65 20 67 to output the g
bdb0: 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 69 66 69 iven.** identifi
bdc0: 65 72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 er. The number
bdd0: 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 returned include
bde0: 73 20 61 6e 79 20 71 75 6f 74 65 73 20 75 73 65 s any quotes use
bdf0: 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73 20 6e 6f d.** but does no
be00: 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 t include the nu
be10: 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a ll terminator..*
be20: 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74 *.** The estimat
be30: 65 20 69 73 20 63 6f 6e 73 65 72 76 61 74 69 76 e is conservativ
be40: 65 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 20 e. It might be
be50: 6c 61 72 67 65 72 20 74 68 61 74 20 77 68 61 74 larger that what
be60: 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e 65 is.** really ne
be70: 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eded..*/.static
be80: 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 int identLength(
be90: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a const char *z){.
bea0: 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28 6e int n;. for(n
beb0: 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b 2b =0; *z; n++, z++
bec0: 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 ){. if( *z=='
bed0: 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a 20 20 7d "' ){ n++; }. }
bee0: 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 32 3b . return n + 2;
bef0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 .}../*.** The fi
bf00: 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 rst parameter is
bf10: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
bf20: 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 output buffer.
bf30: 54 68 65 20 73 65 63 6f 6e 64 20 0a 2a 2a 20 70 The second .** p
bf40: 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f arameter is a po
bf50: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 inter to an inte
bf60: 67 65 72 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ger that contain
bf70: 73 20 74 68 65 20 6f 66 66 73 65 74 20 61 74 0a s the offset at.
bf80: 2a 2a 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 ** which to writ
bf90: 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 e into the outpu
bfa0: 74 20 62 75 66 66 65 72 2e 20 54 68 69 73 20 66 t buffer. This f
bfb0: 75 6e 63 74 69 6f 6e 20 63 6f 70 69 65 73 20 74 unction copies t
bfc0: 68 65 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e he.** nul-termin
bfd0: 61 74 65 64 20 73 74 72 69 6e 67 20 70 6f 69 6e ated string poin
bfe0: 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 74 68 ted to by the th
bff0: 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 7a ird parameter, z
c000: 53 69 67 6e 65 64 49 64 65 6e 74 2c 0a 2a 2a 20 SignedIdent,.**
c010: 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 65 64 to the specified
c020: 20 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20 62 offset in the b
c030: 75 66 66 65 72 20 61 6e 64 20 75 70 64 61 74 65 uffer and update
c040: 73 20 2a 70 49 64 78 20 74 6f 20 72 65 66 65 72 s *pIdx to refer
c050: 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 .** to the first
c060: 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 byte after the
c070: 6c 61 73 74 20 62 79 74 65 20 77 72 69 74 74 65 last byte writte
c080: 6e 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 n before returni
c090: 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 ng..** .** If th
c0a0: 65 20 73 74 72 69 6e 67 20 7a 53 69 67 6e 65 64 e string zSigned
c0b0: 49 64 65 6e 74 20 63 6f 6e 73 69 73 74 73 20 65 Ident consists e
c0c0: 6e 74 69 72 65 6c 79 20 6f 66 20 61 6c 70 68 61 ntirely of alpha
c0d0: 2d 6e 75 6d 65 72 69 63 0a 2a 2a 20 63 68 61 72 -numeric.** char
c0e0: 61 63 74 65 72 73 2c 20 64 6f 65 73 20 6e 6f 74 acters, does not
c0f0: 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 64 69 begin with a di
c100: 67 69 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 git and is not a
c110: 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64 2c 0a 2a n SQL keyword,.*
c120: 2a 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70 * then it is cop
c130: 69 65 64 20 74 6f 20 74 68 65 20 6f 75 74 70 75 ied to the outpu
c140: 74 20 62 75 66 66 65 72 20 65 78 61 63 74 6c 79 t buffer exactly
c150: 20 61 73 20 69 74 20 69 73 2e 20 4f 74 68 65 72 as it is. Other
c160: 77 69 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 71 wise,.** it is q
c170: 75 6f 74 65 64 20 75 73 69 6e 67 20 64 6f 75 62 uoted using doub
c180: 6c 65 2d 71 75 6f 74 65 73 2e 0a 2a 2f 0a 73 74 le-quotes..*/.st
c190: 61 74 69 63 20 76 6f 69 64 20 69 64 65 6e 74 50 atic void identP
c1a0: 75 74 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 ut(char *z, int
c1b0: 2a 70 49 64 78 2c 20 63 68 61 72 20 2a 7a 53 69 *pIdx, char *zSi
c1c0: 67 6e 65 64 49 64 65 6e 74 29 7b 0a 20 20 75 6e gnedIdent){. un
c1d0: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 64 signed char *zId
c1e0: 65 6e 74 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 ent = (unsigned
c1f0: 63 68 61 72 2a 29 7a 53 69 67 6e 65 64 49 64 65 char*)zSignedIde
c200: 6e 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 nt;. int i, j,
c210: 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 69 20 3d needQuote;. i =
c220: 20 2a 70 49 64 78 3b 0a 0a 20 20 66 6f 72 28 6a *pIdx;.. for(j
c230: 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a =0; zIdent[j]; j
c240: 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 ++){. if( !sq
c250: 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 7a 49 64 lite3Isalnum(zId
c260: 65 6e 74 5b 6a 5d 29 20 26 26 20 7a 49 64 65 6e ent[j]) && zIden
c270: 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 62 72 65 61 t[j]!='_' ) brea
c280: 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 64 51 75 6f k;. }. needQuo
c290: 74 65 20 3d 20 73 71 6c 69 74 65 33 49 73 64 69 te = sqlite3Isdi
c2a0: 67 69 74 28 7a 49 64 65 6e 74 5b 30 5d 29 0a 20 git(zIdent[0]).
c2b0: 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 || sq
c2c0: 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 lite3KeywordCode
c2d0: 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f (zIdent, j)!=TK_
c2e0: 49 44 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c ID. |
c2f0: 7c 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d 30 0a 20 | zIdent[j]!=0.
c300: 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6a 3d || j=
c310: 3d 30 3b 0a 0a 20 20 69 66 28 20 6e 65 65 64 51 =0;.. if( needQ
c320: 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 uote ) z[i++] =
c330: 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 '"';. for(j=0;
c340: 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b zIdent[j]; j++){
c350: 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 . z[i++] = zI
c360: 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 dent[j];. if(
c370: 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20 zIdent[j]=='"'
c380: 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a ) z[i++] = '"';.
c390: 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51 75 }. if( needQu
c3a0: 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 ote ) z[i++] = '
c3b0: 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b 0a "';. z[i] = 0;.
c3c0: 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a 0a *pIdx = i;.}..
c3d0: 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 /*.** Generate a
c3e0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
c3f0: 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72 69 atement appropri
c400: 61 74 65 20 66 6f 72 20 74 68 65 20 67 69 76 65 ate for the give
c410: 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65 6d n.** table. Mem
c420: 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ory to hold the
c430: 74 65 78 74 20 6f 66 20 74 68 65 20 73 74 61 74 text of the stat
c440: 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e 65 ement is obtaine
c450: 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 d.** from sqlite
c460: 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 Malloc() and mus
c470: 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68 t be freed by th
c480: 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
c490: 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 on..*/.static ch
c4a0: 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65 53 ar *createTableS
c4b0: 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c tmt(sqlite3 *db,
c4c0: 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e Table *p){. in
c4d0: 74 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 t i, k, n;. cha
c4e0: 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 r *zStmt;. char
c4f0: 20 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 *zSep, *zSep2,
c500: 2a 7a 45 6e 64 3b 0a 20 20 43 6f 6c 75 6d 6e 20 *zEnd;. Column
c510: 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b 0a *pCol;. n = 0;.
c520: 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d 3e for(pCol = p->
c530: 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e aCol, i=0; i<p->
c540: 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b nCol; i++, pCol+
c550: 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64 65 +){. n += ide
c560: 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e 7a ntLength(pCol->z
c570: 4e 61 6d 65 29 20 2b 20 35 3b 0a 20 20 7d 0a 20 Name) + 5;. }.
c580: 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 n += identLengt
c590: 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 h(p->zName);. i
c5a0: 66 28 20 6e 3c 35 30 20 29 7b 20 0a 20 20 20 20 f( n<50 ){ .
c5b0: 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a zSep = "";. z
c5c0: 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20 Sep2 = ",";.
c5d0: 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65 zEnd = ")";. }e
c5e0: 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 lse{. zSep =
c5f0: 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65 70 "\n ";. zSep
c600: 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20 2 = ",\n ";.
c610: 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20 zEnd = "\n)";.
c620: 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36 }. n += 35 + 6
c630: 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d *p->nCol;. zStm
c640: 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
c650: 6c 6f 63 52 61 77 28 30 2c 20 6e 29 3b 0a 20 20 locRaw(0, n);.
c660: 69 66 28 20 7a 53 74 6d 74 3d 3d 30 20 29 7b 0a if( zStmt==0 ){.
c670: 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
c680: 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 iled = 1;. re
c690: 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 turn 0;. }. sq
c6a0: 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
c6b0: 2c 20 7a 53 74 6d 74 2c 20 22 43 52 45 41 54 45 , zStmt, "CREATE
c6c0: 20 54 41 42 4c 45 20 22 29 3b 0a 20 20 6b 20 3d TABLE ");. k =
c6d0: 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
c6e0: 28 7a 53 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 (zStmt);. ident
c6f0: 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 Put(zStmt, &k, p
c700: 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7a 53 74 6d ->zName);. zStm
c710: 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 t[k++] = '(';.
c720: 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c for(pCol=p->aCol
c730: 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c , i=0; i<p->nCol
c740: 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a ; i++, pCol++){.
c750: 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
c760: 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a char * const az
c770: 54 79 70 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 Type[] = {.
c780: 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 /* SQLITE_AFF
c790: 5f 42 4c 4f 42 20 20 20 20 2a 2f 20 22 22 2c 0a _BLOB */ "",.
c7a0: 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 /* SQLIT
c7b0: 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 2a 2f E_AFF_TEXT */
c7c0: 20 22 20 54 45 58 54 22 2c 0a 20 20 20 20 20 20 " TEXT",.
c7d0: 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f /* SQLITE_AFF_
c7e0: 4e 55 4d 45 52 49 43 20 2a 2f 20 22 20 4e 55 4d NUMERIC */ " NUM
c7f0: 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 51 ",. /* SQ
c800: 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
c810: 20 2a 2f 20 22 20 49 4e 54 22 2c 0a 20 20 20 20 */ " INT",.
c820: 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 /* SQLITE_AF
c830: 46 5f 52 45 41 4c 20 20 20 20 2a 2f 20 22 20 52 F_REAL */ " R
c840: 45 41 4c 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20 EAL". };.
c850: 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 63 6f 6e int len;. con
c860: 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 0a st char *zType;.
c870: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
c880: 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d rintf(n-k, &zStm
c890: 74 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20 t[k], zSep);.
c8a0: 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 k += sqlite3Str
c8b0: 6c 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b 5d 29 len30(&zStmt[k])
c8c0: 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a 53 65 ;. zSep = zSe
c8d0: 70 32 3b 0a 20 20 20 20 69 64 65 6e 74 50 75 74 p2;. identPut
c8e0: 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43 6f 6c (zStmt, &k, pCol
c8f0: 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 61 73 ->zName);. as
c900: 73 65 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 sert( pCol->affi
c910: 6e 69 74 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f nity-SQLITE_AFF_
c920: 42 4c 4f 42 20 3e 3d 20 30 20 29 3b 0a 20 20 20 BLOB >= 0 );.
c930: 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 61 assert( pCol->a
c940: 66 66 69 6e 69 74 79 2d 53 51 4c 49 54 45 5f 41 ffinity-SQLITE_A
c950: 46 46 5f 42 4c 4f 42 20 3c 20 41 72 72 61 79 53 FF_BLOB < ArrayS
c960: 69 7a 65 28 61 7a 54 79 70 65 29 20 29 3b 0a 20 ize(azType) );.
c970: 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f testcase( pCo
c980: 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c l->affinity==SQL
c990: 49 54 45 5f 41 46 46 5f 42 4c 4f 42 20 29 3b 0a ITE_AFF_BLOB );.
c9a0: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 testcase( pC
c9b0: 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 ol->affinity==SQ
c9c0: 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 3b LITE_AFF_TEXT );
c9d0: 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 . testcase( p
c9e0: 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 Col->affinity==S
c9f0: 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
ca00: 43 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 C );. testcas
ca10: 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 e( pCol->affinit
ca20: 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e y==SQLITE_AFF_IN
ca30: 54 45 47 45 52 20 29 3b 0a 20 20 20 20 74 65 73 TEGER );. tes
ca40: 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 tcase( pCol->aff
ca50: 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 inity==SQLITE_AF
ca60: 46 5f 52 45 41 4c 20 29 3b 0a 20 20 20 20 0a 20 F_REAL );. .
ca70: 20 20 20 7a 54 79 70 65 20 3d 20 61 7a 54 79 70 zType = azTyp
ca80: 65 5b 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 e[pCol->affinity
ca90: 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c - SQLITE_AFF_BL
caa0: 4f 42 5d 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 OB];. len = s
cab0: 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
cac0: 54 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72 Type);. asser
cad0: 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 t( pCol->affinit
cae0: 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c y==SQLITE_AFF_BL
caf0: 4f 42 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 OB .
cb00: 7c 7c 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 || pCol->affinit
cb10: 79 3d 3d 73 71 6c 69 74 65 33 41 66 66 69 6e 69 y==sqlite3Affini
cb20: 74 79 54 79 70 65 28 7a 54 79 70 65 2c 20 30 29 tyType(zType, 0)
cb30: 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 );. memcpy(&
cb40: 7a 53 74 6d 74 5b 6b 5d 2c 20 7a 54 79 70 65 2c zStmt[k], zType,
cb50: 20 6c 65 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 len);. k +=
cb60: 6c 65 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 len;. assert(
cb70: 20 6b 3c 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 73 k<=n );. }. s
cb80: 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
cb90: 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 n-k, &zStmt[k],
cba0: 22 25 73 22 2c 20 7a 45 6e 64 29 3b 0a 20 20 72 "%s", zEnd);. r
cbb0: 65 74 75 72 6e 20 7a 53 74 6d 74 3b 0a 7d 0a 0a eturn zStmt;.}..
cbc0: 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 61 6e 20 /*.** Resize an
cbd0: 49 6e 64 65 78 20 6f 62 6a 65 63 74 20 74 6f 20 Index object to
cbe0: 68 6f 6c 64 20 4e 20 63 6f 6c 75 6d 6e 73 20 74 hold N columns t
cbf0: 6f 74 61 6c 2e 20 20 52 65 74 75 72 6e 20 53 51 otal. Return SQ
cc00: 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 LITE_OK.** on su
cc10: 63 63 65 73 73 20 61 6e 64 20 53 51 4c 49 54 45 ccess and SQLITE
cc20: 5f 4e 4f 4d 45 4d 20 6f 6e 20 61 6e 20 4f 4f 4d _NOMEM on an OOM
cc30: 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 error..*/.stati
cc40: 63 20 69 6e 74 20 72 65 73 69 7a 65 49 6e 64 65 c int resizeInde
cc50: 78 4f 62 6a 65 63 74 28 73 71 6c 69 74 65 33 20 xObject(sqlite3
cc60: 2a 64 62 2c 20 49 6e 64 65 78 20 2a 70 49 64 78 *db, Index *pIdx
cc70: 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 63 68 61 72 , int N){. char
cc80: 20 2a 7a 45 78 74 72 61 3b 0a 20 20 69 6e 74 20 *zExtra;. int
cc90: 6e 42 79 74 65 3b 0a 20 20 69 66 28 20 70 49 64 nByte;. if( pId
cca0: 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 4e 20 29 20 x->nColumn>=N )
ccb0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
ccc0: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 64 78 ;. assert( pIdx
ccd0: 2d 3e 69 73 52 65 73 69 7a 65 64 3d 3d 30 20 29 ->isResized==0 )
cce0: 3b 0a 20 20 6e 42 79 74 65 20 3d 20 28 73 69 7a ;. nByte = (siz
ccf0: 65 6f 66 28 63 68 61 72 2a 29 20 2b 20 73 69 7a eof(char*) + siz
cd00: 65 6f 66 28 69 31 36 29 20 2b 20 31 29 2a 4e 3b eof(i16) + 1)*N;
cd10: 0a 20 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 . zExtra = sqli
cd20: 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
cd30: 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 db, nByte);. if
cd40: 28 20 7a 45 78 74 72 61 3d 3d 30 20 29 20 72 65 ( zExtra==0 ) re
cd50: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
cd60: 4d 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 45 78 74 M;. memcpy(zExt
cd70: 72 61 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c ra, pIdx->azColl
cd80: 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a , sizeof(char*)*
cd90: 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a pIdx->nColumn);.
cda0: 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 20 3d pIdx->azColl =
cdb0: 20 28 63 68 61 72 2a 2a 29 7a 45 78 74 72 61 3b (char**)zExtra;
cdc0: 0a 20 20 7a 45 78 74 72 61 20 2b 3d 20 73 69 7a . zExtra += siz
cdd0: 65 6f 66 28 63 68 61 72 2a 29 2a 4e 3b 0a 20 20 eof(char*)*N;.
cde0: 6d 65 6d 63 70 79 28 7a 45 78 74 72 61 2c 20 70 memcpy(zExtra, p
cdf0: 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 73 Idx->aiColumn, s
ce00: 69 7a 65 6f 66 28 69 31 36 29 2a 70 49 64 78 2d izeof(i16)*pIdx-
ce10: 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 70 49 64 >nColumn);. pId
ce20: 78 2d 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 x->aiColumn = (i
ce30: 31 36 2a 29 7a 45 78 74 72 61 3b 0a 20 20 7a 45 16*)zExtra;. zE
ce40: 78 74 72 61 20 2b 3d 20 73 69 7a 65 6f 66 28 69 xtra += sizeof(i
ce50: 31 36 29 2a 4e 3b 0a 20 20 6d 65 6d 63 70 79 28 16)*N;. memcpy(
ce60: 7a 45 78 74 72 61 2c 20 70 49 64 78 2d 3e 61 53 zExtra, pIdx->aS
ce70: 6f 72 74 4f 72 64 65 72 2c 20 70 49 64 78 2d 3e ortOrder, pIdx->
ce80: 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 70 49 64 78 nColumn);. pIdx
ce90: 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 ->aSortOrder = (
cea0: 75 38 2a 29 7a 45 78 74 72 61 3b 0a 20 20 70 49 u8*)zExtra;. pI
ceb0: 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 4e 3b dx->nColumn = N;
cec0: 0a 20 20 70 49 64 78 2d 3e 69 73 52 65 73 69 7a . pIdx->isResiz
ced0: 65 64 20 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e ed = 1;. return
cee0: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
cef0: 2a 0a 2a 2a 20 45 73 74 69 6d 61 74 65 20 74 68 *.** Estimate th
cf00: 65 20 74 6f 74 61 6c 20 72 6f 77 20 77 69 64 74 e total row widt
cf10: 68 20 66 6f 72 20 61 20 74 61 62 6c 65 2e 0a 2a h for a table..*
cf20: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 73 /.static void es
cf30: 74 69 6d 61 74 65 54 61 62 6c 65 57 69 64 74 68 timateTableWidth
cf40: 28 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 (Table *pTab){.
cf50: 20 75 6e 73 69 67 6e 65 64 20 77 54 61 62 6c 65 unsigned wTable
cf60: 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 43 6f = 0;. const Co
cf70: 6c 75 6d 6e 20 2a 70 54 61 62 43 6f 6c 3b 0a 20 lumn *pTabCol;.
cf80: 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
cf90: 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 pTab->nCol, pTab
cfa0: 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 Col=pTab->aCol;
cfb0: 69 3e 30 3b 20 69 2d 2d 2c 20 70 54 61 62 43 6f i>0; i--, pTabCo
cfc0: 6c 2b 2b 29 7b 0a 20 20 20 20 77 54 61 62 6c 65 l++){. wTable
cfd0: 20 2b 3d 20 70 54 61 62 43 6f 6c 2d 3e 73 7a 45 += pTabCol->szE
cfe0: 73 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 st;. }. if( pT
cff0: 61 62 2d 3e 69 50 4b 65 79 3c 30 20 29 20 77 54 ab->iPKey<0 ) wT
d000: 61 62 6c 65 2b 2b 3b 0a 20 20 70 54 61 62 2d 3e able++;. pTab->
d010: 73 7a 54 61 62 52 6f 77 20 3d 20 73 71 6c 69 74 szTabRow = sqlit
d020: 65 33 4c 6f 67 45 73 74 28 77 54 61 62 6c 65 2a e3LogEst(wTable*
d030: 34 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 73 74 4);.}../*.** Est
d040: 69 6d 61 74 65 20 74 68 65 20 61 76 65 72 61 67 imate the averag
d050: 65 20 73 69 7a 65 20 6f 66 20 61 20 72 6f 77 20 e size of a row
d060: 66 6f 72 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2f for an index..*/
d070: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 73 74 .static void est
d080: 69 6d 61 74 65 49 6e 64 65 78 57 69 64 74 68 28 imateIndexWidth(
d090: 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 Index *pIdx){.
d0a0: 75 6e 73 69 67 6e 65 64 20 77 49 6e 64 65 78 20 unsigned wIndex
d0b0: 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 = 0;. int i;.
d0c0: 63 6f 6e 73 74 20 43 6f 6c 75 6d 6e 20 2a 61 43 const Column *aC
d0d0: 6f 6c 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c ol = pIdx->pTabl
d0e0: 65 2d 3e 61 43 6f 6c 3b 0a 20 20 66 6f 72 28 69 e->aCol;. for(i
d0f0: 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c =0; i<pIdx->nCol
d100: 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 umn; i++){. i
d110: 31 36 20 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 16 x = pIdx->aiC
d120: 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 61 73 olumn[i];. as
d130: 73 65 72 74 28 20 78 3c 70 49 64 78 2d 3e 70 54 sert( x<pIdx->pT
d140: 61 62 6c 65 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 able->nCol );.
d150: 20 20 77 49 6e 64 65 78 20 2b 3d 20 78 3c 30 20 wIndex += x<0
d160: 3f 20 31 20 3a 20 61 43 6f 6c 5b 70 49 64 78 2d ? 1 : aCol[pIdx-
d170: 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 2e 73 7a >aiColumn[i]].sz
d180: 45 73 74 3b 0a 20 20 7d 0a 20 20 70 49 64 78 2d Est;. }. pIdx-
d190: 3e 73 7a 49 64 78 52 6f 77 20 3d 20 73 71 6c 69 >szIdxRow = sqli
d1a0: 74 65 33 4c 6f 67 45 73 74 28 77 49 6e 64 65 78 te3LogEst(wIndex
d1b0: 2a 34 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 *4);.}../* Retur
d1c0: 6e 20 74 72 75 65 20 69 66 20 76 61 6c 75 65 20 n true if value
d1d0: 78 20 69 73 20 66 6f 75 6e 64 20 61 6e 79 20 6f x is found any o
d1e0: 66 20 74 68 65 20 66 69 72 73 74 20 6e 43 6f 6c f the first nCol
d1f0: 20 65 6e 74 72 69 65 73 20 6f 66 20 61 69 43 6f entries of aiCo
d200: 6c 5b 5d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e l[].*/.static in
d210: 74 20 68 61 73 43 6f 6c 75 6d 6e 28 63 6f 6e 73 t hasColumn(cons
d220: 74 20 69 31 36 20 2a 61 69 43 6f 6c 2c 20 69 6e t i16 *aiCol, in
d230: 74 20 6e 43 6f 6c 2c 20 69 6e 74 20 78 29 7b 0a t nCol, int x){.
d240: 20 20 77 68 69 6c 65 28 20 6e 43 6f 6c 2d 2d 20 while( nCol--
d250: 3e 20 30 20 29 20 69 66 28 20 78 3d 3d 2a 28 61 > 0 ) if( x==*(a
d260: 69 43 6f 6c 2b 2b 29 20 29 20 72 65 74 75 72 6e iCol++) ) return
d270: 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 1;. return 0;.
d280: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
d290: 75 74 69 6e 65 20 72 75 6e 73 20 61 74 20 74 68 utine runs at th
d2a0: 65 20 65 6e 64 20 6f 66 20 70 61 72 73 69 6e 67 e end of parsing
d2b0: 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 a CREATE TABLE
d2c0: 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 0a 2a statement that.*
d2d0: 2a 20 68 61 73 20 61 20 57 49 54 48 4f 55 54 20 * has a WITHOUT
d2e0: 52 4f 57 49 44 20 63 6c 61 75 73 65 2e 20 20 54 ROWID clause. T
d2f0: 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 he job of this r
d300: 6f 75 74 69 6e 65 20 69 73 20 74 6f 20 63 6f 6e outine is to con
d310: 76 65 72 74 20 62 6f 74 68 0a 2a 2a 20 69 6e 74 vert both.** int
d320: 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 64 61 74 ernal schema dat
d330: 61 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 a structures and
d340: 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 56 the generated V
d350: 44 42 45 20 63 6f 64 65 20 73 6f 20 74 68 61 74 DBE code so that
d360: 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 61 70 70 they.** are app
d370: 72 6f 70 72 69 61 74 65 20 66 6f 72 20 61 20 57 ropriate for a W
d380: 49 54 48 4f 55 54 20 52 4f 57 49 44 20 74 61 62 ITHOUT ROWID tab
d390: 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 le instead of a
d3a0: 72 6f 77 69 64 20 74 61 62 6c 65 2e 0a 2a 2a 20 rowid table..**
d3b0: 43 68 61 6e 67 65 73 20 69 6e 63 6c 75 64 65 3a Changes include:
d3c0: 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 20 .**.** (1)
d3d0: 43 6f 6e 76 65 72 74 20 74 68 65 20 4f 50 5f 43 Convert the OP_C
d3e0: 72 65 61 74 65 54 61 62 6c 65 20 69 6e 74 6f 20 reateTable into
d3f0: 61 6e 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 an OP_CreateInde
d400: 78 2e 20 20 54 68 65 72 65 20 69 73 0a 2a 2a 20 x. There is.**
d410: 20 20 20 20 20 20 20 20 20 6e 6f 20 72 6f 77 69 no rowi
d420: 64 20 62 74 72 65 65 20 66 6f 72 20 61 20 57 49 d btree for a WI
d430: 54 48 4f 55 54 20 52 4f 57 49 44 2e 20 20 49 6e THOUT ROWID. In
d440: 73 74 65 61 64 2c 20 74 68 65 20 63 61 6e 6f 6e stead, the canon
d450: 69 63 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ical.**
d460: 20 64 61 74 61 20 73 74 6f 72 61 67 65 20 69 73 data storage is
d470: 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 a covering inde
d480: 78 20 62 74 72 65 65 2e 0a 2a 2a 20 20 20 20 20 x btree..**
d490: 28 32 29 20 20 42 79 70 61 73 73 20 74 68 65 20 (2) Bypass the
d4a0: 63 72 65 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 creation of the
d4b0: 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
d4c0: 62 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 20 20 20 ble entry.**
d4d0: 20 20 20 20 20 20 66 6f 72 20 74 68 65 20 50 52 for the PR
d4e0: 49 4d 41 52 59 20 4b 45 59 20 61 73 20 74 68 65 IMARY KEY as the
d4f0: 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 6e 64 primary key ind
d500: 65 78 20 69 73 20 6e 6f 77 0a 2a 2a 20 20 20 20 ex is now.**
d510: 20 20 20 20 20 20 69 64 65 6e 74 69 66 69 65 64 identified
d520: 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d by the sqlite_m
d530: 61 73 74 65 72 20 74 61 62 6c 65 20 65 6e 74 72 aster table entr
d540: 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 y of the table i
d550: 74 73 65 6c 66 2e 0a 2a 2a 20 20 20 20 20 28 33 tself..** (3
d560: 29 20 20 53 65 74 20 74 68 65 20 49 6e 64 65 78 ) Set the Index
d570: 2e 74 6e 75 6d 20 6f 66 20 74 68 65 20 50 52 49 .tnum of the PRI
d580: 4d 41 52 59 20 4b 45 59 20 49 6e 64 65 78 20 6f MARY KEY Index o
d590: 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20 bject in the.**
d5a0: 20 20 20 20 20 20 20 20 20 73 63 68 65 6d 61 20 schema
d5b0: 74 6f 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 to the rootpage
d5c0: 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 74 61 from the main ta
d5d0: 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 28 34 29 20 ble..** (4)
d5e0: 20 53 65 74 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 Set all columns
d5f0: 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 of the PRIMARY
d600: 4b 45 59 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 KEY schema objec
d610: 74 20 74 6f 20 62 65 20 4e 4f 54 20 4e 55 4c 4c t to be NOT NULL
d620: 2e 0a 2a 2a 20 20 20 20 20 28 35 29 20 20 41 64 ..** (5) Ad
d630: 64 20 61 6c 6c 20 74 61 62 6c 65 20 63 6f 6c 75 d all table colu
d640: 6d 6e 73 20 74 6f 20 74 68 65 20 50 52 49 4d 41 mns to the PRIMA
d650: 52 59 20 4b 45 59 20 49 6e 64 65 78 20 6f 62 6a RY KEY Index obj
d660: 65 63 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ect.**
d670: 73 6f 20 74 68 61 74 20 74 68 65 20 50 52 49 4d so that the PRIM
d680: 41 52 59 20 4b 45 59 20 69 73 20 61 20 63 6f 76 ARY KEY is a cov
d690: 65 72 69 6e 67 20 69 6e 64 65 78 2e 20 20 54 68 ering index. Th
d6a0: 65 20 73 75 72 70 6c 75 73 0a 2a 2a 20 20 20 20 e surplus.**
d6b0: 20 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 61 72 columns ar
d6c0: 65 20 70 61 72 74 20 6f 66 20 4b 65 79 49 6e 66 e part of KeyInf
d6d0: 6f 2e 6e 58 46 69 65 6c 64 20 61 6e 64 20 61 72 o.nXField and ar
d6e0: 65 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 0a 2a e not used for.*
d6f0: 2a 20 20 20 20 20 20 20 20 20 20 73 6f 72 74 69 * sorti
d700: 6e 67 20 6f 72 20 6c 6f 6f 6b 75 70 20 6f 72 20 ng or lookup or
d710: 75 6e 69 71 75 65 6e 65 73 73 20 63 68 65 63 6b uniqueness check
d720: 73 2e 0a 2a 2a 20 20 20 20 20 28 36 29 20 20 52 s..** (6) R
d730: 65 70 6c 61 63 65 20 74 68 65 20 72 6f 77 69 64 eplace the rowid
d740: 20 74 61 69 6c 20 6f 6e 20 61 6c 6c 20 61 75 74 tail on all aut
d750: 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 omatically gener
d760: 61 74 65 64 20 55 4e 49 51 55 45 0a 2a 2a 20 20 ated UNIQUE.**
d770: 20 20 20 20 20 20 20 20 69 6e 64 69 63 65 73 20 indices
d780: 77 69 74 68 20 74 68 65 20 50 52 49 4d 41 52 59 with the PRIMARY
d790: 20 4b 45 59 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2f KEY columns..*/
d7a0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6e .static void con
d7b0: 76 65 72 74 54 6f 57 69 74 68 6f 75 74 52 6f 77 vertToWithoutRow
d7c0: 69 64 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 idTable(Parse *p
d7d0: 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 Parse, Table *pT
d7e0: 61 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 ab){. Index *pI
d7f0: 64 78 3b 0a 20 20 49 6e 64 65 78 20 2a 70 50 6b dx;. Index *pPk
d800: 3b 0a 20 20 69 6e 74 20 6e 50 6b 3b 0a 20 20 69 ;. int nPk;. i
d810: 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 71 6c 69 74 nt i, j;. sqlit
d820: 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
d830: 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d >db;. Vdbe *v =
d840: 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a pParse->pVdbe;.
d850: 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 . /* Convert th
d860: 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 e OP_CreateTable
d870: 20 6f 70 63 6f 64 65 20 74 68 61 74 20 77 6f 75 opcode that wou
d880: 6c 64 20 6e 6f 72 6d 61 6c 6c 79 20 63 72 65 61 ld normally crea
d890: 74 65 20 74 68 65 0a 20 20 2a 2a 20 72 6f 6f 74 te the. ** root
d8a0: 2d 70 61 67 65 20 66 6f 72 20 74 68 65 20 74 61 -page for the ta
d8b0: 62 6c 65 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 43 ble into an OP_C
d8c0: 72 65 61 74 65 49 6e 64 65 78 20 6f 70 63 6f 64 reateIndex opcod
d8d0: 65 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 20 20 e. The index.
d8e0: 2a 2a 20 63 72 65 61 74 65 64 20 77 69 6c 6c 20 ** created will
d8f0: 62 65 63 6f 6d 65 20 74 68 65 20 50 52 49 4d 41 become the PRIMA
d900: 52 59 20 4b 45 59 20 69 6e 64 65 78 2e 0a 20 20 RY KEY index..
d910: 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d */. if( pParse-
d920: 3e 61 64 64 72 43 72 54 61 62 20 29 7b 0a 20 20 >addrCrTab ){.
d930: 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 assert( v );.
d940: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
d950: 61 6e 67 65 4f 70 63 6f 64 65 28 76 2c 20 70 50 angeOpcode(v, pP
d960: 61 72 73 65 2d 3e 61 64 64 72 43 72 54 61 62 2c arse->addrCrTab,
d970: 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 29 OP_CreateIndex)
d980: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 ;. }.. /* Loca
d990: 74 65 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b te the PRIMARY K
d9a0: 45 59 20 69 6e 64 65 78 2e 20 20 4f 72 2c 20 69 EY index. Or, i
d9b0: 66 20 74 68 69 73 20 74 61 62 6c 65 20 77 61 73 f this table was
d9c0: 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a originally. **
d9d0: 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d an INTEGER PRIM
d9e0: 41 52 59 20 4b 45 59 20 74 61 62 6c 65 2c 20 63 ARY KEY table, c
d9f0: 72 65 61 74 65 20 61 20 6e 65 77 20 50 52 49 4d reate a new PRIM
da00: 41 52 59 20 4b 45 59 20 69 6e 64 65 78 2e 20 0a ARY KEY index. .
da10: 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d */. if( pTab-
da20: 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 >iPKey>=0 ){.
da30: 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
da40: 3b 0a 20 20 20 20 54 6f 6b 65 6e 20 69 70 6b 54 ;. Token ipkT
da50: 6f 6b 65 6e 3b 0a 20 20 20 20 69 70 6b 54 6f 6b oken;. ipkTok
da60: 65 6e 2e 7a 20 3d 20 70 54 61 62 2d 3e 61 43 6f en.z = pTab->aCo
da70: 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a l[pTab->iPKey].z
da80: 4e 61 6d 65 3b 0a 20 20 20 20 69 70 6b 54 6f 6b Name;. ipkTok
da90: 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 en.n = sqlite3St
daa0: 72 6c 65 6e 33 30 28 69 70 6b 54 6f 6b 65 6e 2e rlen30(ipkToken.
dab0: 7a 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 z);. pList =
dac0: 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 sqlite3ExprListA
dad0: 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c ppend(pParse, 0,
dae0: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
daf0: 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 sqlite3ExprA
db00: 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 49 44 2c 20 lloc(db, TK_ID,
db10: 26 69 70 6b 54 6f 6b 65 6e 2c 20 30 29 29 3b 0a &ipkToken, 0));.
db20: 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 if( pList==0
db30: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 70 ) return;. p
db40: 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f List->a[0].sortO
db50: 72 64 65 72 20 3d 20 70 50 61 72 73 65 2d 3e 69 rder = pParse->i
db60: 50 6b 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 PkSortOrder;.
db70: 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d assert( pParse-
db80: 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 70 54 61 62 >pNewTable==pTab
db90: 20 29 3b 0a 20 20 20 20 70 50 6b 20 3d 20 73 71 );. pPk = sq
dba0: 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 lite3CreateIndex
dbb0: 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 (pParse, 0, 0, 0
dbc0: 2c 20 70 4c 69 73 74 2c 20 70 54 61 62 2d 3e 6b , pList, pTab->k
dbd0: 65 79 43 6f 6e 66 2c 20 30 2c 20 30 2c 20 30 2c eyConf, 0, 0, 0,
dbe0: 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 50 6b 0);. if( pPk
dbf0: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
dc00: 20 20 70 50 6b 2d 3e 69 64 78 54 79 70 65 20 3d pPk->idxType =
dc10: 20 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 5f SQLITE_IDXTYPE_
dc20: 50 52 49 4d 41 52 59 4b 45 59 3b 0a 20 20 20 20 PRIMARYKEY;.
dc30: 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 pTab->iPKey = -1
dc40: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
dc50: 50 6b 20 3d 20 73 71 6c 69 74 65 33 50 72 69 6d Pk = sqlite3Prim
dc60: 61 72 79 4b 65 79 49 6e 64 65 78 28 70 54 61 62 aryKeyIndex(pTab
dc70: 29 3b 0a 0a 20 20 20 20 2f 2a 20 42 79 70 61 73 );.. /* Bypas
dc80: 73 20 74 68 65 20 63 72 65 61 74 69 6f 6e 20 6f s the creation o
dc90: 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 f the PRIMARY KE
dca0: 59 20 62 74 72 65 65 20 61 6e 64 20 74 68 65 20 Y btree and the
dcb0: 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 0a 20 20 sqlite_master.
dcc0: 20 20 2a 2a 20 74 61 62 6c 65 20 65 6e 74 72 79 ** table entry
dcd0: 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 72 . This is only r
dce0: 65 71 75 69 72 65 64 20 69 66 20 63 75 72 72 65 equired if curre
dcf0: 6e 74 6c 79 20 67 65 6e 65 72 61 74 69 6e 67 20 ntly generating
dd00: 56 44 42 45 0a 20 20 20 20 2a 2a 20 63 6f 64 65 VDBE. ** code
dd10: 20 66 6f 72 20 61 20 43 52 45 41 54 45 20 54 41 for a CREATE TA
dd20: 42 4c 45 20 28 6e 6f 74 20 77 68 65 6e 20 70 61 BLE (not when pa
dd30: 72 73 69 6e 67 20 6f 6e 65 20 61 73 20 70 61 72 rsing one as par
dd40: 74 20 6f 66 20 72 65 61 64 69 6e 67 0a 20 20 20 t of reading.
dd50: 20 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 73 ** a database s
dd60: 63 68 65 6d 61 29 2e 20 20 2a 2f 0a 20 20 20 20 chema). */.
dd70: 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 61 if( v ){. a
dd80: 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e ssert( db->init.
dd90: 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 busy==0 );.
dda0: 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
ddb0: 67 65 4f 70 63 6f 64 65 28 76 2c 20 70 50 6b 2d geOpcode(v, pPk-
ddc0: 3e 74 6e 75 6d 2c 20 4f 50 5f 47 6f 74 6f 29 3b >tnum, OP_Goto);
ddd0: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 . }.. /*.
dde0: 20 20 20 2a 2a 20 52 65 6d 6f 76 65 20 61 6c 6c ** Remove all
ddf0: 20 72 65 64 75 6e 64 61 6e 74 20 63 6f 6c 75 6d redundant colum
de00: 6e 73 20 66 72 6f 6d 20 74 68 65 20 50 52 49 4d ns from the PRIM
de10: 41 52 59 20 4b 45 59 2e 20 20 46 6f 72 20 65 78 ARY KEY. For ex
de20: 61 6d 70 6c 65 2c 20 63 68 61 6e 67 65 0a 20 20 ample, change.
de30: 20 20 2a 2a 20 22 50 52 49 4d 41 52 59 20 4b 45 ** "PRIMARY KE
de40: 59 28 61 2c 62 2c 61 2c 62 2c 63 2c 62 2c 63 2c Y(a,b,a,b,c,b,c,
de50: 64 29 22 20 69 6e 74 6f 20 6a 75 73 74 20 22 50 d)" into just "P
de60: 52 49 4d 41 52 59 20 4b 45 59 28 61 2c 62 2c 63 RIMARY KEY(a,b,c
de70: 2c 64 29 22 2e 20 20 4c 61 74 65 72 0a 20 20 20 ,d)". Later.
de80: 20 2a 2a 20 63 6f 64 65 20 61 73 73 75 6d 65 73 ** code assumes
de90: 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
dea0: 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 72 65 70 contains no rep
deb0: 65 61 74 65 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20 eated columns..
dec0: 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d */. for(i=
ded0: 6a 3d 31 3b 20 69 3c 70 50 6b 2d 3e 6e 4b 65 79 j=1; i<pPk->nKey
dee0: 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Col; i++){.
def0: 20 69 66 28 20 68 61 73 43 6f 6c 75 6d 6e 28 70 if( hasColumn(p
df00: 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 6a 2c Pk->aiColumn, j,
df10: 20 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 pPk->aiColumn[i
df20: 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 ]) ){. pP
df30: 6b 2d 3e 6e 43 6f 6c 75 6d 6e 2d 2d 3b 0a 20 20 k->nColumn--;.
df40: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
df50: 20 20 20 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e pPk->aiColumn
df60: 5b 6a 2b 2b 5d 20 3d 20 70 50 6b 2d 3e 61 69 43 [j++] = pPk->aiC
df70: 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 olumn[i];.
df80: 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 6b 2d }. }. pPk-
df90: 3e 6e 4b 65 79 43 6f 6c 20 3d 20 6a 3b 0a 20 20 >nKeyCol = j;.
dfa0: 7d 0a 20 20 70 50 6b 2d 3e 69 73 43 6f 76 65 72 }. pPk->isCover
dfb0: 69 6e 67 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 ing = 1;. asser
dfc0: 74 28 20 70 50 6b 21 3d 30 20 29 3b 0a 20 20 6e t( pPk!=0 );. n
dfd0: 50 6b 20 3d 20 70 50 6b 2d 3e 6e 4b 65 79 43 6f Pk = pPk->nKeyCo
dfe0: 6c 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 l;.. /* Make su
dff0: 72 65 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 re every column
e000: 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b of the PRIMARY K
e010: 45 59 20 69 73 20 4e 4f 54 20 4e 55 4c 4c 2e 20 EY is NOT NULL.
e020: 20 28 45 78 63 65 70 74 2c 0a 20 20 2a 2a 20 64 (Except,. ** d
e030: 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 74 68 o not enforce th
e040: 69 73 20 66 6f 72 20 69 6d 70 6f 73 74 65 72 20 is for imposter
e050: 74 61 62 6c 65 73 2e 29 20 2a 2f 0a 20 20 69 66 tables.) */. if
e060: 28 20 21 64 62 2d 3e 69 6e 69 74 2e 69 6d 70 6f ( !db->init.impo
e070: 73 74 65 72 54 61 62 6c 65 20 29 7b 0a 20 20 20 sterTable ){.
e080: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 6b 3b for(i=0; i<nPk;
e090: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 54 61 i++){. pTa
e0a0: 62 2d 3e 61 43 6f 6c 5b 70 50 6b 2d 3e 61 69 43 b->aCol[pPk->aiC
e0b0: 6f 6c 75 6d 6e 5b 69 5d 5d 2e 6e 6f 74 4e 75 6c olumn[i]].notNul
e0c0: 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 l = 1;. }.
e0d0: 20 70 50 6b 2d 3e 75 6e 69 71 4e 6f 74 4e 75 6c pPk->uniqNotNul
e0e0: 6c 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a l = 1;. }.. /*
e0f0: 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f The root page o
e100: 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 f the PRIMARY KE
e110: 59 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 72 Y is the table r
e120: 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 70 50 oot page */. pP
e130: 6b 2d 3e 74 6e 75 6d 20 3d 20 70 54 61 62 2d 3e k->tnum = pTab->
e140: 74 6e 75 6d 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 tnum;.. /* Upda
e150: 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 te the in-memory
e160: 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
e170: 6f 66 20 61 6c 6c 20 55 4e 49 51 55 45 20 69 6e of all UNIQUE in
e180: 64 69 63 65 73 20 62 79 20 63 6f 6e 76 65 72 74 dices by convert
e190: 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 69 6e ing. ** the fin
e1a0: 61 6c 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 al rowid column
e1b0: 69 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 into one or more
e1c0: 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 columns of the
e1d0: 50 52 49 4d 41 52 59 20 4b 45 59 2e 0a 20 20 2a PRIMARY KEY.. *
e1e0: 2f 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 /. for(pIdx=pTa
e1f0: 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b b->pIndex; pIdx;
e200: 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 pIdx=pIdx->pNex
e210: 74 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 t){. int n;.
e220: 20 20 20 69 66 28 20 49 73 50 72 69 6d 61 72 79 if( IsPrimary
e230: 4b 65 79 49 6e 64 65 78 28 70 49 64 78 29 20 29 KeyIndex(pIdx) )
e240: 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 continue;. f
e250: 6f 72 28 69 3d 6e 3d 30 3b 20 69 3c 6e 50 6b 3b or(i=n=0; i<nPk;
e260: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
e270: 20 21 68 61 73 43 6f 6c 75 6d 6e 28 70 49 64 78 !hasColumn(pIdx
e280: 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 70 49 64 78 ->aiColumn, pIdx
e290: 2d 3e 6e 4b 65 79 43 6f 6c 2c 20 70 50 6b 2d 3e ->nKeyCol, pPk->
e2a0: 61 69 43 6f 6c 75 6d 6e 5b 69 5d 29 20 29 20 6e aiColumn[i]) ) n
e2b0: 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
e2c0: 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ( n==0 ){.
e2d0: 2f 2a 20 54 68 69 73 20 69 6e 64 65 78 20 69 73 /* This index is
e2e0: 20 61 20 73 75 70 65 72 73 65 74 20 6f 66 20 74 a superset of t
e2f0: 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a he primary key *
e300: 2f 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 /. pIdx->nC
e310: 6f 6c 75 6d 6e 20 3d 20 70 49 64 78 2d 3e 6e 4b olumn = pIdx->nK
e320: 65 79 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e eyCol;. con
e330: 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 tinue;. }.
e340: 20 69 66 28 20 72 65 73 69 7a 65 49 6e 64 65 78 if( resizeIndex
e350: 4f 62 6a 65 63 74 28 64 62 2c 20 70 49 64 78 2c Object(db, pIdx,
e360: 20 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 2b 6e pIdx->nKeyCol+n
e370: 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 ) ) return;.
e380: 66 6f 72 28 69 3d 30 2c 20 6a 3d 70 49 64 78 2d for(i=0, j=pIdx-
e390: 3e 6e 4b 65 79 43 6f 6c 3b 20 69 3c 6e 50 6b 3b >nKeyCol; i<nPk;
e3a0: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
e3b0: 20 21 68 61 73 43 6f 6c 75 6d 6e 28 70 49 64 78 !hasColumn(pIdx
e3c0: 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 70 49 64 78 ->aiColumn, pIdx
e3d0: 2d 3e 6e 4b 65 79 43 6f 6c 2c 20 70 50 6b 2d 3e ->nKeyCol, pPk->
e3e0: 61 69 43 6f 6c 75 6d 6e 5b 69 5d 29 20 29 7b 0a aiColumn[i]) ){.
e3f0: 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61 69 pIdx->ai
e400: 43 6f 6c 75 6d 6e 5b 6a 5d 20 3d 20 70 50 6b 2d Column[j] = pPk-
e410: 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 >aiColumn[i];.
e420: 20 20 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f pIdx->azCo
e430: 6c 6c 5b 6a 5d 20 3d 20 70 50 6b 2d 3e 61 7a 43 ll[j] = pPk->azC
e440: 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 oll[i];.
e450: 6a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 j++;. }.
e460: 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
e470: 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 70 49 Idx->nColumn>=pI
e480: 64 78 2d 3e 6e 4b 65 79 43 6f 6c 2b 6e 20 29 3b dx->nKeyCol+n );
e490: 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 . assert( pId
e4a0: 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 3b x->nColumn>=j );
e4b0: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 61 . }.. /* Add a
e4c0: 6c 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 ll table columns
e4d0: 20 74 6f 20 74 68 65 20 50 52 49 4d 41 52 59 20 to the PRIMARY
e4e0: 4b 45 59 20 69 6e 64 65 78 0a 20 20 2a 2f 0a 20 KEY index. */.
e4f0: 20 69 66 28 20 6e 50 6b 3c 70 54 61 62 2d 3e 6e if( nPk<pTab->n
e500: 43 6f 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 72 Col ){. if( r
e510: 65 73 69 7a 65 49 6e 64 65 78 4f 62 6a 65 63 74 esizeIndexObject
e520: 28 64 62 2c 20 70 50 6b 2c 20 70 54 61 62 2d 3e (db, pPk, pTab->
e530: 6e 43 6f 6c 29 20 29 20 72 65 74 75 72 6e 3b 0a nCol) ) return;.
e540: 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e for(i=0, j=n
e550: 50 6b 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c Pk; i<pTab->nCol
e560: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
e570: 28 20 21 68 61 73 43 6f 6c 75 6d 6e 28 70 50 6b ( !hasColumn(pPk
e580: 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 6a 2c 20 69 ->aiColumn, j, i
e590: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 ) ){. ass
e5a0: 65 72 74 28 20 6a 3c 70 50 6b 2d 3e 6e 43 6f 6c ert( j<pPk->nCol
e5b0: 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 umn );. p
e5c0: 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 20 Pk->aiColumn[j]
e5d0: 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 70 50 6b = i;. pPk
e5e0: 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d 20 3d 20 22 42 ->azColl[j] = "B
e5f0: 49 4e 41 52 59 22 3b 0a 20 20 20 20 20 20 20 20 INARY";.
e600: 6a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 j++;. }.
e610: 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
e620: 50 6b 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 6a 20 29 Pk->nColumn==j )
e630: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 ;. assert( pT
e640: 61 62 2d 3e 6e 43 6f 6c 3d 3d 6a 20 29 3b 0a 20 ab->nCol==j );.
e650: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 6b 2d }else{. pPk-
e660: 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 54 61 62 2d >nColumn = pTab-
e670: 3e 6e 43 6f 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a >nCol;. }.}../*
e680: 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
e690: 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 is called to re
e6a0: 70 6f 72 74 20 74 68 65 20 66 69 6e 61 6c 20 22 port the final "
e6b0: 29 22 20 74 68 61 74 20 74 65 72 6d 69 6e 61 74 )" that terminat
e6c0: 65 73 0a 2a 2a 20 61 20 43 52 45 41 54 45 20 54 es.** a CREATE T
e6d0: 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a ABLE statement..
e6e0: 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 **.** The table
e6f0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6f structure that o
e700: 74 68 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74 ther action rout
e710: 69 6e 65 73 20 68 61 76 65 20 62 65 65 6e 20 62 ines have been b
e720: 75 69 6c 64 69 6e 67 0a 2a 2a 20 69 73 20 61 64 uilding.** is ad
e730: 64 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 ded to the inter
e740: 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2c nal hash tables,
e750: 20 61 73 73 75 6d 69 6e 67 20 6e 6f 20 65 72 72 assuming no err
e760: 6f 72 73 20 68 61 76 65 0a 2a 2a 20 6f 63 63 75 ors have.** occu
e770: 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 rred..**.** An e
e780: 6e 74 72 79 20 66 6f 72 20 74 68 65 20 74 61 62 ntry for the tab
e790: 6c 65 20 69 73 20 6d 61 64 65 20 69 6e 20 74 68 le is made in th
e7a0: 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 6f e master table o
e7b0: 6e 20 64 69 73 6b 2c 20 75 6e 6c 65 73 73 0a 2a n disk, unless.*
e7c0: 2a 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 * this is a temp
e7d0: 6f 72 61 72 79 20 74 61 62 6c 65 20 6f 72 20 64 orary table or d
e7e0: 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 2e b->init.busy==1.
e7f0: 20 20 57 68 65 6e 20 64 62 2d 3e 69 6e 69 74 2e When db->init.
e800: 62 75 73 79 3d 3d 31 0a 2a 2a 20 69 74 20 6d 65 busy==1.** it me
e810: 61 6e 73 20 77 65 20 61 72 65 20 72 65 61 64 69 ans we are readi
e820: 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 ng the sqlite_ma
e830: 73 74 65 72 20 74 61 62 6c 65 20 62 65 63 61 75 ster table becau
e840: 73 65 20 77 65 20 6a 75 73 74 0a 2a 2a 20 63 6f se we just.** co
e850: 6e 6e 65 63 74 65 64 20 74 6f 20 74 68 65 20 64 nnected to the d
e860: 61 74 61 62 61 73 65 20 6f 72 20 62 65 63 61 75 atabase or becau
e870: 73 65 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 se the sqlite_ma
e880: 73 74 65 72 20 74 61 62 6c 65 20 68 61 73 0a 2a ster table has.*
e890: 2a 20 72 65 63 65 6e 74 6c 79 20 63 68 61 6e 67 * recently chang
e8a0: 65 64 2c 20 73 6f 20 74 68 65 20 65 6e 74 72 79 ed, so the entry
e8b0: 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20 for this table
e8c0: 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 69 already exists i
e8d0: 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f n.** the sqlite_
e8e0: 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20 57 master table. W
e8f0: 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f e do not want to
e900: 20 63 72 65 61 74 65 20 69 74 20 61 67 61 69 6e create it again
e910: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
e920: 53 65 6c 65 63 74 20 61 72 67 75 6d 65 6e 74 20 Select argument
e930: 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 is not NULL, it
e940: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 means that this
e950: 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 61 73 20 63 routine.** was c
e960: 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 alled to create
e970: 61 20 74 61 62 6c 65 20 67 65 6e 65 72 61 74 65 a table generate
e980: 64 20 66 72 6f 6d 20 61 20 0a 2a 2a 20 22 43 52 d from a .** "CR
e990: 45 41 54 45 20 54 41 42 4c 45 20 2e 2e 2e 20 41 EATE TABLE ... A
e9a0: 53 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 73 74 S SELECT ..." st
e9b0: 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 63 6f atement. The co
e9c0: 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a lumn names of.**
e9d0: 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 77 the new table w
e9e0: 69 6c 6c 20 6d 61 74 63 68 20 74 68 65 20 72 65 ill match the re
e9f0: 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 sult set of the
ea00: 53 45 4c 45 43 54 2e 0a 2a 2f 0a 76 6f 69 64 20 SELECT..*/.void
ea10: 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 sqlite3EndTable(
ea20: 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
ea30: 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 , /* Pa
ea40: 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 rse context */.
ea50: 20 54 6f 6b 65 6e 20 2a 70 43 6f 6e 73 2c 20 20 Token *pCons,
ea60: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
ea70: 27 2c 27 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 ',' token after
ea80: 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 the last column
ea90: 64 65 66 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e defn. */. Token
eaa0: 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 *pEnd,
eab0: 20 20 20 2f 2a 20 54 68 65 20 27 29 27 20 62 65 /* The ')' be
eac0: 66 6f 72 65 20 6f 70 74 69 6f 6e 73 20 69 6e 20 fore options in
ead0: 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
eae0: 20 2a 2f 0a 20 20 75 38 20 74 61 62 4f 70 74 73 */. u8 tabOpts
eaf0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
eb00: 20 45 78 74 72 61 20 74 61 62 6c 65 20 6f 70 74 Extra table opt
eb10: 69 6f 6e 73 2e 20 55 73 75 61 6c 6c 79 20 30 2e ions. Usually 0.
eb20: 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 */. Select *pS
eb30: 65 6c 65 63 74 20 20 20 20 20 20 20 20 20 2f 2a elect /*
eb40: 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 61 20 22 Select from a "
eb50: 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 20 53 45 CREATE ... AS SE
eb60: 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 20 54 61 LECT" */.){. Ta
eb70: 62 6c 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20 ble *p;
eb80: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
eb90: 65 77 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 ew table */. sq
eba0: 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
ebb0: 73 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65 20 64 se->db; /* The d
ebc0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
ebd0: 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b on */. int iDb;
ebe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ebf0: 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e /* Database in
ec00: 20 77 68 69 63 68 20 74 68 65 20 74 61 62 6c 65 which the table
ec10: 20 6c 69 76 65 73 20 2a 2f 0a 20 20 49 6e 64 65 lives */. Inde
ec20: 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 x *pIdx;
ec30: 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6d 70 6c /* An impl
ec40: 69 65 64 20 69 6e 64 65 78 20 6f 66 20 74 68 65 ied index of the
ec50: 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 69 66 28 table */.. if(
ec60: 20 70 45 6e 64 3d 3d 30 20 26 26 20 70 53 65 6c pEnd==0 && pSel
ec70: 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ect==0 ){. re
ec80: 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 turn;. }. asse
ec90: 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 rt( !db->mallocF
eca0: 61 69 6c 65 64 20 29 3b 0a 20 20 70 20 3d 20 70 ailed );. p = p
ecb0: 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
ecc0: 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
ecd0: 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 eturn;.. assert
ece0: 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 ( !db->init.busy
ecf0: 20 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a || !pSelect );.
ed00: 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d . /* If the db-
ed10: 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 >init.busy is 1
ed20: 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 it means we are
ed30: 72 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 reading the SQL
ed40: 6f 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 off the. ** "sq
ed50: 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 lite_master" or
ed60: 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 "sqlite_temp_mas
ed70: 74 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 ter" table on th
ed80: 65 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 e disk.. ** So
ed90: 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 do not write to
eda0: 74 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 the disk again.
edb0: 20 45 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f Extract the roo
edc0: 74 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 t page number.
edd0: 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 ** for the table
ede0: 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e from the db->in
edf0: 69 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 it.newTnum field
ee00: 2e 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d . (The page num
ee10: 62 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 ber. ** should
ee20: 68 61 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 have been put th
ee30: 65 72 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 ere by the sqlit
ee40: 65 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e eOpenCb routine.
ee50: 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d ). */. if( db-
ee60: 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 >init.busy ){.
ee70: 20 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e p->tnum = db->
ee80: 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 init.newTnum;.
ee90: 7d 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 }.. /* Special
eea0: 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 57 processing for W
eeb0: 49 54 48 4f 55 54 20 52 4f 57 49 44 20 54 61 62 ITHOUT ROWID Tab
eec0: 6c 65 73 20 2a 2f 0a 20 20 69 66 28 20 74 61 62 les */. if( tab
eed0: 4f 70 74 73 20 26 20 54 46 5f 57 69 74 68 6f 75 Opts & TF_Withou
eee0: 74 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 69 66 tRowid ){. if
eef0: 28 20 28 70 2d 3e 74 61 62 46 6c 61 67 73 20 26 ( (p->tabFlags &
ef00: 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e TF_Autoincremen
ef10: 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 t) ){. sqli
ef20: 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
ef30: 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 41 se,. "A
ef40: 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 6e 6f 74 UTOINCREMENT not
ef50: 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 57 49 54 48 allowed on WITH
ef60: 4f 55 54 20 52 4f 57 49 44 20 74 61 62 6c 65 73 OUT ROWID tables
ef70: 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ");. return
ef80: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
ef90: 28 70 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 (p->tabFlags & T
efa0: 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 29 F_HasPrimaryKey)
efb0: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
efc0: 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
efd0: 72 73 65 2c 20 22 50 52 49 4d 41 52 59 20 4b 45 rse, "PRIMARY KE
efe0: 59 20 6d 69 73 73 69 6e 67 20 6f 6e 20 74 61 62 Y missing on tab
eff0: 6c 65 20 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 le %s", p->zName
f000: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
f010: 20 20 20 20 70 2d 3e 74 61 62 46 6c 61 67 73 20 p->tabFlags
f020: 7c 3d 20 54 46 5f 57 69 74 68 6f 75 74 52 6f 77 |= TF_WithoutRow
f030: 69 64 20 7c 20 54 46 5f 4e 6f 56 69 73 69 62 6c id | TF_NoVisibl
f040: 65 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63 6f eRowid;. co
f050: 6e 76 65 72 74 54 6f 57 69 74 68 6f 75 74 52 6f nvertToWithoutRo
f060: 77 69 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c widTable(pParse,
f070: 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a p);. }. }..
f080: 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
f090: 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
f0a0: 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 p->pSchema);..#
f0b0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
f0c0: 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 65 IT_CHECK. /* Re
f0d0: 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 solve names in a
f0e0: 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 ll CHECK constra
f0f0: 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e int expressions.
f100: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 . */. if( p->p
f110: 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c Check ){. sql
f120: 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 66 52 ite3ResolveSelfR
f130: 65 66 65 72 65 6e 63 65 28 70 50 61 72 73 65 2c eference(pParse,
f140: 20 70 2c 20 4e 43 5f 49 73 43 68 65 63 6b 2c 20 p, NC_IsCheck,
f150: 30 2c 20 70 2d 3e 70 43 68 65 63 6b 29 3b 0a 20 0, p->pCheck);.
f160: 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 }.#endif /* !de
f170: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
f180: 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f T_CHECK) */.. /
f190: 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20 61 * Estimate the a
f1a0: 76 65 72 61 67 65 20 72 6f 77 20 73 69 7a 65 20 verage row size
f1b0: 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e for the table an
f1c0: 64 20 66 6f 72 20 61 6c 6c 20 69 6d 70 6c 69 65 d for all implie
f1d0: 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 65 d indices */. e
f1e0: 73 74 69 6d 61 74 65 54 61 62 6c 65 57 69 64 74 stimateTableWidt
f1f0: 68 28 70 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 h(p);. for(pIdx
f200: 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 =p->pIndex; pIdx
f210: 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 ; pIdx=pIdx->pNe
f220: 78 74 29 7b 0a 20 20 20 20 65 73 74 69 6d 61 74 xt){. estimat
f230: 65 49 6e 64 65 78 57 69 64 74 68 28 70 49 64 78 eIndexWidth(pIdx
f240: 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
f250: 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 not initializing
f260: 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 , then create a
f270: 72 65 63 6f 72 64 20 66 6f 72 20 74 68 65 20 6e record for the n
f280: 65 77 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69 6e ew table. ** in
f290: 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 the SQLITE_MAST
f2a0: 45 52 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 ER table of the
f2b0: 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 0a 20 database.. **.
f2c0: 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 ** If this is a
f2d0: 20 54 45 4d 50 4f 52 41 52 59 20 74 61 62 6c 65 TEMPORARY table
f2e0: 2c 20 77 72 69 74 65 20 74 68 65 20 65 6e 74 72 , write the entr
f2f0: 79 20 69 6e 74 6f 20 74 68 65 20 61 75 78 69 6c y into the auxil
f300: 69 61 72 79 0a 20 20 2a 2a 20 66 69 6c 65 20 69 iary. ** file i
f310: 6e 73 74 65 61 64 20 6f 66 20 69 6e 74 6f 20 74 nstead of into t
f320: 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
f330: 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 file.. */. if
f340: 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 ( !db->init.busy
f350: 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 ){. int n;.
f360: 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 Vdbe *v;.
f370: 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20 20 char *zType;
f380: 2f 2a 20 22 76 69 65 77 22 20 6f 72 20 22 74 61 /* "view" or "ta
f390: 62 6c 65 22 20 2a 2f 0a 20 20 20 20 63 68 61 72 ble" */. char
f3a0: 20 2a 7a 54 79 70 65 32 3b 20 20 20 2f 2a 20 22 *zType2; /* "
f3b0: 56 49 45 57 22 20 6f 72 20 22 54 41 42 4c 45 22 VIEW" or "TABLE"
f3c0: 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 */. char *zS
f3d0: 74 6d 74 3b 20 20 20 20 2f 2a 20 54 65 78 74 20 tmt; /* Text
f3e0: 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 of the CREATE TA
f3f0: 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20 56 49 BLE or CREATE VI
f400: 45 57 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a EW statement */.
f410: 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 . v = sqlite3
f420: 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
f430: 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 76 . if( NEVER(v
f440: 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a ==0) ) return;..
f450: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
f460: 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 ddOp1(v, OP_Clos
f470: 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 0a e, 0);.. /* .
f480: 20 20 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ** Initializ
f490: 65 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65 20 e zType for the
f4a0: 6e 65 77 20 76 69 65 77 20 6f 72 20 74 61 62 6c new view or tabl
f4b0: 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 e.. */. if
f4c0: 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 ( p->pSelect==0
f4d0: 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 72 65 ){. /* A re
f4e0: 67 75 6c 61 72 20 74 61 62 6c 65 20 2a 2f 0a 20 gular table */.
f4f0: 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 74 61 zType = "ta
f500: 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a 54 79 70 ble";. zTyp
f510: 65 32 20 3d 20 22 54 41 42 4c 45 22 3b 0a 23 69 e2 = "TABLE";.#i
f520: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
f530: 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 6c 73 65 T_VIEW. }else
f540: 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 76 69 65 {. /* A vie
f550: 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70 65 w */. zType
f560: 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 20 20 20 = "view";.
f570: 20 7a 54 79 70 65 32 20 3d 20 22 56 49 45 57 22 zType2 = "VIEW"
f580: 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a ;.#endif. }..
f590: 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
f5a0: 73 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 s a CREATE TABLE
f5b0: 20 78 78 20 41 53 20 53 45 4c 45 43 54 20 2e 2e xx AS SELECT ..
f5c0: 2e 2c 20 65 78 65 63 75 74 65 20 74 68 65 20 53 ., execute the S
f5d0: 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 73 74 61 ELECT. ** sta
f5e0: 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 75 6c 61 tement to popula
f5f0: 74 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 te the new table
f600: 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 . The root-page
f610: 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 0a 20 number for the.
f620: 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 6c 65 20 ** new table
f630: 69 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 is in register p
f640: 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 0a Parse->regRoot..
f650: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e **. ** On
f660: 63 65 20 74 68 65 20 53 45 4c 45 43 54 20 68 61 ce the SELECT ha
f670: 73 20 62 65 65 6e 20 63 6f 64 65 64 20 62 79 20 s been coded by
f680: 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2c sqlite3Select(),
f690: 20 69 74 20 69 73 20 69 6e 20 61 0a 20 20 20 20 it is in a.
f6a0: 2a 2a 20 73 75 69 74 61 62 6c 65 20 73 74 61 74 ** suitable stat
f6b0: 65 20 74 6f 20 71 75 65 72 79 20 66 6f 72 20 74 e to query for t
f6c0: 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 he column names
f6d0: 61 6e 64 20 74 79 70 65 73 20 74 6f 20 62 65 20 and types to be
f6e0: 75 73 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 used. ** by t
f6f0: 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 20 20 he new table..
f700: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 73 68 **. ** A sh
f710: 61 72 65 64 2d 63 61 63 68 65 20 77 72 69 74 65 ared-cache write
f720: 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 -lock is not req
f730: 75 69 72 65 64 20 74 6f 20 77 72 69 74 65 20 74 uired to write t
f740: 6f 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2c o the new table,
f750: 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 73 63 68 . ** as a sch
f760: 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74 20 68 61 ema-lock must ha
f770: 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
f780: 6f 62 74 61 69 6e 65 64 20 74 6f 20 63 72 65 61 obtained to crea
f790: 74 65 20 69 74 2e 20 53 69 6e 63 65 0a 20 20 20 te it. Since.
f7a0: 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 ** a schema-loc
f7b0: 6b 20 65 78 63 6c 75 64 65 73 20 61 6c 6c 20 6f k excludes all o
f7c0: 74 68 65 72 20 64 61 74 61 62 61 73 65 20 75 73 ther database us
f7d0: 65 72 73 2c 20 74 68 65 20 77 72 69 74 65 2d 6c ers, the write-l
f7e0: 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20 20 2a 2a ock would. **
f7f0: 20 62 65 20 72 65 64 75 6e 64 61 6e 74 2e 0a 20 be redundant..
f800: 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 */. if( pS
f810: 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 53 elect ){. S
f820: 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 electDest dest;
f830: 20 20 20 2f 2a 20 57 68 65 72 65 20 74 68 65 20 /* Where the
f840: 53 45 4c 45 43 54 20 73 68 6f 75 6c 64 20 73 74 SELECT should st
f850: 6f 72 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 ore results */.
f860: 20 20 20 20 20 69 6e 74 20 72 65 67 59 69 65 6c int regYiel
f870: 64 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 d; /* Regi
f880: 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 2d ster holding co-
f890: 72 6f 75 74 69 6e 65 20 65 6e 74 72 79 2d 70 6f routine entry-po
f8a0: 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 int */. int
f8b0: 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20 20 20 addrTop;
f8c0: 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 63 /* Top of the c
f8d0: 6f 2d 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 20 o-routine */.
f8e0: 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b 20 20 int regRec;
f8f0: 20 20 20 20 20 20 20 2f 2a 20 41 20 72 65 63 6f /* A reco
f900: 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 20 rd to be insert
f910: 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62 into the new tab
f920: 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 le */. int
f930: 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 regRowid;
f940: 2f 2a 20 52 6f 77 69 64 20 6f 66 20 74 68 65 20 /* Rowid of the
f950: 6e 65 78 74 20 72 6f 77 20 74 6f 20 69 6e 73 65 next row to inse
f960: 72 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 rt */. int
f970: 61 64 64 72 49 6e 73 4c 6f 6f 70 3b 20 20 20 20 addrInsLoop;
f980: 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 6c 6f /* Top of the lo
f990: 6f 70 20 66 6f 72 20 69 6e 73 65 72 74 69 6e 67 op for inserting
f9a0: 20 72 6f 77 73 20 2a 2f 0a 20 20 20 20 20 20 54 rows */. T
f9b0: 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 20 20 able *pSelTab;
f9c0: 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 74 68 /* A table th
f9d0: 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 at describes the
f9e0: 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20 SELECT results
f9f0: 2a 2f 0a 0a 20 20 20 20 20 20 72 65 67 59 69 65 */.. regYie
fa00: 6c 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e ld = ++pParse->n
fa10: 4d 65 6d 3b 0a 20 20 20 20 20 20 72 65 67 52 65 Mem;. regRe
fa20: 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d c = ++pParse->nM
fa30: 65 6d 3b 0a 20 20 20 20 20 20 72 65 67 52 6f 77 em;. regRow
fa40: 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e id = ++pParse->n
fa50: 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 Mem;. asser
fa60: 74 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 3d 3d t(pParse->nTab==
fa70: 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 1);. sqlite
fa80: 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 3MayAbort(pParse
fa90: 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
faa0: 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
fab0: 5f 4f 70 65 6e 57 72 69 74 65 2c 20 31 2c 20 70 _OpenWrite, 1, p
fac0: 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 Parse->regRoot,
fad0: 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 iDb);. sqli
fae0: 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 te3VdbeChangeP5(
faf0: 76 2c 20 4f 50 46 4c 41 47 5f 50 32 49 53 52 45 v, OPFLAG_P2ISRE
fb00: 47 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 G);. pParse
fb10: 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 20 ->nTab = 2;.
fb20: 20 20 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 addrTop = sqli
fb30: 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 te3VdbeCurrentAd
fb40: 64 72 28 76 29 20 2b 20 31 3b 0a 20 20 20 20 20 dr(v) + 1;.
fb50: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
fb60: 70 33 28 76 2c 20 4f 50 5f 49 6e 69 74 43 6f 72 p3(v, OP_InitCor
fb70: 6f 75 74 69 6e 65 2c 20 72 65 67 59 69 65 6c 64 outine, regYield
fb80: 2c 20 30 2c 20 61 64 64 72 54 6f 70 29 3b 0a 20 , 0, addrTop);.
fb90: 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
fba0: 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 ctDestInit(&dest
fbb0: 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c , SRT_Coroutine,
fbc0: 20 72 65 67 59 69 65 6c 64 29 3b 0a 20 20 20 20 regYield);.
fbd0: 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 sqlite3Select(
fbe0: 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c pParse, pSelect,
fbf0: 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 73 &dest);. s
fc00: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
fc10: 28 76 2c 20 4f 50 5f 45 6e 64 43 6f 72 6f 75 74 (v, OP_EndCorout
fc20: 69 6e 65 2c 20 72 65 67 59 69 65 6c 64 29 3b 0a ine, regYield);.
fc30: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
fc40: 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 eJumpHere(v, add
fc50: 72 54 6f 70 20 2d 20 31 29 3b 0a 20 20 20 20 20 rTop - 1);.
fc60: 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 if( pParse->nEr
fc70: 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 r ) return;.
fc80: 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 pSelTab = sqli
fc90: 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 te3ResultSetOfSe
fca0: 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 lect(pParse, pSe
fcb0: 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 69 66 28 lect);. if(
fcc0: 20 70 53 65 6c 54 61 62 3d 3d 30 20 29 20 72 65 pSelTab==0 ) re
fcd0: 74 75 72 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 turn;. asse
fce0: 72 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d 30 20 29 rt( p->aCol==0 )
fcf0: 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 ;. p->nCol
fd00: 3d 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b = pSelTab->nCol;
fd10: 0a 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d . p->aCol =
fd20: 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a pSelTab->aCol;.
fd30: 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e pSelTab->n
fd40: 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 Col = 0;. p
fd50: 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 SelTab->aCol = 0
fd60: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 ;. sqlite3D
fd70: 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 eleteTable(db, p
fd80: 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 61 SelTab);. a
fd90: 64 64 72 49 6e 73 4c 6f 6f 70 20 3d 20 73 71 6c ddrInsLoop = sql
fda0: 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
fdb0: 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 , OP_Yield, dest
fdc0: 2e 69 53 44 50 61 72 6d 29 3b 0a 20 20 20 20 20 .iSDParm);.
fdd0: 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
fde0: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
fdf0: 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
fe00: 4d 61 6b 65 52 65 63 6f 72 64 2c 20 64 65 73 74 MakeRecord, dest
fe10: 2e 69 53 64 73 74 2c 20 64 65 73 74 2e 6e 53 64 .iSdst, dest.nSd
fe20: 73 74 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20 st, regRec);.
fe30: 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 sqlite3TableA
fe40: 66 66 69 6e 69 74 79 28 76 2c 20 70 2c 20 30 29 ffinity(v, p, 0)
fe50: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
fe60: 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
fe70: 4e 65 77 52 6f 77 69 64 2c 20 31 2c 20 72 65 67 NewRowid, 1, reg
fe80: 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 Rowid);. sq
fe90: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
fea0: 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 31 2c v, OP_Insert, 1,
feb0: 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 regRec, regRowi
fec0: 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 d);. sqlite
fed0: 33 56 64 62 65 47 6f 74 6f 28 76 2c 20 61 64 64 3VdbeGoto(v, add
fee0: 72 49 6e 73 4c 6f 6f 70 29 3b 0a 20 20 20 20 20 rInsLoop);.
fef0: 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
ff00: 48 65 72 65 28 76 2c 20 61 64 64 72 49 6e 73 4c Here(v, addrInsL
ff10: 6f 6f 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 oop);. sqli
ff20: 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
ff30: 20 4f 50 5f 43 6c 6f 73 65 2c 20 31 29 3b 0a 20 OP_Close, 1);.
ff40: 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d }.. /* Com
ff50: 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 pute the complet
ff60: 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 e text of the CR
ff70: 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a EATE statement *
ff80: 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 /. if( pSelec
ff90: 74 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 6d 74 t ){. zStmt
ffa0: 20 3d 20 63 72 65 61 74 65 54 61 62 6c 65 53 74 = createTableSt
ffb0: 6d 74 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d mt(db, p);. }
ffc0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 54 6f 6b 65 else{. Toke
ffd0: 6e 20 2a 70 45 6e 64 32 20 3d 20 74 61 62 4f 70 n *pEnd2 = tabOp
ffe0: 74 73 20 3f 20 26 70 50 61 72 73 65 2d 3e 73 4c ts ? &pParse->sL
fff0: 61 73 74 54 6f 6b 65 6e 20 3a 20 70 45 6e 64 3b astToken : pEnd;
10000 0a 20 20 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 . n = (int)
10010 28 70 45 6e 64 32 2d 3e 7a 20 2d 20 70 50 61 72 (pEnd2->z - pPar
10020 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a se->sNameToken.z
10030 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 6e );. if( pEn
10040 64 32 2d 3e 7a 5b 30 5d 21 3d 27 3b 27 20 29 20 d2->z[0]!=';' )
10050 6e 20 2b 3d 20 70 45 6e 64 32 2d 3e 6e 3b 0a 20 n += pEnd2->n;.
10060 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c zStmt = sql
10070 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 ite3MPrintf(db,
10080 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41 . "CREA
10090 54 45 20 25 73 20 25 2e 2a 73 22 2c 20 7a 54 79 TE %s %.*s", zTy
100a0 70 65 32 2c 20 6e 2c 20 70 50 61 72 73 65 2d 3e pe2, n, pParse->
100b0 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20 sNameToken.z.
100c0 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 );. }..
100d0 20 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 /* A slot for t
100e0 68 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c he record has al
100f0 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 ready been alloc
10100 61 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20 20 ated in the .
10110 20 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 ** SQLITE_MASTE
10120 52 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73 R table. We jus
10130 74 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65 t need to update
10140 20 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20 that slot with
10150 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 all. ** the i
10160 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76 65 nformation we've
10170 20 63 6f 6c 6c 65 63 74 65 64 2e 0a 20 20 20 20 collected..
10180 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 */. sqlite3Ne
10190 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 stedParse(pParse
101a0 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 ,. "UPDATE
101b0 25 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 %Q.%s ".
101c0 20 22 53 45 54 20 74 79 70 65 3d 27 25 73 27 2c "SET type='%s',
101d0 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 name=%Q, tbl_na
101e0 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d me=%Q, rootpage=
101f0 23 25 64 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 #%d, sql=%Q ".
10200 20 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 "WHERE rowi
10210 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 d=#%d",. db
10220 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
10230 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 , SCHEMA_TABLE(i
10240 44 62 29 2c 0a 20 20 20 20 20 20 7a 54 79 70 65 Db),. zType
10250 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 ,. p->zName
10260 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 ,. p->zName
10270 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e ,. pParse->
10280 72 65 67 52 6f 6f 74 2c 0a 20 20 20 20 20 20 7a regRoot,. z
10290 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61 72 Stmt,. pPar
102a0 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 se->regRowid.
102b0 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 );. sqlite3D
102c0 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 bFree(db, zStmt)
102d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 ;. sqlite3Cha
102e0 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 ngeCookie(pParse
102f0 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 , iDb);..#ifndef
10300 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
10310 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f OINCREMENT. /
10320 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
10330 66 20 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65 f we need to cre
10340 61 74 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65 ate an sqlite_se
10350 71 75 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72 quence table for
10360 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20 . ** keeping
10370 74 72 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63 track of autoinc
10380 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20 rement keys..
10390 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 */. if( p->t
103a0 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 abFlags & TF_Aut
103b0 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 oincrement ){.
103c0 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 Db *pDb = &d
103d0 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 b->aDb[iDb];.
103e0 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
103f0 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c e3SchemaMutexHel
10400 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b d(db, iDb, 0) );
10410 0a 20 20 20 20 20 20 69 66 28 20 70 44 62 2d 3e . if( pDb->
10420 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 pSchema->pSeqTab
10430 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
10440 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 qlite3NestedPars
10450 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 e(pParse,.
10460 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c "CREATE TABL
10470 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 65 71 75 E %Q.sqlite_sequ
10480 65 6e 63 65 28 6e 61 6d 65 2c 73 65 71 29 22 2c ence(name,seq)",
10490 0a 20 20 20 20 20 20 20 20 20 20 70 44 62 2d 3e . pDb->
104a0 7a 4e 61 6d 65 0a 20 20 20 20 20 20 20 20 29 3b zName. );
104b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
104c0 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 52 65 endif.. /* Re
104d0 70 61 72 73 65 20 65 76 65 72 79 74 68 69 6e 67 parse everything
104e0 20 74 6f 20 75 70 64 61 74 65 20 6f 75 72 20 69 to update our i
104f0 6e 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 nternal data str
10500 75 63 74 75 72 65 73 20 2a 2f 0a 20 20 20 20 73 uctures */. s
10510 71 6c 69 74 65 33 56 64 62 65 41 64 64 50 61 72 qlite3VdbeAddPar
10520 73 65 53 63 68 65 6d 61 4f 70 28 76 2c 20 69 44 seSchemaOp(v, iD
10530 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 73 71 b,. sq
10540 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
10550 20 22 74 62 6c 5f 6e 61 6d 65 3d 27 25 71 27 20 "tbl_name='%q'
10560 41 4e 44 20 74 79 70 65 21 3d 27 74 72 69 67 67 AND type!='trigg
10570 65 72 27 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 29 er'", p->zName))
10580 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 ;. }... /* Add
10590 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 74 68 the table to th
105a0 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 e in-memory repr
105b0 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 esentation of th
105c0 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f e database.. */
105d0 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e . if( db->init.
105e0 62 75 73 79 20 29 7b 0a 20 20 20 20 54 61 62 6c busy ){. Tabl
105f0 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 53 63 68 e *pOld;. Sch
10600 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 ema *pSchema = p
10610 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 61 ->pSchema;. a
10620 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 ssert( sqlite3Sc
10630 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 hemaMutexHeld(db
10640 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 20 , iDb, 0) );.
10650 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 pOld = sqlite3H
10660 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68 65 ashInsert(&pSche
10670 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 2d 3e ma->tblHash, p->
10680 7a 4e 61 6d 65 2c 20 70 29 3b 0a 20 20 20 20 69 zName, p);. i
10690 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 f( pOld ){.
106a0 20 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 assert( p==pOld
106b0 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d ); /* Malloc m
106c0 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 ust have failed
106d0 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 inside HashInser
106e0 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d t() */. db-
106f0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
10700 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 1;. return;
10710 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 . }. pPars
10720 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 e->pNewTable = 0
10730 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 ;. db->flags
10740 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e |= SQLITE_Intern
10750 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66 6e 64 65 Changes;..#ifnde
10760 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c f SQLITE_OMIT_AL
10770 54 45 52 54 41 42 4c 45 0a 20 20 20 20 69 66 28 TERTABLE. if(
10780 20 21 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a !p->pSelect ){.
10790 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
107a0 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 *zName = (const
107b0 20 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e char *)pParse->
107c0 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 sNameToken.z;.
107d0 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 int nName;.
107e0 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 53 assert( !pS
107f0 65 6c 65 63 74 20 26 26 20 70 43 6f 6e 73 20 26 elect && pCons &
10800 26 20 70 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 & pEnd );.
10810 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 if( pCons->z==0
10820 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6e 73 ){. pCons
10830 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 7d = pEnd;. }
10840 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20 28 . nName = (
10850 69 6e 74 29 28 28 63 6f 6e 73 74 20 63 68 61 72 int)((const char
10860 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e *)pCons->z - zN
10870 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 ame);. p->a
10880 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 31 33 ddColOffset = 13
10890 20 2b 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 + sqlite3Utf8Ch
108a0 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e 4e 61 arLen(zName, nNa
108b0 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 me);. }.#endi
108c0 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 f. }.}..#ifndef
108d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 SQLITE_OMIT_VIE
108e0 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 W./*.** The pars
108f0 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f er calls this ro
10900 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 utine in order t
10910 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 56 o create a new V
10920 49 45 57 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 IEW.*/.void sqli
10930 74 65 33 43 72 65 61 74 65 56 69 65 77 28 0a 20 te3CreateView(.
10940 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
10950 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 /* The parsi
10960 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
10970 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20 20 Token *pBegin,
10980 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 /* The CREATE
10990 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 token that begi
109a0 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ns the statement
109b0 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 */. Token *pNa
109c0 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 me1, /* The
109d0 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 token that holds
109e0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
109f0 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e view */. Token
10a00 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a *pName2, /*
10a10 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 The token that
10a20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f holds the name o
10a30 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 f the view */.
10a40 45 78 70 72 4c 69 73 74 20 2a 70 43 4e 61 6d 65 ExprList *pCName
10a50 73 2c 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c s, /* Optional l
10a60 69 73 74 20 6f 66 20 76 69 65 77 20 63 6f 6c 75 ist of view colu
10a70 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 53 65 mn names */. Se
10a80 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 lect *pSelect,
10a90 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 /* A SELECT sta
10aa0 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c tement that will
10ab0 20 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 become the new
10ac0 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 view */. int is
10ad0 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 Temp, /*
10ae0 54 52 55 45 20 66 6f 72 20 61 20 54 45 4d 50 4f TRUE for a TEMPO
10af0 52 41 52 59 20 76 69 65 77 20 2a 2f 0a 20 20 69 RARY view */. i
10b00 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 nt noErr
10b10 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 65 72 /* Suppress er
10b20 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 66 20 ror messages if
10b30 56 49 45 57 20 61 6c 72 65 61 64 79 20 65 78 69 VIEW already exi
10b40 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c sts */.){. Tabl
10b50 65 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 e *p;. int n;.
10b60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a const char *z;.
10b70 20 20 54 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 Token sEnd;.
10b80 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a 20 20 DbFixer sFix;.
10b90 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30 Token *pName = 0
10ba0 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73 ;. int iDb;. s
10bb0 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
10bc0 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 rse->db;.. if(
10bd0 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30 20 29 pParse->nVar>0 )
10be0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
10bf0 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 orMsg(pParse, "p
10c00 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 6f arameters are no
10c10 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76 69 65 t allowed in vie
10c20 77 73 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 63 ws");. goto c
10c30 72 65 61 74 65 5f 76 69 65 77 5f 66 61 69 6c 3b reate_view_fail;
10c40 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 . }. sqlite3St
10c50 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c artTable(pParse,
10c60 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c pName1, pName2,
10c70 20 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e isTemp, 1, 0, n
10c80 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 61 oErr);. p = pPa
10c90 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
10ca0 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50 if( p==0 || pP
10cb0 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 67 6f 74 arse->nErr ) got
10cc0 6f 20 63 72 65 61 74 65 5f 76 69 65 77 5f 66 61 o create_view_fa
10cd0 69 6c 3b 0a 20 20 73 71 6c 69 74 65 33 54 77 6f il;. sqlite3Two
10ce0 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c PartName(pParse,
10cf0 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c pName1, pName2,
10d00 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20 &pName);. iDb
10d10 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
10d20 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 oIndex(db, p->pS
10d30 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 chema);. sqlite
10d40 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 3FixInit(&sFix,
10d50 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 76 69 pParse, iDb, "vi
10d60 65 77 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 ew", pName);. i
10d70 66 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c f( sqlite3FixSel
10d80 65 63 74 28 26 73 46 69 78 2c 20 70 53 65 6c 65 ect(&sFix, pSele
10d90 63 74 29 20 29 20 67 6f 74 6f 20 63 72 65 61 74 ct) ) goto creat
10da0 65 5f 76 69 65 77 5f 66 61 69 6c 3b 0a 0a 20 20 e_view_fail;..
10db0 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f /* Make a copy o
10dc0 66 20 74 68 65 20 65 6e 74 69 72 65 20 53 45 4c f the entire SEL
10dd0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 ECT statement th
10de0 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 76 at defines the v
10df0 69 65 77 2e 0a 20 20 2a 2a 20 54 68 69 73 20 77 iew.. ** This w
10e00 69 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20 74 68 ill force all th
10e10 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 e Expr.token.z v
10e20 61 6c 75 65 73 20 74 6f 20 62 65 20 64 79 6e 61 alues to be dyna
10e30 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c mically. ** all
10e40 6f 63 61 74 65 64 20 72 61 74 68 65 72 20 74 68 ocated rather th
10e50 61 6e 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 an point to the
10e60 69 6e 70 75 74 20 73 74 72 69 6e 67 20 2d 20 77 input string - w
10e70 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 0a hich means that.
10e80 20 20 2a 2a 20 74 68 65 79 20 77 69 6c 6c 20 70 ** they will p
10e90 65 72 73 69 73 74 20 61 66 74 65 72 20 74 68 65 ersist after the
10ea0 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33 current sqlite3
10eb0 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 72 65 74 _exec() call ret
10ec0 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e urns.. */. p->
10ed0 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 pSelect = sqlite
10ee0 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 3SelectDup(db, p
10ef0 53 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50 5f Select, EXPRDUP_
10f00 52 45 44 55 43 45 29 3b 0a 20 20 70 2d 3e 70 43 REDUCE);. p->pC
10f10 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78 heck = sqlite3Ex
10f20 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 43 prListDup(db, pC
10f30 4e 61 6d 65 73 2c 20 45 58 50 52 44 55 50 5f 52 Names, EXPRDUP_R
10f40 45 44 55 43 45 29 3b 0a 20 20 69 66 28 20 64 62 EDUCE);. if( db
10f50 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
10f60 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 76 69 65 goto create_vie
10f70 77 5f 66 61 69 6c 3b 0a 0a 20 20 2f 2a 20 4c 6f w_fail;.. /* Lo
10f80 63 61 74 65 20 74 68 65 20 65 6e 64 20 6f 66 20 cate the end of
10f90 74 68 65 20 43 52 45 41 54 45 20 56 49 45 57 20 the CREATE VIEW
10fa0 73 74 61 74 65 6d 65 6e 74 2e 20 20 4d 61 6b 65 statement. Make
10fb0 20 73 45 6e 64 20 70 6f 69 6e 74 20 74 6f 0a 20 sEnd point to.
10fc0 20 2a 2a 20 74 68 65 20 65 6e 64 2e 0a 20 20 2a ** the end.. *
10fd0 2f 0a 20 20 73 45 6e 64 20 3d 20 70 50 61 72 73 /. sEnd = pPars
10fe0 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 3b 0a 20 e->sLastToken;.
10ff0 20 61 73 73 65 72 74 28 20 73 45 6e 64 2e 7a 5b assert( sEnd.z[
11000 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 73 0]!=0 );. if( s
11010 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b End.z[0]!=';' ){
11020 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73 . sEnd.z += s
11030 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e End.n;. }. sEn
11040 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 28 d.n = 0;. n = (
11050 69 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20 70 42 int)(sEnd.z - pB
11060 65 67 69 6e 2d 3e 7a 29 3b 0a 20 20 61 73 73 65 egin->z);. asse
11070 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 7a 20 3d rt( n>0 );. z =
11080 20 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 pBegin->z;. wh
11090 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 ile( sqlite3Issp
110a0 61 63 65 28 7a 5b 6e 2d 31 5d 29 20 29 7b 20 6e ace(z[n-1]) ){ n
110b0 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d --; }. sEnd.z =
110c0 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 &z[n-1];. sEnd
110d0 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 .n = 1;.. /* Us
110e0 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c e sqlite3EndTabl
110f0 65 28 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 e() to add the v
11100 69 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 iew to the SQLIT
11110 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a E_MASTER table *
11120 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 /. sqlite3EndTa
11130 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 ble(pParse, 0, &
11140 73 45 6e 64 2c 20 30 2c 20 30 29 3b 0a 0a 63 72 sEnd, 0, 0);..cr
11150 65 61 74 65 5f 76 69 65 77 5f 66 61 69 6c 3a 0a eate_view_fail:.
11160 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
11170 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 elete(db, pSelec
11180 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 t);. sqlite3Exp
11190 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 rListDelete(db,
111a0 70 43 4e 61 6d 65 73 29 3b 0a 20 20 72 65 74 75 pCNames);. retu
111b0 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 rn;.}.#endif /*
111c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
111d0 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 */..#if !define
111e0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
111f0 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 EW) || !defined(
11200 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
11210 55 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 UALTABLE)./*.**
11220 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 The Table struct
11230 75 72 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 ure pTable is re
11240 61 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 ally a VIEW. Fi
11250 6c 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 ll in the names
11260 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e of.** the column
11270 73 20 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e s of the view in
11280 20 74 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 the pTable stru
11290 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 cture. Return t
112a0 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 he number.** of
112b0 65 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 errors. If an e
112c0 72 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 rror is seen lea
112d0 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ve an error mess
112e0 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a age in pParse->z
112f0 45 72 72 4d 73 67 2e 0a 2a 2f 0a 69 6e 74 20 73 ErrMsg..*/.int s
11300 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c qlite3ViewGetCol
11310 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a umnNames(Parse *
11320 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 pParse, Table *p
11330 54 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 Table){. Table
11340 2a 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 *pSelTab; /* A
11350 20 66 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d fake table from
11360 20 77 68 69 63 68 20 77 65 20 67 65 74 20 74 68 which we get th
11370 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a e result set */.
11380 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 Select *pSel;
11390 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 /* Copy of t
113a0 68 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 he SELECT that i
113b0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 mplements the vi
113c0 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 ew */. int nErr
113d0 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d = 0; /* Num
113e0 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e ber of errors en
113f0 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 countered */. i
11400 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 nt n;
11410 20 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 /* Temporarily
11420 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72 holds the number
11430 20 6f 66 20 63 75 72 73 6f 72 73 20 61 73 73 69 of cursors assi
11440 67 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 gned */. sqlite
11450 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
11460 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 db; /* Database
11470 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 connection for
11480 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f malloc errors */
11490 0a 20 20 73 71 6c 69 74 65 33 5f 78 61 75 74 68 . sqlite3_xauth
114a0 20 78 41 75 74 68 3b 20 20 20 20 20 20 20 2f 2a xAuth; /*
114b0 20 53 61 76 65 64 20 78 41 75 74 68 20 70 6f 69 Saved xAuth poi
114c0 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 62 45 6e nter */. u8 bEn
114d0 61 62 6c 65 64 4c 41 3b 20 20 20 20 20 20 20 20 abledLA;
114e0 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 64 62 /* Saved db
114f0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 ->lookaside.bEna
11500 62 6c 65 64 20 73 74 61 74 65 20 2a 2f 0a 0a 20 bled state */..
11510 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 20 assert( pTable
11520 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
11530 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
11540 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74 ABLE. if( sqlit
11550 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 e3VtabCallConnec
11560 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 t(pParse, pTable
11570 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
11580 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
11590 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 }. if( IsVirtua
115a0 6c 28 70 54 61 62 6c 65 29 20 29 20 72 65 74 75 l(pTable) ) retu
115b0 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 rn 0;.#endif..#i
115c0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
115d0 54 5f 56 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f T_VIEW. /* A po
115e0 73 69 74 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e sitive nCol mean
115f0 73 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 s the columns na
11600 6d 65 73 20 66 6f 72 20 74 68 69 73 20 76 69 65 mes for this vie
11610 77 20 61 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 w are. ** alrea
11620 64 79 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 dy known.. */.
11630 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f if( pTable->nCo
11640 6c 3e 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a l>0 ) return 0;.
11650 0a 20 20 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 . /* A negative
11660 20 6e 43 6f 6c 20 69 73 20 61 20 73 70 65 63 69 nCol is a speci
11670 61 6c 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e al marker meanin
11680 67 20 74 68 61 74 20 77 65 20 61 72 65 20 63 75 g that we are cu
11690 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 rrently. ** try
116a0 69 6e 67 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 ing to compute t
116b0 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e he column names.
116c0 20 20 49 66 20 77 65 20 65 6e 74 65 72 20 74 68 If we enter th
116d0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 0a is routine with.
116e0 20 20 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20 ** a negative
116f0 6e 43 6f 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 nCol, it means t
11700 77 6f 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 wo or more views
11710 20 66 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 form a loop, li
11720 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 ke this:. **.
11730 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49 ** CREATE VI
11740 45 57 20 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 EW one AS SELECT
11750 20 2a 20 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a * FROM two;. *
11760 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49 45 * CREATE VIE
11770 57 20 74 77 6f 20 41 53 20 53 45 4c 45 43 54 20 W two AS SELECT
11780 2a 20 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a * FROM one;. **
11790 0a 20 20 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 . ** Actually,
117a0 74 68 65 20 65 72 72 6f 72 20 61 62 6f 76 65 20 the error above
117b0 69 73 20 6e 6f 77 20 63 61 75 67 68 74 20 70 72 is now caught pr
117c0 69 6f 72 20 74 6f 20 72 65 61 63 68 69 6e 67 20 ior to reaching
117d0 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2a this point.. **
117e0 20 42 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 But the followi
117f0 6e 67 20 74 65 73 74 20 69 73 20 73 74 69 6c 6c ng test is still
11800 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 20 69 74 important as it
11810 20 64 6f 65 73 20 63 6f 6d 65 20 75 70 0a 20 20 does come up.
11820 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 ** in the follow
11830 69 6e 67 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 ing:. ** . **
11840 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 CREATE TABLE
11850 20 6d 61 69 6e 2e 65 78 31 28 61 29 3b 0a 20 20 main.ex1(a);.
11860 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 45 ** CREATE TE
11870 4d 50 20 56 49 45 57 20 65 78 31 20 41 53 20 53 MP VIEW ex1 AS S
11880 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 65 78 31 ELECT a FROM ex1
11890 3b 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 ;. ** SELEC
118a0 54 20 2a 20 46 52 4f 4d 20 74 65 6d 70 2e 65 78 T * FROM temp.ex
118b0 31 3b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 1;. */. if( pT
118c0 61 62 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a able->nCol<0 ){.
118d0 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
118e0 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65 Msg(pParse, "vie
118f0 77 20 25 73 20 69 73 20 63 69 72 63 75 6c 61 72 w %s is circular
11900 6c 79 20 64 65 66 69 6e 65 64 22 2c 20 70 54 61 ly defined", pTa
11910 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 ble->zName);.
11920 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
11930 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d assert( pTable-
11940 3e 6e 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f >nCol>=0 );.. /
11950 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69 73 * If we get this
11960 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 far, it means w
11970 65 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 e need to comput
11980 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 e the table name
11990 73 2e 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 s.. ** Note tha
119a0 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 t the call to sq
119b0 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 lite3ResultSetOf
119c0 53 65 6c 65 63 74 28 29 20 77 69 6c 6c 20 65 78 Select() will ex
119d0 70 61 6e 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a pand any. ** "*
119e0 22 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 " elements in th
119f0 65 20 72 65 73 75 6c 74 73 20 73 65 74 20 6f 66 e results set of
11a00 20 74 68 65 20 76 69 65 77 20 61 6e 64 20 77 69 the view and wi
11a10 6c 6c 20 61 73 73 69 67 6e 20 63 75 72 73 6f 72 ll assign cursor
11a20 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c s. ** to the el
11a30 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 46 52 ements of the FR
11a40 4f 4d 20 63 6c 61 75 73 65 2e 20 20 42 75 74 20 OM clause. But
11a50 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 we do not want t
11a60 68 65 73 65 20 63 68 61 6e 67 65 73 0a 20 20 2a hese changes. *
11a70 2a 20 74 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e * to be permanen
11a80 74 2e 20 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 t. So the compu
11a90 74 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f tation is done o
11aa0 6e 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 n a copy of the
11ab0 53 45 4c 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 SELECT. ** stat
11ac0 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e ement that defin
11ad0 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a es the view.. *
11ae0 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 /. assert( pTab
11af0 6c 65 2d 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 le->pSelect );.
11b00 20 62 45 6e 61 62 6c 65 64 4c 41 20 3d 20 64 62 bEnabledLA = db
11b10 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 ->lookaside.bEna
11b20 62 6c 65 64 3b 0a 20 20 69 66 28 20 70 54 61 62 bled;. if( pTab
11b30 6c 65 2d 3e 70 43 68 65 63 6b 20 29 7b 0a 20 20 le->pCheck ){.
11b40 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e db->lookaside.
11b50 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 bEnabled = 0;.
11b60 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 73 sqlite3Columns
11b70 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 FromExprList(pPa
11b80 72 73 65 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68 rse, pTable->pCh
11b90 65 63 6b 2c 20 0a 20 20 20 20 20 20 20 20 20 20 eck, .
11ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11bb0 20 20 20 20 20 26 70 54 61 62 6c 65 2d 3e 6e 43 &pTable->nC
11bc0 6f 6c 2c 20 26 70 54 61 62 6c 65 2d 3e 61 43 6f ol, &pTable->aCo
11bd0 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 l);. }else{.
11be0 20 70 53 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 pSel = sqlite3S
11bf0 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54 61 electDup(db, pTa
11c00 62 6c 65 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 ble->pSelect, 0)
11c10 3b 0a 20 20 20 20 69 66 28 20 70 53 65 6c 20 29 ;. if( pSel )
11c20 7b 0a 20 20 20 20 20 20 6e 20 3d 20 70 50 61 72 {. n = pPar
11c30 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 20 20 se->nTab;.
11c40 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 sqlite3SrcListAs
11c50 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72 signCursors(pPar
11c60 73 65 2c 20 70 53 65 6c 2d 3e 70 53 72 63 29 3b se, pSel->pSrc);
11c70 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e . pTable->n
11c80 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 Col = -1;.
11c90 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 db->lookaside.bE
11ca0 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23 69 66 6e nabled = 0;.#ifn
11cb0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
11cc0 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 AUTHORIZATION.
11cd0 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e xAuth = db->
11ce0 78 41 75 74 68 3b 0a 20 20 20 20 20 20 64 62 2d xAuth;. db-
11cf0 3e 78 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 >xAuth = 0;.
11d00 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 pSelTab = sqli
11d10 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 te3ResultSetOfSe
11d20 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 lect(pParse, pSe
11d30 6c 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78 41 l);. db->xA
11d40 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 23 65 6c uth = xAuth;.#el
11d50 73 65 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 se. pSelTab
11d60 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 = sqlite3Result
11d70 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 SetOfSelect(pPar
11d80 73 65 2c 20 70 53 65 6c 29 3b 0a 23 65 6e 64 69 se, pSel);.#endi
11d90 66 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e f. pParse->
11da0 6e 54 61 62 20 3d 20 6e 3b 0a 20 20 20 20 20 20 nTab = n;.
11db0 69 66 28 20 70 53 65 6c 54 61 62 20 29 7b 0a 20 if( pSelTab ){.
11dc0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
11dd0 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29 Table->aCol==0 )
11de0 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 ;. pTable
11df0 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 ->nCol = pSelTab
11e00 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 ->nCol;.
11e10 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 pTable->aCol = p
11e20 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 SelTab->aCol;.
11e30 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e pSelTab->n
11e40 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 Col = 0;.
11e50 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d pSelTab->aCol =
11e60 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 0;. sqli
11e70 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 64 te3DeleteTable(d
11e80 62 2c 20 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 b, pSelTab);.
11e90 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
11ea0 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 ite3SchemaMutexH
11eb0 65 6c 64 28 64 62 2c 20 30 2c 20 70 54 61 62 6c eld(db, 0, pTabl
11ec0 65 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 e->pSchema) );.
11ed0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
11ee0 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c pTable->nCol
11ef0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6e 45 = 0;. nE
11f00 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rr++;. }.
11f10 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
11f20 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c tDelete(db, pSel
11f30 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a );. } else {.
11f40 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 nErr++;.
11f50 20 20 7d 0a 20 20 7d 0a 20 20 64 62 2d 3e 6c 6f }. }. db->lo
11f60 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 okaside.bEnabled
11f70 20 3d 20 62 45 6e 61 62 6c 65 64 4c 41 3b 0a 20 = bEnabledLA;.
11f80 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 pTable->pSchema
11f90 2d 3e 73 63 68 65 6d 61 46 6c 61 67 73 20 7c 3d ->schemaFlags |=
11fa0 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 DB_UnresetViews
11fb0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ;.#endif /* SQLI
11fc0 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a TE_OMIT_VIEW */.
11fd0 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b 20 20 return nErr;
11fe0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 .}.#endif /* !de
11ff0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
12000 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 T_VIEW) || !defi
12010 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
12020 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20 2a 2f VIRTUALTABLE) */
12030 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
12040 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a 2a _OMIT_VIEW./*.**
12050 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d Clear the colum
12060 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 65 76 65 n names from eve
12070 72 79 20 56 49 45 57 20 69 6e 20 64 61 74 61 62 ry VIEW in datab
12080 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73 74 61 74 ase idx..*/.stat
12090 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 56 69 ic void sqliteVi
120a0 65 77 52 65 73 65 74 41 6c 6c 28 73 71 6c 69 74 ewResetAll(sqlit
120b0 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 64 78 29 e3 *db, int idx)
120c0 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b {. HashElem *i;
120d0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
120e0 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c e3SchemaMutexHel
120f0 64 28 64 62 2c 20 69 64 78 2c 20 30 29 20 29 3b d(db, idx, 0) );
12100 0a 20 20 69 66 28 20 21 44 62 48 61 73 50 72 6f . if( !DbHasPro
12110 70 65 72 74 79 28 64 62 2c 20 69 64 78 2c 20 44 perty(db, idx, D
12120 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 29 20 B_UnresetViews)
12130 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 ) return;. for(
12140 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 i=sqliteHashFirs
12150 74 28 26 64 62 2d 3e 61 44 62 5b 69 64 78 5d 2e t(&db->aDb[idx].
12160 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 pSchema->tblHash
12170 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 ); i;i=sqliteHas
12180 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 54 hNext(i)){. T
12190 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c able *pTab = sql
121a0 69 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a iteHashData(i);.
121b0 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 if( pTab->pS
121c0 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 73 elect ){. s
121d0 71 6c 69 74 65 33 44 65 6c 65 74 65 43 6f 6c 75 qlite3DeleteColu
121e0 6d 6e 4e 61 6d 65 73 28 64 62 2c 20 70 54 61 62 mnNames(db, pTab
121f0 29 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 61 );. pTab->a
12200 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 Col = 0;. p
12210 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 Tab->nCol = 0;.
12220 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c 65 }. }. DbCle
12230 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 arProperty(db, i
12240 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 dx, DB_UnresetVi
12250 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 ews);.}.#else.#
12260 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69 65 define sqliteVie
12270 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a 23 wResetAll(A,B).#
12280 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
12290 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f 2a OMIT_VIEW */../*
122a0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
122b0 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 n is called by t
122c0 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75 73 he VDBE to adjus
122d0 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 t the internal s
122e0 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62 79 chema.** used by
122f0 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68 65 SQLite when the
12300 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f 76 btree layer mov
12310 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74 20 es a table root
12320 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f 6f page. The.** roo
12330 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62 6c t-page of a tabl
12340 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64 61 e or index in da
12350 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20 63 tabase iDb has c
12360 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72 6f hanged from iFro
12370 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a 0a m.** to iTo..**.
12380 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38 3a ** Ticket #1728:
12390 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 The symbol tab
123a0 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 63 le might still c
123b0 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74 69 ontain informati
123c0 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73 20 on.** on tables
123d0 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20 74 and/or indices t
123e0 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f 63 hat are the proc
123f0 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65 6c ess of being del
12400 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75 20 eted..** If you
12410 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e 65 are unlucky, one
12420 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74 65 of those delete
12430 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61 62 d indices or tab
12440 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76 les might.** hav
12450 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 70 e the same rootp
12460 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74 68 age number as th
12470 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72 20 e real table or
12480 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a 2a index that is.**
12490 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20 53 being moved. S
124a0 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 70 o we cannot stop
124b0 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65 72 searching after
124c0 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 the first match
124d0 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65 .** because the
124e0 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69 67 first match mig
124f0 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f 66 ht be for one of
12500 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e 64 the deleted ind
12510 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c 65 ices.** or table
12520 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74 61 s and not the ta
12530 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20 69 ble/index that i
12540 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 s actually being
12550 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d 75 moved..** We mu
12560 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f 70 st continue loop
12570 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74 61 ing until all ta
12580 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 bles and indices
12590 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61 67 with.** rootpag
125a0 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62 65 e==iFrom have be
125b0 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 en converted to
125c0 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65 20 have a rootpage
125d0 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72 64 of iTo.** in ord
125e0 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69 6e er to be certain
125f0 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68 65 that we got the
12600 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a 23 right one..*/.#
12610 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
12620 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 76 6f IT_AUTOVACUUM.vo
12630 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 id sqlite3RootPa
12640 67 65 4d 6f 76 65 64 28 73 71 6c 69 74 65 33 20 geMoved(sqlite3
12650 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 69 6e *db, int iDb, in
12660 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f t iFrom, int iTo
12670 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 ){. HashElem *p
12680 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20 2a 70 48 Elem;. Hash *pH
12690 61 73 68 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a ash;. Db *pDb;.
126a0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
126b0 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c e3SchemaMutexHel
126c0 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b d(db, iDb, 0) );
126d0 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 . pDb = &db->aD
126e0 62 5b 69 44 62 5d 3b 0a 20 20 70 48 61 73 68 20 b[iDb];. pHash
126f0 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d = &pDb->pSchema-
12700 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72 28 >tblHash;. for(
12710 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 pElem=sqliteHash
12720 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70 45 First(pHash); pE
12730 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 lem; pElem=sqlit
12740 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 eHashNext(pElem)
12750 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 ){. Table *pT
12760 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 ab = sqliteHashD
12770 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20 ata(pElem);.
12780 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d 3d if( pTab->tnum==
12790 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 70 iFrom ){. p
127a0 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b Tab->tnum = iTo;
127b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48 61 . }. }. pHa
127c0 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 sh = &pDb->pSche
127d0 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20 66 ma->idxHash;. f
127e0 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 or(pElem=sqliteH
127f0 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b ashFirst(pHash);
12800 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 pElem; pElem=sq
12810 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c liteHashNext(pEl
12820 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 em)){. Index
12830 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48 61 *pIdx = sqliteHa
12840 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 shData(pElem);.
12850 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75 if( pIdx->tnu
12860 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 m==iFrom ){.
12870 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20 69 pIdx->tnum = i
12880 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a To;. }. }.}.
12890 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 #endif../*.** Wr
128a0 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61 73 ite code to eras
128b0 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 e the table with
128c0 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c root-page iTabl
128d0 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 e from database
128e0 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72 69 iDb..** Also wri
128f0 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69 66 te code to modif
12900 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 y the sqlite_mas
12910 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69 6e ter table and in
12920 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a ternal schema.**
12930 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 if a root-page
12940 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 of another table
12950 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68 65 is moved by the
12960 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68 69 btree-layer whi
12970 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20 69 lst.** erasing i
12980 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e 20 Table (this can
12990 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 happen with an a
129a0 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
129b0 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 ase)..*/ .static
129c0 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f 6f void destroyRoo
129d0 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50 61 tPage(Parse *pPa
129e0 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c rse, int iTable,
129f0 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62 int iDb){. Vdb
12a00 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 e *v = sqlite3Ge
12a10 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
12a20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 int r1 = sqlite
12a30 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 3GetTempReg(pPar
12a40 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 se);. sqlite3Vd
12a50 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 beAddOp3(v, OP_D
12a60 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c 20 estroy, iTable,
12a70 72 31 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 r1, iDb);. sqli
12a80 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 te3MayAbort(pPar
12a90 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c se);.#ifndef SQL
12aa0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
12ab0 55 55 4d 0a 20 20 2f 2a 20 4f 50 5f 44 65 73 74 UUM. /* OP_Dest
12ac0 72 6f 79 20 73 74 6f 72 65 73 20 61 6e 20 69 6e roy stores an in
12ad0 20 69 6e 74 65 67 65 72 20 72 31 2e 20 49 66 20 integer r1. If
12ae0 74 68 69 73 20 69 6e 74 65 67 65 72 0a 20 20 2a this integer. *
12af0 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 * is non-zero, t
12b00 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 72 6f hen it is the ro
12b10 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f ot page number o
12b20 66 20 61 20 74 61 62 6c 65 20 6d 6f 76 65 64 20 f a table moved
12b30 74 6f 0a 20 20 2a 2a 20 6c 6f 63 61 74 69 6f 6e to. ** location
12b40 20 69 54 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c iTable. The fol
12b50 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6d 6f 64 69 lowing code modi
12b60 66 69 65 73 20 74 68 65 20 73 71 6c 69 74 65 5f fies the sqlite_
12b70 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 6f 0a master table to.
12b80 20 20 2a 2a 20 72 65 66 6c 65 63 74 20 74 68 69 ** reflect thi
12b90 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 s.. **. ** The
12ba0 20 22 23 4e 4e 4e 22 20 69 6e 20 74 68 65 20 53 "#NNN" in the S
12bb0 51 4c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 QL is a special
12bc0 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 6d 65 constant that me
12bd0 61 6e 73 20 77 68 61 74 65 76 65 72 20 76 61 6c ans whatever val
12be0 75 65 0a 20 20 2a 2a 20 69 73 20 69 6e 20 72 65 ue. ** is in re
12bf0 67 69 73 74 65 72 20 4e 4e 4e 2e 20 20 53 65 65 gister NNN. See
12c00 20 67 72 61 6d 6d 61 72 20 72 75 6c 65 73 20 61 grammar rules a
12c10 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
12c20 68 65 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 20 he TK_REGISTER.
12c30 20 2a 2a 20 74 6f 6b 65 6e 20 66 6f 72 20 61 64 ** token for ad
12c40 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
12c50 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c tion.. */. sql
12c60 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
12c70 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55 pParse, . "U
12c80 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20 PDATE %Q.%s SET
12c90 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52 rootpage=%d WHER
12ca0 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61 E #%d AND rootpa
12cb0 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50 ge=#%d",. pP
12cc0 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 arse->db->aDb[iD
12cd0 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 b].zName, SCHEMA
12ce0 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61 _TABLE(iDb), iTa
12cf0 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65 ble, r1, r1);.#e
12d00 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52 65 ndif. sqlite3Re
12d10 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
12d20 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a rse, r1);.}../*.
12d30 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20 63 6f ** Write VDBE co
12d40 64 65 20 74 6f 20 65 72 61 73 65 20 74 61 62 6c de to erase tabl
12d50 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20 61 e pTab and all a
12d60 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63 65 ssociated indice
12d70 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f s on disk..** Co
12d80 64 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 de to update the
12d90 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
12da0 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72 6e ables and intern
12db0 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e 69 al schema defini
12dc0 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65 tions.** in case
12dd0 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c a root-page bel
12de0 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65 onging to anothe
12df0 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 r table is moved
12e00 20 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 by the btree la
12e10 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61 yer.** is also a
12e20 64 64 65 64 20 28 74 68 69 73 20 63 61 6e 20 68 dded (this can h
12e30 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75 appen with an au
12e40 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 to-vacuum databa
12e50 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 se)..*/.static v
12e60 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c 65 oid destroyTable
12e70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
12e80 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69 Table *pTab){.#i
12e90 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
12ea0 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e _AUTOVACUUM. In
12eb0 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 dex *pIdx;. int
12ec0 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
12ed0 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 hemaToIndex(pPar
12ee0 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 se->db, pTab->pS
12ef0 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f chema);. destro
12f00 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 yRootPage(pParse
12f10 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 , pTab->tnum, iD
12f20 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 b);. for(pIdx=p
12f30 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 Tab->pIndex; pId
12f40 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e x; pIdx=pIdx->pN
12f50 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72 6f ext){. destro
12f60 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 yRootPage(pParse
12f70 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 , pIdx->tnum, iD
12f80 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 b);. }.#else.
12f90 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 /* If the databa
12fa0 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d 76 se may be auto-v
12fb0 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28 69 acuum capable (i
12fc0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
12fd0 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73 TOVACUUM. ** is
12fe0 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 74 not defined), t
12ff0 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72 74 hen it is import
13000 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44 ant to call OP_D
13010 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20 estroy on the.
13020 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 ** table and ind
13030 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e ex root-pages in
13040 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e 67 order, starting
13050 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72 69 with the numeri
13060 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67 cally . ** larg
13070 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 est root-page nu
13080 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61 72 61 mber. This guara
13090 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65 20 ntees that none
130a0 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 of the root-page
130b0 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73 s. ** to be des
130c0 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61 troyed is reloca
130d0 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65 ted by an earlie
130e0 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e r OP_Destroy. i.
130f0 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 66 e. if the. ** f
13100 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f ollowing were co
13110 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f ded:. **. ** O
13120 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20 20 P_Destroy 4 0.
13130 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44 ** .... ** OP_D
13140 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a estroy 5 0. **.
13150 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61 ** and root pa
13160 67 65 20 35 20 68 61 70 70 65 6e 65 64 20 74 6f ge 5 happened to
13170 20 62 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 be the largest
13180 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 root-page number
13190 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 in the. ** dat
131a0 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74 abase, then root
131b0 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62 65 page 5 would be
131c0 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20 34 moved to page 4
131d0 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f by the . ** "O
131e0 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20 6f P_Destroy 4 0" o
131f0 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73 65 pcode. The subse
13200 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f quent "OP_Destro
13210 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69 74 y 5 0" would hit
13220 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73 . ** a free-lis
13230 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 t page.. */. i
13240 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d 3e nt iTab = pTab->
13250 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73 tnum;. int iDes
13260 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77 troyed = 0;.. w
13270 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 49 hile( 1 ){. I
13280 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 ndex *pIdx;.
13290 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30 int iLargest = 0
132a0 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73 74 ;.. if( iDest
132b0 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62 royed==0 || iTab
132c0 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a 20 <iDestroyed ){.
132d0 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20 iLargest =
132e0 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20 iTab;. }.
132f0 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 for(pIdx=pTab->p
13300 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 Index; pIdx; pId
13310 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a x=pIdx->pNext){.
13320 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d int iIdx =
13330 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 pIdx->tnum;.
13340 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d assert( pIdx-
13350 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e >pSchema==pTab->
13360 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 pSchema );.
13370 20 69 66 28 20 28 69 44 65 73 74 72 6f 79 65 64 if( (iDestroyed
13380 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44 65 ==0 || (iIdx<iDe
13390 73 74 72 6f 79 65 64 29 29 20 26 26 20 69 49 64 stroyed)) && iId
133a0 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20 20 x>iLargest ){.
133b0 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d iLargest =
133c0 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20 iIdx;. }.
133d0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c 61 }. if( iLa
133e0 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 rgest==0 ){.
133f0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65 return;. }e
13400 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 lse{. int i
13410 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
13420 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 maToIndex(pParse
13430 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 ->db, pTab->pSch
13440 65 6d 61 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ema);. asse
13450 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 rt( iDb>=0 && iD
13460 62 3c 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44 b<pParse->db->nD
13470 62 20 29 3b 0a 20 20 20 20 20 20 64 65 73 74 72 b );. destr
13480 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 oyRootPage(pPars
13490 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44 62 e, iLargest, iDb
134a0 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74 72 6f );. iDestro
134b0 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b 0a yed = iLargest;.
134c0 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
134d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 .}../*.** Remove
134e0 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 entries from th
134f0 65 20 73 71 6c 69 74 65 5f 73 74 61 74 4e 20 74 e sqlite_statN t
13500 61 62 6c 65 73 20 28 66 6f 72 20 4e 20 69 6e 20 ables (for N in
13510 28 31 2c 32 2c 33 29 29 0a 2a 2a 20 61 66 74 65 (1,2,3)).** afte
13520 72 20 61 20 44 52 4f 50 20 49 4e 44 45 58 20 6f r a DROP INDEX o
13530 72 20 44 52 4f 50 20 54 41 42 4c 45 20 63 6f 6d r DROP TABLE com
13540 6d 61 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mand..*/.static
13550 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 65 61 void sqlite3Clea
13560 72 53 74 61 74 54 61 62 6c 65 73 28 0a 20 20 50 rStatTables(. P
13570 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
13580 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 /* The par
13590 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
135a0 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 int iDb,
135b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
135c0 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 database number
135d0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
135e0 2a 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 *zType, /* "
135f0 69 64 78 22 20 6f 72 20 22 74 62 6c 22 20 2a 2f idx" or "tbl" */
13600 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
13610 4e 61 6d 65 20 20 20 20 20 20 2f 2a 20 4e 61 6d Name /* Nam
13620 65 20 6f 66 20 69 6e 64 65 78 20 6f 72 20 74 61 e of index or ta
13630 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ble */.){. int
13640 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 i;. const char
13650 2a 7a 44 62 4e 61 6d 65 20 3d 20 70 50 61 72 73 *zDbName = pPars
13660 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e e->db->aDb[iDb].
13670 7a 4e 61 6d 65 3b 0a 20 20 66 6f 72 28 69 3d 31 zName;. for(i=1
13680 3b 20 69 3c 3d 34 3b 20 69 2b 2b 29 7b 0a 20 20 ; i<=4; i++){.
13690 20 20 63 68 61 72 20 7a 54 61 62 5b 32 34 5d 3b char zTab[24];
136a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
136b0 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 54 61 rintf(sizeof(zTa
136c0 62 29 2c 7a 54 61 62 2c 22 73 71 6c 69 74 65 5f b),zTab,"sqlite_
136d0 73 74 61 74 25 64 22 2c 69 29 3b 0a 20 20 20 20 stat%d",i);.
136e0 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 if( sqlite3FindT
136f0 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c able(pParse->db,
13700 20 7a 54 61 62 2c 20 7a 44 62 4e 61 6d 65 29 20 zTab, zDbName)
13710 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
13720 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 NestedParse(pPar
13730 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c se,. "DEL
13740 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 ETE FROM %Q.%s W
13750 48 45 52 45 20 25 73 3d 25 51 22 2c 0a 20 20 20 HERE %s=%Q",.
13760 20 20 20 20 20 7a 44 62 4e 61 6d 65 2c 20 7a 54 zDbName, zT
13770 61 62 2c 20 7a 54 79 70 65 2c 20 7a 4e 61 6d 65 ab, zType, zName
13780 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a . );. }.
13790 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}../*.** Gen
137a0 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 72 erate code to dr
137b0 6f 70 20 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a 76 op a table..*/.v
137c0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 44 oid sqlite3CodeD
137d0 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a ropTable(Parse *
137e0 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 pParse, Table *p
137f0 54 61 62 2c 20 69 6e 74 20 69 44 62 2c 20 69 6e Tab, int iDb, in
13800 74 20 69 73 56 69 65 77 29 7b 0a 20 20 56 64 62 t isView){. Vdb
13810 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 e *v;. sqlite3
13820 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
13830 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 ;. Trigger *pTr
13840 69 67 67 65 72 3b 0a 20 20 44 62 20 2a 70 44 62 igger;. Db *pDb
13850 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d = &db->aDb[iDb]
13860 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 ;.. v = sqlite3
13870 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
13880 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 . assert( v!=0
13890 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 );. sqlite3Begi
138a0 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 nWriteOperation(
138b0 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b pParse, 1, iDb);
138c0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
138d0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
138e0 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 LE. if( IsVirtu
138f0 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 al(pTab) ){.
13900 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
13910 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0(v, OP_VBegin);
13920 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
13930 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72 69 67 67 * Drop all trigg
13940 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ers associated w
13950 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 62 65 ith the table be
13960 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 43 6f 64 ing dropped. Cod
13970 65 0a 20 20 2a 2a 20 69 73 20 67 65 6e 65 72 61 e. ** is genera
13980 74 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 65 6e ted to remove en
13990 74 72 69 65 73 20 66 72 6f 6d 20 73 71 6c 69 74 tries from sqlit
139a0 65 5f 6d 61 73 74 65 72 20 61 6e 64 2f 6f 72 0a e_master and/or.
139b0 20 20 2a 2a 20 73 71 6c 69 74 65 5f 74 65 6d 70 ** sqlite_temp
139c0 5f 6d 61 73 74 65 72 20 69 66 20 72 65 71 75 69 _master if requi
139d0 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 54 72 69 red.. */. pTri
139e0 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 gger = sqlite3Tr
139f0 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 iggerList(pParse
13a00 2c 20 70 54 61 62 29 3b 0a 20 20 77 68 69 6c 65 , pTab);. while
13a10 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 ( pTrigger ){.
13a20 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 assert( pTrigg
13a30 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 er->pSchema==pTa
13a40 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a 20 b->pSchema || .
13a50 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d pTrigger-
13a60 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 >pSchema==db->aD
13a70 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a b[1].pSchema );.
13a80 20 20 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 sqlite3DropT
13a90 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65 riggerPtr(pParse
13aa0 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 , pTrigger);.
13ab0 20 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 69 pTrigger = pTri
13ac0 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d gger->pNext;. }
13ad0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
13ae0 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
13af0 45 4e 54 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 ENT. /* Remove
13b00 61 6e 79 20 65 6e 74 72 69 65 73 20 6f 66 20 74 any entries of t
13b10 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e he sqlite_sequen
13b20 63 65 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 ce table associa
13b30 74 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 74 68 ted with. ** th
13b40 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 e table being dr
13b50 6f 70 70 65 64 2e 20 54 68 69 73 20 69 73 20 64 opped. This is d
13b60 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 one before the t
13b70 61 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 0a able is dropped.
13b80 20 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65 ** at the btre
13b90 65 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 e level, in case
13ba0 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 the sqlite_sequ
13bb0 65 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73 ence table needs
13bc0 20 74 6f 0a 20 20 2a 2a 20 6d 6f 76 65 20 61 73 to. ** move as
13bd0 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 a result of the
13be0 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70 65 drop (can happe
13bf0 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d n in auto-vacuum
13c00 20 6d 6f 64 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 mode).. */. i
13c10 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 f( pTab->tabFlag
13c20 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 s & TF_Autoincre
13c30 6d 65 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 ment ){. sqli
13c40 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 te3NestedParse(p
13c50 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 44 45 Parse,. "DE
13c60 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c LETE FROM %Q.sql
13c70 69 74 65 5f 73 65 71 75 65 6e 63 65 20 57 48 45 ite_sequence WHE
13c80 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20 RE name=%Q",.
13c90 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 pDb->zName, p
13ca0 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 Tab->zName. )
13cb0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
13cc0 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53 51 4c 49 /* Drop all SQLI
13cd0 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 TE_MASTER table
13ce0 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 69 65 and index entrie
13cf0 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 s that refer to
13d00 74 68 65 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20 the. ** table.
13d10 54 68 65 20 70 72 6f 67 72 61 6d 20 6e 61 6d 65 The program name
13d20 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 74 loops through t
13d30 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 he master table
13d40 61 6e 64 20 64 65 6c 65 74 65 73 0a 20 20 2a 2a and deletes. **
13d50 20 65 76 65 72 79 20 72 6f 77 20 74 68 61 74 20 every row that
13d60 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c refers to a tabl
13d70 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 e of the same na
13d80 6d 65 20 61 73 20 74 68 65 20 6f 6e 65 20 62 65 me as the one be
13d90 69 6e 67 0a 20 20 2a 2a 20 64 72 6f 70 70 65 64 ing. ** dropped
13da0 2e 20 54 72 69 67 67 65 72 73 20 61 72 65 20 68 . Triggers are h
13db0 61 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65 6c andled separatel
13dc0 79 20 62 65 63 61 75 73 65 20 61 20 74 72 69 67 y because a trig
13dd0 67 65 72 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 ger can be. **
13de0 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 74 created in the t
13df0 65 6d 70 20 64 61 74 61 62 61 73 65 20 74 68 61 emp database tha
13e00 74 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 t refers to a ta
13e10 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65 72 0a 20 ble in another.
13e20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 20 20 ** database..
13e30 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 */. sqlite3Nest
13e40 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 edParse(pParse,
13e50 0a 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 . "DELETE F
13e60 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20 ROM %Q.%s WHERE
13e70 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 20 tbl_name=%Q and
13e80 74 79 70 65 21 3d 27 74 72 69 67 67 65 72 27 22 type!='trigger'"
13e90 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 ,. pDb->zNa
13ea0 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 me, SCHEMA_TABLE
13eb0 28 69 44 62 29 2c 20 70 54 61 62 2d 3e 7a 4e 61 (iDb), pTab->zNa
13ec0 6d 65 29 3b 0a 20 20 69 66 28 20 21 69 73 56 69 me);. if( !isVi
13ed0 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61 6c ew && !IsVirtual
13ee0 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 64 65 (pTab) ){. de
13ef0 73 74 72 6f 79 54 61 62 6c 65 28 70 50 61 72 73 stroyTable(pPars
13f00 65 2c 20 70 54 61 62 29 3b 0a 20 20 7d 0a 0a 20 e, pTab);. }..
13f10 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 74 /* Remove the t
13f20 61 62 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 able entry from
13f30 53 51 4c 69 74 65 27 73 20 69 6e 74 65 72 6e 61 SQLite's interna
13f40 6c 20 73 63 68 65 6d 61 20 61 6e 64 20 6d 6f 64 l schema and mod
13f50 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 ify. ** the sch
13f60 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 20 20 2a 2f ema cookie.. */
13f70 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c . if( IsVirtual
13f80 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 (pTab) ){. sq
13f90 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
13fa0 76 2c 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 v, OP_VDestroy,
13fb0 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d iDb, 0, 0, pTab-
13fc0 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a >zName, 0);. }.
13fd0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
13fe0 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 Op4(v, OP_DropTa
13ff0 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 ble, iDb, 0, 0,
14000 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b pTab->zName, 0);
14010 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 . sqlite3Change
14020 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 Cookie(pParse, i
14030 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 56 69 65 Db);. sqliteVie
14040 77 52 65 73 65 74 41 6c 6c 28 64 62 2c 20 69 44 wResetAll(db, iD
14050 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 b);.}../*.** Thi
14060 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
14070 6c 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f led to do the wo
14080 72 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 rk of a DROP TAB
14090 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a LE statement..**
140a0 20 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 pName is the na
140b0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
140c0 74 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a to be dropped..*
140d0 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 /.void sqlite3Dr
140e0 6f 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 opTable(Parse *p
140f0 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a Parse, SrcList *
14100 70 4e 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 pName, int isVie
14110 77 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 w, int noErr){.
14120 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 Table *pTab;.
14130 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 Vdbe *v;. sqlit
14140 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
14150 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a >db;. int iDb;.
14160 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
14170 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 cFailed ){. g
14180 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 oto exit_drop_ta
14190 62 6c 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 ble;. }. asser
141a0 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d t( pParse->nErr=
141b0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
141c0 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 pName->nSrc==1 )
141d0 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 ;. if( sqlite3R
141e0 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 eadSchema(pParse
141f0 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 ) ) goto exit_dr
14200 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 69 66 28 20 op_table;. if(
14210 6e 6f 45 72 72 20 29 20 64 62 2d 3e 73 75 70 70 noErr ) db->supp
14220 72 65 73 73 45 72 72 2b 2b 3b 0a 20 20 70 54 61 ressErr++;. pTa
14230 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 b = sqlite3Locat
14240 65 54 61 62 6c 65 49 74 65 6d 28 70 50 61 72 73 eTableItem(pPars
14250 65 2c 20 69 73 56 69 65 77 2c 20 26 70 4e 61 6d e, isView, &pNam
14260 65 2d 3e 61 5b 30 5d 29 3b 0a 20 20 69 66 28 20 e->a[0]);. if(
14270 6e 6f 45 72 72 20 29 20 64 62 2d 3e 73 75 70 70 noErr ) db->supp
14280 72 65 73 73 45 72 72 2d 2d 3b 0a 0a 20 20 69 66 ressErr--;.. if
14290 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 ( pTab==0 ){.
142a0 20 69 66 28 20 6e 6f 45 72 72 20 29 20 73 71 6c if( noErr ) sql
142b0 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 4e 61 ite3CodeVerifyNa
142c0 6d 65 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 medSchema(pParse
142d0 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 , pName->a[0].zD
142e0 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 67 6f atabase);. go
142f0 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 to exit_drop_tab
14300 6c 65 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 le;. }. iDb =
14310 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
14320 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 ndex(db, pTab->p
14330 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 Schema);. asser
14340 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 t( iDb>=0 && iDb
14350 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f <db->nDb );.. /
14360 2a 20 49 66 20 70 54 61 62 20 69 73 20 61 20 76 * If pTab is a v
14370 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 63 61 irtual table, ca
14380 6c 6c 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e ll ViewGetColumn
14390 4e 61 6d 65 73 28 29 20 74 6f 20 65 6e 73 75 72 Names() to ensur
143a0 65 0a 20 20 2a 2a 20 69 74 20 69 73 20 69 6e 69 e. ** it is ini
143b0 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 tialized.. */.
143c0 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 if( IsVirtual(p
143d0 54 61 62 29 20 26 26 20 73 71 6c 69 74 65 33 56 Tab) && sqlite3V
143e0 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 iewGetColumnName
143f0 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 s(pParse, pTab)
14400 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 ){. goto exit
14410 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d _drop_table;. }
14420 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14430 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
14440 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 ON. {. int c
14450 6f 64 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 ode;. const c
14460 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 har *zTab = SCHE
14470 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 MA_TABLE(iDb);.
14480 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
14490 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 Db = db->aDb[iDb
144a0 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e ].zName;. con
144b0 73 74 20 63 68 61 72 20 2a 7a 41 72 67 32 20 3d st char *zArg2 =
144c0 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 0;. if( sqli
144d0 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
144e0 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 rse, SQLITE_DELE
144f0 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 TE, zTab, 0, zDb
14500 29 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 )){. goto e
14510 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a xit_drop_table;.
14520 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 }. if( is
14530 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66 View ){. if
14540 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 ( !OMIT_TEMPDB &
14550 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 & iDb==1 ){.
14560 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 code = SQLIT
14570 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 E_DROP_TEMP_VIEW
14580 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
14590 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 code = SQ
145a0 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a LITE_DROP_VIEW;.
145b0 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 }.#ifndef
145c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
145d0 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c UALTABLE. }el
145e0 73 65 20 69 66 28 20 49 73 56 69 72 74 75 61 6c se if( IsVirtual
145f0 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 (pTab) ){.
14600 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 code = SQLITE_DR
14610 4f 50 5f 56 54 41 42 4c 45 3b 0a 20 20 20 20 20 OP_VTABLE;.
14620 20 7a 41 72 67 32 20 3d 20 73 71 6c 69 74 65 33 zArg2 = sqlite3
14630 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 GetVTable(db, pT
14640 61 62 29 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 ab)->pMod->zName
14650 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c ;.#endif. }el
14660 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f se{. if( !O
14670 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 MIT_TEMPDB && iD
14680 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 b==1 ){.
14690 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 code = SQLITE_DR
146a0 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 OP_TEMP_TABLE;.
146b0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
146c0 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 code = SQLIT
146d0 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20 E_DROP_TABLE;.
146e0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
146f0 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 if( sqlite3AuthC
14700 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 heck(pParse, cod
14710 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 e, pTab->zName,
14720 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20 zArg2, zDb) ){.
14730 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 goto exit_d
14740 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d rop_table;. }
14750 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
14760 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
14770 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c , SQLITE_DELETE,
14780 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c pTab->zName, 0,
14790 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 zDb) ){. g
147a0 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 oto exit_drop_ta
147b0 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 ble;. }. }.#
147c0 65 6e 64 69 66 0a 20 20 69 66 28 20 73 71 6c 69 endif. if( sqli
147d0 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 te3StrNICmp(pTab
147e0 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 ->zName, "sqlite
147f0 5f 22 2c 20 37 29 3d 3d 30 20 0a 20 20 20 20 26 _", 7)==0 . &
14800 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d & sqlite3StrNICm
14810 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 p(pTab->zName, "
14820 73 71 6c 69 74 65 5f 73 74 61 74 22 2c 20 31 31 sqlite_stat", 11
14830 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 )!=0 ){. sqli
14840 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
14850 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 se, "table %s ma
14860 79 20 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 y not be dropped
14870 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b ", pTab->zName);
14880 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 . goto exit_d
14890 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a rop_table;. }..
148a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
148b0 4d 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e MIT_VIEW. /* En
148c0 73 75 72 65 20 44 52 4f 50 20 54 41 42 4c 45 20 sure DROP TABLE
148d0 69 73 20 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61 is not used on a
148e0 20 76 69 65 77 2c 20 61 6e 64 20 44 52 4f 50 20 view, and DROP
148f0 56 49 45 57 20 69 73 20 6e 6f 74 20 75 73 65 64 VIEW is not used
14900 0a 20 20 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65 . ** on a table
14910 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 .. */. if( isV
14920 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 iew && pTab->pSe
14930 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 lect==0 ){. s
14940 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
14950 50 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 Parse, "use DROP
14960 20 54 41 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 TABLE to delete
14970 20 74 61 62 6c 65 20 25 73 22 2c 20 70 54 61 62 table %s", pTab
14980 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f ->zName);. go
14990 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 to exit_drop_tab
149a0 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 le;. }. if( !i
149b0 73 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70 sView && pTab->p
149c0 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 Select ){. sq
149d0 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
149e0 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 arse, "use DROP
149f0 56 49 45 57 20 74 6f 20 64 65 6c 65 74 65 20 76 VIEW to delete v
14a00 69 65 77 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a iew %s", pTab->z
14a10 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Name);. goto
14a20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b exit_drop_table;
14a30 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
14a40 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
14a50 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 to remove the ta
14a60 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 ble from the mas
14a70 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f ter table. ** o
14a80 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76 n disk.. */. v
14a90 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
14aa0 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 e(pParse);. if(
14ab0 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 v ){. sqlite
14ac0 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 3BeginWriteOpera
14ad0 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 tion(pParse, 1,
14ae0 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 iDb);. sqlite
14af0 33 43 6c 65 61 72 53 74 61 74 54 61 62 6c 65 73 3ClearStatTables
14b00 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 74 (pParse, iDb, "t
14b10 62 6c 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 bl", pTab->zName
14b20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 6b );. sqlite3Fk
14b30 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 DropTable(pParse
14b40 2c 20 70 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0a , pName, pTab);.
14b50 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 44 sqlite3CodeD
14b60 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c ropTable(pParse,
14b70 20 70 54 61 62 2c 20 69 44 62 2c 20 69 73 56 69 pTab, iDb, isVi
14b80 65 77 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 ew);. }..exit_d
14b90 72 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c rop_table:. sql
14ba0 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 ite3SrcListDelet
14bb0 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a e(db, pName);.}.
14bc0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
14bd0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f ine is called to
14be0 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 66 6f create a new fo
14bf0 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 68 65 reign key on the
14c00 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72 65 6e table.** curren
14c10 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 tly under constr
14c20 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f uction. pFromCo
14c30 6c 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 l determines whi
14c40 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e ch columns.** in
14c50 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 the current tab
14c60 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 le point to the
14c70 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20 49 66 foreign key. If
14c80 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 pFromCol==0 the
14c90 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 68 65 n.** connect the
14ca0 20 6b 65 79 20 74 6f 20 74 68 65 20 6c 61 73 74 key to the last
14cb0 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 65 64 column inserted
14cc0 2e 20 20 70 54 6f 20 69 73 20 74 68 65 20 6e 61 . pTo is the na
14cd0 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 me of.** the tab
14ce0 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 20 28 le referred to (
14cf0 61 2e 6b 2e 61 20 74 68 65 20 22 70 61 72 65 6e a.k.a the "paren
14d00 74 22 20 74 61 62 6c 65 29 2e 20 20 70 54 6f 43 t" table). pToC
14d10 6f 6c 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20 ol is a list.**
14d20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 of tables in the
14d30 20 70 61 72 65 6e 74 20 70 54 6f 20 74 61 62 6c parent pTo tabl
14d40 65 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61 69 e. flags contai
14d50 6e 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d ns all.** inform
14d60 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
14d70 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 conflict resolut
14d80 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 73 ion algorithms s
14d90 70 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74 pecified.** in t
14da0 68 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e he ON DELETE, ON
14db0 20 55 50 44 41 54 45 20 61 6e 64 20 4f 4e 20 49 UPDATE and ON I
14dc0 4e 53 45 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a NSERT clauses..*
14dd0 2a 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73 74 72 *.** An FKey str
14de0 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 ucture is create
14df0 64 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 d and added to t
14e00 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 he table current
14e10 6c 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73 ly.** under cons
14e20 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 truction in the
14e30 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
14e40 65 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 e field..**.** T
14e50 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 he foreign key i
14e60 73 20 73 65 74 20 66 6f 72 20 49 4d 4d 45 44 49 s set for IMMEDI
14e70 41 54 45 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 ATE processing.
14e80 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 A subsequent ca
14e90 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 ll.** to sqlite3
14ea0 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 DeferForeignKey(
14eb0 29 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 ) might change t
14ec0 68 69 73 20 74 6f 20 44 45 46 45 52 52 45 44 2e his to DEFERRED.
14ed0 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
14ee0 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 CreateForeignKey
14ef0 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
14f00 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 e, /* Pars
14f10 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
14f20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 72 6f 6d ExprList *pFrom
14f30 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 Col, /* Columns
14f40 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 74 in this table t
14f50 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 74 68 hat point to oth
14f60 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 6f er table */. To
14f70 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20 20 20 ken *pTo,
14f80 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
14f90 65 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f e other table */
14fa0 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 54 6f . ExprList *pTo
14fb0 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d Col, /* Colum
14fc0 6e 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 ns in the other
14fd0 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 table */. int f
14fe0 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 lags
14ff0 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f /* Conflict reso
15000 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d lution algorithm
15010 73 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 s. */.){. sqlit
15020 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
15030 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c >db;.#ifndef SQL
15040 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e ITE_OMIT_FOREIGN
15050 5f 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70 46 4b _KEY. FKey *pFK
15060 65 79 20 3d 20 30 3b 0a 20 20 46 4b 65 79 20 2a ey = 0;. FKey *
15070 70 4e 65 78 74 54 6f 3b 0a 20 20 54 61 62 6c 65 pNextTo;. Table
15080 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e *p = pParse->pN
15090 65 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 6e ewTable;. int n
150a0 42 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 Byte;. int i;.
150b0 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 int nCol;. cha
150c0 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 r *z;.. assert(
150d0 20 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66 28 pTo!=0 );. if(
150e0 20 70 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c p==0 || IN_DECL
150f0 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 ARE_VTAB ) goto
15100 66 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70 46 fk_end;. if( pF
15110 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 romCol==0 ){.
15120 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e int iCol = p->n
15130 43 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 4e Col-1;. if( N
15140 45 56 45 52 28 69 43 6f 6c 3c 30 29 20 29 20 67 EVER(iCol<0) ) g
15150 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 oto fk_end;.
15160 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 if( pToCol && pT
15170 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 oCol->nExpr!=1 )
15180 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
15190 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
151a0 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 "foreign key on
151b0 25 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73 %s". " s
151c0 68 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20 hould reference
151d0 6f 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 only one column
151e0 6f 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 of table %T",.
151f0 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 p->aCol[i
15200 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 Col].zName, pTo)
15210 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f ;. goto fk_
15220 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e end;. }. n
15230 43 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 Col = 1;. }else
15240 20 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 if( pToCol && p
15250 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 ToCol->nExpr!=pF
15260 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b romCol->nExpr ){
15270 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
15280 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 rMsg(pParse,.
15290 20 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 "number of
152a0 63 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 columns in forei
152b0 67 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 gn key does not
152c0 6d 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 match the number
152d0 20 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63 of ". "c
152e0 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 olumns in the re
152f0 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29 ferenced table")
15300 3b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e ;. goto fk_en
15310 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 d;. }else{.
15320 6e 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d nCol = pFromCol-
15330 3e 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 >nExpr;. }. nB
15340 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 yte = sizeof(*pF
15350 4b 65 79 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a Key) + (nCol-1)*
15360 73 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61 43 sizeof(pFKey->aC
15370 6f 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 ol[0]) + pTo->n
15380 2b 20 31 3b 0a 20 20 69 66 28 20 70 54 6f 43 6f + 1;. if( pToCo
15390 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 l ){. for(i=0
153a0 3b 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 ; i<pToCol->nExp
153b0 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e r; i++){. n
153c0 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 Byte += sqlite3S
153d0 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e trlen30(pToCol->
153e0 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b a[i].zName) + 1;
153f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b . }. }. pFK
15400 65 79 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ey = sqlite3DbMa
15410 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 llocZero(db, nBy
15420 74 65 20 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 te );. if( pFKe
15430 79 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f y==0 ){. goto
15440 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 fk_end;. }. p
15450 46 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b FKey->pFrom = p;
15460 0a 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 . pFKey->pNextF
15470 72 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a rom = p->pFKey;.
15480 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 z = (char*)&pF
15490 4b 65 79 2d 3e 61 43 6f 6c 5b 6e 43 6f 6c 5d 3b Key->aCol[nCol];
154a0 0a 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 . pFKey->zTo =
154b0 7a 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 z;. memcpy(z, p
154c0 54 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a To->z, pTo->n);.
154d0 20 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b z[pTo->n] = 0;
154e0 0a 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 . sqlite3Dequot
154f0 65 28 7a 29 3b 0a 20 20 7a 20 2b 3d 20 70 54 6f e(z);. z += pTo
15500 2d 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65 79 2d 3e ->n+1;. pFKey->
15510 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 nCol = nCol;. i
15520 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 f( pFromCol==0 )
15530 7b 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f {. pFKey->aCo
15540 6c 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e l[0].iFrom = p->
15550 6e 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b nCol-1;. }else{
15560 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
15570 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCol; i++){.
15580 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 int j;. f
15590 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f or(j=0; j<p->nCo
155a0 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 l; j++){.
155b0 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
155c0 43 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a Cmp(p->aCol[j].z
155d0 4e 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e Name, pFromCol->
155e0 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 a[i].zName)==0 )
155f0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 {. pFKe
15600 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d y->aCol[i].iFrom
15610 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 = j;.
15620 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
15630 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
15640 66 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b f( j>=p->nCol ){
15650 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
15660 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
15670 20 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b . "unk
15680 6e 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 nown column \"%s
15690 5c 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 \" in foreign ke
156a0 79 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a y definition", .
156b0 20 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 pFromC
156c0 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b ol->a[i].zName);
156d0 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b . goto fk
156e0 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 _end;. }.
156f0 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 }. }. if( pT
15700 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 oCol ){. for(
15710 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b i=0; i<nCol; i++
15720 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d ){. int n =
15730 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
15740 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e (pToCol->a[i].zN
15750 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 46 4b 65 ame);. pFKe
15760 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 20 y->aCol[i].zCol
15770 3d 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 = z;. memcp
15780 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b 69 y(z, pToCol->a[i
15790 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 ].zName, n);.
157a0 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 z[n] = 0;.
157b0 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 z += n+1;.
157c0 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e }. }. pFKey->
157d0 69 73 44 65 66 65 72 72 65 64 20 3d 20 30 3b 0a isDeferred = 0;.
157e0 20 20 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f 6e pFKey->aAction
157f0 5b 30 5d 20 3d 20 28 75 38 29 28 66 6c 61 67 73 [0] = (u8)(flags
15800 20 26 20 30 78 66 66 29 3b 20 20 20 20 20 20 20 & 0xff);
15810 20 20 20 20 20 2f 2a 20 4f 4e 20 44 45 4c 45 54 /* ON DELET
15820 45 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 46 E action */. pF
15830 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 31 5d 20 Key->aAction[1]
15840 3d 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e 3e = (u8)((flags >>
15850 20 38 20 29 20 26 20 30 78 66 66 29 3b 20 20 20 8 ) & 0xff);
15860 20 2f 2a 20 4f 4e 20 55 50 44 41 54 45 20 61 63 /* ON UPDATE ac
15870 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 tion */.. asser
15880 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 t( sqlite3Schema
15890 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c MutexHeld(db, 0,
158a0 20 70 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a p->pSchema) );.
158b0 20 20 70 4e 65 78 74 54 6f 20 3d 20 28 46 4b 65 pNextTo = (FKe
158c0 79 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 y *)sqlite3HashI
158d0 6e 73 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d nsert(&p->pSchem
158e0 61 2d 3e 66 6b 65 79 48 61 73 68 2c 20 0a 20 20 a->fkeyHash, .
158f0 20 20 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 pFKey->zTo,
15900 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 0a 20 20 (void *)pFKey.
15910 29 3b 0a 20 20 69 66 28 20 70 4e 65 78 74 54 6f );. if( pNextTo
15920 3d 3d 70 46 4b 65 79 20 29 7b 0a 20 20 20 20 64 ==pFKey ){. d
15930 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
15940 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 66 6b = 1;. goto fk
15950 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 _end;. }. if(
15960 70 4e 65 78 74 54 6f 20 29 7b 0a 20 20 20 20 61 pNextTo ){. a
15970 73 73 65 72 74 28 20 70 4e 65 78 74 54 6f 2d 3e ssert( pNextTo->
15980 70 50 72 65 76 54 6f 3d 3d 30 20 29 3b 0a 20 20 pPrevTo==0 );.
15990 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f pFKey->pNextTo
159a0 20 3d 20 70 4e 65 78 74 54 6f 3b 0a 20 20 20 20 = pNextTo;.
159b0 70 4e 65 78 74 54 6f 2d 3e 70 50 72 65 76 54 6f pNextTo->pPrevTo
159c0 20 3d 20 70 46 4b 65 79 3b 0a 20 20 7d 0a 0a 20 = pFKey;. }..
159d0 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 66 6f 72 /* Link the for
159e0 65 69 67 6e 20 6b 65 79 20 74 6f 20 74 68 65 20 eign key to the
159f0 74 61 62 6c 65 20 61 73 20 74 68 65 20 6c 61 73 table as the las
15a00 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a 20 20 70 t step.. */. p
15a10 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b 65 79 3b ->pFKey = pFKey;
15a20 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b 0a 0a 66 . pFKey = 0;..f
15a30 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 k_end:. sqlite3
15a40 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b 65 79 DbFree(db, pFKey
15a50 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 );.#endif /* !de
15a60 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
15a70 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a T_FOREIGN_KEY) *
15a80 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c /. sqlite3ExprL
15a90 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 46 istDelete(db, pF
15aa0 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 romCol);. sqlit
15ab0 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 e3ExprListDelete
15ac0 28 64 62 2c 20 70 54 6f 43 6f 6c 29 3b 0a 7d 0a (db, pToCol);.}.
15ad0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
15ae0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 ine is called wh
15af0 65 6e 20 61 6e 20 49 4e 49 54 49 41 4c 4c 59 20 en an INITIALLY
15b00 49 4d 4d 45 44 49 41 54 45 20 6f 72 20 49 4e 49 IMMEDIATE or INI
15b10 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a TIALLY DEFERRED.
15b20 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 73 65 65 ** clause is see
15b30 6e 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 66 n as part of a f
15b40 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e oreign key defin
15b50 69 74 69 6f 6e 2e 20 20 54 68 65 20 69 73 44 65 ition. The isDe
15b60 66 65 72 72 65 64 0a 2a 2a 20 70 61 72 61 6d 65 ferred.** parame
15b70 74 65 72 20 69 73 20 31 20 66 6f 72 20 49 4e 49 ter is 1 for INI
15b80 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 20 TIALLY DEFERRED
15b90 61 6e 64 20 30 20 66 6f 72 20 49 4e 49 54 49 41 and 0 for INITIA
15ba0 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 2e 0a 2a LLY IMMEDIATE..*
15bb0 2a 20 54 68 65 20 62 65 68 61 76 69 6f 72 20 6f * The behavior o
15bc0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e f the most recen
15bd0 74 6c 79 20 63 72 65 61 74 65 64 20 66 6f 72 65 tly created fore
15be0 69 67 6e 20 6b 65 79 20 69 73 20 61 64 6a 75 73 ign key is adjus
15bf0 74 65 64 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 ted.** according
15c00 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 ly..*/.void sqli
15c10 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b te3DeferForeignK
15c20 65 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ey(Parse *pParse
15c30 2c 20 69 6e 74 20 69 73 44 65 66 65 72 72 65 64 , int isDeferred
15c40 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ){.#ifndef SQLIT
15c50 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b E_OMIT_FOREIGN_K
15c60 45 59 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 EY. Table *pTab
15c70 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b ;. FKey *pFKey;
15c80 0a 20 20 69 66 28 20 28 70 54 61 62 20 3d 20 70 . if( (pTab = p
15c90 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
15ca0 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79 20 3d )==0 || (pFKey =
15cb0 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d 3d 30 pTab->pFKey)==0
15cc0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
15cd0 65 72 74 28 20 69 73 44 65 66 65 72 72 65 64 3d ert( isDeferred=
15ce0 3d 30 20 7c 7c 20 69 73 44 65 66 65 72 72 65 64 =0 || isDeferred
15cf0 3d 3d 31 20 29 3b 20 2f 2a 20 45 56 3a 20 52 2d ==1 ); /* EV: R-
15d00 33 30 33 32 33 2d 32 31 39 31 37 20 2a 2f 0a 20 30323-21917 */.
15d10 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 pFKey->isDeferr
15d20 65 64 20 3d 20 28 75 38 29 69 73 44 65 66 65 72 ed = (u8)isDefer
15d30 72 65 64 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f red;.#endif.}../
15d40 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
15d50 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 72 61 de that will era
15d60 73 65 20 61 6e 64 20 72 65 66 69 6c 6c 20 69 6e se and refill in
15d70 64 65 78 20 2a 70 49 64 78 2e 20 20 54 68 69 73 dex *pIdx. This
15d80 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 is.** used to i
15d90 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 6c nitialize a newl
15da0 79 20 63 72 65 61 74 65 64 20 69 6e 64 65 78 20 y created index
15db0 6f 72 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 or to recompute
15dc0 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f the.** content o
15dd0 66 20 61 6e 20 69 6e 64 65 78 20 69 6e 20 72 65 f an index in re
15de0 73 70 6f 6e 73 65 20 74 6f 20 61 20 52 45 49 4e sponse to a REIN
15df0 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a DEX command..**.
15e00 2a 2a 20 69 66 20 6d 65 6d 52 6f 6f 74 50 61 67 ** if memRootPag
15e10 65 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69 76 e is not negativ
15e20 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 e, it means that
15e30 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 6e 65 the index is ne
15e40 77 6c 79 0a 2a 2a 20 63 72 65 61 74 65 64 2e 20 wly.** created.
15e50 20 54 68 65 20 72 65 67 69 73 74 65 72 20 73 70 The register sp
15e60 65 63 69 66 69 65 64 20 62 79 20 6d 65 6d 52 6f ecified by memRo
15e70 6f 74 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 otPage contains
15e80 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 the.** root page
15e90 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 number of the i
15ea0 6e 64 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f 6f ndex. If memRoo
15eb0 74 50 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 tPage is negativ
15ec0 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 69 e, then.** the i
15ed0 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69 ndex already exi
15ee0 73 74 73 20 61 6e 64 20 6d 75 73 74 20 62 65 20 sts and must be
15ef0 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20 62 cleared before b
15f00 65 69 6e 67 20 72 65 66 69 6c 6c 65 64 20 61 6e eing refilled an
15f10 64 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 d.** the root pa
15f20 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
15f30 20 69 6e 64 65 78 20 69 73 20 74 61 6b 65 6e 20 index is taken
15f40 66 72 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e 75 from pIndex->tnu
15f50 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 m..*/.static voi
15f60 64 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 d sqlite3RefillI
15f70 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 ndex(Parse *pPar
15f80 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 se, Index *pInde
15f90 78 2c 20 69 6e 74 20 6d 65 6d 52 6f 6f 74 50 61 x, int memRootPa
15fa0 67 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 ge){. Table *pT
15fb0 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 ab = pIndex->pTa
15fc0 62 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 61 62 ble; /* The tab
15fd0 6c 65 20 74 68 61 74 20 69 73 20 69 6e 64 65 78 le that is index
15fe0 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 ed */. int iTab
15ff0 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b = pParse->nTab+
16000 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 +; /* Btree
16010 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20 cursor used for
16020 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69 49 pTab */. int iI
16030 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 dx = pParse->nTa
16040 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 b++; /* Btre
16050 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f e cursor used fo
16060 72 20 70 49 6e 64 65 78 20 2a 2f 0a 20 20 69 6e r pIndex */. in
16070 74 20 69 53 6f 72 74 65 72 3b 20 20 20 20 20 20 t iSorter;
16080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16090 43 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 62 79 Cursor opened by
160a0 20 4f 70 65 6e 53 6f 72 74 65 72 20 28 69 66 20 OpenSorter (if
160b0 69 6e 20 75 73 65 29 20 2a 2f 0a 20 20 69 6e 74 in use) */. int
160c0 20 61 64 64 72 31 3b 20 20 20 20 20 20 20 20 20 addr1;
160d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
160e0 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f 66 ddress of top of
160f0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 loop */. int a
16100 64 64 72 32 3b 20 20 20 20 20 20 20 20 20 20 20 ddr2;
16110 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
16120 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 ress to jump to
16130 66 6f 72 20 6e 65 78 74 20 69 74 65 72 61 74 69 for next iterati
16140 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d on */. int tnum
16150 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
16160 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 /* Root p
16170 61 67 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a age of index */.
16180 20 20 69 6e 74 20 69 50 61 72 74 49 64 78 4c 61 int iPartIdxLa
16190 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 bel;
161a0 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 69 73 /* Jump to this
161b0 20 6c 61 62 65 6c 20 74 6f 20 73 6b 69 70 20 61 label to skip a
161c0 20 72 6f 77 20 2a 2f 0a 20 20 56 64 62 65 20 2a row */. Vdbe *
161d0 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
161e0 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 /* Gene
161f0 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 rate code into t
16200 68 69 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68 his virtual mach
16210 69 6e 65 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f ine */. KeyInfo
16220 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 20 *pKey;
16230 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 49 6e /* KeyIn
16240 66 6f 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a fo for index */.
16250 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b int regRecord;
16260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16270 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
16280 64 69 6e 67 20 61 73 73 65 6d 62 6c 65 64 20 69 ding assembled i
16290 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 ndex record */.
162a0 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
162b0 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 Parse->db;
162c0 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
162d0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
162e0 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 int iDb = sqlite
162f0 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 3SchemaToIndex(d
16300 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 b, pIndex->pSche
16310 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 ma);..#ifndef SQ
16320 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
16330 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 IZATION. if( sq
16340 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
16350 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 52 45 Parse, SQLITE_RE
16360 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e 7a INDEX, pIndex->z
16370 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 64 Name, 0,. d
16380 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
16390 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 e ) ){. retur
163a0 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 n;. }.#endif..
163b0 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 77 72 /* Require a wr
163c0 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ite-lock on the
163d0 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f 72 6d table to perform
163e0 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
163f0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c */. sqlite3Tabl
16400 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 eLock(pParse, iD
16410 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 31 b, pTab->tnum, 1
16420 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
16430 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 . v = sqlite3Ge
16440 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
16450 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 if( v==0 ) retu
16460 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f rn;. if( memRoo
16470 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 tPage>=0 ){.
16480 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50 61 tnum = memRootPa
16490 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ge;. }else{.
164a0 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d 3e tnum = pIndex->
164b0 74 6e 75 6d 3b 0a 20 20 7d 0a 20 20 70 4b 65 79 tnum;. }. pKey
164c0 20 3d 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 = sqlite3KeyInf
164d0 6f 4f 66 49 6e 64 65 78 28 70 50 61 72 73 65 2c oOfIndex(pParse,
164e0 20 70 49 6e 64 65 78 29 3b 0a 0a 20 20 2f 2a 20 pIndex);.. /*
164f0 4f 70 65 6e 20 74 68 65 20 73 6f 72 74 65 72 20 Open the sorter
16500 63 75 72 73 6f 72 20 69 66 20 77 65 20 61 72 65 cursor if we are
16510 20 74 6f 20 75 73 65 20 6f 6e 65 2e 20 2a 2f 0a to use one. */.
16520 20 20 69 53 6f 72 74 65 72 20 3d 20 70 50 61 72 iSorter = pPar
16530 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 73 71 se->nTab++;. sq
16540 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
16550 76 2c 20 4f 50 5f 53 6f 72 74 65 72 4f 70 65 6e v, OP_SorterOpen
16560 2c 20 69 53 6f 72 74 65 72 2c 20 30 2c 20 70 49 , iSorter, 0, pI
16570 6e 64 65 78 2d 3e 6e 4b 65 79 43 6f 6c 2c 20 28 ndex->nKeyCol, (
16580 63 68 61 72 2a 29 0a 20 20 20 20 20 20 20 20 20 char*).
16590 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
165a0 65 33 4b 65 79 49 6e 66 6f 52 65 66 28 70 4b 65 e3KeyInfoRef(pKe
165b0 79 29 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b y), P4_KEYINFO);
165c0 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 .. /* Open the
165d0 74 61 62 6c 65 2e 20 4c 6f 6f 70 20 74 68 72 6f table. Loop thro
165e0 75 67 68 20 61 6c 6c 20 72 6f 77 73 20 6f 66 20 ugh all rows of
165f0 74 68 65 20 74 61 62 6c 65 2c 20 69 6e 73 65 72 the table, inser
16600 74 69 6e 67 20 69 6e 64 65 78 0a 20 20 2a 2a 20 ting index. **
16610 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 74 68 65 records into the
16620 20 73 6f 72 74 65 72 2e 20 2a 2f 0a 20 20 73 71 sorter. */. sq
16630 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 lite3OpenTable(p
16640 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69 44 62 Parse, iTab, iDb
16650 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 , pTab, OP_OpenR
16660 65 61 64 29 3b 0a 20 20 61 64 64 72 31 20 3d 20 ead);. addr1 =
16670 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
16680 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 2(v, OP_Rewind,
16690 69 54 61 62 2c 20 30 29 3b 20 56 64 62 65 43 6f iTab, 0); VdbeCo
166a0 76 65 72 61 67 65 28 76 29 3b 0a 20 20 72 65 67 verage(v);. reg
166b0 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33 Record = sqlite3
166c0 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
166d0 65 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 47 65 e);.. sqlite3Ge
166e0 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 nerateIndexKey(p
166f0 50 61 72 73 65 2c 70 49 6e 64 65 78 2c 69 54 61 Parse,pIndex,iTa
16700 62 2c 72 65 67 52 65 63 6f 72 64 2c 30 2c 26 69 b,regRecord,0,&i
16710 50 61 72 74 49 64 78 4c 61 62 65 6c 2c 30 2c 30 PartIdxLabel,0,0
16720 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
16730 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 AddOp2(v, OP_Sor
16740 74 65 72 49 6e 73 65 72 74 2c 20 69 53 6f 72 74 terInsert, iSort
16750 65 72 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a er, regRecord);.
16760 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 sqlite3Resolve
16770 50 61 72 74 49 64 78 4c 61 62 65 6c 28 70 50 61 PartIdxLabel(pPa
16780 72 73 65 2c 20 69 50 61 72 74 49 64 78 4c 61 62 rse, iPartIdxLab
16790 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 el);. sqlite3Vd
167a0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
167b0 65 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 31 ext, iTab, addr1
167c0 2b 31 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 +1); VdbeCoverag
167d0 65 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 e(v);. sqlite3V
167e0 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 dbeJumpHere(v, a
167f0 64 64 72 31 29 3b 0a 20 20 69 66 28 20 6d 65 6d ddr1);. if( mem
16800 52 6f 6f 74 50 61 67 65 3c 30 20 29 20 73 71 6c RootPage<0 ) sql
16810 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
16820 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d , OP_Clear, tnum
16830 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 , iDb);. sqlite
16840 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
16850 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64 P_OpenWrite, iId
16860 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20 x, tnum, iDb, .
16870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16880 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c (char *)pKey,
16890 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 P4_KEYINFO);.
168a0 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
168b0 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 42 55 eP5(v, OPFLAG_BU
168c0 4c 4b 43 53 52 7c 28 28 6d 65 6d 52 6f 6f 74 50 LKCSR|((memRootP
168d0 61 67 65 3e 3d 30 29 3f 4f 50 46 4c 41 47 5f 50 age>=0)?OPFLAG_P
168e0 32 49 53 52 45 47 3a 30 29 29 3b 0a 0a 20 20 61 2ISREG:0));.. a
168f0 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 ddr1 = sqlite3Vd
16900 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 beAddOp2(v, OP_S
16910 6f 72 74 65 72 53 6f 72 74 2c 20 69 53 6f 72 74 orterSort, iSort
16920 65 72 2c 20 30 29 3b 20 56 64 62 65 43 6f 76 65 er, 0); VdbeCove
16930 72 61 67 65 28 76 29 3b 0a 20 20 61 73 73 65 72 rage(v);. asser
16940 74 28 20 70 4b 65 79 21 3d 30 20 7c 7c 20 64 62 t( pKey!=0 || db
16950 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c ->mallocFailed |
16960 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 | pParse->nErr )
16970 3b 0a 20 20 69 66 28 20 49 73 55 6e 69 71 75 65 ;. if( IsUnique
16980 49 6e 64 65 78 28 70 49 6e 64 65 78 29 20 26 26 Index(pIndex) &&
16990 20 70 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20 20 pKey!=0 ){.
169a0 69 6e 74 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 int j2 = sqlite3
169b0 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 VdbeCurrentAddr(
169c0 76 29 20 2b 20 33 3b 0a 20 20 20 20 73 71 6c 69 v) + 3;. sqli
169d0 74 65 33 56 64 62 65 47 6f 74 6f 28 76 2c 20 6a te3VdbeGoto(v, j
169e0 32 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20 2);. addr2 =
169f0 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
16a00 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 ntAddr(v);. s
16a10 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
16a20 49 6e 74 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 Int(v, OP_Sorter
16a30 43 6f 6d 70 61 72 65 2c 20 69 53 6f 72 74 65 72 Compare, iSorter
16a40 2c 20 6a 32 2c 20 72 65 67 52 65 63 6f 72 64 2c , j2, regRecord,
16a50 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
16a60 20 20 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 pIndex
16a70 2d 3e 6e 4b 65 79 43 6f 6c 29 3b 20 56 64 62 65 ->nKeyCol); Vdbe
16a80 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 Coverage(v);.
16a90 20 73 71 6c 69 74 65 33 55 6e 69 71 75 65 43 6f sqlite3UniqueCo
16aa0 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c nstraint(pParse,
16ab0 20 4f 45 5f 41 62 6f 72 74 2c 20 70 49 6e 64 65 OE_Abort, pInde
16ac0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 x);. }else{.
16ad0 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33 addr2 = sqlite3
16ae0 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 VdbeCurrentAddr(
16af0 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 v);. }. sqlite
16b00 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
16b10 50 5f 53 6f 72 74 65 72 44 61 74 61 2c 20 69 53 P_SorterData, iS
16b20 6f 72 74 65 72 2c 20 72 65 67 52 65 63 6f 72 64 orter, regRecord
16b30 2c 20 69 49 64 78 29 3b 0a 20 20 73 71 6c 69 74 , iIdx);. sqlit
16b40 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
16b50 4f 50 5f 4c 61 73 74 2c 20 69 49 64 78 2c 20 30 OP_Last, iIdx, 0
16b60 2c 20 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 , -1);. sqlite3
16b70 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
16b80 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64 78 _IdxInsert, iIdx
16b90 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 30 29 3b , regRecord, 0);
16ba0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
16bb0 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 angeP5(v, OPFLAG
16bc0 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 3b _USESEEKRESULT);
16bd0 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 . sqlite3Releas
16be0 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
16bf0 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 regRecord);. s
16c00 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
16c10 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 4e 65 78 (v, OP_SorterNex
16c20 74 2c 20 69 53 6f 72 74 65 72 2c 20 61 64 64 72 t, iSorter, addr
16c30 32 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 2); VdbeCoverage
16c40 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 (v);. sqlite3Vd
16c50 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 beJumpHere(v, ad
16c60 64 72 31 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 dr1);.. sqlite3
16c70 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
16c80 5f 43 6c 6f 73 65 2c 20 69 54 61 62 29 3b 0a 20 _Close, iTab);.
16c90 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
16ca0 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 p1(v, OP_Close,
16cb0 69 49 64 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 iIdx);. sqlite3
16cc0 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
16cd0 5f 43 6c 6f 73 65 2c 20 69 53 6f 72 74 65 72 29 _Close, iSorter)
16ce0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
16cf0 61 74 65 20 68 65 61 70 20 73 70 61 63 65 20 74 ate heap space t
16d00 6f 20 68 6f 6c 64 20 61 6e 20 49 6e 64 65 78 20 o hold an Index
16d10 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 43 6f 6c object with nCol
16d20 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 columns..**.**
16d30 49 6e 63 72 65 61 73 65 20 74 68 65 20 61 6c 6c Increase the all
16d40 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 74 6f 20 ocation size to
16d50 70 72 6f 76 69 64 65 20 61 6e 20 65 78 74 72 61 provide an extra
16d60 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a 2a nExtra bytes.**
16d70 20 6f 66 20 38 2d 62 79 74 65 20 61 6c 69 67 6e of 8-byte align
16d80 65 64 20 73 70 61 63 65 20 61 66 74 65 72 20 74 ed space after t
16d90 68 65 20 49 6e 64 65 78 20 6f 62 6a 65 63 74 20 he Index object
16da0 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 and return a.**
16db0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 pointer to this
16dc0 65 78 74 72 61 20 73 70 61 63 65 20 69 6e 20 2a extra space in *
16dd0 70 70 45 78 74 72 61 2e 0a 2a 2f 0a 49 6e 64 65 ppExtra..*/.Inde
16de0 78 20 2a 73 71 6c 69 74 65 33 41 6c 6c 6f 63 61 x *sqlite3Alloca
16df0 74 65 49 6e 64 65 78 4f 62 6a 65 63 74 28 0a 20 teIndexObject(.
16e00 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
16e10 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
16e20 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
16e30 20 20 69 31 36 20 6e 43 6f 6c 2c 20 20 20 20 20 i16 nCol,
16e40 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
16e50 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
16e60 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20 2a s in the index *
16e70 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 /. int nExtra,
16e80 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
16e90 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 65 er of bytes of e
16ea0 78 74 72 61 20 73 70 61 63 65 20 74 6f 20 61 6c xtra space to al
16eb0 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a loc */. char **
16ec0 70 70 45 78 74 72 61 20 20 20 20 20 20 20 2f 2a ppExtra /*
16ed0 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
16ee0 22 65 78 74 72 61 22 20 73 70 61 63 65 20 2a 2f "extra" space */
16ef0 0a 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 3b 20 .){. Index *p;
16f00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c /* Al
16f10 6c 6f 63 61 74 65 64 20 69 6e 64 65 78 20 6f 62 located index ob
16f20 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 ject */. int nB
16f30 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f yte; /
16f40 2a 20 42 79 74 65 73 20 6f 66 20 73 70 61 63 65 * Bytes of space
16f50 20 66 6f 72 20 49 6e 64 65 78 20 6f 62 6a 65 63 for Index objec
16f60 74 20 2b 20 61 72 72 61 79 73 20 2a 2f 0a 0a 20 t + arrays */..
16f70 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 nByte = ROUND8(
16f80 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 29 20 2b sizeof(Index)) +
16f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16fa0 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 Index structure
16fb0 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 52 */. R
16fc0 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 63 68 61 OUND8(sizeof(cha
16fd0 72 2a 29 2a 6e 43 6f 6c 29 20 2b 20 20 20 20 20 r*)*nCol) +
16fe0 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 /* Index.azC
16ff0 6f 6c 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 oll */.
17000 20 20 20 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 ROUND8(size
17010 6f 66 28 4c 6f 67 45 73 74 29 2a 28 6e 43 6f 6c of(LogEst)*(nCol
17020 2b 31 29 20 2b 20 20 20 20 20 2f 2a 20 49 6e 64 +1) + /* Ind
17030 65 78 2e 61 69 52 6f 77 4c 6f 67 45 73 74 20 20 ex.aiRowLogEst
17040 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
17050 20 20 20 20 20 73 69 7a 65 6f 66 28 69 31 36 29 sizeof(i16)
17060 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 20 *nCol +
17070 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43 6f /* Index.aiCo
17080 6c 75 6d 6e 20 20 20 2a 2f 0a 20 20 20 20 20 20 lumn */.
17090 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f sizeo
170a0 66 28 75 38 29 2a 6e 43 6f 6c 29 3b 20 20 20 20 f(u8)*nCol);
170b0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
170c0 78 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f 0a x.aSortOrder */.
170d0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d p = sqlite3DbM
170e0 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 allocZero(db, nB
170f0 79 74 65 20 2b 20 6e 45 78 74 72 61 29 3b 0a 20 yte + nExtra);.
17100 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63 68 if( p ){. ch
17110 61 72 20 2a 70 45 78 74 72 61 20 3d 20 28 28 63 ar *pExtra = ((c
17120 68 61 72 2a 29 70 29 2b 52 4f 55 4e 44 38 28 73 har*)p)+ROUND8(s
17130 69 7a 65 6f 66 28 49 6e 64 65 78 29 29 3b 0a 20 izeof(Index));.
17140 20 20 20 70 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 28 p->azColl = (
17150 63 68 61 72 2a 2a 29 70 45 78 74 72 61 3b 20 20 char**)pExtra;
17160 20 20 20 20 20 70 45 78 74 72 61 20 2b 3d 20 52 pExtra += R
17170 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 63 68 61 OUND8(sizeof(cha
17180 72 2a 29 2a 6e 43 6f 6c 29 3b 0a 20 20 20 20 70 r*)*nCol);. p
17190 2d 3e 61 69 52 6f 77 4c 6f 67 45 73 74 20 3d 20 ->aiRowLogEst =
171a0 28 4c 6f 67 45 73 74 2a 29 70 45 78 74 72 61 3b (LogEst*)pExtra;
171b0 20 70 45 78 74 72 61 20 2b 3d 20 73 69 7a 65 6f pExtra += sizeo
171c0 66 28 4c 6f 67 45 73 74 29 2a 28 6e 43 6f 6c 2b f(LogEst)*(nCol+
171d0 31 29 3b 0a 20 20 20 20 70 2d 3e 61 69 43 6f 6c 1);. p->aiCol
171e0 75 6d 6e 20 3d 20 28 69 31 36 2a 29 70 45 78 74 umn = (i16*)pExt
171f0 72 61 3b 20 20 20 20 20 20 20 70 45 78 74 72 61 ra; pExtra
17200 20 2b 3d 20 73 69 7a 65 6f 66 28 69 31 36 29 2a += sizeof(i16)*
17210 6e 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 61 53 6f nCol;. p->aSo
17220 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 70 rtOrder = (u8*)p
17230 45 78 74 72 61 3b 0a 20 20 20 20 70 2d 3e 6e 43 Extra;. p->nC
17240 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 3b 0a 20 20 olumn = nCol;.
17250 20 20 70 2d 3e 6e 4b 65 79 43 6f 6c 20 3d 20 6e p->nKeyCol = n
17260 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 2a 70 70 Col - 1;. *pp
17270 45 78 74 72 61 20 3d 20 28 28 63 68 61 72 2a 29 Extra = ((char*)
17280 70 29 20 2b 20 6e 42 79 74 65 3b 0a 20 20 7d 0a p) + nByte;. }.
17290 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
172a0 2a 0a 2a 2a 20 42 61 63 6b 77 61 72 64 73 20 43 *.** Backwards C
172b0 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 48 61 63 ompatibility Hac
172c0 6b 3a 0a 2a 2a 20 0a 2a 2a 20 48 69 73 74 6f 72 k:.** .** Histor
172d0 69 63 61 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 ical versions of
172e0 20 53 51 4c 69 74 65 20 61 63 63 65 70 74 65 64 SQLite accepted
172f0 20 73 74 72 69 6e 67 73 20 61 73 20 63 6f 6c 75 strings as colu
17300 6d 6e 20 6e 61 6d 65 73 20 69 6e 0a 2a 2a 20 69 mn names in.** i
17310 6e 64 65 78 65 73 20 61 6e 64 20 50 52 49 4d 41 ndexes and PRIMA
17320 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e RY KEY constrain
17330 74 73 20 61 6e 64 20 69 6e 20 55 4e 49 51 55 45 ts and in UNIQUE
17340 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 45 constraints. E
17350 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 xample:.**.**
17360 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 CREATE TABLE x
17370 79 7a 28 61 2c 62 2c 63 2c 64 2c 65 2c 50 52 49 yz(a,b,c,d,e,PRI
17380 4d 41 52 59 20 4b 45 59 28 27 61 27 29 2c 55 4e MARY KEY('a'),UN
17390 49 51 55 45 28 27 62 27 2c 27 63 27 20 43 4f 4c IQUE('b','c' COL
173a0 4c 41 54 45 20 74 72 69 6d 29 0a 2a 2a 20 20 20 LATE trim).**
173b0 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 61 CREATE INDEX a
173c0 62 63 20 4f 4e 20 78 79 7a 28 27 63 27 2c 27 64 bc ON xyz('c','d
173d0 27 20 44 45 53 43 2c 27 65 27 20 43 4f 4c 4c 41 ' DESC,'e' COLLA
173e0 54 45 20 6e 6f 63 61 73 65 20 44 45 53 43 29 3b TE nocase DESC);
173f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 67 .**.** This is g
17400 6f 6f 66 79 2e 20 20 42 75 74 20 74 6f 20 70 72 oofy. But to pr
17410 65 73 65 72 76 65 20 62 61 63 6b 77 61 72 64 73 eserve backwards
17420 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 compatibility w
17430 65 20 63 6f 6e 74 69 6e 75 65 20 74 6f 0a 2a 2a e continue to.**
17440 20 61 63 63 65 70 74 20 69 74 2e 20 20 54 68 69 accept it. Thi
17450 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 s routine does t
17460 68 65 20 6e 65 63 65 73 73 61 72 79 20 63 6f 6e he necessary con
17470 76 65 72 73 69 6f 6e 2e 20 20 49 74 20 63 6f 6e version. It con
17480 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 65 78 70 verts.** the exp
17490 72 65 73 73 69 6f 6e 20 67 69 76 65 6e 20 69 6e ression given in
174a0 20 69 74 73 20 61 72 67 75 6d 65 6e 74 20 66 72 its argument fr
174b0 6f 6d 20 61 20 54 4b 5f 53 54 52 49 4e 47 20 69 om a TK_STRING i
174c0 6e 74 6f 20 61 20 54 4b 5f 49 44 0a 2a 2a 20 69 nto a TK_ID.** i
174d0 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
174e0 20 69 73 20 6a 75 73 74 20 61 20 54 4b 5f 53 54 is just a TK_ST
174f0 52 49 4e 47 20 77 69 74 68 20 61 6e 20 6f 70 74 RING with an opt
17500 69 6f 6e 61 6c 20 43 4f 4c 4c 41 54 45 20 63 6c ional COLLATE cl
17510 61 75 73 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 ause..** If the
17520 65 70 78 72 65 73 73 69 6f 6e 20 69 73 20 61 6e epxression is an
17530 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ything other tha
17540 6e 20 54 4b 5f 53 54 52 49 4e 47 2c 20 74 68 65 n TK_STRING, the
17550 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a expression is.*
17560 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a * unchanged..*/.
17570 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
17580 74 65 33 53 74 72 69 6e 67 54 6f 49 64 28 45 78 te3StringToId(Ex
17590 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d pr *p){. if( p-
175a0 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29 >op==TK_STRING )
175b0 7b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b {. p->op = TK
175c0 5f 49 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 _ID;. }else if(
175d0 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 4c 41 p->op==TK_COLLA
175e0 54 45 20 26 26 20 70 2d 3e 70 4c 65 66 74 2d 3e TE && p->pLeft->
175f0 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29 7b op==TK_STRING ){
17600 0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f . p->pLeft->o
17610 70 20 3d 20 54 4b 5f 49 44 3b 0a 20 20 7d 0a 7d p = TK_ID;. }.}
17620 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
17630 20 6e 65 77 20 69 6e 64 65 78 20 66 6f 72 20 61 new index for a
17640 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e n SQL table. pN
17650 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69 73 20 74 ame1.pName2 is t
17660 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 he name of the i
17670 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62 ndex .** and pTb
17680 6c 4c 69 73 74 20 69 73 20 74 68 65 20 6e 61 6d lList is the nam
17690 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 e of the table t
176a0 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64 hat is to be ind
176b0 65 78 65 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c exed. Both will
176c0 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72 .** be NULL for
176d0 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f a primary key o
176e0 72 20 61 6e 20 69 6e 64 65 78 20 74 68 61 74 20 r an index that
176f0 69 73 20 63 72 65 61 74 65 64 20 74 6f 20 73 61 is created to sa
17700 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55 tisfy a.** UNIQU
17710 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49 E constraint. I
17720 66 20 70 54 61 62 6c 65 20 61 6e 64 20 70 49 6e f pTable and pIn
17730 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73 dex are NULL, us
17740 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 e pParse->pNewTa
17750 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 ble.** as the ta
17760 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65 ble to be indexe
17770 64 2e 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 d. pParse->pNew
17780 54 61 62 6c 65 20 69 73 20 61 20 74 61 62 6c 65 Table is a table
17790 20 74 68 61 74 20 69 73 0a 2a 2a 20 63 75 72 72 that is.** curr
177a0 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 ently being cons
177b0 74 72 75 63 74 65 64 20 62 79 20 61 20 43 52 45 tructed by a CRE
177c0 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
177d0 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 ent..**.** pList
177e0 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 63 6f is a list of co
177f0 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 lumns to be inde
17800 78 65 64 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c xed. pList will
17810 20 62 65 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 be NULL if this
17820 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d 61 72 79 .** is a primary
17830 20 6b 65 79 20 6f 72 20 75 6e 69 71 75 65 2d 63 key or unique-c
17840 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 onstraint on the
17850 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 6f 6c most recent col
17860 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20 umn added.** to
17870 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e the table curren
17880 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 tly under constr
17890 75 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20 uction. .**.**
178a0 49 66 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 If the index is
178b0 63 72 65 61 74 65 64 20 73 75 63 63 65 73 73 66 created successf
178c0 75 6c 6c 79 2c 20 72 65 74 75 72 6e 20 61 20 70 ully, return a p
178d0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
178e0 77 20 49 6e 64 65 78 0a 2a 2a 20 73 74 72 75 63 w Index.** struc
178f0 74 75 72 65 2e 20 54 68 69 73 20 69 73 20 75 73 ture. This is us
17900 65 64 20 62 79 20 73 71 6c 69 74 65 33 41 64 64 ed by sqlite3Add
17910 50 72 69 6d 61 72 79 4b 65 79 28 29 20 74 6f 20 PrimaryKey() to
17920 6d 61 72 6b 20 74 68 65 20 69 6e 64 65 78 0a 2a mark the index.*
17930 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 73 20 * as the tables
17940 70 72 69 6d 61 72 79 20 6b 65 79 20 28 49 6e 64 primary key (Ind
17950 65 78 2e 69 64 78 54 79 70 65 3d 3d 53 51 4c 49 ex.idxType==SQLI
17960 54 45 5f 49 44 58 54 59 50 45 5f 50 52 49 4d 41 TE_IDXTYPE_PRIMA
17970 52 59 4b 45 59 29 0a 2a 2f 0a 49 6e 64 65 78 20 RYKEY).*/.Index
17980 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e *sqlite3CreateIn
17990 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 dex(. Parse *pP
179a0 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 41 6c 6c arse, /* All
179b0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
179c0 75 74 20 74 68 69 73 20 70 61 72 73 65 20 2a 2f ut this parse */
179d0 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 . Token *pName1
179e0 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 , /* First p
179f0 61 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d art of index nam
17a00 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a e. May be NULL *
17a10 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
17a20 32 2c 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 2, /* Second
17a30 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20 6e part of index n
17a40 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c ame. May be NULL
17a50 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 */. SrcList *p
17a60 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54 61 62 6c TblName, /* Tabl
17a70 65 20 74 6f 20 69 6e 64 65 78 2e 20 55 73 65 20 e to index. Use
17a80 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
17a90 65 20 69 66 20 30 20 2a 2f 0a 20 20 45 78 70 72 e if 0 */. Expr
17aa0 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f List *pList, /
17ab0 2a 20 41 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 * A list of colu
17ac0 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 mns to be indexe
17ad0 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 d */. int onErr
17ae0 6f 72 2c 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f or, /* OE_
17af0 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 Abort, OE_Ignore
17b00 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 , OE_Replace, or
17b10 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f OE_None */. To
17b20 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20 20 20 20 ken *pStart,
17b30 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 74 /* The CREATE t
17b40 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73 oken that begins
17b50 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 this statement
17b60 2a 2f 0a 20 20 45 78 70 72 20 2a 70 50 49 57 68 */. Expr *pPIWh
17b70 65 72 65 2c 20 20 20 20 2f 2a 20 57 48 45 52 45 ere, /* WHERE
17b80 20 63 6c 61 75 73 65 20 66 6f 72 20 70 61 72 74 clause for part
17b90 69 61 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 ial indices */.
17ba0 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 2c 20 int sortOrder,
17bb0 20 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72 64 65 /* Sort orde
17bc0 72 20 6f 66 20 70 72 69 6d 61 72 79 20 6b 65 79 r of primary key
17bd0 20 77 68 65 6e 20 70 4c 69 73 74 3d 3d 4e 55 4c when pList==NUL
17be0 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 66 4e 6f 74 L */. int ifNot
17bf0 45 78 69 73 74 20 20 20 20 20 2f 2a 20 4f 6d 69 Exist /* Omi
17c00 74 20 65 72 72 6f 72 20 69 66 20 69 6e 64 65 78 t error if index
17c10 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 already exists
17c20 2a 2f 0a 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 */.){. Index *p
17c30 52 65 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 Ret = 0; /*
17c40 50 6f 69 6e 74 65 72 20 74 6f 20 72 65 74 75 72 Pointer to retur
17c50 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 n */. Table *pT
17c60 61 62 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 ab = 0; /* T
17c70 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78 able to be index
17c80 65 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 ed */. Index *p
17c90 49 6e 64 65 78 20 3d 20 30 3b 20 20 20 2f 2a 20 Index = 0; /*
17ca0 54 68 65 20 69 6e 64 65 78 20 74 6f 20 62 65 20 The index to be
17cb0 63 72 65 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 created */. cha
17cc0 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 r *zName = 0;
17cd0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
17ce0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 index */. int
17cf0 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 nName;
17d00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 /* Number of ch
17d10 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e 61 6d aracters in zNam
17d20 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b e */. int i, j;
17d30 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b . DbFixer sFix;
17d40 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 /* For a
17d50 73 73 69 67 6e 69 6e 67 20 64 61 74 61 62 61 73 ssigning databas
17d60 65 20 6e 61 6d 65 73 20 74 6f 20 70 54 61 62 6c e names to pTabl
17d70 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f e */. int sortO
17d80 72 64 65 72 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 rderMask; /* 1
17d90 20 74 6f 20 68 6f 6e 6f 72 20 44 45 53 43 20 69 to honor DESC i
17da0 6e 20 69 6e 64 65 78 2e 20 20 30 20 74 6f 20 69 n index. 0 to i
17db0 67 6e 6f 72 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 gnore. */. sqli
17dc0 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
17dd0 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b ->db;. Db *pDb;
17de0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17df0 54 68 65 20 73 70 65 63 69 66 69 63 20 74 61 62 The specific tab
17e00 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 le containing th
17e10 65 20 69 6e 64 65 78 65 64 20 64 61 74 61 62 61 e indexed databa
17e20 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b se */. int iDb;
17e30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17e40 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 Index of the dat
17e50 61 62 61 73 65 20 74 68 61 74 20 69 73 20 62 65 abase that is be
17e60 69 6e 67 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 ing written */.
17e70 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 Token *pName =
17e80 30 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69 0; /* Unquali
17e90 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 fied name of the
17ea0 20 69 6e 64 65 78 20 74 6f 20 63 72 65 61 74 65 index to create
17eb0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 */. struct Exp
17ec0 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 69 73 rList_item *pLis
17ed0 74 49 74 65 6d 3b 20 2f 2a 20 46 6f 72 20 6c 6f tItem; /* For lo
17ee0 6f 70 69 6e 67 20 6f 76 65 72 20 70 4c 69 73 74 oping over pList
17ef0 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 */. int nExtra
17f00 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
17f10 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
17f20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 7a 45 allocated for zE
17f30 78 74 72 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 xtra[] */. int
17f40 6e 45 78 74 72 61 43 6f 6c 3b 20 20 20 20 20 20 nExtraCol;
17f50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17f60 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 Number of extra
17f70 63 6f 6c 75 6d 6e 73 20 6e 65 65 64 65 64 20 2a columns needed *
17f80 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61 /. char *zExtra
17f90 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
17fa0 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 /* Extra sp
17fb0 61 63 65 20 61 66 74 65 72 20 74 68 65 20 49 6e ace after the In
17fc0 64 65 78 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 dex object */.
17fd0 49 6e 64 65 78 20 2a 70 50 6b 20 3d 20 30 3b 20 Index *pPk = 0;
17fe0 20 20 20 20 20 2f 2a 20 50 52 49 4d 41 52 59 20 /* PRIMARY
17ff0 4b 45 59 20 69 6e 64 65 78 20 66 6f 72 20 57 49 KEY index for WI
18000 54 48 4f 55 54 20 52 4f 57 49 44 20 74 61 62 6c THOUT ROWID tabl
18010 65 73 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d es */.. if( db-
18020 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c >mallocFailed ||
18030 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 IN_DECLARE_VTAB
18040 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 || pParse->nErr
18050 3e 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 >0 ){. goto e
18060 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 xit_create_index
18070 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 ;. }. if( SQLI
18080 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 TE_OK!=sqlite3Re
18090 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 adSchema(pParse)
180a0 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 ){. goto exi
180b0 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
180c0 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 }.. /*. ** F
180d0 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20 74 68 ind the table th
180e0 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 at is to be inde
180f0 78 65 64 2e 20 20 52 65 74 75 72 6e 20 65 61 72 xed. Return ear
18100 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e ly if not found.
18110 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 62 6c . */. if( pTbl
18120 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20 20 20 20 Name!=0 ){..
18130 2f 2a 20 55 73 65 20 74 68 65 20 74 77 6f 2d 70 /* Use the two-p
18140 61 72 74 20 69 6e 64 65 78 20 6e 61 6d 65 20 74 art index name t
18150 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
18160 64 61 74 61 62 61 73 65 20 0a 20 20 20 20 2a 2a database . **
18170 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20 74 to search for t
18180 68 65 20 74 61 62 6c 65 2e 20 27 46 69 78 27 20 he table. 'Fix'
18190 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 74 the table name t
181a0 6f 20 74 68 69 73 20 64 62 0a 20 20 20 20 2a 2a o this db. **
181b0 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69 6e 67 20 before looking
181c0 75 70 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 up the table..
181d0 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
181e0 20 70 4e 61 6d 65 31 20 26 26 20 70 4e 61 6d 65 pName1 && pName
181f0 32 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 2 );. iDb = s
18200 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d qlite3TwoPartNam
18210 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 e(pParse, pName1
18220 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 , pName2, &pName
18230 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 );. if( iDb<0
18240 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 ) goto exit_cre
18250 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 61 ate_index;. a
18260 73 73 65 72 74 28 20 70 4e 61 6d 65 20 26 26 20 ssert( pName &&
18270 70 4e 61 6d 65 2d 3e 7a 20 29 3b 0a 0a 23 69 66 pName->z );..#if
18280 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
18290 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a 20 49 _TEMPDB. /* I
182a0 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 f the index name
182b0 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69 65 64 was unqualified
182c0 2c 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 74 , check if the t
182d0 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 61 able. ** is a
182e0 20 74 65 6d 70 20 74 61 62 6c 65 2e 20 49 66 20 temp table. If
182f0 73 6f 2c 20 73 65 74 20 74 68 65 20 64 61 74 61 so, set the data
18300 62 61 73 65 20 74 6f 20 31 2e 20 44 6f 20 6e 6f base to 1. Do no
18310 74 20 64 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a t do this. **
18320 20 69 66 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 if initialising
18330 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 a database sche
18340 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ma.. */. i
18350 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 f( !db->init.bus
18360 79 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 y ){. pTab
18370 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 = sqlite3SrcList
18380 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 Lookup(pParse, p
18390 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 TblName);.
183a0 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 if( pName2->n==0
183b0 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61 62 && pTab && pTab
183c0 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 ->pSchema==db->a
183d0 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b Db[1].pSchema ){
183e0 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 31 . iDb = 1
183f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
18400 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69 #endif.. sqli
18410 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 te3FixInit(&sFix
18420 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 , pParse, iDb, "
18430 69 6e 64 65 78 22 2c 20 70 4e 61 6d 65 29 3b 0a index", pName);.
18440 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 if( sqlite3F
18450 69 78 53 72 63 4c 69 73 74 28 26 73 46 69 78 2c ixSrcList(&sFix,
18460 20 70 54 62 6c 4e 61 6d 65 29 20 29 7b 0a 20 20 pTblName) ){.
18470 20 20 20 20 2f 2a 20 42 65 63 61 75 73 65 20 74 /* Because t
18480 68 65 20 70 61 72 73 65 72 20 63 6f 6e 73 74 72 he parser constr
18490 75 63 74 73 20 70 54 62 6c 4e 61 6d 65 20 66 72 ucts pTblName fr
184a0 6f 6d 20 61 20 73 69 6e 67 6c 65 20 69 64 65 6e om a single iden
184b0 74 69 66 69 65 72 2c 0a 20 20 20 20 20 20 2a 2a tifier,. **
184c0 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 sqlite3FixSrcLi
184d0 73 74 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 st can never fai
184e0 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 l. */. asse
184f0 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 rt(0);. }.
18500 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c pTab = sqlite3L
18510 6f 63 61 74 65 54 61 62 6c 65 49 74 65 6d 28 70 ocateTableItem(p
18520 50 61 72 73 65 2c 20 30 2c 20 26 70 54 62 6c 4e Parse, 0, &pTblN
18530 61 6d 65 2d 3e 61 5b 30 5d 29 3b 0a 20 20 20 20 ame->a[0]);.
18540 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c assert( db->mall
18550 6f 63 46 61 69 6c 65 64 3d 3d 30 20 7c 7c 20 70 ocFailed==0 || p
18560 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 Tab==0 );. if
18570 28 20 70 54 61 62 3d 3d 30 20 29 20 67 6f 74 6f ( pTab==0 ) goto
18580 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
18590 65 78 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3d ex;. if( iDb=
185a0 3d 31 20 26 26 20 64 62 2d 3e 61 44 62 5b 69 44 =1 && db->aDb[iD
185b0 62 5d 2e 70 53 63 68 65 6d 61 21 3d 70 54 61 62 b].pSchema!=pTab
185c0 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 ->pSchema ){.
185d0 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
185e0 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 sg(pParse, .
185f0 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 63 "cannot c
18600 72 65 61 74 65 20 61 20 54 45 4d 50 20 69 6e 64 reate a TEMP ind
18610 65 78 20 6f 6e 20 6e 6f 6e 2d 54 45 4d 50 20 74 ex on non-TEMP t
18620 61 62 6c 65 20 5c 22 25 73 5c 22 22 2c 0a 20 20 able \"%s\"",.
18630 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a pTab->z
18640 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 Name);. got
18650 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e o exit_create_in
18660 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 dex;. }. i
18670 66 28 20 21 48 61 73 52 6f 77 69 64 28 70 54 61 f( !HasRowid(pTa
18680 62 29 20 29 20 70 50 6b 20 3d 20 73 71 6c 69 74 b) ) pPk = sqlit
18690 65 33 50 72 69 6d 61 72 79 4b 65 79 49 6e 64 65 e3PrimaryKeyInde
186a0 78 28 70 54 61 62 29 3b 0a 20 20 7d 65 6c 73 65 x(pTab);. }else
186b0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e {. assert( pN
186c0 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 ame==0 );. as
186d0 73 65 72 74 28 20 70 53 74 61 72 74 3d 3d 30 20 sert( pStart==0
186e0 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50 );. pTab = pP
186f0 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b arse->pNewTable;
18700 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 . if( !pTab )
18710 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 goto exit_creat
18720 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 44 62 e_index;. iDb
18730 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 = sqlite3Schema
18740 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 ToIndex(db, pTab
18750 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7d 0a ->pSchema);. }.
18760 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 pDb = &db->aDb
18770 5b 69 44 62 5d 3b 0a 0a 20 20 61 73 73 65 72 74 [iDb];.. assert
18780 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61 ( pTab!=0 );. a
18790 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e ssert( pParse->n
187a0 45 72 72 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 Err==0 );. if(
187b0 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 sqlite3StrNICmp(
187c0 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 pTab->zName, "sq
187d0 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 0a 20 lite_", 7)==0 .
187e0 20 20 20 20 20 20 26 26 20 64 62 2d 3e 69 6e 69 && db->ini
187f0 74 2e 62 75 73 79 3d 3d 30 0a 23 69 66 20 53 51 t.busy==0.#if SQ
18800 4c 49 54 45 5f 55 53 45 52 5f 41 55 54 48 45 4e LITE_USER_AUTHEN
18810 54 49 43 41 54 49 4f 4e 0a 20 20 20 20 20 20 20 TICATION.
18820 26 26 20 73 71 6c 69 74 65 33 55 73 65 72 41 75 && sqlite3UserAu
18830 74 68 54 61 62 6c 65 28 70 54 61 62 2d 3e 7a 4e thTable(pTab->zN
18840 61 6d 65 29 3d 3d 30 0a 23 65 6e 64 69 66 0a 20 ame)==0.#endif.
18850 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
18860 53 74 72 4e 49 43 6d 70 28 26 70 54 61 62 2d 3e StrNICmp(&pTab->
18870 7a 4e 61 6d 65 5b 37 5d 2c 22 61 6c 74 65 72 74 zName[7],"altert
18880 61 62 5f 22 2c 39 29 21 3d 30 20 29 7b 0a 20 20 ab_",9)!=0 ){.
18890 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
188a0 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 g(pParse, "table
188b0 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 %s may not be i
188c0 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d 3e 7a ndexed", pTab->z
188d0 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Name);. goto
188e0 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
188f0 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 x;. }.#ifndef S
18900 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a QLITE_OMIT_VIEW.
18910 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c if( pTab->pSel
18920 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ect ){. sqlit
18930 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
18940 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e 6f e, "views may no
18950 74 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a t be indexed");.
18960 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 goto exit_cr
18970 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a eate_index;. }.
18980 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
18990 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
189a0 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73 ALTABLE. if( Is
189b0 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b Virtual(pTab) ){
189c0 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
189d0 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 rMsg(pParse, "vi
189e0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 rtual tables may
189f0 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 not be indexed"
18a00 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 );. goto exit
18a10 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
18a20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a }.#endif.. /*.
18a30 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 ** Find the na
18a40 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e me of the index.
18a50 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72 Make sure ther
18a60 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 e is not already
18a70 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e another. ** in
18a80 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77 69 74 dex or table wit
18a90 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e h the same name.
18aa0 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 . **. ** Exc
18ab0 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20 61 eption: If we a
18ac0 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6e re reading the n
18ad0 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65 6e ames of permanen
18ae0 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20 74 t indices from t
18af0 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d he. ** sqlite_m
18b00 61 73 74 65 72 20 74 61 62 6c 65 20 28 62 65 63 aster table (bec
18b10 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 ause some other
18b20 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64 20 process changed
18b30 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64 0a the schema) and.
18b40 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 ** one of the
18b50 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c 6c index names coll
18b60 69 64 65 73 20 77 69 74 68 20 74 68 65 20 6e 61 ides with the na
18b70 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61 72 me of a temporar
18b80 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 y table or. **
18b90 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 20 77 index, then we w
18ba0 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 ill continue to
18bb0 70 72 6f 63 65 73 73 20 74 68 69 73 20 69 6e 64 process this ind
18bc0 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 ex.. **. ** If
18bd0 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61 pName==0 it mea
18be0 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 0a 20 ns that we are.
18bf0 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 74 68 ** dealing with
18c00 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f a primary key o
18c10 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 r UNIQUE constra
18c20 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20 74 6f int. We have to
18c30 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 2a 2a invent our. **
18c40 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a own name.. */.
18c50 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 if( pName ){.
18c60 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 zName = sqlit
18c70 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
18c80 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 db, pName);.
18c90 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 67 if( zName==0 ) g
18ca0 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
18cb0 69 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 72 index;. asser
18cc0 74 28 20 70 4e 61 6d 65 2d 3e 7a 21 3d 30 20 29 t( pName->z!=0 )
18cd0 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 ;. if( SQLITE
18ce0 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 _OK!=sqlite3Chec
18cf0 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 kObjectName(pPar
18d00 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 se, zName) ){.
18d10 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 goto exit_cr
18d20 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 eate_index;.
18d30 7d 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 }. if( !db->i
18d40 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 nit.busy ){.
18d50 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e if( sqlite3Fin
18d60 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 dTable(db, zName
18d70 2c 20 30 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 , 0)!=0 ){.
18d80 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
18d90 73 67 28 70 50 61 72 73 65 2c 20 22 74 68 65 72 sg(pParse, "ther
18da0 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 74 e is already a t
18db0 61 62 6c 65 20 6e 61 6d 65 64 20 25 73 22 2c 20 able named %s",
18dc0 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 zName);.
18dd0 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
18de0 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d 0a _index;. }.
18df0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 }. if( sq
18e00 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 lite3FindIndex(d
18e10 62 2c 20 7a 4e 61 6d 65 2c 20 70 44 62 2d 3e 7a b, zName, pDb->z
18e20 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Name)!=0 ){.
18e30 20 20 69 66 28 20 21 69 66 4e 6f 74 45 78 69 73 if( !ifNotExis
18e40 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c t ){. sql
18e50 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
18e60 72 73 65 2c 20 22 69 6e 64 65 78 20 25 73 20 61 rse, "index %s a
18e70 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 lready exists",
18e80 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 65 zName);. }e
18e90 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
18ea0 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 ert( !db->init.b
18eb0 75 73 79 20 29 3b 0a 20 20 20 20 20 20 20 20 73 usy );. s
18ec0 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 qlite3CodeVerify
18ed0 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 Schema(pParse, i
18ee0 44 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Db);. }.
18ef0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
18f00 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d ate_index;. }
18f10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
18f20 74 20 6e 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a t n;. Index *
18f30 70 4c 6f 6f 70 3b 0a 20 20 20 20 66 6f 72 28 70 pLoop;. for(p
18f40 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70 49 6e 64 65 Loop=pTab->pInde
18f50 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f 70 3b 20 70 x, n=1; pLoop; p
18f60 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 4e 65 78 Loop=pLoop->pNex
18f70 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 7a 4e t, n++){}. zN
18f80 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 ame = sqlite3MPr
18f90 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65 intf(db, "sqlite
18fa0 5f 61 75 74 6f 69 6e 64 65 78 5f 25 73 5f 25 64 _autoindex_%s_%d
18fb0 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 ", pTab->zName,
18fc0 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d n);. if( zNam
18fd0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f e==0 ){. go
18fe0 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
18ff0 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ndex;. }. }.
19000 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 . /* Check for
19010 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f authorization to
19020 20 63 72 65 61 74 65 20 61 6e 20 69 6e 64 65 78 create an index
19030 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 .. */.#ifndef S
19040 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
19050 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 RIZATION. {.
19060 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
19070 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 = pDb->zName;.
19080 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 if( sqlite3Au
19090 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 thCheck(pParse,
190a0 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 SQLITE_INSERT, S
190b0 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 CHEMA_TABLE(iDb)
190c0 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 , 0, zDb) ){.
190d0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
190e0 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d ate_index;. }
190f0 0a 20 20 20 20 69 20 3d 20 53 51 4c 49 54 45 5f . i = SQLITE_
19100 43 52 45 41 54 45 5f 49 4e 44 45 58 3b 0a 20 20 CREATE_INDEX;.
19110 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 if( !OMIT_TEMP
19120 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 20 69 DB && iDb==1 ) i
19130 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 = SQLITE_CREATE
19140 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20 20 _TEMP_INDEX;.
19150 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 if( sqlite3Auth
19160 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 69 2c Check(pParse, i,
19170 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e zName, pTab->zN
19180 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 ame, zDb) ){.
19190 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
191a0 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d ate_index;. }
191b0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
191c0 2a 20 49 66 20 70 4c 69 73 74 3d 3d 30 2c 20 69 * If pList==0, i
191d0 74 20 6d 65 61 6e 73 20 74 68 69 73 20 72 6f 75 t means this rou
191e0 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20 tine was called
191f0 74 6f 20 6d 61 6b 65 20 61 20 70 72 69 6d 61 72 to make a primar
19200 79 0a 20 20 2a 2a 20 6b 65 79 20 6f 75 74 20 6f y. ** key out o
19210 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d f the last colum
19220 6e 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 74 n added to the t
19230 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 able under const
19240 72 75 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f ruction.. ** So
19250 20 63 72 65 61 74 65 20 61 20 66 61 6b 65 20 6c create a fake l
19260 69 73 74 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 ist to simulate
19270 74 68 69 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 this.. */. if(
19280 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 pList==0 ){.
19290 20 54 6f 6b 65 6e 20 70 72 65 76 43 6f 6c 3b 0a Token prevCol;.
192a0 20 20 20 20 70 72 65 76 43 6f 6c 2e 7a 20 3d 20 prevCol.z =
192b0 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d pTab->aCol[pTab-
192c0 3e 6e 43 6f 6c 2d 31 5d 2e 7a 4e 61 6d 65 3b 0a >nCol-1].zName;.
192d0 20 20 20 20 70 72 65 76 43 6f 6c 2e 6e 20 3d 20 prevCol.n =
192e0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
192f0 70 72 65 76 43 6f 6c 2e 7a 29 3b 0a 20 20 20 20 prevCol.z);.
19300 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 pList = sqlite3E
19310 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 xprListAppend(pP
19320 61 72 73 65 2c 20 30 2c 0a 20 20 20 20 20 20 20 arse, 0,.
19330 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
19340 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 49 prAlloc(db, TK_I
19350 44 2c 20 26 70 72 65 76 43 6f 6c 2c 20 30 29 29 D, &prevCol, 0))
19360 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d ;. if( pList=
19370 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 =0 ) goto exit_c
19380 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 reate_index;.
19390 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e assert( pList->
193a0 6e 45 78 70 72 3d 3d 31 20 29 3b 0a 20 20 20 20 nExpr==1 );.
193b0 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 sqlite3ExprListS
193c0 65 74 53 6f 72 74 4f 72 64 65 72 28 70 4c 69 73 etSortOrder(pLis
193d0 74 2c 20 73 6f 72 74 4f 72 64 65 72 29 3b 0a 20 t, sortOrder);.
193e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
193f0 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b te3ExprListCheck
19400 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20 70 Length(pParse, p
19410 4c 69 73 74 2c 20 22 69 6e 64 65 78 22 29 3b 0a List, "index");.
19420 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 }.. /* Figure
19430 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 out how many by
19440 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 72 65 tes of space are
19450 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
19460 72 65 20 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 re explicitly.
19470 2a 2a 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c ** specified col
19480 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
19490 6e 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f names.. */. fo
194a0 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e r(i=0; i<pList->
194b0 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
194c0 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 Expr *pExpr = p
194d0 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 List->a[i].pExpr
194e0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 ;. assert( pE
194f0 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 xpr!=0 );. if
19500 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
19510 43 4f 4c 4c 41 54 45 20 29 7b 0a 20 20 20 20 20 COLLATE ){.
19520 20 6e 45 78 74 72 61 20 2b 3d 20 28 31 20 2b 20 nExtra += (1 +
19530 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
19540 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 pExpr->u.zToken)
19550 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
19560 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 /* . ** Allocat
19570 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 75 e the index stru
19580 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e cture. . */. n
19590 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 Name = sqlite3St
195a0 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 rlen30(zName);.
195b0 20 6e 45 78 74 72 61 43 6f 6c 20 3d 20 70 50 6b nExtraCol = pPk
195c0 20 3f 20 70 50 6b 2d 3e 6e 4b 65 79 43 6f 6c 20 ? pPk->nKeyCol
195d0 3a 20 31 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 : 1;. pIndex =
195e0 73 71 6c 69 74 65 33 41 6c 6c 6f 63 61 74 65 49 sqlite3AllocateI
195f0 6e 64 65 78 4f 62 6a 65 63 74 28 64 62 2c 20 70 ndexObject(db, p
19600 4c 69 73 74 2d 3e 6e 45 78 70 72 20 2b 20 6e 45 List->nExpr + nE
19610 78 74 72 61 43 6f 6c 2c 0a 20 20 20 20 20 20 20 xtraCol,.
19620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e n
19640 4e 61 6d 65 20 2b 20 6e 45 78 74 72 61 20 2b 20 Name + nExtra +
19650 31 2c 20 26 7a 45 78 74 72 61 29 3b 0a 20 20 69 1, &zExtra);. i
19660 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
19670 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 led ){. goto
19680 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
19690 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 x;. }. assert(
196a0 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
196b0 4e 4d 45 4e 54 28 70 49 6e 64 65 78 2d 3e 61 69 NMENT(pIndex->ai
196c0 52 6f 77 4c 6f 67 45 73 74 29 20 29 3b 0a 20 20 RowLogEst) );.
196d0 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
196e0 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 49 6e TE_ALIGNMENT(pIn
196f0 64 65 78 2d 3e 61 7a 43 6f 6c 6c 29 20 29 3b 0a dex->azColl) );.
19700 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 20 pIndex->zName
19710 3d 20 7a 45 78 74 72 61 3b 0a 20 20 7a 45 78 74 = zExtra;. zExt
19720 72 61 20 2b 3d 20 6e 4e 61 6d 65 20 2b 20 31 3b ra += nName + 1;
19730 0a 20 20 6d 65 6d 63 70 79 28 70 49 6e 64 65 78 . memcpy(pIndex
19740 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 ->zName, zName,
19750 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 70 49 6e 64 nName+1);. pInd
19760 65 78 2d 3e 70 54 61 62 6c 65 20 3d 20 70 54 61 ex->pTable = pTa
19770 62 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 b;. pIndex->onE
19780 72 72 6f 72 20 3d 20 28 75 38 29 6f 6e 45 72 72 rror = (u8)onErr
19790 6f 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 75 6e or;. pIndex->un
197a0 69 71 4e 6f 74 4e 75 6c 6c 20 3d 20 6f 6e 45 72 iqNotNull = onEr
197b0 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 3b 0a 20 20 ror!=OE_None;.
197c0 70 49 6e 64 65 78 2d 3e 69 64 78 54 79 70 65 20 pIndex->idxType
197d0 3d 20 70 4e 61 6d 65 20 3f 20 53 51 4c 49 54 45 = pName ? SQLITE
197e0 5f 49 44 58 54 59 50 45 5f 41 50 50 44 45 46 20 _IDXTYPE_APPDEF
197f0 3a 20 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 : SQLITE_IDXTYPE
19800 5f 55 4e 49 51 55 45 3b 0a 20 20 70 49 6e 64 65 _UNIQUE;. pInde
19810 78 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d x->pSchema = db-
19820 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d >aDb[iDb].pSchem
19830 61 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6e 4b 65 a;. pIndex->nKe
19840 79 43 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 yCol = pList->nE
19850 78 70 72 3b 0a 20 20 69 66 28 20 70 50 49 57 68 xpr;. if( pPIWh
19860 65 72 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ere ){. sqlit
19870 65 33 52 65 73 6f 6c 76 65 53 65 6c 66 52 65 66 e3ResolveSelfRef
19880 65 72 65 6e 63 65 28 70 50 61 72 73 65 2c 20 70 erence(pParse, p
19890 54 61 62 2c 20 4e 43 5f 50 61 72 74 49 64 78 2c Tab, NC_PartIdx,
198a0 20 70 50 49 57 68 65 72 65 2c 20 30 29 3b 0a 20 pPIWhere, 0);.
198b0 20 20 20 70 49 6e 64 65 78 2d 3e 70 50 61 72 74 pIndex->pPart
198c0 49 64 78 57 68 65 72 65 20 3d 20 70 50 49 57 68 IdxWhere = pPIWh
198d0 65 72 65 3b 0a 20 20 20 20 70 50 49 57 68 65 72 ere;. pPIWher
198e0 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 e = 0;. }. ass
198f0 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 ert( sqlite3Sche
19900 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 maMutexHeld(db,
19910 69 44 62 2c 20 30 29 20 29 3b 0a 0a 20 20 2f 2a iDb, 0) );.. /*
19920 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
19930 20 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e 6f 72 we should honor
19940 20 44 45 53 43 20 72 65 71 75 65 73 74 73 20 6f DESC requests o
19950 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 0a n index columns.
19960 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 62 2d 3e */. if( pDb->
19970 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f pSchema->file_fo
19980 72 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20 20 73 rmat>=4 ){. s
19990 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d ortOrderMask = -
199a0 31 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45 1; /* Honor DE
199b0 53 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 SC */. }else{.
199c0 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b sortOrderMask
199d0 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f = 0; /* Igno
199e0 72 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d 0a 0a re DESC */. }..
199f0 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 /* Analyze the
19a00 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
19a10 69 6f 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 74 ions that form t
19a20 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 he terms of the
19a30 69 6e 64 65 78 20 61 6e 64 0a 20 20 2a 2a 20 72 index and. ** r
19a40 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 73 eport any errors
19a50 2e 20 20 49 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e . In the common
19a60 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 case where the
19a70 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65 78 expression is ex
19a80 61 63 74 6c 79 0a 20 20 2a 2a 20 61 20 74 61 62 actly. ** a tab
19a90 6c 65 20 63 6f 6c 75 6d 6e 2c 20 73 74 6f 72 65 le column, store
19aa0 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 6e 20 that column in
19ab0 61 69 43 6f 6c 75 6d 6e 5b 5d 2e 20 20 46 6f 72 aiColumn[]. For
19ac0 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65 73 73 general express
19ad0 69 6f 6e 73 2c 0a 20 20 2a 2a 20 70 6f 70 75 6c ions,. ** popul
19ae0 61 74 65 20 70 49 6e 64 65 78 2d 3e 61 43 6f 6c ate pIndex->aCol
19af0 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20 2d Expr and store -
19b00 32 20 69 6e 20 61 69 43 6f 6c 75 6d 6e 5b 5d 2e 2 in aiColumn[].
19b10 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a . **. ** TODO:
19b20 20 49 73 73 75 65 20 61 20 77 61 72 6e 69 6e 67 Issue a warning
19b30 20 69 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 if two or more
19b40 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 69 columns of the i
19b50 6e 64 65 78 20 61 72 65 20 69 64 65 6e 74 69 63 ndex are identic
19b60 61 6c 2e 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 49 al.. ** TODO: I
19b70 73 73 75 65 20 61 20 77 61 72 6e 69 6e 67 20 69 ssue a warning i
19b80 66 20 74 68 65 20 74 61 62 6c 65 20 70 72 69 6d f the table prim
19b90 61 72 79 20 6b 65 79 20 69 73 20 75 73 65 64 20 ary key is used
19ba0 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 as part of the.
19bb0 20 2a 2a 20 69 6e 64 65 78 20 6b 65 79 2e 0a 20 ** index key..
19bc0 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 */. for(i=0, p
19bd0 4c 69 73 74 49 74 65 6d 3d 70 4c 69 73 74 2d 3e ListItem=pList->
19be0 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 a; i<pList->nExp
19bf0 72 3b 20 69 2b 2b 2c 20 70 4c 69 73 74 49 74 65 r; i++, pListIte
19c00 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a m++){. Expr *
19c10 70 43 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 pCExpr;
19c20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
19c30 69 2d 74 68 20 69 6e 64 65 78 20 65 78 70 72 65 i-th index expre
19c40 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 ssion */. int
19c50 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 requestedSortOr
19c60 64 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 der; /* A
19c70 53 43 20 6f 72 20 44 45 53 43 20 6f 6e 20 74 68 SC or DESC on th
19c80 65 20 69 2d 74 68 20 65 78 70 72 65 73 73 69 6f e i-th expressio
19c90 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a n */. char *z
19ca0 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Coll;
19cb0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 /* Colla
19cc0 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 tion sequence na
19cd0 6d 65 20 2a 2f 0a 0a 20 20 20 20 73 71 6c 69 74 me */.. sqlit
19ce0 65 33 53 74 72 69 6e 67 54 6f 49 64 28 70 4c 69 e3StringToId(pLi
19cf0 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a stItem->pExpr);.
19d00 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c sqlite3Resol
19d10 76 65 53 65 6c 66 52 65 66 65 72 65 6e 63 65 28 veSelfReference(
19d20 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 4e 43 pParse, pTab, NC
19d30 5f 49 64 78 45 78 70 72 2c 20 70 4c 69 73 74 49 _IdxExpr, pListI
19d40 74 65 6d 2d 3e 70 45 78 70 72 2c 20 30 29 3b 0a tem->pExpr, 0);.
19d50 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e if( pParse->
19d60 6e 45 72 72 20 29 20 67 6f 74 6f 20 65 78 69 74 nErr ) goto exit
19d70 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
19d80 20 20 20 70 43 45 78 70 72 20 3d 20 73 71 6c 69 pCExpr = sqli
19d90 74 65 33 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 te3ExprSkipColla
19da0 74 65 28 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 te(pListItem->pE
19db0 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 xpr);. if( pC
19dc0 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c Expr->op!=TK_COL
19dd0 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 UMN ){. if(
19de0 20 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70 pTab==pParse->p
19df0 4e 65 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 NewTable ){.
19e00 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
19e10 4d 73 67 28 70 50 61 72 73 65 2c 20 22 65 78 70 Msg(pParse, "exp
19e20 72 65 73 73 69 6f 6e 73 20 70 72 6f 68 69 62 69 ressions prohibi
19e30 74 65 64 20 69 6e 20 50 52 49 4d 41 52 59 20 4b ted in PRIMARY K
19e40 45 59 20 61 6e 64 20 22 0a 20 20 20 20 20 20 20 EY and ".
19e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e60 20 20 20 20 20 20 20 20 20 22 55 4e 49 51 55 45 "UNIQUE
19e70 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a constraints");.
19e80 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 goto exi
19e90 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
19ea0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
19eb0 28 20 70 49 6e 64 65 78 2d 3e 61 43 6f 6c 45 78 ( pIndex->aColEx
19ec0 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pr==0 ){.
19ed0 20 45 78 70 72 4c 69 73 74 20 2a 70 43 6f 70 79 ExprList *pCopy
19ee0 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 = sqlite3ExprLi
19ef0 73 74 44 75 70 28 64 62 2c 20 70 4c 69 73 74 2c stDup(db, pList,
19f00 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 0);. pIn
19f10 64 65 78 2d 3e 61 43 6f 6c 45 78 70 72 20 3d 20 dex->aColExpr =
19f20 70 43 6f 70 79 3b 0a 20 20 20 20 20 20 20 20 69 pCopy;. i
19f30 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 f( !db->mallocFa
19f40 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 iled ){.
19f50 20 20 61 73 73 65 72 74 28 20 70 43 6f 70 79 21 assert( pCopy!
19f60 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 =0 );.
19f70 70 4c 69 73 74 49 74 65 6d 20 3d 20 26 70 43 6f pListItem = &pCo
19f80 70 79 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 py->a[i];.
19f90 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
19fa0 20 20 6a 20 3d 20 2d 32 3b 0a 20 20 20 20 20 20 j = -2;.
19fb0 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e pIndex->aiColumn
19fc0 5b 69 5d 20 3d 20 2d 32 3b 0a 20 20 20 20 20 20 [i] = -2;.
19fd0 70 49 6e 64 65 78 2d 3e 75 6e 69 71 4e 6f 74 4e pIndex->uniqNotN
19fe0 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c ull = 0;. }el
19ff0 73 65 7b 0a 20 20 20 20 20 20 6a 20 3d 20 70 43 se{. j = pC
1a000 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 Expr->iColumn;.
1a010 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 3d assert( j<=
1a020 30 78 37 66 66 66 20 29 3b 0a 20 20 20 20 20 20 0x7fff );.
1a030 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20 20 if( j<0 ){.
1a040 20 20 20 6a 20 3d 20 70 54 61 62 2d 3e 69 50 4b j = pTab->iPK
1a050 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 ey;. }else
1a060 69 66 28 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a if( pTab->aCol[j
1a070 5d 2e 6e 6f 74 4e 75 6c 6c 3d 3d 30 20 29 7b 0a ].notNull==0 ){.
1a080 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e pIndex->
1a090 75 6e 69 71 4e 6f 74 4e 75 6c 6c 20 3d 20 30 3b uniqNotNull = 0;
1a0a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1a0b0 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b Index->aiColumn[
1a0c0 69 5d 20 3d 20 28 69 31 36 29 6a 3b 0a 20 20 20 i] = (i16)j;.
1a0d0 20 7d 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 30 }. zColl = 0
1a0e0 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 49 ;. if( pListI
1a0f0 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 6f 70 3d 3d tem->pExpr->op==
1a100 54 4b 5f 43 4f 4c 4c 41 54 45 20 29 7b 0a 20 20 TK_COLLATE ){.
1a110 20 20 20 20 69 6e 74 20 6e 43 6f 6c 6c 3b 0a 20 int nColl;.
1a120 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 4c 69 zColl = pLi
1a130 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 75 stItem->pExpr->u
1a140 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e .zToken;. n
1a150 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 74 Coll = sqlite3St
1a160 72 6c 65 6e 33 30 28 7a 43 6f 6c 6c 29 20 2b 20 rlen30(zColl) +
1a170 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 1;. assert(
1a180 20 6e 45 78 74 72 61 3e 3d 6e 43 6f 6c 6c 20 29 nExtra>=nColl )
1a190 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a ;. memcpy(z
1a1a0 45 78 74 72 61 2c 20 7a 43 6f 6c 6c 2c 20 6e 43 Extra, zColl, nC
1a1b0 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c oll);. zCol
1a1c0 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20 20 l = zExtra;.
1a1d0 20 20 7a 45 78 74 72 61 20 2b 3d 20 6e 43 6f 6c zExtra += nCol
1a1e0 6c 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61 20 l;. nExtra
1a1f0 2d 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 7d 65 -= nColl;. }e
1a200 6c 73 65 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a lse if( j>=0 ){.
1a210 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 54 zColl = pT
1a220 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c ab->aCol[j].zCol
1a230 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 l;. }. if(
1a240 20 21 7a 43 6f 6c 6c 20 29 20 7a 43 6f 6c 6c 20 !zColl ) zColl
1a250 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 20 20 = "BINARY";.
1a260 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 if( !db->init.bu
1a270 73 79 20 26 26 20 21 73 71 6c 69 74 65 33 4c 6f sy && !sqlite3Lo
1a280 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 cateCollSeq(pPar
1a290 73 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 se, zColl) ){.
1a2a0 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 goto exit_cr
1a2b0 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 eate_index;.
1a2c0 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a }. pIndex->az
1a2d0 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b Coll[i] = zColl;
1a2e0 0a 20 20 20 20 72 65 71 75 65 73 74 65 64 53 6f . requestedSo
1a2f0 72 74 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 49 rtOrder = pListI
1a300 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 tem->sortOrder &
1a310 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a sortOrderMask;.
1a320 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 pIndex->aSor
1a330 74 4f 72 64 65 72 5b 69 5d 20 3d 20 28 75 38 29 tOrder[i] = (u8)
1a340 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 requestedSortOrd
1a350 65 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70 er;. }.. /* Ap
1a360 70 65 6e 64 20 74 68 65 20 74 61 62 6c 65 20 6b pend the table k
1a370 65 79 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 ey to the end of
1a380 20 74 68 65 20 69 6e 64 65 78 2e 20 20 46 6f 72 the index. For
1a390 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 0a 20 WITHOUT ROWID.
1a3a0 20 2a 2a 20 74 61 62 6c 65 73 20 28 77 68 65 6e ** tables (when
1a3b0 20 70 50 6b 21 3d 30 29 20 74 68 69 73 20 77 69 pPk!=0) this wi
1a3c0 6c 6c 20 62 65 20 74 68 65 20 64 65 63 6c 61 72 ll be the declar
1a3d0 65 64 20 50 52 49 4d 41 52 59 20 4b 45 59 2e 20 ed PRIMARY KEY.
1a3e0 20 46 6f 72 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c For. ** normal
1a3f0 20 74 61 62 6c 65 73 20 28 77 68 65 6e 20 70 50 tables (when pP
1a400 6b 3d 3d 30 29 20 74 68 69 73 20 77 69 6c 6c 20 k==0) this will
1a410 62 65 20 74 68 65 20 72 6f 77 69 64 2e 0a 20 20 be the rowid..
1a420 2a 2f 0a 20 20 69 66 28 20 70 50 6b 20 29 7b 0a */. if( pPk ){.
1a430 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 for(j=0; j<p
1a440 50 6b 2d 3e 6e 4b 65 79 43 6f 6c 3b 20 6a 2b 2b Pk->nKeyCol; j++
1a450 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 78 20 3d ){. int x =
1a460 20 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a pPk->aiColumn[j
1a470 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ];. assert(
1a480 20 78 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 x>=0 );. i
1a490 66 28 20 68 61 73 43 6f 6c 75 6d 6e 28 70 49 6e f( hasColumn(pIn
1a4a0 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 70 dex->aiColumn, p
1a4b0 49 6e 64 65 78 2d 3e 6e 4b 65 79 43 6f 6c 2c 20 Index->nKeyCol,
1a4c0 78 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 x) ){. pI
1a4d0 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 2d 2d 3b ndex->nColumn--;
1a4e0 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 . }else{.
1a4f0 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 pIndex->a
1a500 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 78 3b 0a iColumn[i] = x;.
1a510 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e pIndex->
1a520 61 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 70 50 6b 2d azColl[i] = pPk-
1a530 3e 61 7a 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 >azColl[j];.
1a540 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 pIndex->aSor
1a550 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 50 6b 2d tOrder[i] = pPk-
1a560 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 3b 0a >aSortOrder[j];.
1a570 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 i++;.
1a580 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 }. }. a
1a590 73 73 65 72 74 28 20 69 3d 3d 70 49 6e 64 65 78 ssert( i==pIndex
1a5a0 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 7d ->nColumn );. }
1a5b0 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 64 65 78 else{. pIndex
1a5c0 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 ->aiColumn[i] =
1a5d0 2d 31 3b 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e -1;. pIndex->
1a5e0 61 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 22 42 49 4e azColl[i] = "BIN
1a5f0 41 52 59 22 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 ARY";. }. sqli
1a600 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 te3DefaultRowEst
1a610 28 70 49 6e 64 65 78 29 3b 0a 20 20 69 66 28 20 (pIndex);. if(
1a620 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
1a630 65 3d 3d 30 20 29 20 65 73 74 69 6d 61 74 65 49 e==0 ) estimateI
1a640 6e 64 65 78 57 69 64 74 68 28 70 49 6e 64 65 78 ndexWidth(pIndex
1a650 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d );.. if( pTab==
1a660 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
1a670 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 e ){. /* This
1a680 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 routine has bee
1a690 6e 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 n called to crea
1a6a0 74 65 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 te an automatic
1a6b0 69 6e 64 65 78 20 61 73 20 61 0a 20 20 20 20 2a index as a. *
1a6c0 2a 20 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 * result of a PR
1a6d0 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 IMARY KEY or UNI
1a6e0 51 55 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 QUE clause on a
1a6f0 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f column definitio
1a700 6e 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 50 n, or. ** a P
1a710 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e RIMARY KEY or UN
1a720 49 51 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c IQUE clause foll
1a730 6f 77 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e owing the column
1a740 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 definitions..
1a750 20 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 ** i.e. one of
1a760 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 :. **. **
1a770 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 CREATE TABLE t(x
1a780 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 PRIMARY KEY, y)
1a790 3b 0a 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 ;. ** CREATE
1a7a0 54 41 42 4c 45 20 74 28 78 2c 20 79 2c 20 55 4e TABLE t(x, y, UN
1a7b0 49 51 55 45 28 78 2c 20 79 29 29 3b 0a 20 20 20 IQUE(x, y));.
1a7c0 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68 65 **. ** Eithe
1a7d0 72 20 77 61 79 2c 20 63 68 65 63 6b 20 74 6f 20 r way, check to
1a7e0 73 65 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 see if the table
1a7f0 20 61 6c 72 65 61 64 79 20 68 61 73 20 73 75 63 already has suc
1a800 68 20 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a 20 h an index. If.
1a810 20 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 ** so, don't
1a820 62 6f 74 68 65 72 20 63 72 65 61 74 69 6e 67 20 bother creating
1a830 74 68 69 73 20 6f 6e 65 2e 20 54 68 69 73 20 6f this one. This o
1a840 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 0a 20 nly applies to.
1a850 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 ** automatica
1a860 6c 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64 69 lly created indi
1a870 63 65 73 2e 20 55 73 65 72 73 20 63 61 6e 20 64 ces. Users can d
1a880 6f 20 61 73 20 74 68 65 79 20 77 69 73 68 20 77 o as they wish w
1a890 69 74 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c 69 ith. ** expli
1a8a0 63 69 74 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 cit indices..
1a8b0 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 77 6f 20 55 **. ** Two U
1a8c0 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41 52 59 NIQUE or PRIMARY
1a8d0 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 73 KEY constraints
1a8e0 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 are considered
1a8f0 65 71 75 69 76 61 6c 65 6e 74 0a 20 20 20 20 2a equivalent. *
1a900 2a 20 28 61 6e 64 20 74 68 75 73 20 73 75 70 70 * (and thus supp
1a910 72 65 73 73 69 6e 67 20 74 68 65 20 73 65 63 6f ressing the seco
1a920 6e 64 20 6f 6e 65 29 20 65 76 65 6e 20 69 66 20 nd one) even if
1a930 74 68 65 79 20 68 61 76 65 20 64 69 66 66 65 72 they have differ
1a940 65 6e 74 0a 20 20 20 20 2a 2a 20 73 6f 72 74 20 ent. ** sort
1a950 6f 72 64 65 72 73 2e 0a 20 20 20 20 2a 2a 0a 20 orders.. **.
1a960 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 61 ** If there a
1a970 72 65 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6c re different col
1a980 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 lating sequences
1a990 20 6f 72 20 69 66 20 74 68 65 20 63 6f 6c 75 6d or if the colum
1a9a0 6e 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 ns of. ** the
1a9b0 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 63 63 75 constraint occu
1a9c0 72 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 6f r in different o
1a9d0 72 64 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 rders, then the
1a9e0 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 0a constraints are.
1a9f0 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65 ** considere
1aa00 64 20 64 69 73 74 69 6e 63 74 20 61 6e 64 20 62 d distinct and b
1aa10 6f 74 68 20 72 65 73 75 6c 74 20 69 6e 20 73 65 oth result in se
1aa20 70 61 72 61 74 65 20 69 6e 64 69 63 65 73 2e 0a parate indices..
1aa30 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 */. Index
1aa40 20 2a 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 *pIdx;. for(
1aa50 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 pIdx=pTab->pInde
1aa60 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 x; pIdx; pIdx=pI
1aa70 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 dx->pNext){.
1aa80 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 61 int k;. a
1aa90 73 73 65 72 74 28 20 49 73 55 6e 69 71 75 65 49 ssert( IsUniqueI
1aaa0 6e 64 65 78 28 70 49 64 78 29 20 29 3b 0a 20 20 ndex(pIdx) );.
1aab0 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 assert( pIdx
1aac0 2d 3e 69 64 78 54 79 70 65 21 3d 53 51 4c 49 54 ->idxType!=SQLIT
1aad0 45 5f 49 44 58 54 59 50 45 5f 41 50 50 44 45 46 E_IDXTYPE_APPDEF
1aae0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1aaf0 28 20 49 73 55 6e 69 71 75 65 49 6e 64 65 78 28 ( IsUniqueIndex(
1ab00 70 49 6e 64 65 78 29 20 29 3b 0a 0a 20 20 20 20 pIndex) );..
1ab10 20 20 69 66 28 20 70 49 64 78 2d 3e 6e 4b 65 79 if( pIdx->nKey
1ab20 43 6f 6c 21 3d 70 49 6e 64 65 78 2d 3e 6e 4b 65 Col!=pIndex->nKe
1ab30 79 43 6f 6c 20 29 20 63 6f 6e 74 69 6e 75 65 3b yCol ) continue;
1ab40 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 . for(k=0;
1ab50 6b 3c 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 3b k<pIdx->nKeyCol;
1ab60 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 k++){. c
1ab70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31 3b 0a 20 onst char *z1;.
1ab80 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 const cha
1ab90 72 20 2a 7a 32 3b 0a 20 20 20 20 20 20 20 20 61 r *z2;. a
1aba0 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 69 43 ssert( pIdx->aiC
1abb0 6f 6c 75 6d 6e 5b 6b 5d 3e 3d 30 20 29 3b 0a 20 olumn[k]>=0 );.
1abc0 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d if( pIdx-
1abd0 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 >aiColumn[k]!=pI
1abe0 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b ndex->aiColumn[k
1abf0 5d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 ] ) break;.
1ac00 20 20 20 7a 31 20 3d 20 70 49 64 78 2d 3e 61 7a z1 = pIdx->az
1ac10 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 Coll[k];.
1ac20 20 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a z2 = pIndex->az
1ac30 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 Coll[k];.
1ac40 20 69 66 28 20 7a 31 21 3d 7a 32 20 26 26 20 73 if( z1!=z2 && s
1ac50 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31 qlite3StrICmp(z1
1ac60 2c 20 7a 32 29 20 29 20 62 72 65 61 6b 3b 0a 20 , z2) ) break;.
1ac70 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1ac80 20 6b 3d 3d 70 49 64 78 2d 3e 6e 4b 65 79 43 6f k==pIdx->nKeyCo
1ac90 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 l ){. if(
1aca0 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d pIdx->onError!=
1acb0 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 pIndex->onError
1acc0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
1acd0 54 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 This constraint
1ace0 63 72 65 61 74 65 73 20 74 68 65 20 73 61 6d 65 creates the same
1acf0 20 69 6e 64 65 78 20 61 73 20 61 20 70 72 65 76 index as a prev
1ad00 69 6f 75 73 0a 20 20 20 20 20 20 20 20 20 20 2a ious. *
1ad10 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 70 65 * constraint spe
1ad20 63 69 66 69 65 64 20 73 6f 6d 65 77 68 65 72 65 cified somewhere
1ad30 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 in the CREATE T
1ad40 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a ABLE statement..
1ad50 20 20 20 20 20 20 20 20 20 20 2a 2a 20 48 6f 77 ** How
1ad60 65 76 65 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46 ever the ON CONF
1ad70 4c 49 43 54 20 63 6c 61 75 73 65 73 20 61 72 65 LICT clauses are
1ad80 20 64 69 66 66 65 72 65 6e 74 2e 20 49 66 20 62 different. If b
1ad90 6f 74 68 20 74 68 69 73 20 0a 20 20 20 20 20 20 oth this .
1ada0 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e ** constrain
1adb0 74 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f t and the previo
1adc0 75 73 20 65 71 75 69 76 61 6c 65 6e 74 20 63 6f us equivalent co
1add0 6e 73 74 72 61 69 6e 74 20 68 61 76 65 20 65 78 nstraint have ex
1ade0 70 6c 69 63 69 74 0a 20 20 20 20 20 20 20 20 20 plicit.
1adf0 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 ** ON CONFLICT
1ae00 63 6c 61 75 73 65 73 20 74 68 69 73 20 69 73 20 clauses this is
1ae10 61 6e 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 an error. Otherw
1ae20 69 73 65 2c 20 75 73 65 20 74 68 65 0a 20 20 20 ise, use the.
1ae30 20 20 20 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 ** explic
1ae40 69 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 itly specified b
1ae50 65 68 61 76 69 6f 72 20 66 6f 72 20 74 68 65 20 ehavior for the
1ae60 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20 20 index..
1ae70 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 */. if
1ae80 28 20 21 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f ( !(pIdx->onErro
1ae90 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c r==OE_Default ||
1aea0 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 pIndex->onError
1aeb0 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29 20 29 7b ==OE_Default) ){
1aec0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
1aed0 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1aee0 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 rse, .
1aef0 20 20 20 20 20 20 22 63 6f 6e 66 6c 69 63 74 69 "conflicti
1af00 6e 67 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 ng ON CONFLICT c
1af10 6c 61 75 73 65 73 20 73 70 65 63 69 66 69 65 64 lauses specified
1af20 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 ", 0);.
1af30 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 }. if(
1af40 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d pIdx->onError==
1af50 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 OE_Default ){.
1af60 20 20 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e pIdx->
1af70 6f 6e 45 72 72 6f 72 20 3d 20 70 49 6e 64 65 78 onError = pIndex
1af80 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 ->onError;.
1af90 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1afa0 0a 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 . pRet =
1afb0 70 49 64 78 3b 0a 20 20 20 20 20 20 20 20 67 6f pIdx;. go
1afc0 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
1afd0 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ndex;. }.
1afe0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69 }. }.. /* Li
1aff0 6e 6b 20 74 68 65 20 6e 65 77 20 49 6e 64 65 78 nk the new Index
1b000 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 69 74 structure to it
1b010 73 20 74 61 62 6c 65 20 61 6e 64 20 74 6f 20 74 s table and to t
1b020 68 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e he other. ** in
1b030 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1b040 20 73 74 72 75 63 74 75 72 65 73 2e 20 0a 20 20 structures. .
1b050 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
1b060 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 0a rse->nErr==0 );.
1b070 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 if( db->init.b
1b080 75 73 79 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 usy ){. Index
1b090 20 2a 70 3b 0a 20 20 20 20 61 73 73 65 72 74 28 *p;. assert(
1b0a0 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 sqlite3SchemaMu
1b0b0 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c 20 70 texHeld(db, 0, p
1b0c0 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 20 Index->pSchema)
1b0d0 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 );. p = sqlit
1b0e0 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 49 e3HashInsert(&pI
1b0f0 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 ndex->pSchema->i
1b100 64 78 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20 dxHash, .
1b110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b120 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 pIndex->zName
1b130 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 69 , pIndex);. i
1b140 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 61 73 f( p ){. as
1b150 73 65 72 74 28 20 70 3d 3d 70 49 6e 64 65 78 20 sert( p==pIndex
1b160 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 ); /* Malloc mu
1b170 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a st have failed *
1b180 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c /. db->mall
1b190 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
1b1a0 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 goto exit_cr
1b1b0 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 eate_index;.
1b1c0 7d 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 }. db->flags
1b1d0 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e |= SQLITE_Intern
1b1e0 43 68 61 6e 67 65 73 3b 0a 20 20 20 20 69 66 28 Changes;. if(
1b1f0 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a pTblName!=0 ){.
1b200 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e pIndex->tn
1b210 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 um = db->init.ne
1b220 77 54 6e 75 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d wTnum;. }. }
1b230 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
1b240 73 20 74 68 65 20 69 6e 69 74 69 61 6c 20 43 52 s the initial CR
1b250 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 EATE INDEX state
1b260 6d 65 6e 74 20 28 6f 72 20 43 52 45 41 54 45 20 ment (or CREATE
1b270 54 41 42 4c 45 20 69 66 20 74 68 65 0a 20 20 2a TABLE if the. *
1b280 2a 20 69 6e 64 65 78 20 69 73 20 61 6e 20 69 6d * index is an im
1b290 70 6c 69 65 64 20 69 6e 64 65 78 20 66 6f 72 20 plied index for
1b2a0 61 20 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d a UNIQUE or PRIM
1b2b0 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 ARY KEY constrai
1b2c0 6e 74 29 20 74 68 65 6e 0a 20 20 2a 2a 20 65 6d nt) then. ** em
1b2d0 69 74 20 63 6f 64 65 20 74 6f 20 61 6c 6c 6f 63 it code to alloc
1b2e0 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 72 6f ate the index ro
1b2f0 6f 74 70 61 67 65 20 6f 6e 20 64 69 73 6b 20 61 otpage on disk a
1b300 6e 64 20 6d 61 6b 65 20 61 6e 20 65 6e 74 72 79 nd make an entry
1b310 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 69 6e for. ** the in
1b320 64 65 78 20 69 6e 20 74 68 65 20 73 71 6c 69 74 dex in the sqlit
1b330 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 e_master table a
1b340 6e 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 nd populate the
1b350 69 6e 64 65 78 20 77 69 74 68 0a 20 20 2a 2a 20 index with. **
1b360 63 6f 6e 74 65 6e 74 2e 20 20 42 75 74 2c 20 64 content. But, d
1b370 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20 69 66 o not do this if
1b380 20 77 65 20 61 72 65 20 73 69 6d 70 6c 79 20 72 we are simply r
1b390 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74 eading the sqlit
1b3a0 65 5f 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 e_master. ** ta
1b3b0 62 6c 65 20 74 6f 20 70 61 72 73 65 20 74 68 65 ble to parse the
1b3c0 20 73 63 68 65 6d 61 2c 20 6f 72 20 69 66 20 74 schema, or if t
1b3d0 68 69 73 20 69 6e 64 65 78 20 69 73 20 74 68 65 his index is the
1b3e0 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 64 PRIMARY KEY ind
1b3f0 65 78 0a 20 20 2a 2a 20 6f 66 20 61 20 57 49 54 ex. ** of a WIT
1b400 48 4f 55 54 20 52 4f 57 49 44 20 74 61 62 6c 65 HOUT ROWID table
1b410 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 .. **. ** If p
1b420 54 62 6c 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 TblName==0 it me
1b430 61 6e 73 20 74 68 69 73 20 69 6e 64 65 78 20 69 ans this index i
1b440 73 20 67 65 6e 65 72 61 74 65 64 20 61 73 20 61 s generated as a
1b450 6e 20 69 6d 70 6c 69 65 64 20 50 52 49 4d 41 52 n implied PRIMAR
1b460 59 20 4b 45 59 0a 20 20 2a 2a 20 6f 72 20 55 4e Y KEY. ** or UN
1b470 49 51 55 45 20 69 6e 64 65 78 20 69 6e 20 61 20 IQUE index in a
1b480 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 CREATE TABLE sta
1b490 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65 20 74 tement. Since t
1b4a0 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 68 61 he table. ** ha
1b4b0 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72 65 61 s just been crea
1b4c0 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69 6e 73 ted, it contains
1b4d0 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74 68 65 no data and the
1b4e0 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c 69 7a index initializ
1b4f0 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65 70 20 ation. ** step
1b500 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e 0a can be skipped..
1b510 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20 */. else if(
1b520 48 61 73 52 6f 77 69 64 28 70 54 61 62 29 20 7c HasRowid(pTab) |
1b530 7c 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b | pTblName!=0 ){
1b540 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 . Vdbe *v;.
1b550 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 char *zStmt;.
1b560 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b int iMem = ++
1b570 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 pParse->nMem;..
1b580 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 v = sqlite3Ge
1b590 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
1b5a0 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f if( v==0 ) go
1b5b0 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
1b5c0 6e 64 65 78 3b 0a 0a 20 20 20 20 73 71 6c 69 74 ndex;.. sqlit
1b5d0 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 e3BeginWriteOper
1b5e0 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c ation(pParse, 1,
1b5f0 20 69 44 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 iDb);.. /* C
1b600 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61 reate the rootpa
1b610 67 65 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 ge for the index
1b620 20 75 73 69 6e 67 20 43 72 65 61 74 65 49 6e 64 using CreateInd
1b630 65 78 2e 20 42 75 74 20 62 65 66 6f 72 65 0a 20 ex. But before.
1b640 20 20 20 2a 2a 20 64 6f 69 6e 67 20 73 6f 2c 20 ** doing so,
1b650 63 6f 64 65 20 61 20 4e 6f 6f 70 20 69 6e 73 74 code a Noop inst
1b660 72 75 63 74 69 6f 6e 20 61 6e 64 20 73 74 6f 72 ruction and stor
1b670 65 20 69 74 73 20 61 64 64 72 65 73 73 20 69 6e e its address in
1b680 20 0a 20 20 20 20 2a 2a 20 49 6e 64 65 78 2e 74 . ** Index.t
1b690 6e 75 6d 2e 20 54 68 69 73 20 69 73 20 72 65 71 num. This is req
1b6a0 75 69 72 65 64 20 69 6e 20 63 61 73 65 20 74 68 uired in case th
1b6b0 69 73 20 69 6e 64 65 78 20 69 73 20 61 63 74 75 is index is actu
1b6c0 61 6c 6c 79 20 61 20 0a 20 20 20 20 2a 2a 20 50 ally a . ** P
1b6d0 52 49 4d 41 52 59 20 4b 45 59 20 61 6e 64 20 74 RIMARY KEY and t
1b6e0 68 65 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 he table is actu
1b6f0 61 6c 6c 79 20 61 20 57 49 54 48 4f 55 54 20 52 ally a WITHOUT R
1b700 4f 57 49 44 20 74 61 62 6c 65 2e 20 49 6e 20 0a OWID table. In .
1b710 20 20 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65 ** that case
1b720 20 74 68 65 20 63 6f 6e 76 65 72 74 54 6f 57 69 the convertToWi
1b730 74 68 6f 75 74 52 6f 77 69 64 54 61 62 6c 65 28 thoutRowidTable(
1b740 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 ) routine will r
1b750 65 70 6c 61 63 65 0a 20 20 20 20 2a 2a 20 74 68 eplace. ** th
1b760 65 20 4e 6f 6f 70 20 77 69 74 68 20 61 20 47 6f e Noop with a Go
1b770 74 6f 20 74 6f 20 6a 75 6d 70 20 6f 76 65 72 20 to to jump over
1b780 74 68 65 20 56 44 42 45 20 63 6f 64 65 20 67 65 the VDBE code ge
1b790 6e 65 72 61 74 65 64 20 62 65 6c 6f 77 2e 20 2a nerated below. *
1b7a0 2f 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e /. pIndex->tn
1b7b0 75 6d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 um = sqlite3Vdbe
1b7c0 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f 6f AddOp0(v, OP_Noo
1b7d0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 p);. sqlite3V
1b7e0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1b7f0 43 72 65 61 74 65 49 6e 64 65 78 2c 20 69 44 62 CreateIndex, iDb
1b800 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 2f 2a , iMem);.. /*
1b810 20 47 61 74 68 65 72 20 74 68 65 20 63 6f 6d 70 Gather the comp
1b820 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 lete text of the
1b830 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 CREATE INDEX st
1b840 61 74 65 6d 65 6e 74 20 69 6e 74 6f 0a 20 20 20 atement into.
1b850 20 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20 76 61 ** the zStmt va
1b860 72 69 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 riable. */.
1b870 20 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a if( pStart ){.
1b880 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 28 69 int n = (i
1b890 6e 74 29 28 70 50 61 72 73 65 2d 3e 73 4c 61 73 nt)(pParse->sLas
1b8a0 74 54 6f 6b 65 6e 2e 7a 20 2d 20 70 4e 61 6d 65 tToken.z - pName
1b8b0 2d 3e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e 73 ->z) + pParse->s
1b8c0 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 LastToken.n;.
1b8d0 20 20 20 69 66 28 20 70 4e 61 6d 65 2d 3e 7a 5b if( pName->z[
1b8e0 6e 2d 31 5d 3d 3d 27 3b 27 20 29 20 6e 2d 2d 3b n-1]==';' ) n--;
1b8f0 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65 . /* A name
1b900 64 20 69 6e 64 65 78 20 77 69 74 68 20 61 6e 20 d index with an
1b910 65 78 70 6c 69 63 69 74 20 43 52 45 41 54 45 20 explicit CREATE
1b920 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 INDEX statement
1b930 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d */. zStmt =
1b940 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1b950 64 62 2c 20 22 43 52 45 41 54 45 25 73 20 49 4e db, "CREATE%s IN
1b960 44 45 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20 DEX %.*s",.
1b970 20 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e onError==OE_N
1b980 6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 one ? "" : " UNI
1b990 51 55 45 22 2c 20 6e 2c 20 70 4e 61 6d 65 2d 3e QUE", n, pName->
1b9a0 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 z);. }else{.
1b9b0 20 20 20 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d /* An autom
1b9c0 61 74 69 63 20 69 6e 64 65 78 20 63 72 65 61 74 atic index creat
1b9d0 65 64 20 62 79 20 61 20 50 52 49 4d 41 52 59 20 ed by a PRIMARY
1b9e0 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6f KEY or UNIQUE co
1b9f0 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 nstraint */.
1ba00 20 20 2f 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c /* zStmt = sql
1ba10 69 74 65 33 4d 50 72 69 6e 74 66 28 22 22 29 3b ite3MPrintf("");
1ba20 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 */. zStmt
1ba30 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 = 0;. }..
1ba40 2f 2a 20 41 64 64 20 61 6e 20 65 6e 74 72 79 20 /* Add an entry
1ba50 69 6e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 in sqlite_master
1ba60 20 66 6f 72 20 74 68 69 73 20 69 6e 64 65 78 0a for this index.
1ba70 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
1ba80 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
1ba90 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 arse, . "
1baa0 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e 25 INSERT INTO %Q.%
1bab0 73 20 56 41 4c 55 45 53 28 27 69 6e 64 65 78 27 s VALUES('index'
1bac0 2c 25 51 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22 ,%Q,%Q,#%d,%Q);"
1bad0 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 ,. db->aD
1bae0 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 b[iDb].zName, SC
1baf0 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c HEMA_TABLE(iDb),
1bb00 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d . pIndex-
1bb10 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 >zName,.
1bb20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 pTab->zName,.
1bb30 20 20 20 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20 iMem,.
1bb40 20 20 20 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a zStmt. );.
1bb50 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1bb60 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 e(db, zStmt);..
1bb70 20 20 20 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69 /* Fill the i
1bb80 6e 64 65 78 20 77 69 74 68 20 64 61 74 61 20 61 ndex with data a
1bb90 6e 64 20 72 65 70 61 72 73 65 20 74 68 65 20 73 nd reparse the s
1bba0 63 68 65 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f chema. Code an O
1bbb0 50 5f 45 78 70 69 72 65 0a 20 20 20 20 2a 2a 20 P_Expire. **
1bbc0 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c to invalidate al
1bbd0 6c 20 70 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 l pre-compiled s
1bbe0 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a tatements.. *
1bbf0 2f 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e 61 /. if( pTblNa
1bc00 6d 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 me ){. sqli
1bc10 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 te3RefillIndex(p
1bc20 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 Parse, pIndex, i
1bc30 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 Mem);. sqli
1bc40 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 te3ChangeCookie(
1bc50 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 pParse, iDb);.
1bc60 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1bc70 64 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70 28 ddParseSchemaOp(
1bc80 76 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 v, iDb,.
1bc90 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1bca0 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 20 41 db, "name='%q' A
1bcb0 4e 44 20 74 79 70 65 3d 27 69 6e 64 65 78 27 22 ND type='index'"
1bcc0 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 , pIndex->zName)
1bcd0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1bce0 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
1bcf0 5f 45 78 70 69 72 65 2c 20 30 29 3b 0a 20 20 20 _Expire, 0);.
1bd00 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 }.. sqlite3V
1bd10 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 dbeJumpHere(v, p
1bd20 49 6e 64 65 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 Index->tnum);.
1bd30 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64 64 }.. /* When add
1bd40 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 ing an index to
1bd50 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 the list of indi
1bd60 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 2c ces for a table,
1bd70 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 make. ** sure
1bd80 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62 65 all indices labe
1bd90 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20 63 led OE_Replace c
1bda0 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74 68 ome after all th
1bdb0 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a 2a ose labeled. **
1bdc0 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68 69 OE_Ignore. Thi
1bdd0 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 66 s is necessary f
1bde0 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74 20 63 or the correct c
1bdf0 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 0a onstraint check.
1be00 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 ** processing
1be10 28 69 6e 20 73 71 6c 69 74 65 33 47 65 6e 65 72 (in sqlite3Gener
1be20 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 ateConstraintChe
1be30 63 6b 73 28 29 29 20 61 73 20 70 61 72 74 20 6f cks()) as part o
1be40 66 0a 20 20 2a 2a 20 55 50 44 41 54 45 20 61 6e f. ** UPDATE an
1be50 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 d INSERT stateme
1be60 6e 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66 nts. . */. if
1be70 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 ( db->init.busy
1be80 7c 7c 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 || pTblName==0 )
1be90 7b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f {. if( onErro
1bea0 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c r!=OE_Replace ||
1beb0 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 pTab->pIndex==0
1bec0 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 54 61 . || pTa
1bed0 62 2d 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 b->pIndex->onErr
1bee0 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b or==OE_Replace){
1bef0 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 . pIndex->p
1bf00 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e Next = pTab->pIn
1bf10 64 65 78 3b 0a 20 20 20 20 20 20 70 54 61 62 2d dex;. pTab-
1bf20 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 >pIndex = pIndex
1bf30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1bf40 20 20 20 49 6e 64 65 78 20 2a 70 4f 74 68 65 72 Index *pOther
1bf50 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b = pTab->pIndex;
1bf60 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 4f . while( pO
1bf70 74 68 65 72 2d 3e 70 4e 65 78 74 20 26 26 20 70 ther->pNext && p
1bf80 4f 74 68 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e Other->pNext->on
1bf90 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 Error!=OE_Replac
1bfa0 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 74 e ){. pOt
1bfb0 68 65 72 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e her = pOther->pN
1bfc0 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ext;. }.
1bfd0 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 pIndex->pNext
1bfe0 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 = pOther->pNext
1bff0 3b 0a 20 20 20 20 20 20 70 4f 74 68 65 72 2d 3e ;. pOther->
1c000 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 3b 0a pNext = pIndex;.
1c010 20 20 20 20 7d 0a 20 20 20 20 70 52 65 74 20 3d }. pRet =
1c020 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 49 6e pIndex;. pIn
1c030 64 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 dex = 0;. }..
1c040 2f 2a 20 43 6c 65 61 6e 20 75 70 20 62 65 66 6f /* Clean up befo
1c050 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 65 78 re exiting */.ex
1c060 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3a it_create_index:
1c070 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20 29 20 . if( pIndex )
1c080 66 72 65 65 49 6e 64 65 78 28 64 62 2c 20 70 49 freeIndex(db, pI
1c090 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 ndex);. sqlite3
1c0a0 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1c0b0 50 49 57 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 PIWhere);. sqli
1c0c0 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 te3ExprListDelet
1c0d0 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 e(db, pList);.
1c0e0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 sqlite3SrcListDe
1c0f0 6c 65 74 65 28 64 62 2c 20 70 54 62 6c 4e 61 6d lete(db, pTblNam
1c100 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 e);. sqlite3DbF
1c110 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a ree(db, zName);.
1c120 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d return pRet;.}
1c130 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 ../*.** Fill the
1c140 20 49 6e 64 65 78 2e 61 69 52 6f 77 45 73 74 5b Index.aiRowEst[
1c150 5d 20 61 72 72 61 79 20 77 69 74 68 20 64 65 66 ] array with def
1c160 61 75 6c 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ault information
1c170 20 2d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a - information.*
1c180 2a 20 74 6f 20 62 65 20 75 73 65 64 20 77 68 65 * to be used whe
1c190 6e 20 77 65 20 68 61 76 65 20 6e 6f 74 20 72 75 n we have not ru
1c1a0 6e 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f n the ANALYZE co
1c1b0 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 mmand..**.** aiR
1c1c0 6f 77 45 73 74 5b 30 5d 20 69 73 20 73 75 70 70 owEst[0] is supp
1c1d0 6f 73 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 osed to contain
1c1e0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c the number of el
1c1f0 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 69 6e ements in the in
1c200 64 65 78 2e 0a 2a 2a 20 53 69 6e 63 65 20 77 65 dex..** Since we
1c210 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 do not know, gu
1c220 65 73 73 20 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 ess 1 million.
1c230 61 69 52 6f 77 45 73 74 5b 31 5d 20 69 73 20 61 aiRowEst[1] is a
1c240 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 n estimate of th
1c250 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72 e.** number of r
1c260 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ows in the table
1c270 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20 that match any
1c280 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65 particular value
1c290 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 of the.** first
1c2a0 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 column of the i
1c2b0 6e 64 65 78 2e 20 20 61 69 52 6f 77 45 73 74 5b ndex. aiRowEst[
1c2c0 32 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 2] is an estimat
1c2d0 65 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 0a e of the number.
1c2e0 2a 2a 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 ** of rows that
1c2f0 6d 61 74 63 68 20 61 6e 79 20 70 61 72 74 69 63 match any partic
1c300 75 6c 61 72 20 63 6f 6d 62 69 6e 61 74 69 6f 6e ular combination
1c310 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 32 20 of the first 2
1c320 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 columns.** of th
1c330 65 20 69 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f e index. And so
1c340 20 66 6f 72 74 68 2e 20 20 49 74 20 6d 75 73 74 forth. It must
1c350 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 63 always be the c
1c360 61 73 65 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 ase that.*.**
1c370 20 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 aiRowEst
1c380 5b 4e 5d 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d [N]<=aiRowEst[N-
1c390 31 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 1].**
1c3a0 61 69 52 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a aiRowEst[N]>=1.*
1c3b0 2a 0a 2a 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 *.** Apart from
1c3c0 74 68 61 74 2c 20 77 65 20 68 61 76 65 20 6c 69 that, we have li
1c3d0 74 74 6c 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 ttle to go on be
1c3e0 73 69 64 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 sides intuition
1c3f0 61 73 20 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 as to.** how aiR
1c400 6f 77 45 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 owEst[] should b
1c410 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 e initialized.
1c420 54 68 65 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 The numbers gene
1c430 72 61 74 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 rated here.** ar
1c440 65 20 62 61 73 65 64 20 6f 6e 20 74 79 70 69 63 e based on typic
1c450 61 6c 20 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 al values found
1c460 69 6e 20 61 63 74 75 61 6c 20 69 6e 64 69 63 65 in actual indice
1c470 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 s..*/.void sqlit
1c480 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 e3DefaultRowEst(
1c490 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 Index *pIdx){.
1c4a0 2f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /*
1c4b0 20 20 31 30 2c 20 20 39 2c 20 20 38 2c 20 20 37 10, 9, 8, 7
1c4c0 2c 20 20 36 20 2a 2f 0a 20 20 4c 6f 67 45 73 74 , 6 */. LogEst
1c4d0 20 61 56 61 6c 5b 5d 20 3d 20 7b 20 33 33 2c 20 aVal[] = { 33,
1c4e0 33 32 2c 20 33 30 2c 20 32 38 2c 20 32 36 20 7d 32, 30, 28, 26 }
1c4f0 3b 0a 20 20 4c 6f 67 45 73 74 20 2a 61 20 3d 20 ;. LogEst *a =
1c500 70 49 64 78 2d 3e 61 69 52 6f 77 4c 6f 67 45 73 pIdx->aiRowLogEs
1c510 74 3b 0a 20 20 69 6e 74 20 6e 43 6f 70 79 20 3d t;. int nCopy =
1c520 20 4d 49 4e 28 41 72 72 61 79 53 69 7a 65 28 61 MIN(ArraySize(a
1c530 56 61 6c 29 2c 20 70 49 64 78 2d 3e 6e 4b 65 79 Val), pIdx->nKey
1c540 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a Col);. int i;..
1c550 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 69 72 /* Set the fir
1c560 73 74 20 65 6e 74 72 79 20 28 6e 75 6d 62 65 72 st entry (number
1c570 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 of rows in the
1c580 69 6e 64 65 78 29 20 74 6f 20 74 68 65 20 65 73 index) to the es
1c590 74 69 6d 61 74 65 64 20 0a 20 20 2a 2a 20 6e 75 timated . ** nu
1c5a0 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 mber of rows in
1c5b0 74 68 65 20 74 61 62 6c 65 2e 20 4f 72 20 31 30 the table. Or 10
1c5c0 2c 20 69 66 20 74 68 65 20 65 73 74 69 6d 61 74 , if the estimat
1c5d0 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 ed number of row
1c5e0 73 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 74 s . ** in the t
1c5f0 61 62 6c 65 20 69 73 20 6c 65 73 73 20 74 68 61 able is less tha
1c600 6e 20 74 68 61 74 2e 20 20 2a 2f 0a 20 20 61 5b n that. */. a[
1c610 30 5d 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 0] = pIdx->pTabl
1c620 65 2d 3e 6e 52 6f 77 4c 6f 67 45 73 74 3b 0a 20 e->nRowLogEst;.
1c630 20 69 66 28 20 61 5b 30 5d 3c 33 33 20 29 20 61 if( a[0]<33 ) a
1c640 5b 30 5d 20 3d 20 33 33 3b 20 20 20 20 20 20 20 [0] = 33;
1c650 20 61 73 73 65 72 74 28 20 33 33 3d 3d 73 71 6c assert( 33==sql
1c660 69 74 65 33 4c 6f 67 45 73 74 28 31 30 29 20 29 ite3LogEst(10) )
1c670 3b 0a 0a 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 ;.. /* Estimate
1c680 20 74 68 61 74 20 61 5b 31 5d 20 69 73 20 31 30 that a[1] is 10
1c690 2c 20 61 5b 32 5d 20 69 73 20 39 2c 20 61 5b 33 , a[2] is 9, a[3
1c6a0 5d 20 69 73 20 38 2c 20 61 5b 34 5d 20 69 73 20 ] is 8, a[4] is
1c6b0 37 2c 20 61 5b 35 5d 20 69 73 0a 20 20 2a 2a 20 7, a[5] is. **
1c6c0 36 20 61 6e 64 20 65 61 63 68 20 73 75 62 73 65 6 and each subse
1c6d0 71 75 65 6e 74 20 76 61 6c 75 65 20 28 69 66 20 quent value (if
1c6e0 61 6e 79 29 20 69 73 20 35 2e 20 20 2a 2f 0a 20 any) is 5. */.
1c6f0 20 6d 65 6d 63 70 79 28 26 61 5b 31 5d 2c 20 61 memcpy(&a[1], a
1c700 56 61 6c 2c 20 6e 43 6f 70 79 2a 73 69 7a 65 6f Val, nCopy*sizeo
1c710 66 28 4c 6f 67 45 73 74 29 29 3b 0a 20 20 66 6f f(LogEst));. fo
1c720 72 28 69 3d 6e 43 6f 70 79 2b 31 3b 20 69 3c 3d r(i=nCopy+1; i<=
1c730 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 3b 20 69 pIdx->nKeyCol; i
1c740 2b 2b 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 ++){. a[i] =
1c750 32 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 23;
1c760 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 32 assert( 2
1c770 33 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 3==sqlite3LogEst
1c780 28 35 29 20 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 (5) );. }.. as
1c790 73 65 72 74 28 20 30 3d 3d 73 71 6c 69 74 65 33 sert( 0==sqlite3
1c7a0 4c 6f 67 45 73 74 28 31 29 20 29 3b 0a 20 20 69 LogEst(1) );. i
1c7b0 66 28 20 49 73 55 6e 69 71 75 65 49 6e 64 65 78 f( IsUniqueIndex
1c7c0 28 70 49 64 78 29 20 29 20 61 5b 70 49 64 78 2d (pIdx) ) a[pIdx-
1c7d0 3e 6e 4b 65 79 43 6f 6c 5d 20 3d 20 30 3b 0a 7d >nKeyCol] = 0;.}
1c7e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
1c7f0 74 69 6e 65 20 77 69 6c 6c 20 64 72 6f 70 20 61 tine will drop a
1c800 6e 20 65 78 69 73 74 69 6e 67 20 6e 61 6d 65 64 n existing named
1c810 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20 72 6f index. This ro
1c820 75 74 69 6e 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 utine.** impleme
1c830 6e 74 73 20 74 68 65 20 44 52 4f 50 20 49 4e 44 nts the DROP IND
1c840 45 58 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f EX statement..*/
1c850 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f .void sqlite3Dro
1c860 70 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 pIndex(Parse *pP
1c870 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 arse, SrcList *p
1c880 4e 61 6d 65 2c 20 69 6e 74 20 69 66 45 78 69 73 Name, int ifExis
1c890 74 73 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 ts){. Index *pI
1c8a0 6e 64 65 78 3b 0a 20 20 56 64 62 65 20 2a 76 3b ndex;. Vdbe *v;
1c8b0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1c8c0 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
1c8d0 6e 74 20 69 44 62 3b 0a 0a 20 20 61 73 73 65 72 nt iDb;.. asser
1c8e0 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d t( pParse->nErr=
1c8f0 3d 30 20 29 3b 20 20 20 2f 2a 20 4e 65 76 65 72 =0 ); /* Never
1c900 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 72 69 called with pri
1c910 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 or errors */. i
1c920 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1c930 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 led ){. goto
1c940 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b exit_drop_index;
1c950 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1c960 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b Name->nSrc==1 );
1c970 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
1c980 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 !=sqlite3ReadSch
1c990 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 ema(pParse) ){.
1c9a0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
1c9b0 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70 p_index;. }. p
1c9c0 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 Index = sqlite3F
1c9d0 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 70 4e 61 indIndex(db, pNa
1c9e0 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 me->a[0].zName,
1c9f0 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 pName->a[0].zDat
1ca00 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49 abase);. if( pI
1ca10 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 69 ndex==0 ){. i
1ca20 66 28 20 21 69 66 45 78 69 73 74 73 20 29 7b 0a f( !ifExists ){.
1ca30 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1ca40 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e orMsg(pParse, "n
1ca50 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25 53 o such index: %S
1ca60 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 ", pName, 0);.
1ca70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
1ca80 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 qlite3CodeVerify
1ca90 4e 61 6d 65 64 53 63 68 65 6d 61 28 70 50 61 72 NamedSchema(pPar
1caa0 73 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e se, pName->a[0].
1cab0 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 zDatabase);.
1cac0 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68 }. pParse->ch
1cad0 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 eckSchema = 1;.
1cae0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
1caf0 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 p_index;. }. i
1cb00 66 28 20 70 49 6e 64 65 78 2d 3e 69 64 78 54 79 f( pIndex->idxTy
1cb10 70 65 21 3d 53 51 4c 49 54 45 5f 49 44 58 54 59 pe!=SQLITE_IDXTY
1cb20 50 45 5f 41 50 50 44 45 46 20 29 7b 0a 20 20 20 PE_APPDEF ){.
1cb30 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1cb40 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20 (pParse, "index
1cb50 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1cb60 55 4e 49 51 55 45 20 22 0a 20 20 20 20 20 20 22 UNIQUE ". "
1cb70 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 or PRIMARY KEY c
1cb80 6f 6e 73 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 onstraint cannot
1cb90 20 62 65 20 64 72 6f 70 70 65 64 22 2c 20 30 29 be dropped", 0)
1cba0 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f ;. goto exit_
1cbb0 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a drop_index;. }.
1cbc0 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
1cbd0 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
1cbe0 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 pIndex->pSchema
1cbf0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
1cc00 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 E_OMIT_AUTHORIZA
1cc10 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 TION. {. int
1cc20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 code = SQLITE_D
1cc30 52 4f 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 ROP_INDEX;. T
1cc40 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e able *pTab = pIn
1cc50 64 65 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 dex->pTable;.
1cc60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
1cc70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
1cc80 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 zName;. const
1cc90 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 char *zTab = SC
1cca0 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b HEMA_TABLE(iDb);
1ccb0 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1ccc0 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
1ccd0 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c , SQLITE_DELETE,
1cce0 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 zTab, 0, zDb) )
1ccf0 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 {. goto exi
1cd00 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 t_drop_index;.
1cd10 20 20 7d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 }. if( !OMI
1cd20 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 20 T_TEMPDB && iDb
1cd30 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f ) code = SQLITE_
1cd40 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b DROP_TEMP_INDEX;
1cd50 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1cd60 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
1cd70 2c 20 63 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e , code, pIndex->
1cd80 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 zName, pTab->zNa
1cd90 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 me, zDb) ){.
1cda0 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 goto exit_drop
1cdb0 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 _index;. }.
1cdc0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 }.#endif.. /* G
1cdd0 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
1cde0 72 65 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 remove the index
1cdf0 20 61 6e 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 and from the ma
1ce00 73 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 ster table */.
1ce10 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
1ce20 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 be(pParse);. if
1ce30 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( v ){. sqlit
1ce40 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 e3BeginWriteOper
1ce50 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c ation(pParse, 1,
1ce60 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 iDb);. sqlit
1ce70 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
1ce80 61 72 73 65 2c 0a 20 20 20 20 20 20 20 22 44 45 arse,. "DE
1ce90 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 LETE FROM %Q.%s
1cea0 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 20 41 4e WHERE name=%Q AN
1ceb0 44 20 74 79 70 65 3d 27 69 6e 64 65 78 27 22 2c D type='index'",
1cec0 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b . db->aDb[
1ced0 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 iDb].zName, SCHE
1cee0 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 MA_TABLE(iDb), p
1cef0 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 Index->zName.
1cf00 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 );. sqlite3C
1cf10 6c 65 61 72 53 74 61 74 54 61 62 6c 65 73 28 70 learStatTables(p
1cf20 50 61 72 73 65 2c 20 69 44 62 2c 20 22 69 64 78 Parse, iDb, "idx
1cf30 22 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 ", pIndex->zName
1cf40 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 );. sqlite3Ch
1cf50 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 angeCookie(pPars
1cf60 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 64 65 73 e, iDb);. des
1cf70 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 troyRootPage(pPa
1cf80 72 73 65 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 rse, pIndex->tnu
1cf90 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c m, iDb);. sql
1cfa0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
1cfb0 2c 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 , OP_DropIndex,
1cfc0 69 44 62 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 iDb, 0, 0, pInde
1cfd0 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 x->zName, 0);.
1cfe0 7d 0a 0a 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 }..exit_drop_ind
1cff0 65 78 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 ex:. sqlite3Src
1d000 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
1d010 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Name);.}../*.**
1d020 70 41 72 72 61 79 20 69 73 20 61 20 70 6f 69 6e pArray is a poin
1d030 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 ter to an array
1d040 6f 66 20 6f 62 6a 65 63 74 73 2e 20 45 61 63 68 of objects. Each
1d050 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a object in the.*
1d060 2a 20 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 * array is szEnt
1d070 72 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ry bytes in size
1d080 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75 . This routine u
1d090 73 65 73 20 73 71 6c 69 74 65 33 44 62 52 65 61 ses sqlite3DbRea
1d0a0 6c 6c 6f 63 28 29 0a 2a 2a 20 74 6f 20 65 78 74 lloc().** to ext
1d0b0 65 6e 64 20 74 68 65 20 61 72 72 61 79 20 73 6f end the array so
1d0c0 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 73 that there is s
1d0d0 70 61 63 65 20 66 6f 72 20 61 20 6e 65 77 20 6f pace for a new o
1d0e0 62 6a 65 63 74 20 61 74 20 74 68 65 20 65 6e 64 bject at the end
1d0f0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 ..**.** When thi
1d100 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
1d110 6c 6c 65 64 2c 20 2a 70 6e 45 6e 74 72 79 20 63 lled, *pnEntry c
1d120 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 75 72 72 ontains the curr
1d130 65 6e 74 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 ent size of.** t
1d140 68 65 20 61 72 72 61 79 20 28 69 6e 20 65 6e 74 he array (in ent
1d150 72 69 65 73 20 2d 20 73 6f 20 74 68 65 20 61 6c ries - so the al
1d160 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28 2a 70 location is ((*p
1d170 6e 45 6e 74 72 79 29 20 2a 20 73 7a 45 6e 74 72 nEntry) * szEntr
1d180 79 29 20 62 79 74 65 73 0a 2a 2a 20 69 6e 20 74 y) bytes.** in t
1d190 6f 74 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 otal)..**.** If
1d1a0 74 68 65 20 72 65 61 6c 6c 6f 63 28 29 20 69 73 the realloc() is
1d1b0 20 73 75 63 63 65 73 73 66 75 6c 20 28 69 2e 65 successful (i.e
1d1c0 2e 20 69 66 20 6e 6f 20 4f 4f 4d 20 63 6f 6e 64 . if no OOM cond
1d1d0 69 74 69 6f 6e 20 6f 63 63 75 72 73 29 2c 20 74 ition occurs), t
1d1e0 68 65 0a 2a 2a 20 73 70 61 63 65 20 61 6c 6c 6f he.** space allo
1d1f0 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 6e 65 cated for the ne
1d200 77 20 6f 62 6a 65 63 74 20 69 73 20 7a 65 72 6f w object is zero
1d210 65 64 2c 20 2a 70 6e 45 6e 74 72 79 20 75 70 64 ed, *pnEntry upd
1d220 61 74 65 64 20 74 6f 0a 2a 2a 20 72 65 66 6c 65 ated to.** refle
1d230 63 74 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 ct the new size
1d240 6f 66 20 74 68 65 20 61 72 72 61 79 20 61 6e 64 of the array and
1d250 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1d260 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e e new allocation
1d270 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 2a 70 .** returned. *p
1d280 49 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 Idx is set to th
1d290 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e e index of the n
1d2a0 65 77 20 61 72 72 61 79 20 65 6e 74 72 79 20 69 ew array entry i
1d2b0 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a n this case..**.
1d2c0 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 ** Otherwise, if
1d2d0 20 74 68 65 20 72 65 61 6c 6c 6f 63 28 29 20 66 the realloc() f
1d2e0 61 69 6c 73 2c 20 2a 70 49 64 78 20 69 73 20 73 ails, *pIdx is s
1d2f0 65 74 20 74 6f 20 2d 31 2c 20 2a 70 6e 45 6e 74 et to -1, *pnEnt
1d300 72 79 20 72 65 6d 61 69 6e 73 0a 2a 2a 20 75 6e ry remains.** un
1d310 63 68 61 6e 67 65 64 20 61 6e 64 20 61 20 63 6f changed and a co
1d320 70 79 20 6f 66 20 70 41 72 72 61 79 20 72 65 74 py of pArray ret
1d330 75 72 6e 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 2a urned..*/.void *
1d340 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f sqlite3ArrayAllo
1d350 63 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 20 cate(. sqlite3
1d360 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6e *db, /* Con
1d370 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 nection to notif
1d380 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c y of malloc fail
1d390 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a ures */. void *
1d3a0 70 41 72 72 61 79 2c 20 20 20 20 20 2f 2a 20 41 pArray, /* A
1d3b0 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e rray of objects.
1d3c0 20 20 4d 69 67 68 74 20 62 65 20 72 65 61 6c 6c Might be reall
1d3d0 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 ocated */. int
1d3e0 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 20 2f 2a szEntry, /*
1d3f0 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 6f 62 Size of each ob
1d400 6a 65 63 74 20 69 6e 20 74 68 65 20 61 72 72 61 ject in the arra
1d410 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 6e y */. int *pnEn
1d420 74 72 79 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 try, /* Numb
1d430 65 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 63 75 er of objects cu
1d440 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a rrently in use *
1d450 2f 0a 20 20 69 6e 74 20 2a 70 49 64 78 20 20 20 /. int *pIdx
1d460 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
1d470 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 6e 65 he index of a ne
1d480 77 20 73 6c 6f 74 20 68 65 72 65 20 2a 2f 0a 29 w slot here */.)
1d490 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 {. char *z;. i
1d4a0 6e 74 20 6e 20 3d 20 2a 70 6e 45 6e 74 72 79 3b nt n = *pnEntry;
1d4b0 0a 20 20 69 66 28 20 28 6e 20 26 20 28 6e 2d 31 . if( (n & (n-1
1d4c0 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 ))==0 ){. int
1d4d0 20 73 7a 20 3d 20 28 6e 3d 3d 30 29 20 3f 20 31 sz = (n==0) ? 1
1d4e0 20 3a 20 32 2a 6e 3b 0a 20 20 20 20 76 6f 69 64 : 2*n;. void
1d4f0 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 *pNew = sqlite3
1d500 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 41 DbRealloc(db, pA
1d510 72 72 61 79 2c 20 73 7a 2a 73 7a 45 6e 74 72 79 rray, sz*szEntry
1d520 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d );. if( pNew=
1d530 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 49 64 =0 ){. *pId
1d540 78 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 x = -1;. re
1d550 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 20 20 20 turn pArray;.
1d560 20 7d 0a 20 20 20 20 70 41 72 72 61 79 20 3d 20 }. pArray =
1d570 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 pNew;. }. z =
1d580 28 63 68 61 72 2a 29 70 41 72 72 61 79 3b 0a 20 (char*)pArray;.
1d590 20 6d 65 6d 73 65 74 28 26 7a 5b 6e 20 2a 20 73 memset(&z[n * s
1d5a0 7a 45 6e 74 72 79 5d 2c 20 30 2c 20 73 7a 45 6e zEntry], 0, szEn
1d5b0 74 72 79 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 try);. *pIdx =
1d5c0 6e 3b 0a 20 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b n;. ++*pnEntry;
1d5d0 0a 20 20 72 65 74 75 72 6e 20 70 41 72 72 61 79 . return pArray
1d5e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e ;.}../*.** Appen
1d5f0 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 d a new element
1d600 74 6f 20 74 68 65 20 67 69 76 65 6e 20 49 64 4c to the given IdL
1d610 69 73 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e ist. Create a n
1d620 65 77 20 49 64 4c 69 73 74 20 69 66 0a 2a 2a 20 ew IdList if.**
1d630 6e 65 65 64 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 need be..**.** A
1d640 20 6e 65 77 20 49 64 4c 69 73 74 20 69 73 20 72 new IdList is r
1d650 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c eturned, or NULL
1d660 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 if malloc() fai
1d670 6c 73 2e 0a 2a 2f 0a 49 64 4c 69 73 74 20 2a 73 ls..*/.IdList *s
1d680 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 qlite3IdListAppe
1d690 6e 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 nd(sqlite3 *db,
1d6a0 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 IdList *pList, T
1d6b0 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 oken *pToken){.
1d6c0 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c int i;. if( pL
1d6d0 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c ist==0 ){. pL
1d6e0 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d ist = sqlite3DbM
1d6f0 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
1d700 7a 65 6f 66 28 49 64 4c 69 73 74 29 20 29 3b 0a zeof(IdList) );.
1d710 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 if( pList==0
1d720 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d ) return 0;. }
1d730 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 73 71 . pList->a = sq
1d740 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 lite3ArrayAlloca
1d750 74 65 28 0a 20 20 20 20 20 20 64 62 2c 0a 20 20 te(. db,.
1d760 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c 0a 20 20 pList->a,.
1d770 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 69 73 74 sizeof(pList
1d780 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 20 20 26 ->a[0]),. &
1d790 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a 20 20 20 20 pList->nId,.
1d7a0 20 20 26 69 0a 20 20 29 3b 0a 20 20 69 66 28 20 &i. );. if(
1d7b0 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 i<0 ){. sqlit
1d7c0 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 e3IdListDelete(d
1d7d0 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 b, pList);. r
1d7e0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 eturn 0;. }. p
1d7f0 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 List->a[i].zName
1d800 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
1d810 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b omToken(db, pTok
1d820 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c en);. return pL
1d830 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 ist;.}../*.** De
1d840 6c 65 74 65 20 61 6e 20 49 64 4c 69 73 74 2e 0a lete an IdList..
1d850 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 49 */.void sqlite3I
1d860 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 dListDelete(sqli
1d870 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 te3 *db, IdList
1d880 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 *pList){. int i
1d890 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 ;. if( pList==0
1d8a0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 ) return;. for
1d8b0 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e (i=0; i<pList->n
1d8c0 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 Id; i++){. sq
1d8d0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1d8e0 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d pList->a[i].zNam
1d8f0 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 e);. }. sqlite
1d900 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 3DbFree(db, pLis
1d910 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 t->a);. sqlite3
1d920 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 DbFree(db, pList
1d930 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
1d940 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 rn the index in
1d950 70 4c 69 73 74 20 6f 66 20 74 68 65 20 69 64 65 pList of the ide
1d960 6e 74 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 ntifier named zI
1d970 64 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a d. Return -1.**
1d980 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a if not found..*
1d990 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c /.int sqlite3IdL
1d9a0 69 73 74 49 6e 64 65 78 28 49 64 4c 69 73 74 20 istIndex(IdList
1d9b0 2a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 *pList, const ch
1d9c0 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e ar *zName){. in
1d9d0 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 t i;. if( pList
1d9e0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b ==0 ) return -1;
1d9f0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c . for(i=0; i<pL
1da00 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a ist->nId; i++){.
1da10 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1da20 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b trICmp(pList->a[
1da30 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 i].zName, zName)
1da40 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a ==0 ) return i;.
1da50 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b }. return -1;
1da60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 61 6e 64 .}../*.** Expand
1da70 20 74 68 65 20 73 70 61 63 65 20 61 6c 6c 6f 63 the space alloc
1da80 61 74 65 64 20 66 6f 72 20 74 68 65 20 67 69 76 ated for the giv
1da90 65 6e 20 53 72 63 4c 69 73 74 20 6f 62 6a 65 63 en SrcList objec
1daa0 74 20 62 79 0a 2a 2a 20 63 72 65 61 74 69 6e 67 t by.** creating
1dab0 20 6e 45 78 74 72 61 20 6e 65 77 20 73 6c 6f 74 nExtra new slot
1dac0 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 s beginning at i
1dad0 53 74 61 72 74 2e 20 20 69 53 74 61 72 74 20 69 Start. iStart i
1dae0 73 20 7a 65 72 6f 20 62 61 73 65 64 2e 0a 2a 2a s zero based..**
1daf0 20 4e 65 77 20 73 6c 6f 74 73 20 61 72 65 20 7a New slots are z
1db00 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 eroed..**.** For
1db10 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 example, suppos
1db20 65 20 61 20 53 72 63 4c 69 73 74 20 69 6e 69 74 e a SrcList init
1db30 69 61 6c 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 ially contains t
1db40 77 6f 20 65 6e 74 72 69 65 73 3a 20 41 2c 42 2e wo entries: A,B.
1db50 0a 2a 2a 20 54 6f 20 61 70 70 65 6e 64 20 33 20 .** To append 3
1db60 6e 65 77 20 65 6e 74 72 69 65 73 20 6f 6e 74 6f new entries onto
1db70 20 74 68 65 20 65 6e 64 2c 20 64 6f 20 74 68 69 the end, do thi
1db80 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 s:.**.** sqli
1db90 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 te3SrcListEnlarg
1dba0 65 28 64 62 2c 20 70 53 72 63 6c 69 73 74 2c 20 e(db, pSrclist,
1dbb0 33 2c 20 32 29 3b 0a 2a 2a 0a 2a 2a 20 41 66 74 3, 2);.**.** Aft
1dbc0 65 72 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 er the call abov
1dbd0 65 20 69 74 20 77 6f 75 6c 64 20 63 6f 6e 74 61 e it would conta
1dbe0 69 6e 3a 20 20 41 2c 20 42 2c 20 6e 69 6c 2c 20 in: A, B, nil,
1dbf0 6e 69 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20 49 66 20 nil, nil..** If
1dc00 74 68 65 20 69 53 74 61 72 74 20 61 72 67 75 6d the iStart argum
1dc10 65 6e 74 20 68 61 64 20 62 65 65 6e 20 31 20 69 ent had been 1 i
1dc20 6e 73 74 65 61 64 20 6f 66 20 32 2c 20 74 68 65 nstead of 2, the
1dc30 6e 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 n the result.**
1dc40 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 3a would have been:
1dc50 20 20 41 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e A, nil, nil, n
1dc60 69 6c 2c 20 42 2e 20 20 54 6f 20 70 72 65 70 65 il, B. To prepe
1dc70 6e 64 20 74 68 65 20 6e 65 77 20 73 6c 6f 74 73 nd the new slots
1dc80 2c 0a 2a 2a 20 74 68 65 20 69 53 74 61 72 74 20 ,.** the iStart
1dc90 76 61 6c 75 65 20 77 6f 75 6c 64 20 62 65 20 30 value would be 0
1dca0 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 74 68 . The result th
1dcb0 65 6e 20 77 6f 75 6c 64 0a 2a 2a 20 62 65 3a 20 en would.** be:
1dcc0 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 nil, nil, nil, A
1dcd0 2c 20 42 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 , B..**.** If a
1dce0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1dcf0 6e 20 66 61 69 6c 73 20 74 68 65 20 53 72 63 4c n fails the SrcL
1dd00 69 73 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ist is unchanged
1dd10 2e 20 20 54 68 65 0a 2a 2a 20 64 62 2d 3e 6d 61 . The.** db->ma
1dd20 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
1dd30 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 74 will be set to t
1dd40 72 75 65 2e 0a 2a 2f 0a 53 72 63 4c 69 73 74 20 rue..*/.SrcList
1dd50 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 *sqlite3SrcListE
1dd60 6e 6c 61 72 67 65 28 0a 20 20 73 71 6c 69 74 65 nlarge(. sqlite
1dd70 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 3 *db, /*
1dd80 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 Database connect
1dd90 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 ion to notify of
1dda0 20 4f 4f 4d 20 65 72 72 6f 72 73 20 2a 2f 0a 20 OOM errors */.
1ddb0 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 SrcList *pSrc,
1ddc0 20 20 20 20 2f 2a 20 54 68 65 20 53 72 63 4c 69 /* The SrcLi
1ddd0 73 74 20 74 6f 20 62 65 20 65 6e 6c 61 72 67 65 st to be enlarge
1dde0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 d */. int nExtr
1ddf0 61 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d a, /* Num
1de00 62 65 72 20 6f 66 20 6e 65 77 20 73 6c 6f 74 73 ber of new slots
1de10 20 74 6f 20 61 64 64 20 74 6f 20 70 53 72 63 2d to add to pSrc-
1de20 3e 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 53 >a[] */. int iS
1de30 74 61 72 74 20 20 20 20 20 20 20 20 20 2f 2a 20 tart /*
1de40 49 6e 64 65 78 20 69 6e 20 70 53 72 63 2d 3e 61 Index in pSrc->a
1de50 5b 5d 20 6f 66 20 66 69 72 73 74 20 6e 65 77 20 [] of first new
1de60 73 6c 6f 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 slot */.){. int
1de70 20 69 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 i;.. /* Sanity
1de80 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 63 61 6c checking on cal
1de90 6c 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 ling parameters
1dea0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 */. assert( iSt
1deb0 61 72 74 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 art>=0 );. asse
1dec0 72 74 28 20 6e 45 78 74 72 61 3e 3d 31 20 29 3b rt( nExtra>=1 );
1ded0 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21 . assert( pSrc!
1dee0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1def0 69 53 74 61 72 74 3c 3d 70 53 72 63 2d 3e 6e 53 iStart<=pSrc->nS
1df00 72 63 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f rc );.. /* Allo
1df10 63 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 cate additional
1df20 73 70 61 63 65 20 69 66 20 6e 65 65 64 65 64 20 space if needed
1df30 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 29 70 53 */. if( (u32)pS
1df40 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3e rc->nSrc+nExtra>
1df50 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a pSrc->nAlloc ){.
1df60 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 SrcList *pNe
1df70 77 3b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f w;. int nAllo
1df80 63 20 3d 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e c = pSrc->nSrc+n
1df90 45 78 74 72 61 3b 0a 20 20 20 20 69 6e 74 20 6e Extra;. int n
1dfa0 47 6f 74 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 Got;. pNew =
1dfb0 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
1dfc0 28 64 62 2c 20 70 53 72 63 2c 0a 20 20 20 20 20 (db, pSrc,.
1dfd0 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 sizeof
1dfe0 28 2a 70 53 72 63 29 20 2b 20 28 6e 41 6c 6c 6f (*pSrc) + (nAllo
1dff0 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 53 72 63 c-1)*sizeof(pSrc
1e000 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 ->a[0]) );. i
1e010 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 f( pNew==0 ){.
1e020 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1e030 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1e040 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 53 72 return pSr
1e050 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 72 c;. }. pSr
1e060 63 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 6e 47 c = pNew;. nG
1e070 6f 74 20 3d 20 28 73 71 6c 69 74 65 33 44 62 4d ot = (sqlite3DbM
1e080 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 4e allocSize(db, pN
1e090 65 77 29 20 2d 20 73 69 7a 65 6f 66 28 2a 70 53 ew) - sizeof(*pS
1e0a0 72 63 29 29 2f 73 69 7a 65 6f 66 28 70 53 72 63 rc))/sizeof(pSrc
1e0b0 2d 3e 61 5b 30 5d 29 2b 31 3b 0a 20 20 20 20 70 ->a[0])+1;. p
1e0c0 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 47 Src->nAlloc = nG
1e0d0 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f ot;. }.. /* Mo
1e0e0 76 65 20 65 78 69 73 74 69 6e 67 20 73 6c 6f 74 ve existing slot
1e0f0 73 20 74 68 61 74 20 63 6f 6d 65 20 61 66 74 65 s that come afte
1e100 72 20 74 68 65 20 6e 65 77 6c 79 20 69 6e 73 65 r the newly inse
1e110 72 74 65 64 20 73 6c 6f 74 73 0a 20 20 2a 2a 20 rted slots. **
1e120 6f 75 74 20 6f 66 20 74 68 65 20 77 61 79 20 2a out of the way *
1e130 2f 0a 20 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e /. for(i=pSrc->
1e140 6e 53 72 63 2d 31 3b 20 69 3e 3d 69 53 74 61 72 nSrc-1; i>=iStar
1e150 74 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70 53 72 t; i--){. pSr
1e160 63 2d 3e 61 5b 69 2b 6e 45 78 74 72 61 5d 20 3d c->a[i+nExtra] =
1e170 20 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 7d pSrc->a[i];. }
1e180 0a 20 20 70 53 72 63 2d 3e 6e 53 72 63 20 2b 3d . pSrc->nSrc +=
1e190 20 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a 20 5a nExtra;.. /* Z
1e1a0 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20 61 6c ero the newly al
1e1b0 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 2a 2f located slots */
1e1c0 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72 63 2d . memset(&pSrc-
1e1d0 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c 20 73 >a[iStart], 0, s
1e1e0 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d izeof(pSrc->a[0]
1e1f0 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66 6f 72 )*nExtra);. for
1e200 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69 53 74 (i=iStart; i<iSt
1e210 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b 2b 29 art+nExtra; i++)
1e220 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 5d {. pSrc->a[i]
1e230 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 .iCursor = -1;.
1e240 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 }.. /* Return
1e250 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1e260 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c 69 73 enlarged SrcLis
1e270 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 53 t */. return pS
1e280 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70 rc;.}.../*.** Ap
1e290 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 6c 65 pend a new table
1e2a0 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 69 76 name to the giv
1e2b0 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 72 65 en SrcList. Cre
1e2c0 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c 69 73 ate a new SrcLis
1e2d0 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e t if.** need be.
1e2e0 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73 A new entry is
1e2f0 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 created in the
1e300 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 66 20 SrcList even if
1e310 70 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c 2e 0a pTable is NULL..
1e320 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73 74 20 **.** A SrcList
1e330 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 is returned, or
1e340 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 NULL if there is
1e350 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 an OOM error.
1e360 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 The returned.**
1e370 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 62 65 SrcList might be
1e380 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
1e390 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 77 61 SrcList that wa
1e3a0 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20 6d 69 s input or it mi
1e3b0 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65 77 20 ght be.** a new
1e3c0 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f 4d 20 one. If an OOM
1e3d0 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63 75 72 error does occur
1e3e0 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f s, then the prio
1e3f0 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69 73 74 r value of pList
1e400 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 70 75 .** that is inpu
1e410 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e t to this routin
1e420 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c e is automatical
1e430 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 ly freed..**.**
1e440 49 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20 If pDatabase is
1e450 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 not null, it mea
1e460 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c ns that the tabl
1e470 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61 e has an optiona
1e480 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6e 61 l.** database na
1e490 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 6b 65 me prefix. Like
1e4a0 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 61 73 this: "databas
1e4b0 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 20 70 e.table". The p
1e4c0 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69 6e Database.** poin
1e4d0 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 ts to the table
1e4e0 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70 54 61 name and the pTa
1e4f0 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ble points to th
1e500 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e e database name.
1e510 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73 74 2e .** The SrcList.
1e520 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64 20 a[].zName field
1e530 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 is filled with t
1e540 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 68 he table name wh
1e550 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d ich might.** com
1e560 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20 28 69 e from pTable (i
1e570 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20 4e f pDatabase is N
1e580 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44 61 ULL) or from pDa
1e590 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72 63 tabase. .** Src
1e5a0 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 List.a[].zDataba
1e5b0 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 se is filled wit
1e5c0 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e h the database n
1e5d0 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 2c ame from pTable,
1e5e0 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c 4c .** or with NULL
1e5f0 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73 65 20 if no database
1e600 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a is specified..**
1e610 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 .** In other wor
1e620 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b 65 ds, if call like
1e630 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
1e640 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c sqlite3SrcL
1e650 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c istAppend(D,A,B,
1e660 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42 0);.**.** Then B
1e670 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 is a table name
1e680 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 and the databas
1e690 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70 65 63 e name is unspec
1e6a0 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c 6c 65 ified. If calle
1e6b0 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a d.** like this:.
1e6c0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 **.** sq
1e6d0 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 lite3SrcListAppe
1e6e0 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a nd(D,A,B,C);.**.
1e6f0 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 68 65 ** Then C is the
1e700 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 table name and
1e710 42 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 B is the databas
1e720 65 20 6e 61 6d 65 2e 20 20 49 66 20 43 20 69 73 e name. If C is
1e730 20 64 65 66 69 6e 65 64 0a 2a 2a 20 74 68 65 6e defined.** then
1e740 20 73 6f 20 69 73 20 42 2e 20 20 49 6e 20 6f 74 so is B. In ot
1e750 68 65 72 20 77 6f 72 64 73 2c 20 77 65 20 6e 65 her words, we ne
1e760 76 65 72 20 68 61 76 65 20 61 20 63 61 73 65 20 ver have a case
1e770 77 68 65 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 where:.**.**
1e780 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c sqlite3SrcL
1e790 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 30 2c istAppend(D,A,0,
1e7a0 43 29 3b 0a 2a 2a 0a 2a 2a 20 42 6f 74 68 20 70 C);.**.** Both p
1e7b0 54 61 62 6c 65 20 61 6e 64 20 70 44 61 74 61 62 Table and pDatab
1e7c0 61 73 65 20 61 72 65 20 61 73 73 75 6d 65 64 20 ase are assumed
1e7d0 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 54 to be quoted. T
1e7e0 68 65 79 20 61 72 65 20 64 65 71 75 6f 74 65 64 hey are dequoted
1e7f0 0a 2a 2a 20 62 65 66 6f 72 65 20 62 65 69 6e 67 .** before being
1e800 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 72 added to the Sr
1e810 63 4c 69 73 74 2e 0a 2a 2f 0a 53 72 63 4c 69 73 cList..*/.SrcLis
1e820 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 t *sqlite3SrcLis
1e830 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c 69 74 tAppend(. sqlit
1e840 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f e3 *db, /
1e850 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 * Connection to
1e860 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 notify of malloc
1e870 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 53 failures */. S
1e880 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 rcList *pList,
1e890 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f 20 /* Append to
1e8a0 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20 4e 55 this SrcList. NU
1e8b0 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 LL creates a new
1e8c0 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54 6f SrcList */. To
1e8d0 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 ken *pTable,
1e8e0 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61 70 /* Table to ap
1e8f0 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 pend */. Token
1e900 2a 70 44 61 74 61 62 61 73 65 20 20 20 20 2f 2a *pDatabase /*
1e910 20 44 61 74 61 62 61 73 65 20 6f 66 20 74 68 65 Database of the
1e920 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 table */.){. s
1e930 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
1e940 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 73 73 em *pItem;. ass
1e950 65 72 74 28 20 70 44 61 74 61 62 61 73 65 3d 3d ert( pDatabase==
1e960 30 20 7c 7c 20 70 54 61 62 6c 65 21 3d 30 20 29 0 || pTable!=0 )
1e970 3b 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76 ; /* Cannot hav
1e980 65 20 43 20 77 69 74 68 6f 75 74 20 42 20 2a 2f e C without B */
1e990 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 . if( pList==0
1e9a0 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 ){. pList = s
1e9b0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
1e9c0 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 53 72 ro(db, sizeof(Sr
1e9d0 63 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 cList) );. if
1e9e0 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 ( pList==0 ) ret
1e9f0 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 74 urn 0;. pList
1ea00 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 ->nAlloc = 1;.
1ea10 7d 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 }. pList = sqli
1ea20 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 te3SrcListEnlarg
1ea30 65 28 64 62 2c 20 70 4c 69 73 74 2c 20 31 2c 20 e(db, pList, 1,
1ea40 70 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 0a 20 20 pList->nSrc);.
1ea50 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1ea60 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 iled ){. sqli
1ea70 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 te3SrcListDelete
1ea80 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 (db, pList);.
1ea90 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
1eaa0 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d pItem = &pList-
1eab0 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 >a[pList->nSrc-1
1eac0 5d 3b 0a 20 20 69 66 28 20 70 44 61 74 61 62 61 ];. if( pDataba
1ead0 73 65 20 26 26 20 70 44 61 74 61 62 61 73 65 2d se && pDatabase-
1eae0 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 61 >z==0 ){. pDa
1eaf0 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 7d 0a tabase = 0;. }.
1eb00 20 20 69 66 28 20 70 44 61 74 61 62 61 73 65 20 if( pDatabase
1eb10 29 7b 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 ){. Token *pT
1eb20 65 6d 70 20 3d 20 70 44 61 74 61 62 61 73 65 3b emp = pDatabase;
1eb30 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20 3d . pDatabase =
1eb40 20 70 54 61 62 6c 65 3b 0a 20 20 20 20 70 54 61 pTable;. pTa
1eb50 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0a 20 20 7d ble = pTemp;. }
1eb60 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 . pItem->zName
1eb70 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
1eb80 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c mToken(db, pTabl
1eb90 65 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a 44 61 e);. pItem->zDa
1eba0 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 tabase = sqlite3
1ebb0 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 NameFromToken(db
1ebc0 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20 20 , pDatabase);.
1ebd0 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a return pList;.}.
1ebe0 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 56 64 ./*.** Assign Vd
1ebf0 62 65 43 75 72 73 6f 72 20 69 6e 64 65 78 20 6e beCursor index n
1ec00 75 6d 62 65 72 73 20 74 6f 20 61 6c 6c 20 74 61 umbers to all ta
1ec10 62 6c 65 73 20 69 6e 20 61 20 53 72 63 4c 69 73 bles in a SrcLis
1ec20 74 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 t.*/.void sqlite
1ec30 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 3SrcListAssignCu
1ec40 72 73 6f 72 73 28 50 61 72 73 65 20 2a 70 50 61 rsors(Parse *pPa
1ec50 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c rse, SrcList *pL
1ec60 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ist){. int i;.
1ec70 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
1ec80 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 item *pItem;. a
1ec90 73 73 65 72 74 28 70 4c 69 73 74 20 7c 7c 20 70 ssert(pList || p
1eca0 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
1ecb0 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 cFailed );. if(
1ecc0 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f pList ){. fo
1ecd0 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 r(i=0, pItem=pLi
1ece0 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e st->a; i<pList->
1ecf0 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d nSrc; i++, pItem
1ed00 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 ++){. if( p
1ed10 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3e 3d 30 Item->iCursor>=0
1ed20 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
1ed30 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d pItem->iCursor =
1ed40 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b pParse->nTab++;
1ed50 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d . if( pItem
1ed60 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 ->pSelect ){.
1ed70 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c sqlite3SrcL
1ed80 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 istAssignCursors
1ed90 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e (pParse, pItem->
1eda0 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 29 3b 0a pSelect->pSrc);.
1edb0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1edc0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 }.}../*.** Delet
1edd0 65 20 61 6e 20 65 6e 74 69 72 65 20 53 72 63 4c e an entire SrcL
1ede0 69 73 74 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c ist including al
1edf0 6c 20 69 74 73 20 73 75 62 73 74 72 75 63 74 75 l its substructu
1ee00 72 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 re..*/.void sqli
1ee10 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 te3SrcListDelete
1ee20 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 (sqlite3 *db, Sr
1ee30 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 cList *pList){.
1ee40 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
1ee50 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
1ee60 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 Item;. if( pLis
1ee70 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 t==0 ) return;.
1ee80 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 for(pItem=pList
1ee90 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 ->a, i=0; i<pLis
1eea0 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 t->nSrc; i++, pI
1eeb0 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 tem++){. sqli
1eec0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 te3DbFree(db, pI
1eed0 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b tem->zDatabase);
1eee0 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1eef0 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e ee(db, pItem->zN
1ef00 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ame);. sqlite
1ef10 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 3DbFree(db, pIte
1ef20 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 m->zAlias);.
1ef30 69 66 28 20 70 49 74 65 6d 2d 3e 66 67 2e 69 73 if( pItem->fg.is
1ef40 49 6e 64 65 78 65 64 42 79 20 29 20 73 71 6c 69 IndexedBy ) sqli
1ef50 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 te3DbFree(db, pI
1ef60 74 65 6d 2d 3e 75 31 2e 7a 49 6e 64 65 78 65 64 tem->u1.zIndexed
1ef70 42 79 29 3b 0a 20 20 20 20 69 66 28 20 70 49 74 By);. if( pIt
1ef80 65 6d 2d 3e 66 67 2e 69 73 54 61 62 46 75 6e 63 em->fg.isTabFunc
1ef90 20 29 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 ) sqlite3ExprLi
1efa0 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 stDelete(db, pIt
1efb0 65 6d 2d 3e 75 31 2e 70 46 75 6e 63 41 72 67 29 em->u1.pFuncArg)
1efc0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c ;. sqlite3Del
1efd0 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 49 74 eteTable(db, pIt
1efe0 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73 em->pTab);. s
1eff0 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
1f000 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 te(db, pItem->pS
1f010 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 elect);. sqli
1f020 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1f030 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 , pItem->pOn);.
1f040 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 sqlite3IdList
1f050 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d Delete(db, pItem
1f060 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20 ->pUsing);. }.
1f070 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1f080 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a b, pList);.}../*
1f090 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1f0a0 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
1f0b0 65 20 70 61 72 73 65 72 20 74 6f 20 61 64 64 20 e parser to add
1f0c0 61 20 6e 65 77 20 74 65 72 6d 20 74 6f 20 74 68 a new term to th
1f0d0 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72 e.** end of a gr
1f0e0 6f 77 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73 owing FROM claus
1f0f0 65 2e 20 20 54 68 65 20 22 70 22 20 70 61 72 61 e. The "p" para
1f100 6d 65 74 65 72 20 69 73 20 74 68 65 20 70 61 72 meter is the par
1f110 74 20 6f 66 0a 2a 2a 20 74 68 65 20 46 52 4f 4d t of.** the FROM
1f120 20 63 6c 61 75 73 65 20 74 68 61 74 20 68 61 73 clause that has
1f130 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f already been co
1f140 6e 73 74 72 75 63 74 65 64 2e 20 20 22 70 22 20 nstructed. "p"
1f150 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 74 68 is NULL.** if th
1f160 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 is is the first
1f170 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d term of the FROM
1f180 20 63 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65 clause. pTable
1f190 20 61 6e 64 20 70 44 61 74 61 62 61 73 65 0a 2a and pDatabase.*
1f1a0 2a 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f * are the name o
1f1b0 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 f the table and
1f1c0 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 20 69 database named i
1f1d0 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
1f1e0 65 20 74 65 72 6d 2e 0a 2a 2a 20 70 44 61 74 61 e term..** pData
1f1f0 62 61 73 65 20 69 73 20 4e 55 4c 4c 20 69 66 20 base is NULL if
1f200 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
1f210 65 20 71 75 61 6c 69 66 69 65 72 20 69 73 20 6d e qualifier is m
1f220 69 73 73 69 6e 67 20 2d 20 74 68 65 0a 2a 2a 20 issing - the.**
1f230 75 73 75 61 6c 20 63 61 73 65 2e 20 20 49 66 20 usual case. If
1f240 74 68 65 20 74 65 72 6d 20 68 61 73 20 61 6e 20 the term has an
1f250 61 6c 69 61 73 2c 20 74 68 65 6e 20 70 41 6c 69 alias, then pAli
1f260 61 73 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 as points to the
1f270 0a 2a 2a 20 61 6c 69 61 73 20 74 6f 6b 65 6e 2e .** alias token.
1f280 20 20 49 66 20 74 68 65 20 74 65 72 6d 20 69 73 If the term is
1f290 20 61 20 73 75 62 71 75 65 72 79 2c 20 74 68 65 a subquery, the
1f2a0 6e 20 70 53 75 62 71 75 65 72 79 20 69 73 20 74 n pSubquery is t
1f2b0 68 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 he.** SELECT sta
1f2c0 74 65 6d 65 6e 74 20 74 68 61 74 20 74 68 65 20 tement that the
1f2d0 73 75 62 71 75 65 72 79 20 65 6e 63 6f 64 65 73 subquery encodes
1f2e0 2e 20 20 54 68 65 20 70 54 61 62 6c 65 20 61 6e . The pTable an
1f2f0 64 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 70 d.** pDatabase p
1f300 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 4e 55 arameters are NU
1f310 4c 4c 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 LL for subquerie
1f320 73 2e 20 20 54 68 65 20 70 4f 6e 20 61 6e 64 20 s. The pOn and
1f330 70 55 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 pUsing.** parame
1f340 74 65 72 73 20 61 72 65 20 74 68 65 20 63 6f 6e ters are the con
1f350 74 65 6e 74 20 6f 66 20 74 68 65 20 4f 4e 20 61 tent of the ON a
1f360 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 nd USING clauses
1f370 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ..**.** Return a
1f380 20 6e 65 77 20 53 72 63 4c 69 73 74 20 77 68 69 new SrcList whi
1f390 63 68 20 65 6e 63 6f 64 65 73 20 69 73 20 74 68 ch encodes is th
1f3a0 65 20 46 52 4f 4d 20 77 69 74 68 20 74 68 65 20 e FROM with the
1f3b0 6e 65 77 0a 2a 2a 20 74 65 72 6d 20 61 64 64 65 new.** term adde
1f3c0 64 2e 0a 2a 2f 0a 53 72 63 4c 69 73 74 20 2a 73 d..*/.SrcList *s
1f3d0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 qlite3SrcListApp
1f3e0 65 6e 64 46 72 6f 6d 54 65 72 6d 28 0a 20 20 50 endFromTerm(. P
1f3f0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
1f400 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
1f410 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 g context */. S
1f420 72 63 4c 69 73 74 20 2a 70 2c 20 20 20 20 20 20 rcList *p,
1f430 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 /* The le
1f440 66 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 46 ft part of the F
1f450 52 4f 4d 20 63 6c 61 75 73 65 20 61 6c 72 65 61 ROM clause alrea
1f460 64 79 20 73 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b dy seen */. Tok
1f470 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 en *pTable,
1f480 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1f490 74 68 65 20 74 61 62 6c 65 20 74 6f 20 61 64 64 the table to add
1f4a0 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 to the FROM cla
1f4b0 75 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a use */. Token *
1f4c0 70 44 61 74 61 62 61 73 65 2c 20 20 20 20 20 20 pDatabase,
1f4d0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1f4e0 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
1f4f0 69 6e 67 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 ing pTable */.
1f500 54 6f 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20 20 Token *pAlias,
1f510 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
1f520 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f ight-hand side o
1f530 66 20 74 68 65 20 41 53 20 73 75 62 65 78 70 72 f the AS subexpr
1f540 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 ession */. Sele
1f550 63 74 20 2a 70 53 75 62 71 75 65 72 79 2c 20 20 ct *pSubquery,
1f560 20 20 20 20 2f 2a 20 41 20 73 75 62 71 75 65 72 /* A subquer
1f570 79 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 y used in place
1f580 6f 66 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 of a table name
1f590 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c 20 */. Expr *pOn,
1f5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f5b0 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 The ON clause of
1f5c0 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c a join */. IdL
1f5d0 69 73 74 20 2a 70 55 73 69 6e 67 20 20 20 20 20 ist *pUsing
1f5e0 20 20 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e /* The USIN
1f5f0 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f G clause of a jo
1f600 69 6e 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 in */.){. struc
1f610 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a t SrcList_item *
1f620 70 49 74 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 pItem;. sqlite3
1f630 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
1f640 62 3b 0a 20 20 69 66 28 20 21 70 20 26 26 20 28 b;. if( !p && (
1f650 70 4f 6e 20 7c 7c 20 70 55 73 69 6e 67 29 20 29 pOn || pUsing) )
1f660 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
1f670 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 orMsg(pParse, "a
1f680 20 4a 4f 49 4e 20 63 6c 61 75 73 65 20 69 73 20 JOIN clause is
1f690 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 required before
1f6a0 25 73 22 2c 20 0a 20 20 20 20 20 20 28 70 4f 6e %s", . (pOn
1f6b0 20 3f 20 22 4f 4e 22 20 3a 20 22 55 53 49 4e 47 ? "ON" : "USING
1f6c0 22 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 67 6f "). );. go
1f6d0 74 6f 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f 65 to append_from_e
1f6e0 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 rror;. }. p =
1f6f0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 sqlite3SrcListAp
1f700 70 65 6e 64 28 64 62 2c 20 70 2c 20 70 54 61 62 pend(db, p, pTab
1f710 6c 65 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a le, pDatabase);.
1f720 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 if( p==0 || NE
1f730 56 45 52 28 70 2d 3e 6e 53 72 63 3d 3d 30 29 20 VER(p->nSrc==0)
1f740 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 65 ){. goto appe
1f750 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a 20 nd_from_error;.
1f760 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26 70 2d }. pItem = &p-
1f770 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 >a[p->nSrc-1];.
1f780 20 61 73 73 65 72 74 28 20 70 41 6c 69 61 73 21 assert( pAlias!
1f790 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 41 6c 69 =0 );. if( pAli
1f7a0 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 49 74 as->n ){. pIt
1f7b0 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c em->zAlias = sql
1f7c0 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
1f7d0 6e 28 64 62 2c 20 70 41 6c 69 61 73 29 3b 0a 20 n(db, pAlias);.
1f7e0 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70 53 65 6c }. pItem->pSel
1f7f0 65 63 74 20 3d 20 70 53 75 62 71 75 65 72 79 3b ect = pSubquery;
1f800 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 . pItem->pOn =
1f810 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 55 pOn;. pItem->pU
1f820 73 69 6e 67 20 3d 20 70 55 73 69 6e 67 3b 0a 20 sing = pUsing;.
1f830 20 72 65 74 75 72 6e 20 70 3b 0a 0a 20 61 70 70 return p;.. app
1f840 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3a 0a end_from_error:.
1f850 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 assert( p==0 )
1f860 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 ;. sqlite3ExprD
1f870 65 6c 65 74 65 28 64 62 2c 20 70 4f 6e 29 3b 0a elete(db, pOn);.
1f880 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 sqlite3IdListD
1f890 65 6c 65 74 65 28 64 62 2c 20 70 55 73 69 6e 67 elete(db, pUsing
1f8a0 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 );. sqlite3Sele
1f8b0 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 75 ctDelete(db, pSu
1f8c0 62 71 75 65 72 79 29 3b 0a 20 20 72 65 74 75 72 bquery);. retur
1f8d0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 n 0;.}../*.** Ad
1f8e0 64 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 d an INDEXED BY
1f8f0 6f 72 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 or NOT INDEXED c
1f900 6c 61 75 73 65 20 74 6f 20 74 68 65 20 6d 6f 73 lause to the mos
1f910 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 t recently added
1f920 20 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 6f 66 20 .** element of
1f930 74 68 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20 the source-list
1f940 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
1f950 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a cond argument..*
1f960 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 /.void sqlite3Sr
1f970 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 cListIndexedBy(P
1f980 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 arse *pParse, Sr
1f990 63 4c 69 73 74 20 2a 70 2c 20 54 6f 6b 65 6e 20 cList *p, Token
1f9a0 2a 70 49 6e 64 65 78 65 64 42 79 29 7b 0a 20 20 *pIndexedBy){.
1f9b0 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 65 64 assert( pIndexed
1f9c0 42 79 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 By!=0 );. if( p
1f9d0 20 26 26 20 41 4c 57 41 59 53 28 70 2d 3e 6e 53 && ALWAYS(p->nS
1f9e0 72 63 3e 30 29 20 29 7b 0a 20 20 20 20 73 74 72 rc>0) ){. str
1f9f0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1fa00 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b *pItem = &p->a[
1fa10 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 20 20 p->nSrc-1];.
1fa20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 66 assert( pItem->f
1fa30 67 2e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 g.notIndexed==0
1fa40 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1fa50 49 74 65 6d 2d 3e 66 67 2e 69 73 49 6e 64 65 78 Item->fg.isIndex
1fa60 65 64 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 61 edBy==0 );. a
1fa70 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 66 67 ssert( pItem->fg
1fa80 2e 69 73 54 61 62 46 75 6e 63 3d 3d 30 20 29 3b .isTabFunc==0 );
1fa90 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 65 . if( pIndexe
1faa0 64 42 79 2d 3e 6e 3d 3d 31 20 26 26 20 21 70 49 dBy->n==1 && !pI
1fab0 6e 64 65 78 65 64 42 79 2d 3e 7a 20 29 7b 0a 20 ndexedBy->z ){.
1fac0 20 20 20 20 20 2f 2a 20 41 20 22 4e 4f 54 20 49 /* A "NOT I
1fad0 4e 44 45 58 45 44 22 20 63 6c 61 75 73 65 20 77 NDEXED" clause w
1fae0 61 73 20 73 75 70 70 6c 69 65 64 2e 20 53 65 65 as supplied. See
1faf0 20 70 61 72 73 65 2e 79 20 0a 20 20 20 20 20 20 parse.y .
1fb00 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 22 69 6e ** construct "in
1fb10 64 65 78 65 64 5f 6f 70 74 22 20 66 6f 72 20 64 dexed_opt" for d
1fb20 65 74 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 20 etails. */.
1fb30 20 70 49 74 65 6d 2d 3e 66 67 2e 6e 6f 74 49 6e pItem->fg.notIn
1fb40 64 65 78 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d dexed = 1;. }
1fb50 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 74 65 else{. pIte
1fb60 6d 2d 3e 75 31 2e 7a 49 6e 64 65 78 65 64 42 79 m->u1.zIndexedBy
1fb70 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
1fb80 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e omToken(pParse->
1fb90 64 62 2c 20 70 49 6e 64 65 78 65 64 42 79 29 3b db, pIndexedBy);
1fba0 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 66 67 . pItem->fg
1fbb0 2e 69 73 49 6e 64 65 78 65 64 42 79 20 3d 20 28 .isIndexedBy = (
1fbc0 70 49 74 65 6d 2d 3e 75 31 2e 7a 49 6e 64 65 78 pItem->u1.zIndex
1fbd0 65 64 42 79 21 3d 30 29 3b 0a 20 20 20 20 7d 0a edBy!=0);. }.
1fbe0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 }.}../*.** Add
1fbf0 20 74 68 65 20 6c 69 73 74 20 6f 66 20 66 75 6e the list of fun
1fc00 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 ction arguments
1fc10 74 6f 20 74 68 65 20 53 72 63 4c 69 73 74 20 65 to the SrcList e
1fc20 6e 74 72 79 20 66 6f 72 20 61 0a 2a 2a 20 74 61 ntry for a.** ta
1fc30 62 6c 65 2d 76 61 6c 75 65 64 2d 66 75 6e 63 74 ble-valued-funct
1fc40 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ion..*/.void sql
1fc50 69 74 65 33 53 72 63 4c 69 73 74 46 75 6e 63 41 ite3SrcListFuncA
1fc60 72 67 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 rgs(Parse *pPars
1fc70 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 45 e, SrcList *p, E
1fc80 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b xprList *pList){
1fc90 0a 20 20 69 66 28 20 70 20 26 26 20 70 4c 69 73 . if( p && pLis
1fca0 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 t ){. struct
1fcb0 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
1fcc0 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e tem = &p->a[p->n
1fcd0 53 72 63 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 Src-1];. asse
1fce0 72 74 28 20 70 49 74 65 6d 2d 3e 66 67 2e 6e 6f rt( pItem->fg.no
1fcf0 74 49 6e 64 65 78 65 64 3d 3d 30 20 29 3b 0a 20 tIndexed==0 );.
1fd00 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d assert( pItem
1fd10 2d 3e 66 67 2e 69 73 49 6e 64 65 78 65 64 42 79 ->fg.isIndexedBy
1fd20 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
1fd30 74 28 20 70 49 74 65 6d 2d 3e 66 67 2e 69 73 54 t( pItem->fg.isT
1fd40 61 62 46 75 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 abFunc==0 );.
1fd50 20 70 49 74 65 6d 2d 3e 75 31 2e 70 46 75 6e 63 pItem->u1.pFunc
1fd60 41 72 67 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 Arg = pList;.
1fd70 20 70 49 74 65 6d 2d 3e 66 67 2e 69 73 54 61 62 pItem->fg.isTab
1fd80 46 75 6e 63 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 Func = 1;. }els
1fd90 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 e{. sqlite3Ex
1fda0 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 prListDelete(pPa
1fdb0 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b rse->db, pList);
1fdc0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 . }.}../*.** Wh
1fdd0 65 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20 61 en building up a
1fde0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e 20 FROM clause in
1fdf0 74 68 65 20 70 61 72 73 65 72 2c 20 74 68 65 20 the parser, the
1fe00 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a join operator.**
1fe10 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 74 is initially at
1fe20 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65 tached to the le
1fe30 66 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 74 ft operand. But
1fe40 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 the code genera
1fe50 74 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 74 tor.** expects t
1fe60 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 he join operator
1fe70 20 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 69 to be on the ri
1fe80 67 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 ght operand. Th
1fe90 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 68 is routine.** Sh
1fea0 69 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70 ifts all join op
1feb0 65 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 66 erators from lef
1fec0 74 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20 61 t to right for a
1fed0 6e 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a 2a n entire FROM.**
1fee0 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45 clause..**.** E
1fef0 78 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 20 xample: Suppose
1ff00 74 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b 65 the join is like
1ff10 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
1ff20 20 20 20 20 20 20 20 41 20 6e 61 74 75 72 61 6c A natural
1ff30 20 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a cross join B.**
1ff40 0a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 .** The operator
1ff50 20 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f is "natural cro
1ff60 73 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41 ss join". The A
1ff70 20 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20 and B operands
1ff80 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e are stored.** in
1ff90 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e p->a[0] and p->
1ffa0 61 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65 a[1], respective
1ffb0 6c 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 ly. The parser
1ffc0 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73 initially stores
1ffd0 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 the.** operator
1ffe0 20 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 72 with A. This r
1fff0 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 68 outine shifts th
20000 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72 at operator over
20010 20 74 6f 20 42 2e 0a 2a 2f 0a 76 6f 69 64 20 73 to B..*/.void s
20020 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 qlite3SrcListShi
20030 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 ftJoinType(SrcLi
20040 73 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 st *p){. if( p
20050 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
20060 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 53 72 63 2d for(i=p->nSrc-
20070 31 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 1; i>0; i--){.
20080 20 20 20 20 70 2d 3e 61 5b 69 5d 2e 66 67 2e 6a p->a[i].fg.j
20090 6f 69 6e 74 79 70 65 20 3d 20 70 2d 3e 61 5b 69 ointype = p->a[i
200a0 2d 31 5d 2e 66 67 2e 6a 6f 69 6e 74 79 70 65 3b -1].fg.jointype;
200b0 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 5b . }. p->a[
200c0 30 5d 2e 66 67 2e 6a 6f 69 6e 74 79 70 65 20 3d 0].fg.jointype =
200d0 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0;. }.}../*.**
200e0 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 Begin a transac
200f0 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c tion.*/.void sql
20100 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 ite3BeginTransac
20110 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 tion(Parse *pPar
20120 73 65 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 se, int type){.
20130 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
20140 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 Vdbe *v;. int i
20150 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ;.. assert( pPa
20160 72 73 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d rse!=0 );. db =
20170 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 pParse->db;. a
20180 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
20190 2f 2a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b /* if( db->aDb[
201a0 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 0].pBt==0 ) retu
201b0 72 6e 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c rn; */. if( sql
201c0 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
201d0 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 arse, SQLITE_TRA
201e0 4e 53 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e NSACTION, "BEGIN
201f0 22 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 ", 0, 0) ){.
20200 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 return;. }. v
20210 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
20220 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 (pParse);. if(
20230 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 !v ) return;. i
20240 66 28 20 74 79 70 65 21 3d 54 4b 5f 44 45 46 45 f( type!=TK_DEFE
20250 52 52 45 44 20 29 7b 0a 20 20 20 20 66 6f 72 28 RRED ){. for(
20260 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
20270 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
20280 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
20290 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c OP_Transaction,
202a0 20 69 2c 20 28 74 79 70 65 3d 3d 54 4b 5f 45 58 i, (type==TK_EX
202b0 43 4c 55 53 49 56 45 29 2b 31 29 3b 0a 20 20 20 CLUSIVE)+1);.
202c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 sqlite3VdbeUs
202d0 65 73 42 74 72 65 65 28 76 2c 20 69 29 3b 0a 20 esBtree(v, i);.
202e0 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
202f0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20300 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 30 OP_AutoCommit, 0
20310 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 , 0);.}../*.** C
20320 6f 6d 6d 69 74 20 61 20 74 72 61 6e 73 61 63 74 ommit a transact
20330 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 ion.*/.void sqli
20340 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 te3CommitTransac
20350 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 tion(Parse *pPar
20360 73 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a se){. Vdbe *v;.
20370 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 . assert( pPars
20380 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 e!=0 );. assert
20390 28 20 70 50 61 72 73 65 2d 3e 64 62 21 3d 30 20 ( pParse->db!=0
203a0 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
203b0 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
203c0 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 , SQLITE_TRANSAC
203d0 54 49 4f 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 TION, "COMMIT",
203e0 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 0, 0) ){. ret
203f0 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20 73 urn;. }. v = s
20400 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
20410 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 arse);. if( v )
20420 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
20430 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 eAddOp2(v, OP_Au
20440 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 30 29 3b toCommit, 1, 0);
20450 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f . }.}../*.** Ro
20460 6c 6c 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63 llback a transac
20470 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c tion.*/.void sql
20480 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e ite3RollbackTran
20490 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 saction(Parse *p
204a0 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65 20 2a Parse){. Vdbe *
204b0 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 v;.. assert( pP
204c0 61 72 73 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 arse!=0 );. ass
204d0 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 62 21 ert( pParse->db!
204e0 3d 30 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 =0 );. if( sqli
204f0 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
20500 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e rse, SQLITE_TRAN
20510 53 41 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c 42 41 SACTION, "ROLLBA
20520 43 4b 22 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 CK", 0, 0) ){.
20530 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
20540 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
20550 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 be(pParse);. if
20560 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( v ){. sqlit
20570 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20580 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 OP_AutoCommit, 1
20590 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a , 1);. }.}../*.
205a0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
205b0 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
205c0 65 20 70 61 72 73 65 72 20 77 68 65 6e 20 69 74 e parser when it
205d0 20 70 61 72 73 65 73 20 61 20 63 6f 6d 6d 61 6e parses a comman
205e0 64 20 74 6f 20 63 72 65 61 74 65 2c 0a 2a 2a 20 d to create,.**
205f0 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 release or rollb
20600 61 63 6b 20 61 6e 20 53 51 4c 20 73 61 76 65 70 ack an SQL savep
20610 6f 69 6e 74 2e 20 0a 2a 2f 0a 76 6f 69 64 20 73 oint. .*/.void s
20620 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 qlite3Savepoint(
20630 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
20640 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70 4e nt op, Token *pN
20650 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e ame){. char *zN
20660 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d ame = sqlite3Nam
20670 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 eFromToken(pPars
20680 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 e->db, pName);.
20690 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 if( zName ){.
206a0 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 Vdbe *v = sqli
206b0 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
206c0 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 e);.#ifndef SQLI
206d0 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
206e0 41 54 49 4f 4e 0a 20 20 20 20 73 74 61 74 69 63 ATION. static
206f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f const char * co
20700 6e 73 74 20 61 7a 5b 5d 20 3d 20 7b 20 22 42 45 nst az[] = { "BE
20710 47 49 4e 22 2c 20 22 52 45 4c 45 41 53 45 22 2c GIN", "RELEASE",
20720 20 22 52 4f 4c 4c 42 41 43 4b 22 20 7d 3b 0a 20 "ROLLBACK" };.
20730 20 20 20 61 73 73 65 72 74 28 20 21 53 41 56 45 assert( !SAVE
20740 50 4f 49 4e 54 5f 42 45 47 49 4e 20 26 26 20 53 POINT_BEGIN && S
20750 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
20760 3d 3d 31 20 26 26 20 53 41 56 45 50 4f 49 4e 54 ==1 && SAVEPOINT
20770 5f 52 4f 4c 4c 42 41 43 4b 3d 3d 32 20 29 3b 0a _ROLLBACK==2 );.
20780 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 #endif. if( !
20790 76 20 7c 7c 20 73 71 6c 69 74 65 33 41 75 74 68 v || sqlite3Auth
207a0 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 Check(pParse, SQ
207b0 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 2c 20 LITE_SAVEPOINT,
207c0 61 7a 5b 6f 70 5d 2c 20 7a 4e 61 6d 65 2c 20 30 az[op], zName, 0
207d0 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) ){. sqlit
207e0 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d e3DbFree(pParse-
207f0 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 >db, zName);.
20800 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
20810 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
20820 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 61 76 AddOp4(v, OP_Sav
20830 65 70 6f 69 6e 74 2c 20 6f 70 2c 20 30 2c 20 30 epoint, op, 0, 0
20840 2c 20 7a 4e 61 6d 65 2c 20 50 34 5f 44 59 4e 41 , zName, P4_DYNA
20850 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a MIC);. }.}../*.
20860 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 ** Make sure the
20870 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 69 TEMP database i
20880 73 20 6f 70 65 6e 20 61 6e 64 20 61 76 61 69 6c s open and avail
20890 61 62 6c 65 20 66 6f 72 20 75 73 65 2e 20 20 52 able for use. R
208a0 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d eturn.** the num
208b0 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 ber of errors.
208c0 4c 65 61 76 65 20 61 6e 79 20 65 72 72 6f 72 20 Leave any error
208d0 6d 65 73 73 61 67 65 73 20 69 6e 20 74 68 65 20 messages in the
208e0 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 pParse structure
208f0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
20900 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 OpenTempDatabase
20910 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b (Parse *pParse){
20920 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
20930 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
20940 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 f( db->aDb[1].pB
20950 74 3d 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d t==0 && !pParse-
20960 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 >explain ){.
20970 69 6e 74 20 72 63 3b 0a 20 20 20 20 42 74 72 65 int rc;. Btre
20980 65 20 2a 70 42 74 3b 0a 20 20 20 20 73 74 61 74 e *pBt;. stat
20990 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 ic const int fla
209a0 67 73 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 gs = .
209b0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
209c0 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 20 20 WRITE |.
209d0 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 SQLITE_OPEN_CR
209e0 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 EATE |.
209f0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
20a00 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 20 20 LUSIVE |.
20a10 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 SQLITE_OPEN_D
20a20 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 ELETEONCLOSE |.
20a30 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
20a40 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a 20 OPEN_TEMP_DB;..
20a50 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
20a60 74 72 65 65 4f 70 65 6e 28 64 62 2d 3e 70 56 66 treeOpen(db->pVf
20a70 73 2c 20 30 2c 20 64 62 2c 20 26 70 42 74 2c 20 s, 0, db, &pBt,
20a80 30 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 0, flags);. i
20a90 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
20aa0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
20ab0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
20ac0 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 , "unable to ope
20ad0 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 64 61 n a temporary da
20ae0 74 61 62 61 73 65 20 22 0a 20 20 20 20 20 20 20 tabase ".
20af0 20 22 66 69 6c 65 20 66 6f 72 20 73 74 6f 72 69 "file for stori
20b00 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 ng temporary tab
20b10 6c 65 73 22 29 3b 0a 20 20 20 20 20 20 70 50 61 les");. pPa
20b20 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 rse->rc = rc;.
20b30 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
20b40 20 20 7d 0a 20 20 20 20 64 62 2d 3e 61 44 62 5b }. db->aDb[
20b50 31 5d 2e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 1].pBt = pBt;.
20b60 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 assert( db->aD
20b70 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a b[1].pSchema );.
20b80 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e if( SQLITE_N
20b90 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 42 74 72 OMEM==sqlite3Btr
20ba0 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 42 eeSetPageSize(pB
20bb0 74 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 t, db->nextPages
20bc0 69 7a 65 2c 20 2d 31 2c 20 30 29 20 29 7b 0a 20 ize, -1, 0) ){.
20bd0 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 db->mallocF
20be0 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 ailed = 1;.
20bf0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
20c00 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
20c10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 .}../*.** Record
20c20 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 the fact that t
20c30 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 he schema cookie
20c40 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 will need to be
20c50 20 76 65 72 69 66 69 65 64 0a 2a 2a 20 66 6f 72 verified.** for
20c60 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 20 database iDb.
20c70 54 68 65 20 63 6f 64 65 20 74 6f 20 61 63 74 75 The code to actu
20c80 61 6c 6c 79 20 76 65 72 69 66 79 20 74 68 65 20 ally verify the
20c90 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 2a 2a schema cookie.**
20ca0 20 77 69 6c 6c 20 6f 63 63 75 72 20 61 74 20 74 will occur at t
20cb0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 6f he end of the to
20cc0 70 2d 6c 65 76 65 6c 20 56 44 42 45 20 61 6e 64 p-level VDBE and
20cd0 20 77 69 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 will be generat
20ce0 65 64 0a 2a 2a 20 6c 61 74 65 72 2c 20 62 79 20 ed.** later, by
20cf0 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 sqlite3FinishCod
20d00 69 6e 67 28 29 2e 0a 2a 2f 0a 76 6f 69 64 20 73 ing()..*/.void s
20d10 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 qlite3CodeVerify
20d20 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 Schema(Parse *pP
20d30 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a arse, int iDb){.
20d40 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 Parse *pToplev
20d50 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 el = sqlite3Pars
20d60 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 eToplevel(pParse
20d70 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 );. sqlite3 *db
20d80 20 3d 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 = pToplevel->db
20d90 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 ;.. assert( iDb
20da0 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e >=0 && iDb<db->n
20db0 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
20dc0 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 db->aDb[iDb].pBt
20dd0 21 3d 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b !=0 || iDb==1 );
20de0 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 53 . assert( iDb<S
20df0 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 QLITE_MAX_ATTACH
20e00 45 44 2b 32 20 29 3b 0a 20 20 61 73 73 65 72 74 ED+2 );. assert
20e10 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d ( sqlite3SchemaM
20e20 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 utexHeld(db, iDb
20e30 2c 20 30 29 20 29 3b 0a 20 20 69 66 28 20 44 62 , 0) );. if( Db
20e40 4d 61 73 6b 54 65 73 74 28 70 54 6f 70 6c 65 76 MaskTest(pToplev
20e50 65 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 2c 20 el->cookieMask,
20e60 69 44 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 44 iDb)==0 ){. D
20e70 62 4d 61 73 6b 53 65 74 28 70 54 6f 70 6c 65 76 bMaskSet(pToplev
20e80 65 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 2c 20 el->cookieMask,
20e90 69 44 62 29 3b 0a 20 20 20 20 70 54 6f 70 6c 65 iDb);. pTople
20ea0 76 65 6c 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 vel->cookieValue
20eb0 5b 69 44 62 5d 20 3d 20 64 62 2d 3e 61 44 62 5b [iDb] = db->aDb[
20ec0 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 iDb].pSchema->sc
20ed0 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 0a 20 20 20 hema_cookie;.
20ee0 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 if( !OMIT_TEMPD
20ef0 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 B && iDb==1 ){.
20f00 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e sqlite3Open
20f10 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 54 6f TempDatabase(pTo
20f20 70 6c 65 76 65 6c 29 3b 0a 20 20 20 20 7d 0a 20 plevel);. }.
20f30 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 61 }.}../*.** If a
20f40 72 67 75 6d 65 6e 74 20 7a 44 62 20 69 73 20 4e rgument zDb is N
20f50 55 4c 4c 2c 20 74 68 65 6e 20 63 61 6c 6c 20 73 ULL, then call s
20f60 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 qlite3CodeVerify
20f70 53 63 68 65 6d 61 28 29 20 66 6f 72 20 65 61 63 Schema() for eac
20f80 68 20 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 h .** attached d
20f90 61 74 61 62 61 73 65 2e 20 4f 74 68 65 72 77 69 atabase. Otherwi
20fa0 73 65 2c 20 69 6e 76 6f 6b 65 20 69 74 20 66 6f se, invoke it fo
20fb0 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e r the database n
20fc0 61 6d 65 64 20 7a 44 62 20 6f 6e 6c 79 2e 0a 2a amed zDb only..*
20fd0 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f /.void sqlite3Co
20fe0 64 65 56 65 72 69 66 79 4e 61 6d 65 64 53 63 68 deVerifyNamedSch
20ff0 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 ema(Parse *pPars
21000 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
21010 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a Db){. sqlite3 *
21020 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
21030 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 . int i;. for(
21040 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
21050 69 2b 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 i++){. Db *pD
21060 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b b = &db->aDb[i];
21070 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 . if( pDb->pB
21080 74 20 26 26 20 28 21 7a 44 62 20 7c 7c 20 30 3d t && (!zDb || 0=
21090 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 =sqlite3StrICmp(
210a0 7a 44 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 zDb, pDb->zName)
210b0 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) ){. sqlit
210c0 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 e3CodeVerifySche
210d0 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b 0a 20 ma(pParse, i);.
210e0 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
210f0 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 * Generate VDBE
21100 63 6f 64 65 20 74 68 61 74 20 70 72 65 70 61 72 code that prepar
21110 65 73 20 66 6f 72 20 64 6f 69 6e 67 20 61 6e 20 es for doing an
21120 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 0a 2a operation that.*
21130 2a 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 * might change t
21140 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a he database..**.
21150 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
21160 73 74 61 72 74 73 20 61 20 6e 65 77 20 74 72 61 starts a new tra
21170 6e 73 61 63 74 69 6f 6e 20 69 66 20 77 65 20 61 nsaction if we a
21180 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 77 re not already w
21190 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 ithin.** a trans
211a0 61 63 74 69 6f 6e 2e 20 20 49 66 20 77 65 20 61 action. If we a
211b0 72 65 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 re already withi
211c0 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c n a transaction,
211d0 20 74 68 65 6e 20 61 20 63 68 65 63 6b 70 6f 69 then a checkpoi
211e0 6e 74 0a 2a 2a 20 69 73 20 73 65 74 20 69 66 20 nt.** is set if
211f0 74 68 65 20 73 65 74 53 74 61 74 65 6d 65 6e 74 the setStatement
21200 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 parameter is tr
21210 75 65 2e 20 20 41 20 63 68 65 63 6b 70 6f 69 6e ue. A checkpoin
21220 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 t should.** be s
21230 65 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e et for operation
21240 73 20 74 68 61 74 20 6d 69 67 68 74 20 66 61 69 s that might fai
21250 6c 20 28 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 l (due to a cons
21260 74 72 61 69 6e 74 29 20 70 61 72 74 20 6f 66 0a traint) part of.
21270 2a 2a 20 74 68 65 20 77 61 79 20 74 68 72 6f 75 ** the way throu
21280 67 68 20 61 6e 64 20 77 68 69 63 68 20 77 69 6c gh and which wil
21290 6c 20 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20 73 l need to undo s
212a0 6f 6d 65 20 77 72 69 74 65 73 20 77 69 74 68 6f ome writes witho
212b0 75 74 20 68 61 76 69 6e 67 20 74 6f 0a 2a 2a 20 ut having to.**
212c0 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 77 68 6f rollback the who
212d0 6c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 le transaction.
212e0 20 46 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 For operations
212f0 77 68 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72 where all constr
21300 61 69 6e 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 aints.** can be
21310 63 68 65 63 6b 65 64 20 62 65 66 6f 72 65 20 61 checked before a
21320 6e 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d ny changes are m
21330 61 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 ade to the datab
21340 61 73 65 2c 20 69 74 20 69 73 20 6e 65 76 65 72 ase, it is never
21350 0a 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f .** necessary to
21360 20 75 6e 64 6f 20 61 20 77 72 69 74 65 20 61 6e undo a write an
21370 64 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 d the checkpoint
21380 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 should not be s
21390 65 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 et..*/.void sqli
213a0 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 te3BeginWriteOpe
213b0 72 61 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 ration(Parse *pP
213c0 61 72 73 65 2c 20 69 6e 74 20 73 65 74 53 74 61 arse, int setSta
213d0 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69 44 62 29 tement, int iDb)
213e0 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c {. Parse *pTopl
213f0 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 evel = sqlite3Pa
21400 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 rseToplevel(pPar
21410 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f se);. sqlite3Co
21420 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 deVerifySchema(p
21430 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 44 Parse, iDb);. D
21440 62 4d 61 73 6b 53 65 74 28 70 54 6f 70 6c 65 76 bMaskSet(pToplev
21450 65 6c 2d 3e 77 72 69 74 65 4d 61 73 6b 2c 20 69 el->writeMask, i
21460 44 62 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c Db);. pToplevel
21470 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 7c ->isMultiWrite |
21480 3d 20 73 65 74 53 74 61 74 65 6d 65 6e 74 3b 0a = setStatement;.
21490 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 }../*.** Indicat
214a0 65 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 e that the state
214b0 6d 65 6e 74 20 63 75 72 72 65 6e 74 6c 79 20 75 ment currently u
214c0 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
214d0 6e 20 6d 69 67 68 74 20 77 72 69 74 65 0a 2a 2a n might write.**
214e0 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 65 more than one e
214f0 6e 74 72 79 20 28 65 78 61 6d 70 6c 65 3a 20 64 ntry (example: d
21500 65 6c 65 74 69 6e 67 20 6f 6e 65 20 72 6f 77 20 eleting one row
21510 74 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 61 then inserting a
21520 6e 6f 74 68 65 72 2c 0a 2a 2a 20 69 6e 73 65 72 nother,.** inser
21530 74 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 72 6f ting multiple ro
21540 77 73 20 69 6e 20 61 20 74 61 62 6c 65 2c 20 6f ws in a table, o
21550 72 20 69 6e 73 65 72 74 69 6e 67 20 61 20 72 6f r inserting a ro
21560 77 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 w and index entr
21570 69 65 73 2e 29 0a 2a 2a 20 49 66 20 61 6e 20 61 ies.).** If an a
21580 62 6f 72 74 20 6f 63 63 75 72 73 20 61 66 74 65 bort occurs afte
21590 72 20 73 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 r some of these
215a0 77 72 69 74 65 73 20 68 61 76 65 20 63 6f 6d 70 writes have comp
215b0 6c 65 74 65 64 2c 20 74 68 65 6e 20 69 74 20 77 leted, then it w
215c0 69 6c 6c 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 ill.** be necess
215d0 61 72 79 20 74 6f 20 75 6e 64 6f 20 74 68 65 20 ary to undo the
215e0 63 6f 6d 70 6c 65 74 65 64 20 77 72 69 74 65 73 completed writes
215f0 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
21600 33 4d 75 6c 74 69 57 72 69 74 65 28 50 61 72 73 3MultiWrite(Pars
21610 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 50 61 e *pParse){. Pa
21620 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d rse *pToplevel =
21630 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 sqlite3ParseTop
21640 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 level(pParse);.
21650 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 69 73 4d 75 pToplevel->isMu
21660 6c 74 69 57 72 69 74 65 20 3d 20 31 3b 0a 7d 0a ltiWrite = 1;.}.
21670 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 64 65 ./* .** The code
21680 20 67 65 6e 65 72 61 74 6f 72 20 63 61 6c 6c 73 generator calls
21690 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 66 this routine if
216a0 20 69 73 20 64 69 73 63 6f 76 65 72 73 20 74 68 is discovers th
216b0 61 74 20 69 74 20 69 73 0a 2a 2a 20 70 6f 73 73 at it is.** poss
216c0 69 62 6c 65 20 74 6f 20 61 62 6f 72 74 20 61 20 ible to abort a
216d0 73 74 61 74 65 6d 65 6e 74 20 70 72 69 6f 72 20 statement prior
216e0 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20 20 to completion.
216f0 49 6e 20 6f 72 64 65 72 20 74 6f 20 0a 2a 2a 20 In order to .**
21700 70 65 72 66 6f 72 6d 20 74 68 69 73 20 61 62 6f perform this abo
21710 72 74 20 77 69 74 68 6f 75 74 20 63 6f 72 72 75 rt without corru
21720 70 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 pting the databa
21730 73 65 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 6d se, we need to m
21740 61 6b 65 0a 2a 2a 20 73 75 72 65 20 74 68 61 74 ake.** sure that
21750 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 the statement i
21760 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 61 s protected by a
21770 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
21780 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 65 action..**.** Te
21790 63 68 6e 69 63 61 6c 6c 79 2c 20 77 65 20 6f 6e chnically, we on
217a0 6c 79 20 6e 65 65 64 20 74 6f 20 73 65 74 20 74 ly need to set t
217b0 68 65 20 6d 61 79 41 62 6f 72 74 20 66 6c 61 67 he mayAbort flag
217c0 20 69 66 20 74 68 65 0a 2a 2a 20 69 73 4d 75 6c if the.** isMul
217d0 74 69 57 72 69 74 65 20 66 6c 61 67 20 77 61 73 tiWrite flag was
217e0 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 2e previously set.
217f0 20 20 54 68 65 72 65 20 69 73 20 61 20 74 69 6d There is a tim
21800 65 20 64 65 70 65 6e 64 65 6e 63 79 0a 2a 2a 20 e dependency.**
21810 73 75 63 68 20 74 68 61 74 20 74 68 65 20 61 62 such that the ab
21820 6f 72 74 20 6d 75 73 74 20 6f 63 63 75 72 20 61 ort must occur a
21830 66 74 65 72 20 74 68 65 20 6d 75 6c 74 69 77 72 fter the multiwr
21840 69 74 65 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 ite. This makes
21850 0a 2a 2a 20 73 6f 6d 65 20 73 74 61 74 65 6d 65 .** some stateme
21860 6e 74 73 20 69 6e 76 6f 6c 76 69 6e 67 20 74 68 nts involving th
21870 65 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 e REPLACE confli
21880 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c ct resolution al
21890 67 6f 72 69 74 68 6d 0a 2a 2a 20 67 6f 20 61 20 gorithm.** go a
218a0 6c 69 74 74 6c 65 20 66 61 73 74 65 72 2e 20 20 little faster.
218b0 42 75 74 20 74 61 6b 69 6e 67 20 61 64 76 61 6e But taking advan
218c0 74 61 67 65 20 6f 66 20 74 68 69 73 20 74 69 6d tage of this tim
218d0 65 20 64 65 70 65 6e 64 65 6e 63 79 0a 2a 2a 20 e dependency.**
218e0 6d 61 6b 65 73 20 69 74 20 6d 6f 72 65 20 64 69 makes it more di
218f0 66 66 69 63 75 6c 74 20 74 6f 20 70 72 6f 76 65 fficult to prove
21900 20 74 68 61 74 20 74 68 65 20 63 6f 64 65 20 69 that the code i
21910 73 20 63 6f 72 72 65 63 74 20 28 69 6e 20 0a 2a s correct (in .*
21920 2a 20 70 61 72 74 69 63 75 6c 61 72 2c 20 69 74 * particular, it
21930 20 70 72 65 76 65 6e 74 73 20 75 73 20 66 72 6f prevents us fro
21940 6d 20 77 72 69 74 69 6e 67 20 61 6e 20 65 66 66 m writing an eff
21950 65 63 74 69 76 65 0a 2a 2a 20 69 6d 70 6c 65 6d ective.** implem
21960 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
21970 74 65 33 41 73 73 65 72 74 4d 61 79 41 62 6f 72 te3AssertMayAbor
21980 74 28 29 29 20 61 6e 64 20 73 6f 20 77 65 20 68 t()) and so we h
21990 61 76 65 20 63 68 6f 73 65 6e 0a 2a 2a 20 74 6f ave chosen.** to
219a0 20 74 61 6b 65 20 74 68 65 20 73 61 66 65 20 72 take the safe r
219b0 6f 75 74 65 20 61 6e 64 20 73 6b 69 70 20 74 68 oute and skip th
219c0 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a e optimization..
219d0 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 4d */.void sqlite3M
219e0 61 79 41 62 6f 72 74 28 50 61 72 73 65 20 2a 70 ayAbort(Parse *p
219f0 50 61 72 73 65 29 7b 0a 20 20 50 61 72 73 65 20 Parse){. Parse
21a00 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c *pToplevel = sql
21a10 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 ite3ParseTopleve
21a20 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 70 54 6f l(pParse);. pTo
21a30 70 6c 65 76 65 6c 2d 3e 6d 61 79 41 62 6f 72 74 plevel->mayAbort
21a40 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 = 1;.}../*.** C
21a50 6f 64 65 20 61 6e 20 4f 50 5f 48 61 6c 74 20 74 ode an OP_Halt t
21a60 68 61 74 20 63 61 75 73 65 73 20 74 68 65 20 76 hat causes the v
21a70 64 62 65 20 74 6f 20 72 65 74 75 72 6e 20 61 6e dbe to return an
21a80 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
21a90 4e 54 0a 2a 2a 20 65 72 72 6f 72 2e 20 54 68 65 NT.** error. The
21aa0 20 6f 6e 45 72 72 6f 72 20 70 61 72 61 6d 65 74 onError paramet
21ab0 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 er determines wh
21ac0 69 63 68 20 28 69 66 20 61 6e 79 29 20 6f 66 20 ich (if any) of
21ad0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a the statement.**
21ae0 20 61 6e 64 2f 6f 72 20 63 75 72 72 65 6e 74 20 and/or current
21af0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 transaction is r
21b00 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 76 olled back..*/.v
21b10 6f 69 64 20 73 71 6c 69 74 65 33 48 61 6c 74 43 oid sqlite3HaltC
21b20 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72 onstraint(. Par
21b30 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f se *pParse, /
21b40 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
21b50 74 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f t */. int errCo
21b60 64 65 2c 20 20 20 20 20 20 2f 2a 20 65 78 74 65 de, /* exte
21b70 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 nded error code
21b80 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 */. int onError
21b90 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 , /* Constr
21ba0 61 69 6e 74 20 74 79 70 65 20 2a 2f 0a 20 20 63 aint type */. c
21bb0 68 61 72 20 2a 70 34 2c 20 20 20 20 20 20 20 20 har *p4,
21bc0 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 /* Error messag
21bd0 65 20 2a 2f 0a 20 20 69 38 20 70 34 74 79 70 65 e */. i8 p4type
21be0 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 34 5f 53 , /* P4_S
21bf0 54 41 54 49 43 20 6f 72 20 50 34 5f 54 52 41 4e TATIC or P4_TRAN
21c00 53 49 45 4e 54 20 2a 2f 0a 20 20 75 38 20 70 35 SIENT */. u8 p5
21c10 45 72 72 6d 73 67 20 20 20 20 20 20 20 2f 2a 20 Errmsg /*
21c20 50 35 5f 45 72 72 4d 73 67 20 74 79 70 65 20 2a P5_ErrMsg type *
21c30 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d /.){. Vdbe *v =
21c40 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
21c50 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 pParse);. asser
21c60 74 28 20 28 65 72 72 43 6f 64 65 26 30 78 66 66 t( (errCode&0xff
21c70 29 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 )==SQLITE_CONSTR
21c80 41 49 4e 54 20 29 3b 0a 20 20 69 66 28 20 6f 6e AINT );. if( on
21c90 45 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 Error==OE_Abort
21ca0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 ){. sqlite3Ma
21cb0 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a yAbort(pParse);.
21cc0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
21cd0 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 eAddOp4(v, OP_Ha
21ce0 6c 74 2c 20 65 72 72 43 6f 64 65 2c 20 6f 6e 45 lt, errCode, onE
21cf0 72 72 6f 72 2c 20 30 2c 20 70 34 2c 20 70 34 74 rror, 0, p4, p4t
21d00 79 70 65 29 3b 0a 20 20 69 66 28 20 70 35 45 72 ype);. if( p5Er
21d10 72 6d 73 67 20 29 20 73 71 6c 69 74 65 33 56 64 rmsg ) sqlite3Vd
21d20 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70 35 beChangeP5(v, p5
21d30 45 72 72 6d 73 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a Errmsg);.}../*.*
21d40 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 48 61 6c * Code an OP_Hal
21d50 74 20 64 75 65 20 74 6f 20 55 4e 49 51 55 45 20 t due to UNIQUE
21d60 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 or PRIMARY KEY c
21d70 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 onstraint violat
21d80 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ion..*/.void sql
21d90 69 74 65 33 55 6e 69 71 75 65 43 6f 6e 73 74 72 ite3UniqueConstr
21da0 61 69 6e 74 28 0a 20 20 50 61 72 73 65 20 2a 70 aint(. Parse *p
21db0 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 Parse, /* Par
21dc0 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
21dd0 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 int onError,
21de0 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e /* Constrain
21df0 74 20 74 79 70 65 20 2a 2f 0a 20 20 49 6e 64 65 t type */. Inde
21e00 78 20 2a 70 49 64 78 20 20 20 20 20 20 20 2f 2a x *pIdx /*
21e10 20 54 68 65 20 69 6e 64 65 78 20 74 68 61 74 20 The index that
21e20 74 72 69 67 67 65 72 73 20 74 68 65 20 63 6f 6e triggers the con
21e30 73 74 72 61 69 6e 74 20 2a 2f 0a 29 7b 0a 20 20 straint */.){.
21e40 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 69 6e char *zErr;. in
21e50 74 20 6a 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 t j;. StrAccum
21e60 65 72 72 4d 73 67 3b 0a 20 20 54 61 62 6c 65 20 errMsg;. Table
21e70 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 *pTab = pIdx->pT
21e80 61 62 6c 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 able;.. sqlite3
21e90 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 65 72 StrAccumInit(&er
21ea0 72 4d 73 67 2c 20 70 50 61 72 73 65 2d 3e 64 62 rMsg, pParse->db
21eb0 2c 20 30 2c 20 30 2c 20 32 30 30 29 3b 0a 20 20 , 0, 0, 200);.
21ec0 69 66 28 20 70 49 64 78 2d 3e 61 43 6f 6c 45 78 if( pIdx->aColEx
21ed0 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 pr ){. sqlite
21ee0 33 58 50 72 69 6e 74 66 28 26 65 72 72 4d 73 67 3XPrintf(&errMsg
21ef0 2c 20 30 2c 20 22 69 6e 64 65 78 20 27 25 71 27 , 0, "index '%q'
21f00 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b ", pIdx->zName);
21f10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f . }else{. fo
21f20 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e r(j=0; j<pIdx->n
21f30 4b 65 79 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 KeyCol; j++){.
21f40 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 0a char *zCol;.
21f50 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 assert( pI
21f60 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3e dx->aiColumn[j]>
21f70 3d 30 20 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c =0 );. zCol
21f80 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 = pTab->aCol[pI
21f90 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 5d dx->aiColumn[j]]
21fa0 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 .zName;. if
21fb0 28 20 6a 20 29 20 73 71 6c 69 74 65 33 53 74 72 ( j ) sqlite3Str
21fc0 41 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72 72 AccumAppend(&err
21fd0 4d 73 67 2c 20 22 2c 20 22 2c 20 32 29 3b 0a 20 Msg, ", ", 2);.
21fe0 20 20 20 20 20 73 71 6c 69 74 65 33 58 50 72 69 sqlite3XPri
21ff0 6e 74 66 28 26 65 72 72 4d 73 67 2c 20 30 2c 20 ntf(&errMsg, 0,
22000 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e 7a "%s.%s", pTab->z
22010 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 Name, zCol);.
22020 20 7d 0a 20 20 7d 0a 20 20 7a 45 72 72 20 3d 20 }. }. zErr =
22030 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
22040 69 6e 69 73 68 28 26 65 72 72 4d 73 67 29 3b 0a inish(&errMsg);.
22050 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e sqlite3HaltCon
22060 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20 straint(pParse,
22070 0a 20 20 20 20 49 73 50 72 69 6d 61 72 79 4b 65 . IsPrimaryKe
22080 79 49 6e 64 65 78 28 70 49 64 78 29 20 3f 20 53 yIndex(pIdx) ? S
22090 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
220a0 5f 50 52 49 4d 41 52 59 4b 45 59 20 0a 20 20 20 _PRIMARYKEY .
220b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
220c0 20 20 20 20 20 20 20 20 20 3a 20 53 51 4c 49 54 : SQLIT
220d0 45 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 55 4e 49 E_CONSTRAINT_UNI
220e0 51 55 45 2c 0a 20 20 20 20 6f 6e 45 72 72 6f 72 QUE,. onError
220f0 2c 20 7a 45 72 72 2c 20 50 34 5f 44 59 4e 41 4d , zErr, P4_DYNAM
22100 49 43 2c 20 50 35 5f 43 6f 6e 73 74 72 61 69 6e IC, P5_Constrain
22110 74 55 6e 69 71 75 65 29 3b 0a 7d 0a 0a 0a 2f 2a tUnique);.}.../*
22120 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 48 .** Code an OP_H
22130 61 6c 74 20 64 75 65 20 74 6f 20 6e 6f 6e 2d 75 alt due to non-u
22140 6e 69 71 75 65 20 72 6f 77 69 64 2e 0a 2a 2f 0a nique rowid..*/.
22150 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 69 void sqlite3Rowi
22160 64 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 dConstraint(. P
22170 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
22180 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
22190 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 ext */. int onE
221a0 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 43 6f rror, /* Co
221b0 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f nflict resolutio
221c0 6e 20 61 6c 67 6f 72 69 74 68 6d 20 2a 2f 0a 20 n algorithm */.
221d0 20 54 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20 Table *pTab
221e0 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
221f0 77 69 74 68 20 74 68 65 20 6e 6f 6e 2d 75 6e 69 with the non-uni
22200 71 75 65 20 72 6f 77 69 64 20 2a 2f 20 0a 29 7b que rowid */ .){
22210 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20 . char *zMsg;.
22220 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 int rc;. if( p
22230 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b Tab->iPKey>=0 ){
22240 0a 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 . zMsg = sqli
22250 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 te3MPrintf(pPars
22260 65 2d 3e 64 62 2c 20 22 25 73 2e 25 73 22 2c 20 e->db, "%s.%s",
22270 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 pTab->zName,.
22280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22290 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f pTab->aCo
222a0 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a l[pTab->iPKey].z
222b0 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 Name);. rc =
222c0 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
222d0 54 5f 50 52 49 4d 41 52 59 4b 45 59 3b 0a 20 20 T_PRIMARYKEY;.
222e0 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4d 73 67 20 }else{. zMsg
222f0 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
22300 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 25 73 (pParse->db, "%s
22310 2e 72 6f 77 69 64 22 2c 20 70 54 61 62 2d 3e 7a .rowid", pTab->z
22320 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 Name);. rc =
22330 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
22340 54 5f 52 4f 57 49 44 3b 0a 20 20 7d 0a 20 20 73 T_ROWID;. }. s
22350 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 qlite3HaltConstr
22360 61 69 6e 74 28 70 50 61 72 73 65 2c 20 72 63 2c aint(pParse, rc,
22370 20 6f 6e 45 72 72 6f 72 2c 20 7a 4d 73 67 2c 20 onError, zMsg,
22380 50 34 5f 44 59 4e 41 4d 49 43 2c 0a 20 20 20 20 P4_DYNAMIC,.
22390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
223a0 20 20 20 20 50 35 5f 43 6f 6e 73 74 72 61 69 6e P5_Constrain
223b0 74 55 6e 69 71 75 65 29 3b 0a 7d 0a 0a 2f 2a 0a tUnique);.}../*.
223c0 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 ** Check to see
223d0 69 66 20 70 49 6e 64 65 78 20 75 73 65 73 20 74 if pIndex uses t
223e0 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 he collating seq
223f0 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 52 65 uence pColl. Re
22400 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 turn.** true if
22410 69 74 20 64 6f 65 73 20 61 6e 64 20 66 61 6c 73 it does and fals
22420 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 e if it does not
22430 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
22440 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 ITE_OMIT_REINDEX
22450 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c .static int coll
22460 61 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e 73 74 ationMatch(const
22470 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49 6e char *zColl, In
22480 64 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20 20 dex *pIndex){.
22490 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 int i;. assert(
224a0 20 7a 43 6f 6c 6c 21 3d 30 20 29 3b 0a 20 20 66 zColl!=0 );. f
224b0 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78 or(i=0; i<pIndex
224c0 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b ->nColumn; i++){
224d0 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
224e0 2a 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 *z = pIndex->azC
224f0 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 oll[i];. asse
22500 72 74 28 20 7a 21 3d 30 20 7c 7c 20 70 49 6e 64 rt( z!=0 || pInd
22510 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3c ex->aiColumn[i]<
22520 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 0 );. if( pIn
22530 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d dex->aiColumn[i]
22540 3e 3d 30 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 >=0 && 0==sqlite
22550 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c 3StrICmp(z, zCol
22560 6c 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 l) ){. retu
22570 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 1;. }. }.
22580 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 return 0;.}.#e
22590 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f ndif../*.** Reco
225a0 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 mpute all indice
225b0 73 20 6f 66 20 70 54 61 62 20 74 68 61 74 20 75 s of pTab that u
225c0 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 se the collating
225d0 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e sequence pColl.
225e0 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 .** If pColl==0
225f0 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 20 61 then recompute a
22600 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 ll indices of pT
22610 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ab..*/.#ifndef S
22620 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 QLITE_OMIT_REIND
22630 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 EX.static void r
22640 65 69 6e 64 65 78 54 61 62 6c 65 28 50 61 72 73 eindexTable(Pars
22650 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 e *pParse, Table
22660 20 2a 70 54 61 62 2c 20 63 68 61 72 20 63 6f 6e *pTab, char con
22670 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49 6e st *zColl){. In
22680 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 dex *pIndex;
22690 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 /* An
226a0 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 index associated
226b0 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a 20 with pTab */..
226c0 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54 61 62 for(pIndex=pTab
226d0 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 ->pIndex; pIndex
226e0 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65 78 2d ; pIndex=pIndex-
226f0 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
22700 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f 6c zColl==0 || col
22710 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f 6c lationMatch(zCol
22720 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 l, pIndex) ){.
22730 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 int iDb = sq
22740 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
22750 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 ex(pParse->db, p
22760 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Tab->pSchema);.
22770 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 sqlite3Begi
22780 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 nWriteOperation(
22790 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b pParse, 0, iDb);
227a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 . sqlite3Re
227b0 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 fillIndex(pParse
227c0 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 , pIndex, -1);.
227d0 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 }. }.}.#endi
227e0 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 f../*.** Recompu
227f0 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f te all indices o
22800 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 f all tables in
22810 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 77 68 all databases wh
22820 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69 63 ere the.** indic
22830 65 73 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 es use the colla
22840 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 ting sequence pC
22850 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d 3d oll. If pColl==
22860 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 0 then recompute
22870 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 .** all indices
22880 65 76 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a 23 everywhere..*/.#
22890 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
228a0 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 IT_REINDEX.stati
228b0 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78 44 61 c void reindexDa
228c0 74 61 62 61 73 65 73 28 50 61 72 73 65 20 2a 70 tabases(Parse *p
228d0 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f 6e 73 Parse, char cons
228e0 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62 20 t *zColl){. Db
228f0 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 *pDb;
22900 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 /* A si
22910 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f ngle database */
22920 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 . int iDb;
22930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
22940 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 * The database i
22950 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 ndex number */.
22960 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
22970 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 Parse->db; /*
22980 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
22990 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61 73 nection */. Has
229a0 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20 20 hElem *k;
229b0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
229c0 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74 61 62 looping over tab
229d0 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20 20 les in pDb */.
229e0 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 Table *pTab;
229f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
22a00 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 table in the da
22a10 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20 61 73 73 tabase */.. ass
22a20 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
22a30 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 eHoldsAllMutexes
22a40 28 64 62 29 20 29 3b 20 20 2f 2a 20 4e 65 65 64 (db) ); /* Need
22a50 65 64 20 66 6f 72 20 73 63 68 65 6d 61 20 61 63 ed for schema ac
22a60 63 65 73 73 20 2a 2f 0a 20 20 66 6f 72 28 69 44 cess */. for(iD
22a70 62 3d 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44 62 b=0, pDb=db->aDb
22a80 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 ; iDb<db->nDb; i
22a90 44 62 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20 20 Db++, pDb++){.
22aa0 20 20 61 73 73 65 72 74 28 20 70 44 62 21 3d 30 assert( pDb!=0
22ab0 20 29 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73 71 );. for(k=sq
22ac0 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 70 liteHashFirst(&p
22ad0 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c Db->pSchema->tbl
22ae0 48 61 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71 6c Hash); k; k=sql
22af0 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b iteHashNext(k)){
22b00 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 28 54 . pTab = (T
22b10 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 68 able*)sqliteHash
22b20 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 20 20 72 Data(k);. r
22b30 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61 72 eindexTable(pPar
22b40 73 65 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c 29 se, pTab, zColl)
22b50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 ;. }. }.}.#e
22b60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 ndif../*.** Gene
22b70 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 rate code for th
22b80 65 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61 6e e REINDEX comman
22b90 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 d..**.**
22ba0 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 REINDEX
22bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22bc0 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 20 -- 1.**
22bd0 20 20 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c 6c REINDEX <coll
22be0 61 74 69 6f 6e 3e 20 20 20 20 20 20 20 20 20 20 ation>
22bf0 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 -- 2.**
22c00 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c 64 REINDEX ?<d
22c10 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 atabase>.?<table
22c20 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20 20 name> -- 3.**
22c30 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f REINDEX ?
22c40 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e 64 <database>.?<ind
22c50 65 78 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a 2a exname> -- 4.**
22c60 0a 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 .** Form 1 cause
22c70 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e s all indices in
22c80 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 all attached da
22c90 74 61 62 61 73 65 73 20 74 6f 20 62 65 20 72 65 tabases to be re
22ca0 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20 32 built..** Form 2
22cb0 20 72 65 62 75 69 6c 64 73 20 61 6c 6c 20 69 6e rebuilds all in
22cc0 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 dices in all dat
22cd0 61 62 61 73 65 73 20 74 68 61 74 20 75 73 65 20 abases that use
22ce0 74 68 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f 6c the named.** col
22cf0 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e lating function.
22d00 20 20 46 6f 72 6d 73 20 33 20 61 6e 64 20 34 20 Forms 3 and 4
22d10 72 65 62 75 69 6c 64 20 74 68 65 20 6e 61 6d 65 rebuild the name
22d20 64 20 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a 2a d index or all.*
22d30 2a 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 * indices associ
22d40 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 ated with the na
22d50 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 med table..*/.#i
22d60 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
22d70 54 5f 52 45 49 4e 44 45 58 0a 76 6f 69 64 20 73 T_REINDEX.void s
22d80 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61 qlite3Reindex(Pa
22d90 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b rse *pParse, Tok
22da0 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65 en *pName1, Toke
22db0 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 43 6f n *pName2){. Co
22dc0 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 llSeq *pColl;
22dd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
22de0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
22df0 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 2c to be reindexed,
22e00 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 or NULL */. ch
22e10 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 ar *z;
22e20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
22e30 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20 e of a table or
22e40 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74 index */. const
22e50 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 char *zDb;
22e60 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
22e70 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a f the database *
22e80 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b /. Table *pTab;
22e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22ea0 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 /* A table in th
22eb0 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
22ec0 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 Index *pIndex;
22ed0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
22ee0 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 n index associat
22ef0 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a ed with pTab */.
22f00 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
22f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
22f20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e The database in
22f30 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 dex number */.
22f40 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
22f50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 arse->db; /* T
22f60 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
22f70 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 ection */. Toke
22f80 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 20 n *pObjName;
22f90 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
22fa0 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 of the table or
22fb0 69 6e 64 65 78 20 74 6f 20 62 65 20 72 65 69 6e index to be rein
22fc0 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 dexed */.. /* R
22fd0 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 ead the database
22fe0 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 schema. If an e
22ff0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 rror occurs, lea
23000 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ve an error mess
23010 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 age. ** and cod
23020 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 e in pParse and
23030 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a return NULL. */.
23040 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
23050 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 =sqlite3ReadSche
23060 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 ma(pParse) ){.
23070 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 return;. }..
23080 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 if( pName1==0 )
23090 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 44 61 74 {. reindexDat
230a0 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 30 abases(pParse, 0
230b0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
230c0 20 7d 65 6c 73 65 20 69 66 28 20 4e 45 56 45 52 }else if( NEVER
230d0 28 70 4e 61 6d 65 32 3d 3d 30 29 20 7c 7c 20 70 (pName2==0) || p
230e0 4e 61 6d 65 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 Name2->z==0 ){.
230f0 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a char *zColl;.
23100 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d assert( pNam
23110 65 31 2d 3e 7a 20 29 3b 0a 20 20 20 20 7a 43 6f e1->z );. zCo
23120 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 ll = sqlite3Name
23130 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 FromToken(pParse
23140 2d 3e 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 ->db, pName1);.
23150 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20 if( !zColl )
23160 72 65 74 75 72 6e 3b 0a 20 20 20 20 70 43 6f 6c return;. pCol
23170 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 l = sqlite3FindC
23180 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 ollSeq(db, ENC(d
23190 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29 3b 0a 20 b), zColl, 0);.
231a0 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a if( pColl ){.
231b0 20 20 20 20 20 20 72 65 69 6e 64 65 78 44 61 74 reindexDat
231c0 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 7a abases(pParse, z
231d0 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c Coll);. sql
231e0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
231f0 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 72 65 74 Coll);. ret
23200 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 urn;. }. s
23210 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
23220 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 zColl);. }. i
23230 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 Db = sqlite3TwoP
23240 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 artName(pParse,
23250 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 pName1, pName2,
23260 26 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 &pObjName);. if
23270 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e ( iDb<0 ) return
23280 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e ;. z = sqlite3N
23290 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
232a0 20 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 pObjName);. if
232b0 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( z==0 ) return;
232c0 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 . zDb = db->aDb
232d0 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70 [iDb].zName;. p
232e0 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e Tab = sqlite3Fin
232f0 64 54 61 62 6c 65 28 64 62 2c 20 7a 2c 20 7a 44 dTable(db, z, zD
23300 62 29 3b 0a 20 20 69 66 28 20 70 54 61 62 20 29 b);. if( pTab )
23310 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 {. reindexTab
23320 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c le(pParse, pTab,
23330 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
23340 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 DbFree(db, z);.
23350 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
23360 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 pIndex = sqlite
23370 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 3FindIndex(db, z
23380 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 , zDb);. sqlite
23390 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 3DbFree(db, z);.
233a0 20 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a if( pIndex ){.
233b0 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e sqlite3Begin
233c0 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 WriteOperation(p
233d0 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a Parse, 0, iDb);.
233e0 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c sqlite3Refil
233f0 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 lIndex(pParse, p
23400 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 Index, -1);.
23410 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 return;. }. sq
23420 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
23430 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f arse, "unable to
23440 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 6f 62 identify the ob
23450 6a 65 63 74 20 74 6f 20 62 65 20 72 65 69 6e 64 ject to be reind
23460 65 78 65 64 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 exed");.}.#endif
23470 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
23480 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
23490 72 65 20 74 68 61 74 20 69 73 20 61 70 70 72 6f re that is appro
234a0 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 priate for the g
234b0 69 76 65 6e 20 49 6e 64 65 78 2e 0a 2a 2a 0a 2a iven Index..**.*
234c0 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 * The KeyInfo st
234d0 72 75 63 74 75 72 65 20 66 6f 72 20 61 6e 20 69 ructure for an i
234e0 6e 64 65 78 20 69 73 20 63 61 63 68 65 64 20 69 ndex is cached i
234f0 6e 20 74 68 65 20 49 6e 64 65 78 20 6f 62 6a 65 n the Index obje
23500 63 74 2e 0a 2a 2a 20 53 6f 20 74 68 65 72 65 20 ct..** So there
23510 6d 69 67 68 74 20 62 65 20 6d 75 6c 74 69 70 6c might be multipl
23520 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 e references to
23530 74 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 the returned poi
23540 6e 74 65 72 2e 20 20 54 68 65 0a 2a 2a 20 63 61 nter. The.** ca
23550 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ller should not
23560 74 72 79 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 try to modify th
23570 65 20 4b 65 79 49 6e 66 6f 20 6f 62 6a 65 63 74 e KeyInfo object
23580 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
23590 65 72 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 er should invoke
235a0 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f 55 sqlite3KeyInfoU
235b0 6e 72 65 66 28 29 20 6f 6e 20 74 68 65 20 72 65 nref() on the re
235c0 74 75 72 6e 65 64 20 6f 62 6a 65 63 74 0a 2a 2a turned object.**
235d0 20 77 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e when it has fin
235e0 69 73 68 65 64 20 75 73 69 6e 67 20 69 74 2e 0a ished using it..
235f0 2a 2f 0a 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 */.KeyInfo *sqli
23600 74 65 33 4b 65 79 49 6e 66 6f 4f 66 49 6e 64 65 te3KeyInfoOfInde
23610 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c x(Parse *pParse,
23620 20 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 Index *pIdx){.
23630 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 int i;. int nC
23640 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 ol = pIdx->nColu
23650 6d 6e 3b 0a 20 20 69 6e 74 20 6e 4b 65 79 20 3d mn;. int nKey =
23660 20 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 3b 0a pIdx->nKeyCol;.
23670 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b KeyInfo *pKey;
23680 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e . if( pParse->n
23690 45 72 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a Err ) return 0;.
236a0 20 20 69 66 28 20 70 49 64 78 2d 3e 75 6e 69 71 if( pIdx->uniq
236b0 4e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 NotNull ){. p
236c0 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4b 65 79 Key = sqlite3Key
236d0 49 6e 66 6f 41 6c 6c 6f 63 28 70 50 61 72 73 65 InfoAlloc(pParse
236e0 2d 3e 64 62 2c 20 6e 4b 65 79 2c 20 6e 43 6f 6c ->db, nKey, nCol
236f0 2d 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 7b -nKey);. }else{
23700 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 . pKey = sqli
23710 74 65 33 4b 65 79 49 6e 66 6f 41 6c 6c 6f 63 28 te3KeyInfoAlloc(
23720 70 50 61 72 73 65 2d 3e 64 62 2c 20 6e 43 6f 6c pParse->db, nCol
23730 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 , 0);. }. if(
23740 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 73 65 pKey ){. asse
23750 72 74 28 20 73 71 6c 69 74 65 33 4b 65 79 49 6e rt( sqlite3KeyIn
23760 66 6f 49 73 57 72 69 74 65 61 62 6c 65 28 70 4b foIsWriteable(pK
23770 65 79 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 ey) );. for(i
23780 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 =0; i<nCol; i++)
23790 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 {. char *zC
237a0 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f oll = pIdx->azCo
237b0 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 ll[i];. ass
237c0 65 72 74 28 20 7a 43 6f 6c 6c 21 3d 30 20 29 3b ert( zColl!=0 );
237d0 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f . pKey->aCo
237e0 6c 6c 5b 69 5d 20 3d 20 73 74 72 63 6d 70 28 7a ll[i] = strcmp(z
237f0 43 6f 6c 6c 2c 22 42 49 4e 41 52 59 22 29 3d 3d Coll,"BINARY")==
23800 30 20 3f 20 30 20 3a 0a 20 20 20 20 20 20 20 20 0 ? 0 :.
23810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23820 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c sqlite3LocateCol
23830 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f lSeq(pParse, zCo
23840 6c 6c 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d ll);. pKey-
23850 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d >aSortOrder[i] =
23860 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 pIdx->aSortOrde
23870 72 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 r[i];. }.
23880 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 if( pParse->nErr
23890 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
238a0 33 4b 65 79 49 6e 66 6f 55 6e 72 65 66 28 70 4b 3KeyInfoUnref(pK
238b0 65 79 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 20 ey);. pKey
238c0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 0;. }. }.
238d0 20 72 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a return pKey;.}.
238e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
238f0 4f 4d 49 54 5f 43 54 45 0a 2f 2a 20 0a 2a 2a 20 OMIT_CTE./* .**
23900 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
23910 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 70 65 72 invoked once per
23920 20 43 54 45 20 62 79 20 74 68 65 20 70 61 72 73 CTE by the pars
23930 65 72 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 er while parsing
23940 20 61 20 0a 2a 2a 20 57 49 54 48 20 63 6c 61 75 a .** WITH clau
23950 73 65 2e 20 0a 2a 2f 0a 57 69 74 68 20 2a 73 71 se. .*/.With *sq
23960 6c 69 74 65 33 57 69 74 68 41 64 64 28 0a 20 20 lite3WithAdd(.
23970 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
23980 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
23990 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
239a0 57 69 74 68 20 2a 70 57 69 74 68 2c 20 20 20 20 With *pWith,
239b0 20 20 20 20 20 20 20 20 2f 2a 20 45 78 69 73 74 /* Exist
239c0 69 6e 67 20 57 49 54 48 20 63 6c 61 75 73 65 2c ing WITH clause,
239d0 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f or NULL */. To
239e0 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 20 20 20 20 ken *pName,
239f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
23a00 20 74 68 65 20 63 6f 6d 6d 6f 6e 2d 74 61 62 6c the common-tabl
23a10 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 e */. ExprList
23a20 2a 70 41 72 67 6c 69 73 74 2c 20 20 20 20 20 2f *pArglist, /
23a30 2a 20 4f 70 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d * Optional colum
23a40 6e 20 6e 61 6d 65 20 6c 69 73 74 20 66 6f 72 20 n name list for
23a50 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 53 the table */. S
23a60 65 6c 65 63 74 20 2a 70 51 75 65 72 79 20 20 20 elect *pQuery
23a70 20 20 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20 /* Query
23a80 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 used to initiali
23a90 7a 65 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a ze the table */.
23aa0 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
23ab0 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
23ac0 20 57 69 74 68 20 2a 70 4e 65 77 3b 0a 20 20 63 With *pNew;. c
23ad0 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 0a 20 20 2f har *zName;.. /
23ae0 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
23af0 20 43 54 45 20 6e 61 6d 65 20 69 73 20 75 6e 69 CTE name is uni
23b00 71 75 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 que within this
23b10 57 49 54 48 20 63 6c 61 75 73 65 2e 20 49 66 0a WITH clause. If.
23b20 20 20 2a 2a 20 6e 6f 74 2c 20 73 74 6f 72 65 20 ** not, store
23b30 61 6e 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20 an error in the
23b40 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 2e Parse structure.
23b50 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 */. zName = sq
23b60 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
23b70 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 en(pParse->db, p
23b80 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 Name);. if( zNa
23b90 6d 65 20 26 26 20 70 57 69 74 68 20 29 7b 0a 20 me && pWith ){.
23ba0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
23bb0 72 28 69 3d 30 3b 20 69 3c 70 57 69 74 68 2d 3e r(i=0; i<pWith->
23bc0 6e 43 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCte; i++){.
23bd0 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 if( sqlite3Str
23be0 49 43 6d 70 28 7a 4e 61 6d 65 2c 20 70 57 69 74 ICmp(zName, pWit
23bf0 68 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d h->a[i].zName)==
23c00 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 0 ){. sql
23c10 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
23c20 72 73 65 2c 20 22 64 75 70 6c 69 63 61 74 65 20 rse, "duplicate
23c30 57 49 54 48 20 74 61 62 6c 65 20 6e 61 6d 65 3a WITH table name:
23c40 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 %s", zName);.
23c50 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
23c60 0a 20 20 69 66 28 20 70 57 69 74 68 20 29 7b 0a . if( pWith ){.
23c70 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 int nByte =
23c80 73 69 7a 65 6f 66 28 2a 70 57 69 74 68 29 20 2b sizeof(*pWith) +
23c90 20 28 73 69 7a 65 6f 66 28 70 57 69 74 68 2d 3e (sizeof(pWith->
23ca0 61 5b 31 5d 29 20 2a 20 70 57 69 74 68 2d 3e 6e a[1]) * pWith->n
23cb0 43 74 65 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d Cte);. pNew =
23cc0 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
23cd0 63 28 64 62 2c 20 70 57 69 74 68 2c 20 6e 42 79 c(db, pWith, nBy
23ce0 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 te);. }else{.
23cf0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
23d00 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
23d10 20 73 69 7a 65 6f 66 28 2a 70 57 69 74 68 29 29 sizeof(*pWith))
23d20 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
23d30 7a 4e 61 6d 65 21 3d 30 20 7c 7c 20 70 4e 65 77 zName!=0 || pNew
23d40 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
23d50 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
23d60 64 3d 3d 30 20 7c 7c 20 70 4e 65 77 3d 3d 30 20 d==0 || pNew==0
23d70 29 3b 0a 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d );.. if( pNew==
23d80 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
23d90 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
23da0 62 2c 20 70 41 72 67 6c 69 73 74 29 3b 0a 20 20 b, pArglist);.
23db0 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
23dc0 65 6c 65 74 65 28 64 62 2c 20 70 51 75 65 72 79 elete(db, pQuery
23dd0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
23de0 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b Free(db, zName);
23df0 0a 20 20 20 20 70 4e 65 77 20 3d 20 70 57 69 74 . pNew = pWit
23e00 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 h;. }else{.
23e10 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 43 pNew->a[pNew->nC
23e20 74 65 5d 2e 70 53 65 6c 65 63 74 20 3d 20 70 51 te].pSelect = pQ
23e30 75 65 72 79 3b 0a 20 20 20 20 70 4e 65 77 2d 3e uery;. pNew->
23e40 61 5b 70 4e 65 77 2d 3e 6e 43 74 65 5d 2e 70 43 a[pNew->nCte].pC
23e50 6f 6c 73 20 3d 20 70 41 72 67 6c 69 73 74 3b 0a ols = pArglist;.
23e60 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 pNew->a[pNew
23e70 2d 3e 6e 43 74 65 5d 2e 7a 4e 61 6d 65 20 3d 20 ->nCte].zName =
23e80 7a 4e 61 6d 65 3b 0a 20 20 20 20 70 4e 65 77 2d zName;. pNew-
23e90 3e 61 5b 70 4e 65 77 2d 3e 6e 43 74 65 5d 2e 7a >a[pNew->nCte].z
23ea0 43 74 65 45 72 72 20 3d 20 30 3b 0a 20 20 20 20 CteErr = 0;.
23eb0 70 4e 65 77 2d 3e 6e 43 74 65 2b 2b 3b 0a 20 20 pNew->nCte++;.
23ec0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }.. return pNew
23ed0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
23ee0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
23ef0 74 68 65 20 57 69 74 68 20 6f 62 6a 65 63 74 20 the With object
23f00 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
23f10 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a cond argument..*
23f20 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 57 69 /.void sqlite3Wi
23f30 74 68 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 thDelete(sqlite3
23f40 20 2a 64 62 2c 20 57 69 74 68 20 2a 70 57 69 74 *db, With *pWit
23f50 68 29 7b 0a 20 20 69 66 28 20 70 57 69 74 68 20 h){. if( pWith
23f60 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
23f70 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 57 69 for(i=0; i<pWi
23f80 74 68 2d 3e 6e 43 74 65 3b 20 69 2b 2b 29 7b 0a th->nCte; i++){.
23f90 20 20 20 20 20 20 73 74 72 75 63 74 20 43 74 65 struct Cte
23fa0 20 2a 70 43 74 65 20 3d 20 26 70 57 69 74 68 2d *pCte = &pWith-
23fb0 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 73 71 6c >a[i];. sql
23fc0 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 ite3ExprListDele
23fd0 74 65 28 64 62 2c 20 70 43 74 65 2d 3e 70 43 6f te(db, pCte->pCo
23fe0 6c 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ls);. sqlit
23ff0 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 e3SelectDelete(d
24000 62 2c 20 70 43 74 65 2d 3e 70 53 65 6c 65 63 74 b, pCte->pSelect
24010 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
24020 44 62 46 72 65 65 28 64 62 2c 20 70 43 74 65 2d DbFree(db, pCte-
24030 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 >zName);. }.
24040 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
24050 28 64 62 2c 20 70 57 69 74 68 29 3b 0a 20 20 7d (db, pWith);. }
24060 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 .}.#endif /* !de
24070 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
24080 54 5f 43 54 45 29 20 2a 2f 0a T_CTE) */.