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 23 69 66 6e 64 65 66 20 53 51 t.h"..#ifndef SQ
02f0: 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
0300: 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65 _CACHE./*.** The
0310: 20 54 61 62 6c 65 4c 6f 63 6b 20 73 74 72 75 63 TableLock struc
0320: 74 75 72 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 ture is only use
0330: 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 d by the sqlite3
0340: 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64 0a TableLock() and.
0350: 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b ** codeTableLock
0360: 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a s() functions..*
0370: 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 4c 6f /.struct TableLo
0380: 63 6b 20 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 ck {. int iDb;
0390: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
03a0: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
03b0: 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 ntaining the tab
03c0: 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 le to be locked
03d0: 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 3b 20 20 */. int iTab;
03e0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
03f0: 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
0400: 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 the table to be
0410: 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 locked */. u8 i
0420: 73 57 72 69 74 65 4c 6f 63 6b 3b 20 20 20 20 20 sWriteLock;
0430: 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 77 /* True for w
0440: 72 69 74 65 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 rite lock. Fals
0450: 65 20 66 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 e for a read loc
0460: 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 k */. const cha
0470: 72 20 2a 7a 4c 6f 63 6b 4e 61 6d 65 3b 20 2f 2a r *zLockName; /*
0480: 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 Name of the tab
0490: 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 le */.};../*.**
04a0: 52 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 Record the fact
04b0: 74 68 61 74 20 77 65 20 77 61 6e 74 20 74 6f 20 that we want to
04c0: 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20 61 74 20 lock a table at
04d0: 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a 2a 0a 2a run-time. .**.*
04e0: 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 * The table to b
04f0: 65 20 6c 6f 63 6b 65 64 20 68 61 73 20 72 6f 6f e locked has roo
0500: 74 20 70 61 67 65 20 69 54 61 62 20 61 6e 64 20 t page iTab and
0510: 69 73 20 66 6f 75 6e 64 20 69 6e 20 64 61 74 61 is found in data
0520: 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 20 72 base iDb..** A r
0530: 65 61 64 20 6f 72 20 61 20 77 72 69 74 65 20 6c ead or a write l
0540: 6f 63 6b 20 63 61 6e 20 62 65 20 74 61 6b 65 6e ock can be taken
0550: 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 69 73 depending on is
0560: 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a Writelock..**.**
0570: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 This routine ju
0580: 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 66 st records the f
0590: 61 63 74 20 74 68 61 74 20 74 68 65 20 6c 6f 63 act that the loc
05a0: 6b 20 69 73 20 64 65 73 69 72 65 64 2e 20 20 54 k is desired. T
05b0: 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 6d 61 he.** code to ma
05c0: 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f 63 63 75 ke the lock occu
05d0: 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 r is generated b
05e0: 79 20 61 20 6c 61 74 65 72 20 63 61 6c 6c 20 74 y a later call t
05f0: 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f o.** codeTableLo
0600: 63 6b 73 28 29 20 77 68 69 63 68 20 6f 63 63 75 cks() which occu
0610: 72 73 20 64 75 72 69 6e 67 20 73 71 6c 69 74 65 rs during sqlite
0620: 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e 3FinishCoding().
0630: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
0640: 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50 61 72 TableLock(. Par
0650: 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
0660: 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
0670: 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c xt */. int iDb,
0680: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
0690: 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 dex of the datab
06a0: 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ase containing t
06b0: 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b he table to lock
06c0: 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 */. int iTab,
06d0: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
06e0: 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
06f0: 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 the table to be
0700: 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 locked */. u8 i
0710: 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20 20 2f sWriteLock, /
0720: 2a 20 54 72 75 65 20 66 6f 72 20 61 20 77 72 69 * True for a wri
0730: 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e te lock */. con
0740: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 st char *zName
0750: 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 /* Name of the t
0760: 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 able to be locke
0770: 64 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 d */.){. Parse
0780: 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c *pToplevel = sql
0790: 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 ite3ParseTopleve
07a0: 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 l(pParse);. int
07b0: 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 i;. int nBytes
07c0: 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 ;. TableLock *p
07d0: 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e ;. assert( iDb>
07e0: 3d 30 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 =0 );..#ifdef SQ
07f0: 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4e 43 55 52 LITE_OMIT_CONCUR
0800: 52 45 4e 54 0a 20 20 69 66 28 20 69 44 62 3d 3d RENT. if( iDb==
0810: 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 1 ) return;. if
0820: 28 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 53 ( !sqlite3BtreeS
0830: 68 61 72 61 62 6c 65 28 70 50 61 72 73 65 2d 3e harable(pParse->
0840: 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 db->aDb[iDb].pBt
0850: 29 20 29 20 72 65 74 75 72 6e 3b 0a 23 65 6e 64 ) ) return;.#end
0860: 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c if. for(i=0; i<
0870: 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c pToplevel->nTabl
0880: 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 eLock; i++){.
0890: 20 70 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c 2d p = &pToplevel-
08a0: 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a >aTableLock[i];.
08b0: 20 20 20 20 69 66 28 20 70 2d 3e 69 44 62 3d 3d if( p->iDb==
08c0: 69 44 62 20 26 26 20 70 2d 3e 69 54 61 62 3d 3d iDb && p->iTab==
08d0: 69 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70 2d iTab ){. p-
08e0: 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 >isWriteLock = (
08f0: 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c p->isWriteLock |
0900: 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a | isWriteLock);.
0910: 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
0920: 20 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65 }. }.. nByte
0930: 73 20 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 s = sizeof(Table
0940: 4c 6f 63 6b 29 20 2a 20 28 70 54 6f 70 6c 65 76 Lock) * (pToplev
0950: 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31 el->nTableLock+1
0960: 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e );. pToplevel->
0970: 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 0a 20 20 20 aTableLock =.
0980: 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c sqlite3DbReal
0990: 6c 6f 63 4f 72 46 72 65 65 28 70 54 6f 70 6c 65 locOrFree(pTople
09a0: 76 65 6c 2d 3e 64 62 2c 20 70 54 6f 70 6c 65 76 vel->db, pToplev
09b0: 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 el->aTableLock,
09c0: 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 nBytes);. if( p
09d0: 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 Toplevel->aTable
09e0: 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d 20 Lock ){. p =
09f0: 26 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 &pToplevel->aTab
0a00: 6c 65 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65 6c leLock[pToplevel
0a10: 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b ->nTableLock++];
0a20: 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d 20 69 44 . p->iDb = iD
0a30: 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 20 3d b;. p->iTab =
0a40: 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 73 iTab;. p->is
0a50: 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69 73 57 72 WriteLock = isWr
0a60: 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 70 2d 3e iteLock;. p->
0a70: 7a 4c 6f 63 6b 4e 61 6d 65 20 3d 20 7a 4e 61 6d zLockName = zNam
0a80: 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
0a90: 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c pToplevel->nTabl
0aa0: 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 73 eLock = 0;. s
0ab0: 71 6c 69 74 65 33 4f 6f 6d 46 61 75 6c 74 28 70 qlite3OomFault(p
0ac0: 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 29 3b 0a 20 Toplevel->db);.
0ad0: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 }.}../*.** Code
0ae0: 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b an OP_TableLock
0af0: 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 instruction for
0b00: 20 65 61 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b each table lock
0b10: 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 ed by the.** sta
0b20: 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 tement (configur
0b30: 65 64 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 ed by calls to s
0b40: 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 qlite3TableLock(
0b50: 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ))..*/.static vo
0b60: 69 64 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b id codeTableLock
0b70: 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 s(Parse *pParse)
0b80: 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 {. int i;. Vdb
0b90: 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20 20 70 56 e *pVdbe; .. pV
0ba0: 64 62 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 dbe = sqlite3Get
0bb0: 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
0bc0: 61 73 73 65 72 74 28 20 70 56 64 62 65 21 3d 30 assert( pVdbe!=0
0bd0: 20 29 3b 20 2f 2a 20 73 71 6c 69 74 65 33 47 65 ); /* sqlite3Ge
0be0: 74 56 64 62 65 20 63 61 6e 6e 6f 74 20 66 61 69 tVdbe cannot fai
0bf0: 6c 3a 20 56 44 42 45 20 61 6c 72 65 61 64 79 20 l: VDBE already
0c00: 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 allocated */..
0c10: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 for(i=0; i<pPars
0c20: 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 e->nTableLock; i
0c30: 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f ++){. TableLo
0c40: 63 6b 20 2a 70 20 3d 20 26 70 50 61 72 73 65 2d ck *p = &pParse-
0c50: 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a >aTableLock[i];.
0c60: 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e int p1 = p->
0c70: 69 44 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 iDb;. sqlite3
0c80: 56 64 62 65 41 64 64 4f 70 34 28 70 56 64 62 65 VdbeAddOp4(pVdbe
0c90: 2c 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 , OP_TableLock,
0ca0: 70 31 2c 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e p1, p->iTab, p->
0cb0: 69 73 57 72 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 isWriteLock,.
0cc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0cd0: 20 20 20 70 2d 3e 7a 4c 6f 63 6b 4e 61 6d 65 2c p->zLockName,
0ce0: 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d P4_STATIC);. }
0cf0: 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 .}.#else. #defi
0d00: 6e 65 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b ne codeTableLock
0d10: 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a s(x).#endif../*.
0d20: 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
0d30: 66 20 74 68 65 20 67 69 76 65 6e 20 79 44 62 4d f the given yDbM
0d40: 61 73 6b 20 6f 62 6a 65 63 74 20 69 73 20 65 6d ask object is em
0d50: 70 74 79 20 2d 20 69 66 20 69 74 20 63 6f 6e 74 pty - if it cont
0d60: 61 69 6e 73 20 6e 6f 0a 2a 2a 20 31 20 62 69 74 ains no.** 1 bit
0d70: 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 s. This routine
0d80: 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
0d90: 44 62 4d 61 73 6b 41 6c 6c 5a 65 72 6f 28 29 20 DbMaskAllZero()
0da0: 61 6e 64 20 44 62 4d 61 73 6b 4e 6f 74 5a 65 72 and DbMaskNotZer
0db0: 6f 28 29 0a 2a 2a 20 6d 61 63 72 6f 73 20 77 68 o().** macros wh
0dc0: 65 6e 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 en SQLITE_MAX_AT
0dd0: 54 41 43 48 45 44 20 69 73 20 67 72 65 61 74 65 TACHED is greate
0de0: 72 20 74 68 61 6e 20 33 30 2e 0a 2a 2f 0a 23 69 r than 30..*/.#i
0df0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 f SQLITE_MAX_ATT
0e00: 41 43 48 45 44 3e 33 30 0a 69 6e 74 20 73 71 6c ACHED>30.int sql
0e10: 69 74 65 33 44 62 4d 61 73 6b 41 6c 6c 5a 65 72 ite3DbMaskAllZer
0e20: 6f 28 79 44 62 4d 61 73 6b 20 6d 29 7b 0a 20 20 o(yDbMask m){.
0e30: 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
0e40: 3b 20 69 3c 73 69 7a 65 6f 66 28 79 44 62 4d 61 ; i<sizeof(yDbMa
0e50: 73 6b 29 3b 20 69 2b 2b 29 20 69 66 28 20 6d 5b sk); i++) if( m[
0e60: 69 5d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 i] ) return 0;.
0e70: 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e return 1;.}.#en
0e80: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
0e90: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
0ea0: 64 20 61 66 74 65 72 20 61 20 73 69 6e 67 6c 65 d after a single
0eb0: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 68 SQL statement h
0ec0: 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65 as been.** parse
0ed0: 64 20 61 6e 64 20 61 20 56 44 42 45 20 70 72 6f d and a VDBE pro
0ee0: 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65 20 gram to execute
0ef0: 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 68 that statement h
0f00: 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61 as been.** prepa
0f10: 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 red. This routi
0f20: 6e 65 20 70 75 74 73 20 74 68 65 20 66 69 6e 69 ne puts the fini
0f30: 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e shing touches on
0f40: 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f the.** VDBE pro
0f50: 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74 73 20 gram and resets
0f60: 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 the pParse struc
0f70: 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e 65 78 ture for the nex
0f80: 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a t.** parse..**.*
0f90: 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 61 * Note that if a
0fa0: 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 n error occurred
0fb0: 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 68 , it might be th
0fc0: 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e e case that.** n
0fd0: 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61 73 20 o VDBE code was
0fe0: 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 76 6f generated..*/.vo
0ff0: 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 id sqlite3Finish
1000: 43 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70 50 Coding(Parse *pP
1010: 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 arse){. sqlite3
1020: 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b *db;. Vdbe *v;
1030: 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 .. assert( pPar
1040: 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 3d 3d 30 se->pToplevel==0
1050: 20 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 );. db = pPars
1060: 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 e->db;. if( pPa
1070: 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 rse->nested ) re
1080: 74 75 72 6e 3b 0a 20 20 69 66 28 20 64 62 2d 3e turn;. if( db->
1090: 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 mallocFailed ||
10a0: 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a pParse->nErr ){.
10b0: 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e if( pParse->
10c0: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc==SQLITE_OK )
10d0: 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c pParse->rc = SQL
10e0: 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 ITE_ERROR;. r
10f0: 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a eturn;. }.. /*
1100: 20 42 65 67 69 6e 20 62 79 20 67 65 6e 65 72 61 Begin by genera
1110: 74 69 6e 67 20 73 6f 6d 65 20 74 65 72 6d 69 6e ting some termin
1120: 61 74 69 6f 6e 20 63 6f 64 65 20 61 74 20 74 68 ation code at th
1130: 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a e end of the. *
1140: 2a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 0a 20 * vdbe program.
1150: 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */. v = sqlite
1160: 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
1170: 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 ;. assert( !pPa
1180: 72 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 rse->isMultiWrit
1190: 65 20 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c e . || sql
11a0: 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d 61 ite3VdbeAssertMa
11b0: 79 41 62 6f 72 74 28 76 2c 20 70 50 61 72 73 65 yAbort(v, pParse
11c0: 2d 3e 6d 61 79 41 62 6f 72 74 29 29 3b 0a 20 20 ->mayAbort));.
11d0: 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c if( v ){. sql
11e0: 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 ite3VdbeAddOp0(v
11f0: 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a 0a 23 69 66 , OP_Halt);..#if
1200: 20 53 51 4c 49 54 45 5f 55 53 45 52 5f 41 55 54 SQLITE_USER_AUT
1210: 48 45 4e 54 49 43 41 54 49 4f 4e 0a 20 20 20 20 HENTICATION.
1220: 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 if( pParse->nTab
1230: 6c 65 4c 6f 63 6b 3e 30 20 26 26 20 64 62 2d 3e leLock>0 && db->
1240: 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a init.busy==0 ){.
1250: 20 20 20 20 20 20 73 71 6c 69 74 65 33 55 73 65 sqlite3Use
1260: 72 41 75 74 68 49 6e 69 74 28 64 62 29 3b 0a 20 rAuthInit(db);.
1270: 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 75 74 if( db->aut
1280: 68 2e 61 75 74 68 4c 65 76 65 6c 3c 55 41 55 54 h.authLevel<UAUT
1290: 48 5f 55 73 65 72 20 29 7b 0a 20 20 20 20 20 20 H_User ){.
12a0: 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
12b0: 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 72 20 g(pParse, "user
12c0: 6e 6f 74 20 61 75 74 68 65 6e 74 69 63 61 74 65 not authenticate
12d0: 64 22 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 d");. pPa
12e0: 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 rse->rc = SQLITE
12f0: 5f 41 55 54 48 5f 55 53 45 52 3b 0a 20 20 20 20 _AUTH_USER;.
1300: 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
1310: 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
1320: 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6f .. /* The coo
1330: 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e kie mask contain
1340: 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 s one bit for ea
1350: 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ch database file
1360: 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 28 42 open.. ** (B
1370: 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61 69 6e it 0 is for main
1380: 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72 20 74 , bit 1 is for t
1390: 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 emp, and so fort
13a0: 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a 20 20 h.) Bits are.
13b0: 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65 61 63 ** set for eac
13c0: 68 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 h database that
13d0: 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65 72 61 is used. Genera
13e0: 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61 72 74 te code to start
13f0: 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 a. ** transa
1400: 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20 75 73 ction on each us
1410: 65 64 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 ed database and
1420: 74 6f 20 76 65 72 69 66 79 20 74 68 65 20 73 63 to verify the sc
1430: 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 20 20 hema cookie.
1440: 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20 ** on each used
1450: 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f database.. */
1460: 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c . if( db->mal
1470: 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 0a 20 20 locFailed==0 .
1480: 20 20 20 26 26 20 28 44 62 4d 61 73 6b 4e 6f 6e && (DbMaskNon
1490: 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 63 6f 6f Zero(pParse->coo
14a0: 6b 69 65 4d 61 73 6b 29 20 7c 7c 20 70 50 61 72 kieMask) || pPar
14b0: 73 65 2d 3e 70 43 6f 6e 73 74 45 78 70 72 29 0a se->pConstExpr).
14c0: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ){. int
14d0: 20 69 44 62 2c 20 69 3b 0a 20 20 20 20 20 20 61 iDb, i;. a
14e0: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 ssert( sqlite3Vd
14f0: 62 65 47 65 74 4f 70 28 76 2c 20 30 29 2d 3e 6f beGetOp(v, 0)->o
1500: 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 69 74 20 29 pcode==OP_Init )
1510: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1520: 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 30 dbeJumpHere(v, 0
1530: 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 44 62 );. for(iDb
1540: 3d 30 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b =0; iDb<db->nDb;
1550: 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20 20 iDb++){.
1560: 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
1570: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 44 62 ;. if( Db
1580: 4d 61 73 6b 54 65 73 74 28 70 50 61 72 73 65 2d MaskTest(pParse-
1590: 3e 63 6f 6f 6b 69 65 4d 61 73 6b 2c 20 69 44 62 >cookieMask, iDb
15a0: 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b )==0 ) continue;
15b0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
15c0: 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c VdbeUsesBtree(v,
15d0: 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 70 iDb);. p
15e0: 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 Schema = db->aDb
15f0: 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 [iDb].pSchema;.
1600: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1610: 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 0a 20 beAddOp4Int(v,.
1620: 20 20 20 20 20 20 20 20 20 4f 50 5f 54 72 61 6e OP_Tran
1630: 73 61 63 74 69 6f 6e 2c 20 20 20 20 20 20 20 20 saction,
1640: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1650: 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 20 pcode */.
1660: 20 20 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 iDb,
1670: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1680: 20 20 20 20 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 /* P1 */.
1690: 20 20 20 20 20 20 20 20 20 44 62 4d 61 73 6b 54 DbMaskT
16a0: 65 73 74 28 70 50 61 72 73 65 2d 3e 77 72 69 74 est(pParse->writ
16b0: 65 4d 61 73 6b 2c 69 44 62 29 2c 20 2f 2a 20 50 eMask,iDb), /* P
16c0: 32 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 2 */. p
16d0: 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 Schema->schema_c
16e0: 6f 6f 6b 69 65 2c 20 20 20 20 20 20 20 20 20 20 ookie,
16f0: 20 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 /* P3 */.
1700: 20 20 20 20 20 70 53 63 68 65 6d 61 2d 3e 69 47 pSchema->iG
1710: 65 6e 65 72 61 74 69 6f 6e 20 20 20 20 20 20 20 eneration
1720: 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f /* P4 */
1730: 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 . );.
1740: 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 if( db->init
1750: 2e 62 75 73 79 3d 3d 30 20 29 20 73 71 6c 69 74 .busy==0 ) sqlit
1760: 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 e3VdbeChangeP5(v
1770: 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 56 64 , 1);. Vd
1780: 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 0a 20 20 beComment((v,.
1790: 20 20 20 20 20 20 20 20 20 20 20 20 22 75 73 65 "use
17a0: 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3d 25 64 22 sStmtJournal=%d"
17b0: 2c 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f , pParse->mayAbo
17c0: 72 74 20 26 26 20 70 50 61 72 73 65 2d 3e 69 73 rt && pParse->is
17d0: 4d 75 6c 74 69 57 72 69 74 65 29 29 3b 0a 20 20 MultiWrite));.
17e0: 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
17f0: 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1800: 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 66 6f 72 LTABLE. for
1810: 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e (i=0; i<pParse->
1820: 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b nVtabLock; i++){
1830: 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 76 . char *v
1840: 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29 73 71 tab = (char *)sq
1850: 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 lite3GetVTable(d
1860: 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 74 61 b, pParse->apVta
1870: 62 4c 6f 63 6b 5b 69 5d 29 3b 0a 20 20 20 20 20 bLock[i]);.
1880: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1890: 64 4f 70 34 28 76 2c 20 4f 50 5f 56 42 65 67 69 dOp4(v, OP_VBegi
18a0: 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 76 74 61 62 n, 0, 0, 0, vtab
18b0: 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 , P4_VTAB);.
18c0: 20 20 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65 }. pParse
18d0: 2d 3e 6e 56 74 61 62 4c 6f 63 6b 20 3d 20 30 3b ->nVtabLock = 0;
18e0: 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f .#endif.. /
18f0: 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 63 * Once all the c
1900: 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65 65 6e ookies have been
1910: 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 74 72 verified and tr
1920: 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 65 ansactions opene
1930: 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62 74 d, . ** obt
1940: 61 69 6e 20 74 68 65 20 72 65 71 75 69 72 65 64 ain the required
1950: 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54 68 table-locks. Th
1960: 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75 6e is is a no-op un
1970: 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20 20 20 less the .
1980: 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 ** shared-cache
1990: 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c feature is enabl
19a0: 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ed.. */.
19b0: 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b codeTableLock
19c0: 73 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 s(pParse);..
19d0: 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
19e0: 61 6e 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e any AUTOINCREMEN
19f0: 54 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 T data structure
1a00: 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 s required..
1a10: 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
1a20: 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 e3AutoincrementB
1a30: 65 67 69 6e 28 70 50 61 72 73 65 29 3b 0a 0a 20 egin(pParse);..
1a40: 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 63 6f 6e /* Code con
1a50: 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e stant expression
1a60: 73 20 74 68 61 74 20 77 68 65 72 65 20 66 61 63 s that where fac
1a70: 74 6f 72 65 64 20 6f 75 74 20 6f 66 20 69 6e 6e tored out of inn
1a80: 65 72 20 6c 6f 6f 70 73 20 2a 2f 0a 20 20 20 20 er loops */.
1a90: 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 43 if( pParse->pC
1aa0: 6f 6e 73 74 45 78 70 72 20 29 7b 0a 20 20 20 20 onstExpr ){.
1ab0: 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 ExprList *pE
1ac0: 4c 20 3d 20 70 50 61 72 73 65 2d 3e 70 43 6f 6e L = pParse->pCon
1ad0: 73 74 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 stExpr;.
1ae0: 70 50 61 72 73 65 2d 3e 6f 6b 43 6f 6e 73 74 46 pParse->okConstF
1af0: 61 63 74 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 actor = 0;.
1b00: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 for(i=0; i<pE
1b10: 4c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a L->nExpr; i++){.
1b20: 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1b30: 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 3ExprCode(pParse
1b40: 2c 20 70 45 4c 2d 3e 61 5b 69 5d 2e 70 45 78 70 , pEL->a[i].pExp
1b50: 72 2c 20 70 45 4c 2d 3e 61 5b 69 5d 2e 75 2e 69 r, pEL->a[i].u.i
1b60: 43 6f 6e 73 74 45 78 70 72 52 65 67 29 3b 0a 20 ConstExprReg);.
1b70: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1b80: 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c .. /* Final
1b90: 6c 79 2c 20 6a 75 6d 70 20 62 61 63 6b 20 74 6f ly, jump back to
1ba0: 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
1bb0: 66 20 74 68 65 20 65 78 65 63 75 74 61 62 6c 65 f the executable
1bc0: 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 20 20 code. */.
1bd0: 73 71 6c 69 74 65 33 56 64 62 65 47 6f 74 6f 28 sqlite3VdbeGoto(
1be0: 76 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d v, 1);. }. }
1bf0: 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 ... /* Get the
1c00: 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72 65 61 VDBE program rea
1c10: 64 79 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e dy for execution
1c20: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76 20 26 26 . */. if( v &&
1c30: 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 pParse->nErr==0
1c40: 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 && !db->mallocF
1c50: 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 ailed ){. /*
1c60: 41 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 6f 6e 65 A minimum of one
1c70: 20 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 69 cursor is requi
1c80: 72 65 64 20 69 66 20 61 75 74 6f 69 6e 63 72 65 red if autoincre
1c90: 6d 65 6e 74 20 69 73 20 75 73 65 64 0a 20 20 20 ment is used.
1ca0: 20 2a 20 20 53 65 65 20 74 69 63 6b 65 74 20 5b * See ticket [
1cb0: 61 36 39 36 33 37 39 63 31 66 30 38 38 36 36 5d a696379c1f08866]
1cc0: 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 */. if( pPar
1cd0: 73 65 2d 3e 70 41 69 6e 63 21 3d 30 20 26 26 20 se->pAinc!=0 &&
1ce0: 70 50 61 72 73 65 2d 3e 6e 54 61 62 3d 3d 30 20 pParse->nTab==0
1cf0: 29 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d ) pParse->nTab =
1d00: 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 1;. sqlite3V
1d10: 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 dbeMakeReady(v,
1d20: 70 50 61 72 73 65 29 3b 0a 20 20 20 20 70 50 61 pParse);. pPa
1d30: 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 rse->rc = SQLITE
1d40: 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a _DONE;. }else{.
1d50: 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d pParse->rc =
1d60: 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1d70: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 }.}../*.** Run
1d80: 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 the parser and c
1d90: 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 72 65 ode generator re
1da0: 63 75 72 73 69 76 65 6c 79 20 69 6e 20 6f 72 64 cursively in ord
1db0: 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a er to generate.*
1dc0: 2a 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 53 * code for the S
1dd0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 QL statement giv
1de0: 65 6e 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 en onto the end
1df0: 6f 66 20 74 68 65 20 70 50 61 72 73 65 20 63 6f of the pParse co
1e00: 6e 74 65 78 74 0a 2a 2a 20 63 75 72 72 65 6e 74 ntext.** current
1e10: 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 ly under constru
1e20: 63 74 69 6f 6e 2e 20 20 57 68 65 6e 20 74 68 65 ction. When the
1e30: 20 70 61 72 73 65 72 20 69 73 20 72 75 6e 20 72 parser is run r
1e40: 65 63 75 72 73 69 76 65 6c 79 0a 2a 2a 20 74 68 ecursively.** th
1e50: 69 73 20 77 61 79 2c 20 74 68 65 20 66 69 6e 61 is way, the fina
1e60: 6c 20 4f 50 5f 48 61 6c 74 20 69 73 20 6e 6f 74 l OP_Halt is not
1e70: 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 6f 74 appended and ot
1e80: 68 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 her initializati
1e90: 6f 6e 0a 2a 2a 20 61 6e 64 20 66 69 6e 61 6c 69 on.** and finali
1ea0: 7a 61 74 69 6f 6e 20 73 74 65 70 73 20 61 72 65 zation steps are
1eb0: 20 6f 6d 69 74 74 65 64 20 62 65 63 61 75 73 65 omitted because
1ec0: 20 74 68 6f 73 65 20 61 72 65 20 68 61 6e 64 6c those are handl
1ed0: 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20 6f 75 ing by the.** ou
1ee0: 74 65 72 6d 6f 73 74 20 70 61 72 73 65 72 2e 0a termost parser..
1ef0: 2a 2a 0a 2a 2a 20 4e 6f 74 20 65 76 65 72 79 74 **.** Not everyt
1f00: 68 69 6e 67 20 69 73 20 6e 65 73 74 61 62 6c 65 hing is nestable
1f10: 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 . This facility
1f20: 20 69 73 20 64 65 73 69 67 6e 65 64 20 74 6f 20 is designed to
1f30: 70 65 72 6d 69 74 0a 2a 2a 20 49 4e 53 45 52 54 permit.** INSERT
1f40: 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 20 44 45 , UPDATE, and DE
1f50: 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 73 20 LETE operations
1f60: 61 67 61 69 6e 73 74 20 53 51 4c 49 54 45 5f 4d against SQLITE_M
1f70: 41 53 54 45 52 2e 20 20 55 73 65 0a 2a 2a 20 63 ASTER. Use.** c
1f80: 61 72 65 20 69 66 20 79 6f 75 20 64 65 63 69 64 are if you decid
1f90: 65 20 74 6f 20 74 72 79 20 74 6f 20 75 73 65 20 e to try to use
1fa0: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 6f 72 this routine for
1fb0: 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 75 72 70 some other purp
1fc0: 6f 73 65 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 oses..*/.void sq
1fd0: 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
1fe0: 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1ff0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
2000: 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
2010: 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 list ap;. char
2020: 2a 7a 53 71 6c 3b 0a 20 20 63 68 61 72 20 2a 7a *zSql;. char *z
2030: 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71 ErrMsg = 0;. sq
2040: 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
2050: 73 65 2d 3e 64 62 3b 0a 20 20 63 68 61 72 20 73 se->db;. char s
2060: 61 76 65 42 75 66 5b 50 41 52 53 45 5f 54 41 49 aveBuf[PARSE_TAI
2070: 4c 5f 53 5a 5d 3b 0a 0a 20 20 69 66 28 20 70 50 L_SZ];.. if( pP
2080: 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 arse->nErr ) ret
2090: 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
20a0: 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3c 31 30 Parse->nested<10
20b0: 20 29 3b 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 ); /* Nesting
20c0: 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 6f should only be o
20d0: 66 20 6c 69 6d 69 74 65 64 20 64 65 70 74 68 20 f limited depth
20e0: 2a 2f 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 */. va_start(ap
20f0: 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 , zFormat);. zS
2100: 71 6c 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 ql = sqlite3VMPr
2110: 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 intf(db, zFormat
2120: 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
2130: 61 70 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d ap);. if( zSql=
2140: 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
2150: 3b 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 ; /* A malloc
2160: 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 must have failed
2170: 20 2a 2f 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 */. }. pParse
2180: 2d 3e 6e 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 ->nested++;. me
2190: 6d 63 70 79 28 73 61 76 65 42 75 66 2c 20 50 41 mcpy(saveBuf, PA
21a0: 52 53 45 5f 54 41 49 4c 28 70 50 61 72 73 65 29 RSE_TAIL(pParse)
21b0: 2c 20 50 41 52 53 45 5f 54 41 49 4c 5f 53 5a 29 , PARSE_TAIL_SZ)
21c0: 3b 0a 20 20 6d 65 6d 73 65 74 28 50 41 52 53 45 ;. memset(PARSE
21d0: 5f 54 41 49 4c 28 70 50 61 72 73 65 29 2c 20 30 _TAIL(pParse), 0
21e0: 2c 20 50 41 52 53 45 5f 54 41 49 4c 5f 53 5a 29 , PARSE_TAIL_SZ)
21f0: 3b 0a 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 ;. sqlite3RunPa
2200: 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71 rser(pParse, zSq
2210: 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 l, &zErrMsg);.
2220: 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
2230: 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71 , zErrMsg);. sq
2240: 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
2250: 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d 63 70 79 28 zSql);. memcpy(
2260: 50 41 52 53 45 5f 54 41 49 4c 28 70 50 61 72 73 PARSE_TAIL(pPars
2270: 65 29 2c 20 73 61 76 65 42 75 66 2c 20 50 41 52 e), saveBuf, PAR
2280: 53 45 5f 54 41 49 4c 5f 53 5a 29 3b 0a 20 20 70 SE_TAIL_SZ);. p
2290: 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b Parse->nested--;
22a0: 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 55 .}..#if SQLITE_U
22b0: 53 45 52 5f 41 55 54 48 45 4e 54 49 43 41 54 49 SER_AUTHENTICATI
22c0: 4f 4e 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 ON./*.** Return
22d0: 54 52 55 45 20 69 66 20 7a 54 61 62 6c 65 20 69 TRUE if zTable i
22e0: 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
22f0: 65 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 74 e system table t
2300: 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 0a 2a hat stores the.*
2310: 2a 20 6c 69 73 74 20 6f 66 20 75 73 65 72 73 20 * list of users
2320: 61 6e 64 20 74 68 65 69 72 20 61 63 63 65 73 73 and their access
2330: 20 63 72 65 64 65 6e 74 69 61 6c 73 2e 0a 2a 2f credentials..*/
2340: 0a 69 6e 74 20 73 71 6c 69 74 65 33 55 73 65 72 .int sqlite3User
2350: 41 75 74 68 54 61 62 6c 65 28 63 6f 6e 73 74 20 AuthTable(const
2360: 63 68 61 72 20 2a 7a 54 61 62 6c 65 29 7b 0a 20 char *zTable){.
2370: 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f return sqlite3_
2380: 73 74 72 69 63 6d 70 28 7a 54 61 62 6c 65 2c 20 stricmp(zTable,
2390: 22 73 71 6c 69 74 65 5f 75 73 65 72 22 29 3d 3d "sqlite_user")==
23a0: 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 0;.}.#endif../*.
23b0: 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e ** Locate the in
23c0: 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 -memory structur
23d0: 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 e that describes
23e0: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 a particular da
23f0: 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 tabase.** table
2400: 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f given the name o
2410: 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 f that table and
2420: 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 (optionally) th
2430: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a e name of the.**
2440: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
2450: 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 ning the table.
2460: 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 Return NULL if
2470: 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a not found..**.**
2480: 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 If zDatabase is
2490: 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0, all database
24a0: 73 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 s are searched f
24b0: 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 or the table and
24c0: 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61 the.** first ma
24d0: 74 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20 tching table is
24e0: 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 returned. (No c
24f0: 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c hecking for dupl
2500: 69 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e icate table.** n
2510: 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 ames is done.)
2520: 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 The search order
2530: 20 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20 is TEMP first,
2540: 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 then MAIN, then
2550: 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 any.** auxiliary
2560: 20 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 databases added
2570: 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 using the ATTAC
2580: 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a H command..**.**
2590: 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 See also sqlite
25a0: 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a 3LocateTable()..
25b0: 2a 2f 0a 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 */.Table *sqlite
25c0: 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 3FindTable(sqlit
25d0: 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
25e0: 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 ar *zName, const
25f0: 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 char *zDatabase
2600: 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 ){. Table *p =
2610: 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f 0;. int i;.. /
2620: 2a 20 41 6c 6c 20 6d 75 74 65 78 65 73 20 61 72 * All mutexes ar
2630: 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 e required for s
2640: 63 68 65 6d 61 20 61 63 63 65 73 73 2e 20 20 4d chema access. M
2650: 61 6b 65 20 73 75 72 65 20 77 65 20 68 6f 6c 64 ake sure we hold
2660: 20 74 68 65 6d 2e 20 2a 2f 0a 20 20 61 73 73 65 them. */. asse
2670: 72 74 28 20 7a 44 61 74 61 62 61 73 65 21 3d 30 rt( zDatabase!=0
2680: 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65 || sqlite3Btree
2690: 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
26a0: 64 62 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 db) );.#if SQLIT
26b0: 45 5f 55 53 45 52 5f 41 55 54 48 45 4e 54 49 43 E_USER_AUTHENTIC
26c0: 41 54 49 4f 4e 0a 20 20 2f 2a 20 4f 6e 6c 79 20 ATION. /* Only
26d0: 74 68 65 20 61 64 6d 69 6e 20 75 73 65 72 20 69 the admin user i
26e0: 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6b 6e 6f s allowed to kno
26f0: 77 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 w that the sqlit
2700: 65 5f 75 73 65 72 20 74 61 62 6c 65 0a 20 20 2a e_user table. *
2710: 2a 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 66 * exists */. if
2720: 28 20 64 62 2d 3e 61 75 74 68 2e 61 75 74 68 4c ( db->auth.authL
2730: 65 76 65 6c 3c 55 41 55 54 48 5f 41 64 6d 69 6e evel<UAUTH_Admin
2740: 20 26 26 20 73 71 6c 69 74 65 33 55 73 65 72 41 && sqlite3UserA
2750: 75 74 68 54 61 62 6c 65 28 7a 4e 61 6d 65 29 21 uthTable(zName)!
2760: 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
2770: 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 0;. }.#endif.
2780: 20 77 68 69 6c 65 28 31 29 7b 0a 20 20 20 20 66 while(1){. f
2790: 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 or(i=OMIT_TEMPDB
27a0: 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
27b0: 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d ){. int j =
27c0: 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 (i<2) ? i^1 : i
27d0: 3b 20 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45 ; /* Search TE
27e0: 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a MP before MAIN *
27f0: 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 44 61 74 /. if( zDat
2800: 61 62 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 abase==0 || sqli
2810: 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74 61 te3StrICmp(zData
2820: 62 61 73 65 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d base, db->aDb[j]
2830: 2e 7a 44 62 53 4e 61 6d 65 29 3d 3d 30 20 29 7b .zDbSName)==0 ){
2840: 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
2850: 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 sqlite3SchemaMu
2860: 74 65 78 48 65 6c 64 28 64 62 2c 20 6a 2c 20 30 texHeld(db, j, 0
2870: 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d ) );. p =
2880: 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 sqlite3HashFind
2890: 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 (&db->aDb[j].pSc
28a0: 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a hema->tblHash, z
28b0: 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 69 Name);. i
28c0: 66 28 20 70 20 29 20 72 65 74 75 72 6e 20 70 3b f( p ) return p;
28d0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
28e0: 20 20 20 2f 2a 20 4e 6f 74 20 66 6f 75 6e 64 2e /* Not found.
28f0: 20 20 49 66 20 74 68 65 20 6e 61 6d 65 20 77 65 If the name we
2900: 20 77 65 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f were looking fo
2910: 72 20 77 61 73 20 74 65 6d 70 2e 73 71 6c 69 74 r was temp.sqlit
2920: 65 5f 6d 61 73 74 65 72 0a 20 20 20 20 2a 2a 20 e_master. **
2930: 74 68 65 6e 20 63 68 61 6e 67 65 20 74 68 65 20 then change the
2940: 6e 61 6d 65 20 74 6f 20 73 71 6c 69 74 65 5f 74 name to sqlite_t
2950: 65 6d 70 5f 6d 61 73 74 65 72 20 61 6e 64 20 74 emp_master and t
2960: 72 79 20 61 67 61 69 6e 2e 20 2a 2f 0a 20 20 20 ry again. */.
2970: 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
2980: 43 6d 70 28 7a 4e 61 6d 65 2c 20 4d 41 53 54 45 Cmp(zName, MASTE
2990: 52 5f 4e 41 4d 45 29 21 3d 30 20 29 20 62 72 65 R_NAME)!=0 ) bre
29a0: 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ak;. if( sqli
29b0: 74 65 33 5f 73 74 72 69 63 6d 70 28 7a 44 61 74 te3_stricmp(zDat
29c0: 61 62 61 73 65 2c 20 64 62 2d 3e 61 44 62 5b 31 abase, db->aDb[1
29d0: 5d 2e 7a 44 62 53 4e 61 6d 65 29 21 3d 30 20 29 ].zDbSName)!=0 )
29e0: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7a 4e 61 6d break;. zNam
29f0: 65 20 3d 20 54 45 4d 50 5f 4d 41 53 54 45 52 5f e = TEMP_MASTER_
2a00: 4e 41 4d 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 NAME;. }. retu
2a10: 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c rn 0;.}../*.** L
2a20: 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d ocate the in-mem
2a30: 6f 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 ory structure th
2a40: 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70 at describes a p
2a50: 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 articular databa
2a60: 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 se.** table give
2a70: 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 n the name of th
2a80: 61 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 at table and (op
2a90: 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 tionally) the na
2aa0: 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 me of the.** dat
2ab0: 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 abase containing
2ac0: 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 the table. Ret
2ad0: 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 urn NULL if not
2ae0: 66 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61 found. Also lea
2af0: 76 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d ve an.** error m
2b00: 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 essage in pParse
2b10: 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a ->zErrMsg..**.**
2b20: 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 The difference
2b30: 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 between this rou
2b40: 74 69 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33 tine and sqlite3
2b50: 46 69 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74 FindTable() is t
2b60: 68 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 hat this.** rout
2b70: 69 6e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72 ine leaves an er
2b80: 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 ror message in p
2b90: 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77 Parse->zErrMsg w
2ba0: 68 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46 here.** sqlite3F
2bb0: 69 6e 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20 indTable() does
2bc0: 6e 6f 74 2e 0a 2a 2f 0a 54 61 62 6c 65 20 2a 73 not..*/.Table *s
2bd0: 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c qlite3LocateTabl
2be0: 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
2bf0: 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63 se, /* c
2c00: 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 ontext in which
2c10: 74 6f 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 to report errors
2c20: 20 2a 2f 0a 20 20 75 33 32 20 66 6c 61 67 73 2c */. u32 flags,
2c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2c40: 4c 4f 43 41 54 45 5f 56 49 45 57 20 6f 72 20 4c LOCATE_VIEW or L
2c50: 4f 43 41 54 45 5f 4e 4f 45 52 52 20 2a 2f 0a 20 OCATE_NOERR */.
2c60: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
2c70: 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 me, /* Name
2c80: 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 of the table we
2c90: 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 are looking for
2ca0: 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
2cb0: 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a 20 4e *zDbase /* N
2cc0: 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
2cd0: 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65 20 4e ase. Might be N
2ce0: 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c ULL */.){. Tabl
2cf0: 65 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 e *p;. sqlite3
2d00: 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
2d10: 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 ;.. /* Read the
2d20: 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
2d30: 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
2d40: 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 curs, leave an e
2d50: 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a rror message. *
2d60: 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 * and code in pP
2d70: 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 arse and return
2d80: 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 28 NULL. */. if( (
2d90: 64 62 2d 3e 6d 44 62 46 6c 61 67 73 20 26 20 44 db->mDbFlags & D
2da0: 42 46 4c 41 47 5f 53 63 68 65 6d 61 4b 6e 6f 77 BFLAG_SchemaKnow
2db0: 6e 4f 6b 29 3d 3d 30 20 0a 20 20 20 26 26 20 53 nOk)==0 . && S
2dc0: 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 QLITE_OK!=sqlite
2dd0: 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 3ReadSchema(pPar
2de0: 73 65 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 se). ){. ret
2df0: 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 20 urn 0;. }.. p
2e00: 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 = sqlite3FindTab
2e10: 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 le(db, zName, zD
2e20: 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d base);. if( p==
2e30: 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 0 ){. const c
2e40: 68 61 72 20 2a 7a 4d 73 67 20 3d 20 66 6c 61 67 har *zMsg = flag
2e50: 73 20 26 20 4c 4f 43 41 54 45 5f 56 49 45 57 20 s & LOCATE_VIEW
2e60: 3f 20 22 6e 6f 20 73 75 63 68 20 76 69 65 77 22 ? "no such view"
2e70: 20 3a 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c : "no such tabl
2e80: 65 22 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 e";.#ifndef SQLI
2e90: 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
2ea0: 41 42 4c 45 0a 20 20 20 20 69 66 28 20 73 71 6c ABLE. if( sql
2eb0: 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 64 ite3FindDbName(d
2ec0: 62 2c 20 7a 44 62 61 73 65 29 3c 31 20 29 7b 0a b, zDbase)<1 ){.
2ed0: 20 20 20 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d /* If zNam
2ee0: 65 20 69 73 20 74 68 65 20 6e 6f 74 20 74 68 65 e is the not the
2ef0: 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 name of a table
2f00: 20 69 6e 20 74 68 65 20 73 63 68 65 6d 61 20 63 in the schema c
2f10: 72 65 61 74 65 64 20 75 73 69 6e 67 0a 20 20 20 reated using.
2f20: 20 20 20 2a 2a 20 43 52 45 41 54 45 2c 20 74 68 ** CREATE, th
2f30: 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 en check to see
2f40: 69 66 20 69 74 20 69 73 20 74 68 65 20 6e 61 6d if it is the nam
2f50: 65 20 6f 66 20 61 6e 20 76 69 72 74 75 61 6c 20 e of an virtual
2f60: 74 61 62 6c 65 20 74 68 61 74 0a 20 20 20 20 20 table that.
2f70: 20 2a 2a 20 63 61 6e 20 62 65 20 61 6e 20 65 70 ** can be an ep
2f80: 6f 6e 79 6d 6f 75 73 20 76 69 72 74 75 61 6c 20 onymous virtual
2f90: 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 table. */.
2fa0: 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 Module *pMod = (
2fb0: 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74 65 33 48 Module*)sqlite3H
2fc0: 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d 6f ashFind(&db->aMo
2fd0: 64 75 6c 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 dule, zName);.
2fe0: 20 20 20 20 69 66 28 20 70 4d 6f 64 3d 3d 30 20 if( pMod==0
2ff0: 26 26 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 && sqlite3_strni
3000: 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 70 72 61 67 cmp(zName, "prag
3010: 6d 61 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 ma_", 7)==0 ){.
3020: 20 20 20 20 20 20 20 70 4d 6f 64 20 3d 20 73 71 pMod = sq
3030: 6c 69 74 65 33 50 72 61 67 6d 61 56 74 61 62 52 lite3PragmaVtabR
3040: 65 67 69 73 74 65 72 28 64 62 2c 20 7a 4e 61 6d egister(db, zNam
3050: 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
3060: 20 20 69 66 28 20 70 4d 6f 64 20 26 26 20 73 71 if( pMod && sq
3070: 6c 69 74 65 33 56 74 61 62 45 70 6f 6e 79 6d 6f lite3VtabEponymo
3080: 75 73 54 61 62 6c 65 49 6e 69 74 28 70 50 61 72 usTableInit(pPar
3090: 73 65 2c 20 70 4d 6f 64 29 20 29 7b 0a 20 20 20 se, pMod) ){.
30a0: 20 20 20 20 20 72 65 74 75 72 6e 20 70 4d 6f 64 return pMod
30b0: 2d 3e 70 45 70 6f 54 61 62 3b 0a 20 20 20 20 20 ->pEpoTab;.
30c0: 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
30d0: 20 20 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 if( (flags &
30e0: 20 4c 4f 43 41 54 45 5f 4e 4f 45 52 52 29 3d 3d LOCATE_NOERR)==
30f0: 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 0 ){. if( z
3100: 44 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 Dbase ){.
3110: 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
3120: 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 (pParse, "%s: %s
3130: 2e 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 44 62 61 .%s", zMsg, zDba
3140: 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 se, zName);.
3150: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
3160: 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
3170: 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 (pParse, "%s: %s
3180: 22 2c 20 7a 4d 73 67 2c 20 7a 4e 61 6d 65 29 3b ", zMsg, zName);
3190: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
31a0: 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 Parse->checkSche
31b0: 6d 61 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ma = 1;. }.
31c0: 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }.. return p;.}
31d0: 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 ../*.** Locate t
31e0: 68 65 20 74 61 62 6c 65 20 69 64 65 6e 74 69 66 he table identif
31f0: 69 65 64 20 62 79 20 2a 70 2e 0a 2a 2a 0a 2a 2a ied by *p..**.**
3200: 20 54 68 69 73 20 69 73 20 61 20 77 72 61 70 70 This is a wrapp
3210: 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69 74 65 er around sqlite
3220: 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 20 3LocateTable().
3230: 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 The difference b
3240: 65 74 77 65 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 etween.** sqlite
3250: 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 20 61 3LocateTable() a
3260: 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e nd this function
3270: 20 69 73 20 74 68 61 74 20 74 68 69 73 20 66 75 is that this fu
3280: 6e 63 74 69 6f 6e 20 72 65 73 74 72 69 63 74 73 nction restricts
3290: 0a 2a 2a 20 74 68 65 20 73 65 61 72 63 68 20 74 .** the search t
32a0: 6f 20 73 63 68 65 6d 61 20 28 70 2d 3e 70 53 63 o schema (p->pSc
32b0: 68 65 6d 61 29 20 69 66 20 69 74 20 69 73 20 6e hema) if it is n
32c0: 6f 74 20 4e 55 4c 4c 2e 20 70 2d 3e 70 53 63 68 ot NULL. p->pSch
32d0: 65 6d 61 20 6d 61 79 20 62 65 0a 2a 2a 20 6e 6f ema may be.** no
32e0: 6e 2d 4e 55 4c 4c 20 69 66 20 69 74 20 69 73 20 n-NULL if it is
32f0: 70 61 72 74 20 6f 66 20 61 20 76 69 65 77 20 6f part of a view o
3300: 72 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 r trigger progra
3310: 6d 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 53 65 m definition. Se
3320: 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69 78 53 e.** sqlite3FixS
3330: 72 63 4c 69 73 74 28 29 20 66 6f 72 20 64 65 74 rcList() for det
3340: 61 69 6c 73 2e 0a 2a 2f 0a 54 61 62 6c 65 20 2a ails..*/.Table *
3350: 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
3360: 6c 65 49 74 65 6d 28 0a 20 20 50 61 72 73 65 20 leItem(. Parse
3370: 2a 70 50 61 72 73 65 2c 20 0a 20 20 75 33 32 20 *pParse, . u32
3380: 66 6c 61 67 73 2c 0a 20 20 73 74 72 75 63 74 20 flags,. struct
3390: 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 0a SrcList_item *p.
33a0: 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ){. const char
33b0: 2a 7a 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 *zDb;. assert(
33c0: 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 30 20 7c 7c p->pSchema==0 ||
33d0: 20 70 2d 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30 p->zDatabase==0
33e0: 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53 63 );. if( p->pSc
33f0: 68 65 6d 61 20 29 7b 0a 20 20 20 20 69 6e 74 20 hema ){. int
3400: 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
3410: 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 emaToIndex(pPars
3420: 65 2d 3e 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d e->db, p->pSchem
3430: 61 29 3b 0a 20 20 20 20 7a 44 62 20 3d 20 70 50 a);. zDb = pP
3440: 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 arse->db->aDb[iD
3450: 62 5d 2e 7a 44 62 53 4e 61 6d 65 3b 0a 20 20 7d b].zDbSName;. }
3460: 65 6c 73 65 7b 0a 20 20 20 20 7a 44 62 20 3d 20 else{. zDb =
3470: 70 2d 3e 7a 44 61 74 61 62 61 73 65 3b 0a 20 20 p->zDatabase;.
3480: 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
3490: 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 e3LocateTable(pP
34a0: 61 72 73 65 2c 20 66 6c 61 67 73 2c 20 70 2d 3e arse, flags, p->
34b0: 7a 4e 61 6d 65 2c 20 7a 44 62 29 3b 0a 7d 0a 0a zName, zDb);.}..
34c0: 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 /*.** Locate the
34d0: 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 in-memory struc
34e0: 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 ture that descri
34f0: 62 65 73 20 0a 2a 2a 20 61 20 70 61 72 74 69 63 bes .** a partic
3500: 75 6c 61 72 20 69 6e 64 65 78 20 67 69 76 65 6e ular index given
3510: 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 the name of tha
3520: 74 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 t index.** and t
3530: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 he name of the d
3540: 61 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e atabase that con
3550: 74 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 2e tains the index.
3560: 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 .** Return NULL
3570: 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a if not found..**
3580: 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 65 .** If zDatabase
3590: 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 is 0, all datab
35a0: 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65 ases are searche
35b0: 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74 61 62 d for the.** tab
35c0: 6c 65 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 le and the first
35d0: 20 6d 61 74 63 68 69 6e 67 20 69 6e 64 65 78 20 matching index
35e0: 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 4e is returned. (N
35f0: 6f 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f o checking.** fo
3600: 72 20 64 75 70 6c 69 63 61 74 65 20 69 6e 64 65 r duplicate inde
3610: 78 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e x names is done.
3620: 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f 72 ) The search or
3630: 64 65 72 20 69 73 0a 2a 2a 20 54 45 4d 50 20 66 der is.** TEMP f
3640: 69 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c irst, then MAIN,
3650: 20 74 68 65 6e 20 61 6e 79 20 61 75 78 69 6c 69 then any auxili
3660: 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64 ary databases ad
3670: 64 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 ded.** using the
3680: 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e ATTACH command.
3690: 0a 2a 2f 0a 49 6e 64 65 78 20 2a 73 71 6c 69 74 .*/.Index *sqlit
36a0: 65 33 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 e3FindIndex(sqli
36b0: 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 te3 *db, const c
36c0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 har *zName, cons
36d0: 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 t char *zDb){.
36e0: 49 6e 64 65 78 20 2a 70 20 3d 20 30 3b 0a 20 20 Index *p = 0;.
36f0: 69 6e 74 20 69 3b 0a 20 20 2f 2a 20 41 6c 6c 20 int i;. /* All
3700: 6d 75 74 65 78 65 73 20 61 72 65 20 72 65 71 75 mutexes are requ
3710: 69 72 65 64 20 66 6f 72 20 73 63 68 65 6d 61 20 ired for schema
3720: 61 63 63 65 73 73 2e 20 20 4d 61 6b 65 20 73 75 access. Make su
3730: 72 65 20 77 65 20 68 6f 6c 64 20 74 68 65 6d 2e re we hold them.
3740: 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 44 */. assert( zD
3750: 62 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 42 b!=0 || sqlite3B
3760: 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
3770: 78 65 73 28 64 62 29 20 29 3b 0a 20 20 66 6f 72 xes(db) );. for
3780: 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 (i=OMIT_TEMPDB;
3790: 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
37a0: 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c . int j = (i<
37b0: 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2) ? i^1 : i; /
37c0: 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 * Search TEMP be
37d0: 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 fore MAIN */.
37e0: 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
37f0: 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 = db->aDb[j].pS
3800: 63 68 65 6d 61 3b 0a 20 20 20 20 61 73 73 65 72 chema;. asser
3810: 74 28 20 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 t( pSchema );.
3820: 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c if( zDb && sql
3830: 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62 2c ite3StrICmp(zDb,
3840: 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 44 62 53 db->aDb[j].zDbS
3850: 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 Name) ) continue
3860: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 ;. assert( sq
3870: 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 lite3SchemaMutex
3880: 48 65 6c 64 28 64 62 2c 20 6a 2c 20 30 29 20 29 Held(db, j, 0) )
3890: 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 ;. p = sqlite
38a0: 33 48 61 73 68 46 69 6e 64 28 26 70 53 63 68 65 3HashFind(&pSche
38b0: 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 ma->idxHash, zNa
38c0: 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 me);. if( p )
38d0: 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 break;. }. re
38e0: 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
38f0: 20 52 65 63 6c 61 69 6d 20 74 68 65 20 6d 65 6d Reclaim the mem
3900: 6f 72 79 20 75 73 65 64 20 62 79 20 61 6e 20 69 ory used by an i
3910: 6e 64 65 78 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ndex.*/.void sql
3920: 69 74 65 33 46 72 65 65 49 6e 64 65 78 28 73 71 ite3FreeIndex(sq
3930: 6c 69 74 65 33 20 2a 64 62 2c 20 49 6e 64 65 78 lite3 *db, Index
3940: 20 2a 70 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 *p){.#ifndef SQ
3950: 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a LITE_OMIT_ANALYZ
3960: 45 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 E. sqlite3Delet
3970: 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 64 62 eIndexSamples(db
3980: 2c 20 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 , p);.#endif. s
3990: 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
39a0: 28 64 62 2c 20 70 2d 3e 70 50 61 72 74 49 64 78 (db, p->pPartIdx
39b0: 57 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 Where);. sqlite
39c0: 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 3ExprListDelete(
39d0: 64 62 2c 20 70 2d 3e 61 43 6f 6c 45 78 70 72 29 db, p->aColExpr)
39e0: 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
39f0: 65 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 e(db, p->zColAff
3a00: 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73 52 65 );. if( p->isRe
3a10: 73 69 7a 65 64 20 29 20 73 71 6c 69 74 65 33 44 sized ) sqlite3D
3a20: 62 46 72 65 65 28 64 62 2c 20 28 76 6f 69 64 20 bFree(db, (void
3a30: 2a 29 70 2d 3e 61 7a 43 6f 6c 6c 29 3b 0a 23 69 *)p->azColl);.#i
3a40: 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
3a50: 4c 45 5f 53 54 41 54 33 5f 4f 52 5f 53 54 41 54 LE_STAT3_OR_STAT
3a60: 34 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 4. sqlite3_free
3a70: 28 70 2d 3e 61 69 52 6f 77 45 73 74 29 3b 0a 23 (p->aiRowEst);.#
3a80: 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 44 endif. sqlite3D
3a90: 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a bFree(db, p);.}.
3aa0: 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 69 ./*.** For the i
3ab0: 6e 64 65 78 20 63 61 6c 6c 65 64 20 7a 49 64 78 ndex called zIdx
3ac0: 4e 61 6d 65 20 77 68 69 63 68 20 69 73 20 66 6f Name which is fo
3ad0: 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 und in the datab
3ae0: 61 73 65 20 69 44 62 2c 0a 2a 2a 20 75 6e 6c 69 ase iDb,.** unli
3af0: 6b 65 20 74 68 61 74 20 69 6e 64 65 78 20 66 72 ke that index fr
3b00: 6f 6d 20 69 74 73 20 54 61 62 6c 65 20 74 68 65 om its Table the
3b10: 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 69 6e 64 n remove the ind
3b20: 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 ex from.** the i
3b30: 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 20 ndex hash table
3b40: 61 6e 64 20 66 72 65 65 20 61 6c 6c 20 6d 65 6d and free all mem
3b50: 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 20 61 ory structures a
3b60: 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
3b70: 68 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a h the index..*/.
3b80: 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 void sqlite3Unli
3b90: 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 nkAndDeleteIndex
3ba0: 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e (sqlite3 *db, in
3bb0: 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 t iDb, const cha
3bc0: 72 20 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a 20 20 r *zIdxName){.
3bd0: 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 Index *pIndex;.
3be0: 20 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 0a 20 Hash *pHash;..
3bf0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
3c00: 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 SchemaMutexHeld(
3c10: 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 20 db, iDb, 0) );.
3c20: 20 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44 pHash = &db->aD
3c30: 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e b[iDb].pSchema->
3c40: 69 64 78 48 61 73 68 3b 0a 20 20 70 49 6e 64 65 idxHash;. pInde
3c50: 78 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 x = sqlite3HashI
3c60: 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 49 64 nsert(pHash, zId
3c70: 78 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66 28 xName, 0);. if(
3c80: 20 41 4c 57 41 59 53 28 70 49 6e 64 65 78 29 20 ALWAYS(pIndex)
3c90: 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 ){. if( pInde
3ca0: 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 x->pTable->pInde
3cb0: 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 x==pIndex ){.
3cc0: 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c pIndex->pTabl
3cd0: 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 e->pIndex = pInd
3ce0: 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d ex->pNext;. }
3cf0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 else{. Inde
3d00: 78 20 2a 70 3b 0a 20 20 20 20 20 20 2f 2a 20 4a x *p;. /* J
3d10: 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66 20 ustification of
3d20: 41 4c 57 41 59 53 28 29 3b 20 20 54 68 65 20 69 ALWAYS(); The i
3d30: 6e 64 65 78 20 6d 75 73 74 20 62 65 20 6f 6e 20 ndex must be on
3d40: 74 68 65 20 6c 69 73 74 20 6f 66 0a 20 20 20 20 the list of.
3d50: 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 2a 2f ** indices. */
3d60: 0a 20 20 20 20 20 20 70 20 3d 20 70 49 6e 64 65 . p = pInde
3d70: 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 x->pTable->pInde
3d80: 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 x;. while(
3d90: 41 4c 57 41 59 53 28 70 29 20 26 26 20 70 2d 3e ALWAYS(p) && p->
3da0: 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 20 29 7b pNext!=pIndex ){
3db0: 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 7d p = p->pNext; }
3dc0: 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 . if( ALWAY
3dd0: 53 28 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d S(p && p->pNext=
3de0: 3d 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 =pIndex) ){.
3df0: 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
3e00: 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 Index->pNext;.
3e10: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
3e20: 73 71 6c 69 74 65 33 46 72 65 65 49 6e 64 65 78 sqlite3FreeIndex
3e30: 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 (db, pIndex);.
3e40: 7d 0a 20 20 64 62 2d 3e 6d 44 62 46 6c 61 67 73 }. db->mDbFlags
3e50: 20 7c 3d 20 44 42 46 4c 41 47 5f 53 63 68 65 6d |= DBFLAG_Schem
3e60: 61 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a aChange;.}../*.*
3e70: 2a 20 4c 6f 6f 6b 20 74 68 72 6f 75 67 68 20 74 * Look through t
3e80: 68 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 6e 20 he list of open
3e90: 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 69 database files i
3ea0: 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 61 6e 64 20 n db->aDb[] and
3eb0: 69 66 0a 2a 2a 20 61 6e 79 20 68 61 76 65 20 62 if.** any have b
3ec0: 65 65 6e 20 63 6c 6f 73 65 64 2c 20 72 65 6d 6f een closed, remo
3ed0: 76 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 ve them from the
3ee0: 20 6c 69 73 74 2e 20 20 52 65 61 6c 6c 6f 63 61 list. Realloca
3ef0: 74 65 20 74 68 65 0a 2a 2a 20 64 62 2d 3e 61 44 te the.** db->aD
3f00: 62 5b 5d 20 73 74 72 75 63 74 75 72 65 20 74 6f b[] structure to
3f10: 20 61 20 73 6d 61 6c 6c 65 72 20 73 69 7a 65 2c a smaller size,
3f20: 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a if possible..**
3f30: 0a 2a 2a 20 45 6e 74 72 79 20 30 20 28 74 68 65 .** Entry 0 (the
3f40: 20 22 6d 61 69 6e 22 20 64 61 74 61 62 61 73 65 "main" database
3f50: 29 20 61 6e 64 20 65 6e 74 72 79 20 31 20 28 74 ) and entry 1 (t
3f60: 68 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 he "temp" databa
3f70: 73 65 29 0a 2a 2a 20 61 72 65 20 6e 65 76 65 72 se).** are never
3f80: 20 63 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20 candidates for
3f90: 62 65 69 6e 67 20 63 6f 6c 6c 61 70 73 65 64 2e being collapsed.
3fa0: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
3fb0: 43 6f 6c 6c 61 70 73 65 44 61 74 61 62 61 73 65 CollapseDatabase
3fc0: 41 72 72 61 79 28 73 71 6c 69 74 65 33 20 2a 64 Array(sqlite3 *d
3fd0: 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a b){. int i, j;.
3fe0: 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c 64 for(i=j=2; i<d
3ff0: 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
4000: 20 20 73 74 72 75 63 74 20 44 62 20 2a 70 44 62 struct Db *pDb
4010: 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a = &db->aDb[i];.
4020: 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 if( pDb->pBt
4030: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
4040: 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
4050: 44 62 2d 3e 7a 44 62 53 4e 61 6d 65 29 3b 0a 20 Db->zDbSName);.
4060: 20 20 20 20 20 70 44 62 2d 3e 7a 44 62 53 4e 61 pDb->zDbSNa
4070: 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f me = 0;. co
4080: 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 ntinue;. }.
4090: 20 20 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 if( j<i ){.
40a0: 20 20 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 db->aDb[j] =
40b0: 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 db->aDb[i];.
40c0: 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 }. j++;. }.
40d0: 20 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 db->nDb = j;.
40e0: 69 66 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 if( db->nDb<=2 &
40f0: 26 20 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 & db->aDb!=db->a
4100: 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 DbStatic ){.
4110: 6d 65 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 memcpy(db->aDbSt
4120: 61 74 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 atic, db->aDb, 2
4130: 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b *sizeof(db->aDb[
4140: 30 5d 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 0]));. sqlite
4150: 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 3DbFree(db, db->
4160: 61 44 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 aDb);. db->aD
4170: 62 20 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 b = db->aDbStati
4180: 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 c;. }.}../*.**
4190: 52 65 73 65 74 20 74 68 65 20 73 63 68 65 6d 61 Reset the schema
41a0: 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 for the databas
41b0: 65 20 61 74 20 69 6e 64 65 78 20 69 44 62 2e 20 e at index iDb.
41c0: 20 41 6c 73 6f 20 72 65 73 65 74 20 74 68 65 0a Also reset the.
41d0: 2a 2a 20 54 45 4d 50 20 73 63 68 65 6d 61 2e 20 ** TEMP schema.
41e0: 20 54 68 65 20 72 65 73 65 74 20 69 73 20 64 65 The reset is de
41f0: 66 65 72 72 65 64 20 69 66 20 64 62 2d 3e 6e 53 ferred if db->nS
4200: 63 68 65 6d 61 4c 6f 63 6b 20 69 73 20 6e 6f 74 chemaLock is not
4210: 20 7a 65 72 6f 2e 0a 2a 2a 20 44 65 66 65 72 72 zero..** Deferr
4220: 65 64 20 72 65 73 65 74 73 20 6d 61 79 20 62 65 ed resets may be
4230: 20 72 75 6e 20 62 79 20 63 61 6c 6c 69 6e 67 20 run by calling
4240: 77 69 74 68 20 69 44 62 3c 30 2e 0a 2a 2f 0a 76 with iDb<0..*/.v
4250: 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 oid sqlite3Reset
4260: 4f 6e 65 53 63 68 65 6d 61 28 73 71 6c 69 74 65 OneSchema(sqlite
4270: 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 3 *db, int iDb){
4280: 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 . int i;. asse
4290: 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 rt( iDb<db->nDb
42a0: 29 3b 0a 0a 20 20 69 66 28 20 69 44 62 3e 3d 30 );.. if( iDb>=0
42b0: 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
42c0: 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 sqlite3SchemaMut
42d0: 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 exHeld(db, iDb,
42e0: 30 29 20 29 3b 0a 20 20 20 20 44 62 53 65 74 50 0) );. DbSetP
42f0: 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c roperty(db, iDb,
4300: 20 44 42 5f 52 65 73 65 74 57 61 6e 74 65 64 29 DB_ResetWanted)
4310: 3b 0a 20 20 20 20 44 62 53 65 74 50 72 6f 70 65 ;. DbSetPrope
4320: 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 52 65 rty(db, 1, DB_Re
4330: 73 65 74 57 61 6e 74 65 64 29 3b 0a 20 20 20 20 setWanted);.
4340: 64 62 2d 3e 6d 44 62 46 6c 61 67 73 20 26 3d 20 db->mDbFlags &=
4350: 7e 44 42 46 4c 41 47 5f 53 63 68 65 6d 61 4b 6e ~DBFLAG_SchemaKn
4360: 6f 77 6e 4f 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 ownOk;. }.. if
4370: 28 20 64 62 2d 3e 6e 53 63 68 65 6d 61 4c 6f 63 ( db->nSchemaLoc
4380: 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 k==0 ){. for(
4390: 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
43a0: 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
43b0: 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 DbHasProperty(db
43c0: 2c 20 69 2c 20 44 42 5f 52 65 73 65 74 57 61 6e , i, DB_ResetWan
43d0: 74 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ted) ){.
43e0: 73 71 6c 69 74 65 33 53 63 68 65 6d 61 43 6c 65 sqlite3SchemaCle
43f0: 61 72 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 ar(db->aDb[i].pS
4400: 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 7d 0a chema);. }.
4410: 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
4420: 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 63 68 ** Erase all sch
4430: 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ema information
4440: 66 72 6f 6d 20 61 6c 6c 20 61 74 74 61 63 68 65 from all attache
4450: 64 20 64 61 74 61 62 61 73 65 73 20 28 69 6e 63 d databases (inc
4460: 6c 75 64 69 6e 67 0a 2a 2a 20 22 6d 61 69 6e 22 luding.** "main"
4470: 20 61 6e 64 20 22 74 65 6d 70 22 29 20 66 6f 72 and "temp") for
4480: 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 a single databa
4490: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a se connection..*
44a0: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 /.void sqlite3Re
44b0: 73 65 74 41 6c 6c 53 63 68 65 6d 61 73 4f 66 43 setAllSchemasOfC
44c0: 6f 6e 6e 65 63 74 69 6f 6e 28 73 71 6c 69 74 65 onnection(sqlite
44d0: 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 3 *db){. int i;
44e0: 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
44f0: 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 61 nterAll(db);. a
4500: 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 63 68 65 ssert( db->nSche
4510: 6d 61 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 66 maLock==0 );. f
4520: 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
4530: 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44 62 20 b; i++){. Db
4540: 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b *pDb = &db->aDb[
4550: 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d i];. if( pDb-
4560: 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 >pSchema ){.
4570: 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 43 sqlite3SchemaC
4580: 6c 65 61 72 28 70 44 62 2d 3e 70 53 63 68 65 6d lear(pDb->pSchem
4590: 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 a);. }. }.
45a0: 64 62 2d 3e 6d 44 62 46 6c 61 67 73 20 26 3d 20 db->mDbFlags &=
45b0: 7e 28 44 42 46 4c 41 47 5f 53 63 68 65 6d 61 43 ~(DBFLAG_SchemaC
45c0: 68 61 6e 67 65 7c 44 42 46 4c 41 47 5f 53 63 68 hange|DBFLAG_Sch
45d0: 65 6d 61 4b 6e 6f 77 6e 4f 6b 29 3b 0a 20 20 73 emaKnownOk);. s
45e0: 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b qlite3VtabUnlock
45f0: 4c 69 73 74 28 64 62 29 3b 0a 20 20 73 71 6c 69 List(db);. sqli
4600: 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c te3BtreeLeaveAll
4610: 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 (db);. sqlite3C
4620: 6f 6c 6c 61 70 73 65 44 61 74 61 62 61 73 65 41 ollapseDatabaseA
4630: 72 72 61 79 28 64 62 29 3b 0a 7d 0a 0a 2f 2a 0a rray(db);.}../*.
4640: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
4650: 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 is called when a
4660: 20 63 6f 6d 6d 69 74 20 6f 63 63 75 72 73 2e 0a commit occurs..
4670: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 43 */.void sqlite3C
4680: 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 ommitInternalCha
4690: 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 nges(sqlite3 *db
46a0: 29 7b 0a 20 20 64 62 2d 3e 6d 44 62 46 6c 61 67 ){. db->mDbFlag
46b0: 73 20 26 3d 20 7e 44 42 46 4c 41 47 5f 53 63 68 s &= ~DBFLAG_Sch
46c0: 65 6d 61 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a emaChange;.}../*
46d0: 0a 2a 2a 20 44 65 6c 65 74 65 20 6d 65 6d 6f 72 .** Delete memor
46e0: 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 y allocated for
46f0: 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 the column names
4700: 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20 76 of a table or v
4710: 69 65 77 20 28 74 68 65 0a 2a 2a 20 54 61 62 6c iew (the.** Tabl
4720: 65 2e 61 43 6f 6c 5b 5d 20 61 72 72 61 79 29 2e e.aCol[] array).
4730: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
4740: 44 65 6c 65 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 DeleteColumnName
4750: 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 s(sqlite3 *db, T
4760: 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 able *pTable){.
4770: 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e int i;. Column
4780: 20 2a 70 43 6f 6c 3b 0a 20 20 61 73 73 65 72 74 *pCol;. assert
4790: 28 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 ( pTable!=0 );.
47a0: 20 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 if( (pCol = pTa
47b0: 62 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b ble->aCol)!=0 ){
47c0: 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
47d0: 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b pTable->nCol; i+
47e0: 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 +, pCol++){.
47f0: 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
4800: 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 db, pCol->zName)
4810: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
4820: 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 xprDelete(db, pC
4830: 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 ol->pDflt);.
4840: 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
4850: 64 62 2c 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 db, pCol->zColl)
4860: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
4870: 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 te3DbFree(db, pT
4880: 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a 20 20 7d able->aCol);. }
4890: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 .}../*.** Remove
48a0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61 74 61 the memory data
48b0: 20 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f structures asso
48c0: 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
48d0: 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 given.** Table.
48e0: 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 No changes are
48f0: 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62 79 20 made to disk by
4900: 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a this routine..**
4910: 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
4920: 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20 74 68 just deletes th
4930: 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 e data structure
4940: 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 75 . It does not u
4950: 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62 nlink.** the tab
4960: 6c 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 le data structur
4970: 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 e from the hash
4980: 74 61 62 6c 65 2e 20 20 42 75 74 20 69 74 20 64 table. But it d
4990: 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a 2a 20 6d oes destroy.** m
49a0: 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 emory structures
49b0: 20 6f 66 20 74 68 65 20 69 6e 64 69 63 65 73 20 of the indices
49c0: 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 and foreign keys
49d0: 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
49e0: 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 0a .** the table..
49f0: 2a 2a 0a 2a 2a 20 54 68 65 20 64 62 20 70 61 72 **.** The db par
4a00: 61 6d 65 74 65 72 20 69 73 20 6f 70 74 69 6f 6e ameter is option
4a10: 61 6c 2e 20 20 49 74 20 69 73 20 6e 65 65 64 65 al. It is neede
4a20: 64 20 69 66 20 74 68 65 20 54 61 62 6c 65 20 6f d if the Table o
4a30: 62 6a 65 63 74 20 0a 2a 2a 20 63 6f 6e 74 61 69 bject .** contai
4a40: 6e 73 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d ns lookaside mem
4a50: 6f 72 79 2e 20 20 28 54 61 62 6c 65 20 6f 62 6a ory. (Table obj
4a60: 65 63 74 73 20 69 6e 20 74 68 65 20 73 63 68 65 ects in the sche
4a70: 6d 61 20 64 6f 20 6e 6f 74 20 75 73 65 0a 2a 2a ma do not use.**
4a80: 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 lookaside memor
4a90: 79 2c 20 62 75 74 20 73 6f 6d 65 20 65 70 68 65 y, but some ephe
4aa0: 6d 65 72 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65 meral Table obje
4ab0: 63 74 73 20 64 6f 2e 29 20 20 4f 72 20 74 68 65 cts do.) Or the
4ac0: 0a 2a 2a 20 64 62 20 70 61 72 61 6d 65 74 65 72 .** db parameter
4ad0: 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 can be used wit
4ae0: 68 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 h db->pnBytesFre
4af0: 65 64 20 74 6f 20 6d 65 61 73 75 72 65 20 74 68 ed to measure th
4b00: 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73 65 64 e memory.** used
4b10: 20 62 79 20 74 68 65 20 54 61 62 6c 65 20 6f 62 by the Table ob
4b20: 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ject..*/.static
4b30: 76 6f 69 64 20 53 51 4c 49 54 45 5f 4e 4f 49 4e void SQLITE_NOIN
4b40: 4c 49 4e 45 20 64 65 6c 65 74 65 54 61 62 6c 65 LINE deleteTable
4b50: 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 (sqlite3 *db, Ta
4b60: 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 ble *pTable){.
4b70: 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a Index *pIndex, *
4b80: 70 4e 65 78 74 3b 0a 0a 23 69 66 64 65 66 20 53 pNext;..#ifdef S
4b90: 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a QLITE_DEBUG. /*
4ba0: 20 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 Record the numb
4bb0: 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e er of outstandin
4bc0: 67 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f g lookaside allo
4bd0: 63 61 74 69 6f 6e 73 20 69 6e 20 73 63 68 65 6d cations in schem
4be0: 61 20 54 61 62 6c 65 73 0a 20 20 2a 2a 20 70 72 a Tables. ** pr
4bf0: 69 6f 72 20 74 6f 20 64 6f 69 6e 67 20 61 6e 79 ior to doing any
4c00: 20 66 72 65 65 28 29 20 6f 70 65 72 61 74 69 6f free() operatio
4c10: 6e 73 2e 20 20 53 69 6e 63 65 20 73 63 68 65 6d ns. Since schem
4c20: 61 20 54 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 a Tables do not
4c30: 75 73 65 0a 20 20 2a 2a 20 6c 6f 6f 6b 61 73 69 use. ** lookasi
4c40: 64 65 2c 20 74 68 69 73 20 6e 75 6d 62 65 72 20 de, this number
4c50: 73 68 6f 75 6c 64 20 6e 6f 74 20 63 68 61 6e 67 should not chang
4c60: 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f e. */. int nLoo
4c70: 6b 61 73 69 64 65 20 3d 20 30 3b 0a 20 20 69 66 kaside = 0;. if
4c80: 28 20 64 62 20 26 26 20 28 70 54 61 62 6c 65 2d ( db && (pTable-
4c90: 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 >tabFlags & TF_E
4ca0: 70 68 65 6d 65 72 61 6c 29 3d 3d 30 20 29 7b 0a phemeral)==0 ){.
4cb0: 20 20 20 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 3d nLookaside =
4cc0: 20 73 71 6c 69 74 65 33 4c 6f 6f 6b 61 73 69 64 sqlite3Lookasid
4cd0: 65 55 73 65 64 28 64 62 2c 20 30 29 3b 0a 20 20 eUsed(db, 0);.
4ce0: 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 }.#endif.. /* D
4cf0: 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 elete all indice
4d00: 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
4d10: 68 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f h this table. */
4d20: 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 20 3d 20 . for(pIndex =
4d30: 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 pTable->pIndex;
4d40: 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 pIndex; pIndex=p
4d50: 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 Next){. pNext
4d60: 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 = pIndex->pNext
4d70: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 ;. assert( pI
4d80: 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 ndex->pSchema==p
4d90: 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 0a 20 Table->pSchema.
4da0: 20 20 20 20 20 20 20 20 7c 7c 20 28 49 73 56 69 || (IsVi
4db0: 72 74 75 61 6c 28 70 54 61 62 6c 65 29 20 26 26 rtual(pTable) &&
4dc0: 20 70 49 6e 64 65 78 2d 3e 69 64 78 54 79 70 65 pIndex->idxType
4dd0: 21 3d 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 !=SQLITE_IDXTYPE
4de0: 5f 41 50 50 44 45 46 29 20 29 3b 0a 20 20 20 20 _APPDEF) );.
4df0: 69 66 28 20 28 64 62 3d 3d 30 20 7c 7c 20 64 62 if( (db==0 || db
4e00: 2d 3e 70 6e 42 79 74 65 73 46 72 65 65 64 3d 3d ->pnBytesFreed==
4e10: 30 29 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 0) && !IsVirtual
4e20: 28 70 54 61 62 6c 65 29 20 29 7b 0a 20 20 20 20 (pTable) ){.
4e30: 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 char *zName =
4e40: 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 3b 20 0a pIndex->zName; .
4e50: 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 20 28 TESTONLY (
4e60: 20 49 6e 64 65 78 20 2a 70 4f 6c 64 20 3d 20 29 Index *pOld = )
4e70: 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 sqlite3HashInse
4e80: 72 74 28 0a 20 20 20 20 20 20 20 20 20 26 70 49 rt(. &pI
4e90: 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 ndex->pSchema->i
4ea0: 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 30 dxHash, zName, 0
4eb0: 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 . );.
4ec0: 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c assert( db==0 ||
4ed0: 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 sqlite3SchemaMu
4ee0: 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c 20 70 texHeld(db, 0, p
4ef0: 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 20 Index->pSchema)
4f00: 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
4f10: 20 70 4f 6c 64 3d 3d 70 49 6e 64 65 78 20 7c 7c pOld==pIndex ||
4f20: 20 70 4f 6c 64 3d 3d 30 20 29 3b 0a 20 20 20 20 pOld==0 );.
4f30: 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 46 72 65 }. sqlite3Fre
4f40: 65 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65 eIndex(db, pInde
4f50: 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 x);. }.. /* De
4f60: 6c 65 74 65 20 61 6e 79 20 66 6f 72 65 69 67 6e lete any foreign
4f70: 20 6b 65 79 73 20 61 74 74 61 63 68 65 64 20 74 keys attached t
4f80: 6f 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f o this table. */
4f90: 0a 20 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 . sqlite3FkDele
4fa0: 74 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a te(db, pTable);.
4fb0: 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 . /* Delete the
4fc0: 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 Table structure
4fd0: 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 itself.. */.
4fe0: 73 71 6c 69 74 65 33 44 65 6c 65 74 65 43 6f 6c sqlite3DeleteCol
4ff0: 75 6d 6e 4e 61 6d 65 73 28 64 62 2c 20 70 54 61 umnNames(db, pTa
5000: 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 ble);. sqlite3D
5010: 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 bFree(db, pTable
5020: 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 ->zName);. sqli
5030: 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 te3DbFree(db, pT
5040: 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a able->zColAff);.
5050: 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
5060: 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 elete(db, pTable
5070: 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71 ->pSelect);. sq
5080: 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
5090: 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e ete(db, pTable->
50a0: 70 43 68 65 63 6b 29 3b 0a 23 69 66 6e 64 65 66 pCheck);.#ifndef
50b0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
50c0: 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 TUALTABLE. sqli
50d0: 74 65 33 56 74 61 62 43 6c 65 61 72 28 64 62 2c te3VtabClear(db,
50e0: 20 70 54 61 62 6c 65 29 3b 0a 23 65 6e 64 69 66 pTable);.#endif
50f0: 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
5100: 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 0a 20 (db, pTable);..
5110: 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 /* Verify that
5120: 6e 6f 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d no lookaside mem
5130: 6f 72 79 20 77 61 73 20 75 73 65 64 20 62 79 20 ory was used by
5140: 73 63 68 65 6d 61 20 74 61 62 6c 65 73 20 2a 2f schema tables */
5150: 0a 20 20 61 73 73 65 72 74 28 20 6e 4c 6f 6f 6b . assert( nLook
5160: 61 73 69 64 65 3d 3d 30 20 7c 7c 20 6e 4c 6f 6f aside==0 || nLoo
5170: 6b 61 73 69 64 65 3d 3d 73 71 6c 69 74 65 33 4c kaside==sqlite3L
5180: 6f 6f 6b 61 73 69 64 65 55 73 65 64 28 64 62 2c ookasideUsed(db,
5190: 30 29 20 29 3b 0a 7d 0a 76 6f 69 64 20 73 71 6c 0) );.}.void sql
51a0: 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 ite3DeleteTable(
51b0: 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 sqlite3 *db, Tab
51c0: 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 2f le *pTable){. /
51d0: 2a 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 * Do not delete
51e0: 74 68 65 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 the table until
51f0: 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
5200: 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f unt reaches zero
5210: 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 . */. if( !pTab
5220: 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 le ) return;. i
5230: 66 28 20 28 28 21 64 62 20 7c 7c 20 64 62 2d 3e f( ((!db || db->
5240: 70 6e 42 79 74 65 73 46 72 65 65 64 3d 3d 30 29 pnBytesFreed==0)
5250: 20 26 26 20 28 2d 2d 70 54 61 62 6c 65 2d 3e 6e && (--pTable->n
5260: 54 61 62 52 65 66 29 3e 30 29 20 29 20 72 65 74 TabRef)>0) ) ret
5270: 75 72 6e 3b 0a 20 20 64 65 6c 65 74 65 54 61 62 urn;. deleteTab
5280: 6c 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a le(db, pTable);.
5290: 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b }.../*.** Unlink
52a0: 20 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65 the given table
52b0: 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 from the hash t
52c0: 61 62 6c 65 73 20 61 6e 64 20 74 68 65 20 64 65 ables and the de
52d0: 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c lete the.** tabl
52e0: 65 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 e structure with
52f0: 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 all its indices
5300: 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 and foreign key
5310: 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 s..*/.void sqlit
5320: 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 e3UnlinkAndDelet
5330: 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a eTable(sqlite3 *
5340: 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e db, int iDb, con
5350: 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d st char *zTabNam
5360: 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a e){. Table *p;.
5370: 20 20 44 62 20 2a 70 44 62 3b 0a 0a 20 20 61 73 Db *pDb;.. as
5380: 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 sert( db!=0 );.
5390: 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
53a0: 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
53b0: 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 54 61 62 ;. assert( zTab
53c0: 4e 61 6d 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Name );. assert
53d0: 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d ( sqlite3SchemaM
53e0: 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 utexHeld(db, iDb
53f0: 2c 20 30 29 20 29 3b 0a 20 20 74 65 73 74 63 61 , 0) );. testca
5400: 73 65 28 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 3d se( zTabName[0]=
5410: 3d 30 20 29 3b 20 20 2f 2a 20 5a 65 72 6f 2d 6c =0 ); /* Zero-l
5420: 65 6e 67 74 68 20 74 61 62 6c 65 20 6e 61 6d 65 ength table name
5430: 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 2a 2f s are allowed */
5440: 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 . pDb = &db->aD
5450: 62 5b 69 44 62 5d 3b 0a 20 20 70 20 3d 20 73 71 b[iDb];. p = sq
5460: 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 lite3HashInsert(
5470: 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 &pDb->pSchema->t
5480: 62 6c 48 61 73 68 2c 20 7a 54 61 62 4e 61 6d 65 blHash, zTabName
5490: 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 , 0);. sqlite3D
54a0: 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 eleteTable(db, p
54b0: 29 3b 0a 20 20 64 62 2d 3e 6d 44 62 46 6c 61 67 );. db->mDbFlag
54c0: 73 20 7c 3d 20 44 42 46 4c 41 47 5f 53 63 68 65 s |= DBFLAG_Sche
54d0: 6d 61 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a maChange;.}../*.
54e0: 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e ** Given a token
54f0: 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72 69 6e , return a strin
5500: 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20 g that consists
5510: 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 of the text of t
5520: 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 2e 20 20 53 hat.** token. S
5530: 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 pace to hold the
5540: 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 returned string
5550: 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 .** is obtained
5560: 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f from sqliteMallo
5570: 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 c() and must be
5580: 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c freed by the cal
5590: 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ling.** function
55a0: 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 71 75 6f 74 ..**.** Any quot
55b0: 61 74 69 6f 6e 20 6d 61 72 6b 73 20 28 65 78 3a ation marks (ex:
55c0: 20 20 22 6e 61 6d 65 22 2c 20 27 6e 61 6d 65 27 "name", 'name'
55d0: 2c 20 5b 6e 61 6d 65 5d 2c 20 6f 72 20 60 6e 61 , [name], or `na
55e0: 6d 65 60 29 20 74 68 61 74 0a 2a 2a 20 73 75 72 me`) that.** sur
55f0: 72 6f 75 6e 64 20 74 68 65 20 62 6f 64 79 20 6f round the body o
5600: 66 20 74 68 65 20 74 6f 6b 65 6e 20 61 72 65 20 f the token are
5610: 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 removed..**.** T
5620: 6f 6b 65 6e 73 20 61 72 65 20 6f 66 74 65 6e 20 okens are often
5630: 6a 75 73 74 20 70 6f 69 6e 74 65 72 73 20 69 6e just pointers in
5640: 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 to the original
5650: 53 51 4c 20 74 65 78 74 20 61 6e 64 20 73 6f 0a SQL text and so.
5660: 2a 2a 20 61 72 65 20 6e 6f 74 20 5c 30 30 30 20 ** are not \000
5670: 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 61 terminated and a
5680: 72 65 20 6e 6f 74 20 70 65 72 73 69 73 74 65 6e re not persisten
5690: 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 t. The returned
56a0: 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 5c 30 string.** is \0
56b0: 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 00 terminated an
56c0: 64 20 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e d is persistent.
56d0: 0a 2a 2f 0a 63 68 61 72 20 2a 73 71 6c 69 74 65 .*/.char *sqlite
56e0: 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 3NameFromToken(s
56f0: 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 qlite3 *db, Toke
5700: 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 n *pName){. cha
5710: 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 r *zName;. if(
5720: 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 pName ){. zNa
5730: 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
5740: 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a rNDup(db, (char*
5750: 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 )pName->z, pName
5760: 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ->n);. sqlite
5770: 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 3Dequote(zName);
5780: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4e . }else{. zN
5790: 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ame = 0;. }. r
57a0: 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a eturn zName;.}..
57b0: 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 73 /*.** Open the s
57c0: 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
57d0: 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 le stored in dat
57e0: 61 62 61 73 65 20 6e 75 6d 62 65 72 20 69 44 62 abase number iDb
57f0: 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 2e for.** writing.
5800: 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6f 70 The table is op
5810: 65 6e 65 64 20 75 73 69 6e 67 20 63 75 72 73 6f ened using curso
5820: 72 20 30 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c r 0..*/.void sql
5830: 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 ite3OpenMasterTa
5840: 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69 6e ble(Parse *p, in
5850: 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a t iDb){. Vdbe *
5860: 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
5870: 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 be(p);. sqlite3
5880: 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 62 TableLock(p, iDb
5890: 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 , MASTER_ROOT, 1
58a0: 2c 20 4d 41 53 54 45 52 5f 4e 41 4d 45 29 3b 0a , MASTER_NAME);.
58b0: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
58c0: 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 4f 70 65 Op4Int(v, OP_Ope
58d0: 6e 57 72 69 74 65 2c 20 30 2c 20 4d 41 53 54 45 nWrite, 0, MASTE
58e0: 52 5f 52 4f 4f 54 2c 20 69 44 62 2c 20 35 29 3b R_ROOT, iDb, 5);
58f0: 0a 20 20 69 66 28 20 70 2d 3e 6e 54 61 62 3d 3d . if( p->nTab==
5900: 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 54 61 62 0 ){. p->nTab
5910: 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a = 1;. }.}../*.
5920: 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 ** Parameter zNa
5930: 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e me points to a n
5940: 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 62 75 ul-terminated bu
5950: 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ffer containing
5960: 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 the name.** of a
5970: 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 69 6e database ("main
5980: 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65 ", "temp" or the
5990: 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61 name of an atta
59a0: 63 68 65 64 20 64 62 29 2e 20 54 68 69 73 0a 2a ched db). This.*
59b0: 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 * function retur
59c0: 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 ns the index of
59d0: 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 the named databa
59e0: 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c se in db->aDb[],
59f0: 20 6f 72 0a 2a 2a 20 2d 31 20 69 66 20 74 68 65 or.** -1 if the
5a00: 20 6e 61 6d 65 64 20 64 62 20 63 61 6e 6e 6f 74 named db cannot
5a10: 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e be found..*/.in
5a20: 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e t sqlite3FindDbN
5a30: 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ame(sqlite3 *db,
5a40: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
5a50: 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 2d me){. int i = -
5a60: 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 1; /* Da
5a70: 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f tabase number */
5a80: 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a . if( zName ){.
5a90: 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 Db *pDb;.
5aa0: 20 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d for(i=(db->nDb-
5ab0: 31 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 1), pDb=&db->aDb
5ac0: 5b 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 [i]; i>=0; i--,
5ad0: 70 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 pDb--){. if
5ae0: 28 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72 ( 0==sqlite3_str
5af0: 69 63 6d 70 28 70 44 62 2d 3e 7a 44 62 53 4e 61 icmp(pDb->zDbSNa
5b00: 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 20 62 72 65 me, zName) ) bre
5b10: 61 6b 3b 0a 20 20 20 20 20 20 2f 2a 20 22 6d 61 ak;. /* "ma
5b20: 69 6e 22 20 69 73 20 61 6c 77 61 79 73 20 61 6e in" is always an
5b30: 20 61 63 63 65 70 74 61 62 6c 65 20 61 6c 69 61 acceptable alia
5b40: 73 20 66 6f 72 20 74 68 65 20 70 72 69 6d 61 72 s for the primar
5b50: 79 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 y database.
5b60: 20 2a 2a 20 65 76 65 6e 20 69 66 20 69 74 20 68 ** even if it h
5b70: 61 73 20 62 65 65 6e 20 72 65 6e 61 6d 65 64 20 as been renamed
5b80: 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 44 42 43 using SQLITE_DBC
5b90: 4f 4e 46 49 47 5f 4d 41 49 4e 44 42 4e 41 4d 45 ONFIG_MAINDBNAME
5ba0: 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 . */. if( i
5bb0: 3d 3d 30 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 ==0 && 0==sqlite
5bc0: 33 5f 73 74 72 69 63 6d 70 28 22 6d 61 69 6e 22 3_stricmp("main"
5bd0: 2c 20 7a 4e 61 6d 65 29 20 29 20 62 72 65 61 6b , zName) ) break
5be0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
5bf0: 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn i;.}../*.**
5c00: 20 54 68 65 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d The token *pNam
5c10: 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e e contains the n
5c20: 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 ame of a databas
5c30: 65 20 28 65 69 74 68 65 72 20 22 6d 61 69 6e 22 e (either "main"
5c40: 20 6f 72 0a 2a 2a 20 22 74 65 6d 70 22 20 6f 72 or.** "temp" or
5c50: 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 the name of an
5c60: 61 74 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 attached db). Th
5c70: 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
5c80: 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 ns the.** index
5c90: 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 of the named dat
5ca0: 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 abase in db->aDb
5cb0: 5b 5d 2c 20 6f 72 20 2d 31 20 69 66 20 74 68 65 [], or -1 if the
5cc0: 20 6e 61 6d 65 64 20 64 62 20 0a 2a 2a 20 64 6f named db .** do
5cd0: 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2f es not exist..*/
5ce0: 0a 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 .int sqlite3Find
5cf0: 44 62 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 Db(sqlite3 *db,
5d00: 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 Token *pName){.
5d10: 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
5d20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5d30: 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
5d40: 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68 e number */. ch
5d50: 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 ar *zName;
5d60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5d70: 20 20 20 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72 /* Name we ar
5d80: 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 e searching for
5d90: 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c */. zName = sql
5da0: 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
5db0: 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 n(db, pName);.
5dc0: 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 i = sqlite3FindD
5dd0: 62 4e 61 6d 65 28 64 62 2c 20 7a 4e 61 6d 65 29 bName(db, zName)
5de0: 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
5df0: 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 e(db, zName);.
5e00: 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 return i;.}../*
5e10: 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 The table or vie
5e20: 77 20 6f 72 20 74 72 69 67 67 65 72 20 6e 61 6d w or trigger nam
5e30: 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 e is passed to t
5e40: 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69 61 20 his routine via
5e50: 74 6f 6b 65 6e 73 0a 2a 2a 20 70 4e 61 6d 65 31 tokens.** pName1
5e60: 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49 66 20 and pName2. If
5e70: 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 the table name w
5e80: 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 as fully qualifi
5e90: 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a ed, for example:
5ea0: 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 .**.** CREATE TA
5eb0: 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e BLE xxx.yyy (...
5ec0: 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 6e 20 70 );.** .** Then p
5ed0: 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 Name1 is set to
5ee0: 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 "xxx" and pName2
5ef0: 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f "yyy". On the o
5f00: 74 68 65 72 20 68 61 6e 64 20 69 66 0a 2a 2a 20 ther hand if.**
5f10: 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 the table name i
5f20: 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c s not fully qual
5f30: 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a ified, i.e.:.**.
5f40: 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 ** CREATE TABLE
5f50: 79 79 79 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 yyy(...);.**.**
5f60: 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 Then pName1 is s
5f70: 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e 64 20 et to "yyy" and
5f80: 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a 2a 2a pName2 is ""..**
5f90: 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
5fa0: 20 73 65 74 73 20 74 68 65 20 2a 70 70 55 6e 71 sets the *ppUnq
5fb0: 75 61 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 ual pointer to p
5fc0: 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f 6b 65 oint at the toke
5fd0: 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 2a 2a 20 n (pName1 or.**
5fe0: 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73 74 6f pName2) that sto
5ff0: 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c 69 66 res the unqualif
6000: 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 ied table name.
6010: 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 The index of th
6020: 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 22 78 e.** database "x
6030: 78 78 22 20 69 73 20 72 65 74 75 72 6e 65 64 2e xx" is returned.
6040: 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 54 .*/.int sqlite3T
6050: 77 6f 50 61 72 74 4e 61 6d 65 28 0a 20 20 50 61 woPartName(. Pa
6060: 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
6070: 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 /* Parsing and
6080: 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 code generating
6090: 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f context */. To
60a0: 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 ken *pName1,
60b0: 20 20 2f 2a 20 54 68 65 20 22 78 78 78 22 20 69 /* The "xxx" i
60c0: 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 2e n the name "xxx.
60d0: 79 79 79 22 20 6f 72 20 22 78 78 78 22 20 2a 2f yyy" or "xxx" */
60e0: 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 . Token *pName2
60f0: 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 79 , /* The "y
6100: 79 79 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 yy" in the name
6110: 22 78 78 78 2e 79 79 79 22 20 2a 2f 0a 20 20 54 "xxx.yyy" */. T
6120: 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 61 6c 20 20 oken **pUnqual
6130: 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
6140: 75 6e 71 75 61 6c 69 66 69 65 64 20 6f 62 6a 65 unqualified obje
6150: 63 74 20 6e 61 6d 65 20 68 65 72 65 20 2a 2f 0a ct name here */.
6160: 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 ){. int iDb;
6170: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6180: 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 6f 6c /* Database hol
6190: 64 69 6e 67 20 74 68 65 20 6f 62 6a 65 63 74 20 ding the object
61a0: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
61b0: 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a = pParse->db;..
61c0: 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32 assert( pName2
61d0: 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4e 61 !=0 );. if( pNa
61e0: 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 me2->n>0 ){.
61f0: 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 if( db->init.bus
6200: 79 20 29 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 y ) {. sqli
6210: 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
6220: 73 65 2c 20 22 63 6f 72 72 75 70 74 20 64 61 74 se, "corrupt dat
6230: 61 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20 72 abase");. r
6240: 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a eturn -1;. }.
6250: 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 *pUnqual = p
6260: 4e 61 6d 65 32 3b 0a 20 20 20 20 69 44 62 20 3d Name2;. iDb =
6270: 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64 sqlite3FindDb(d
6280: 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 b, pName1);.
6290: 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 if( iDb<0 ){.
62a0: 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
62b0: 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e sg(pParse, "unkn
62c0: 6f 77 6e 20 64 61 74 61 62 61 73 65 20 25 54 22 own database %T"
62d0: 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 , pName1);.
62e0: 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 return -1;.
62f0: 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 }. }else{. a
6300: 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e ssert( db->init.
6310: 69 44 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e iDb==0 || db->in
6320: 69 74 2e 62 75 73 79 20 7c 7c 20 49 4e 5f 52 45 it.busy || IN_RE
6330: 4e 41 4d 45 5f 4f 42 4a 45 43 54 0a 20 20 20 20 NAME_OBJECT.
6340: 20 20 20 20 20 20 20 20 20 7c 7c 20 28 64 62 2d || (db-
6350: 3e 6d 44 62 46 6c 61 67 73 20 26 20 44 42 46 4c >mDbFlags & DBFL
6360: 41 47 5f 56 61 63 75 75 6d 29 21 3d 30 29 3b 0a AG_Vacuum)!=0);.
6370: 20 20 20 20 69 44 62 20 3d 20 64 62 2d 3e 69 6e iDb = db->in
6380: 69 74 2e 69 44 62 3b 0a 20 20 20 20 2a 70 55 6e it.iDb;. *pUn
6390: 71 75 61 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 qual = pName1;.
63a0: 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 44 62 3b }. return iDb;
63b0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
63c0: 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
63d0: 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 55 o check if the U
63e0: 54 46 2d 38 20 73 74 72 69 6e 67 20 7a 4e 61 6d TF-8 string zNam
63f0: 65 20 69 73 20 61 20 6c 65 67 61 6c 0a 2a 2a 20 e is a legal.**
6400: 75 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 unqualified name
6410: 20 66 6f 72 20 61 20 6e 65 77 20 73 63 68 65 6d for a new schem
6420: 61 20 6f 62 6a 65 63 74 20 28 74 61 62 6c 65 2c a object (table,
6430: 20 69 6e 64 65 78 2c 20 76 69 65 77 20 6f 72 0a index, view or.
6440: 2a 2a 20 74 72 69 67 67 65 72 29 2e 20 41 6c 6c ** trigger). All
6450: 20 6e 61 6d 65 73 20 61 72 65 20 6c 65 67 61 6c names are legal
6460: 20 65 78 63 65 70 74 20 74 68 6f 73 65 20 74 68 except those th
6470: 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 at begin with th
6480: 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 73 71 6c e string.** "sql
6490: 69 74 65 5f 22 20 28 69 6e 20 75 70 70 65 72 2c ite_" (in upper,
64a0: 20 6c 6f 77 65 72 20 6f 72 20 6d 69 78 65 64 20 lower or mixed
64b0: 63 61 73 65 29 2e 20 54 68 69 73 20 70 6f 72 74 case). This port
64c0: 69 6f 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65 73 ion of the names
64d0: 70 61 63 65 0a 2a 2a 20 69 73 20 72 65 73 65 72 pace.** is reser
64e0: 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c ved for internal
64f0: 20 75 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c use..*/.int sql
6500: 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e ite3CheckObjectN
6510: 61 6d 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ame(Parse *pPars
6520: 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
6530: 4e 61 6d 65 29 7b 0a 20 20 69 66 28 20 21 70 50 Name){. if( !pP
6540: 61 72 73 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 arse->db->init.b
6550: 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e usy && pParse->n
6560: 65 73 74 65 64 3d 3d 30 20 0a 20 20 20 20 20 20 ested==0 .
6570: 20 20 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e && (pParse->
6580: 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
6590: 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d TE_WriteSchema)=
65a0: 3d 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 =0. &&
65b0: 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 0==sqlite3StrNIC
65c0: 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 mp(zName, "sqlit
65d0: 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20 20 20 73 e_", 7) ){. s
65e0: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
65f0: 50 61 72 73 65 2c 20 22 6f 62 6a 65 63 74 20 6e Parse, "object n
6600: 61 6d 65 20 72 65 73 65 72 76 65 64 20 66 6f 72 ame reserved for
6610: 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 3a 20 25 internal use: %
6620: 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 s", zName);.
6630: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
6640: 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ROR;. }. retur
6650: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
6660: 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
6670: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 64 PRIMARY KEY ind
6680: 65 78 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a 2f ex of a table.*/
6690: 0a 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 50 .Index *sqlite3P
66a0: 72 69 6d 61 72 79 4b 65 79 49 6e 64 65 78 28 54 rimaryKeyIndex(T
66b0: 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 49 able *pTab){. I
66c0: 6e 64 65 78 20 2a 70 3b 0a 20 20 66 6f 72 28 70 ndex *p;. for(p
66d0: 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
66e0: 20 26 26 20 21 49 73 50 72 69 6d 61 72 79 4b 65 && !IsPrimaryKe
66f0: 79 49 6e 64 65 78 28 70 29 3b 20 70 3d 70 2d 3e yIndex(p); p=p->
6700: 70 4e 65 78 74 29 7b 7d 0a 20 20 72 65 74 75 72 pNext){}. retur
6710: 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n p;.}../*.** Re
6720: 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 turn the column
6730: 6f 66 20 69 6e 64 65 78 20 70 49 64 78 20 74 68 of index pIdx th
6740: 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 at corresponds t
6750: 6f 20 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d o table.** colum
6760: 6e 20 69 43 6f 6c 2e 20 20 52 65 74 75 72 6e 20 n iCol. Return
6770: 2d 31 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e -1 if not found.
6780: 0a 2a 2f 0a 69 31 36 20 73 71 6c 69 74 65 33 43 .*/.i16 sqlite3C
6790: 6f 6c 75 6d 6e 4f 66 49 6e 64 65 78 28 49 6e 64 olumnOfIndex(Ind
67a0: 65 78 20 2a 70 49 64 78 2c 20 69 31 36 20 69 43 ex *pIdx, i16 iC
67b0: 6f 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 ol){. int i;.
67c0: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d for(i=0; i<pIdx-
67d0: 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a >nColumn; i++){.
67e0: 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 49 if( iCol==pI
67f0: 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 dx->aiColumn[i]
6800: 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 7d 0a ) return i;. }.
6810: 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a return -1;.}..
6820: 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 63 6f 6e 73 /*.** Begin cons
6830: 74 72 75 63 74 69 6e 67 20 61 20 6e 65 77 20 74 tructing a new t
6840: 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 able representat
6850: 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 ion in memory.
6860: 54 68 69 73 20 69 73 0a 2a 2a 20 74 68 65 20 66 This is.** the f
6870: 69 72 73 74 20 6f 66 20 73 65 76 65 72 61 6c 20 irst of several
6880: 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 action routines
6890: 74 68 61 74 20 67 65 74 20 63 61 6c 6c 65 64 20 that get called
68a0: 69 6e 20 72 65 73 70 6f 6e 73 65 0a 2a 2a 20 74 in response.** t
68b0: 6f 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 o a CREATE TABLE
68c0: 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 statement. In
68d0: 70 61 72 74 69 63 75 6c 61 72 2c 20 74 68 69 73 particular, this
68e0: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
68f0: 65 64 0a 2a 2a 20 61 66 74 65 72 20 73 65 65 69 ed.** after seei
6900: 6e 67 20 74 6f 6b 65 6e 73 20 22 43 52 45 41 54 ng tokens "CREAT
6910: 45 22 20 61 6e 64 20 22 54 41 42 4c 45 22 20 61 E" and "TABLE" a
6920: 6e 64 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d nd the table nam
6930: 65 2e 20 54 68 65 20 69 73 54 65 6d 70 0a 2a 2a e. The isTemp.**
6940: 20 66 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 flag is true if
6950: 20 74 68 65 20 74 61 62 6c 65 20 73 68 6f 75 6c the table shoul
6960: 64 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 d be stored in t
6970: 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 he auxiliary dat
6980: 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 6e abase.** file in
6990: 73 74 65 61 64 20 6f 66 20 69 6e 20 74 68 65 20 stead of in the
69a0: 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
69b0: 6c 65 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 le. This is nor
69c0: 6d 61 6c 6c 79 20 74 68 65 20 63 61 73 65 0a 2a mally the case.*
69d0: 2a 20 77 68 65 6e 20 74 68 65 20 22 54 45 4d 50 * when the "TEMP
69e0: 22 20 6f 72 20 22 54 45 4d 50 4f 52 41 52 59 22 " or "TEMPORARY"
69f0: 20 6b 65 79 77 6f 72 64 20 6f 63 63 75 72 73 20 keyword occurs
6a00: 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 43 52 in between.** CR
6a10: 45 41 54 45 20 61 6e 64 20 54 41 42 4c 45 2e 0a EATE and TABLE..
6a20: 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 74 61 **.** The new ta
6a30: 62 6c 65 20 72 65 63 6f 72 64 20 69 73 20 69 6e ble record is in
6a40: 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 20 70 75 itialized and pu
6a50: 74 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 t in pParse->pNe
6a60: 77 54 61 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f wTable..** As mo
6a70: 72 65 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 re of the CREATE
6a80: 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
6a90: 20 69 73 20 70 61 72 73 65 64 2c 20 61 64 64 69 is parsed, addi
6aa0: 74 69 6f 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a tional action.**
6ab0: 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20 62 routines will b
6ac0: 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 64 64 20 e called to add
6ad0: 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e more information
6ae0: 20 74 6f 20 74 68 69 73 20 72 65 63 6f 72 64 2e to this record.
6af0: 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f .** At the end o
6b00: 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 f the CREATE TAB
6b10: 4c 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 LE statement, th
6b20: 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c e sqlite3EndTabl
6b30: 65 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 e() routine.** i
6b40: 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 s called to comp
6b50: 6c 65 74 65 20 74 68 65 20 63 6f 6e 73 74 72 75 lete the constru
6b60: 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 ction of the new
6b70: 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a table record..*
6b80: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 /.void sqlite3St
6b90: 61 72 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73 artTable(. Pars
6ba0: 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 e *pParse, /*
6bb0: 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a Parser context *
6bc0: 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
6bd0: 31 2c 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 1, /* First pa
6be0: 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f rt of the name o
6bf0: 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 f the table or v
6c00: 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a iew */. Token *
6c10: 70 4e 61 6d 65 32 2c 20 20 20 2f 2a 20 53 65 63 pName2, /* Sec
6c20: 6f 6e 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 ond part of the
6c30: 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
6c40: 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 e or view */. i
6c50: 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 nt isTemp,
6c60: 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
6c70: 69 73 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 is a TEMP table
6c80: 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c */. int isView,
6c90: 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
6ca0: 20 74 68 69 73 20 69 73 20 61 20 56 49 45 57 20 this is a VIEW
6cb0: 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 72 74 75 */. int isVirtu
6cc0: 61 6c 2c 20 20 20 2f 2a 20 54 72 75 65 20 69 66 al, /* True if
6cd0: 20 74 68 69 73 20 69 73 20 61 20 56 49 52 54 55 this is a VIRTU
6ce0: 41 4c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e AL table */. in
6cf0: 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 2f t noErr /
6d00: 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 * Do nothing if
6d10: 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 table already ex
6d20: 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 ists */.){. Tab
6d30: 6c 65 20 2a 70 54 61 62 6c 65 3b 0a 20 20 63 68 le *pTable;. ch
6d40: 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f ar *zName = 0; /
6d50: 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
6d60: 65 20 6e 65 77 20 74 61 62 6c 65 20 2a 2f 0a 20 e new table */.
6d70: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
6d80: 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 Parse->db;. Vdb
6d90: 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b e *v;. int iDb;
6da0: 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
6db0: 62 61 73 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 base number to c
6dc0: 72 65 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 reate the table
6dd0: 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 in */. Token *p
6de0: 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 55 6e 71 75 Name; /* Unqu
6df0: 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 alified name of
6e00: 74 68 65 20 74 61 62 6c 65 20 74 6f 20 63 72 65 the table to cre
6e10: 61 74 65 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 ate */.. if( db
6e20: 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 64 ->init.busy && d
6e30: 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3d b->init.newTnum=
6e40: 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70 65 =1 ){. /* Spe
6e50: 63 69 61 6c 20 63 61 73 65 3a 20 20 50 61 72 73 cial case: Pars
6e60: 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d ing the sqlite_m
6e70: 61 73 74 65 72 20 6f 72 20 73 71 6c 69 74 65 5f aster or sqlite_
6e80: 74 65 6d 70 5f 6d 61 73 74 65 72 20 73 63 68 65 temp_master sche
6e90: 6d 61 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 ma */. iDb =
6ea0: 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20 20 db->init.iDb;.
6eb0: 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 zName = sqlite
6ec0: 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 53 43 3DbStrDup(db, SC
6ed0: 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 29 HEMA_TABLE(iDb))
6ee0: 3b 0a 20 20 20 20 70 4e 61 6d 65 20 3d 20 70 4e ;. pName = pN
6ef0: 61 6d 65 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ame1;. }else{.
6f00: 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 6d 6f 6e /* The common
6f10: 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 69 44 62 case */. iDb
6f20: 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 = sqlite3TwoPar
6f30: 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e tName(pParse, pN
6f40: 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 ame1, pName2, &p
6f50: 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 Name);. if( i
6f60: 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 Db<0 ) return;.
6f70: 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d if( !OMIT_TEM
6f80: 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 26 26 PDB && isTemp &&
6f90: 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 26 26 20 pName2->n>0 &&
6fa0: 69 44 62 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 iDb!=1 ){.
6fb0: 2f 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 /* If creating a
6fc0: 20 74 65 6d 70 20 74 61 62 6c 65 2c 20 74 68 65 temp table, the
6fd0: 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65 name may not be
6fe0: 20 71 75 61 6c 69 66 69 65 64 2e 20 55 6e 6c 65 qualified. Unle
6ff0: 73 73 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 ss . ** the
7000: 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 database name i
7010: 73 20 22 74 65 6d 70 22 20 61 6e 79 77 61 79 2e s "temp" anyway.
7020: 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
7030: 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
7040: 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74 61 e, "temporary ta
7050: 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 ble name must be
7060: 20 75 6e 71 75 61 6c 69 66 69 65 64 22 29 3b 0a unqualified");.
7070: 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
7080: 20 20 7d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 }. if( !OMI
7090: 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 T_TEMPDB && isTe
70a0: 6d 70 20 29 20 69 44 62 20 3d 20 31 3b 0a 20 20 mp ) iDb = 1;.
70b0: 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 zName = sqlite
70c0: 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
70d0: 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 b, pName);. i
70e0: 66 28 20 49 4e 5f 52 45 4e 41 4d 45 5f 4f 42 4a f( IN_RENAME_OBJ
70f0: 45 43 54 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ECT ){. sql
7100: 69 74 65 33 52 65 6e 61 6d 65 54 6f 6b 65 6e 4d ite3RenameTokenM
7110: 61 70 28 70 50 61 72 73 65 2c 20 28 76 6f 69 64 ap(pParse, (void
7120: 2a 29 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 29 3b *)zName, pName);
7130: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 50 61 . }. }. pPa
7140: 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 rse->sNameToken
7150: 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 66 28 20 = *pName;. if(
7160: 7a 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 zName==0 ) retur
7170: 6e 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f n;. if( SQLITE_
7180: 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b OK!=sqlite3Check
7190: 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 ObjectName(pPars
71a0: 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 e, zName) ){.
71b0: 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c goto begin_tabl
71c0: 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 e_error;. }. i
71d0: 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d f( db->init.iDb=
71e0: 3d 31 20 29 20 69 73 54 65 6d 70 20 3d 20 31 3b =1 ) isTemp = 1;
71f0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
7200: 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
7210: 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 69 73 54 ON. assert( isT
7220: 65 6d 70 3d 3d 30 20 7c 7c 20 69 73 54 65 6d 70 emp==0 || isTemp
7230: 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==1 );. assert(
7240: 20 69 73 56 69 65 77 3d 3d 30 20 7c 7c 20 69 73 isView==0 || is
7250: 56 69 65 77 3d 3d 31 20 29 3b 0a 20 20 7b 0a 20 View==1 );. {.
7260: 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
7270: 75 38 20 61 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 u8 aCode[] = {.
7280: 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 SQLITE_CRE
7290: 41 54 45 5f 54 41 42 4c 45 2c 0a 20 20 20 20 20 ATE_TABLE,.
72a0: 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f SQLITE_CREATE_
72b0: 54 45 4d 50 5f 54 41 42 4c 45 2c 0a 20 20 20 20 TEMP_TABLE,.
72c0: 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 SQLITE_CREATE
72d0: 5f 56 49 45 57 2c 0a 20 20 20 20 20 20 20 53 51 _VIEW,. SQ
72e0: 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 LITE_CREATE_TEMP
72f0: 5f 56 49 45 57 0a 20 20 20 20 7d 3b 0a 20 20 20 _VIEW. };.
7300: 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d char *zDb = db-
7310: 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 4e 61 >aDb[iDb].zDbSNa
7320: 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 me;. if( sqli
7330: 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
7340: 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 rse, SQLITE_INSE
7350: 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 RT, SCHEMA_TABLE
7360: 28 69 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 (isTemp), 0, zDb
7370: 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ) ){. goto
7380: 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f begin_table_erro
7390: 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 r;. }. if(
73a0: 20 21 69 73 56 69 72 74 75 61 6c 20 26 26 20 73 !isVirtual && s
73b0: 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
73c0: 70 50 61 72 73 65 2c 20 28 69 6e 74 29 61 43 6f pParse, (int)aCo
73d0: 64 65 5b 69 73 54 65 6d 70 2b 32 2a 69 73 56 69 de[isTemp+2*isVi
73e0: 65 77 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ew],.
73f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7400: 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d zNam
7410: 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 e, 0, zDb) ){.
7420: 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 goto begin_t
7430: 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 able_error;.
7440: 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }. }.#endif..
7450: 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
7460: 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 new table name
7470: 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 does not collide
7480: 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e with an existin
7490: 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 g. ** index or
74a0: 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 table name in th
74b0: 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e e same database.
74c0: 20 20 49 73 73 75 65 20 61 6e 20 65 72 72 6f 72 Issue an error
74d0: 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20 2a 2a message if. **
74e0: 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20 65 78 it does. The ex
74f0: 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20 74 68 ception is if th
7500: 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e e statement bein
7510: 67 20 70 61 72 73 65 64 20 77 61 73 20 70 61 73 g parsed was pas
7520: 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73 sed. ** to an s
7530: 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 qlite3_declare_v
7540: 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74 tab() call. In t
7550: 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20 74 68 hat case only th
7560: 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 e column names.
7570: 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20 77 69 ** and types wi
7580: 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f 20 74 ll be used, so t
7590: 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
75a0: 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61 6d 65 to test for name
75b0: 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 space. ** colli
75c0: 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 sions.. */. if
75d0: 28 20 21 49 4e 5f 53 50 45 43 49 41 4c 5f 50 41 ( !IN_SPECIAL_PA
75e0: 52 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72 20 RSE ){. char
75f0: 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 *zDb = db->aDb[i
7600: 44 62 5d 2e 7a 44 62 53 4e 61 6d 65 3b 0a 20 20 Db].zDbSName;.
7610: 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
7620: 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 =sqlite3ReadSche
7630: 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 ma(pParse) ){.
7640: 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 goto begin_t
7650: 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 able_error;.
7660: 7d 0a 20 20 20 20 70 54 61 62 6c 65 20 3d 20 73 }. pTable = s
7670: 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
7680: 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 3b db, zName, zDb);
7690: 0a 20 20 20 20 69 66 28 20 70 54 61 62 6c 65 20 . if( pTable
76a0: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 6e 6f ){. if( !no
76b0: 45 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Err ){. s
76c0: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
76d0: 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 54 Parse, "table %T
76e0: 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 already exists"
76f0: 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 , pName);.
7700: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 }else{. a
7710: 73 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 ssert( !db->init
7720: 2e 62 75 73 79 20 7c 7c 20 43 4f 52 52 55 50 54 .busy || CORRUPT
7730: 5f 44 42 20 29 3b 0a 20 20 20 20 20 20 20 20 73 _DB );. s
7740: 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 qlite3CodeVerify
7750: 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 Schema(pParse, i
7760: 44 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Db);. }.
7770: 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 goto begin_ta
7780: 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d ble_error;. }
7790: 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
77a0: 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e FindIndex(db, zN
77b0: 61 6d 65 2c 20 7a 44 62 29 21 3d 30 20 29 7b 0a ame, zDb)!=0 ){.
77c0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
77d0: 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 orMsg(pParse, "t
77e0: 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
77f0: 61 6e 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 25 an index named %
7800: 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 s", zName);.
7810: 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 goto begin_tab
7820: 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a le_error;. }.
7830: 20 20 7d 0a 0a 20 20 70 54 61 62 6c 65 20 3d 20 }.. pTable =
7840: 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
7850: 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 ero(db, sizeof(T
7860: 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 70 54 able));. if( pT
7870: 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 able==0 ){. a
7880: 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f ssert( db->mallo
7890: 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 70 cFailed );. p
78a0: 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 Parse->rc = SQLI
78b0: 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 TE_NOMEM_BKPT;.
78c0: 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b pParse->nErr+
78d0: 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 +;. goto begi
78e0: 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 n_table_error;.
78f0: 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 }. pTable->zNa
7900: 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 me = zName;. pT
7910: 61 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 able->iPKey = -1
7920: 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 ;. pTable->pSch
7930: 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 ema = db->aDb[iD
7940: 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 b].pSchema;. pT
7950: 61 62 6c 65 2d 3e 6e 54 61 62 52 65 66 20 3d 20 able->nTabRef =
7960: 31 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 1;.#ifdef SQLITE
7970: 5f 44 45 46 41 55 4c 54 5f 52 4f 57 45 53 54 0a _DEFAULT_ROWEST.
7980: 20 20 70 54 61 62 6c 65 2d 3e 6e 52 6f 77 4c 6f pTable->nRowLo
7990: 67 45 73 74 20 3d 20 73 71 6c 69 74 65 33 4c 6f gEst = sqlite3Lo
79a0: 67 45 73 74 28 53 51 4c 49 54 45 5f 44 45 46 41 gEst(SQLITE_DEFA
79b0: 55 4c 54 5f 52 4f 57 45 53 54 29 3b 0a 23 65 6c ULT_ROWEST);.#el
79c0: 73 65 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52 6f se. pTable->nRo
79d0: 77 4c 6f 67 45 73 74 20 3d 20 32 30 30 3b 20 61 wLogEst = 200; a
79e0: 73 73 65 72 74 28 20 32 30 30 3d 3d 73 71 6c 69 ssert( 200==sqli
79f0: 74 65 33 4c 6f 67 45 73 74 28 31 30 34 38 35 37 te3LogEst(104857
7a00: 36 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 61 6) );.#endif. a
7a10: 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 ssert( pParse->p
7a20: 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 NewTable==0 );.
7a30: 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
7a40: 6c 65 20 3d 20 70 54 61 62 6c 65 3b 0a 0a 20 20 le = pTable;..
7a50: 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 /* If this is th
7a60: 65 20 6d 61 67 69 63 20 73 71 6c 69 74 65 5f 73 e magic sqlite_s
7a70: 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 equence table us
7a80: 65 64 20 62 79 20 61 75 74 6f 69 6e 63 72 65 6d ed by autoincrem
7a90: 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 ent,. ** then r
7aa0: 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 65 72 20 ecord a pointer
7ab0: 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 69 6e to this table in
7ac0: 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
7ad0: 73 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a se structure. *
7ae0: 2a 20 73 6f 20 74 68 61 74 20 49 4e 53 45 52 54 * so that INSERT
7af0: 20 63 61 6e 20 66 69 6e 64 20 74 68 65 20 74 61 can find the ta
7b00: 62 6c 65 20 65 61 73 69 6c 79 2e 0a 20 20 2a 2f ble easily.. */
7b10: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
7b20: 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 OMIT_AUTOINCREME
7b30: 4e 54 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 NT. if( !pParse
7b40: 2d 3e 6e 65 73 74 65 64 20 26 26 20 73 74 72 63 ->nested && strc
7b50: 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 mp(zName, "sqlit
7b60: 65 5f 73 65 71 75 65 6e 63 65 22 29 3d 3d 30 20 e_sequence")==0
7b70: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ){. assert( s
7b80: 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 qlite3SchemaMute
7b90: 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 xHeld(db, iDb, 0
7ba0: 29 20 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d ) );. pTable-
7bb0: 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 >pSchema->pSeqTa
7bc0: 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 7d 0a b = pTable;. }.
7bd0: 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 #endif.. /* Beg
7be0: 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68 in generating th
7bf0: 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c e code that will
7c00: 20 69 6e 73 65 72 74 20 74 68 65 20 74 61 62 6c insert the tabl
7c10: 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a 20 20 e record into.
7c20: 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 ** the SQLITE_MA
7c30: 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e 6f 74 STER table. Not
7c40: 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 e in particular
7c50: 74 68 61 74 20 77 65 20 6d 75 73 74 20 67 6f 20 that we must go
7c60: 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64 20 61 ahead. ** and a
7c70: 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65 63 6f llocate the reco
7c80: 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 rd number for th
7c90: 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f e table entry no
7ca0: 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79 0a 20 w. Before any.
7cb0: 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 ** PRIMARY KEY
7cc0: 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77 6f 72 or UNIQUE keywor
7cd0: 64 73 20 61 72 65 20 70 61 72 73 65 64 2e 20 20 ds are parsed.
7ce0: 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 77 Those keywords w
7cf0: 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a 20 69 ill cause. ** i
7d00: 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 72 65 ndices to be cre
7d10: 61 74 65 64 20 61 6e 64 20 74 68 65 20 74 61 62 ated and the tab
7d20: 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74 20 63 le record must c
7d30: 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65 20 0a ome before the .
7d40: 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 20 48 ** indices. H
7d50: 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f 72 64 ence, the record
7d60: 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
7d70: 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6c table must be al
7d80: 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 77 located. ** now
7d90: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 .. */. if( !db
7da0: 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 28 ->init.busy && (
7db0: 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
7dc0: 62 65 28 70 50 61 72 73 65 29 29 21 3d 30 20 29 be(pParse))!=0 )
7dd0: 7b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 31 3b {. int addr1;
7de0: 0a 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 . int fileFor
7df0: 6d 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 mat;. int reg
7e00: 31 2c 20 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 1, reg2, reg3;.
7e10: 20 20 20 2f 2a 20 6e 75 6c 6c 52 6f 77 5b 5d 20 /* nullRow[]
7e20: 69 73 20 61 6e 20 4f 50 5f 52 65 63 6f 72 64 20 is an OP_Record
7e30: 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 6f encoding of a ro
7e40: 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 35 20 4e w containing 5 N
7e50: 55 4c 4c 73 20 2a 2f 0a 20 20 20 20 73 74 61 74 ULLs */. stat
7e60: 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6e 75 ic const char nu
7e70: 6c 6c 52 6f 77 5b 5d 20 3d 20 7b 20 36 2c 20 30 llRow[] = { 6, 0
7e80: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a , 0, 0, 0, 0 };.
7e90: 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e sqlite3Begin
7ea0: 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 WriteOperation(p
7eb0: 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a Parse, 1, iDb);.
7ec0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
7ed0: 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
7ee0: 45 0a 20 20 20 20 69 66 28 20 69 73 56 69 72 74 E. if( isVirt
7ef0: 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ual ){. sql
7f00: 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 ite3VdbeAddOp0(v
7f10: 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 , OP_VBegin);.
7f20: 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
7f30: 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 66 /* If the file f
7f40: 6f 72 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 69 ormat and encodi
7f50: 6e 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ng in the databa
7f60: 73 65 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e se have not been
7f70: 20 73 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 65 set, . ** se
7f80: 74 20 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 t them now..
7f90: 2a 2f 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50 */. reg1 = pP
7fa0: 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d arse->regRowid =
7fb0: 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
7fc0: 0a 20 20 20 20 72 65 67 32 20 3d 20 70 50 61 72 . reg2 = pPar
7fd0: 73 65 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b se->regRoot = ++
7fe0: 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
7ff0: 20 20 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73 reg3 = ++pPars
8000: 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c e->nMem;. sql
8010: 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
8020: 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c , OP_ReadCookie,
8030: 20 69 44 62 2c 20 72 65 67 33 2c 20 42 54 52 45 iDb, reg3, BTRE
8040: 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 29 3b 0a E_FILE_FORMAT);.
8050: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 sqlite3VdbeU
8060: 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 sesBtree(v, iDb)
8070: 3b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 ;. addr1 = sq
8080: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
8090: 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b v, OP_If, reg3);
80a0: 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
80b0: 3b 0a 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 74 ;. fileFormat
80c0: 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 = (db->flags &
80d0: 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c SQLITE_LegacyFil
80e0: 65 46 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20 20 eFmt)!=0 ?.
80f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 3a 1 :
8100: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 SQLITE_MAX_FILE
8110: 5f 46 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71 6c _FORMAT;. sql
8120: 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
8130: 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 , OP_SetCookie,
8140: 69 44 62 2c 20 42 54 52 45 45 5f 46 49 4c 45 5f iDb, BTREE_FILE_
8150: 46 4f 52 4d 41 54 2c 20 66 69 6c 65 46 6f 72 6d FORMAT, fileForm
8160: 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 at);. sqlite3
8170: 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
8180: 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c _SetCookie, iDb,
8190: 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f BTREE_TEXT_ENCO
81a0: 44 49 4e 47 2c 20 45 4e 43 28 64 62 29 29 3b 0a DING, ENC(db));.
81b0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
81c0: 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 umpHere(v, addr1
81d0: 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 );.. /* This
81e0: 6a 75 73 74 20 63 72 65 61 74 65 73 20 61 20 70 just creates a p
81f0: 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f lace-holder reco
8200: 72 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 rd in the sqlite
8210: 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 _master table..
8220: 20 20 20 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 ** The record
8230: 20 63 72 65 61 74 65 64 20 64 6f 65 73 20 6e 6f created does no
8240: 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69 t contain anythi
8250: 6e 67 20 79 65 74 2e 20 20 49 74 20 77 69 6c 6c ng yet. It will
8260: 20 62 65 20 72 65 70 6c 61 63 65 64 0a 20 20 20 be replaced.
8270: 20 2a 2a 20 62 79 20 74 68 65 20 72 65 61 6c 20 ** by the real
8280: 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 20 67 65 entry in code ge
8290: 6e 65 72 61 74 65 64 20 61 74 20 73 71 6c 69 74 nerated at sqlit
82a0: 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20 e3EndTable()..
82b0: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
82c0: 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 6e 65 rowid for the ne
82d0: 77 20 65 6e 74 72 79 20 69 73 20 6c 65 66 74 20 w entry is left
82e0: 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72 in register pPar
82f0: 73 65 2d 3e 72 65 67 52 6f 77 69 64 2e 0a 20 20 se->regRowid..
8300: 20 20 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 ** The root pa
8310: 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
8320: 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20 6c 65 new table is le
8330: 66 74 20 69 6e 20 72 65 67 20 70 50 61 72 73 65 ft in reg pParse
8340: 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a ->regRoot.. *
8350: 2a 20 54 68 65 20 72 6f 77 69 64 20 61 6e 64 20 * The rowid and
8360: 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
8370: 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 65 64 values are need
8380: 65 64 20 62 79 20 74 68 65 20 63 6f 64 65 20 74 ed by the code t
8390: 68 61 74 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 hat. ** sqlit
83a0: 65 33 45 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20 e3EndTable will
83b0: 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f generate.. */
83c0: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
83d0: 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
83e0: 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
83f0: 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
8400: 41 42 4c 45 29 0a 20 20 20 20 69 66 28 20 69 73 ABLE). if( is
8410: 56 69 65 77 20 7c 7c 20 69 73 56 69 72 74 75 61 View || isVirtua
8420: 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 l ){. sqlit
8430: 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
8440: 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 OP_Integer, 0, r
8450: 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a eg2);. }else.
8460: 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 #endif. {.
8470: 20 20 20 70 50 61 72 73 65 2d 3e 61 64 64 72 43 pParse->addrC
8480: 72 54 61 62 20 3d 0a 20 20 20 20 20 20 20 20 20 rTab =.
8490: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
84a0: 33 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 42 74 3(v, OP_CreateBt
84b0: 72 65 65 2c 20 69 44 62 2c 20 72 65 67 32 2c 20 ree, iDb, reg2,
84c0: 42 54 52 45 45 5f 49 4e 54 4b 45 59 29 3b 0a 20 BTREE_INTKEY);.
84d0: 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
84e0: 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 OpenMasterTable(
84f0: 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 pParse, iDb);.
8500: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
8510: 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 Op2(v, OP_NewRow
8520: 69 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a 20 20 id, 0, reg1);.
8530: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
8540: 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 Op4(v, OP_Blob,
8550: 36 2c 20 72 65 67 33 2c 20 30 2c 20 6e 75 6c 6c 6, reg3, 0, null
8560: 52 6f 77 2c 20 50 34 5f 53 54 41 54 49 43 29 3b Row, P4_STATIC);
8570: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
8580: 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 AddOp3(v, OP_Ins
8590: 65 72 74 2c 20 30 2c 20 72 65 67 33 2c 20 72 65 ert, 0, reg3, re
85a0: 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 g1);. sqlite3
85b0: 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 VdbeChangeP5(v,
85c0: 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a OPFLAG_APPEND);.
85d0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
85e0: 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 ddOp0(v, OP_Clos
85f0: 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f e);. }.. /* No
8600: 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72 29 rmal (non-error)
8610: 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 72 65 return. */. re
8620: 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 turn;.. /* If a
8630: 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
8640: 77 65 20 6a 75 6d 70 20 68 65 72 65 20 2a 2f 0a we jump here */.
8650: 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f begin_table_erro
8660: 72 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 r:. sqlite3DbFr
8670: 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 ee(db, zName);.
8680: 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 53 return;.}../* S
8690: 65 74 20 70 72 6f 70 65 72 74 69 65 73 20 6f 66 et properties of
86a0: 20 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 a table column
86b0: 62 61 73 65 64 20 6f 6e 20 74 68 65 20 28 6d 61 based on the (ma
86c0: 67 69 63 61 6c 29 0a 2a 2a 20 6e 61 6d 65 20 6f gical).** name o
86d0: 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f f the column..*/
86e0: 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
86f0: 4c 45 5f 48 49 44 44 45 4e 5f 43 4f 4c 55 4d 4e LE_HIDDEN_COLUMN
8700: 53 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f S.void sqlite3Co
8710: 6c 75 6d 6e 50 72 6f 70 65 72 74 69 65 73 46 72 lumnPropertiesFr
8720: 6f 6d 4e 61 6d 65 28 54 61 62 6c 65 20 2a 70 54 omName(Table *pT
8730: 61 62 2c 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c ab, Column *pCol
8740: 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ){. if( sqlite3
8750: 5f 73 74 72 6e 69 63 6d 70 28 70 43 6f 6c 2d 3e _strnicmp(pCol->
8760: 7a 4e 61 6d 65 2c 20 22 5f 5f 68 69 64 64 65 6e zName, "__hidden
8770: 5f 5f 22 2c 20 31 30 29 3d 3d 30 20 29 7b 0a 20 __", 10)==0 ){.
8780: 20 20 20 70 43 6f 6c 2d 3e 63 6f 6c 46 6c 61 67 pCol->colFlag
8790: 73 20 7c 3d 20 43 4f 4c 46 4c 41 47 5f 48 49 44 s |= COLFLAG_HID
87a0: 44 45 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 DEN;. }else if(
87b0: 20 70 54 61 62 20 26 26 20 70 43 6f 6c 21 3d 70 pTab && pCol!=p
87c0: 54 61 62 2d 3e 61 43 6f 6c 20 26 26 20 28 70 43 Tab->aCol && (pC
87d0: 6f 6c 5b 2d 31 5d 2e 63 6f 6c 46 6c 61 67 73 20 ol[-1].colFlags
87e0: 26 20 43 4f 4c 46 4c 41 47 5f 48 49 44 44 45 4e & COLFLAG_HIDDEN
87f0: 29 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 74 ) ){. pTab->t
8800: 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 4f 4f abFlags |= TF_OO
8810: 4f 48 69 64 64 65 6e 3b 0a 20 20 7d 0a 7d 0a 23 OHidden;. }.}.#
8820: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 endif.../*.** Ad
8830: 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 74 d a new column t
8840: 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 o the table curr
8850: 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 ently being cons
8860: 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 tructed..**.** T
8870: 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 he parser calls
8880: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 63 this routine onc
8890: 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d e for each colum
88a0: 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a n declaration.**
88b0: 20 69 6e 20 61 20 43 52 45 41 54 45 20 54 41 42 in a CREATE TAB
88c0: 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 73 LE statement. s
88d0: 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 qlite3StartTable
88e0: 28 29 20 67 65 74 73 20 63 61 6c 6c 65 64 0a 2a () gets called.*
88f0: 2a 20 66 69 72 73 74 20 74 6f 20 67 65 74 20 74 * first to get t
8900: 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20 20 54 68 hings going. Th
8910: 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
8920: 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 65 61 is called for ea
8930: 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f ch.** column..*/
8940: 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 .void sqlite3Add
8950: 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 Column(Parse *pP
8960: 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 arse, Token *pNa
8970: 6d 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 me, Token *pType
8980: 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 ){. Table *p;.
8990: 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a int i;. char *
89a0: 7a 3b 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 z;. char *zType
89b0: 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c ;. Column *pCol
89c0: 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
89d0: 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
89e0: 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d if( (p = pParse-
89f0: 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 >pNewTable)==0 )
8a00: 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 return;. if( p
8a10: 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c 69 ->nCol+1>db->aLi
8a20: 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
8a30: 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 _COLUMN] ){.
8a40: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
8a50: 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e pParse, "too man
8a60: 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73 22 y columns on %s"
8a70: 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 , p->zName);.
8a80: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a return;. }. z
8a90: 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
8aa0: 6f 63 52 61 77 28 64 62 2c 20 70 4e 61 6d 65 2d ocRaw(db, pName-
8ab0: 3e 6e 20 2b 20 70 54 79 70 65 2d 3e 6e 20 2b 20 >n + pType->n +
8ac0: 32 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 2);. if( z==0 )
8ad0: 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 49 return;. if( I
8ae0: 4e 5f 52 45 4e 41 4d 45 5f 4f 42 4a 45 43 54 20 N_RENAME_OBJECT
8af0: 29 20 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 54 ) sqlite3RenameT
8b00: 6f 6b 65 6e 4d 61 70 28 70 50 61 72 73 65 2c 20 okenMap(pParse,
8b10: 28 76 6f 69 64 2a 29 7a 2c 20 70 4e 61 6d 65 29 (void*)z, pName)
8b20: 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 4e ;. memcpy(z, pN
8b30: 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e ame->z, pName->n
8b40: 29 3b 0a 20 20 7a 5b 70 4e 61 6d 65 2d 3e 6e 5d );. z[pName->n]
8b50: 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 = 0;. sqlite3D
8b60: 65 71 75 6f 74 65 28 7a 29 3b 0a 20 20 66 6f 72 equote(z);. for
8b70: 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b (i=0; i<p->nCol;
8b80: 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 i++){. if( s
8b90: 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 7a qlite3_stricmp(z
8ba0: 2c 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 , p->aCol[i].zNa
8bb0: 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 me)==0 ){.
8bc0: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
8bd0: 70 50 61 72 73 65 2c 20 22 64 75 70 6c 69 63 61 pParse, "duplica
8be0: 74 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 te column name:
8bf0: 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73 %s", z);. s
8c00: 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
8c10: 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 z);. retur
8c20: 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 n;. }. }. i
8c30: 66 28 20 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 f( (p->nCol & 0x
8c40: 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 43 6f 6c 7)==0 ){. Col
8c50: 75 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20 20 20 61 umn *aNew;. a
8c60: 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 New = sqlite3DbR
8c70: 65 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e 61 43 6f ealloc(db,p->aCo
8c80: 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 l,(p->nCol+8)*si
8c90: 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 zeof(p->aCol[0])
8ca0: 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d );. if( aNew=
8cb0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
8cc0: 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 te3DbFree(db, z)
8cd0: 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
8ce0: 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f }. p->aCo
8cf0: 6c 20 3d 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20 l = aNew;. }.
8d00: 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b pCol = &p->aCol[
8d10: 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 p->nCol];. mems
8d20: 65 74 28 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65 et(pCol, 0, size
8d30: 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b of(p->aCol[0]));
8d40: 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d . pCol->zName =
8d50: 20 7a 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f 6c z;. sqlite3Col
8d60: 75 6d 6e 50 72 6f 70 65 72 74 69 65 73 46 72 6f umnPropertiesFro
8d70: 6d 4e 61 6d 65 28 70 2c 20 70 43 6f 6c 29 3b 0a mName(p, pCol);.
8d80: 20 0a 20 20 69 66 28 20 70 54 79 70 65 2d 3e 6e . if( pType->n
8d90: 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 ==0 ){. /* If
8da0: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 74 79 70 there is no typ
8db0: 65 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f 6c e specified, col
8dc0: 75 6d 6e 73 20 68 61 76 65 20 74 68 65 20 64 65 umns have the de
8dd0: 66 61 75 6c 74 20 61 66 66 69 6e 69 74 79 0a 20 fault affinity.
8de0: 20 20 20 2a 2a 20 27 42 4c 4f 42 27 20 77 69 74 ** 'BLOB' wit
8df0: 68 20 61 20 64 65 66 61 75 6c 74 20 73 69 7a 65 h a default size
8e00: 20 6f 66 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a of 4 bytes. */.
8e10: 20 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 pCol->affini
8e20: 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f ty = SQLITE_AFF_
8e30: 42 4c 4f 42 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e BLOB;. pCol->
8e40: 73 7a 45 73 74 20 3d 20 31 3b 0a 23 69 66 64 65 szEst = 1;.#ifde
8e50: 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
8e60: 53 4f 52 54 45 52 5f 52 45 46 45 52 45 4e 43 45 SORTER_REFERENCE
8e70: 53 0a 20 20 20 20 69 66 28 20 34 3e 3d 73 71 6c S. if( 4>=sql
8e80: 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
8e90: 2e 73 7a 53 6f 72 74 65 72 52 65 66 20 29 7b 0a .szSorterRef ){.
8ea0: 20 20 20 20 20 20 70 43 6f 6c 2d 3e 63 6f 6c 46 pCol->colF
8eb0: 6c 61 67 73 20 7c 3d 20 43 4f 4c 46 4c 41 47 5f lags |= COLFLAG_
8ec0: 53 4f 52 54 45 52 52 45 46 3b 0a 20 20 20 20 7d SORTERREF;. }
8ed0: 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b .#endif. }else{
8ee0: 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 7a 20 2b . zType = z +
8ef0: 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
8f00: 28 7a 29 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d (z) + 1;. mem
8f10: 63 70 79 28 7a 54 79 70 65 2c 20 70 54 79 70 65 cpy(zType, pType
8f20: 2d 3e 7a 2c 20 70 54 79 70 65 2d 3e 6e 29 3b 0a ->z, pType->n);.
8f30: 20 20 20 20 7a 54 79 70 65 5b 70 54 79 70 65 2d zType[pType-
8f40: 3e 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c >n] = 0;. sql
8f50: 69 74 65 33 44 65 71 75 6f 74 65 28 7a 54 79 70 ite3Dequote(zTyp
8f60: 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 61 66 e);. pCol->af
8f70: 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 finity = sqlite3
8f80: 41 66 66 69 6e 69 74 79 54 79 70 65 28 7a 54 79 AffinityType(zTy
8f90: 70 65 2c 20 70 43 6f 6c 29 3b 0a 20 20 20 20 70 pe, pCol);. p
8fa0: 43 6f 6c 2d 3e 63 6f 6c 46 6c 61 67 73 20 7c 3d Col->colFlags |=
8fb0: 20 43 4f 4c 46 4c 41 47 5f 48 41 53 54 59 50 45 COLFLAG_HASTYPE
8fc0: 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 43 6f 6c 2b ;. }. p->nCol+
8fd0: 2b 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6e +;. pParse->con
8fe0: 73 74 72 61 69 6e 74 4e 61 6d 65 2e 6e 20 3d 20 straintName.n =
8ff0: 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 0;.}../*.** This
9000: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
9010: 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 ed by the parser
9020: 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 while in the mi
9030: 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 ddle of.** parsi
9040: 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c ng a CREATE TABL
9050: 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 E statement. A
9060: 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 "NOT NULL" const
9070: 72 61 69 6e 74 20 68 61 73 0a 2a 2a 20 62 65 65 raint has.** bee
9080: 6e 20 73 65 65 6e 20 6f 6e 20 61 20 63 6f 6c 75 n seen on a colu
9090: 6d 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e mn. This routin
90a0: 65 20 73 65 74 73 20 74 68 65 20 6e 6f 74 4e 75 e sets the notNu
90b0: 6c 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68 ll flag on.** th
90c0: 65 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 e column current
90d0: 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 ly under constru
90e0: 63 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 ction..*/.void s
90f0: 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c qlite3AddNotNull
9100: 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
9110: 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 int onError){.
9120: 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75 Table *p;. Colu
9130: 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 70 20 3d 20 mn *pCol;. p =
9140: 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
9150: 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c e;. if( p==0 ||
9160: 20 4e 45 56 45 52 28 70 2d 3e 6e 43 6f 6c 3c 31 NEVER(p->nCol<1
9170: 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 ) ) return;. pC
9180: 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d ol = &p->aCol[p-
9190: 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 70 43 6f 6c >nCol-1];. pCol
91a0: 2d 3e 6e 6f 74 4e 75 6c 6c 20 3d 20 28 75 38 29 ->notNull = (u8)
91b0: 6f 6e 45 72 72 6f 72 3b 0a 20 20 70 2d 3e 74 61 onError;. p->ta
91c0: 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61 73 bFlags |= TF_Has
91d0: 4e 6f 74 4e 75 6c 6c 3b 0a 0a 20 20 2f 2a 20 53 NotNull;.. /* S
91e0: 65 74 20 74 68 65 20 75 6e 69 71 4e 6f 74 4e 75 et the uniqNotNu
91f0: 6c 6c 20 66 6c 61 67 20 6f 6e 20 61 6e 79 20 55 ll flag on any U
9200: 4e 49 51 55 45 20 6f 72 20 50 4b 20 69 6e 64 65 NIQUE or PK inde
9210: 78 65 73 20 61 6c 72 65 61 64 79 20 63 72 65 61 xes already crea
9220: 74 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73 ted. ** on this
9230: 20 63 6f 6c 75 6d 6e 2e 20 20 2a 2f 0a 20 20 69 column. */. i
9240: 66 28 20 70 43 6f 6c 2d 3e 63 6f 6c 46 6c 61 67 f( pCol->colFlag
9250: 73 20 26 20 43 4f 4c 46 4c 41 47 5f 55 4e 49 51 s & COLFLAG_UNIQ
9260: 55 45 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 UE ){. Index
9270: 2a 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 *pIdx;. for(p
9280: 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 Idx=p->pIndex; p
9290: 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
92a0: 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 pNext){. as
92b0: 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 4b 65 79 sert( pIdx->nKey
92c0: 43 6f 6c 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e Col==1 && pIdx->
92d0: 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 onError!=OE_None
92e0: 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 );. if( pI
92f0: 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d dx->aiColumn[0]=
9300: 3d 70 2d 3e 6e 43 6f 6c 2d 31 20 29 7b 0a 20 20 =p->nCol-1 ){.
9310: 20 20 20 20 20 20 70 49 64 78 2d 3e 75 6e 69 71 pIdx->uniq
9320: 4e 6f 74 4e 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 NotNull = 1;.
9330: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d }. }. }.}
9340: 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 65 ../*.** Scan the
9350: 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6e 61 6d column type nam
9360: 65 20 7a 54 79 70 65 20 28 6c 65 6e 67 74 68 20 e zType (length
9370: 6e 54 79 70 65 29 20 61 6e 64 20 72 65 74 75 72 nType) and retur
9380: 6e 20 74 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 n the.** associa
9390: 74 65 64 20 61 66 66 69 6e 69 74 79 20 74 79 70 ted affinity typ
93a0: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
93b0: 75 74 69 6e 65 20 64 6f 65 73 20 61 20 63 61 73 utine does a cas
93c0: 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 65 e-independent se
93d0: 61 72 63 68 20 6f 66 20 7a 54 79 70 65 20 66 6f arch of zType fo
93e0: 72 20 74 68 65 20 0a 2a 2a 20 73 75 62 73 74 72 r the .** substr
93f0: 69 6e 67 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c ings in the foll
9400: 6f 77 69 6e 67 20 74 61 62 6c 65 2e 20 49 66 20 owing table. If
9410: 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 one of the subst
9420: 72 69 6e 67 73 20 69 73 0a 2a 2a 20 66 6f 75 6e rings is.** foun
9430: 64 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e d, the correspon
9440: 64 69 6e 67 20 61 66 66 69 6e 69 74 79 20 69 73 ding affinity is
9450: 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 7a 54 returned. If zT
9460: 79 70 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 ype contains.**
9470: 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 more than one of
9480: 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 2c the substrings,
9490: 20 65 6e 74 72 69 65 73 20 74 6f 77 61 72 64 20 entries toward
94a0: 74 68 65 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 74 the top of .** t
94b0: 68 65 20 74 61 62 6c 65 20 74 61 6b 65 20 70 72 he table take pr
94c0: 69 6f 72 69 74 79 2e 20 46 6f 72 20 65 78 61 6d iority. For exam
94d0: 70 6c 65 2c 20 69 66 20 7a 54 79 70 65 20 69 73 ple, if zType is
94e0: 20 27 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20 'BLOBINT', .**
94f0: 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
9500: 45 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a ER is returned..
9510: 2a 2a 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67 20 **.** Substring
9520: 20 20 20 20 7c 20 41 66 66 69 6e 69 74 79 0a 2a | Affinity.*
9530: 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
9540: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
9550: 2d 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 20 20 20 --.** 'INT'
9560: 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 | SQLITE_AFF
9570: 5f 49 4e 54 45 47 45 52 0a 2a 2a 20 27 43 48 41 _INTEGER.** 'CHA
9580: 52 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 R' | SQLI
9590: 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 TE_AFF_TEXT.** '
95a0: 43 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53 CLOB' | S
95b0: 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a QLITE_AFF_TEXT.*
95c0: 2a 20 27 54 45 58 54 27 20 20 20 20 20 20 20 20 * 'TEXT'
95d0: 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 | SQLITE_AFF_TEX
95e0: 54 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 20 20 20 T.** 'BLOB'
95f0: 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f | SQLITE_AFF_
9600: 42 4c 4f 42 0a 2a 2a 20 27 52 45 41 4c 27 20 20 BLOB.** 'REAL'
9610: 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 | SQLITE_A
9620: 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41 FF_REAL.** 'FLOA
9630: 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 ' | SQLIT
9640: 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 44 E_AFF_REAL.** 'D
9650: 4f 55 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 OUB' | SQ
9660: 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a LITE_AFF_REAL.**
9670: 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 .** If none of t
9680: 68 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e he substrings in
9690: 20 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65 the above table
96a0: 20 61 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53 are found,.** S
96b0: 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
96c0: 43 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a C is returned..*
96d0: 2f 0a 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 /.char sqlite3Af
96e0: 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 finityType(const
96f0: 20 63 68 61 72 20 2a 7a 49 6e 2c 20 43 6f 6c 75 char *zIn, Colu
9700: 6d 6e 20 2a 70 43 6f 6c 29 7b 0a 20 20 75 33 32 mn *pCol){. u32
9710: 20 68 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 61 h = 0;. char a
9720: 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f ff = SQLITE_AFF_
9730: 4e 55 4d 45 52 49 43 3b 0a 20 20 63 6f 6e 73 74 NUMERIC;. const
9740: 20 63 68 61 72 20 2a 7a 43 68 61 72 20 3d 20 30 char *zChar = 0
9750: 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 49 6e ;.. assert( zIn
9760: 21 3d 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 !=0 );. while(
9770: 7a 49 6e 5b 30 5d 20 29 7b 0a 20 20 20 20 68 20 zIn[0] ){. h
9780: 3d 20 28 68 3c 3c 38 29 20 2b 20 73 71 6c 69 74 = (h<<8) + sqlit
9790: 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 e3UpperToLower[(
97a0: 2a 7a 49 6e 29 26 30 78 66 66 5d 3b 0a 20 20 20 *zIn)&0xff];.
97b0: 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 69 66 28 20 zIn++;. if(
97c0: 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 h==(('c'<<24)+('
97d0: 68 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 h'<<16)+('a'<<8)
97e0: 2b 27 72 27 29 20 29 7b 20 20 20 20 20 20 20 20 +'r') ){
97f0: 20 20 20 20 20 2f 2a 20 43 48 41 52 20 2a 2f 0a /* CHAR */.
9800: 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 aff = SQLI
9810: 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 TE_AFF_TEXT;.
9820: 20 20 20 7a 43 68 61 72 20 3d 20 7a 49 6e 3b 0a zChar = zIn;.
9830: 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d }else if( h=
9840: 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 6c 27 =(('c'<<24)+('l'
9850: 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 <<16)+('o'<<8)+'
9860: 62 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 b') ){ /*
9870: 43 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 61 66 CLOB */. af
9880: 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 f = SQLITE_AFF_T
9890: 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 EXT;. }else i
98a0: 66 28 20 68 3d 3d 28 28 27 74 27 3c 3c 32 34 29 f( h==(('t'<<24)
98b0: 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 78 27 3c +('e'<<16)+('x'<
98c0: 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20 20 <8)+'t') ){
98d0: 20 20 2f 2a 20 54 45 58 54 20 2a 2f 0a 20 20 20 /* TEXT */.
98e0: 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f aff = SQLITE_
98f0: 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 AFF_TEXT;. }e
9900: 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 62 27 lse if( h==(('b'
9910: 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b <<24)+('l'<<16)+
9920: 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 ('o'<<8)+'b')
9930: 20 20 20 20 20 20 20 2f 2a 20 42 4c 4f 42 20 2a /* BLOB *
9940: 2f 0a 20 20 20 20 20 20 20 20 26 26 20 28 61 66 /. && (af
9950: 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 f==SQLITE_AFF_NU
9960: 4d 45 52 49 43 20 7c 7c 20 61 66 66 3d 3d 53 51 MERIC || aff==SQ
9970: 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 29 20 29 LITE_AFF_REAL) )
9980: 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 {. aff = SQ
9990: 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 3b 0a 20 LITE_AFF_BLOB;.
99a0: 20 20 20 20 20 69 66 28 20 7a 49 6e 5b 30 5d 3d if( zIn[0]=
99b0: 3d 27 28 27 20 29 20 7a 43 68 61 72 20 3d 20 7a ='(' ) zChar = z
99c0: 49 6e 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 In;.#ifndef SQLI
99d0: 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
99e0: 5f 50 4f 49 4e 54 0a 20 20 20 20 7d 65 6c 73 65 _POINT. }else
99f0: 20 69 66 28 20 68 3d 3d 28 28 27 72 27 3c 3c 32 if( h==(('r'<<2
9a00: 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 61 4)+('e'<<16)+('a
9a10: 27 3c 3c 38 29 2b 27 6c 27 29 20 20 20 20 20 20 '<<8)+'l')
9a20: 20 20 20 20 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 /* REAL */.
9a30: 20 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 && aff==S
9a40: 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
9a50: 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d C ){. aff =
9a60: 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
9a70: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
9a80: 68 3d 3d 28 28 27 66 27 3c 3c 32 34 29 2b 28 27 h==(('f'<<24)+('
9a90: 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 l'<<16)+('o'<<8)
9aa0: 2b 27 61 27 29 20 20 20 20 20 20 20 20 20 20 2f +'a') /
9ab0: 2a 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 20 20 20 * FLOA */.
9ac0: 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 && aff==SQLITE
9ad0: 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a _AFF_NUMERIC ){.
9ae0: 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 aff = SQLI
9af0: 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 TE_AFF_REAL;.
9b00: 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 }else if( h==((
9b10: 27 64 27 3c 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 'd'<<24)+('o'<<1
9b20: 36 29 2b 28 27 75 27 3c 3c 38 29 2b 27 62 27 29 6)+('u'<<8)+'b')
9b30: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 4f 55 /* DOU
9b40: 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 B */. &&
9b50: 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f aff==SQLITE_AFF_
9b60: 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 NUMERIC ){.
9b70: 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 aff = SQLITE_AF
9b80: 46 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 F_REAL;.#endif.
9b90: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 68 26 }else if( (h&
9ba0: 30 78 30 30 46 46 46 46 46 46 29 3d 3d 28 28 27 0x00FFFFFF)==(('
9bb0: 69 27 3c 3c 31 36 29 2b 28 27 6e 27 3c 3c 38 29 i'<<16)+('n'<<8)
9bc0: 2b 27 74 27 29 20 29 7b 20 20 20 20 2f 2a 20 49 +'t') ){ /* I
9bd0: 4e 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 NT */. aff
9be0: 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 = SQLITE_AFF_INT
9bf0: 45 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 EGER;. brea
9c00: 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 k;. }. }..
9c10: 2f 2a 20 49 66 20 70 43 6f 6c 20 69 73 20 6e 6f /* If pCol is no
9c20: 74 20 4e 55 4c 4c 2c 20 73 74 6f 72 65 20 61 6e t NULL, store an
9c30: 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 estimate of the
9c40: 20 66 69 65 6c 64 20 73 69 7a 65 2e 20 20 54 68 field size. Th
9c50: 65 0a 20 20 2a 2a 20 65 73 74 69 6d 61 74 65 20 e. ** estimate
9c60: 69 73 20 73 63 61 6c 65 64 20 73 6f 20 74 68 61 is scaled so tha
9c70: 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e t the size of an
9c80: 20 69 6e 74 65 67 65 72 20 69 73 20 31 2e 20 20 integer is 1.
9c90: 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 20 29 7b */. if( pCol ){
9ca0: 0a 20 20 20 20 69 6e 74 20 76 20 3d 20 30 3b 20 . int v = 0;
9cb0: 20 20 2f 2a 20 64 65 66 61 75 6c 74 20 73 69 7a /* default siz
9cc0: 65 20 69 73 20 61 70 70 72 6f 78 20 34 20 62 79 e is approx 4 by
9cd0: 74 65 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 tes */. if( a
9ce0: 66 66 3c 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 ff<SQLITE_AFF_NU
9cf0: 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 69 MERIC ){. i
9d00: 66 28 20 7a 43 68 61 72 20 29 7b 0a 20 20 20 20 f( zChar ){.
9d10: 20 20 20 20 77 68 69 6c 65 28 20 7a 43 68 61 72 while( zChar
9d20: 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 [0] ){.
9d30: 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 64 69 if( sqlite3Isdi
9d40: 67 69 74 28 7a 43 68 61 72 5b 30 5d 29 20 29 7b git(zChar[0]) ){
9d50: 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
9d60: 42 4c 4f 42 28 6b 29 2c 20 56 41 52 43 48 41 52 BLOB(k), VARCHAR
9d70: 28 6b 29 2c 20 43 48 41 52 28 6b 29 20 2d 3e 20 (k), CHAR(k) ->
9d80: 72 3d 28 6b 2f 34 2b 31 29 20 2a 2f 0a 20 20 20 r=(k/4+1) */.
9d90: 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
9da0: 47 65 74 49 6e 74 33 32 28 7a 43 68 61 72 2c 20 GetInt32(zChar,
9db0: 26 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 &v);.
9dc0: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
9dd0: 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 43 }. zC
9de0: 68 61 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d har++;. }
9df0: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
9e00: 20 20 20 20 20 20 76 20 3d 20 31 36 3b 20 20 20 v = 16;
9e10: 2f 2a 20 42 4c 4f 42 2c 20 54 45 58 54 2c 20 43 /* BLOB, TEXT, C
9e20: 4c 4f 42 20 2d 3e 20 72 3d 35 20 20 28 61 70 70 LOB -> r=5 (app
9e30: 72 6f 78 20 32 30 20 62 79 74 65 73 29 2a 2f 0a rox 20 bytes)*/.
9e40: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 }. }.#i
9e50: 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
9e60: 4c 45 5f 53 4f 52 54 45 52 5f 52 45 46 45 52 45 LE_SORTER_REFERE
9e70: 4e 43 45 53 0a 20 20 20 20 69 66 28 20 76 3e 3d NCES. if( v>=
9e80: 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9e90: 66 69 67 2e 73 7a 53 6f 72 74 65 72 52 65 66 20 fig.szSorterRef
9ea0: 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 63 ){. pCol->c
9eb0: 6f 6c 46 6c 61 67 73 20 7c 3d 20 43 4f 4c 46 4c olFlags |= COLFL
9ec0: 41 47 5f 53 4f 52 54 45 52 52 45 46 3b 0a 20 20 AG_SORTERREF;.
9ed0: 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 76 }.#endif. v
9ee0: 20 3d 20 76 2f 34 20 2b 20 31 3b 0a 20 20 20 20 = v/4 + 1;.
9ef0: 69 66 28 20 76 3e 32 35 35 20 29 20 76 20 3d 20 if( v>255 ) v =
9f00: 32 35 35 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 73 255;. pCol->s
9f10: 7a 45 73 74 20 3d 20 76 3b 0a 20 20 7d 0a 20 20 zEst = v;. }.
9f20: 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f return aff;.}../
9f30: 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 *.** The express
9f40: 69 6f 6e 20 69 73 20 74 68 65 20 64 65 66 61 75 ion is the defau
9f50: 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
9f60: 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 most recently a
9f70: 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f dded column.** o
9f80: 66 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 f the table curr
9f90: 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 ently under cons
9fa0: 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 truction..**.**
9fb0: 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 65 78 Default value ex
9fc0: 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 74 20 62 pressions must b
9fd0: 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69 e constant. Rai
9fe0: 73 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 se an exception
9ff0: 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f if this.** is no
a000: 74 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a t the case..**.*
a010: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
a020: 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
a030: 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 parser while in
a040: 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a the middle of.**
a050: 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 parsing a CREAT
a060: 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
a070: 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 t..*/.void sqlit
a080: 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 e3AddDefaultValu
a090: 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
a0a0: 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a se, /*
a0b0: 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
a0c0: 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 */. Expr *pExp
a0d0: 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f r, /
a0e0: 2a 20 54 68 65 20 70 61 72 73 65 64 20 65 78 70 * The parsed exp
a0f0: 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 64 ression of the d
a100: 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a efault value */.
a110: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
a120: 74 61 72 74 2c 20 20 20 20 20 20 2f 2a 20 53 74 tart, /* St
a130: 61 72 74 20 6f 66 20 74 68 65 20 64 65 66 61 75 art of the defau
a140: 6c 74 20 76 61 6c 75 65 20 74 65 78 74 20 2a 2f lt value text */
a150: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
a160: 45 6e 64 20 20 20 20 20 20 20 20 20 2f 2a 20 46 End /* F
a170: 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 70 irst character p
a180: 61 73 74 20 65 6e 64 20 6f 66 20 64 65 66 61 75 ast end of defau
a190: 74 20 76 61 6c 75 65 20 74 65 78 74 20 2a 2f 0a t value text */.
a1a0: 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 ){. Table *p;.
a1b0: 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 Column *pCol;.
a1c0: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
a1d0: 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 20 3d Parse->db;. p =
a1e0: 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
a1f0: 6c 65 3b 0a 20 20 69 66 28 20 70 21 3d 30 20 29 le;. if( p!=0 )
a200: 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20 26 28 70 {. pCol = &(p
a210: 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 ->aCol[p->nCol-1
a220: 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c ]);. if( !sql
a230: 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
a240: 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 70 45 78 ntOrFunction(pEx
a250: 70 72 2c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 pr, db->init.bus
a260: 79 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 y) ){. sqli
a270: 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
a280: 73 65 2c 20 22 64 65 66 61 75 6c 74 20 76 61 6c se, "default val
a290: 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 ue of column [%s
a2a0: 5d 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e ] is not constan
a2b0: 74 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 t",. pC
a2c0: 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ol->zName);.
a2d0: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
a2e0: 41 20 63 6f 70 79 20 6f 66 20 70 45 78 70 72 20 A copy of pExpr
a2f0: 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 is used instead
a300: 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 2c of the original,
a310: 20 61 73 20 70 45 78 70 72 20 63 6f 6e 74 61 69 as pExpr contai
a320: 6e 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 ns. ** toke
a330: 6e 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f ns that point to
a340: 20 76 6f 6c 61 74 69 6c 65 20 6d 65 6d 6f 72 79 volatile memory
a350: 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
a360: 20 45 78 70 72 20 78 3b 0a 20 20 20 20 20 20 73 Expr x;. s
a370: 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
a380: 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 (db, pCol->pDflt
a390: 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 );. memset(
a3a0: 26 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 78 29 &x, 0, sizeof(x)
a3b0: 29 3b 0a 20 20 20 20 20 20 78 2e 6f 70 20 3d 20 );. x.op =
a3c0: 54 4b 5f 53 50 41 4e 3b 0a 20 20 20 20 20 20 78 TK_SPAN;. x
a3d0: 2e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 .u.zToken = sqli
a3e0: 74 65 33 44 62 53 70 61 6e 44 75 70 28 64 62 2c te3DbSpanDup(db,
a3f0: 20 7a 53 74 61 72 74 2c 20 7a 45 6e 64 29 3b 0a zStart, zEnd);.
a400: 20 20 20 20 20 20 78 2e 70 4c 65 66 74 20 3d 20 x.pLeft =
a410: 70 45 78 70 72 3b 0a 20 20 20 20 20 20 78 2e 66 pExpr;. x.f
a420: 6c 61 67 73 20 3d 20 45 50 5f 53 6b 69 70 3b 0a lags = EP_Skip;.
a430: 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c pCol->pDfl
a440: 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 t = sqlite3ExprD
a450: 75 70 28 64 62 2c 20 26 78 2c 20 45 58 50 52 44 up(db, &x, EXPRD
a460: 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 UP_REDUCE);.
a470: 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
a480: 64 62 2c 20 78 2e 75 2e 7a 54 6f 6b 65 6e 29 3b db, x.u.zToken);
a490: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
a4a0: 20 49 4e 5f 52 45 4e 41 4d 45 5f 4f 42 4a 45 43 IN_RENAME_OBJEC
a4b0: 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 T ){. sqlite3
a4c0: 52 65 6e 61 6d 65 45 78 70 72 55 6e 6d 61 70 28 RenameExprUnmap(
a4d0: 70 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a pParse, pExpr);.
a4e0: 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 }. sqlite3Exp
a4f0: 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 rDelete(db, pExp
a500: 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 61 63 r);.}../*.** Bac
a510: 6b 77 61 72 64 73 20 43 6f 6d 70 61 74 69 62 69 kwards Compatibi
a520: 6c 69 74 79 20 48 61 63 6b 3a 0a 2a 2a 20 0a 2a lity Hack:.** .*
a530: 2a 20 48 69 73 74 6f 72 69 63 61 6c 20 76 65 72 * Historical ver
a540: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
a550: 61 63 63 65 70 74 65 64 20 73 74 72 69 6e 67 73 accepted strings
a560: 20 61 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 as column names
a570: 20 69 6e 0a 2a 2a 20 69 6e 64 65 78 65 73 20 61 in.** indexes a
a580: 6e 64 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 nd PRIMARY KEY c
a590: 6f 6e 73 74 72 61 69 6e 74 73 20 61 6e 64 20 69 onstraints and i
a5a0: 6e 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 n UNIQUE constra
a5b0: 69 6e 74 73 2e 20 20 45 78 61 6d 70 6c 65 3a 0a ints. Example:.
a5c0: 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 **.** CREATE
a5d0: 20 54 41 42 4c 45 20 78 79 7a 28 61 2c 62 2c 63 TABLE xyz(a,b,c
a5e0: 2c 64 2c 65 2c 50 52 49 4d 41 52 59 20 4b 45 59 ,d,e,PRIMARY KEY
a5f0: 28 27 61 27 29 2c 55 4e 49 51 55 45 28 27 62 27 ('a'),UNIQUE('b'
a600: 2c 27 63 27 20 43 4f 4c 4c 41 54 45 20 74 72 69 ,'c' COLLATE tri
a610: 6d 29 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 m).** CREATE
a620: 20 49 4e 44 45 58 20 61 62 63 20 4f 4e 20 78 79 INDEX abc ON xy
a630: 7a 28 27 63 27 2c 27 64 27 20 44 45 53 43 2c 27 z('c','d' DESC,'
a640: 65 27 20 43 4f 4c 4c 41 54 45 20 6e 6f 63 61 73 e' COLLATE nocas
a650: 65 20 44 45 53 43 29 3b 0a 2a 2a 0a 2a 2a 20 54 e DESC);.**.** T
a660: 68 69 73 20 69 73 20 67 6f 6f 66 79 2e 20 20 42 his is goofy. B
a670: 75 74 20 74 6f 20 70 72 65 73 65 72 76 65 20 62 ut to preserve b
a680: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
a690: 62 69 6c 69 74 79 20 77 65 20 63 6f 6e 74 69 6e bility we contin
a6a0: 75 65 20 74 6f 0a 2a 2a 20 61 63 63 65 70 74 20 ue to.** accept
a6b0: 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e it. This routin
a6c0: 65 20 64 6f 65 73 20 74 68 65 20 6e 65 63 65 73 e does the neces
a6d0: 73 61 72 79 20 63 6f 6e 76 65 72 73 69 6f 6e 2e sary conversion.
a6e0: 20 20 49 74 20 63 6f 6e 76 65 72 74 73 0a 2a 2a It converts.**
a6f0: 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
a700: 67 69 76 65 6e 20 69 6e 20 69 74 73 20 61 72 67 given in its arg
a710: 75 6d 65 6e 74 20 66 72 6f 6d 20 61 20 54 4b 5f ument from a TK_
a720: 53 54 52 49 4e 47 20 69 6e 74 6f 20 61 20 54 4b STRING into a TK
a730: 5f 49 44 0a 2a 2a 20 69 66 20 74 68 65 20 65 78 _ID.** if the ex
a740: 70 72 65 73 73 69 6f 6e 20 69 73 20 6a 75 73 74 pression is just
a750: 20 61 20 54 4b 5f 53 54 52 49 4e 47 20 77 69 74 a TK_STRING wit
a760: 68 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 43 4f h an optional CO
a770: 4c 4c 41 54 45 20 63 6c 61 75 73 65 2e 0a 2a 2a LLATE clause..**
a780: 20 49 66 20 74 68 65 20 65 70 78 72 65 73 73 69 If the epxressi
a790: 6f 6e 20 69 73 20 61 6e 79 74 68 69 6e 67 20 6f on is anything o
a7a0: 74 68 65 72 20 74 68 61 6e 20 54 4b 5f 53 54 52 ther than TK_STR
a7b0: 49 4e 47 2c 20 74 68 65 20 65 78 70 72 65 73 73 ING, the express
a7c0: 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 63 68 61 6e ion is.** unchan
a7d0: 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ged..*/.static v
a7e0: 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 69 6e oid sqlite3Strin
a7f0: 67 54 6f 49 64 28 45 78 70 72 20 2a 70 29 7b 0a gToId(Expr *p){.
a800: 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f if( p->op==TK_
a810: 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 70 2d STRING ){. p-
a820: 3e 6f 70 20 3d 20 54 4b 5f 49 44 3b 0a 20 20 7d >op = TK_ID;. }
a830: 65 6c 73 65 20 69 66 28 20 70 2d 3e 6f 70 3d 3d else if( p->op==
a840: 54 4b 5f 43 4f 4c 4c 41 54 45 20 26 26 20 70 2d TK_COLLATE && p-
a850: 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 53 >pLeft->op==TK_S
a860: 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 70 2d 3e TRING ){. p->
a870: 70 4c 65 66 74 2d 3e 6f 70 20 3d 20 54 4b 5f 49 pLeft->op = TK_I
a880: 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D;. }.}../*.**
a890: 44 65 73 69 67 6e 61 74 65 20 74 68 65 20 50 52 Designate the PR
a8a0: 49 4d 41 52 59 20 4b 45 59 20 66 6f 72 20 74 68 IMARY KEY for th
a8b0: 65 20 74 61 62 6c 65 2e 20 20 70 4c 69 73 74 20 e table. pList
a8c0: 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d is a list of nam
a8d0: 65 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e es .** of column
a8e0: 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 s that form the
a8f0: 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 primary key. If
a900: 20 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c 2c 20 pList is NULL,
a910: 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 then the.** most
a920: 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 recently added
a930: 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 column of the ta
a940: 62 6c 65 20 69 73 20 74 68 65 20 70 72 69 6d 61 ble is the prima
a950: 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 ry key..**.** A
a960: 74 61 62 6c 65 20 63 61 6e 20 68 61 76 65 20 61 table can have a
a970: 74 20 6d 6f 73 74 20 6f 6e 65 20 70 72 69 6d 61 t most one prima
a980: 72 79 20 6b 65 79 2e 20 20 49 66 20 74 68 65 20 ry key. If the
a990: 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68 61 table already ha
a9a0: 73 0a 2a 2a 20 61 20 70 72 69 6d 61 72 79 20 6b s.** a primary k
a9b0: 65 79 20 28 61 6e 64 20 74 68 69 73 20 69 73 20 ey (and this is
a9c0: 74 68 65 20 73 65 63 6f 6e 64 20 70 72 69 6d 61 the second prima
a9d0: 72 79 20 6b 65 79 29 20 74 68 65 6e 20 63 72 65 ry key) then cre
a9e0: 61 74 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e ate an.** error.
a9f0: 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 52 .**.** If the PR
aa00: 49 4d 41 52 59 20 4b 45 59 20 69 73 20 6f 6e 20 IMARY KEY is on
aa10: 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 a single column
aa20: 77 68 6f 73 65 20 64 61 74 61 74 79 70 65 20 69 whose datatype i
aa30: 73 20 49 4e 54 45 47 45 52 2c 0a 2a 2a 20 74 68 s INTEGER,.** th
aa40: 65 6e 20 77 65 20 77 69 6c 6c 20 74 72 79 20 74 en we will try t
aa50: 6f 20 75 73 65 20 74 68 61 74 20 63 6f 6c 75 6d o use that colum
aa60: 6e 20 61 73 20 74 68 65 20 72 6f 77 69 64 2e 20 n as the rowid.
aa70: 20 53 65 74 20 74 68 65 20 54 61 62 6c 65 2e 69 Set the Table.i
aa80: 50 4b 65 79 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 PKey.** field of
aa90: 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72 the table under
aaa0: 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f construction to
aab0: 20 62 65 20 74 68 65 20 69 6e 64 65 78 20 6f 66 be the index of
aac0: 20 74 68 65 0a 2a 2a 20 49 4e 54 45 47 45 52 20 the.** INTEGER
aad0: 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 PRIMARY KEY colu
aae0: 6d 6e 2e 20 20 54 61 62 6c 65 2e 69 50 4b 65 79 mn. Table.iPKey
aaf0: 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20 69 66 is set to -1 if
ab00: 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 there is.** no
ab10: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
ab20: 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 KEY..**.** If th
ab30: 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 61 6e 20 e key is not an
ab40: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
ab50: 4b 45 59 2c 20 74 68 65 6e 20 63 72 65 61 74 65 KEY, then create
ab60: 20 61 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 64 a unique.** ind
ab70: 65 78 20 66 6f 72 20 74 68 65 20 6b 65 79 2e 20 ex for the key.
ab80: 20 4e 6f 20 69 6e 64 65 78 20 69 73 20 63 72 65 No index is cre
ab90: 61 74 65 64 20 66 6f 72 20 49 4e 54 45 47 45 52 ated for INTEGER
aba0: 20 50 52 49 4d 41 52 59 20 4b 45 59 73 2e 0a 2a PRIMARY KEYs..*
abb0: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 /.void sqlite3Ad
abc0: 64 50 72 69 6d 61 72 79 4b 65 79 28 0a 20 20 50 dPrimaryKey(. P
abd0: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
abe0: 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
abf0: 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ext */. ExprLis
ac00: 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 t *pList, /* Li
ac10: 73 74 20 6f 66 20 66 69 65 6c 64 20 6e 61 6d 65 st of field name
ac20: 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 s to be indexed
ac30: 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 */. int onError
ac40: 2c 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 , /* What t
ac50: 6f 20 64 6f 20 77 69 74 68 20 61 20 75 6e 69 71 o do with a uniq
ac60: 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 ueness conflict
ac70: 2a 2f 0a 20 20 69 6e 74 20 61 75 74 6f 49 6e 63 */. int autoInc
ac80: 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 , /* True i
ac90: 66 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45 4d f the AUTOINCREM
aca0: 45 4e 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 ENT keyword is p
acb0: 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 resent */. int
acc0: 73 6f 72 74 4f 72 64 65 72 20 20 20 20 20 2f 2a sortOrder /*
acd0: 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 6f SQLITE_SO_ASC o
ace0: 72 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 r SQLITE_SO_DESC
acf0: 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a */.){. Table *
ad00: 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 pTab = pParse->p
ad10: 4e 65 77 54 61 62 6c 65 3b 0a 20 20 43 6f 6c 75 NewTable;. Colu
ad20: 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b 0a 20 20 mn *pCol = 0;.
ad30: 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 2c 20 69 int iCol = -1, i
ad40: 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20 ;. int nTerm;.
ad50: 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 67 if( pTab==0 ) g
ad60: 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f oto primary_key_
ad70: 65 78 69 74 3b 0a 20 20 69 66 28 20 70 54 61 62 exit;. if( pTab
ad80: 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f ->tabFlags & TF_
ad90: 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 29 7b HasPrimaryKey ){
ada0: 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
adb0: 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 rMsg(pParse, .
adc0: 20 20 20 20 22 74 61 62 6c 65 20 5c 22 25 73 5c "table \"%s\
add0: 22 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 " has more than
ade0: 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b 65 79 22 one primary key"
adf0: 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
ae00: 20 20 20 20 67 6f 74 6f 20 70 72 69 6d 61 72 79 goto primary
ae10: 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 7d 0a 20 _key_exit;. }.
ae20: 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 pTab->tabFlags
ae30: 7c 3d 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 |= TF_HasPrimary
ae40: 4b 65 79 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 Key;. if( pList
ae50: 3d 3d 30 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20 ==0 ){. iCol
ae60: 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2d 20 31 = pTab->nCol - 1
ae70: 3b 0a 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 ;. pCol = &pT
ae80: 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a ab->aCol[iCol];.
ae90: 20 20 20 20 70 43 6f 6c 2d 3e 63 6f 6c 46 6c 61 pCol->colFla
aea0: 67 73 20 7c 3d 20 43 4f 4c 46 4c 41 47 5f 50 52 gs |= COLFLAG_PR
aeb0: 49 4d 4b 45 59 3b 0a 20 20 20 20 6e 54 65 72 6d IMKEY;. nTerm
aec0: 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
aed0: 20 20 20 6e 54 65 72 6d 20 3d 20 70 4c 69 73 74 nTerm = pList
aee0: 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 66 6f 72 ->nExpr;. for
aef0: 28 69 3d 30 3b 20 69 3c 6e 54 65 72 6d 3b 20 69 (i=0; i<nTerm; i
af00: 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 ++){. Expr
af10: 2a 70 43 45 78 70 72 20 3d 20 73 71 6c 69 74 65 *pCExpr = sqlite
af20: 33 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 74 65 3ExprSkipCollate
af30: 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 (pList->a[i].pEx
af40: 70 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 pr);. asser
af50: 74 28 20 70 43 45 78 70 72 21 3d 30 20 29 3b 0a t( pCExpr!=0 );.
af60: 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
af70: 69 6e 67 54 6f 49 64 28 70 43 45 78 70 72 29 3b ingToId(pCExpr);
af80: 0a 20 20 20 20 20 20 69 66 28 20 70 43 45 78 70 . if( pCExp
af90: 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a r->op==TK_ID ){.
afa0: 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 const ch
afb0: 61 72 20 2a 7a 43 4e 61 6d 65 20 3d 20 70 43 45 ar *zCName = pCE
afc0: 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 xpr->u.zToken;.
afd0: 20 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d for(iCol=
afe0: 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 0; iCol<pTab->nC
aff0: 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 ol; iCol++){.
b000: 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
b010: 65 33 53 74 72 49 43 6d 70 28 7a 43 4e 61 6d 65 e3StrICmp(zCName
b020: 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f , pTab->aCol[iCo
b030: 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a l].zName)==0 ){.
b040: 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c pCol
b050: 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 = &pTab->aCol[i
b060: 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 Col];.
b070: 20 20 70 43 6f 6c 2d 3e 63 6f 6c 46 6c 61 67 73 pCol->colFlags
b080: 20 7c 3d 20 43 4f 4c 46 4c 41 47 5f 50 52 49 4d |= COLFLAG_PRIM
b090: 4b 45 59 3b 0a 20 20 20 20 20 20 20 20 20 20 20 KEY;.
b0a0: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
b0b0: 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
b0c0: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
b0d0: 20 20 69 66 28 20 6e 54 65 72 6d 3d 3d 31 0a 20 if( nTerm==1.
b0e0: 20 20 26 26 20 70 43 6f 6c 0a 20 20 20 26 26 20 && pCol. &&
b0f0: 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 73 sqlite3StrICmp(s
b100: 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 54 79 70 65 qlite3ColumnType
b110: 28 70 43 6f 6c 2c 22 22 29 2c 20 22 49 4e 54 45 (pCol,""), "INTE
b120: 47 45 52 22 29 3d 3d 30 0a 20 20 20 26 26 20 73 GER")==0. && s
b130: 6f 72 74 4f 72 64 65 72 21 3d 53 51 4c 49 54 45 ortOrder!=SQLITE
b140: 5f 53 4f 5f 44 45 53 43 0a 20 20 29 7b 0a 20 20 _SO_DESC. ){.
b150: 20 20 69 66 28 20 49 4e 5f 52 45 4e 41 4d 45 5f if( IN_RENAME_
b160: 4f 42 4a 45 43 54 20 26 26 20 70 4c 69 73 74 20 OBJECT && pList
b170: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
b180: 52 65 6e 61 6d 65 54 6f 6b 65 6e 52 65 6d 61 70 RenameTokenRemap
b190: 28 70 50 61 72 73 65 2c 20 26 70 54 61 62 2d 3e (pParse, &pTab->
b1a0: 69 50 4b 65 79 2c 20 70 4c 69 73 74 2d 3e 61 5b iPKey, pList->a[
b1b0: 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0].pExpr);. }
b1c0: 0a 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 . pTab->iPKey
b1d0: 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 54 61 = iCol;. pTa
b1e0: 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d 20 28 75 38 b->keyConf = (u8
b1f0: 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 61 73 )onError;. as
b200: 73 65 72 74 28 20 61 75 74 6f 49 6e 63 3d 3d 30 sert( autoInc==0
b210: 20 7c 7c 20 61 75 74 6f 49 6e 63 3d 3d 31 20 29 || autoInc==1 )
b220: 3b 0a 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46 ;. pTab->tabF
b230: 6c 61 67 73 20 7c 3d 20 61 75 74 6f 49 6e 63 2a lags |= autoInc*
b240: 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 TF_Autoincrement
b250: 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 ;. if( pList
b260: 29 20 70 50 61 72 73 65 2d 3e 69 50 6b 53 6f 72 ) pParse->iPkSor
b270: 74 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 2d 3e tOrder = pList->
b280: 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a a[0].sortOrder;.
b290: 20 20 7d 65 6c 73 65 20 69 66 28 20 61 75 74 6f }else if( auto
b2a0: 49 6e 63 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 Inc ){.#ifndef S
b2b0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
b2c0: 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 73 71 6c NCREMENT. sql
b2d0: 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
b2e0: 72 73 65 2c 20 22 41 55 54 4f 49 4e 43 52 45 4d rse, "AUTOINCREM
b2f0: 45 4e 54 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f ENT is only allo
b300: 77 65 64 20 6f 6e 20 61 6e 20 22 0a 20 20 20 20 wed on an ".
b310: 20 20 20 22 49 4e 54 45 47 45 52 20 50 52 49 4d "INTEGER PRIM
b320: 41 52 59 20 4b 45 59 22 29 3b 0a 23 65 6e 64 69 ARY KEY");.#endi
b330: 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 f. }else{. s
b340: 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 qlite3CreateInde
b350: 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 x(pParse, 0, 0,
b360: 30 2c 20 70 4c 69 73 74 2c 20 6f 6e 45 72 72 6f 0, pList, onErro
b370: 72 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 r, 0,.
b380: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b390: 20 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c 20 30 0, sortOrder, 0
b3a0: 2c 20 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 , SQLITE_IDXTYPE
b3b0: 5f 50 52 49 4d 41 52 59 4b 45 59 29 3b 0a 20 20 _PRIMARYKEY);.
b3c0: 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d pList = 0;. }
b3d0: 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 ..primary_key_ex
b3e0: 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 it:. sqlite3Exp
b3f0: 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 rListDelete(pPar
b400: 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a se->db, pList);.
b410: 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a return;.}../*.
b420: 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 45 ** Add a new CHE
b430: 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f CK constraint to
b440: 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 the table curre
b450: 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 ntly under const
b460: 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 ruction..*/.void
b470: 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b sqlite3AddCheck
b480: 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 Constraint(. Pa
b490: 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
b4a0: 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
b4b0: 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 xt */. Expr *pC
b4c0: 68 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 heckExpr /* The
b4d0: 20 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f check expressio
b4e0: 6e 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 n */.){.#ifndef
b4f0: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 SQLITE_OMIT_CHEC
b500: 4b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 K. Table *pTab
b510: 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 = pParse->pNewTa
b520: 62 6c 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a ble;. sqlite3 *
b530: 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
b540: 0a 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21 . if( pTab && !
b550: 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 0a IN_DECLARE_VTAB.
b560: 20 20 20 26 26 20 21 73 71 6c 69 74 65 33 42 74 && !sqlite3Bt
b570: 72 65 65 49 73 52 65 61 64 6f 6e 6c 79 28 64 62 reeIsReadonly(db
b580: 2d 3e 61 44 62 5b 64 62 2d 3e 69 6e 69 74 2e 69 ->aDb[db->init.i
b590: 44 62 5d 2e 70 42 74 29 0a 20 20 29 7b 0a 20 20 Db].pBt). ){.
b5a0: 20 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 3d pTab->pCheck =
b5b0: 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
b5c0: 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 Append(pParse, p
b5d0: 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 70 43 68 Tab->pCheck, pCh
b5e0: 65 63 6b 45 78 70 72 29 3b 0a 20 20 20 20 69 66 eckExpr);. if
b5f0: 28 20 70 50 61 72 73 65 2d 3e 63 6f 6e 73 74 72 ( pParse->constr
b600: 61 69 6e 74 4e 61 6d 65 2e 6e 20 29 7b 0a 20 20 aintName.n ){.
b610: 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c sqlite3ExprL
b620: 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73 istSetName(pPars
b630: 65 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c e, pTab->pCheck,
b640: 20 26 70 50 61 72 73 65 2d 3e 63 6f 6e 73 74 72 &pParse->constr
b650: 61 69 6e 74 4e 61 6d 65 2c 20 31 29 3b 0a 20 20 aintName, 1);.
b660: 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 }. }else.#end
b670: 69 66 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 if. {. sqlit
b680: 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 e3ExprDelete(pPa
b690: 72 73 65 2d 3e 64 62 2c 20 70 43 68 65 63 6b 45 rse->db, pCheckE
b6a0: 78 70 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a xpr);. }.}../*.
b6b0: 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 ** Set the colla
b6c0: 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 6f 66 tion function of
b6d0: 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
b6e0: 6c 79 20 70 61 72 73 65 64 20 74 61 62 6c 65 20 ly parsed table
b6f0: 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 74 68 65 column.** to the
b700: 20 43 6f 6c 6c 53 65 71 20 67 69 76 65 6e 2e 0a CollSeq given..
b710: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 41 */.void sqlite3A
b720: 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 ddCollateType(Pa
b730: 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b rse *pParse, Tok
b740: 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 54 en *pToken){. T
b750: 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 able *p;. int i
b760: 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b ;. char *zColl;
b770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
b780: 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f Dequoted name o
b790: 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 f collation sequ
b7a0: 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ence */. sqlite
b7b0: 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 28 70 3 *db;.. if( (p
b7c0: 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 = pParse->pNewT
b7d0: 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 able)==0 ) retur
b7e0: 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c n;. i = p->nCol
b7f0: 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 -1;. db = pPars
b800: 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d e->db;. zColl =
b810: 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
b820: 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e Token(db, pToken
b830: 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 );. if( !zColl
b840: 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 ) return;.. if(
b850: 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f sqlite3LocateCo
b860: 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 llSeq(pParse, zC
b870: 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 oll) ){. Inde
b880: 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 73 71 6c x *pIdx;. sql
b890: 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
b8a0: 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 29 ->aCol[i].zColl)
b8b0: 3b 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d ;. p->aCol[i]
b8c0: 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a .zColl = zColl;.
b8d0: 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
b8e0: 20 63 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 column is decla
b8f0: 72 65 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 red as "<name> P
b900: 52 49 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 RIMARY KEY COLLA
b910: 54 45 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 TE <type>",.
b920: 2a 2a 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 ** then an index
b930: 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 may have been c
b940: 72 65 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 reated on this c
b950: 6f 6c 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 olumn before the
b960: 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f . ** collatio
b970: 6e 20 74 79 70 65 20 77 61 73 20 61 64 64 65 64 n type was added
b980: 2e 20 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 . Correct this i
b990: 66 20 69 74 20 69 73 20 74 68 65 20 63 61 73 65 f it is the case
b9a0: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
b9b0: 28 70 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b (pIdx=p->pIndex;
b9c0: 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 pIdx; pIdx=pIdx
b9d0: 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
b9e0: 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 4b assert( pIdx->nK
b9f0: 65 79 43 6f 6c 3d 3d 31 20 29 3b 0a 20 20 20 20 eyCol==1 );.
ba00: 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f if( pIdx->aiCo
ba10: 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 lumn[0]==i ){.
ba20: 20 20 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f pIdx->azCo
ba30: 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b ll[0] = p->aCol[
ba40: 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 i].zColl;.
ba50: 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b }. }. }else{
ba60: 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
ba70: 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 ee(db, zColl);.
ba80: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
ba90: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
baa0: 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 s the collation
bab0: 73 65 71 75 65 6e 63 65 20 66 6f 72 20 64 61 74 sequence for dat
bac0: 61 62 61 73 65 20 6e 61 74 69 76 65 20 74 65 78 abase native tex
bad0: 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 t.** encoding id
bae0: 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 entified by the
baf0: 73 74 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 string zName, le
bb00: 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a ngth nName..**.*
bb10: 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 * If the request
bb20: 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 ed collation seq
bb30: 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 uence is not ava
bb40: 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 ilable, or not a
bb50: 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 vailable.** in t
bb60: 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 he database nati
bb70: 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 ve encoding, the
bb80: 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f collation facto
bb90: 72 79 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f ry is invoked to
bba0: 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 74 2e 20 .** request it.
bbb0: 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e If the collation
bbc0: 20 66 61 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f factory does no
bbd0: 74 20 73 75 70 70 6c 79 20 73 75 63 68 20 61 20 t supply such a
bbe0: 73 65 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 sequence,.** and
bbf0: 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 69 73 the sequence is
bc00: 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e available in an
bc10: 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 other text encod
bc20: 69 6e 67 2c 20 74 68 65 6e 20 74 68 61 74 20 69 ing, then that i
bc30: 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e s.** returned in
bc40: 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 stead..**.** If
bc50: 6e 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 no versions of t
bc60: 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c he requested col
bc70: 6c 61 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 lations sequence
bc80: 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 are available,
bc90: 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 or.** another er
bca0: 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c ror occurs, NULL
bcb0: 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
bcc0: 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
bcd0: 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a e written into.*
bce0: 2a 20 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 * pParse..**.**
bcf0: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
bd00: 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
bd10: 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c sqlite3FindColl
bd20: 53 65 71 28 29 2e 20 20 54 68 69 73 20 72 6f 75 Seq(). This rou
bd30: 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 tine.** invokes
bd40: 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 the collation fa
bd50: 63 74 6f 72 79 20 69 66 20 74 68 65 20 6e 61 6d ctory if the nam
bd60: 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e ed collation can
bd70: 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 not be found.**
bd80: 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e and generates an
bd90: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a error message..
bda0: 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
bdb0: 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 sqlite3FindCollS
bdc0: 65 71 28 29 2c 20 73 71 6c 69 74 65 33 47 65 74 eq(), sqlite3Get
bdd0: 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a 43 6f 6c CollSeq().*/.Col
bde0: 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 lSeq *sqlite3Loc
bdf0: 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 ateCollSeq(Parse
be00: 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 *pParse, const
be10: 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 char *zName){.
be20: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
be30: 61 72 73 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65 arse->db;. u8 e
be40: 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 nc = ENC(db);.
be50: 75 38 20 69 6e 69 74 62 75 73 79 20 3d 20 64 62 u8 initbusy = db
be60: 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43 ->init.busy;. C
be70: 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a ollSeq *pColl;..
be80: 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
be90: 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 3FindCollSeq(db,
bea0: 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 69 6e 69 enc, zName, ini
beb0: 74 62 75 73 79 29 3b 0a 20 20 69 66 28 20 21 69 tbusy);. if( !i
bec0: 6e 69 74 62 75 73 79 20 26 26 20 28 21 70 43 6f nitbusy && (!pCo
bed0: 6c 6c 20 7c 7c 20 21 70 43 6f 6c 6c 2d 3e 78 43 ll || !pColl->xC
bee0: 6d 70 29 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c mp) ){. pColl
bef0: 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c = sqlite3GetCol
bf00: 6c 53 65 71 28 70 50 61 72 73 65 2c 20 65 6e 63 lSeq(pParse, enc
bf10: 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29 3b , pColl, zName);
bf20: 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 . }.. return p
bf30: 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 Coll;.}.../*.**
bf40: 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 Generate code th
bf50: 61 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e at will incremen
bf60: 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f t the schema coo
bf70: 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 kie..**.** The s
bf80: 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 73 20 chema cookie is
bf90: 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
bfa0: 65 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d e when the schem
bfb0: 61 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 61 74 a for the.** dat
bfc0: 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 abase changes.
bfd0: 41 66 74 65 72 20 65 61 63 68 20 73 63 68 65 6d After each schem
bfe0: 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20 63 6f a change, the co
bff0: 6f 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20 63 68 okie value.** ch
c000: 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61 20 70 anges. When a p
c010: 72 6f 63 65 73 73 20 66 69 72 73 74 20 72 65 61 rocess first rea
c020: 64 73 20 74 68 65 20 73 63 68 65 6d 61 20 69 74 ds the schema it
c030: 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20 records the.**
c040: 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65 61 66 cookie. Thereaf
c050: 74 65 72 2c 20 77 68 65 6e 65 76 65 72 20 69 74 ter, whenever it
c060: 20 67 6f 65 73 20 74 6f 20 61 63 63 65 73 73 20 goes to access
c070: 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a the database,.**
c080: 20 69 74 20 63 68 65 63 6b 73 20 74 68 65 20 63 it checks the c
c090: 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20 73 75 ookie to make su
c0a0: 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 re the schema ha
c0b0: 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a 2a 2a s not changed.**
c0c0: 20 73 69 6e 63 65 20 69 74 20 77 61 73 20 6c 61 since it was la
c0d0: 73 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 54 st read..**.** T
c0e0: 68 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f 74 20 his plan is not
c0f0: 63 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c 6c 65 completely bulle
c100: 74 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69 73 20 t-proof. It is
c110: 70 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 possible for.**
c120: 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20 63 68 the schema to ch
c130: 61 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20 74 69 ange multiple ti
c140: 6d 65 73 20 61 6e 64 20 66 6f 72 20 74 68 65 20 mes and for the
c150: 63 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a 2a 20 cookie to be.**
c160: 73 65 74 20 62 61 63 6b 20 74 6f 20 70 72 69 6f set back to prio
c170: 72 20 76 61 6c 75 65 2e 20 20 42 75 74 20 73 63 r value. But sc
c180: 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65 hema changes are
c190: 20 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a 20 61 infrequent.** a
c1a0: 6e 64 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69 nd the probabili
c1b0: 74 79 20 6f 66 20 68 69 74 74 69 6e 67 20 74 68 ty of hitting th
c1c0: 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20 76 61 e same cookie va
c1d0: 6c 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 31 lue is only.** 1
c1e0: 20 63 68 61 6e 63 65 20 69 6e 20 32 5e 33 32 2e chance in 2^32.
c1f0: 20 20 53 6f 20 77 65 27 72 65 20 73 61 66 65 20 So we're safe
c200: 65 6e 6f 75 67 68 2e 0a 2a 2a 0a 2a 2a 20 49 4d enough..**.** IM
c210: 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a PLEMENTATION-OF:
c220: 20 52 2d 33 34 32 33 30 2d 35 36 30 34 39 20 53 R-34230-56049 S
c230: 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 QLite automatica
c240: 6c 6c 79 20 69 6e 63 72 65 6d 65 6e 74 73 0a 2a lly increments.*
c250: 2a 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 * the schema-ver
c260: 73 69 6f 6e 20 77 68 65 6e 65 76 65 72 20 74 68 sion whenever th
c270: 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 e schema changes
c280: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
c290: 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50 61 3ChangeCookie(Pa
c2a0: 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
c2b0: 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 iDb){. sqlite3
c2c0: 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
c2d0: 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 b;. Vdbe *v = p
c2e0: 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 Parse->pVdbe;.
c2f0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 assert( sqlite3S
c300: 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 chemaMutexHeld(d
c310: 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 b, iDb, 0) );.
c320: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
c330: 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 3(v, OP_SetCooki
c340: 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 53 43 e, iDb, BTREE_SC
c350: 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 0a 20 HEMA_VERSION, .
c360: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c370: 20 20 28 69 6e 74 29 28 31 2b 28 75 6e 73 69 67 (int)(1+(unsig
c380: 6e 65 64 29 64 62 2d 3e 61 44 62 5b 69 44 62 5d ned)db->aDb[iDb]
c390: 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 .pSchema->schema
c3a0: 5f 63 6f 6f 6b 69 65 29 29 3b 0a 7d 0a 0a 2f 2a _cookie));.}../*
c3b0: 0a 2a 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20 .** Measure the
c3c0: 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
c3d0: 74 65 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f ters needed to o
c3e0: 75 74 70 75 74 20 74 68 65 20 67 69 76 65 6e 0a utput the given.
c3f0: 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 ** identifier.
c400: 54 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72 The number retur
c410: 6e 65 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79 ned includes any
c420: 20 71 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a 20 quotes used.**
c430: 62 75 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 but does not inc
c440: 6c 75 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 65 lude the null te
c450: 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 rminator..**.**
c460: 54 68 65 20 65 73 74 69 6d 61 74 65 20 69 73 20 The estimate is
c470: 63 6f 6e 73 65 72 76 61 74 69 76 65 2e 20 20 49 conservative. I
c480: 74 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 t might be large
c490: 72 20 74 68 61 74 20 77 68 61 74 20 69 73 0a 2a r that what is.*
c4a0: 2a 20 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 2e * really needed.
c4b0: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 .*/.static int i
c4c0: 64 65 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 74 dentLength(const
c4d0: 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 char *z){. int
c4e0: 20 6e 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 2a n;. for(n=0; *
c4f0: 7a 3b 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 z; n++, z++){.
c500: 20 20 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 7b if( *z=='"' ){
c510: 20 6e 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 65 n++; }. }. re
c520: 74 75 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f turn n + 2;.}../
c530: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 *.** The first p
c540: 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f arameter is a po
c550: 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 75 74 70 inter to an outp
c560: 75 74 20 62 75 66 66 65 72 2e 20 54 68 65 20 73 ut buffer. The s
c570: 65 63 6f 6e 64 20 0a 2a 2a 20 70 61 72 61 6d 65 econd .** parame
c580: 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ter is a pointer
c590: 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 to an integer t
c5a0: 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
c5b0: 20 6f 66 66 73 65 74 20 61 74 0a 2a 2a 20 77 68 offset at.** wh
c5c0: 69 63 68 20 74 6f 20 77 72 69 74 65 20 69 6e 74 ich to write int
c5d0: 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 o the output buf
c5e0: 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 fer. This functi
c5f0: 6f 6e 20 63 6f 70 69 65 73 20 74 68 65 0a 2a 2a on copies the.**
c600: 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 nul-terminated
c610: 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 string pointed t
c620: 6f 20 62 79 20 74 68 65 20 74 68 69 72 64 20 70 o by the third p
c630: 61 72 61 6d 65 74 65 72 2c 20 7a 53 69 67 6e 65 arameter, zSigne
c640: 64 49 64 65 6e 74 2c 0a 2a 2a 20 74 6f 20 74 68 dIdent,.** to th
c650: 65 20 73 70 65 63 69 66 69 65 64 20 6f 66 66 73 e specified offs
c660: 65 74 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 et in the buffer
c670: 20 61 6e 64 20 75 70 64 61 74 65 73 20 2a 70 49 and updates *pI
c680: 64 78 20 74 6f 20 72 65 66 65 72 0a 2a 2a 20 74 dx to refer.** t
c690: 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 o the first byte
c6a0: 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 after the last
c6b0: 62 79 74 65 20 77 72 69 74 74 65 6e 20 62 65 66 byte written bef
c6c0: 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
c6d0: 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 * .** If the str
c6e0: 69 6e 67 20 7a 53 69 67 6e 65 64 49 64 65 6e 74 ing zSignedIdent
c6f0: 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65 consists entire
c700: 6c 79 20 6f 66 20 61 6c 70 68 61 2d 6e 75 6d 65 ly of alpha-nume
c710: 72 69 63 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 ric.** character
c720: 73 2c 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 s, does not begi
c730: 6e 20 77 69 74 68 20 61 20 64 69 67 69 74 20 61 n with a digit a
c740: 6e 64 20 69 73 20 6e 6f 74 20 61 6e 20 53 51 4c nd is not an SQL
c750: 20 6b 65 79 77 6f 72 64 2c 0a 2a 2a 20 74 68 65 keyword,.** the
c760: 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 n it is copied t
c770: 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 o the output buf
c780: 66 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 69 fer exactly as i
c790: 74 20 69 73 2e 20 4f 74 68 65 72 77 69 73 65 2c t is. Otherwise,
c7a0: 0a 2a 2a 20 69 74 20 69 73 20 71 75 6f 74 65 64 .** it is quoted
c7b0: 20 75 73 69 6e 67 20 64 6f 75 62 6c 65 2d 71 75 using double-qu
c7c0: 6f 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 otes..*/.static
c7d0: 76 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 68 void identPut(ch
c7e0: 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 ar *z, int *pIdx
c7f0: 2c 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 , char *zSignedI
c800: 64 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65 dent){. unsigne
c810: 64 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d d char *zIdent =
c820: 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a (unsigned char*
c830: 29 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 )zSignedIdent;.
c840: 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 int i, j, needQ
c850: 75 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 uote;. i = *pId
c860: 78 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a x;.. for(j=0; z
c870: 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a Ident[j]; j++){.
c880: 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 if( !sqlite3
c890: 49 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 6a Isalnum(zIdent[j
c8a0: 5d 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d 21 ]) && zIdent[j]!
c8b0: 3d 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 20 ='_' ) break;.
c8c0: 7d 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 }. needQuote =
c8d0: 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a sqlite3Isdigit(z
c8e0: 49 64 65 6e 74 5b 30 5d 29 0a 20 20 20 20 20 20 Ident[0]).
c8f0: 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
c900: 4b 65 79 77 6f 72 64 43 6f 64 65 28 7a 49 64 65 KeywordCode(zIde
c910: 6e 74 2c 20 6a 29 21 3d 54 4b 5f 49 44 0a 20 20 nt, j)!=TK_ID.
c920: 20 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 49 64 || zId
c930: 65 6e 74 5b 6a 5d 21 3d 30 0a 20 20 20 20 20 20 ent[j]!=0.
c940: 20 20 20 20 20 20 7c 7c 20 6a 3d 3d 30 3b 0a 0a || j==0;..
c950: 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 if( needQuote
c960: 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a ) z[i++] = '"';.
c970: 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e for(j=0; zIden
c980: 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 t[j]; j++){.
c990: 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e 74 5b z[i++] = zIdent[
c9a0: 6a 5d 3b 0a 20 20 20 20 69 66 28 20 7a 49 64 65 j];. if( zIde
c9b0: 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a 5b 69 nt[j]=='"' ) z[i
c9c0: 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7d 0a 20 ++] = '"';. }.
c9d0: 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 if( needQuote )
c9e0: 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 z[i++] = '"';.
c9f0: 20 7a 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 49 z[i] = 0;. *pI
ca00: 64 78 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a dx = i;.}../*.**
ca10: 20 47 65 6e 65 72 61 74 65 20 61 20 43 52 45 41 Generate a CREA
ca20: 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 TE TABLE stateme
ca30: 6e 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 nt appropriate f
ca40: 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 or the given.**
ca50: 74 61 62 6c 65 2e 20 20 4d 65 6d 6f 72 79 20 74 table. Memory t
ca60: 6f 20 68 6f 6c 64 20 74 68 65 20 74 65 78 74 20 o hold the text
ca70: 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 of the statement
ca80: 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 is obtained.**
ca90: 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f from sqliteMallo
caa0: 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 c() and must be
cab0: 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c freed by the cal
cac0: 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a ling function..*
cad0: 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 /.static char *c
cae0: 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28 73 reateTableStmt(s
caf0: 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c qlite3 *db, Tabl
cb00: 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 2c 20 e *p){. int i,
cb10: 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 53 k, n;. char *zS
cb20: 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a 53 65 tmt;. char *zSe
cb30: 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45 6e 64 p, *zSep2, *zEnd
cb40: 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c ;. Column *pCol
cb50: 3b 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 ;. n = 0;. for
cb60: 28 70 43 6f 6c 20 3d 20 70 2d 3e 61 43 6f 6c 2c (pCol = p->aCol,
cb70: 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b i=0; i<p->nCol;
cb80: 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 i++, pCol++){.
cb90: 20 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e n += identLen
cba0: 67 74 68 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 gth(pCol->zName)
cbb0: 20 2b 20 35 3b 0a 20 20 7d 0a 20 20 6e 20 2b 3d + 5;. }. n +=
cbc0: 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 2d 3e identLength(p->
cbd0: 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 6e 3c zName);. if( n<
cbe0: 35 30 20 29 7b 20 0a 20 20 20 20 7a 53 65 70 20 50 ){ . zSep
cbf0: 3d 20 22 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 = "";. zSep2
cc00: 3d 20 22 2c 22 3b 0a 20 20 20 20 7a 45 6e 64 20 = ",";. zEnd
cc10: 3d 20 22 29 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a = ")";. }else{.
cc20: 20 20 20 20 7a 53 65 70 20 3d 20 22 5c 6e 20 20 zSep = "\n
cc30: 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22 ";. zSep2 = "
cc40: 2c 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 45 6e 64 ,\n ";. zEnd
cc50: 20 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d 0a 20 20 = "\n)";. }.
cc60: 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d 3e 6e n += 35 + 6*p->n
cc70: 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20 3d 20 73 Col;. zStmt = s
cc80: 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
cc90: 77 28 30 2c 20 6e 29 3b 0a 20 20 69 66 28 20 7a w(0, n);. if( z
cca0: 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 Stmt==0 ){. s
ccb0: 71 6c 69 74 65 33 4f 6f 6d 46 61 75 6c 74 28 64 qlite3OomFault(d
ccc0: 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 b);. return 0
ccd0: 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
cce0: 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74 6d snprintf(n, zStm
ccf0: 74 2c 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 t, "CREATE TABLE
cd00: 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71 6c 69 74 ");. k = sqlit
cd10: 65 33 53 74 72 6c 65 6e 33 30 28 7a 53 74 6d 74 e3Strlen30(zStmt
cd20: 29 3b 0a 20 20 69 64 65 6e 74 50 75 74 28 7a 53 );. identPut(zS
cd30: 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61 6d tmt, &k, p->zNam
cd40: 65 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d e);. zStmt[k++]
cd50: 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72 28 70 43 = '(';. for(pC
cd60: 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b ol=p->aCol, i=0;
cd70: 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c i<p->nCol; i++,
cd80: 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73 74 pCol++){. st
cd90: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
cda0: 2a 20 63 6f 6e 73 74 20 61 7a 54 79 70 65 5b 5d * const azType[]
cdb0: 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 = {. /*
cdc0: 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 20 SQLITE_AFF_BLOB
cdd0: 20 20 20 2a 2f 20 22 22 2c 0a 20 20 20 20 20 20 */ "",.
cde0: 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f /* SQLITE_AFF_
cdf0: 54 45 58 54 20 20 20 20 2a 2f 20 22 20 54 45 58 TEXT */ " TEX
ce00: 54 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 T",. /* S
ce10: 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
ce20: 43 20 2a 2f 20 22 20 4e 55 4d 22 2c 0a 20 20 20 C */ " NUM",.
ce30: 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 /* SQLITE_A
ce40: 46 46 5f 49 4e 54 45 47 45 52 20 2a 2f 20 22 20 FF_INTEGER */ "
ce50: 49 4e 54 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a INT",. /*
ce60: 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
ce70: 20 20 20 20 2a 2f 20 22 20 52 45 41 4c 22 0a 20 */ " REAL".
ce80: 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6c 65 };. int le
ce90: 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 n;. const cha
cea0: 72 20 2a 7a 54 79 70 65 3b 0a 0a 20 20 20 20 73 r *zType;.. s
ceb0: 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
cec0: 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 n-k, &zStmt[k],
ced0: 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 zSep);. k +=
cee0: 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
cef0: 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20 20 20 20 &zStmt[k]);.
cf00: 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b 0a 20 20 zSep = zSep2;.
cf10: 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d 74 identPut(zStmt
cf20: 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d , &k, pCol->zNam
cf30: 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 e);. assert(
cf40: 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2d 53 pCol->affinity-S
cf50: 51 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 20 3e QLITE_AFF_BLOB >
cf60: 3d 20 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 = 0 );. asser
cf70: 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 t( pCol->affinit
cf80: 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c 4f y-SQLITE_AFF_BLO
cf90: 42 20 3c 20 41 72 72 61 79 53 69 7a 65 28 61 7a B < ArraySize(az
cfa0: 54 79 70 65 29 20 29 3b 0a 20 20 20 20 74 65 73 Type) );. tes
cfb0: 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 tcase( pCol->aff
cfc0: 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 inity==SQLITE_AF
cfd0: 46 5f 42 4c 4f 42 20 29 3b 0a 20 20 20 20 74 65 F_BLOB );. te
cfe0: 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 stcase( pCol->af
cff0: 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
d000: 46 46 5f 54 45 58 54 20 29 3b 0a 20 20 20 20 74 FF_TEXT );. t
d010: 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 estcase( pCol->a
d020: 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
d030: 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 AFF_NUMERIC );.
d040: 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f testcase( pCo
d050: 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c l->affinity==SQL
d060: 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 ITE_AFF_INTEGER
d070: 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
d080: 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d pCol->affinity=
d090: 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c =SQLITE_AFF_REAL
d0a0: 20 29 3b 0a 20 20 20 20 0a 20 20 20 20 7a 54 79 );. . zTy
d0b0: 70 65 20 3d 20 61 7a 54 79 70 65 5b 70 43 6f 6c pe = azType[pCol
d0c0: 2d 3e 61 66 66 69 6e 69 74 79 20 2d 20 53 51 4c ->affinity - SQL
d0d0: 49 54 45 5f 41 46 46 5f 42 4c 4f 42 5d 3b 0a 20 ITE_AFF_BLOB];.
d0e0: 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 len = sqlite3
d0f0: 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29 3b Strlen30(zType);
d100: 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f . assert( pCo
d110: 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c l->affinity==SQL
d120: 49 54 45 5f 41 46 46 5f 42 4c 4f 42 20 0a 20 20 ITE_AFF_BLOB .
d130: 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43 6f || pCo
d140: 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 73 71 6c l->affinity==sql
d150: 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 ite3AffinityType
d160: 28 7a 54 79 70 65 2c 20 30 29 20 29 3b 0a 20 20 (zType, 0) );.
d170: 20 20 6d 65 6d 63 70 79 28 26 7a 53 74 6d 74 5b memcpy(&zStmt[
d180: 6b 5d 2c 20 7a 54 79 70 65 2c 20 6c 65 6e 29 3b k], zType, len);
d190: 0a 20 20 20 20 6b 20 2b 3d 20 6c 65 6e 3b 0a 20 . k += len;.
d1a0: 20 20 20 61 73 73 65 72 74 28 20 6b 3c 3d 6e 20 assert( k<=n
d1b0: 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
d1c0: 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 _snprintf(n-k, &
d1d0: 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 zStmt[k], "%s",
d1e0: 7a 45 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 zEnd);. return
d1f0: 7a 53 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 zStmt;.}../*.**
d200: 52 65 73 69 7a 65 20 61 6e 20 49 6e 64 65 78 20 Resize an Index
d210: 6f 62 6a 65 63 74 20 74 6f 20 68 6f 6c 64 20 4e object to hold N
d220: 20 63 6f 6c 75 6d 6e 73 20 74 6f 74 61 6c 2e 20 columns total.
d230: 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
d240: 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 K.** on success
d250: 61 6e 64 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d and SQLITE_NOMEM
d260: 20 6f 6e 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 on an OOM error
d270: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d280: 72 65 73 69 7a 65 49 6e 64 65 78 4f 62 6a 65 63 resizeIndexObjec
d290: 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 t(sqlite3 *db, I
d2a0: 6e 64 65 78 20 2a 70 49 64 78 2c 20 69 6e 74 20 ndex *pIdx, int
d2b0: 4e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 N){. char *zExt
d2c0: 72 61 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b ra;. int nByte;
d2d0: 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 6e 43 6f . if( pIdx->nCo
d2e0: 6c 75 6d 6e 3e 3d 4e 20 29 20 72 65 74 75 72 6e lumn>=N ) return
d2f0: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 SQLITE_OK;. as
d300: 73 65 72 74 28 20 70 49 64 78 2d 3e 69 73 52 65 sert( pIdx->isRe
d310: 73 69 7a 65 64 3d 3d 30 20 29 3b 0a 20 20 6e 42 sized==0 );. nB
d320: 79 74 65 20 3d 20 28 73 69 7a 65 6f 66 28 63 68 yte = (sizeof(ch
d330: 61 72 2a 29 20 2b 20 73 69 7a 65 6f 66 28 69 31 ar*) + sizeof(i1
d340: 36 29 20 2b 20 31 29 2a 4e 3b 0a 20 20 7a 45 78 6) + 1)*N;. zEx
d350: 74 72 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d tra = sqlite3DbM
d360: 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 allocZero(db, nB
d370: 79 74 65 29 3b 0a 20 20 69 66 28 20 7a 45 78 74 yte);. if( zExt
d380: 72 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 ra==0 ) return S
d390: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 QLITE_NOMEM_BKPT
d3a0: 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 45 78 74 72 ;. memcpy(zExtr
d3b0: 61 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 2c a, pIdx->azColl,
d3c0: 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 70 sizeof(char*)*p
d3d0: 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 Idx->nColumn);.
d3e0: 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 pIdx->azColl =
d3f0: 28 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 7a 45 (const char**)zE
d400: 78 74 72 61 3b 0a 20 20 7a 45 78 74 72 61 20 2b xtra;. zExtra +
d410: 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a = sizeof(char*)*
d420: 4e 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 45 78 74 N;. memcpy(zExt
d430: 72 61 2c 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 ra, pIdx->aiColu
d440: 6d 6e 2c 20 73 69 7a 65 6f 66 28 69 31 36 29 2a mn, sizeof(i16)*
d450: 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a pIdx->nColumn);.
d460: 20 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e pIdx->aiColumn
d470: 20 3d 20 28 69 31 36 2a 29 7a 45 78 74 72 61 3b = (i16*)zExtra;
d480: 0a 20 20 7a 45 78 74 72 61 20 2b 3d 20 73 69 7a . zExtra += siz
d490: 65 6f 66 28 69 31 36 29 2a 4e 3b 0a 20 20 6d 65 eof(i16)*N;. me
d4a0: 6d 63 70 79 28 7a 45 78 74 72 61 2c 20 70 49 64 mcpy(zExtra, pId
d4b0: 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 70 x->aSortOrder, p
d4c0: 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 Idx->nColumn);.
d4d0: 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 pIdx->aSortOrde
d4e0: 72 20 3d 20 28 75 38 2a 29 7a 45 78 74 72 61 3b r = (u8*)zExtra;
d4f0: 0a 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e . pIdx->nColumn
d500: 20 3d 20 4e 3b 0a 20 20 70 49 64 78 2d 3e 69 73 = N;. pIdx->is
d510: 52 65 73 69 7a 65 64 20 3d 20 31 3b 0a 20 20 72 Resized = 1;. r
d520: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d530: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 73 74 69 6d 61 .}../*.** Estima
d540: 74 65 20 74 68 65 20 74 6f 74 61 6c 20 72 6f 77 te the total row
d550: 20 77 69 64 74 68 20 66 6f 72 20 61 20 74 61 62 width for a tab
d560: 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
d570: 69 64 20 65 73 74 69 6d 61 74 65 54 61 62 6c 65 id estimateTable
d580: 57 69 64 74 68 28 54 61 62 6c 65 20 2a 70 54 61 Width(Table *pTa
d590: 62 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 77 b){. unsigned w
d5a0: 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 63 6f 6e Table = 0;. con
d5b0: 73 74 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 43 st Column *pTabC
d5c0: 6f 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 ol;. int i;. f
d5d0: 6f 72 28 69 3d 70 54 61 62 2d 3e 6e 43 6f 6c 2c or(i=pTab->nCol,
d5e0: 20 70 54 61 62 43 6f 6c 3d 70 54 61 62 2d 3e 61 pTabCol=pTab->a
d5f0: 43 6f 6c 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 Col; i>0; i--, p
d600: 54 61 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 77 TabCol++){. w
d610: 54 61 62 6c 65 20 2b 3d 20 70 54 61 62 43 6f 6c Table += pTabCol
d620: 2d 3e 73 7a 45 73 74 3b 0a 20 20 7d 0a 20 20 69 ->szEst;. }. i
d630: 66 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 30 f( pTab->iPKey<0
d640: 20 29 20 77 54 61 62 6c 65 2b 2b 3b 0a 20 20 70 ) wTable++;. p
d650: 54 61 62 2d 3e 73 7a 54 61 62 52 6f 77 20 3d 20 Tab->szTabRow =
d660: 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 77 54 sqlite3LogEst(wT
d670: 61 62 6c 65 2a 34 29 3b 0a 7d 0a 0a 2f 2a 0a 2a able*4);.}../*.*
d680: 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20 61 * Estimate the a
d690: 76 65 72 61 67 65 20 73 69 7a 65 20 6f 66 20 61 verage size of a
d6a0: 20 72 6f 77 20 66 6f 72 20 61 6e 20 69 6e 64 65 row for an inde
d6b0: 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 x..*/.static voi
d6c0: 64 20 65 73 74 69 6d 61 74 65 49 6e 64 65 78 57 d estimateIndexW
d6d0: 69 64 74 68 28 49 6e 64 65 78 20 2a 70 49 64 78 idth(Index *pIdx
d6e0: 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 77 49 ){. unsigned wI
d6f0: 6e 64 65 78 20 3d 20 30 3b 0a 20 20 69 6e 74 20 ndex = 0;. int
d700: 69 3b 0a 20 20 63 6f 6e 73 74 20 43 6f 6c 75 6d i;. const Colum
d710: 6e 20 2a 61 43 6f 6c 20 3d 20 70 49 64 78 2d 3e n *aCol = pIdx->
d720: 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20 pTable->aCol;.
d730: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d for(i=0; i<pIdx-
d740: 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a >nColumn; i++){.
d750: 20 20 20 20 69 31 36 20 78 20 3d 20 70 49 64 78 i16 x = pIdx
d760: 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 ->aiColumn[i];.
d770: 20 20 20 61 73 73 65 72 74 28 20 78 3c 70 49 64 assert( x<pId
d780: 78 2d 3e 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 x->pTable->nCol
d790: 29 3b 0a 20 20 20 20 77 49 6e 64 65 78 20 2b 3d );. wIndex +=
d7a0: 20 78 3c 30 20 3f 20 31 20 3a 20 61 43 6f 6c 5b x<0 ? 1 : aCol[
d7b0: 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 pIdx->aiColumn[i
d7c0: 5d 5d 2e 73 7a 45 73 74 3b 0a 20 20 7d 0a 20 20 ]].szEst;. }.
d7d0: 70 49 64 78 2d 3e 73 7a 49 64 78 52 6f 77 20 3d pIdx->szIdxRow =
d7e0: 20 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 77 sqlite3LogEst(w
d7f0: 49 6e 64 65 78 2a 34 29 3b 0a 7d 0a 0a 2f 2a 20 Index*4);.}../*
d800: 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 76 Return true if v
d810: 61 6c 75 65 20 78 20 69 73 20 66 6f 75 6e 64 20 alue x is found
d820: 61 6e 79 20 6f 66 20 74 68 65 20 66 69 72 73 74 any of the first
d830: 20 6e 43 6f 6c 20 65 6e 74 72 69 65 73 20 6f 66 nCol entries of
d840: 20 61 69 43 6f 6c 5b 5d 0a 2a 2f 0a 73 74 61 74 aiCol[].*/.stat
d850: 69 63 20 69 6e 74 20 68 61 73 43 6f 6c 75 6d 6e ic int hasColumn
d860: 28 63 6f 6e 73 74 20 69 31 36 20 2a 61 69 43 6f (const i16 *aiCo
d870: 6c 2c 20 69 6e 74 20 6e 43 6f 6c 2c 20 69 6e 74 l, int nCol, int
d880: 20 78 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e 43 x){. while( nC
d890: 6f 6c 2d 2d 20 3e 20 30 20 29 20 69 66 28 20 78 ol-- > 0 ) if( x
d8a0: 3d 3d 2a 28 61 69 43 6f 6c 2b 2b 29 20 29 20 72 ==*(aiCol++) ) r
d8b0: 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 eturn 1;. retur
d8c0: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 63 6f 6d n 0;.}../* Recom
d8d0: 70 75 74 65 20 74 68 65 20 63 6f 6c 4e 6f 74 49 pute the colNotI
d8e0: 64 78 65 64 20 66 69 65 6c 64 20 6f 66 20 74 68 dxed field of th
d8f0: 65 20 49 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 63 e Index..**.** c
d900: 6f 6c 4e 6f 74 49 64 78 65 64 20 69 73 20 61 20 olNotIdxed is a
d910: 62 69 74 6d 61 73 6b 20 74 68 61 74 20 68 61 73 bitmask that has
d920: 20 61 20 30 20 62 69 74 20 72 65 70 72 65 73 65 a 0 bit represe
d930: 6e 74 69 6e 67 20 65 61 63 68 20 69 6e 64 65 78 nting each index
d940: 65 64 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 74 68 ed.** columns th
d950: 61 74 20 61 72 65 20 77 69 74 68 69 6e 20 74 68 at are within th
d960: 65 20 66 69 72 73 74 20 36 33 20 63 6f 6c 75 6d e first 63 colum
d970: 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e ns of the table.
d980: 20 20 54 68 65 0a 2a 2a 20 68 69 67 68 2d 6f 72 The.** high-or
d990: 64 65 72 20 62 69 74 20 6f 66 20 63 6f 6c 4e 6f der bit of colNo
d9a0: 74 49 64 78 65 64 20 69 73 20 61 6c 77 61 79 73 tIdxed is always
d9b0: 20 31 2e 20 20 41 6c 6c 20 75 6e 69 6e 64 65 78 1. All unindex
d9c0: 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6f 66 ed columns.** of
d9d0: 20 74 68 65 20 74 61 62 6c 65 20 68 61 76 65 20 the table have
d9e0: 61 20 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 a 1..**.** The c
d9f0: 6f 6c 4e 6f 74 49 64 78 65 64 20 6d 61 73 6b 20 olNotIdxed mask
da00: 69 73 20 41 4e 44 2d 65 64 20 77 69 74 68 20 74 is AND-ed with t
da10: 68 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 he SrcList.a[].c
da20: 6f 6c 55 73 65 64 20 6d 61 73 6b 0a 2a 2a 20 74 olUsed mask.** t
da30: 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 o determine if t
da40: 68 65 20 69 6e 64 65 78 20 69 73 20 63 6f 76 65 he index is cove
da50: 72 69 6e 67 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73 ring index..*/.s
da60: 74 61 74 69 63 20 76 6f 69 64 20 72 65 63 6f 6d tatic void recom
da70: 70 75 74 65 43 6f 6c 75 6d 6e 73 4e 6f 74 49 6e puteColumnsNotIn
da80: 64 65 78 65 64 28 49 6e 64 65 78 20 2a 70 49 64 dexed(Index *pId
da90: 78 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 20 x){. Bitmask m
daa0: 3d 20 30 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 = 0;. int j;.
dab0: 66 6f 72 28 6a 3d 70 49 64 78 2d 3e 6e 43 6f 6c for(j=pIdx->nCol
dac0: 75 6d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d umn-1; j>=0; j--
dad0: 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70 ){. int x = p
dae0: 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d Idx->aiColumn[j]
daf0: 3b 0a 20 20 20 20 69 66 28 20 78 3e 3d 30 20 29 ;. if( x>=0 )
db00: 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
db10: 28 20 78 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 ( x==BMS-1 );.
db20: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 78 3d testcase( x=
db30: 3d 42 4d 53 2d 32 20 29 3b 0a 20 20 20 20 20 20 =BMS-2 );.
db40: 69 66 28 20 78 3c 42 4d 53 2d 31 20 29 20 6d 20 if( x<BMS-1 ) m
db50: 7c 3d 20 4d 41 53 4b 42 49 54 28 78 29 3b 0a 20 |= MASKBIT(x);.
db60: 20 20 20 7d 0a 20 20 7d 0a 20 20 70 49 64 78 2d }. }. pIdx-
db70: 3e 63 6f 6c 4e 6f 74 49 64 78 65 64 20 3d 20 7e >colNotIdxed = ~
db80: 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 m;. assert( (pI
db90: 64 78 2d 3e 63 6f 6c 4e 6f 74 49 64 78 65 64 3e dx->colNotIdxed>
dba0: 3e 36 33 29 3d 3d 31 20 29 3b 0a 7d 0a 0a 2f 2a >63)==1 );.}../*
dbb0: 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
dbc0: 20 72 75 6e 73 20 61 74 20 74 68 65 20 65 6e 64 runs at the end
dbd0: 20 6f 66 20 70 61 72 73 69 6e 67 20 61 20 43 52 of parsing a CR
dbe0: 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
dbf0: 6d 65 6e 74 20 74 68 61 74 0a 2a 2a 20 68 61 73 ment that.** has
dc00: 20 61 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 a WITHOUT ROWID
dc10: 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20 6a 6f clause. The jo
dc20: 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e b of this routin
dc30: 65 20 69 73 20 74 6f 20 63 6f 6e 76 65 72 74 20 e is to convert
dc40: 62 6f 74 68 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c both.** internal
dc50: 20 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 72 schema data str
dc60: 75 63 74 75 72 65 73 20 61 6e 64 20 74 68 65 20 uctures and the
dc70: 67 65 6e 65 72 61 74 65 64 20 56 44 42 45 20 63 generated VDBE c
dc80: 6f 64 65 20 73 6f 20 74 68 61 74 20 74 68 65 79 ode so that they
dc90: 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69 .** are appropri
dca0: 61 74 65 20 66 6f 72 20 61 20 57 49 54 48 4f 55 ate for a WITHOU
dcb0: 54 20 52 4f 57 49 44 20 74 61 62 6c 65 20 69 6e T ROWID table in
dcc0: 73 74 65 61 64 20 6f 66 20 61 20 72 6f 77 69 64 stead of a rowid
dcd0: 20 74 61 62 6c 65 2e 0a 2a 2a 20 43 68 61 6e 67 table..** Chang
dce0: 65 73 20 69 6e 63 6c 75 64 65 3a 0a 2a 2a 0a 2a es include:.**.*
dcf0: 2a 20 20 20 20 20 28 31 29 20 20 53 65 74 20 61 * (1) Set a
dd00: 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 ll columns of th
dd10: 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 73 63 e PRIMARY KEY sc
dd20: 68 65 6d 61 20 6f 62 6a 65 63 74 20 74 6f 20 62 hema object to b
dd30: 65 20 4e 4f 54 20 4e 55 4c 4c 2e 0a 2a 2a 20 20 e NOT NULL..**
dd40: 20 20 20 28 32 29 20 20 43 6f 6e 76 65 72 74 20 (2) Convert
dd50: 50 33 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 P3 parameter of
dd60: 74 68 65 20 4f 50 5f 43 72 65 61 74 65 42 74 72 the OP_CreateBtr
dd70: 65 65 20 66 72 6f 6d 20 42 54 52 45 45 5f 49 4e ee from BTREE_IN
dd80: 54 4b 45 59 20 0a 2a 2a 20 20 20 20 20 20 20 20 TKEY .**
dd90: 20 20 69 6e 74 6f 20 42 54 52 45 45 5f 42 4c 4f into BTREE_BLO
dda0: 42 4b 45 59 2e 0a 2a 2a 20 20 20 20 20 28 33 29 BKEY..** (3)
ddb0: 20 20 42 79 70 61 73 73 20 74 68 65 20 63 72 65 Bypass the cre
ddc0: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
ddd0: 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
dde0: 20 65 6e 74 72 79 0a 2a 2a 20 20 20 20 20 20 20 entry.**
ddf0: 20 20 20 66 6f 72 20 74 68 65 20 50 52 49 4d 41 for the PRIMA
de00: 52 59 20 4b 45 59 20 61 73 20 74 68 65 20 70 72 RY KEY as the pr
de10: 69 6d 61 72 79 20 6b 65 79 20 69 6e 64 65 78 20 imary key index
de20: 69 73 20 6e 6f 77 0a 2a 2a 20 20 20 20 20 20 20 is now.**
de30: 20 20 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 identified by
de40: 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 the sqlite_mast
de50: 65 72 20 74 61 62 6c 65 20 65 6e 74 72 79 20 6f er table entry o
de60: 66 20 74 68 65 20 74 61 62 6c 65 20 69 74 73 65 f the table itse
de70: 6c 66 2e 0a 2a 2a 20 20 20 20 20 28 34 29 20 20 lf..** (4)
de80: 53 65 74 20 74 68 65 20 49 6e 64 65 78 2e 74 6e Set the Index.tn
de90: 75 6d 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 um of the PRIMAR
dea0: 59 20 4b 45 59 20 49 6e 64 65 78 20 6f 62 6a 65 Y KEY Index obje
deb0: 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 ct in the.**
dec0: 20 20 20 20 20 20 73 63 68 65 6d 61 20 74 6f 20 schema to
ded0: 74 68 65 20 72 6f 6f 74 70 61 67 65 20 66 72 6f the rootpage fro
dee0: 6d 20 74 68 65 20 6d 61 69 6e 20 74 61 62 6c 65 m the main table
def0: 2e 0a 2a 2a 20 20 20 20 20 28 35 29 20 20 41 64 ..** (5) Ad
df00: 64 20 61 6c 6c 20 74 61 62 6c 65 20 63 6f 6c 75 d all table colu
df10: 6d 6e 73 20 74 6f 20 74 68 65 20 50 52 49 4d 41 mns to the PRIMA
df20: 52 59 20 4b 45 59 20 49 6e 64 65 78 20 6f 62 6a RY KEY Index obj
df30: 65 63 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ect.**
df40: 73 6f 20 74 68 61 74 20 74 68 65 20 50 52 49 4d so that the PRIM
df50: 41 52 59 20 4b 45 59 20 69 73 20 61 20 63 6f 76 ARY KEY is a cov
df60: 65 72 69 6e 67 20 69 6e 64 65 78 2e 20 20 54 68 ering index. Th
df70: 65 20 73 75 72 70 6c 75 73 0a 2a 2a 20 20 20 20 e surplus.**
df80: 20 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 61 72 columns ar
df90: 65 20 70 61 72 74 20 6f 66 20 4b 65 79 49 6e 66 e part of KeyInf
dfa0: 6f 2e 6e 41 6c 6c 46 69 65 6c 64 20 61 6e 64 20 o.nAllField and
dfb0: 61 72 65 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 are not used for
dfc0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 6f 72 .** sor
dfd0: 74 69 6e 67 20 6f 72 20 6c 6f 6f 6b 75 70 20 6f ting or lookup o
dfe0: 72 20 75 6e 69 71 75 65 6e 65 73 73 20 63 68 65 r uniqueness che
dff0: 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 28 36 29 20 cks..** (6)
e000: 20 52 65 70 6c 61 63 65 20 74 68 65 20 72 6f 77 Replace the row
e010: 69 64 20 74 61 69 6c 20 6f 6e 20 61 6c 6c 20 61 id tail on all a
e020: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e utomatically gen
e030: 65 72 61 74 65 64 20 55 4e 49 51 55 45 0a 2a 2a erated UNIQUE.**
e040: 20 20 20 20 20 20 20 20 20 20 69 6e 64 69 63 65 indice
e050: 73 20 77 69 74 68 20 74 68 65 20 50 52 49 4d 41 s with the PRIMA
e060: 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 73 2e 0a RY KEY columns..
e070: 2a 2a 0a 2a 2a 20 46 6f 72 20 76 69 72 74 75 61 **.** For virtua
e080: 6c 20 74 61 62 6c 65 73 2c 20 6f 6e 6c 79 20 28 l tables, only (
e090: 31 29 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 1) is performed.
e0a0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
e0b0: 63 6f 6e 76 65 72 74 54 6f 57 69 74 68 6f 75 74 convertToWithout
e0c0: 52 6f 77 69 64 54 61 62 6c 65 28 50 61 72 73 65 RowidTable(Parse
e0d0: 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 *pParse, Table
e0e0: 2a 70 54 61 62 29 7b 0a 20 20 49 6e 64 65 78 20 *pTab){. Index
e0f0: 2a 70 49 64 78 3b 0a 20 20 49 6e 64 65 78 20 2a *pIdx;. Index *
e100: 70 50 6b 3b 0a 20 20 69 6e 74 20 6e 50 6b 3b 0a pPk;. int nPk;.
e110: 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 71 int i, j;. sq
e120: 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
e130: 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a se->db;. Vdbe *
e140: 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
e150: 65 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 65 76 e;.. /* Mark ev
e160: 65 72 79 20 50 52 49 4d 41 52 59 20 4b 45 59 20 ery PRIMARY KEY
e170: 63 6f 6c 75 6d 6e 20 61 73 20 4e 4f 54 20 4e 55 column as NOT NU
e180: 4c 4c 20 28 65 78 63 65 70 74 20 66 6f 72 20 69 LL (except for i
e190: 6d 70 6f 73 74 65 72 20 74 61 62 6c 65 73 29 0a mposter tables).
e1a0: 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e */. if( !db->
e1b0: 69 6e 69 74 2e 69 6d 70 6f 73 74 65 72 54 61 62 init.imposterTab
e1c0: 6c 65 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d le ){. for(i=
e1d0: 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0; i<pTab->nCol;
e1e0: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
e1f0: 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e (pTab->aCol[i].
e200: 63 6f 6c 46 6c 61 67 73 20 26 20 43 4f 4c 46 4c colFlags & COLFL
e210: 41 47 5f 50 52 49 4d 4b 45 59 29 21 3d 30 20 29 AG_PRIMKEY)!=0 )
e220: 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e {. pTab->
e230: 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 aCol[i].notNull
e240: 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 = OE_Abort;.
e250: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
e260: 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 /* Convert the
e270: 50 33 20 6f 70 65 72 61 6e 64 20 6f 66 20 74 68 P3 operand of th
e280: 65 20 4f 50 5f 43 72 65 61 74 65 42 74 72 65 65 e OP_CreateBtree
e290: 20 6f 70 63 6f 64 65 20 66 72 6f 6d 20 42 54 52 opcode from BTR
e2a0: 45 45 5f 49 4e 54 4b 45 59 0a 20 20 2a 2a 20 69 EE_INTKEY. ** i
e2b0: 6e 74 6f 20 42 54 52 45 45 5f 42 4c 4f 42 4b 45 nto BTREE_BLOBKE
e2c0: 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 Y.. */. if( pP
e2d0: 61 72 73 65 2d 3e 61 64 64 72 43 72 54 61 62 20 arse->addrCrTab
e2e0: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 76 ){. assert( v
e2f0: 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 );. sqlite3V
e300: 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 70 dbeChangeP3(v, p
e310: 50 61 72 73 65 2d 3e 61 64 64 72 43 72 54 61 62 Parse->addrCrTab
e320: 2c 20 42 54 52 45 45 5f 42 4c 4f 42 4b 45 59 29 , BTREE_BLOBKEY)
e330: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 ;. }.. /* Loca
e340: 74 65 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b te the PRIMARY K
e350: 45 59 20 69 6e 64 65 78 2e 20 20 4f 72 2c 20 69 EY index. Or, i
e360: 66 20 74 68 69 73 20 74 61 62 6c 65 20 77 61 73 f this table was
e370: 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a originally. **
e380: 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d an INTEGER PRIM
e390: 41 52 59 20 4b 45 59 20 74 61 62 6c 65 2c 20 63 ARY KEY table, c
e3a0: 72 65 61 74 65 20 61 20 6e 65 77 20 50 52 49 4d reate a new PRIM
e3b0: 41 52 59 20 4b 45 59 20 69 6e 64 65 78 2e 20 0a ARY KEY index. .
e3c0: 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d */. if( pTab-
e3d0: 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 >iPKey>=0 ){.
e3e0: 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
e3f0: 3b 0a 20 20 20 20 54 6f 6b 65 6e 20 69 70 6b 54 ;. Token ipkT
e400: 6f 6b 65 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 oken;. sqlite
e410: 33 54 6f 6b 65 6e 49 6e 69 74 28 26 69 70 6b 54 3TokenInit(&ipkT
e420: 6f 6b 65 6e 2c 20 70 54 61 62 2d 3e 61 43 6f 6c oken, pTab->aCol
e430: 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e [pTab->iPKey].zN
e440: 61 6d 65 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 ame);. pList
e450: 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 = sqlite3ExprLis
e460: 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 tAppend(pParse,
e470: 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 0, .
e480: 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
e490: 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 49 44 rAlloc(db, TK_ID
e4a0: 2c 20 26 69 70 6b 54 6f 6b 65 6e 2c 20 30 29 29 , &ipkToken, 0))
e4b0: 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d ;. if( pList=
e4c0: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 =0 ) return;.
e4d0: 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72 pList->a[0].sor
e4e0: 74 4f 72 64 65 72 20 3d 20 70 50 61 72 73 65 2d tOrder = pParse-
e4f0: 3e 69 50 6b 53 6f 72 74 4f 72 64 65 72 3b 0a 20 >iPkSortOrder;.
e500: 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 assert( pPars
e510: 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 70 54 e->pNewTable==pT
e520: 61 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ab );. sqlite
e530: 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 3CreateIndex(pPa
e540: 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 70 4c rse, 0, 0, 0, pL
e550: 69 73 74 2c 20 70 54 61 62 2d 3e 6b 65 79 43 6f ist, pTab->keyCo
e560: 6e 66 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a nf, 0, 0, 0, 0,.
e570: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e580: 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 44 SQLITE_ID
e590: 58 54 59 50 45 5f 50 52 49 4d 41 52 59 4b 45 59 XTYPE_PRIMARYKEY
e5a0: 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
e5b0: 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 allocFailed || p
e5c0: 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 Parse->nErr ) re
e5d0: 74 75 72 6e 3b 0a 20 20 20 20 70 50 6b 20 3d 20 turn;. pPk =
e5e0: 73 71 6c 69 74 65 33 50 72 69 6d 61 72 79 4b 65 sqlite3PrimaryKe
e5f0: 79 49 6e 64 65 78 28 70 54 61 62 29 3b 0a 20 20 yIndex(pTab);.
e600: 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20 pTab->iPKey =
e610: 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 -1;. }else{.
e620: 20 70 50 6b 20 3d 20 73 71 6c 69 74 65 33 50 72 pPk = sqlite3Pr
e630: 69 6d 61 72 79 4b 65 79 49 6e 64 65 78 28 70 54 imaryKeyIndex(pT
e640: 61 62 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 ab);.. /*.
e650: 20 2a 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 72 ** Remove all r
e660: 65 64 75 6e 64 61 6e 74 20 63 6f 6c 75 6d 6e 73 edundant columns
e670: 20 66 72 6f 6d 20 74 68 65 20 50 52 49 4d 41 52 from the PRIMAR
e680: 59 20 4b 45 59 2e 20 20 46 6f 72 20 65 78 61 6d Y KEY. For exam
e690: 70 6c 65 2c 20 63 68 61 6e 67 65 0a 20 20 20 20 ple, change.
e6a0: 2a 2a 20 22 50 52 49 4d 41 52 59 20 4b 45 59 28 ** "PRIMARY KEY(
e6b0: 61 2c 62 2c 61 2c 62 2c 63 2c 62 2c 63 2c 64 29 a,b,a,b,c,b,c,d)
e6c0: 22 20 69 6e 74 6f 20 6a 75 73 74 20 22 50 52 49 " into just "PRI
e6d0: 4d 41 52 59 20 4b 45 59 28 61 2c 62 2c 63 2c 64 MARY KEY(a,b,c,d
e6e0: 29 22 2e 20 20 4c 61 74 65 72 0a 20 20 20 20 2a )". Later. *
e6f0: 2a 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 * code assumes t
e700: 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 he PRIMARY KEY c
e710: 6f 6e 74 61 69 6e 73 20 6e 6f 20 72 65 70 65 61 ontains no repea
e720: 74 65 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 20 ted columns..
e730: 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 6a 3d */. for(i=j=
e740: 31 3b 20 69 3c 70 50 6b 2d 3e 6e 4b 65 79 43 6f 1; i<pPk->nKeyCo
e750: 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 l; i++){. i
e760: 66 28 20 68 61 73 43 6f 6c 75 6d 6e 28 70 50 6b f( hasColumn(pPk
e770: 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 6a 2c 20 70 ->aiColumn, j, p
e780: 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 29 Pk->aiColumn[i])
e790: 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 6b 2d ){. pPk-
e7a0: 3e 6e 43 6f 6c 75 6d 6e 2d 2d 3b 0a 20 20 20 20 >nColumn--;.
e7b0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
e7c0: 20 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a pPk->aiColumn[j
e7d0: 2b 2b 5d 20 3d 20 70 50 6b 2d 3e 61 69 43 6f 6c ++] = pPk->aiCol
e7e0: 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a umn[i];. }.
e7f0: 20 20 20 20 7d 0a 20 20 20 20 70 50 6b 2d 3e 6e }. pPk->n
e800: 4b 65 79 43 6f 6c 20 3d 20 6a 3b 0a 20 20 7d 0a KeyCol = j;. }.
e810: 20 20 61 73 73 65 72 74 28 20 70 50 6b 21 3d 30 assert( pPk!=0
e820: 20 29 3b 0a 20 20 70 50 6b 2d 3e 69 73 43 6f 76 );. pPk->isCov
e830: 65 72 69 6e 67 20 3d 20 31 3b 0a 20 20 69 66 28 ering = 1;. if(
e840: 20 21 64 62 2d 3e 69 6e 69 74 2e 69 6d 70 6f 73 !db->init.impos
e850: 74 65 72 54 61 62 6c 65 20 29 20 70 50 6b 2d 3e terTable ) pPk->
e860: 75 6e 69 71 4e 6f 74 4e 75 6c 6c 20 3d 20 31 3b uniqNotNull = 1;
e870: 0a 20 20 6e 50 6b 20 3d 20 70 50 6b 2d 3e 6e 4b . nPk = pPk->nK
e880: 65 79 43 6f 6c 3b 0a 0a 20 20 2f 2a 20 42 79 70 eyCol;.. /* Byp
e890: 61 73 73 20 74 68 65 20 63 72 65 61 74 69 6f 6e ass the creation
e8a0: 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 of the PRIMARY
e8b0: 4b 45 59 20 62 74 72 65 65 20 61 6e 64 20 74 68 KEY btree and th
e8c0: 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 0a e sqlite_master.
e8d0: 20 20 2a 2a 20 74 61 62 6c 65 20 65 6e 74 72 79 ** table entry
e8e0: 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 72 . This is only r
e8f0: 65 71 75 69 72 65 64 20 69 66 20 63 75 72 72 65 equired if curre
e900: 6e 74 6c 79 20 67 65 6e 65 72 61 74 69 6e 67 20 ntly generating
e910: 56 44 42 45 0a 20 20 2a 2a 20 63 6f 64 65 20 66 VDBE. ** code f
e920: 6f 72 20 61 20 43 52 45 41 54 45 20 54 41 42 4c or a CREATE TABL
e930: 45 20 28 6e 6f 74 20 77 68 65 6e 20 70 61 72 73 E (not when pars
e940: 69 6e 67 20 6f 6e 65 20 61 73 20 70 61 72 74 20 ing one as part
e950: 6f 66 20 72 65 61 64 69 6e 67 0a 20 20 2a 2a 20 of reading. **
e960: 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d a database schem
e970: 61 29 2e 20 20 2a 2f 0a 20 20 69 66 28 20 76 20 a). */. if( v
e980: 26 26 20 70 50 6b 2d 3e 74 6e 75 6d 3e 30 20 29 && pPk->tnum>0 )
e990: 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 {. assert( db
e9a0: 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 ->init.busy==0 )
e9b0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
e9c0: 65 43 68 61 6e 67 65 4f 70 63 6f 64 65 28 76 2c eChangeOpcode(v,
e9d0: 20 70 50 6b 2d 3e 74 6e 75 6d 2c 20 4f 50 5f 47 pPk->tnum, OP_G
e9e0: 6f 74 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 oto);. }.. /*
e9f0: 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 The root page of
ea00: 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
ea10: 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 72 6f is the table ro
ea20: 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 70 50 6b ot page */. pPk
ea30: 2d 3e 74 6e 75 6d 20 3d 20 70 54 61 62 2d 3e 74 ->tnum = pTab->t
ea40: 6e 75 6d 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 num;.. /* Updat
ea50: 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 e the in-memory
ea60: 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
ea70: 66 20 61 6c 6c 20 55 4e 49 51 55 45 20 69 6e 64 f all UNIQUE ind
ea80: 69 63 65 73 20 62 79 20 63 6f 6e 76 65 72 74 69 ices by converti
ea90: 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 69 6e 61 ng. ** the fina
eaa0: 6c 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69 l rowid column i
eab0: 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 nto one or more
eac0: 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 50 columns of the P
ead0: 52 49 4d 41 52 59 20 4b 45 59 2e 0a 20 20 2a 2f RIMARY KEY.. */
eae0: 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 . for(pIdx=pTab
eaf0: 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 ->pIndex; pIdx;
eb00: 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 pIdx=pIdx->pNext
eb10: 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 ){. int n;.
eb20: 20 20 69 66 28 20 49 73 50 72 69 6d 61 72 79 4b if( IsPrimaryK
eb30: 65 79 49 6e 64 65 78 28 70 49 64 78 29 20 29 20 eyIndex(pIdx) )
eb40: 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 6f continue;. fo
eb50: 72 28 69 3d 6e 3d 30 3b 20 69 3c 6e 50 6b 3b 20 r(i=n=0; i<nPk;
eb60: 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
eb70: 21 68 61 73 43 6f 6c 75 6d 6e 28 70 49 64 78 2d !hasColumn(pIdx-
eb80: 3e 61 69 43 6f 6c 75 6d 6e 2c 20 70 49 64 78 2d >aiColumn, pIdx-
eb90: 3e 6e 4b 65 79 43 6f 6c 2c 20 70 50 6b 2d 3e 61 >nKeyCol, pPk->a
eba0: 69 43 6f 6c 75 6d 6e 5b 69 5d 29 20 29 20 6e 2b iColumn[i]) ) n+
ebb0: 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 +;. }. if(
ebc0: 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f n==0 ){. /
ebd0: 2a 20 54 68 69 73 20 69 6e 64 65 78 20 69 73 20 * This index is
ebe0: 61 20 73 75 70 65 72 73 65 74 20 6f 66 20 74 68 a superset of th
ebf0: 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f e primary key */
ec00: 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f . pIdx->nCo
ec10: 6c 75 6d 6e 20 3d 20 70 49 64 78 2d 3e 6e 4b 65 lumn = pIdx->nKe
ec20: 79 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 yCol;. cont
ec30: 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 inue;. }.
ec40: 69 66 28 20 72 65 73 69 7a 65 49 6e 64 65 78 4f if( resizeIndexO
ec50: 62 6a 65 63 74 28 64 62 2c 20 70 49 64 78 2c 20 bject(db, pIdx,
ec60: 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 2b 6e 29 pIdx->nKeyCol+n)
ec70: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 66 ) return;. f
ec80: 6f 72 28 69 3d 30 2c 20 6a 3d 70 49 64 78 2d 3e or(i=0, j=pIdx->
ec90: 6e 4b 65 79 43 6f 6c 3b 20 69 3c 6e 50 6b 3b 20 nKeyCol; i<nPk;
eca0: 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
ecb0: 21 68 61 73 43 6f 6c 75 6d 6e 28 70 49 64 78 2d !hasColumn(pIdx-
ecc0: 3e 61 69 43 6f 6c 75 6d 6e 2c 20 70 49 64 78 2d >aiColumn, pIdx-
ecd0: 3e 6e 4b 65 79 43 6f 6c 2c 20 70 50 6b 2d 3e 61 >nKeyCol, pPk->a
ece0: 69 43 6f 6c 75 6d 6e 5b 69 5d 29 20 29 7b 0a 20 iColumn[i]) ){.
ecf0: 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61 69 43 pIdx->aiC
ed00: 6f 6c 75 6d 6e 5b 6a 5d 20 3d 20 70 50 6b 2d 3e olumn[j] = pPk->
ed10: 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 aiColumn[i];.
ed20: 20 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c pIdx->azCol
ed30: 6c 5b 6a 5d 20 3d 20 70 50 6b 2d 3e 61 7a 43 6f l[j] = pPk->azCo
ed40: 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 6a ll[i];. j
ed50: 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
ed60: 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 }. assert( pI
ed70: 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 70 49 64 dx->nColumn>=pId
ed80: 78 2d 3e 6e 4b 65 79 43 6f 6c 2b 6e 20 29 3b 0a x->nKeyCol+n );.
ed90: 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 assert( pIdx
eda0: 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 3b 0a ->nColumn>=j );.
edb0: 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 61 6c }.. /* Add al
edc0: 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 l table columns
edd0: 74 6f 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b to the PRIMARY K
ede0: 45 59 20 69 6e 64 65 78 0a 20 20 2a 2f 0a 20 20 EY index. */.
edf0: 69 66 28 20 6e 50 6b 3c 70 54 61 62 2d 3e 6e 43 if( nPk<pTab->nC
ee00: 6f 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 72 65 ol ){. if( re
ee10: 73 69 7a 65 49 6e 64 65 78 4f 62 6a 65 63 74 28 sizeIndexObject(
ee20: 64 62 2c 20 70 50 6b 2c 20 70 54 61 62 2d 3e 6e db, pPk, pTab->n
ee30: 43 6f 6c 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 Col) ) return;.
ee40: 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 50 for(i=0, j=nP
ee50: 6b 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b k; i<pTab->nCol;
ee60: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
ee70: 20 21 68 61 73 43 6f 6c 75 6d 6e 28 70 50 6b 2d !hasColumn(pPk-
ee80: 3e 61 69 43 6f 6c 75 6d 6e 2c 20 6a 2c 20 69 29 >aiColumn, j, i)
ee90: 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
eea0: 72 74 28 20 6a 3c 70 50 6b 2d 3e 6e 43 6f 6c 75 rt( j<pPk->nColu
eeb0: 6d 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 50 mn );. pP
eec0: 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 20 3d k->aiColumn[j] =
eed0: 20 69 3b 0a 20 20 20 20 20 20 20 20 70 50 6b 2d i;. pPk-
eee0: 3e 61 7a 43 6f 6c 6c 5b 6a 5d 20 3d 20 73 71 6c >azColl[j] = sql
eef0: 69 74 65 33 53 74 72 42 49 4e 41 52 59 3b 0a 20 ite3StrBINARY;.
ef00: 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 j++;.
ef10: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 }. }. as
ef20: 73 65 72 74 28 20 70 50 6b 2d 3e 6e 43 6f 6c 75 sert( pPk->nColu
ef30: 6d 6e 3d 3d 6a 20 29 3b 0a 20 20 20 20 61 73 73 mn==j );. ass
ef40: 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d ert( pTab->nCol=
ef50: 3d 6a 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 =j );. }else{.
ef60: 20 20 20 70 50 6b 2d 3e 6e 43 6f 6c 75 6d 6e 20 pPk->nColumn
ef70: 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 = pTab->nCol;.
ef80: 7d 0a 20 20 72 65 63 6f 6d 70 75 74 65 43 6f 6c }. recomputeCol
ef90: 75 6d 6e 73 4e 6f 74 49 6e 64 65 78 65 64 28 70 umnsNotIndexed(p
efa0: 50 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 Pk);.}../*.** Th
efb0: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
efc0: 6c 6c 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 lled to report t
efd0: 68 65 20 66 69 6e 61 6c 20 22 29 22 20 74 68 61 he final ")" tha
efe0: 74 20 74 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 t terminates.**
eff0: 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 a CREATE TABLE s
f000: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 tatement..**.**
f010: 54 68 65 20 74 61 62 6c 65 20 73 74 72 75 63 74 The table struct
f020: 75 72 65 20 74 68 61 74 20 6f 74 68 65 72 20 61 ure that other a
f030: 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68 ction routines h
f040: 61 76 65 20 62 65 65 6e 20 62 75 69 6c 64 69 6e ave been buildin
f050: 67 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f g.** is added to
f060: 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 the internal ha
f070: 73 68 20 74 61 62 6c 65 73 2c 20 61 73 73 75 6d sh tables, assum
f080: 69 6e 67 20 6e 6f 20 65 72 72 6f 72 73 20 68 61 ing no errors ha
f090: 76 65 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a ve.** occurred..
f0a0: 2a 2a 0a 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66 **.** An entry f
f0b0: 6f 72 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 or the table is
f0c0: 6d 61 64 65 20 69 6e 20 74 68 65 20 6d 61 73 74 made in the mast
f0d0: 65 72 20 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b er table on disk
f0e0: 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73 , unless.** this
f0f0: 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 is a temporary
f100: 74 61 62 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69 table or db->ini
f110: 74 2e 62 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e t.busy==1. When
f120: 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d db->init.busy==
f130: 31 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 77 65 1.** it means we
f140: 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 are reading the
f150: 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
f160: 61 62 6c 65 20 62 65 63 61 75 73 65 20 77 65 20 able because we
f170: 6a 75 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 just.** connecte
f180: 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 d to the databas
f190: 65 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 e or because the
f1a0: 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
f1b0: 61 62 6c 65 20 68 61 73 0a 2a 2a 20 72 65 63 65 able has.** rece
f1c0: 6e 74 6c 79 20 63 68 61 6e 67 65 64 2c 20 73 6f ntly changed, so
f1d0: 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 the entry for t
f1e0: 68 69 73 20 74 61 62 6c 65 20 61 6c 72 65 61 64 his table alread
f1f0: 79 20 65 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74 y exists in.** t
f200: 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 he sqlite_master
f210: 20 74 61 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e table. We do n
f220: 6f 74 20 77 61 6e 74 20 74 6f 20 63 72 65 61 74 ot want to creat
f230: 65 20 69 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a e it again..**.*
f240: 2a 20 49 66 20 74 68 65 20 70 53 65 6c 65 63 74 * If the pSelect
f250: 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 argument is not
f260: 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 NULL, it means
f270: 74 68 61 74 20 74 68 69 73 20 72 6f 75 74 69 6e that this routin
f280: 65 0a 2a 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 e.** was called
f290: 74 6f 20 63 72 65 61 74 65 20 61 20 74 61 62 6c to create a tabl
f2a0: 65 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d e generated from
f2b0: 20 61 20 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 a .** "CREATE T
f2c0: 41 42 4c 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 ABLE ... AS SELE
f2d0: 43 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e CT ..." statemen
f2e0: 74 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e t. The column n
f2f0: 61 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e ames of.** the n
f300: 65 77 20 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61 ew table will ma
f310: 74 63 68 20 74 68 65 20 72 65 73 75 6c 74 20 73 tch the result s
f320: 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 et of the SELECT
f330: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
f340: 33 45 6e 64 54 61 62 6c 65 28 0a 20 20 50 61 72 3EndTable(. Par
f350: 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
f360: 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f /* Parse co
f370: 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e ntext */. Token
f380: 20 2a 70 43 6f 6e 73 2c 20 20 20 20 20 20 20 20 *pCons,
f390: 20 20 20 2f 2a 20 54 68 65 20 27 2c 27 20 74 6f /* The ',' to
f3a0: 6b 65 6e 20 61 66 74 65 72 20 74 68 65 20 6c 61 ken after the la
f3b0: 73 74 20 63 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 st column defn.
f3c0: 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 */. Token *pEnd
f3d0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
f3e0: 54 68 65 20 27 29 27 20 62 65 66 6f 72 65 20 6f The ')' before o
f3f0: 70 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 43 52 ptions in the CR
f400: 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20 EATE TABLE */.
f410: 75 38 20 74 61 62 4f 70 74 73 2c 20 20 20 20 20 u8 tabOpts,
f420: 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 /* Extra
f430: 20 74 61 62 6c 65 20 6f 70 74 69 6f 6e 73 2e 20 table options.
f440: 55 73 75 61 6c 6c 79 20 30 2e 20 2a 2f 0a 20 20 Usually 0. */.
f450: 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 Select *pSelect
f460: 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 /* Selec
f470: 74 20 66 72 6f 6d 20 61 20 22 43 52 45 41 54 45 t from a "CREATE
f480: 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 22 20 ... AS SELECT"
f490: 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 */.){. Table *p
f4a0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
f4b0: 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 62 /* The new tab
f4c0: 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 le */. sqlite3
f4d0: 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
f4e0: 3b 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 ; /* The databas
f4f0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
f500: 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
f510: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
f520: 61 74 61 62 61 73 65 20 69 6e 20 77 68 69 63 68 atabase in which
f530: 20 74 68 65 20 74 61 62 6c 65 20 6c 69 76 65 73 the table lives
f540: 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 */. Index *pId
f550: 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
f560: 2f 2a 20 41 6e 20 69 6d 70 6c 69 65 64 20 69 6e /* An implied in
f570: 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65 dex of the table
f580: 20 2a 2f 0a 0a 20 20 69 66 28 20 70 45 6e 64 3d */.. if( pEnd=
f590: 3d 30 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 =0 && pSelect==0
f5a0: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
f5b0: 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 64 }. assert( !d
f5c0: 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
f5d0: 29 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d );. p = pParse-
f5e0: 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 >pNewTable;. if
f5f0: 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( p==0 ) return;
f600: 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 .. /* If the db
f610: 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 ->init.busy is 1
f620: 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 it means we are
f630: 20 72 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c reading the SQL
f640: 20 6f 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 off the. ** "s
f650: 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 qlite_master" or
f660: 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 "sqlite_temp_ma
f670: 73 74 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 ster" table on t
f680: 68 65 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f he disk.. ** So
f690: 20 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f do not write to
f6a0: 20 74 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e the disk again.
f6b0: 20 20 45 78 74 72 61 63 74 20 74 68 65 20 72 6f Extract the ro
f6c0: 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 ot page number.
f6d0: 20 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c ** for the tabl
f6e0: 65 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 e from the db->i
f6f0: 6e 69 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c nit.newTnum fiel
f700: 64 2e 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 d. (The page nu
f710: 6d 62 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 mber. ** should
f720: 20 68 61 76 65 20 62 65 65 6e 20 70 75 74 20 74 have been put t
f730: 68 65 72 65 20 62 79 20 74 68 65 20 73 71 6c 69 here by the sqli
f740: 74 65 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 teOpenCb routine
f750: 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 .). **. ** If
f760: 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 the root page nu
f770: 6d 62 65 72 20 69 73 20 31 2c 20 74 68 61 74 20 mber is 1, that
f780: 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 74 68 means this is th
f790: 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 0a e sqlite_master.
f7a0: 20 20 2a 2a 20 74 61 62 6c 65 20 69 74 73 65 6c ** table itsel
f7b0: 66 2e 20 20 53 6f 20 6d 61 72 6b 20 69 74 20 72 f. So mark it r
f7c0: 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20 2a 2f 0a 20 ead-only.. */.
f7d0: 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 if( db->init.bu
f7e0: 73 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 53 sy ){. if( pS
f7f0: 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 73 elect ){. s
f800: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
f810: 50 61 72 73 65 2c 20 22 22 29 3b 0a 20 20 20 20 Parse, "");.
f820: 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
f830: 20 20 20 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 p->tnum = db
f840: 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a ->init.newTnum;.
f850: 20 20 20 20 69 66 28 20 70 2d 3e 74 6e 75 6d 3d if( p->tnum=
f860: 3d 31 20 29 20 70 2d 3e 74 61 62 46 6c 61 67 73 =1 ) p->tabFlags
f870: 20 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c 79 3b |= TF_Readonly;
f880: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 70 65 63 69 . }.. /* Speci
f890: 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f al processing fo
f8a0: 72 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 20 r WITHOUT ROWID
f8b0: 54 61 62 6c 65 73 20 2a 2f 0a 20 20 69 66 28 20 Tables */. if(
f8c0: 74 61 62 4f 70 74 73 20 26 20 54 46 5f 57 69 74 tabOpts & TF_Wit
f8d0: 68 6f 75 74 52 6f 77 69 64 20 29 7b 0a 20 20 20 houtRowid ){.
f8e0: 20 69 66 28 20 28 70 2d 3e 74 61 62 46 6c 61 67 if( (p->tabFlag
f8f0: 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 s & TF_Autoincre
f900: 6d 65 6e 74 29 20 29 7b 0a 20 20 20 20 20 20 73 ment) ){. s
f910: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
f920: 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 Parse,.
f930: 20 22 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 "AUTOINCREMENT
f940: 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 57 not allowed on W
f950: 49 54 48 4f 55 54 20 52 4f 57 49 44 20 74 61 62 ITHOUT ROWID tab
f960: 6c 65 73 22 29 3b 0a 20 20 20 20 20 20 72 65 74 les");. ret
f970: 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 urn;. }. i
f980: 66 28 20 28 70 2d 3e 74 61 62 46 6c 61 67 73 20 f( (p->tabFlags
f990: 26 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b & TF_HasPrimaryK
f9a0: 65 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ey)==0 ){.
f9b0: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
f9c0: 70 50 61 72 73 65 2c 20 22 50 52 49 4d 41 52 59 pParse, "PRIMARY
f9d0: 20 4b 45 59 20 6d 69 73 73 69 6e 67 20 6f 6e 20 KEY missing on
f9e0: 74 61 62 6c 65 20 25 73 22 2c 20 70 2d 3e 7a 4e table %s", p->zN
f9f0: 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ame);. }else{
fa00: 0a 20 20 20 20 20 20 70 2d 3e 74 61 62 46 6c 61 . p->tabFla
fa10: 67 73 20 7c 3d 20 54 46 5f 57 69 74 68 6f 75 74 gs |= TF_Without
fa20: 52 6f 77 69 64 20 7c 20 54 46 5f 4e 6f 56 69 73 Rowid | TF_NoVis
fa30: 69 62 6c 65 52 6f 77 69 64 3b 0a 20 20 20 20 20 ibleRowid;.
fa40: 20 63 6f 6e 76 65 72 74 54 6f 57 69 74 68 6f 75 convertToWithou
fa50: 74 52 6f 77 69 64 54 61 62 6c 65 28 70 50 61 72 tRowidTable(pPar
fa60: 73 65 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 se, p);. }.
fa70: 7d 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 }.. iDb = sqlit
fa80: 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
fa90: 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b db, p->pSchema);
faa0: 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
fab0: 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a _OMIT_CHECK. /*
fac0: 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 Resolve names i
fad0: 6e 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 n all CHECK cons
fae0: 74 72 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f traint expressio
faf0: 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ns.. */. if( p
fb00: 2d 3e 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 ->pCheck ){.
fb10: 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 sqlite3ResolveSe
fb20: 6c 66 52 65 66 65 72 65 6e 63 65 28 70 50 61 72 lfReference(pPar
fb30: 73 65 2c 20 70 2c 20 4e 43 5f 49 73 43 68 65 63 se, p, NC_IsChec
fb40: 6b 2c 20 30 2c 20 70 2d 3e 70 43 68 65 63 6b 29 k, 0, p->pCheck)
fb50: 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
fb60: 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
fb70: 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a OMIT_CHECK) */..
fb80: 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 20 74 68 /* Estimate th
fb90: 65 20 61 76 65 72 61 67 65 20 72 6f 77 20 73 69 e average row si
fba0: 7a 65 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 ze for the table
fbb0: 20 61 6e 64 20 66 6f 72 20 61 6c 6c 20 69 6d 70 and for all imp
fbc0: 6c 69 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a lied indices */.
fbd0: 20 20 65 73 74 69 6d 61 74 65 54 61 62 6c 65 57 estimateTableW
fbe0: 69 64 74 68 28 70 29 3b 0a 20 20 66 6f 72 28 70 idth(p);. for(p
fbf0: 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 Idx=p->pIndex; p
fc00: 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
fc10: 70 4e 65 78 74 29 7b 0a 20 20 20 20 65 73 74 69 pNext){. esti
fc20: 6d 61 74 65 49 6e 64 65 78 57 69 64 74 68 28 70 mateIndexWidth(p
fc30: 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Idx);. }.. /*
fc40: 49 66 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a If not initializ
fc50: 69 6e 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65 ing, then create
fc60: 20 61 20 72 65 63 6f 72 64 20 66 6f 72 20 74 68 a record for th
fc70: 65 20 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a 2a e new table. **
fc80: 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d in the SQLITE_M
fc90: 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66 20 74 ASTER table of t
fca0: 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a he database.. *
fcb0: 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 *. ** If this i
fcc0: 73 20 61 20 54 45 4d 50 4f 52 41 52 59 20 74 61 s a TEMPORARY ta
fcd0: 62 6c 65 2c 20 77 72 69 74 65 20 74 68 65 20 65 ble, write the e
fce0: 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 61 75 ntry into the au
fcf0: 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69 6c xiliary. ** fil
fd00: 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 74 e instead of int
fd10: 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 o the main datab
fd20: 61 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 ase file.. */.
fd30: 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 if( !db->init.b
fd40: 75 73 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e usy ){. int n
fd50: 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 ;. Vdbe *v;.
fd60: 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 char *zType;
fd70: 20 20 20 2f 2a 20 22 76 69 65 77 22 20 6f 72 20 /* "view" or
fd80: 22 74 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20 63 "table" */. c
fd90: 68 61 72 20 2a 7a 54 79 70 65 32 3b 20 20 20 2f har *zType2; /
fda0: 2a 20 22 56 49 45 57 22 20 6f 72 20 22 54 41 42 * "VIEW" or "TAB
fdb0: 4c 45 22 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 LE" */. char
fdc0: 2a 7a 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54 65 *zStmt; /* Te
fdd0: 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 xt of the CREATE
fde0: 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 TABLE or CREATE
fdf0: 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74 20 VIEW statement
fe00: 2a 2f 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 */.. v = sqli
fe10: 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
fe20: 65 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 e);. if( NEVE
fe30: 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e R(v==0) ) return
fe40: 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ;.. sqlite3Vd
fe50: 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 beAddOp1(v, OP_C
fe60: 6c 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f lose, 0);.. /
fe70: 2a 20 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69 61 * . ** Initia
fe80: 6c 69 7a 65 20 7a 54 79 70 65 20 66 6f 72 20 74 lize zType for t
fe90: 68 65 20 6e 65 77 20 76 69 65 77 20 6f 72 20 74 he new view or t
fea0: 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 able.. */.
feb0: 20 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d if( p->pSelect=
fec0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 =0 ){. /* A
fed0: 20 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20 2a regular table *
fee0: 2f 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 /. zType =
fef0: 22 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a "table";. z
ff00: 54 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22 3b Type2 = "TABLE";
ff10: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
ff20: 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 OMIT_VIEW. }e
ff30: 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 lse{. /* A
ff40: 76 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 view */. zT
ff50: 79 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 ype = "view";.
ff60: 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56 49 zType2 = "VI
ff70: 45 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 EW";.#endif.
ff80: 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 }.. /* If thi
ff90: 73 20 69 73 20 61 20 43 52 45 41 54 45 20 54 41 s is a CREATE TA
ffa0: 42 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43 54 BLE xx AS SELECT
ffb0: 20 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68 ..., execute th
ffc0: 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 e SELECT. **
ffd0: 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 statement to pop
ffe0: 75 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74 61 ulate the new ta
fff0: 62 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 ble. The root-pa
10000 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 ge number for th
10010 65 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 e. ** new tab
10020 6c 65 20 69 73 20 69 6e 20 72 65 67 69 73 74 65 le is in registe
10030 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f r pParse->regRoo
10040 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a t.. **. **
10050 20 4f 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54 Once the SELECT
10060 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20 has been coded
10070 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 by sqlite3Select
10080 28 29 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20 (), it is in a.
10090 20 20 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73 ** suitable s
100a0 74 61 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f tate to query fo
100b0 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d r the column nam
100c0 65 73 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20 es and types to
100d0 62 65 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62 be used. ** b
100e0 79 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e y the new table.
100f0 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 . **. ** A
10100 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 77 72 shared-cache wr
10110 69 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 ite-lock is not
10120 72 65 71 75 69 72 65 64 20 74 6f 20 77 72 69 74 required to writ
10130 65 20 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62 e to the new tab
10140 6c 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 le,. ** as a
10150 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74 schema-lock must
10160 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 have already be
10170 65 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63 en obtained to c
10180 72 65 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a reate it. Since.
10190 20 20 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d ** a schema-
101a0 6c 6f 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c lock excludes al
101b0 6c 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 l other database
101c0 20 75 73 65 72 73 2c 20 74 68 65 20 77 72 69 74 users, the writ
101d0 65 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20 e-lock would.
101e0 20 2a 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74 ** be redundant
101f0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
10200 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 pSelect ){.
10210 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 SelectDest des
10220 74 3b 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 t; /* Where t
10230 68 65 20 53 45 4c 45 43 54 20 73 68 6f 75 6c 64 he SELECT should
10240 20 73 74 6f 72 65 20 72 65 73 75 6c 74 73 20 2a store results *
10250 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 59 /. int regY
10260 69 65 6c 64 3b 20 20 20 20 20 20 20 2f 2a 20 52 ield; /* R
10270 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 egister holding
10280 63 6f 2d 72 6f 75 74 69 6e 65 20 65 6e 74 72 79 co-routine entry
10290 2d 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 -point */.
102a0 69 6e 74 20 61 64 64 72 54 6f 70 3b 20 20 20 20 int addrTop;
102b0 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 /* Top of th
102c0 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 2a 2f 0a e co-routine */.
102d0 20 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 int regRec
102e0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 72 ; /* A r
102f0 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 ecord to be inse
10300 72 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 rt into the new
10310 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 table */. i
10320 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 nt regRowid;
10330 20 20 20 2f 2a 20 52 6f 77 69 64 20 6f 66 20 74 /* Rowid of t
10340 68 65 20 6e 65 78 74 20 72 6f 77 20 74 6f 20 69 he next row to i
10350 6e 73 65 72 74 20 2a 2f 0a 20 20 20 20 20 20 69 nsert */. i
10360 6e 74 20 61 64 64 72 49 6e 73 4c 6f 6f 70 3b 20 nt addrInsLoop;
10370 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 /* Top of the
10380 20 6c 6f 6f 70 20 66 6f 72 20 69 6e 73 65 72 74 loop for insert
10390 69 6e 67 20 72 6f 77 73 20 2a 2f 0a 20 20 20 20 ing rows */.
103a0 20 20 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 Table *pSelTab
103b0 3b 20 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 ; /* A table
103c0 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
103d0 74 68 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c the SELECT resul
103e0 74 73 20 2a 2f 0a 0a 20 20 20 20 20 20 72 65 67 ts */.. reg
103f0 59 69 65 6c 64 20 3d 20 2b 2b 70 50 61 72 73 65 Yield = ++pParse
10400 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 72 65 ->nMem;. re
10410 67 52 65 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d gRec = ++pParse-
10420 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 72 65 67 >nMem;. reg
10430 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 Rowid = ++pParse
10440 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 ->nMem;. as
10450 73 65 72 74 28 70 50 61 72 73 65 2d 3e 6e 54 61 sert(pParse->nTa
10460 62 3d 3d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c b==1);. sql
10470 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 ite3MayAbort(pPa
10480 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 rse);. sqli
10490 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
104a0 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 31 OP_OpenWrite, 1
104b0 2c 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f , pParse->regRoo
104c0 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 t, iDb);. s
104d0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
104e0 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 50 32 49 P5(v, OPFLAG_P2I
104f0 53 52 45 47 29 3b 0a 20 20 20 20 20 20 70 50 61 SREG);. pPa
10500 72 73 65 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 rse->nTab = 2;.
10510 20 20 20 20 20 61 64 64 72 54 6f 70 20 3d 20 73 addrTop = s
10520 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
10530 74 41 64 64 72 28 76 29 20 2b 20 31 3b 0a 20 20 tAddr(v) + 1;.
10540 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
10550 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 69 74 ddOp3(v, OP_Init
10560 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 59 69 Coroutine, regYi
10570 65 6c 64 2c 20 30 2c 20 61 64 64 72 54 6f 70 29 eld, 0, addrTop)
10580 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 ;. if( pPar
10590 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 se->nErr ) retur
105a0 6e 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 n;. pSelTab
105b0 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 = sqlite3Result
105c0 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 SetOfSelect(pPar
105d0 73 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 se, pSelect);.
105e0 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 3d if( pSelTab=
105f0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 =0 ) return;.
10600 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 43 assert( p->aC
10610 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 ol==0 );. p
10620 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 ->nCol = pSelTab
10630 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 2d ->nCol;. p-
10640 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d >aCol = pSelTab-
10650 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 70 53 65 >aCol;. pSe
10660 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a lTab->nCol = 0;.
10670 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 61 pSelTab->a
10680 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 Col = 0;. s
10690 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c qlite3DeleteTabl
106a0 65 28 64 62 2c 20 70 53 65 6c 54 61 62 29 3b 0a e(db, pSelTab);.
106b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c sqlite3Sel
106c0 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 ectDestInit(&des
106d0 74 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 t, SRT_Coroutine
106e0 2c 20 72 65 67 59 69 65 6c 64 29 3b 0a 20 20 20 , regYield);.
106f0 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
10700 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 (pParse, pSelect
10710 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 , &dest);.
10720 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 if( pParse->nErr
10730 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 ) return;.
10740 20 73 71 6c 69 74 65 33 56 64 62 65 45 6e 64 43 sqlite3VdbeEndC
10750 6f 72 6f 75 74 69 6e 65 28 76 2c 20 72 65 67 59 oroutine(v, regY
10760 69 65 6c 64 29 3b 0a 20 20 20 20 20 20 73 71 6c ield);. sql
10770 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
10780 28 76 2c 20 61 64 64 72 54 6f 70 20 2d 20 31 29 (v, addrTop - 1)
10790 3b 0a 20 20 20 20 20 20 61 64 64 72 49 6e 73 4c ;. addrInsL
107a0 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 oop = sqlite3Vdb
107b0 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 eAddOp1(v, OP_Yi
107c0 65 6c 64 2c 20 64 65 73 74 2e 69 53 44 50 61 72 eld, dest.iSDPar
107d0 6d 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f m);. VdbeCo
107e0 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 20 verage(v);.
107f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
10800 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 p3(v, OP_MakeRec
10810 6f 72 64 2c 20 64 65 73 74 2e 69 53 64 73 74 2c ord, dest.iSdst,
10820 20 64 65 73 74 2e 6e 53 64 73 74 2c 20 72 65 67 dest.nSdst, reg
10830 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 Rec);. sqli
10840 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 te3TableAffinity
10850 28 76 2c 20 70 2c 20 30 29 3b 0a 20 20 20 20 20 (v, p, 0);.
10860 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
10870 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 p2(v, OP_NewRowi
10880 64 2c 20 31 2c 20 72 65 67 52 6f 77 69 64 29 3b d, 1, regRowid);
10890 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
108a0 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 beAddOp3(v, OP_I
108b0 6e 73 65 72 74 2c 20 31 2c 20 72 65 67 52 65 63 nsert, 1, regRec
108c0 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 , regRowid);.
108d0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 47 6f sqlite3VdbeGo
108e0 74 6f 28 76 2c 20 61 64 64 72 49 6e 73 4c 6f 6f to(v, addrInsLoo
108f0 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 p);. sqlite
10900 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
10910 20 61 64 64 72 49 6e 73 4c 6f 6f 70 29 3b 0a 20 addrInsLoop);.
10920 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
10930 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f AddOp1(v, OP_Clo
10940 73 65 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 se, 1);. }..
10950 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 /* Compute th
10960 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 e complete text
10970 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 73 74 of the CREATE st
10980 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 69 atement */. i
10990 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 f( pSelect ){.
109a0 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 72 65 61 zStmt = crea
109b0 74 65 54 61 62 6c 65 53 74 6d 74 28 64 62 2c 20 teTableStmt(db,
109c0 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 p);. }else{.
109d0 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 Token *pEnd
109e0 32 20 3d 20 74 61 62 4f 70 74 73 20 3f 20 26 70 2 = tabOpts ? &p
109f0 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 Parse->sLastToke
10a00 6e 20 3a 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 n : pEnd;.
10a10 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 32 2d n = (int)(pEnd2-
10a20 3e 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 >z - pParse->sNa
10a30 6d 65 54 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 20 20 meToken.z);.
10a40 20 20 69 66 28 20 70 45 6e 64 32 2d 3e 7a 5b 30 if( pEnd2->z[0
10a50 5d 21 3d 27 3b 27 20 29 20 6e 20 2b 3d 20 70 45 ]!=';' ) n += pE
10a60 6e 64 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 7a 53 nd2->n;. zS
10a70 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 tmt = sqlite3MPr
10a80 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 intf(db, .
10a90 20 20 20 20 22 43 52 45 41 54 45 20 25 73 20 25 "CREATE %s %
10aa0 2e 2a 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c .*s", zType2, n,
10ab0 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f pParse->sNameTo
10ac0 6b 65 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 ken.z. );.
10ad0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 }.. /* A s
10ae0 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f lot for the reco
10af0 72 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 rd has already b
10b00 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e een allocated in
10b10 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c the . ** SQL
10b20 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 ITE_MASTER table
10b30 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 . We just need
10b40 74 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 to update that s
10b50 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 lot with all.
10b60 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 ** the informat
10b70 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 ion we've collec
10b80 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
10b90 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 sqlite3NestedPar
10ba0 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 se(pParse,.
10bb0 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 22 "UPDATE %Q.%s "
10bc0 0a 20 20 20 20 20 20 20 20 20 22 53 45 54 20 74 . "SET t
10bd0 79 70 65 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25 ype='%s', name=%
10be0 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 Q, tbl_name=%Q,
10bf0 72 6f 6f 74 70 61 67 65 3d 23 25 64 2c 20 73 71 rootpage=#%d, sq
10c00 6c 3d 25 51 20 22 0a 20 20 20 20 20 20 20 22 57 l=%Q ". "W
10c10 48 45 52 45 20 72 6f 77 69 64 3d 23 25 64 22 2c HERE rowid=#%d",
10c20 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 . db->aDb[i
10c30 44 62 5d 2e 7a 44 62 53 4e 61 6d 65 2c 20 4d 41 Db].zDbSName, MA
10c40 53 54 45 52 5f 4e 41 4d 45 2c 0a 20 20 20 20 20 STER_NAME,.
10c50 20 7a 54 79 70 65 2c 0a 20 20 20 20 20 20 70 2d zType,. p-
10c60 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 2d >zName,. p-
10c70 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 50 >zName,. pP
10c80 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a 20 arse->regRoot,.
10c90 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 zStmt,.
10ca0 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 pParse->regRow
10cb0 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 id. );. sq
10cc0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
10cd0 7a 53 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 zStmt);. sqli
10ce0 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 te3ChangeCookie(
10cf0 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 23 pParse, iDb);..#
10d00 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
10d10 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 IT_AUTOINCREMENT
10d20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f . /* Check to
10d30 20 73 65 65 20 69 66 20 77 65 20 6e 65 65 64 20 see if we need
10d40 74 6f 20 63 72 65 61 74 65 20 61 6e 20 73 71 6c to create an sql
10d50 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 ite_sequence tab
10d60 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a 20 6b 65 le for. ** ke
10d70 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 61 eping track of a
10d80 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6b 65 79 utoincrement key
10d90 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
10da0 28 20 28 70 2d 3e 74 61 62 46 6c 61 67 73 20 26 ( (p->tabFlags &
10db0 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e TF_Autoincremen
10dc0 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 44 t)!=0 ){. D
10dd0 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 b *pDb = &db->aD
10de0 62 5b 69 44 62 5d 3b 0a 20 20 20 20 20 20 61 73 b[iDb];. as
10df0 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 sert( sqlite3Sch
10e00 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c emaMutexHeld(db,
10e10 20 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 20 20 iDb, 0) );.
10e20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 if( pDb->pSche
10e30 6d 61 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 ma->pSeqTab==0 )
10e40 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
10e50 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 3NestedParse(pPa
10e60 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 rse,. "
10e70 43 52 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e CREATE TABLE %Q.
10e80 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 28 sqlite_sequence(
10e90 6e 61 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 name,seq)",.
10ea0 20 20 20 20 20 20 70 44 62 2d 3e 7a 44 62 53 4e pDb->zDbSN
10eb0 61 6d 65 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 ame. );.
10ec0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e }. }.#en
10ed0 64 69 66 0a 0a 20 20 20 20 2f 2a 20 52 65 70 61 dif.. /* Repa
10ee0 72 73 65 20 65 76 65 72 79 74 68 69 6e 67 20 74 rse everything t
10ef0 6f 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e 74 o update our int
10f00 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 ernal data struc
10f10 74 75 72 65 73 20 2a 2f 0a 20 20 20 20 73 71 6c tures */. sql
10f20 69 74 65 33 56 64 62 65 41 64 64 50 61 72 73 65 ite3VdbeAddParse
10f30 53 63 68 65 6d 61 4f 70 28 76 2c 20 69 44 62 2c SchemaOp(v, iDb,
10f40 0a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 . sqli
10f50 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 te3MPrintf(db, "
10f60 74 62 6c 5f 6e 61 6d 65 3d 27 25 71 27 20 41 4e tbl_name='%q' AN
10f70 44 20 74 79 70 65 21 3d 27 74 72 69 67 67 65 72 D type!='trigger
10f80 27 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 29 3b 0a '", p->zName));.
10f90 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 20 74 }... /* Add t
10fa0 68 65 20 74 61 62 6c 65 20 74 6f 20 74 68 65 20 he table to the
10fb0 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 in-memory repres
10fc0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
10fd0 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 database.. */.
10fe0 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 if( db->init.bu
10ff0 73 79 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 sy ){. Table
11000 2a 70 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d *pOld;. Schem
11010 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e a *pSchema = p->
11020 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 61 73 73 pSchema;. ass
11030 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 ert( sqlite3Sche
11040 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 maMutexHeld(db,
11050 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 20 20 70 iDb, 0) );. p
11060 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 Old = sqlite3Has
11070 68 49 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 hInsert(&pSchema
11080 2d 3e 74 62 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e ->tblHash, p->zN
11090 61 6d 65 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 ame, p);. if(
110a0 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 61 pOld ){. a
110b0 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 20 29 ssert( p==pOld )
110c0 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 ; /* Malloc mus
110d0 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e t have failed in
110e0 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 74 28 side HashInsert(
110f0 29 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 ) */. sqlit
11100 65 33 4f 6f 6d 46 61 75 6c 74 28 64 62 29 3b 0a e3OomFault(db);.
11110 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
11120 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e }. pParse->
11130 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 pNewTable = 0;.
11140 20 20 20 64 62 2d 3e 6d 44 62 46 6c 61 67 73 20 db->mDbFlags
11150 7c 3d 20 44 42 46 4c 41 47 5f 53 63 68 65 6d 61 |= DBFLAG_Schema
11160 43 68 61 6e 67 65 3b 0a 0a 23 69 66 6e 64 65 66 Change;..#ifndef
11170 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 SQLITE_OMIT_ALT
11180 45 52 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 ERTABLE. if(
11190 21 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 !p->pSelect ){.
111a0 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
111b0 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 *zName = (const
111c0 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 73 char *)pParse->s
111d0 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 NameToken.z;.
111e0 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 int nName;.
111f0 20 20 20 20 61 73 73 65 72 74 28 20 21 70 53 65 assert( !pSe
11200 6c 65 63 74 20 26 26 20 70 43 6f 6e 73 20 26 26 lect && pCons &&
11210 20 70 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 69 pEnd );. i
11220 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 f( pCons->z==0 )
11230 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6e 73 20 {. pCons
11240 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a = pEnd;. }.
11250 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20 28 69 nName = (i
11260 6e 74 29 28 28 63 6f 6e 73 74 20 63 68 61 72 20 nt)((const char
11270 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e 61 *)pCons->z - zNa
11280 6d 65 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 me);. p->ad
11290 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 31 33 20 dColOffset = 13
112a0 2b 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 + sqlite3Utf8Cha
112b0 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e 4e 61 6d rLen(zName, nNam
112c0 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
112d0 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 . }.}..#ifndef
112e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
112f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 ./*.** The parse
11300 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 r calls this rou
11310 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f tine in order to
11320 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 56 49 create a new VI
11330 45 57 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 EW.*/.void sqlit
11340 65 33 43 72 65 61 74 65 56 69 65 77 28 0a 20 20 e3CreateView(.
11350 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
11360 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e /* The parsin
11370 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 g context */. T
11380 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20 20 20 oken *pBegin,
11390 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 /* The CREATE
113a0 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e token that begin
113b0 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 s the statement
113c0 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d */. Token *pNam
113d0 65 31 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 e1, /* The t
113e0 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 oken that holds
113f0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
11400 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 view */. Token
11410 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 *pName2, /*
11420 54 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 The token that h
11430 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 olds the name of
11440 20 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 45 the view */. E
11450 78 70 72 4c 69 73 74 20 2a 70 43 4e 61 6d 65 73 xprList *pCNames
11460 2c 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c 69 , /* Optional li
11470 73 74 20 6f 66 20 76 69 65 77 20 63 6f 6c 75 6d st of view colum
11480 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 53 65 6c n names */. Sel
11490 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 ect *pSelect,
114a0 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 74 /* A SELECT stat
114b0 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20 ement that will
114c0 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 76 become the new v
114d0 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 iew */. int isT
114e0 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 emp, /* T
114f0 52 55 45 20 66 6f 72 20 61 20 54 45 4d 50 4f 52 RUE for a TEMPOR
11500 41 52 59 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e ARY view */. in
11510 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 20 t noErr
11520 20 2f 2a 20 53 75 70 70 72 65 73 73 20 65 72 72 /* Suppress err
11530 6f 72 20 6d 65 73 73 61 67 65 73 20 69 66 20 56 or messages if V
11540 49 45 57 20 61 6c 72 65 61 64 79 20 65 78 69 73 IEW already exis
11550 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 ts */.){. Table
11560 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 *p;. int n;.
11570 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 const char *z;.
11580 20 54 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 Token sEnd;. D
11590 62 46 69 78 65 72 20 73 46 69 78 3b 0a 20 20 54 bFixer sFix;. T
115a0 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30 3b oken *pName = 0;
115b0 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73 71 . int iDb;. sq
115c0 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
115d0 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 se->db;.. if( p
115e0 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30 20 29 7b Parse->nVar>0 ){
115f0 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
11600 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 61 rMsg(pParse, "pa
11610 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 6f 74 rameters are not
11620 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76 69 65 77 allowed in view
11630 73 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 63 72 s");. goto cr
11640 65 61 74 65 5f 76 69 65 77 5f 66 61 69 6c 3b 0a eate_view_fail;.
11650 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 61 }. sqlite3Sta
11660 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 rtTable(pParse,
11670 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 pName1, pName2,
11680 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e 6f isTemp, 1, 0, no
11690 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 61 72 Err);. p = pPar
116a0 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 se->pNewTable;.
116b0 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61 if( p==0 || pPa
116c0 72 73 65 2d 3e 6e 45 72 72 20 29 20 67 6f 74 6f rse->nErr ) goto
116d0 20 63 72 65 61 74 65 5f 76 69 65 77 5f 66 61 69 create_view_fai
116e0 6c 3b 0a 20 20 73 71 6c 69 74 65 33 54 77 6f 50 l;. sqlite3TwoP
116f0 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 artName(pParse,
11700 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 pName1, pName2,
11710 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20 3d &pName);. iDb =
11720 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
11730 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 Index(db, p->pSc
11740 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 hema);. sqlite3
11750 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 FixInit(&sFix, p
11760 50 61 72 73 65 2c 20 69 44 62 2c 20 22 76 69 65 Parse, iDb, "vie
11770 77 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 w", pName);. if
11780 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 ( sqlite3FixSele
11790 63 74 28 26 73 46 69 78 2c 20 70 53 65 6c 65 63 ct(&sFix, pSelec
117a0 74 29 20 29 20 67 6f 74 6f 20 63 72 65 61 74 65 t) ) goto create
117b0 5f 76 69 65 77 5f 66 61 69 6c 3b 0a 0a 20 20 2f _view_fail;.. /
117c0 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 * Make a copy of
117d0 20 74 68 65 20 65 6e 74 69 72 65 20 53 45 4c 45 the entire SELE
117e0 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 CT statement tha
117f0 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 76 69 t defines the vi
11800 65 77 2e 0a 20 20 2a 2a 20 54 68 69 73 20 77 69 ew.. ** This wi
11810 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20 74 68 65 ll force all the
11820 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 Expr.token.z va
11830 6c 75 65 73 20 74 6f 20 62 65 20 64 79 6e 61 6d lues to be dynam
11840 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f ically. ** allo
11850 63 61 74 65 64 20 72 61 74 68 65 72 20 74 68 61 cated rather tha
11860 6e 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69 n point to the i
11870 6e 70 75 74 20 73 74 72 69 6e 67 20 2d 20 77 68 nput string - wh
11880 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 ich means that.
11890 20 2a 2a 20 74 68 65 79 20 77 69 6c 6c 20 70 65 ** they will pe
118a0 72 73 69 73 74 20 61 66 74 65 72 20 74 68 65 20 rsist after the
118b0 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f current sqlite3_
118c0 65 78 65 63 28 29 20 63 61 6c 6c 20 72 65 74 75 exec() call retu
118d0 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 rns.. */. if(
118e0 49 4e 5f 52 45 4e 41 4d 45 5f 4f 42 4a 45 43 54 IN_RENAME_OBJECT
118f0 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 65 6c 65 ){. p->pSele
11900 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 ct = pSelect;.
11910 20 20 70 53 65 6c 65 63 74 20 3d 20 30 3b 0a 20 pSelect = 0;.
11920 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 70 }else{. p->p
11930 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 Select = sqlite3
11940 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 SelectDup(db, pS
11950 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50 5f 52 elect, EXPRDUP_R
11960 45 44 55 43 45 29 3b 0a 20 20 7d 0a 20 20 70 2d EDUCE);. }. p-
11970 3e 70 43 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 >pCheck = sqlite
11980 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 3ExprListDup(db,
11990 20 70 43 4e 61 6d 65 73 2c 20 45 58 50 52 44 55 pCNames, EXPRDU
119a0 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 69 66 28 P_REDUCE);. if(
119b0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
119c0 64 20 29 20 67 6f 74 6f 20 63 72 65 61 74 65 5f d ) goto create_
119d0 76 69 65 77 5f 66 61 69 6c 3b 0a 0a 20 20 2f 2a view_fail;.. /*
119e0 20 4c 6f 63 61 74 65 20 74 68 65 20 65 6e 64 20 Locate the end
119f0 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 of the CREATE VI
11a00 45 57 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4d EW statement. M
11a10 61 6b 65 20 73 45 6e 64 20 70 6f 69 6e 74 20 74 ake sEnd point t
11a20 6f 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 2e 0a o. ** the end..
11a30 20 20 2a 2f 0a 20 20 73 45 6e 64 20 3d 20 70 50 */. sEnd = pP
11a40 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e arse->sLastToken
11a50 3b 0a 20 20 61 73 73 65 72 74 28 20 73 45 6e 64 ;. assert( sEnd
11a60 2e 7a 5b 30 5d 21 3d 30 20 7c 7c 20 73 45 6e 64 .z[0]!=0 || sEnd
11a70 2e 6e 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 73 .n==0 );. if( s
11a80 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b End.z[0]!=';' ){
11a90 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73 . sEnd.z += s
11aa0 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e End.n;. }. sEn
11ab0 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 28 d.n = 0;. n = (
11ac0 69 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20 70 42 int)(sEnd.z - pB
11ad0 65 67 69 6e 2d 3e 7a 29 3b 0a 20 20 61 73 73 65 egin->z);. asse
11ae0 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 7a 20 3d rt( n>0 );. z =
11af0 20 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 pBegin->z;. wh
11b00 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 ile( sqlite3Issp
11b10 61 63 65 28 7a 5b 6e 2d 31 5d 29 20 29 7b 20 6e ace(z[n-1]) ){ n
11b20 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d --; }. sEnd.z =
11b30 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 &z[n-1];. sEnd
11b40 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 .n = 1;.. /* Us
11b50 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c e sqlite3EndTabl
11b60 65 28 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 e() to add the v
11b70 69 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 iew to the SQLIT
11b80 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a E_MASTER table *
11b90 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 /. sqlite3EndTa
11ba0 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 ble(pParse, 0, &
11bb0 73 45 6e 64 2c 20 30 2c 20 30 29 3b 0a 0a 63 72 sEnd, 0, 0);..cr
11bc0 65 61 74 65 5f 76 69 65 77 5f 66 61 69 6c 3a 0a eate_view_fail:.
11bd0 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
11be0 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 elete(db, pSelec
11bf0 74 29 3b 0a 20 20 69 66 28 20 49 4e 5f 52 45 4e t);. if( IN_REN
11c00 41 4d 45 5f 4f 42 4a 45 43 54 20 29 7b 0a 20 20 AME_OBJECT ){.
11c10 20 20 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 45 sqlite3RenameE
11c20 78 70 72 6c 69 73 74 55 6e 6d 61 70 28 70 50 61 xprlistUnmap(pPa
11c30 72 73 65 2c 20 70 43 4e 61 6d 65 73 29 3b 0a 20 rse, pCNames);.
11c40 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 }. sqlite3Expr
11c50 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
11c60 43 4e 61 6d 65 73 29 3b 0a 20 20 72 65 74 75 72 CNames);. retur
11c70 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 n;.}.#endif /* S
11c80 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 QLITE_OMIT_VIEW
11c90 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 */..#if !defined
11ca0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 (SQLITE_OMIT_VIE
11cb0 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 W) || !defined(S
11cc0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
11cd0 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 ALTABLE)./*.** T
11ce0 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 he Table structu
11cf0 72 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 61 re pTable is rea
11d00 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c lly a VIEW. Fil
11d10 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f l in the names o
11d20 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73 f.** the columns
11d30 20 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e 20 of the view in
11d40 74 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 63 the pTable struc
11d50 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68 ture. Return th
11d60 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65 e number.** of e
11d70 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72 rrors. If an er
11d80 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76 ror is seen leav
11d90 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 e an error messa
11da0 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 ge in pParse->zE
11db0 72 72 4d 73 67 2e 0a 2a 2f 0a 69 6e 74 20 73 71 rrMsg..*/.int sq
11dc0 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 lite3ViewGetColu
11dd0 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 mnNames(Parse *p
11de0 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 Parse, Table *pT
11df0 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a able){. Table *
11e00 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 pSelTab; /* A
11e10 66 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 fake table from
11e20 77 68 69 63 68 20 77 65 20 67 65 74 20 74 68 65 which we get the
11e30 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
11e40 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 Select *pSel;
11e50 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 /* Copy of th
11e60 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d e SELECT that im
11e70 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 plements the vie
11e80 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 w */. int nErr
11e90 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 = 0; /* Numb
11ea0 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 er of errors enc
11eb0 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e ountered */. in
11ec0 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 t n;
11ed0 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 /* Temporarily h
11ee0 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 olds the number
11ef0 6f 66 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 of cursors assig
11f00 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ned */. sqlite3
11f10 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
11f20 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 b; /* Database
11f30 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d connection for m
11f40 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a alloc errors */.
11f50 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
11f60 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
11f70 0a 20 20 69 6e 74 20 72 63 3b 0a 23 65 6e 64 69 . int rc;.#endi
11f80 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
11f90 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
11fa0 49 4f 4e 0a 20 20 73 71 6c 69 74 65 33 5f 78 61 ION. sqlite3_xa
11fb0 75 74 68 20 78 41 75 74 68 3b 20 20 20 20 20 20 uth xAuth;
11fc0 20 2f 2a 20 53 61 76 65 64 20 78 41 75 74 68 20 /* Saved xAuth
11fd0 70 6f 69 6e 74 65 72 20 2a 2f 0a 23 65 6e 64 69 pointer */.#endi
11fe0 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 f.. assert( pTa
11ff0 62 6c 65 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 ble );..#ifndef
12000 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
12010 55 41 4c 54 41 42 4c 45 0a 20 20 64 62 2d 3e 6e UALTABLE. db->n
12020 53 63 68 65 6d 61 4c 6f 63 6b 2b 2b 3b 0a 20 20 SchemaLock++;.
12030 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 rc = sqlite3Vtab
12040 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50 61 72 CallConnect(pPar
12050 73 65 2c 20 70 54 61 62 6c 65 29 3b 0a 20 20 64 se, pTable);. d
12060 62 2d 3e 6e 53 63 68 65 6d 61 4c 6f 63 6b 2d 2d b->nSchemaLock--
12070 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
12080 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
12090 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 if( IsVirtual(
120a0 70 54 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e pTable) ) return
120b0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 0;.#endif..#ifn
120c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
120d0 56 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 VIEW. /* A posi
120e0 74 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 tive nCol means
120f0 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 the columns name
12100 73 20 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 s for this view
12110 61 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 are. ** already
12120 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 known.. */. i
12130 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e f( pTable->nCol>
12140 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 0 ) return 0;..
12150 20 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e /* A negative n
12160 43 6f 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c Col is a special
12170 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 marker meaning
12180 74 68 61 74 20 77 65 20 61 72 65 20 63 75 72 72 that we are curr
12190 65 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e ently. ** tryin
121a0 67 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 g to compute the
121b0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 column names.
121c0 49 66 20 77 65 20 65 6e 74 65 72 20 74 68 69 73 If we enter this
121d0 20 72 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 routine with.
121e0 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43 ** a negative nC
121f0 6f 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f ol, it means two
12200 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66 or more views f
12210 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 orm a loop, like
12220 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a this:. **. **
12230 20 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 CREATE VIEW
12240 20 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a one AS SELECT *
12250 20 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 FROM two;. **
12260 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 CREATE VIEW
12270 74 77 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20 two AS SELECT *
12280 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 FROM one;. **.
12290 20 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 ** Actually, th
122a0 65 20 65 72 72 6f 72 20 61 62 6f 76 65 20 69 73 e error above is
122b0 20 6e 6f 77 20 63 61 75 67 68 74 20 70 72 69 6f now caught prio
122c0 72 20 74 6f 20 72 65 61 63 68 69 6e 67 20 74 68 r to reaching th
122d0 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2a 20 42 is point.. ** B
122e0 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ut the following
122f0 20 74 65 73 74 20 69 73 20 73 74 69 6c 6c 20 69 test is still i
12300 6d 70 6f 72 74 61 6e 74 20 61 73 20 69 74 20 64 mportant as it d
12310 6f 65 73 20 63 6f 6d 65 20 75 70 0a 20 20 2a 2a oes come up. **
12320 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e in the followin
12330 67 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 g:. ** . **
12340 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6d CREATE TABLE m
12350 61 69 6e 2e 65 78 31 28 61 29 3b 0a 20 20 2a 2a ain.ex1(a);. **
12360 20 20 20 20 20 43 52 45 41 54 45 20 54 45 4d 50 CREATE TEMP
12370 20 56 49 45 57 20 65 78 31 20 41 53 20 53 45 4c VIEW ex1 AS SEL
12380 45 43 54 20 61 20 46 52 4f 4d 20 65 78 31 3b 0a ECT a FROM ex1;.
12390 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 ** SELECT
123a0 2a 20 46 52 4f 4d 20 74 65 6d 70 2e 65 78 31 3b * FROM temp.ex1;
123b0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 . */. if( pTab
123c0 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 le->nCol<0 ){.
123d0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
123e0 67 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 g(pParse, "view
123f0 25 73 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79 %s is circularly
12400 20 64 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c defined", pTabl
12410 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 e->zName);. r
12420 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 eturn 1;. }. a
12430 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e ssert( pTable->n
12440 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 Col>=0 );.. /*
12450 49 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 If we get this f
12460 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20 ar, it means we
12470 6e 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 need to compute
12480 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e the table names.
12490 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 . ** Note that
124a0 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 the call to sqli
124b0 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 te3ResultSetOfSe
124c0 6c 65 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61 lect() will expa
124d0 6e 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 nd any. ** "*"
124e0 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 elements in the
124f0 72 65 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74 results set of t
12500 68 65 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c he view and will
12510 20 61 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a assign cursors.
12520 20 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d ** to the elem
12530 65 6e 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d ents of the FROM
12540 20 63 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65 clause. But we
12550 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 do not want the
12560 73 65 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 se changes. **
12570 74 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e to be permanent.
12580 20 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 So the computa
12590 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 tion is done on
125a0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 a copy of the SE
125b0 4c 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d LECT. ** statem
125c0 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 ent that defines
125d0 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a the view.. */.
125e0 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 assert( pTable
125f0 2d 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 ->pSelect );. p
12600 53 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c Sel = sqlite3Sel
12610 65 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c ectDup(db, pTabl
12620 65 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a e->pSelect, 0);.
12630 20 20 69 66 28 20 70 53 65 6c 20 29 7b 0a 23 69 if( pSel ){.#i
12640 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
12650 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 T_ALTERTABLE.
12660 20 75 38 20 65 50 61 72 73 65 4d 6f 64 65 20 3d u8 eParseMode =
12670 20 70 50 61 72 73 65 2d 3e 65 50 61 72 73 65 4d pParse->eParseM
12680 6f 64 65 3b 0a 20 20 20 20 70 50 61 72 73 65 2d ode;. pParse-
12690 3e 65 50 61 72 73 65 4d 6f 64 65 20 3d 20 50 41 >eParseMode = PA
126a0 52 53 45 5f 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3b RSE_MODE_NORMAL;
126b0 0a 23 65 6e 64 69 66 0a 20 20 20 20 6e 20 3d 20 .#endif. n =
126c0 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 pParse->nTab;.
126d0 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 sqlite3SrcList
126e0 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 AssignCursors(pP
126f0 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70 53 72 63 arse, pSel->pSrc
12700 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e );. pTable->n
12710 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 64 62 Col = -1;. db
12720 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 44 69 73 ->lookaside.bDis
12730 61 62 6c 65 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 able++;.#ifndef
12740 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
12750 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 78 41 ORIZATION. xA
12760 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 68 3b uth = db->xAuth;
12770 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d . db->xAuth =
12780 20 30 3b 0a 20 20 20 20 70 53 65 6c 54 61 62 20 0;. pSelTab
12790 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 = sqlite3ResultS
127a0 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 etOfSelect(pPars
127b0 65 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 64 62 e, pSel);. db
127c0 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b ->xAuth = xAuth;
127d0 0a 23 65 6c 73 65 0a 20 20 20 20 70 53 65 6c 54 .#else. pSelT
127e0 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 ab = sqlite3Resu
127f0 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 ltSetOfSelect(pP
12800 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 23 65 6e arse, pSel);.#en
12810 64 69 66 0a 20 20 20 20 70 50 61 72 73 65 2d 3e dif. pParse->
12820 6e 54 61 62 20 3d 20 6e 3b 0a 20 20 20 20 69 66 nTab = n;. if
12830 28 20 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b ( pTable->pCheck
12840 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 52 45 ){. /* CRE
12850 41 54 45 20 56 49 45 57 20 6e 61 6d 65 28 61 72 ATE VIEW name(ar
12860 67 6c 69 73 74 29 20 41 53 20 2e 2e 2e 0a 20 20 glist) AS ....
12870 20 20 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65 73 ** The names
12880 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 of the columns
12890 69 6e 20 74 68 65 20 74 61 62 6c 65 20 61 72 65 in the table are
128a0 20 74 61 6b 65 6e 20 66 72 6f 6d 0a 20 20 20 20 taken from.
128b0 20 20 2a 2a 20 61 72 67 6c 69 73 74 20 77 68 69 ** arglist whi
128c0 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ch is stored in
128d0 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 2e 20 pTable->pCheck.
128e0 20 54 68 65 20 70 43 68 65 63 6b 20 66 69 65 6c The pCheck fiel
128f0 64 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 72 6d 61 d. ** norma
12900 6c 6c 79 20 68 6f 6c 64 73 20 43 48 45 43 4b 20 lly holds CHECK
12910 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 61 constraints on a
12920 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65 n ordinary table
12930 2c 20 62 75 74 20 66 6f 72 0a 20 20 20 20 20 20 , but for.
12940 2a 2a 20 61 20 56 49 45 57 20 69 74 20 68 6f 6c ** a VIEW it hol
12950 64 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 63 ds the list of c
12960 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 0a 20 20 20 olumn names..
12970 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 */. sqli
12980 74 65 33 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 te3ColumnsFromEx
12990 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 prList(pParse, p
129a0 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 2c 20 0a Table->pCheck, .
129b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
129c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
129d0 20 26 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 2c 20 &pTable->nCol,
129e0 26 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a &pTable->aCol);.
129f0 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 if( db->ma
12a00 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 0a 20 llocFailed==0 .
12a10 20 20 20 20 20 20 26 26 20 70 50 61 72 73 65 2d && pParse-
12a20 3e 6e 45 72 72 3d 3d 30 0a 20 20 20 20 20 20 20 >nErr==0.
12a30 26 26 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3d && pTable->nCol=
12a40 3d 70 53 65 6c 2d 3e 70 45 4c 69 73 74 2d 3e 6e =pSel->pEList->n
12a50 45 78 70 72 0a 20 20 20 20 20 20 29 7b 0a 20 20 Expr. ){.
12a60 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c sqlite3Sel
12a70 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 ectAddColumnType
12a80 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 AndCollation(pPa
12a90 72 73 65 2c 20 70 54 61 62 6c 65 2c 20 70 53 65 rse, pTable, pSe
12aa0 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 l);. }.
12ab0 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 54 61 }else if( pSelTa
12ac0 62 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 52 b ){. /* CR
12ad0 45 41 54 45 20 56 49 45 57 20 6e 61 6d 65 20 41 EATE VIEW name A
12ae0 53 2e 2e 2e 20 20 77 69 74 68 6f 75 74 20 61 6e S... without an
12af0 20 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 argument list.
12b00 20 43 6f 6e 73 74 72 75 63 74 0a 20 20 20 20 20 Construct.
12b10 20 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e ** the column n
12b20 61 6d 65 73 20 66 72 6f 6d 20 74 68 65 20 53 45 ames from the SE
12b30 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 LECT statement t
12b40 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 hat defines the
12b50 76 69 65 77 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 view.. */.
12b60 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 assert( pTa
12b70 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a ble->aCol==0 );.
12b80 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 pTable->nC
12b90 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 6e 43 ol = pSelTab->nC
12ba0 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 ol;. pTable
12bb0 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 ->aCol = pSelTab
12bc0 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 70 53 ->aCol;. pS
12bd0 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b elTab->nCol = 0;
12be0 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e . pSelTab->
12bf0 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 aCol = 0;.
12c00 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 assert( sqlite3S
12c10 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 chemaMutexHeld(d
12c20 62 2c 20 30 2c 20 70 54 61 62 6c 65 2d 3e 70 53 b, 0, pTable->pS
12c30 63 68 65 6d 61 29 20 29 3b 0a 20 20 20 20 7d 65 chema) );. }e
12c40 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62 6c lse{. pTabl
12c50 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 e->nCol = 0;.
12c60 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 7d nErr++;. }
12c70 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 . sqlite3Dele
12c80 74 65 54 61 62 6c 65 28 64 62 2c 20 70 53 65 6c teTable(db, pSel
12c90 54 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Tab);. sqlite
12ca0 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 3SelectDelete(db
12cb0 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 64 62 2d , pSel);. db-
12cc0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 44 69 73 61 >lookaside.bDisa
12cd0 62 6c 65 2d 2d 3b 0a 23 69 66 6e 64 65 66 20 53 ble--;.#ifndef S
12ce0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 QLITE_OMIT_ALTER
12cf0 54 41 42 4c 45 0a 20 20 20 20 70 50 61 72 73 65 TABLE. pParse
12d00 2d 3e 65 50 61 72 73 65 4d 6f 64 65 20 3d 20 65 ->eParseMode = e
12d10 50 61 72 73 65 4d 6f 64 65 3b 0a 23 65 6e 64 69 ParseMode;.#endi
12d20 66 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 f. } else {.
12d30 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 70 nErr++;. }. p
12d40 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e Table->pSchema->
12d50 73 63 68 65 6d 61 46 6c 61 67 73 20 7c 3d 20 44 schemaFlags |= D
12d60 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0a B_UnresetViews;.
12d70 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
12d80 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 Failed ){. sq
12d90 6c 69 74 65 33 44 65 6c 65 74 65 43 6f 6c 75 6d lite3DeleteColum
12da0 6e 4e 61 6d 65 73 28 64 62 2c 20 70 54 61 62 6c nNames(db, pTabl
12db0 65 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e e);. pTable->
12dc0 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 70 54 aCol = 0;. pT
12dd0 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a able->nCol = 0;.
12de0 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.#endif /* SQ
12df0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a LITE_OMIT_VIEW *
12e00 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b /. return nErr;
12e10 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 .}.#endif /* !
12e20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
12e30 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 MIT_VIEW) || !de
12e40 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
12e50 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20 T_VIRTUALTABLE)
12e60 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
12e70 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a TE_OMIT_VIEW./*.
12e80 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6c ** Clear the col
12e90 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 65 umn names from e
12ea0 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61 74 very VIEW in dat
12eb0 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73 74 abase idx..*/.st
12ec0 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
12ed0 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71 6c ViewResetAll(sql
12ee0 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 64 ite3 *db, int id
12ef0 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a x){. HashElem *
12f00 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c i;. assert( sql
12f10 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 ite3SchemaMutexH
12f20 65 6c 64 28 64 62 2c 20 69 64 78 2c 20 30 29 20 eld(db, idx, 0)
12f30 29 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73 50 );. if( !DbHasP
12f40 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78 2c roperty(db, idx,
12f50 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 DB_UnresetViews
12f60 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f ) ) return;. fo
12f70 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 r(i=sqliteHashFi
12f80 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64 78 rst(&db->aDb[idx
12f90 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 ].pSchema->tblHa
12fa0 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65 48 sh); i;i=sqliteH
12fb0 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 ashNext(i)){.
12fc0 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 Table *pTab = s
12fd0 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29 qliteHashData(i)
12fe0 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e ;. if( pTab->
12ff0 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 pSelect ){.
13000 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 43 6f sqlite3DeleteCo
13010 6c 75 6d 6e 4e 61 6d 65 73 28 64 62 2c 20 70 54 lumnNames(db, pT
13020 61 62 29 3b 0a 20 20 20 20 20 20 70 54 61 62 2d ab);. pTab-
13030 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 >aCol = 0;.
13040 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b pTab->nCol = 0;
13050 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 . }. }. DbC
13060 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c learProperty(db,
13070 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 idx, DB_Unreset
13080 56 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a Views);.}.#else.
13090 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 # define sqliteV
130a0 69 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 iewResetAll(A,B)
130b0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
130c0 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a E_OMIT_VIEW */..
130d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
130e0 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ion is called by
130f0 20 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a the VDBE to adj
13100 75 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ust the internal
13110 20 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 schema.** used
13120 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 by SQLite when t
13130 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d he btree layer m
13140 6f 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f oves a table roo
13150 74 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 t page. The.** r
13160 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 oot-page of a ta
13170 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 ble or index in
13180 64 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73 database iDb has
13190 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 changed from iF
131a0 72 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a rom.** to iTo..*
131b0 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 *.** Ticket #172
131c0 38 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 8: The symbol t
131d0 61 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c able might still
131e0 20 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 contain informa
131f0 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 tion.** on table
13200 73 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 s and/or indices
13210 20 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72 that are the pr
13220 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 ocess of being d
13230 65 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f eleted..** If yo
13240 75 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f u are unlucky, o
13250 6e 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 ne of those dele
13260 74 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 ted indices or t
13270 61 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 ables might.** h
13280 61 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f ave the same roo
13290 74 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 tpage number as
132a0 74 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f the real table o
132b0 72 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a r index that is.
132c0 2a 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 ** being moved.
132d0 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 So we cannot st
132e0 6f 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 op searching aft
132f0 65 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 er the first mat
13300 63 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 ch .** because t
13310 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d he first match m
13320 69 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 ight be for one
13330 6f 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 of the deleted i
13340 6e 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 ndices.** or tab
13350 6c 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 les and not the
13360 74 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 table/index that
13370 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 is actually bei
13380 6e 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 ng moved..** We
13390 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f must continue lo
133a0 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 oping until all
133b0 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 tables and indic
133c0 65 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 es with.** rootp
133d0 61 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 age==iFrom have
133e0 62 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 been converted t
133f0 6f 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 o have a rootpag
13400 65 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f e of iTo.** in o
13410 72 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 rder to be certa
13420 69 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 in that we got t
13430 68 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f he right one..*/
13440 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
13450 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
13460 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 void sqlite3Root
13470 50 61 67 65 4d 6f 76 65 64 28 73 71 6c 69 74 65 PageMoved(sqlite
13480 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 3 *db, int iDb,
13490 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 int iFrom, int i
134a0 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 To){. HashElem
134b0 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20 2a *pElem;. Hash *
134c0 70 48 61 73 68 3b 0a 20 20 44 62 20 2a 70 44 62 pHash;. Db *pDb
134d0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
134e0 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 ite3SchemaMutexH
134f0 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 eld(db, iDb, 0)
13500 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e );. pDb = &db->
13510 61 44 62 5b 69 44 62 5d 3b 0a 20 20 70 48 61 73 aDb[iDb];. pHas
13520 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d h = &pDb->pSchem
13530 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f a->tblHash;. fo
13540 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 r(pElem=sqliteHa
13550 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 shFirst(pHash);
13560 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c pElem; pElem=sql
13570 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 iteHashNext(pEle
13580 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a m)){. Table *
13590 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 pTab = sqliteHas
135a0 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 hData(pElem);.
135b0 20 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d if( pTab->tnum
135c0 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 ==iFrom ){.
135d0 20 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 pTab->tnum = iT
135e0 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 o;. }. }. p
135f0 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 Hash = &pDb->pSc
13600 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 hema->idxHash;.
13610 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 for(pElem=sqlit
13620 65 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 eHashFirst(pHash
13630 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d ); pElem; pElem=
13640 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 sqliteHashNext(p
13650 45 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 Elem)){. Inde
13660 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 x *pIdx = sqlite
13670 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b HashData(pElem);
13680 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 . if( pIdx->t
13690 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 num==iFrom ){.
136a0 20 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d pIdx->tnum =
136b0 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a iTo;. }. }.
136c0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
136d0 57 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 Write code to er
136e0 61 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 ase the table wi
136f0 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 th root-page iTa
13700 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 ble from databas
13710 65 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 e iDb..** Also w
13720 72 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 rite code to mod
13730 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d ify the sqlite_m
13740 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 aster table and
13750 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a internal schema.
13760 2a 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 ** if a root-pag
13770 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 e of another tab
13780 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 le is moved by t
13790 68 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 he btree-layer w
137a0 68 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 hilst.** erasing
137b0 20 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 iTable (this ca
137c0 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e n happen with an
137d0 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 auto-vacuum dat
137e0 61 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 abase)..*/ .stat
137f0 69 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 ic void destroyR
13800 6f 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 ootPage(Parse *p
13810 50 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c Parse, int iTabl
13820 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 e, int iDb){. V
13830 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 dbe *v = sqlite3
13840 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
13850 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 . int r1 = sqli
13860 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
13870 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 arse);. assert(
13880 20 69 54 61 62 6c 65 3e 31 20 29 3b 0a 20 20 73 iTable>1 );. s
13890 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
138a0 28 76 2c 20 4f 50 5f 44 65 73 74 72 6f 79 2c 20 (v, OP_Destroy,
138b0 69 54 61 62 6c 65 2c 20 72 31 2c 20 69 44 62 29 iTable, r1, iDb)
138c0 3b 0a 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 ;. sqlite3MayAb
138d0 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 23 69 66 ort(pParse);.#if
138e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
138f0 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a _AUTOVACUUM. /*
13900 20 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 OP_Destroy stor
13910 65 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 es an in integer
13920 20 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 r1. If this int
13930 65 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e eger. ** is non
13940 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 -zero, then it i
13950 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 s the root page
13960 6e 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c number of a tabl
13970 65 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 e moved to. **
13980 6c 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e location iTable.
13990 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
139a0 6f 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 ode modifies the
139b0 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
139c0 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 able to. ** ref
139d0 6c 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a lect this.. **.
139e0 20 20 2a 2a 20 54 68 65 20 22 23 4e 4e 4e 22 20 ** The "#NNN"
139f0 69 6e 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 in the SQL is a
13a00 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 special constant
13a10 20 74 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 that means what
13a20 65 76 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 ever value. **
13a30 69 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 4e is in register N
13a40 4e 4e 2e 20 20 53 65 65 20 67 72 61 6d 6d 61 72 NN. See grammar
13a50 20 72 75 6c 65 73 20 61 73 73 6f 63 69 61 74 65 rules associate
13a60 64 20 77 69 74 68 20 74 68 65 20 54 4b 5f 52 45 d with the TK_RE
13a70 47 49 53 54 45 52 0a 20 20 2a 2a 20 74 6f 6b 65 GISTER. ** toke
13a80 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c n for additional
13a90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 information..
13aa0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 */. sqlite3Nest
13ab0 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 edParse(pParse,
13ac0 0a 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 . "UPDATE %Q
13ad0 2e 25 73 20 53 45 54 20 72 6f 6f 74 70 61 67 65 .%s SET rootpage
13ae0 3d 25 64 20 57 48 45 52 45 20 23 25 64 20 41 4e =%d WHERE #%d AN
13af0 44 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 22 2c D rootpage=#%d",
13b00 0a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 . pParse->db
13b10 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 4e ->aDb[iDb].zDbSN
13b20 61 6d 65 2c 20 4d 41 53 54 45 52 5f 4e 41 4d 45 ame, MASTER_NAME
13b30 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c 20 72 31 , iTable, r1, r1
13b40 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 );.#endif. sqli
13b50 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
13b60 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d g(pParse, r1);.}
13b70 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 56 44 ../*.** Write VD
13b80 42 45 20 63 6f 64 65 20 74 6f 20 65 72 61 73 65 BE code to erase
13b90 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20 table pTab and
13ba0 61 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 69 all associated i
13bb0 6e 64 69 63 65 73 20 6f 6e 20 64 69 73 6b 2e 0a ndices on disk..
13bc0 2a 2a 20 43 6f 64 65 20 74 6f 20 75 70 64 61 74 ** Code to updat
13bd0 65 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 e the sqlite_mas
13be0 74 65 72 20 74 61 62 6c 65 73 20 61 6e 64 20 69 ter tables and i
13bf0 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 64 nternal schema d
13c00 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20 69 6e efinitions.** in
13c10 20 63 61 73 65 20 61 20 72 6f 6f 74 2d 70 61 67 case a root-pag
13c20 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 e belonging to a
13c30 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 69 73 20 nother table is
13c40 6d 6f 76 65 64 20 62 79 20 74 68 65 20 62 74 72 moved by the btr
13c50 65 65 20 6c 61 79 65 72 0a 2a 2a 20 69 73 20 61 ee layer.** is a
13c60 6c 73 6f 20 61 64 64 65 64 20 28 74 68 69 73 20 lso added (this
13c70 63 61 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 can happen with
13c80 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 an auto-vacuum d
13c90 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 atabase)..*/.sta
13ca0 74 69 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 tic void destroy
13cb0 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 Table(Parse *pPa
13cc0 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 rse, Table *pTab
13cd0 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 ){. /* If the d
13ce0 61 74 61 62 61 73 65 20 6d 61 79 20 62 65 20 61 atabase may be a
13cf0 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62 uto-vacuum capab
13d00 6c 65 20 28 69 66 20 53 51 4c 49 54 45 5f 4f 4d le (if SQLITE_OM
13d10 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
13d20 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 ** is not define
13d30 64 29 2c 20 74 68 65 6e 20 69 74 20 69 73 20 69 d), then it is i
13d40 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c mportant to call
13d50 20 4f 50 5f 44 65 73 74 72 6f 79 20 6f 6e 20 74 OP_Destroy on t
13d60 68 65 0a 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e he. ** table an
13d70 64 20 69 6e 64 65 78 20 72 6f 6f 74 2d 70 61 67 d index root-pag
13d80 65 73 20 69 6e 20 6f 72 64 65 72 2c 20 73 74 61 es in order, sta
13d90 72 74 69 6e 67 20 77 69 74 68 20 74 68 65 20 6e rting with the n
13da0 75 6d 65 72 69 63 61 6c 6c 79 20 0a 20 20 2a 2a umerically . **
13db0 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 largest root-pa
13dc0 67 65 20 6e 75 6d 62 65 72 2e 20 54 68 69 73 20 ge number. This
13dd0 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 guarantees that
13de0 6e 6f 6e 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 none of the root
13df0 2d 70 61 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 -pages. ** to b
13e00 65 20 64 65 73 74 72 6f 79 65 64 20 69 73 20 72 e destroyed is r
13e10 65 6c 6f 63 61 74 65 64 20 62 79 20 61 6e 20 65 elocated by an e
13e20 61 72 6c 69 65 72 20 4f 50 5f 44 65 73 74 72 6f arlier OP_Destro
13e30 79 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 0a 20 y. i.e. if the.
13e40 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 65 ** following we
13e50 72 65 20 63 6f 64 65 64 3a 0a 20 20 2a 2a 0a 20 re coded:. **.
13e60 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 34 ** OP_Destroy 4
13e70 20 30 0a 20 20 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 0. ** .... **
13e80 20 4f 50 5f 44 65 73 74 72 6f 79 20 35 20 30 0a OP_Destroy 5 0.
13e90 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20 72 6f **. ** and ro
13ea0 6f 74 20 70 61 67 65 20 35 20 68 61 70 70 65 6e ot page 5 happen
13eb0 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c 61 72 ed to be the lar
13ec0 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e gest root-page n
13ed0 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 20 20 2a umber in the. *
13ee0 2a 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e * database, then
13ef0 20 72 6f 6f 74 20 70 61 67 65 20 35 20 77 6f 75 root page 5 wou
13f00 6c 64 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 70 ld be moved to p
13f10 61 67 65 20 34 20 62 79 20 74 68 65 20 0a 20 20 age 4 by the .
13f20 2a 2a 20 22 4f 50 5f 44 65 73 74 72 6f 79 20 34 ** "OP_Destroy 4
13f30 20 30 22 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 0" opcode. The
13f40 73 75 62 73 65 71 75 65 6e 74 20 22 4f 50 5f 44 subsequent "OP_D
13f50 65 73 74 72 6f 79 20 35 20 30 22 20 77 6f 75 6c estroy 5 0" woul
13f60 64 20 68 69 74 0a 20 20 2a 2a 20 61 20 66 72 65 d hit. ** a fre
13f70 65 2d 6c 69 73 74 20 70 61 67 65 2e 0a 20 20 2a e-list page.. *
13f80 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 /. int iTab = p
13f90 54 61 62 2d 3e 74 6e 75 6d 3b 0a 20 20 69 6e 74 Tab->tnum;. int
13fa0 20 69 44 65 73 74 72 6f 79 65 64 20 3d 20 30 3b iDestroyed = 0;
13fb0 0a 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a .. while( 1 ){.
13fc0 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b Index *pIdx;
13fd0 0a 20 20 20 20 69 6e 74 20 69 4c 61 72 67 65 73 . int iLarges
13fe0 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 t = 0;.. if(
13ff0 69 44 65 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c iDestroyed==0 ||
14000 20 69 54 61 62 3c 69 44 65 73 74 72 6f 79 65 64 iTab<iDestroyed
14010 20 29 7b 0a 20 20 20 20 20 20 69 4c 61 72 67 65 ){. iLarge
14020 73 74 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 7d st = iTab;. }
14030 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 . for(pIdx=pT
14040 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 ab->pIndex; pIdx
14050 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 ; pIdx=pIdx->pNe
14060 78 74 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 xt){. int i
14070 49 64 78 20 3d 20 70 49 64 78 2d 3e 74 6e 75 6d Idx = pIdx->tnum
14080 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
14090 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 pIdx->pSchema==p
140a0 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a Tab->pSchema );.
140b0 20 20 20 20 20 20 69 66 28 20 28 69 44 65 73 74 if( (iDest
140c0 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 28 69 49 64 royed==0 || (iId
140d0 78 3c 69 44 65 73 74 72 6f 79 65 64 29 29 20 26 x<iDestroyed)) &
140e0 26 20 69 49 64 78 3e 69 4c 61 72 67 65 73 74 20 & iIdx>iLargest
140f0 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 61 72 67 ){. iLarg
14100 65 73 74 20 3d 20 69 49 64 78 3b 0a 20 20 20 20 est = iIdx;.
14110 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
14120 28 20 69 4c 61 72 67 65 73 74 3d 3d 30 20 29 7b ( iLargest==0 ){
14130 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
14140 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
14150 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 int iDb = sqlite
14160 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 3SchemaToIndex(p
14170 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d Parse->db, pTab-
14180 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 >pSchema);.
14190 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
141a0 26 26 20 69 44 62 3c 70 50 61 72 73 65 2d 3e 64 && iDb<pParse->d
141b0 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 20 20 b->nDb );.
141c0 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 destroyRootPage(
141d0 70 50 61 72 73 65 2c 20 69 4c 61 72 67 65 73 74 pParse, iLargest
141e0 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 69 44 , iDb);. iD
141f0 65 73 74 72 6f 79 65 64 20 3d 20 69 4c 61 72 67 estroyed = iLarg
14200 65 73 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d est;. }. }.}
14210 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 65 ../*.** Remove e
14220 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20 ntries from the
14230 73 71 6c 69 74 65 5f 73 74 61 74 4e 20 74 61 62 sqlite_statN tab
14240 6c 65 73 20 28 66 6f 72 20 4e 20 69 6e 20 28 31 les (for N in (1
14250 2c 32 2c 33 29 29 0a 2a 2a 20 61 66 74 65 72 20 ,2,3)).** after
14260 61 20 44 52 4f 50 20 49 4e 44 45 58 20 6f 72 20 a DROP INDEX or
14270 44 52 4f 50 20 54 41 42 4c 45 20 63 6f 6d 6d 61 DROP TABLE comma
14280 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f nd..*/.static vo
14290 69 64 20 73 71 6c 69 74 65 33 43 6c 65 61 72 53 id sqlite3ClearS
142a0 74 61 74 54 61 62 6c 65 73 28 0a 20 20 50 61 72 tatTables(. Par
142b0 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
142c0 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 /* The parsi
142d0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
142e0 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 int iDb,
142f0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
14300 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f tabase number */
14310 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
14320 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 69 64 Type, /* "id
14330 78 22 20 6f 72 20 22 74 62 6c 22 20 2a 2f 0a 20 x" or "tbl" */.
14340 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
14350 6d 65 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 me /* Name
14360 6f 66 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c of index or tabl
14370 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b e */.){. int i;
14380 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
14390 44 62 4e 61 6d 65 20 3d 20 70 50 61 72 73 65 2d DbName = pParse-
143a0 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 >db->aDb[iDb].zD
143b0 62 53 4e 61 6d 65 3b 0a 20 20 66 6f 72 28 69 3d bSName;. for(i=
143c0 31 3b 20 69 3c 3d 34 3b 20 69 2b 2b 29 7b 0a 20 1; i<=4; i++){.
143d0 20 20 20 63 68 61 72 20 7a 54 61 62 5b 32 34 5d char zTab[24]
143e0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e ;. sqlite3_sn
143f0 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 54 printf(sizeof(zT
14400 61 62 29 2c 7a 54 61 62 2c 22 73 71 6c 69 74 65 ab),zTab,"sqlite
14410 5f 73 74 61 74 25 64 22 2c 69 29 3b 0a 20 20 20 _stat%d",i);.
14420 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 if( sqlite3Find
14430 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 Table(pParse->db
14440 2c 20 7a 54 61 62 2c 20 7a 44 62 4e 61 6d 65 29 , zTab, zDbName)
14450 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
14460 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 3NestedParse(pPa
14470 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 rse,. "DE
14480 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 LETE FROM %Q.%s
14490 57 48 45 52 45 20 25 73 3d 25 51 22 2c 0a 20 20 WHERE %s=%Q",.
144a0 20 20 20 20 20 20 7a 44 62 4e 61 6d 65 2c 20 7a zDbName, z
144b0 54 61 62 2c 20 7a 54 79 70 65 2c 20 7a 4e 61 6d Tab, zType, zNam
144c0 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d e. );. }
144d0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 . }.}../*.** Ge
144e0 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 nerate code to d
144f0 72 6f 70 20 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a rop a table..*/.
14500 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 void sqlite3Code
14510 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 20 DropTable(Parse
14520 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a *pParse, Table *
14530 70 54 61 62 2c 20 69 6e 74 20 69 44 62 2c 20 69 pTab, int iDb, i
14540 6e 74 20 69 73 56 69 65 77 29 7b 0a 20 20 56 64 nt isView){. Vd
14550 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 be *v;. sqlite3
14560 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
14570 62 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 b;. Trigger *pT
14580 72 69 67 67 65 72 3b 0a 20 20 44 62 20 2a 70 44 rigger;. Db *pD
14590 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 b = &db->aDb[iDb
145a0 5d 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 ];.. v = sqlite
145b0 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
145c0 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 ;. assert( v!=0
145d0 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 );. sqlite3Beg
145e0 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e inWriteOperation
145f0 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 (pParse, 1, iDb)
14600 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
14610 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
14620 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 BLE. if( IsVirt
14630 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 ual(pTab) ){.
14640 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
14650 70 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 p0(v, OP_VBegin)
14660 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
14670 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72 69 67 /* Drop all trig
14680 67 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 gers associated
14690 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 62 with the table b
146a0 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 43 6f eing dropped. Co
146b0 64 65 0a 20 20 2a 2a 20 69 73 20 67 65 6e 65 72 de. ** is gener
146c0 61 74 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 65 ated to remove e
146d0 6e 74 72 69 65 73 20 66 72 6f 6d 20 73 71 6c 69 ntries from sqli
146e0 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 2f 6f 72 te_master and/or
146f0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 74 65 6d . ** sqlite_tem
14700 70 5f 6d 61 73 74 65 72 20 69 66 20 72 65 71 75 p_master if requ
14710 69 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 54 72 ired.. */. pTr
14720 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 igger = sqlite3T
14730 72 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 riggerList(pPars
14740 65 2c 20 70 54 61 62 29 3b 0a 20 20 77 68 69 6c e, pTab);. whil
14750 65 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 e( pTrigger ){.
14760 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 assert( pTrig
14770 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 ger->pSchema==pT
14780 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a ab->pSchema || .
14790 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72 pTrigger
147a0 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 ->pSchema==db->a
147b0 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b Db[1].pSchema );
147c0 0a 20 20 20 20 73 71 6c 69 74 65 33 44 72 6f 70 . sqlite3Drop
147d0 54 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73 TriggerPtr(pPars
147e0 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 e, pTrigger);.
147f0 20 20 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 pTrigger = pTr
14800 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 igger->pNext;.
14810 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
14820 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 E_OMIT_AUTOINCRE
14830 4d 45 4e 54 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 MENT. /* Remove
14840 20 61 6e 79 20 65 6e 74 72 69 65 73 20 6f 66 20 any entries of
14850 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 the sqlite_seque
14860 6e 63 65 20 74 61 62 6c 65 20 61 73 73 6f 63 69 nce table associ
14870 61 74 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 74 ated with. ** t
14880 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 he table being d
14890 72 6f 70 70 65 64 2e 20 54 68 69 73 20 69 73 20 ropped. This is
148a0 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 done before the
148b0 74 61 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 table is dropped
148c0 0a 20 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72 . ** at the btr
148d0 65 65 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 ee level, in cas
148e0 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 e the sqlite_seq
148f0 75 65 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64 uence table need
14900 73 20 74 6f 0a 20 20 2a 2a 20 6d 6f 76 65 20 61 s to. ** move a
14910 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 s a result of th
14920 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70 e drop (can happ
14930 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 en in auto-vacuu
14940 6d 20 6d 6f 64 65 29 2e 0a 20 20 2a 2f 0a 20 20 m mode).. */.
14950 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 if( pTab->tabFla
14960 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 gs & TF_Autoincr
14970 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c ement ){. sql
14980 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
14990 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 44 pParse,. "D
149a0 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 ELETE FROM %Q.sq
149b0 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 57 48 lite_sequence WH
149c0 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 ERE name=%Q",.
149d0 20 20 20 20 70 44 62 2d 3e 7a 44 62 53 4e 61 6d pDb->zDbSNam
149e0 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 e, pTab->zName.
149f0 20 20 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 );. }.#endif
14a00 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 .. /* Drop all
14a10 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 SQLITE_MASTER ta
14a20 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 65 6e ble and index en
14a30 74 72 69 65 73 20 74 68 61 74 20 72 65 66 65 72 tries that refer
14a40 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 74 61 62 to the. ** tab
14a50 6c 65 2e 20 54 68 65 20 70 72 6f 67 72 61 6d 20 le. The program
14a60 6e 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 name loops throu
14a70 67 68 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 gh the master ta
14a80 62 6c 65 20 61 6e 64 20 64 65 6c 65 74 65 73 0a ble and deletes.
14a90 20 20 2a 2a 20 65 76 65 72 79 20 72 6f 77 20 74 ** every row t
14aa0 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 20 hat refers to a
14ab0 74 61 62 6c 65 20 6f 66 20 74 68 65 20 73 61 6d table of the sam
14ac0 65 20 6e 61 6d 65 20 61 73 20 74 68 65 20 6f 6e e name as the on
14ad0 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64 72 6f e being. ** dro
14ae0 70 70 65 64 2e 20 54 72 69 67 67 65 72 73 20 61 pped. Triggers a
14af0 72 65 20 68 61 6e 64 6c 65 64 20 73 65 70 61 72 re handled separ
14b00 61 74 65 6c 79 20 62 65 63 61 75 73 65 20 61 20 ately because a
14b10 74 72 69 67 67 65 72 20 63 61 6e 20 62 65 0a 20 trigger can be.
14b20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 20 74 ** created in t
14b30 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 he temp database
14b40 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 that refers to
14b50 61 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 a table in anoth
14b60 65 72 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 er. ** database
14b70 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
14b80 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 NestedParse(pPar
14b90 73 65 2c 20 0a 20 20 20 20 20 20 22 44 45 4c 45 se, . "DELE
14ba0 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 TE FROM %Q.%s WH
14bb0 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 ERE tbl_name=%Q
14bc0 61 6e 64 20 74 79 70 65 21 3d 27 74 72 69 67 67 and type!='trigg
14bd0 65 72 27 22 2c 0a 20 20 20 20 20 20 70 44 62 2d er'",. pDb-
14be0 3e 7a 44 62 53 4e 61 6d 65 2c 20 4d 41 53 54 45 >zDbSName, MASTE
14bf0 52 5f 4e 41 4d 45 2c 20 70 54 61 62 2d 3e 7a 4e R_NAME, pTab->zN
14c00 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 69 73 56 ame);. if( !isV
14c10 69 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61 iew && !IsVirtua
14c20 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 64 l(pTab) ){. d
14c30 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50 61 72 estroyTable(pPar
14c40 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 7d 0a 0a se, pTab);. }..
14c50 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 /* Remove the
14c60 74 61 62 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d table entry from
14c70 20 53 51 4c 69 74 65 27 73 20 69 6e 74 65 72 6e SQLite's intern
14c80 61 6c 20 73 63 68 65 6d 61 20 61 6e 64 20 6d 6f al schema and mo
14c90 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 73 63 dify. ** the sc
14ca0 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 20 20 2a hema cookie.. *
14cb0 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 /. if( IsVirtua
14cc0 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 l(pTab) ){. s
14cd0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
14ce0 28 76 2c 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c (v, OP_VDestroy,
14cf0 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 iDb, 0, 0, pTab
14d00 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d ->zName, 0);. }
14d10 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
14d20 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 dOp4(v, OP_DropT
14d30 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c able, iDb, 0, 0,
14d40 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 pTab->zName, 0)
14d50 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 ;. sqlite3Chang
14d60 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 eCookie(pParse,
14d70 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 56 69 iDb);. sqliteVi
14d80 65 77 52 65 73 65 74 41 6c 6c 28 64 62 2c 20 69 ewResetAll(db, i
14d90 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 Db);.}../*.** Th
14da0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
14db0 6c 6c 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 lled to do the w
14dc0 6f 72 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41 ork of a DROP TA
14dd0 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a BLE statement..*
14de0 2a 20 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e * pName is the n
14df0 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
14e00 20 74 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a to be dropped..
14e10 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 44 */.void sqlite3D
14e20 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a ropTable(Parse *
14e30 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 pParse, SrcList
14e40 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 *pName, int isVi
14e50 65 77 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a ew, int noErr){.
14e60 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 Table *pTab;.
14e70 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 Vdbe *v;. sqli
14e80 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
14e90 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b ->db;. int iDb;
14ea0 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c .. if( db->mall
14eb0 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
14ec0 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 goto exit_drop_t
14ed0 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 able;. }. asse
14ee0 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 rt( pParse->nErr
14ef0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
14f00 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 pName->nSrc==1
14f10 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
14f20 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 ReadSchema(pPars
14f30 65 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 64 e) ) goto exit_d
14f40 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 69 66 28 rop_table;. if(
14f50 20 6e 6f 45 72 72 20 29 20 64 62 2d 3e 73 75 70 noErr ) db->sup
14f60 70 72 65 73 73 45 72 72 2b 2b 3b 0a 20 20 61 73 pressErr++;. as
14f70 73 65 72 74 28 20 69 73 56 69 65 77 3d 3d 30 20 sert( isView==0
14f80 7c 7c 20 69 73 56 69 65 77 3d 3d 4c 4f 43 41 54 || isView==LOCAT
14f90 45 5f 56 49 45 57 20 29 3b 0a 20 20 70 54 61 62 E_VIEW );. pTab
14fa0 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
14fb0 54 61 62 6c 65 49 74 65 6d 28 70 50 61 72 73 65 TableItem(pParse
14fc0 2c 20 69 73 56 69 65 77 2c 20 26 70 4e 61 6d 65 , isView, &pName
14fd0 2d 3e 61 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e ->a[0]);. if( n
14fe0 6f 45 72 72 20 29 20 64 62 2d 3e 73 75 70 70 72 oErr ) db->suppr
14ff0 65 73 73 45 72 72 2d 2d 3b 0a 0a 20 20 69 66 28 essErr--;.. if(
15000 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 pTab==0 ){.
15010 69 66 28 20 6e 6f 45 72 72 20 29 20 73 71 6c 69 if( noErr ) sqli
15020 74 65 33 43 6f 64 65 56 65 72 69 66 79 4e 61 6d te3CodeVerifyNam
15030 65 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c edSchema(pParse,
15040 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 pName->a[0].zDa
15050 74 61 62 61 73 65 29 3b 0a 20 20 20 20 67 6f 74 tabase);. got
15060 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c o exit_drop_tabl
15070 65 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 e;. }. iDb = s
15080 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
15090 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 dex(db, pTab->pS
150a0 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 chema);. assert
150b0 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c ( iDb>=0 && iDb<
150c0 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a db->nDb );.. /*
150d0 20 49 66 20 70 54 61 62 20 69 73 20 61 20 76 69 If pTab is a vi
150e0 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 63 61 6c rtual table, cal
150f0 6c 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e l ViewGetColumnN
15100 61 6d 65 73 28 29 20 74 6f 20 65 6e 73 75 72 65 ames() to ensure
15110 0a 20 20 2a 2a 20 69 74 20 69 73 20 69 6e 69 74 . ** it is init
15120 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 ialized.. */.
15130 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 if( IsVirtual(pT
15140 61 62 29 20 26 26 20 73 71 6c 69 74 65 33 56 69 ab) && sqlite3Vi
15150 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 ewGetColumnNames
15160 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 (pParse, pTab) )
15170 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f {. goto exit_
15180 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a drop_table;. }.
15190 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
151a0 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
151b0 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f N. {. int co
151c0 64 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 de;. const ch
151d0 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d ar *zTab = SCHEM
151e0 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 A_TABLE(iDb);.
151f0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
15200 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d b = db->aDb[iDb]
15210 2e 7a 44 62 53 4e 61 6d 65 3b 0a 20 20 20 20 63 .zDbSName;. c
15220 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 32 onst char *zArg2
15230 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 = 0;. if( sq
15240 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
15250 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 Parse, SQLITE_DE
15260 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a LETE, zTab, 0, z
15270 44 62 29 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f Db)){. goto
15280 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 exit_drop_table
15290 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
152a0 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 isView ){.
152b0 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 if( !OMIT_TEMPDB
152c0 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 && iDb==1 ){.
152d0 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c code = SQL
152e0 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 ITE_DROP_TEMP_VI
152f0 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b EW;. }else{
15300 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 . code =
15310 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 SQLITE_DROP_VIEW
15320 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 ;. }.#ifnde
15330 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
15340 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d RTUALTABLE. }
15350 65 6c 73 65 20 69 66 28 20 49 73 56 69 72 74 75 else if( IsVirtu
15360 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 al(pTab) ){.
15370 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f code = SQLITE_
15380 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0a 20 20 20 DROP_VTABLE;.
15390 20 20 20 7a 41 72 67 32 20 3d 20 73 71 6c 69 74 zArg2 = sqlit
153a0 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 e3GetVTable(db,
153b0 70 54 61 62 29 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 pTab)->pMod->zNa
153c0 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d me;.#endif. }
153d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 else{. if(
153e0 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 !OMIT_TEMPDB &&
153f0 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 iDb==1 ){.
15400 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f code = SQLITE_
15410 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b DROP_TEMP_TABLE;
15420 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
15430 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c code = SQL
15440 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a ITE_DROP_TABLE;.
15450 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
15460 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 if( sqlite3Aut
15470 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 hCheck(pParse, c
15480 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 ode, pTab->zName
15490 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b , zArg2, zDb) ){
154a0 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 . goto exit
154b0 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 _drop_table;.
154c0 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }. if( sqlit
154d0 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
154e0 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 se, SQLITE_DELET
154f0 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 E, pTab->zName,
15500 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 0, zDb) ){.
15510 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f goto exit_drop_
15520 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d table;. }. }
15530 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 73 71 .#endif. if( sq
15540 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 lite3StrNICmp(pT
15550 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 ab->zName, "sqli
15560 74 65 5f 22 2c 20 37 29 3d 3d 30 20 0a 20 20 20 te_", 7)==0 .
15570 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 && sqlite3StrNI
15580 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c Cmp(pTab->zName,
15590 20 22 73 71 6c 69 74 65 5f 73 74 61 74 22 2c 20 "sqlite_stat",
155a0 31 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 11)!=0 ){. sq
155b0 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
155c0 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 arse, "table %s
155d0 6d 61 79 20 6e 6f 74 20 62 65 20 64 72 6f 70 70 may not be dropp
155e0 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 ed", pTab->zName
155f0 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 );. goto exit
15600 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d _drop_table;. }
15610 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
15620 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 _OMIT_VIEW. /*
15630 45 6e 73 75 72 65 20 44 52 4f 50 20 54 41 42 4c Ensure DROP TABL
15640 45 20 69 73 20 6e 6f 74 20 75 73 65 64 20 6f 6e E is not used on
15650 20 61 20 76 69 65 77 2c 20 61 6e 64 20 44 52 4f a view, and DRO
15660 50 20 56 49 45 57 20 69 73 20 6e 6f 74 20 75 73 P VIEW is not us
15670 65 64 0a 20 20 2a 2a 20 6f 6e 20 61 20 74 61 62 ed. ** on a tab
15680 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 le.. */. if( i
15690 73 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70 sView && pTab->p
156a0 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 Select==0 ){.
156b0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
156c0 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 44 52 (pParse, "use DR
156d0 4f 50 20 54 41 42 4c 45 20 74 6f 20 64 65 6c 65 OP TABLE to dele
156e0 74 65 20 74 61 62 6c 65 20 25 73 22 2c 20 70 54 te table %s", pT
156f0 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ab->zName);.
15700 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 goto exit_drop_t
15710 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 able;. }. if(
15720 21 69 73 56 69 65 77 20 26 26 20 70 54 61 62 2d !isView && pTab-
15730 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 >pSelect ){.
15740 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
15750 70 50 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f pParse, "use DRO
15760 50 20 56 49 45 57 20 74 6f 20 64 65 6c 65 74 65 P VIEW to delete
15770 20 76 69 65 77 20 25 73 22 2c 20 70 54 61 62 2d view %s", pTab-
15780 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 >zName);. got
15790 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c o exit_drop_tabl
157a0 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 e;. }.#endif..
157b0 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 /* Generate cod
157c0 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 e to remove the
157d0 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 6d table from the m
157e0 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a aster table. **
157f0 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 on disk.. */.
15800 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 v = sqlite3GetV
15810 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 dbe(pParse);. i
15820 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 f( v ){. sqli
15830 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 te3BeginWriteOpe
15840 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 ration(pParse, 1
15850 2c 20 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 , iDb);. if(
15860 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 !isView ){.
15870 20 73 71 6c 69 74 65 33 43 6c 65 61 72 53 74 61 sqlite3ClearSta
15880 74 54 61 62 6c 65 73 28 70 50 61 72 73 65 2c 20 tTables(pParse,
15890 69 44 62 2c 20 22 74 62 6c 22 2c 20 70 54 61 62 iDb, "tbl", pTab
158a0 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 ->zName);.
158b0 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 61 62 sqlite3FkDropTab
158c0 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 le(pParse, pName
158d0 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 , pTab);. }.
158e0 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 44 72 sqlite3CodeDr
158f0 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 opTable(pParse,
15900 70 54 61 62 2c 20 69 44 62 2c 20 69 73 56 69 65 pTab, iDb, isVie
15910 77 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72 w);. }..exit_dr
15920 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 op_table:. sqli
15930 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 te3SrcListDelete
15940 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a (db, pName);.}..
15950 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
15960 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 ne is called to
15970 63 72 65 61 74 65 20 61 20 6e 65 77 20 66 6f 72 create a new for
15980 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 68 65 20 eign key on the
15990 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72 65 6e 74 table.** current
159a0 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 ly under constru
159b0 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f 6c ction. pFromCol
159c0 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 determines whic
159d0 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 h columns.** in
159e0 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c the current tabl
159f0 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 e point to the f
15a00 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20 49 66 20 oreign key. If
15a10 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 6e pFromCol==0 then
15a20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 68 65 20 .** connect the
15a30 6b 65 79 20 74 6f 20 74 68 65 20 6c 61 73 74 20 key to the last
15a40 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 65 64 2e column inserted.
15a50 20 20 70 54 6f 20 69 73 20 74 68 65 20 6e 61 6d pTo is the nam
15a60 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c e of.** the tabl
15a70 65 20 72 65 66 65 72 72 65 64 20 74 6f 20 28 61 e referred to (a
15a80 2e 6b 2e 61 20 74 68 65 20 22 70 61 72 65 6e 74 .k.a the "parent
15a90 22 20 74 61 62 6c 65 29 2e 20 20 70 54 6f 43 6f " table). pToCo
15aa0 6c 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20 6f l is a list.** o
15ab0 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 f tables in the
15ac0 70 61 72 65 6e 74 20 70 54 6f 20 74 61 62 6c 65 parent pTo table
15ad0 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61 69 6e . flags contain
15ae0 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 s all.** informa
15af0 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 tion about the c
15b00 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 onflict resoluti
15b10 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 73 70 on algorithms sp
15b20 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74 68 ecified.** in th
15b30 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e 20 e ON DELETE, ON
15b40 55 50 44 41 54 45 20 61 6e 64 20 4f 4e 20 49 4e UPDATE and ON IN
15b50 53 45 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a SERT clauses..**
15b60 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73 74 72 75 .** An FKey stru
15b70 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 cture is created
15b80 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 and added to th
15b90 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c e table currentl
15ba0 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73 74 y.** under const
15bb0 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 ruction in the p
15bc0 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
15bd0 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 field..**.** Th
15be0 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 e foreign key is
15bf0 20 73 65 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 set for IMMEDIA
15c00 54 45 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 TE processing.
15c10 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c A subsequent cal
15c20 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 44 l.** to sqlite3D
15c30 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 29 eferForeignKey()
15c40 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 might change th
15c50 69 73 20 74 6f 20 44 45 46 45 52 52 45 44 2e 0a is to DEFERRED..
15c60 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 43 */.void sqlite3C
15c70 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 reateForeignKey(
15c80 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
15c90 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 , /* Parsi
15ca0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
15cb0 45 78 70 72 4c 69 73 74 20 2a 70 46 72 6f 6d 43 ExprList *pFromC
15cc0 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 ol, /* Columns
15cd0 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 74 68 in this table th
15ce0 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 74 68 65 at point to othe
15cf0 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b r table */. Tok
15d00 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20 20 20 20 en *pTo,
15d10 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
15d20 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a other table */.
15d30 20 20 45 78 70 72 4c 69 73 74 20 2a 70 54 6f 43 ExprList *pToC
15d40 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e ol, /* Column
15d50 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 74 s in the other t
15d60 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c able */. int fl
15d70 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f ags /
15d80 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c * Conflict resol
15d90 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 ution algorithms
15da0 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 . */.){. sqlite
15db0 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
15dc0 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 db;.#ifndef SQLI
15dd0 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f TE_OMIT_FOREIGN_
15de0 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 KEY. FKey *pFKe
15df0 79 20 3d 20 30 3b 0a 20 20 46 4b 65 79 20 2a 70 y = 0;. FKey *p
15e00 4e 65 78 74 54 6f 3b 0a 20 20 54 61 62 6c 65 20 NextTo;. Table
15e10 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 *p = pParse->pNe
15e20 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 6e 42 wTable;. int nB
15e30 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 yte;. int i;.
15e40 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 72 int nCol;. char
15e50 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 *z;.. assert(
15e60 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 pTo!=0 );. if(
15e70 70 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 p==0 || IN_DECLA
15e80 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 66 RE_VTAB ) goto f
15e90 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70 46 72 k_end;. if( pFr
15ea0 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 omCol==0 ){.
15eb0 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43 int iCol = p->nC
15ec0 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 4e 45 ol-1;. if( NE
15ed0 56 45 52 28 69 43 6f 6c 3c 30 29 20 29 20 67 6f VER(iCol<0) ) go
15ee0 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69 to fk_end;. i
15ef0 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f f( pToCol && pTo
15f00 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b Col->nExpr!=1 ){
15f10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
15f20 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
15f30 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25 foreign key on %
15f40 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73 68 s". " sh
15f50 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f ould reference o
15f60 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f nly one column o
15f70 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20 f table %T",.
15f80 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43 p->aCol[iC
15f90 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b ol].zName, pTo);
15fa0 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 . goto fk_e
15fb0 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43 nd;. }. nC
15fc0 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 ol = 1;. }else
15fd0 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 if( pToCol && pT
15fe0 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72 oCol->nExpr!=pFr
15ff0 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a omCol->nExpr ){.
16000 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
16010 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 Msg(pParse,.
16020 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63 "number of c
16030 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67 olumns in foreig
16040 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d n key does not m
16050 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20 atch the number
16060 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f of ". "co
16070 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66 lumns in the ref
16080 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b erenced table");
16090 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 . goto fk_end
160a0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e ;. }else{. n
160b0 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e Col = pFromCol->
160c0 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79 nExpr;. }. nBy
160d0 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b te = sizeof(*pFK
160e0 65 79 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 ey) + (nCol-1)*s
160f0 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61 43 6f izeof(pFKey->aCo
16100 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b l[0]) + pTo->n +
16110 20 31 3b 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 1;. if( pToCol
16120 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ){. for(i=0;
16130 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 i<pToCol->nExpr
16140 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 42 ; i++){. nB
16150 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 yte += sqlite3St
16160 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61 rlen30(pToCol->a
16170 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a [i].zName) + 1;.
16180 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 }. }. pFKe
16190 79 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c y = sqlite3DbMal
161a0 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 locZero(db, nByt
161b0 65 20 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79 e );. if( pFKey
161c0 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ==0 ){. goto
161d0 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46 fk_end;. }. pF
161e0 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a Key->pFrom = p;.
161f0 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 pFKey->pNextFr
16200 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20 om = p->pFKey;.
16210 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b z = (char*)&pFK
16220 65 79 2d 3e 61 43 6f 6c 5b 6e 43 6f 6c 5d 3b 0a ey->aCol[nCol];.
16230 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a pFKey->zTo = z
16240 3b 0a 20 20 69 66 28 20 49 4e 5f 52 45 4e 41 4d ;. if( IN_RENAM
16250 45 5f 4f 42 4a 45 43 54 20 29 7b 0a 20 20 20 20 E_OBJECT ){.
16260 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 54 6f 6b sqlite3RenameTok
16270 65 6e 4d 61 70 28 70 50 61 72 73 65 2c 20 28 76 enMap(pParse, (v
16280 6f 69 64 2a 29 7a 2c 20 70 54 6f 29 3b 0a 20 20 oid*)z, pTo);.
16290 7d 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 }. memcpy(z, pT
162a0 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20 o->z, pTo->n);.
162b0 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a z[pTo->n] = 0;.
162c0 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 sqlite3Dequote
162d0 28 7a 29 3b 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d (z);. z += pTo-
162e0 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e >n+1;. pFKey->n
162f0 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66 Col = nCol;. if
16300 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b ( pFromCol==0 ){
16310 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c . pFKey->aCol
16320 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e [0].iFrom = p->n
16330 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a Col-1;. }else{.
16340 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
16350 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Col; i++){.
16360 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f int j;. fo
16370 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c r(j=0; j<p->nCol
16380 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; j++){.
16390 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
163a0 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e mp(p->aCol[j].zN
163b0 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 ame, pFromCol->a
163c0 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b [i].zName)==0 ){
163d0 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 . pFKey
163e0 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 ->aCol[i].iFrom
163f0 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 = j;. b
16400 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
16410 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
16420 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a ( j>=p->nCol ){.
16430 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
16440 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
16450 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e . "unkn
16460 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c own column \"%s\
16470 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 " in foreign key
16480 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20 definition", .
16490 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f pFromCo
164a0 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a l->a[i].zName);.
164b0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f goto fk_
164c0 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 end;. }.
164d0 20 20 20 69 66 28 20 49 4e 5f 52 45 4e 41 4d 45 if( IN_RENAME
164e0 5f 4f 42 4a 45 43 54 20 29 7b 0a 20 20 20 20 20 _OBJECT ){.
164f0 20 20 20 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 sqlite3Rename
16500 54 6f 6b 65 6e 52 65 6d 61 70 28 70 50 61 72 73 TokenRemap(pPars
16510 65 2c 20 26 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b e, &pFKey->aCol[
16520 69 5d 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b i], pFromCol->a[
16530 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 i].zName);.
16540 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 }. }. }. i
16550 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 f( pToCol ){.
16560 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c for(i=0; i<nCol
16570 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e ; i++){. in
16580 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 t n = sqlite3Str
16590 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61 5b len30(pToCol->a[
165a0 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 i].zName);.
165b0 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e pFKey->aCol[i].
165c0 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20 zCol = z;.
165d0 69 66 28 20 49 4e 5f 52 45 4e 41 4d 45 5f 4f 42 if( IN_RENAME_OB
165e0 4a 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20 JECT ){.
165f0 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 54 6f 6b sqlite3RenameTok
16600 65 6e 52 65 6d 61 70 28 70 50 61 72 73 65 2c 20 enRemap(pParse,
16610 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e z, pToCol->a[i].
16620 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a zName);. }.
16630 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 memcpy(z,
16640 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 pToCol->a[i].zNa
16650 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7a 5b me, n);. z[
16660 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 20 n] = 0;. z
16670 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 += n+1;. }.
16680 7d 0a 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 }. pFKey->isDef
16690 65 72 72 65 64 20 3d 20 30 3b 0a 20 20 70 46 4b erred = 0;. pFK
166a0 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 30 5d 20 3d ey->aAction[0] =
166b0 20 28 75 38 29 28 66 6c 61 67 73 20 26 20 30 78 (u8)(flags & 0x
166c0 66 66 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 ff);
166d0 2f 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 63 74 /* ON DELETE act
166e0 69 6f 6e 20 2a 2f 0a 20 20 70 46 4b 65 79 2d 3e ion */. pFKey->
166f0 61 41 63 74 69 6f 6e 5b 31 5d 20 3d 20 28 75 38 aAction[1] = (u8
16700 29 28 28 66 6c 61 67 73 20 3e 3e 20 38 20 29 20 )((flags >> 8 )
16710 26 20 30 78 66 66 29 3b 20 20 20 20 2f 2a 20 4f & 0xff); /* O
16720 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e 20 N UPDATE action
16730 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 */.. assert( sq
16740 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 lite3SchemaMutex
16750 48 65 6c 64 28 64 62 2c 20 30 2c 20 70 2d 3e 70 Held(db, 0, p->p
16760 53 63 68 65 6d 61 29 20 29 3b 0a 20 20 70 4e 65 Schema) );. pNe
16770 78 74 54 6f 20 3d 20 28 46 4b 65 79 20 2a 29 73 xtTo = (FKey *)s
16780 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 qlite3HashInsert
16790 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6b (&p->pSchema->fk
167a0 65 79 48 61 73 68 2c 20 0a 20 20 20 20 20 20 70 eyHash, . p
167b0 46 4b 65 79 2d 3e 7a 54 6f 2c 20 28 76 6f 69 64 FKey->zTo, (void
167c0 20 2a 29 70 46 4b 65 79 0a 20 20 29 3b 0a 20 20 *)pFKey. );.
167d0 69 66 28 20 70 4e 65 78 74 54 6f 3d 3d 70 46 4b if( pNextTo==pFK
167e0 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ey ){. sqlite
167f0 33 4f 6f 6d 46 61 75 6c 74 28 64 62 29 3b 0a 20 3OomFault(db);.
16800 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a goto fk_end;.
16810 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 78 74 54 }. if( pNextT
16820 6f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 o ){. assert(
16830 20 70 4e 65 78 74 54 6f 2d 3e 70 50 72 65 76 54 pNextTo->pPrevT
16840 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70 46 4b 65 o==0 );. pFKe
16850 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 70 4e 65 y->pNextTo = pNe
16860 78 74 54 6f 3b 0a 20 20 20 20 70 4e 65 78 74 54 xtTo;. pNextT
16870 6f 2d 3e 70 50 72 65 76 54 6f 20 3d 20 70 46 4b o->pPrevTo = pFK
16880 65 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69 ey;. }.. /* Li
16890 6e 6b 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b nk the foreign k
168a0 65 79 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 ey to the table
168b0 61 73 20 74 68 65 20 6c 61 73 74 20 73 74 65 70 as the last step
168c0 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 46 4b 65 .. */. p->pFKe
168d0 79 20 3d 20 70 46 4b 65 79 3b 0a 20 20 70 46 4b y = pFKey;. pFK
168e0 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e 64 3a ey = 0;..fk_end:
168f0 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
16900 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 23 65 6e (db, pFKey);.#en
16910 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
16920 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 SQLITE_OMIT_FORE
16930 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20 73 71 IGN_KEY) */. sq
16940 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
16950 65 74 65 28 64 62 2c 20 70 46 72 6f 6d 43 6f 6c ete(db, pFromCol
16960 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
16970 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
16980 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ToCol);.}../*.**
16990 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
169a0 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 called when an
169b0 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 INITIALLY IMMEDI
169c0 41 54 45 20 6f 72 20 49 4e 49 54 49 41 4c 4c 59 ATE or INITIALLY
169d0 20 44 45 46 45 52 52 45 44 0a 2a 2a 20 63 6c 61 DEFERRED.** cla
169e0 75 73 65 20 69 73 20 73 65 65 6e 20 61 73 20 70 use is seen as p
169f0 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e art of a foreign
16a00 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e key definition.
16a10 20 20 54 68 65 20 69 73 44 65 66 65 72 72 65 64 The isDeferred
16a20 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 .** parameter is
16a30 20 31 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 1 for INITIALLY
16a40 20 44 45 46 45 52 52 45 44 20 61 6e 64 20 30 20 DEFERRED and 0
16a50 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d for INITIALLY IM
16a60 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 54 68 65 20 MEDIATE..** The
16a70 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 behavior of the
16a80 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 most recently cr
16a90 65 61 74 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 eated foreign ke
16aa0 79 20 69 73 20 61 64 6a 75 73 74 65 64 0a 2a 2a y is adjusted.**
16ab0 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2f accordingly..*/
16ac0 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 .void sqlite3Def
16ad0 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 erForeignKey(Par
16ae0 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
16af0 69 73 44 65 66 65 72 72 65 64 29 7b 0a 23 69 66 isDeferred){.#if
16b00 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16b10 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 54 _FOREIGN_KEY. T
16b20 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 46 4b able *pTab;. FK
16b30 65 79 20 2a 70 46 4b 65 79 3b 0a 20 20 69 66 28 ey *pFKey;. if(
16b40 20 28 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d (pTab = pParse-
16b50 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 7c >pNewTable)==0 |
16b60 7c 20 28 70 46 4b 65 79 20 3d 20 70 54 61 62 2d | (pFKey = pTab-
16b70 3e 70 46 4b 65 79 29 3d 3d 30 20 29 20 72 65 74 >pFKey)==0 ) ret
16b80 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 urn;. assert( i
16b90 73 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c 20 sDeferred==0 ||
16ba0 69 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29 3b isDeferred==1 );
16bb0 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d /* EV: R-30323-
16bc0 32 31 39 31 37 20 2a 2f 0a 20 20 70 46 4b 65 79 21917 */. pFKey
16bd0 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 28 ->isDeferred = (
16be0 75 38 29 69 73 44 65 66 65 72 72 65 64 3b 0a 23 u8)isDeferred;.#
16bf0 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 endif.}../*.** G
16c00 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 enerate code tha
16c10 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e 64 t will erase and
16c20 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70 refill index *p
16c30 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a Idx. This is.**
16c40 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c used to initial
16c50 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 61 ize a newly crea
16c60 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f 20 ted index or to
16c70 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a recompute the.**
16c80 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69 content of an i
16c90 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 ndex in response
16ca0 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63 6f to a REINDEX co
16cb0 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20 mmand..**.** if
16cc0 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 6e memRootPage is n
16cd0 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74 20 ot negative, it
16ce0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 69 means that the i
16cf0 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a ndex is newly.**
16d00 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 72 created. The r
16d10 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69 65 egister specifie
16d20 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65 d by memRootPage
16d30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a contains the.**
16d40 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 root page numbe
16d50 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 r of the index.
16d60 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 If memRootPage
16d70 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 is negative, the
16d80 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 61 n.** the index a
16d90 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e lready exists an
16da0 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72 65 d must be cleare
16db0 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 72 d before being r
16dc0 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74 efilled and.** t
16dd0 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d he root page num
16de0 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 ber of the index
16df0 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70 is taken from p
16e00 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a Index->tnum..*/.
16e10 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
16e20 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 50 te3RefillIndex(P
16e30 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e arse *pParse, In
16e40 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74 dex *pIndex, int
16e50 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20 memRootPage){.
16e60 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 Table *pTab = p
16e70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20 Index->pTable;
16e80 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 /* The table tha
16e90 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a t is indexed */.
16ea0 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 int iTab = pPa
16eb0 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 rse->nTab++;
16ec0 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72 /* Btree cursor
16ed0 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20 2a used for pTab *
16ee0 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70 /. int iIdx = p
16ef0 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 Parse->nTab++;
16f00 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 /* Btree curs
16f10 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e 64 or used for pInd
16f20 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 53 6f 72 ex */. int iSor
16f30 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 ter;
16f40 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 /* Cursor
16f50 20 6f 70 65 6e 65 64 20 62 79 20 4f 70 65 6e 53 opened by OpenS
16f60 6f 72 74 65 72 20 28 69 66 20 69 6e 20 75 73 65 orter (if in use
16f70 29 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 31 ) */. int addr1
16f80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
16f90 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
16fa0 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 of top of loop
16fb0 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 32 3b 20 */. int addr2;
16fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16fd0 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 74 /* Address t
16fe0 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 6e 65 o jump to for ne
16ff0 78 74 20 69 74 65 72 61 74 69 6f 6e 20 2a 2f 0a xt iteration */.
17000 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 int tnum;
17010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17020 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 /* Root page of
17030 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 index */. int
17040 69 50 61 72 74 49 64 78 4c 61 62 65 6c 3b 20 20 iPartIdxLabel;
17050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 /* Ju
17060 6d 70 20 74 6f 20 74 68 69 73 20 6c 61 62 65 6c mp to this label
17070 20 74 6f 20 73 6b 69 70 20 61 20 72 6f 77 20 2a to skip a row *
17080 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 /. Vdbe *v;
17090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
170a0 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 /* Generate c
170b0 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69 ode into this vi
170c0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f rtual machine */
170d0 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 . KeyInfo *pKey
170e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
170f0 20 20 2f 2a 20 4b 65 79 49 6e 66 6f 20 66 6f 72 /* KeyInfo for
17100 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 index */. int
17110 72 65 67 52 65 63 6f 72 64 3b 20 20 20 20 20 20 regRecord;
17120 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
17130 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 61 gister holding a
17140 73 73 65 6d 62 6c 65 64 20 69 6e 64 65 78 20 72 ssembled index r
17150 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 ecord */. sqlit
17160 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
17170 3e 64 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 >db; /* The
17180 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
17190 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 tion */. int iD
171a0 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
171b0 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 6e aToIndex(db, pIn
171c0 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a dex->pSchema);..
171d0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
171e0 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
171f0 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 N. if( sqlite3A
17200 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c uthCheck(pParse,
17210 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 45 58 2c SQLITE_REINDEX,
17220 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 pIndex->zName,
17230 30 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 0,. db->aDb
17240 5b 69 44 62 5d 2e 7a 44 62 53 4e 61 6d 65 20 29 [iDb].zDbSName )
17250 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
17260 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
17270 20 52 65 71 75 69 72 65 20 61 20 77 72 69 74 65 Require a write
17280 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 -lock on the tab
17290 6c 65 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 le to perform th
172a0 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a is operation */.
172b0 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f sqlite3TableLo
172c0 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 ck(pParse, iDb,
172d0 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 31 2c 20 70 pTab->tnum, 1, p
172e0 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 Tab->zName);..
172f0 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
17300 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 be(pParse);. if
17310 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( v==0 ) return;
17320 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f 74 50 61 . if( memRootPa
17330 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 74 6e 75 ge>=0 ){. tnu
17340 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3b m = memRootPage;
17350 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 74 6e . }else{. tn
17360 75 6d 20 3d 20 70 49 6e 64 65 78 2d 3e 74 6e 75 um = pIndex->tnu
17370 6d 3b 0a 20 20 7d 0a 20 20 70 4b 65 79 20 3d 20 m;. }. pKey =
17380 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f 4f 66 sqlite3KeyInfoOf
17390 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 Index(pParse, pI
173a0 6e 64 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 ndex);. assert(
173b0 20 70 4b 65 79 21 3d 30 20 7c 7c 20 64 62 2d 3e pKey!=0 || db->
173c0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 mallocFailed ||
173d0 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 3b 0a pParse->nErr );.
173e0 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 . /* Open the s
173f0 6f 72 74 65 72 20 63 75 72 73 6f 72 20 69 66 20 orter cursor if
17400 77 65 20 61 72 65 20 74 6f 20 75 73 65 20 6f 6e we are to use on
17410 65 2e 20 2a 2f 0a 20 20 69 53 6f 72 74 65 72 20 e. */. iSorter
17420 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b = pParse->nTab++
17430 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
17440 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 6f 72 74 ddOp4(v, OP_Sort
17450 65 72 4f 70 65 6e 2c 20 69 53 6f 72 74 65 72 2c erOpen, iSorter,
17460 20 30 2c 20 70 49 6e 64 65 78 2d 3e 6e 4b 65 79 0, pIndex->nKey
17470 43 6f 6c 2c 20 28 63 68 61 72 2a 29 0a 20 20 20 Col, (char*).
17480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17490 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f 52 sqlite3KeyInfoR
174a0 65 66 28 70 4b 65 79 29 2c 20 50 34 5f 4b 45 59 ef(pKey), P4_KEY
174b0 49 4e 46 4f 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 INFO);.. /* Ope
174c0 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 4c 6f 6f n the table. Loo
174d0 70 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 72 6f p through all ro
174e0 77 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2c ws of the table,
174f0 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 64 65 78 inserting index
17500 0a 20 20 2a 2a 20 72 65 63 6f 72 64 73 20 69 6e . ** records in
17510 74 6f 20 74 68 65 20 73 6f 72 74 65 72 2e 20 2a to the sorter. *
17520 2f 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 /. sqlite3OpenT
17530 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 able(pParse, iTa
17540 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 b, iDb, pTab, OP
17550 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 _OpenRead);. ad
17560 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 dr1 = sqlite3Vdb
17570 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 eAddOp2(v, OP_Re
17580 77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 20 wind, iTab, 0);
17590 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b VdbeCoverage(v);
175a0 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 . regRecord = s
175b0 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
175c0 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 (pParse);. sqli
175d0 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 70 50 te3MultiWrite(pP
175e0 61 72 73 65 29 3b 0a 0a 20 20 73 71 6c 69 74 65 arse);.. sqlite
175f0 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 3GenerateIndexKe
17600 79 28 70 50 61 72 73 65 2c 70 49 6e 64 65 78 2c y(pParse,pIndex,
17610 69 54 61 62 2c 72 65 67 52 65 63 6f 72 64 2c 30 iTab,regRecord,0
17620 2c 26 69 50 61 72 74 49 64 78 4c 61 62 65 6c 2c ,&iPartIdxLabel,
17630 30 2c 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 0,0);. sqlite3V
17640 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
17650 53 6f 72 74 65 72 49 6e 73 65 72 74 2c 20 69 53 SorterInsert, iS
17660 6f 72 74 65 72 2c 20 72 65 67 52 65 63 6f 72 64 orter, regRecord
17670 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f );. sqlite3Reso
17680 6c 76 65 50 61 72 74 49 64 78 4c 61 62 65 6c 28 lvePartIdxLabel(
17690 70 50 61 72 73 65 2c 20 69 50 61 72 74 49 64 78 pParse, iPartIdx
176a0 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65 Label);. sqlite
176b0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
176c0 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c 20 61 64 P_Next, iTab, ad
176d0 64 72 31 2b 31 29 3b 20 56 64 62 65 43 6f 76 65 dr1+1); VdbeCove
176e0 72 61 67 65 28 76 29 3b 0a 20 20 73 71 6c 69 74 rage(v);. sqlit
176f0 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
17700 2c 20 61 64 64 72 31 29 3b 0a 20 20 69 66 28 20 , addr1);. if(
17710 6d 65 6d 52 6f 6f 74 50 61 67 65 3c 30 20 29 20 memRootPage<0 )
17720 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
17730 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 74 2(v, OP_Clear, t
17740 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c num, iDb);. sql
17750 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
17760 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 , OP_OpenWrite,
17770 69 49 64 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c iIdx, tnum, iDb,
17780 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
17790 20 20 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b (char *)pK
177a0 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b ey, P4_KEYINFO);
177b0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
177c0 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 angeP5(v, OPFLAG
177d0 5f 42 55 4c 4b 43 53 52 7c 28 28 6d 65 6d 52 6f _BULKCSR|((memRo
177e0 6f 74 50 61 67 65 3e 3d 30 29 3f 4f 50 46 4c 41 otPage>=0)?OPFLA
177f0 47 5f 50 32 49 53 52 45 47 3a 30 29 29 3b 0a 0a G_P2ISREG:0));..
17800 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 addr1 = sqlite
17810 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
17820 50 5f 53 6f 72 74 65 72 53 6f 72 74 2c 20 69 53 P_SorterSort, iS
17830 6f 72 74 65 72 2c 20 30 29 3b 20 56 64 62 65 43 orter, 0); VdbeC
17840 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 69 66 overage(v);. if
17850 28 20 49 73 55 6e 69 71 75 65 49 6e 64 65 78 28 ( IsUniqueIndex(
17860 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 69 pIndex) ){. i
17870 6e 74 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 nt j2 = sqlite3V
17880 64 62 65 47 6f 74 6f 28 76 2c 20 31 29 3b 0a 20 dbeGoto(v, 1);.
17890 20 20 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74 addr2 = sqlit
178a0 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 e3VdbeCurrentAdd
178b0 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 r(v);. sqlite
178c0 33 56 64 62 65 56 65 72 69 66 79 41 62 6f 72 74 3VdbeVerifyAbort
178d0 61 62 6c 65 28 76 2c 20 4f 45 5f 41 62 6f 72 74 able(v, OE_Abort
178e0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
178f0 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f beAddOp4Int(v, O
17900 50 5f 53 6f 72 74 65 72 43 6f 6d 70 61 72 65 2c P_SorterCompare,
17910 20 69 53 6f 72 74 65 72 2c 20 6a 32 2c 20 72 65 iSorter, j2, re
17920 67 52 65 63 6f 72 64 2c 0a 20 20 20 20 20 20 20 gRecord,.
17930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17940 20 20 70 49 6e 64 65 78 2d 3e 6e 4b 65 79 43 6f pIndex->nKeyCo
17950 6c 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 l); VdbeCoverage
17960 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 (v);. sqlite3
17970 55 6e 69 71 75 65 43 6f 6e 73 74 72 61 69 6e 74 UniqueConstraint
17980 28 70 50 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 (pParse, OE_Abor
17990 74 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 t, pIndex);.
179a0 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
179b0 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 7d 65 ere(v, j2);. }e
179c0 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 32 20 3d lse{. addr2 =
179d0 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
179e0 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 7d 0a entAddr(v);. }.
179f0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
17a00 4f 70 33 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 Op3(v, OP_Sorter
17a10 44 61 74 61 2c 20 69 53 6f 72 74 65 72 2c 20 72 Data, iSorter, r
17a20 65 67 52 65 63 6f 72 64 2c 20 69 49 64 78 29 3b egRecord, iIdx);
17a30 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
17a40 64 4f 70 31 28 76 2c 20 4f 50 5f 53 65 65 6b 45 dOp1(v, OP_SeekE
17a50 6e 64 2c 20 69 49 64 78 29 3b 0a 20 20 73 71 6c nd, iIdx);. sql
17a60 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
17a70 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 , OP_IdxInsert,
17a80 69 49 64 78 2c 20 72 65 67 52 65 63 6f 72 64 29 iIdx, regRecord)
17a90 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 ;. sqlite3VdbeC
17aa0 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 hangeP5(v, OPFLA
17ab0 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 G_USESEEKRESULT)
17ac0 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 ;. sqlite3Relea
17ad0 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 seTempReg(pParse
17ae0 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 , regRecord);.
17af0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
17b00 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 4e 65 2(v, OP_SorterNe
17b10 78 74 2c 20 69 53 6f 72 74 65 72 2c 20 61 64 64 xt, iSorter, add
17b20 72 32 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 r2); VdbeCoverag
17b30 65 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 e(v);. sqlite3V
17b40 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 dbeJumpHere(v, a
17b50 64 64 72 31 29 3b 0a 0a 20 20 73 71 6c 69 74 65 ddr1);.. sqlite
17b60 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
17b70 50 5f 43 6c 6f 73 65 2c 20 69 54 61 62 29 3b 0a P_Close, iTab);.
17b80 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
17b90 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c Op1(v, OP_Close,
17ba0 20 69 49 64 78 29 3b 0a 20 20 73 71 6c 69 74 65 iIdx);. sqlite
17bb0 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
17bc0 50 5f 43 6c 6f 73 65 2c 20 69 53 6f 72 74 65 72 P_Close, iSorter
17bd0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f );.}../*.** Allo
17be0 63 61 74 65 20 68 65 61 70 20 73 70 61 63 65 20 cate heap space
17bf0 74 6f 20 68 6f 6c 64 20 61 6e 20 49 6e 64 65 78 to hold an Index
17c00 20 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 43 6f object with nCo
17c10 6c 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a l columns..**.**
17c20 20 49 6e 63 72 65 61 73 65 20 74 68 65 20 61 6c Increase the al
17c30 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 74 6f location size to
17c40 20 70 72 6f 76 69 64 65 20 61 6e 20 65 78 74 72 provide an extr
17c50 61 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a a nExtra bytes.*
17c60 2a 20 6f 66 20 38 2d 62 79 74 65 20 61 6c 69 67 * of 8-byte alig
17c70 6e 65 64 20 73 70 61 63 65 20 61 66 74 65 72 20 ned space after
17c80 74 68 65 20 49 6e 64 65 78 20 6f 62 6a 65 63 74 the Index object
17c90 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a and return a.**
17ca0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 pointer to this
17cb0 20 65 78 74 72 61 20 73 70 61 63 65 20 69 6e 20 extra space in
17cc0 2a 70 70 45 78 74 72 61 2e 0a 2a 2f 0a 49 6e 64 *ppExtra..*/.Ind
17cd0 65 78 20 2a 73 71 6c 69 74 65 33 41 6c 6c 6f 63 ex *sqlite3Alloc
17ce0 61 74 65 49 6e 64 65 78 4f 62 6a 65 63 74 28 0a ateIndexObject(.
17cf0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
17d00 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
17d10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
17d20 0a 20 20 69 31 36 20 6e 43 6f 6c 2c 20 20 20 20 . i16 nCol,
17d30 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
17d40 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
17d50 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20 ns in the index
17d60 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c */. int nExtra,
17d70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
17d80 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
17d90 65 78 74 72 61 20 73 70 61 63 65 20 74 6f 20 61 extra space to a
17da0 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a lloc */. char *
17db0 2a 70 70 45 78 74 72 61 20 20 20 20 20 20 20 2f *ppExtra /
17dc0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
17dd0 20 22 65 78 74 72 61 22 20 73 70 61 63 65 20 2a "extra" space *
17de0 2f 0a 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 3b /.){. Index *p;
17df0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
17e00 6c 6c 6f 63 61 74 65 64 20 69 6e 64 65 78 20 6f llocated index o
17e10 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e bject */. int n
17e20 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 Byte;
17e30 2f 2a 20 42 79 74 65 73 20 6f 66 20 73 70 61 63 /* Bytes of spac
17e40 65 20 66 6f 72 20 49 6e 64 65 78 20 6f 62 6a 65 e for Index obje
17e50 63 74 20 2b 20 61 72 72 61 79 73 20 2a 2f 0a 0a ct + arrays */..
17e60 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 nByte = ROUND8
17e70 28 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 29 20 (sizeof(Index))
17e80 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f + /
17e90 2a 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 * Index structur
17ea0 65 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 e */.
17eb0 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 63 68 ROUND8(sizeof(ch
17ec0 61 72 2a 29 2a 6e 43 6f 6c 29 20 2b 20 20 20 20 ar*)*nCol) +
17ed0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a /* Index.az
17ee0 43 6f 6c 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 Coll */.
17ef0 20 20 20 20 20 20 52 4f 55 4e 44 38 28 73 69 7a ROUND8(siz
17f00 65 6f 66 28 4c 6f 67 45 73 74 29 2a 28 6e 43 6f eof(LogEst)*(nCo
17f10 6c 2b 31 29 20 2b 20 20 20 20 20 2f 2a 20 49 6e l+1) + /* In
17f20 64 65 78 2e 61 69 52 6f 77 4c 6f 67 45 73 74 20 dex.aiRowLogEst
17f30 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 */.
17f40 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69 31 36 sizeof(i16
17f50 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 )*nCol +
17f60 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43 /* Index.aiC
17f70 6f 6c 75 6d 6e 20 20 20 2a 2f 0a 20 20 20 20 20 olumn */.
17f80 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 size
17f90 6f 66 28 75 38 29 2a 6e 43 6f 6c 29 3b 20 20 20 of(u8)*nCol);
17fa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
17fb0 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f ex.aSortOrder */
17fc0 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 . p = sqlite3Db
17fd0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e MallocZero(db, n
17fe0 42 79 74 65 20 2b 20 6e 45 78 74 72 61 29 3b 0a Byte + nExtra);.
17ff0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63 if( p ){. c
18000 68 61 72 20 2a 70 45 78 74 72 61 20 3d 20 28 28 har *pExtra = ((
18010 63 68 61 72 2a 29 70 29 2b 52 4f 55 4e 44 38 28 char*)p)+ROUND8(
18020 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 29 3b 0a sizeof(Index));.
18030 20 20 20 20 70 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 p->azColl =
18040 28 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 70 45 (const char**)pE
18050 78 74 72 61 3b 20 70 45 78 74 72 61 20 2b 3d 20 xtra; pExtra +=
18060 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 63 68 ROUND8(sizeof(ch
18070 61 72 2a 29 2a 6e 43 6f 6c 29 3b 0a 20 20 20 20 ar*)*nCol);.
18080 70 2d 3e 61 69 52 6f 77 4c 6f 67 45 73 74 20 3d p->aiRowLogEst =
18090 20 28 4c 6f 67 45 73 74 2a 29 70 45 78 74 72 61 (LogEst*)pExtra
180a0 3b 20 70 45 78 74 72 61 20 2b 3d 20 73 69 7a 65 ; pExtra += size
180b0 6f 66 28 4c 6f 67 45 73 74 29 2a 28 6e 43 6f 6c of(LogEst)*(nCol
180c0 2b 31 29 3b 0a 20 20 20 20 70 2d 3e 61 69 43 6f +1);. p->aiCo
180d0 6c 75 6d 6e 20 3d 20 28 69 31 36 2a 29 70 45 78 lumn = (i16*)pEx
180e0 74 72 61 3b 20 20 20 20 20 20 20 70 45 78 74 72 tra; pExtr
180f0 61 20 2b 3d 20 73 69 7a 65 6f 66 28 69 31 36 29 a += sizeof(i16)
18100 2a 6e 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 61 53 *nCol;. p->aS
18110 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 ortOrder = (u8*)
18120 70 45 78 74 72 61 3b 0a 20 20 20 20 70 2d 3e 6e pExtra;. p->n
18130 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 3b 0a 20 Column = nCol;.
18140 20 20 20 70 2d 3e 6e 4b 65 79 43 6f 6c 20 3d 20 p->nKeyCol =
18150 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 2a 70 nCol - 1;. *p
18160 70 45 78 74 72 61 20 3d 20 28 28 63 68 61 72 2a pExtra = ((char*
18170 29 70 29 20 2b 20 6e 42 79 74 65 3b 0a 20 20 7d )p) + nByte;. }
18180 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
18190 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
181a0 65 77 20 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 ew index for an
181b0 53 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d SQL table. pNam
181c0 65 31 2e 70 4e 61 6d 65 32 20 69 73 20 74 68 65 e1.pName2 is the
181d0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 name of the ind
181e0 65 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c ex .** and pTblL
181f0 69 73 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ist is the name
18200 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 of the table tha
18210 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 t is to be index
18220 65 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a ed. Both will .
18230 2a 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 ** be NULL for a
18240 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 primary key or
18250 61 6e 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 an index that is
18260 20 63 72 65 61 74 65 64 20 74 6f 20 73 61 74 69 created to sati
18270 73 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 sfy a.** UNIQUE
18280 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49 66 20 constraint. If
18290 70 54 61 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 pTable and pInde
182a0 78 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 x are NULL, use
182b0 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
182c0 65 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c e.** as the tabl
182d0 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e e to be indexed.
182e0 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 pParse->pNewTa
182f0 62 6c 65 20 69 73 20 61 20 74 61 62 6c 65 20 74 ble is a table t
18300 68 61 74 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e hat is.** curren
18310 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 tly being constr
18320 75 63 74 65 64 20 62 79 20 61 20 43 52 45 41 54 ucted by a CREAT
18330 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
18340 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 t..**.** pList i
18350 73 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 s a list of colu
18360 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 mns to be indexe
18370 64 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 d. pList will b
18380 65 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a e NULL if this.*
18390 2a 20 69 73 20 61 20 70 72 69 6d 61 72 79 20 6b * is a primary k
183a0 65 79 20 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e ey or unique-con
183b0 73 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d straint on the m
183c0 6f 73 74 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d ost recent colum
183d0 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 n added.** to th
183e0 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c e table currentl
183f0 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 y under construc
18400 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 76 6f 69 64 20 tion. .*/.void
18410 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 sqlite3CreateInd
18420 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 ex(. Parse *pPa
18430 72 73 65 2c 20 20 20 20 20 2f 2a 20 41 6c 6c 20 rse, /* All
18440 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
18450 74 20 74 68 69 73 20 70 61 72 73 65 20 2a 2f 0a t this parse */.
18460 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c Token *pName1,
18470 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 /* First pa
18480 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 rt of index name
18490 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f . May be NULL */
184a0 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 . Token *pName2
184b0 2c 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 , /* Second
184c0 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 part of index na
184d0 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 me. May be NULL
184e0 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 */. SrcList *pT
184f0 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 blName, /* Table
18500 20 74 6f 20 69 6e 64 65 78 2e 20 55 73 65 20 70 to index. Use p
18510 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
18520 20 69 66 20 30 20 2a 2f 0a 20 20 45 78 70 72 4c if 0 */. ExprL
18530 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a ist *pList, /*
18540 20 41 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d A list of colum
18550 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 ns to be indexed
18560 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f */. int onErro
18570 72 2c 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 r, /* OE_A
18580 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c bort, OE_Ignore,
18590 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 OE_Replace, or
185a0 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b OE_None */. Tok
185b0 65 6e 20 2a 70 53 74 61 72 74 2c 20 20 20 20 20 en *pStart,
185c0 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 74 6f /* The CREATE to
185d0 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 ken that begins
185e0 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 2a this statement *
185f0 2f 0a 20 20 45 78 70 72 20 2a 70 50 49 57 68 65 /. Expr *pPIWhe
18600 72 65 2c 20 20 20 20 2f 2a 20 57 48 45 52 45 20 re, /* WHERE
18610 63 6c 61 75 73 65 20 66 6f 72 20 70 61 72 74 69 clause for parti
18620 61 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 al indices */.
18630 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 2c 20 20 int sortOrder,
18640 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72 64 65 72 /* Sort order
18650 20 6f 66 20 70 72 69 6d 61 72 79 20 6b 65 79 20 of primary key
18660 77 68 65 6e 20 70 4c 69 73 74 3d 3d 4e 55 4c 4c when pList==NULL
18670 20 2a 2f 0a 20 20 69 6e 74 20 69 66 4e 6f 74 45 */. int ifNotE
18680 78 69 73 74 2c 20 20 20 20 2f 2a 20 4f 6d 69 74 xist, /* Omit
18690 20 65 72 72 6f 72 20 69 66 20 69 6e 64 65 78 20 error if index
186a0 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a already exists *
186b0 2f 0a 20 20 75 38 20 69 64 78 54 79 70 65 20 20 /. u8 idxType
186c0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e /* The in
186d0 64 65 78 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 dex type */.){.
186e0 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 Table *pTab = 0
186f0 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 ; /* Table t
18700 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a o be indexed */.
18710 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 20 Index *pIndex
18720 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e = 0; /* The in
18730 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74 65 dex to be create
18740 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 d */. char *zNa
18750 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e me = 0; /* N
18760 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 ame of the index
18770 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b */. int nName;
18780 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
18790 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
187a0 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 rs in zName */.
187b0 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 44 62 46 int i, j;. DbF
187c0 69 78 65 72 20 73 46 69 78 3b 20 20 20 20 20 20 ixer sFix;
187d0 20 20 2f 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 /* For assigni
187e0 6e 67 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 ng database name
187f0 73 20 74 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 s to pTable */.
18800 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 int sortOrderMa
18810 73 6b 3b 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f sk; /* 1 to ho
18820 6e 6f 72 20 44 45 53 43 20 69 6e 20 69 6e 64 65 nor DESC in inde
18830 78 2e 20 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e x. 0 to ignore.
18840 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
18850 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
18860 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 Db *pDb;
18870 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 /* The sp
18880 65 63 69 66 69 63 20 74 61 62 6c 65 20 63 6f 6e ecific table con
18890 74 61 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 taining the inde
188a0 78 65 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a xed database */.
188b0 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
188c0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
188d0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
188e0 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 77 72 that is being wr
188f0 69 74 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e itten */. Token
18900 20 2a 70 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 *pName = 0;
18910 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e /* Unqualified n
18920 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 ame of the index
18930 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 to create */.
18940 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
18950 69 74 65 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b item *pListItem;
18960 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 /* For looping
18970 6f 76 65 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 over pList */.
18980 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b 20 int nExtra = 0;
18990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
189a0 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63 61 /* Space alloca
189b0 74 65 64 20 66 6f 72 20 7a 45 78 74 72 61 5b 5d ted for zExtra[]
189c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 */. int nExtra
189d0 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Col;
189e0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
189f0 20 6f 66 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e of extra column
18a00 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 63 68 s needed */. ch
18a10 61 72 20 2a 7a 45 78 74 72 61 20 3d 20 30 3b 20 ar *zExtra = 0;
18a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18a30 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61 66 * Extra space af
18a40 74 65 72 20 74 68 65 20 49 6e 64 65 78 20 6f 62 ter the Index ob
18a50 6a 65 63 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 ject */. Index
18a60 2a 70 50 6b 20 3d 20 30 3b 20 20 20 20 20 20 2f *pPk = 0; /
18a70 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e * PRIMARY KEY in
18a80 64 65 78 20 66 6f 72 20 57 49 54 48 4f 55 54 20 dex for WITHOUT
18a90 52 4f 57 49 44 20 74 61 62 6c 65 73 20 2a 2f 0a ROWID tables */.
18aa0 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
18ab0 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73 cFailed || pPars
18ac0 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 20 20 e->nErr>0 ){.
18ad0 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 goto exit_creat
18ae0 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 e_index;. }. i
18af0 66 28 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 f( IN_DECLARE_VT
18b00 41 42 20 26 26 20 69 64 78 54 79 70 65 21 3d 53 AB && idxType!=S
18b10 51 4c 49 54 45 5f 49 44 58 54 59 50 45 5f 50 52 QLITE_IDXTYPE_PR
18b20 49 4d 41 52 59 4b 45 59 20 29 7b 0a 20 20 20 20 IMARYKEY ){.
18b30 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
18b40 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 66 _index;. }. if
18b50 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c ( SQLITE_OK!=sql
18b60 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 ite3ReadSchema(p
18b70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 67 6f Parse) ){. go
18b80 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
18b90 6e 64 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a ndex;. }.. /*.
18ba0 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 74 61 ** Find the ta
18bb0 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 ble that is to b
18bc0 65 20 69 6e 64 65 78 65 64 2e 20 20 52 65 74 75 e indexed. Retu
18bd0 72 6e 20 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 rn early if not
18be0 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 found.. */. if
18bf0 28 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b ( pTblName!=0 ){
18c00 0a 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 .. /* Use the
18c10 20 74 77 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 two-part index
18c20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e name to determin
18c30 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a e the database .
18c40 20 20 20 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 ** to search
18c50 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 for the table.
18c60 27 46 69 78 27 20 74 68 65 20 74 61 62 6c 65 20 'Fix' the table
18c70 6e 61 6d 65 20 74 6f 20 74 68 69 73 20 64 62 0a name to this db.
18c80 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f ** before lo
18c90 6f 6b 69 6e 67 20 75 70 20 74 68 65 20 74 61 62 oking up the tab
18ca0 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 le.. */. a
18cb0 73 73 65 72 74 28 20 70 4e 61 6d 65 31 20 26 26 ssert( pName1 &&
18cc0 20 70 4e 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 pName2 );. i
18cd0 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 Db = sqlite3TwoP
18ce0 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 artName(pParse,
18cf0 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 pName1, pName2,
18d00 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 &pName);. if(
18d10 20 69 44 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 iDb<0 ) goto ex
18d20 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b it_create_index;
18d30 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 . assert( pNa
18d40 6d 65 20 26 26 20 70 4e 61 6d 65 2d 3e 7a 20 29 me && pName->z )
18d50 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
18d60 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 E_OMIT_TEMPDB.
18d70 20 20 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 /* If the inde
18d80 78 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61 x name was unqua
18d90 6c 69 66 69 65 64 2c 20 63 68 65 63 6b 20 69 66 lified, check if
18da0 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a the table. *
18db0 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c * is a temp tabl
18dc0 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68 e. If so, set th
18dd0 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 31 2e e database to 1.
18de0 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 0a Do not do this.
18df0 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 69 61 ** if initia
18e00 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62 61 73 lising a databas
18e10 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f e schema.. */
18e20 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e . if( !db->in
18e30 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 it.busy ){.
18e40 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 pTab = sqlite3S
18e50 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 rcListLookup(pPa
18e60 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a rse, pTblName);.
18e70 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 if( pName2
18e80 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26 ->n==0 && pTab &
18e90 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d & pTab->pSchema=
18ea0 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 =db->aDb[1].pSch
18eb0 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ema ){. i
18ec0 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a Db = 1;. }.
18ed0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
18ee0 20 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 sqlite3FixInit
18ef0 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 (&sFix, pParse,
18f00 69 44 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e iDb, "index", pN
18f10 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 73 71 ame);. if( sq
18f20 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 lite3FixSrcList(
18f30 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65 29 &sFix, pTblName)
18f40 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 42 65 63 ){. /* Bec
18f50 61 75 73 65 20 74 68 65 20 70 61 72 73 65 72 20 ause the parser
18f60 63 6f 6e 73 74 72 75 63 74 73 20 70 54 62 6c 4e constructs pTblN
18f70 61 6d 65 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c ame from a singl
18f80 65 20 69 64 65 6e 74 69 66 69 65 72 2c 0a 20 20 e identifier,.
18f90 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 46 69 ** sqlite3Fi
18fa0 78 53 72 63 4c 69 73 74 20 63 61 6e 20 6e 65 76 xSrcList can nev
18fb0 65 72 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 er fail. */.
18fc0 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20 assert(0);.
18fd0 20 7d 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 }. pTab = sq
18fe0 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
18ff0 49 74 65 6d 28 70 50 61 72 73 65 2c 20 30 2c 20 Item(pParse, 0,
19000 26 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 29 &pTblName->a[0])
19010 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 ;. assert( db
19020 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d ->mallocFailed==
19030 30 20 7c 7c 20 70 54 61 62 3d 3d 30 20 29 3b 0a 0 || pTab==0 );.
19040 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 if( pTab==0
19050 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 ) goto exit_crea
19060 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 66 te_index;. if
19070 28 20 69 44 62 3d 3d 31 20 26 26 20 64 62 2d 3e ( iDb==1 && db->
19080 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 aDb[iDb].pSchema
19090 21 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 !=pTab->pSchema
190a0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
190b0 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
190c0 20 0a 20 20 20 20 20 20 20 20 20 20 20 22 63 61 . "ca
190d0 6e 6e 6f 74 20 63 72 65 61 74 65 20 61 20 54 45 nnot create a TE
190e0 4d 50 20 69 6e 64 65 78 20 6f 6e 20 6e 6f 6e 2d MP index on non-
190f0 54 45 4d 50 20 74 61 62 6c 65 20 5c 22 25 73 5c TEMP table \"%s\
19100 22 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 "",. p
19110 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 Tab->zName);.
19120 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
19130 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d ate_index;. }
19140 0a 20 20 20 20 69 66 28 20 21 48 61 73 52 6f 77 . if( !HasRow
19150 69 64 28 70 54 61 62 29 20 29 20 70 50 6b 20 3d id(pTab) ) pPk =
19160 20 73 71 6c 69 74 65 33 50 72 69 6d 61 72 79 4b sqlite3PrimaryK
19170 65 79 49 6e 64 65 78 28 70 54 61 62 29 3b 0a 20 eyIndex(pTab);.
19180 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
19190 72 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29 3b 0a rt( pName==0 );.
191a0 20 20 20 20 61 73 73 65 72 74 28 20 70 53 74 61 assert( pSta
191b0 72 74 3d 3d 30 20 29 3b 0a 20 20 20 20 70 54 61 rt==0 );. pTa
191c0 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 b = pParse->pNew
191d0 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28 20 21 Table;. if( !
191e0 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 pTab ) goto exit
191f0 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
19200 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 iDb = sqlite3
19210 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
19220 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 , pTab->pSchema)
19230 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20 26 64 ;. }. pDb = &d
19240 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 b->aDb[iDb];..
19250 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 assert( pTab!=0
19260 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
19270 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 0a rse->nErr==0 );.
19280 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 if( sqlite3Str
19290 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d NICmp(pTab->zNam
192a0 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 e, "sqlite_", 7)
192b0 3d 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20 64 ==0 . && d
192c0 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 0a b->init.busy==0.
192d0 23 69 66 20 53 51 4c 49 54 45 5f 55 53 45 52 5f #if SQLITE_USER_
192e0 41 55 54 48 45 4e 54 49 43 41 54 49 4f 4e 0a 20 AUTHENTICATION.
192f0 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
19300 55 73 65 72 41 75 74 68 54 61 62 6c 65 28 70 54 UserAuthTable(pT
19310 61 62 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 0a 23 65 ab->zName)==0.#e
19320 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
19330 54 45 5f 41 4c 4c 4f 57 5f 53 51 4c 49 54 45 5f TE_ALLOW_SQLITE_
19340 4d 41 53 54 45 52 5f 49 4e 44 45 58 0a 20 20 20 MASTER_INDEX.
19350 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 53 74 && sqlite3St
19360 72 49 43 6d 70 28 26 70 54 61 62 2d 3e 7a 4e 61 rICmp(&pTab->zNa
19370 6d 65 5b 37 5d 2c 22 6d 61 73 74 65 72 22 29 21 me[7],"master")!
19380 3d 30 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 =0.#endif.
19390 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 && sqlite3StrNI
193a0 43 6d 70 28 26 70 54 61 62 2d 3e 7a 4e 61 6d 65 Cmp(&pTab->zName
193b0 5b 37 5d 2c 22 61 6c 74 65 72 74 61 62 5f 22 2c [7],"altertab_",
193c0 39 29 21 3d 30 0a 20 29 7b 0a 20 20 20 20 73 71 9)!=0. ){. sq
193d0 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
193e0 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 arse, "table %s
193f0 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 may not be index
19400 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 ed", pTab->zName
19410 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 );. goto exit
19420 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
19430 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
19440 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 E_OMIT_VIEW. if
19450 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 ( pTab->pSelect
19460 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
19470 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
19480 76 69 65 77 73 20 6d 61 79 20 6e 6f 74 20 62 65 views may not be
19490 20 69 6e 64 65 78 65 64 22 29 3b 0a 20 20 20 20 indexed");.
194a0 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
194b0 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64 _index;. }.#end
194c0 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 if.#ifndef SQLIT
194d0 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
194e0 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 BLE. if( IsVirt
194f0 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 ual(pTab) ){.
19500 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
19510 28 70 50 61 72 73 65 2c 20 22 76 69 72 74 75 61 (pParse, "virtua
19520 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 l tables may not
19530 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a 20 be indexed");.
19540 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
19550 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 ate_index;. }.#
19560 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a endif.. /*. **
19570 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f Find the name o
19580 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 4d 61 f the index. Ma
19590 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73 ke sure there is
195a0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 6e 6f not already ano
195b0 74 68 65 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20 ther. ** index
195c0 6f 72 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 or table with th
195d0 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 20 0a 20 e same name. .
195e0 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 69 **. ** Excepti
195f0 6f 6e 3a 20 20 49 66 20 77 65 20 61 72 65 20 72 on: If we are r
19600 65 61 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 73 eading the names
19610 20 6f 66 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e of permanent in
19620 64 69 63 65 73 20 66 72 6f 6d 20 74 68 65 0a 20 dices from the.
19630 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 ** sqlite_maste
19640 72 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 65 r table (because
19650 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
19660 65 73 73 20 63 68 61 6e 67 65 64 20 74 68 65 20 ess changed the
19670 73 63 68 65 6d 61 29 20 61 6e 64 0a 20 20 2a 2a schema) and. **
19680 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 64 65 one of the inde
19690 78 20 6e 61 6d 65 73 20 63 6f 6c 6c 69 64 65 73 x names collides
196a0 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 20 6f with the name o
196b0 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 f a temporary ta
196c0 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65 ble or. ** inde
196d0 78 2c 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 x, then we will
196e0 63 6f 6e 74 69 6e 75 65 20 74 6f 20 70 72 6f 63 continue to proc
196f0 65 73 73 20 74 68 69 73 20 69 6e 64 65 78 2e 0a ess this index..
19700 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 4e 61 **. ** If pNa
19710 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 me==0 it means t
19720 68 61 74 20 77 65 20 61 72 65 0a 20 20 2a 2a 20 hat we are. **
19730 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 70 dealing with a p
19740 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 55 4e rimary key or UN
19750 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e IQUE constraint.
19760 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76 We have to inv
19770 65 6e 74 20 6f 75 72 0a 20 20 2a 2a 20 6f 77 6e ent our. ** own
19780 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 name.. */. if
19790 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a ( pName ){. z
197a0 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 Name = sqlite3Na
197b0 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 meFromToken(db,
197c0 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 pName);. if(
197d0 7a 4e 61 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20 zName==0 ) goto
197e0 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
197f0 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 x;. assert( p
19800 4e 61 6d 65 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 Name->z!=0 );.
19810 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
19820 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a =sqlite3CheckObj
19830 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 ectName(pParse,
19840 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 zName) ){.
19850 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
19860 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 _index;. }.
19870 20 20 69 66 28 20 21 49 4e 5f 52 45 4e 41 4d 45 if( !IN_RENAME
19880 5f 4f 42 4a 45 43 54 20 29 7b 0a 20 20 20 20 20 _OBJECT ){.
19890 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 if( !db->init.b
198a0 75 73 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69 usy ){. i
198b0 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 f( sqlite3FindTa
198c0 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 30 ble(db, zName, 0
198d0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
198e0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
198f0 67 28 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 g(pParse, "there
19900 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 74 61 is already a ta
19910 62 6c 65 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a ble named %s", z
19920 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Name);.
19930 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 goto exit_creat
19940 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 20 e_index;.
19950 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
19960 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 if( sqlite3Find
19970 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c Index(db, zName,
19980 20 70 44 62 2d 3e 7a 44 62 53 4e 61 6d 65 29 21 pDb->zDbSName)!
19990 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 =0 ){. if
199a0 28 20 21 69 66 4e 6f 74 45 78 69 73 74 20 29 7b ( !ifNotExist ){
199b0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
199c0 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
199d0 65 2c 20 22 69 6e 64 65 78 20 25 73 20 61 6c 72 e, "index %s alr
199e0 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 7a 4e eady exists", zN
199f0 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 ame);. }e
19a00 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 lse{. a
19a10 73 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 ssert( !db->init
19a20 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 20 20 20 .busy );.
19a30 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 sqlite3CodeVe
19a40 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 rifySchema(pPars
19a50 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 e, iDb);.
19a60 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
19a70 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
19a80 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d x;. }. }
19a90 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
19aa0 74 20 6e 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a t n;. Index *
19ab0 70 4c 6f 6f 70 3b 0a 20 20 20 20 66 6f 72 28 70 pLoop;. for(p
19ac0 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70 49 6e 64 65 Loop=pTab->pInde
19ad0 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f 70 3b 20 70 x, n=1; pLoop; p
19ae0 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 4e 65 78 Loop=pLoop->pNex
19af0 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 7a 4e t, n++){}. zN
19b00 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 ame = sqlite3MPr
19b10 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65 intf(db, "sqlite
19b20 5f 61 75 74 6f 69 6e 64 65 78 5f 25 73 5f 25 64 _autoindex_%s_%d
19b30 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 ", pTab->zName,
19b40 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d n);. if( zNam
19b50 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f e==0 ){. go
19b60 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
19b70 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ndex;. }..
19b80 20 2f 2a 20 41 75 74 6f 6d 61 74 69 63 20 69 6e /* Automatic in
19b90 64 65 78 20 6e 61 6d 65 73 20 67 65 6e 65 72 61 dex names genera
19ba0 74 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 ted from within
19bb0 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f sqlite3_declare_
19bc0 76 74 61 62 28 29 0a 20 20 20 20 2a 2a 20 6d 75 vtab(). ** mu
19bd0 73 74 20 68 61 76 65 20 6e 61 6d 65 73 20 74 68 st have names th
19be0 61 74 20 61 72 65 20 64 69 73 74 69 6e 63 74 20 at are distinct
19bf0 66 72 6f 6d 20 6e 6f 72 6d 61 6c 20 61 75 74 6f from normal auto
19c00 6d 61 74 69 63 20 69 6e 64 65 78 20 6e 61 6d 65 matic index name
19c10 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f s.. ** The fo
19c20 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e llowing statemen
19c30 74 20 63 6f 6e 76 65 72 74 73 20 22 73 71 6c 69 t converts "sqli
19c40 74 65 33 5f 61 75 74 6f 69 6e 64 65 78 2e 2e 2e te3_autoindex...
19c50 22 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 22 73 " into. ** "s
19c60 71 6c 69 74 65 33 5f 62 75 74 6f 69 6e 64 65 78 qlite3_butoindex
19c70 2e 2e 2e 22 20 69 6e 20 6f 72 64 65 72 20 74 6f ..." in order to
19c80 20 6d 61 6b 65 20 74 68 65 20 6e 61 6d 65 73 20 make the names
19c90 64 69 73 74 69 6e 63 74 2e 0a 20 20 20 20 2a 2a distinct.. **
19ca0 20 54 68 65 20 22 76 74 61 62 5f 65 72 72 2e 74 The "vtab_err.t
19cb0 65 73 74 22 20 74 65 73 74 20 64 65 6d 6f 6e 73 est" test demons
19cc0 74 72 61 74 65 73 20 74 68 65 20 6e 65 65 64 20 trates the need
19cd0 6f 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e of this statemen
19ce0 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 4e t. */. if( IN
19cf0 5f 53 50 45 43 49 41 4c 5f 50 41 52 53 45 20 29 _SPECIAL_PARSE )
19d00 20 7a 4e 61 6d 65 5b 37 5d 2b 2b 3b 0a 20 20 7d zName[7]++;. }
19d10 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 .. /* Check for
19d20 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 authorization t
19d30 6f 20 63 72 65 61 74 65 20 61 6e 20 69 6e 64 65 o create an inde
19d40 78 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 x.. */.#ifndef
19d50 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
19d60 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 ORIZATION. if(
19d70 21 49 4e 5f 52 45 4e 41 4d 45 5f 4f 42 4a 45 43 !IN_RENAME_OBJEC
19d80 54 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 T ){. const c
19d90 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d 3e har *zDb = pDb->
19da0 7a 44 62 53 4e 61 6d 65 3b 0a 20 20 20 20 69 66 zDbSName;. if
19db0 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 ( sqlite3AuthChe
19dc0 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ck(pParse, SQLIT
19dd0 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 E_INSERT, SCHEMA
19de0 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 _TABLE(iDb), 0,
19df0 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f zDb) ){. go
19e00 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
19e10 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ndex;. }.
19e20 69 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 i = SQLITE_CREAT
19e30 45 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 E_INDEX;. if(
19e40 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 !OMIT_TEMPDB &&
19e50 20 69 44 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 iDb==1 ) i = SQ
19e60 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 LITE_CREATE_TEMP
19e70 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 _INDEX;. if(
19e80 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b sqlite3AuthCheck
19e90 28 70 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d (pParse, i, zNam
19ea0 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 e, pTab->zName,
19eb0 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f zDb) ){. go
19ec0 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
19ed0 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ndex;. }. }.
19ee0 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 #endif.. /* If
19ef0 70 4c 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 pList==0, it mea
19f00 6e 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ns this routine
19f10 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 was called to ma
19f20 6b 65 20 61 20 70 72 69 6d 61 72 79 0a 20 20 2a ke a primary. *
19f30 2a 20 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65 * key out of the
19f40 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 last column add
19f50 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 ed to the table
19f60 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 under constructi
19f70 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 on.. ** So crea
19f80 74 65 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74 te a fake list t
19f90 6f 20 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e o simulate this.
19fa0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 . */. if( pLis
19fb0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 54 6f 6b 65 t==0 ){. Toke
19fc0 6e 20 70 72 65 76 43 6f 6c 3b 0a 20 20 20 20 43 n prevCol;. C
19fd0 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 olumn *pCol = &p
19fe0 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e Tab->aCol[pTab->
19ff0 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 20 20 70 43 6f nCol-1];. pCo
1a000 6c 2d 3e 63 6f 6c 46 6c 61 67 73 20 7c 3d 20 43 l->colFlags |= C
1a010 4f 4c 46 4c 41 47 5f 55 4e 49 51 55 45 3b 0a 20 OLFLAG_UNIQUE;.
1a020 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 49 sqlite3TokenI
1a030 6e 69 74 28 26 70 72 65 76 43 6f 6c 2c 20 70 43 nit(&prevCol, pC
1a040 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ol->zName);.
1a050 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 pList = sqlite3E
1a060 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 xprListAppend(pP
1a070 61 72 73 65 2c 20 30 2c 0a 20 20 20 20 20 20 20 arse, 0,.
1a080 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
1a090 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 49 prAlloc(db, TK_I
1a0a0 44 2c 20 26 70 72 65 76 43 6f 6c 2c 20 30 29 29 D, &prevCol, 0))
1a0b0 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d ;. if( pList=
1a0c0 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 =0 ) goto exit_c
1a0d0 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 reate_index;.
1a0e0 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e assert( pList->
1a0f0 6e 45 78 70 72 3d 3d 31 20 29 3b 0a 20 20 20 20 nExpr==1 );.
1a100 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 sqlite3ExprListS
1a110 65 74 53 6f 72 74 4f 72 64 65 72 28 70 4c 69 73 etSortOrder(pLis
1a120 74 2c 20 73 6f 72 74 4f 72 64 65 72 29 3b 0a 20 t, sortOrder);.
1a130 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
1a140 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b te3ExprListCheck
1a150 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20 70 Length(pParse, p
1a160 4c 69 73 74 2c 20 22 69 6e 64 65 78 22 29 3b 0a List, "index");.
1a170 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 }.. /* Figure
1a180 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 out how many by
1a190 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 72 65 tes of space are
1a1a0 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
1a1b0 72 65 20 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 re explicitly.
1a1c0 2a 2a 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c ** specified col
1a1d0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
1a1e0 6e 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f names.. */. fo
1a1f0 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e r(i=0; i<pList->
1a200 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
1a210 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 Expr *pExpr = p
1a220 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 List->a[i].pExpr
1a230 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 ;. assert( pE
1a240 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 xpr!=0 );. if
1a250 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
1a260 43 4f 4c 4c 41 54 45 20 29 7b 0a 20 20 20 20 20 COLLATE ){.
1a270 20 6e 45 78 74 72 61 20 2b 3d 20 28 31 20 2b 20 nExtra += (1 +
1a280 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1a290 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 pExpr->u.zToken)
1a2a0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
1a2b0 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 /* . ** Allocat
1a2c0 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 75 e the index stru
1a2d0 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e cture. . */. n
1a2e0 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 Name = sqlite3St
1a2f0 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 rlen30(zName);.
1a300 20 6e 45 78 74 72 61 43 6f 6c 20 3d 20 70 50 6b nExtraCol = pPk
1a310 20 3f 20 70 50 6b 2d 3e 6e 4b 65 79 43 6f 6c 20 ? pPk->nKeyCol
1a320 3a 20 31 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 : 1;. pIndex =
1a330 73 71 6c 69 74 65 33 41 6c 6c 6f 63 61 74 65 49 sqlite3AllocateI
1a340 6e 64 65 78 4f 62 6a 65 63 74 28 64 62 2c 20 70 ndexObject(db, p
1a350 4c 69 73 74 2d 3e 6e 45 78 70 72 20 2b 20 6e 45 List->nExpr + nE
1a360 78 74 72 61 43 6f 6c 2c 0a 20 20 20 20 20 20 20 xtraCol,.
1a370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e n
1a390 4e 61 6d 65 20 2b 20 6e 45 78 74 72 61 20 2b 20 Name + nExtra +
1a3a0 31 2c 20 26 7a 45 78 74 72 61 29 3b 0a 20 20 69 1, &zExtra);. i
1a3b0 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1a3c0 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 led ){. goto
1a3d0 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
1a3e0 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 x;. }. assert(
1a3f0 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
1a400 4e 4d 45 4e 54 28 70 49 6e 64 65 78 2d 3e 61 69 NMENT(pIndex->ai
1a410 52 6f 77 4c 6f 67 45 73 74 29 20 29 3b 0a 20 20 RowLogEst) );.
1a420 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
1a430 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 49 6e TE_ALIGNMENT(pIn
1a440 64 65 78 2d 3e 61 7a 43 6f 6c 6c 29 20 29 3b 0a dex->azColl) );.
1a450 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 20 pIndex->zName
1a460 3d 20 7a 45 78 74 72 61 3b 0a 20 20 7a 45 78 74 = zExtra;. zExt
1a470 72 61 20 2b 3d 20 6e 4e 61 6d 65 20 2b 20 31 3b ra += nName + 1;
1a480 0a 20 20 6d 65 6d 63 70 79 28 70 49 6e 64 65 78 . memcpy(pIndex
1a490 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 ->zName, zName,
1a4a0 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 70 49 6e 64 nName+1);. pInd
1a4b0 65 78 2d 3e 70 54 61 62 6c 65 20 3d 20 70 54 61 ex->pTable = pTa
1a4c0 62 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 b;. pIndex->onE
1a4d0 72 72 6f 72 20 3d 20 28 75 38 29 6f 6e 45 72 72 rror = (u8)onErr
1a4e0 6f 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 75 6e or;. pIndex->un
1a4f0 69 71 4e 6f 74 4e 75 6c 6c 20 3d 20 6f 6e 45 72 iqNotNull = onEr
1a500 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 3b 0a 20 20 ror!=OE_None;.
1a510 70 49 6e 64 65 78 2d 3e 69 64 78 54 79 70 65 20 pIndex->idxType
1a520 3d 20 69 64 78 54 79 70 65 3b 0a 20 20 70 49 6e = idxType;. pIn
1a530 64 65 78 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 dex->pSchema = d
1a540 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 b->aDb[iDb].pSch
1a550 65 6d 61 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6e ema;. pIndex->n
1a560 4b 65 79 43 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e KeyCol = pList->
1a570 6e 45 78 70 72 3b 0a 20 20 69 66 28 20 70 50 49 nExpr;. if( pPI
1a580 57 68 65 72 65 20 29 7b 0a 20 20 20 20 73 71 6c Where ){. sql
1a590 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 66 52 ite3ResolveSelfR
1a5a0 65 66 65 72 65 6e 63 65 28 70 50 61 72 73 65 2c eference(pParse,
1a5b0 20 70 54 61 62 2c 20 4e 43 5f 50 61 72 74 49 64 pTab, NC_PartId
1a5c0 78 2c 20 70 50 49 57 68 65 72 65 2c 20 30 29 3b x, pPIWhere, 0);
1a5d0 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 50 61 . pIndex->pPa
1a5e0 72 74 49 64 78 57 68 65 72 65 20 3d 20 70 50 49 rtIdxWhere = pPI
1a5f0 57 68 65 72 65 3b 0a 20 20 20 20 70 50 49 57 68 Where;. pPIWh
1a600 65 72 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 ere = 0;. }. a
1a610 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 ssert( sqlite3Sc
1a620 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 hemaMutexHeld(db
1a630 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 0a 20 20 , iDb, 0) );..
1a640 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 /* Check to see
1a650 69 66 20 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e if we should hon
1a660 6f 72 20 44 45 53 43 20 72 65 71 75 65 73 74 73 or DESC requests
1a670 20 6f 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e on index column
1a680 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 62 s. */. if( pDb
1a690 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f ->pSchema->file_
1a6a0 66 6f 72 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20 format>=4 ){.
1a6b0 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d sortOrderMask =
1a6c0 20 2d 31 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20 -1; /* Honor
1a6d0 44 45 53 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b DESC */. }else{
1a6e0 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 . sortOrderMa
1a6f0 73 6b 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67 sk = 0; /* Ig
1a700 6e 6f 72 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d nore DESC */. }
1a710 0a 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 74 .. /* Analyze t
1a720 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 he list of expre
1a730 73 73 69 6f 6e 73 20 74 68 61 74 20 66 6f 72 6d ssions that form
1a740 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 the terms of th
1a750 65 20 69 6e 64 65 78 20 61 6e 64 0a 20 20 2a 2a e index and. **
1a760 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f report any erro
1a770 72 73 2e 20 20 49 6e 20 74 68 65 20 63 6f 6d 6d rs. In the comm
1a780 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 74 68 on case where th
1a790 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
1a7a0 65 78 61 63 74 6c 79 0a 20 20 2a 2a 20 61 20 74 exactly. ** a t
1a7b0 61 62 6c 65 20 63 6f 6c 75 6d 6e 2c 20 73 74 6f able column, sto
1a7c0 72 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 re that column i
1a7d0 6e 20 61 69 43 6f 6c 75 6d 6e 5b 5d 2e 20 20 46 n aiColumn[]. F
1a7e0 6f 72 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65 or general expre
1a7f0 73 73 69 6f 6e 73 2c 0a 20 20 2a 2a 20 70 6f 70 ssions,. ** pop
1a800 75 6c 61 74 65 20 70 49 6e 64 65 78 2d 3e 61 43 ulate pIndex->aC
1a810 6f 6c 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65 olExpr and store
1a820 20 58 4e 5f 45 58 50 52 20 28 2d 32 29 20 69 6e XN_EXPR (-2) in
1a830 20 61 69 43 6f 6c 75 6d 6e 5b 5d 2e 0a 20 20 2a aiColumn[].. *
1a840 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 49 73 73 *. ** TODO: Iss
1a850 75 65 20 61 20 77 61 72 6e 69 6e 67 20 69 66 20 ue a warning if
1a860 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6c 75 two or more colu
1a870 6d 6e 73 20 6f 66 20 74 68 65 20 69 6e 64 65 78 mns of the index
1a880 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c 2e 0a are identical..
1a890 20 20 2a 2a 20 54 4f 44 4f 3a 20 49 73 73 75 65 ** TODO: Issue
1a8a0 20 61 20 77 61 72 6e 69 6e 67 20 69 66 20 74 68 a warning if th
1a8b0 65 20 74 61 62 6c 65 20 70 72 69 6d 61 72 79 20 e table primary
1a8c0 6b 65 79 20 69 73 20 75 73 65 64 20 61 73 20 70 key is used as p
1a8d0 61 72 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 art of the. **
1a8e0 69 6e 64 65 78 20 6b 65 79 2e 0a 20 20 2a 2f 0a index key.. */.
1a8f0 20 20 70 4c 69 73 74 49 74 65 6d 20 3d 20 70 4c pListItem = pL
1a900 69 73 74 2d 3e 61 3b 0a 20 20 69 66 28 20 49 4e ist->a;. if( IN
1a910 5f 52 45 4e 41 4d 45 5f 4f 42 4a 45 43 54 20 29 _RENAME_OBJECT )
1a920 7b 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 43 {. pIndex->aC
1a930 6f 6c 45 78 70 72 20 3d 20 70 4c 69 73 74 3b 0a olExpr = pList;.
1a940 20 20 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 pList = 0;.
1a950 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
1a960 70 49 6e 64 65 78 2d 3e 6e 4b 65 79 43 6f 6c 3b pIndex->nKeyCol;
1a970 20 69 2b 2b 2c 20 70 4c 69 73 74 49 74 65 6d 2b i++, pListItem+
1a980 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 43 +){. Expr *pC
1a990 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
1a9a0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d /* The i-
1a9b0 74 68 20 69 6e 64 65 78 20 65 78 70 72 65 73 73 th index express
1a9c0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 ion */. int r
1a9d0 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 equestedSortOrde
1a9e0 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 53 43 r; /* ASC
1a9f0 20 6f 72 20 44 45 53 43 20 6f 6e 20 74 68 65 20 or DESC on the
1aa00 69 2d 74 68 20 65 78 70 72 65 73 73 69 6f 6e 20 i-th expression
1aa10 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 */. const cha
1aa20 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 r *zColl;
1aa30 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 /* Collati
1aa40 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 on sequence name
1aa50 20 2a 2f 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 */.. sqlite3
1aa60 53 74 72 69 6e 67 54 6f 49 64 28 70 4c 69 73 74 StringToId(pList
1aa70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 Item->pExpr);.
1aa80 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 sqlite3Resolve
1aa90 53 65 6c 66 52 65 66 65 72 65 6e 63 65 28 70 50 SelfReference(pP
1aaa0 61 72 73 65 2c 20 70 54 61 62 2c 20 4e 43 5f 49 arse, pTab, NC_I
1aab0 64 78 45 78 70 72 2c 20 70 4c 69 73 74 49 74 65 dxExpr, pListIte
1aac0 6d 2d 3e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 m->pExpr, 0);.
1aad0 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 if( pParse->nE
1aae0 72 72 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 rr ) goto exit_c
1aaf0 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 reate_index;.
1ab00 20 70 43 45 78 70 72 20 3d 20 73 71 6c 69 74 65 pCExpr = sqlite
1ab10 33 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 74 65 3ExprSkipCollate
1ab20 28 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 (pListItem->pExp
1ab30 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 45 78 r);. if( pCEx
1ab40 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d pr->op!=TK_COLUM
1ab50 4e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 N ){. if( p
1ab60 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e 65 Tab==pParse->pNe
1ab70 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 wTable ){.
1ab80 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1ab90 67 28 70 50 61 72 73 65 2c 20 22 65 78 70 72 65 g(pParse, "expre
1aba0 73 73 69 6f 6e 73 20 70 72 6f 68 69 62 69 74 65 ssions prohibite
1abb0 64 20 69 6e 20 50 52 49 4d 41 52 59 20 4b 45 59 d in PRIMARY KEY
1abc0 20 61 6e 64 20 22 0a 20 20 20 20 20 20 20 20 20 and ".
1abd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1abe0 20 20 20 20 20 20 20 22 55 4e 49 51 55 45 20 63 "UNIQUE c
1abf0 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 onstraints");.
1ac00 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f goto exit_
1ac10 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
1ac20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1ac30 70 49 6e 64 65 78 2d 3e 61 43 6f 6c 45 78 70 72 pIndex->aColExpr
1ac40 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
1ac50 49 6e 64 65 78 2d 3e 61 43 6f 6c 45 78 70 72 20 Index->aColExpr
1ac60 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 = pList;.
1ac70 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 pList = 0;.
1ac80 20 20 7d 0a 20 20 20 20 20 20 6a 20 3d 20 58 4e }. j = XN
1ac90 5f 45 58 50 52 3b 0a 20 20 20 20 20 20 70 49 6e _EXPR;. pIn
1aca0 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d dex->aiColumn[i]
1acb0 20 3d 20 58 4e 5f 45 58 50 52 3b 0a 20 20 20 20 = XN_EXPR;.
1acc0 20 20 70 49 6e 64 65 78 2d 3e 75 6e 69 71 4e 6f pIndex->uniqNo
1acd0 74 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 7d tNull = 0;. }
1ace0 65 6c 73 65 7b 0a 20 20 20 20 20 20 6a 20 3d 20 else{. j =
1acf0 70 43 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b pCExpr->iColumn;
1ad00 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a . assert( j
1ad10 3c 3d 30 78 37 66 66 66 20 29 3b 0a 20 20 20 20 <=0x7fff );.
1ad20 20 20 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20 if( j<0 ){.
1ad30 20 20 20 20 20 6a 20 3d 20 70 54 61 62 2d 3e 69 j = pTab->i
1ad40 50 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 PKey;. }els
1ad50 65 20 69 66 28 20 70 54 61 62 2d 3e 61 43 6f 6c e if( pTab->aCol
1ad60 5b 6a 5d 2e 6e 6f 74 4e 75 6c 6c 3d 3d 30 20 29 [j].notNull==0 )
1ad70 7b 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 {. pIndex
1ad80 2d 3e 75 6e 69 71 4e 6f 74 4e 75 6c 6c 20 3d 20 ->uniqNotNull =
1ad90 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
1ada0 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d pIndex->aiColum
1adb0 6e 5b 69 5d 20 3d 20 28 69 31 36 29 6a 3b 0a 20 n[i] = (i16)j;.
1adc0 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d }. zColl =
1add0 20 30 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 0;. if( pLis
1ade0 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 6f 70 tItem->pExpr->op
1adf0 3d 3d 54 4b 5f 43 4f 4c 4c 41 54 45 20 29 7b 0a ==TK_COLLATE ){.
1ae00 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 6c 3b int nColl;
1ae10 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 . zColl = p
1ae20 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d ListItem->pExpr-
1ae30 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 >u.zToken;.
1ae40 20 6e 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 nColl = sqlite3
1ae50 53 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 6c 29 20 Strlen30(zColl)
1ae60 2b 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 + 1;. asser
1ae70 74 28 20 6e 45 78 74 72 61 3e 3d 6e 43 6f 6c 6c t( nExtra>=nColl
1ae80 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 );. memcpy
1ae90 28 7a 45 78 74 72 61 2c 20 7a 43 6f 6c 6c 2c 20 (zExtra, zColl,
1aea0 6e 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 7a 43 nColl);. zC
1aeb0 6f 6c 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 oll = zExtra;.
1aec0 20 20 20 20 7a 45 78 74 72 61 20 2b 3d 20 6e 43 zExtra += nC
1aed0 6f 6c 6c 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 oll;. nExtr
1aee0 61 20 2d 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 a -= nColl;.
1aef0 7d 65 6c 73 65 20 69 66 28 20 6a 3e 3d 30 20 29 }else if( j>=0 )
1af00 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 {. zColl =
1af10 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 pTab->aCol[j].zC
1af20 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 oll;. }. i
1af30 66 28 20 21 7a 43 6f 6c 6c 20 29 20 7a 43 6f 6c f( !zColl ) zCol
1af40 6c 20 3d 20 73 71 6c 69 74 65 33 53 74 72 42 49 l = sqlite3StrBI
1af50 4e 41 52 59 3b 0a 20 20 20 20 69 66 28 20 21 64 NARY;. if( !d
1af60 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 b->init.busy &&
1af70 21 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f !sqlite3LocateCo
1af80 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 llSeq(pParse, zC
1af90 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 67 6f oll) ){. go
1afa0 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
1afb0 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ndex;. }.
1afc0 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 pIndex->azColl[i
1afd0 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 20 20 72 ] = zColl;. r
1afe0 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 equestedSortOrde
1aff0 72 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 73 r = pListItem->s
1b000 6f 72 74 4f 72 64 65 72 20 26 20 73 6f 72 74 4f ortOrder & sortO
1b010 72 64 65 72 4d 61 73 6b 3b 0a 20 20 20 20 70 49 rderMask;. pI
1b020 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 ndex->aSortOrder
1b030 5b 69 5d 20 3d 20 28 75 38 29 72 65 71 75 65 73 [i] = (u8)reques
1b040 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 tedSortOrder;.
1b050 7d 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 }.. /* Append t
1b060 68 65 20 74 61 62 6c 65 20 6b 65 79 20 74 6f 20 he table key to
1b070 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 the end of the i
1b080 6e 64 65 78 2e 20 20 46 6f 72 20 57 49 54 48 4f ndex. For WITHO
1b090 55 54 20 52 4f 57 49 44 0a 20 20 2a 2a 20 74 61 UT ROWID. ** ta
1b0a0 62 6c 65 73 20 28 77 68 65 6e 20 70 50 6b 21 3d bles (when pPk!=
1b0b0 30 29 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 0) this will be
1b0c0 74 68 65 20 64 65 63 6c 61 72 65 64 20 50 52 49 the declared PRI
1b0d0 4d 41 52 59 20 4b 45 59 2e 20 20 46 6f 72 0a 20 MARY KEY. For.
1b0e0 20 2a 2a 20 6e 6f 72 6d 61 6c 20 74 61 62 6c 65 ** normal table
1b0f0 73 20 28 77 68 65 6e 20 70 50 6b 3d 3d 30 29 20 s (when pPk==0)
1b100 74 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 this will be the
1b110 20 72 6f 77 69 64 2e 0a 20 20 2a 2f 0a 20 20 69 rowid.. */. i
1b120 66 28 20 70 50 6b 20 29 7b 0a 20 20 20 20 66 6f f( pPk ){. fo
1b130 72 28 6a 3d 30 3b 20 6a 3c 70 50 6b 2d 3e 6e 4b r(j=0; j<pPk->nK
1b140 65 79 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 eyCol; j++){.
1b150 20 20 20 69 6e 74 20 78 20 3d 20 70 50 6b 2d 3e int x = pPk->
1b160 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 aiColumn[j];.
1b170 20 20 20 61 73 73 65 72 74 28 20 78 3e 3d 30 20 assert( x>=0
1b180 29 3b 0a 20 20 20 20 20 20 69 66 28 20 68 61 73 );. if( has
1b190 43 6f 6c 75 6d 6e 28 70 49 6e 64 65 78 2d 3e 61 Column(pIndex->a
1b1a0 69 43 6f 6c 75 6d 6e 2c 20 70 49 6e 64 65 78 2d iColumn, pIndex-
1b1b0 3e 6e 4b 65 79 43 6f 6c 2c 20 78 29 20 29 7b 0a >nKeyCol, x) ){.
1b1c0 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e pIndex->
1b1d0 6e 43 6f 6c 75 6d 6e 2d 2d 3b 20 0a 20 20 20 20 nColumn--; .
1b1e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1b1f0 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d pIndex->aiColum
1b200 6e 5b 69 5d 20 3d 20 78 3b 0a 20 20 20 20 20 20 n[i] = x;.
1b210 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c pIndex->azColl
1b220 5b 69 5d 20 3d 20 70 50 6b 2d 3e 61 7a 43 6f 6c [i] = pPk->azCol
1b230 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 70 49 l[j];. pI
1b240 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 ndex->aSortOrder
1b250 5b 69 5d 20 3d 20 70 50 6b 2d 3e 61 53 6f 72 74 [i] = pPk->aSort
1b260 4f 72 64 65 72 5b 6a 5d 3b 0a 20 20 20 20 20 20 Order[j];.
1b270 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 i++;. }.
1b280 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1b290 20 69 3d 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c i==pIndex->nCol
1b2a0 75 6d 6e 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a umn );. }else{.
1b2b0 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f pIndex->aiCo
1b2c0 6c 75 6d 6e 5b 69 5d 20 3d 20 58 4e 5f 52 4f 57 lumn[i] = XN_ROW
1b2d0 49 44 3b 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e ID;. pIndex->
1b2e0 61 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69 azColl[i] = sqli
1b2f0 74 65 33 53 74 72 42 49 4e 41 52 59 3b 0a 20 20 te3StrBINARY;.
1b300 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 }. sqlite3Defau
1b310 6c 74 52 6f 77 45 73 74 28 70 49 6e 64 65 78 29 ltRowEst(pIndex)
1b320 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
1b330 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 20 65 pNewTable==0 ) e
1b340 73 74 69 6d 61 74 65 49 6e 64 65 78 57 69 64 74 stimateIndexWidt
1b350 68 28 70 49 6e 64 65 78 29 3b 0a 0a 20 20 2f 2a h(pIndex);.. /*
1b360 20 49 66 20 74 68 69 73 20 69 6e 64 65 78 20 63 If this index c
1b370 6f 6e 74 61 69 6e 73 20 65 76 65 72 79 20 63 6f ontains every co
1b380 6c 75 6d 6e 20 6f 66 20 69 74 73 20 74 61 62 6c lumn of its tabl
1b390 65 2c 20 74 68 65 6e 20 6d 61 72 6b 0a 20 20 2a e, then mark. *
1b3a0 2a 20 69 74 20 61 73 20 61 20 63 6f 76 65 72 69 * it as a coveri
1b3b0 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 61 73 ng index */. as
1b3c0 73 65 72 74 28 20 48 61 73 52 6f 77 69 64 28 70 sert( HasRowid(p
1b3d0 54 61 62 29 20 0a 20 20 20 20 20 20 7c 7c 20 70 Tab) . || p
1b3e0 54 61 62 2d 3e 69 50 4b 65 79 3c 30 20 7c 7c 20 Tab->iPKey<0 ||
1b3f0 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 4f 66 49 sqlite3ColumnOfI
1b400 6e 64 65 78 28 70 49 6e 64 65 78 2c 20 70 54 61 ndex(pIndex, pTa
1b410 62 2d 3e 69 50 4b 65 79 29 3e 3d 30 20 29 3b 0a b->iPKey)>=0 );.
1b420 20 20 72 65 63 6f 6d 70 75 74 65 43 6f 6c 75 6d recomputeColum
1b430 6e 73 4e 6f 74 49 6e 64 65 78 65 64 28 70 49 6e nsNotIndexed(pIn
1b440 64 65 78 29 3b 0a 20 20 69 66 28 20 70 54 62 6c dex);. if( pTbl
1b450 4e 61 6d 65 21 3d 30 20 26 26 20 70 49 6e 64 65 Name!=0 && pInde
1b460 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 70 54 61 62 x->nColumn>=pTab
1b470 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 70 49 ->nCol ){. pI
1b480 6e 64 65 78 2d 3e 69 73 43 6f 76 65 72 69 6e 67 ndex->isCovering
1b490 20 3d 20 31 3b 0a 20 20 20 20 66 6f 72 28 6a 3d = 1;. for(j=
1b4a0 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0; j<pTab->nCol;
1b4b0 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 j++){. if(
1b4c0 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 j==pTab->iPKey
1b4d0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1b4e0 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 6c if( sqlite3Col
1b4f0 75 6d 6e 4f 66 49 6e 64 65 78 28 70 49 6e 64 65 umnOfIndex(pInde
1b500 78 2c 6a 29 3e 3d 30 20 29 20 63 6f 6e 74 69 6e x,j)>=0 ) contin
1b510 75 65 3b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 ue;. pIndex
1b520 2d 3e 69 73 43 6f 76 65 72 69 6e 67 20 3d 20 30 ->isCovering = 0
1b530 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1b540 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
1b550 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e pTab==pParse->pN
1b560 65 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f ewTable ){. /
1b570 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 * This routine h
1b580 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 74 as been called t
1b590 6f 20 63 72 65 61 74 65 20 61 6e 20 61 75 74 6f o create an auto
1b5a0 6d 61 74 69 63 20 69 6e 64 65 78 20 61 73 20 61 matic index as a
1b5b0 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 20 6f . ** result o
1b5c0 66 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20 f a PRIMARY KEY
1b5d0 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73 65 or UNIQUE clause
1b5e0 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 64 65 66 on a column def
1b5f0 69 6e 69 74 69 6f 6e 2c 20 6f 72 0a 20 20 20 20 inition, or.
1b600 2a 2a 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 ** a PRIMARY KEY
1b610 20 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73 or UNIQUE claus
1b620 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 e following the
1b630 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f column definitio
1b640 6e 73 2e 0a 20 20 20 20 2a 2a 20 69 2e 65 2e 20 ns.. ** i.e.
1b650 6f 6e 65 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 one of:. **.
1b660 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 ** CREATE TAB
1b670 4c 45 20 74 28 78 20 50 52 49 4d 41 52 59 20 4b LE t(x PRIMARY K
1b680 45 59 2c 20 79 29 3b 0a 20 20 20 20 2a 2a 20 43 EY, y);. ** C
1b690 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 2c REATE TABLE t(x,
1b6a0 20 79 2c 20 55 4e 49 51 55 45 28 78 2c 20 79 29 y, UNIQUE(x, y)
1b6b0 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a );. **. **
1b6c0 20 45 69 74 68 65 72 20 77 61 79 2c 20 63 68 65 Either way, che
1b6d0 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 ck to see if the
1b6e0 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68 table already h
1b6f0 61 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65 78 as such an index
1b700 2e 20 49 66 0a 20 20 20 20 2a 2a 20 73 6f 2c 20 . If. ** so,
1b710 64 6f 6e 27 74 20 62 6f 74 68 65 72 20 63 72 65 don't bother cre
1b720 61 74 69 6e 67 20 74 68 69 73 20 6f 6e 65 2e 20 ating this one.
1b730 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 This only applie
1b740 73 20 74 6f 0a 20 20 20 20 2a 2a 20 61 75 74 6f s to. ** auto
1b750 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 matically create
1b760 64 20 69 6e 64 69 63 65 73 2e 20 55 73 65 72 73 d indices. Users
1b770 20 63 61 6e 20 64 6f 20 61 73 20 74 68 65 79 20 can do as they
1b780 77 69 73 68 20 77 69 74 68 0a 20 20 20 20 2a 2a wish with. **
1b790 20 65 78 70 6c 69 63 69 74 20 69 6e 64 69 63 65 explicit indice
1b7a0 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s.. **. **
1b7b0 20 54 77 6f 20 55 4e 49 51 55 45 20 6f 72 20 50 Two UNIQUE or P
1b7c0 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 RIMARY KEY const
1b7d0 72 61 69 6e 74 73 20 61 72 65 20 63 6f 6e 73 69 raints are consi
1b7e0 64 65 72 65 64 20 65 71 75 69 76 61 6c 65 6e 74 dered equivalent
1b7f0 0a 20 20 20 20 2a 2a 20 28 61 6e 64 20 74 68 75 . ** (and thu
1b800 73 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 s suppressing th
1b810 65 20 73 65 63 6f 6e 64 20 6f 6e 65 29 20 65 76 e second one) ev
1b820 65 6e 20 69 66 20 74 68 65 79 20 68 61 76 65 20 en if they have
1b830 64 69 66 66 65 72 65 6e 74 0a 20 20 20 20 2a 2a different. **
1b840 20 73 6f 72 74 20 6f 72 64 65 72 73 2e 0a 20 20 sort orders..
1b850 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 **. ** If t
1b860 68 65 72 65 20 61 72 65 20 64 69 66 66 65 72 65 here are differe
1b870 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 nt collating seq
1b880 75 65 6e 63 65 73 20 6f 72 20 69 66 20 74 68 65 uences or if the
1b890 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 20 20 columns of.
1b8a0 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e ** the constrain
1b8b0 74 20 6f 63 63 75 72 20 69 6e 20 64 69 66 66 65 t occur in diffe
1b8c0 72 65 6e 74 20 6f 72 64 65 72 73 2c 20 74 68 65 rent orders, the
1b8d0 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 n the constraint
1b8e0 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 63 6f 6e s are. ** con
1b8f0 73 69 64 65 72 65 64 20 64 69 73 74 69 6e 63 74 sidered distinct
1b900 20 61 6e 64 20 62 6f 74 68 20 72 65 73 75 6c 74 and both result
1b910 20 69 6e 20 73 65 70 61 72 61 74 65 20 69 6e 64 in separate ind
1b920 69 63 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ices.. */.
1b930 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 Index *pIdx;.
1b940 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d for(pIdx=pTab-
1b950 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 >pIndex; pIdx; p
1b960 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 Idx=pIdx->pNext)
1b970 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 {. int k;.
1b980 20 20 20 20 20 61 73 73 65 72 74 28 20 49 73 55 assert( IsU
1b990 6e 69 71 75 65 49 6e 64 65 78 28 70 49 64 78 29 niqueIndex(pIdx)
1b9a0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1b9b0 28 20 70 49 64 78 2d 3e 69 64 78 54 79 70 65 21 ( pIdx->idxType!
1b9c0 3d 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 5f =SQLITE_IDXTYPE_
1b9d0 41 50 50 44 45 46 20 29 3b 0a 20 20 20 20 20 20 APPDEF );.
1b9e0 61 73 73 65 72 74 28 20 49 73 55 6e 69 71 75 65 assert( IsUnique
1b9f0 49 6e 64 65 78 28 70 49 6e 64 65 78 29 20 29 3b Index(pIndex) );
1ba00 0a 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 .. if( pIdx
1ba10 2d 3e 6e 4b 65 79 43 6f 6c 21 3d 70 49 6e 64 65 ->nKeyCol!=pInde
1ba20 78 2d 3e 6e 4b 65 79 43 6f 6c 20 29 20 63 6f 6e x->nKeyCol ) con
1ba30 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 66 6f 72 tinue;. for
1ba40 28 6b 3d 30 3b 20 6b 3c 70 49 64 78 2d 3e 6e 4b (k=0; k<pIdx->nK
1ba50 65 79 43 6f 6c 3b 20 6b 2b 2b 29 7b 0a 20 20 20 eyCol; k++){.
1ba60 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1ba70 2a 7a 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e *z1;. con
1ba80 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 20 st char *z2;.
1ba90 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 assert( pId
1baa0 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 3e 3d x->aiColumn[k]>=
1bab0 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0 );. if(
1bac0 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b pIdx->aiColumn[
1bad0 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69 43 6f k]!=pIndex->aiCo
1bae0 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61 6b 3b lumn[k] ) break;
1baf0 0a 20 20 20 20 20 20 20 20 7a 31 20 3d 20 70 49 . z1 = pI
1bb00 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 dx->azColl[k];.
1bb10 20 20 20 20 20 20 20 7a 32 20 3d 20 70 49 6e 64 z2 = pInd
1bb20 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 ex->azColl[k];.
1bb30 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
1bb40 65 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 e3StrICmp(z1, z2
1bb50 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 ) ) break;.
1bb60 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d }. if( k==
1bb70 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 20 29 7b pIdx->nKeyCol ){
1bb80 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 . if( pId
1bb90 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e 64 x->onError!=pInd
1bba0 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 ex->onError ){.
1bbb0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
1bbc0 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65 61 constraint crea
1bbd0 74 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e 64 tes the same ind
1bbe0 65 78 20 61 73 20 61 20 70 72 65 76 69 6f 75 73 ex as a previous
1bbf0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f . ** co
1bc00 6e 73 74 72 61 69 6e 74 20 73 70 65 63 69 66 69 nstraint specifi
1bc10 65 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 ed somewhere in
1bc20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
1bc30 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 statement..
1bc40 20 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65 72 ** However
1bc50 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 the ON CONFLICT
1bc60 20 63 6c 61 75 73 65 73 20 61 72 65 20 64 69 66 clauses are dif
1bc70 66 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68 20 ferent. If both
1bc80 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 20 20 this .
1bc90 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e ** constraint an
1bca0 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 d the previous e
1bcb0 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74 72 quivalent constr
1bcc0 61 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69 63 aint have explic
1bcd0 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 it. **
1bce0 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 ON CONFLICT clau
1bcf0 73 65 73 20 74 68 69 73 20 69 73 20 61 6e 20 65 ses this is an e
1bd00 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c rror. Otherwise,
1bd10 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 20 use the.
1bd20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 ** explicitly
1bd30 20 73 70 65 63 69 66 69 65 64 20 62 65 68 61 76 specified behav
1bd40 69 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 ior for the inde
1bd50 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a x.. */.
1bd60 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 28 if( !(
1bd70 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f pIdx->onError==O
1bd80 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70 49 6e E_Default || pIn
1bd90 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 dex->onError==OE
1bda0 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20 20 20 _Default) ){.
1bdb0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1bdc0 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1bdd0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1bde0 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67 20 4f "conflicting O
1bdf0 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 N CONFLICT claus
1be00 65 73 20 73 70 65 63 69 66 69 65 64 22 2c 20 30 es specified", 0
1be10 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
1be20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 if( pId
1be30 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 x->onError==OE_D
1be40 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 efault ){.
1be50 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72 pIdx->onEr
1be60 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e ror = pIndex->on
1be70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 Error;.
1be80 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1be90 20 20 20 20 20 69 66 28 20 69 64 78 54 79 70 65 if( idxType
1bea0 3d 3d 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 ==SQLITE_IDXTYPE
1beb0 5f 50 52 49 4d 41 52 59 4b 45 59 20 29 20 70 49 _PRIMARYKEY ) pI
1bec0 64 78 2d 3e 69 64 78 54 79 70 65 20 3d 20 69 64 dx->idxType = id
1bed0 78 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 67 xType;. g
1bee0 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
1bef0 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d 0a 20 index;. }.
1bf00 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
1bf10 21 49 4e 5f 52 45 4e 41 4d 45 5f 4f 42 4a 45 43 !IN_RENAME_OBJEC
1bf20 54 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 4c 69 6e T ){.. /* Lin
1bf30 6b 20 74 68 65 20 6e 65 77 20 49 6e 64 65 78 20 k the new Index
1bf40 73 74 72 75 63 74 75 72 65 20 74 6f 20 69 74 73 structure to its
1bf50 20 74 61 62 6c 65 20 61 6e 64 20 74 6f 20 74 68 table and to th
1bf60 65 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 69 e other. ** i
1bf70 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
1bf80 65 20 73 74 72 75 63 74 75 72 65 73 2e 20 0a 20 e structures. .
1bf90 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
1bfa0 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d ( pParse->nErr==
1bfb0 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 0 );. if( db-
1bfc0 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 >init.busy ){.
1bfd0 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 Index *p;.
1bfe0 20 20 20 20 61 73 73 65 72 74 28 20 21 49 4e 5f assert( !IN_
1bff0 53 50 45 43 49 41 4c 5f 50 41 52 53 45 20 29 3b SPECIAL_PARSE );
1c000 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
1c010 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 qlite3SchemaMute
1c020 78 48 65 6c 64 28 64 62 2c 20 30 2c 20 70 49 6e xHeld(db, 0, pIn
1c030 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b dex->pSchema) );
1c040 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 . p = sqlit
1c050 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 49 e3HashInsert(&pI
1c060 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 ndex->pSchema->i
1c070 64 78 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20 dxHash, .
1c080 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 pIndex->zName
1c090 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 20 , pIndex);.
1c0a0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 if( p ){.
1c0b0 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 49 6e assert( p==pIn
1c0c0 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f dex ); /* Mallo
1c0d0 63 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c c must have fail
1c0e0 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 ed */. sq
1c0f0 6c 69 74 65 33 4f 6f 6d 46 61 75 6c 74 28 64 62 lite3OomFault(db
1c100 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 );. goto
1c110 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
1c120 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 x;. }.
1c130 20 64 62 2d 3e 6d 44 62 46 6c 61 67 73 20 7c 3d db->mDbFlags |=
1c140 20 44 42 46 4c 41 47 5f 53 63 68 65 6d 61 43 68 DBFLAG_SchemaCh
1c150 61 6e 67 65 3b 0a 20 20 20 20 20 20 69 66 28 20 ange;. if(
1c160 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 20 pTblName!=0 ){.
1c170 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 pIndex->t
1c180 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e num = db->init.n
1c190 65 77 54 6e 75 6d 3b 0a 20 20 20 20 20 20 7d 0a ewTnum;. }.
1c1a0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
1c1b0 20 74 68 69 73 20 69 73 20 74 68 65 20 69 6e 69 this is the ini
1c1c0 74 69 61 6c 20 43 52 45 41 54 45 20 49 4e 44 45 tial CREATE INDE
1c1d0 58 20 73 74 61 74 65 6d 65 6e 74 20 28 6f 72 20 X statement (or
1c1e0 43 52 45 41 54 45 20 54 41 42 4c 45 20 69 66 20 CREATE TABLE if
1c1f0 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 the. ** index
1c200 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64 20 69 is an implied i
1c210 6e 64 65 78 20 66 6f 72 20 61 20 55 4e 49 51 55 ndex for a UNIQU
1c220 45 20 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59 E or PRIMARY KEY
1c230 20 63 6f 6e 73 74 72 61 69 6e 74 29 20 74 68 65 constraint) the
1c240 6e 0a 20 20 20 20 2a 2a 20 65 6d 69 74 20 63 6f n. ** emit co
1c250 64 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 de to allocate t
1c260 68 65 20 69 6e 64 65 78 20 72 6f 6f 74 70 61 67 he index rootpag
1c270 65 20 6f 6e 20 64 69 73 6b 20 61 6e 64 20 6d 61 e on disk and ma
1c280 6b 65 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 0a ke an entry for.
1c290 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 ** the index
1c2a0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d in the sqlite_m
1c2b0 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 aster table and
1c2c0 70 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e 64 populate the ind
1c2d0 65 78 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 63 ex with. ** c
1c2e0 6f 6e 74 65 6e 74 2e 20 20 42 75 74 2c 20 64 6f ontent. But, do
1c2f0 20 6e 6f 74 20 64 6f 20 74 68 69 73 20 69 66 20 not do this if
1c300 77 65 20 61 72 65 20 73 69 6d 70 6c 79 20 72 65 we are simply re
1c310 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 ading the sqlite
1c320 5f 6d 61 73 74 65 72 0a 20 20 20 20 2a 2a 20 74 _master. ** t
1c330 61 62 6c 65 20 74 6f 20 70 61 72 73 65 20 74 68 able to parse th
1c340 65 20 73 63 68 65 6d 61 2c 20 6f 72 20 69 66 20 e schema, or if
1c350 74 68 69 73 20 69 6e 64 65 78 20 69 73 20 74 68 this index is th
1c360 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e e PRIMARY KEY in
1c370 64 65 78 0a 20 20 20 20 2a 2a 20 6f 66 20 61 20 dex. ** of a
1c380 57 49 54 48 4f 55 54 20 52 4f 57 49 44 20 74 61 WITHOUT ROWID ta
1c390 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ble.. **.
1c3a0 2a 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d ** If pTblName==
1c3b0 30 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 0 it means this
1c3c0 69 6e 64 65 78 20 69 73 20 67 65 6e 65 72 61 74 index is generat
1c3d0 65 64 20 61 73 20 61 6e 20 69 6d 70 6c 69 65 64 ed as an implied
1c3e0 20 50 52 49 4d 41 52 59 20 4b 45 59 0a 20 20 20 PRIMARY KEY.
1c3f0 20 2a 2a 20 6f 72 20 55 4e 49 51 55 45 20 69 6e ** or UNIQUE in
1c400 64 65 78 20 69 6e 20 61 20 43 52 45 41 54 45 20 dex in a CREATE
1c410 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e TABLE statement.
1c420 20 20 53 69 6e 63 65 20 74 68 65 20 74 61 62 6c Since the tabl
1c430 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 6a 75 73 e. ** has jus
1c440 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 t been created,
1c450 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 it contains no d
1c460 61 74 61 20 61 6e 64 20 74 68 65 20 69 6e 64 65 ata and the inde
1c470 78 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e x initialization
1c480 0a 20 20 20 20 2a 2a 20 73 74 65 70 20 63 61 6e . ** step can
1c490 20 62 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 be skipped..
1c4a0 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 28 */. else if(
1c4b0 20 48 61 73 52 6f 77 69 64 28 70 54 61 62 29 20 HasRowid(pTab)
1c4c0 7c 7c 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 || pTblName!=0 )
1c4d0 7b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76 3b {. Vdbe *v;
1c4e0 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53 74 . char *zSt
1c4f0 6d 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d mt;. int iM
1c500 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e em = ++pParse->n
1c510 4d 65 6d 3b 0a 0a 20 20 20 20 20 20 76 20 3d 20 Mem;.. v =
1c520 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
1c530 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 66 Parse);. if
1c540 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 ( v==0 ) goto ex
1c550 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b it_create_index;
1c560 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 .. sqlite3B
1c570 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 eginWriteOperati
1c580 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 on(pParse, 1, iD
1c590 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 b);.. /* Cr
1c5a0 65 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61 67 eate the rootpag
1c5b0 65 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 e for the index
1c5c0 75 73 69 6e 67 20 43 72 65 61 74 65 49 6e 64 65 using CreateInde
1c5d0 78 2e 20 42 75 74 20 62 65 66 6f 72 65 0a 20 20 x. But before.
1c5e0 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 73 6f 2c ** doing so,
1c5f0 20 63 6f 64 65 20 61 20 4e 6f 6f 70 20 69 6e 73 code a Noop ins
1c600 74 72 75 63 74 69 6f 6e 20 61 6e 64 20 73 74 6f truction and sto
1c610 72 65 20 69 74 73 20 61 64 64 72 65 73 73 20 69 re its address i
1c620 6e 20 0a 20 20 20 20 20 20 2a 2a 20 49 6e 64 65 n . ** Inde
1c630 78 2e 74 6e 75 6d 2e 20 54 68 69 73 20 69 73 20 x.tnum. This is
1c640 72 65 71 75 69 72 65 64 20 69 6e 20 63 61 73 65 required in case
1c650 20 74 68 69 73 20 69 6e 64 65 78 20 69 73 20 61 this index is a
1c660 63 74 75 61 6c 6c 79 20 61 20 0a 20 20 20 20 20 ctually a .
1c670 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 ** PRIMARY KEY
1c680 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 73 and the table is
1c690 20 61 63 74 75 61 6c 6c 79 20 61 20 57 49 54 48 actually a WITH
1c6a0 4f 55 54 20 52 4f 57 49 44 20 74 61 62 6c 65 2e OUT ROWID table.
1c6b0 20 49 6e 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 In . ** th
1c6c0 61 74 20 63 61 73 65 20 74 68 65 20 63 6f 6e 76 at case the conv
1c6d0 65 72 74 54 6f 57 69 74 68 6f 75 74 52 6f 77 69 ertToWithoutRowi
1c6e0 64 54 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 dTable() routine
1c6f0 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 0a 20 20 will replace.
1c700 20 20 20 20 2a 2a 20 74 68 65 20 4e 6f 6f 70 20 ** the Noop
1c710 77 69 74 68 20 61 20 47 6f 74 6f 20 74 6f 20 6a with a Goto to j
1c720 75 6d 70 20 6f 76 65 72 20 74 68 65 20 56 44 42 ump over the VDB
1c730 45 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 E code generated
1c740 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 below. */.
1c750 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20 pIndex->tnum =
1c760 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1c770 30 28 76 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 0(v, OP_Noop);.
1c780 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1c790 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 72 65 AddOp3(v, OP_Cre
1c7a0 61 74 65 42 74 72 65 65 2c 20 69 44 62 2c 20 69 ateBtree, iDb, i
1c7b0 4d 65 6d 2c 20 42 54 52 45 45 5f 42 4c 4f 42 4b Mem, BTREE_BLOBK
1c7c0 45 59 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 EY);.. /* G
1c7d0 61 74 68 65 72 20 74 68 65 20 63 6f 6d 70 6c 65 ather the comple
1c7e0 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 te text of the C
1c7f0 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 REATE INDEX stat
1c800 65 6d 65 6e 74 20 69 6e 74 6f 0a 20 20 20 20 20 ement into.
1c810 20 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20 76 61 ** the zStmt va
1c820 72 69 61 62 6c 65 0a 20 20 20 20 20 20 2a 2f 0a riable. */.
1c830 20 20 20 20 20 20 69 66 28 20 70 53 74 61 72 74 if( pStart
1c840 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
1c850 6e 20 3d 20 28 69 6e 74 29 28 70 50 61 72 73 65 n = (int)(pParse
1c860 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20 2d ->sLastToken.z -
1c870 20 70 4e 61 6d 65 2d 3e 7a 29 20 2b 20 70 50 61 pName->z) + pPa
1c880 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e rse->sLastToken.
1c890 6e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 n;. if( p
1c8a0 4e 61 6d 65 2d 3e 7a 5b 6e 2d 31 5d 3d 3d 27 3b Name->z[n-1]==';
1c8b0 27 20 29 20 6e 2d 2d 3b 0a 20 20 20 20 20 20 20 ' ) n--;.
1c8c0 20 2f 2a 20 41 20 6e 61 6d 65 64 20 69 6e 64 65 /* A named inde
1c8d0 78 20 77 69 74 68 20 61 6e 20 65 78 70 6c 69 63 x with an explic
1c8e0 69 74 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 it CREATE INDEX
1c8f0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 statement */.
1c900 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c zStmt = sql
1c910 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 ite3MPrintf(db,
1c920 22 43 52 45 41 54 45 25 73 20 49 4e 44 45 58 20 "CREATE%s INDEX
1c930 25 2e 2a 73 22 2c 0a 20 20 20 20 20 20 20 20 20 %.*s",.
1c940 20 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e onError==OE_N
1c950 6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 one ? "" : " UNI
1c960 51 55 45 22 2c 20 6e 2c 20 70 4e 61 6d 65 2d 3e QUE", n, pName->
1c970 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b z);. }else{
1c980 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 61 . /* An a
1c990 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 63 utomatic index c
1c9a0 72 65 61 74 65 64 20 62 79 20 61 20 50 52 49 4d reated by a PRIM
1c9b0 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 ARY KEY or UNIQU
1c9c0 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a E constraint */.
1c9d0 20 20 20 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74 /* zStmt
1c9e0 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
1c9f0 66 28 22 22 29 3b 20 2a 2f 0a 20 20 20 20 20 20 f(""); */.
1ca00 20 20 7a 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 zStmt = 0;.
1ca10 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 }.. /* A
1ca20 64 64 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 73 dd an entry in s
1ca30 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 66 6f 72 qlite_master for
1ca40 20 74 68 69 73 20 69 6e 64 65 78 0a 20 20 20 20 this index.
1ca50 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
1ca60 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
1ca70 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 arse, .
1ca80 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 "INSERT INTO %Q
1ca90 2e 25 73 20 56 41 4c 55 45 53 28 27 69 6e 64 65 .%s VALUES('inde
1caa0 78 27 2c 25 51 2c 25 51 2c 23 25 64 2c 25 51 29 x',%Q,%Q,#%d,%Q)
1cab0 3b 22 2c 0a 20 20 20 20 20 20 20 20 20 20 64 62 ;",. db
1cac0 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 4e ->aDb[iDb].zDbSN
1cad0 61 6d 65 2c 20 4d 41 53 54 45 52 5f 4e 41 4d 45 ame, MASTER_NAME
1cae0 2c 0a 20 20 20 20 20 20 20 20 20 20 70 49 6e 64 ,. pInd
1caf0 65 78 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 ex->zName,.
1cb00 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 pTab->zName
1cb10 2c 0a 20 20 20 20 20 20 20 20 20 20 69 4d 65 6d ,. iMem
1cb20 2c 0a 20 20 20 20 20 20 20 20 20 20 7a 53 74 6d ,. zStm
1cb30 74 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 t. );.
1cb40 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1cb50 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a ee(db, zStmt);..
1cb60 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 74 68 /* Fill th
1cb70 65 20 69 6e 64 65 78 20 77 69 74 68 20 64 61 74 e index with dat
1cb80 61 20 61 6e 64 20 72 65 70 61 72 73 65 20 74 68 a and reparse th
1cb90 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65 20 61 e schema. Code a
1cba0 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20 20 20 20 n OP_Expire.
1cbb0 20 20 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 ** to invalida
1cbc0 74 65 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70 69 te all pre-compi
1cbd0 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a led statements..
1cbe0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
1cbf0 66 28 20 70 54 62 6c 4e 61 6d 65 20 29 7b 0a 20 f( pTblName ){.
1cc00 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
1cc10 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 fillIndex(pParse
1cc20 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d 29 3b , pIndex, iMem);
1cc30 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1cc40 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 ChangeCookie(pPa
1cc50 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 rse, iDb);.
1cc60 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1cc70 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70 28 76 dParseSchemaOp(v
1cc80 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 , iDb,.
1cc90 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 sqlite3MPrint
1cca0 66 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 f(db, "name='%q'
1ccb0 20 41 4e 44 20 74 79 70 65 3d 27 69 6e 64 65 78 AND type='index
1ccc0 27 22 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d '", pIndex->zNam
1ccd0 65 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c e));. sql
1cce0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1ccf0 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 , OP_Expire, 0,
1cd00 31 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 1);. }..
1cd10 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
1cd20 6d 70 48 65 72 65 28 76 2c 20 70 49 6e 64 65 78 mpHere(v, pIndex
1cd30 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 7d 0a 20 ->tnum);. }.
1cd40 20 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64 }.. /* When ad
1cd50 64 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f ding an index to
1cd60 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64 the list of ind
1cd70 69 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 ices for a table
1cd80 2c 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 , make. ** sure
1cd90 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62 all indices lab
1cda0 65 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20 eled OE_Replace
1cdb0 63 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74 come after all t
1cdc0 68 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a hose labeled. *
1cdd0 2a 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68 * OE_Ignore. Th
1cde0 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 is is necessary
1cdf0 66 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74 20 for the correct
1ce00 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b constraint check
1ce10 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 . ** processing
1ce20 20 28 69 6e 20 73 71 6c 69 74 65 33 47 65 6e 65 (in sqlite3Gene
1ce30 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 rateConstraintCh
1ce40 65 63 6b 73 28 29 29 20 61 73 20 70 61 72 74 20 ecks()) as part
1ce50 6f 66 0a 20 20 2a 2a 20 55 50 44 41 54 45 20 61 of. ** UPDATE a
1ce60 6e 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d nd INSERT statem
1ce70 65 6e 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 ents. . */. i
1ce80 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 f( db->init.busy
1ce90 20 7c 7c 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 || pTblName==0
1cea0 29 7b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 ){. if( onErr
1ceb0 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 7c or!=OE_Replace |
1cec0 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d | pTab->pIndex==
1ced0 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 54 0. || pT
1cee0 61 62 2d 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 ab->pIndex->onEr
1cef0 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 29 ror==OE_Replace)
1cf00 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e {. pIndex->
1cf10 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 49 pNext = pTab->pI
1cf20 6e 64 65 78 3b 0a 20 20 20 20 20 20 70 54 61 62 ndex;. pTab
1cf30 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 ->pIndex = pInde
1cf40 78 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 x;. }else{.
1cf50 20 20 20 20 49 6e 64 65 78 20 2a 70 4f 74 68 65 Index *pOthe
1cf60 72 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 r = pTab->pIndex
1cf70 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 ;. while( p
1cf80 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 26 26 20 Other->pNext &&
1cf90 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f pOther->pNext->o
1cfa0 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 nError!=OE_Repla
1cfb0 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f ce ){. pO
1cfc0 74 68 65 72 20 3d 20 70 4f 74 68 65 72 2d 3e 70 ther = pOther->p
1cfd0 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
1cfe0 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 pIndex->pNex
1cff0 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 t = pOther->pNex
1d000 74 3b 0a 20 20 20 20 20 20 70 4f 74 68 65 72 2d t;. pOther-
1d010 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 3b >pNext = pIndex;
1d020 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 . }. pInde
1d030 78 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 73 x = 0;. }. els
1d040 65 20 69 66 28 20 49 4e 5f 52 45 4e 41 4d 45 5f e if( IN_RENAME_
1d050 4f 42 4a 45 43 54 20 29 7b 0a 20 20 20 20 61 73 OBJECT ){. as
1d060 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e sert( pParse->pN
1d070 65 77 49 6e 64 65 78 3d 3d 30 20 29 3b 0a 20 20 ewIndex==0 );.
1d080 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 49 6e pParse->pNewIn
1d090 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 dex = pIndex;.
1d0a0 20 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 pIndex = 0;.
1d0b0 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 }.. /* Clean up
1d0c0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
1d0d0 2a 2f 0a 65 78 69 74 5f 63 72 65 61 74 65 5f 69 */.exit_create_i
1d0e0 6e 64 65 78 3a 0a 20 20 69 66 28 20 70 49 6e 64 ndex:. if( pInd
1d0f0 65 78 20 29 20 73 71 6c 69 74 65 33 46 72 65 65 ex ) sqlite3Free
1d100 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65 78 Index(db, pIndex
1d110 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
1d120 44 65 6c 65 74 65 28 64 62 2c 20 70 50 49 57 68 Delete(db, pPIWh
1d130 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 ere);. sqlite3E
1d140 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 xprListDelete(db
1d150 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 , pList);. sqli
1d160 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 te3SrcListDelete
1d170 28 64 62 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a (db, pTblName);.
1d180 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1d190 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f db, zName);.}../
1d1a0 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e *.** Fill the In
1d1b0 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 20 61 dex.aiRowEst[] a
1d1c0 72 72 61 79 20 77 69 74 68 20 64 65 66 61 75 6c rray with defaul
1d1d0 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 t information -
1d1e0 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 74 information.** t
1d1f0 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 77 o be used when w
1d200 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e 20 74 e have not run t
1d210 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 he ANALYZE comma
1d220 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 6f 77 45 nd..**.** aiRowE
1d230 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f 73 65 st[0] is suppose
1d240 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 d to contain the
1d250 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 number of eleme
1d260 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 nts in the index
1d270 2e 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20 64 6f ..** Since we do
1d280 20 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65 73 73 not know, guess
1d290 20 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61 69 52 1 million. aiR
1d2a0 6f 77 45 73 74 5b 31 5d 20 69 73 20 61 6e 20 65 owEst[1] is an e
1d2b0 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 0a 2a stimate of the.*
1d2c0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 * number of rows
1d2d0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74 68 in the table th
1d2e0 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 at match any par
1d2f0 74 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 ticular value of
1d300 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 63 6f the.** first co
1d310 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 lumn of the inde
1d320 78 2e 20 20 61 69 52 6f 77 45 73 74 5b 32 5d 20 x. aiRowEst[2]
1d330 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f is an estimate o
1d340 66 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 f the number.**
1d350 6f 66 20 72 6f 77 73 20 74 68 61 74 20 6d 61 74 of rows that mat
1d360 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 ch any particula
1d370 72 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 r combination of
1d380 20 74 68 65 20 66 69 72 73 74 20 32 20 63 6f 6c the first 2 col
1d390 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 20 69 umns.** of the i
1d3a0 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f 20 66 6f ndex. And so fo
1d3b0 72 74 68 2e 20 20 49 74 20 6d 75 73 74 20 61 6c rth. It must al
1d3c0 77 61 79 73 20 62 65 20 74 68 65 20 63 61 73 65 ways be the case
1d3d0 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 20 20 20 that.*.**
1d3e0 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d aiRowEst[N]
1d3f0 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d 31 5d 0a <=aiRowEst[N-1].
1d400 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 52 ** aiR
1d410 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a owEst[N]>=1.**.*
1d420 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 74 68 61 * Apart from tha
1d430 74 2c 20 77 65 20 68 61 76 65 20 6c 69 74 74 6c t, we have littl
1d440 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 73 69 64 e to go on besid
1d450 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 61 73 20 es intuition as
1d460 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 6f 77 45 to.** how aiRowE
1d470 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 69 st[] should be i
1d480 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68 65 nitialized. The
1d490 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 72 61 74 numbers generat
1d4a0 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 65 20 62 ed here.** are b
1d4b0 61 73 65 64 20 6f 6e 20 74 79 70 69 63 61 6c 20 ased on typical
1d4c0 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 69 6e 20 values found in
1d4d0 61 63 74 75 61 6c 20 69 6e 64 69 63 65 73 2e 0a actual indices..
1d4e0 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 44 */.void sqlite3D
1d4f0 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 efaultRowEst(Ind
1d500 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 2f 2a 20 ex *pIdx){. /*
1d510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
1d520 30 2c 20 20 39 2c 20 20 38 2c 20 20 37 2c 20 20 0, 9, 8, 7,
1d530 36 20 2a 2f 0a 20 20 4c 6f 67 45 73 74 20 61 56 6 */. LogEst aV
1d540 61 6c 5b 5d 20 3d 20 7b 20 33 33 2c 20 33 32 2c al[] = { 33, 32,
1d550 20 33 30 2c 20 32 38 2c 20 32 36 20 7d 3b 0a 20 30, 28, 26 };.
1d560 20 4c 6f 67 45 73 74 20 2a 61 20 3d 20 70 49 64 LogEst *a = pId
1d570 78 2d 3e 61 69 52 6f 77 4c 6f 67 45 73 74 3b 0a x->aiRowLogEst;.
1d580 20 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 int nCopy = MI
1d590 4e 28 41 72 72 61 79 53 69 7a 65 28 61 56 61 6c N(ArraySize(aVal
1d5a0 29 2c 20 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c ), pIdx->nKeyCol
1d5b0 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f );. int i;.. /
1d5c0 2a 20 49 6e 64 65 78 65 73 20 77 69 74 68 20 64 * Indexes with d
1d5d0 65 66 61 75 6c 74 20 72 6f 77 20 65 73 74 69 6d efault row estim
1d5e0 61 74 65 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ates should not
1d5f0 68 61 76 65 20 73 74 61 74 31 20 64 61 74 61 20 have stat1 data
1d600 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70 49 */. assert( !pI
1d610 64 78 2d 3e 68 61 73 53 74 61 74 31 20 29 3b 0a dx->hasStat1 );.
1d620 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 69 . /* Set the fi
1d630 72 73 74 20 65 6e 74 72 79 20 28 6e 75 6d 62 65 rst entry (numbe
1d640 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 r of rows in the
1d650 20 69 6e 64 65 78 29 20 74 6f 20 74 68 65 20 65 index) to the e
1d660 73 74 69 6d 61 74 65 64 20 0a 20 20 2a 2a 20 6e stimated . ** n
1d670 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e umber of rows in
1d680 20 74 68 65 20 74 61 62 6c 65 2c 20 6f 72 20 68 the table, or h
1d690 61 6c 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f alf the number o
1d6a0 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 f rows in the ta
1d6b0 62 6c 65 0a 20 20 2a 2a 20 66 6f 72 20 61 20 70 ble. ** for a p
1d6c0 61 72 74 69 61 6c 20 69 6e 64 65 78 2e 20 20 20 artial index.
1d6d0 42 75 74 20 64 6f 20 6e 6f 74 20 6c 65 74 20 74 But do not let t
1d6e0 68 65 20 65 73 74 69 6d 61 74 65 20 64 72 6f 70 he estimate drop
1d6f0 20 62 65 6c 6f 77 20 31 30 2e 20 2a 2f 0a 20 20 below 10. */.
1d700 61 5b 30 5d 20 3d 20 70 49 64 78 2d 3e 70 54 61 a[0] = pIdx->pTa
1d710 62 6c 65 2d 3e 6e 52 6f 77 4c 6f 67 45 73 74 3b ble->nRowLogEst;
1d720 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 70 50 61 . if( pIdx->pPa
1d730 72 74 49 64 78 57 68 65 72 65 21 3d 30 20 29 20 rtIdxWhere!=0 )
1d740 61 5b 30 5d 20 2d 3d 20 31 30 3b 20 20 61 73 73 a[0] -= 10; ass
1d750 65 72 74 28 20 31 30 3d 3d 73 71 6c 69 74 65 33 ert( 10==sqlite3
1d760 4c 6f 67 45 73 74 28 32 29 20 29 3b 0a 20 20 69 LogEst(2) );. i
1d770 66 28 20 61 5b 30 5d 3c 33 33 20 29 20 61 5b 30 f( a[0]<33 ) a[0
1d780 5d 20 3d 20 33 33 3b 20 20 20 20 20 20 20 20 20 ] = 33;
1d790 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
1d7a0 20 33 33 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 45 33==sqlite3LogE
1d7b0 73 74 28 31 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 st(10) );.. /*
1d7c0 45 73 74 69 6d 61 74 65 20 74 68 61 74 20 61 5b Estimate that a[
1d7d0 31 5d 20 69 73 20 31 30 2c 20 61 5b 32 5d 20 69 1] is 10, a[2] i
1d7e0 73 20 39 2c 20 61 5b 33 5d 20 69 73 20 38 2c 20 s 9, a[3] is 8,
1d7f0 61 5b 34 5d 20 69 73 20 37 2c 20 61 5b 35 5d 20 a[4] is 7, a[5]
1d800 69 73 0a 20 20 2a 2a 20 36 20 61 6e 64 20 65 61 is. ** 6 and ea
1d810 63 68 20 73 75 62 73 65 71 75 65 6e 74 20 76 61 ch subsequent va
1d820 6c 75 65 20 28 69 66 20 61 6e 79 29 20 69 73 20 lue (if any) is
1d830 35 2e 20 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 5. */. memcpy(
1d840 26 61 5b 31 5d 2c 20 61 56 61 6c 2c 20 6e 43 6f &a[1], aVal, nCo
1d850 70 79 2a 73 69 7a 65 6f 66 28 4c 6f 67 45 73 74 py*sizeof(LogEst
1d860 29 29 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 6f 70 ));. for(i=nCop
1d870 79 2b 31 3b 20 69 3c 3d 70 49 64 78 2d 3e 6e 4b y+1; i<=pIdx->nK
1d880 65 79 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 eyCol; i++){.
1d890 20 61 5b 69 5d 20 3d 20 32 33 3b 20 20 20 20 20 a[i] = 23;
1d8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
1d8b0 73 73 65 72 74 28 20 32 33 3d 3d 73 71 6c 69 74 ssert( 23==sqlit
1d8c0 65 33 4c 6f 67 45 73 74 28 35 29 20 29 3b 0a 20 e3LogEst(5) );.
1d8d0 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 30 3d }.. assert( 0=
1d8e0 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 31 =sqlite3LogEst(1
1d8f0 29 20 29 3b 0a 20 20 69 66 28 20 49 73 55 6e 69 ) );. if( IsUni
1d900 71 75 65 49 6e 64 65 78 28 70 49 64 78 29 20 29 queIndex(pIdx) )
1d910 20 61 5b 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c a[pIdx->nKeyCol
1d920 5d 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ] = 0;.}../*.**
1d930 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
1d940 6c 20 64 72 6f 70 20 61 6e 20 65 78 69 73 74 69 l drop an existi
1d950 6e 67 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 ng named index.
1d960 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a This routine.**
1d970 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
1d980 44 52 4f 50 20 49 4e 44 45 58 20 73 74 61 74 65 DROP INDEX state
1d990 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ment..*/.void sq
1d9a0 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 lite3DropIndex(P
1d9b0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 arse *pParse, Sr
1d9c0 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e cList *pName, in
1d9d0 74 20 69 66 45 78 69 73 74 73 29 7b 0a 20 20 49 t ifExists){. I
1d9e0 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 ndex *pIndex;.
1d9f0 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 Vdbe *v;. sqlit
1da00 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
1da10 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a >db;. int iDb;.
1da20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 . assert( pPars
1da30 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 20 20 e->nErr==0 );
1da40 2f 2a 20 4e 65 76 65 72 20 63 61 6c 6c 65 64 20 /* Never called
1da50 77 69 74 68 20 70 72 69 6f 72 20 65 72 72 6f 72 with prior error
1da60 73 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d s */. if( db->m
1da70 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1da80 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
1da90 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61 p_index;. }. a
1daa0 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 ssert( pName->nS
1dab0 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53 rc==1 );. if( S
1dac0 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 QLITE_OK!=sqlite
1dad0 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 3ReadSchema(pPar
1dae0 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 se) ){. goto
1daf0 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b exit_drop_index;
1db00 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 . }. pIndex =
1db10 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 sqlite3FindIndex
1db20 28 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d (db, pName->a[0]
1db30 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 .zName, pName->a
1db40 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a [0].zDatabase);.
1db50 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 if( pIndex==0
1db60 29 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78 ){. if( !ifEx
1db70 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71 ists ){. sq
1db80 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1db90 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 arse, "no such i
1dba0 6e 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65 ndex: %S", pName
1dbb0 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , 0);. }else{
1dbc0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f . sqlite3Co
1dbd0 64 65 56 65 72 69 66 79 4e 61 6d 65 64 53 63 68 deVerifyNamedSch
1dbe0 65 6d 61 28 70 50 61 72 73 65 2c 20 70 4e 61 6d ema(pParse, pNam
1dbf0 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 e->a[0].zDatabas
1dc00 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 e);. }. pP
1dc10 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d arse->checkSchem
1dc20 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 a = 1;. goto
1dc30 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b exit_drop_index;
1dc40 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 64 65 . }. if( pInde
1dc50 78 2d 3e 69 64 78 54 79 70 65 21 3d 53 51 4c 49 x->idxType!=SQLI
1dc60 54 45 5f 49 44 58 54 59 50 45 5f 41 50 50 44 45 TE_IDXTYPE_APPDE
1dc70 46 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 F ){. sqlite3
1dc80 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1dc90 20 22 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 "index associat
1dca0 65 64 20 77 69 74 68 20 55 4e 49 51 55 45 20 22 ed with UNIQUE "
1dcb0 0a 20 20 20 20 20 20 22 6f 72 20 50 52 49 4d 41 . "or PRIMA
1dcc0 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e RY KEY constrain
1dcd0 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 72 6f 70 t cannot be drop
1dce0 70 65 64 22 2c 20 30 29 3b 0a 20 20 20 20 67 6f ped", 0);. go
1dcf0 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 to exit_drop_ind
1dd00 65 78 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 ex;. }. iDb =
1dd10 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
1dd20 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65 78 2d ndex(db, pIndex-
1dd30 3e 70 53 63 68 65 6d 61 29 3b 0a 23 69 66 6e 64 >pSchema);.#ifnd
1dd40 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1dd50 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b UTHORIZATION. {
1dd60 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 . int code =
1dd70 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 SQLITE_DROP_INDE
1dd80 58 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 X;. Table *pT
1dd90 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 ab = pIndex->pTa
1dda0 62 6c 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 ble;. const c
1ddb0 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 har *zDb = db->a
1ddc0 44 62 5b 69 44 62 5d 2e 7a 44 62 53 4e 61 6d 65 Db[iDb].zDbSName
1ddd0 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 ;. const char
1dde0 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f *zTab = SCHEMA_
1ddf0 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 TABLE(iDb);.
1de00 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 if( sqlite3AuthC
1de10 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c heck(pParse, SQL
1de20 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 ITE_DELETE, zTab
1de30 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 , 0, zDb) ){.
1de40 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
1de50 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 p_index;. }.
1de60 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d if( !OMIT_TEM
1de70 50 44 42 20 26 26 20 69 44 62 20 29 20 63 6f 64 PDB && iDb ) cod
1de80 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f e = SQLITE_DROP_
1de90 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 TEMP_INDEX;.
1dea0 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 if( sqlite3AuthC
1deb0 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 heck(pParse, cod
1dec0 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 e, pIndex->zName
1ded0 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a , pTab->zName, z
1dee0 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 Db) ){. got
1def0 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 o exit_drop_inde
1df00 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e x;. }. }.#en
1df10 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 dif.. /* Genera
1df20 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 te code to remov
1df30 65 20 74 68 65 20 69 6e 64 65 78 20 61 6e 64 20 e the index and
1df40 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 65 72 20 from the master
1df50 74 61 62 6c 65 20 2a 2f 0a 20 20 76 20 3d 20 73 table */. v = s
1df60 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
1df70 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 arse);. if( v )
1df80 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 {. sqlite3Beg
1df90 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e inWriteOperation
1dfa0 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 (pParse, 1, iDb)
1dfb0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 ;. sqlite3Nes
1dfc0 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
1dfd0 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 . "DELETE
1dfe0 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 FROM %Q.%s WHERE
1dff0 20 6e 61 6d 65 3d 25 51 20 41 4e 44 20 74 79 70 name=%Q AND typ
1e000 65 3d 27 69 6e 64 65 78 27 22 2c 0a 20 20 20 20 e='index'",.
1e010 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e db->aDb[iDb].
1e020 7a 44 62 53 4e 61 6d 65 2c 20 4d 41 53 54 45 52 zDbSName, MASTER
1e030 5f 4e 41 4d 45 2c 20 70 49 6e 64 65 78 2d 3e 7a _NAME, pIndex->z
1e040 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 Name. );.
1e050 73 71 6c 69 74 65 33 43 6c 65 61 72 53 74 61 74 sqlite3ClearStat
1e060 54 61 62 6c 65 73 28 70 50 61 72 73 65 2c 20 69 Tables(pParse, i
1e070 44 62 2c 20 22 69 64 78 22 2c 20 70 49 6e 64 65 Db, "idx", pInde
1e080 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 x->zName);. s
1e090 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b qlite3ChangeCook
1e0a0 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b ie(pParse, iDb);
1e0b0 0a 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 . destroyRoot
1e0c0 50 61 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e Page(pParse, pIn
1e0d0 64 65 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b dex->tnum, iDb);
1e0e0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1e0f0 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f AddOp4(v, OP_Dro
1e100 70 49 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c 20 pIndex, iDb, 0,
1e110 30 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0, pIndex->zName
1e120 2c 20 30 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f , 0);. }..exit_
1e130 64 72 6f 70 5f 69 6e 64 65 78 3a 0a 20 20 73 71 drop_index:. sq
1e140 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 lite3SrcListDele
1e150 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d te(db, pName);.}
1e160 0a 0a 2f 2a 0a 2a 2a 20 70 41 72 72 61 79 20 69 ../*.** pArray i
1e170 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1e180 6e 20 61 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 n array of objec
1e190 74 73 2e 20 45 61 63 68 20 6f 62 6a 65 63 74 20 ts. Each object
1e1a0 69 6e 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 in the.** array
1e1b0 69 73 20 73 7a 45 6e 74 72 79 20 62 79 74 65 73 is szEntry bytes
1e1c0 20 69 6e 20 73 69 7a 65 2e 20 54 68 69 73 20 72 in size. This r
1e1d0 6f 75 74 69 6e 65 20 75 73 65 73 20 73 71 6c 69 outine uses sqli
1e1e0 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 29 0a 2a te3DbRealloc().*
1e1f0 2a 20 74 6f 20 65 78 74 65 6e 64 20 74 68 65 20 * to extend the
1e200 61 72 72 61 79 20 73 6f 20 74 68 61 74 20 74 68 array so that th
1e210 65 72 65 20 69 73 20 73 70 61 63 65 20 66 6f 72 ere is space for
1e220 20 61 20 6e 65 77 20 6f 62 6a 65 63 74 20 61 74 a new object at
1e230 20 74 68 65 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 the end..**.**
1e240 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 When this functi
1e250 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 2a 70 on is called, *p
1e260 6e 45 6e 74 72 79 20 63 6f 6e 74 61 69 6e 73 20 nEntry contains
1e270 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 the current size
1e280 20 6f 66 0a 2a 2a 20 74 68 65 20 61 72 72 61 79 of.** the array
1e290 20 28 69 6e 20 65 6e 74 72 69 65 73 20 2d 20 73 (in entries - s
1e2a0 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e o the allocation
1e2b0 20 69 73 20 28 28 2a 70 6e 45 6e 74 72 79 29 20 is ((*pnEntry)
1e2c0 2a 20 73 7a 45 6e 74 72 79 29 20 62 79 74 65 73 * szEntry) bytes
1e2d0 0a 2a 2a 20 69 6e 20 74 6f 74 61 6c 29 2e 0a 2a .** in total)..*
1e2e0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 61 6c *.** If the real
1e2f0 6c 6f 63 28 29 20 69 73 20 73 75 63 63 65 73 73 loc() is success
1e300 66 75 6c 20 28 69 2e 65 2e 20 69 66 20 6e 6f 20 ful (i.e. if no
1e310 4f 4f 4d 20 63 6f 6e 64 69 74 69 6f 6e 20 6f 63 OOM condition oc
1e320 63 75 72 73 29 2c 20 74 68 65 0a 2a 2a 20 73 70 curs), the.** sp
1e330 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ace allocated fo
1e340 72 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 r the new object
1e350 20 69 73 20 7a 65 72 6f 65 64 2c 20 2a 70 6e 45 is zeroed, *pnE
1e360 6e 74 72 79 20 75 70 64 61 74 65 64 20 74 6f 0a ntry updated to.
1e370 2a 2a 20 72 65 66 6c 65 63 74 20 74 68 65 20 6e ** reflect the n
1e380 65 77 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 ew size of the a
1e390 72 72 61 79 20 61 6e 64 20 61 20 70 6f 69 6e 74 rray and a point
1e3a0 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61 6c er to the new al
1e3b0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 location.** retu
1e3c0 72 6e 65 64 2e 20 2a 70 49 64 78 20 69 73 20 73 rned. *pIdx is s
1e3d0 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 et to the index
1e3e0 6f 66 20 74 68 65 20 6e 65 77 20 61 72 72 61 79 of the new array
1e3f0 20 65 6e 74 72 79 20 69 6e 20 74 68 69 73 20 63 entry in this c
1e400 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 ase..**.** Other
1e410 77 69 73 65 2c 20 69 66 20 74 68 65 20 72 65 61 wise, if the rea
1e420 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2c 20 2a 70 lloc() fails, *p
1e430 49 64 78 20 69 73 20 73 65 74 20 74 6f 20 2d 31 Idx is set to -1
1e440 2c 20 2a 70 6e 45 6e 74 72 79 20 72 65 6d 61 69 , *pnEntry remai
1e450 6e 73 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 ns.** unchanged
1e460 61 6e 64 20 61 20 63 6f 70 79 20 6f 66 20 70 41 and a copy of pA
1e470 72 72 61 79 20 72 65 74 75 72 6e 65 64 2e 0a 2a rray returned..*
1e480 2f 0a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 /.void *sqlite3A
1e490 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 rrayAllocate(.
1e4a0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
1e4b0 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 /* Connection
1e4c0 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c to notify of mal
1e4d0 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a loc failures */.
1e4e0 20 20 76 6f 69 64 20 2a 70 41 72 72 61 79 2c 20 void *pArray,
1e4f0 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 /* Array of
1e500 6f 62 6a 65 63 74 73 2e 20 20 4d 69 67 68 74 20 objects. Might
1e510 62 65 20 72 65 61 6c 6c 6f 63 61 74 65 64 20 2a be reallocated *
1e520 2f 0a 20 20 69 6e 74 20 73 7a 45 6e 74 72 79 2c /. int szEntry,
1e530 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1e540 20 65 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20 each object in
1e550 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 the array */. i
1e560 6e 74 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 nt *pnEntry,
1e570 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 /* Number of ob
1e580 6a 65 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 jects currently
1e590 69 6e 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 in use */. int
1e5a0 2a 70 49 64 78 20 20 20 20 20 20 20 20 20 2f 2a *pIdx /*
1e5b0 20 57 72 69 74 65 20 74 68 65 20 69 6e 64 65 78 Write the index
1e5c0 20 6f 66 20 61 20 6e 65 77 20 73 6c 6f 74 20 68 of a new slot h
1e5d0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 ere */.){. char
1e5e0 20 2a 7a 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 2a *z;. int n = *
1e5f0 70 6e 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 28 pnEntry;. if( (
1e600 6e 20 26 20 28 6e 2d 31 29 29 3d 3d 30 20 29 7b n & (n-1))==0 ){
1e610 0a 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 28 6e . int sz = (n
1e620 3d 3d 30 29 20 3f 20 31 20 3a 20 32 2a 6e 3b 0a ==0) ? 1 : 2*n;.
1e630 20 20 20 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d void *pNew =
1e640 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
1e650 63 28 64 62 2c 20 70 41 72 72 61 79 2c 20 73 7a c(db, pArray, sz
1e660 2a 73 7a 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 *szEntry);. i
1e670 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 f( pNew==0 ){.
1e680 20 20 20 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a *pIdx = -1;.
1e690 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 41 72 return pAr
1e6a0 72 61 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ray;. }. p
1e6b0 41 72 72 61 79 20 3d 20 70 4e 65 77 3b 0a 20 20 Array = pNew;.
1e6c0 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 }. z = (char*)p
1e6d0 41 72 72 61 79 3b 0a 20 20 6d 65 6d 73 65 74 28 Array;. memset(
1e6e0 26 7a 5b 6e 20 2a 20 73 7a 45 6e 74 72 79 5d 2c &z[n * szEntry],
1e6f0 20 30 2c 20 73 7a 45 6e 74 72 79 29 3b 0a 20 20 0, szEntry);.
1e700 2a 70 49 64 78 20 3d 20 6e 3b 0a 20 20 2b 2b 2a *pIdx = n;. ++*
1e710 70 6e 45 6e 74 72 79 3b 0a 20 20 72 65 74 75 72 pnEntry;. retur
1e720 6e 20 70 41 72 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a n pArray;.}../*.
1e730 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65 77 20 ** Append a new
1e740 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 67 element to the g
1e750 69 76 65 6e 20 49 64 4c 69 73 74 2e 20 20 43 72 iven IdList. Cr
1e760 65 61 74 65 20 61 20 6e 65 77 20 49 64 4c 69 73 eate a new IdLis
1e770 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e t if.** need be.
1e780 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 49 64 4c .**.** A new IdL
1e790 69 73 74 20 69 73 20 72 65 74 75 72 6e 65 64 2c ist is returned,
1e7a0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6d 61 6c 6c or NULL if mall
1e7b0 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 49 oc() fails..*/.I
1e7c0 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 dList *sqlite3Id
1e7d0 4c 69 73 74 41 70 70 65 6e 64 28 50 61 72 73 65 ListAppend(Parse
1e7e0 20 2a 70 50 61 72 73 65 2c 20 49 64 4c 69 73 74 *pParse, IdList
1e7f0 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a *pList, Token *
1e800 70 54 6f 6b 65 6e 29 7b 0a 20 20 73 71 6c 69 74 pToken){. sqlit
1e810 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
1e820 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 >db;. int i;.
1e830 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a if( pList==0 ){.
1e840 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 pList = sqli
1e850 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
1e860 64 62 2c 20 73 69 7a 65 6f 66 28 49 64 4c 69 73 db, sizeof(IdLis
1e870 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c t) );. if( pL
1e880 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ist==0 ) return
1e890 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 0;. }. pList->
1e8a0 61 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 a = sqlite3Array
1e8b0 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 Allocate(.
1e8c0 64 62 2c 0a 20 20 20 20 20 20 70 4c 69 73 74 2d db,. pList-
1e8d0 3e 61 2c 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 >a,. sizeof
1e8e0 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20 (pList->a[0]),.
1e8f0 20 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 49 64 &pList->nId
1e900 2c 0a 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a ,. &i. );.
1e910 20 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 if( i<0 ){.
1e920 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 sqlite3IdListDe
1e930 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b lete(db, pList);
1e940 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
1e950 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d }. pList->a[i]
1e960 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 .zName = sqlite3
1e970 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 NameFromToken(db
1e980 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 , pToken);. if(
1e990 20 49 4e 5f 52 45 4e 41 4d 45 5f 4f 42 4a 45 43 IN_RENAME_OBJEC
1e9a0 54 20 26 26 20 70 4c 69 73 74 2d 3e 61 5b 69 5d T && pList->a[i]
1e9b0 2e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 73 71 .zName ){. sq
1e9c0 6c 69 74 65 33 52 65 6e 61 6d 65 54 6f 6b 65 6e lite3RenameToken
1e9d0 4d 61 70 28 70 50 61 72 73 65 2c 20 28 76 6f 69 Map(pParse, (voi
1e9e0 64 2a 29 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a d*)pList->a[i].z
1e9f0 4e 61 6d 65 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 Name, pToken);.
1ea00 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 }. return pLis
1ea10 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 t;.}../*.** Dele
1ea20 74 65 20 61 6e 20 49 64 4c 69 73 74 2e 0a 2a 2f te an IdList..*/
1ea30 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64 4c .void sqlite3IdL
1ea40 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 istDelete(sqlite
1ea50 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 3 *db, IdList *p
1ea60 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a List){. int i;.
1ea70 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 if( pList==0 )
1ea80 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 return;. for(i
1ea90 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 =0; i<pList->nId
1eaa0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 ; i++){. sqli
1eab0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c te3DbFree(db, pL
1eac0 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 ist->a[i].zName)
1ead0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 ;. }. sqlite3D
1eae0 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d bFree(db, pList-
1eaf0 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 >a);. sqlite3Db
1eb00 46 72 65 65 4e 4e 28 64 62 2c 20 70 4c 69 73 74 FreeNN(db, pList
1eb10 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
1eb20 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 rn the index in
1eb30 70 4c 69 73 74 20 6f 66 20 74 68 65 20 69 64 65 pList of the ide
1eb40 6e 74 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 ntifier named zI
1eb50 64 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a d. Return -1.**
1eb60 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a if not found..*
1eb70 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c /.int sqlite3IdL
1eb80 69 73 74 49 6e 64 65 78 28 49 64 4c 69 73 74 20 istIndex(IdList
1eb90 2a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 *pList, const ch
1eba0 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e ar *zName){. in
1ebb0 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 t i;. if( pList
1ebc0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b ==0 ) return -1;
1ebd0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c . for(i=0; i<pL
1ebe0 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a ist->nId; i++){.
1ebf0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1ec00 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b trICmp(pList->a[
1ec10 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 i].zName, zName)
1ec20 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a ==0 ) return i;.
1ec30 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b }. return -1;
1ec40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 61 6e 64 .}../*.** Expand
1ec50 20 74 68 65 20 73 70 61 63 65 20 61 6c 6c 6f 63 the space alloc
1ec60 61 74 65 64 20 66 6f 72 20 74 68 65 20 67 69 76 ated for the giv
1ec70 65 6e 20 53 72 63 4c 69 73 74 20 6f 62 6a 65 63 en SrcList objec
1ec80 74 20 62 79 0a 2a 2a 20 63 72 65 61 74 69 6e 67 t by.** creating
1ec90 20 6e 45 78 74 72 61 20 6e 65 77 20 73 6c 6f 74 nExtra new slot
1eca0 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 s beginning at i
1ecb0 53 74 61 72 74 2e 20 20 69 53 74 61 72 74 20 69 Start. iStart i
1ecc0 73 20 7a 65 72 6f 20 62 61 73 65 64 2e 0a 2a 2a s zero based..**
1ecd0 20 4e 65 77 20 73 6c 6f 74 73 20 61 72 65 20 7a New slots are z
1ece0 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 eroed..**.** For
1ecf0 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 example, suppos
1ed00 65 20 61 20 53 72 63 4c 69 73 74 20 69 6e 69 74 e a SrcList init
1ed10 69 61 6c 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 ially contains t
1ed20 77 6f 20 65 6e 74 72 69 65 73 3a 20 41 2c 42 2e wo entries: A,B.
1ed30 0a 2a 2a 20 54 6f 20 61 70 70 65 6e 64 20 33 20 .** To append 3
1ed40 6e 65 77 20 65 6e 74 72 69 65 73 20 6f 6e 74 6f new entries onto
1ed50 20 74 68 65 20 65 6e 64 2c 20 64 6f 20 74 68 69 the end, do thi
1ed60 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 s:.**.** sqli
1ed70 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 te3SrcListEnlarg
1ed80 65 28 64 62 2c 20 70 53 72 63 6c 69 73 74 2c 20 e(db, pSrclist,
1ed90 33 2c 20 32 29 3b 0a 2a 2a 0a 2a 2a 20 41 66 74 3, 2);.**.** Aft
1eda0 65 72 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 er the call abov
1edb0 65 20 69 74 20 77 6f 75 6c 64 20 63 6f 6e 74 61 e it would conta
1edc0 69 6e 3a 20 20 41 2c 20 42 2c 20 6e 69 6c 2c 20 in: A, B, nil,
1edd0 6e 69 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20 49 66 20 nil, nil..** If
1ede0 74 68 65 20 69 53 74 61 72 74 20 61 72 67 75 6d the iStart argum
1edf0 65 6e 74 20 68 61 64 20 62 65 65 6e 20 31 20 69 ent had been 1 i
1ee00 6e 73 74 65 61 64 20 6f 66 20 32 2c 20 74 68 65 nstead of 2, the
1ee10 6e 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 n the result.**
1ee20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 3a would have been:
1ee30 20 20 41 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e A, nil, nil, n
1ee40 69 6c 2c 20 42 2e 20 20 54 6f 20 70 72 65 70 65 il, B. To prepe
1ee50 6e 64 20 74 68 65 20 6e 65 77 20 73 6c 6f 74 73 nd the new slots
1ee60 2c 0a 2a 2a 20 74 68 65 20 69 53 74 61 72 74 20 ,.** the iStart
1ee70 76 61 6c 75 65 20 77 6f 75 6c 64 20 62 65 20 30 value would be 0
1ee80 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 74 68 . The result th
1ee90 65 6e 20 77 6f 75 6c 64 0a 2a 2a 20 62 65 3a 20 en would.** be:
1eea0 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 nil, nil, nil, A
1eeb0 2c 20 42 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 , B..**.** If a
1eec0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1eed0 6e 20 66 61 69 6c 73 20 74 68 65 20 53 72 63 4c n fails the SrcL
1eee0 69 73 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ist is unchanged
1eef0 2e 20 20 54 68 65 0a 2a 2a 20 64 62 2d 3e 6d 61 . The.** db->ma
1ef00 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
1ef10 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 74 will be set to t
1ef20 72 75 65 2e 0a 2a 2f 0a 53 72 63 4c 69 73 74 20 rue..*/.SrcList
1ef30 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 *sqlite3SrcListE
1ef40 6e 6c 61 72 67 65 28 0a 20 20 73 71 6c 69 74 65 nlarge(. sqlite
1ef50 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 3 *db, /*
1ef60 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 Database connect
1ef70 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 ion to notify of
1ef80 20 4f 4f 4d 20 65 72 72 6f 72 73 20 2a 2f 0a 20 OOM errors */.
1ef90 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 SrcList *pSrc,
1efa0 20 20 20 20 2f 2a 20 54 68 65 20 53 72 63 4c 69 /* The SrcLi
1efb0 73 74 20 74 6f 20 62 65 20 65 6e 6c 61 72 67 65 st to be enlarge
1efc0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 d */. int nExtr
1efd0 61 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d a, /* Num
1efe0 62 65 72 20 6f 66 20 6e 65 77 20 73 6c 6f 74 73 ber of new slots
1eff0 20 74 6f 20 61 64 64 20 74 6f 20 70 53 72 63 2d to add to pSrc-
1f000 3e 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 53 >a[] */. int iS
1f010 74 61 72 74 20 20 20 20 20 20 20 20 20 2f 2a 20 tart /*
1f020 49 6e 64 65 78 20 69 6e 20 70 53 72 63 2d 3e 61 Index in pSrc->a
1f030 5b 5d 20 6f 66 20 66 69 72 73 74 20 6e 65 77 20 [] of first new
1f040 73 6c 6f 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 slot */.){. int
1f050 20 69 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 i;.. /* Sanity
1f060 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 63 61 6c checking on cal
1f070 6c 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 ling parameters
1f080 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 */. assert( iSt
1f090 61 72 74 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 art>=0 );. asse
1f0a0 72 74 28 20 6e 45 78 74 72 61 3e 3d 31 20 29 3b rt( nExtra>=1 );
1f0b0 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21 . assert( pSrc!
1f0c0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1f0d0 69 53 74 61 72 74 3c 3d 70 53 72 63 2d 3e 6e 53 iStart<=pSrc->nS
1f0e0 72 63 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f rc );.. /* Allo
1f0f0 63 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 cate additional
1f100 73 70 61 63 65 20 69 66 20 6e 65 65 64 65 64 20 space if needed
1f110 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 29 70 53 */. if( (u32)pS
1f120 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3e rc->nSrc+nExtra>
1f130 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a pSrc->nAlloc ){.
1f140 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 SrcList *pNe
1f150 77 3b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f w;. int nAllo
1f160 63 20 3d 20 70 53 72 63 2d 3e 6e 53 72 63 2a 32 c = pSrc->nSrc*2
1f170 2b 6e 45 78 74 72 61 3b 0a 20 20 20 20 69 6e 74 +nExtra;. int
1f180 20 6e 47 6f 74 3b 0a 20 20 20 20 70 4e 65 77 20 nGot;. pNew
1f190 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c = sqlite3DbReall
1f1a0 6f 63 28 64 62 2c 20 70 53 72 63 2c 0a 20 20 20 oc(db, pSrc,.
1f1b0 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 size
1f1c0 6f 66 28 2a 70 53 72 63 29 20 2b 20 28 6e 41 6c of(*pSrc) + (nAl
1f1d0 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 53 loc-1)*sizeof(pS
1f1e0 72 63 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 20 rc->a[0]) );.
1f1f0 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a if( pNew==0 ){.
1f200 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 assert( db
1f210 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1f220 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 ;. return p
1f230 53 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 Src;. }. p
1f240 53 72 63 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 Src = pNew;.
1f250 6e 47 6f 74 20 3d 20 28 73 71 6c 69 74 65 33 44 nGot = (sqlite3D
1f260 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 bMallocSize(db,
1f270 70 4e 65 77 29 20 2d 20 73 69 7a 65 6f 66 28 2a pNew) - sizeof(*
1f280 70 53 72 63 29 29 2f 73 69 7a 65 6f 66 28 70 53 pSrc))/sizeof(pS
1f290 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b 0a 20 20 20 rc->a[0])+1;.
1f2a0 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 pSrc->nAlloc =
1f2b0 6e 47 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 nGot;. }.. /*
1f2c0 4d 6f 76 65 20 65 78 69 73 74 69 6e 67 20 73 6c Move existing sl
1f2d0 6f 74 73 20 74 68 61 74 20 63 6f 6d 65 20 61 66 ots that come af
1f2e0 74 65 72 20 74 68 65 20 6e 65 77 6c 79 20 69 6e ter the newly in
1f2f0 73 65 72 74 65 64 20 73 6c 6f 74 73 0a 20 20 2a serted slots. *
1f300 2a 20 6f 75 74 20 6f 66 20 74 68 65 20 77 61 79 * out of the way
1f310 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 70 53 72 63 */. for(i=pSrc
1f320 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 69 53 74 ->nSrc-1; i>=iSt
1f330 61 72 74 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70 art; i--){. p
1f340 53 72 63 2d 3e 61 5b 69 2b 6e 45 78 74 72 61 5d Src->a[i+nExtra]
1f350 20 3d 20 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 = pSrc->a[i];.
1f360 20 7d 0a 20 20 70 53 72 63 2d 3e 6e 53 72 63 20 }. pSrc->nSrc
1f370 2b 3d 20 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a += nExtra;.. /*
1f380 20 5a 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20 Zero the newly
1f390 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 allocated slots
1f3a0 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72 */. memset(&pSr
1f3b0 63 2d 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c c->a[iStart], 0,
1f3c0 20 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b sizeof(pSrc->a[
1f3d0 30 5d 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66 0])*nExtra);. f
1f3e0 6f 72 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69 or(i=iStart; i<i
1f3f0 53 74 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b Start+nExtra; i+
1f400 2b 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b +){. pSrc->a[
1f410 69 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b i].iCursor = -1;
1f420 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 . }.. /* Retur
1f430 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
1f440 68 65 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c he enlarged SrcL
1f450 69 73 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 ist */. return
1f460 70 53 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 pSrc;.}.../*.**
1f470 41 70 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 Append a new tab
1f480 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 le name to the g
1f490 69 76 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 iven SrcList. C
1f4a0 72 65 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c reate a new SrcL
1f4b0 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 ist if.** need b
1f4c0 65 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 e. A new entry
1f4d0 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 is created in th
1f4e0 65 20 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 e SrcList even i
1f4f0 66 20 70 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c f pTable is NULL
1f500 2e 0a 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73 ..**.** A SrcLis
1f510 74 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f t is returned, o
1f520 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 r NULL if there
1f530 69 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e is an OOM error.
1f540 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a The returned.*
1f550 2a 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 * SrcList might
1f560 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 be the same as t
1f570 68 65 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 he SrcList that
1f580 77 61 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20 was input or it
1f590 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65 might be.** a ne
1f5a0 77 20 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f w one. If an OO
1f5b0 4d 20 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63 M error does occ
1f5c0 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72 urs, then the pr
1f5d0 69 6f 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69 ior value of pLi
1f5e0 73 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e st.** that is in
1f5f0 70 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 put to this rout
1f600 69 6e 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63 ine is automatic
1f610 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a ally freed..**.*
1f620 2a 20 49 66 20 70 44 61 74 61 62 61 73 65 20 69 * If pDatabase i
1f630 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d s not null, it m
1f640 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 eans that the ta
1f650 62 6c 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f ble has an optio
1f660 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 nal.** database
1f670 6e 61 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 name prefix. Li
1f680 6b 65 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 ke this: "datab
1f690 61 73 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 ase.table". The
1f6a0 20 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f pDatabase.** po
1f6b0 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c ints to the tabl
1f6c0 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70 e name and the p
1f6d0 54 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 Table points to
1f6e0 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
1f6f0 65 2e 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73 e..** The SrcLis
1f700 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c t.a[].zName fiel
1f710 64 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 d is filled with
1f720 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 the table name
1f730 77 68 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63 which might.** c
1f740 6f 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20 ome from pTable
1f750 28 69 66 20 70 44 61 74 61 62 61 73 65 20 69 73 (if pDatabase is
1f760 20 4e 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70 NULL) or from p
1f770 44 61 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53 Database. .** S
1f780 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 rcList.a[].zData
1f790 62 61 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77 base is filled w
1f7a0 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 ith the database
1f7b0 20 6e 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c name from pTabl
1f7c0 65 2c 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55 e,.** or with NU
1f7d0 4c 4c 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73 LL if no databas
1f7e0 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a e is specified..
1f7f0 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 **.** In other w
1f800 6f 72 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69 ords, if call li
1f810 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
1f820 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 sqlite3Sr
1f830 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c cListAppend(D,A,
1f840 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e B,0);.**.** Then
1f850 20 42 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 B is a table na
1f860 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 me and the datab
1f870 61 73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70 ase name is unsp
1f880 65 63 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c ecified. If cal
1f890 6c 65 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 led.** like this
1f8a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 :.**.**
1f8b0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 sqlite3SrcListAp
1f8c0 70 65 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a pend(D,A,B,C);.*
1f8d0 2a 0a 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 *.** Then C is t
1f8e0 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e he table name an
1f8f0 64 20 42 20 69 73 20 74 68 65 20 64 61 74 61 62 d B is the datab
1f900 61 73 65 20 6e 61 6d 65 2e 20 20 49 66 20 43 20 ase name. If C
1f910 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 74 68 is defined.** th
1f920 65 6e 20 73 6f 20 69 73 20 42 2e 20 20 49 6e 20 en so is B. In
1f930 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 65 20 other words, we
1f940 6e 65 76 65 72 20 68 61 76 65 20 61 20 63 61 73 never have a cas
1f950 65 20 77 68 65 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 e where:.**.**
1f960 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 sqlite3Sr
1f970 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c cListAppend(D,A,
1f980 30 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 42 6f 74 68 0,C);.**.** Both
1f990 20 70 54 61 62 6c 65 20 61 6e 64 20 70 44 61 74 pTable and pDat
1f9a0 61 62 61 73 65 20 61 72 65 20 61 73 73 75 6d 65 abase are assume
1f9b0 64 20 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 d to be quoted.
1f9c0 20 54 68 65 79 20 61 72 65 20 64 65 71 75 6f 74 They are dequot
1f9d0 65 64 0a 2a 2a 20 62 65 66 6f 72 65 20 62 65 69 ed.** before bei
1f9e0 6e 67 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 ng added to the
1f9f0 53 72 63 4c 69 73 74 2e 0a 2a 2f 0a 53 72 63 4c SrcList..*/.SrcL
1fa00 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c ist *sqlite3SrcL
1fa10 69 73 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c istAppend(. sql
1fa20 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
1fa30 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 /* Connection t
1fa40 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c o notify of mall
1fa50 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 oc failures */.
1fa60 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c SrcList *pList,
1fa70 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 /* Append t
1fa80 6f 20 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20 o this SrcList.
1fa90 4e 55 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e NULL creates a n
1faa0 65 77 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 ew SrcList */.
1fab0 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 Token *pTable,
1fac0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 /* Table to
1fad0 61 70 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 append */. Toke
1fae0 6e 20 2a 70 44 61 74 61 62 61 73 65 20 20 20 20 n *pDatabase
1faf0 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 66 20 74 /* Database of t
1fb00 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 he table */.){.
1fb10 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
1fb20 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 item *pItem;. a
1fb30 73 73 65 72 74 28 20 70 44 61 74 61 62 61 73 65 ssert( pDatabase
1fb40 3d 3d 30 20 7c 7c 20 70 54 61 62 6c 65 21 3d 30 ==0 || pTable!=0
1fb50 20 29 3b 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 ); /* Cannot h
1fb60 61 76 65 20 43 20 77 69 74 68 6f 75 74 20 42 20 ave C without B
1fb70 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 */. assert( db!
1fb80 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 =0 );. if( pLis
1fb90 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 t==0 ){. pLis
1fba0 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
1fbb0 6c 6f 63 52 61 77 4e 4e 28 64 62 2c 20 73 69 7a locRawNN(db, siz
1fbc0 65 6f 66 28 53 72 63 4c 69 73 74 29 20 29 3b 0a eof(SrcList) );.
1fbd0 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 if( pList==0
1fbe0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
1fbf0 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d pList->nAlloc =
1fc00 20 31 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 1;. pList->n
1fc10 53 72 63 20 3d 20 31 3b 0a 20 20 20 20 6d 65 6d Src = 1;. mem
1fc20 73 65 74 28 26 70 4c 69 73 74 2d 3e 61 5b 30 5d set(&pList->a[0]
1fc30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 4c 69 73 , 0, sizeof(pLis
1fc40 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 20 70 t->a[0]));. p
1fc50 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 List->a[0].iCurs
1fc60 6f 72 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 or = -1;. }else
1fc70 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 {. pList = sq
1fc80 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 lite3SrcListEnla
1fc90 72 67 65 28 64 62 2c 20 70 4c 69 73 74 2c 20 31 rge(db, pList, 1
1fca0 2c 20 70 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 0a , pList->nSrc);.
1fcb0 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 }. if( db->ma
1fcc0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1fcd0 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 sqlite3SrcList
1fce0 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 Delete(db, pList
1fcf0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
1fd00 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26 . }. pItem = &
1fd10 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e pList->a[pList->
1fd20 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66 28 20 70 nSrc-1];. if( p
1fd30 44 61 74 61 62 61 73 65 20 26 26 20 70 44 61 74 Database && pDat
1fd40 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 abase->z==0 ){.
1fd50 20 20 20 70 44 61 74 61 62 61 73 65 20 3d 20 30 pDatabase = 0
1fd60 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61 74 ;. }. if( pDat
1fd70 61 62 61 73 65 20 29 7b 0a 20 20 20 20 70 49 74 abase ){. pIt
1fd80 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 em->zName = sqli
1fd90 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
1fda0 28 64 62 2c 20 70 44 61 74 61 62 61 73 65 29 3b (db, pDatabase);
1fdb0 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 . pItem->zDat
1fdc0 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 4e abase = sqlite3N
1fdd0 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
1fde0 20 70 54 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 pTable);. }els
1fdf0 65 7b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e e{. pItem->zN
1fe00 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d ame = sqlite3Nam
1fe10 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 eFromToken(db, p
1fe20 54 61 62 6c 65 29 3b 0a 20 20 20 20 70 49 74 65 Table);. pIte
1fe30 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 30 m->zDatabase = 0
1fe40 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
1fe50 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 List;.}../*.** A
1fe60 73 73 69 67 6e 20 56 64 62 65 43 75 72 73 6f 72 ssign VdbeCursor
1fe70 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 73 20 74 index numbers t
1fe80 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 o all tables in
1fe90 61 20 53 72 63 4c 69 73 74 0a 2a 2f 0a 76 6f 69 a SrcList.*/.voi
1fea0 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 d sqlite3SrcList
1feb0 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50 61 AssignCursors(Pa
1fec0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 rse *pParse, Src
1fed0 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 List *pList){.
1fee0 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
1fef0 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
1ff00 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 70 4c tem;. assert(pL
1ff10 69 73 74 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 ist || pParse->d
1ff20 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1ff30 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 );. if( pList )
1ff40 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 {. for(i=0, p
1ff50 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 Item=pList->a; i
1ff60 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b <pList->nSrc; i+
1ff70 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 +, pItem++){.
1ff80 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 if( pItem->iC
1ff90 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65 61 6b ursor>=0 ) break
1ffa0 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 ;. pItem->i
1ffb0 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d Cursor = pParse-
1ffc0 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69 >nTab++;. i
1ffd0 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 f( pItem->pSelec
1ffe0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c t ){. sql
1fff0 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 ite3SrcListAssig
20000 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c nCursors(pParse,
20010 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2d pItem->pSelect-
20020 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a >pSrc);. }.
20030 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
20040 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 ** Delete an ent
20050 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e 63 6c ire SrcList incl
20060 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20 73 75 uding all its su
20070 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 76 bstructure..*/.v
20080 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 oid sqlite3SrcLi
20090 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 stDelete(sqlite3
200a0 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 *db, SrcList *p
200b0 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a List){. int i;.
200c0 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
200d0 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
200e0 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 if( pList==0 ) r
200f0 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70 49 74 eturn;. for(pIt
20100 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 em=pList->a, i=0
20110 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b ; i<pList->nSrc;
20120 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i++, pItem++){.
20130 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
20140 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 e(db, pItem->zDa
20150 74 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71 6c tabase);. sql
20160 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
20170 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 Item->zName);.
20180 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
20190 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 db, pItem->zAlia
201a0 73 29 3b 0a 20 20 20 20 69 66 28 20 70 49 74 65 s);. if( pIte
201b0 6d 2d 3e 66 67 2e 69 73 49 6e 64 65 78 65 64 42 m->fg.isIndexedB
201c0 79 20 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 y ) sqlite3DbFre
201d0 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 75 31 2e e(db, pItem->u1.
201e0 7a 49 6e 64 65 78 65 64 42 79 29 3b 0a 20 20 20 zIndexedBy);.
201f0 20 69 66 28 20 70 49 74 65 6d 2d 3e 66 67 2e 69 if( pItem->fg.i
20200 73 54 61 62 46 75 6e 63 20 29 20 73 71 6c 69 74 sTabFunc ) sqlit
20210 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 e3ExprListDelete
20220 28 64 62 2c 20 70 49 74 65 6d 2d 3e 75 31 2e 70 (db, pItem->u1.p
20230 46 75 6e 63 41 72 67 29 3b 0a 20 20 20 20 73 71 FuncArg);. sq
20240 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 lite3DeleteTable
20250 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62 (db, pItem->pTab
20260 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 );. sqlite3Se
20270 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 lectDelete(db, p
20280 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a Item->pSelect);.
20290 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
202a0 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d elete(db, pItem-
202b0 3e 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 >pOn);. sqlit
202c0 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 e3IdListDelete(d
202d0 62 2c 20 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 b, pItem->pUsing
202e0 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
202f0 44 62 46 72 65 65 4e 4e 28 64 62 2c 20 70 4c 69 DbFreeNN(db, pLi
20300 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 st);.}../*.** Th
20310 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
20320 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 lled by the pars
20330 65 72 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20 er to add a new
20340 74 65 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20 65 term to the.** e
20350 6e 64 20 6f 66 20 61 20 67 72 6f 77 69 6e 67 20 nd of a growing
20360 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 FROM clause. Th
20370 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20 e "p" parameter
20380 69 73 20 74 68 65 20 70 61 72 74 20 6f 66 0a 2a is the part of.*
20390 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 * the FROM claus
203a0 65 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 e that has alrea
203b0 64 79 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63 dy been construc
203c0 74 65 64 2e 20 20 22 70 22 20 69 73 20 4e 55 4c ted. "p" is NUL
203d0 4c 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 L.** if this is
203e0 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 6f the first term o
203f0 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 f the FROM claus
20400 65 2e 20 20 70 54 61 62 6c 65 20 61 6e 64 20 70 e. pTable and p
20410 44 61 74 61 62 61 73 65 0a 2a 2a 20 61 72 65 20 Database.** are
20420 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
20430 74 61 62 6c 65 20 61 6e 64 20 64 61 74 61 62 61 table and databa
20440 73 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 se named in the
20450 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d FROM clause term
20460 2e 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 69 ..** pDatabase i
20470 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 64 61 s NULL if the da
20480 74 61 62 61 73 65 20 6e 61 6d 65 20 71 75 61 6c tabase name qual
20490 69 66 69 65 72 20 69 73 20 6d 69 73 73 69 6e 67 ifier is missing
204a0 20 2d 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c 20 - the.** usual
204b0 63 61 73 65 2e 20 20 49 66 20 74 68 65 20 74 65 case. If the te
204c0 72 6d 20 68 61 73 20 61 6e 20 61 6c 69 61 73 2c rm has an alias,
204d0 20 74 68 65 6e 20 70 41 6c 69 61 73 20 70 6f 69 then pAlias poi
204e0 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c nts to the.** al
204f0 69 61 73 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74 ias token. If t
20500 68 65 20 74 65 72 6d 20 69 73 20 61 20 73 75 62 he term is a sub
20510 71 75 65 72 79 2c 20 74 68 65 6e 20 70 53 75 62 query, then pSub
20520 71 75 65 72 79 20 69 73 20 74 68 65 0a 2a 2a 20 query is the.**
20530 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
20540 20 74 68 61 74 20 74 68 65 20 73 75 62 71 75 65 that the subque
20550 72 79 20 65 6e 63 6f 64 65 73 2e 20 20 54 68 65 ry encodes. The
20560 20 70 54 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70 pTable and.** p
20570 44 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 Database paramet
20580 65 72 73 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72 ers are NULL for
20590 20 73 75 62 71 75 65 72 69 65 73 2e 20 20 54 68 subqueries. Th
205a0 65 20 70 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67 e pOn and pUsing
205b0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 .** parameters a
205c0 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f re the content o
205d0 66 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 f the ON and USI
205e0 4e 47 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a NG clauses..**.*
205f0 2a 20 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53 * Return a new S
20600 72 63 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63 rcList which enc
20610 6f 64 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d odes is the FROM
20620 20 77 69 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a with the new.**
20630 20 74 65 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a term added..*/.
20640 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 SrcList *sqlite3
20650 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f SrcListAppendFro
20660 6d 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a mTerm(. Parse *
20670 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
20680 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20690 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 ext */. SrcList
206a0 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
206b0 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 70 61 72 /* The left par
206c0 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c t of the FROM cl
206d0 61 75 73 65 20 61 6c 72 65 61 64 79 20 73 65 65 ause already see
206e0 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 n */. Token *pT
206f0 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f able, /
20700 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 * Name of the ta
20710 62 6c 65 20 74 6f 20 61 64 64 20 74 6f 20 74 68 ble to add to th
20720 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f e FROM clause */
20730 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 . Token *pDatab
20740 61 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 ase, /* Na
20750 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
20760 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 se containing pT
20770 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 able */. Token
20780 2a 70 41 6c 69 61 73 2c 20 20 20 20 20 20 20 20 *pAlias,
20790 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 /* The right-h
207a0 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 and side of the
207b0 41 53 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e AS subexpression
207c0 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 */. Select *pS
207d0 75 62 71 75 65 72 79 2c 20 20 20 20 20 20 2f 2a ubquery, /*
207e0 20 41 20 73 75 62 71 75 65 72 79 20 75 73 65 64 A subquery used
207f0 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 in place of a t
20800 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 able name */. E
20810 78 70 72 20 2a 70 4f 6e 2c 20 20 20 20 20 20 20 xpr *pOn,
20820 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e /* The ON
20830 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 clause of a joi
20840 6e 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 n */. IdList *p
20850 55 73 69 6e 67 20 20 20 20 20 20 20 20 20 20 2f Using /
20860 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 * The USING clau
20870 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a se of a join */.
20880 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c ){. struct SrcL
20890 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
208a0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
208b0 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
208c0 66 28 20 21 70 20 26 26 20 28 70 4f 6e 20 7c 7c f( !p && (pOn ||
208d0 20 70 55 73 69 6e 67 29 20 29 7b 0a 20 20 20 20 pUsing) ){.
208e0 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
208f0 70 50 61 72 73 65 2c 20 22 61 20 4a 4f 49 4e 20 pParse, "a JOIN
20900 63 6c 61 75 73 65 20 69 73 20 72 65 71 75 69 72 clause is requir
20910 65 64 20 62 65 66 6f 72 65 20 25 73 22 2c 20 0a ed before %s", .
20920 20 20 20 20 20 20 28 70 4f 6e 20 3f 20 22 4f 4e (pOn ? "ON
20930 22 20 3a 20 22 55 53 49 4e 47 22 29 0a 20 20 20 " : "USING").
20940 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 );. goto app
20950 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a end_from_error;.
20960 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 }. p = sqlite
20970 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 3SrcListAppend(d
20980 62 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 b, p, pTable, pD
20990 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 atabase);. if(
209a0 70 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f p==0 ){. goto
209b0 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 append_from_err
209c0 6f 72 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 or;. }. assert
209d0 28 20 70 2d 3e 6e 53 72 63 3e 30 20 29 3b 0a 20 ( p->nSrc>0 );.
209e0 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 pItem = &p->a[p
209f0 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 61 73 73 ->nSrc-1];. ass
20a00 65 72 74 28 20 28 70 54 61 62 6c 65 3d 3d 30 29 ert( (pTable==0)
20a10 3d 3d 28 70 44 61 74 61 62 61 73 65 3d 3d 30 29 ==(pDatabase==0)
20a20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 );. assert( pI
20a30 74 65 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 7c 7c tem->zName==0 ||
20a40 20 70 44 61 74 61 62 61 73 65 21 3d 30 20 29 3b pDatabase!=0 );
20a50 0a 20 20 69 66 28 20 49 4e 5f 52 45 4e 41 4d 45 . if( IN_RENAME
20a60 5f 4f 42 4a 45 43 54 20 26 26 20 70 49 74 65 6d _OBJECT && pItem
20a70 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 54 ->zName ){. T
20a80 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 28 oken *pToken = (
20a90 41 4c 57 41 59 53 28 70 44 61 74 61 62 61 73 65 ALWAYS(pDatabase
20aa0 29 20 26 26 20 70 44 61 74 61 62 61 73 65 2d 3e ) && pDatabase->
20ab0 7a 29 20 3f 20 70 44 61 74 61 62 61 73 65 20 3a z) ? pDatabase :
20ac0 20 70 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c pTable;. sql
20ad0 69 74 65 33 52 65 6e 61 6d 65 54 6f 6b 65 6e 4d ite3RenameTokenM
20ae0 61 70 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d ap(pParse, pItem
20af0 2d 3e 7a 4e 61 6d 65 2c 20 70 54 6f 6b 65 6e 29 ->zName, pToken)
20b00 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
20b10 70 41 6c 69 61 73 21 3d 30 20 29 3b 0a 20 20 69 pAlias!=0 );. i
20b20 66 28 20 70 41 6c 69 61 73 2d 3e 6e 20 29 7b 0a f( pAlias->n ){.
20b30 20 20 20 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 pItem->zAlia
20b40 73 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 s = sqlite3NameF
20b50 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 41 6c romToken(db, pAl
20b60 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65 ias);. }. pIte
20b70 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 75 m->pSelect = pSu
20b80 62 71 75 65 72 79 3b 0a 20 20 70 49 74 65 6d 2d bquery;. pItem-
20b90 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20 70 49 >pOn = pOn;. pI
20ba0 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 70 55 tem->pUsing = pU
20bb0 73 69 6e 67 3b 0a 20 20 72 65 74 75 72 6e 20 70 sing;. return p
20bc0 3b 0a 0a 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f ;.. append_from_
20bd0 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28 error:. assert(
20be0 20 70 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 p==0 );. sqlit
20bf0 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
20c00 20 70 4f 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 pOn);. sqlite3
20c10 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c IdListDelete(db,
20c20 20 70 55 73 69 6e 67 29 3b 0a 20 20 73 71 6c 69 pUsing);. sqli
20c30 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 te3SelectDelete(
20c40 64 62 2c 20 70 53 75 62 71 75 65 72 79 29 3b 0a db, pSubquery);.
20c50 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
20c60 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e 44 45 *.** Add an INDE
20c70 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 49 4e XED BY or NOT IN
20c80 44 45 58 45 44 20 63 6c 61 75 73 65 20 74 6f 20 DEXED clause to
20c90 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
20ca0 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c 65 6d y added .** elem
20cb0 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 72 63 ent of the sourc
20cc0 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20 61 73 e-list passed as
20cd0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
20ce0 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ment..*/.void sq
20cf0 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 lite3SrcListInde
20d00 78 65 64 42 79 28 50 61 72 73 65 20 2a 70 50 61 xedBy(Parse *pPa
20d10 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c rse, SrcList *p,
20d20 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64 Token *pIndexed
20d30 42 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 By){. assert( p
20d40 49 6e 64 65 78 65 64 42 79 21 3d 30 20 29 3b 0a IndexedBy!=0 );.
20d50 20 20 69 66 28 20 70 20 26 26 20 70 49 6e 64 65 if( p && pInde
20d60 78 65 64 42 79 2d 3e 6e 3e 30 20 29 7b 0a 20 20 xedBy->n>0 ){.
20d70 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
20d80 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
20d90 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 53 72 assert( p->nSr
20da0 63 3e 30 20 29 3b 0a 20 20 20 20 70 49 74 65 6d c>0 );. pItem
20db0 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 = &p->a[p->nSrc
20dc0 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 -1];. assert(
20dd0 20 70 49 74 65 6d 2d 3e 66 67 2e 6e 6f 74 49 6e pItem->fg.notIn
20de0 64 65 78 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 dexed==0 );.
20df0 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 66 assert( pItem->f
20e00 67 2e 69 73 49 6e 64 65 78 65 64 42 79 3d 3d 30 g.isIndexedBy==0
20e10 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
20e20 70 49 74 65 6d 2d 3e 66 67 2e 69 73 54 61 62 46 pItem->fg.isTabF
20e30 75 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 unc==0 );. if
20e40 28 20 70 49 6e 64 65 78 65 64 42 79 2d 3e 6e 3d ( pIndexedBy->n=
20e50 3d 31 20 26 26 20 21 70 49 6e 64 65 78 65 64 42 =1 && !pIndexedB
20e60 79 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 2f 2a y->z ){. /*
20e70 20 41 20 22 4e 4f 54 20 49 4e 44 45 58 45 44 22 A "NOT INDEXED"
20e80 20 63 6c 61 75 73 65 20 77 61 73 20 73 75 70 70 clause was supp
20e90 6c 69 65 64 2e 20 53 65 65 20 70 61 72 73 65 2e lied. See parse.
20ea0 79 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 y . ** cons
20eb0 74 72 75 63 74 20 22 69 6e 64 65 78 65 64 5f 6f truct "indexed_o
20ec0 70 74 22 20 66 6f 72 20 64 65 74 61 69 6c 73 2e pt" for details.
20ed0 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d */. pItem-
20ee0 3e 66 67 2e 6e 6f 74 49 6e 64 65 78 65 64 20 3d >fg.notIndexed =
20ef0 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 1;. }else{.
20f00 20 20 20 20 20 70 49 74 65 6d 2d 3e 75 31 2e 7a pItem->u1.z
20f10 49 6e 64 65 78 65 64 42 79 20 3d 20 73 71 6c 69 IndexedBy = sqli
20f20 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
20f30 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 6e (pParse->db, pIn
20f40 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20 20 20 dexedBy);.
20f50 70 49 74 65 6d 2d 3e 66 67 2e 69 73 49 6e 64 65 pItem->fg.isInde
20f60 78 65 64 42 79 20 3d 20 31 3b 0a 20 20 20 20 7d xedBy = 1;. }
20f70 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 . }.}../*.** Ad
20f80 64 20 74 68 65 20 6c 69 73 74 20 6f 66 20 66 75 d the list of fu
20f90 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 nction arguments
20fa0 20 74 6f 20 74 68 65 20 53 72 63 4c 69 73 74 20 to the SrcList
20fb0 65 6e 74 72 79 20 66 6f 72 20 61 0a 2a 2a 20 74 entry for a.** t
20fc0 61 62 6c 65 2d 76 61 6c 75 65 64 2d 66 75 6e 63 able-valued-func
20fd0 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 tion..*/.void sq
20fe0 6c 69 74 65 33 53 72 63 4c 69 73 74 46 75 6e 63 lite3SrcListFunc
20ff0 41 72 67 73 28 50 61 72 73 65 20 2a 70 50 61 72 Args(Parse *pPar
21000 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 se, SrcList *p,
21010 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 ExprList *pList)
21020 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 {. if( p ){.
21030 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
21040 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 item *pItem = &p
21050 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a ->a[p->nSrc-1];.
21060 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 assert( pIte
21070 6d 2d 3e 66 67 2e 6e 6f 74 49 6e 64 65 78 65 64 m->fg.notIndexed
21080 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
21090 74 28 20 70 49 74 65 6d 2d 3e 66 67 2e 69 73 49 t( pItem->fg.isI
210a0 6e 64 65 78 65 64 42 79 3d 3d 30 20 29 3b 0a 20 ndexedBy==0 );.
210b0 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d assert( pItem
210c0 2d 3e 66 67 2e 69 73 54 61 62 46 75 6e 63 3d 3d ->fg.isTabFunc==
210d0 30 20 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 0 );. pItem->
210e0 75 31 2e 70 46 75 6e 63 41 72 67 20 3d 20 70 4c u1.pFuncArg = pL
210f0 69 73 74 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e ist;. pItem->
21100 66 67 2e 69 73 54 61 62 46 75 6e 63 20 3d 20 31 fg.isTabFunc = 1
21110 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
21120 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 qlite3ExprListDe
21130 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c lete(pParse->db,
21140 20 70 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 0a pList);. }.}..
21150 2f 2a 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 64 /*.** When build
21160 69 6e 67 20 75 70 20 61 20 46 52 4f 4d 20 63 6c ing up a FROM cl
21170 61 75 73 65 20 69 6e 20 74 68 65 20 70 61 72 73 ause in the pars
21180 65 72 2c 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 er, the join ope
21190 72 61 74 6f 72 0a 2a 2a 20 69 73 20 69 6e 69 74 rator.** is init
211a0 69 61 6c 6c 79 20 61 74 74 61 63 68 65 64 20 74 ially attached t
211b0 6f 20 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 o the left opera
211c0 6e 64 2e 20 20 42 75 74 20 74 68 65 20 63 6f 64 nd. But the cod
211d0 65 20 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 e generator.** e
211e0 78 70 65 63 74 73 20 74 68 65 20 6a 6f 69 6e 20 xpects the join
211f0 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f operator to be o
21200 6e 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 n the right oper
21210 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 and. This routi
21220 6e 65 0a 2a 2a 20 53 68 69 66 74 73 20 61 6c 6c ne.** Shifts all
21230 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 73 20 join operators
21240 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 from left to rig
21250 68 74 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 ht for an entire
21260 20 46 52 4f 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e FROM.** clause.
21270 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 .**.** Example:
21280 53 75 70 70 6f 73 65 20 74 68 65 20 6a 6f 69 6e Suppose the join
21290 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a is like this:.*
212a0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 41 *.** A
212b0 20 6e 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a natural cross j
212c0 6f 69 6e 20 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 oin B.**.** The
212d0 6f 70 65 72 61 74 6f 72 20 69 73 20 22 6e 61 74 operator is "nat
212e0 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 22 ural cross join"
212f0 2e 20 20 54 68 65 20 41 20 61 6e 64 20 42 20 6f . The A and B o
21300 70 65 72 61 6e 64 73 20 61 72 65 20 73 74 6f 72 perands are stor
21310 65 64 0a 2a 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d ed.** in p->a[0]
21320 20 61 6e 64 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 and p->a[1], re
21330 73 70 65 63 74 69 76 65 6c 79 2e 20 20 54 68 65 spectively. The
21340 20 70 61 72 73 65 72 20 69 6e 69 74 69 61 6c 6c parser initiall
21350 79 20 73 74 6f 72 65 73 20 74 68 65 0a 2a 2a 20 y stores the.**
21360 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 41 2e operator with A.
21370 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 This routine s
21380 68 69 66 74 73 20 74 68 61 74 20 6f 70 65 72 61 hifts that opera
21390 74 6f 72 20 6f 76 65 72 20 74 6f 20 42 2e 0a 2a tor over to B..*
213a0 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 /.void sqlite3Sr
213b0 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 cListShiftJoinTy
213c0 70 65 28 53 72 63 4c 69 73 74 20 2a 70 29 7b 0a pe(SrcList *p){.
213d0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 if( p ){. i
213e0 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
213f0 70 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 p->nSrc-1; i>0;
21400 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 i--){. p->a
21410 5b 69 5d 2e 66 67 2e 6a 6f 69 6e 74 79 70 65 20 [i].fg.jointype
21420 3d 20 70 2d 3e 61 5b 69 2d 31 5d 2e 66 67 2e 6a = p->a[i-1].fg.j
21430 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 0a 20 ointype;. }.
21440 20 20 20 70 2d 3e 61 5b 30 5d 2e 66 67 2e 6a 6f p->a[0].fg.jo
21450 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20 7d 0a intype = 0;. }.
21460 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
21470 65 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72 20 e VDBE code for
21480 61 20 42 45 47 49 4e 20 73 74 61 74 65 6d 65 6e a BEGIN statemen
21490 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 t..*/.void sqlit
214a0 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 e3BeginTransacti
214b0 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 on(Parse *pParse
214c0 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73 , int type){. s
214d0 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 qlite3 *db;. Vd
214e0 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a be *v;. int i;.
214f0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 . assert( pPars
21500 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 e!=0 );. db = p
21510 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 Parse->db;. ass
21520 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 ert( db!=0 );.
21530 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 if( sqlite3AuthC
21540 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c heck(pParse, SQL
21550 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c ITE_TRANSACTION,
21560 20 22 42 45 47 49 4e 22 2c 20 30 2c 20 30 29 20 "BEGIN", 0, 0)
21570 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 ){. return;.
21580 20 7d 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 }. v = sqlite3
21590 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
215a0 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75 . if( !v ) retu
215b0 72 6e 3b 0a 20 20 69 66 28 20 74 79 70 65 3d 3d rn;. if( type==
215c0 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20 7c 7c 20 TK_IMMEDIATE ||
215d0 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c 55 53 49 type==TK_EXCLUSI
215e0 56 45 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d VE ){. for(i=
215f0 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
21600 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 +){. sqlite
21610 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21620 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 P_Transaction, i
21630 2c 20 28 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c , (type==TK_EXCL
21640 55 53 49 56 45 29 2b 31 29 3b 0a 20 20 20 20 20 USIVE)+1);.
21650 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 sqlite3VdbeUses
21660 42 74 72 65 65 28 76 2c 20 69 29 3b 0a 20 20 20 Btree(v, i);.
21670 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 }. }. sqlite3
21680 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21690 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20 _AutoCommit, 0,
216a0 30 2c 20 28 74 79 70 65 3d 3d 54 4b 5f 43 4f 4e 0, (type==TK_CON
216b0 43 55 52 52 45 4e 54 29 29 3b 0a 7d 0a 0a 2f 2a CURRENT));.}../*
216c0 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 .** Generate VDB
216d0 45 20 63 6f 64 65 20 66 6f 72 20 61 20 43 4f 4d E code for a COM
216e0 4d 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 MIT or ROLLBACK
216f0 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 43 6f statement..** Co
21700 64 65 20 66 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 de for ROLLBACK
21710 69 73 20 67 65 6e 65 72 61 74 65 64 20 69 66 20 is generated if
21720 65 54 79 70 65 3d 3d 54 4b 5f 52 4f 4c 4c 42 41 eType==TK_ROLLBA
21730 43 4b 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 2a CK. Otherwise.*
21740 2a 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 * code is genera
21750 74 65 64 20 66 6f 72 20 61 20 43 4f 4d 4d 49 54 ted for a COMMIT
21760 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
21770 33 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 3EndTransaction(
21780 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
21790 6e 74 20 65 54 79 70 65 29 7b 0a 20 20 56 64 62 nt eType){. Vdb
217a0 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 73 52 6f e *v;. int isRo
217b0 6c 6c 62 61 63 6b 3b 0a 0a 20 20 61 73 73 65 72 llback;.. asser
217c0 74 28 20 70 50 61 72 73 65 21 3d 30 20 29 3b 0a t( pParse!=0 );.
217d0 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
217e0 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 ->db!=0 );. ass
217f0 65 72 74 28 20 65 54 79 70 65 3d 3d 54 4b 5f 43 ert( eType==TK_C
21800 4f 4d 4d 49 54 20 7c 7c 20 65 54 79 70 65 3d 3d OMMIT || eType==
21810 54 4b 5f 45 4e 44 20 7c 7c 20 65 54 79 70 65 3d TK_END || eType=
21820 3d 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a =TK_ROLLBACK );.
21830 20 20 69 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 65 isRollback = e
21840 54 79 70 65 3d 3d 54 4b 5f 52 4f 4c 4c 42 41 43 Type==TK_ROLLBAC
21850 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 K;. if( sqlite3
21860 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
21870 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 , SQLITE_TRANSAC
21880 54 49 4f 4e 2c 20 0a 20 20 20 20 20 20 20 69 73 TION, . is
21890 52 6f 6c 6c 62 61 63 6b 20 3f 20 22 52 4f 4c 4c Rollback ? "ROLL
218a0 42 41 43 4b 22 20 3a 20 22 43 4f 4d 4d 49 54 22 BACK" : "COMMIT"
218b0 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 , 0, 0) ){. r
218c0 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d eturn;. }. v =
218d0 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
218e0 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 pParse);. if( v
218f0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
21900 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21910 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 69 AutoCommit, 1, i
21920 73 52 6f 6c 6c 62 61 63 6b 29 3b 0a 20 20 7d 0a sRollback);. }.
21930 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
21940 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
21950 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 by the parser w
21960 68 65 6e 20 69 74 20 70 61 72 73 65 73 20 61 20 hen it parses a
21970 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 72 65 61 74 command to creat
21980 65 2c 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 72 e,.** release or
21990 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20 53 51 4c rollback an SQL
219a0 20 73 61 76 65 70 6f 69 6e 74 2e 20 0a 2a 2f 0a savepoint. .*/.
219b0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65 void sqlite3Save
219c0 70 6f 69 6e 74 28 50 61 72 73 65 20 2a 70 50 61 point(Parse *pPa
219d0 72 73 65 2c 20 69 6e 74 20 6f 70 2c 20 54 6f 6b rse, int op, Tok
219e0 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68 en *pName){. ch
219f0 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 ar *zName = sqli
21a00 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
21a10 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 (pParse->db, pNa
21a20 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 me);. if( zName
21a30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 ){. Vdbe *v
21a40 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
21a50 28 70 50 61 72 73 65 29 3b 0a 23 69 66 6e 64 65 (pParse);.#ifnde
21a60 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
21a70 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 THORIZATION.
21a80 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
21a90 72 20 2a 20 63 6f 6e 73 74 20 61 7a 5b 5d 20 3d r * const az[] =
21aa0 20 7b 20 22 42 45 47 49 4e 22 2c 20 22 52 45 4c { "BEGIN", "REL
21ab0 45 41 53 45 22 2c 20 22 52 4f 4c 4c 42 41 43 4b EASE", "ROLLBACK
21ac0 22 20 7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 " };. assert(
21ad0 20 21 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 !SAVEPOINT_BEGI
21ae0 4e 20 26 26 20 53 41 56 45 50 4f 49 4e 54 5f 52 N && SAVEPOINT_R
21af0 45 4c 45 41 53 45 3d 3d 31 20 26 26 20 53 41 56 ELEASE==1 && SAV
21b00 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3d EPOINT_ROLLBACK=
21b10 3d 32 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 =2 );.#endif.
21b20 20 69 66 28 20 21 76 20 7c 7c 20 73 71 6c 69 74 if( !v || sqlit
21b30 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
21b40 73 65 2c 20 53 51 4c 49 54 45 5f 53 41 56 45 50 se, SQLITE_SAVEP
21b50 4f 49 4e 54 2c 20 61 7a 5b 6f 70 5d 2c 20 7a 4e OINT, az[op], zN
21b60 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 ame, 0) ){.
21b70 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
21b80 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 Parse->db, zName
21b90 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b );. return;
21ba0 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
21bb0 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
21bc0 4f 50 5f 53 61 76 65 70 6f 69 6e 74 2c 20 6f 70 OP_Savepoint, op
21bd0 2c 20 30 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 50 , 0, 0, zName, P
21be0 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 4_DYNAMIC);. }.
21bf0 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 }../*.** Make su
21c00 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 re the TEMP data
21c10 62 61 73 65 20 69 73 20 6f 70 65 6e 20 61 6e 64 base is open and
21c20 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 available for u
21c30 73 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 se. Return.** t
21c40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 he number of err
21c50 6f 72 73 2e 20 20 4c 65 61 76 65 20 61 6e 79 20 ors. Leave any
21c60 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 error messages i
21c70 6e 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 n the pParse str
21c80 75 63 74 75 72 65 2e 0a 2a 2f 0a 69 6e 74 20 73 ucture..*/.int s
21c90 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 qlite3OpenTempDa
21ca0 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 70 50 tabase(Parse *pP
21cb0 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 arse){. sqlite3
21cc0 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
21cd0 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 b;. if( db->aDb
21ce0 5b 31 5d 2e 70 42 74 3d 3d 30 20 26 26 20 21 70 [1].pBt==0 && !p
21cf0 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 Parse->explain )
21d00 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 {. int rc;.
21d10 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 Btree *pBt;.
21d20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 static const i
21d30 6e 74 20 66 6c 61 67 73 20 3d 20 0a 20 20 20 20 nt flags = .
21d40 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 SQLITE_OPE
21d50 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20 N_READWRITE |.
21d60 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f SQLITE_O
21d70 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 PEN_CREATE |.
21d80 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
21d90 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 EN_EXCLUSIVE |.
21da0 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
21db0 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
21dc0 53 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 SE |. S
21dd0 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f QLITE_OPEN_TEMP_
21de0 44 42 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 DB;.. rc = sq
21df0 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 64 lite3BtreeOpen(d
21e00 62 2d 3e 70 56 66 73 2c 20 30 2c 20 64 62 2c 20 b->pVfs, 0, db,
21e10 26 70 42 74 2c 20 30 2c 20 66 6c 61 67 73 29 3b &pBt, 0, flags);
21e20 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
21e30 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
21e40 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
21e50 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 pParse, "unable
21e60 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 to open a tempor
21e70 61 72 79 20 64 61 74 61 62 61 73 65 20 22 0a 20 ary database ".
21e80 20 20 20 20 20 20 20 22 66 69 6c 65 20 66 6f 72 "file for
21e90 20 73 74 6f 72 69 6e 67 20 74 65 6d 70 6f 72 61 storing tempora
21ea0 72 79 20 74 61 62 6c 65 73 22 29 3b 0a 20 20 20 ry tables");.
21eb0 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 pParse->rc =
21ec0 72 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e rc;. return
21ed0 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 1;. }. db
21ee0 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 20 3d 20 70 ->aDb[1].pBt = p
21ef0 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 Bt;. assert(
21f00 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 db->aDb[1].pSche
21f10 6d 61 20 29 3b 0a 20 20 20 20 69 66 28 20 53 51 ma );. if( SQ
21f20 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 LITE_NOMEM==sqli
21f30 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 te3BtreeSetPageS
21f40 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e 6e 65 78 ize(pBt, db->nex
21f50 74 50 61 67 65 73 69 7a 65 2c 20 2d 31 2c 20 30 tPagesize, -1, 0
21f60 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) ){. sqlit
21f70 65 33 4f 6f 6d 46 61 75 6c 74 28 64 62 29 3b 0a e3OomFault(db);.
21f80 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
21f90 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
21fa0 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn 0;.}../*.** R
21fb0 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 ecord the fact t
21fc0 68 61 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 hat the schema c
21fd0 6f 6f 6b 69 65 20 77 69 6c 6c 20 6e 65 65 64 20 ookie will need
21fe0 74 6f 20 62 65 20 76 65 72 69 66 69 65 64 0a 2a to be verified.*
21ff0 2a 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 69 * for database i
22000 44 62 2e 20 20 54 68 65 20 63 6f 64 65 20 74 6f Db. The code to
22010 20 61 63 74 75 61 6c 6c 79 20 76 65 72 69 66 79 actually verify
22020 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b the schema cook
22030 69 65 0a 2a 2a 20 77 69 6c 6c 20 6f 63 63 75 72 ie.** will occur
22040 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
22050 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 56 44 42 he top-level VDB
22060 45 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 67 65 E and will be ge
22070 6e 65 72 61 74 65 64 0a 2a 2a 20 6c 61 74 65 72 nerated.** later
22080 2c 20 62 79 20 73 71 6c 69 74 65 33 46 69 6e 69 , by sqlite3Fini
22090 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a 76 shCoding()..*/.v
220a0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 oid sqlite3CodeV
220b0 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 erifySchema(Pars
220c0 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
220d0 44 62 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 Db){. Parse *pT
220e0 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 oplevel = sqlite
220f0 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 3ParseToplevel(p
22100 50 61 72 73 65 29 3b 0a 0a 20 20 61 73 73 65 72 Parse);.. asser
22110 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 t( iDb>=0 && iDb
22120 3c 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 <pParse->db->nDb
22130 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
22140 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 arse->db->aDb[iD
22150 62 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69 44 62 b].pBt!=0 || iDb
22160 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==1 );. assert(
22170 20 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41 58 5f iDb<SQLITE_MAX_
22180 41 54 54 41 43 48 45 44 2b 32 20 29 3b 0a 20 20 ATTACHED+2 );.
22190 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 assert( sqlite3S
221a0 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 70 chemaMutexHeld(p
221b0 50 61 72 73 65 2d 3e 64 62 2c 20 69 44 62 2c 20 Parse->db, iDb,
221c0 30 29 20 29 3b 0a 20 20 69 66 28 20 44 62 4d 61 0) );. if( DbMa
221d0 73 6b 54 65 73 74 28 70 54 6f 70 6c 65 76 65 6c skTest(pToplevel
221e0 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 2c 20 69 44 ->cookieMask, iD
221f0 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 44 62 4d b)==0 ){. DbM
22200 61 73 6b 53 65 74 28 70 54 6f 70 6c 65 76 65 6c askSet(pToplevel
22210 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 2c 20 69 44 ->cookieMask, iD
22220 62 29 3b 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 b);. if( !OMI
22230 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d T_TEMPDB && iDb=
22240 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =1 ){. sqli
22250 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 te3OpenTempDatab
22260 61 73 65 28 70 54 6f 70 6c 65 76 65 6c 29 3b 0a ase(pToplevel);.
22270 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
22280 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a ** If argument z
22290 44 62 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e Db is NULL, then
222a0 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 43 6f 64 call sqlite3Cod
222b0 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 29 20 eVerifySchema()
222c0 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 61 74 74 for each .** att
222d0 61 63 68 65 64 20 64 61 74 61 62 61 73 65 2e 20 ached database.
222e0 4f 74 68 65 72 77 69 73 65 2c 20 69 6e 76 6f 6b Otherwise, invok
222f0 65 20 69 74 20 66 6f 72 20 74 68 65 20 64 61 74 e it for the dat
22300 61 62 61 73 65 20 6e 61 6d 65 64 20 7a 44 62 20 abase named zDb
22310 6f 6e 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 only..*/.void sq
22320 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 4e lite3CodeVerifyN
22330 61 6d 65 64 53 63 68 65 6d 61 28 50 61 72 73 65 amedSchema(Parse
22340 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 *pParse, const
22350 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 73 71 char *zDb){. sq
22360 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
22370 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b se->db;. int i;
22380 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
22390 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
223a0 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e Db *pDb = &db->
223b0 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 aDb[i];. if(
223c0 70 44 62 2d 3e 70 42 74 20 26 26 20 28 21 7a 44 pDb->pBt && (!zD
223d0 62 20 7c 7c 20 30 3d 3d 73 71 6c 69 74 65 33 53 b || 0==sqlite3S
223e0 74 72 49 43 6d 70 28 7a 44 62 2c 20 70 44 62 2d trICmp(zDb, pDb-
223f0 3e 7a 44 62 53 4e 61 6d 65 29 29 20 29 7b 0a 20 >zDbSName)) ){.
22400 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 sqlite3Code
22410 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 VerifySchema(pPa
22420 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 rse, i);. }.
22430 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 }.}../*.** Gene
22440 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 rate VDBE code t
22450 68 61 74 20 70 72 65 70 61 72 65 73 20 66 6f 72 hat prepares for
22460 20 64 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61 74 doing an operat
22470 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67 68 ion that.** migh
22480 74 20 63 68 61 6e 67 65 20 74 68 65 20 64 61 74 t change the dat
22490 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 abase..**.** Thi
224a0 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73 s routine starts
224b0 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 a new transacti
224c0 6f 6e 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74 on if we are not
224d0 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 0a already within.
224e0 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ** a transaction
224f0 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 6c 72 . If we are alr
22500 65 61 64 79 20 77 69 74 68 69 6e 20 61 20 74 72 eady within a tr
22510 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 ansaction, then
22520 61 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20 a checkpoint.**
22530 69 73 20 73 65 74 20 69 66 20 74 68 65 20 73 65 is set if the se
22540 74 53 74 61 74 65 6d 65 6e 74 20 70 61 72 61 6d tStatement param
22550 65 74 65 72 20 69 73 20 74 72 75 65 2e 20 20 41 eter is true. A
22560 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 checkpoint shou
22570 6c 64 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f 72 ld.** be set for
22580 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 operations that
22590 20 6d 69 67 68 74 20 66 61 69 6c 20 28 64 75 65 might fail (due
225a0 20 74 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 to a constraint
225b0 29 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 ) part of.** the
225c0 20 77 61 79 20 74 68 72 6f 75 67 68 20 61 6e 64 way through and
225d0 20 77 68 69 63 68 20 77 69 6c 6c 20 6e 65 65 64 which will need
225e0 20 74 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77 72 to undo some wr
225f0 69 74 65 73 20 77 69 74 68 6f 75 74 20 68 61 76 ites without hav
22600 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 61 ing to.** rollba
22610 63 6b 20 74 68 65 20 77 68 6f 6c 65 20 74 72 61 ck the whole tra
22620 6e 73 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20 6f nsaction. For o
22630 70 65 72 61 74 69 6f 6e 73 20 77 68 65 72 65 20 perations where
22640 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a all constraints.
22650 2a 2a 20 63 61 6e 20 62 65 20 63 68 65 63 6b 65 ** can be checke
22660 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 d before any cha
22670 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f nges are made to
22680 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 the database, i
22690 74 20 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e 65 t is never.** ne
226a0 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20 cessary to undo
226b0 61 20 77 72 69 74 65 20 61 6e 64 20 74 68 65 20 a write and the
226c0 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c checkpoint shoul
226d0 64 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2f d not be set..*/
226e0 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 .void sqlite3Beg
226f0 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e inWriteOperation
22700 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
22710 69 6e 74 20 73 65 74 53 74 61 74 65 6d 65 6e 74 int setStatement
22720 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 50 61 , int iDb){. Pa
22730 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d rse *pToplevel =
22740 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 sqlite3ParseTop
22750 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 level(pParse);.
22760 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 sqlite3CodeVeri
22770 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c fySchema(pParse,
22780 20 69 44 62 29 3b 0a 20 20 44 62 4d 61 73 6b 53 iDb);. DbMaskS
22790 65 74 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 77 72 et(pToplevel->wr
227a0 69 74 65 4d 61 73 6b 2c 20 69 44 62 29 3b 0a 20 iteMask, iDb);.
227b0 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 69 73 4d 75 pToplevel->isMu
227c0 6c 74 69 57 72 69 74 65 20 7c 3d 20 73 65 74 53 ltiWrite |= setS
227d0 74 61 74 65 6d 65 6e 74 3b 0a 7d 0a 0a 2f 2a 0a tatement;.}../*.
227e0 2a 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 ** Indicate that
227f0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 63 the statement c
22800 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 urrently under c
22810 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6d 69 67 68 onstruction migh
22820 74 20 77 72 69 74 65 0a 2a 2a 20 6d 6f 72 65 20 t write.** more
22830 74 68 61 6e 20 6f 6e 65 20 65 6e 74 72 79 20 28 than one entry (
22840 65 78 61 6d 70 6c 65 3a 20 64 65 6c 65 74 69 6e example: deletin
22850 67 20 6f 6e 65 20 72 6f 77 20 74 68 65 6e 20 69 g one row then i
22860 6e 73 65 72 74 69 6e 67 20 61 6e 6f 74 68 65 72 nserting another
22870 2c 0a 2a 2a 20 69 6e 73 65 72 74 69 6e 67 20 6d ,.** inserting m
22880 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 69 6e 20 ultiple rows in
22890 61 20 74 61 62 6c 65 2c 20 6f 72 20 69 6e 73 65 a table, or inse
228a0 72 74 69 6e 67 20 61 20 72 6f 77 20 61 6e 64 20 rting a row and
228b0 69 6e 64 65 78 20 65 6e 74 72 69 65 73 2e 29 0a index entries.).
228c0 2a 2a 20 49 66 20 61 6e 20 61 62 6f 72 74 20 6f ** If an abort o
228d0 63 63 75 72 73 20 61 66 74 65 72 20 73 6f 6d 65 ccurs after some
228e0 20 6f 66 20 74 68 65 73 65 20 77 72 69 74 65 73 of these writes
228f0 20 68 61 76 65 20 63 6f 6d 70 6c 65 74 65 64 2c have completed,
22900 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 0a 2a 2a then it will.**
22910 20 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f be necessary to
22920 20 75 6e 64 6f 20 74 68 65 20 63 6f 6d 70 6c 65 undo the comple
22930 74 65 64 20 77 72 69 74 65 73 2e 0a 2a 2f 0a 76 ted writes..*/.v
22940 6f 69 64 20 73 71 6c 69 74 65 33 4d 75 6c 74 69 oid sqlite3Multi
22950 57 72 69 74 65 28 50 61 72 73 65 20 2a 70 50 61 Write(Parse *pPa
22960 72 73 65 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 rse){. Parse *p
22970 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 Toplevel = sqlit
22980 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 e3ParseToplevel(
22990 70 50 61 72 73 65 29 3b 0a 20 20 70 54 6f 70 6c pParse);. pTopl
229a0 65 76 65 6c 2d 3e 69 73 4d 75 6c 74 69 57 72 69 evel->isMultiWri
229b0 74 65 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a te = 1;.}../* .*
229c0 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 * The code gener
229d0 61 74 6f 72 20 63 61 6c 6c 73 20 74 68 69 73 20 ator calls this
229e0 72 6f 75 74 69 6e 65 20 69 66 20 69 73 20 64 69 routine if is di
229f0 73 63 6f 76 65 72 73 20 74 68 61 74 20 69 74 20 scovers that it
22a00 69 73 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 is.** possible t
22a10 6f 20 61 62 6f 72 74 20 61 20 73 74 61 74 65 6d o abort a statem
22a20 65 6e 74 20 70 72 69 6f 72 20 74 6f 20 63 6f 6d ent prior to com
22a30 70 6c 65 74 69 6f 6e 2e 20 20 49 6e 20 6f 72 64 pletion. In ord
22a40 65 72 20 74 6f 20 0a 2a 2a 20 70 65 72 66 6f 72 er to .** perfor
22a50 6d 20 74 68 69 73 20 61 62 6f 72 74 20 77 69 74 m this abort wit
22a60 68 6f 75 74 20 63 6f 72 72 75 70 74 69 6e 67 20 hout corrupting
22a70 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 77 65 the database, we
22a80 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 0a 2a 2a need to make.**
22a90 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 sure that the s
22aa0 74 61 74 65 6d 65 6e 74 20 69 73 20 70 72 6f 74 tatement is prot
22ab0 65 63 74 65 64 20 62 79 20 61 20 73 74 61 74 65 ected by a state
22ac0 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
22ad0 2e 0a 2a 2a 0a 2a 2a 20 54 65 63 68 6e 69 63 61 ..**.** Technica
22ae0 6c 6c 79 2c 20 77 65 20 6f 6e 6c 79 20 6e 65 65 lly, we only nee
22af0 64 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61 79 d to set the may
22b00 41 62 6f 72 74 20 66 6c 61 67 20 69 66 20 74 68 Abort flag if th
22b10 65 0a 2a 2a 20 69 73 4d 75 6c 74 69 57 72 69 74 e.** isMultiWrit
22b20 65 20 66 6c 61 67 20 77 61 73 20 70 72 65 76 69 e flag was previ
22b30 6f 75 73 6c 79 20 73 65 74 2e 20 20 54 68 65 72 ously set. Ther
22b40 65 20 69 73 20 61 20 74 69 6d 65 20 64 65 70 65 e is a time depe
22b50 6e 64 65 6e 63 79 0a 2a 2a 20 73 75 63 68 20 74 ndency.** such t
22b60 68 61 74 20 74 68 65 20 61 62 6f 72 74 20 6d 75 hat the abort mu
22b70 73 74 20 6f 63 63 75 72 20 61 66 74 65 72 20 74 st occur after t
22b80 68 65 20 6d 75 6c 74 69 77 72 69 74 65 2e 20 20 he multiwrite.
22b90 54 68 69 73 20 6d 61 6b 65 73 0a 2a 2a 20 73 6f This makes.** so
22ba0 6d 65 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e me statements in
22bb0 76 6f 6c 76 69 6e 67 20 74 68 65 20 52 45 50 4c volving the REPL
22bc0 41 43 45 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 ACE conflict res
22bd0 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 olution algorith
22be0 6d 0a 2a 2a 20 67 6f 20 61 20 6c 69 74 74 6c 65 m.** go a little
22bf0 20 66 61 73 74 65 72 2e 20 20 42 75 74 20 74 61 faster. But ta
22c00 6b 69 6e 67 20 61 64 76 61 6e 74 61 67 65 20 6f king advantage o
22c10 66 20 74 68 69 73 20 74 69 6d 65 20 64 65 70 65 f this time depe
22c20 6e 64 65 6e 63 79 0a 2a 2a 20 6d 61 6b 65 73 20 ndency.** makes
22c30 69 74 20 6d 6f 72 65 20 64 69 66 66 69 63 75 6c it more difficul
22c40 74 20 74 6f 20 70 72 6f 76 65 20 74 68 61 74 20 t to prove that
22c50 74 68 65 20 63 6f 64 65 20 69 73 20 63 6f 72 72 the code is corr
22c60 65 63 74 20 28 69 6e 20 0a 2a 2a 20 70 61 72 74 ect (in .** part
22c70 69 63 75 6c 61 72 2c 20 69 74 20 70 72 65 76 65 icular, it preve
22c80 6e 74 73 20 75 73 20 66 72 6f 6d 20 77 72 69 74 nts us from writ
22c90 69 6e 67 20 61 6e 20 65 66 66 65 63 74 69 76 65 ing an effective
22ca0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
22cb0 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 41 73 73 on of sqlite3Ass
22cc0 65 72 74 4d 61 79 41 62 6f 72 74 28 29 29 20 61 ertMayAbort()) a
22cd0 6e 64 20 73 6f 20 77 65 20 68 61 76 65 20 63 68 nd so we have ch
22ce0 6f 73 65 6e 0a 2a 2a 20 74 6f 20 74 61 6b 65 20 osen.** to take
22cf0 74 68 65 20 73 61 66 65 20 72 6f 75 74 65 20 61 the safe route a
22d00 6e 64 20 73 6b 69 70 20 74 68 65 20 6f 70 74 69 nd skip the opti
22d10 6d 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 mization..*/.voi
22d20 64 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 d sqlite3MayAbor
22d30 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 t(Parse *pParse)
22d40 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c {. Parse *pTopl
22d50 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 evel = sqlite3Pa
22d60 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 rseToplevel(pPar
22d70 73 65 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c se);. pToplevel
22d80 2d 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31 3b 0a ->mayAbort = 1;.
22d90 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e }../*.** Code an
22da0 20 4f 50 5f 48 61 6c 74 20 74 68 61 74 20 63 61 OP_Halt that ca
22db0 75 73 65 73 20 74 68 65 20 76 64 62 65 20 74 6f uses the vdbe to
22dc0 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 return an SQLIT
22dd0 45 5f 43 4f 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 E_CONSTRAINT.**
22de0 65 72 72 6f 72 2e 20 54 68 65 20 6f 6e 45 72 72 error. The onErr
22df0 6f 72 20 70 61 72 61 6d 65 74 65 72 20 64 65 74 or parameter det
22e00 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20 28 69 ermines which (i
22e10 66 20 61 6e 79 29 20 6f 66 20 74 68 65 20 73 74 f any) of the st
22e20 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 2f 6f atement.** and/o
22e30 72 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 r current transa
22e40 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 ction is rolled
22e50 62 61 63 6b 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 back..*/.void sq
22e60 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 lite3HaltConstra
22e70 69 6e 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 int(. Parse *pP
22e80 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 arse, /* Pars
22e90 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
22ea0 20 69 6e 74 20 65 72 72 43 6f 64 65 2c 20 20 20 int errCode,
22eb0 20 20 20 2f 2a 20 65 78 74 65 6e 64 65 64 20 65 /* extended e
22ec0 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 20 20 69 rror code */. i
22ed0 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 nt onError,
22ee0 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 74 /* Constraint t
22ef0 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 ype */. char *p
22f00 34 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 4, /* Er
22f10 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 ror message */.
22f20 20 69 38 20 70 34 74 79 70 65 2c 20 20 20 20 20 i8 p4type,
22f30 20 20 20 2f 2a 20 50 34 5f 53 54 41 54 49 43 20 /* P4_STATIC
22f40 6f 72 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 20 or P4_TRANSIENT
22f50 2a 2f 0a 20 20 75 38 20 70 35 45 72 72 6d 73 67 */. u8 p5Errmsg
22f60 20 20 20 20 20 20 20 2f 2a 20 50 35 5f 45 72 72 /* P5_Err
22f70 4d 73 67 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 Msg type */.){.
22f80 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
22f90 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
22fa0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 65 72 );. assert( (er
22fb0 72 43 6f 64 65 26 30 78 66 66 29 3d 3d 53 51 4c rCode&0xff)==SQL
22fc0 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 ITE_CONSTRAINT )
22fd0 3b 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d ;. if( onError=
22fe0 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 =OE_Abort ){.
22ff0 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 sqlite3MayAbort
23000 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 (pParse);. }.
23010 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
23020 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 65 72 4(v, OP_Halt, er
23030 72 43 6f 64 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 rCode, onError,
23040 30 2c 20 70 34 2c 20 70 34 74 79 70 65 29 3b 0a 0, p4, p4type);.
23050 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
23060 6e 67 65 50 35 28 76 2c 20 70 35 45 72 72 6d 73 ngeP5(v, p5Errms
23070 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 g);.}../*.** Cod
23080 65 20 61 6e 20 4f 50 5f 48 61 6c 74 20 64 75 65 e an OP_Halt due
23090 20 74 6f 20 55 4e 49 51 55 45 20 6f 72 20 50 52 to UNIQUE or PR
230a0 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 IMARY KEY constr
230b0 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2e 0a aint violation..
230c0 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 55 */.void sqlite3U
230d0 6e 69 71 75 65 43 6f 6e 73 74 72 61 69 6e 74 28 niqueConstraint(
230e0 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
230f0 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 , /* Parsing
23100 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 context */. int
23110 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f onError, /
23120 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 74 79 70 * Constraint typ
23130 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 e */. Index *pI
23140 64 78 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 dx /* The
23150 69 6e 64 65 78 20 74 68 61 74 20 74 72 69 67 67 index that trigg
23160 65 72 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 ers the constrai
23170 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 nt */.){. char
23180 2a 7a 45 72 72 3b 0a 20 20 69 6e 74 20 6a 3b 0a *zErr;. int j;.
23190 20 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73 StrAccum errMs
231a0 67 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 g;. Table *pTab
231b0 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b = pIdx->pTable;
231c0 0a 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 .. sqlite3StrAc
231d0 63 75 6d 49 6e 69 74 28 26 65 72 72 4d 73 67 2c cumInit(&errMsg,
231e0 20 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 20 pParse->db, 0,
231f0 30 2c 20 32 30 30 29 3b 0a 20 20 69 66 28 20 70 0, 200);. if( p
23200 49 64 78 2d 3e 61 43 6f 6c 45 78 70 72 20 29 7b Idx->aColExpr ){
23210 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 72 . sqlite3_str
23220 5f 61 70 70 65 6e 64 66 28 26 65 72 72 4d 73 67 _appendf(&errMsg
23230 2c 20 22 69 6e 64 65 78 20 27 25 71 27 22 2c 20 , "index '%q'",
23240 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pIdx->zName);.
23250 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 6a }else{. for(j
23260 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 4b 65 79 =0; j<pIdx->nKey
23270 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 Col; j++){.
23280 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 0a 20 20 20 char *zCol;.
23290 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d assert( pIdx-
232a0 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3e 3d 30 20 >aiColumn[j]>=0
232b0 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20 );. zCol =
232c0 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d pTab->aCol[pIdx-
232d0 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e >aiColumn[j]].zN
232e0 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20 6a ame;. if( j
232f0 20 29 20 73 71 6c 69 74 65 33 5f 73 74 72 5f 61 ) sqlite3_str_a
23300 70 70 65 6e 64 28 26 65 72 72 4d 73 67 2c 20 22 ppend(&errMsg, "
23310 2c 20 22 2c 20 32 29 3b 0a 20 20 20 20 20 20 73 , ", 2);. s
23320 71 6c 69 74 65 33 5f 73 74 72 5f 61 70 70 65 6e qlite3_str_appen
23330 64 61 6c 6c 28 26 65 72 72 4d 73 67 2c 20 70 54 dall(&errMsg, pT
23340 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ab->zName);.
23350 20 20 73 71 6c 69 74 65 33 5f 73 74 72 5f 61 70 sqlite3_str_ap
23360 70 65 6e 64 28 26 65 72 72 4d 73 67 2c 20 22 2e pend(&errMsg, ".
23370 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c ", 1);. sql
23380 69 74 65 33 5f 73 74 72 5f 61 70 70 65 6e 64 61 ite3_str_appenda
23390 6c 6c 28 26 65 72 72 4d 73 67 2c 20 7a 43 6f 6c ll(&errMsg, zCol
233a0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a );. }. }. z
233b0 45 72 72 20 3d 20 73 71 6c 69 74 65 33 53 74 72 Err = sqlite3Str
233c0 41 63 63 75 6d 46 69 6e 69 73 68 28 26 65 72 72 AccumFinish(&err
233d0 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 48 Msg);. sqlite3H
233e0 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 altConstraint(pP
233f0 61 72 73 65 2c 20 0a 20 20 20 20 49 73 50 72 69 arse, . IsPri
23400 6d 61 72 79 4b 65 79 49 6e 64 65 78 28 70 49 64 maryKeyIndex(pId
23410 78 29 20 3f 20 53 51 4c 49 54 45 5f 43 4f 4e 53 x) ? SQLITE_CONS
23420 54 52 41 49 4e 54 5f 50 52 49 4d 41 52 59 4b 45 TRAINT_PRIMARYKE
23430 59 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 Y .
23440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a :
23450 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
23460 4e 54 5f 55 4e 49 51 55 45 2c 0a 20 20 20 20 6f NT_UNIQUE,. o
23470 6e 45 72 72 6f 72 2c 20 7a 45 72 72 2c 20 50 34 nError, zErr, P4
23480 5f 44 59 4e 41 4d 49 43 2c 20 50 35 5f 43 6f 6e _DYNAMIC, P5_Con
23490 73 74 72 61 69 6e 74 55 6e 69 71 75 65 29 3b 0a straintUnique);.
234a0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 }.../*.** Code a
234b0 6e 20 4f 50 5f 48 61 6c 74 20 64 75 65 20 74 6f n OP_Halt due to
234c0 20 6e 6f 6e 2d 75 6e 69 71 75 65 20 72 6f 77 69 non-unique rowi
234d0 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 d..*/.void sqlit
234e0 65 33 52 6f 77 69 64 43 6f 6e 73 74 72 61 69 6e e3RowidConstrain
234f0 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 t(. Parse *pPar
23500 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e se, /* Parsin
23510 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 g context */. i
23520 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 nt onError,
23530 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 /* Conflict res
23540 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 olution algorith
23550 6d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 m */. Table *pT
23560 61 62 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ab /* The
23570 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6e table with the n
23580 6f 6e 2d 75 6e 69 71 75 65 20 72 6f 77 69 64 20 on-unique rowid
23590 2a 2f 20 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a */ .){. char *z
235a0 4d 73 67 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 Msg;. int rc;.
235b0 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 if( pTab->iPKey
235c0 3e 3d 30 20 29 7b 0a 20 20 20 20 7a 4d 73 67 20 >=0 ){. zMsg
235d0 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
235e0 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 25 73 (pParse->db, "%s
235f0 2e 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d .%s", pTab->zNam
23600 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
23610 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 pTa
23620 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 b->aCol[pTab->iP
23630 4b 65 79 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 Key].zName);.
23640 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e rc = SQLITE_CON
23650 53 54 52 41 49 4e 54 5f 50 52 49 4d 41 52 59 4b STRAINT_PRIMARYK
23660 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 EY;. }else{.
23670 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d zMsg = sqlite3M
23680 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 Printf(pParse->d
23690 62 2c 20 22 25 73 2e 72 6f 77 69 64 22 2c 20 70 b, "%s.rowid", p
236a0 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 Tab->zName);.
236b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e rc = SQLITE_CON
236c0 53 54 52 41 49 4e 54 5f 52 4f 57 49 44 3b 0a 20 STRAINT_ROWID;.
236d0 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 6c 74 }. sqlite3Halt
236e0 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 Constraint(pPars
236f0 65 2c 20 72 63 2c 20 6f 6e 45 72 72 6f 72 2c 20 e, rc, onError,
23700 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43 zMsg, P4_DYNAMIC
23710 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
23720 20 20 20 20 20 20 20 20 20 20 50 35 5f 43 6f 6e P5_Con
23730 73 74 72 61 69 6e 74 55 6e 69 71 75 65 29 3b 0a straintUnique);.
23740 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 }../*.** Check t
23750 6f 20 73 65 65 20 69 66 20 70 49 6e 64 65 78 20 o see if pIndex
23760 75 73 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 uses the collati
23770 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c ng sequence pCol
23780 6c 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 l. Return.** tr
23790 75 65 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e ue if it does an
237a0 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 64 6f d false if it do
237b0 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 es not..*/.#ifnd
237c0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 ef SQLITE_OMIT_R
237d0 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20 69 6e EINDEX.static in
237e0 74 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 t collationMatch
237f0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f (const char *zCo
23800 6c 6c 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 ll, Index *pInde
23810 78 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 x){. int i;. a
23820 73 73 65 72 74 28 20 7a 43 6f 6c 6c 21 3d 30 20 ssert( zColl!=0
23830 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c );. for(i=0; i<
23840 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b pIndex->nColumn;
23850 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 i++){. const
23860 20 63 68 61 72 20 2a 7a 20 3d 20 70 49 6e 64 65 char *z = pInde
23870 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 x->azColl[i];.
23880 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 7c assert( z!=0 |
23890 7c 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 | pIndex->aiColu
238a0 6d 6e 5b 69 5d 3c 30 20 29 3b 0a 20 20 20 20 69 mn[i]<0 );. i
238b0 66 28 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c f( pIndex->aiCol
238c0 75 6d 6e 5b 69 5d 3e 3d 30 20 26 26 20 30 3d 3d umn[i]>=0 && 0==
238d0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a sqlite3StrICmp(z
238e0 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 , zColl) ){.
238f0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
23900 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
23910 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
23920 2a 20 52 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 * Recompute all
23930 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62 20 indices of pTab
23940 74 68 61 74 20 75 73 65 20 74 68 65 20 63 6f 6c that use the col
23950 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
23960 70 43 6f 6c 6c 2e 0a 2a 2a 20 49 66 20 70 43 6f pColl..** If pCo
23970 6c 6c 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d ll==0 then recom
23980 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 pute all indices
23990 20 6f 66 20 70 54 61 62 2e 0a 2a 2f 0a 23 69 66 of pTab..*/.#if
239a0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
239b0 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20 _REINDEX.static
239c0 76 6f 69 64 20 72 65 69 6e 64 65 78 54 61 62 6c void reindexTabl
239d0 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
239e0 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 68 Table *pTab, ch
239f0 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 ar const *zColl)
23a00 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 {. Index *pInde
23a10 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
23a20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f /* An index asso
23a30 63 69 61 74 65 64 20 77 69 74 68 20 70 54 61 62 ciated with pTab
23a40 20 2a 2f 0a 0a 20 20 66 6f 72 28 70 49 6e 64 65 */.. for(pInde
23a50 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 x=pTab->pIndex;
23a60 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 pIndex; pIndex=p
23a70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 Index->pNext){.
23a80 20 20 20 69 66 28 20 7a 43 6f 6c 6c 3d 3d 30 20 if( zColl==0
23a90 7c 7c 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 || collationMatc
23aa0 68 28 7a 43 6f 6c 6c 2c 20 70 49 6e 64 65 78 29 h(zColl, pIndex)
23ab0 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 ){. int iD
23ac0 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
23ad0 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d aToIndex(pParse-
23ae0 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 >db, pTab->pSche
23af0 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ma);. sqlit
23b00 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 e3BeginWriteOper
23b10 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c ation(pParse, 0,
23b20 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c iDb);. sql
23b30 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 ite3RefillIndex(
23b40 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 pParse, pIndex,
23b50 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d -1);. }. }.}
23b60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
23b70 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 ecompute all ind
23b80 69 63 65 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c ices of all tabl
23b90 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 es in all databa
23ba0 73 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a 2a ses where the.**
23bb0 20 69 6e 64 69 63 65 73 20 75 73 65 20 74 68 65 indices use the
23bc0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
23bd0 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 49 66 20 70 nce pColl. If p
23be0 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20 72 65 63 Coll==0 then rec
23bf0 6f 6d 70 75 74 65 0a 2a 2a 20 61 6c 6c 20 69 6e ompute.** all in
23c00 64 69 63 65 73 20 65 76 65 72 79 77 68 65 72 65 dices everywhere
23c10 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
23c20 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 ITE_OMIT_REINDEX
23c30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 69 .static void rei
23c40 6e 64 65 78 44 61 74 61 62 61 73 65 73 28 50 61 ndexDatabases(Pa
23c50 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 68 61 rse *pParse, cha
23c60 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b r const *zColl){
23c70 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 . Db *pDb;
23c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
23c90 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62 * A single datab
23ca0 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 ase */. int iDb
23cb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
23cc0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
23cd0 62 61 73 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 base index numbe
23ce0 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a r */. sqlite3 *
23cf0 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
23d00 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
23d10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
23d20 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 20 . HashElem *k;
23d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
23d40 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 * For looping ov
23d50 65 72 20 74 61 62 6c 65 73 20 69 6e 20 70 44 62 er tables in pDb
23d60 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 */. Table *pTa
23d70 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
23d80 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 /* A table in
23d90 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
23da0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
23db0 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d e3BtreeHoldsAllM
23dc0 75 74 65 78 65 73 28 64 62 29 20 29 3b 20 20 2f utexes(db) ); /
23dd0 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 73 63 68 * Needed for sch
23de0 65 6d 61 20 61 63 63 65 73 73 20 2a 2f 0a 20 20 ema access */.
23df0 66 6f 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64 for(iDb=0, pDb=d
23e00 62 2d 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e b->aDb; iDb<db->
23e10 6e 44 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b nDb; iDb++, pDb+
23e20 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 +){. assert(
23e30 70 44 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f pDb!=0 );. fo
23e40 72 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 r(k=sqliteHashFi
23e50 72 73 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d rst(&pDb->pSchem
23e60 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b a->tblHash); k;
23e70 20 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 k=sqliteHashNex
23e80 74 28 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61 t(k)){. pTa
23e90 62 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 b = (Table*)sqli
23ea0 74 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 teHashData(k);.
23eb0 20 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c reindexTabl
23ec0 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 e(pParse, pTab,
23ed0 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 zColl);. }.
23ee0 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a }.}.#endif../*.*
23ef0 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
23f00 66 6f 72 20 74 68 65 20 52 45 49 4e 44 45 58 20 for the REINDEX
23f10 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 command..**.**
23f20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 20 REINDEX
23f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23f40 20 20 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a -- 1.**
23f50 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 REINDEX
23f60 20 3c 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20 <collation>
23f70 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a -- 2.
23f80 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 ** REINDE
23f90 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f X ?<database>.?
23fa0 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20 <tablename> --
23fb0 33 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 3.** REIN
23fc0 44 45 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e DEX ?<database>
23fd0 2e 3f 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d .?<indexname> -
23fe0 2d 20 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31 - 4.**.** Form 1
23ff0 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69 causes all indi
24000 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63 ces in all attac
24010 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74 6f hed databases to
24020 20 62 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20 be rebuilt..**
24030 46 6f 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20 Form 2 rebuilds
24040 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 all indices in a
24050 6c 6c 20 64 61 74 61 62 61 73 65 73 20 74 68 61 ll databases tha
24060 74 20 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a t use the named.
24070 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e ** collating fun
24080 63 74 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20 ction. Forms 3
24090 61 6e 64 20 34 20 72 65 62 75 69 6c 64 20 74 68 and 4 rebuild th
240a0 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72 e named index or
240b0 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 all.** indices
240c0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
240d0 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e the named table.
240e0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
240f0 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a TE_OMIT_REINDEX.
24100 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e void sqlite3Rein
24110 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 dex(Parse *pPars
24120 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 e, Token *pName1
24130 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 , Token *pName2)
24140 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f {. CollSeq *pCo
24150 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
24160 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 /* Collating seq
24170 75 65 6e 63 65 20 74 6f 20 62 65 20 72 65 69 6e uence to be rein
24180 64 65 78 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a dexed, or NULL *
24190 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 /. char *z;
241a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
241b0 2f 2a 20 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 /* Name of a tab
241c0 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 le or index */.
241d0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
241e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
241f0 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
24200 62 61 73 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 base */. Table
24210 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 *pTab;
24220 20 20 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 /* A table
24230 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
24240 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e */. Index *pIn
24250 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 dex;
24260 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 /* An index as
24270 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54 sociated with pT
24280 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b ab */. int iDb;
24290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
242a0 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
242b0 61 73 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 ase index number
242c0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
242d0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 b = pParse->db;
242e0 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
242f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
24300 20 20 54 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d Token *pObjNam
24310 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
24320 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 Name of the tab
24330 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 le or index to b
24340 65 20 72 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a e reindexed */..
24350 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 /* Read the da
24360 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 tabase schema. I
24370 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
24380 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f s, leave an erro
24390 72 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 r message. ** a
243a0 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 nd code in pPars
243b0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c e and return NUL
243c0 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 L. */. if( SQLI
243d0 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 TE_OK!=sqlite3Re
243e0 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 adSchema(pParse)
243f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
24400 20 20 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 }.. if( pName
24410 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 1==0 ){. rein
24420 64 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61 dexDatabases(pPa
24430 72 73 65 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 rse, 0);. ret
24440 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 urn;. }else if(
24450 20 4e 45 56 45 52 28 70 4e 61 6d 65 32 3d 3d 30 NEVER(pName2==0
24460 29 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e 7a 3d 3d ) || pName2->z==
24470 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 0 ){. char *z
24480 43 6f 6c 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 Coll;. assert
24490 28 20 70 4e 61 6d 65 31 2d 3e 7a 20 29 3b 0a 20 ( pName1->z );.
244a0 20 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 zColl = sqlit
244b0 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
244c0 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d pParse->db, pNam
244d0 65 31 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 e1);. if( !zC
244e0 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 oll ) return;.
244f0 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
24500 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 3FindCollSeq(db,
24510 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c ENC(db), zColl,
24520 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 0);. if( pCo
24530 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 65 69 6e ll ){. rein
24540 64 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61 dexDatabases(pPa
24550 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 rse, zColl);.
24560 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
24570 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 (db, zColl);.
24580 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
24590 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
245a0 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 ee(db, zColl);.
245b0 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 }. iDb = sqlit
245c0 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 e3TwoPartName(pP
245d0 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e arse, pName1, pN
245e0 61 6d 65 32 2c 20 26 70 4f 62 6a 4e 61 6d 65 29 ame2, &pObjName)
245f0 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 ;. if( iDb<0 )
24600 72 65 74 75 72 6e 3b 0a 20 20 7a 20 3d 20 73 71 return;. z = sq
24610 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
24620 65 6e 28 64 62 2c 20 70 4f 62 6a 4e 61 6d 65 29 en(db, pObjName)
24630 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 ;. if( z==0 ) r
24640 65 74 75 72 6e 3b 0a 20 20 7a 44 62 20 3d 20 64 eturn;. zDb = d
24650 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 b->aDb[iDb].zDbS
24660 4e 61 6d 65 3b 0a 20 20 70 54 61 62 20 3d 20 73 Name;. pTab = s
24670 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
24680 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 69 db, z, zDb);. i
24690 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 72 f( pTab ){. r
246a0 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61 72 eindexTable(pPar
246b0 73 65 2c 20 70 54 61 62 2c 20 30 29 3b 0a 20 20 se, pTab, 0);.
246c0 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
246d0 64 62 2c 20 7a 29 3b 0a 20 20 20 20 72 65 74 75 db, z);. retu
246e0 72 6e 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 rn;. }. pIndex
246f0 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e = sqlite3FindIn
24700 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b dex(db, z, zDb);
24710 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
24720 28 64 62 2c 20 7a 29 3b 0a 20 20 69 66 28 20 70 (db, z);. if( p
24730 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c Index ){. sql
24740 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 ite3BeginWriteOp
24750 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 eration(pParse,
24760 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 0, iDb);. sql
24770 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 ite3RefillIndex(
24780 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 pParse, pIndex,
24790 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b -1);. return;
247a0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 . }. sqlite3Er
247b0 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
247c0 75 6e 61 62 6c 65 20 74 6f 20 69 64 65 6e 74 69 unable to identi
247d0 66 79 20 74 68 65 20 6f 62 6a 65 63 74 20 74 6f fy the object to
247e0 20 62 65 20 72 65 69 6e 64 65 78 65 64 22 29 3b be reindexed");
247f0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
24800 20 52 65 74 75 72 6e 20 61 20 4b 65 79 49 6e 66 Return a KeyInf
24810 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 o structure that
24820 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 is appropriate
24830 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 49 6e for the given In
24840 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 dex..**.** The c
24850 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 69 6e 76 aller should inv
24860 6f 6b 65 20 73 71 6c 69 74 65 33 4b 65 79 49 6e oke sqlite3KeyIn
24870 66 6f 55 6e 72 65 66 28 29 20 6f 6e 20 74 68 65 foUnref() on the
24880 20 72 65 74 75 72 6e 65 64 20 6f 62 6a 65 63 74 returned object
24890 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68 61 73 20 .** when it has
248a0 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 69 finished using i
248b0 74 2e 0a 2a 2f 0a 4b 65 79 49 6e 66 6f 20 2a 73 t..*/.KeyInfo *s
248c0 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f 4f 66 49 qlite3KeyInfoOfI
248d0 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 ndex(Parse *pPar
248e0 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 64 78 29 se, Index *pIdx)
248f0 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
24900 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 nCol = pIdx->nC
24910 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e 4b 65 olumn;. int nKe
24920 79 20 3d 20 70 49 64 78 2d 3e 6e 4b 65 79 43 6f y = pIdx->nKeyCo
24930 6c 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b l;. KeyInfo *pK
24940 65 79 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 ey;. if( pParse
24950 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 20 ->nErr ) return
24960 30 3b 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 75 0;. if( pIdx->u
24970 6e 69 71 4e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 niqNotNull ){.
24980 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 pKey = sqlite3
24990 4b 65 79 49 6e 66 6f 41 6c 6c 6f 63 28 70 50 61 KeyInfoAlloc(pPa
249a0 72 73 65 2d 3e 64 62 2c 20 6e 4b 65 79 2c 20 6e rse->db, nKey, n
249b0 43 6f 6c 2d 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c Col-nKey);. }el
249c0 73 65 7b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 se{. pKey = s
249d0 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f 41 6c 6c qlite3KeyInfoAll
249e0 6f 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6e oc(pParse->db, n
249f0 43 6f 6c 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 Col, 0);. }. i
24a00 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 f( pKey ){. a
24a10 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 4b 65 ssert( sqlite3Ke
24a20 79 49 6e 66 6f 49 73 57 72 69 74 65 61 62 6c 65 yInfoIsWriteable
24a30 28 70 4b 65 79 29 20 29 3b 0a 20 20 20 20 66 6f (pKey) );. fo
24a40 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 r(i=0; i<nCol; i
24a50 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 ++){. const
24a60 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70 char *zColl = p
24a70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a Idx->azColl[i];.
24a80 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f 6c pKey->aCol
24a90 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3d 3d 73 71 l[i] = zColl==sq
24aa0 6c 69 74 65 33 53 74 72 42 49 4e 41 52 59 20 3f lite3StrBINARY ?
24ab0 20 30 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20 0 :.
24ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
24ad0 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 ite3LocateCollSe
24ae0 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 q(pParse, zColl)
24af0 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 53 ;. pKey->aS
24b00 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 ortOrder[i] = pI
24b10 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 dx->aSortOrder[i
24b20 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 ];. }. if(
24b30 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b pParse->nErr ){
24b40 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
24b50 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 Parse->rc==SQLIT
24b60 45 5f 45 52 52 4f 52 5f 4d 49 53 53 49 4e 47 5f E_ERROR_MISSING_
24b70 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 20 COLLSEQ );.
24b80 20 69 66 28 20 70 49 64 78 2d 3e 62 4e 6f 51 75 if( pIdx->bNoQu
24b90 65 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ery==0 ){.
24ba0 20 20 2f 2a 20 44 65 61 63 74 69 76 61 74 65 20 /* Deactivate
24bb0 74 68 65 20 69 6e 64 65 78 20 62 65 63 61 75 73 the index becaus
24bc0 65 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 61 6e e it contains an
24bd0 20 75 6e 6b 6e 6f 77 6e 20 63 6f 6c 6c 61 74 69 unknown collati
24be0 6e 67 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 ng. ** se
24bf0 71 75 65 6e 63 65 2e 20 20 54 68 65 20 6f 6e 6c quence. The onl
24c00 79 20 77 61 79 20 74 6f 20 72 65 61 63 74 69 76 y way to reactiv
24c10 65 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 74 e the index is t
24c20 6f 20 72 65 6c 6f 61 64 20 74 68 65 0a 20 20 20 o reload the.
24c30 20 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 2e 20 ** schema.
24c40 20 41 64 64 69 6e 67 20 74 68 65 20 6d 69 73 73 Adding the miss
24c50 69 6e 67 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 ing collating se
24c60 71 75 65 6e 63 65 20 6c 61 74 65 72 20 64 6f 65 quence later doe
24c70 73 20 6e 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a s not. **
24c80 20 72 65 61 63 74 69 76 65 20 74 68 65 20 69 6e reactive the in
24c90 64 65 78 2e 20 20 54 68 65 20 61 70 70 6c 69 63 dex. The applic
24ca0 61 74 69 6f 6e 20 68 61 64 20 74 68 65 20 63 68 ation had the ch
24cb0 61 6e 63 65 20 74 6f 20 72 65 67 69 73 74 65 72 ance to register
24cc0 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
24cd0 6d 69 73 73 69 6e 67 20 69 6e 64 65 78 20 75 73 missing index us
24ce0 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f ing the collatio
24cf0 6e 2d 6e 65 65 64 65 64 20 63 61 6c 6c 62 61 63 n-needed callbac
24d00 6b 2e 20 20 46 6f 72 0a 20 20 20 20 20 20 20 20 k. For.
24d10 2a 2a 20 73 69 6d 70 6c 69 63 69 74 79 2c 20 53 ** simplicity, S
24d20 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 67 QLite will not g
24d30 69 76 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 ive the applicat
24d40 69 6f 6e 20 61 20 73 65 63 6f 6e 64 20 63 68 61 ion a second cha
24d50 6e 63 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a nce.. */.
24d60 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 62 4e pIdx->bN
24d70 6f 51 75 65 72 79 20 3d 20 31 3b 0a 20 20 20 20 oQuery = 1;.
24d80 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d pParse->rc =
24d90 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 5f 52 45 SQLITE_ERROR_RE
24da0 54 52 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 TRY;. }.
24db0 20 20 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 sqlite3KeyInf
24dc0 6f 55 6e 72 65 66 28 70 4b 65 79 29 3b 0a 20 20 oUnref(pKey);.
24dd0 20 20 20 20 70 4b 65 79 20 3d 20 30 3b 0a 20 20 pKey = 0;.
24de0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
24df0 20 70 4b 65 79 3b 0a 7d 0a 0a 23 69 66 6e 64 65 pKey;.}..#ifnde
24e00 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 54 f SQLITE_OMIT_CT
24e10 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f E./* .** This ro
24e20 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 utine is invoked
24e30 20 6f 6e 63 65 20 70 65 72 20 43 54 45 20 62 79 once per CTE by
24e40 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c the parser whil
24e50 65 20 70 61 72 73 69 6e 67 20 61 20 0a 2a 2a 20 e parsing a .**
24e60 57 49 54 48 20 63 6c 61 75 73 65 2e 20 0a 2a 2f WITH clause. .*/
24e70 0a 57 69 74 68 20 2a 73 71 6c 69 74 65 33 57 69 .With *sqlite3Wi
24e80 74 68 41 64 64 28 0a 20 20 50 61 72 73 65 20 2a thAdd(. Parse *
24e90 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
24ea0 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
24eb0 65 78 74 20 2a 2f 0a 20 20 57 69 74 68 20 2a 70 ext */. With *p
24ec0 57 69 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 With,
24ed0 20 2f 2a 20 45 78 69 73 74 69 6e 67 20 57 49 54 /* Existing WIT
24ee0 48 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e 55 4c H clause, or NUL
24ef0 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e L */. Token *pN
24f00 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f ame, /
24f10 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f * Name of the co
24f20 6d 6d 6f 6e 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 mmon-table */.
24f30 45 78 70 72 4c 69 73 74 20 2a 70 41 72 67 6c 69 ExprList *pArgli
24f40 73 74 2c 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f st, /* Optio
24f50 6e 61 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 nal column name
24f60 6c 69 73 74 20 66 6f 72 20 74 68 65 20 74 61 62 list for the tab
24f70 6c 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a le */. Select *
24f80 70 51 75 65 72 79 20 20 20 20 20 20 20 20 20 20 pQuery
24f90 2f 2a 20 51 75 65 72 79 20 75 73 65 64 20 74 6f /* Query used to
24fa0 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 initialize the
24fb0 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 table */.){. sq
24fc0 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
24fd0 73 65 2d 3e 64 62 3b 0a 20 20 57 69 74 68 20 2a se->db;. With *
24fe0 70 4e 65 77 3b 0a 20 20 63 68 61 72 20 2a 7a 4e pNew;. char *zN
24ff0 61 6d 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ame;.. /* Check
25000 20 74 68 61 74 20 74 68 65 20 43 54 45 20 6e 61 that the CTE na
25010 6d 65 20 69 73 20 75 6e 69 71 75 65 20 77 69 74 me is unique wit
25020 68 69 6e 20 74 68 69 73 20 57 49 54 48 20 63 6c hin this WITH cl
25030 61 75 73 65 2e 20 49 66 0a 20 20 2a 2a 20 6e 6f ause. If. ** no
25040 74 2c 20 73 74 6f 72 65 20 61 6e 20 65 72 72 6f t, store an erro
25050 72 20 69 6e 20 74 68 65 20 50 61 72 73 65 20 73 r in the Parse s
25060 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 7a tructure. */. z
25070 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 Name = sqlite3Na
25080 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 meFromToken(pPar
25090 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a se->db, pName);.
250a0 20 20 69 66 28 20 7a 4e 61 6d 65 20 26 26 20 70 if( zName && p
250b0 57 69 74 68 20 29 7b 0a 20 20 20 20 69 6e 74 20 With ){. int
250c0 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 i;. for(i=0;
250d0 69 3c 70 57 69 74 68 2d 3e 6e 43 74 65 3b 20 69 i<pWith->nCte; i
250e0 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 ++){. if( s
250f0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4e qlite3StrICmp(zN
25100 61 6d 65 2c 20 70 57 69 74 68 2d 3e 61 5b 69 5d ame, pWith->a[i]
25110 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 .zName)==0 ){.
25120 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
25130 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 64 orMsg(pParse, "d
25140 75 70 6c 69 63 61 74 65 20 57 49 54 48 20 74 61 uplicate WITH ta
25150 62 6c 65 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a ble name: %s", z
25160 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Name);. }.
25170 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
25180 70 57 69 74 68 20 29 7b 0a 20 20 20 20 69 6e 74 pWith ){. int
25190 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 nByte = sizeof(
251a0 2a 70 57 69 74 68 29 20 2b 20 28 73 69 7a 65 6f *pWith) + (sizeo
251b0 66 28 70 57 69 74 68 2d 3e 61 5b 31 5d 29 20 2a f(pWith->a[1]) *
251c0 20 70 57 69 74 68 2d 3e 6e 43 74 65 29 3b 0a 20 pWith->nCte);.
251d0 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 pNew = sqlite
251e0 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 3DbRealloc(db, p
251f0 57 69 74 68 2c 20 6e 42 79 74 65 29 3b 0a 20 20 With, nByte);.
25200 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 }else{. pNew
25210 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
25220 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 cZero(db, sizeof
25230 28 2a 70 57 69 74 68 29 29 3b 0a 20 20 7d 0a 20 (*pWith));. }.
25240 20 61 73 73 65 72 74 28 20 28 70 4e 65 77 21 3d assert( (pNew!=
25250 30 20 26 26 20 7a 4e 61 6d 65 21 3d 30 29 20 7c 0 && zName!=0) |
25260 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
25270 65 64 20 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d ed );.. if( db-
25280 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
25290 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
252a0 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
252b0 41 72 67 6c 69 73 74 29 3b 0a 20 20 20 20 73 71 Arglist);. sq
252c0 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 lite3SelectDelet
252d0 65 28 64 62 2c 20 70 51 75 65 72 79 29 3b 0a 20 e(db, pQuery);.
252e0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
252f0 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 (db, zName);.
25300 20 70 4e 65 77 20 3d 20 70 57 69 74 68 3b 0a 20 pNew = pWith;.
25310 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 }else{. pNew
25320 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 43 74 65 5d 2e ->a[pNew->nCte].
25330 70 53 65 6c 65 63 74 20 3d 20 70 51 75 65 72 79 pSelect = pQuery
25340 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e ;. pNew->a[pN
25350 65 77 2d 3e 6e 43 74 65 5d 2e 70 43 6f 6c 73 20 ew->nCte].pCols
25360 3d 20 70 41 72 67 6c 69 73 74 3b 0a 20 20 20 20 = pArglist;.
25370 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 43 pNew->a[pNew->nC
25380 74 65 5d 2e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d te].zName = zNam
25390 65 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 e;. pNew->a[p
253a0 4e 65 77 2d 3e 6e 43 74 65 5d 2e 7a 43 74 65 45 New->nCte].zCteE
253b0 72 72 20 3d 20 30 3b 0a 20 20 20 20 70 4e 65 77 rr = 0;. pNew
253c0 2d 3e 6e 43 74 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 ->nCte++;. }..
253d0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
253e0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 ./*.** Free the
253f0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
25400 57 69 74 68 20 6f 62 6a 65 63 74 20 70 61 73 73 With object pass
25410 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
25420 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f argument..*/.vo
25430 69 64 20 73 71 6c 69 74 65 33 57 69 74 68 44 65 id sqlite3WithDe
25440 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 lete(sqlite3 *db
25450 2c 20 57 69 74 68 20 2a 70 57 69 74 68 29 7b 0a , With *pWith){.
25460 20 20 69 66 28 20 70 57 69 74 68 20 29 7b 0a 20 if( pWith ){.
25470 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
25480 72 28 69 3d 30 3b 20 69 3c 70 57 69 74 68 2d 3e r(i=0; i<pWith->
25490 6e 43 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCte; i++){.
254a0 20 20 73 74 72 75 63 74 20 43 74 65 20 2a 70 43 struct Cte *pC
254b0 74 65 20 3d 20 26 70 57 69 74 68 2d 3e 61 5b 69 te = &pWith->a[i
254c0 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ];. sqlite3
254d0 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
254e0 62 2c 20 70 43 74 65 2d 3e 70 43 6f 6c 73 29 3b b, pCte->pCols);
254f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 . sqlite3Se
25500 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 lectDelete(db, p
25510 43 74 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 Cte->pSelect);.
25520 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
25530 65 65 28 64 62 2c 20 70 43 74 65 2d 3e 7a 4e 61 ee(db, pCte->zNa
25540 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 me);. }. s
25550 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
25560 20 70 57 69 74 68 29 3b 0a 20 20 7d 0a 7d 0a 23 pWith);. }.}.#
25570 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 endif /* !define
25580 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 54 d(SQLITE_OMIT_CT
25590 45 29 20 2a 2f 0a E) */.