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 20 20 69 66 28 20 69 44 62 =0 );.. if( iDb
07f0: 3d 3d 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==1 ) return;.
0800: 69 66 28 20 21 73 71 6c 69 74 65 33 42 74 72 65 if( !sqlite3Btre
0810: 65 53 68 61 72 61 62 6c 65 28 70 50 61 72 73 65 eSharable(pParse
0820: 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 ->db->aDb[iDb].p
0830: 42 74 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 Bt) ) return;.
0840: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 70 6c for(i=0; i<pTopl
0850: 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b evel->nTableLock
0860: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 ; i++){. p =
0870: 26 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 &pToplevel->aTab
0880: 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 leLock[i];. i
0890: 66 28 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20 26 f( p->iDb==iDb &
08a0: 26 20 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62 20 & p->iTab==iTab
08b0: 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 57 72 ){. p->isWr
08c0: 69 74 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 iteLock = (p->is
08d0: 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 WriteLock || isW
08e0: 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 riteLock);.
08f0: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
0900: 20 7d 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20 73 }.. nBytes = s
0910: 69 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b 29 izeof(TableLock)
0920: 20 2a 20 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e * (pToplevel->n
0930: 54 61 62 6c 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 TableLock+1);.
0940: 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c pToplevel->aTabl
0950: 65 4c 6f 63 6b 20 3d 0a 20 20 20 20 20 20 73 71 eLock =. sq
0960: 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 lite3DbReallocOr
0970: 46 72 65 65 28 70 54 6f 70 6c 65 76 65 6c 2d 3e Free(pToplevel->
0980: 64 62 2c 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 db, pToplevel->a
0990: 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e 42 79 74 65 TableLock, nByte
09a0: 73 29 3b 0a 20 20 69 66 28 20 70 54 6f 70 6c 65 s);. if( pTople
09b0: 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 vel->aTableLock
09c0: 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 54 6f 70 ){. p = &pTop
09d0: 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 level->aTableLoc
09e0: 6b 5b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 k[pToplevel->nTa
09f0: 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a 20 20 20 20 bleLock++];.
0a00: 70 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 p->iDb = iDb;.
0a10: 20 20 70 2d 3e 69 54 61 62 20 3d 20 69 54 61 62 p->iTab = iTab
0a20: 3b 0a 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 ;. p->isWrite
0a30: 4c 6f 63 6b 20 3d 20 69 73 57 72 69 74 65 4c 6f Lock = isWriteLo
0a40: 63 6b 3b 0a 20 20 20 20 70 2d 3e 7a 4c 6f 63 6b ck;. p->zLock
0a50: 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 Name = zName;.
0a60: 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 70 6c }else{. pTopl
0a70: 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b evel->nTableLock
0a80: 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 0;. sqlite
0a90: 33 4f 6f 6d 46 61 75 6c 74 28 70 54 6f 70 6c 65 3OomFault(pTople
0aa0: 76 65 6c 2d 3e 64 62 29 3b 0a 20 20 7d 0a 7d 0a vel->db);. }.}.
0ab0: 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f ./*.** Code an O
0ac0: 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 P_TableLock inst
0ad0: 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 ruction for each
0ae0: 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62 79 table locked by
0af0: 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e the.** statemen
0b00: 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20 62 79 t (configured by
0b10: 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
0b20: 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a 2a 3TableLock())..*
0b30: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f /.static void co
0b40: 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50 61 72 deTableLocks(Par
0b50: 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 se *pParse){. i
0b60: 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a 70 56 nt i;. Vdbe *pV
0b70: 64 62 65 3b 20 0a 0a 20 20 70 56 64 62 65 20 3d dbe; .. pVdbe =
0b80: 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
0b90: 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 pParse);. asser
0ba0: 74 28 20 70 56 64 62 65 21 3d 30 20 29 3b 20 2f t( pVdbe!=0 ); /
0bb0: 2a 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 * sqlite3GetVdbe
0bc0: 20 63 61 6e 6e 6f 74 20 66 61 69 6c 3a 20 56 44 cannot fail: VD
0bd0: 42 45 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 BE already alloc
0be0: 61 74 65 64 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 ated */.. for(i
0bf0: 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 =0; i<pParse->nT
0c00: 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a ableLock; i++){.
0c10: 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 TableLock *p
0c20: 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 = &pParse->aTab
0c30: 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 leLock[i];. i
0c40: 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b 0a nt p1 = p->iDb;.
0c50: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
0c60: 64 64 4f 70 34 28 70 56 64 62 65 2c 20 4f 50 5f ddOp4(pVdbe, OP_
0c70: 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20 70 TableLock, p1, p
0c80: 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73 57 72 69 ->iTab, p->isWri
0c90: 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 teLock,.
0ca0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2d p-
0cb0: 3e 7a 4c 6f 63 6b 4e 61 6d 65 2c 20 50 34 5f 53 >zLockName, P4_S
0cc0: 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 TATIC);. }.}.#e
0cd0: 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 63 6f lse. #define co
0ce0: 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a deTableLocks(x).
0cf0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 #endif../*.** Re
0d00: 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
0d10: 20 67 69 76 65 6e 20 79 44 62 4d 61 73 6b 20 6f given yDbMask o
0d20: 62 6a 65 63 74 20 69 73 20 65 6d 70 74 79 20 2d bject is empty -
0d30: 20 69 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 if it contains
0d40: 6e 6f 0a 2a 2a 20 31 20 62 69 74 73 2e 20 20 54 no.** 1 bits. T
0d50: 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
0d60: 73 65 64 20 62 79 20 74 68 65 20 44 62 4d 61 73 sed by the DbMas
0d70: 6b 41 6c 6c 5a 65 72 6f 28 29 20 61 6e 64 20 44 kAllZero() and D
0d80: 62 4d 61 73 6b 4e 6f 74 5a 65 72 6f 28 29 0a 2a bMaskNotZero().*
0d90: 2a 20 6d 61 63 72 6f 73 20 77 68 65 6e 20 53 51 * macros when SQ
0da0: 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 LITE_MAX_ATTACHE
0db0: 44 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 D is greater tha
0dc0: 6e 20 33 30 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c n 30..*/.#if SQL
0dd0: 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 ITE_MAX_ATTACHED
0de0: 3e 33 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 44 >30.int sqlite3D
0df0: 62 4d 61 73 6b 41 6c 6c 5a 65 72 6f 28 79 44 62 bMaskAllZero(yDb
0e00: 4d 61 73 6b 20 6d 29 7b 0a 20 20 69 6e 74 20 69 Mask m){. int i
0e10: 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 ;. for(i=0; i<s
0e20: 69 7a 65 6f 66 28 79 44 62 4d 61 73 6b 29 3b 20 izeof(yDbMask);
0e30: 69 2b 2b 29 20 69 66 28 20 6d 5b 69 5d 20 29 20 i++) if( m[i] )
0e40: 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 return 0;. retu
0e50: 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a rn 1;.}.#endif..
0e60: 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
0e70: 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 ne is called aft
0e80: 65 72 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 er a single SQL
0e90: 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 statement has be
0ea0: 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61 6e 64 en.** parsed and
0eb0: 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 a VDBE program
0ec0: 74 6f 20 65 78 65 63 75 74 65 20 74 68 61 74 20 to execute that
0ed0: 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 statement has be
0ee0: 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64 2e 20 en.** prepared.
0ef0: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 75 This routine pu
0f00: 74 73 20 74 68 65 20 66 69 6e 69 73 68 69 6e 67 ts the finishing
0f10: 20 74 6f 75 63 68 65 73 20 6f 6e 20 74 68 65 0a touches on the.
0f20: 2a 2a 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 ** VDBE program
0f30: 61 6e 64 20 72 65 73 65 74 73 20 74 68 65 20 70 and resets the p
0f40: 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 Parse structure
0f50: 66 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 for the next.**
0f60: 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 parse..**.** Not
0f70: 65 20 74 68 61 74 20 69 66 20 61 6e 20 65 72 72 e that if an err
0f80: 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 69 74 20 or occurred, it
0f90: 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73 might be the cas
0fa0: 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 e that.** no VDB
0fb0: 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e 65 72 E code was gener
0fc0: 61 74 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ated..*/.void sq
0fd0: 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e lite3FinishCodin
0fe0: 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 g(Parse *pParse)
0ff0: 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
1000: 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 61 . Vdbe *v;.. a
1010: 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 ssert( pParse->p
1020: 54 6f 70 6c 65 76 65 6c 3d 3d 30 20 29 3b 0a 20 Toplevel==0 );.
1030: 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
1040: 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
1050: 6e 65 73 74 65 64 20 29 20 72 65 74 75 72 6e 3b nested ) return;
1060: 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
1070: 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73 cFailed || pPars
1080: 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 69 e->nErr ){. i
1090: 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 f( pParse->rc==S
10a0: 51 4c 49 54 45 5f 4f 4b 20 29 20 70 50 61 72 73 QLITE_OK ) pPars
10b0: 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 e->rc = SQLITE_E
10c0: 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e RROR;. return
10d0: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 ;. }.. /* Begi
10e0: 6e 20 62 79 20 67 65 6e 65 72 61 74 69 6e 67 20 n by generating
10f0: 73 6f 6d 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e some termination
1100: 20 63 6f 64 65 20 61 74 20 74 68 65 20 65 6e 64 code at the end
1110: 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 of the. ** vdb
1120: 65 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2f 0a 20 e program. */.
1130: 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 v = sqlite3GetV
1140: 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 dbe(pParse);. a
1150: 73 73 65 72 74 28 20 21 70 50 61 72 73 65 2d 3e ssert( !pParse->
1160: 69 73 4d 75 6c 74 69 57 72 69 74 65 20 0a 20 20 isMultiWrite .
1170: 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 || sqlite3V
1180: 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 dbeAssertMayAbor
1190: 74 28 76 2c 20 70 50 61 72 73 65 2d 3e 6d 61 79 t(v, pParse->may
11a0: 41 62 6f 72 74 29 29 3b 0a 20 20 69 66 28 20 76 Abort));. if( v
11b0: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
11c0: 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f dbeAddOp0(v, OP_
11d0: 48 61 6c 74 29 3b 0a 0a 23 69 66 20 53 51 4c 49 Halt);..#if SQLI
11e0: 54 45 5f 55 53 45 52 5f 41 55 54 48 45 4e 54 49 TE_USER_AUTHENTI
11f0: 43 41 54 49 4f 4e 0a 20 20 20 20 69 66 28 20 70 CATION. if( p
1200: 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 Parse->nTableLoc
1210: 6b 3e 30 20 26 26 20 64 62 2d 3e 69 6e 69 74 2e k>0 && db->init.
1220: 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 busy==0 ){.
1230: 20 73 71 6c 69 74 65 33 55 73 65 72 41 75 74 68 sqlite3UserAuth
1240: 49 6e 69 74 28 64 62 29 3b 0a 20 20 20 20 20 20 Init(db);.
1250: 69 66 28 20 64 62 2d 3e 61 75 74 68 2e 61 75 74 if( db->auth.aut
1260: 68 4c 65 76 65 6c 3c 55 41 55 54 48 5f 55 73 65 hLevel<UAUTH_Use
1270: 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c r ){. sql
1280: 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1290: 72 73 65 2c 20 22 75 73 65 72 20 6e 6f 74 20 61 rse, "user not a
12a0: 75 74 68 65 6e 74 69 63 61 74 65 64 22 29 3b 0a uthenticated");.
12b0: 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
12c0: 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 rc = SQLITE_AUTH
12d0: 5f 55 53 45 52 3b 0a 20 20 20 20 20 20 20 20 72 _USER;. r
12e0: 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 eturn;. }.
12f0: 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
1300: 20 2f 2a 20 54 68 65 20 63 6f 6f 6b 69 65 20 6d /* The cookie m
1310: 61 73 6b 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 ask contains one
1320: 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 64 61 bit for each da
1330: 74 61 62 61 73 65 20 66 69 6c 65 20 6f 70 65 6e tabase file open
1340: 2e 0a 20 20 20 20 2a 2a 20 28 42 69 74 20 30 20 .. ** (Bit 0
1350: 69 73 20 66 6f 72 20 6d 61 69 6e 2c 20 62 69 74 is for main, bit
1360: 20 31 20 69 73 20 66 6f 72 20 74 65 6d 70 2c 20 1 is for temp,
1370: 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 29 20 20 and so forth.)
1380: 42 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 Bits are. **
1390: 73 65 74 20 66 6f 72 20 65 61 63 68 20 64 61 74 set for each dat
13a0: 61 62 61 73 65 20 74 68 61 74 20 69 73 20 75 73 abase that is us
13b0: 65 64 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f ed. Generate co
13c0: 64 65 20 74 6f 20 73 74 61 72 74 20 61 0a 20 20 de to start a.
13d0: 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
13e0: 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20 64 61 on each used da
13f0: 74 61 62 61 73 65 20 61 6e 64 20 74 6f 20 76 65 tabase and to ve
1400: 72 69 66 79 20 74 68 65 20 73 63 68 65 6d 61 20 rify the schema
1410: 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a 20 6f 6e cookie. ** on
1420: 20 65 61 63 68 20 75 73 65 64 20 64 61 74 61 62 each used datab
1430: 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ase.. */.
1440: 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1450: 69 6c 65 64 3d 3d 30 20 0a 20 20 20 20 20 26 26 iled==0 . &&
1460: 20 28 44 62 4d 61 73 6b 4e 6f 6e 5a 65 72 6f 28 (DbMaskNonZero(
1470: 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 pParse->cookieMa
1480: 73 6b 29 20 7c 7c 20 70 50 61 72 73 65 2d 3e 70 sk) || pParse->p
1490: 43 6f 6e 73 74 45 78 70 72 29 0a 20 20 20 20 29 ConstExpr). )
14a0: 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 2c {. int iDb,
14b0: 20 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 i;. assert
14c0: 28 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 ( sqlite3VdbeGet
14d0: 4f 70 28 76 2c 20 30 29 2d 3e 6f 70 63 6f 64 65 Op(v, 0)->opcode
14e0: 3d 3d 4f 50 5f 49 6e 69 74 20 29 3b 0a 20 20 20 ==OP_Init );.
14f0: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
1500: 6d 70 48 65 72 65 28 76 2c 20 30 29 3b 0a 20 20 mpHere(v, 0);.
1510: 20 20 20 20 66 6f 72 28 69 44 62 3d 30 3b 20 69 for(iDb=0; i
1520: 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b Db<db->nDb; iDb+
1530: 2b 29 7b 0a 20 20 20 20 20 20 20 20 53 63 68 65 +){. Sche
1540: 6d 61 20 2a 70 53 63 68 65 6d 61 3b 0a 20 20 20 ma *pSchema;.
1550: 20 20 20 20 20 69 66 28 20 44 62 4d 61 73 6b 54 if( DbMaskT
1560: 65 73 74 28 70 50 61 72 73 65 2d 3e 63 6f 6f 6b est(pParse->cook
1570: 69 65 4d 61 73 6b 2c 20 69 44 62 29 3d 3d 30 20 ieMask, iDb)==0
1580: 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1590: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 sqlite3VdbeU
15a0: 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 sesBtree(v, iDb)
15b0: 3b 0a 20 20 20 20 20 20 20 20 70 53 63 68 65 6d ;. pSchem
15c0: 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d a = db->aDb[iDb]
15d0: 2e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 .pSchema;.
15e0: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
15f0: 4f 70 34 49 6e 74 28 76 2c 0a 20 20 20 20 20 20 Op4Int(v,.
1600: 20 20 20 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 OP_Transacti
1610: 6f 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 on,
1620: 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 /* Opcode
1630: 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 44 */. iD
1640: 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b,
1650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1660: 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 20 20 /* P1 */.
1670: 20 20 20 20 44 62 4d 61 73 6b 54 65 73 74 28 70 DbMaskTest(p
1680: 50 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 73 6b Parse->writeMask
1690: 2c 69 44 62 29 2c 20 2f 2a 20 50 32 20 2a 2f 0a ,iDb), /* P2 */.
16a0: 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d pSchem
16b0: 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 a->schema_cookie
16c0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
16d0: 50 33 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 P3 */.
16e0: 70 53 63 68 65 6d 61 2d 3e 69 47 65 6e 65 72 61 pSchema->iGenera
16f0: 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 tion
1700: 20 20 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 20 20 /* P4 */.
1710: 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 );. i
1720: 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 f( db->init.busy
1730: 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 ==0 ) sqlite3Vdb
1740: 65 43 68 61 6e 67 65 50 35 28 76 2c 20 31 29 3b eChangeP5(v, 1);
1750: 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d . VdbeCom
1760: 6d 65 6e 74 28 28 76 2c 0a 20 20 20 20 20 20 20 ment((v,.
1770: 20 20 20 20 20 20 20 22 75 73 65 73 53 74 6d 74 "usesStmt
1780: 4a 6f 75 72 6e 61 6c 3d 25 64 22 2c 20 70 50 61 Journal=%d", pPa
1790: 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 20 26 26 rse->mayAbort &&
17a0: 20 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69 pParse->isMulti
17b0: 57 72 69 74 65 29 29 3b 0a 20 20 20 20 20 20 7d Write));. }
17c0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
17d0: 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
17e0: 45 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b E. for(i=0;
17f0: 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 i<pParse->nVtab
1800: 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Lock; i++){.
1810: 20 20 20 20 63 68 61 72 20 2a 76 74 61 62 20 3d char *vtab =
1820: 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
1830: 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 50 GetVTable(db, pP
1840: 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b arse->apVtabLock
1850: 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 [i]);. sq
1860: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
1870: 76 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c v, OP_VBegin, 0,
1880: 20 30 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 5f 0, 0, vtab, P4_
1890: 56 54 41 42 29 3b 0a 20 20 20 20 20 20 7d 0a 20 VTAB);. }.
18a0: 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 pParse->nVt
18b0: 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 23 65 6e 64 abLock = 0;.#end
18c0: 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 63 if.. /* Onc
18d0: 65 20 61 6c 6c 20 74 68 65 20 63 6f 6f 6b 69 65 e all the cookie
18e0: 73 20 68 61 76 65 20 62 65 65 6e 20 76 65 72 69 s have been veri
18f0: 66 69 65 64 20 61 6e 64 20 74 72 61 6e 73 61 63 fied and transac
1900: 74 69 6f 6e 73 20 6f 70 65 6e 65 64 2c 20 0a 20 tions opened, .
1910: 20 20 20 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 ** obtain t
1920: 68 65 20 72 65 71 75 69 72 65 64 20 74 61 62 6c he required tabl
1930: 65 2d 6c 6f 63 6b 73 2e 20 54 68 69 73 20 69 73 e-locks. This is
1940: 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 a no-op unless
1950: 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 68 the . ** sh
1960: 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 ared-cache featu
1970: 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 re is enabled..
1980: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f */. co
1990: 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 50 61 deTableLocks(pPa
19a0: 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 rse);.. /*
19b0: 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 79 20 41 Initialize any A
19c0: 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 64 61 74 UTOINCREMENT dat
19d0: 61 20 73 74 72 75 63 74 75 72 65 73 20 72 65 71 a structures req
19e0: 75 69 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a uired.. */.
19f0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 sqlite3Aut
1a00: 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 oincrementBegin(
1a10: 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 pParse);..
1a20: 2f 2a 20 43 6f 64 65 20 63 6f 6e 73 74 61 6e 74 /* Code constant
1a30: 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 expressions tha
1a40: 74 20 77 68 65 72 65 20 66 61 63 74 6f 72 65 64 t where factored
1a50: 20 6f 75 74 20 6f 66 20 69 6e 6e 65 72 20 6c 6f out of inner lo
1a60: 6f 70 73 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ops */. if(
1a70: 20 70 50 61 72 73 65 2d 3e 70 43 6f 6e 73 74 45 pParse->pConstE
1a80: 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 45 xpr ){. E
1a90: 78 70 72 4c 69 73 74 20 2a 70 45 4c 20 3d 20 70 xprList *pEL = p
1aa0: 50 61 72 73 65 2d 3e 70 43 6f 6e 73 74 45 78 70 Parse->pConstExp
1ab0: 72 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 r;. pPars
1ac0: 65 2d 3e 6f 6b 43 6f 6e 73 74 46 61 63 74 6f 72 e->okConstFactor
1ad0: 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f = 0;. fo
1ae0: 72 28 69 3d 30 3b 20 69 3c 70 45 4c 2d 3e 6e 45 r(i=0; i<pEL->nE
1af0: 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
1b00: 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1b10: 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 4c Code(pParse, pEL
1b20: 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 45 ->a[i].pExpr, pE
1b30: 4c 2d 3e 61 5b 69 5d 2e 75 2e 69 43 6f 6e 73 74 L->a[i].u.iConst
1b40: 45 78 70 72 52 65 67 29 3b 0a 20 20 20 20 20 20 ExprReg);.
1b50: 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1b60: 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 6a /* Finally, j
1b70: 75 6d 70 20 62 61 63 6b 20 74 6f 20 74 68 65 20 ump back to the
1b80: 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
1b90: 20 65 78 65 63 75 74 61 62 6c 65 20 63 6f 64 65 executable code
1ba0: 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 . */. sqlit
1bb0: 65 33 56 64 62 65 47 6f 74 6f 28 76 2c 20 31 29 e3VdbeGoto(v, 1)
1bc0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 ;. }. }...
1bd0: 2f 2a 20 47 65 74 20 74 68 65 20 56 44 42 45 20 /* Get the VDBE
1be0: 70 72 6f 67 72 61 6d 20 72 65 61 64 79 20 66 6f program ready fo
1bf0: 72 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f r execution. */
1c00: 0a 20 20 69 66 28 20 76 20 26 26 20 70 50 61 72 . if( v && pPar
1c10: 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 21 se->nErr==0 && !
1c20: 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1c30: 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1c40: 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 pParse->iCacheLe
1c50: 76 65 6c 3d 3d 30 20 29 3b 20 20 2f 2a 20 44 69 vel==0 ); /* Di
1c60: 73 61 62 6c 65 73 20 61 6e 64 20 72 65 2d 65 6e sables and re-en
1c70: 61 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f 0a 20 ables match */.
1c80: 20 20 20 2f 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 /* A minimum
1c90: 6f 66 20 6f 6e 65 20 63 75 72 73 6f 72 20 69 73 of one cursor is
1ca0: 20 72 65 71 75 69 72 65 64 20 69 66 20 61 75 74 required if aut
1cb0: 6f 69 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 73 oincrement is us
1cc0: 65 64 0a 20 20 20 20 2a 20 20 53 65 65 20 74 69 ed. * See ti
1cd0: 63 6b 65 74 20 5b 61 36 39 36 33 37 39 63 31 66 cket [a696379c1f
1ce0: 30 38 38 36 36 5d 20 2a 2f 0a 20 20 20 20 69 66 08866] */. if
1cf0: 28 20 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 21 ( pParse->pAinc!
1d00: 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 =0 && pParse->nT
1d10: 61 62 3d 3d 30 20 29 20 70 50 61 72 73 65 2d 3e ab==0 ) pParse->
1d20: 6e 54 61 62 20 3d 20 31 3b 0a 20 20 20 20 73 71 nTab = 1;. sq
1d30: 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 lite3VdbeMakeRea
1d40: 64 79 28 76 2c 20 70 50 61 72 73 65 29 3b 0a 20 dy(v, pParse);.
1d50: 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 pParse->rc =
1d60: 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d SQLITE_DONE;. }
1d70: 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 72 73 65 else{. pParse
1d80: 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 ->rc = SQLITE_ER
1d90: 52 4f 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ROR;. }.}../*.*
1da0: 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65 72 * Run the parser
1db0: 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 and code genera
1dc0: 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c 79 20 tor recursively
1dd0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 in order to gene
1de0: 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 rate.** code for
1df0: 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
1e00: 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20 74 68 nt given onto th
1e10: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 50 61 e end of the pPa
1e20: 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 63 rse context.** c
1e30: 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 urrently under c
1e40: 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 57 68 onstruction. Wh
1e50: 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 69 73 en the parser is
1e60: 20 72 75 6e 20 72 65 63 75 72 73 69 76 65 6c 79 run recursively
1e70: 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20 74 68 .** this way, th
1e80: 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 e final OP_Halt
1e90: 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65 64 20 is not appended
1ea0: 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74 69 61 and other initia
1eb0: 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 lization.** and
1ec0: 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73 74 65 finalization ste
1ed0: 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 62 ps are omitted b
1ee0: 65 63 61 75 73 65 20 74 68 6f 73 65 20 61 72 65 ecause those are
1ef0: 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 68 65 handling by the
1f00: 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20 70 61 .** outermost pa
1f10: 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 rser..**.** Not
1f20: 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 6e 65 everything is ne
1f30: 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20 66 61 stable. This fa
1f40: 63 69 6c 69 74 79 20 69 73 20 64 65 73 69 67 6e cility is design
1f50: 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a 2a 20 ed to permit.**
1f60: 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 INSERT, UPDATE,
1f70: 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 72 61 and DELETE opera
1f80: 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 53 51 tions against SQ
1f90: 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 55 73 LITE_MASTER. Us
1fa0: 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 6f 75 e.** care if you
1fb0: 20 64 65 63 69 64 65 20 74 6f 20 74 72 79 20 74 decide to try t
1fc0: 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 69 o use this routi
1fd0: 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 ne for some othe
1fe0: 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a 76 r purposes..*/.v
1ff0: 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 oid sqlite3Neste
2000: 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 dParse(Parse *pP
2010: 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 arse, const char
2020: 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
2030: 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
2040: 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 63 char *zSql;. c
2050: 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 har *zErrMsg = 0
2060: 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
2070: 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
2080: 63 68 61 72 20 73 61 76 65 42 75 66 5b 50 41 52 char saveBuf[PAR
2090: 53 45 5f 54 41 49 4c 5f 53 5a 5d 3b 0a 0a 20 20 SE_TAIL_SZ];..
20a0: 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 if( pParse->nErr
20b0: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
20c0: 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 ert( pParse->nes
20d0: 74 65 64 3c 31 30 20 29 3b 20 20 2f 2a 20 4e 65 ted<10 ); /* Ne
20e0: 73 74 69 6e 67 20 73 68 6f 75 6c 64 20 6f 6e 6c sting should onl
20f0: 79 20 62 65 20 6f 66 20 6c 69 6d 69 74 65 64 20 y be of limited
2100: 64 65 70 74 68 20 2a 2f 0a 20 20 76 61 5f 73 74 depth */. va_st
2110: 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
2120: 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 ;. zSql = sqlit
2130: 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a e3VMPrintf(db, z
2140: 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
2150: 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 a_end(ap);. if(
2160: 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 zSql==0 ){.
2170: 72 65 74 75 72 6e 3b 20 20 20 2f 2a 20 41 20 6d return; /* A m
2180: 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 alloc must have
2190: 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 failed */. }.
21a0: 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 2b 2b pParse->nested++
21b0: 3b 0a 20 20 6d 65 6d 63 70 79 28 73 61 76 65 42 ;. memcpy(saveB
21c0: 75 66 2c 20 50 41 52 53 45 5f 54 41 49 4c 28 70 uf, PARSE_TAIL(p
21d0: 50 61 72 73 65 29 2c 20 50 41 52 53 45 5f 54 41 Parse), PARSE_TA
21e0: 49 4c 5f 53 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 IL_SZ);. memset
21f0: 28 50 41 52 53 45 5f 54 41 49 4c 28 70 50 61 72 (PARSE_TAIL(pPar
2200: 73 65 29 2c 20 30 2c 20 50 41 52 53 45 5f 54 41 se), 0, PARSE_TA
2210: 49 4c 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 IL_SZ);. sqlite
2220: 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72 73 3RunParser(pPars
2230: 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 e, zSql, &zErrMs
2240: 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 g);. sqlite3DbF
2250: 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 ree(db, zErrMsg)
2260: 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
2270: 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d e(db, zSql);. m
2280: 65 6d 63 70 79 28 50 41 52 53 45 5f 54 41 49 4c emcpy(PARSE_TAIL
2290: 28 70 50 61 72 73 65 29 2c 20 73 61 76 65 42 75 (pParse), saveBu
22a0: 66 2c 20 50 41 52 53 45 5f 54 41 49 4c 5f 53 5a f, PARSE_TAIL_SZ
22b0: 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 );. pParse->nes
22c0: 74 65 64 2d 2d 3b 0a 7d 0a 0a 23 69 66 20 53 51 ted--;.}..#if SQ
22d0: 4c 49 54 45 5f 55 53 45 52 5f 41 55 54 48 45 4e LITE_USER_AUTHEN
22e0: 54 49 43 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 52 TICATION./*.** R
22f0: 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a 54 eturn TRUE if zT
2300: 61 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 able is the name
2310: 20 6f 66 20 74 68 65 20 73 79 73 74 65 6d 20 74 of the system t
2320: 61 62 6c 65 20 74 68 61 74 20 73 74 6f 72 65 73 able that stores
2330: 20 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 the.** list of
2340: 75 73 65 72 73 20 61 6e 64 20 74 68 65 69 72 20 users and their
2350: 61 63 63 65 73 73 20 63 72 65 64 65 6e 74 69 61 access credentia
2360: 6c 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 ls..*/.int sqlit
2370: 65 33 55 73 65 72 41 75 74 68 54 61 62 6c 65 28 e3UserAuthTable(
2380: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 const char *zTab
2390: 6c 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 le){. return sq
23a0: 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 7a 54 lite3_stricmp(zT
23b0: 61 62 6c 65 2c 20 22 73 71 6c 69 74 65 5f 75 73 able, "sqlite_us
23c0: 65 72 22 29 3d 3d 30 3b 0a 7d 0a 23 65 6e 64 69 er")==0;.}.#endi
23d0: 66 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 f../*.** Locate
23e0: 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 the in-memory st
23f0: 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 ructure that des
2400: 63 72 69 62 65 73 20 61 20 70 61 72 74 69 63 75 cribes a particu
2410: 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 lar database.**
2420: 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 table given the
2430: 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 name of that tab
2440: 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c le and (optional
2450: 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 ly) the name of
2460: 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
2470: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 containing the t
2480: 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 able. Return NU
2490: 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e LL if not found.
24a0: 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 .**.** If zDatab
24b0: 61 73 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 ase is 0, all da
24c0: 74 61 62 61 73 65 73 20 61 72 65 20 73 65 61 72 tabases are sear
24d0: 63 68 65 64 20 66 6f 72 20 74 68 65 20 74 61 62 ched for the tab
24e0: 6c 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 66 69 le and the.** fi
24f0: 72 73 74 20 6d 61 74 63 68 69 6e 67 20 74 61 62 rst matching tab
2500: 6c 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 le is returned.
2510: 20 28 4e 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f (No checking fo
2520: 72 20 64 75 70 6c 69 63 61 74 65 20 74 61 62 6c r duplicate tabl
2530: 65 0a 2a 2a 20 6e 61 6d 65 73 20 69 73 20 64 6f e.** names is do
2540: 6e 65 2e 29 20 20 54 68 65 20 73 65 61 72 63 68 ne.) The search
2550: 20 6f 72 64 65 72 20 69 73 20 54 45 4d 50 20 66 order is TEMP f
2560: 69 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c irst, then MAIN,
2570: 20 74 68 65 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 then any.** aux
2580: 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73 iliary databases
2590: 20 61 64 64 65 64 20 75 73 69 6e 67 20 74 68 65 added using the
25a0: 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e ATTACH command.
25b0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
25c0: 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
25d0: 6c 65 28 29 2e 0a 2a 2f 0a 54 61 62 6c 65 20 2a le()..*/.Table *
25e0: 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 sqlite3FindTable
25f0: 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
2600: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
2610: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 const char *zDa
2620: 74 61 62 61 73 65 29 7b 0a 20 20 54 61 62 6c 65 tabase){. Table
2630: 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 *p = 0;. int i
2640: 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 20 6d 75 74 65 ;.. /* All mute
2650: 78 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64 xes are required
2660: 20 66 6f 72 20 73 63 68 65 6d 61 20 61 63 63 65 for schema acce
2670: 73 73 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 77 ss. Make sure w
2680: 65 20 68 6f 6c 64 20 74 68 65 6d 2e 20 2a 2f 0a e hold them. */.
2690: 20 20 61 73 73 65 72 74 28 20 7a 44 61 74 61 62 assert( zDatab
26a0: 61 73 65 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ase!=0 || sqlite
26b0: 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 3BtreeHoldsAllMu
26c0: 74 65 78 65 73 28 64 62 29 20 29 3b 0a 23 69 66 texes(db) );.#if
26d0: 20 53 51 4c 49 54 45 5f 55 53 45 52 5f 41 55 54 SQLITE_USER_AUT
26e0: 48 45 4e 54 49 43 41 54 49 4f 4e 0a 20 20 2f 2a HENTICATION. /*
26f0: 20 4f 6e 6c 79 20 74 68 65 20 61 64 6d 69 6e 20 Only the admin
2700: 75 73 65 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 user is allowed
2710: 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 to know that the
2720: 20 73 71 6c 69 74 65 5f 75 73 65 72 20 74 61 62 sqlite_user tab
2730: 6c 65 0a 20 20 2a 2a 20 65 78 69 73 74 73 20 2a le. ** exists *
2740: 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 68 /. if( db->auth
2750: 2e 61 75 74 68 4c 65 76 65 6c 3c 55 41 55 54 48 .authLevel<UAUTH
2760: 5f 41 64 6d 69 6e 20 26 26 20 73 71 6c 69 74 65 _Admin && sqlite
2770: 33 55 73 65 72 41 75 74 68 54 61 62 6c 65 28 7a 3UserAuthTable(z
2780: 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Name)!=0 ){.
2790: 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 return 0;. }.#e
27a0: 6e 64 69 66 0a 20 20 77 68 69 6c 65 28 31 29 7b ndif. while(1){
27b0: 0a 20 20 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f . for(i=OMIT_
27c0: 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 TEMPDB; i<db->nD
27d0: 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 b; i++){. i
27e0: 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 nt j = (i<2) ? i
27f0: 5e 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53 65 61 ^1 : i; /* Sea
2800: 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 rch TEMP before
2810: 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 20 20 69 66 MAIN */. if
2820: 28 20 7a 44 61 74 61 62 61 73 65 3d 3d 30 20 7c ( zDatabase==0 |
2830: 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 | sqlite3StrICmp
2840: 28 7a 44 61 74 61 62 61 73 65 2c 20 64 62 2d 3e (zDatabase, db->
2850: 61 44 62 5b 6a 5d 2e 7a 44 62 53 4e 61 6d 65 29 aDb[j].zDbSName)
2860: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ==0 ){. a
2870: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 ssert( sqlite3Sc
2880: 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 hemaMutexHeld(db
2890: 2c 20 6a 2c 20 30 29 20 29 3b 0a 20 20 20 20 20 , j, 0) );.
28a0: 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 p = sqlite3Ha
28b0: 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b shFind(&db->aDb[
28c0: 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 j].pSchema->tblH
28d0: 61 73 68 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 ash, zName);.
28e0: 20 20 20 20 20 69 66 28 20 70 20 29 20 72 65 74 if( p ) ret
28f0: 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 urn p;. }.
2900: 20 20 20 7d 0a 20 20 20 20 2f 2a 20 4e 6f 74 20 }. /* Not
2910: 66 6f 75 6e 64 2e 20 20 49 66 20 74 68 65 20 6e found. If the n
2920: 61 6d 65 20 77 65 20 77 65 72 65 20 6c 6f 6f 6b ame we were look
2930: 69 6e 67 20 66 6f 72 20 77 61 73 20 74 65 6d 70 ing for was temp
2940: 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 0a 20 .sqlite_master.
2950: 20 20 20 2a 2a 20 74 68 65 6e 20 63 68 61 6e 67 ** then chang
2960: 65 20 74 68 65 20 6e 61 6d 65 20 74 6f 20 73 71 e the name to sq
2970: 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 lite_temp_master
2980: 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 20 and try again.
2990: 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
29a0: 65 33 53 74 72 49 43 6d 70 28 7a 4e 61 6d 65 2c e3StrICmp(zName,
29b0: 20 4d 41 53 54 45 52 5f 4e 41 4d 45 29 21 3d 30 MASTER_NAME)!=0
29c0: 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 ) break;. if
29d0: 28 20 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d ( sqlite3_stricm
29e0: 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64 62 2d p(zDatabase, db-
29f0: 3e 61 44 62 5b 31 5d 2e 7a 44 62 53 4e 61 6d 65 >aDb[1].zDbSName
2a00: 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 )!=0 ) break;.
2a10: 20 20 7a 4e 61 6d 65 20 3d 20 54 45 4d 50 5f 4d zName = TEMP_M
2a20: 41 53 54 45 52 5f 4e 41 4d 45 3b 0a 20 20 7d 0a ASTER_NAME;. }.
2a30: 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
2a40: 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 *.** Locate the
2a50: 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 in-memory struct
2a60: 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 ure that describ
2a70: 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 es a particular
2a80: 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c database.** tabl
2a90: 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 e given the name
2aa0: 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 of that table a
2ab0: 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 nd (optionally)
2ac0: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a the name of the.
2ad0: 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 ** database cont
2ae0: 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 aining the table
2af0: 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 . Return NULL i
2b00: 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 20 20 41 6c f not found. Al
2b10: 73 6f 20 6c 65 61 76 65 20 61 6e 0a 2a 2a 20 65 so leave an.** e
2b20: 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 rror message in
2b30: 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e pParse->zErrMsg.
2b40: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 .**.** The diffe
2b50: 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 rence between th
2b60: 69 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 73 is routine and s
2b70: 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
2b80: 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a ) is that this.*
2b90: 2a 20 72 6f 75 74 69 6e 65 20 6c 65 61 76 65 73 * routine leaves
2ba0: 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
2bb0: 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 e in pParse->zEr
2bc0: 72 4d 73 67 20 77 68 65 72 65 0a 2a 2a 20 73 71 rMsg where.** sq
2bd0: 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29 lite3FindTable()
2be0: 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 54 61 does not..*/.Ta
2bf0: 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 ble *sqlite3Loca
2c00: 74 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 teTable(. Parse
2c10: 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
2c20: 20 20 2f 2a 20 63 6f 6e 74 65 78 74 20 69 6e 20 /* context in
2c30: 77 68 69 63 68 20 74 6f 20 72 65 70 6f 72 74 20 which to report
2c40: 65 72 72 6f 72 73 20 2a 2f 0a 20 20 75 33 32 20 errors */. u32
2c50: 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
2c60: 20 20 20 2f 2a 20 4c 4f 43 41 54 45 5f 56 49 45 /* LOCATE_VIE
2c70: 57 20 6f 72 20 4c 4f 43 41 54 45 5f 4e 4f 45 52 W or LOCATE_NOER
2c80: 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 R */. const cha
2c90: 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 2f 2a r *zName, /*
2ca0: 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 Name of the tab
2cb0: 6c 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e le we are lookin
2cc0: 67 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 g for */. const
2cd0: 20 63 68 61 72 20 2a 7a 44 62 61 73 65 20 20 20 char *zDbase
2ce0: 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
2cf0: 20 64 61 74 61 62 61 73 65 2e 20 20 4d 69 67 68 database. Migh
2d00: 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a t be NULL */.){.
2d10: 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a 20 20 2f Table *p;.. /
2d20: 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61 62 * Read the datab
2d30: 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 ase schema. If a
2d40: 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
2d50: 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d leave an error m
2d60: 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 essage. ** and
2d70: 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 code in pParse a
2d80: 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 nd return NULL.
2d90: 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f */. if( SQLITE_
2da0: 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 OK!=sqlite3ReadS
2db0: 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b chema(pParse) ){
2dc0: 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
2dd0: 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 }.. p = sqlite
2de0: 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61 72 73 3FindTable(pPars
2df0: 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 e->db, zName, zD
2e00: 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d base);. if( p==
2e10: 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 0 ){. const c
2e20: 68 61 72 20 2a 7a 4d 73 67 20 3d 20 66 6c 61 67 har *zMsg = flag
2e30: 73 20 26 20 4c 4f 43 41 54 45 5f 56 49 45 57 20 s & LOCATE_VIEW
2e40: 3f 20 22 6e 6f 20 73 75 63 68 20 76 69 65 77 22 ? "no such view"
2e50: 20 3a 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c : "no such tabl
2e60: 65 22 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 e";.#ifndef SQLI
2e70: 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
2e80: 41 42 4c 45 0a 20 20 20 20 69 66 28 20 73 71 6c ABLE. if( sql
2e90: 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 70 ite3FindDbName(p
2ea0: 50 61 72 73 65 2d 3e 64 62 2c 20 7a 44 62 61 73 Parse->db, zDbas
2eb0: 65 29 3c 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a e)<1 ){. /*
2ec0: 20 49 66 20 7a 4e 61 6d 65 20 69 73 20 74 68 65 If zName is the
2ed0: 20 6e 6f 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 not the name of
2ee0: 20 61 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 a table in the
2ef0: 73 63 68 65 6d 61 20 63 72 65 61 74 65 64 20 75 schema created u
2f00: 73 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 43 52 sing. ** CR
2f10: 45 41 54 45 2c 20 74 68 65 6e 20 63 68 65 63 6b EATE, then check
2f20: 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 69 73 to see if it is
2f30: 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 the name of an
2f40: 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 68 virtual table th
2f50: 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 61 6e 20 at. ** can
2f60: 62 65 20 61 6e 20 65 70 6f 6e 79 6d 6f 75 73 20 be an eponymous
2f70: 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 2a virtual table. *
2f80: 2f 0a 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 2a /. Module *
2f90: 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 2a 29 pMod = (Module*)
2fa0: 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 sqlite3HashFind(
2fb0: 26 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4d 6f &pParse->db->aMo
2fc0: 64 75 6c 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 dule, zName);.
2fd0: 20 20 20 20 69 66 28 20 70 4d 6f 64 3d 3d 30 20 if( pMod==0
2fe0: 26 26 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 && sqlite3_strni
2ff0: 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 70 72 61 67 cmp(zName, "prag
3000: 6d 61 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 ma_", 7)==0 ){.
3010: 20 20 20 20 20 20 20 70 4d 6f 64 20 3d 20 73 71 pMod = sq
3020: 6c 69 74 65 33 50 72 61 67 6d 61 56 74 61 62 52 lite3PragmaVtabR
3030: 65 67 69 73 74 65 72 28 70 50 61 72 73 65 2d 3e egister(pParse->
3040: 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 db, zName);.
3050: 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4d }. if( pM
3060: 6f 64 20 26 26 20 73 71 6c 69 74 65 33 56 74 61 od && sqlite3Vta
3070: 62 45 70 6f 6e 79 6d 6f 75 73 54 61 62 6c 65 49 bEponymousTableI
3080: 6e 69 74 28 70 50 61 72 73 65 2c 20 70 4d 6f 64 nit(pParse, pMod
3090: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 ) ){. ret
30a0: 75 72 6e 20 70 4d 6f 64 2d 3e 70 45 70 6f 54 61 urn pMod->pEpoTa
30b0: 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d b;. }. }
30c0: 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 .#endif. if(
30d0: 28 66 6c 61 67 73 20 26 20 4c 4f 43 41 54 45 5f (flags & LOCATE_
30e0: 4e 4f 45 52 52 29 3d 3d 30 20 29 7b 0a 20 20 20 NOERR)==0 ){.
30f0: 20 20 20 69 66 28 20 7a 44 62 61 73 65 20 29 7b if( zDbase ){
3100: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
3110: 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
3120: 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 4d "%s: %s.%s", zM
3130: 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e 61 6d sg, zDbase, zNam
3140: 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b e);. }else{
3150: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
3160: 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
3170: 20 22 25 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c "%s: %s", zMsg,
3180: 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d zName);. }
3190: 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63 . pParse->c
31a0: 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a heckSchema = 1;.
31b0: 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
31c0: 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
31d0: 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 Locate the table
31e0: 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 2a identified by *
31f0: 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 p..**.** This is
3200: 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e a wrapper aroun
3210: 64 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 d sqlite3LocateT
3220: 61 62 6c 65 28 29 2e 20 54 68 65 20 64 69 66 66 able(). The diff
3230: 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 0a 2a erence between.*
3240: 2a 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 * sqlite3LocateT
3250: 61 62 6c 65 28 29 20 61 6e 64 20 74 68 69 73 20 able() and this
3260: 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 61 74 function is that
3270: 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
3280: 65 73 74 72 69 63 74 73 0a 2a 2a 20 74 68 65 20 estricts.** the
3290: 73 65 61 72 63 68 20 74 6f 20 73 63 68 65 6d 61 search to schema
32a0: 20 28 70 2d 3e 70 53 63 68 65 6d 61 29 20 69 66 (p->pSchema) if
32b0: 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e it is not NULL.
32c0: 20 70 2d 3e 70 53 63 68 65 6d 61 20 6d 61 79 20 p->pSchema may
32d0: 62 65 0a 2a 2a 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 be.** non-NULL i
32e0: 66 20 69 74 20 69 73 20 70 61 72 74 20 6f 66 20 f it is part of
32f0: 61 20 76 69 65 77 20 6f 72 20 74 72 69 67 67 65 a view or trigge
3300: 72 20 70 72 6f 67 72 61 6d 20 64 65 66 69 6e 69 r program defini
3310: 74 69 6f 6e 2e 20 53 65 65 0a 2a 2a 20 73 71 6c tion. See.** sql
3320: 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 29 ite3FixSrcList()
3330: 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f for details..*/
3340: 0a 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c .Table *sqlite3L
3350: 6f 63 61 74 65 54 61 62 6c 65 49 74 65 6d 28 0a ocateTableItem(.
3360: 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
3370: 20 0a 20 20 75 33 32 20 66 6c 61 67 73 2c 0a 20 . u32 flags,.
3380: 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
3390: 69 74 65 6d 20 2a 70 0a 29 7b 0a 20 20 63 6f 6e item *p.){. con
33a0: 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 st char *zDb;.
33b0: 61 73 73 65 72 74 28 20 70 2d 3e 70 53 63 68 65 assert( p->pSche
33c0: 6d 61 3d 3d 30 20 7c 7c 20 70 2d 3e 7a 44 61 74 ma==0 || p->zDat
33d0: 61 62 61 73 65 3d 3d 30 20 29 3b 0a 20 20 69 66 abase==0 );. if
33e0: 28 20 70 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a ( p->pSchema ){.
33f0: 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 int iDb = sq
3400: 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
3410: 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 ex(pParse->db, p
3420: 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 ->pSchema);.
3430: 7a 44 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 zDb = pParse->db
3440: 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 4e ->aDb[iDb].zDbSN
3450: 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ame;. }else{.
3460: 20 20 7a 44 62 20 3d 20 70 2d 3e 7a 44 61 74 61 zDb = p->zData
3470: 62 61 73 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 base;. }. retu
3480: 72 6e 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 rn sqlite3Locate
3490: 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 66 6c Table(pParse, fl
34a0: 61 67 73 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a ags, p->zName, z
34b0: 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f Db);.}../*.** Lo
34c0: 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f cate the in-memo
34d0: 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ry structure tha
34e0: 74 20 64 65 73 63 72 69 62 65 73 20 0a 2a 2a 20 t describes .**
34f0: 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 a particular ind
3500: 65 78 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d ex given the nam
3510: 65 20 6f 66 20 74 68 61 74 20 69 6e 64 65 78 0a e of that index.
3520: 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 20 ** and the name
3530: 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
3540: 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
3550: 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 52 65 74 75 e index..** Retu
3560: 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 rn NULL if not f
3570: 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a ound..**.** If z
3580: 44 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 Database is 0, a
3590: 6c 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 ll databases are
35a0: 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 searched for th
35b0: 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 74 e.** table and t
35c0: 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e he first matchin
35d0: 67 20 69 6e 64 65 78 20 69 73 20 72 65 74 75 72 g index is retur
35e0: 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 ned. (No checki
35f0: 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 70 6c 69 63 ng.** for duplic
3600: 61 74 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 ate index names
3610: 69 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 is done.) The s
3620: 65 61 72 63 68 20 6f 72 64 65 72 20 69 73 0a 2a earch order is.*
3630: 2a 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74 68 * TEMP first, th
3640: 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e en MAIN, then an
3650: 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 y auxiliary data
3660: 62 61 73 65 73 20 61 64 64 65 64 0a 2a 2a 20 75 bases added.** u
3670: 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 sing the ATTACH
3680: 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 49 6e 64 65 command..*/.Inde
3690: 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e x *sqlite3FindIn
36a0: 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c dex(sqlite3 *db,
36b0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
36c0: 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a me, const char *
36d0: 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 zDb){. Index *p
36e0: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 = 0;. int i;.
36f0: 20 2f 2a 20 41 6c 6c 20 6d 75 74 65 78 65 73 20 /* All mutexes
3700: 61 72 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 are required for
3710: 20 73 63 68 65 6d 61 20 61 63 63 65 73 73 2e 20 schema access.
3720: 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68 6f Make sure we ho
3730: 6c 64 20 74 68 65 6d 2e 20 2a 2f 0a 20 20 61 73 ld them. */. as
3740: 73 65 72 74 28 20 7a 44 62 21 3d 30 20 7c 7c 20 sert( zDb!=0 ||
3750: 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
3760: 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 sAllMutexes(db)
3770: 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f );. for(i=OMIT_
3780: 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 TEMPDB; i<db->nD
3790: 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 b; i++){. int
37a0: 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 j = (i<2) ? i^1
37b0: 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 : i; /* Search
37c0: 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 TEMP before MAI
37d0: 4e 20 2a 2f 0a 20 20 20 20 53 63 68 65 6d 61 20 N */. Schema
37e0: 2a 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 *pSchema = db->a
37f0: 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 Db[j].pSchema;.
3800: 20 20 20 61 73 73 65 72 74 28 20 70 53 63 68 65 assert( pSche
3810: 6d 61 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 44 ma );. if( zD
3820: 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 b && sqlite3StrI
3830: 43 6d 70 28 7a 44 62 2c 20 64 62 2d 3e 61 44 62 Cmp(zDb, db->aDb
3840: 5b 6a 5d 2e 7a 44 62 53 4e 61 6d 65 29 20 29 20 [j].zDbSName) )
3850: 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 continue;. as
3860: 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 sert( sqlite3Sch
3870: 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c emaMutexHeld(db,
3880: 20 6a 2c 20 30 29 20 29 3b 0a 20 20 20 20 70 20 j, 0) );. p
3890: 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e = sqlite3HashFin
38a0: 64 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 d(&pSchema->idxH
38b0: 61 73 68 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 ash, zName);.
38c0: 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a if( p ) break;.
38d0: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
38e0: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d }../*.** Reclaim
38f0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 the memory used
3900: 20 62 79 20 61 6e 20 69 6e 64 65 78 0a 2a 2f 0a by an index.*/.
3910: 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 static void free
3920: 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 Index(sqlite3 *d
3930: 62 2c 20 49 6e 64 65 78 20 2a 70 29 7b 0a 23 69 b, Index *p){.#i
3940: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
3950: 54 5f 41 4e 41 4c 59 5a 45 0a 20 20 73 71 6c 69 T_ANALYZE. sqli
3960: 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53 61 te3DeleteIndexSa
3970: 6d 70 6c 65 73 28 64 62 2c 20 70 29 3b 0a 23 65 mples(db, p);.#e
3980: 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 45 78 ndif. sqlite3Ex
3990: 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e prDelete(db, p->
39a0: 70 50 61 72 74 49 64 78 57 68 65 72 65 29 3b 0a pPartIdxWhere);.
39b0: 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 sqlite3ExprLis
39c0: 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 61 tDelete(db, p->a
39d0: 43 6f 6c 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 ColExpr);. sqli
39e0: 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
39f0: 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 69 66 28 >zColAff);. if(
3a00: 20 70 2d 3e 69 73 52 65 73 69 7a 65 64 20 29 20 p->isResized )
3a10: 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
3a20: 2c 20 28 76 6f 69 64 20 2a 29 70 2d 3e 61 7a 43 , (void *)p->azC
3a30: 6f 6c 6c 29 3b 0a 23 69 66 64 65 66 20 53 51 4c oll);.#ifdef SQL
3a40: 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 ITE_ENABLE_STAT3
3a50: 5f 4f 52 5f 53 54 41 54 34 0a 20 20 73 71 6c 69 _OR_STAT4. sqli
3a60: 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 69 52 6f te3_free(p->aiRo
3a70: 77 45 73 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 wEst);.#endif.
3a80: 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
3a90: 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 , p);.}../*.** F
3aa0: 6f 72 20 74 68 65 20 69 6e 64 65 78 20 63 61 6c or the index cal
3ab0: 6c 65 64 20 7a 49 64 78 4e 61 6d 65 20 77 68 69 led zIdxName whi
3ac0: 63 68 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 ch is found in t
3ad0: 68 65 20 64 61 74 61 62 61 73 65 20 69 44 62 2c he database iDb,
3ae0: 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74 68 61 74 20 .** unlike that
3af0: 69 6e 64 65 78 20 66 72 6f 6d 20 69 74 73 20 54 index from its T
3b00: 61 62 6c 65 20 74 68 65 6e 20 72 65 6d 6f 76 65 able then remove
3b10: 20 74 68 65 20 69 6e 64 65 78 20 66 72 6f 6d 0a the index from.
3b20: 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 ** the index has
3b30: 68 20 74 61 62 6c 65 20 61 6e 64 20 66 72 65 65 h table and free
3b40: 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 73 74 72 75 all memory stru
3b50: 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74 65 ctures associate
3b60: 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 69 6e d.** with the in
3b70: 64 65 78 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c dex..*/.void sql
3b80: 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c ite3UnlinkAndDel
3b90: 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33 eteIndex(sqlite3
3ba0: 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 *db, int iDb, c
3bb0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 78 4e onst char *zIdxN
3bc0: 61 6d 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 ame){. Index *p
3bd0: 49 6e 64 65 78 3b 0a 20 20 48 61 73 68 20 2a 70 Index;. Hash *p
3be0: 48 61 73 68 3b 0a 0a 20 20 61 73 73 65 72 74 28 Hash;.. assert(
3bf0: 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 sqlite3SchemaMu
3c00: 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c texHeld(db, iDb,
3c10: 20 30 29 20 29 3b 0a 20 20 70 48 61 73 68 20 3d 0) );. pHash =
3c20: 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 &db->aDb[iDb].p
3c30: 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b Schema->idxHash;
3c40: 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 . pIndex = sqli
3c50: 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 te3HashInsert(pH
3c60: 61 73 68 2c 20 7a 49 64 78 4e 61 6d 65 2c 20 30 ash, zIdxName, 0
3c70: 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 );. if( ALWAYS(
3c80: 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 69 pIndex) ){. i
3c90: 66 28 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c f( pIndex->pTabl
3ca0: 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e 64 65 e->pIndex==pInde
3cb0: 78 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 x ){. pInde
3cc0: 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 x->pTable->pInde
3cd0: 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 x = pIndex->pNex
3ce0: 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
3cf0: 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 Index *p;.
3d00: 20 20 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 /* Justifica
3d10: 74 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 tion of ALWAYS()
3d20: 3b 20 20 54 68 65 20 69 6e 64 65 78 20 6d 75 73 ; The index mus
3d30: 74 20 62 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 t be on the list
3d40: 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64 of. ** ind
3d50: 69 63 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70 ices. */. p
3d60: 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c = pIndex->pTabl
3d70: 65 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 e->pIndex;.
3d80: 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 70 while( ALWAYS(p
3d90: 29 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 ) && p->pNext!=p
3da0: 49 6e 64 65 78 20 29 7b 20 70 20 3d 20 70 2d 3e Index ){ p = p->
3db0: 70 4e 65 78 74 3b 20 7d 0a 20 20 20 20 20 20 69 pNext; }. i
3dc0: 66 28 20 41 4c 57 41 59 53 28 70 20 26 26 20 70 f( ALWAYS(p && p
3dd0: 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 65 78 29 ->pNext==pIndex)
3de0: 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 ){. p->p
3df0: 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 Next = pIndex->p
3e00: 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
3e10: 20 20 7d 0a 20 20 20 20 66 72 65 65 49 6e 64 65 }. freeInde
3e20: 78 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0a 20 x(db, pIndex);.
3e30: 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c }. db->flags |
3e40: 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 = SQLITE_InternC
3e50: 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a hanges;.}../*.**
3e60: 20 4c 6f 6f 6b 20 74 68 72 6f 75 67 68 20 74 68 Look through th
3e70: 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 6e 20 64 e list of open d
3e80: 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 69 6e atabase files in
3e90: 20 64 62 2d 3e 61 44 62 5b 5d 20 61 6e 64 20 69 db->aDb[] and i
3ea0: 66 0a 2a 2a 20 61 6e 79 20 68 61 76 65 20 62 65 f.** any have be
3eb0: 65 6e 20 63 6c 6f 73 65 64 2c 20 72 65 6d 6f 76 en closed, remov
3ec0: 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20 e them from the
3ed0: 6c 69 73 74 2e 20 20 52 65 61 6c 6c 6f 63 61 74 list. Reallocat
3ee0: 65 20 74 68 65 0a 2a 2a 20 64 62 2d 3e 61 44 62 e the.** db->aDb
3ef0: 5b 5d 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 [] structure to
3f00: 61 20 73 6d 61 6c 6c 65 72 20 73 69 7a 65 2c 20 a smaller size,
3f10: 69 66 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a if possible..**.
3f20: 2a 2a 20 45 6e 74 72 79 20 30 20 28 74 68 65 20 ** Entry 0 (the
3f30: 22 6d 61 69 6e 22 20 64 61 74 61 62 61 73 65 29 "main" database)
3f40: 20 61 6e 64 20 65 6e 74 72 79 20 31 20 28 74 68 and entry 1 (th
3f50: 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 73 e "temp" databas
3f60: 65 29 0a 2a 2a 20 61 72 65 20 6e 65 76 65 72 20 e).** are never
3f70: 63 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20 62 candidates for b
3f80: 65 69 6e 67 20 63 6f 6c 6c 61 70 73 65 64 2e 0a eing collapsed..
3f90: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 43 */.void sqlite3C
3fa0: 6f 6c 6c 61 70 73 65 44 61 74 61 62 61 73 65 41 ollapseDatabaseA
3fb0: 72 72 61 79 28 73 71 6c 69 74 65 33 20 2a 64 62 rray(sqlite3 *db
3fc0: 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 ){. int i, j;.
3fd0: 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c 64 62 for(i=j=2; i<db
3fe0: 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
3ff0: 20 73 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 struct Db *pDb
4000: 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 = &db->aDb[i];.
4010: 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d if( pDb->pBt=
4020: 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
4030: 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 te3DbFree(db, pD
4040: 62 2d 3e 7a 44 62 53 4e 61 6d 65 29 3b 0a 20 20 b->zDbSName);.
4050: 20 20 20 20 70 44 62 2d 3e 7a 44 62 53 4e 61 6d pDb->zDbSNam
4060: 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e e = 0;. con
4070: 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 tinue;. }.
4080: 20 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20 if( j<i ){.
4090: 20 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 db->aDb[j] = d
40a0: 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d b->aDb[i];. }
40b0: 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 . j++;. }.
40c0: 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 69 db->nDb = j;. i
40d0: 66 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 26 f( db->nDb<=2 &&
40e0: 20 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 44 db->aDb!=db->aD
40f0: 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 6d bStatic ){. m
4100: 65 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 61 emcpy(db->aDbSta
4110: 74 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 2a tic, db->aDb, 2*
4120: 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 sizeof(db->aDb[0
4130: 5d 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ]));. sqlite3
4140: 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 DbFree(db, db->a
4150: 44 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 Db);. db->aDb
4160: 20 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 = db->aDbStatic
4170: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
4180: 65 73 65 74 20 74 68 65 20 73 63 68 65 6d 61 20 eset the schema
4190: 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 for the database
41a0: 20 61 74 20 69 6e 64 65 78 20 69 44 62 2e 20 20 at index iDb.
41b0: 41 6c 73 6f 20 72 65 73 65 74 20 74 68 65 0a 2a Also reset the.*
41c0: 2a 20 54 45 4d 50 20 73 63 68 65 6d 61 2e 0a 2a * TEMP schema..*
41d0: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 /.void sqlite3Re
41e0: 73 65 74 4f 6e 65 53 63 68 65 6d 61 28 73 71 6c setOneSchema(sql
41f0: 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 ite3 *db, int iD
4200: 62 29 7b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 b){. Db *pDb;.
4210: 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d assert( iDb<db-
4220: 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 >nDb );.. /* Ca
4230: 73 65 20 31 3a 20 20 52 65 73 65 74 20 74 68 65 se 1: Reset the
4240: 20 73 69 6e 67 6c 65 20 73 63 68 65 6d 61 20 69 single schema i
4250: 64 65 6e 74 69 66 69 65 64 20 62 79 20 69 44 62 dentified by iDb
4260: 20 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62 2d */. pDb = &db-
4270: 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 61 73 73 >aDb[iDb];. ass
4280: 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 ert( sqlite3Sche
4290: 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 maMutexHeld(db,
42a0: 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 61 73 73 iDb, 0) );. ass
42b0: 65 72 74 28 20 70 44 62 2d 3e 70 53 63 68 65 6d ert( pDb->pSchem
42c0: 61 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 a!=0 );. sqlite
42d0: 33 53 63 68 65 6d 61 43 6c 65 61 72 28 70 44 62 3SchemaClear(pDb
42e0: 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f ->pSchema);.. /
42f0: 2a 20 49 66 20 61 6e 79 20 64 61 74 61 62 61 73 * If any databas
4300: 65 20 6f 74 68 65 72 20 74 68 61 6e 20 54 45 4d e other than TEM
4310: 50 20 69 73 20 72 65 73 65 74 2c 20 74 68 65 6e P is reset, then
4320: 20 61 6c 73 6f 20 72 65 73 65 74 20 54 45 4d 50 also reset TEMP
4330: 0a 20 20 2a 2a 20 73 69 6e 63 65 20 54 45 4d 50 . ** since TEMP
4340: 20 6d 69 67 68 74 20 62 65 20 68 6f 6c 64 69 6e might be holdin
4350: 67 20 74 72 69 67 67 65 72 73 20 74 68 61 74 20 g triggers that
4360: 72 65 66 65 72 65 6e 63 65 20 74 61 62 6c 65 73 reference tables
4370: 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 in the. ** oth
4380: 65 72 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a er database.. *
4390: 2f 0a 20 20 69 66 28 20 69 44 62 21 3d 31 20 29 /. if( iDb!=1 )
43a0: 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d {. pDb = &db-
43b0: 3e 61 44 62 5b 31 5d 3b 0a 20 20 20 20 61 73 73 >aDb[1];. ass
43c0: 65 72 74 28 20 70 44 62 2d 3e 70 53 63 68 65 6d ert( pDb->pSchem
43d0: 61 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 a!=0 );. sqli
43e0: 74 65 33 53 63 68 65 6d 61 43 6c 65 61 72 28 70 te3SchemaClear(p
43f0: 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 Db->pSchema);.
4400: 7d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f }. return;.}../
4410: 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 *.** Erase all s
4420: 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f chema informatio
4430: 6e 20 66 72 6f 6d 20 61 6c 6c 20 61 74 74 61 63 n from all attac
4440: 68 65 64 20 64 61 74 61 62 61 73 65 73 20 28 69 hed databases (i
4450: 6e 63 6c 75 64 69 6e 67 0a 2a 2a 20 22 6d 61 69 ncluding.** "mai
4460: 6e 22 20 61 6e 64 20 22 74 65 6d 70 22 29 20 66 n" and "temp") f
4470: 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 or a single data
4480: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
4490: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
44a0: 52 65 73 65 74 41 6c 6c 53 63 68 65 6d 61 73 4f ResetAllSchemasO
44b0: 66 43 6f 6e 6e 65 63 74 69 6f 6e 28 73 71 6c 69 fConnection(sqli
44c0: 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 te3 *db){. int
44d0: 69 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 i;. sqlite3Btre
44e0: 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 eEnterAll(db);.
44f0: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
4500: 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44 nDb; i++){. D
4510: 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 b *pDb = &db->aD
4520: 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 b[i];. if( pD
4530: 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 b->pSchema ){.
4540: 20 20 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d sqlite3Schem
4550: 61 43 6c 65 61 72 28 70 44 62 2d 3e 70 53 63 68 aClear(pDb->pSch
4560: 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ema);. }. }.
4570: 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e db->flags &= ~
4580: 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
4590: 6e 67 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 56 nges;. sqlite3V
45a0: 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 tabUnlockList(db
45b0: 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
45c0: 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 eLeaveAll(db);.
45d0: 20 73 71 6c 69 74 65 33 43 6f 6c 6c 61 70 73 65 sqlite3Collapse
45e0: 44 61 74 61 62 61 73 65 41 72 72 61 79 28 64 62 DatabaseArray(db
45f0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 );.}../*.** This
4600: 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
4610: 65 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74 ed when a commit
4620: 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 76 6f 69 64 occurs..*/.void
4630: 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e sqlite3CommitIn
4640: 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 ternalChanges(sq
4650: 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64 62 lite3 *db){. db
4660: 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 ->flags &= ~SQLI
4670: 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 TE_InternChanges
4680: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
4690: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
46a0: 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d ed for the colum
46b0: 6e 20 6e 61 6d 65 73 20 6f 66 20 61 20 74 61 62 n names of a tab
46c0: 6c 65 20 6f 72 20 76 69 65 77 20 28 74 68 65 0a le or view (the.
46d0: 2a 2a 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 ** Table.aCol[]
46e0: 61 72 72 61 79 29 2e 0a 2a 2f 0a 76 6f 69 64 20 array)..*/.void
46f0: 73 71 6c 69 74 65 33 44 65 6c 65 74 65 43 6f 6c sqlite3DeleteCol
4700: 75 6d 6e 4e 61 6d 65 73 28 73 71 6c 69 74 65 33 umnNames(sqlite3
4710: 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 *db, Table *pTa
4720: 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ble){. int i;.
4730: 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 Column *pCol;.
4740: 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21 assert( pTable!
4750: 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 6f =0 );. if( (pCo
4760: 6c 20 3d 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c l = pTable->aCol
4770: 29 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 )!=0 ){. for(
4780: 69 3d 30 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e i=0; i<pTable->n
4790: 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b Col; i++, pCol++
47a0: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
47b0: 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d DbFree(db, pCol-
47c0: 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 >zName);. s
47d0: 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
47e0: 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 (db, pCol->pDflt
47f0: 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
4800: 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d DbFree(db, pCol-
4810: 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 >zColl);. }.
4820: 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
4830: 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f (db, pTable->aCo
4840: 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a l);. }.}../*.**
4850: 20 52 65 6d 6f 76 65 20 74 68 65 20 6d 65 6d 6f Remove the memo
4860: 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75 72 ry data structur
4870: 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
4880: 74 68 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 th the given.**
4890: 54 61 62 6c 65 2e 20 20 4e 6f 20 63 68 61 6e 67 Table. No chang
48a0: 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 64 es are made to d
48b0: 69 73 6b 20 62 79 20 74 68 69 73 20 72 6f 75 74 isk by this rout
48c0: 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ine..**.** This
48d0: 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 64 65 6c routine just del
48e0: 65 74 65 73 20 74 68 65 20 64 61 74 61 20 73 74 etes the data st
48f0: 72 75 63 74 75 72 65 2e 20 20 49 74 20 64 6f 65 ructure. It doe
4900: 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 s not unlink.**
4910: 74 68 65 20 74 61 62 6c 65 20 64 61 74 61 20 73 the table data s
4920: 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 tructure from th
4930: 65 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 42 e hash table. B
4940: 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74 72 ut it does destr
4950: 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74 72 oy.** memory str
4960: 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20 69 uctures of the i
4970: 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 69 ndices and forei
4980: 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 74 gn keys associat
4990: 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 20 ed with .** the
49a0: 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 table..**.** The
49b0: 20 64 62 20 70 61 72 61 6d 65 74 65 72 20 69 73 db parameter is
49c0: 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 74 20 69 optional. It i
49d0: 73 20 6e 65 65 64 65 64 20 69 66 20 74 68 65 20 s needed if the
49e0: 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 0a 2a 2a Table object .**
49f0: 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 6f 6b 61 73 contains lookas
4a00: 69 64 65 20 6d 65 6d 6f 72 79 2e 20 20 28 54 61 ide memory. (Ta
4a10: 62 6c 65 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 ble objects in t
4a20: 68 65 20 73 63 68 65 6d 61 20 64 6f 20 6e 6f 74 he schema do not
4a30: 20 75 73 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 use.** lookasid
4a40: 65 20 6d 65 6d 6f 72 79 2c 20 62 75 74 20 73 6f e memory, but so
4a50: 6d 65 20 65 70 68 65 6d 65 72 61 6c 20 54 61 62 me ephemeral Tab
4a60: 6c 65 20 6f 62 6a 65 63 74 73 20 64 6f 2e 29 20 le objects do.)
4a70: 20 4f 72 20 74 68 65 0a 2a 2a 20 64 62 20 70 61 Or the.** db pa
4a80: 72 61 6d 65 74 65 72 20 63 61 6e 20 62 65 20 75 rameter can be u
4a90: 73 65 64 20 77 69 74 68 20 64 62 2d 3e 70 6e 42 sed with db->pnB
4aa0: 79 74 65 73 46 72 65 65 64 20 74 6f 20 6d 65 61 ytesFreed to mea
4ab0: 73 75 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 0a sure the memory.
4ac0: 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 54 ** used by the T
4ad0: 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a able object..*/.
4ae0: 73 74 61 74 69 63 20 76 6f 69 64 20 53 51 4c 49 static void SQLI
4af0: 54 45 5f 4e 4f 49 4e 4c 49 4e 45 20 64 65 6c 65 TE_NOINLINE dele
4b00: 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 teTable(sqlite3
4b10: 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 *db, Table *pTab
4b20: 6c 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 le){. Index *pI
4b30: 6e 64 65 78 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 ndex, *pNext;.
4b40: 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 6e 4c TESTONLY( int nL
4b50: 6f 6f 6b 61 73 69 64 65 3b 20 29 20 2f 2a 20 55 ookaside; ) /* U
4b60: 73 65 64 20 74 6f 20 76 65 72 69 66 79 20 6c 6f sed to verify lo
4b70: 6f 6b 61 73 69 64 65 20 6e 6f 74 20 75 73 65 64 okaside not used
4b80: 20 66 6f 72 20 73 63 68 65 6d 61 20 2a 2f 0a 0a for schema */..
4b90: 20 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20 /* Record the
4ba0: 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61 number of outsta
4bb0: 6e 64 69 6e 67 20 6c 6f 6f 6b 61 73 69 64 65 20 nding lookaside
4bc0: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 20 73 allocations in s
4bd0: 63 68 65 6d 61 20 54 61 62 6c 65 73 0a 20 20 2a chema Tables. *
4be0: 2a 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e 67 * prior to doing
4bf0: 20 61 6e 79 20 66 72 65 65 28 29 20 6f 70 65 72 any free() oper
4c00: 61 74 69 6f 6e 73 2e 20 20 53 69 6e 63 65 20 73 ations. Since s
4c10: 63 68 65 6d 61 20 54 61 62 6c 65 73 20 64 6f 20 chema Tables do
4c20: 6e 6f 74 20 75 73 65 0a 20 20 2a 2a 20 6c 6f 6f not use. ** loo
4c30: 6b 61 73 69 64 65 2c 20 74 68 69 73 20 6e 75 6d kaside, this num
4c40: 62 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 ber should not c
4c50: 68 61 6e 67 65 2e 20 2a 2f 0a 20 20 54 45 53 54 hange. */. TEST
4c60: 4f 4e 4c 59 28 20 6e 4c 6f 6f 6b 61 73 69 64 65 ONLY( nLookaside
4c70: 20 3d 20 28 64 62 20 26 26 20 28 70 54 61 62 6c = (db && (pTabl
4c80: 65 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 e->tabFlags & TF
4c90: 5f 45 70 68 65 6d 65 72 61 6c 29 3d 3d 30 29 20 _Ephemeral)==0)
4ca0: 3f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ?.
4cb0: 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6c db->l
4cc0: 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 20 3a 20 ookaside.nOut :
4cd0: 30 20 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 0 );.. /* Delet
4ce0: 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 e all indices as
4cf0: 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
4d00: 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 is table. */. f
4d10: 6f 72 28 70 49 6e 64 65 78 20 3d 20 70 54 61 62 or(pIndex = pTab
4d20: 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 le->pIndex; pInd
4d30: 65 78 3b 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 ex; pIndex=pNext
4d40: 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 ){. pNext = p
4d50: 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 Index->pNext;.
4d60: 20 20 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 assert( pIndex
4d70: 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c ->pSchema==pTabl
4d80: 65 2d 3e 70 53 63 68 65 6d 61 0a 20 20 20 20 20 e->pSchema.
4d90: 20 20 20 20 7c 7c 20 28 49 73 56 69 72 74 75 61 || (IsVirtua
4da0: 6c 28 70 54 61 62 6c 65 29 20 26 26 20 70 49 6e l(pTable) && pIn
4db0: 64 65 78 2d 3e 69 64 78 54 79 70 65 21 3d 53 51 dex->idxType!=SQ
4dc0: 4c 49 54 45 5f 49 44 58 54 59 50 45 5f 41 50 50 LITE_IDXTYPE_APP
4dd0: 44 45 46 29 20 29 3b 0a 20 20 20 20 69 66 28 20 DEF) );. if(
4de0: 28 64 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 70 6e (db==0 || db->pn
4df0: 42 79 74 65 73 46 72 65 65 64 3d 3d 30 29 20 26 BytesFreed==0) &
4e00: 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 & !IsVirtual(pTa
4e10: 62 6c 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 ble) ){. ch
4e20: 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 49 6e 64 ar *zName = pInd
4e30: 65 78 2d 3e 7a 4e 61 6d 65 3b 20 0a 20 20 20 20 ex->zName; .
4e40: 20 20 54 45 53 54 4f 4e 4c 59 20 28 20 49 6e 64 TESTONLY ( Ind
4e50: 65 78 20 2a 70 4f 6c 64 20 3d 20 29 20 73 71 6c ex *pOld = ) sql
4e60: 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 0a ite3HashInsert(.
4e70: 20 20 20 20 20 20 20 20 20 26 70 49 6e 64 65 78 &pIndex
4e80: 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 ->pSchema->idxHa
4e90: 73 68 2c 20 7a 4e 61 6d 65 2c 20 30 0a 20 20 20 sh, zName, 0.
4ea0: 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 );. asse
4eb0: 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c rt( db==0 || sql
4ec0: 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 ite3SchemaMutexH
4ed0: 65 6c 64 28 64 62 2c 20 30 2c 20 70 49 6e 64 65 eld(db, 0, pInde
4ee0: 78 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 x->pSchema) );.
4ef0: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 6c assert( pOl
4f00: 64 3d 3d 70 49 6e 64 65 78 20 7c 7c 20 70 4f 6c d==pIndex || pOl
4f10: 64 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 d==0 );. }.
4f20: 20 20 66 72 65 65 49 6e 64 65 78 28 64 62 2c 20 freeIndex(db,
4f30: 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 pIndex);. }..
4f40: 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 66 6f /* Delete any fo
4f50: 72 65 69 67 6e 20 6b 65 79 73 20 61 74 74 61 63 reign keys attac
4f60: 68 65 64 20 74 6f 20 74 68 69 73 20 74 61 62 6c hed to this tabl
4f70: 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46 e. */. sqlite3F
4f80: 6b 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 kDelete(db, pTab
4f90: 6c 65 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 le);.. /* Delet
4fa0: 65 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 e the Table stru
4fb0: 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 20 20 cture itself..
4fc0: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 */. sqlite3Dele
4fd0: 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 64 62 teColumnNames(db
4fe0: 2c 20 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c , pTable);. sql
4ff0: 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
5000: 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 Table->zName);.
5010: 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
5020: 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 b, pTable->zColA
5030: 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 ff);. sqlite3Se
5040: 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 lectDelete(db, p
5050: 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b Table->pSelect);
5060: 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 . sqlite3ExprLi
5070: 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 stDelete(db, pTa
5080: 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b 0a 23 69 ble->pCheck);.#i
5090: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
50a0: 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
50b0: 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 sqlite3VtabClea
50c0: 72 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 23 r(db, pTable);.#
50d0: 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 44 endif. sqlite3D
50e0: 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 bFree(db, pTable
50f0: 29 3b 0a 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 );.. /* Verify
5100: 74 68 61 74 20 6e 6f 20 6c 6f 6f 6b 61 73 69 64 that no lookasid
5110: 65 20 6d 65 6d 6f 72 79 20 77 61 73 20 75 73 65 e memory was use
5120: 64 20 62 79 20 73 63 68 65 6d 61 20 74 61 62 6c d by schema tabl
5130: 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 es */. assert(
5140: 6e 4c 6f 6f 6b 61 73 69 64 65 3d 3d 30 20 7c 7c nLookaside==0 ||
5150: 20 6e 4c 6f 6f 6b 61 73 69 64 65 3d 3d 64 62 2d nLookaside==db-
5160: 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 20 >lookaside.nOut
5170: 29 3b 0a 7d 0a 76 6f 69 64 20 73 71 6c 69 74 65 );.}.void sqlite
5180: 33 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71 6c 3DeleteTable(sql
5190: 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 ite3 *db, Table
51a0: 2a 70 54 61 62 6c 65 29 7b 0a 20 20 2f 2a 20 44 *pTable){. /* D
51b0: 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 o not delete the
51c0: 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 74 68 65 table until the
51d0: 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
51e0: 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e 20 2a reaches zero. *
51f0: 2f 0a 20 20 69 66 28 20 21 70 54 61 62 6c 65 20 /. if( !pTable
5200: 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
5210: 28 28 21 64 62 20 7c 7c 20 64 62 2d 3e 70 6e 42 ((!db || db->pnB
5220: 79 74 65 73 46 72 65 65 64 3d 3d 30 29 20 26 26 ytesFreed==0) &&
5230: 20 28 2d 2d 70 54 61 62 6c 65 2d 3e 6e 54 61 62 (--pTable->nTab
5240: 52 65 66 29 3e 30 29 20 29 20 72 65 74 75 72 6e Ref)>0) ) return
5250: 3b 0a 20 20 64 65 6c 65 74 65 54 61 62 6c 65 28 ;. deleteTable(
5260: 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 7d 0a 0a db, pTable);.}..
5270: 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 ./*.** Unlink th
5280: 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 66 72 e given table fr
5290: 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c om the hash tabl
52a0: 65 73 20 61 6e 64 20 74 68 65 20 64 65 6c 65 74 es and the delet
52b0: 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 73 e the.** table s
52c0: 74 72 75 63 74 75 72 65 20 77 69 74 68 20 61 6c tructure with al
52d0: 6c 20 69 74 73 20 69 6e 64 69 63 65 73 20 61 6e l its indices an
52e0: 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a d foreign keys..
52f0: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 55 */.void sqlite3U
5300: 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 nlinkAndDeleteTa
5310: 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ble(sqlite3 *db,
5320: 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 int iDb, const
5330: 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 29 7b char *zTabName){
5340: 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 44 . Table *p;. D
5350: 62 20 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72 b *pDb;.. asser
5360: 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 t( db!=0 );. as
5370: 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 sert( iDb>=0 &&
5380: 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 iDb<db->nDb );.
5390: 20 61 73 73 65 72 74 28 20 7a 54 61 62 4e 61 6d assert( zTabNam
53a0: 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 e );. assert( s
53b0: 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 qlite3SchemaMute
53c0: 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 xHeld(db, iDb, 0
53d0: 29 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 ) );. testcase(
53e0: 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 3d 3d 30 20 zTabName[0]==0
53f0: 29 3b 20 20 2f 2a 20 5a 65 72 6f 2d 6c 65 6e 67 ); /* Zero-leng
5400: 74 68 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 61 th table names a
5410: 72 65 20 61 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 re allowed */.
5420: 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 pDb = &db->aDb[i
5430: 44 62 5d 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 Db];. p = sqlit
5440: 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 44 e3HashInsert(&pD
5450: 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 b->pSchema->tblH
5460: 61 73 68 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 30 ash, zTabName, 0
5470: 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 );. sqlite3Dele
5480: 74 65 54 61 62 6c 65 28 64 62 2c 20 70 29 3b 0a teTable(db, p);.
5490: 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 db->flags |= S
54a0: 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e QLITE_InternChan
54b0: 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 ges;.}../*.** Gi
54c0: 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74 ven a token, ret
54d0: 75 72 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 urn a string tha
54e0: 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 t consists of th
54f0: 65 20 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a e text of that.*
5500: 2a 20 74 6f 6b 65 6e 2e 20 20 53 70 61 63 65 20 * token. Space
5510: 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 to hold the retu
5520: 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 rned string.** i
5530: 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
5540: 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 sqliteMalloc() a
5550: 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 nd must be freed
5560: 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a by the calling.
5570: 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a ** function..**.
5580: 2a 2a 20 41 6e 79 20 71 75 6f 74 61 74 69 6f 6e ** Any quotation
5590: 20 6d 61 72 6b 73 20 28 65 78 3a 20 20 22 6e 61 marks (ex: "na
55a0: 6d 65 22 2c 20 27 6e 61 6d 65 27 2c 20 5b 6e 61 me", 'name', [na
55b0: 6d 65 5d 2c 20 6f 72 20 60 6e 61 6d 65 60 29 20 me], or `name`)
55c0: 74 68 61 74 0a 2a 2a 20 73 75 72 72 6f 75 6e 64 that.** surround
55d0: 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 the body of the
55e0: 20 74 6f 6b 65 6e 20 61 72 65 20 72 65 6d 6f 76 token are remov
55f0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 ed..**.** Tokens
5600: 20 61 72 65 20 6f 66 74 65 6e 20 6a 75 73 74 20 are often just
5610: 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f 20 74 68 pointers into th
5620: 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 e original SQL t
5630: 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a 20 61 72 ext and so.** ar
5640: 65 20 6e 6f 74 20 5c 30 30 30 20 74 65 72 6d 69 e not \000 termi
5650: 6e 61 74 65 64 20 61 6e 64 20 61 72 65 20 6e 6f nated and are no
5660: 74 20 70 65 72 73 69 73 74 65 6e 74 2e 20 20 54 t persistent. T
5670: 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 he returned stri
5680: 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30 20 74 65 ng.** is \000 te
5690: 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 69 73 20 rminated and is
56a0: 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 63 persistent..*/.c
56b0: 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 har *sqlite3Name
56c0: 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 FromToken(sqlite
56d0: 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 3 *db, Token *pN
56e0: 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e ame){. char *zN
56f0: 61 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 ame;. if( pName
5700: 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 ){. zName =
5710: 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 sqlite3DbStrNDup
5720: 28 64 62 2c 20 28 63 68 61 72 2a 29 70 4e 61 6d (db, (char*)pNam
5730: 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b e->z, pName->n);
5740: 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75 . sqlite3Dequ
5750: 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 7d 65 ote(zName);. }e
5760: 6c 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d lse{. zName =
5770: 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
5780: 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a zName;.}../*.**
5790: 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 Open the sqlite
57a0: 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 73 74 _master table st
57b0: 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 ored in database
57c0: 20 6e 75 6d 62 65 72 20 69 44 62 20 66 6f 72 0a number iDb for.
57d0: 2a 2a 20 77 72 69 74 69 6e 67 2e 20 54 68 65 20 ** writing. The
57e0: 74 61 62 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 table is opened
57f0: 75 73 69 6e 67 20 63 75 72 73 6f 72 20 30 2e 0a using cursor 0..
5800: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 4f */.void sqlite3O
5810: 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50 penMasterTable(P
5820: 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69 44 62 arse *p, int iDb
5830: 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 ){. Vdbe *v = s
5840: 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29 qlite3GetVdbe(p)
5850: 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 ;. sqlite3Table
5860: 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41 53 Lock(p, iDb, MAS
5870: 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 4d 41 53 TER_ROOT, 1, MAS
5880: 54 45 52 5f 4e 41 4d 45 29 3b 0a 20 20 73 71 6c TER_NAME);. sql
5890: 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e ite3VdbeAddOp4In
58a0: 74 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 t(v, OP_OpenWrit
58b0: 65 2c 20 30 2c 20 4d 41 53 54 45 52 5f 52 4f 4f e, 0, MASTER_ROO
58c0: 54 2c 20 69 44 62 2c 20 35 29 3b 0a 20 20 69 66 T, iDb, 5);. if
58d0: 28 20 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b 0a ( p->nTab==0 ){.
58e0: 20 20 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31 3b p->nTab = 1;
58f0: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 . }.}../*.** Pa
5900: 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f rameter zName po
5910: 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74 65 ints to a nul-te
5920: 72 6d 69 6e 61 74 65 64 20 62 75 66 66 65 72 20 rminated buffer
5930: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6e containing the n
5940: 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74 61 ame.** of a data
5950: 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 base ("main", "t
5960: 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 emp" or the name
5970: 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 of an attached
5980: 64 62 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e db). This.** fun
5990: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
59a0: 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e e index of the n
59b0: 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 69 6e amed database in
59c0: 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a 2a db->aDb[], or.*
59d0: 2a 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65 * -1 if the name
59e0: 64 20 64 62 20 63 61 6e 6e 6f 74 20 62 65 20 66 d db cannot be f
59f0: 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ound..*/.int sql
5a00: 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 ite3FindDbName(s
5a10: 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
5a20: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a t char *zName){.
5a30: 20 20 69 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20 int i = -1;
5a40: 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
5a50: 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 66 e number */. if
5a60: 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 44 ( zName ){. D
5a70: 62 20 2a 70 44 62 3b 0a 20 20 20 20 66 6f 72 28 b *pDb;. for(
5a80: 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 29 2c 20 70 i=(db->nDb-1), p
5a90: 44 62 3d 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 20 Db=&db->aDb[i];
5aa0: 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 44 62 2d 2d i>=0; i--, pDb--
5ab0: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d ){. if( 0==
5ac0: 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 sqlite3_stricmp(
5ad0: 70 44 62 2d 3e 7a 44 62 53 4e 61 6d 65 2c 20 7a pDb->zDbSName, z
5ae0: 4e 61 6d 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 Name) ) break;.
5af0: 20 20 20 20 20 2f 2a 20 22 6d 61 69 6e 22 20 69 /* "main" i
5b00: 73 20 61 6c 77 61 79 73 20 61 6e 20 61 63 63 65 s always an acce
5b10: 70 74 61 62 6c 65 20 61 6c 69 61 73 20 66 6f 72 ptable alias for
5b20: 20 74 68 65 20 70 72 69 6d 61 72 79 20 64 61 74 the primary dat
5b30: 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 65 abase. ** e
5b40: 76 65 6e 20 69 66 20 69 74 20 68 61 73 20 62 65 ven if it has be
5b50: 65 6e 20 72 65 6e 61 6d 65 64 20 75 73 69 6e 67 en renamed using
5b60: 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 SQLITE_DBCONFIG
5b70: 5f 4d 41 49 4e 44 42 4e 41 4d 45 2e 20 2a 2f 0a _MAINDBNAME. */.
5b80: 20 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20 26 if( i==0 &
5b90: 26 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72 & 0==sqlite3_str
5ba0: 69 63 6d 70 28 22 6d 61 69 6e 22 2c 20 7a 4e 61 icmp("main", zNa
5bb0: 6d 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 me) ) break;.
5bc0: 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
5bd0: 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 i;.}../*.** The
5be0: 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 63 6f 6e token *pName con
5bf0: 74 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f tains the name o
5c00: 66 20 61 20 64 61 74 61 62 61 73 65 20 28 65 69 f a database (ei
5c10: 74 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 0a 2a ther "main" or.*
5c20: 2a 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65 20 * "temp" or the
5c30: 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61 63 name of an attac
5c40: 68 65 64 20 64 62 29 2e 20 54 68 69 73 20 72 6f hed db). This ro
5c50: 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 utine returns th
5c60: 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 e.** index of th
5c70: 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 e named database
5c80: 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f in db->aDb[], o
5c90: 72 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65 r -1 if the name
5ca0: 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f d db .** does no
5cb0: 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 69 6e 74 20 t exist..*/.int
5cc0: 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 sqlite3FindDb(sq
5cd0: 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e lite3 *db, Token
5ce0: 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 *pName){. int
5cf0: 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
5d00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5d10: 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d /* Database num
5d20: 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ber */. char *z
5d30: 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 Name;
5d40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5d50: 20 4e 61 6d 65 20 77 65 20 61 72 65 20 73 65 61 Name we are sea
5d60: 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 rching for */.
5d70: 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e zName = sqlite3N
5d80: 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
5d90: 20 70 4e 61 6d 65 29 3b 0a 20 20 69 20 3d 20 73 pName);. i = s
5da0: 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 qlite3FindDbName
5db0: 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 (db, zName);. s
5dc0: 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
5dd0: 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 zName);. retur
5de0: 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74 n i;.}../* The t
5df0: 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6f 72 20 able or view or
5e00: 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73 20 trigger name is
5e10: 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 72 passed to this r
5e20: 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e outine via token
5e30: 73 0a 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 s.** pName1 and
5e40: 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74 pName2. If the t
5e50: 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75 able name was fu
5e60: 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66 lly qualified, f
5e70: 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a or example:.**.*
5e80: 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 * CREATE TABLE x
5e90: 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a xx.yyy (...);.**
5ea0: 20 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 .** Then pName1
5eb0: 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22 is set to "xxx"
5ec0: 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79 and pName2 "yyy
5ed0: 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 ". On the other
5ee0: 68 61 6e 64 20 69 66 0a 2a 2a 20 74 68 65 20 74 hand if.** the t
5ef0: 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 able name is not
5f00: 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 fully qualified
5f10: 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 , i.e.:.**.** CR
5f20: 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e EATE TABLE yyy(.
5f30: 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 ..);.**.** Then
5f40: 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f pName1 is set to
5f50: 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 "yyy" and pName
5f60: 32 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 2 is ""..**.** T
5f70: 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 his routine sets
5f80: 20 74 68 65 20 2a 70 70 55 6e 71 75 61 6c 20 70 the *ppUnqual p
5f90: 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 ointer to point
5fa0: 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e at the token (pN
5fb0: 61 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65 ame1 or.** pName
5fc0: 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 2) that stores t
5fd0: 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 he unqualified t
5fe0: 61 62 6c 65 20 6e 61 6d 65 2e 20 20 54 68 65 20 able name. The
5ff0: 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 index of the.**
6000: 64 61 74 61 62 61 73 65 20 22 78 78 78 22 20 69 database "xxx" i
6010: 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 69 s returned..*/.i
6020: 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 nt sqlite3TwoPar
6030: 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a tName(. Parse *
6040: 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 pParse, /*
6050: 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 Parsing and code
6060: 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 generating cont
6070: 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a ext */. Token *
6080: 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 pName1, /*
6090: 54 68 65 20 22 78 78 78 22 20 69 6e 20 74 68 65 The "xxx" in the
60a0: 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 name "xxx.yyy"
60b0: 6f 72 20 22 78 78 78 22 20 2a 2f 0a 20 20 54 6f or "xxx" */. To
60c0: 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 ken *pName2,
60d0: 20 20 2f 2a 20 54 68 65 20 22 79 79 79 22 20 69 /* The "yyy" i
60e0: 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 2e n the name "xxx.
60f0: 79 79 79 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 yyy" */. Token
6100: 2a 2a 70 55 6e 71 75 61 6c 20 20 20 20 20 2f 2a **pUnqual /*
6110: 20 57 72 69 74 65 20 74 68 65 20 75 6e 71 75 61 Write the unqua
6120: 6c 69 66 69 65 64 20 6f 62 6a 65 63 74 20 6e 61 lified object na
6130: 6d 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 me here */.){.
6140: 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
6150: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
6160: 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 atabase holding
6170: 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 the object */.
6180: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
6190: 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 arse->db;.. ass
61a0: 65 72 74 28 20 70 4e 61 6d 65 32 21 3d 30 20 29 ert( pName2!=0 )
61b0: 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e ;. if( pName2->
61c0: 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 64 n>0 ){. if( d
61d0: 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20 7b b->init.busy ) {
61e0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
61f0: 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
6200: 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
6210: 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ");. return
6220: 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a -1;. }. *
6230: 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 pUnqual = pName2
6240: 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 ;. iDb = sqli
6250: 74 65 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e te3FindDb(db, pN
6260: 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 ame1);. if( i
6270: 44 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 Db<0 ){. sq
6280: 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
6290: 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 arse, "unknown d
62a0: 61 74 61 62 61 73 65 20 25 54 22 2c 20 70 4e 61 atabase %T", pNa
62b0: 6d 65 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 me1);. retu
62c0: 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d rn -1;. }. }
62d0: 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
62e0: 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d ( db->init.iDb==
62f0: 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 0 || db->init.bu
6300: 73 79 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 sy || (db->flags
6310: 20 26 20 53 51 4c 49 54 45 5f 56 61 63 75 75 6d & SQLITE_Vacuum
6320: 29 21 3d 30 29 3b 0a 20 20 20 20 69 44 62 20 3d )!=0);. iDb =
6330: 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20 db->init.iDb;.
6340: 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e *pUnqual = pN
6350: 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ame1;. }. retu
6360: 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn iDb;.}../*.**
6370: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
6380: 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 used to check i
6390: 66 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 f the UTF-8 stri
63a0: 6e 67 20 7a 4e 61 6d 65 20 69 73 20 61 20 6c 65 ng zName is a le
63b0: 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 66 69 gal.** unqualifi
63c0: 65 64 20 6e 61 6d 65 20 66 6f 72 20 61 20 6e 65 ed name for a ne
63d0: 77 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 w schema object
63e0: 28 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 76 (table, index, v
63f0: 69 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67 67 65 iew or.** trigge
6400: 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 61 72 r). All names ar
6410: 65 20 6c 65 67 61 6c 20 65 78 63 65 70 74 20 74 e legal except t
6420: 68 6f 73 65 20 74 68 61 74 20 62 65 67 69 6e 20 hose that begin
6430: 77 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 0a with the string.
6440: 2a 2a 20 22 73 71 6c 69 74 65 5f 22 20 28 69 6e ** "sqlite_" (in
6450: 20 75 70 70 65 72 2c 20 6c 6f 77 65 72 20 6f 72 upper, lower or
6460: 20 6d 69 78 65 64 20 63 61 73 65 29 2e 20 54 68 mixed case). Th
6470: 69 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 is portion of th
6480: 65 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a 20 69 e namespace.** i
6490: 73 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 s reserved for i
64a0: 6e 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f 0a nternal use..*/.
64b0: 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b int sqlite3Check
64c0: 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 ObjectName(Parse
64d0: 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 *pParse, const
64e0: 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 char *zName){.
64f0: 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64 62 2d if( !pParse->db-
6500: 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70 50 >init.busy && pP
6510: 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 arse->nested==0
6520: 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28 70 . && (p
6530: 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 Parse->db->flags
6540: 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 & SQLITE_WriteS
6550: 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 chema)==0.
6560: 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 && 0==sqlite
6570: 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 3StrNICmp(zName,
6580: 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 "sqlite_", 7) )
6590: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
65a0: 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f orMsg(pParse, "o
65b0: 62 6a 65 63 74 20 6e 61 6d 65 20 72 65 73 65 72 bject name reser
65c0: 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c ved for internal
65d0: 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 use: %s", zName
65e0: 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
65f0: 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
6600: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
6610: 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 OK;.}../*.** Ret
6620: 75 72 6e 20 74 68 65 20 50 52 49 4d 41 52 59 20 urn the PRIMARY
6630: 4b 45 59 20 69 6e 64 65 78 20 6f 66 20 61 20 74 KEY index of a t
6640: 61 62 6c 65 0a 2a 2f 0a 49 6e 64 65 78 20 2a 73 able.*/.Index *s
6650: 71 6c 69 74 65 33 50 72 69 6d 61 72 79 4b 65 79 qlite3PrimaryKey
6660: 49 6e 64 65 78 28 54 61 62 6c 65 20 2a 70 54 61 Index(Table *pTa
6670: 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 3b 0a b){. Index *p;.
6680: 20 20 66 6f 72 28 70 3d 70 54 61 62 2d 3e 70 49 for(p=pTab->pI
6690: 6e 64 65 78 3b 20 70 20 26 26 20 21 49 73 50 72 ndex; p && !IsPr
66a0: 69 6d 61 72 79 4b 65 79 49 6e 64 65 78 28 70 29 imaryKeyIndex(p)
66b0: 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 7d 0a ; p=p->pNext){}.
66c0: 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
66d0: 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
66e0: 63 6f 6c 75 6d 6e 20 6f 66 20 69 6e 64 65 78 20 column of index
66f0: 70 49 64 78 20 74 68 61 74 20 63 6f 72 72 65 73 pIdx that corres
6700: 70 6f 6e 64 73 20 74 6f 20 74 61 62 6c 65 0a 2a ponds to table.*
6710: 2a 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 2e 20 20 * column iCol.
6720: 52 65 74 75 72 6e 20 2d 31 20 69 66 20 6e 6f 74 Return -1 if not
6730: 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 31 36 20 73 found..*/.i16 s
6740: 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 4f 66 49 6e qlite3ColumnOfIn
6750: 64 65 78 28 49 6e 64 65 78 20 2a 70 49 64 78 2c dex(Index *pIdx,
6760: 20 69 31 36 20 69 43 6f 6c 29 7b 0a 20 20 69 6e i16 iCol){. in
6770: 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
6780: 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b i<pIdx->nColumn;
6790: 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 i++){. if( i
67a0: 43 6f 6c 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c Col==pIdx->aiCol
67b0: 75 6d 6e 5b 69 5d 20 29 20 72 65 74 75 72 6e 20 umn[i] ) return
67c0: 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 i;. }. return
67d0: 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 -1;.}../*.** Beg
67e0: 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 in constructing
67f0: 61 20 6e 65 77 20 74 61 62 6c 65 20 72 65 70 72 a new table repr
6800: 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d 65 esentation in me
6810: 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 0a 2a mory. This is.*
6820: 2a 20 74 68 65 20 66 69 72 73 74 20 6f 66 20 73 * the first of s
6830: 65 76 65 72 61 6c 20 61 63 74 69 6f 6e 20 72 6f everal action ro
6840: 75 74 69 6e 65 73 20 74 68 61 74 20 67 65 74 20 utines that get
6850: 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e called in respon
6860: 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41 54 se.** to a CREAT
6870: 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
6880: 74 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 t. In particula
6890: 72 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 r, this routine
68a0: 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 74 is called.** aft
68b0: 65 72 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e 73 er seeing tokens
68c0: 20 22 43 52 45 41 54 45 22 20 61 6e 64 20 22 54 "CREATE" and "T
68d0: 41 42 4c 45 22 20 61 6e 64 20 74 68 65 20 74 61 ABLE" and the ta
68e0: 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 69 73 ble name. The is
68f0: 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73 20 Temp.** flag is
6900: 74 72 75 65 20 69 66 20 74 68 65 20 74 61 62 6c true if the tabl
6910: 65 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 e should be stor
6920: 65 64 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 ed in the auxili
6930: 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ary database.**
6940: 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 file instead of
6950: 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 in the main data
6960: 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 base file. This
6970: 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 65 is normally the
6980: 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74 68 case.** when th
6990: 65 20 22 54 45 4d 50 22 20 6f 72 20 22 54 45 4d e "TEMP" or "TEM
69a0: 50 4f 52 41 52 59 22 20 6b 65 79 77 6f 72 64 20 PORARY" keyword
69b0: 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65 65 occurs in betwee
69c0: 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61 6e 64 20 n.** CREATE and
69d0: 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 TABLE..**.** The
69e0: 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72 new table recor
69f0: 64 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 d is initialized
6a00: 20 61 6e 64 20 70 75 74 20 69 6e 20 70 50 61 72 and put in pPar
6a10: 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a 2a se->pNewTable..*
6a20: 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20 74 68 65 * As more of the
6a30: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
6a40: 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 atement is parse
6a50: 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 63 d, additional ac
6a60: 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
6a70: 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 will be called
6a80: 74 6f 20 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f to add more info
6a90: 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 rmation to this
6aa0: 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 68 record..** At th
6ab0: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 e end of the CRE
6ac0: 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
6ad0: 65 6e 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33 ent, the sqlite3
6ae0: 45 6e 64 54 61 62 6c 65 28 29 20 72 6f 75 74 69 EndTable() routi
6af0: 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 ne.** is called
6b00: 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 to complete the
6b10: 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 construction of
6b20: 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 the new table re
6b30: 63 6f 72 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 cord..*/.void sq
6b40: 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 lite3StartTable(
6b50: 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
6b60: 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f , /* Parser co
6b70: 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e ntext */. Token
6b80: 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46 *pName1, /* F
6b90: 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 irst part of the
6ba0: 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
6bb0: 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 le or view */.
6bc0: 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 Token *pName2,
6bd0: 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20 /* Second part
6be0: 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 of the name of t
6bf0: 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 he table or view
6c00: 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 */. int isTemp
6c10: 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 , /* True i
6c20: 66 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 f this is a TEMP
6c30: 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
6c40: 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f 2a 20 isView, /*
6c50: 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 True if this is
6c60: 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20 a VIEW */. int
6c70: 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20 isVirtual, /*
6c80: 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 True if this is
6c90: 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c 65 20 a VIRTUAL table
6ca0: 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 */. int noErr
6cb0: 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 /* Do noth
6cc0: 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61 6c 72 ing if table alr
6cd0: 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 eady exists */.)
6ce0: 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c {. Table *pTabl
6cf0: 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 e;. char *zName
6d00: 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d = 0; /* The nam
6d10: 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 e of the new tab
6d20: 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 le */. sqlite3
6d30: 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
6d40: 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 ;. Vdbe *v;. i
6d50: 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
6d60: 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 /* Database numb
6d70: 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 er to create the
6d80: 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 table in */. T
6d90: 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 oken *pName;
6da0: 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e /* Unqualified n
6db0: 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
6dc0: 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20 to create */..
6dd0: 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 if( db->init.bu
6de0: 73 79 20 26 26 20 64 62 2d 3e 69 6e 69 74 2e 6e sy && db->init.n
6df0: 65 77 54 6e 75 6d 3d 3d 31 20 29 7b 0a 20 20 20 ewTnum==1 ){.
6e00: 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 /* Special case
6e10: 3a 20 20 50 61 72 73 69 6e 67 20 74 68 65 20 73 : Parsing the s
6e20: 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 6f 72 20 qlite_master or
6e30: 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 sqlite_temp_mast
6e40: 65 72 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 20 er schema */.
6e50: 20 69 44 62 20 3d 20 64 62 2d 3e 69 6e 69 74 2e iDb = db->init.
6e60: 69 44 62 3b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d iDb;. zName =
6e70: 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
6e80: 28 64 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c (db, SCHEMA_TABL
6e90: 45 28 69 44 62 29 29 3b 0a 20 20 20 20 70 4e 61 E(iDb));. pNa
6ea0: 6d 65 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d me = pName1;. }
6eb0: 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 else{. /* The
6ec0: 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 2a 2f 0a common case */.
6ed0: 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 iDb = sqlite
6ee0: 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 3TwoPartName(pPa
6ef0: 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 rse, pName1, pNa
6f00: 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 me2, &pName);.
6f10: 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 if( iDb<0 ) re
6f20: 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 21 4f turn;. if( !O
6f30: 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 MIT_TEMPDB && is
6f40: 54 65 6d 70 20 26 26 20 70 4e 61 6d 65 32 2d 3e Temp && pName2->
6f50: 6e 3e 30 20 26 26 20 69 44 62 21 3d 31 20 29 7b n>0 && iDb!=1 ){
6f60: 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 63 72 65 . /* If cre
6f70: 61 74 69 6e 67 20 61 20 74 65 6d 70 20 74 61 62 ating a temp tab
6f80: 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20 6d 61 79 le, the name may
6f90: 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65 not be qualifie
6fa0: 64 2e 20 55 6e 6c 65 73 73 20 0a 20 20 20 20 20 d. Unless .
6fb0: 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 ** the database
6fc0: 20 6e 61 6d 65 20 69 73 20 22 74 65 6d 70 22 20 name is "temp"
6fd0: 61 6e 79 77 61 79 2e 20 20 2a 2f 0a 20 20 20 20 anyway. */.
6fe0: 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
6ff0: 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f g(pParse, "tempo
7000: 72 61 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 20 rary table name
7010: 6d 75 73 74 20 62 65 20 75 6e 71 75 61 6c 69 66 must be unqualif
7020: 69 65 64 22 29 3b 0a 20 20 20 20 20 20 72 65 74 ied");. ret
7030: 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 urn;. }. i
7040: 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 f( !OMIT_TEMPDB
7050: 26 26 20 69 73 54 65 6d 70 20 29 20 69 44 62 20 && isTemp ) iDb
7060: 3d 20 31 3b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d = 1;. zName =
7070: 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
7080: 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 Token(db, pName)
7090: 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e ;. }. pParse->
70a0: 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70 4e sNameToken = *pN
70b0: 61 6d 65 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 ame;. if( zName
70c0: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
70d0: 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
70e0: 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 qlite3CheckObjec
70f0: 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e tName(pParse, zN
7100: 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f ame) ){. goto
7110: 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
7120: 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 or;. }. if( db
7130: 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29 20 ->init.iDb==1 )
7140: 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66 6e isTemp = 1;.#ifn
7150: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
7160: 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 AUTHORIZATION.
7170: 61 73 73 65 72 74 28 20 69 73 54 65 6d 70 3d 3d assert( isTemp==
7180: 30 20 7c 7c 20 69 73 54 65 6d 70 3d 3d 31 20 29 0 || isTemp==1 )
7190: 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 56 69 ;. assert( isVi
71a0: 65 77 3d 3d 30 20 7c 7c 20 69 73 56 69 65 77 3d ew==0 || isView=
71b0: 3d 31 20 29 3b 0a 20 20 7b 0a 20 20 20 20 73 74 =1 );. {. st
71c0: 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 43 atic const u8 aC
71d0: 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 ode[] = {.
71e0: 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
71f0: 41 42 4c 45 2c 0a 20 20 20 20 20 20 20 53 51 4c ABLE,. SQL
7200: 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
7210: 54 41 42 4c 45 2c 0a 20 20 20 20 20 20 20 53 51 TABLE,. SQ
7220: 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 LITE_CREATE_VIEW
7230: 2c 0a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f ,. SQLITE_
7240: 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 CREATE_TEMP_VIEW
7250: 0a 20 20 20 20 7d 3b 0a 20 20 20 20 63 68 61 72 . };. char
7260: 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b *zDb = db->aDb[
7270: 69 44 62 5d 2e 7a 44 62 53 4e 61 6d 65 3b 0a 20 iDb].zDbSName;.
7280: 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 if( sqlite3Au
7290: 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 thCheck(pParse,
72a0: 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 SQLITE_INSERT, S
72b0: 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 73 54 65 CHEMA_TABLE(isTe
72c0: 6d 70 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a mp), 0, zDb) ){.
72d0: 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e goto begin
72e0: 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 _table_error;.
72f0: 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 56 }. if( !isV
7300: 69 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74 65 irtual && sqlite
7310: 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 3AuthCheck(pPars
7320: 65 2c 20 28 69 6e 74 29 61 43 6f 64 65 5b 69 73 e, (int)aCode[is
7330: 54 65 6d 70 2b 32 2a 69 73 56 69 65 77 5d 2c 0a Temp+2*isView],.
7340: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7350: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7360: 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 30 2c zName, 0,
7370: 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 zDb) ){. g
7380: 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f oto begin_table_
7390: 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d error;. }. }
73a0: 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 .#endif.. /* Ma
73b0: 6b 65 20 73 75 72 65 20 74 68 65 20 6e 65 77 20 ke sure the new
73c0: 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f 65 73 20 table name does
73d0: 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 not collide with
73e0: 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20 2a an existing. *
73f0: 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 * index or table
7400: 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 61 6d name in the sam
7410: 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 73 73 e database. Iss
7420: 75 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ue an error mess
7430: 61 67 65 20 69 66 0a 20 20 2a 2a 20 69 74 20 64 age if. ** it d
7440: 6f 65 73 2e 20 54 68 65 20 65 78 63 65 70 74 69 oes. The excepti
7450: 6f 6e 20 69 73 20 69 66 20 74 68 65 20 73 74 61 on is if the sta
7460: 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 70 61 72 tement being par
7470: 73 65 64 20 77 61 73 20 70 61 73 73 65 64 0a 20 sed was passed.
7480: 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 ** to an sqlite
7490: 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 3_declare_vtab()
74a0: 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61 74 20 63 call. In that c
74b0: 61 73 65 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c ase only the col
74c0: 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 61 umn names. ** a
74d0: 6e 64 20 74 79 70 65 73 20 77 69 6c 6c 20 62 65 nd types will be
74e0: 20 75 73 65 64 2c 20 73 6f 20 74 68 65 72 65 20 used, so there
74f0: 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 is no need to te
7500: 73 74 20 66 6f 72 20 6e 61 6d 65 73 70 61 63 65 st for namespace
7510: 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69 6f 6e 73 . ** collisions
7520: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 49 4e .. */. if( !IN
7530: 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b _DECLARE_VTAB ){
7540: 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d . char *zDb =
7550: 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 db->aDb[iDb].zD
7560: 62 53 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 bSName;. if(
7570: 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 SQLITE_OK!=sqlit
7580: 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 e3ReadSchema(pPa
7590: 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f rse) ){. go
75a0: 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 to begin_table_e
75b0: 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rror;. }.
75c0: 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 pTable = sqlite3
75d0: 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e FindTable(db, zN
75e0: 61 6d 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 ame, zDb);. i
75f0: 66 28 20 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 f( pTable ){.
7600: 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b if( !noErr ){
7610: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
7620: 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
7630: 20 22 74 61 62 6c 65 20 25 54 20 61 6c 72 65 61 "table %T alrea
7640: 64 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d dy exists", pNam
7650: 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b e);. }else{
7660: 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
7670: 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 !db->init.busy
7680: 7c 7c 20 43 4f 52 52 55 50 54 5f 44 42 20 29 3b || CORRUPT_DB );
7690: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
76a0: 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 CodeVerifySchema
76b0: 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 (pParse, iDb);.
76c0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 }. got
76d0: 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 o begin_table_er
76e0: 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ror;. }. i
76f0: 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e f( sqlite3FindIn
7700: 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a dex(db, zName, z
7710: 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 Db)!=0 ){.
7720: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
7730: 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 pParse, "there i
7740: 73 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 s already an ind
7750: 65 78 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e ex named %s", zN
7760: 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f ame);. goto
7770: 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
7780: 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 or;. }. }..
7790: 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 pTable = sqlite
77a0: 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
77b0: 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 , sizeof(Table))
77c0: 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d ;. if( pTable==
77d0: 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
77e0: 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
77f0: 64 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d d );. pParse-
7800: 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d >rc = SQLITE_NOM
7810: 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 70 50 61 EM_BKPT;. pPa
7820: 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 rse->nErr++;.
7830: 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c goto begin_tabl
7840: 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 e_error;. }. p
7850: 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d 20 7a Table->zName = z
7860: 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c 65 2d 3e Name;. pTable->
7870: 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 70 54 iPKey = -1;. pT
7880: 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 3d 20 able->pSchema =
7890: 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 db->aDb[iDb].pSc
78a0: 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c 65 2d 3e hema;. pTable->
78b0: 6e 54 61 62 52 65 66 20 3d 20 31 3b 0a 20 20 70 nTabRef = 1;. p
78c0: 54 61 62 6c 65 2d 3e 6e 52 6f 77 4c 6f 67 45 73 Table->nRowLogEs
78d0: 74 20 3d 20 32 30 30 3b 20 61 73 73 65 72 74 28 t = 200; assert(
78e0: 20 32 30 30 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 200==sqlite3Log
78f0: 45 73 74 28 31 30 34 38 35 37 36 29 20 29 3b 0a Est(1048576) );.
7900: 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
7910: 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 ->pNewTable==0 )
7920: 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 ;. pParse->pNew
7930: 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b 0a Table = pTable;.
7940: 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 . /* If this is
7950: 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69 74 the magic sqlit
7960: 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 e_sequence table
7970: 20 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e 63 used by autoinc
7980: 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 65 rement,. ** the
7990: 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 n record a point
79a0: 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 er to this table
79b0: 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 in the main dat
79c0: 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 0a abase structure.
79d0: 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e 53 ** so that INS
79e0: 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68 65 ERT can find the
79f0: 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a 20 table easily..
7a00: 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
7a10: 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 TE_OMIT_AUTOINCR
7a20: 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50 61 EMENT. if( !pPa
7a30: 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 73 rse->nested && s
7a40: 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 trcmp(zName, "sq
7a50: 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29 3d lite_sequence")=
7a60: 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
7a70: 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d ( sqlite3SchemaM
7a80: 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 utexHeld(db, iDb
7a90: 2c 20 30 29 20 29 3b 0a 20 20 20 20 70 54 61 62 , 0) );. pTab
7aa0: 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 le->pSchema->pSe
7ab0: 71 54 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 qTab = pTable;.
7ac0: 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
7ad0: 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 Begin generating
7ae0: 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 77 the code that w
7af0: 69 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 20 74 ill insert the t
7b00: 61 62 6c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f able record into
7b10: 0a 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 . ** the SQLITE
7b20: 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 _MASTER table.
7b30: 4e 6f 74 65 20 69 6e 20 70 61 72 74 69 63 75 6c Note in particul
7b40: 61 72 20 74 68 61 74 20 77 65 20 6d 75 73 74 20 ar that we must
7b50: 67 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e go ahead. ** an
7b60: 64 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 d allocate the r
7b70: 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 ecord number for
7b80: 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 the table entry
7b90: 20 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 61 6e now. Before an
7ba0: 79 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b y. ** PRIMARY K
7bb0: 45 59 20 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 EY or UNIQUE key
7bc0: 77 6f 72 64 73 20 61 72 65 20 70 61 72 73 65 64 words are parsed
7bd0: 2e 20 20 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 . Those keyword
7be0: 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a s will cause. *
7bf0: 2a 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 * indices to be
7c00: 63 72 65 61 74 65 64 20 61 6e 64 20 74 68 65 20 created and the
7c10: 74 61 62 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 table record mus
7c20: 74 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 t come before th
7c30: 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e e . ** indices.
7c40: 20 20 48 65 6e 63 65 2c 20 74 68 65 20 72 65 63 Hence, the rec
7c50: 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 ord number for t
7c60: 68 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 he table must be
7c70: 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 allocated. **
7c80: 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 now.. */. if(
7c90: 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 !db->init.busy &
7ca0: 26 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65 & (v = sqlite3Ge
7cb0: 74 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d tVdbe(pParse))!=
7cc0: 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 64 64 0 ){. int add
7cd0: 72 31 3b 0a 20 20 20 20 69 6e 74 20 66 69 6c 65 r1;. int file
7ce0: 46 6f 72 6d 61 74 3b 0a 20 20 20 20 69 6e 74 20 Format;. int
7cf0: 72 65 67 31 2c 20 72 65 67 32 2c 20 72 65 67 33 reg1, reg2, reg3
7d00: 3b 0a 20 20 20 20 2f 2a 20 6e 75 6c 6c 52 6f 77 ;. /* nullRow
7d10: 5b 5d 20 69 73 20 61 6e 20 4f 50 5f 52 65 63 6f [] is an OP_Reco
7d20: 72 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 rd encoding of a
7d30: 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 row containing
7d40: 35 20 4e 55 4c 4c 73 20 2a 2f 0a 20 20 20 20 73 5 NULLs */. s
7d50: 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
7d60: 20 6e 75 6c 6c 52 6f 77 5b 5d 20 3d 20 7b 20 36 nullRow[] = { 6
7d70: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 , 0, 0, 0, 0, 0
7d80: 7d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 };. sqlite3Be
7d90: 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f ginWriteOperatio
7da0: 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 n(pParse, 1, iDb
7db0: 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
7dc0: 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
7dd0: 41 42 4c 45 0a 20 20 20 20 69 66 28 20 69 73 56 ABLE. if( isV
7de0: 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 irtual ){.
7df0: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
7e00: 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0(v, OP_VBegin);
7e10: 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
7e20: 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c /* If the fil
7e30: 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 65 6e 63 e format and enc
7e40: 6f 64 69 6e 67 20 69 6e 20 74 68 65 20 64 61 74 oding in the dat
7e50: 61 62 61 73 65 20 68 61 76 65 20 6e 6f 74 20 62 abase have not b
7e60: 65 65 6e 20 73 65 74 2c 20 0a 20 20 20 20 2a 2a een set, . **
7e70: 20 73 65 74 20 74 68 65 6d 20 6e 6f 77 2e 0a 20 set them now..
7e80: 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 31 20 3d */. reg1 =
7e90: 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 pParse->regRowi
7ea0: 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d d = ++pParse->nM
7eb0: 65 6d 3b 0a 20 20 20 20 72 65 67 32 20 3d 20 70 em;. reg2 = p
7ec0: 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 20 3d Parse->regRoot =
7ed0: 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
7ee0: 0a 20 20 20 20 72 65 67 33 20 3d 20 2b 2b 70 50 . reg3 = ++pP
7ef0: 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
7f00: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
7f10: 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 3(v, OP_ReadCook
7f20: 69 65 2c 20 69 44 62 2c 20 72 65 67 33 2c 20 42 ie, iDb, reg3, B
7f30: 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 TREE_FILE_FORMAT
7f40: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
7f50: 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 beUsesBtree(v, i
7f60: 44 62 29 3b 0a 20 20 20 20 61 64 64 72 31 20 3d Db);. addr1 =
7f70: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
7f80: 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 p1(v, OP_If, reg
7f90: 33 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 3); VdbeCoverage
7fa0: 28 76 29 3b 0a 20 20 20 20 66 69 6c 65 46 6f 72 (v);. fileFor
7fb0: 6d 61 74 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 mat = (db->flags
7fc0: 20 26 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 & SQLITE_Legacy
7fd0: 46 69 6c 65 46 6d 74 29 21 3d 30 20 3f 0a 20 20 FileFmt)!=0 ?.
7fe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7ff0: 31 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 1 : SQLITE_MAX_F
8000: 49 4c 45 5f 46 4f 52 4d 41 54 3b 0a 20 20 20 20 ILE_FORMAT;.
8010: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
8020: 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 3(v, OP_SetCooki
8030: 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 46 49 e, iDb, BTREE_FI
8040: 4c 45 5f 46 4f 52 4d 41 54 2c 20 66 69 6c 65 46 LE_FORMAT, fileF
8050: 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 ormat);. sqli
8060: 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
8070: 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 OP_SetCookie, i
8080: 44 62 2c 20 42 54 52 45 45 5f 54 45 58 54 5f 45 Db, BTREE_TEXT_E
8090: 4e 43 4f 44 49 4e 47 2c 20 45 4e 43 28 64 62 29 NCODING, ENC(db)
80a0: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
80b0: 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 beJumpHere(v, ad
80c0: 64 72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 dr1);.. /* Th
80d0: 69 73 20 6a 75 73 74 20 63 72 65 61 74 65 73 20 is just creates
80e0: 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 a place-holder r
80f0: 65 63 6f 72 64 20 69 6e 20 74 68 65 20 73 71 6c ecord in the sql
8100: 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
8110: 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 63 .. ** The rec
8120: 6f 72 64 20 63 72 65 61 74 65 64 20 64 6f 65 73 ord created does
8130: 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 not contain any
8140: 74 68 69 6e 67 20 79 65 74 2e 20 20 49 74 20 77 thing yet. It w
8150: 69 6c 6c 20 62 65 20 72 65 70 6c 61 63 65 64 0a ill be replaced.
8160: 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 72 65 ** by the re
8170: 61 6c 20 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 al entry in code
8180: 20 67 65 6e 65 72 61 74 65 64 20 61 74 20 73 71 generated at sq
8190: 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 29 2e lite3EndTable().
81a0: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
81b0: 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 he rowid for the
81c0: 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 6c 65 new entry is le
81d0: 66 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 ft in register p
81e0: 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 2e Parse->regRowid.
81f0: 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 6f 74 . ** The root
8200: 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
8210: 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 73 the new table is
8220: 20 6c 65 66 74 20 69 6e 20 72 65 67 20 70 50 61 left in reg pPa
8230: 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 rse->regRoot..
8240: 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 61 ** The rowid a
8250: 6e 64 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d nd root page num
8260: 62 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 6e ber values are n
8270: 65 65 64 65 64 20 62 79 20 74 68 65 20 63 6f 64 eeded by the cod
8280: 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 73 71 e that. ** sq
8290: 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 20 77 69 lite3EndTable wi
82a0: 6c 6c 20 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 ll generate..
82b0: 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 */.#if !defined
82c0: 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 (SQLITE_OMIT_VIE
82d0: 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 W) || !defined(S
82e0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
82f0: 41 4c 54 41 42 4c 45 29 0a 20 20 20 20 69 66 28 ALTABLE). if(
8300: 20 69 73 56 69 65 77 20 7c 7c 20 69 73 56 69 72 isView || isVir
8310: 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 tual ){. sq
8320: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
8330: 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
8340: 2c 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c , reg2);. }el
8350: 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a se.#endif. {.
8360: 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 64 pParse->ad
8370: 64 72 43 72 54 61 62 20 3d 20 73 71 6c 69 74 65 drCrTab = sqlite
8380: 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
8390: 50 5f 43 72 65 61 74 65 54 61 62 6c 65 2c 20 69 P_CreateTable, i
83a0: 44 62 2c 20 72 65 67 32 29 3b 0a 20 20 20 20 7d Db, reg2);. }
83b0: 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e . sqlite3Open
83c0: 4d 61 73 74 65 72 54 61 62 6c 65 28 70 50 61 72 MasterTable(pPar
83d0: 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 se, iDb);. sq
83e0: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
83f0: 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 v, OP_NewRowid,
8400: 30 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73 71 0, reg1);. sq
8410: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
8420: 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 36 2c 20 72 v, OP_Blob, 6, r
8430: 65 67 33 2c 20 30 2c 20 6e 75 6c 6c 52 6f 77 2c eg3, 0, nullRow,
8440: 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 P4_STATIC);.
8450: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
8460: 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c p3(v, OP_Insert,
8470: 20 30 2c 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0, reg3, reg1);
8480: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
8490: 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c ChangeP5(v, OPFL
84a0: 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 AG_APPEND);.
84b0: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
84c0: 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 0(v, OP_Close);.
84d0: 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c }.. /* Normal
84e0: 20 28 6e 6f 6e 2d 65 72 72 6f 72 29 20 72 65 74 (non-error) ret
84f0: 75 72 6e 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e urn. */. return
8500: 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 ;.. /* If an er
8510: 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 65 20 6a ror occurs, we j
8520: 75 6d 70 20 68 65 72 65 20 2a 2f 0a 62 65 67 69 ump here */.begi
8530: 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 n_table_error:.
8540: 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
8550: 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 b, zName);. ret
8560: 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 53 65 74 20 70 urn;.}../* Set p
8570: 72 6f 70 65 72 74 69 65 73 20 6f 66 20 61 20 74 roperties of a t
8580: 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 62 61 73 65 able column base
8590: 64 20 6f 6e 20 74 68 65 20 28 6d 61 67 69 63 61 d on the (magica
85a0: 6c 29 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 l).** name of th
85b0: 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 23 69 66 e column..*/.#if
85c0: 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 48 SQLITE_ENABLE_H
85d0: 49 44 44 45 4e 5f 43 4f 4c 55 4d 4e 53 0a 76 6f IDDEN_COLUMNS.vo
85e0: 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e id sqlite3Column
85f0: 50 72 6f 70 65 72 74 69 65 73 46 72 6f 6d 4e 61 PropertiesFromNa
8600: 6d 65 28 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 me(Table *pTab,
8610: 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 29 7b 0a 20 Column *pCol){.
8620: 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74 72 if( sqlite3_str
8630: 6e 69 63 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d nicmp(pCol->zNam
8640: 65 2c 20 22 5f 5f 68 69 64 64 65 6e 5f 5f 22 2c e, "__hidden__",
8650: 20 31 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 10)==0 ){. p
8660: 43 6f 6c 2d 3e 63 6f 6c 46 6c 61 67 73 20 7c 3d Col->colFlags |=
8670: 20 43 4f 4c 46 4c 41 47 5f 48 49 44 44 45 4e 3b COLFLAG_HIDDEN;
8680: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 61 . }else if( pTa
8690: 62 20 26 26 20 70 43 6f 6c 21 3d 70 54 61 62 2d b && pCol!=pTab-
86a0: 3e 61 43 6f 6c 20 26 26 20 28 70 43 6f 6c 5b 2d >aCol && (pCol[-
86b0: 31 5d 2e 63 6f 6c 46 6c 61 67 73 20 26 20 43 4f 1].colFlags & CO
86c0: 4c 46 4c 41 47 5f 48 49 44 44 45 4e 29 20 29 7b LFLAG_HIDDEN) ){
86d0: 0a 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c . pTab->tabFl
86e0: 61 67 73 20 7c 3d 20 54 46 5f 4f 4f 4f 48 69 64 ags |= TF_OOOHid
86f0: 64 65 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 den;. }.}.#endi
8700: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 f.../*.** Add a
8710: 6e 65 77 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 68 new column to th
8720: 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c e table currentl
8730: 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 y being construc
8740: 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ted..**.** The p
8750: 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 arser calls this
8760: 20 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20 66 6f routine once fo
8770: 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 64 65 r each column de
8780: 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 claration.** in
8790: 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 a CREATE TABLE s
87a0: 74 61 74 65 6d 65 6e 74 2e 20 20 73 71 6c 69 74 tatement. sqlit
87b0: 65 33 53 74 61 72 74 54 61 62 6c 65 28 29 20 67 e3StartTable() g
87c0: 65 74 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 69 ets called.** fi
87d0: 72 73 74 20 74 6f 20 67 65 74 20 74 68 69 6e 67 rst to get thing
87e0: 73 20 67 6f 69 6e 67 2e 20 20 54 68 65 6e 20 74 s going. Then t
87f0: 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
8800: 61 6c 6c 65 64 20 66 6f 72 20 65 61 63 68 0a 2a alled for each.*
8810: 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 76 6f 69 * column..*/.voi
8820: 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 d sqlite3AddColu
8830: 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 mn(Parse *pParse
8840: 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 , Token *pName,
8850: 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 Token *pType){.
8860: 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 Table *p;. int
8870: 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 i;. char *z;.
8880: 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 0a 20 20 char *zType;.
8890: 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 Column *pCol;.
88a0: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
88b0: 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 arse->db;. if(
88c0: 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 (p = pParse->pNe
88d0: 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 wTable)==0 ) ret
88e0: 75 72 6e 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f urn;.#if SQLITE_
88f0: 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 MAX_COLUMN. if(
8900: 20 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e 61 p->nCol+1>db->a
8910: 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
8920: 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 IT_COLUMN] ){.
8930: 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
8940: 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d g(pParse, "too m
8950: 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 any columns on %
8960: 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 s", p->zName);.
8970: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 return;. }.#
8980: 65 6e 64 69 66 0a 20 20 7a 20 3d 20 73 71 6c 69 endif. z = sqli
8990: 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
89a0: 62 2c 20 70 4e 61 6d 65 2d 3e 6e 20 2b 20 70 54 b, pName->n + pT
89b0: 79 70 65 2d 3e 6e 20 2b 20 32 29 3b 0a 20 20 69 ype->n + 2);. i
89c0: 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e f( z==0 ) return
89d0: 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 4e ;. memcpy(z, pN
89e0: 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e ame->z, pName->n
89f0: 29 3b 0a 20 20 7a 5b 70 4e 61 6d 65 2d 3e 6e 5d );. z[pName->n]
8a00: 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 = 0;. sqlite3D
8a10: 65 71 75 6f 74 65 28 7a 29 3b 0a 20 20 66 6f 72 equote(z);. for
8a20: 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b (i=0; i<p->nCol;
8a30: 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 i++){. if( s
8a40: 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 7a qlite3_stricmp(z
8a50: 2c 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 , p->aCol[i].zNa
8a60: 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 me)==0 ){.
8a70: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
8a80: 70 50 61 72 73 65 2c 20 22 64 75 70 6c 69 63 61 pParse, "duplica
8a90: 74 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 te column name:
8aa0: 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73 %s", z);. s
8ab0: 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
8ac0: 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 z);. retur
8ad0: 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 n;. }. }. i
8ae0: 66 28 20 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 f( (p->nCol & 0x
8af0: 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 43 6f 6c 7)==0 ){. Col
8b00: 75 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20 20 20 61 umn *aNew;. a
8b10: 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 New = sqlite3DbR
8b20: 65 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e 61 43 6f ealloc(db,p->aCo
8b30: 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 l,(p->nCol+8)*si
8b40: 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 zeof(p->aCol[0])
8b50: 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d );. if( aNew=
8b60: 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
8b70: 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 te3DbFree(db, z)
8b80: 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
8b90: 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f }. p->aCo
8ba0: 6c 20 3d 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20 l = aNew;. }.
8bb0: 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b pCol = &p->aCol[
8bc0: 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 p->nCol];. mems
8bd0: 65 74 28 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65 et(pCol, 0, size
8be0: 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b of(p->aCol[0]));
8bf0: 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d . pCol->zName =
8c00: 20 7a 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f 6c z;. sqlite3Col
8c10: 75 6d 6e 50 72 6f 70 65 72 74 69 65 73 46 72 6f umnPropertiesFro
8c20: 6d 4e 61 6d 65 28 70 2c 20 70 43 6f 6c 29 3b 0a mName(p, pCol);.
8c30: 20 0a 20 20 69 66 28 20 70 54 79 70 65 2d 3e 6e . if( pType->n
8c40: 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 ==0 ){. /* If
8c50: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 74 79 70 there is no typ
8c60: 65 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f 6c e specified, col
8c70: 75 6d 6e 73 20 68 61 76 65 20 74 68 65 20 64 65 umns have the de
8c80: 66 61 75 6c 74 20 61 66 66 69 6e 69 74 79 0a 20 fault affinity.
8c90: 20 20 20 2a 2a 20 27 42 4c 4f 42 27 2e 20 2a 2f ** 'BLOB'. */
8ca0: 0a 20 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e . pCol->affin
8cb0: 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 ity = SQLITE_AFF
8cc0: 5f 42 4c 4f 42 3b 0a 20 20 20 20 70 43 6f 6c 2d _BLOB;. pCol-
8cd0: 3e 73 7a 45 73 74 20 3d 20 31 3b 0a 20 20 7d 65 >szEst = 1;. }e
8ce0: 6c 73 65 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d lse{. zType =
8cf0: 20 7a 20 2b 20 73 71 6c 69 74 65 33 53 74 72 6c z + sqlite3Strl
8d00: 65 6e 33 30 28 7a 29 20 2b 20 31 3b 0a 20 20 20 en30(z) + 1;.
8d10: 20 6d 65 6d 63 70 79 28 7a 54 79 70 65 2c 20 70 memcpy(zType, p
8d20: 54 79 70 65 2d 3e 7a 2c 20 70 54 79 70 65 2d 3e Type->z, pType->
8d30: 6e 29 3b 0a 20 20 20 20 7a 54 79 70 65 5b 70 54 n);. zType[pT
8d40: 79 70 65 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 20 ype->n] = 0;.
8d50: 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 sqlite3Dequote(
8d60: 7a 54 79 70 65 29 3b 0a 20 20 20 20 70 43 6f 6c zType);. pCol
8d70: 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c ->affinity = sql
8d80: 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 ite3AffinityType
8d90: 28 7a 54 79 70 65 2c 20 26 70 43 6f 6c 2d 3e 73 (zType, &pCol->s
8da0: 7a 45 73 74 29 3b 0a 20 20 20 20 70 43 6f 6c 2d zEst);. pCol-
8db0: 3e 63 6f 6c 46 6c 61 67 73 20 7c 3d 20 43 4f 4c >colFlags |= COL
8dc0: 46 4c 41 47 5f 48 41 53 54 59 50 45 3b 0a 20 20 FLAG_HASTYPE;.
8dd0: 7d 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 20 }. p->nCol++;.
8de0: 20 70 50 61 72 73 65 2d 3e 63 6f 6e 73 74 72 61 pParse->constra
8df0: 69 6e 74 4e 61 6d 65 2e 6e 20 3d 20 30 3b 0a 7d intName.n = 0;.}
8e00: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
8e10: 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 tine is called b
8e20: 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 y the parser whi
8e30: 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 le in the middle
8e40: 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 of.** parsing a
8e50: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
8e60: 61 74 65 6d 65 6e 74 2e 20 20 41 20 22 4e 4f 54 atement. A "NOT
8e70: 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e NULL" constrain
8e80: 74 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 73 65 t has.** been se
8e90: 65 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 en on a column.
8ea0: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
8eb0: 74 73 20 74 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 ts the notNull f
8ec0: 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f lag on.** the co
8ed0: 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 lumn currently u
8ee0: 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
8ef0: 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 n..*/.void sqlit
8f00: 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 e3AddNotNull(Par
8f10: 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
8f20: 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 54 61 62 6c onError){. Tabl
8f30: 65 20 2a 70 3b 0a 20 20 70 20 3d 20 70 50 61 72 e *p;. p = pPar
8f40: 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 se->pNewTable;.
8f50: 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 if( p==0 || NEV
8f60: 45 52 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 ER(p->nCol<1) )
8f70: 72 65 74 75 72 6e 3b 0a 20 20 70 2d 3e 61 43 6f return;. p->aCo
8f80: 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 2e 6e 6f 74 l[p->nCol-1].not
8f90: 4e 75 6c 6c 20 3d 20 28 75 38 29 6f 6e 45 72 72 Null = (u8)onErr
8fa0: 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 or;.}../*.** Sca
8fb0: 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 n the column typ
8fc0: 65 20 6e 61 6d 65 20 7a 54 79 70 65 20 28 6c 65 e name zType (le
8fd0: 6e 67 74 68 20 6e 54 79 70 65 29 20 61 6e 64 20 ngth nType) and
8fe0: 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 73 return the.** as
8ff0: 73 6f 63 69 61 74 65 64 20 61 66 66 69 6e 69 74 sociated affinit
9000: 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 y type..**.** Th
9010: 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 is routine does
9020: 61 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 a case-independe
9030: 6e 74 20 73 65 61 72 63 68 20 6f 66 20 7a 54 79 nt search of zTy
9040: 70 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 73 pe for the .** s
9050: 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 ubstrings in the
9060: 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 following table
9070: 2e 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 . If one of the
9080: 73 75 62 73 74 72 69 6e 67 73 20 69 73 0a 2a 2a substrings is.**
9090: 20 66 6f 75 6e 64 2c 20 74 68 65 20 63 6f 72 72 found, the corr
90a0: 65 73 70 6f 6e 64 69 6e 67 20 61 66 66 69 6e 69 esponding affini
90b0: 74 79 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 ty is returned.
90c0: 49 66 20 7a 54 79 70 65 20 63 6f 6e 74 61 69 6e If zType contain
90d0: 73 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f s.** more than o
90e0: 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 ne of the substr
90f0: 69 6e 67 73 2c 20 65 6e 74 72 69 65 73 20 74 6f ings, entries to
9100: 77 61 72 64 20 74 68 65 20 74 6f 70 20 6f 66 20 ward the top of
9110: 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 74 61 .** the table ta
9120: 6b 65 20 70 72 69 6f 72 69 74 79 2e 20 46 6f 72 ke priority. For
9130: 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 7a 54 79 example, if zTy
9140: 70 65 20 69 73 20 27 42 4c 4f 42 49 4e 54 27 2c pe is 'BLOBINT',
9150: 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f .** SQLITE_AFF_
9160: 49 4e 54 45 47 45 52 20 69 73 20 72 65 74 75 72 INTEGER is retur
9170: 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 73 74 ned..**.** Subst
9180: 72 69 6e 67 20 20 20 20 20 7c 20 41 66 66 69 6e ring | Affin
9190: 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d ity.** ---------
91a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
91b0: 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 27 -------.** 'INT'
91c0: 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 | SQLIT
91d0: 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 0a 2a 2a E_AFF_INTEGER.**
91e0: 20 27 43 48 41 52 27 20 20 20 20 20 20 20 20 7c 'CHAR' |
91f0: 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
9200: 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 20 20 20 20 .** 'CLOB'
9210: 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 | SQLITE_AFF_T
9220: 45 58 54 0a 2a 2a 20 27 54 45 58 54 27 20 20 20 EXT.** 'TEXT'
9230: 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 | SQLITE_AF
9240: 46 5f 54 45 58 54 0a 2a 2a 20 27 42 4c 4f 42 27 F_TEXT.** 'BLOB'
9250: 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 | SQLITE
9260: 5f 41 46 46 5f 42 4c 4f 42 0a 2a 2a 20 27 52 45 _AFF_BLOB.** 'RE
9270: 41 4c 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c AL' | SQL
9280: 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 ITE_AFF_REAL.**
9290: 27 46 4c 4f 41 27 20 20 20 20 20 20 20 20 7c 20 'FLOA' |
92a0: 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a SQLITE_AFF_REAL.
92b0: 2a 2a 20 27 44 4f 55 42 27 20 20 20 20 20 20 20 ** 'DOUB'
92c0: 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 | SQLITE_AFF_RE
92d0: 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 AL.**.** If none
92e0: 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e of the substrin
92f0: 67 73 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 gs in the above
9300: 74 61 62 6c 65 20 61 72 65 20 66 6f 75 6e 64 2c table are found,
9310: 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e .** SQLITE_AFF_N
9320: 55 4d 45 52 49 43 20 69 73 20 72 65 74 75 72 6e UMERIC is return
9330: 65 64 2e 0a 2a 2f 0a 63 68 61 72 20 73 71 6c 69 ed..*/.char sqli
9340: 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 te3AffinityType(
9350: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c const char *zIn,
9360: 20 75 38 20 2a 70 73 7a 45 73 74 29 7b 0a 20 20 u8 *pszEst){.
9370: 75 33 32 20 68 20 3d 20 30 3b 0a 20 20 63 68 61 u32 h = 0;. cha
9380: 72 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 r aff = SQLITE_A
9390: 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 63 6f FF_NUMERIC;. co
93a0: 6e 73 74 20 63 68 61 72 20 2a 7a 43 68 61 72 20 nst char *zChar
93b0: 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
93c0: 7a 49 6e 21 3d 30 20 29 3b 0a 20 20 77 68 69 6c zIn!=0 );. whil
93d0: 65 28 20 7a 49 6e 5b 30 5d 20 29 7b 0a 20 20 20 e( zIn[0] ){.
93e0: 20 68 20 3d 20 28 68 3c 3c 38 29 20 2b 20 73 71 h = (h<<8) + sq
93f0: 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
9400: 72 5b 28 2a 7a 49 6e 29 26 30 78 66 66 5d 3b 0a r[(*zIn)&0xff];.
9410: 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 69 zIn++;. i
9420: 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 f( h==(('c'<<24)
9430: 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27 61 27 3c +('h'<<16)+('a'<
9440: 3c 38 29 2b 27 72 27 29 20 29 7b 20 20 20 20 20 <8)+'r') ){
9450: 20 20 20 20 20 20 20 20 2f 2a 20 43 48 41 52 20 /* CHAR
9460: 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 */. aff = S
9470: 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a QLITE_AFF_TEXT;.
9480: 20 20 20 20 20 20 7a 43 68 61 72 20 3d 20 7a 49 zChar = zI
9490: 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 n;. }else if(
94a0: 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 h==(('c'<<24)+(
94b0: 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 'l'<<16)+('o'<<8
94c0: 29 2b 27 62 27 29 20 29 7b 20 20 20 20 20 20 20 )+'b') ){
94d0: 2f 2a 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 /* CLOB */.
94e0: 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 aff = SQLITE_AF
94f0: 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 F_TEXT;. }els
9500: 65 20 69 66 28 20 68 3d 3d 28 28 27 74 27 3c 3c e if( h==(('t'<<
9510: 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 24)+('e'<<16)+('
9520: 78 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 x'<<8)+'t') ){
9530: 20 20 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f 0a /* TEXT */.
9540: 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 aff = SQLI
9550: 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 TE_AFF_TEXT;.
9560: 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 }else if( h==((
9570: 27 62 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 'b'<<24)+('l'<<1
9580: 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 6)+('o'<<8)+'b')
9590: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 4c 4f /* BLO
95a0: 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 B */. &&
95b0: 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 (aff==SQLITE_AFF
95c0: 5f 4e 55 4d 45 52 49 43 20 7c 7c 20 61 66 66 3d _NUMERIC || aff=
95d0: 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c =SQLITE_AFF_REAL
95e0: 29 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d ) ){. aff =
95f0: 20 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 SQLITE_AFF_BLOB
9600: 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 49 6e 5b ;. if( zIn[
9610: 30 5d 3d 3d 27 28 27 20 29 20 7a 43 68 61 72 20 0]=='(' ) zChar
9620: 3d 20 7a 49 6e 3b 0a 23 69 66 6e 64 65 66 20 53 = zIn;.#ifndef S
9630: 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
9640: 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 7d 65 ING_POINT. }e
9650: 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 72 27 lse if( h==(('r'
9660: 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b <<24)+('e'<<16)+
9670: 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20 20 20 ('a'<<8)+'l')
9680: 20 20 20 20 20 20 20 2f 2a 20 52 45 41 4c 20 2a /* REAL *
9690: 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 /. && aff
96a0: 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d ==SQLITE_AFF_NUM
96b0: 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 ERIC ){. af
96c0: 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 f = SQLITE_AFF_R
96d0: 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 EAL;. }else i
96e0: 66 28 20 68 3d 3d 28 28 27 66 27 3c 3c 32 34 29 f( h==(('f'<<24)
96f0: 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c +('l'<<16)+('o'<
9700: 3c 38 29 2b 27 61 27 29 20 20 20 20 20 20 20 20 <8)+'a')
9710: 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 /* FLOA */.
9720: 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c && aff==SQL
9730: 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 ITE_AFF_NUMERIC
9740: 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 ){. aff = S
9750: 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a QLITE_AFF_REAL;.
9760: 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d }else if( h=
9770: 3d 28 28 27 64 27 3c 3c 32 34 29 2b 28 27 6f 27 =(('d'<<24)+('o'
9780: 3c 3c 31 36 29 2b 28 27 75 27 3c 3c 38 29 2b 27 <<16)+('u'<<8)+'
9790: 62 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 b') /*
97a0: 44 4f 55 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 DOUB */.
97b0: 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 && aff==SQLITE_A
97c0: 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 FF_NUMERIC ){.
97d0: 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
97e0: 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 _AFF_REAL;.#endi
97f0: 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 f. }else if(
9800: 28 68 26 30 78 30 30 46 46 46 46 46 46 29 3d 3d (h&0x00FFFFFF)==
9810: 28 28 27 69 27 3c 3c 31 36 29 2b 28 27 6e 27 3c (('i'<<16)+('n'<
9820: 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20 2f <8)+'t') ){ /
9830: 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 61 * INT */. a
9840: 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f ff = SQLITE_AFF_
9850: 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 62 INTEGER;. b
9860: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
9870: 0a 20 20 2f 2a 20 49 66 20 70 73 7a 45 73 74 20 . /* If pszEst
9880: 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74 6f is not NULL, sto
9890: 72 65 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f re an estimate o
98a0: 66 20 74 68 65 20 66 69 65 6c 64 20 73 69 7a 65 f the field size
98b0: 2e 20 20 54 68 65 0a 20 20 2a 2a 20 65 73 74 69 . The. ** esti
98c0: 6d 61 74 65 20 69 73 20 73 63 61 6c 65 64 20 73 mate is scaled s
98d0: 6f 20 74 68 61 74 20 74 68 65 20 73 69 7a 65 20 o that the size
98e0: 6f 66 20 61 6e 20 69 6e 74 65 67 65 72 20 69 73 of an integer is
98f0: 20 31 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 73 1. */. if( ps
9900: 7a 45 73 74 20 29 7b 0a 20 20 20 20 2a 70 73 7a zEst ){. *psz
9910: 45 73 74 20 3d 20 31 3b 20 20 20 2f 2a 20 64 65 Est = 1; /* de
9920: 66 61 75 6c 74 20 73 69 7a 65 20 69 73 20 61 70 fault size is ap
9930: 70 72 6f 78 20 34 20 62 79 74 65 73 20 2a 2f 0a prox 4 bytes */.
9940: 20 20 20 20 69 66 28 20 61 66 66 3c 53 51 4c 49 if( aff<SQLI
9950: 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 TE_AFF_NUMERIC )
9960: 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 43 68 61 {. if( zCha
9970: 72 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 r ){. whi
9980: 6c 65 28 20 7a 43 68 61 72 5b 30 5d 20 29 7b 0a le( zChar[0] ){.
9990: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
99a0: 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 43 68 lite3Isdigit(zCh
99b0: 61 72 5b 30 5d 29 20 29 7b 0a 20 20 20 20 20 20 ar[0]) ){.
99c0: 20 20 20 20 20 20 69 6e 74 20 76 20 3d 20 30 3b int v = 0;
99d0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
99e0: 69 74 65 33 47 65 74 49 6e 74 33 32 28 7a 43 68 ite3GetInt32(zCh
99f0: 61 72 2c 20 26 76 29 3b 0a 20 20 20 20 20 20 20 ar, &v);.
9a00: 20 20 20 20 20 76 20 3d 20 76 2f 34 20 2b 20 31 v = v/4 + 1
9a10: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
9a20: 28 20 76 3e 32 35 35 20 29 20 76 20 3d 20 32 35 ( v>255 ) v = 25
9a30: 35 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 5;. *
9a40: 70 73 7a 45 73 74 20 3d 20 76 3b 20 2f 2a 20 42 pszEst = v; /* B
9a50: 4c 4f 42 28 6b 29 2c 20 56 41 52 43 48 41 52 28 LOB(k), VARCHAR(
9a60: 6b 29 2c 20 43 48 41 52 28 6b 29 20 2d 3e 20 72 k), CHAR(k) -> r
9a70: 3d 28 6b 2f 34 2b 31 29 20 2a 2f 0a 20 20 20 20 =(k/4+1) */.
9a80: 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9a90: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
9aa0: 20 20 20 20 20 7a 43 68 61 72 2b 2b 3b 0a 20 20 zChar++;.
9ab0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
9ac0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 70 73 lse{. *ps
9ad0: 7a 45 73 74 20 3d 20 35 3b 20 20 20 2f 2a 20 42 zEst = 5; /* B
9ae0: 4c 4f 42 2c 20 54 45 58 54 2c 20 43 4c 4f 42 20 LOB, TEXT, CLOB
9af0: 2d 3e 20 72 3d 35 20 20 28 61 70 70 72 6f 78 20 -> r=5 (approx
9b00: 32 30 20 62 79 74 65 73 29 2a 2f 0a 20 20 20 20 20 bytes)*/.
9b10: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
9b20: 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f return aff;.}../
9b30: 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 *.** The express
9b40: 69 6f 6e 20 69 73 20 74 68 65 20 64 65 66 61 75 ion is the defau
9b50: 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
9b60: 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 most recently a
9b70: 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f dded column.** o
9b80: 66 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 f the table curr
9b90: 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 ently under cons
9ba0: 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 truction..**.**
9bb0: 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 65 78 Default value ex
9bc0: 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 74 20 62 pressions must b
9bd0: 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69 e constant. Rai
9be0: 73 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 se an exception
9bf0: 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f if this.** is no
9c00: 74 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a t the case..**.*
9c10: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
9c20: 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
9c30: 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 parser while in
9c40: 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a the middle of.**
9c50: 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 parsing a CREAT
9c60: 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
9c70: 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 t..*/.void sqlit
9c80: 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 e3AddDefaultValu
9c90: 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
9ca0: 20 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61 6e ExprSpan *pSpan
9cb0: 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 ){. Table *p;.
9cc0: 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 Column *pCol;.
9cd0: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
9ce0: 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 20 3d Parse->db;. p =
9cf0: 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
9d00: 6c 65 3b 0a 20 20 69 66 28 20 70 21 3d 30 20 29 le;. if( p!=0 )
9d10: 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20 26 28 70 {. pCol = &(p
9d20: 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 ->aCol[p->nCol-1
9d30: 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c ]);. if( !sql
9d40: 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
9d50: 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 70 53 70 ntOrFunction(pSp
9d60: 61 6e 2d 3e 70 45 78 70 72 2c 20 64 62 2d 3e 69 an->pExpr, db->i
9d70: 6e 69 74 2e 62 75 73 79 29 20 29 7b 0a 20 20 20 nit.busy) ){.
9d80: 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
9d90: 73 67 28 70 50 61 72 73 65 2c 20 22 64 65 66 61 sg(pParse, "defa
9da0: 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c ult value of col
9db0: 75 6d 6e 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20 umn [%s] is not
9dc0: 63 6f 6e 73 74 61 6e 74 22 2c 0a 20 20 20 20 20 constant",.
9dd0: 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 pCol->zName
9de0: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
9df0: 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 /* A copy of
9e00: 20 70 45 78 70 72 20 69 73 20 75 73 65 64 20 69 pExpr is used i
9e10: 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6f 72 nstead of the or
9e20: 69 67 69 6e 61 6c 2c 20 61 73 20 70 45 78 70 72 iginal, as pExpr
9e30: 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 contains.
9e40: 2a 2a 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 70 ** tokens that p
9e50: 6f 69 6e 74 20 74 6f 20 76 6f 6c 61 74 69 6c 65 oint to volatile
9e60: 20 6d 65 6d 6f 72 79 2e 20 54 68 65 20 27 73 70 memory. The 'sp
9e70: 61 6e 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 an' of the expre
9e80: 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 ssion. ** i
9e90: 73 20 72 65 71 75 69 72 65 64 20 62 79 20 70 72 s required by pr
9ea0: 61 67 6d 61 20 74 61 62 6c 65 5f 69 6e 66 6f 2e agma table_info.
9eb0: 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
9ec0: 45 78 70 72 20 78 3b 0a 20 20 20 20 20 20 73 71 Expr x;. sq
9ed0: 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
9ee0: 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 db, pCol->pDflt)
9ef0: 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 ;. memset(&
9f00: 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 78 29 29 x, 0, sizeof(x))
9f10: 3b 0a 20 20 20 20 20 20 78 2e 6f 70 20 3d 20 54 ;. x.op = T
9f20: 4b 5f 53 50 41 4e 3b 0a 20 20 20 20 20 20 78 2e K_SPAN;. x.
9f30: 75 2e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 u.zToken = sqlit
9f40: 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 e3DbStrNDup(db,
9f50: 28 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e 7a 53 (char*)pSpan->zS
9f60: 74 61 72 74 2c 0a 20 20 20 20 20 20 20 20 20 20 tart,.
9f70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f80: 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 28 (int)(
9f90: 70 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 pSpan->zEnd - pS
9fa0: 70 61 6e 2d 3e 7a 53 74 61 72 74 29 29 3b 0a 20 pan->zStart));.
9fb0: 20 20 20 20 20 78 2e 70 4c 65 66 74 20 3d 20 70 x.pLeft = p
9fc0: 53 70 61 6e 2d 3e 70 45 78 70 72 3b 0a 20 20 20 Span->pExpr;.
9fd0: 20 20 20 78 2e 66 6c 61 67 73 20 3d 20 45 50 5f x.flags = EP_
9fe0: 53 6b 69 70 3b 0a 20 20 20 20 20 20 70 43 6f 6c Skip;. pCol
9ff0: 2d 3e 70 44 66 6c 74 20 3d 20 73 71 6c 69 74 65 ->pDflt = sqlite
a000: 33 45 78 70 72 44 75 70 28 64 62 2c 20 26 78 2c 3ExprDup(db, &x,
a010: 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 EXPRDUP_REDUCE)
a020: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 ;. sqlite3D
a030: 62 46 72 65 65 28 64 62 2c 20 78 2e 75 2e 7a 54 bFree(db, x.u.zT
a040: 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d oken);. }. }
a050: 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 . sqlite3ExprDe
a060: 6c 65 74 65 28 64 62 2c 20 70 53 70 61 6e 2d 3e lete(db, pSpan->
a070: 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pExpr);.}../*.**
a080: 20 42 61 63 6b 77 61 72 64 73 20 43 6f 6d 70 61 Backwards Compa
a090: 74 69 62 69 6c 69 74 79 20 48 61 63 6b 3a 0a 2a tibility Hack:.*
a0a0: 2a 20 0a 2a 2a 20 48 69 73 74 6f 72 69 63 61 6c * .** Historical
a0b0: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
a0c0: 69 74 65 20 61 63 63 65 70 74 65 64 20 73 74 72 ite accepted str
a0d0: 69 6e 67 73 20 61 73 20 63 6f 6c 75 6d 6e 20 6e ings as column n
a0e0: 61 6d 65 73 20 69 6e 0a 2a 2a 20 69 6e 64 65 78 ames in.** index
a0f0: 65 73 20 61 6e 64 20 50 52 49 4d 41 52 59 20 4b es and PRIMARY K
a100: 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 EY constraints a
a110: 6e 64 20 69 6e 20 55 4e 49 51 55 45 20 63 6f 6e nd in UNIQUE con
a120: 73 74 72 61 69 6e 74 73 2e 20 20 45 78 61 6d 70 straints. Examp
a130: 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 le:.**.** CR
a140: 45 41 54 45 20 54 41 42 4c 45 20 78 79 7a 28 61 EATE TABLE xyz(a
a150: 2c 62 2c 63 2c 64 2c 65 2c 50 52 49 4d 41 52 59 ,b,c,d,e,PRIMARY
a160: 20 4b 45 59 28 27 61 27 29 2c 55 4e 49 51 55 45 KEY('a'),UNIQUE
a170: 28 27 62 27 2c 27 63 27 20 43 4f 4c 4c 41 54 45 ('b','c' COLLATE
a180: 20 74 72 69 6d 29 0a 2a 2a 20 20 20 20 20 43 52 trim).** CR
a190: 45 41 54 45 20 49 4e 44 45 58 20 61 62 63 20 4f EATE INDEX abc O
a1a0: 4e 20 78 79 7a 28 27 63 27 2c 27 64 27 20 44 45 N xyz('c','d' DE
a1b0: 53 43 2c 27 65 27 20 43 4f 4c 4c 41 54 45 20 6e SC,'e' COLLATE n
a1c0: 6f 63 61 73 65 20 44 45 53 43 29 3b 0a 2a 2a 0a ocase DESC);.**.
a1d0: 2a 2a 20 54 68 69 73 20 69 73 20 67 6f 6f 66 79 ** This is goofy
a1e0: 2e 20 20 42 75 74 20 74 6f 20 70 72 65 73 65 72 . But to preser
a1f0: 76 65 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d ve backwards com
a200: 70 61 74 69 62 69 6c 69 74 79 20 77 65 20 63 6f patibility we co
a210: 6e 74 69 6e 75 65 20 74 6f 0a 2a 2a 20 61 63 63 ntinue to.** acc
a220: 65 70 74 20 69 74 2e 20 20 54 68 69 73 20 72 6f ept it. This ro
a230: 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 6e utine does the n
a240: 65 63 65 73 73 61 72 79 20 63 6f 6e 76 65 72 73 ecessary convers
a250: 69 6f 6e 2e 20 20 49 74 20 63 6f 6e 76 65 72 74 ion. It convert
a260: 73 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73 s.** the express
a270: 69 6f 6e 20 67 69 76 65 6e 20 69 6e 20 69 74 73 ion given in its
a280: 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 61 argument from a
a290: 20 54 4b 5f 53 54 52 49 4e 47 20 69 6e 74 6f 20 TK_STRING into
a2a0: 61 20 54 4b 5f 49 44 0a 2a 2a 20 69 66 20 74 68 a TK_ID.** if th
a2b0: 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
a2c0: 6a 75 73 74 20 61 20 54 4b 5f 53 54 52 49 4e 47 just a TK_STRING
a2d0: 20 77 69 74 68 20 61 6e 20 6f 70 74 69 6f 6e 61 with an optiona
a2e0: 6c 20 43 4f 4c 4c 41 54 45 20 63 6c 61 75 73 65 l COLLATE clause
a2f0: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 65 70 78 72 ..** If the epxr
a300: 65 73 73 69 6f 6e 20 69 73 20 61 6e 79 74 68 69 ession is anythi
a310: 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 54 4b ng other than TK
a320: 5f 53 54 52 49 4e 47 2c 20 74 68 65 20 65 78 70 _STRING, the exp
a330: 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e ression is.** un
a340: 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 changed..*/.stat
a350: 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 ic void sqlite3S
a360: 74 72 69 6e 67 54 6f 49 64 28 45 78 70 72 20 2a tringToId(Expr *
a370: 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d p){. if( p->op=
a380: 3d 54 4b 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 =TK_STRING ){.
a390: 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 44 3b p->op = TK_ID;
a3a0: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
a3b0: 6f 70 3d 3d 54 4b 5f 43 4f 4c 4c 41 54 45 20 26 op==TK_COLLATE &
a3c0: 26 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d & p->pLeft->op==
a3d0: 54 4b 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 TK_STRING ){.
a3e0: 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 20 3d 20 p->pLeft->op =
a3f0: 54 4b 5f 49 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a TK_ID;. }.}../*
a400: 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74 68 .** Designate th
a410: 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 66 6f e PRIMARY KEY fo
a420: 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20 70 4c r the table. pL
a430: 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 ist is a list of
a440: 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 6f names .** of co
a450: 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 lumns that form
a460: 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e the primary key.
a470: 20 20 49 66 20 70 4c 69 73 74 20 69 73 20 4e 55 If pList is NU
a480: 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 LL, then the.**
a490: 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 most recently ad
a4a0: 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 ded column of th
a4b0: 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 e table is the p
a4c0: 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a rimary key..**.*
a4d0: 2a 20 41 20 74 61 62 6c 65 20 63 61 6e 20 68 61 * A table can ha
a4e0: 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 70 ve at most one p
a4f0: 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20 rimary key. If
a500: 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 the table alread
a510: 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d 61 y has.** a prima
a520: 72 79 20 6b 65 79 20 28 61 6e 64 20 74 68 69 73 ry key (and this
a530: 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 is the second p
a540: 72 69 6d 61 72 79 20 6b 65 79 29 20 74 68 65 6e rimary key) then
a550: 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65 72 create an.** er
a560: 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ror..**.** If th
a570: 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 e PRIMARY KEY is
a580: 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c on a single col
a590: 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 61 74 79 umn whose dataty
a5a0: 70 65 20 69 73 20 49 4e 54 45 47 45 52 2c 0a 2a pe is INTEGER,.*
a5b0: 2a 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 74 * then we will t
a5c0: 72 79 20 74 6f 20 75 73 65 20 74 68 61 74 20 63 ry to use that c
a5d0: 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 6f 77 olumn as the row
a5e0: 69 64 2e 20 20 53 65 74 20 74 68 65 20 54 61 62 id. Set the Tab
a5f0: 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65 6c le.iPKey.** fiel
a600: 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 75 d of the table u
a610: 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
a620: 6e 20 74 6f 20 62 65 20 74 68 65 20 69 6e 64 65 n to be the inde
a630: 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54 45 x of the.** INTE
a640: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 GER PRIMARY KEY
a650: 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e 69 column. Table.i
a660: 50 4b 65 79 20 69 73 20 73 65 74 20 74 6f 20 2d PKey is set to -
a670: 31 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 1 if there is.**
a680: 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d no INTEGER PRIM
a690: 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 ARY KEY..**.** I
a6a0: 66 20 74 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 f the key is not
a6b0: 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d an INTEGER PRIM
a6c0: 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 63 72 ARY KEY, then cr
a6d0: 65 61 74 65 20 61 20 75 6e 69 71 75 65 0a 2a 2a eate a unique.**
a6e0: 20 69 6e 64 65 78 20 66 6f 72 20 74 68 65 20 6b index for the k
a6f0: 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69 73 ey. No index is
a700: 20 63 72 65 61 74 65 64 20 66 6f 72 20 49 4e 54 created for INT
a710: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
a720: 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 s..*/.void sqlit
a730: 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 e3AddPrimaryKey(
a740: 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
a750: 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 , /* Parsing
a760: 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
a770: 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f rList *pList, /
a780: 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c 64 20 * List of field
a790: 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e 64 65 names to be inde
a7a0: 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 xed */. int onE
a7b0: 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57 68 rror, /* Wh
a7c0: 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 61 20 at to do with a
a7d0: 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c uniqueness confl
a7e0: 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74 ict */. int aut
a7f0: 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54 72 oInc, /* Tr
a800: 75 65 20 69 66 20 74 68 65 20 41 55 54 4f 49 4e ue if the AUTOIN
a810: 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20 CREMENT keyword
a820: 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 is present */.
a830: 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 20 20 int sortOrder
a840: 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41 /* SQLITE_SO_A
a850: 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f SC or SQLITE_SO_
a860: 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 DESC */.){. Tab
a870: 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73 le *pTab = pPars
a880: 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 e->pNewTable;.
a890: 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 Column *pCol = 0
a8a0: 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d ;. int iCol = -
a8b0: 31 2c 20 69 3b 0a 20 20 69 6e 74 20 6e 54 65 72 1, i;. int nTer
a8c0: 6d 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 m;. if( pTab==0
a8d0: 20 29 20 67 6f 74 6f 20 70 72 69 6d 61 72 79 5f ) goto primary_
a8e0: 6b 65 79 5f 65 78 69 74 3b 0a 20 20 69 66 28 20 key_exit;. if(
a8f0: 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 pTab->tabFlags &
a900: 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 TF_HasPrimaryKe
a910: 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 y ){. sqlite3
a920: 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
a930: 20 0a 20 20 20 20 20 20 22 74 61 62 6c 65 20 5c . "table \
a940: 22 25 73 5c 22 20 68 61 73 20 6d 6f 72 65 20 74 "%s\" has more t
a950: 68 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 han one primary
a960: 6b 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d key", pTab->zNam
a970: 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 70 72 69 e);. goto pri
a980: 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 mary_key_exit;.
a990: 20 7d 0a 20 20 70 54 61 62 2d 3e 74 61 62 46 6c }. pTab->tabFl
a9a0: 61 67 73 20 7c 3d 20 54 46 5f 48 61 73 50 72 69 ags |= TF_HasPri
a9b0: 6d 61 72 79 4b 65 79 3b 0a 20 20 69 66 28 20 70 maryKey;. if( p
a9c0: 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 List==0 ){. i
a9d0: 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c Col = pTab->nCol
a9e0: 20 2d 20 31 3b 0a 20 20 20 20 70 43 6f 6c 20 3d - 1;. pCol =
a9f0: 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f &pTab->aCol[iCo
aa00: 6c 5d 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 63 6f l];. pCol->co
aa10: 6c 46 6c 61 67 73 20 7c 3d 20 43 4f 4c 46 4c 41 lFlags |= COLFLA
aa20: 47 5f 50 52 49 4d 4b 45 59 3b 0a 20 20 20 20 6e G_PRIMKEY;. n
aa30: 54 65 72 6d 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 Term = 1;. }els
aa40: 65 7b 0a 20 20 20 20 6e 54 65 72 6d 20 3d 20 70 e{. nTerm = p
aa50: 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 List->nExpr;.
aa60: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 65 72 for(i=0; i<nTer
aa70: 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 m; i++){. E
aa80: 78 70 72 20 2a 70 43 45 78 70 72 20 3d 20 73 71 xpr *pCExpr = sq
aa90: 6c 69 74 65 33 45 78 70 72 53 6b 69 70 43 6f 6c lite3ExprSkipCol
aaa0: 6c 61 74 65 28 70 4c 69 73 74 2d 3e 61 5b 69 5d late(pList->a[i]
aab0: 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 61 .pExpr);. a
aac0: 73 73 65 72 74 28 20 70 43 45 78 70 72 21 3d 30 ssert( pCExpr!=0
aad0: 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
aae0: 33 53 74 72 69 6e 67 54 6f 49 64 28 70 43 45 78 3StringToId(pCEx
aaf0: 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 pr);. if( p
ab00: 43 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 CExpr->op==TK_ID
ab10: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 ){. cons
ab20: 74 20 63 68 61 72 20 2a 7a 43 4e 61 6d 65 20 3d t char *zCName =
ab30: 20 70 43 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 pCExpr->u.zToke
ab40: 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 n;. for(i
ab50: 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 Col=0; iCol<pTab
ab60: 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b ->nCol; iCol++){
ab70: 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 . if( s
ab80: 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 43 qlite3StrICmp(zC
ab90: 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c Name, pTab->aCol
aba0: 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 [iCol].zName)==0
abb0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
abc0: 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 pCol = &pTab->aC
abd0: 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 ol[iCol];.
abe0: 20 20 20 20 20 20 70 43 6f 6c 2d 3e 63 6f 6c 46 pCol->colF
abf0: 6c 61 67 73 20 7c 3d 20 43 4f 4c 46 4c 41 47 5f lags |= COLFLAG_
ac00: 50 52 49 4d 4b 45 59 3b 0a 20 20 20 20 20 20 20 PRIMKEY;.
ac10: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
ac20: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
ac30: 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
ac40: 20 20 7d 0a 20 20 69 66 28 20 6e 54 65 72 6d 3d }. if( nTerm=
ac50: 3d 31 0a 20 20 20 26 26 20 70 43 6f 6c 0a 20 20 =1. && pCol.
ac60: 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
ac70: 6d 70 28 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e mp(sqlite3Column
ac80: 54 79 70 65 28 70 43 6f 6c 2c 22 22 29 2c 20 22 Type(pCol,""), "
ac90: 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a 20 20 20 INTEGER")==0.
aca0: 26 26 20 73 6f 72 74 4f 72 64 65 72 21 3d 53 51 && sortOrder!=SQ
acb0: 4c 49 54 45 5f 53 4f 5f 44 45 53 43 0a 20 20 29 LITE_SO_DESC. )
acc0: 7b 0a 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 {. pTab->iPKe
acd0: 79 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 54 y = iCol;. pT
ace0: 61 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d 20 28 75 ab->keyConf = (u
acf0: 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 61 8)onError;. a
ad00: 73 73 65 72 74 28 20 61 75 74 6f 49 6e 63 3d 3d ssert( autoInc==
ad10: 30 20 7c 7c 20 61 75 74 6f 49 6e 63 3d 3d 31 20 0 || autoInc==1
ad20: 29 3b 0a 20 20 20 20 70 54 61 62 2d 3e 74 61 62 );. pTab->tab
ad30: 46 6c 61 67 73 20 7c 3d 20 61 75 74 6f 49 6e 63 Flags |= autoInc
ad40: 2a 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e *TF_Autoincremen
ad50: 74 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 t;. if( pList
ad60: 20 29 20 70 50 61 72 73 65 2d 3e 69 50 6b 53 6f ) pParse->iPkSo
ad70: 72 74 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 2d rtOrder = pList-
ad80: 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 3b >a[0].sortOrder;
ad90: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 75 74 . }else if( aut
ada0: 6f 49 6e 63 20 29 7b 0a 23 69 66 6e 64 65 66 20 oInc ){.#ifndef
adb0: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
adc0: 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 73 71 INCREMENT. sq
add0: 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
ade0: 61 72 73 65 2c 20 22 41 55 54 4f 49 4e 43 52 45 arse, "AUTOINCRE
adf0: 4d 45 4e 54 20 69 73 20 6f 6e 6c 79 20 61 6c 6c MENT is only all
ae00: 6f 77 65 64 20 6f 6e 20 61 6e 20 22 0a 20 20 20 owed on an ".
ae10: 20 20 20 20 22 49 4e 54 45 47 45 52 20 50 52 49 "INTEGER PRI
ae20: 4d 41 52 59 20 4b 45 59 22 29 3b 0a 23 65 6e 64 MARY KEY");.#end
ae30: 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 if. }else{.
ae40: 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 sqlite3CreateInd
ae50: 65 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c ex(pParse, 0, 0,
ae60: 20 30 2c 20 70 4c 69 73 74 2c 20 6f 6e 45 72 72 0, pList, onErr
ae70: 6f 72 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 or, 0,.
ae80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae90: 20 20 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c 20 0, sortOrder,
aea0: 30 2c 20 53 51 4c 49 54 45 5f 49 44 58 54 59 50 0, SQLITE_IDXTYP
aeb0: 45 5f 50 52 49 4d 41 52 59 4b 45 59 29 3b 0a 20 E_PRIMARYKEY);.
aec0: 20 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 pList = 0;.
aed0: 7d 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 }..primary_key_e
aee0: 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 xit:. sqlite3Ex
aef0: 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 prListDelete(pPa
af00: 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b rse->db, pList);
af10: 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a . return;.}../*
af20: 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 .** Add a new CH
af30: 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 ECK constraint t
af40: 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 o the table curr
af50: 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 ently under cons
af60: 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 truction..*/.voi
af70: 64 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 d sqlite3AddChec
af80: 6b 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 kConstraint(. P
af90: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
afa0: 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
afb0: 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ext */. Expr *p
afc0: 43 68 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 CheckExpr /* Th
afd0: 65 20 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 e check expressi
afe0: 6f 6e 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 on */.){.#ifndef
aff0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 SQLITE_OMIT_CHE
b000: 43 4b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 CK. Table *pTab
b010: 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 = pParse->pNewT
b020: 61 62 6c 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 able;. sqlite3
b030: 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
b040: 3b 0a 20 20 69 66 28 20 70 54 61 62 20 26 26 20 ;. if( pTab &&
b050: 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 !IN_DECLARE_VTAB
b060: 0a 20 20 20 26 26 20 21 73 71 6c 69 74 65 33 42 . && !sqlite3B
b070: 74 72 65 65 49 73 52 65 61 64 6f 6e 6c 79 28 64 treeIsReadonly(d
b080: 62 2d 3e 61 44 62 5b 64 62 2d 3e 69 6e 69 74 2e b->aDb[db->init.
b090: 69 44 62 5d 2e 70 42 74 29 0a 20 20 29 7b 0a 20 iDb].pBt). ){.
b0a0: 20 20 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 pTab->pCheck
b0b0: 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 = sqlite3ExprLis
b0c0: 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 tAppend(pParse,
b0d0: 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 70 43 pTab->pCheck, pC
b0e0: 68 65 63 6b 45 78 70 72 29 3b 0a 20 20 20 20 69 heckExpr);. i
b0f0: 66 28 20 70 50 61 72 73 65 2d 3e 63 6f 6e 73 74 f( pParse->const
b100: 72 61 69 6e 74 4e 61 6d 65 2e 6e 20 29 7b 0a 20 raintName.n ){.
b110: 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
b120: 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 ListSetName(pPar
b130: 73 65 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b se, pTab->pCheck
b140: 2c 20 26 70 50 61 72 73 65 2d 3e 63 6f 6e 73 74 , &pParse->const
b150: 72 61 69 6e 74 4e 61 6d 65 2c 20 31 29 3b 0a 20 raintName, 1);.
b160: 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e }. }else.#en
b170: 64 69 66 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 dif. {. sqli
b180: 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 te3ExprDelete(pP
b190: 61 72 73 65 2d 3e 64 62 2c 20 70 43 68 65 63 6b arse->db, pCheck
b1a0: 45 78 70 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a Expr);. }.}../*
b1b0: 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c .** Set the coll
b1c0: 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 6f ation function o
b1d0: 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e f the most recen
b1e0: 74 6c 79 20 70 61 72 73 65 64 20 74 61 62 6c 65 tly parsed table
b1f0: 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 74 68 column.** to th
b200: 65 20 43 6f 6c 6c 53 65 71 20 67 69 76 65 6e 2e e CollSeq given.
b210: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
b220: 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 AddCollateType(P
b230: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f arse *pParse, To
b240: 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 ken *pToken){.
b250: 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 Table *p;. int
b260: 69 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c i;. char *zColl
b270: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
b280: 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 * Dequoted name
b290: 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 of collation seq
b2a0: 75 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 uence */. sqlit
b2b0: 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 28 e3 *db;.. if( (
b2c0: 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 p = pParse->pNew
b2d0: 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 Table)==0 ) retu
b2e0: 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f rn;. i = p->nCo
b2f0: 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 l-1;. db = pPar
b300: 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 se->db;. zColl
b310: 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
b320: 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65 mToken(db, pToke
b330: 6e 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c n);. if( !zColl
b340: 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 ) return;.. if
b350: 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 ( sqlite3LocateC
b360: 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a ollSeq(pParse, z
b370: 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 49 6e 64 Coll) ){. Ind
b380: 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 73 71 ex *pIdx;. sq
b390: 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
b3a0: 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c p->aCol[i].zColl
b3b0: 29 3b 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 );. p->aCol[i
b3c0: 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b ].zColl = zColl;
b3d0: 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 . . /* If th
b3e0: 65 20 63 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c e column is decl
b3f0: 61 72 65 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 ared as "<name>
b400: 50 52 49 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c PRIMARY KEY COLL
b410: 41 54 45 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 ATE <type>",.
b420: 20 2a 2a 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 ** then an inde
b430: 78 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 x may have been
b440: 63 72 65 61 74 65 64 20 6f 6e 20 74 68 69 73 20 created on this
b450: 63 6f 6c 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 column before th
b460: 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 e. ** collati
b470: 6f 6e 20 74 79 70 65 20 77 61 73 20 61 64 64 65 on type was adde
b480: 64 2e 20 43 6f 72 72 65 63 74 20 74 68 69 73 20 d. Correct this
b490: 69 66 20 69 74 20 69 73 20 74 68 65 20 63 61 73 if it is the cas
b4a0: 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f e.. */. fo
b4b0: 72 28 70 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 r(pIdx=p->pIndex
b4c0: 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 ; pIdx; pIdx=pId
b4d0: 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 x->pNext){.
b4e0: 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6e assert( pIdx->n
b4f0: 4b 65 79 43 6f 6c 3d 3d 31 20 29 3b 0a 20 20 20 KeyCol==1 );.
b500: 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 if( pIdx->aiC
b510: 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 olumn[0]==i ){.
b520: 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 pIdx->azC
b530: 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c oll[0] = p->aCol
b540: 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 [i].zColl;.
b550: 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
b560: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
b570: 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a ree(db, zColl);.
b580: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
b590: 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
b5a0: 6e 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ns the collation
b5b0: 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 64 61 sequence for da
b5c0: 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 74 65 tabase native te
b5d0: 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 xt.** encoding i
b5e0: 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 dentified by the
b5f0: 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c string zName, l
b600: 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a ength nName..**.
b610: 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 ** If the reques
b620: 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 ted collation se
b630: 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 quence is not av
b640: 61 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 ailable, or not
b650: 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 available.** in
b660: 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 the database nat
b670: 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 ive encoding, th
b680: 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 e collation fact
b690: 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 ory is invoked t
b6a0: 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 74 2e o.** request it.
b6b0: 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f If the collatio
b6c0: 6e 20 66 61 63 74 6f 72 79 20 64 6f 65 73 20 6e n factory does n
b6d0: 6f 74 20 73 75 70 70 6c 79 20 73 75 63 68 20 61 ot supply such a
b6e0: 20 73 65 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e sequence,.** an
b6f0: 64 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 69 d the sequence i
b700: 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 s available in a
b710: 6e 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63 6f nother text enco
b720: 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 61 74 20 ding, then that
b730: 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 is.** returned i
b740: 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 nstead..**.** If
b750: 20 6e 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 no versions of
b760: 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f the requested co
b770: 6c 6c 61 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 llations sequenc
b780: 65 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c e are available,
b790: 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 or.** another e
b7a0: 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c rror occurs, NUL
b7b0: 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e L is returned an
b7c0: 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 d an error messa
b7d0: 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a ge written into.
b7e0: 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a ** pParse..**.**
b7f0: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
b800: 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e a wrapper aroun
b810: 64 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c d sqlite3FindCol
b820: 6c 53 65 71 28 29 2e 20 20 54 68 69 73 20 72 6f lSeq(). This ro
b830: 75 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 utine.** invokes
b840: 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 the collation f
b850: 61 63 74 6f 72 79 20 69 66 20 74 68 65 20 6e 61 actory if the na
b860: 6d 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 med collation ca
b870: 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a nnot be found.**
b880: 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 and generates a
b890: 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e n error message.
b8a0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
b8b0: 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c sqlite3FindColl
b8c0: 53 65 71 28 29 2c 20 73 71 6c 69 74 65 33 47 65 Seq(), sqlite3Ge
b8d0: 74 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a 43 6f tCollSeq().*/.Co
b8e0: 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f llSeq *sqlite3Lo
b8f0: 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 cateCollSeq(Pars
b900: 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
b910: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 char *zName){.
b920: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
b930: 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 75 38 20 Parse->db;. u8
b940: 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 enc = ENC(db);.
b950: 20 75 38 20 69 6e 69 74 62 75 73 79 20 3d 20 64 u8 initbusy = d
b960: 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0a 20 20 b->init.busy;.
b970: 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a CollSeq *pColl;.
b980: 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 . pColl = sqlit
b990: 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 e3FindCollSeq(db
b9a0: 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 69 6e , enc, zName, in
b9b0: 69 74 62 75 73 79 29 3b 0a 20 20 69 66 28 20 21 itbusy);. if( !
b9c0: 69 6e 69 74 62 75 73 79 20 26 26 20 28 21 70 43 initbusy && (!pC
b9d0: 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c 6c 2d 3e 78 oll || !pColl->x
b9e0: 43 6d 70 29 20 29 7b 0a 20 20 20 20 70 43 6f 6c Cmp) ){. pCol
b9f0: 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43 6f l = sqlite3GetCo
ba00: 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 65 6e llSeq(pParse, en
ba10: 63 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29 c, pColl, zName)
ba20: 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
ba30: 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a pColl;.}.../*.**
ba40: 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
ba50: 68 61 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 hat will increme
ba60: 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f nt the schema co
ba70: 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 okie..**.** The
ba80: 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 73 schema cookie is
ba90: 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 used to determi
baa0: 6e 65 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 ne when the sche
bab0: 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 61 ma for the.** da
bac0: 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 tabase changes.
bad0: 20 41 66 74 65 72 20 65 61 63 68 20 73 63 68 65 After each sche
bae0: 6d 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20 63 ma change, the c
baf0: 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20 63 ookie value.** c
bb00: 68 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61 20 hanges. When a
bb10: 70 72 6f 63 65 73 73 20 66 69 72 73 74 20 72 65 process first re
bb20: 61 64 73 20 74 68 65 20 73 63 68 65 6d 61 20 69 ads the schema i
bb30: 74 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a t records the.**
bb40: 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65 61 cookie. Therea
bb50: 66 74 65 72 2c 20 77 68 65 6e 65 76 65 72 20 69 fter, whenever i
bb60: 74 20 67 6f 65 73 20 74 6f 20 61 63 63 65 73 73 t goes to access
bb70: 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a the database,.*
bb80: 2a 20 69 74 20 63 68 65 63 6b 73 20 74 68 65 20 * it checks the
bb90: 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20 73 cookie to make s
bba0: 75 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 68 ure the schema h
bbb0: 61 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a 2a as not changed.*
bbc0: 2a 20 73 69 6e 63 65 20 69 74 20 77 61 73 20 6c * since it was l
bbd0: 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 ast read..**.**
bbe0: 54 68 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f 74 This plan is not
bbf0: 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c 6c completely bull
bc00: 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69 73 et-proof. It is
bc10: 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a 2a possible for.**
bc20: 20 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20 63 the schema to c
bc30: 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20 74 hange multiple t
bc40: 69 6d 65 73 20 61 6e 64 20 66 6f 72 20 74 68 65 imes and for the
bc50: 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a 2a cookie to be.**
bc60: 20 73 65 74 20 62 61 63 6b 20 74 6f 20 70 72 69 set back to pri
bc70: 6f 72 20 76 61 6c 75 65 2e 20 20 42 75 74 20 73 or value. But s
bc80: 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61 72 chema changes ar
bc90: 65 20 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a 20 e infrequent.**
bca0: 61 6e 64 20 74 68 65 20 70 72 6f 62 61 62 69 6c and the probabil
bcb0: 69 74 79 20 6f 66 20 68 69 74 74 69 6e 67 20 74 ity of hitting t
bcc0: 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20 76 he same cookie v
bcd0: 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 alue is only.**
bce0: 31 20 63 68 61 6e 63 65 20 69 6e 20 32 5e 33 32 1 chance in 2^32
bcf0: 2e 20 20 53 6f 20 77 65 27 72 65 20 73 61 66 65 . So we're safe
bd00: 20 65 6e 6f 75 67 68 2e 0a 2a 2a 0a 2a 2a 20 49 enough..**.** I
bd10: 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 MPLEMENTATION-OF
bd20: 3a 20 52 2d 33 34 32 33 30 2d 35 36 30 34 39 20 : R-34230-56049
bd30: 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 SQLite automatic
bd40: 61 6c 6c 79 20 69 6e 63 72 65 6d 65 6e 74 73 0a ally increments.
bd50: 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 ** the schema-ve
bd60: 72 73 69 6f 6e 20 77 68 65 6e 65 76 65 72 20 74 rsion whenever t
bd70: 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 he schema change
bd80: 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 s..*/.void sqlit
bd90: 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50 e3ChangeCookie(P
bda0: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
bdb0: 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 t iDb){. sqlite
bdc0: 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
bdd0: 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 db;. Vdbe *v =
bde0: 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
bdf0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
be00: 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 SchemaMutexHeld(
be10: 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 20 db, iDb, 0) );.
be20: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
be30: 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b p3(v, OP_SetCook
be40: 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 53 ie, iDb, BTREE_S
be50: 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 0a CHEMA_VERSION, .
be60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be70: 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d db->aDb[iDb]
be80: 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 .pSchema->schema
be90: 5f 63 6f 6f 6b 69 65 2b 31 29 3b 0a 7d 0a 0a 2f _cookie+1);.}../
bea0: 2a 0a 2a 2a 20 4d 65 61 73 75 72 65 20 74 68 65 *.** Measure the
beb0: 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 number of chara
bec0: 63 74 65 72 73 20 6e 65 65 64 65 64 20 74 6f 20 cters needed to
bed0: 6f 75 74 70 75 74 20 74 68 65 20 67 69 76 65 6e output the given
bee0: 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 2e 20 .** identifier.
bef0: 20 54 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 The number retu
bf00: 72 6e 65 64 20 69 6e 63 6c 75 64 65 73 20 61 6e rned includes an
bf10: 79 20 71 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a y quotes used.**
bf20: 20 62 75 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e but does not in
bf30: 63 6c 75 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 clude the null t
bf40: 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a erminator..**.**
bf50: 20 54 68 65 20 65 73 74 69 6d 61 74 65 20 69 73 The estimate is
bf60: 20 63 6f 6e 73 65 72 76 61 74 69 76 65 2e 20 20 conservative.
bf70: 49 74 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 It might be larg
bf80: 65 72 20 74 68 61 74 20 77 68 61 74 20 69 73 0a er that what is.
bf90: 2a 2a 20 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 ** really needed
bfa0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
bfb0: 69 64 65 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 identLength(cons
bfc0: 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e t char *z){. in
bfd0: 74 20 6e 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 t n;. for(n=0;
bfe0: 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 *z; n++, z++){.
bff0: 20 20 20 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 if( *z=='"' )
c000: 7b 20 6e 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 { n++; }. }. r
c010: 65 74 75 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a eturn n + 2;.}..
c020: 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 /*.** The first
c030: 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 parameter is a p
c040: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 75 74 ointer to an out
c050: 70 75 74 20 62 75 66 66 65 72 2e 20 54 68 65 20 put buffer. The
c060: 73 65 63 6f 6e 64 20 0a 2a 2a 20 70 61 72 61 6d second .** param
c070: 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 eter is a pointe
c080: 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 r to an integer
c090: 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
c0a0: 65 20 6f 66 66 73 65 74 20 61 74 0a 2a 2a 20 77 e offset at.** w
c0b0: 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 69 6e hich to write in
c0c0: 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 to the output bu
c0d0: 66 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 ffer. This funct
c0e0: 69 6f 6e 20 63 6f 70 69 65 73 20 74 68 65 0a 2a ion copies the.*
c0f0: 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 * nul-terminated
c100: 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 string pointed
c110: 74 6f 20 62 79 20 74 68 65 20 74 68 69 72 64 20 to by the third
c120: 70 61 72 61 6d 65 74 65 72 2c 20 7a 53 69 67 6e parameter, zSign
c130: 65 64 49 64 65 6e 74 2c 0a 2a 2a 20 74 6f 20 74 edIdent,.** to t
c140: 68 65 20 73 70 65 63 69 66 69 65 64 20 6f 66 66 he specified off
c150: 73 65 74 20 69 6e 20 74 68 65 20 62 75 66 66 65 set in the buffe
c160: 72 20 61 6e 64 20 75 70 64 61 74 65 73 20 2a 70 r and updates *p
c170: 49 64 78 20 74 6f 20 72 65 66 65 72 0a 2a 2a 20 Idx to refer.**
c180: 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74 to the first byt
c190: 65 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 e after the last
c1a0: 20 62 79 74 65 20 77 72 69 74 74 65 6e 20 62 65 byte written be
c1b0: 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a fore returning..
c1c0: 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 ** .** If the st
c1d0: 72 69 6e 67 20 7a 53 69 67 6e 65 64 49 64 65 6e ring zSignedIden
c1e0: 74 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 t consists entir
c1f0: 65 6c 79 20 6f 66 20 61 6c 70 68 61 2d 6e 75 6d ely of alpha-num
c200: 65 72 69 63 0a 2a 2a 20 63 68 61 72 61 63 74 65 eric.** characte
c210: 72 73 2c 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 rs, does not beg
c220: 69 6e 20 77 69 74 68 20 61 20 64 69 67 69 74 20 in with a digit
c230: 61 6e 64 20 69 73 20 6e 6f 74 20 61 6e 20 53 51 and is not an SQ
c240: 4c 20 6b 65 79 77 6f 72 64 2c 0a 2a 2a 20 74 68 L keyword,.** th
c250: 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 en it is copied
c260: 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 to the output bu
c270: 66 66 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 ffer exactly as
c280: 69 74 20 69 73 2e 20 4f 74 68 65 72 77 69 73 65 it is. Otherwise
c290: 2c 0a 2a 2a 20 69 74 20 69 73 20 71 75 6f 74 65 ,.** it is quote
c2a0: 64 20 75 73 69 6e 67 20 64 6f 75 62 6c 65 2d 71 d using double-q
c2b0: 75 6f 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 uotes..*/.static
c2c0: 20 76 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 void identPut(c
c2d0: 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 har *z, int *pId
c2e0: 78 2c 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 x, char *zSigned
c2f0: 49 64 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e Ident){. unsign
c300: 65 64 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 ed char *zIdent
c310: 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
c320: 2a 29 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a *)zSignedIdent;.
c330: 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 int i, j, need
c340: 51 75 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 Quote;. i = *pI
c350: 64 78 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 dx;.. for(j=0;
c360: 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b zIdent[j]; j++){
c370: 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
c380: 33 49 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 3Isalnum(zIdent[
c390: 6a 5d 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d j]) && zIdent[j]
c3a0: 21 3d 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 !='_' ) break;.
c3b0: 20 7d 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d }. needQuote =
c3c0: 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
c3d0: 7a 49 64 65 6e 74 5b 30 5d 29 0a 20 20 20 20 20 zIdent[0]).
c3e0: 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
c3f0: 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 7a 49 64 3KeywordCode(zId
c400: 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f 49 44 0a 20 ent, j)!=TK_ID.
c410: 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 49 || zI
c420: 64 65 6e 74 5b 6a 5d 21 3d 30 0a 20 20 20 20 20 dent[j]!=0.
c430: 20 20 20 20 20 20 20 7c 7c 20 6a 3d 3d 30 3b 0a || j==0;.
c440: 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 . if( needQuote
c450: 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b ) z[i++] = '"';
c460: 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 . for(j=0; zIde
c470: 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 nt[j]; j++){.
c480: 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e 74 z[i++] = zIdent
c490: 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20 7a 49 64 [j];. if( zId
c4a0: 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a 5b ent[j]=='"' ) z[
c4b0: 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7d 0a i++] = '"';. }.
c4c0: 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 if( needQuote
c4d0: 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a ) z[i++] = '"';.
c4e0: 20 20 7a 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 z[i] = 0;. *p
c4f0: 49 64 78 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a Idx = i;.}../*.*
c500: 2a 20 47 65 6e 65 72 61 74 65 20 61 20 43 52 45 * Generate a CRE
c510: 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
c520: 65 6e 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 ent appropriate
c530: 66 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a for the given.**
c540: 20 74 61 62 6c 65 2e 20 20 4d 65 6d 6f 72 79 20 table. Memory
c550: 74 6f 20 68 6f 6c 64 20 74 68 65 20 74 65 78 74 to hold the text
c560: 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e of the statemen
c570: 74 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a t is obtained.**
c580: 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c from sqliteMall
c590: 6f 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62 65 oc() and must be
c5a0: 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 freed by the ca
c5b0: 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a lling function..
c5c0: 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a */.static char *
c5d0: 63 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28 createTableStmt(
c5e0: 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 sqlite3 *db, Tab
c5f0: 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 2c le *p){. int i,
c600: 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a 7a k, n;. char *z
c610: 53 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a 53 Stmt;. char *zS
c620: 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45 6e ep, *zSep2, *zEn
c630: 64 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f d;. Column *pCo
c640: 6c 3b 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 66 6f l;. n = 0;. fo
c650: 72 28 70 43 6f 6c 20 3d 20 70 2d 3e 61 43 6f 6c r(pCol = p->aCol
c660: 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c , i=0; i<p->nCol
c670: 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a ; i++, pCol++){.
c680: 20 20 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 n += identLe
c690: 6e 67 74 68 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 ngth(pCol->zName
c6a0: 29 20 2b 20 35 3b 0a 20 20 7d 0a 20 20 6e 20 2b ) + 5;. }. n +
c6b0: 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 2d = identLength(p-
c6c0: 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 6e >zName);. if( n
c6d0: 3c 35 30 20 29 7b 20 0a 20 20 20 20 7a 53 65 70 <50 ){ . zSep
c6e0: 20 3d 20 22 22 3b 0a 20 20 20 20 7a 53 65 70 32 = "";. zSep2
c6f0: 20 3d 20 22 2c 22 3b 0a 20 20 20 20 7a 45 6e 64 = ",";. zEnd
c700: 20 3d 20 22 29 22 3b 0a 20 20 7d 65 6c 73 65 7b = ")";. }else{
c710: 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 5c 6e 20 . zSep = "\n
c720: 20 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 ";. zSep2 =
c730: 22 2c 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 45 6e ",\n ";. zEn
c740: 64 20 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d 0a 20 d = "\n)";. }.
c750: 20 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d 3e n += 35 + 6*p->
c760: 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20 3d 20 nCol;. zStmt =
c770: 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
c780: 61 77 28 30 2c 20 6e 29 3b 0a 20 20 69 66 28 20 aw(0, n);. if(
c790: 7a 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 zStmt==0 ){.
c7a0: 73 71 6c 69 74 65 33 4f 6f 6d 46 61 75 6c 74 28 sqlite3OomFault(
c7b0: 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 db);. return
c7c0: 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 0;. }. sqlite3
c7d0: 5f 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74 _snprintf(n, zSt
c7e0: 6d 74 2c 20 22 43 52 45 41 54 45 20 54 41 42 4c mt, "CREATE TABL
c7f0: 45 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71 6c 69 E ");. k = sqli
c800: 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 53 74 6d te3Strlen30(zStm
c810: 74 29 3b 0a 20 20 69 64 65 6e 74 50 75 74 28 7a t);. identPut(z
c820: 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61 Stmt, &k, p->zNa
c830: 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b me);. zStmt[k++
c840: 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72 28 70 ] = '(';. for(p
c850: 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 Col=p->aCol, i=0
c860: 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b ; i<p->nCol; i++
c870: 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73 , pCol++){. s
c880: 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
c890: 20 2a 20 63 6f 6e 73 74 20 61 7a 54 79 70 65 5b * const azType[
c8a0: 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a ] = {. /*
c8b0: 20 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 SQLITE_AFF_BLOB
c8c0: 20 20 20 20 2a 2f 20 22 22 2c 0a 20 20 20 20 20 */ "",.
c8d0: 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 /* SQLITE_AFF
c8e0: 5f 54 45 58 54 20 20 20 20 2a 2f 20 22 20 54 45 _TEXT */ " TE
c8f0: 58 54 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20 XT",. /*
c900: 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
c910: 49 43 20 2a 2f 20 22 20 4e 55 4d 22 2c 0a 20 20 IC */ " NUM",.
c920: 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f /* SQLITE_
c930: 41 46 46 5f 49 4e 54 45 47 45 52 20 2a 2f 20 22 AFF_INTEGER */ "
c940: 20 49 4e 54 22 2c 0a 20 20 20 20 20 20 20 20 2f INT",. /
c950: 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 * SQLITE_AFF_REA
c960: 4c 20 20 20 20 2a 2f 20 22 20 52 45 41 4c 22 0a L */ " REAL".
c970: 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6c };. int l
c980: 65 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 en;. const ch
c990: 61 72 20 2a 7a 54 79 70 65 3b 0a 0a 20 20 20 20 ar *zType;..
c9a0: 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
c9b0: 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c (n-k, &zStmt[k],
c9c0: 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20 2b 3d zSep);. k +=
c9d0: 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
c9e0: 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20 20 20 (&zStmt[k]);.
c9f0: 20 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b 0a 20 zSep = zSep2;.
ca00: 20 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d identPut(zStm
ca10: 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 t, &k, pCol->zNa
ca20: 6d 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 me);. assert(
ca30: 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2d pCol->affinity-
ca40: 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 20 SQLITE_AFF_BLOB
ca50: 3e 3d 20 30 20 29 3b 0a 20 20 20 20 61 73 73 65 >= 0 );. asse
ca60: 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 rt( pCol->affini
ca70: 74 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c ty-SQLITE_AFF_BL
ca80: 4f 42 20 3c 20 41 72 72 61 79 53 69 7a 65 28 61 OB < ArraySize(a
ca90: 7a 54 79 70 65 29 20 29 3b 0a 20 20 20 20 74 65 zType) );. te
caa0: 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 stcase( pCol->af
cab0: 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
cac0: 46 46 5f 42 4c 4f 42 20 29 3b 0a 20 20 20 20 74 FF_BLOB );. t
cad0: 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 estcase( pCol->a
cae0: 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
caf0: 41 46 46 5f 54 45 58 54 20 29 3b 0a 20 20 20 20 AFF_TEXT );.
cb00: 74 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e testcase( pCol->
cb10: 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 affinity==SQLITE
cb20: 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a _AFF_NUMERIC );.
cb30: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 testcase( pC
cb40: 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 ol->affinity==SQ
cb50: 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
cb60: 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
cb70: 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 ( pCol->affinity
cb80: 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 ==SQLITE_AFF_REA
cb90: 4c 20 29 3b 0a 20 20 20 20 0a 20 20 20 20 7a 54 L );. . zT
cba0: 79 70 65 20 3d 20 61 7a 54 79 70 65 5b 70 43 6f ype = azType[pCo
cbb0: 6c 2d 3e 61 66 66 69 6e 69 74 79 20 2d 20 53 51 l->affinity - SQ
cbc0: 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 5d 3b 0a LITE_AFF_BLOB];.
cbd0: 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 len = sqlite
cbe0: 33 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29 3Strlen30(zType)
cbf0: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
cc00: 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 ol->affinity==SQ
cc10: 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 20 0a 20 LITE_AFF_BLOB .
cc20: 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43 || pC
cc30: 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 73 71 ol->affinity==sq
cc40: 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 lite3AffinityTyp
cc50: 65 28 7a 54 79 70 65 2c 20 30 29 20 29 3b 0a 20 e(zType, 0) );.
cc60: 20 20 20 6d 65 6d 63 70 79 28 26 7a 53 74 6d 74 memcpy(&zStmt
cc70: 5b 6b 5d 2c 20 7a 54 79 70 65 2c 20 6c 65 6e 29 [k], zType, len)
cc80: 3b 0a 20 20 20 20 6b 20 2b 3d 20 6c 65 6e 3b 0a ;. k += len;.
cc90: 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 3d 6e assert( k<=n
cca0: 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 );. }. sqlite
ccb0: 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 3_snprintf(n-k,
ccc0: 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c &zStmt[k], "%s",
ccd0: 20 7a 45 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e zEnd);. return
cce0: 20 7a 53 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a zStmt;.}../*.**
ccf0: 20 52 65 73 69 7a 65 20 61 6e 20 49 6e 64 65 78 Resize an Index
cd00: 20 6f 62 6a 65 63 74 20 74 6f 20 68 6f 6c 64 20 object to hold
cd10: 4e 20 63 6f 6c 75 6d 6e 73 20 74 6f 74 61 6c 2e N columns total.
cd20: 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
cd30: 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 OK.** on success
cd40: 20 61 6e 64 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 and SQLITE_NOME
cd50: 4d 20 6f 6e 20 61 6e 20 4f 4f 4d 20 65 72 72 6f M on an OOM erro
cd60: 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
cd70: 20 72 65 73 69 7a 65 49 6e 64 65 78 4f 62 6a 65 resizeIndexObje
cd80: 63 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ct(sqlite3 *db,
cd90: 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 69 6e 74 Index *pIdx, int
cda0: 20 4e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 45 78 N){. char *zEx
cdb0: 74 72 61 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 tra;. int nByte
cdc0: 3b 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 6e 43 ;. if( pIdx->nC
cdd0: 6f 6c 75 6d 6e 3e 3d 4e 20 29 20 72 65 74 75 72 olumn>=N ) retur
cde0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 n SQLITE_OK;. a
cdf0: 73 73 65 72 74 28 20 70 49 64 78 2d 3e 69 73 52 ssert( pIdx->isR
ce00: 65 73 69 7a 65 64 3d 3d 30 20 29 3b 0a 20 20 6e esized==0 );. n
ce10: 42 79 74 65 20 3d 20 28 73 69 7a 65 6f 66 28 63 Byte = (sizeof(c
ce20: 68 61 72 2a 29 20 2b 20 73 69 7a 65 6f 66 28 69 har*) + sizeof(i
ce30: 31 36 29 20 2b 20 31 29 2a 4e 3b 0a 20 20 7a 45 16) + 1)*N;. zE
ce40: 78 74 72 61 20 3d 20 73 71 6c 69 74 65 33 44 62 xtra = sqlite3Db
ce50: 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e MallocZero(db, n
ce60: 42 79 74 65 29 3b 0a 20 20 69 66 28 20 7a 45 78 Byte);. if( zEx
ce70: 74 72 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 tra==0 ) return
ce80: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 SQLITE_NOMEM_BKP
ce90: 54 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 45 78 74 T;. memcpy(zExt
cea0: 72 61 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c ra, pIdx->azColl
ceb0: 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a , sizeof(char*)*
cec0: 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a pIdx->nColumn);.
ced0: 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 20 3d pIdx->azColl =
cee0: 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 7a (const char**)z
cef0: 45 78 74 72 61 3b 0a 20 20 7a 45 78 74 72 61 20 Extra;. zExtra
cf00: 2b 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 += sizeof(char*)
cf10: 2a 4e 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 45 78 *N;. memcpy(zEx
cf20: 74 72 61 2c 20 70 49 64 78 2d 3e 61 69 43 6f 6c tra, pIdx->aiCol
cf30: 75 6d 6e 2c 20 73 69 7a 65 6f 66 28 69 31 36 29 umn, sizeof(i16)
cf40: 2a 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b *pIdx->nColumn);
cf50: 0a 20 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d . pIdx->aiColum
cf60: 6e 20 3d 20 28 69 31 36 2a 29 7a 45 78 74 72 61 n = (i16*)zExtra
cf70: 3b 0a 20 20 7a 45 78 74 72 61 20 2b 3d 20 73 69 ;. zExtra += si
cf80: 7a 65 6f 66 28 69 31 36 29 2a 4e 3b 0a 20 20 6d zeof(i16)*N;. m
cf90: 65 6d 63 70 79 28 7a 45 78 74 72 61 2c 20 70 49 emcpy(zExtra, pI
cfa0: 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 dx->aSortOrder,
cfb0: 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a pIdx->nColumn);.
cfc0: 20 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 pIdx->aSortOrd
cfd0: 65 72 20 3d 20 28 75 38 2a 29 7a 45 78 74 72 61 er = (u8*)zExtra
cfe0: 3b 0a 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d ;. pIdx->nColum
cff0: 6e 20 3d 20 4e 3b 0a 20 20 70 49 64 78 2d 3e 69 n = N;. pIdx->i
d000: 73 52 65 73 69 7a 65 64 20 3d 20 31 3b 0a 20 20 sResized = 1;.
d010: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d020: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 73 74 69 6d ;.}../*.** Estim
d030: 61 74 65 20 74 68 65 20 74 6f 74 61 6c 20 72 6f ate the total ro
d040: 77 20 77 69 64 74 68 20 66 6f 72 20 61 20 74 61 w width for a ta
d050: 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ble..*/.static v
d060: 6f 69 64 20 65 73 74 69 6d 61 74 65 54 61 62 6c oid estimateTabl
d070: 65 57 69 64 74 68 28 54 61 62 6c 65 20 2a 70 54 eWidth(Table *pT
d080: 61 62 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 ab){. unsigned
d090: 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 63 6f wTable = 0;. co
d0a0: 6e 73 74 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 nst Column *pTab
d0b0: 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 Col;. int i;.
d0c0: 66 6f 72 28 69 3d 70 54 61 62 2d 3e 6e 43 6f 6c for(i=pTab->nCol
d0d0: 2c 20 70 54 61 62 43 6f 6c 3d 70 54 61 62 2d 3e , pTabCol=pTab->
d0e0: 61 43 6f 6c 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 aCol; i>0; i--,
d0f0: 70 54 61 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 pTabCol++){.
d100: 77 54 61 62 6c 65 20 2b 3d 20 70 54 61 62 43 6f wTable += pTabCo
d110: 6c 2d 3e 73 7a 45 73 74 3b 0a 20 20 7d 0a 20 20 l->szEst;. }.
d120: 69 66 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c if( pTab->iPKey<
d130: 30 20 29 20 77 54 61 62 6c 65 2b 2b 3b 0a 20 20 0 ) wTable++;.
d140: 70 54 61 62 2d 3e 73 7a 54 61 62 52 6f 77 20 3d pTab->szTabRow =
d150: 20 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 77 sqlite3LogEst(w
d160: 54 61 62 6c 65 2a 34 29 3b 0a 7d 0a 0a 2f 2a 0a Table*4);.}../*.
d170: 2a 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20 ** Estimate the
d180: 61 76 65 72 61 67 65 20 73 69 7a 65 20 6f 66 20 average size of
d190: 61 20 72 6f 77 20 66 6f 72 20 61 6e 20 69 6e 64 a row for an ind
d1a0: 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ex..*/.static vo
d1b0: 69 64 20 65 73 74 69 6d 61 74 65 49 6e 64 65 78 id estimateIndex
d1c0: 57 69 64 74 68 28 49 6e 64 65 78 20 2a 70 49 64 Width(Index *pId
d1d0: 78 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 77 x){. unsigned w
d1e0: 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 69 6e 74 Index = 0;. int
d1f0: 20 69 3b 0a 20 20 63 6f 6e 73 74 20 43 6f 6c 75 i;. const Colu
d200: 6d 6e 20 2a 61 43 6f 6c 20 3d 20 70 49 64 78 2d mn *aCol = pIdx-
d210: 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 >pTable->aCol;.
d220: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 for(i=0; i<pIdx
d230: 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b ->nColumn; i++){
d240: 0a 20 20 20 20 69 31 36 20 78 20 3d 20 70 49 64 . i16 x = pId
d250: 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a x->aiColumn[i];.
d260: 20 20 20 20 61 73 73 65 72 74 28 20 78 3c 70 49 assert( x<pI
d270: 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c dx->pTable->nCol
d280: 20 29 3b 0a 20 20 20 20 77 49 6e 64 65 78 20 2b );. wIndex +
d290: 3d 20 78 3c 30 20 3f 20 31 20 3a 20 61 43 6f 6c = x<0 ? 1 : aCol
d2a0: 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b [pIdx->aiColumn[
d2b0: 69 5d 5d 2e 73 7a 45 73 74 3b 0a 20 20 7d 0a 20 i]].szEst;. }.
d2c0: 20 70 49 64 78 2d 3e 73 7a 49 64 78 52 6f 77 20 pIdx->szIdxRow
d2d0: 3d 20 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 = sqlite3LogEst(
d2e0: 77 49 6e 64 65 78 2a 34 29 3b 0a 7d 0a 0a 2f 2a wIndex*4);.}../*
d2f0: 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
d300: 76 61 6c 75 65 20 78 20 69 73 20 66 6f 75 6e 64 value x is found
d310: 20 61 6e 79 20 6f 66 20 74 68 65 20 66 69 72 73 any of the firs
d320: 74 20 6e 43 6f 6c 20 65 6e 74 72 69 65 73 20 6f t nCol entries o
d330: 66 20 61 69 43 6f 6c 5b 5d 0a 2a 2f 0a 73 74 61 f aiCol[].*/.sta
d340: 74 69 63 20 69 6e 74 20 68 61 73 43 6f 6c 75 6d tic int hasColum
d350: 6e 28 63 6f 6e 73 74 20 69 31 36 20 2a 61 69 43 n(const i16 *aiC
d360: 6f 6c 2c 20 69 6e 74 20 6e 43 6f 6c 2c 20 69 6e ol, int nCol, in
d370: 74 20 78 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e t x){. while( n
d380: 43 6f 6c 2d 2d 20 3e 20 30 20 29 20 69 66 28 20 Col-- > 0 ) if(
d390: 78 3d 3d 2a 28 61 69 43 6f 6c 2b 2b 29 20 29 20 x==*(aiCol++) )
d3a0: 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 return 1;. retu
d3b0: 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn 0;.}../*.** T
d3c0: 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 his routine runs
d3d0: 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 70 at the end of p
d3e0: 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 arsing a CREATE
d3f0: 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 TABLE statement
d400: 74 68 61 74 0a 2a 2a 20 68 61 73 20 61 20 57 49 that.** has a WI
d410: 54 48 4f 55 54 20 52 4f 57 49 44 20 63 6c 61 75 THOUT ROWID clau
d420: 73 65 2e 20 20 54 68 65 20 6a 6f 62 20 6f 66 20 se. The job of
d430: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
d440: 74 6f 20 63 6f 6e 76 65 72 74 20 62 6f 74 68 0a to convert both.
d450: 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 ** internal sche
d460: 6d 61 20 64 61 74 61 20 73 74 72 75 63 74 75 72 ma data structur
d470: 65 73 20 61 6e 64 20 74 68 65 20 67 65 6e 65 72 es and the gener
d480: 61 74 65 64 20 56 44 42 45 20 63 6f 64 65 20 73 ated VDBE code s
d490: 6f 20 74 68 61 74 20 74 68 65 79 0a 2a 2a 20 61 o that they.** a
d4a0: 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 re appropriate f
d4b0: 6f 72 20 61 20 57 49 54 48 4f 55 54 20 52 4f 57 or a WITHOUT ROW
d4c0: 49 44 20 74 61 62 6c 65 20 69 6e 73 74 65 61 64 ID table instead
d4d0: 20 6f 66 20 61 20 72 6f 77 69 64 20 74 61 62 6c of a rowid tabl
d4e0: 65 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 69 6e e..** Changes in
d4f0: 63 6c 75 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 clude:.**.**
d500: 20 28 31 29 20 20 53 65 74 20 61 6c 6c 20 63 6f (1) Set all co
d510: 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 50 52 49 lumns of the PRI
d520: 4d 41 52 59 20 4b 45 59 20 73 63 68 65 6d 61 20 MARY KEY schema
d530: 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 4e 4f 54 object to be NOT
d540: 20 4e 55 4c 4c 2e 0a 2a 2a 20 20 20 20 20 28 32 NULL..** (2
d550: 29 20 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4f ) Convert the O
d560: 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 69 6e P_CreateTable in
d570: 74 6f 20 61 6e 20 4f 50 5f 43 72 65 61 74 65 49 to an OP_CreateI
d580: 6e 64 65 78 2e 20 20 54 68 65 72 65 20 69 73 0a ndex. There is.
d590: 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 6f 20 72 ** no r
d5a0: 6f 77 69 64 20 62 74 72 65 65 20 66 6f 72 20 61 owid btree for a
d5b0: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 2e 20 WITHOUT ROWID.
d5c0: 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 63 61 Instead, the ca
d5d0: 6e 6f 6e 69 63 61 6c 0a 2a 2a 20 20 20 20 20 20 nonical.**
d5e0: 20 20 20 20 64 61 74 61 20 73 74 6f 72 61 67 65 data storage
d5f0: 20 69 73 20 61 20 63 6f 76 65 72 69 6e 67 20 69 is a covering i
d600: 6e 64 65 78 20 62 74 72 65 65 2e 0a 2a 2a 20 20 ndex btree..**
d610: 20 20 20 28 33 29 20 20 42 79 70 61 73 73 20 74 (3) Bypass t
d620: 68 65 20 63 72 65 61 74 69 6f 6e 20 6f 66 20 74 he creation of t
d630: 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 he sqlite_master
d640: 20 74 61 62 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 table entry.**
d650: 20 20 20 20 20 20 20 20 20 66 6f 72 20 74 68 65 for the
d660: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 61 73 20 PRIMARY KEY as
d670: 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 the primary key
d680: 69 6e 64 65 78 20 69 73 20 6e 6f 77 0a 2a 2a 20 index is now.**
d690: 20 20 20 20 20 20 20 20 20 69 64 65 6e 74 69 66 identif
d6a0: 69 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 ied by the sqlit
d6b0: 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 65 e_master table e
d6c0: 6e 74 72 79 20 6f 66 20 74 68 65 20 74 61 62 6c ntry of the tabl
d6d0: 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 20 20 20 e itself..**
d6e0: 20 28 34 29 20 20 53 65 74 20 74 68 65 20 49 6e (4) Set the In
d6f0: 64 65 78 2e 74 6e 75 6d 20 6f 66 20 74 68 65 20 dex.tnum of the
d700: 50 52 49 4d 41 52 59 20 4b 45 59 20 49 6e 64 65 PRIMARY KEY Inde
d710: 78 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 0a x object in the.
d720: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 63 68 65 ** sche
d730: 6d 61 20 74 6f 20 74 68 65 20 72 6f 6f 74 70 61 ma to the rootpa
d740: 67 65 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ge from the main
d750: 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 28 table..** (
d760: 35 29 20 20 41 64 64 20 61 6c 6c 20 74 61 62 6c 5) Add all tabl
d770: 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 74 68 65 e columns to the
d780: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 49 6e 64 PRIMARY KEY Ind
d790: 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 20 20 20 ex object.**
d7a0: 20 20 20 20 20 20 73 6f 20 74 68 61 74 20 74 68 so that th
d7b0: 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 e PRIMARY KEY is
d7c0: 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 a covering inde
d7d0: 78 2e 20 20 54 68 65 20 73 75 72 70 6c 75 73 0a x. The surplus.
d7e0: 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75 ** colu
d7f0: 6d 6e 73 20 61 72 65 20 70 61 72 74 20 6f 66 20 mns are part of
d800: 4b 65 79 49 6e 66 6f 2e 6e 58 46 69 65 6c 64 20 KeyInfo.nXField
d810: 61 6e 64 20 61 72 65 20 6e 6f 74 20 75 73 65 64 and are not used
d820: 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 for.**
d830: 20 73 6f 72 74 69 6e 67 20 6f 72 20 6c 6f 6f 6b sorting or look
d840: 75 70 20 6f 72 20 75 6e 69 71 75 65 6e 65 73 73 up or uniqueness
d850: 20 63 68 65 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 checks..**
d860: 28 36 29 20 20 52 65 70 6c 61 63 65 20 74 68 65 (6) Replace the
d870: 20 72 6f 77 69 64 20 74 61 69 6c 20 6f 6e 20 61 rowid tail on a
d880: 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ll automatically
d890: 20 67 65 6e 65 72 61 74 65 64 20 55 4e 49 51 55 generated UNIQU
d8a0: 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e E.** in
d8b0: 64 69 63 65 73 20 77 69 74 68 20 74 68 65 20 50 dices with the P
d8c0: 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d RIMARY KEY colum
d8d0: 6e 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 76 69 ns..**.** For vi
d8e0: 72 74 75 61 6c 20 74 61 62 6c 65 73 2c 20 6f 6e rtual tables, on
d8f0: 6c 79 20 28 31 29 20 69 73 20 70 65 72 66 6f 72 ly (1) is perfor
d900: 6d 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 med..*/.static v
d910: 6f 69 64 20 63 6f 6e 76 65 72 74 54 6f 57 69 74 oid convertToWit
d920: 68 6f 75 74 52 6f 77 69 64 54 61 62 6c 65 28 50 houtRowidTable(P
d930: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 arse *pParse, Ta
d940: 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 49 6e ble *pTab){. In
d950: 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 49 6e 64 dex *pIdx;. Ind
d960: 65 78 20 2a 70 50 6b 3b 0a 20 20 69 6e 74 20 6e ex *pPk;. int n
d970: 50 6b 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a Pk;. int i, j;.
d980: 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
d990: 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56 64 pParse->db;. Vd
d9a0: 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e be *v = pParse->
d9b0: 70 56 64 62 65 3b 0a 0a 20 20 2f 2a 20 4d 61 72 pVdbe;.. /* Mar
d9c0: 6b 20 65 76 65 72 79 20 50 52 49 4d 41 52 59 20 k every PRIMARY
d9d0: 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 73 20 4e 4f KEY column as NO
d9e0: 54 20 4e 55 4c 4c 20 28 65 78 63 65 70 74 20 66 T NULL (except f
d9f0: 6f 72 20 69 6d 70 6f 73 74 65 72 20 74 61 62 6c or imposter tabl
da00: 65 73 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 es). */. if( !
da10: 64 62 2d 3e 69 6e 69 74 2e 69 6d 70 6f 73 74 65 db->init.imposte
da20: 72 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 66 6f rTable ){. fo
da30: 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e r(i=0; i<pTab->n
da40: 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Col; i++){.
da50: 20 69 66 28 20 28 70 54 61 62 2d 3e 61 43 6f 6c if( (pTab->aCol
da60: 5b 69 5d 2e 63 6f 6c 46 6c 61 67 73 20 26 20 43 [i].colFlags & C
da70: 4f 4c 46 4c 41 47 5f 50 52 49 4d 4b 45 59 29 21 OLFLAG_PRIMKEY)!
da80: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 =0 ){. pT
da90: 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e ab->aCol[i].notN
daa0: 75 6c 6c 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a ull = OE_Abort;.
dab0: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
dac0: 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 72 65 6d 61 }.. /* The rema
dad0: 69 6e 69 6e 67 20 74 72 61 6e 73 66 6f 72 6d 61 ining transforma
dae0: 74 69 6f 6e 73 20 6f 6e 6c 79 20 61 70 70 6c 79 tions only apply
daf0: 20 74 6f 20 62 2d 74 72 65 65 20 74 61 62 6c 65 to b-tree table
db00: 73 2c 20 6e 6f 74 20 74 6f 0a 20 20 2a 2a 20 76 s, not to. ** v
db10: 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 2a 2f irtual tables */
db20: 0a 20 20 69 66 28 20 49 4e 5f 44 45 43 4c 41 52 . if( IN_DECLAR
db30: 45 5f 56 54 41 42 20 29 20 72 65 74 75 72 6e 3b E_VTAB ) return;
db40: 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 .. /* Convert t
db50: 68 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c he OP_CreateTabl
db60: 65 20 6f 70 63 6f 64 65 20 74 68 61 74 20 77 6f e opcode that wo
db70: 75 6c 64 20 6e 6f 72 6d 61 6c 6c 79 20 63 72 65 uld normally cre
db80: 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 72 6f 6f ate the. ** roo
db90: 74 2d 70 61 67 65 20 66 6f 72 20 74 68 65 20 74 t-page for the t
dba0: 61 62 6c 65 20 69 6e 74 6f 20 61 6e 20 4f 50 5f able into an OP_
dbb0: 43 72 65 61 74 65 49 6e 64 65 78 20 6f 70 63 6f CreateIndex opco
dbc0: 64 65 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 20 de. The index.
dbd0: 20 2a 2a 20 63 72 65 61 74 65 64 20 77 69 6c 6c ** created will
dbe0: 20 62 65 63 6f 6d 65 20 74 68 65 20 50 52 49 4d become the PRIM
dbf0: 41 52 59 20 4b 45 59 20 69 6e 64 65 78 2e 0a 20 ARY KEY index..
dc00: 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 */. if( pParse
dc10: 2d 3e 61 64 64 72 43 72 54 61 62 20 29 7b 0a 20 ->addrCrTab ){.
dc20: 20 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a assert( v );.
dc30: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
dc40: 68 61 6e 67 65 4f 70 63 6f 64 65 28 76 2c 20 70 hangeOpcode(v, p
dc50: 50 61 72 73 65 2d 3e 61 64 64 72 43 72 54 61 62 Parse->addrCrTab
dc60: 2c 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 , OP_CreateIndex
dc70: 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 );. }.. /* Loc
dc80: 61 74 65 20 74 68 65 20 50 52 49 4d 41 52 59 20 ate the PRIMARY
dc90: 4b 45 59 20 69 6e 64 65 78 2e 20 20 4f 72 2c 20 KEY index. Or,
dca0: 69 66 20 74 68 69 73 20 74 61 62 6c 65 20 77 61 if this table wa
dcb0: 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a s originally. *
dcc0: 2a 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 * an INTEGER PRI
dcd0: 4d 41 52 59 20 4b 45 59 20 74 61 62 6c 65 2c 20 MARY KEY table,
dce0: 63 72 65 61 74 65 20 61 20 6e 65 77 20 50 52 49 create a new PRI
dcf0: 4d 41 52 59 20 4b 45 59 20 69 6e 64 65 78 2e 20 MARY KEY index.
dd00: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 . */. if( pTab
dd10: 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 ->iPKey>=0 ){.
dd20: 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 ExprList *pLis
dd30: 74 3b 0a 20 20 20 20 54 6f 6b 65 6e 20 69 70 6b t;. Token ipk
dd40: 54 6f 6b 65 6e 3b 0a 20 20 20 20 73 71 6c 69 74 Token;. sqlit
dd50: 65 33 54 6f 6b 65 6e 49 6e 69 74 28 26 69 70 6b e3TokenInit(&ipk
dd60: 54 6f 6b 65 6e 2c 20 70 54 61 62 2d 3e 61 43 6f Token, pTab->aCo
dd70: 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a l[pTab->iPKey].z
dd80: 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4c 69 73 74 Name);. pList
dd90: 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 = sqlite3ExprLi
dda0: 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c stAppend(pParse,
ddb0: 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 0, .
ddc0: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
ddd0: 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 49 prAlloc(db, TK_I
dde0: 44 2c 20 26 69 70 6b 54 6f 6b 65 6e 2c 20 30 29 D, &ipkToken, 0)
ddf0: 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 );. if( pList
de00: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
de10: 20 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f pList->a[0].so
de20: 72 74 4f 72 64 65 72 20 3d 20 70 50 61 72 73 65 rtOrder = pParse
de30: 2d 3e 69 50 6b 53 6f 72 74 4f 72 64 65 72 3b 0a ->iPkSortOrder;.
de40: 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 assert( pPar
de50: 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 70 se->pNewTable==p
de60: 54 61 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 Tab );. sqlit
de70: 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 e3CreateIndex(pP
de80: 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 70 arse, 0, 0, 0, p
de90: 4c 69 73 74 2c 20 70 54 61 62 2d 3e 6b 65 79 43 List, pTab->keyC
dea0: 6f 6e 66 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c onf, 0, 0, 0, 0,
deb0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
dec0: 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 SQLITE_I
ded0: 44 58 54 59 50 45 5f 50 52 49 4d 41 52 59 4b 45 DXTYPE_PRIMARYKE
dee0: 59 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e Y);. if( db->
def0: 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 mallocFailed ) r
df00: 65 74 75 72 6e 3b 0a 20 20 20 20 70 50 6b 20 3d eturn;. pPk =
df10: 20 73 71 6c 69 74 65 33 50 72 69 6d 61 72 79 4b sqlite3PrimaryK
df20: 65 79 49 6e 64 65 78 28 70 54 61 62 29 3b 0a 20 eyIndex(pTab);.
df30: 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d pTab->iPKey =
df40: 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 -1;. }else{.
df50: 20 20 70 50 6b 20 3d 20 73 71 6c 69 74 65 33 50 pPk = sqlite3P
df60: 72 69 6d 61 72 79 4b 65 79 49 6e 64 65 78 28 70 rimaryKeyIndex(p
df70: 54 61 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 42 79 Tab);.. /* By
df80: 70 61 73 73 20 74 68 65 20 63 72 65 61 74 69 6f pass the creatio
df90: 6e 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 n of the PRIMARY
dfa0: 20 4b 45 59 20 62 74 72 65 65 20 61 6e 64 20 74 KEY btree and t
dfb0: 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 he sqlite_master
dfc0: 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 65 6e . ** table en
dfd0: 74 72 79 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c try. This is onl
dfe0: 79 20 72 65 71 75 69 72 65 64 20 69 66 20 63 75 y required if cu
dff0: 72 72 65 6e 74 6c 79 20 67 65 6e 65 72 61 74 69 rrently generati
e000: 6e 67 20 56 44 42 45 0a 20 20 20 20 2a 2a 20 63 ng VDBE. ** c
e010: 6f 64 65 20 66 6f 72 20 61 20 43 52 45 41 54 45 ode for a CREATE
e020: 20 54 41 42 4c 45 20 28 6e 6f 74 20 77 68 65 6e TABLE (not when
e030: 20 70 61 72 73 69 6e 67 20 6f 6e 65 20 61 73 20 parsing one as
e040: 70 61 72 74 20 6f 66 20 72 65 61 64 69 6e 67 0a part of reading.
e050: 20 20 20 20 2a 2a 20 61 20 64 61 74 61 62 61 73 ** a databas
e060: 65 20 73 63 68 65 6d 61 29 2e 20 20 2a 2f 0a 20 e schema). */.
e070: 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 if( v ){.
e080: 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e assert( db->in
e090: 69 74 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 it.busy==0 );.
e0a0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
e0b0: 68 61 6e 67 65 4f 70 63 6f 64 65 28 76 2c 20 70 hangeOpcode(v, p
e0c0: 50 6b 2d 3e 74 6e 75 6d 2c 20 4f 50 5f 47 6f 74 Pk->tnum, OP_Got
e0d0: 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f o);. }.. /
e0e0: 2a 0a 20 20 20 20 2a 2a 20 52 65 6d 6f 76 65 20 *. ** Remove
e0f0: 61 6c 6c 20 72 65 64 75 6e 64 61 6e 74 20 63 6f all redundant co
e100: 6c 75 6d 6e 73 20 66 72 6f 6d 20 74 68 65 20 50 lumns from the P
e110: 52 49 4d 41 52 59 20 4b 45 59 2e 20 20 46 6f 72 RIMARY KEY. For
e120: 20 65 78 61 6d 70 6c 65 2c 20 63 68 61 6e 67 65 example, change
e130: 0a 20 20 20 20 2a 2a 20 22 50 52 49 4d 41 52 59 . ** "PRIMARY
e140: 20 4b 45 59 28 61 2c 62 2c 61 2c 62 2c 63 2c 62 KEY(a,b,a,b,c,b
e150: 2c 63 2c 64 29 22 20 69 6e 74 6f 20 6a 75 73 74 ,c,d)" into just
e160: 20 22 50 52 49 4d 41 52 59 20 4b 45 59 28 61 2c "PRIMARY KEY(a,
e170: 62 2c 63 2c 64 29 22 2e 20 20 4c 61 74 65 72 0a b,c,d)". Later.
e180: 20 20 20 20 2a 2a 20 63 6f 64 65 20 61 73 73 75 ** code assu
e190: 6d 65 73 20 74 68 65 20 50 52 49 4d 41 52 59 20 mes the PRIMARY
e1a0: 4b 45 59 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 KEY contains no
e1b0: 72 65 70 65 61 74 65 64 20 63 6f 6c 75 6d 6e 73 repeated columns
e1c0: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
e1d0: 28 69 3d 6a 3d 31 3b 20 69 3c 70 50 6b 2d 3e 6e (i=j=1; i<pPk->n
e1e0: 4b 65 79 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 KeyCol; i++){.
e1f0: 20 20 20 20 69 66 28 20 68 61 73 43 6f 6c 75 6d if( hasColum
e200: 6e 28 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 2c n(pPk->aiColumn,
e210: 20 6a 2c 20 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d j, pPk->aiColum
e220: 6e 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 n[i]) ){.
e230: 20 70 50 6b 2d 3e 6e 43 6f 6c 75 6d 6e 2d 2d 3b pPk->nColumn--;
e240: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
e250: 20 20 20 20 20 20 70 50 6b 2d 3e 61 69 43 6f 6c pPk->aiCol
e260: 75 6d 6e 5b 6a 2b 2b 5d 20 3d 20 70 50 6b 2d 3e umn[j++] = pPk->
e270: 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 aiColumn[i];.
e280: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 }. }. p
e290: 50 6b 2d 3e 6e 4b 65 79 43 6f 6c 20 3d 20 6a 3b Pk->nKeyCol = j;
e2a0: 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
e2b0: 50 6b 21 3d 30 20 29 3b 0a 20 20 70 50 6b 2d 3e Pk!=0 );. pPk->
e2c0: 69 73 43 6f 76 65 72 69 6e 67 20 3d 20 31 3b 0a isCovering = 1;.
e2d0: 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e if( !db->init.
e2e0: 69 6d 70 6f 73 74 65 72 54 61 62 6c 65 20 29 20 imposterTable )
e2f0: 70 50 6b 2d 3e 75 6e 69 71 4e 6f 74 4e 75 6c 6c pPk->uniqNotNull
e300: 20 3d 20 31 3b 0a 20 20 6e 50 6b 20 3d 20 70 50 = 1;. nPk = pP
e310: 6b 2d 3e 6e 4b 65 79 43 6f 6c 3b 0a 0a 20 20 2f k->nKeyCol;.. /
e320: 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 * The root page
e330: 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b of the PRIMARY K
e340: 45 59 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 EY is the table
e350: 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 70 root page */. p
e360: 50 6b 2d 3e 74 6e 75 6d 20 3d 20 70 54 61 62 2d Pk->tnum = pTab-
e370: 3e 74 6e 75 6d 3b 0a 0a 20 20 2f 2a 20 55 70 64 >tnum;.. /* Upd
e380: 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 ate the in-memor
e390: 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e y representation
e3a0: 20 6f 66 20 61 6c 6c 20 55 4e 49 51 55 45 20 69 of all UNIQUE i
e3b0: 6e 64 69 63 65 73 20 62 79 20 63 6f 6e 76 65 72 ndices by conver
e3c0: 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 69 ting. ** the fi
e3d0: 6e 61 6c 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e nal rowid column
e3e0: 20 69 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 into one or mor
e3f0: 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 e columns of the
e400: 20 50 52 49 4d 41 52 59 20 4b 45 59 2e 0a 20 20 PRIMARY KEY..
e410: 2a 2f 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 */. for(pIdx=pT
e420: 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 ab->pIndex; pIdx
e430: 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 ; pIdx=pIdx->pNe
e440: 78 74 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a xt){. int n;.
e450: 20 20 20 20 69 66 28 20 49 73 50 72 69 6d 61 72 if( IsPrimar
e460: 79 4b 65 79 49 6e 64 65 78 28 70 49 64 78 29 20 yKeyIndex(pIdx)
e470: 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
e480: 66 6f 72 28 69 3d 6e 3d 30 3b 20 69 3c 6e 50 6b for(i=n=0; i<nPk
e490: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
e4a0: 28 20 21 68 61 73 43 6f 6c 75 6d 6e 28 70 49 64 ( !hasColumn(pId
e4b0: 78 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 70 49 64 x->aiColumn, pId
e4c0: 78 2d 3e 6e 4b 65 79 43 6f 6c 2c 20 70 50 6b 2d x->nKeyCol, pPk-
e4d0: 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 29 20 29 20 >aiColumn[i]) )
e4e0: 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 n++;. }. i
e4f0: 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 f( n==0 ){.
e500: 20 2f 2a 20 54 68 69 73 20 69 6e 64 65 78 20 69 /* This index i
e510: 73 20 61 20 73 75 70 65 72 73 65 74 20 6f 66 20 s a superset of
e520: 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 the primary key
e530: 2a 2f 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 6e */. pIdx->n
e540: 43 6f 6c 75 6d 6e 20 3d 20 70 49 64 78 2d 3e 6e Column = pIdx->n
e550: 4b 65 79 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f KeyCol;. co
e560: 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 ntinue;. }.
e570: 20 20 69 66 28 20 72 65 73 69 7a 65 49 6e 64 65 if( resizeInde
e580: 78 4f 62 6a 65 63 74 28 64 62 2c 20 70 49 64 78 xObject(db, pIdx
e590: 2c 20 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 2b , pIdx->nKeyCol+
e5a0: 6e 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 n) ) return;.
e5b0: 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 70 49 64 78 for(i=0, j=pIdx
e5c0: 2d 3e 6e 4b 65 79 43 6f 6c 3b 20 69 3c 6e 50 6b ->nKeyCol; i<nPk
e5d0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
e5e0: 28 20 21 68 61 73 43 6f 6c 75 6d 6e 28 70 49 64 ( !hasColumn(pId
e5f0: 78 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 70 49 64 x->aiColumn, pId
e600: 78 2d 3e 6e 4b 65 79 43 6f 6c 2c 20 70 50 6b 2d x->nKeyCol, pPk-
e610: 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 29 20 29 7b >aiColumn[i]) ){
e620: 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61 . pIdx->a
e630: 69 43 6f 6c 75 6d 6e 5b 6a 5d 20 3d 20 70 50 6b iColumn[j] = pPk
e640: 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 ->aiColumn[i];.
e650: 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 pIdx->azC
e660: 6f 6c 6c 5b 6a 5d 20 3d 20 70 50 6b 2d 3e 61 7a oll[j] = pPk->az
e670: 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 Coll[i];.
e680: 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 j++;. }.
e690: 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
e6a0: 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 70 pIdx->nColumn>=p
e6b0: 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 2b 6e 20 29 Idx->nKeyCol+n )
e6c0: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 ;. assert( pI
e6d0: 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 dx->nColumn>=j )
e6e0: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 ;. }.. /* Add
e6f0: 61 6c 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e all table column
e700: 73 20 74 6f 20 74 68 65 20 50 52 49 4d 41 52 59 s to the PRIMARY
e710: 20 4b 45 59 20 69 6e 64 65 78 0a 20 20 2a 2f 0a KEY index. */.
e720: 20 20 69 66 28 20 6e 50 6b 3c 70 54 61 62 2d 3e if( nPk<pTab->
e730: 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 nCol ){. if(
e740: 72 65 73 69 7a 65 49 6e 64 65 78 4f 62 6a 65 63 resizeIndexObjec
e750: 74 28 64 62 2c 20 70 50 6b 2c 20 70 54 61 62 2d t(db, pPk, pTab-
e760: 3e 6e 43 6f 6c 29 20 29 20 72 65 74 75 72 6e 3b >nCol) ) return;
e770: 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d . for(i=0, j=
e780: 6e 50 6b 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f nPk; i<pTab->nCo
e790: 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 l; i++){. i
e7a0: 66 28 20 21 68 61 73 43 6f 6c 75 6d 6e 28 70 50 f( !hasColumn(pP
e7b0: 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 6a 2c 20 k->aiColumn, j,
e7c0: 69 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 i) ){. as
e7d0: 73 65 72 74 28 20 6a 3c 70 50 6b 2d 3e 6e 43 6f sert( j<pPk->nCo
e7e0: 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 lumn );.
e7f0: 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d pPk->aiColumn[j]
e800: 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 70 50 = i;. pP
e810: 6b 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d 20 3d 20 73 k->azColl[j] = s
e820: 71 6c 69 74 65 33 53 74 72 42 49 4e 41 52 59 3b qlite3StrBINARY;
e830: 0a 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 . j++;.
e840: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
e850: 61 73 73 65 72 74 28 20 70 50 6b 2d 3e 6e 43 6f assert( pPk->nCo
e860: 6c 75 6d 6e 3d 3d 6a 20 29 3b 0a 20 20 20 20 61 lumn==j );. a
e870: 73 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f ssert( pTab->nCo
e880: 6c 3d 3d 6a 20 29 3b 0a 20 20 7d 65 6c 73 65 7b l==j );. }else{
e890: 0a 20 20 20 20 70 50 6b 2d 3e 6e 43 6f 6c 75 6d . pPk->nColum
e8a0: 6e 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a n = pTab->nCol;.
e8b0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
e8c0: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
e8d0: 6c 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68 led to report th
e8e0: 65 20 66 69 6e 61 6c 20 22 29 22 20 74 68 61 74 e final ")" that
e8f0: 20 74 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 terminates.** a
e900: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
e910: 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 atement..**.** T
e920: 68 65 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 he table structu
e930: 72 65 20 74 68 61 74 20 6f 74 68 65 72 20 61 63 re that other ac
e940: 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 tion routines ha
e950: 76 65 20 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 ve been building
e960: 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 .** is added to
e970: 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 the internal has
e980: 68 20 74 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 h tables, assumi
e990: 6e 67 20 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 ng no errors hav
e9a0: 65 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a e.** occurred..*
e9b0: 2a 0a 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f *.** An entry fo
e9c0: 72 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6d r the table is m
e9d0: 61 64 65 20 69 6e 20 74 68 65 20 6d 61 73 74 65 ade in the maste
e9e0: 72 20 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c r table on disk,
e9f0: 20 75 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 unless.** this
ea00: 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 is a temporary t
ea10: 61 62 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 able or db->init
ea20: 2e 62 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 .busy==1. When
ea30: 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 db->init.busy==1
ea40: 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 77 65 20 .** it means we
ea50: 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 are reading the
ea60: 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
ea70: 62 6c 65 20 62 65 63 61 75 73 65 20 77 65 20 6a ble because we j
ea80: 75 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 ust.** connected
ea90: 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
eaa0: 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20 or because the
eab0: 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
eac0: 62 6c 65 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e ble has.** recen
ead0: 74 6c 79 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 tly changed, so
eae0: 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 the entry for th
eaf0: 69 73 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 is table already
eb00: 20 65 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 exists in.** th
eb10: 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 e sqlite_master
eb20: 74 61 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f table. We do no
eb30: 74 20 77 61 6e 74 20 74 6f 20 63 72 65 61 74 65 t want to create
eb40: 20 69 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a it again..**.**
eb50: 20 49 66 20 74 68 65 20 70 53 65 6c 65 63 74 20 If the pSelect
eb60: 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 argument is not
eb70: 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 NULL, it means t
eb80: 68 61 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hat this routine
eb90: 0a 2a 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 74 .** was called t
eba0: 6f 20 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 o create a table
ebb0: 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 generated from
ebc0: 61 20 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 41 a .** "CREATE TA
ebd0: 42 4c 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 BLE ... AS SELEC
ebe0: 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 T ..." statement
ebf0: 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 . The column na
ec00: 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 mes of.** the ne
ec10: 77 20 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 w table will mat
ec20: 63 68 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 ch the result se
ec30: 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e t of the SELECT.
ec40: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
ec50: 45 6e 64 54 61 62 6c 65 28 0a 20 20 50 61 72 73 EndTable(. Pars
ec60: 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
ec70: 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e /* Parse con
ec80: 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 text */. Token
ec90: 2a 70 43 6f 6e 73 2c 20 20 20 20 20 20 20 20 20 *pCons,
eca0: 20 20 2f 2a 20 54 68 65 20 27 2c 27 20 74 6f 6b /* The ',' tok
ecb0: 65 6e 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 en after the las
ecc0: 74 20 63 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a t column defn. *
ecd0: 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c /. Token *pEnd,
ece0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
ecf0: 68 65 20 27 29 27 20 62 65 66 6f 72 65 20 6f 70 he ')' before op
ed00: 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 43 52 45 tions in the CRE
ed10: 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20 75 ATE TABLE */. u
ed20: 38 20 74 61 62 4f 70 74 73 2c 20 20 20 20 20 20 8 tabOpts,
ed30: 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 /* Extra
ed40: 74 61 62 6c 65 20 6f 70 74 69 6f 6e 73 2e 20 55 table options. U
ed50: 73 75 61 6c 6c 79 20 30 2e 20 2a 2f 0a 20 20 53 sually 0. */. S
ed60: 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 elect *pSelect
ed70: 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 /* Select
ed80: 20 66 72 6f 6d 20 61 20 22 43 52 45 41 54 45 20 from a "CREATE
ed90: 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 22 20 2a ... AS SELECT" *
eda0: 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b /.){. Table *p;
edb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
edc0: 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c /* The new tabl
edd0: 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a e */. sqlite3 *
ede0: 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
edf0: 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
ee00: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
ee10: 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
ee20: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
ee30: 74 61 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 tabase in which
ee40: 74 68 65 20 74 61 62 6c 65 20 6c 69 76 65 73 20 the table lives
ee50: 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 */. Index *pIdx
ee60: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
ee70: 2a 20 41 6e 20 69 6d 70 6c 69 65 64 20 69 6e 64 * An implied ind
ee80: 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 ex of the table
ee90: 2a 2f 0a 0a 20 20 69 66 28 20 70 45 6e 64 3d 3d */.. if( pEnd==
eea0: 30 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 20 0 && pSelect==0
eeb0: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 ){. return;.
eec0: 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 }. assert( !db
eed0: 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
eee0: 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e ;. p = pParse->
eef0: 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 pNewTable;. if(
ef00: 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a p==0 ) return;.
ef10: 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e . assert( !db->
ef20: 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 21 70 53 init.busy || !pS
ef30: 65 6c 65 63 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 elect );.. /* I
ef40: 66 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 f the db->init.b
ef50: 75 73 79 20 69 73 20 31 20 69 74 20 6d 65 61 6e usy is 1 it mean
ef60: 73 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 s we are reading
ef70: 20 74 68 65 20 53 51 4c 20 6f 66 66 20 74 68 65 the SQL off the
ef80: 0a 20 20 2a 2a 20 22 73 71 6c 69 74 65 5f 6d 61 . ** "sqlite_ma
ef90: 73 74 65 72 22 20 6f 72 20 22 73 71 6c 69 74 65 ster" or "sqlite
efa0: 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 20 74 61 _temp_master" ta
efb0: 62 6c 65 20 6f 6e 20 74 68 65 20 64 69 73 6b 2e ble on the disk.
efc0: 0a 20 20 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74 20 . ** So do not
efd0: 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 69 73 write to the dis
efe0: 6b 20 61 67 61 69 6e 2e 20 20 45 78 74 72 61 63 k again. Extrac
eff0: 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 t the root page
f000: 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 number. ** for
f010: 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 the table from t
f020: 68 65 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 he db->init.newT
f030: 6e 75 6d 20 66 69 65 6c 64 2e 20 20 28 54 68 65 num field. (The
f040: 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a page number. *
f050: 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 * should have be
f060: 65 6e 20 70 75 74 20 74 68 65 72 65 20 62 79 20 en put there by
f070: 74 68 65 20 73 71 6c 69 74 65 4f 70 65 6e 43 62 the sqliteOpenCb
f080: 20 72 6f 75 74 69 6e 65 2e 29 0a 20 20 2a 2a 0a routine.). **.
f090: 20 20 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 ** If the root
f0a0: 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
f0b0: 31 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 1, that means th
f0c0: 69 73 20 69 73 20 74 68 65 20 73 71 6c 69 74 65 is is the sqlite
f0d0: 5f 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 62 _master. ** tab
f0e0: 6c 65 20 69 74 73 65 6c 66 2e 20 20 53 6f 20 6d le itself. So m
f0f0: 61 72 6b 20 69 74 20 72 65 61 64 2d 6f 6e 6c 79 ark it read-only
f100: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d .. */. if( db-
f110: 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 >init.busy ){.
f120: 20 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e p->tnum = db->
f130: 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 init.newTnum;.
f140: 20 20 69 66 28 20 70 2d 3e 74 6e 75 6d 3d 3d 31 if( p->tnum==1
f150: 20 29 20 70 2d 3e 74 61 62 46 6c 61 67 73 20 7c ) p->tabFlags |
f160: 3d 20 54 46 5f 52 65 61 64 6f 6e 6c 79 3b 0a 20 = TF_Readonly;.
f170: 20 7d 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c }.. /* Special
f180: 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 processing for
f190: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 20 54 61 WITHOUT ROWID Ta
f1a0: 62 6c 65 73 20 2a 2f 0a 20 20 69 66 28 20 74 61 bles */. if( ta
f1b0: 62 4f 70 74 73 20 26 20 54 46 5f 57 69 74 68 6f bOpts & TF_Witho
f1c0: 75 74 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 69 utRowid ){. i
f1d0: 66 28 20 28 70 2d 3e 74 61 62 46 6c 61 67 73 20 f( (p->tabFlags
f1e0: 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 & TF_Autoincreme
f1f0: 6e 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c nt) ){. sql
f200: 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
f210: 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 rse,. "
f220: 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 6e 6f AUTOINCREMENT no
f230: 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 57 49 54 t allowed on WIT
f240: 48 4f 55 54 20 52 4f 57 49 44 20 74 61 62 6c 65 HOUT ROWID table
f250: 73 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 s");. retur
f260: 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 n;. }. if(
f270: 20 28 70 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 (p->tabFlags &
f280: 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 TF_HasPrimaryKey
f290: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )==0 ){. sq
f2a0: 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
f2b0: 61 72 73 65 2c 20 22 50 52 49 4d 41 52 59 20 4b arse, "PRIMARY K
f2c0: 45 59 20 6d 69 73 73 69 6e 67 20 6f 6e 20 74 61 EY missing on ta
f2d0: 62 6c 65 20 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d ble %s", p->zNam
f2e0: 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 e);. }else{.
f2f0: 20 20 20 20 20 70 2d 3e 74 61 62 46 6c 61 67 73 p->tabFlags
f300: 20 7c 3d 20 54 46 5f 57 69 74 68 6f 75 74 52 6f |= TF_WithoutRo
f310: 77 69 64 20 7c 20 54 46 5f 4e 6f 56 69 73 69 62 wid | TF_NoVisib
f320: 6c 65 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63 leRowid;. c
f330: 6f 6e 76 65 72 74 54 6f 57 69 74 68 6f 75 74 52 onvertToWithoutR
f340: 6f 77 69 64 54 61 62 6c 65 28 70 50 61 72 73 65 owidTable(pParse
f350: 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a , p);. }. }.
f360: 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 . iDb = sqlite3
f370: 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
f380: 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a , p->pSchema);..
f390: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
f3a0: 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 MIT_CHECK. /* R
f3b0: 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 esolve names in
f3c0: 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 all CHECK constr
f3d0: 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 aint expressions
f3e0: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
f3f0: 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 73 71 pCheck ){. sq
f400: 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 66 lite3ResolveSelf
f410: 52 65 66 65 72 65 6e 63 65 28 70 50 61 72 73 65 Reference(pParse
f420: 2c 20 70 2c 20 4e 43 5f 49 73 43 68 65 63 6b 2c , p, NC_IsCheck,
f430: 20 30 2c 20 70 2d 3e 70 43 68 65 63 6b 29 3b 0a 0, p->pCheck);.
f440: 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 }.#endif /* !d
f450: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
f460: 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 IT_CHECK) */..
f470: 2f 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20 /* Estimate the
f480: 61 76 65 72 61 67 65 20 72 6f 77 20 73 69 7a 65 average row size
f490: 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 for the table a
f4a0: 6e 64 20 66 6f 72 20 61 6c 6c 20 69 6d 70 6c 69 nd for all impli
f4b0: 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 ed indices */.
f4c0: 65 73 74 69 6d 61 74 65 54 61 62 6c 65 57 69 64 estimateTableWid
f4d0: 74 68 28 70 29 3b 0a 20 20 66 6f 72 28 70 49 64 th(p);. for(pId
f4e0: 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 x=p->pIndex; pId
f4f0: 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e x; pIdx=pIdx->pN
f500: 65 78 74 29 7b 0a 20 20 20 20 65 73 74 69 6d 61 ext){. estima
f510: 74 65 49 6e 64 65 78 57 69 64 74 68 28 70 49 64 teIndexWidth(pId
f520: 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 x);. }.. /* If
f530: 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e not initializin
f540: 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 g, then create a
f550: 20 72 65 63 6f 72 64 20 66 6f 72 20 74 68 65 20 record for the
f560: 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69 new table. ** i
f570: 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 n the SQLITE_MAS
f580: 54 45 52 20 74 61 62 6c 65 20 6f 66 20 74 68 65 TER table of the
f590: 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 0a database.. **.
f5a0: 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 ** If this is
f5b0: 61 20 54 45 4d 50 4f 52 41 52 59 20 74 61 62 6c a TEMPORARY tabl
f5c0: 65 2c 20 77 72 69 74 65 20 74 68 65 20 65 6e 74 e, write the ent
f5d0: 72 79 20 69 6e 74 6f 20 74 68 65 20 61 75 78 69 ry into the auxi
f5e0: 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69 6c 65 20 liary. ** file
f5f0: 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 74 6f 20 instead of into
f600: 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
f610: 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 e file.. */. i
f620: 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 f( !db->init.bus
f630: 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a y ){. int n;.
f640: 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 Vdbe *v;.
f650: 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20 char *zType;
f660: 20 2f 2a 20 22 76 69 65 77 22 20 6f 72 20 22 74 /* "view" or "t
f670: 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20 63 68 61 able" */. cha
f680: 72 20 2a 7a 54 79 70 65 32 3b 20 20 20 2f 2a 20 r *zType2; /*
f690: 22 56 49 45 57 22 20 6f 72 20 22 54 41 42 4c 45 "VIEW" or "TABLE
f6a0: 22 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a " */. char *z
f6b0: 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54 65 78 74 Stmt; /* Text
f6c0: 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 of the CREATE T
f6d0: 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20 56 ABLE or CREATE V
f6e0: 49 45 57 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f IEW statement */
f6f0: 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 .. v = sqlite
f700: 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
f710: 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 ;. if( NEVER(
f720: 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a v==0) ) return;.
f730: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
f740: 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f AddOp1(v, OP_Clo
f750: 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 se, 0);.. /*
f760: 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69 . ** Initiali
f770: 7a 65 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65 ze zType for the
f780: 20 6e 65 77 20 76 69 65 77 20 6f 72 20 74 61 62 new view or tab
f790: 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 le.. */. i
f7a0: 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 f( p->pSelect==0
f7b0: 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 72 ){. /* A r
f7c0: 65 67 75 6c 61 72 20 74 61 62 6c 65 20 2a 2f 0a egular table */.
f7d0: 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 74 zType = "t
f7e0: 61 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a 54 79 able";. zTy
f7f0: 70 65 32 20 3d 20 22 54 41 42 4c 45 22 3b 0a 23 pe2 = "TABLE";.#
f800: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
f810: 49 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 6c 73 IT_VIEW. }els
f820: 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 76 69 e{. /* A vi
f830: 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70 ew */. zTyp
f840: 65 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 20 20 e = "view";.
f850: 20 20 7a 54 79 70 65 32 20 3d 20 22 56 49 45 57 zType2 = "VIEW
f860: 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a ";.#endif. }.
f870: 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
f880: 69 73 20 61 20 43 52 45 41 54 45 20 54 41 42 4c is a CREATE TABL
f890: 45 20 78 78 20 41 53 20 53 45 4c 45 43 54 20 2e E xx AS SELECT .
f8a0: 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68 65 20 .., execute the
f8b0: 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 73 74 SELECT. ** st
f8c0: 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 75 6c atement to popul
f8d0: 61 74 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c ate the new tabl
f8e0: 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 e. The root-page
f8f0: 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 0a number for the.
f900: 20 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 6c 65 ** new table
f910: 20 69 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 is in register
f920: 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e pParse->regRoot.
f930: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f . **. ** O
f940: 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54 20 68 nce the SELECT h
f950: 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20 62 79 as been coded by
f960: 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 sqlite3Select()
f970: 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20 20 20 , it is in a.
f980: 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73 74 61 ** suitable sta
f990: 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f 72 20 te to query for
f9a0: 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 the column names
f9b0: 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20 62 65 and types to be
f9c0: 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 used. ** by
f9d0: 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 20 the new table..
f9e0: 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 73 **. ** A s
f9f0: 68 61 72 65 64 2d 63 61 63 68 65 20 77 72 69 74 hared-cache writ
fa00: 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 e-lock is not re
fa10: 71 75 69 72 65 64 20 74 6f 20 77 72 69 74 65 20 quired to write
fa20: 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 to the new table
fa30: 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 73 63 ,. ** as a sc
fa40: 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74 20 68 hema-lock must h
fa50: 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e ave already been
fa60: 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63 72 65 obtained to cre
fa70: 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a 20 20 ate it. Since.
fa80: 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d 6c 6f ** a schema-lo
fa90: 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c 6c 20 ck excludes all
faa0: 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 75 other database u
fab0: 73 65 72 73 2c 20 74 68 65 20 77 72 69 74 65 2d sers, the write-
fac0: 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20 20 2a lock would. *
fad0: 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74 2e 0a * be redundant..
fae0: 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
faf0: 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 Select ){.
fb00: 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b SelectDest dest;
fb10: 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 68 65 /* Where the
fb20: 20 53 45 4c 45 43 54 20 73 68 6f 75 6c 64 20 73 SELECT should s
fb30: 74 6f 72 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a tore results */.
fb40: 20 20 20 20 20 20 69 6e 74 20 72 65 67 59 69 65 int regYie
fb50: 6c 64 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 67 ld; /* Reg
fb60: 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f ister holding co
fb70: 2d 72 6f 75 74 69 6e 65 20 65 6e 74 72 79 2d 70 -routine entry-p
fb80: 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e oint */. in
fb90: 74 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20 20 t addrTop;
fba0: 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 /* Top of the
fbb0: 63 6f 2d 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 co-routine */.
fbc0: 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b 20 int regRec;
fbd0: 20 20 20 20 20 20 20 20 2f 2a 20 41 20 72 65 63 /* A rec
fbe0: 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 ord to be insert
fbf0: 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 74 61 into the new ta
fc00: 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ble */. int
fc10: 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 regRowid;
fc20: 20 2f 2a 20 52 6f 77 69 64 20 6f 66 20 74 68 65 /* Rowid of the
fc30: 20 6e 65 78 74 20 72 6f 77 20 74 6f 20 69 6e 73 next row to ins
fc40: 65 72 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ert */. int
fc50: 20 61 64 64 72 49 6e 73 4c 6f 6f 70 3b 20 20 20 addrInsLoop;
fc60: 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 6c /* Top of the l
fc70: 6f 6f 70 20 66 6f 72 20 69 6e 73 65 72 74 69 6e oop for insertin
fc80: 67 20 72 6f 77 73 20 2a 2f 0a 20 20 20 20 20 20 g rows */.
fc90: 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 20 Table *pSelTab;
fca0: 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 74 /* A table t
fcb0: 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 hat describes th
fcc0: 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73 e SELECT results
fcd0: 20 2a 2f 0a 0a 20 20 20 20 20 20 72 65 67 59 69 */.. regYi
fce0: 65 6c 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e eld = ++pParse->
fcf0: 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 72 65 67 52 nMem;. regR
fd00: 65 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e ec = ++pParse->n
fd10: 4d 65 6d 3b 0a 20 20 20 20 20 20 72 65 67 52 6f Mem;. regRo
fd20: 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e wid = ++pParse->
fd30: 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 nMem;. asse
fd40: 72 74 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 3d rt(pParse->nTab=
fd50: 3d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 =1);. sqlit
fd60: 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 e3MayAbort(pPars
fd70: 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 e);. sqlite
fd80: 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
fd90: 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 31 2c 20 P_OpenWrite, 1,
fda0: 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c pParse->regRoot,
fdb0: 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c iDb);. sql
fdc0: 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 ite3VdbeChangeP5
fdd0: 28 76 2c 20 4f 50 46 4c 41 47 5f 50 32 49 53 52 (v, OPFLAG_P2ISR
fde0: 45 47 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 EG);. pPars
fdf0: 65 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 e->nTab = 2;.
fe00: 20 20 20 61 64 64 72 54 6f 70 20 3d 20 73 71 6c addrTop = sql
fe10: 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 ite3VdbeCurrentA
fe20: 64 64 72 28 76 29 20 2b 20 31 3b 0a 20 20 20 20 ddr(v) + 1;.
fe30: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
fe40: 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 69 74 43 6f Op3(v, OP_InitCo
fe50: 72 6f 75 74 69 6e 65 2c 20 72 65 67 59 69 65 6c routine, regYiel
fe60: 64 2c 20 30 2c 20 61 64 64 72 54 6f 70 29 3b 0a d, 0, addrTop);.
fe70: 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c sqlite3Sel
fe80: 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 ectDestInit(&des
fe90: 74 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 t, SRT_Coroutine
fea0: 2c 20 72 65 67 59 69 65 6c 64 29 3b 0a 20 20 20 , regYield);.
feb0: 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
fec0: 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 (pParse, pSelect
fed0: 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 , &dest);.
fee0: 73 71 6c 69 74 65 33 56 64 62 65 45 6e 64 43 6f sqlite3VdbeEndCo
fef0: 72 6f 75 74 69 6e 65 28 76 2c 20 72 65 67 59 69 routine(v, regYi
ff00: 65 6c 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 eld);. sqli
ff10: 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
ff20: 76 2c 20 61 64 64 72 54 6f 70 20 2d 20 31 29 3b v, addrTop - 1);
ff30: 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 . if( pPars
ff40: 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e e->nErr ) return
ff50: 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 20 ;. pSelTab
ff60: 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 = sqlite3ResultS
ff70: 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 etOfSelect(pPars
ff80: 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 e, pSelect);.
ff90: 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 3d 3d if( pSelTab==
ffa0: 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 0 ) return;.
ffb0: 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f assert( p->aCo
ffc0: 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d l==0 );. p-
ffd0: 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d >nCol = pSelTab-
ffe0: 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 2d 3e >nCol;. p->
fff0: 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e aCol = pSelTab->
10000 61 43 6f 6c 3b 0a 20 20 20 20 20 20 70 53 65 6c aCol;. pSel
10010 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 Tab->nCol = 0;.
10020 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 pSelTab->aC
10030 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 ol = 0;. sq
10040 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 lite3DeleteTable
10050 28 64 62 2c 20 70 53 65 6c 54 61 62 29 3b 0a 20 (db, pSelTab);.
10060 20 20 20 20 20 61 64 64 72 49 6e 73 4c 6f 6f 70 addrInsLoop
10070 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
10080 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 dOp1(v, OP_Yield
10090 2c 20 64 65 73 74 2e 69 53 44 50 61 72 6d 29 3b , dest.iSDParm);
100a0 0a 20 20 20 20 20 20 56 64 62 65 43 6f 76 65 72 . VdbeCover
100b0 61 67 65 28 76 29 3b 0a 20 20 20 20 20 20 73 71 age(v);. sq
100c0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
100d0 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 v, OP_MakeRecord
100e0 2c 20 64 65 73 74 2e 69 53 64 73 74 2c 20 64 65 , dest.iSdst, de
100f0 73 74 2e 6e 53 64 73 74 2c 20 72 65 67 52 65 63 st.nSdst, regRec
10100 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
10110 54 61 62 6c 65 41 66 66 69 6e 69 74 79 28 76 2c TableAffinity(v,
10120 20 70 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 p, 0);. sq
10130 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
10140 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 v, OP_NewRowid,
10150 31 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 1, regRowid);.
10160 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
10170 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 ddOp3(v, OP_Inse
10180 72 74 2c 20 31 2c 20 72 65 67 52 65 63 2c 20 72 rt, 1, regRec, r
10190 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 egRowid);.
101a0 73 71 6c 69 74 65 33 56 64 62 65 47 6f 74 6f 28 sqlite3VdbeGoto(
101b0 76 2c 20 61 64 64 72 49 6e 73 4c 6f 6f 70 29 3b v, addrInsLoop);
101c0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
101d0 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 beJumpHere(v, ad
101e0 64 72 49 6e 73 4c 6f 6f 70 29 3b 0a 20 20 20 20 drInsLoop);.
101f0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
10200 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c Op1(v, OP_Close,
10210 20 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 1);. }..
10220 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 /* Compute the c
10230 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 omplete text of
10240 74 68 65 20 43 52 45 41 54 45 20 73 74 61 74 65 the CREATE state
10250 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 ment */. if(
10260 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 pSelect ){.
10270 20 7a 53 74 6d 74 20 3d 20 63 72 65 61 74 65 54 zStmt = createT
10280 61 62 6c 65 53 74 6d 74 28 64 62 2c 20 70 29 3b ableStmt(db, p);
10290 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
102a0 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 32 20 3d Token *pEnd2 =
102b0 20 74 61 62 4f 70 74 73 20 3f 20 26 70 50 61 72 tabOpts ? &pPar
102c0 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 20 3a se->sLastToken :
102d0 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 6e 20 3d pEnd;. n =
102e0 20 28 69 6e 74 29 28 70 45 6e 64 32 2d 3e 7a 20 (int)(pEnd2->z
102f0 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 - pParse->sNameT
10300 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 20 20 20 20 69 oken.z);. i
10310 66 28 20 70 45 6e 64 32 2d 3e 7a 5b 30 5d 21 3d f( pEnd2->z[0]!=
10320 27 3b 27 20 29 20 6e 20 2b 3d 20 70 45 6e 64 32 ';' ) n += pEnd2
10330 2d 3e 6e 3b 0a 20 20 20 20 20 20 7a 53 74 6d 74 ->n;. zStmt
10340 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
10350 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 20 f(db, .
10360 20 22 43 52 45 41 54 45 20 25 73 20 25 2e 2a 73 "CREATE %s %.*s
10370 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 70 50 ", zType2, n, pP
10380 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e arse->sNameToken
10390 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 .z. );.
103a0 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f 74 }.. /* A slot
103b0 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 for the record
103c0 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
103d0 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 allocated in th
103e0 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 e . ** SQLITE
103f0 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 _MASTER table.
10400 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f 20 We just need to
10410 75 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f 74 update that slot
10420 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a 2a with all. **
10430 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e the information
10440 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65 64 we've collected
10450 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c .. */. sql
10460 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
10470 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55 pParse,. "U
10480 50 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20 20 PDATE %Q.%s ".
10490 20 20 20 20 20 20 20 22 53 45 54 20 74 79 70 65 "SET type
104a0 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 ='%s', name=%Q,
104b0 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f tbl_name=%Q, roo
104c0 74 70 61 67 65 3d 23 25 64 2c 20 73 71 6c 3d 25 tpage=#%d, sql=%
104d0 51 20 22 0a 20 20 20 20 20 20 20 22 57 48 45 52 Q ". "WHER
104e0 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20 E rowid=#%d",.
104f0 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d db->aDb[iDb]
10500 2e 7a 44 62 53 4e 61 6d 65 2c 20 4d 41 53 54 45 .zDbSName, MASTE
10510 52 5f 4e 41 4d 45 2c 0a 20 20 20 20 20 20 7a 54 R_NAME,. zT
10520 79 70 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e ype,. p->zN
10530 61 6d 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e ame,. p->zN
10540 61 6d 65 2c 0a 20 20 20 20 20 20 70 50 61 72 73 ame,. pPars
10550 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a 20 20 20 20 e->regRoot,.
10560 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 zStmt,. p
10570 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a Parse->regRowid.
10580 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 );. sqlit
10590 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 e3DbFree(db, zSt
105a0 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 mt);. sqlite3
105b0 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 ChangeCookie(pPa
105c0 72 73 65 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e rse, iDb);..#ifn
105d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
105e0 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 AUTOINCREMENT.
105f0 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 /* Check to se
10600 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 e if we need to
10610 63 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65 create an sqlite
10620 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 _sequence table
10630 66 6f 72 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69 for. ** keepi
10640 6e 67 20 74 72 61 63 6b 20 6f 66 20 61 75 74 6f ng track of auto
10650 69 6e 63 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a increment keys..
10660 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 */. if( (
10670 70 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 p->tabFlags & TF
10680 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 21 _Autoincrement)!
10690 3d 30 20 29 7b 0a 20 20 20 20 20 20 44 62 20 2a =0 ){. Db *
106a0 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 pDb = &db->aDb[i
106b0 44 62 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 Db];. asser
106c0 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 t( sqlite3Schema
106d0 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 MutexHeld(db, iD
106e0 62 2c 20 30 29 20 29 3b 0a 20 20 20 20 20 20 69 b, 0) );. i
106f0 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d f( pDb->pSchema-
10700 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b 0a 20 >pSeqTab==0 ){.
10710 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 sqlite3Ne
10720 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 stedParse(pParse
10730 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45 ,. "CRE
10740 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c ATE TABLE %Q.sql
10750 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d ite_sequence(nam
10760 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20 20 20 e,seq)",.
10770 20 20 20 70 44 62 2d 3e 7a 44 62 53 4e 61 6d 65 pDb->zDbSName
10780 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 . );.
10790 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
107a0 0a 0a 20 20 20 20 2f 2a 20 52 65 70 61 72 73 65 .. /* Reparse
107b0 20 65 76 65 72 79 74 68 69 6e 67 20 74 6f 20 75 everything to u
107c0 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e pdate our intern
107d0 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 al data structur
107e0 65 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 es */. sqlite
107f0 33 56 64 62 65 41 64 64 50 61 72 73 65 53 63 68 3VdbeAddParseSch
10800 65 6d 61 4f 70 28 76 2c 20 69 44 62 2c 0a 20 20 emaOp(v, iDb,.
10810 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
10820 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 62 6c MPrintf(db, "tbl
10830 5f 6e 61 6d 65 3d 27 25 71 27 20 41 4e 44 20 74 _name='%q' AND t
10840 79 70 65 21 3d 27 74 72 69 67 67 65 72 27 22 2c ype!='trigger'",
10850 20 70 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 7d p->zName));. }
10860 0a 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 ... /* Add the
10870 74 61 62 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d table to the in-
10880 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 memory represent
10890 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 ation of the dat
108a0 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 abase.. */. if
108b0 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 ( db->init.busy
108c0 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4f ){. Table *pO
108d0 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a ld;. Schema *
108e0 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e 70 53 63 pSchema = p->pSc
108f0 68 65 6d 61 3b 0a 20 20 20 20 61 73 73 65 72 74 hema;. assert
10900 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d ( sqlite3SchemaM
10910 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 utexHeld(db, iDb
10920 2c 20 30 29 20 29 3b 0a 20 20 20 20 70 4f 6c 64 , 0) );. pOld
10930 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e = sqlite3HashIn
10940 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 sert(&pSchema->t
10950 62 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d 65 blHash, p->zName
10960 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 4f , p);. if( pO
10970 6c 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ld ){. asse
10980 72 74 28 20 70 3d 3d 70 4f 6c 64 20 29 3b 20 20 rt( p==pOld );
10990 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 /* Malloc must h
109a0 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 ave failed insid
109b0 65 20 48 61 73 68 49 6e 73 65 72 74 28 29 20 2a e HashInsert() *
109c0 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f /. sqlite3O
109d0 6f 6d 46 61 75 6c 74 28 64 62 29 3b 0a 20 20 20 omFault(db);.
109e0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
109f0 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 . pParse->pNe
10a00 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 wTable = 0;.
10a10 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c db->flags |= SQL
10a20 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 ITE_InternChange
10a30 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 s;..#ifndef SQLI
10a40 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 TE_OMIT_ALTERTAB
10a50 4c 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 70 LE. if( !p->p
10a60 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 Select ){.
10a70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
10a80 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 e = (const char
10a90 2a 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 *)pParse->sNameT
10aa0 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69 6e oken.z;. in
10ab0 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 61 t nName;. a
10ac0 73 73 65 72 74 28 20 21 70 53 65 6c 65 63 74 20 ssert( !pSelect
10ad0 26 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e 64 && pCons && pEnd
10ae0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 );. if( pC
10af0 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 ons->z==0 ){.
10b00 20 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45 6e pCons = pEn
10b10 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 d;. }.
10b20 20 6e 4e 61 6d 65 20 3d 20 28 69 6e 74 29 28 28 nName = (int)((
10b30 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 43 6f const char *)pCo
10b40 6e 73 2d 3e 7a 20 2d 20 7a 4e 61 6d 65 29 3b 0a ns->z - zName);.
10b50 20 20 20 20 20 20 70 2d 3e 61 64 64 43 6f 6c 4f p->addColO
10b60 66 66 73 65 74 20 3d 20 31 33 20 2b 20 73 71 6c ffset = 13 + sql
10b70 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 ite3Utf8CharLen(
10b80 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 zName, nName);.
10b90 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
10ba0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
10bb0 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a E_OMIT_VIEW./*.*
10bc0 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c * The parser cal
10bd0 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ls this routine
10be0 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 in order to crea
10bf0 74 65 20 61 20 6e 65 77 20 56 49 45 57 0a 2a 2f te a new VIEW.*/
10c00 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 .void sqlite3Cre
10c10 61 74 65 56 69 65 77 28 0a 20 20 50 61 72 73 65 ateView(. Parse
10c20 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a *pParse, /*
10c30 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e The parsing con
10c40 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 text */. Token
10c50 2a 70 42 65 67 69 6e 2c 20 20 20 20 20 2f 2a 20 *pBegin, /*
10c60 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e The CREATE token
10c70 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 65 that begins the
10c80 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
10c90 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 Token *pName1,
10ca0 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 /* The token
10cb0 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e that holds the n
10cc0 61 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 ame of the view
10cd0 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d */. Token *pNam
10ce0 65 32 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 e2, /* The t
10cf0 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 oken that holds
10d00 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
10d10 76 69 65 77 20 2a 2f 0a 20 20 45 78 70 72 4c 69 view */. ExprLi
10d20 73 74 20 2a 70 43 4e 61 6d 65 73 2c 20 2f 2a 20 st *pCNames, /*
10d30 4f 70 74 69 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 Optional list of
10d40 20 76 69 65 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d view column nam
10d50 65 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a es */. Select *
10d60 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 41 20 pSelect, /* A
10d70 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
10d80 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d that will becom
10d90 65 20 74 68 65 20 6e 65 77 20 76 69 65 77 20 2a e the new view *
10da0 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 /. int isTemp,
10db0 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 66 /* TRUE f
10dc0 6f 72 20 61 20 54 45 4d 50 4f 52 41 52 59 20 76 or a TEMPORARY v
10dd0 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 iew */. int noE
10de0 72 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 rr /* S
10df0 75 70 70 72 65 73 73 20 65 72 72 6f 72 20 6d 65 uppress error me
10e00 73 73 61 67 65 73 20 69 66 20 56 49 45 57 20 61 ssages if VIEW a
10e10 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f lready exists */
10e20 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a .){. Table *p;.
10e30 20 20 69 6e 74 20 6e 3b 0a 20 20 63 6f 6e 73 74 int n;. const
10e40 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54 6f 6b 65 char *z;. Toke
10e50 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46 69 78 65 n sEnd;. DbFixe
10e60 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b 65 6e 20 r sFix;. Token
10e70 2a 70 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 69 6e *pName = 0;. in
10e80 74 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 t iDb;. sqlite3
10e90 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
10ea0 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 b;.. if( pParse
10eb0 2d 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 ->nVar>0 ){.
10ec0 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
10ed0 70 50 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74 pParse, "paramet
10ee0 65 72 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f ers are not allo
10ef0 77 65 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a wed in views");.
10f00 20 20 20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f goto create_
10f10 76 69 65 77 5f 66 61 69 6c 3b 0a 20 20 7d 0a 20 view_fail;. }.
10f20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 sqlite3StartTab
10f30 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 le(pParse, pName
10f40 31 2c 20 70 4e 61 6d 65 32 2c 20 69 73 54 65 6d 1, pName2, isTem
10f50 70 2c 20 31 2c 20 30 2c 20 6e 6f 45 72 72 29 3b p, 1, 0, noErr);
10f60 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 . p = pParse->p
10f70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 NewTable;. if(
10f80 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e p==0 || pParse->
10f90 6e 45 72 72 20 29 20 67 6f 74 6f 20 63 72 65 61 nErr ) goto crea
10fa0 74 65 5f 76 69 65 77 5f 66 61 69 6c 3b 0a 20 20 te_view_fail;.
10fb0 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 sqlite3TwoPartNa
10fc0 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 me(pParse, pName
10fd0 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 1, pName2, &pNam
10fe0 65 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 e);. iDb = sqli
10ff0 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
11000 28 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 (db, p->pSchema)
11010 3b 0a 20 20 73 71 6c 69 74 65 33 46 69 78 49 6e ;. sqlite3FixIn
11020 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 it(&sFix, pParse
11030 2c 20 69 44 62 2c 20 22 76 69 65 77 22 2c 20 70 , iDb, "view", p
11040 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 73 71 6c Name);. if( sql
11050 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 26 73 ite3FixSelect(&s
11060 46 69 78 2c 20 70 53 65 6c 65 63 74 29 20 29 20 Fix, pSelect) )
11070 67 6f 74 6f 20 63 72 65 61 74 65 5f 76 69 65 77 goto create_view
11080 5f 66 61 69 6c 3b 0a 0a 20 20 2f 2a 20 4d 61 6b _fail;.. /* Mak
11090 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 e a copy of the
110a0 65 6e 74 69 72 65 20 53 45 4c 45 43 54 20 73 74 entire SELECT st
110b0 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66 atement that def
110c0 69 6e 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20 ines the view..
110d0 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 66 6f ** This will fo
110e0 72 63 65 20 61 6c 6c 20 74 68 65 20 45 78 70 72 rce all the Expr
110f0 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 20 .token.z values
11100 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 61 6c 6c to be dynamicall
11110 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 y. ** allocated
11120 20 72 61 74 68 65 72 20 74 68 61 6e 20 70 6f 69 rather than poi
11130 6e 74 20 74 6f 20 74 68 65 20 69 6e 70 75 74 20 nt to the input
11140 73 74 72 69 6e 67 20 2d 20 77 68 69 63 68 20 6d string - which m
11150 65 61 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 eans that. ** t
11160 68 65 79 20 77 69 6c 6c 20 70 65 72 73 69 73 74 hey will persist
11170 20 61 66 74 65 72 20 74 68 65 20 63 75 72 72 65 after the curre
11180 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 nt sqlite3_exec(
11190 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a ) call returns..
111a0 20 20 2a 2f 0a 20 20 70 2d 3e 70 53 65 6c 65 63 */. p->pSelec
111b0 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 t = sqlite3Selec
111c0 74 44 75 70 28 64 62 2c 20 70 53 65 6c 65 63 74 tDup(db, pSelect
111d0 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 , EXPRDUP_REDUCE
111e0 29 3b 0a 20 20 70 2d 3e 70 43 68 65 63 6b 20 3d );. p->pCheck =
111f0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
11200 44 75 70 28 64 62 2c 20 70 43 4e 61 6d 65 73 2c Dup(db, pCNames,
11210 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 EXPRDUP_REDUCE)
11220 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c ;. if( db->mall
11230 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 ocFailed ) goto
11240 63 72 65 61 74 65 5f 76 69 65 77 5f 66 61 69 6c create_view_fail
11250 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 ;.. /* Locate t
11260 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52 he end of the CR
11270 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65 6d EATE VIEW statem
11280 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64 20 ent. Make sEnd
11290 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 68 point to. ** th
112a0 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 45 e end.. */. sE
112b0 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 nd = pParse->sLa
112c0 73 74 54 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 stToken;. asser
112d0 74 28 20 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30 20 t( sEnd.z[0]!=0
112e0 29 3b 0a 20 20 69 66 28 20 73 45 6e 64 2e 7a 5b );. if( sEnd.z[
112f0 30 5d 21 3d 27 3b 27 20 29 7b 0a 20 20 20 20 73 0]!=';' ){. s
11300 45 6e 64 2e 7a 20 2b 3d 20 73 45 6e 64 2e 6e 3b End.z += sEnd.n;
11310 0a 20 20 7d 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 . }. sEnd.n =
11320 30 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 28 73 0;. n = (int)(s
11330 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e 2d 3e End.z - pBegin->
11340 7a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e z);. assert( n>
11350 30 20 29 3b 0a 20 20 7a 20 3d 20 70 42 65 67 69 0 );. z = pBegi
11360 6e 2d 3e 7a 3b 0a 20 20 77 68 69 6c 65 28 20 73 n->z;. while( s
11370 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b qlite3Isspace(z[
11380 6e 2d 31 5d 29 20 29 7b 20 6e 2d 2d 3b 20 7d 0a n-1]) ){ n--; }.
11390 20 20 73 45 6e 64 2e 7a 20 3d 20 26 7a 5b 6e 2d sEnd.z = &z[n-
113a0 31 5d 3b 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 31 1];. sEnd.n = 1
113b0 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 73 71 6c 69 ;.. /* Use sqli
113c0 74 65 33 45 6e 64 54 61 62 6c 65 28 29 20 74 6f te3EndTable() to
113d0 20 61 64 64 20 74 68 65 20 76 69 65 77 20 74 6f add the view to
113e0 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 the SQLITE_MAST
113f0 45 52 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 ER table */. sq
11400 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 lite3EndTable(pP
11410 61 72 73 65 2c 20 30 2c 20 26 73 45 6e 64 2c 20 arse, 0, &sEnd,
11420 30 2c 20 30 29 3b 0a 0a 63 72 65 61 74 65 5f 76 0, 0);..create_v
11430 69 65 77 5f 66 61 69 6c 3a 0a 20 20 73 71 6c 69 iew_fail:. sqli
11440 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 te3SelectDelete(
11450 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 db, pSelect);.
11460 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
11470 65 6c 65 74 65 28 64 62 2c 20 70 43 4e 61 6d 65 elete(db, pCName
11480 73 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a s);. return;.}.
11490 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
114a0 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 23 _OMIT_VIEW */..#
114b0 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
114c0 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c TE_OMIT_VIEW) ||
114d0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
114e0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
114f0 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 LE)./*.** The Ta
11500 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 54 ble structure pT
11510 61 62 6c 65 20 69 73 20 72 65 61 6c 6c 79 20 61 able is really a
11520 20 56 49 45 57 2e 20 20 46 69 6c 6c 20 69 6e 20 VIEW. Fill in
11530 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20 the names of.**
11540 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 the columns of t
11550 68 65 20 76 69 65 77 20 69 6e 20 74 68 65 20 70 he view in the p
11560 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 2e Table structure.
11570 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d Return the num
11580 62 65 72 0a 2a 2a 20 6f 66 20 65 72 72 6f 72 73 ber.** of errors
11590 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69 . If an error i
115a0 73 20 73 65 65 6e 20 6c 65 61 76 65 20 61 6e 20 s seen leave an
115b0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
115c0 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 pParse->zErrMsg
115d0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
115e0 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d ViewGetColumnNam
115f0 65 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 es(Parse *pParse
11600 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 , Table *pTable)
11610 7b 0a 20 20 54 61 62 6c 65 20 2a 70 53 65 6c 54 {. Table *pSelT
11620 61 62 3b 20 20 20 2f 2a 20 41 20 66 61 6b 65 20 ab; /* A fake
11630 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 table from which
11640 20 77 65 20 67 65 74 20 74 68 65 20 72 65 73 75 we get the resu
11650 6c 74 20 73 65 74 20 2a 2f 0a 20 20 53 65 6c 65 lt set */. Sele
11660 63 74 20 2a 70 53 65 6c 3b 20 20 20 20 20 2f 2a ct *pSel; /*
11670 20 43 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c Copy of the SEL
11680 45 43 54 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ECT that impleme
11690 6e 74 73 20 74 68 65 20 76 69 65 77 20 2a 2f 0a nts the view */.
116a0 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 20 int nErr = 0;
116b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
116c0 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 errors encounte
116d0 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 red */. int n;
116e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
116f0 6d 70 6f 72 61 72 69 6c 79 20 68 6f 6c 64 73 20 mporarily holds
11700 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75 the number of cu
11710 72 73 6f 72 73 20 61 73 73 69 67 6e 65 64 20 2a rsors assigned *
11720 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 /. sqlite3 *db
11730 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f = pParse->db; /
11740 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * Database conne
11750 63 74 69 6f 6e 20 66 6f 72 20 6d 61 6c 6c 6f 63 ction for malloc
11760 20 65 72 72 6f 72 73 20 2a 2f 0a 23 69 66 6e 64 errors */.#ifnd
11770 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
11780 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 73 UTHORIZATION. s
11790 71 6c 69 74 65 33 5f 78 61 75 74 68 20 78 41 75 qlite3_xauth xAu
117a0 74 68 3b 20 20 20 20 20 20 20 2f 2a 20 53 61 76 th; /* Sav
117b0 65 64 20 78 41 75 74 68 20 70 6f 69 6e 74 65 72 ed xAuth pointer
117c0 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 */.#endif.. as
117d0 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a sert( pTable );.
117e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
117f0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
11800 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 E. if( sqlite3V
11810 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 tabCallConnect(p
11820 50 61 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29 Parse, pTable) )
11830 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
11840 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 ITE_ERROR;. }.
11850 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 if( IsVirtual(p
11860 54 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20 Table) ) return
11870 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 0;.#endif..#ifnd
11880 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
11890 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 IEW. /* A posit
118a0 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 ive nCol means t
118b0 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 he columns names
118c0 20 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 for this view a
118d0 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 re. ** already
118e0 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 known.. */. if
118f0 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 ( pTable->nCol>0
11900 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 ) return 0;..
11910 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43 /* A negative nC
11920 6f 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 ol is a special
11930 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 marker meaning t
11940 68 61 74 20 77 65 20 61 72 65 20 63 75 72 72 65 hat we are curre
11950 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 ntly. ** trying
11960 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 to compute the
11970 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 column names. I
11980 66 20 77 65 20 65 6e 74 65 72 20 74 68 69 73 20 f we enter this
11990 72 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a routine with. *
119a0 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f * a negative nCo
119b0 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 l, it means two
119c0 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f or more views fo
119d0 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 rm a loop, like
119e0 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 this:. **. **
119f0 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 CREATE VIEW
11a00 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20 one AS SELECT *
11a10 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 FROM two;. **
11a20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 74 CREATE VIEW t
11a30 77 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 wo AS SELECT * F
11a40 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 ROM one;. **.
11a50 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 65 ** Actually, the
11a60 20 65 72 72 6f 72 20 61 62 6f 76 65 20 69 73 20 error above is
11a70 6e 6f 77 20 63 61 75 67 68 74 20 70 72 69 6f 72 now caught prior
11a80 20 74 6f 20 72 65 61 63 68 69 6e 67 20 74 68 69 to reaching thi
11a90 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2a 20 42 75 s point.. ** Bu
11aa0 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 t the following
11ab0 74 65 73 74 20 69 73 20 73 74 69 6c 6c 20 69 6d test is still im
11ac0 70 6f 72 74 61 6e 74 20 61 73 20 69 74 20 64 6f portant as it do
11ad0 65 73 20 63 6f 6d 65 20 75 70 0a 20 20 2a 2a 20 es come up. **
11ae0 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 in the following
11af0 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 :. ** . **
11b00 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6d 61 CREATE TABLE ma
11b10 69 6e 2e 65 78 31 28 61 29 3b 0a 20 20 2a 2a 20 in.ex1(a);. **
11b20 20 20 20 20 43 52 45 41 54 45 20 54 45 4d 50 20 CREATE TEMP
11b30 56 49 45 57 20 65 78 31 20 41 53 20 53 45 4c 45 VIEW ex1 AS SELE
11b40 43 54 20 61 20 46 52 4f 4d 20 65 78 31 3b 0a 20 CT a FROM ex1;.
11b50 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 2a ** SELECT *
11b60 20 46 52 4f 4d 20 74 65 6d 70 2e 65 78 31 3b 0a FROM temp.ex1;.
11b70 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c */. if( pTabl
11b80 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 e->nCol<0 ){.
11b90 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
11ba0 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 25 (pParse, "view %
11bb0 73 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20 s is circularly
11bc0 64 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 defined", pTable
11bd0 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 ->zName);. re
11be0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 turn 1;. }. as
11bf0 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 sert( pTable->nC
11c00 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 ol>=0 );.. /* I
11c10 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61 f we get this fa
11c20 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e r, it means we n
11c30 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 eed to compute t
11c40 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a he table names..
11c50 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 ** Note that t
11c60 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 he call to sqlit
11c70 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c e3ResultSetOfSel
11c80 65 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e ect() will expan
11c90 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 d any. ** "*" e
11ca0 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72 lements in the r
11cb0 65 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68 esults set of th
11cc0 65 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 e view and will
11cd0 61 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 assign cursors.
11ce0 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 ** to the eleme
11cf0 6e 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 nts of the FROM
11d00 63 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65 20 clause. But we
11d10 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 do not want thes
11d20 65 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 e changes. ** t
11d30 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 o be permanent.
11d40 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74 So the computat
11d50 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 ion is done on a
11d60 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c copy of the SEL
11d70 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 ECT. ** stateme
11d80 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 nt that defines
11d90 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 the view.. */.
11da0 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d assert( pTable-
11db0 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 >pSelect );. pS
11dc0 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 el = sqlite3Sele
11dd0 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 ctDup(db, pTable
11de0 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 ->pSelect, 0);.
11df0 20 69 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 if( pSel ){.
11e00 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 n = pParse->nTa
11e10 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 b;. sqlite3Sr
11e20 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f cListAssignCurso
11e30 72 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d rs(pParse, pSel-
11e40 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 >pSrc);. pTab
11e50 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 20 le->nCol = -1;.
11e60 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 db->lookaside
11e70 2e 62 44 69 73 61 62 6c 65 2b 2b 3b 0a 23 69 66 .bDisable++;.#if
11e80 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
11e90 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 _AUTHORIZATION.
11ea0 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 xAuth = db->x
11eb0 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41 Auth;. db->xA
11ec0 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53 65 uth = 0;. pSe
11ed0 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 lTab = sqlite3Re
11ee0 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 sultSetOfSelect(
11ef0 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 20 pParse, pSel);.
11f00 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 db->xAuth = x
11f10 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20 20 Auth;.#else.
11f20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 pSelTab = sqlite
11f30 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 3ResultSetOfSele
11f40 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 ct(pParse, pSel)
11f50 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61 ;.#endif. pPa
11f60 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 rse->nTab = n;.
11f70 20 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 70 if( pTable->p
11f80 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f Check ){. /
11f90 2a 20 43 52 45 41 54 45 20 56 49 45 57 20 6e 61 * CREATE VIEW na
11fa0 6d 65 28 61 72 67 6c 69 73 74 29 20 41 53 20 2e me(arglist) AS .
11fb0 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 ... ** The
11fc0 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c names of the col
11fd0 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c umns in the tabl
11fe0 65 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d e are taken from
11ff0 0a 20 20 20 20 20 20 2a 2a 20 61 72 67 6c 69 73 . ** arglis
12000 74 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 65 t which is store
12010 64 20 69 6e 20 70 54 61 62 6c 65 2d 3e 70 43 68 d in pTable->pCh
12020 65 63 6b 2e 20 20 54 68 65 20 70 43 68 65 63 6b eck. The pCheck
12030 20 66 69 65 6c 64 0a 20 20 20 20 20 20 2a 2a 20 field. **
12040 6e 6f 72 6d 61 6c 6c 79 20 68 6f 6c 64 73 20 43 normally holds C
12050 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 HECK constraints
12060 20 6f 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 on an ordinary
12070 74 61 62 6c 65 2c 20 62 75 74 20 66 6f 72 0a 20 table, but for.
12080 20 20 20 20 20 2a 2a 20 61 20 56 49 45 57 20 69 ** a VIEW i
12090 74 20 68 6f 6c 64 73 20 74 68 65 20 6c 69 73 74 t holds the list
120a0 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 of column names
120b0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
120c0 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 73 46 sqlite3ColumnsF
120d0 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 romExprList(pPar
120e0 73 65 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68 65 se, pTable->pChe
120f0 63 6b 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 ck, .
12100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12110 20 20 20 20 20 20 26 70 54 61 62 6c 65 2d 3e 6e &pTable->n
12120 43 6f 6c 2c 20 26 70 54 61 62 6c 65 2d 3e 61 43 Col, &pTable->aC
12130 6f 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 ol);. if( d
12140 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d b->mallocFailed=
12150 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20 70 50 =0 . && pP
12160 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 0a 20 20 arse->nErr==0.
12170 20 20 20 20 20 26 26 20 70 54 61 62 6c 65 2d 3e && pTable->
12180 6e 43 6f 6c 3d 3d 70 53 65 6c 2d 3e 70 45 4c 69 nCol==pSel->pELi
12190 73 74 2d 3e 6e 45 78 70 72 0a 20 20 20 20 20 20 st->nExpr.
121a0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
121b0 65 33 53 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d e3SelectAddColum
121c0 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f nTypeAndCollatio
121d0 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 n(pParse, pTable
121e0 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 20 20 7d , pSel);. }
121f0 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
12200 53 65 6c 54 61 62 20 29 7b 0a 20 20 20 20 20 20 SelTab ){.
12210 2f 2a 20 43 52 45 41 54 45 20 56 49 45 57 20 6e /* CREATE VIEW n
12220 61 6d 65 20 41 53 2e 2e 2e 20 20 77 69 74 68 6f ame AS... witho
12230 75 74 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c ut an argument l
12240 69 73 74 2e 20 20 43 6f 6e 73 74 72 75 63 74 0a ist. Construct.
12250 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6c ** the col
12260 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 74 umn names from t
12270 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
12280 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 ent that defines
12290 20 74 68 65 20 76 69 65 77 2e 0a 20 20 20 20 20 the view..
122a0 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 */. assert
122b0 28 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d ( pTable->aCol==
122c0 30 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 0 );. pTabl
122d0 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 e->nCol = pSelTa
122e0 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 b->nCol;. p
122f0 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 Table->aCol = pS
12300 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 elTab->aCol;.
12310 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c pSelTab->nCol
12320 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 53 65 6c = 0;. pSel
12330 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 Tab->aCol = 0;.
12340 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
12350 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 ite3SchemaMutexH
12360 65 6c 64 28 64 62 2c 20 30 2c 20 70 54 61 62 6c eld(db, 0, pTabl
12370 65 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 e->pSchema) );.
12380 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
12390 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 pTable->nCol = 0
123a0 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a ;. nErr++;.
123b0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
123c0 33 44 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 3DeleteTable(db,
123d0 20 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 73 pSelTab);. s
123e0 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
123f0 74 65 28 64 62 2c 20 70 53 65 6c 29 3b 0a 20 20 te(db, pSel);.
12400 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e db->lookaside.
12410 62 44 69 73 61 62 6c 65 2d 2d 3b 0a 20 20 7d 20 bDisable--;. }
12420 65 6c 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b else {. nErr+
12430 2b 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d +;. }. pTable-
12440 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 >pSchema->schema
12450 46 6c 61 67 73 20 7c 3d 20 44 42 5f 55 6e 72 65 Flags |= DB_Unre
12460 73 65 74 56 69 65 77 73 3b 0a 23 65 6e 64 69 66 setViews;.#endif
12470 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
12480 56 49 45 57 20 2a 2f 0a 20 20 72 65 74 75 72 6e VIEW */. return
12490 20 6e 45 72 72 3b 20 20 0a 7d 0a 23 65 6e 64 69 nErr; .}.#endi
124a0 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 f /* !defined(SQ
124b0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
124c0 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
124d0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
124e0 41 42 4c 45 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 ABLE) */..#ifnde
124f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
12500 45 57 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 EW./*.** Clear t
12510 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 he column names
12520 66 72 6f 6d 20 65 76 65 72 79 20 56 49 45 57 20 from every VIEW
12530 69 6e 20 64 61 74 61 62 61 73 65 20 69 64 78 2e in database idx.
12540 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
12550 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 sqliteViewResetA
12560 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ll(sqlite3 *db,
12570 69 6e 74 20 69 64 78 29 7b 0a 20 20 48 61 73 68 int idx){. Hash
12580 45 6c 65 6d 20 2a 69 3b 0a 20 20 61 73 73 65 72 Elem *i;. asser
12590 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 t( sqlite3Schema
125a0 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 64 MutexHeld(db, id
125b0 78 2c 20 30 29 20 29 3b 0a 20 20 69 66 28 20 21 x, 0) );. if( !
125c0 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 DbHasProperty(db
125d0 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 , idx, DB_Unrese
125e0 74 56 69 65 77 73 29 20 29 20 72 65 74 75 72 6e tViews) ) return
125f0 3b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 ;. for(i=sqlite
12600 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 HashFirst(&db->a
12610 44 62 5b 69 64 78 5d 2e 70 53 63 68 65 6d 61 2d Db[idx].pSchema-
12620 3e 74 62 6c 48 61 73 68 29 3b 20 69 3b 69 3d 73 >tblHash); i;i=s
12630 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 qliteHashNext(i)
12640 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 ){. Table *pT
12650 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 ab = sqliteHashD
12660 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28 20 ata(i);. if(
12670 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b pTab->pSelect ){
12680 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 . sqlite3De
12690 6c 65 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 leteColumnNames(
126a0 64 62 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 db, pTab);.
126b0 20 70 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b pTab->aCol = 0;
126c0 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 43 6f . pTab->nCo
126d0 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d l = 0;. }. }
126e0 0a 20 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 . DbClearProper
126f0 74 79 28 64 62 2c 20 69 64 78 2c 20 44 42 5f 55 ty(db, idx, DB_U
12700 6e 72 65 73 65 74 56 69 65 77 73 29 3b 0a 7d 0a nresetViews);.}.
12710 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
12720 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c qliteViewResetAl
12730 6c 28 41 2c 42 29 0a 23 65 6e 64 69 66 20 2f 2a l(A,B).#endif /*
12740 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 SQLITE_OMIT_VIE
12750 57 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 W */../*.** This
12760 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
12770 6c 65 64 20 62 79 20 74 68 65 20 56 44 42 45 20 led by the VDBE
12780 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 69 6e to adjust the in
12790 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a ternal schema.**
127a0 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 used by SQLite
127b0 77 68 65 6e 20 74 68 65 20 62 74 72 65 65 20 6c when the btree l
127c0 61 79 65 72 20 6d 6f 76 65 73 20 61 20 74 61 62 ayer moves a tab
127d0 6c 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 54 68 le root page. Th
127e0 65 0a 2a 2a 20 72 6f 6f 74 2d 70 61 67 65 20 6f e.** root-page o
127f0 66 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 f a table or ind
12800 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 ex in database i
12810 44 62 20 68 61 73 20 63 68 61 6e 67 65 64 20 66 Db has changed f
12820 72 6f 6d 20 69 46 72 6f 6d 0a 2a 2a 20 74 6f 20 rom iFrom.** to
12830 69 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 iTo..**.** Ticke
12840 74 20 23 31 37 32 38 3a 20 20 54 68 65 20 73 79 t #1728: The sy
12850 6d 62 6f 6c 20 74 61 62 6c 65 20 6d 69 67 68 74 mbol table might
12860 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 69 still contain i
12870 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6f 6e nformation.** on
12880 20 74 61 62 6c 65 73 20 61 6e 64 2f 6f 72 20 69 tables and/or i
12890 6e 64 69 63 65 73 20 74 68 61 74 20 61 72 65 20 ndices that are
128a0 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62 the process of b
128b0 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 2a 2a eing deleted..**
128c0 20 49 66 20 79 6f 75 20 61 72 65 20 75 6e 6c 75 If you are unlu
128d0 63 6b 79 2c 20 6f 6e 65 20 6f 66 20 74 68 6f 73 cky, one of thos
128e0 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69 63 65 e deleted indice
128f0 73 20 6f 72 20 74 61 62 6c 65 73 20 6d 69 67 68 s or tables migh
12900 74 0a 2a 2a 20 68 61 76 65 20 74 68 65 20 73 61 t.** have the sa
12910 6d 65 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 me rootpage numb
12920 65 72 20 61 73 20 74 68 65 20 72 65 61 6c 20 74 er as the real t
12930 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 68 able or index th
12940 61 74 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 6d at is.** being m
12950 6f 76 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e oved. So we can
12960 6e 6f 74 20 73 74 6f 70 20 73 65 61 72 63 68 69 not stop searchi
12970 6e 67 20 61 66 74 65 72 20 74 68 65 20 66 69 72 ng after the fir
12980 73 74 20 6d 61 74 63 68 20 0a 2a 2a 20 62 65 63 st match .** bec
12990 61 75 73 65 20 74 68 65 20 66 69 72 73 74 20 6d ause the first m
129a0 61 74 63 68 20 6d 69 67 68 74 20 62 65 20 66 6f atch might be fo
129b0 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 6c r one of the del
129c0 65 74 65 64 20 69 6e 64 69 63 65 73 0a 2a 2a 20 eted indices.**
129d0 6f 72 20 74 61 62 6c 65 73 20 61 6e 64 20 6e 6f or tables and no
129e0 74 20 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65 t the table/inde
129f0 78 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c x that is actual
12a00 6c 79 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 0a ly being moved..
12a10 2a 2a 20 57 65 20 6d 75 73 74 20 63 6f 6e 74 69 ** We must conti
12a20 6e 75 65 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 nue looping unti
12a30 6c 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64 l all tables and
12a40 20 69 6e 64 69 63 65 73 20 77 69 74 68 0a 2a 2a indices with.**
12a50 20 72 6f 6f 74 70 61 67 65 3d 3d 69 46 72 6f 6d rootpage==iFrom
12a60 20 68 61 76 65 20 62 65 65 6e 20 63 6f 6e 76 65 have been conve
12a70 72 74 65 64 20 74 6f 20 68 61 76 65 20 61 20 72 rted to have a r
12a80 6f 6f 74 70 61 67 65 20 6f 66 20 69 54 6f 0a 2a ootpage of iTo.*
12a90 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 * in order to be
12aa0 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 77 65 certain that we
12ab0 20 67 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f got the right o
12ac0 6e 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ne..*/.#ifndef S
12ad0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
12ae0 41 43 55 55 4d 0a 76 6f 69 64 20 73 71 6c 69 74 ACUUM.void sqlit
12af0 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 e3RootPageMoved(
12b00 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
12b10 20 69 44 62 2c 20 69 6e 74 20 69 46 72 6f 6d 2c iDb, int iFrom,
12b20 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 48 61 73 int iTo){. Has
12b30 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 hElem *pElem;.
12b40 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 44 Hash *pHash;. D
12b50 62 20 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72 b *pDb;.. asser
12b60 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 t( sqlite3Schema
12b70 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 MutexHeld(db, iD
12b80 62 2c 20 30 29 20 29 3b 0a 20 20 70 44 62 20 3d b, 0) );. pDb =
12b90 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a &db->aDb[iDb];.
12ba0 20 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e pHash = &pDb->
12bb0 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 pSchema->tblHash
12bc0 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 ;. for(pElem=sq
12bd0 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 70 48 liteHashFirst(pH
12be0 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c ash); pElem; pEl
12bf0 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 em=sqliteHashNex
12c00 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54 t(pElem)){. T
12c10 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c able *pTab = sql
12c20 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 iteHashData(pEle
12c30 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 m);. if( pTab
12c40 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b ->tnum==iFrom ){
12c50 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 74 6e 75 . pTab->tnu
12c60 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 m = iTo;. }.
12c70 20 7d 0a 20 20 70 48 61 73 68 20 3d 20 26 70 44 }. pHash = &pD
12c80 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 b->pSchema->idxH
12c90 61 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d ash;. for(pElem
12ca0 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 =sqliteHashFirst
12cb0 28 70 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 (pHash); pElem;
12cc0 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 pElem=sqliteHash
12cd0 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 Next(pElem)){.
12ce0 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 Index *pIdx =
12cf0 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 sqliteHashData(p
12d00 45 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 Elem);. if( p
12d10 49 64 78 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d Idx->tnum==iFrom
12d20 20 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e ){. pIdx->
12d30 74 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20 tnum = iTo;.
12d40 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a }. }.}.#endif..
12d50 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63 6f 64 65 /*.** Write code
12d60 20 74 6f 20 65 72 61 73 65 20 74 68 65 20 74 61 to erase the ta
12d70 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 ble with root-pa
12d80 67 65 20 69 54 61 62 6c 65 20 66 72 6f 6d 20 64 ge iTable from d
12d90 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 atabase iDb..**
12da0 41 6c 73 6f 20 77 72 69 74 65 20 63 6f 64 65 20 Also write code
12db0 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71 to modify the sq
12dc0 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c lite_master tabl
12dd0 65 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73 e and internal s
12de0 63 68 65 6d 61 0a 2a 2a 20 69 66 20 61 20 72 6f chema.** if a ro
12df0 6f 74 2d 70 61 67 65 20 6f 66 20 61 6e 6f 74 68 ot-page of anoth
12e00 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 er table is move
12e10 64 20 62 79 20 74 68 65 20 62 74 72 65 65 2d 6c d by the btree-l
12e20 61 79 65 72 20 77 68 69 6c 73 74 0a 2a 2a 20 65 ayer whilst.** e
12e30 72 61 73 69 6e 67 20 69 54 61 62 6c 65 20 28 74 rasing iTable (t
12e40 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 his can happen w
12e50 69 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 ith an auto-vacu
12e60 75 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f um database)..*/
12e70 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 .static void de
12e80 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 50 61 stroyRootPage(Pa
12e90 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
12ea0 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 69 44 62 iTable, int iDb
12eb0 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 ){. Vdbe *v = s
12ec0 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
12ed0 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 31 20 arse);. int r1
12ee0 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 = sqlite3GetTemp
12ef0 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 61 Reg(pParse);. a
12f00 73 73 65 72 74 28 20 69 54 61 62 6c 65 3e 31 20 ssert( iTable>1
12f10 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
12f20 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 65 73 AddOp3(v, OP_Des
12f30 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c 20 72 31 troy, iTable, r1
12f40 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 , iDb);. sqlite
12f50 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 3MayAbort(pParse
12f60 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
12f70 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
12f80 4d 0a 20 20 2f 2a 20 4f 50 5f 44 65 73 74 72 6f M. /* OP_Destro
12f90 79 20 73 74 6f 72 65 73 20 61 6e 20 69 6e 20 69 y stores an in i
12fa0 6e 74 65 67 65 72 20 72 31 2e 20 49 66 20 74 68 nteger r1. If th
12fb0 69 73 20 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 is integer. **
12fc0 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
12fd0 6e 20 69 74 20 69 73 20 74 68 65 20 72 6f 6f 74 n it is the root
12fe0 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
12ff0 61 20 74 61 62 6c 65 20 6d 6f 76 65 64 20 74 6f a table moved to
13000 0a 20 20 2a 2a 20 6c 6f 63 61 74 69 6f 6e 20 69 . ** location i
13010 54 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f Table. The follo
13020 77 69 6e 67 20 63 6f 64 65 20 6d 6f 64 69 66 69 wing code modifi
13030 65 73 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 es the sqlite_ma
13040 73 74 65 72 20 74 61 62 6c 65 20 74 6f 0a 20 20 ster table to.
13050 2a 2a 20 72 65 66 6c 65 63 74 20 74 68 69 73 2e ** reflect this.
13060 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 22 . **. ** The "
13070 23 4e 4e 4e 22 20 69 6e 20 74 68 65 20 53 51 4c #NNN" in the SQL
13080 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 63 6f is a special co
13090 6e 73 74 61 6e 74 20 74 68 61 74 20 6d 65 61 6e nstant that mean
130a0 73 20 77 68 61 74 65 76 65 72 20 76 61 6c 75 65 s whatever value
130b0 0a 20 20 2a 2a 20 69 73 20 69 6e 20 72 65 67 69 . ** is in regi
130c0 73 74 65 72 20 4e 4e 4e 2e 20 20 53 65 65 20 67 ster NNN. See g
130d0 72 61 6d 6d 61 72 20 72 75 6c 65 73 20 61 73 73 rammar rules ass
130e0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
130f0 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 20 20 2a TK_REGISTER. *
13100 2a 20 74 6f 6b 65 6e 20 66 6f 72 20 61 64 64 69 * token for addi
13110 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
13120 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 on.. */. sqlit
13130 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
13140 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55 50 44 arse, . "UPD
13150 41 54 45 20 25 51 2e 25 73 20 53 45 54 20 72 6f ATE %Q.%s SET ro
13160 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52 45 20 otpage=%d WHERE
13170 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 #%d AND rootpage
13180 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50 61 72 =#%d",. pPar
13190 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d se->db->aDb[iDb]
131a0 2e 7a 44 62 53 4e 61 6d 65 2c 20 4d 41 53 54 45 .zDbSName, MASTE
131b0 52 5f 4e 41 4d 45 2c 20 69 54 61 62 6c 65 2c 20 R_NAME, iTable,
131c0 72 31 2c 20 72 31 29 3b 0a 23 65 6e 64 69 66 0a r1, r1);.#endif.
131d0 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
131e0 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
131f0 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 r1);.}../*.** Wr
13200 69 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 6f ite VDBE code to
13210 20 65 72 61 73 65 20 74 61 62 6c 65 20 70 54 61 erase table pTa
13220 62 20 61 6e 64 20 61 6c 6c 20 61 73 73 6f 63 69 b and all associ
13230 61 74 65 64 20 69 6e 64 69 63 65 73 20 6f 6e 20 ated indices on
13240 64 69 73 6b 2e 0a 2a 2a 20 43 6f 64 65 20 74 6f disk..** Code to
13250 20 75 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 update the sqli
13260 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 73 te_master tables
13270 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73 63 and internal sc
13280 68 65 6d 61 20 64 65 66 69 6e 69 74 69 6f 6e 73 hema definitions
13290 0a 2a 2a 20 69 6e 20 63 61 73 65 20 61 20 72 6f .** in case a ro
132a0 6f 74 2d 70 61 67 65 20 62 65 6c 6f 6e 67 69 6e ot-page belongin
132b0 67 20 74 6f 20 61 6e 6f 74 68 65 72 20 74 61 62 g to another tab
132c0 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 le is moved by t
132d0 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 0a 2a he btree layer.*
132e0 2a 20 69 73 20 61 6c 73 6f 20 61 64 64 65 64 20 * is also added
132f0 28 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e (this can happen
13300 20 77 69 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 with an auto-va
13310 63 75 75 6d 20 64 61 74 61 62 61 73 65 29 2e 0a cuum database)..
13320 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 */.static void d
13330 65 73 74 72 6f 79 54 61 62 6c 65 28 50 61 72 73 estroyTable(Pars
13340 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 e *pParse, Table
13350 20 2a 70 54 61 62 29 7b 0a 23 69 66 64 65 66 20 *pTab){.#ifdef
13360 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
13370 56 41 43 55 55 4d 0a 20 20 49 6e 64 65 78 20 2a VACUUM. Index *
13380 70 49 64 78 3b 0a 20 20 69 6e 74 20 69 44 62 20 pIdx;. int iDb
13390 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
133a0 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 oIndex(pParse->d
133b0 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 b, pTab->pSchema
133c0 29 3b 0a 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 );. destroyRoot
133d0 50 61 67 65 28 70 50 61 72 73 65 2c 20 70 54 61 Page(pParse, pTa
133e0 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 b->tnum, iDb);.
133f0 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e for(pIdx=pTab->
13400 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 pIndex; pIdx; pI
13410 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b dx=pIdx->pNext){
13420 0a 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 . destroyRoot
13430 50 61 67 65 28 70 50 61 72 73 65 2c 20 70 49 64 Page(pParse, pId
13440 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 x->tnum, iDb);.
13450 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 49 66 }.#else. /* If
13460 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 61 the database ma
13470 79 20 62 65 20 61 75 74 6f 2d 76 61 63 75 75 6d y be auto-vacuum
13480 20 63 61 70 61 62 6c 65 20 28 69 66 20 53 51 4c capable (if SQL
13490 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
134a0 55 55 4d 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 UUM. ** is not
134b0 64 65 66 69 6e 65 64 29 2c 20 74 68 65 6e 20 69 defined), then i
134c0 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 t is important t
134d0 6f 20 63 61 6c 6c 20 4f 50 5f 44 65 73 74 72 6f o call OP_Destro
134e0 79 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 74 61 y on the. ** ta
134f0 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 72 6f ble and index ro
13500 6f 74 2d 70 61 67 65 73 20 69 6e 20 6f 72 64 65 ot-pages in orde
13510 72 2c 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 r, starting with
13520 20 74 68 65 20 6e 75 6d 65 72 69 63 61 6c 6c 79 the numerically
13530 20 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 72 . ** largest r
13540 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e oot-page number.
13550 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 65 73 This guarantees
13560 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 that none of th
13570 65 20 72 6f 6f 74 2d 70 61 67 65 73 0a 20 20 2a e root-pages. *
13580 2a 20 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 * to be destroye
13590 64 20 69 73 20 72 65 6c 6f 63 61 74 65 64 20 62 d is relocated b
135a0 79 20 61 6e 20 65 61 72 6c 69 65 72 20 4f 50 5f y an earlier OP_
135b0 44 65 73 74 72 6f 79 2e 20 69 2e 65 2e 20 69 66 Destroy. i.e. if
135c0 20 74 68 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 the. ** follow
135d0 69 6e 67 20 77 65 72 65 20 63 6f 64 65 64 3a 0a ing were coded:.
135e0 20 20 2a 2a 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 **. ** OP_Des
135f0 74 72 6f 79 20 34 20 30 0a 20 20 2a 2a 20 2e 2e troy 4 0. ** ..
13600 2e 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f .. ** OP_Destro
13610 79 20 35 20 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 y 5 0. **. **
13620 61 6e 64 20 72 6f 6f 74 20 70 61 67 65 20 35 20 and root page 5
13630 68 61 70 70 65 6e 65 64 20 74 6f 20 62 65 20 74 happened to be t
13640 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d he largest root-
13650 70 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 page number in t
13660 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 he. ** database
13670 2c 20 74 68 65 6e 20 72 6f 6f 74 20 70 61 67 65 , then root page
13680 20 35 20 77 6f 75 6c 64 20 62 65 20 6d 6f 76 65 5 would be move
13690 64 20 74 6f 20 70 61 67 65 20 34 20 62 79 20 74 d to page 4 by t
136a0 68 65 20 0a 20 20 2a 2a 20 22 4f 50 5f 44 65 73 he . ** "OP_Des
136b0 74 72 6f 79 20 34 20 30 22 20 6f 70 63 6f 64 65 troy 4 0" opcode
136c0 2e 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 . The subsequent
136d0 20 22 4f 50 5f 44 65 73 74 72 6f 79 20 35 20 30 "OP_Destroy 5 0
136e0 22 20 77 6f 75 6c 64 20 68 69 74 0a 20 20 2a 2a " would hit. **
136f0 20 61 20 66 72 65 65 2d 6c 69 73 74 20 70 61 67 a free-list pag
13700 65 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 54 e.. */. int iT
13710 61 62 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b ab = pTab->tnum;
13720 0a 20 20 69 6e 74 20 69 44 65 73 74 72 6f 79 65 . int iDestroye
13730 64 20 3d 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28 d = 0;.. while(
13740 20 31 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 1 ){. Index
13750 2a 70 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 69 *pIdx;. int i
13760 4c 61 72 67 65 73 74 20 3d 20 30 3b 0a 0a 20 20 Largest = 0;..
13770 20 20 69 66 28 20 69 44 65 73 74 72 6f 79 65 64 if( iDestroyed
13780 3d 3d 30 20 7c 7c 20 69 54 61 62 3c 69 44 65 73 ==0 || iTab<iDes
13790 74 72 6f 79 65 64 20 29 7b 0a 20 20 20 20 20 20 troyed ){.
137a0 69 4c 61 72 67 65 73 74 20 3d 20 69 54 61 62 3b iLargest = iTab;
137b0 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 . }. for(p
137c0 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 Idx=pTab->pIndex
137d0 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 ; pIdx; pIdx=pId
137e0 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 x->pNext){.
137f0 20 69 6e 74 20 69 49 64 78 20 3d 20 70 49 64 78 int iIdx = pIdx
13800 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20 61 73 ->tnum;. as
13810 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68 sert( pIdx->pSch
13820 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 ema==pTab->pSche
13830 6d 61 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ma );. if(
13840 28 69 44 65 73 74 72 6f 79 65 64 3d 3d 30 20 7c (iDestroyed==0 |
13850 7c 20 28 69 49 64 78 3c 69 44 65 73 74 72 6f 79 | (iIdx<iDestroy
13860 65 64 29 29 20 26 26 20 69 49 64 78 3e 69 4c 61 ed)) && iIdx>iLa
13870 72 67 65 73 74 20 29 7b 0a 20 20 20 20 20 20 20 rgest ){.
13880 20 69 4c 61 72 67 65 73 74 20 3d 20 69 49 64 78 iLargest = iIdx
13890 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
138a0 20 20 20 20 69 66 28 20 69 4c 61 72 67 65 73 74 if( iLargest
138b0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
138c0 75 72 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a urn;. }else{.
138d0 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 int iDb =
138e0 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
138f0 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c ndex(pParse->db,
13900 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b pTab->pSchema);
13910 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
13920 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 70 50 61 Db>=0 && iDb<pPa
13930 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a rse->db->nDb );.
13940 20 20 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f destroyRoo
13950 74 50 61 67 65 28 70 50 61 72 73 65 2c 20 69 4c tPage(pParse, iL
13960 61 72 67 65 73 74 2c 20 69 44 62 29 3b 0a 20 20 argest, iDb);.
13970 20 20 20 20 69 44 65 73 74 72 6f 79 65 64 20 3d iDestroyed =
13980 20 69 4c 61 72 67 65 73 74 3b 0a 20 20 20 20 7d iLargest;. }
13990 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
139a0 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 65 6e 74 72 *.** Remove entr
139b0 69 65 73 20 66 72 6f 6d 20 74 68 65 20 73 71 6c ies from the sql
139c0 69 74 65 5f 73 74 61 74 4e 20 74 61 62 6c 65 73 ite_statN tables
139d0 20 28 66 6f 72 20 4e 20 69 6e 20 28 31 2c 32 2c (for N in (1,2,
139e0 33 29 29 0a 2a 2a 20 61 66 74 65 72 20 61 20 44 3)).** after a D
139f0 52 4f 50 20 49 4e 44 45 58 20 6f 72 20 44 52 4f ROP INDEX or DRO
13a00 50 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e P TABLE command.
13a10 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
13a20 73 71 6c 69 74 65 33 43 6c 65 61 72 53 74 61 74 sqlite3ClearStat
13a30 54 61 62 6c 65 73 28 0a 20 20 50 61 72 73 65 20 Tables(. Parse
13a40 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
13a50 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 /* The parsing
13a60 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 context */. int
13a70 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 iDb,
13a80 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
13a90 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 ase number */.
13aa0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 const char *zTyp
13ab0 65 2c 20 20 20 20 20 2f 2a 20 22 69 64 78 22 20 e, /* "idx"
13ac0 6f 72 20 22 74 62 6c 22 20 2a 2f 0a 20 20 63 6f or "tbl" */. co
13ad0 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 nst char *zName
13ae0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
13af0 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 2a index or table *
13b00 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 /.){. int i;.
13b10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e const char *zDbN
13b20 61 6d 65 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 ame = pParse->db
13b30 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 4e ->aDb[iDb].zDbSN
13b40 61 6d 65 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 ame;. for(i=1;
13b50 69 3c 3d 34 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<=4; i++){.
13b60 63 68 61 72 20 7a 54 61 62 5b 32 34 5d 3b 0a 20 char zTab[24];.
13b70 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
13b80 6e 74 66 28 73 69 7a 65 6f 66 28 7a 54 61 62 29 ntf(sizeof(zTab)
13b90 2c 7a 54 61 62 2c 22 73 71 6c 69 74 65 5f 73 74 ,zTab,"sqlite_st
13ba0 61 74 25 64 22 2c 69 29 3b 0a 20 20 20 20 69 66 at%d",i);. if
13bb0 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 ( sqlite3FindTab
13bc0 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a le(pParse->db, z
13bd0 54 61 62 2c 20 7a 44 62 4e 61 6d 65 29 20 29 7b Tab, zDbName) ){
13be0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 . sqlite3Ne
13bf0 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 stedParse(pParse
13c00 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 ,. "DELET
13c10 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 E FROM %Q.%s WHE
13c20 52 45 20 25 73 3d 25 51 22 2c 0a 20 20 20 20 20 RE %s=%Q",.
13c30 20 20 20 7a 44 62 4e 61 6d 65 2c 20 7a 54 61 62 zDbName, zTab
13c40 2c 20 7a 54 79 70 65 2c 20 7a 4e 61 6d 65 0a 20 , zType, zName.
13c50 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 );. }.
13c60 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
13c70 61 74 65 20 63 6f 64 65 20 74 6f 20 64 72 6f 70 ate code to drop
13c80 20 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a 76 6f 69 a table..*/.voi
13c90 64 20 73 71 6c 69 74 65 33 43 6f 64 65 44 72 6f d sqlite3CodeDro
13ca0 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 pTable(Parse *pP
13cb0 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 arse, Table *pTa
13cc0 62 2c 20 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 b, int iDb, int
13cd0 69 73 56 69 65 77 29 7b 0a 20 20 56 64 62 65 20 isView){. Vdbe
13ce0 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 *v;. sqlite3 *d
13cf0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
13d00 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 Trigger *pTrig
13d10 67 65 72 3b 0a 20 20 44 62 20 2a 70 44 62 20 3d ger;. Db *pDb =
13d20 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a &db->aDb[iDb];.
13d30 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 . v = sqlite3Ge
13d40 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
13d50 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b assert( v!=0 );
13d60 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 . sqlite3BeginW
13d70 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 riteOperation(pP
13d80 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a arse, 1, iDb);..
13d90 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
13da0 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
13db0 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c . if( IsVirtual
13dc0 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 (pTab) ){. sq
13dd0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
13de0 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 v, OP_VBegin);.
13df0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
13e00 44 72 6f 70 20 61 6c 6c 20 74 72 69 67 67 65 72 Drop all trigger
13e10 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
13e20 68 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e h the table bein
13e30 67 20 64 72 6f 70 70 65 64 2e 20 43 6f 64 65 0a g dropped. Code.
13e40 20 20 2a 2a 20 69 73 20 67 65 6e 65 72 61 74 65 ** is generate
13e50 64 20 74 6f 20 72 65 6d 6f 76 65 20 65 6e 74 72 d to remove entr
13e60 69 65 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 5f ies from sqlite_
13e70 6d 61 73 74 65 72 20 61 6e 64 2f 6f 72 0a 20 20 master and/or.
13e80 2a 2a 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d ** sqlite_temp_m
13e90 61 73 74 65 72 20 69 66 20 72 65 71 75 69 72 65 aster if require
13ea0 64 2e 0a 20 20 2a 2f 0a 20 20 70 54 72 69 67 67 d.. */. pTrigg
13eb0 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 er = sqlite3Trig
13ec0 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 gerList(pParse,
13ed0 70 54 61 62 29 3b 0a 20 20 77 68 69 6c 65 28 20 pTab);. while(
13ee0 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 pTrigger ){.
13ef0 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65 72 assert( pTrigger
13f00 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d ->pSchema==pTab-
13f10 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a 20 20 20 >pSchema || .
13f20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70 pTrigger->p
13f30 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b Schema==db->aDb[
13f40 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 1].pSchema );.
13f50 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 sqlite3DropTri
13f60 67 67 65 72 50 74 72 28 70 50 61 72 73 65 2c 20 ggerPtr(pParse,
13f70 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 70 pTrigger);. p
13f80 54 72 69 67 67 65 72 20 3d 20 70 54 72 69 67 67 Trigger = pTrigg
13f90 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a er->pNext;. }..
13fa0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
13fb0 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e MIT_AUTOINCREMEN
13fc0 54 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e T. /* Remove an
13fd0 79 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 y entries of the
13fe0 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 sqlite_sequence
13ff0 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 table associate
14000 64 20 77 69 74 68 0a 20 20 2a 2a 20 74 68 65 20 d with. ** the
14010 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 table being drop
14020 70 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f 6e ped. This is don
14030 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 61 62 e before the tab
14040 6c 65 20 69 73 20 64 72 6f 70 70 65 64 0a 20 20 le is dropped.
14050 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65 65 20 ** at the btree
14060 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20 74 level, in case t
14070 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e he sqlite_sequen
14080 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20 74 ce table needs t
14090 6f 0a 20 20 2a 2a 20 6d 6f 76 65 20 61 73 20 61 o. ** move as a
140a0 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 64 result of the d
140b0 72 6f 70 20 28 63 61 6e 20 68 61 70 70 65 6e 20 rop (can happen
140c0 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d in auto-vacuum m
140d0 6f 64 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ode).. */. if(
140e0 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 pTab->tabFlags
140f0 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 & TF_Autoincreme
14100 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 nt ){. sqlite
14110 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 3NestedParse(pPa
14120 72 73 65 2c 0a 20 20 20 20 20 20 22 44 45 4c 45 rse,. "DELE
14130 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 TE FROM %Q.sqlit
14140 65 5f 73 65 71 75 65 6e 63 65 20 57 48 45 52 45 e_sequence WHERE
14150 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 name=%Q",.
14160 20 70 44 62 2d 3e 7a 44 62 53 4e 61 6d 65 2c 20 pDb->zDbSName,
14170 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 pTab->zName.
14180 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 );. }.#endif..
14190 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53 51 4c /* Drop all SQL
141a0 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 ITE_MASTER table
141b0 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 69 and index entri
141c0 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f es that refer to
141d0 20 74 68 65 0a 20 20 2a 2a 20 74 61 62 6c 65 2e the. ** table.
141e0 20 54 68 65 20 70 72 6f 67 72 61 6d 20 6e 61 6d The program nam
141f0 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 e loops through
14200 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 the master table
14210 20 61 6e 64 20 64 65 6c 65 74 65 73 0a 20 20 2a and deletes. *
14220 2a 20 65 76 65 72 79 20 72 6f 77 20 74 68 61 74 * every row that
14230 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 refers to a tab
14240 6c 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e le of the same n
14250 61 6d 65 20 61 73 20 74 68 65 20 6f 6e 65 20 62 ame as the one b
14260 65 69 6e 67 0a 20 20 2a 2a 20 64 72 6f 70 70 65 eing. ** droppe
14270 64 2e 20 54 72 69 67 67 65 72 73 20 61 72 65 20 d. Triggers are
14280 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65 handled separate
14290 6c 79 20 62 65 63 61 75 73 65 20 61 20 74 72 69 ly because a tri
142a0 67 67 65 72 20 63 61 6e 20 62 65 0a 20 20 2a 2a gger can be. **
142b0 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 created in the
142c0 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 74 68 temp database th
142d0 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 at refers to a t
142e0 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65 72 0a able in another.
142f0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 20 ** database..
14300 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 */. sqlite3Nes
14310 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
14320 20 0a 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 . "DELETE
14330 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 FROM %Q.%s WHERE
14340 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 tbl_name=%Q and
14350 20 74 79 70 65 21 3d 27 74 72 69 67 67 65 72 27 type!='trigger'
14360 22 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 44 ",. pDb->zD
14370 62 53 4e 61 6d 65 2c 20 4d 41 53 54 45 52 5f 4e bSName, MASTER_N
14380 41 4d 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 AME, pTab->zName
14390 29 3b 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 );. if( !isView
143a0 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 && !IsVirtual(p
143b0 54 61 62 29 20 29 7b 0a 20 20 20 20 64 65 73 74 Tab) ){. dest
143c0 72 6f 79 54 61 62 6c 65 28 70 50 61 72 73 65 2c royTable(pParse,
143d0 20 70 54 61 62 29 3b 0a 20 20 7d 0a 0a 20 20 2f pTab);. }.. /
143e0 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 74 61 62 * Remove the tab
143f0 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 53 51 le entry from SQ
14400 4c 69 74 65 27 73 20 69 6e 74 65 72 6e 61 6c 20 Lite's internal
14410 73 63 68 65 6d 61 20 61 6e 64 20 6d 6f 64 69 66 schema and modif
14420 79 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d y. ** the schem
14430 61 20 63 6f 6f 6b 69 65 2e 0a 20 20 2a 2f 0a 20 a cookie.. */.
14440 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 if( IsVirtual(p
14450 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 Tab) ){. sqli
14460 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
14470 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 OP_VDestroy, iD
14480 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a b, 0, 0, pTab->z
14490 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 Name, 0);. }.
144a0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
144b0 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 4(v, OP_DropTabl
144c0 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 e, iDb, 0, 0, pT
144d0 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 ab->zName, 0);.
144e0 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f sqlite3ChangeCo
144f0 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 okie(pParse, iDb
14500 29 3b 0a 20 20 73 71 6c 69 74 65 56 69 65 77 52 );. sqliteViewR
14510 65 73 65 74 41 6c 6c 28 64 62 2c 20 69 44 62 29 esetAll(db, iDb)
14520 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
14530 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
14540 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b d to do the work
14550 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45 of a DROP TABLE
14560 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70 statement..** p
14570 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 Name is the name
14580 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
14590 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a be dropped..*/.
145a0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 void sqlite3Drop
145b0 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 Table(Parse *pPa
145c0 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e rse, SrcList *pN
145d0 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c ame, int isView,
145e0 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 int noErr){. T
145f0 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 able *pTab;. Vd
14600 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 be *v;. sqlite3
14610 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
14620 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 b;. int iDb;..
14630 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
14640 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 ailed ){. got
14650 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c o exit_drop_tabl
14660 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 e;. }. assert(
14670 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 pParse->nErr==0
14680 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e );. assert( pN
14690 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a ame->nSrc==1 );.
146a0 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 if( sqlite3Rea
146b0 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 dSchema(pParse)
146c0 29 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 ) goto exit_drop
146d0 5f 74 61 62 6c 65 3b 0a 20 20 69 66 28 20 6e 6f _table;. if( no
146e0 45 72 72 20 29 20 64 62 2d 3e 73 75 70 70 72 65 Err ) db->suppre
146f0 73 73 45 72 72 2b 2b 3b 0a 20 20 61 73 73 65 72 ssErr++;. asser
14700 74 28 20 69 73 56 69 65 77 3d 3d 30 20 7c 7c 20 t( isView==0 ||
14710 69 73 56 69 65 77 3d 3d 4c 4f 43 41 54 45 5f 56 isView==LOCATE_V
14720 49 45 57 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 IEW );. pTab =
14730 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
14740 6c 65 49 74 65 6d 28 70 50 61 72 73 65 2c 20 69 leItem(pParse, i
14750 73 56 69 65 77 2c 20 26 70 4e 61 6d 65 2d 3e 61 sView, &pName->a
14760 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 6f 45 72 [0]);. if( noEr
14770 72 20 29 20 64 62 2d 3e 73 75 70 70 72 65 73 73 r ) db->suppress
14780 45 72 72 2d 2d 3b 0a 0a 20 20 69 66 28 20 70 54 Err--;.. if( pT
14790 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 ab==0 ){. if(
147a0 20 6e 6f 45 72 72 20 29 20 73 71 6c 69 74 65 33 noErr ) sqlite3
147b0 43 6f 64 65 56 65 72 69 66 79 4e 61 6d 65 64 53 CodeVerifyNamedS
147c0 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70 4e chema(pParse, pN
147d0 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 ame->a[0].zDatab
147e0 61 73 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ase);. goto e
147f0 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a xit_drop_table;.
14800 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 }. iDb = sqli
14810 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
14820 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 (db, pTab->pSche
14830 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 ma);. assert( i
14840 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d Db>=0 && iDb<db-
14850 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 >nDb );.. /* If
14860 20 70 54 61 62 20 69 73 20 61 20 76 69 72 74 75 pTab is a virtu
14870 61 6c 20 74 61 62 6c 65 2c 20 63 61 6c 6c 20 56 al table, call V
14880 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 iewGetColumnName
14890 73 28 29 20 74 6f 20 65 6e 73 75 72 65 0a 20 20 s() to ensure.
148a0 2a 2a 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c ** it is initial
148b0 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ized.. */. if(
148c0 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
148d0 20 26 26 20 73 71 6c 69 74 65 33 56 69 65 77 47 && sqlite3ViewG
148e0 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 etColumnNames(pP
148f0 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 arse, pTab) ){.
14900 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
14910 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 69 66 p_table;. }.#if
14920 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14930 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 _AUTHORIZATION.
14940 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b {. int code;
14950 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
14960 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 *zTab = SCHEMA_T
14970 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 63 ABLE(iDb);. c
14980 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d onst char *zDb =
14990 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 db->aDb[iDb].zD
149a0 62 53 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 bSName;. cons
149b0 74 20 63 68 61 72 20 2a 7a 41 72 67 32 20 3d 20 t char *zArg2 =
149c0 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 0;. if( sqlit
149d0 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
149e0 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 se, SQLITE_DELET
149f0 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 E, zTab, 0, zDb)
14a00 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 ){. goto ex
14a10 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 it_drop_table;.
14a20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56 }. if( isV
14a30 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 iew ){. if(
14a40 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 !OMIT_TEMPDB &&
14a50 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 iDb==1 ){.
14a60 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 code = SQLITE
14a70 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 3b _DROP_TEMP_VIEW;
14a80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
14a90 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c code = SQL
14aa0 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a 20 ITE_DROP_VIEW;.
14ab0 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 }.#ifndef S
14ac0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
14ad0 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 ALTABLE. }els
14ae0 65 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 e if( IsVirtual(
14af0 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 63 pTab) ){. c
14b00 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f ode = SQLITE_DRO
14b10 50 5f 56 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 P_VTABLE;.
14b20 7a 41 72 67 32 20 3d 20 73 71 6c 69 74 65 33 47 zArg2 = sqlite3G
14b30 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 etVTable(db, pTa
14b40 62 29 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 3b b)->pMod->zName;
14b50 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 .#endif. }els
14b60 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d e{. if( !OM
14b70 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 IT_TEMPDB && iDb
14b80 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 ==1 ){. c
14b90 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f ode = SQLITE_DRO
14ba0 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 P_TEMP_TABLE;.
14bb0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
14bc0 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 code = SQLITE
14bd0 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20 20 _DROP_TABLE;.
14be0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
14bf0 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 f( sqlite3AuthCh
14c00 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 eck(pParse, code
14c10 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a , pTab->zName, z
14c20 41 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20 20 Arg2, zDb) ){.
14c30 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 goto exit_dr
14c40 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a op_table;. }.
14c50 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 if( sqlite3A
14c60 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c uthCheck(pParse,
14c70 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 SQLITE_DELETE,
14c80 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 pTab->zName, 0,
14c90 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f zDb) ){. go
14ca0 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 to exit_drop_tab
14cb0 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 le;. }. }.#e
14cc0 6e 64 69 66 0a 20 20 69 66 28 20 73 71 6c 69 74 ndif. if( sqlit
14cd0 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d e3StrNICmp(pTab-
14ce0 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f >zName, "sqlite_
14cf0 22 2c 20 37 29 3d 3d 30 20 0a 20 20 20 20 26 26 ", 7)==0 . &&
14d00 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 sqlite3StrNICmp
14d10 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 (pTab->zName, "s
14d20 71 6c 69 74 65 5f 73 74 61 74 22 2c 20 31 31 29 qlite_stat", 11)
14d30 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 !=0 ){. sqlit
14d40 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
14d50 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 e, "table %s may
14d60 20 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 not be dropped"
14d70 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
14d80 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 goto exit_dr
14d90 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23 op_table;. }..#
14da0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14db0 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e 73 IT_VIEW. /* Ens
14dc0 75 72 65 20 44 52 4f 50 20 54 41 42 4c 45 20 69 ure DROP TABLE i
14dd0 73 20 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61 20 s not used on a
14de0 76 69 65 77 2c 20 61 6e 64 20 44 52 4f 50 20 56 view, and DROP V
14df0 49 45 57 20 69 73 20 6e 6f 74 20 75 73 65 64 0a IEW is not used.
14e00 20 20 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65 2e ** on a table.
14e10 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 . */. if( isVi
14e20 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c ew && pTab->pSel
14e30 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 ect==0 ){. sq
14e40 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
14e50 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 arse, "use DROP
14e60 54 41 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 20 TABLE to delete
14e70 74 61 62 6c 65 20 25 73 22 2c 20 70 54 61 62 2d table %s", pTab-
14e80 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 >zName);. got
14e90 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c o exit_drop_tabl
14ea0 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73 e;. }. if( !is
14eb0 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 View && pTab->pS
14ec0 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c elect ){. sql
14ed0 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
14ee0 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 56 rse, "use DROP V
14ef0 49 45 57 20 74 6f 20 64 65 6c 65 74 65 20 76 69 IEW to delete vi
14f00 65 77 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e ew %s", pTab->zN
14f10 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ame);. goto e
14f20 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a xit_drop_table;.
14f30 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
14f40 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
14f50 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 o remove the tab
14f60 6c 65 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 le from the mast
14f70 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 6e er table. ** on
14f80 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76 20 disk.. */. v
14f90 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
14fa0 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 (pParse);. if(
14fb0 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 v ){. sqlite3
14fc0 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 BeginWriteOperat
14fd0 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 ion(pParse, 1, i
14fe0 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Db);. sqlite3
14ff0 43 6c 65 61 72 53 74 61 74 54 61 62 6c 65 73 28 ClearStatTables(
15000 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 74 62 pParse, iDb, "tb
15010 6c 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 l", pTab->zName)
15020 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 6b 44 ;. sqlite3FkD
15030 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c ropTable(pParse,
15040 20 70 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0a 20 pName, pTab);.
15050 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 44 72 sqlite3CodeDr
15060 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 opTable(pParse,
15070 70 54 61 62 2c 20 69 44 62 2c 20 69 73 56 69 65 pTab, iDb, isVie
15080 77 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72 w);. }..exit_dr
15090 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 op_table:. sqli
150a0 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 te3SrcListDelete
150b0 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a (db, pName);.}..
150c0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
150d0 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 ne is called to
150e0 63 72 65 61 74 65 20 61 20 6e 65 77 20 66 6f 72 create a new for
150f0 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 68 65 20 eign key on the
15100 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72 65 6e 74 table.** current
15110 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 ly under constru
15120 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f 6c ction. pFromCol
15130 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 determines whic
15140 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 h columns.** in
15150 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c the current tabl
15160 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 e point to the f
15170 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20 49 66 20 oreign key. If
15180 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 6e pFromCol==0 then
15190 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 68 65 20 .** connect the
151a0 6b 65 79 20 74 6f 20 74 68 65 20 6c 61 73 74 20 key to the last
151b0 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 65 64 2e column inserted.
151c0 20 20 70 54 6f 20 69 73 20 74 68 65 20 6e 61 6d pTo is the nam
151d0 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c e of.** the tabl
151e0 65 20 72 65 66 65 72 72 65 64 20 74 6f 20 28 61 e referred to (a
151f0 2e 6b 2e 61 20 74 68 65 20 22 70 61 72 65 6e 74 .k.a the "parent
15200 22 20 74 61 62 6c 65 29 2e 20 20 70 54 6f 43 6f " table). pToCo
15210 6c 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20 6f l is a list.** o
15220 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 f tables in the
15230 70 61 72 65 6e 74 20 70 54 6f 20 74 61 62 6c 65 parent pTo table
15240 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61 69 6e . flags contain
15250 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 s all.** informa
15260 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 tion about the c
15270 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 onflict resoluti
15280 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 73 70 on algorithms sp
15290 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74 68 ecified.** in th
152a0 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e 20 e ON DELETE, ON
152b0 55 50 44 41 54 45 20 61 6e 64 20 4f 4e 20 49 4e UPDATE and ON IN
152c0 53 45 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a SERT clauses..**
152d0 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73 74 72 75 .** An FKey stru
152e0 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 cture is created
152f0 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 and added to th
15300 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c e table currentl
15310 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73 74 y.** under const
15320 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 ruction in the p
15330 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
15340 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 field..**.** Th
15350 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 e foreign key is
15360 20 73 65 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 set for IMMEDIA
15370 54 45 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 TE processing.
15380 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c A subsequent cal
15390 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 44 l.** to sqlite3D
153a0 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 29 eferForeignKey()
153b0 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 might change th
153c0 69 73 20 74 6f 20 44 45 46 45 52 52 45 44 2e 0a is to DEFERRED..
153d0 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 43 */.void sqlite3C
153e0 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 reateForeignKey(
153f0 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
15400 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 , /* Parsi
15410 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
15420 45 78 70 72 4c 69 73 74 20 2a 70 46 72 6f 6d 43 ExprList *pFromC
15430 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 ol, /* Columns
15440 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 74 68 in this table th
15450 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 74 68 65 at point to othe
15460 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b r table */. Tok
15470 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20 20 20 20 en *pTo,
15480 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
15490 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a other table */.
154a0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 54 6f 43 ExprList *pToC
154b0 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e ol, /* Column
154c0 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 74 s in the other t
154d0 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c able */. int fl
154e0 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f ags /
154f0 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c * Conflict resol
15500 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 ution algorithms
15510 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 . */.){. sqlite
15520 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
15530 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 db;.#ifndef SQLI
15540 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f TE_OMIT_FOREIGN_
15550 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 KEY. FKey *pFKe
15560 79 20 3d 20 30 3b 0a 20 20 46 4b 65 79 20 2a 70 y = 0;. FKey *p
15570 4e 65 78 74 54 6f 3b 0a 20 20 54 61 62 6c 65 20 NextTo;. Table
15580 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 *p = pParse->pNe
15590 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 6e 42 wTable;. int nB
155a0 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 yte;. int i;.
155b0 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 72 int nCol;. char
155c0 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 *z;.. assert(
155d0 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 pTo!=0 );. if(
155e0 70 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 p==0 || IN_DECLA
155f0 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 66 RE_VTAB ) goto f
15600 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70 46 72 k_end;. if( pFr
15610 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 omCol==0 ){.
15620 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43 int iCol = p->nC
15630 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 4e 45 ol-1;. if( NE
15640 56 45 52 28 69 43 6f 6c 3c 30 29 20 29 20 67 6f VER(iCol<0) ) go
15650 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69 to fk_end;. i
15660 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f f( pToCol && pTo
15670 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b Col->nExpr!=1 ){
15680 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
15690 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
156a0 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25 foreign key on %
156b0 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73 68 s". " sh
156c0 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f ould reference o
156d0 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f nly one column o
156e0 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20 f table %T",.
156f0 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43 p->aCol[iC
15700 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b ol].zName, pTo);
15710 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 . goto fk_e
15720 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43 nd;. }. nC
15730 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 ol = 1;. }else
15740 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 if( pToCol && pT
15750 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72 oCol->nExpr!=pFr
15760 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a omCol->nExpr ){.
15770 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
15780 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 Msg(pParse,.
15790 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63 "number of c
157a0 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67 olumns in foreig
157b0 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d n key does not m
157c0 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20 atch the number
157d0 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f of ". "co
157e0 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66 lumns in the ref
157f0 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b erenced table");
15800 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 . goto fk_end
15810 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e ;. }else{. n
15820 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e Col = pFromCol->
15830 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79 nExpr;. }. nBy
15840 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b te = sizeof(*pFK
15850 65 79 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 ey) + (nCol-1)*s
15860 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61 43 6f izeof(pFKey->aCo
15870 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b l[0]) + pTo->n +
15880 20 31 3b 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 1;. if( pToCol
15890 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ){. for(i=0;
158a0 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 i<pToCol->nExpr
158b0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 42 ; i++){. nB
158c0 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 yte += sqlite3St
158d0 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61 rlen30(pToCol->a
158e0 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a [i].zName) + 1;.
158f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 }. }. pFKe
15900 79 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c y = sqlite3DbMal
15910 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 locZero(db, nByt
15920 65 20 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79 e );. if( pFKey
15930 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ==0 ){. goto
15940 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46 fk_end;. }. pF
15950 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a Key->pFrom = p;.
15960 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 pFKey->pNextFr
15970 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20 om = p->pFKey;.
15980 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b z = (char*)&pFK
15990 65 79 2d 3e 61 43 6f 6c 5b 6e 43 6f 6c 5d 3b 0a ey->aCol[nCol];.
159a0 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a pFKey->zTo = z
159b0 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 ;. memcpy(z, pT
159c0 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20 o->z, pTo->n);.
159d0 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a z[pTo->n] = 0;.
159e0 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 sqlite3Dequote
159f0 28 7a 29 3b 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d (z);. z += pTo-
15a00 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e >n+1;. pFKey->n
15a10 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66 Col = nCol;. if
15a20 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b ( pFromCol==0 ){
15a30 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c . pFKey->aCol
15a40 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e [0].iFrom = p->n
15a50 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a Col-1;. }else{.
15a60 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
15a70 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Col; i++){.
15a80 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f int j;. fo
15a90 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c r(j=0; j<p->nCol
15aa0 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; j++){.
15ab0 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
15ac0 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e mp(p->aCol[j].zN
15ad0 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 ame, pFromCol->a
15ae0 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b [i].zName)==0 ){
15af0 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 . pFKey
15b00 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 ->aCol[i].iFrom
15b10 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 = j;. b
15b20 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
15b30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
15b40 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a ( j>=p->nCol ){.
15b50 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
15b60 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
15b70 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e . "unkn
15b80 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c own column \"%s\
15b90 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 " in foreign key
15ba0 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20 definition", .
15bb0 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f pFromCo
15bc0 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a l->a[i].zName);.
15bd0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f goto fk_
15be0 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 end;. }.
15bf0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f }. }. if( pTo
15c00 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 Col ){. for(i
15c10 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 =0; i<nCol; i++)
15c20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 {. int n =
15c30 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
15c40 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 pToCol->a[i].zNa
15c50 6d 65 29 3b 0a 20 20 20 20 20 20 70 46 4b 65 79 me);. pFKey
15c60 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 20 3d ->aCol[i].zCol =
15c70 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 z;. memcpy
15c80 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d (z, pToCol->a[i]
15c90 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 .zName, n);.
15ca0 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 z[n] = 0;.
15cb0 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20 z += n+1;.
15cc0 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 69 }. }. pFKey->i
15cd0 73 44 65 66 65 72 72 65 64 20 3d 20 30 3b 0a 20 sDeferred = 0;.
15ce0 20 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b pFKey->aAction[
15cf0 30 5d 20 3d 20 28 75 38 29 28 66 6c 61 67 73 20 0] = (u8)(flags
15d00 26 20 30 78 66 66 29 3b 20 20 20 20 20 20 20 20 & 0xff);
15d10 20 20 20 20 2f 2a 20 4f 4e 20 44 45 4c 45 54 45 /* ON DELETE
15d20 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 46 4b action */. pFK
15d30 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 31 5d 20 3d ey->aAction[1] =
15d40 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e 3e 20 (u8)((flags >>
15d50 38 20 29 20 26 20 30 78 66 66 29 3b 20 20 20 20 8 ) & 0xff);
15d60 2f 2a 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74 /* ON UPDATE act
15d70 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ion */.. assert
15d80 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d ( sqlite3SchemaM
15d90 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c 20 utexHeld(db, 0,
15da0 70 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 p->pSchema) );.
15db0 20 70 4e 65 78 74 54 6f 20 3d 20 28 46 4b 65 79 pNextTo = (FKey
15dc0 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 6e *)sqlite3HashIn
15dd0 73 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 sert(&p->pSchema
15de0 2d 3e 66 6b 65 79 48 61 73 68 2c 20 0a 20 20 20 ->fkeyHash, .
15df0 20 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 28 pFKey->zTo, (
15e00 76 6f 69 64 20 2a 29 70 46 4b 65 79 0a 20 20 29 void *)pFKey. )
15e10 3b 0a 20 20 69 66 28 20 70 4e 65 78 74 54 6f 3d ;. if( pNextTo=
15e20 3d 70 46 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 =pFKey ){. sq
15e30 6c 69 74 65 33 4f 6f 6d 46 61 75 6c 74 28 64 62 lite3OomFault(db
15e40 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 );. goto fk_e
15e50 6e 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e nd;. }. if( pN
15e60 65 78 74 54 6f 20 29 7b 0a 20 20 20 20 61 73 73 extTo ){. ass
15e70 65 72 74 28 20 70 4e 65 78 74 54 6f 2d 3e 70 50 ert( pNextTo->pP
15e80 72 65 76 54 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 revTo==0 );.
15e90 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d pFKey->pNextTo =
15ea0 20 70 4e 65 78 74 54 6f 3b 0a 20 20 20 20 70 4e pNextTo;. pN
15eb0 65 78 74 54 6f 2d 3e 70 50 72 65 76 54 6f 20 3d extTo->pPrevTo =
15ec0 20 70 46 4b 65 79 3b 0a 20 20 7d 0a 0a 20 20 2f pFKey;. }.. /
15ed0 2a 20 4c 69 6e 6b 20 74 68 65 20 66 6f 72 65 69 * Link the forei
15ee0 67 6e 20 6b 65 79 20 74 6f 20 74 68 65 20 74 61 gn key to the ta
15ef0 62 6c 65 20 61 73 20 74 68 65 20 6c 61 73 74 20 ble as the last
15f00 73 74 65 70 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e step.. */. p->
15f10 70 46 4b 65 79 20 3d 20 70 46 4b 65 79 3b 0a 20 pFKey = pFKey;.
15f20 20 70 46 4b 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f pFKey = 0;..fk_
15f30 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 end:. sqlite3Db
15f40 46 72 65 65 28 64 62 2c 20 70 46 4b 65 79 29 3b Free(db, pFKey);
15f50 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
15f60 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
15f70 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a FOREIGN_KEY) */.
15f80 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 sqlite3ExprLis
15f90 74 44 65 6c 65 74 65 28 64 62 2c 20 70 46 72 6f tDelete(db, pFro
15fa0 6d 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 mCol);. sqlite3
15fb0 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
15fc0 62 2c 20 70 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f b, pToCol);.}../
15fd0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
15fe0 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e e is called when
15ff0 20 61 6e 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d an INITIALLY IM
16000 4d 45 44 49 41 54 45 20 6f 72 20 49 4e 49 54 49 MEDIATE or INITI
16010 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 2a 2a ALLY DEFERRED.**
16020 20 63 6c 61 75 73 65 20 69 73 20 73 65 65 6e 20 clause is seen
16030 61 73 20 70 61 72 74 20 6f 66 20 61 20 66 6f 72 as part of a for
16040 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 eign key definit
16050 69 6f 6e 2e 20 20 54 68 65 20 69 73 44 65 66 65 ion. The isDefe
16060 72 72 65 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 rred.** paramete
16070 72 20 69 73 20 31 20 66 6f 72 20 49 4e 49 54 49 r is 1 for INITI
16080 41 4c 4c 59 20 44 45 46 45 52 52 45 44 20 61 6e ALLY DEFERRED an
16090 64 20 30 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c d 0 for INITIALL
160a0 59 20 49 4d 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 Y IMMEDIATE..**
160b0 54 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 The behavior of
160c0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
160d0 79 20 63 72 65 61 74 65 64 20 66 6f 72 65 69 67 y created foreig
160e0 6e 20 6b 65 79 20 69 73 20 61 64 6a 75 73 74 65 n key is adjuste
160f0 64 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 d.** accordingly
16100 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
16110 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 3DeferForeignKey
16120 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
16130 69 6e 74 20 69 73 44 65 66 65 72 72 65 64 29 7b int isDeferred){
16140 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
16150 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
16160 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a . Table *pTab;.
16170 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 0a 20 FKey *pFKey;.
16180 20 69 66 28 20 28 70 54 61 62 20 3d 20 70 50 61 if( (pTab = pPa
16190 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d rse->pNewTable)=
161a0 3d 30 20 7c 7c 20 28 70 46 4b 65 79 20 3d 20 70 =0 || (pFKey = p
161b0 54 61 62 2d 3e 70 46 4b 65 79 29 3d 3d 30 20 29 Tab->pFKey)==0 )
161c0 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 return;. asser
161d0 74 28 20 69 73 44 65 66 65 72 72 65 64 3d 3d 30 t( isDeferred==0
161e0 20 7c 7c 20 69 73 44 65 66 65 72 72 65 64 3d 3d || isDeferred==
161f0 31 20 29 3b 20 2f 2a 20 45 56 3a 20 52 2d 33 30 1 ); /* EV: R-30
16200 33 32 33 2d 32 31 39 31 37 20 2a 2f 0a 20 20 70 323-21917 */. p
16210 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 FKey->isDeferred
16220 20 3d 20 28 75 38 29 69 73 44 65 66 65 72 72 65 = (u8)isDeferre
16230 64 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a d;.#endif.}../*.
16240 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
16250 20 74 68 61 74 20 77 69 6c 6c 20 65 72 61 73 65 that will erase
16260 20 61 6e 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 and refill inde
16270 78 20 2a 70 49 64 78 2e 20 20 54 68 69 73 20 69 x *pIdx. This i
16280 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 s.** used to ini
16290 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 tialize a newly
162a0 63 72 65 61 74 65 64 20 69 6e 64 65 78 20 6f 72 created index or
162b0 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68 to recompute th
162c0 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 e.** content of
162d0 61 6e 20 69 6e 64 65 78 20 69 6e 20 72 65 73 70 an index in resp
162e0 6f 6e 73 65 20 74 6f 20 61 20 52 45 49 4e 44 45 onse to a REINDE
162f0 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a X command..**.**
16300 20 69 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 if memRootPage
16310 69 73 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c is not negative,
16320 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 it means that t
16330 68 65 20 69 6e 64 65 78 20 69 73 20 6e 65 77 6c he index is newl
16340 79 0a 2a 2a 20 63 72 65 61 74 65 64 2e 20 20 54 y.** created. T
16350 68 65 20 72 65 67 69 73 74 65 72 20 73 70 65 63 he register spec
16360 69 66 69 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74 ified by memRoot
16370 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 Page contains th
16380 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6e e.** root page n
16390 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 umber of the ind
163a0 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f 6f 74 50 ex. If memRootP
163b0 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c age is negative,
163c0 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 then.** the ind
163d0 65 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ex already exist
163e0 73 20 61 6e 64 20 6d 75 73 74 20 62 65 20 63 6c s and must be cl
163f0 65 61 72 65 64 20 62 65 66 6f 72 65 20 62 65 69 eared before bei
16400 6e 67 20 72 65 66 69 6c 6c 65 64 20 61 6e 64 0a ng refilled and.
16410 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 ** the root page
16420 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 number of the i
16430 6e 64 65 78 20 69 73 20 74 61 6b 65 6e 20 66 72 ndex is taken fr
16440 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e om pIndex->tnum.
16450 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
16460 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 sqlite3RefillInd
16470 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ex(Parse *pParse
16480 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c , Index *pIndex,
16490 20 69 6e 74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 int memRootPage
164a0 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 ){. Table *pTab
164b0 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c = pIndex->pTabl
164c0 65 3b 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 e; /* The table
164d0 20 74 68 61 74 20 69 73 20 69 6e 64 65 78 65 64 that is indexed
164e0 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d */. int iTab =
164f0 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b pParse->nTab++;
16500 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 /* Btree cu
16510 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20 70 54 rsor used for pT
16520 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 ab */. int iIdx
16530 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b = pParse->nTab+
16540 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 +; /* Btree
16550 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20 cursor used for
16560 70 49 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 pIndex */. int
16570 69 53 6f 72 74 65 72 3b 20 20 20 20 20 20 20 20 iSorter;
16580 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 /* Cu
16590 72 73 6f 72 20 6f 70 65 6e 65 64 20 62 79 20 4f rsor opened by O
165a0 70 65 6e 53 6f 72 74 65 72 20 28 69 66 20 69 6e penSorter (if in
165b0 20 75 73 65 29 20 2a 2f 0a 20 20 69 6e 74 20 61 use) */. int a
165c0 64 64 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 ddr1;
165d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
165e0 72 65 73 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c ress of top of l
165f0 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 oop */. int add
16600 72 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 r2;
16610 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 /* Addre
16620 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f ss to jump to fo
16630 72 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e r next iteration
16640 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 */. int tnum;
16650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16660 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
16670 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20 e of index */.
16680 69 6e 74 20 69 50 61 72 74 49 64 78 4c 61 62 65 int iPartIdxLabe
16690 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f l; /
166a0 2a 20 4a 75 6d 70 20 74 6f 20 74 68 69 73 20 6c * Jump to this l
166b0 61 62 65 6c 20 74 6f 20 73 6b 69 70 20 61 20 72 abel to skip a r
166c0 6f 77 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b ow */. Vdbe *v;
166d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
166e0 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 /* Genera
166f0 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 te code into thi
16700 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e s virtual machin
16710 65 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a e */. KeyInfo *
16720 70 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 pKey;
16730 20 20 20 20 20 20 2f 2a 20 4b 65 79 49 6e 66 6f /* KeyInfo
16740 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 for index */.
16750 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20 20 int regRecord;
16760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16770 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 * Register holdi
16780 6e 67 20 61 73 73 65 6d 62 6c 65 64 20 69 6e 64 ng assembled ind
16790 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 ex record */. s
167a0 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
167b0 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 2f 2a rse->db; /*
167c0 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
167d0 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e nnection */. in
167e0 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 t iDb = sqlite3S
167f0 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
16800 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 pIndex->pSchema
16810 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
16820 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
16830 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 ATION. if( sqli
16840 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
16850 72 73 65 2c 20 53 51 4c 49 54 45 5f 52 45 49 4e rse, SQLITE_REIN
16860 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 DEX, pIndex->zNa
16870 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 64 62 2d me, 0,. db-
16880 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 4e 61 >aDb[iDb].zDbSNa
16890 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 me ) ){. retu
168a0 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a rn;. }.#endif..
168b0 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 77 /* Require a w
168c0 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 rite-lock on the
168d0 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f 72 table to perfor
168e0 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e m this operation
168f0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62 */. sqlite3Tab
16900 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 leLock(pParse, i
16910 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 Db, pTab->tnum,
16920 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 1, pTab->zName);
16930 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 .. v = sqlite3G
16940 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
16950 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 if( v==0 ) ret
16960 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f urn;. if( memRo
16970 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 otPage>=0 ){.
16980 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50 tnum = memRootP
16990 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 age;. }else{.
169a0 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d tnum = pIndex-
169b0 3e 74 6e 75 6d 3b 0a 20 20 7d 0a 20 20 70 4b 65 >tnum;. }. pKe
169c0 79 20 3d 20 73 71 6c 69 74 65 33 4b 65 79 49 6e y = sqlite3KeyIn
169d0 66 6f 4f 66 49 6e 64 65 78 28 70 50 61 72 73 65 foOfIndex(pParse
169e0 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 61 73 73 , pIndex);. ass
169f0 65 72 74 28 20 70 4b 65 79 21 3d 30 20 7c 7c 20 ert( pKey!=0 ||
16a00 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
16a10 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 || pParse->nErr
16a20 20 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 );.. /* Open t
16a30 68 65 20 73 6f 72 74 65 72 20 63 75 72 73 6f 72 he sorter cursor
16a40 20 69 66 20 77 65 20 61 72 65 20 74 6f 20 75 73 if we are to us
16a50 65 20 6f 6e 65 2e 20 2a 2f 0a 20 20 69 53 6f 72 e one. */. iSor
16a60 74 65 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 ter = pParse->nT
16a70 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56 ab++;. sqlite3V
16a80 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
16a90 53 6f 72 74 65 72 4f 70 65 6e 2c 20 69 53 6f 72 SorterOpen, iSor
16aa0 74 65 72 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e ter, 0, pIndex->
16ab0 6e 4b 65 79 43 6f 6c 2c 20 28 63 68 61 72 2a 29 nKeyCol, (char*)
16ac0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
16ad0 20 20 20 20 20 73 71 6c 69 74 65 33 4b 65 79 49 sqlite3KeyI
16ae0 6e 66 6f 52 65 66 28 70 4b 65 79 29 2c 20 50 34 nfoRef(pKey), P4
16af0 5f 4b 45 59 49 4e 46 4f 29 3b 0a 0a 20 20 2f 2a _KEYINFO);.. /*
16b00 20 4f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 2e Open the table.
16b10 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c Loop through al
16b20 6c 20 72 6f 77 73 20 6f 66 20 74 68 65 20 74 61 l rows of the ta
16b30 62 6c 65 2c 20 69 6e 73 65 72 74 69 6e 67 20 69 ble, inserting i
16b40 6e 64 65 78 0a 20 20 2a 2a 20 72 65 63 6f 72 64 ndex. ** record
16b50 73 20 69 6e 74 6f 20 74 68 65 20 73 6f 72 74 65 s into the sorte
16b60 72 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4f r. */. sqlite3O
16b70 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c penTable(pParse,
16b80 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54 61 62 iTab, iDb, pTab
16b90 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a , OP_OpenRead);.
16ba0 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 addr1 = sqlite
16bb0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
16bc0 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62 2c 20 P_Rewind, iTab,
16bd0 30 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 0); VdbeCoverage
16be0 28 76 29 3b 0a 20 20 72 65 67 52 65 63 6f 72 64 (v);. regRecord
16bf0 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
16c00 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 0a 20 pReg(pParse);..
16c10 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 sqlite3Generate
16c20 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c IndexKey(pParse,
16c30 70 49 6e 64 65 78 2c 69 54 61 62 2c 72 65 67 52 pIndex,iTab,regR
16c40 65 63 6f 72 64 2c 30 2c 26 69 50 61 72 74 49 64 ecord,0,&iPartId
16c50 78 4c 61 62 65 6c 2c 30 2c 30 29 3b 0a 20 20 73 xLabel,0,0);. s
16c60 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
16c70 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 49 6e 73 (v, OP_SorterIns
16c80 65 72 74 2c 20 69 53 6f 72 74 65 72 2c 20 72 65 ert, iSorter, re
16c90 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 gRecord);. sqli
16ca0 74 65 33 52 65 73 6f 6c 76 65 50 61 72 74 49 64 te3ResolvePartId
16cb0 78 4c 61 62 65 6c 28 70 50 61 72 73 65 2c 20 69 xLabel(pParse, i
16cc0 50 61 72 74 49 64 78 4c 61 62 65 6c 29 3b 0a 20 PartIdxLabel);.
16cd0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
16ce0 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 p2(v, OP_Next, i
16cf0 54 61 62 2c 20 61 64 64 72 31 2b 31 29 3b 20 56 Tab, addr1+1); V
16d00 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a dbeCoverage(v);.
16d10 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
16d20 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b pHere(v, addr1);
16d30 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f 74 50 61 . if( memRootPa
16d40 67 65 3c 30 20 29 20 73 71 6c 69 74 65 33 56 64 ge<0 ) sqlite3Vd
16d50 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 beAddOp2(v, OP_C
16d60 6c 65 61 72 2c 20 74 6e 75 6d 2c 20 69 44 62 29 lear, tnum, iDb)
16d70 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
16d80 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp4(v, OP_Open
16d90 57 72 69 74 65 2c 20 69 49 64 78 2c 20 74 6e 75 Write, iIdx, tnu
16da0 6d 2c 20 69 44 62 2c 20 0a 20 20 20 20 20 20 20 m, iDb, .
16db0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 (ch
16dc0 61 72 20 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 ar *)pKey, P4_KE
16dd0 59 49 4e 46 4f 29 3b 0a 20 20 73 71 6c 69 74 65 YINFO);. sqlite
16de0 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 3VdbeChangeP5(v,
16df0 20 4f 50 46 4c 41 47 5f 42 55 4c 4b 43 53 52 7c OPFLAG_BULKCSR|
16e00 28 28 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d 30 ((memRootPage>=0
16e10 29 3f 4f 50 46 4c 41 47 5f 50 32 49 53 52 45 47 )?OPFLAG_P2ISREG
16e20 3a 30 29 29 3b 0a 0a 20 20 61 64 64 72 31 20 3d :0));.. addr1 =
16e30 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
16e40 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 53 p2(v, OP_SorterS
16e50 6f 72 74 2c 20 69 53 6f 72 74 65 72 2c 20 30 29 ort, iSorter, 0)
16e60 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 ; VdbeCoverage(v
16e70 29 3b 0a 20 20 69 66 28 20 49 73 55 6e 69 71 75 );. if( IsUniqu
16e80 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 20 29 eIndex(pIndex) )
16e90 7b 0a 20 20 20 20 69 6e 74 20 6a 32 20 3d 20 73 {. int j2 = s
16ea0 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
16eb0 74 41 64 64 72 28 76 29 20 2b 20 33 3b 0a 20 20 tAddr(v) + 3;.
16ec0 20 20 73 71 6c 69 74 65 33 56 64 62 65 47 6f 74 sqlite3VdbeGot
16ed0 6f 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 61 64 o(v, j2);. ad
16ee0 64 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 dr2 = sqlite3Vdb
16ef0 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b eCurrentAddr(v);
16f00 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
16f10 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f AddOp4Int(v, OP_
16f20 53 6f 72 74 65 72 43 6f 6d 70 61 72 65 2c 20 69 SorterCompare, i
16f30 53 6f 72 74 65 72 2c 20 6a 32 2c 20 72 65 67 52 Sorter, j2, regR
16f40 65 63 6f 72 64 2c 0a 20 20 20 20 20 20 20 20 20 ecord,.
16f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16f60 70 49 6e 64 65 78 2d 3e 6e 4b 65 79 43 6f 6c 29 pIndex->nKeyCol)
16f70 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 ; VdbeCoverage(v
16f80 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 55 6e );. sqlite3Un
16f90 69 71 75 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 iqueConstraint(p
16fa0 50 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c Parse, OE_Abort,
16fb0 20 70 49 6e 64 65 78 29 3b 0a 20 20 7d 65 6c 73 pIndex);. }els
16fc0 65 7b 0a 20 20 20 20 61 64 64 72 32 20 3d 20 73 e{. addr2 = s
16fd0 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
16fe0 74 41 64 64 72 28 76 29 3b 0a 20 20 7d 0a 20 20 tAddr(v);. }.
16ff0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
17000 33 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 44 61 3(v, OP_SorterDa
17010 74 61 2c 20 69 53 6f 72 74 65 72 2c 20 72 65 67 ta, iSorter, reg
17020 52 65 63 6f 72 64 2c 20 69 49 64 78 29 3b 0a 20 Record, iIdx);.
17030 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
17040 70 33 28 76 2c 20 4f 50 5f 4c 61 73 74 2c 20 69 p3(v, OP_Last, i
17050 49 64 78 2c 20 30 2c 20 2d 31 29 3b 0a 20 20 73 Idx, 0, -1);. s
17060 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
17070 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 (v, OP_IdxInsert
17080 2c 20 69 49 64 78 2c 20 72 65 67 52 65 63 6f 72 , iIdx, regRecor
17090 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 d);. sqlite3Vdb
170a0 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 eChangeP5(v, OPF
170b0 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c LAG_USESEEKRESUL
170c0 54 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c T);. sqlite3Rel
170d0 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
170e0 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a se, regRecord);.
170f0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
17100 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 Op2(v, OP_Sorter
17110 4e 65 78 74 2c 20 69 53 6f 72 74 65 72 2c 20 61 Next, iSorter, a
17120 64 64 72 32 29 3b 20 56 64 62 65 43 6f 76 65 72 ddr2); VdbeCover
17130 61 67 65 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 age(v);. sqlite
17140 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
17150 20 61 64 64 72 31 29 3b 0a 0a 20 20 73 71 6c 69 addr1);.. sqli
17160 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
17170 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61 62 29 OP_Close, iTab)
17180 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
17190 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 ddOp1(v, OP_Clos
171a0 65 2c 20 69 49 64 78 29 3b 0a 20 20 73 71 6c 69 e, iIdx);. sqli
171b0 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
171c0 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 6f 72 74 OP_Close, iSort
171d0 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c er);.}../*.** Al
171e0 6c 6f 63 61 74 65 20 68 65 61 70 20 73 70 61 63 locate heap spac
171f0 65 20 74 6f 20 68 6f 6c 64 20 61 6e 20 49 6e 64 e to hold an Ind
17200 65 78 20 6f 62 6a 65 63 74 20 77 69 74 68 20 6e ex object with n
17210 43 6f 6c 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a Col columns..**.
17220 2a 2a 20 49 6e 63 72 65 61 73 65 20 74 68 65 20 ** Increase the
17230 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 allocation size
17240 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 65 78 to provide an ex
17250 74 72 61 20 6e 45 78 74 72 61 20 62 79 74 65 73 tra nExtra bytes
17260 0a 2a 2a 20 6f 66 20 38 2d 62 79 74 65 20 61 6c .** of 8-byte al
17270 69 67 6e 65 64 20 73 70 61 63 65 20 61 66 74 65 igned space afte
17280 72 20 74 68 65 20 49 6e 64 65 78 20 6f 62 6a 65 r the Index obje
17290 63 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a ct and return a.
172a0 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 ** pointer to th
172b0 69 73 20 65 78 74 72 61 20 73 70 61 63 65 20 69 is extra space i
172c0 6e 20 2a 70 70 45 78 74 72 61 2e 0a 2a 2f 0a 49 n *ppExtra..*/.I
172d0 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 41 6c 6c ndex *sqlite3All
172e0 6f 63 61 74 65 49 6e 64 65 78 4f 62 6a 65 63 74 ocateIndexObject
172f0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
17300 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
17310 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
17320 2a 2f 0a 20 20 69 31 36 20 6e 43 6f 6c 2c 20 20 */. i16 nCol,
17330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
17340 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c al number of col
17350 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65 umns in the inde
17360 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 x */. int nExtr
17370 61 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e a, /* N
17380 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
17390 66 20 65 78 74 72 61 20 73 70 61 63 65 20 74 6f f extra space to
173a0 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 alloc */. char
173b0 20 2a 2a 70 70 45 78 74 72 61 20 20 20 20 20 20 **ppExtra
173c0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 /* Pointer to t
173d0 68 65 20 22 65 78 74 72 61 22 20 73 70 61 63 65 he "extra" space
173e0 20 2a 2f 0a 29 7b 0a 20 20 49 6e 64 65 78 20 2a */.){. Index *
173f0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a p; /*
17400 20 41 6c 6c 6f 63 61 74 65 64 20 69 6e 64 65 78 Allocated index
17410 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 object */. int
17420 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 nByte;
17430 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 73 70 /* Bytes of sp
17440 61 63 65 20 66 6f 72 20 49 6e 64 65 78 20 6f 62 ace for Index ob
17450 6a 65 63 74 20 2b 20 61 72 72 61 79 73 20 2a 2f ject + arrays */
17460 0a 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e .. nByte = ROUN
17470 44 38 28 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 D8(sizeof(Index)
17480 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 ) +
17490 20 2f 2a 20 49 6e 64 65 78 20 73 74 72 75 63 74 /* Index struct
174a0 75 72 65 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ure */.
174b0 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 ROUND8(sizeof(
174c0 63 68 61 72 2a 29 2a 6e 43 6f 6c 29 20 2b 20 20 char*)*nCol) +
174d0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e /* Index.
174e0 61 7a 43 6f 6c 6c 20 20 20 20 20 2a 2f 0a 20 20 azColl */.
174f0 20 20 20 20 20 20 20 20 52 4f 55 4e 44 38 28 73 ROUND8(s
17500 69 7a 65 6f 66 28 4c 6f 67 45 73 74 29 2a 28 6e izeof(LogEst)*(n
17510 43 6f 6c 2b 31 29 20 2b 20 20 20 20 20 2f 2a 20 Col+1) + /*
17520 49 6e 64 65 78 2e 61 69 52 6f 77 4c 6f 67 45 73 Index.aiRowLogEs
17530 74 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 t */.
17540 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69 sizeof(i
17550 31 36 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 16)*nCol +
17560 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 /* Index.a
17570 69 43 6f 6c 75 6d 6e 20 20 20 2a 2f 0a 20 20 20 iColumn */.
17580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 si
17590 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 29 3b 20 zeof(u8)*nCol);
175a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
175b0 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20 ndex.aSortOrder
175c0 2a 2f 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 */. p = sqlite3
175d0 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
175e0 20 6e 42 79 74 65 20 2b 20 6e 45 78 74 72 61 29 nByte + nExtra)
175f0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
17600 20 63 68 61 72 20 2a 70 45 78 74 72 61 20 3d 20 char *pExtra =
17610 28 28 63 68 61 72 2a 29 70 29 2b 52 4f 55 4e 44 ((char*)p)+ROUND
17620 38 28 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 29 8(sizeof(Index))
17630 3b 0a 20 20 20 20 70 2d 3e 61 7a 43 6f 6c 6c 20 ;. p->azColl
17640 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 = (const char**)
17650 70 45 78 74 72 61 3b 20 70 45 78 74 72 61 20 2b pExtra; pExtra +
17660 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 = ROUND8(sizeof(
17670 63 68 61 72 2a 29 2a 6e 43 6f 6c 29 3b 0a 20 20 char*)*nCol);.
17680 20 20 70 2d 3e 61 69 52 6f 77 4c 6f 67 45 73 74 p->aiRowLogEst
17690 20 3d 20 28 4c 6f 67 45 73 74 2a 29 70 45 78 74 = (LogEst*)pExt
176a0 72 61 3b 20 70 45 78 74 72 61 20 2b 3d 20 73 69 ra; pExtra += si
176b0 7a 65 6f 66 28 4c 6f 67 45 73 74 29 2a 28 6e 43 zeof(LogEst)*(nC
176c0 6f 6c 2b 31 29 3b 0a 20 20 20 20 70 2d 3e 61 69 ol+1);. p->ai
176d0 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 2a 29 70 Column = (i16*)p
176e0 45 78 74 72 61 3b 20 20 20 20 20 20 20 70 45 78 Extra; pEx
176f0 74 72 61 20 2b 3d 20 73 69 7a 65 6f 66 28 69 31 tra += sizeof(i1
17700 36 29 2a 6e 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 6)*nCol;. p->
17710 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 aSortOrder = (u8
17720 2a 29 70 45 78 74 72 61 3b 0a 20 20 20 20 70 2d *)pExtra;. p-
17730 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 3b >nColumn = nCol;
17740 0a 20 20 20 20 70 2d 3e 6e 4b 65 79 43 6f 6c 20 . p->nKeyCol
17750 3d 20 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 = nCol - 1;.
17760 2a 70 70 45 78 74 72 61 20 3d 20 28 28 63 68 61 *ppExtra = ((cha
17770 72 2a 29 70 29 20 2b 20 6e 42 79 74 65 3b 0a 20 r*)p) + nByte;.
17780 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
17790 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
177a0 20 6e 65 77 20 69 6e 64 65 78 20 66 6f 72 20 61 new index for a
177b0 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e n SQL table. pN
177c0 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69 73 20 74 ame1.pName2 is t
177d0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 he name of the i
177e0 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62 ndex .** and pTb
177f0 6c 4c 69 73 74 20 69 73 20 74 68 65 20 6e 61 6d lList is the nam
17800 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 e of the table t
17810 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64 hat is to be ind
17820 65 78 65 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c exed. Both will
17830 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72 .** be NULL for
17840 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f a primary key o
17850 72 20 61 6e 20 69 6e 64 65 78 20 74 68 61 74 20 r an index that
17860 69 73 20 63 72 65 61 74 65 64 20 74 6f 20 73 61 is created to sa
17870 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55 tisfy a.** UNIQU
17880 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49 E constraint. I
17890 66 20 70 54 61 62 6c 65 20 61 6e 64 20 70 49 6e f pTable and pIn
178a0 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73 dex are NULL, us
178b0 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 e pParse->pNewTa
178c0 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 ble.** as the ta
178d0 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65 ble to be indexe
178e0 64 2e 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 d. pParse->pNew
178f0 54 61 62 6c 65 20 69 73 20 61 20 74 61 62 6c 65 Table is a table
17900 20 74 68 61 74 20 69 73 0a 2a 2a 20 63 75 72 72 that is.** curr
17910 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 ently being cons
17920 74 72 75 63 74 65 64 20 62 79 20 61 20 43 52 45 tructed by a CRE
17930 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
17940 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 ent..**.** pList
17950 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 63 6f is a list of co
17960 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 lumns to be inde
17970 78 65 64 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c xed. pList will
17980 20 62 65 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 be NULL if this
17990 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d 61 72 79 .** is a primary
179a0 20 6b 65 79 20 6f 72 20 75 6e 69 71 75 65 2d 63 key or unique-c
179b0 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 onstraint on the
179c0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 6f 6c most recent col
179d0 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20 umn added.** to
179e0 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e the table curren
179f0 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 tly under constr
17a00 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 76 6f 69 uction. .*/.voi
17a10 64 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 d sqlite3CreateI
17a20 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 ndex(. Parse *p
17a30 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 41 6c Parse, /* Al
17a40 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 l information ab
17a50 6f 75 74 20 74 68 69 73 20 70 61 72 73 65 20 2a out this parse *
17a60 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
17a70 31 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 1, /* First
17a80 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 part of index na
17a90 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 me. May be NULL
17aa0 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d */. Token *pNam
17ab0 65 32 2c 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e e2, /* Secon
17ac0 64 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20 d part of index
17ad0 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c name. May be NUL
17ae0 4c 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a L */. SrcList *
17af0 70 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54 61 62 pTblName, /* Tab
17b00 6c 65 20 74 6f 20 69 6e 64 65 78 2e 20 55 73 65 le to index. Use
17b10 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
17b20 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 45 78 70 le if 0 */. Exp
17b30 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 rList *pList,
17b40 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 63 6f 6c /* A list of col
17b50 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 umns to be index
17b60 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 ed */. int onEr
17b70 72 6f 72 2c 20 20 20 20 20 20 20 2f 2a 20 4f 45 ror, /* OE
17b80 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 _Abort, OE_Ignor
17b90 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f e, OE_Replace, o
17ba0 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54 r OE_None */. T
17bb0 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20 20 20 oken *pStart,
17bc0 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 /* The CREATE
17bd0 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e token that begin
17be0 73 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 s this statement
17bf0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 50 49 57 */. Expr *pPIW
17c00 68 65 72 65 2c 20 20 20 20 2f 2a 20 57 48 45 52 here, /* WHER
17c10 45 20 63 6c 61 75 73 65 20 66 6f 72 20 70 61 72 E clause for par
17c20 74 69 61 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a tial indices */.
17c30 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 2c int sortOrder,
17c40 20 20 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72 64 /* Sort ord
17c50 65 72 20 6f 66 20 70 72 69 6d 61 72 79 20 6b 65 er of primary ke
17c60 79 20 77 68 65 6e 20 70 4c 69 73 74 3d 3d 4e 55 y when pList==NU
17c70 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 66 4e 6f LL */. int ifNo
17c80 74 45 78 69 73 74 2c 20 20 20 20 2f 2a 20 4f 6d tExist, /* Om
17c90 69 74 20 65 72 72 6f 72 20 69 66 20 69 6e 64 65 it error if inde
17ca0 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 x already exists
17cb0 20 2a 2f 0a 20 20 75 38 20 69 64 78 54 79 70 65 */. u8 idxType
17cc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
17cd0 69 6e 64 65 78 20 74 79 70 65 20 2a 2f 0a 29 7b index type */.){
17ce0 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d . Table *pTab =
17cf0 20 30 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 0; /* Table
17d00 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a to be indexed *
17d10 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 /. Index *pInde
17d20 78 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 x = 0; /* The
17d30 69 6e 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 index to be crea
17d40 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ted */. char *z
17d50 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a Name = 0; /*
17d60 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 Name of the ind
17d70 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d ex */. int nNam
17d80 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
17d90 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 Number of charac
17da0 74 65 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f ters in zName */
17db0 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 44 . int i, j;. D
17dc0 62 46 69 78 65 72 20 73 46 69 78 3b 20 20 20 20 bFixer sFix;
17dd0 20 20 20 20 2f 2a 20 46 6f 72 20 61 73 73 69 67 /* For assig
17de0 6e 69 6e 67 20 64 61 74 61 62 61 73 65 20 6e 61 ning database na
17df0 6d 65 73 20 74 6f 20 70 54 61 62 6c 65 20 2a 2f mes to pTable */
17e00 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 . int sortOrder
17e10 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 20 74 6f 20 Mask; /* 1 to
17e20 68 6f 6e 6f 72 20 44 45 53 43 20 69 6e 20 69 6e honor DESC in in
17e30 64 65 78 2e 20 20 30 20 74 6f 20 69 67 6e 6f 72 dex. 0 to ignor
17e40 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 e. */. sqlite3
17e50 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
17e60 3b 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20 ;. Db *pDb;
17e70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
17e80 73 70 65 63 69 66 69 63 20 74 61 62 6c 65 20 63 specific table c
17e90 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 69 6e ontaining the in
17ea0 64 65 78 65 64 20 64 61 74 61 62 61 73 65 20 2a dexed database *
17eb0 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 /. int iDb;
17ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
17ed0 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 x of the databas
17ee0 65 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 e that is being
17ef0 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b written */. Tok
17f00 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30 3b 20 20 en *pName = 0;
17f10 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 /* Unqualified
17f20 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 name of the ind
17f30 65 78 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a ex to create */.
17f40 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
17f50 74 5f 69 74 65 6d 20 2a 70 4c 69 73 74 49 74 65 t_item *pListIte
17f60 6d 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e m; /* For loopin
17f70 67 20 6f 76 65 72 20 70 4c 69 73 74 20 2a 2f 0a g over pList */.
17f80 20 20 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 int nExtra = 0
17f90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17fa0 20 20 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c 6f /* Space allo
17fb0 63 61 74 65 64 20 66 6f 72 20 7a 45 78 74 72 61 cated for zExtra
17fc0 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 [] */. int nExt
17fd0 72 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 raCol;
17fe0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
17ff0 65 72 20 6f 66 20 65 78 74 72 61 20 63 6f 6c 75 er of extra colu
18000 6d 6e 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 mns needed */.
18010 63 68 61 72 20 2a 7a 45 78 74 72 61 20 3d 20 30 char *zExtra = 0
18020 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
18030 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 /* Extra space
18040 61 66 74 65 72 20 74 68 65 20 49 6e 64 65 78 20 after the Index
18050 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 49 6e 64 65 object */. Inde
18060 78 20 2a 70 50 6b 20 3d 20 30 3b 20 20 20 20 20 x *pPk = 0;
18070 20 2f 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 /* PRIMARY KEY
18080 69 6e 64 65 78 20 66 6f 72 20 57 49 54 48 4f 55 index for WITHOU
18090 54 20 52 4f 57 49 44 20 74 61 62 6c 65 73 20 2a T ROWID tables *
180a0 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c /.. if( db->mal
180b0 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 locFailed || pPa
180c0 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 rse->nErr>0 ){.
180d0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
180e0 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 ate_index;. }.
180f0 20 69 66 28 20 49 4e 5f 44 45 43 4c 41 52 45 5f if( IN_DECLARE_
18100 56 54 41 42 20 26 26 20 69 64 78 54 79 70 65 21 VTAB && idxType!
18110 3d 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 5f =SQLITE_IDXTYPE_
18120 50 52 49 4d 41 52 59 4b 45 59 20 29 7b 0a 20 20 PRIMARYKEY ){.
18130 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 goto exit_crea
18140 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 te_index;. }.
18150 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
18160 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 qlite3ReadSchema
18170 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 (pParse) ){.
18180 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
18190 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f _index;. }.. /
181a0 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 *. ** Find the
181b0 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f table that is to
181c0 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 52 65 be indexed. Re
181d0 74 75 72 6e 20 65 61 72 6c 79 20 69 66 20 6e 6f turn early if no
181e0 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 t found.. */.
181f0 69 66 28 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 if( pTblName!=0
18200 29 7b 0a 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 ){.. /* Use t
18210 68 65 20 74 77 6f 2d 70 61 72 74 20 69 6e 64 65 he two-part inde
18220 78 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d x name to determ
18230 69 6e 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ine the database
18240 20 0a 20 20 20 20 2a 2a 20 74 6f 20 73 65 61 72 . ** to sear
18250 63 68 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 ch for the table
18260 2e 20 27 46 69 78 27 20 74 68 65 20 74 61 62 6c . 'Fix' the tabl
18270 65 20 6e 61 6d 65 20 74 6f 20 74 68 69 73 20 64 e name to this d
18280 62 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 b. ** before
18290 6c 6f 6f 6b 69 6e 67 20 75 70 20 74 68 65 20 74 looking up the t
182a0 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 able.. */.
182b0 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 20 assert( pName1
182c0 26 26 20 70 4e 61 6d 65 32 20 29 3b 0a 20 20 20 && pName2 );.
182d0 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 iDb = sqlite3Tw
182e0 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 oPartName(pParse
182f0 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 , pName1, pName2
18300 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 , &pName);. i
18310 66 28 20 69 44 62 3c 30 20 29 20 67 6f 74 6f 20 f( iDb<0 ) goto
18320 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
18330 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 x;. assert( p
18340 4e 61 6d 65 20 26 26 20 70 4e 61 6d 65 2d 3e 7a Name && pName->z
18350 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c );..#ifndef SQL
18360 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a ITE_OMIT_TEMPDB.
18370 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 6e /* If the in
18380 64 65 78 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 dex name was unq
18390 75 61 6c 69 66 69 65 64 2c 20 63 68 65 63 6b 20 ualified, check
183a0 69 66 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 if the table.
183b0 20 2a 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 ** is a temp ta
183c0 62 6c 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 ble. If so, set
183d0 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
183e0 31 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 1. Do not do thi
183f0 73 0a 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 s. ** if init
18400 69 61 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62 ialising a datab
18410 61 73 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 ase schema..
18420 2a 2f 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e */. if( !db->
18430 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 init.busy ){.
18440 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 pTab = sqlite
18450 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 3SrcListLookup(p
18460 50 61 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 Parse, pTblName)
18470 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d ;. if( pNam
18480 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 e2->n==0 && pTab
18490 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d && pTab->pSchem
184a0 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 a==db->aDb[1].pS
184b0 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 chema ){.
184c0 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 iDb = 1;.
184d0 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }. }.#endif..
184e0 20 20 20 20 73 71 6c 69 74 65 33 46 69 78 49 6e sqlite3FixIn
184f0 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 it(&sFix, pParse
18500 2c 20 69 44 62 2c 20 22 69 6e 64 65 78 22 2c 20 , iDb, "index",
18510 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 pName);. if(
18520 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 sqlite3FixSrcLis
18530 74 28 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d t(&sFix, pTblNam
18540 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 42 e) ){. /* B
18550 65 63 61 75 73 65 20 74 68 65 20 70 61 72 73 65 ecause the parse
18560 72 20 63 6f 6e 73 74 72 75 63 74 73 20 70 54 62 r constructs pTb
18570 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20 73 69 6e lName from a sin
18580 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c 0a gle identifier,.
18590 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 ** sqlite3
185a0 46 69 78 53 72 63 4c 69 73 74 20 63 61 6e 20 6e FixSrcList can n
185b0 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 ever fail. */.
185c0 20 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 assert(0);.
185d0 20 20 20 7d 0a 20 20 20 20 70 54 61 62 20 3d 20 }. pTab =
185e0 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
185f0 6c 65 49 74 65 6d 28 70 50 61 72 73 65 2c 20 30 leItem(pParse, 0
18600 2c 20 26 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 , &pTblName->a[0
18610 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ]);. assert(
18620 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
18630 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 30 20 29 ==0 || pTab==0 )
18640 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d ;. if( pTab==
18650 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 0 ) goto exit_cr
18660 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 eate_index;.
18670 69 66 28 20 69 44 62 3d 3d 31 20 26 26 20 64 62 if( iDb==1 && db
18680 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 ->aDb[iDb].pSche
18690 6d 61 21 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d ma!=pTab->pSchem
186a0 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 a ){. sqlit
186b0 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
186c0 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 22 e, . "
186d0 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 61 20 cannot create a
186e0 54 45 4d 50 20 69 6e 64 65 78 20 6f 6e 20 6e 6f TEMP index on no
186f0 6e 2d 54 45 4d 50 20 74 61 62 6c 65 20 5c 22 25 n-TEMP table \"%
18700 73 5c 22 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\"",.
18710 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pTab->zName);.
18720 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 goto exit_c
18730 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 reate_index;.
18740 20 7d 0a 20 20 20 20 69 66 28 20 21 48 61 73 52 }. if( !HasR
18750 6f 77 69 64 28 70 54 61 62 29 20 29 20 70 50 6b owid(pTab) ) pPk
18760 20 3d 20 73 71 6c 69 74 65 33 50 72 69 6d 61 72 = sqlite3Primar
18770 79 4b 65 79 49 6e 64 65 78 28 70 54 61 62 29 3b yKeyIndex(pTab);
18780 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
18790 73 65 72 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29 sert( pName==0 )
187a0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 ;. assert( pS
187b0 74 61 72 74 3d 3d 30 20 29 3b 0a 20 20 20 20 70 tart==0 );. p
187c0 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e Tab = pParse->pN
187d0 65 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28 ewTable;. if(
187e0 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 !pTab ) goto ex
187f0 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b it_create_index;
18800 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 . iDb = sqlit
18810 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
18820 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d db, pTab->pSchem
18830 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20 a);. }. pDb =
18840 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a &db->aDb[iDb];..
18850 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d assert( pTab!=
18860 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
18870 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 Parse->nErr==0 )
18880 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 ;. if( sqlite3S
18890 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e trNICmp(pTab->zN
188a0 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 ame, "sqlite_",
188b0 37 29 3d 3d 30 20 0a 20 20 20 20 20 20 20 26 26 7)==0 . &&
188c0 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d db->init.busy==
188d0 30 0a 23 69 66 20 53 51 4c 49 54 45 5f 55 53 45 0.#if SQLITE_USE
188e0 52 5f 41 55 54 48 45 4e 54 49 43 41 54 49 4f 4e R_AUTHENTICATION
188f0 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 . && sqlit
18900 65 33 55 73 65 72 41 75 74 68 54 61 62 6c 65 28 e3UserAuthTable(
18910 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 0a pTab->zName)==0.
18920 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 26 26 #endif. &&
18930 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 sqlite3StrNICmp
18940 28 26 70 54 61 62 2d 3e 7a 4e 61 6d 65 5b 37 5d (&pTab->zName[7]
18950 2c 22 61 6c 74 65 72 74 61 62 5f 22 2c 39 29 21 ,"altertab_",9)!
18960 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
18970 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
18980 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 , "table %s may
18990 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 2c not be indexed",
189a0 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pTab->zName);.
189b0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
189c0 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 ate_index;. }.#
189d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
189e0 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70 54 IT_VIEW. if( pT
189f0 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 ab->pSelect ){.
18a00 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
18a10 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 sg(pParse, "view
18a20 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 s may not be ind
18a30 65 78 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f exed");. goto
18a40 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
18a50 65 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 ex;. }.#endif.#
18a60 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18a70 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
18a80 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 if( IsVirtual(
18a90 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c pTab) ){. sql
18aa0 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
18ab0 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61 rse, "virtual ta
18ac0 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 bles may not be
18ad0 69 6e 64 65 78 65 64 22 29 3b 0a 20 20 20 20 67 indexed");. g
18ae0 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
18af0 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 index;. }.#endi
18b00 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e f.. /*. ** Fin
18b10 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 d the name of th
18b20 65 20 69 6e 64 65 78 2e 20 20 4d 61 6b 65 20 73 e index. Make s
18b30 75 72 65 20 74 68 65 72 65 20 69 73 20 6e 6f 74 ure there is not
18b40 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 72 already another
18b50 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 . ** index or t
18b60 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 61 able with the sa
18b70 6d 65 20 6e 61 6d 65 2e 20 20 0a 20 20 2a 2a 0a me name. . **.
18b80 20 20 2a 2a 20 45 78 63 65 70 74 69 6f 6e 3a 20 ** Exception:
18b90 20 49 66 20 77 65 20 61 72 65 20 72 65 61 64 69 If we are readi
18ba0 6e 67 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 ng the names of
18bb0 70 65 72 6d 61 6e 65 6e 74 20 69 6e 64 69 63 65 permanent indice
18bc0 73 20 66 72 6f 6d 20 74 68 65 0a 20 20 2a 2a 20 s from the. **
18bd0 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
18be0 62 6c 65 20 28 62 65 63 61 75 73 65 20 73 6f 6d ble (because som
18bf0 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 e other process
18c00 63 68 61 6e 67 65 64 20 74 68 65 20 73 63 68 65 changed the sche
18c10 6d 61 29 20 61 6e 64 0a 20 20 2a 2a 20 6f 6e 65 ma) and. ** one
18c20 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61 of the index na
18c30 6d 65 73 20 63 6f 6c 6c 69 64 65 73 20 77 69 74 mes collides wit
18c40 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 h the name of a
18c50 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 temporary table
18c60 6f 72 0a 20 20 2a 2a 20 69 6e 64 65 78 2c 20 74 or. ** index, t
18c70 68 65 6e 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 hen we will cont
18c80 69 6e 75 65 20 74 6f 20 70 72 6f 63 65 73 73 20 inue to process
18c90 74 68 69 73 20 69 6e 64 65 78 2e 0a 20 20 2a 2a this index.. **
18ca0 0a 20 20 2a 2a 20 49 66 20 70 4e 61 6d 65 3d 3d . ** If pName==
18cb0 30 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 0 it means that
18cc0 77 65 20 61 72 65 0a 20 20 2a 2a 20 64 65 61 6c we are. ** deal
18cd0 69 6e 67 20 77 69 74 68 20 61 20 70 72 69 6d 61 ing with a prima
18ce0 72 79 20 6b 65 79 20 6f 72 20 55 4e 49 51 55 45 ry key or UNIQUE
18cf0 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 57 65 constraint. We
18d00 20 68 61 76 65 20 74 6f 20 69 6e 76 65 6e 74 20 have to invent
18d10 6f 75 72 0a 20 20 2a 2a 20 6f 77 6e 20 6e 61 6d our. ** own nam
18d20 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4e e.. */. if( pN
18d30 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 ame ){. zName
18d40 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
18d50 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d omToken(db, pNam
18d60 65 29 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d e);. if( zNam
18d70 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 e==0 ) goto exit
18d80 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
18d90 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 assert( pName
18da0 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 ->z!=0 );. if
18db0 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c ( SQLITE_OK!=sql
18dc0 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e ite3CheckObjectN
18dd0 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d ame(pParse, zNam
18de0 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f e) ){. goto
18df0 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
18e00 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ex;. }. if
18e10 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 ( !db->init.busy
18e20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 ){. if( sq
18e30 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 lite3FindTable(d
18e40 62 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 b, zName, 0)!=0
18e50 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
18e60 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
18e70 65 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 e, "there is alr
18e80 65 61 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d eady a table nam
18e90 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a ed %s", zName);.
18ea0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 goto exi
18eb0 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
18ec0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
18ed0 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e if( sqlite3Fin
18ee0 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 dIndex(db, zName
18ef0 2c 20 70 44 62 2d 3e 7a 44 62 53 4e 61 6d 65 29 , pDb->zDbSName)
18f00 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 !=0 ){. if(
18f10 20 21 69 66 4e 6f 74 45 78 69 73 74 20 29 7b 0a !ifNotExist ){.
18f20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
18f30 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
18f40 22 69 6e 64 65 78 20 25 73 20 61 6c 72 65 61 64 "index %s alread
18f50 79 20 65 78 69 73 74 73 22 2c 20 7a 4e 61 6d 65 y exists", zName
18f60 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
18f70 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
18f80 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 !db->init.busy )
18f90 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
18fa0 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 3CodeVerifySchem
18fb0 61 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a a(pParse, iDb);.
18fc0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f }. go
18fd0 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
18fe0 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 ndex;. }. }e
18ff0 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a lse{. int n;.
19000 20 20 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 Index *pLoop
19010 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d ;. for(pLoop=
19020 70 54 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d pTab->pIndex, n=
19030 31 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 1; pLoop; pLoop=
19040 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b pLoop->pNext, n+
19050 2b 29 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d +){}. zName =
19060 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
19070 64 62 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f db, "sqlite_auto
19080 69 6e 64 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 index_%s_%d", pT
19090 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 ab->zName, n);.
190a0 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 if( zName==0
190b0 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 ){. goto ex
190c0 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b it_create_index;
190d0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 . }.. /* A
190e0 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 6e utomatic index n
190f0 61 6d 65 73 20 67 65 6e 65 72 61 74 65 64 20 66 ames generated f
19100 72 6f 6d 20 77 69 74 68 69 6e 20 73 71 6c 69 74 rom within sqlit
19110 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 e3_declare_vtab(
19120 29 0a 20 20 20 20 2a 2a 20 6d 75 73 74 20 68 61 ). ** must ha
19130 76 65 20 6e 61 6d 65 73 20 74 68 61 74 20 61 72 ve names that ar
19140 65 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 e distinct from
19150 6e 6f 72 6d 61 6c 20 61 75 74 6f 6d 61 74 69 63 normal automatic
19160 20 69 6e 64 65 78 20 6e 61 6d 65 73 2e 0a 20 20 index names..
19170 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ** The followi
19180 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e ng statement con
19190 76 65 72 74 73 20 22 73 71 6c 69 74 65 33 5f 61 verts "sqlite3_a
191a0 75 74 6f 69 6e 64 65 78 2e 2e 2e 22 20 69 6e 74 utoindex..." int
191b0 6f 0a 20 20 20 20 2a 2a 20 22 73 71 6c 69 74 65 o. ** "sqlite
191c0 33 5f 62 75 74 6f 69 6e 64 65 78 2e 2e 2e 22 20 3_butoindex..."
191d0 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65 in order to make
191e0 20 74 68 65 20 6e 61 6d 65 73 20 64 69 73 74 69 the names disti
191f0 6e 63 74 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 nct.. ** The
19200 22 76 74 61 62 5f 65 72 72 2e 74 65 73 74 22 20 "vtab_err.test"
19210 74 65 73 74 20 64 65 6d 6f 6e 73 74 72 61 74 65 test demonstrate
19220 73 20 74 68 65 20 6e 65 65 64 20 6f 66 20 74 68 s the need of th
19230 69 73 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f is statement. */
19240 0a 20 20 20 20 69 66 28 20 49 4e 5f 44 45 43 4c . if( IN_DECL
19250 41 52 45 5f 56 54 41 42 20 29 20 7a 4e 61 6d 65 ARE_VTAB ) zName
19260 5b 37 5d 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a [7]++;. }.. /*
19270 20 43 68 65 63 6b 20 66 6f 72 20 61 75 74 68 6f Check for autho
19280 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 rization to crea
19290 74 65 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a te an index.. *
192a0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
192b0 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
192c0 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 ION. {. cons
192d0 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 t char *zDb = pD
192e0 62 2d 3e 7a 44 62 53 4e 61 6d 65 3b 0a 20 20 20 b->zDbSName;.
192f0 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 if( sqlite3Auth
19300 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 Check(pParse, SQ
19310 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 LITE_INSERT, SCH
19320 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 EMA_TABLE(iDb),
19330 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 0, zDb) ){.
19340 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 goto exit_creat
19350 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 e_index;. }.
19360 20 20 20 69 20 3d 20 53 51 4c 49 54 45 5f 43 52 i = SQLITE_CR
19370 45 41 54 45 5f 49 4e 44 45 58 3b 0a 20 20 20 20 EATE_INDEX;.
19380 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 if( !OMIT_TEMPDB
19390 20 26 26 20 69 44 62 3d 3d 31 20 29 20 69 20 3d && iDb==1 ) i =
193a0 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
193b0 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 EMP_INDEX;. i
193c0 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 f( sqlite3AuthCh
193d0 65 63 6b 28 70 50 61 72 73 65 2c 20 69 2c 20 7a eck(pParse, i, z
193e0 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d Name, pTab->zNam
193f0 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 e, zDb) ){.
19400 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 goto exit_creat
19410 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 e_index;. }.
19420 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
19430 49 66 20 70 4c 69 73 74 3d 3d 30 2c 20 69 74 20 If pList==0, it
19440 6d 65 61 6e 73 20 74 68 69 73 20 72 6f 75 74 69 means this routi
19450 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f ne was called to
19460 20 6d 61 6b 65 20 61 20 70 72 69 6d 61 72 79 0a make a primary.
19470 20 20 2a 2a 20 6b 65 79 20 6f 75 74 20 6f 66 20 ** key out of
19480 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 the last column
19490 61 64 64 65 64 20 74 6f 20 74 68 65 20 74 61 62 added to the tab
194a0 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 le under constru
194b0 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 63 ction.. ** So c
194c0 72 65 61 74 65 20 61 20 66 61 6b 65 20 6c 69 73 reate a fake lis
194d0 74 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 74 68 t to simulate th
194e0 69 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 is.. */. if( p
194f0 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 54 List==0 ){. T
19500 6f 6b 65 6e 20 70 72 65 76 43 6f 6c 3b 0a 20 20 oken prevCol;.
19510 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 49 6e sqlite3TokenIn
19520 69 74 28 26 70 72 65 76 43 6f 6c 2c 20 70 54 61 it(&prevCol, pTa
19530 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 b->aCol[pTab->nC
19540 6f 6c 2d 31 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 ol-1].zName);.
19550 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 pList = sqlite
19560 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 3ExprListAppend(
19570 70 50 61 72 73 65 2c 20 30 2c 0a 20 20 20 20 20 pParse, 0,.
19580 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
19590 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b ExprAlloc(db, TK
195a0 5f 49 44 2c 20 26 70 72 65 76 43 6f 6c 2c 20 30 _ID, &prevCol, 0
195b0 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 ));. if( pLis
195c0 74 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 t==0 ) goto exit
195d0 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
195e0 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 assert( pList
195f0 2d 3e 6e 45 78 70 72 3d 3d 31 20 29 3b 0a 20 20 ->nExpr==1 );.
19600 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 sqlite3ExprLis
19610 74 53 65 74 53 6f 72 74 4f 72 64 65 72 28 70 4c tSetSortOrder(pL
19620 69 73 74 2c 20 73 6f 72 74 4f 72 64 65 72 29 3b ist, sortOrder);
19630 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
19640 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 lite3ExprListChe
19650 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c ckLength(pParse,
19660 20 70 4c 69 73 74 2c 20 22 69 6e 64 65 78 22 29 pList, "index")
19670 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 ;. }.. /* Figu
19680 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 re out how many
19690 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 bytes of space a
196a0 72 65 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 re required to s
196b0 74 6f 72 65 20 65 78 70 6c 69 63 69 74 6c 79 0a tore explicitly.
196c0 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 63 ** specified c
196d0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
196e0 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 e names.. */.
196f0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 for(i=0; i<pList
19700 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 ->nExpr; i++){.
19710 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d Expr *pExpr =
19720 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 pList->a[i].pEx
19730 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 pr;. assert(
19740 70 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 pExpr!=0 );.
19750 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 if( pExpr->op==T
19760 4b 5f 43 4f 4c 4c 41 54 45 20 29 7b 0a 20 20 20 K_COLLATE ){.
19770 20 20 20 6e 45 78 74 72 61 20 2b 3d 20 28 31 20 nExtra += (1
19780 2b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 + sqlite3Strlen3
19790 30 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 0(pExpr->u.zToke
197a0 6e 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a n));. }. }..
197b0 20 20 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 /* . ** Alloc
197c0 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 ate the index st
197d0 72 75 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 ructure. . */.
197e0 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 nName = sqlite3
197f0 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b Strlen30(zName);
19800 0a 20 20 6e 45 78 74 72 61 43 6f 6c 20 3d 20 70 . nExtraCol = p
19810 50 6b 20 3f 20 70 50 6b 2d 3e 6e 4b 65 79 43 6f Pk ? pPk->nKeyCo
19820 6c 20 3a 20 31 3b 0a 20 20 70 49 6e 64 65 78 20 l : 1;. pIndex
19830 3d 20 73 71 6c 69 74 65 33 41 6c 6c 6f 63 61 74 = sqlite3Allocat
19840 65 49 6e 64 65 78 4f 62 6a 65 63 74 28 64 62 2c eIndexObject(db,
19850 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 2b 20 pList->nExpr +
19860 6e 45 78 74 72 61 43 6f 6c 2c 0a 20 20 20 20 20 nExtraCol,.
19870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19890 20 6e 4e 61 6d 65 20 2b 20 6e 45 78 74 72 61 20 nName + nExtra
198a0 2b 20 31 2c 20 26 7a 45 78 74 72 61 29 3b 0a 20 + 1, &zExtra);.
198b0 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
198c0 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 ailed ){. got
198d0 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e o exit_create_in
198e0 64 65 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 dex;. }. asser
198f0 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c t( EIGHT_BYTE_AL
19900 49 47 4e 4d 45 4e 54 28 70 49 6e 64 65 78 2d 3e IGNMENT(pIndex->
19910 61 69 52 6f 77 4c 6f 67 45 73 74 29 20 29 3b 0a aiRowLogEst) );.
19920 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
19930 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 BYTE_ALIGNMENT(p
19940 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 29 20 29 Index->azColl) )
19950 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d ;. pIndex->zNam
19960 65 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 7a 45 e = zExtra;. zE
19970 78 74 72 61 20 2b 3d 20 6e 4e 61 6d 65 20 2b 20 xtra += nName +
19980 31 3b 0a 20 20 6d 65 6d 63 70 79 28 70 49 6e 64 1;. memcpy(pInd
19990 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 ex->zName, zName
199a0 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 70 49 , nName+1);. pI
199b0 6e 64 65 78 2d 3e 70 54 61 62 6c 65 20 3d 20 70 ndex->pTable = p
199c0 54 61 62 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6f Tab;. pIndex->o
199d0 6e 45 72 72 6f 72 20 3d 20 28 75 38 29 6f 6e 45 nError = (u8)onE
199e0 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e rror;. pIndex->
199f0 75 6e 69 71 4e 6f 74 4e 75 6c 6c 20 3d 20 6f 6e uniqNotNull = on
19a00 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 3b 0a Error!=OE_None;.
19a10 20 20 70 49 6e 64 65 78 2d 3e 69 64 78 54 79 70 pIndex->idxTyp
19a20 65 20 3d 20 69 64 78 54 79 70 65 3b 0a 20 20 70 e = idxType;. p
19a30 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 20 3d Index->pSchema =
19a40 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 db->aDb[iDb].pS
19a50 63 68 65 6d 61 3b 0a 20 20 70 49 6e 64 65 78 2d chema;. pIndex-
19a60 3e 6e 4b 65 79 43 6f 6c 20 3d 20 70 4c 69 73 74 >nKeyCol = pList
19a70 2d 3e 6e 45 78 70 72 3b 0a 20 20 69 66 28 20 70 ->nExpr;. if( p
19a80 50 49 57 68 65 72 65 20 29 7b 0a 20 20 20 20 73 PIWhere ){. s
19a90 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c qlite3ResolveSel
19aa0 66 52 65 66 65 72 65 6e 63 65 28 70 50 61 72 73 fReference(pPars
19ab0 65 2c 20 70 54 61 62 2c 20 4e 43 5f 50 61 72 74 e, pTab, NC_Part
19ac0 49 64 78 2c 20 70 50 49 57 68 65 72 65 2c 20 30 Idx, pPIWhere, 0
19ad0 29 3b 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 );. pIndex->p
19ae0 50 61 72 74 49 64 78 57 68 65 72 65 20 3d 20 70 PartIdxWhere = p
19af0 50 49 57 68 65 72 65 3b 0a 20 20 20 20 70 50 49 PIWhere;. pPI
19b00 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 Where = 0;. }.
19b10 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
19b20 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 SchemaMutexHeld(
19b30 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 0a db, iDb, 0) );..
19b40 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 /* Check to se
19b50 65 20 69 66 20 77 65 20 73 68 6f 75 6c 64 20 68 e if we should h
19b60 6f 6e 6f 72 20 44 45 53 43 20 72 65 71 75 65 73 onor DESC reques
19b70 74 73 20 6f 6e 20 69 6e 64 65 78 20 63 6f 6c 75 ts on index colu
19b80 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 mns. */. if( p
19b90 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c Db->pSchema->fil
19ba0 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 29 7b 0a 20 e_format>=4 ){.
19bb0 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b sortOrderMask
19bc0 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 48 6f 6e 6f = -1; /* Hono
19bd0 72 20 44 45 53 43 20 2a 2f 0a 20 20 7d 65 6c 73 r DESC */. }els
19be0 65 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 e{. sortOrder
19bf0 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 2f 2a 20 Mask = 0; /*
19c00 49 67 6e 6f 72 65 20 44 45 53 43 20 2a 2f 0a 20 Ignore DESC */.
19c10 20 7d 0a 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 }.. /* Analyze
19c20 20 74 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 the list of exp
19c30 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 66 6f ressions that fo
19c40 72 6d 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 rm the terms of
19c50 74 68 65 20 69 6e 64 65 78 20 61 6e 64 0a 20 20 the index and.
19c60 2a 2a 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 ** report any er
19c70 72 6f 72 73 2e 20 20 49 6e 20 74 68 65 20 63 6f rors. In the co
19c80 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 mmon case where
19c90 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
19ca0 73 20 65 78 61 63 74 6c 79 0a 20 20 2a 2a 20 61 s exactly. ** a
19cb0 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 2c 20 73 table column, s
19cc0 74 6f 72 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e tore that column
19cd0 20 69 6e 20 61 69 43 6f 6c 75 6d 6e 5b 5d 2e 20 in aiColumn[].
19ce0 20 46 6f 72 20 67 65 6e 65 72 61 6c 20 65 78 70 For general exp
19cf0 72 65 73 73 69 6f 6e 73 2c 0a 20 20 2a 2a 20 70 ressions,. ** p
19d00 6f 70 75 6c 61 74 65 20 70 49 6e 64 65 78 2d 3e opulate pIndex->
19d10 61 43 6f 6c 45 78 70 72 20 61 6e 64 20 73 74 6f aColExpr and sto
19d20 72 65 20 58 4e 5f 45 58 50 52 20 28 2d 32 29 20 re XN_EXPR (-2)
19d30 69 6e 20 61 69 43 6f 6c 75 6d 6e 5b 5d 2e 0a 20 in aiColumn[]..
19d40 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 49 **. ** TODO: I
19d50 73 73 75 65 20 61 20 77 61 72 6e 69 6e 67 20 69 ssue a warning i
19d60 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f f two or more co
19d70 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 69 6e 64 lumns of the ind
19d80 65 78 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c ex are identical
19d90 2e 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 49 73 73 .. ** TODO: Iss
19da0 75 65 20 61 20 77 61 72 6e 69 6e 67 20 69 66 20 ue a warning if
19db0 74 68 65 20 74 61 62 6c 65 20 70 72 69 6d 61 72 the table primar
19dc0 79 20 6b 65 79 20 69 73 20 75 73 65 64 20 61 73 y key is used as
19dd0 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 20 2a part of the. *
19de0 2a 20 69 6e 64 65 78 20 6b 65 79 2e 0a 20 20 2a * index key.. *
19df0 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 69 /. for(i=0, pLi
19e00 73 74 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b stItem=pList->a;
19e10 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b i<pList->nExpr;
19e20 20 69 2b 2b 2c 20 70 4c 69 73 74 49 74 65 6d 2b i++, pListItem+
19e30 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 43 +){. Expr *pC
19e40 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
19e50 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d /* The i-
19e60 74 68 20 69 6e 64 65 78 20 65 78 70 72 65 73 73 th index express
19e70 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 ion */. int r
19e80 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 equestedSortOrde
19e90 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 53 43 r; /* ASC
19ea0 20 6f 72 20 44 45 53 43 20 6f 6e 20 74 68 65 20 or DESC on the
19eb0 69 2d 74 68 20 65 78 70 72 65 73 73 69 6f 6e 20 i-th expression
19ec0 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 */. const cha
19ed0 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 r *zColl;
19ee0 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 /* Collati
19ef0 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 on sequence name
19f00 20 2a 2f 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 */.. sqlite3
19f10 53 74 72 69 6e 67 54 6f 49 64 28 70 4c 69 73 74 StringToId(pList
19f20 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 Item->pExpr);.
19f30 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 sqlite3Resolve
19f40 53 65 6c 66 52 65 66 65 72 65 6e 63 65 28 70 50 SelfReference(pP
19f50 61 72 73 65 2c 20 70 54 61 62 2c 20 4e 43 5f 49 arse, pTab, NC_I
19f60 64 78 45 78 70 72 2c 20 70 4c 69 73 74 49 74 65 dxExpr, pListIte
19f70 6d 2d 3e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 m->pExpr, 0);.
19f80 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 if( pParse->nE
19f90 72 72 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 rr ) goto exit_c
19fa0 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 reate_index;.
19fb0 20 70 43 45 78 70 72 20 3d 20 73 71 6c 69 74 65 pCExpr = sqlite
19fc0 33 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 74 65 3ExprSkipCollate
19fd0 28 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 (pListItem->pExp
19fe0 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 45 78 r);. if( pCEx
19ff0 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d pr->op!=TK_COLUM
1a000 4e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 N ){. if( p
1a010 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e 65 Tab==pParse->pNe
1a020 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 wTable ){.
1a030 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1a040 67 28 70 50 61 72 73 65 2c 20 22 65 78 70 72 65 g(pParse, "expre
1a050 73 73 69 6f 6e 73 20 70 72 6f 68 69 62 69 74 65 ssions prohibite
1a060 64 20 69 6e 20 50 52 49 4d 41 52 59 20 4b 45 59 d in PRIMARY KEY
1a070 20 61 6e 64 20 22 0a 20 20 20 20 20 20 20 20 20 and ".
1a080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a090 20 20 20 20 20 20 20 22 55 4e 49 51 55 45 20 63 "UNIQUE c
1a0a0 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 onstraints");.
1a0b0 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f goto exit_
1a0c0 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
1a0d0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1a0e0 70 49 6e 64 65 78 2d 3e 61 43 6f 6c 45 78 70 72 pIndex->aColExpr
1a0f0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 45 ==0 ){. E
1a100 78 70 72 4c 69 73 74 20 2a 70 43 6f 70 79 20 3d xprList *pCopy =
1a110 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
1a120 44 75 70 28 64 62 2c 20 70 4c 69 73 74 2c 20 30 Dup(db, pList, 0
1a130 29 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 );. pInde
1a140 78 2d 3e 61 43 6f 6c 45 78 70 72 20 3d 20 70 43 x->aColExpr = pC
1a150 6f 70 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 opy;. if(
1a160 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c !db->mallocFail
1a170 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ed ){.
1a180 61 73 73 65 72 74 28 20 70 43 6f 70 79 21 3d 30 assert( pCopy!=0
1a190 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4c );. pL
1a1a0 69 73 74 49 74 65 6d 20 3d 20 26 70 43 6f 70 79 istItem = &pCopy
1a1b0 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 ->a[i];.
1a1c0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
1a1d0 6a 20 3d 20 58 4e 5f 45 58 50 52 3b 0a 20 20 20 j = XN_EXPR;.
1a1e0 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c pIndex->aiCol
1a1f0 75 6d 6e 5b 69 5d 20 3d 20 58 4e 5f 45 58 50 52 umn[i] = XN_EXPR
1a200 3b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e ;. pIndex->
1a210 75 6e 69 71 4e 6f 74 4e 75 6c 6c 20 3d 20 30 3b uniqNotNull = 0;
1a220 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1a230 20 20 6a 20 3d 20 70 43 45 78 70 72 2d 3e 69 43 j = pCExpr->iC
1a240 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 61 73 73 olumn;. ass
1a250 65 72 74 28 20 6a 3c 3d 30 78 37 66 66 66 20 29 ert( j<=0x7fff )
1a260 3b 0a 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20 ;. if( j<0
1a270 29 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 70 ){. j = p
1a280 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 Tab->iPKey;.
1a290 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 61 62 }else if( pTab
1a2a0 2d 3e 61 43 6f 6c 5b 6a 5d 2e 6e 6f 74 4e 75 6c ->aCol[j].notNul
1a2b0 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 l==0 ){.
1a2c0 70 49 6e 64 65 78 2d 3e 75 6e 69 71 4e 6f 74 4e pIndex->uniqNotN
1a2d0 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d ull = 0;. }
1a2e0 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 . pIndex->a
1a2f0 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 28 69 31 iColumn[i] = (i1
1a300 36 29 6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 6)j;. }. z
1a310 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 Coll = 0;. if
1a320 28 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 ( pListItem->pEx
1a330 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 4c 41 pr->op==TK_COLLA
1a340 54 45 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 TE ){. int
1a350 6e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7a 43 6f nColl;. zCo
1a360 6c 6c 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e ll = pListItem->
1a370 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b pExpr->u.zToken;
1a380 0a 20 20 20 20 20 20 6e 43 6f 6c 6c 20 3d 20 73 . nColl = s
1a390 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
1a3a0 43 6f 6c 6c 29 20 2b 20 31 3b 0a 20 20 20 20 20 Coll) + 1;.
1a3b0 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61 3e assert( nExtra>
1a3c0 3d 6e 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 =nColl );.
1a3d0 6d 65 6d 63 70 79 28 7a 45 78 74 72 61 2c 20 7a memcpy(zExtra, z
1a3e0 43 6f 6c 6c 2c 20 6e 43 6f 6c 6c 29 3b 0a 20 20 Coll, nColl);.
1a3f0 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 7a 45 78 74 zColl = zExt
1a400 72 61 3b 0a 20 20 20 20 20 20 7a 45 78 74 72 61 ra;. zExtra
1a410 20 2b 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 20 += nColl;.
1a420 20 6e 45 78 74 72 61 20 2d 3d 20 6e 43 6f 6c 6c nExtra -= nColl
1a430 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1a440 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 43 j>=0 ){. zC
1a450 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c oll = pTab->aCol
1a460 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 7d [j].zColl;. }
1a470 0a 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 . if( !zColl
1a480 29 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 ) zColl = sqlite
1a490 33 53 74 72 42 49 4e 41 52 59 3b 0a 20 20 20 20 3StrBINARY;.
1a4a0 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 if( !db->init.bu
1a4b0 73 79 20 26 26 20 21 73 71 6c 69 74 65 33 4c 6f sy && !sqlite3Lo
1a4c0 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 cateCollSeq(pPar
1a4d0 73 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 se, zColl) ){.
1a4e0 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 goto exit_cr
1a4f0 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 eate_index;.
1a500 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a }. pIndex->az
1a510 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b Coll[i] = zColl;
1a520 0a 20 20 20 20 72 65 71 75 65 73 74 65 64 53 6f . requestedSo
1a530 72 74 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 49 rtOrder = pListI
1a540 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 tem->sortOrder &
1a550 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a sortOrderMask;.
1a560 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 pIndex->aSor
1a570 74 4f 72 64 65 72 5b 69 5d 20 3d 20 28 75 38 29 tOrder[i] = (u8)
1a580 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 requestedSortOrd
1a590 65 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70 er;. }.. /* Ap
1a5a0 70 65 6e 64 20 74 68 65 20 74 61 62 6c 65 20 6b pend the table k
1a5b0 65 79 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 ey to the end of
1a5c0 20 74 68 65 20 69 6e 64 65 78 2e 20 20 46 6f 72 the index. For
1a5d0 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 0a 20 WITHOUT ROWID.
1a5e0 20 2a 2a 20 74 61 62 6c 65 73 20 28 77 68 65 6e ** tables (when
1a5f0 20 70 50 6b 21 3d 30 29 20 74 68 69 73 20 77 69 pPk!=0) this wi
1a600 6c 6c 20 62 65 20 74 68 65 20 64 65 63 6c 61 72 ll be the declar
1a610 65 64 20 50 52 49 4d 41 52 59 20 4b 45 59 2e 20 ed PRIMARY KEY.
1a620 20 46 6f 72 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c For. ** normal
1a630 20 74 61 62 6c 65 73 20 28 77 68 65 6e 20 70 50 tables (when pP
1a640 6b 3d 3d 30 29 20 74 68 69 73 20 77 69 6c 6c 20 k==0) this will
1a650 62 65 20 74 68 65 20 72 6f 77 69 64 2e 0a 20 20 be the rowid..
1a660 2a 2f 0a 20 20 69 66 28 20 70 50 6b 20 29 7b 0a */. if( pPk ){.
1a670 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 for(j=0; j<p
1a680 50 6b 2d 3e 6e 4b 65 79 43 6f 6c 3b 20 6a 2b 2b Pk->nKeyCol; j++
1a690 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 78 20 3d ){. int x =
1a6a0 20 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a pPk->aiColumn[j
1a6b0 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ];. assert(
1a6c0 20 78 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 x>=0 );. i
1a6d0 66 28 20 68 61 73 43 6f 6c 75 6d 6e 28 70 49 6e f( hasColumn(pIn
1a6e0 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 2c 20 70 dex->aiColumn, p
1a6f0 49 6e 64 65 78 2d 3e 6e 4b 65 79 43 6f 6c 2c 20 Index->nKeyCol,
1a700 78 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 x) ){. pI
1a710 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 2d 2d 3b ndex->nColumn--;
1a720 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 . }else{.
1a730 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 pIndex->a
1a740 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 78 3b 0a iColumn[i] = x;.
1a750 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e pIndex->
1a760 61 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 70 50 6b 2d azColl[i] = pPk-
1a770 3e 61 7a 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 >azColl[j];.
1a780 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 pIndex->aSor
1a790 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 50 6b 2d tOrder[i] = pPk-
1a7a0 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 3b 0a >aSortOrder[j];.
1a7b0 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 i++;.
1a7c0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 }. }. a
1a7d0 73 73 65 72 74 28 20 69 3d 3d 70 49 6e 64 65 78 ssert( i==pIndex
1a7e0 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 7d ->nColumn );. }
1a7f0 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 64 65 78 else{. pIndex
1a800 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 ->aiColumn[i] =
1a810 58 4e 5f 52 4f 57 49 44 3b 0a 20 20 20 20 70 49 XN_ROWID;. pI
1a820 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20 ndex->azColl[i]
1a830 3d 20 73 71 6c 69 74 65 33 53 74 72 42 49 4e 41 = sqlite3StrBINA
1a840 52 59 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 RY;. }. sqlite
1a850 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 70 3DefaultRowEst(p
1a860 49 6e 64 65 78 29 3b 0a 20 20 69 66 28 20 70 50 Index);. if( pP
1a870 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d arse->pNewTable=
1a880 3d 30 20 29 20 65 73 74 69 6d 61 74 65 49 6e 64 =0 ) estimateInd
1a890 65 78 57 69 64 74 68 28 70 49 6e 64 65 78 29 3b exWidth(pIndex);
1a8a0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
1a8b0 6e 64 65 78 20 63 6f 6e 74 61 69 6e 73 20 65 76 ndex contains ev
1a8c0 65 72 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 69 74 ery column of it
1a8d0 73 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 s table, then ma
1a8e0 72 6b 0a 20 20 2a 2a 20 69 74 20 61 73 20 61 20 rk. ** it as a
1a8f0 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 20 2a covering index *
1a900 2f 0a 20 20 61 73 73 65 72 74 28 20 48 61 73 52 /. assert( HasR
1a910 6f 77 69 64 28 70 54 61 62 29 20 0a 20 20 20 20 owid(pTab) .
1a920 20 20 7c 7c 20 70 54 61 62 2d 3e 69 50 4b 65 79 || pTab->iPKey
1a930 3c 30 20 7c 7c 20 73 71 6c 69 74 65 33 43 6f 6c <0 || sqlite3Col
1a940 75 6d 6e 4f 66 49 6e 64 65 78 28 70 49 6e 64 65 umnOfIndex(pInde
1a950 78 2c 20 70 54 61 62 2d 3e 69 50 4b 65 79 29 3e x, pTab->iPKey)>
1a960 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 54 62 6c =0 );. if( pTbl
1a970 4e 61 6d 65 21 3d 30 20 26 26 20 70 49 6e 64 65 Name!=0 && pInde
1a980 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 70 54 61 62 x->nColumn>=pTab
1a990 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 70 49 ->nCol ){. pI
1a9a0 6e 64 65 78 2d 3e 69 73 43 6f 76 65 72 69 6e 67 ndex->isCovering
1a9b0 20 3d 20 31 3b 0a 20 20 20 20 66 6f 72 28 6a 3d = 1;. for(j=
1a9c0 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0; j<pTab->nCol;
1a9d0 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 j++){. if(
1a9e0 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 j==pTab->iPKey
1a9f0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1aa00 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 6c if( sqlite3Col
1aa10 75 6d 6e 4f 66 49 6e 64 65 78 28 70 49 6e 64 65 umnOfIndex(pInde
1aa20 78 2c 6a 29 3e 3d 30 20 29 20 63 6f 6e 74 69 6e x,j)>=0 ) contin
1aa30 75 65 3b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 ue;. pIndex
1aa40 2d 3e 69 73 43 6f 76 65 72 69 6e 67 20 3d 20 30 ->isCovering = 0
1aa50 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1aa60 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
1aa70 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e pTab==pParse->pN
1aa80 65 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f ewTable ){. /
1aa90 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 * This routine h
1aaa0 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 74 as been called t
1aab0 6f 20 63 72 65 61 74 65 20 61 6e 20 61 75 74 6f o create an auto
1aac0 6d 61 74 69 63 20 69 6e 64 65 78 20 61 73 20 61 matic index as a
1aad0 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 20 6f . ** result o
1aae0 66 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20 f a PRIMARY KEY
1aaf0 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73 65 or UNIQUE clause
1ab00 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 64 65 66 on a column def
1ab10 69 6e 69 74 69 6f 6e 2c 20 6f 72 0a 20 20 20 20 inition, or.
1ab20 2a 2a 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 ** a PRIMARY KEY
1ab30 20 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73 or UNIQUE claus
1ab40 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 e following the
1ab50 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f column definitio
1ab60 6e 73 2e 0a 20 20 20 20 2a 2a 20 69 2e 65 2e 20 ns.. ** i.e.
1ab70 6f 6e 65 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 one of:. **.
1ab80 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 ** CREATE TAB
1ab90 4c 45 20 74 28 78 20 50 52 49 4d 41 52 59 20 4b LE t(x PRIMARY K
1aba0 45 59 2c 20 79 29 3b 0a 20 20 20 20 2a 2a 20 43 EY, y);. ** C
1abb0 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 2c REATE TABLE t(x,
1abc0 20 79 2c 20 55 4e 49 51 55 45 28 78 2c 20 79 29 y, UNIQUE(x, y)
1abd0 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a );. **. **
1abe0 20 45 69 74 68 65 72 20 77 61 79 2c 20 63 68 65 Either way, che
1abf0 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 ck to see if the
1ac00 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68 table already h
1ac10 61 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65 78 as such an index
1ac20 2e 20 49 66 0a 20 20 20 20 2a 2a 20 73 6f 2c 20 . If. ** so,
1ac30 64 6f 6e 27 74 20 62 6f 74 68 65 72 20 63 72 65 don't bother cre
1ac40 61 74 69 6e 67 20 74 68 69 73 20 6f 6e 65 2e 20 ating this one.
1ac50 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 This only applie
1ac60 73 20 74 6f 0a 20 20 20 20 2a 2a 20 61 75 74 6f s to. ** auto
1ac70 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 matically create
1ac80 64 20 69 6e 64 69 63 65 73 2e 20 55 73 65 72 73 d indices. Users
1ac90 20 63 61 6e 20 64 6f 20 61 73 20 74 68 65 79 20 can do as they
1aca0 77 69 73 68 20 77 69 74 68 0a 20 20 20 20 2a 2a wish with. **
1acb0 20 65 78 70 6c 69 63 69 74 20 69 6e 64 69 63 65 explicit indice
1acc0 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s.. **. **
1acd0 20 54 77 6f 20 55 4e 49 51 55 45 20 6f 72 20 50 Two UNIQUE or P
1ace0 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 RIMARY KEY const
1acf0 72 61 69 6e 74 73 20 61 72 65 20 63 6f 6e 73 69 raints are consi
1ad00 64 65 72 65 64 20 65 71 75 69 76 61 6c 65 6e 74 dered equivalent
1ad10 0a 20 20 20 20 2a 2a 20 28 61 6e 64 20 74 68 75 . ** (and thu
1ad20 73 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 s suppressing th
1ad30 65 20 73 65 63 6f 6e 64 20 6f 6e 65 29 20 65 76 e second one) ev
1ad40 65 6e 20 69 66 20 74 68 65 79 20 68 61 76 65 20 en if they have
1ad50 64 69 66 66 65 72 65 6e 74 0a 20 20 20 20 2a 2a different. **
1ad60 20 73 6f 72 74 20 6f 72 64 65 72 73 2e 0a 20 20 sort orders..
1ad70 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 **. ** If t
1ad80 68 65 72 65 20 61 72 65 20 64 69 66 66 65 72 65 here are differe
1ad90 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 nt collating seq
1ada0 75 65 6e 63 65 73 20 6f 72 20 69 66 20 74 68 65 uences or if the
1adb0 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 20 20 columns of.
1adc0 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e ** the constrain
1add0 74 20 6f 63 63 75 72 20 69 6e 20 64 69 66 66 65 t occur in diffe
1ade0 72 65 6e 74 20 6f 72 64 65 72 73 2c 20 74 68 65 rent orders, the
1adf0 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 n the constraint
1ae00 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 63 6f 6e s are. ** con
1ae10 73 69 64 65 72 65 64 20 64 69 73 74 69 6e 63 74 sidered distinct
1ae20 20 61 6e 64 20 62 6f 74 68 20 72 65 73 75 6c 74 and both result
1ae30 20 69 6e 20 73 65 70 61 72 61 74 65 20 69 6e 64 in separate ind
1ae40 69 63 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ices.. */.
1ae50 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 Index *pIdx;.
1ae60 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d for(pIdx=pTab-
1ae70 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 >pIndex; pIdx; p
1ae80 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 Idx=pIdx->pNext)
1ae90 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 {. int k;.
1aea0 20 20 20 20 20 61 73 73 65 72 74 28 20 49 73 55 assert( IsU
1aeb0 6e 69 71 75 65 49 6e 64 65 78 28 70 49 64 78 29 niqueIndex(pIdx)
1aec0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1aed0 28 20 70 49 64 78 2d 3e 69 64 78 54 79 70 65 21 ( pIdx->idxType!
1aee0 3d 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 5f =SQLITE_IDXTYPE_
1aef0 41 50 50 44 45 46 20 29 3b 0a 20 20 20 20 20 20 APPDEF );.
1af00 61 73 73 65 72 74 28 20 49 73 55 6e 69 71 75 65 assert( IsUnique
1af10 49 6e 64 65 78 28 70 49 6e 64 65 78 29 20 29 3b Index(pIndex) );
1af20 0a 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 .. if( pIdx
1af30 2d 3e 6e 4b 65 79 43 6f 6c 21 3d 70 49 6e 64 65 ->nKeyCol!=pInde
1af40 78 2d 3e 6e 4b 65 79 43 6f 6c 20 29 20 63 6f 6e x->nKeyCol ) con
1af50 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 66 6f 72 tinue;. for
1af60 28 6b 3d 30 3b 20 6b 3c 70 49 64 78 2d 3e 6e 4b (k=0; k<pIdx->nK
1af70 65 79 43 6f 6c 3b 20 6b 2b 2b 29 7b 0a 20 20 20 eyCol; k++){.
1af80 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1af90 2a 7a 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e *z1;. con
1afa0 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 20 st char *z2;.
1afb0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 assert( pId
1afc0 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 3e 3d x->aiColumn[k]>=
1afd0 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0 );. if(
1afe0 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b pIdx->aiColumn[
1aff0 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69 43 6f k]!=pIndex->aiCo
1b000 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61 6b 3b lumn[k] ) break;
1b010 0a 20 20 20 20 20 20 20 20 7a 31 20 3d 20 70 49 . z1 = pI
1b020 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 dx->azColl[k];.
1b030 20 20 20 20 20 20 20 7a 32 20 3d 20 70 49 6e 64 z2 = pInd
1b040 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 ex->azColl[k];.
1b050 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
1b060 65 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 e3StrICmp(z1, z2
1b070 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 ) ) break;.
1b080 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d }. if( k==
1b090 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 20 29 7b pIdx->nKeyCol ){
1b0a0 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 . if( pId
1b0b0 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e 64 x->onError!=pInd
1b0c0 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 ex->onError ){.
1b0d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
1b0e0 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65 61 constraint crea
1b0f0 74 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e 64 tes the same ind
1b100 65 78 20 61 73 20 61 20 70 72 65 76 69 6f 75 73 ex as a previous
1b110 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f . ** co
1b120 6e 73 74 72 61 69 6e 74 20 73 70 65 63 69 66 69 nstraint specifi
1b130 65 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 ed somewhere in
1b140 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
1b150 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 statement..
1b160 20 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65 72 ** However
1b170 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 the ON CONFLICT
1b180 20 63 6c 61 75 73 65 73 20 61 72 65 20 64 69 66 clauses are dif
1b190 66 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68 20 ferent. If both
1b1a0 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 20 20 this .
1b1b0 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e ** constraint an
1b1c0 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 d the previous e
1b1d0 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74 72 quivalent constr
1b1e0 61 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69 63 aint have explic
1b1f0 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 it. **
1b200 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 ON CONFLICT clau
1b210 73 65 73 20 74 68 69 73 20 69 73 20 61 6e 20 65 ses this is an e
1b220 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c rror. Otherwise,
1b230 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 20 use the.
1b240 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 ** explicitly
1b250 20 73 70 65 63 69 66 69 65 64 20 62 65 68 61 76 specified behav
1b260 69 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 ior for the inde
1b270 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a x.. */.
1b280 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 28 if( !(
1b290 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f pIdx->onError==O
1b2a0 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70 49 6e E_Default || pIn
1b2b0 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 dex->onError==OE
1b2c0 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20 20 20 _Default) ){.
1b2d0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1b2e0 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1b2f0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b300 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67 20 4f "conflicting O
1b310 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 N CONFLICT claus
1b320 65 73 20 73 70 65 63 69 66 69 65 64 22 2c 20 30 es specified", 0
1b330 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
1b340 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 if( pId
1b350 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 x->onError==OE_D
1b360 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 efault ){.
1b370 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72 pIdx->onEr
1b380 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e ror = pIndex->on
1b390 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 Error;.
1b3a0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1b3b0 20 20 20 20 20 69 66 28 20 69 64 78 54 79 70 65 if( idxType
1b3c0 3d 3d 53 51 4c 49 54 45 5f 49 44 58 54 59 50 45 ==SQLITE_IDXTYPE
1b3d0 5f 50 52 49 4d 41 52 59 4b 45 59 20 29 20 70 49 _PRIMARYKEY ) pI
1b3e0 64 78 2d 3e 69 64 78 54 79 70 65 20 3d 20 69 64 dx->idxType = id
1b3f0 78 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 67 xType;. g
1b400 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
1b410 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d 0a 20 index;. }.
1b420 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c }. }.. /* L
1b430 69 6e 6b 20 74 68 65 20 6e 65 77 20 49 6e 64 65 ink the new Inde
1b440 78 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 69 x structure to i
1b450 74 73 20 74 61 62 6c 65 20 61 6e 64 20 74 6f 20 ts table and to
1b460 74 68 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 69 the other. ** i
1b470 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
1b480 65 20 73 74 72 75 63 74 75 72 65 73 2e 20 0a 20 e structures. .
1b490 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
1b4a0 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b arse->nErr==0 );
1b4b0 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e . if( db->init.
1b4c0 62 75 73 79 20 29 7b 0a 20 20 20 20 49 6e 64 65 busy ){. Inde
1b4d0 78 20 2a 70 3b 0a 20 20 20 20 61 73 73 65 72 74 x *p;. assert
1b4e0 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 ( !IN_DECLARE_VT
1b4f0 41 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 AB );. assert
1b500 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d ( sqlite3SchemaM
1b510 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c 20 utexHeld(db, 0,
1b520 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 pIndex->pSchema)
1b530 20 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 );. p = sqli
1b540 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 te3HashInsert(&p
1b550 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e Index->pSchema->
1b560 69 64 78 48 61 73 68 2c 20 0a 20 20 20 20 20 20 idxHash, .
1b570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b580 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d pIndex->zNam
1b590 65 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 e, pIndex);.
1b5a0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 61 if( p ){. a
1b5b0 73 73 65 72 74 28 20 70 3d 3d 70 49 6e 64 65 78 ssert( p==pIndex
1b5c0 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d ); /* Malloc m
1b5d0 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 ust have failed
1b5e0 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1b5f0 4f 6f 6d 46 61 75 6c 74 28 64 62 29 3b 0a 20 20 OomFault(db);.
1b600 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 goto exit_cr
1b610 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 eate_index;.
1b620 7d 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 }. db->flags
1b630 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e |= SQLITE_Intern
1b640 43 68 61 6e 67 65 73 3b 0a 20 20 20 20 69 66 28 Changes;. if(
1b650 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a pTblName!=0 ){.
1b660 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e pIndex->tn
1b670 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 um = db->init.ne
1b680 77 54 6e 75 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d wTnum;. }. }
1b690 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
1b6a0 73 20 74 68 65 20 69 6e 69 74 69 61 6c 20 43 52 s the initial CR
1b6b0 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 EATE INDEX state
1b6c0 6d 65 6e 74 20 28 6f 72 20 43 52 45 41 54 45 20 ment (or CREATE
1b6d0 54 41 42 4c 45 20 69 66 20 74 68 65 0a 20 20 2a TABLE if the. *
1b6e0 2a 20 69 6e 64 65 78 20 69 73 20 61 6e 20 69 6d * index is an im
1b6f0 70 6c 69 65 64 20 69 6e 64 65 78 20 66 6f 72 20 plied index for
1b700 61 20 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d a UNIQUE or PRIM
1b710 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 ARY KEY constrai
1b720 6e 74 29 20 74 68 65 6e 0a 20 20 2a 2a 20 65 6d nt) then. ** em
1b730 69 74 20 63 6f 64 65 20 74 6f 20 61 6c 6c 6f 63 it code to alloc
1b740 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 72 6f ate the index ro
1b750 6f 74 70 61 67 65 20 6f 6e 20 64 69 73 6b 20 61 otpage on disk a
1b760 6e 64 20 6d 61 6b 65 20 61 6e 20 65 6e 74 72 79 nd make an entry
1b770 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 69 6e for. ** the in
1b780 64 65 78 20 69 6e 20 74 68 65 20 73 71 6c 69 74 dex in the sqlit
1b790 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 e_master table a
1b7a0 6e 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 nd populate the
1b7b0 69 6e 64 65 78 20 77 69 74 68 0a 20 20 2a 2a 20 index with. **
1b7c0 63 6f 6e 74 65 6e 74 2e 20 20 42 75 74 2c 20 64 content. But, d
1b7d0 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20 69 66 o not do this if
1b7e0 20 77 65 20 61 72 65 20 73 69 6d 70 6c 79 20 72 we are simply r
1b7f0 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74 eading the sqlit
1b800 65 5f 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 e_master. ** ta
1b810 62 6c 65 20 74 6f 20 70 61 72 73 65 20 74 68 65 ble to parse the
1b820 20 73 63 68 65 6d 61 2c 20 6f 72 20 69 66 20 74 schema, or if t
1b830 68 69 73 20 69 6e 64 65 78 20 69 73 20 74 68 65 his index is the
1b840 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 64 PRIMARY KEY ind
1b850 65 78 0a 20 20 2a 2a 20 6f 66 20 61 20 57 49 54 ex. ** of a WIT
1b860 48 4f 55 54 20 52 4f 57 49 44 20 74 61 62 6c 65 HOUT ROWID table
1b870 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 .. **. ** If p
1b880 54 62 6c 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 TblName==0 it me
1b890 61 6e 73 20 74 68 69 73 20 69 6e 64 65 78 20 69 ans this index i
1b8a0 73 20 67 65 6e 65 72 61 74 65 64 20 61 73 20 61 s generated as a
1b8b0 6e 20 69 6d 70 6c 69 65 64 20 50 52 49 4d 41 52 n implied PRIMAR
1b8c0 59 20 4b 45 59 0a 20 20 2a 2a 20 6f 72 20 55 4e Y KEY. ** or UN
1b8d0 49 51 55 45 20 69 6e 64 65 78 20 69 6e 20 61 20 IQUE index in a
1b8e0 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 CREATE TABLE sta
1b8f0 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65 20 74 tement. Since t
1b900 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 68 61 he table. ** ha
1b910 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72 65 61 s just been crea
1b920 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69 6e 73 ted, it contains
1b930 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74 68 65 no data and the
1b940 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c 69 7a index initializ
1b950 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65 70 20 ation. ** step
1b960 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e 0a can be skipped..
1b970 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20 */. else if(
1b980 48 61 73 52 6f 77 69 64 28 70 54 61 62 29 20 7c HasRowid(pTab) |
1b990 7c 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b | pTblName!=0 ){
1b9a0 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 . Vdbe *v;.
1b9b0 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 char *zStmt;.
1b9c0 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b int iMem = ++
1b9d0 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 pParse->nMem;..
1b9e0 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 v = sqlite3Ge
1b9f0 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
1ba00 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f if( v==0 ) go
1ba10 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
1ba20 6e 64 65 78 3b 0a 0a 20 20 20 20 73 71 6c 69 74 ndex;.. sqlit
1ba30 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 e3BeginWriteOper
1ba40 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c ation(pParse, 1,
1ba50 20 69 44 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 iDb);.. /* C
1ba60 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61 reate the rootpa
1ba70 67 65 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 ge for the index
1ba80 20 75 73 69 6e 67 20 43 72 65 61 74 65 49 6e 64 using CreateInd
1ba90 65 78 2e 20 42 75 74 20 62 65 66 6f 72 65 0a 20 ex. But before.
1baa0 20 20 20 2a 2a 20 64 6f 69 6e 67 20 73 6f 2c 20 ** doing so,
1bab0 63 6f 64 65 20 61 20 4e 6f 6f 70 20 69 6e 73 74 code a Noop inst
1bac0 72 75 63 74 69 6f 6e 20 61 6e 64 20 73 74 6f 72 ruction and stor
1bad0 65 20 69 74 73 20 61 64 64 72 65 73 73 20 69 6e e its address in
1bae0 20 0a 20 20 20 20 2a 2a 20 49 6e 64 65 78 2e 74 . ** Index.t
1baf0 6e 75 6d 2e 20 54 68 69 73 20 69 73 20 72 65 71 num. This is req
1bb00 75 69 72 65 64 20 69 6e 20 63 61 73 65 20 74 68 uired in case th
1bb10 69 73 20 69 6e 64 65 78 20 69 73 20 61 63 74 75 is index is actu
1bb20 61 6c 6c 79 20 61 20 0a 20 20 20 20 2a 2a 20 50 ally a . ** P
1bb30 52 49 4d 41 52 59 20 4b 45 59 20 61 6e 64 20 74 RIMARY KEY and t
1bb40 68 65 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 he table is actu
1bb50 61 6c 6c 79 20 61 20 57 49 54 48 4f 55 54 20 52 ally a WITHOUT R
1bb60 4f 57 49 44 20 74 61 62 6c 65 2e 20 49 6e 20 0a OWID table. In .
1bb70 20 20 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65 ** that case
1bb80 20 74 68 65 20 63 6f 6e 76 65 72 74 54 6f 57 69 the convertToWi
1bb90 74 68 6f 75 74 52 6f 77 69 64 54 61 62 6c 65 28 thoutRowidTable(
1bba0 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 ) routine will r
1bbb0 65 70 6c 61 63 65 0a 20 20 20 20 2a 2a 20 74 68 eplace. ** th
1bbc0 65 20 4e 6f 6f 70 20 77 69 74 68 20 61 20 47 6f e Noop with a Go
1bbd0 74 6f 20 74 6f 20 6a 75 6d 70 20 6f 76 65 72 20 to to jump over
1bbe0 74 68 65 20 56 44 42 45 20 63 6f 64 65 20 67 65 the VDBE code ge
1bbf0 6e 65 72 61 74 65 64 20 62 65 6c 6f 77 2e 20 2a nerated below. *
1bc00 2f 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e /. pIndex->tn
1bc10 75 6d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 um = sqlite3Vdbe
1bc20 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f 6f AddOp0(v, OP_Noo
1bc30 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 p);. sqlite3V
1bc40 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1bc50 43 72 65 61 74 65 49 6e 64 65 78 2c 20 69 44 62 CreateIndex, iDb
1bc60 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 2f 2a , iMem);.. /*
1bc70 20 47 61 74 68 65 72 20 74 68 65 20 63 6f 6d 70 Gather the comp
1bc80 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 lete text of the
1bc90 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 CREATE INDEX st
1bca0 61 74 65 6d 65 6e 74 20 69 6e 74 6f 0a 20 20 20 atement into.
1bcb0 20 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20 76 61 ** the zStmt va
1bcc0 72 69 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 riable. */.
1bcd0 20 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a if( pStart ){.
1bce0 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 28 69 int n = (i
1bcf0 6e 74 29 28 70 50 61 72 73 65 2d 3e 73 4c 61 73 nt)(pParse->sLas
1bd00 74 54 6f 6b 65 6e 2e 7a 20 2d 20 70 4e 61 6d 65 tToken.z - pName
1bd10 2d 3e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e 73 ->z) + pParse->s
1bd20 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 LastToken.n;.
1bd30 20 20 20 69 66 28 20 70 4e 61 6d 65 2d 3e 7a 5b if( pName->z[
1bd40 6e 2d 31 5d 3d 3d 27 3b 27 20 29 20 6e 2d 2d 3b n-1]==';' ) n--;
1bd50 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65 . /* A name
1bd60 64 20 69 6e 64 65 78 20 77 69 74 68 20 61 6e 20 d index with an
1bd70 65 78 70 6c 69 63 69 74 20 43 52 45 41 54 45 20 explicit CREATE
1bd80 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 INDEX statement
1bd90 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d */. zStmt =
1bda0 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1bdb0 64 62 2c 20 22 43 52 45 41 54 45 25 73 20 49 4e db, "CREATE%s IN
1bdc0 44 45 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20 DEX %.*s",.
1bdd0 20 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e onError==OE_N
1bde0 6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 one ? "" : " UNI
1bdf0 51 55 45 22 2c 20 6e 2c 20 70 4e 61 6d 65 2d 3e QUE", n, pName->
1be00 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 z);. }else{.
1be10 20 20 20 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d /* An autom
1be20 61 74 69 63 20 69 6e 64 65 78 20 63 72 65 61 74 atic index creat
1be30 65 64 20 62 79 20 61 20 50 52 49 4d 41 52 59 20 ed by a PRIMARY
1be40 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6f KEY or UNIQUE co
1be50 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 nstraint */.
1be60 20 20 2f 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c /* zStmt = sql
1be70 69 74 65 33 4d 50 72 69 6e 74 66 28 22 22 29 3b ite3MPrintf("");
1be80 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 */. zStmt
1be90 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 = 0;. }..
1bea0 2f 2a 20 41 64 64 20 61 6e 20 65 6e 74 72 79 20 /* Add an entry
1beb0 69 6e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 in sqlite_master
1bec0 20 66 6f 72 20 74 68 69 73 20 69 6e 64 65 78 0a for this index.
1bed0 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
1bee0 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
1bef0 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 arse, . "
1bf00 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e 25 INSERT INTO %Q.%
1bf10 73 20 56 41 4c 55 45 53 28 27 69 6e 64 65 78 27 s VALUES('index'
1bf20 2c 25 51 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22 ,%Q,%Q,#%d,%Q);"
1bf30 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 ,. db->aD
1bf40 62 5b 69 44 62 5d 2e 7a 44 62 53 4e 61 6d 65 2c b[iDb].zDbSName,
1bf50 20 4d 41 53 54 45 52 5f 4e 41 4d 45 2c 0a 20 20 MASTER_NAME,.
1bf60 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e pIndex->zN
1bf70 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 70 54 61 ame,. pTa
1bf80 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 b->zName,.
1bf90 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 iMem,.
1bfa0 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20 zStmt. );.
1bfb0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1bfc0 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 20 20 20 b, zStmt);..
1bfd0 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e 64 65 /* Fill the inde
1bfe0 78 20 77 69 74 68 20 64 61 74 61 20 61 6e 64 20 x with data and
1bff0 72 65 70 61 72 73 65 20 74 68 65 20 73 63 68 65 reparse the sche
1c000 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 ma. Code an OP_E
1c010 78 70 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f 20 xpire. ** to
1c020 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 invalidate all p
1c030 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 re-compiled stat
1c040 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 ements.. */.
1c050 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 20 if( pTblName
1c060 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1c070 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 RefillIndex(pPar
1c080 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d se, pIndex, iMem
1c090 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1c0a0 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 ChangeCookie(pPa
1c0b0 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 rse, iDb);.
1c0c0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 50 sqlite3VdbeAddP
1c0d0 61 72 73 65 53 63 68 65 6d 61 4f 70 28 76 2c 20 arseSchemaOp(v,
1c0e0 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 73 71 iDb,. sq
1c0f0 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
1c100 20 22 6e 61 6d 65 3d 27 25 71 27 20 41 4e 44 20 "name='%q' AND
1c110 74 79 70 65 3d 27 69 6e 64 65 78 27 22 2c 20 70 type='index'", p
1c120 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a Index->zName));.
1c130 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1c140 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 45 78 eAddOp0(v, OP_Ex
1c150 70 69 72 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 pire);. }..
1c160 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1c170 70 48 65 72 65 28 76 2c 20 70 49 6e 64 65 78 2d pHere(v, pIndex-
1c180 3e 74 6e 75 6d 29 3b 0a 20 20 7d 0a 0a 20 20 2f >tnum);. }.. /
1c190 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20 61 6e * When adding an
1c1a0 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 6c 69 index to the li
1c1b0 73 74 20 6f 66 20 69 6e 64 69 63 65 73 20 66 6f st of indices fo
1c1c0 72 20 61 20 74 61 62 6c 65 2c 20 6d 61 6b 65 0a r a table, make.
1c1d0 20 20 2a 2a 20 73 75 72 65 20 61 6c 6c 20 69 6e ** sure all in
1c1e0 64 69 63 65 73 20 6c 61 62 65 6c 65 64 20 4f 45 dices labeled OE
1c1f0 5f 52 65 70 6c 61 63 65 20 63 6f 6d 65 20 61 66 _Replace come af
1c200 74 65 72 20 61 6c 6c 20 74 68 6f 73 65 20 6c 61 ter all those la
1c210 62 65 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f 49 67 beled. ** OE_Ig
1c220 6e 6f 72 65 2e 20 20 54 68 69 73 20 69 73 20 6e nore. This is n
1c230 65 63 65 73 73 61 72 79 20 66 6f 72 20 74 68 65 ecessary for the
1c240 20 63 6f 72 72 65 63 74 20 63 6f 6e 73 74 72 61 correct constra
1c250 69 6e 74 20 63 68 65 63 6b 0a 20 20 2a 2a 20 70 int check. ** p
1c260 72 6f 63 65 73 73 69 6e 67 20 28 69 6e 20 73 71 rocessing (in sq
1c270 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e lite3GenerateCon
1c280 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 29 29 straintChecks())
1c290 20 61 73 20 70 61 72 74 20 6f 66 0a 20 20 2a 2a as part of. **
1c2a0 20 55 50 44 41 54 45 20 61 6e 64 20 49 4e 53 45 UPDATE and INSE
1c2b0 52 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 RT statements.
1c2c0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e . */. if( db->
1c2d0 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 70 54 62 init.busy || pTb
1c2e0 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 lName==0 ){.
1c2f0 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f if( onError!=OE_
1c300 52 65 70 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d Replace || pTab-
1c310 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 20 20 20 20 >pIndex==0.
1c320 20 20 20 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e || pTab->pIn
1c330 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 dex->onError==OE
1c340 5f 52 65 70 6c 61 63 65 29 7b 0a 20 20 20 20 20 _Replace){.
1c350 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d pIndex->pNext =
1c360 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 pTab->pIndex;.
1c370 20 20 20 20 20 70 54 61 62 2d 3e 70 49 6e 64 65 pTab->pInde
1c380 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 x = pIndex;.
1c390 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 }else{. Ind
1c3a0 65 78 20 2a 70 4f 74 68 65 72 20 3d 20 70 54 61 ex *pOther = pTa
1c3b0 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 b->pIndex;.
1c3c0 20 77 68 69 6c 65 28 20 70 4f 74 68 65 72 2d 3e while( pOther->
1c3d0 70 4e 65 78 74 20 26 26 20 70 4f 74 68 65 72 2d pNext && pOther-
1c3e0 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 72 6f 72 21 >pNext->onError!
1c3f0 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 =OE_Replace ){.
1c400 20 20 20 20 20 20 20 70 4f 74 68 65 72 20 3d 20 pOther =
1c410 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 pOther->pNext;.
1c420 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 6e }. pIn
1c430 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 dex->pNext = pOt
1c440 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 her->pNext;.
1c450 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 pOther->pNext
1c460 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 0a = pIndex;. }.
1c470 20 20 20 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a pIndex = 0;.
1c480 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 }.. /* Clean
1c490 75 70 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e up before exitin
1c4a0 67 20 2a 2f 0a 65 78 69 74 5f 63 72 65 61 74 65 g */.exit_create
1c4b0 5f 69 6e 64 65 78 3a 0a 20 20 69 66 28 20 70 49 _index:. if( pI
1c4c0 6e 64 65 78 20 29 20 66 72 65 65 49 6e 64 65 78 ndex ) freeIndex
1c4d0 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 (db, pIndex);.
1c4e0 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1c4f0 65 28 64 62 2c 20 70 50 49 57 68 65 72 65 29 3b e(db, pPIWhere);
1c500 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 . sqlite3ExprLi
1c510 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 stDelete(db, pLi
1c520 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 st);. sqlite3Sr
1c530 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 cListDelete(db,
1c540 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c pTblName);. sql
1c550 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1c560 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Name);.}../*.**
1c570 46 69 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e 61 Fill the Index.a
1c580 69 52 6f 77 45 73 74 5b 5d 20 61 72 72 61 79 20 iRowEst[] array
1c590 77 69 74 68 20 64 65 66 61 75 6c 74 20 69 6e 66 with default inf
1c5a0 6f 72 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72 ormation - infor
1c5b0 6d 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 mation.** to be
1c5c0 75 73 65 64 20 77 68 65 6e 20 77 65 20 68 61 76 used when we hav
1c5d0 65 20 6e 6f 74 20 72 75 6e 20 74 68 65 20 41 4e e not run the AN
1c5e0 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a ALYZE command..*
1c5f0 2a 0a 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d *.** aiRowEst[0]
1c600 20 69 73 20 73 75 70 70 6f 73 65 64 20 74 6f 20 is supposed to
1c610 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 contain the numb
1c620 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 er of elements i
1c630 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 n the index..**
1c640 53 69 6e 63 65 20 77 65 20 64 6f 20 6e 6f 74 20 Since we do not
1c650 6b 6e 6f 77 2c 20 67 75 65 73 73 20 31 20 6d 69 know, guess 1 mi
1c660 6c 6c 69 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74 llion. aiRowEst
1c670 5b 31 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61 [1] is an estima
1c680 74 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d te of the.** num
1c690 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 ber of rows in t
1c6a0 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 he table that ma
1c6b0 74 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c tch any particul
1c6c0 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a ar value of the.
1c6d0 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 ** first column
1c6e0 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 61 of the index. a
1c6f0 69 52 6f 77 45 73 74 5b 32 5d 20 69 73 20 61 6e iRowEst[2] is an
1c700 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 estimate of the
1c710 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f number.** of ro
1c720 77 73 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e ws that match an
1c730 79 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6d y particular com
1c740 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 bination of the
1c750 66 69 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a first 2 columns.
1c760 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e ** of the index.
1c770 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 And so forth.
1c780 20 49 74 20 6d 75 73 74 20 61 6c 77 61 79 73 20 It must always
1c790 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
1c7a0 0a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 .*.**
1c7b0 61 69 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 aiRowEst[N]<=aiR
1c7c0 6f 77 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20 owEst[N-1].**
1c7d0 20 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 aiRowEst
1c7e0 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61 [N]>=1.**.** Apa
1c7f0 72 74 20 66 72 6f 6d 20 74 68 61 74 2c 20 77 65 rt from that, we
1c800 20 68 61 76 65 20 6c 69 74 74 6c 65 20 74 6f 20 have little to
1c810 67 6f 20 6f 6e 20 62 65 73 69 64 65 73 20 69 6e go on besides in
1c820 74 75 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a tuition as to.**
1c830 20 68 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d 20 how aiRowEst[]
1c840 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 should be initia
1c850 6c 69 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d 62 lized. The numb
1c860 65 72 73 20 67 65 6e 65 72 61 74 65 64 20 68 65 ers generated he
1c870 72 65 0a 2a 2a 20 61 72 65 20 62 61 73 65 64 20 re.** are based
1c880 6f 6e 20 74 79 70 69 63 61 6c 20 76 61 6c 75 65 on typical value
1c890 73 20 66 6f 75 6e 64 20 69 6e 20 61 63 74 75 61 s found in actua
1c8a0 6c 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 76 6f l indices..*/.vo
1c8b0 69 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c id sqlite3Defaul
1c8c0 74 52 6f 77 45 73 74 28 49 6e 64 65 78 20 2a 70 tRowEst(Index *p
1c8d0 49 64 78 29 7b 0a 20 20 2f 2a 20 20 20 20 20 20 Idx){. /*
1c8e0 20 20 20 20 20 20 20 20 20 20 31 30 2c 20 20 39 10, 9
1c8f0 2c 20 20 38 2c 20 20 37 2c 20 20 36 20 2a 2f 0a , 8, 7, 6 */.
1c900 20 20 4c 6f 67 45 73 74 20 61 56 61 6c 5b 5d 20 LogEst aVal[]
1c910 3d 20 7b 20 33 33 2c 20 33 32 2c 20 33 30 2c 20 = { 33, 32, 30,
1c920 32 38 2c 20 32 36 20 7d 3b 0a 20 20 4c 6f 67 45 28, 26 };. LogE
1c930 73 74 20 2a 61 20 3d 20 70 49 64 78 2d 3e 61 69 st *a = pIdx->ai
1c940 52 6f 77 4c 6f 67 45 73 74 3b 0a 20 20 69 6e 74 RowLogEst;. int
1c950 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 41 72 72 nCopy = MIN(Arr
1c960 61 79 53 69 7a 65 28 61 56 61 6c 29 2c 20 70 49 aySize(aVal), pI
1c970 64 78 2d 3e 6e 4b 65 79 43 6f 6c 29 3b 0a 20 20 dx->nKeyCol);.
1c980 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a 20 49 6e 64 int i;.. /* Ind
1c990 65 78 65 73 20 77 69 74 68 20 64 65 66 61 75 6c exes with defaul
1c9a0 74 20 72 6f 77 20 65 73 74 69 6d 61 74 65 73 20 t row estimates
1c9b0 73 68 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 should not have
1c9c0 73 74 61 74 31 20 64 61 74 61 20 2a 2f 0a 20 20 stat1 data */.
1c9d0 61 73 73 65 72 74 28 20 21 70 49 64 78 2d 3e 68 assert( !pIdx->h
1c9e0 61 73 53 74 61 74 31 20 29 3b 0a 0a 20 20 2f 2a asStat1 );.. /*
1c9f0 20 53 65 74 20 74 68 65 20 66 69 72 73 74 20 65 Set the first e
1ca00 6e 74 72 79 20 28 6e 75 6d 62 65 72 20 6f 66 20 ntry (number of
1ca10 72 6f 77 73 20 69 6e 20 74 68 65 20 69 6e 64 65 rows in the inde
1ca20 78 29 20 74 6f 20 74 68 65 20 65 73 74 69 6d 61 x) to the estima
1ca30 74 65 64 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 ted . ** number
1ca40 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 of rows in the
1ca50 74 61 62 6c 65 2c 20 6f 72 20 68 61 6c 66 20 74 table, or half t
1ca60 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
1ca70 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 0a 20 s in the table.
1ca80 20 2a 2a 20 66 6f 72 20 61 20 70 61 72 74 69 61 ** for a partia
1ca90 6c 20 69 6e 64 65 78 2e 20 20 20 42 75 74 20 64 l index. But d
1caa0 6f 20 6e 6f 74 20 6c 65 74 20 74 68 65 20 65 73 o not let the es
1cab0 74 69 6d 61 74 65 20 64 72 6f 70 20 62 65 6c 6f timate drop belo
1cac0 77 20 31 30 2e 20 2a 2f 0a 20 20 61 5b 30 5d 20 w 10. */. a[0]
1cad0 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e = pIdx->pTable->
1cae0 6e 52 6f 77 4c 6f 67 45 73 74 3b 0a 20 20 69 66 nRowLogEst;. if
1caf0 28 20 70 49 64 78 2d 3e 70 50 61 72 74 49 64 78 ( pIdx->pPartIdx
1cb00 57 68 65 72 65 21 3d 30 20 29 20 61 5b 30 5d 20 Where!=0 ) a[0]
1cb10 2d 3d 20 31 30 3b 20 20 61 73 73 65 72 74 28 20 -= 10; assert(
1cb20 31 30 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 10==sqlite3LogEs
1cb30 74 28 32 29 20 29 3b 0a 20 20 69 66 28 20 61 5b t(2) );. if( a[
1cb40 30 5d 3c 33 33 20 29 20 61 5b 30 5d 20 3d 20 33 0]<33 ) a[0] = 3
1cb50 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3;
1cb60 20 20 20 20 61 73 73 65 72 74 28 20 33 33 3d 3d assert( 33==
1cb70 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 31 30 sqlite3LogEst(10
1cb80 29 20 29 3b 0a 0a 20 20 2f 2a 20 45 73 74 69 6d ) );.. /* Estim
1cb90 61 74 65 20 74 68 61 74 20 61 5b 31 5d 20 69 73 ate that a[1] is
1cba0 20 31 30 2c 20 61 5b 32 5d 20 69 73 20 39 2c 20 10, a[2] is 9,
1cbb0 61 5b 33 5d 20 69 73 20 38 2c 20 61 5b 34 5d 20 a[3] is 8, a[4]
1cbc0 69 73 20 37 2c 20 61 5b 35 5d 20 69 73 0a 20 20 is 7, a[5] is.
1cbd0 2a 2a 20 36 20 61 6e 64 20 65 61 63 68 20 73 75 ** 6 and each su
1cbe0 62 73 65 71 75 65 6e 74 20 76 61 6c 75 65 20 28 bsequent value (
1cbf0 69 66 20 61 6e 79 29 20 69 73 20 35 2e 20 20 2a if any) is 5. *
1cc00 2f 0a 20 20 6d 65 6d 63 70 79 28 26 61 5b 31 5d /. memcpy(&a[1]
1cc10 2c 20 61 56 61 6c 2c 20 6e 43 6f 70 79 2a 73 69 , aVal, nCopy*si
1cc20 7a 65 6f 66 28 4c 6f 67 45 73 74 29 29 3b 0a 20 zeof(LogEst));.
1cc30 20 66 6f 72 28 69 3d 6e 43 6f 70 79 2b 31 3b 20 for(i=nCopy+1;
1cc40 69 3c 3d 70 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c i<=pIdx->nKeyCol
1cc50 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 5b 69 5d ; i++){. a[i]
1cc60 20 3d 20 32 33 3b 20 20 20 20 20 20 20 20 20 20 = 23;
1cc70 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1cc80 28 20 32 33 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 ( 23==sqlite3Log
1cc90 45 73 74 28 35 29 20 29 3b 0a 20 20 7d 0a 0a 20 Est(5) );. }..
1cca0 20 61 73 73 65 72 74 28 20 30 3d 3d 73 71 6c 69 assert( 0==sqli
1ccb0 74 65 33 4c 6f 67 45 73 74 28 31 29 20 29 3b 0a te3LogEst(1) );.
1ccc0 20 20 69 66 28 20 49 73 55 6e 69 71 75 65 49 6e if( IsUniqueIn
1ccd0 64 65 78 28 70 49 64 78 29 20 29 20 61 5b 70 49 dex(pIdx) ) a[pI
1cce0 64 78 2d 3e 6e 4b 65 79 43 6f 6c 5d 20 3d 20 30 dx->nKeyCol] = 0
1ccf0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
1cd00 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 72 6f routine will dro
1cd10 70 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6e 61 p an existing na
1cd20 6d 65 64 20 69 6e 64 65 78 2e 20 20 54 68 69 73 med index. This
1cd30 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6d 70 6c routine.** impl
1cd40 65 6d 65 6e 74 73 20 74 68 65 20 44 52 4f 50 20 ements the DROP
1cd50 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 2e INDEX statement.
1cd60 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
1cd70 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 20 DropIndex(Parse
1cd80 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 *pParse, SrcList
1cd90 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69 66 45 *pName, int ifE
1cda0 78 69 73 74 73 29 7b 0a 20 20 49 6e 64 65 78 20 xists){. Index
1cdb0 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64 62 65 20 *pIndex;. Vdbe
1cdc0 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 *v;. sqlite3 *d
1cdd0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1cde0 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 61 73 int iDb;.. as
1cdf0 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 sert( pParse->nE
1ce00 72 72 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 4e 65 rr==0 ); /* Ne
1ce10 76 65 72 20 63 61 6c 6c 65 64 20 77 69 74 68 20 ver called with
1ce20 70 72 69 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0a prior errors */.
1ce30 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
1ce40 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f Failed ){. go
1ce50 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 to exit_drop_ind
1ce60 65 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 ex;. }. assert
1ce70 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 ( pName->nSrc==1
1ce80 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 );. if( SQLITE
1ce90 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 _OK!=sqlite3Read
1cea0 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 Schema(pParse) )
1ceb0 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f {. goto exit_
1cec0 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a drop_index;. }.
1ced0 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 pIndex = sqlit
1cee0 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 e3FindIndex(db,
1cef0 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d pName->a[0].zNam
1cf00 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a e, pName->a[0].z
1cf10 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 Database);. if(
1cf20 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 pIndex==0 ){.
1cf30 20 20 69 66 28 20 21 69 66 45 78 69 73 74 73 20 if( !ifExists
1cf40 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1cf50 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1cf60 20 22 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a "no such index:
1cf70 20 25 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b %S", pName, 0);
1cf80 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1cf90 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 sqlite3CodeVer
1cfa0 69 66 79 4e 61 6d 65 64 53 63 68 65 6d 61 28 70 ifyNamedSchema(p
1cfb0 50 61 72 73 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b Parse, pName->a[
1cfc0 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 0].zDatabase);.
1cfd0 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d }. pParse-
1cfe0 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 >checkSchema = 1
1cff0 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f ;. goto exit_
1d000 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a drop_index;. }.
1d010 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 69 64 if( pIndex->id
1d020 78 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 49 44 xType!=SQLITE_ID
1d030 58 54 59 50 45 5f 41 50 50 44 45 46 20 29 7b 0a XTYPE_APPDEF ){.
1d040 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1d050 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64 Msg(pParse, "ind
1d060 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ex associated wi
1d070 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20 20 20 th UNIQUE ".
1d080 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 "or PRIMARY KE
1d090 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 6e Y constraint can
1d0a0 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 2c not be dropped",
1d0b0 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 0);. goto ex
1d0c0 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 it_drop_index;.
1d0d0 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 }. iDb = sqlit
1d0e0 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1d0f0 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 db, pIndex->pSch
1d100 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ema);.#ifndef SQ
1d110 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
1d120 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 IZATION. {.
1d130 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 int code = SQLIT
1d140 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a 20 20 E_DROP_INDEX;.
1d150 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
1d160 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 0a pIndex->pTable;.
1d170 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
1d180 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 zDb = db->aDb[iD
1d190 62 5d 2e 7a 44 62 53 4e 61 6d 65 3b 0a 20 20 20 b].zDbSName;.
1d1a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 const char *zTa
1d1b0 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 b = SCHEMA_TABLE
1d1c0 28 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 (iDb);. if( s
1d1d0 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
1d1e0 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 pParse, SQLITE_D
1d1f0 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 ELETE, zTab, 0,
1d200 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f zDb) ){. go
1d210 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 to exit_drop_ind
1d220 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ex;. }. if
1d230 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 ( !OMIT_TEMPDB &
1d240 26 20 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 & iDb ) code = S
1d250 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f QLITE_DROP_TEMP_
1d260 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 INDEX;. if( s
1d270 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
1d280 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 pParse, code, pI
1d290 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 ndex->zName, pTa
1d2a0 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 b->zName, zDb) )
1d2b0 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 {. goto exi
1d2c0 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 t_drop_index;.
1d2d0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }. }.#endif..
1d2e0 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f /* Generate co
1d2f0 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 de to remove the
1d300 20 69 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 index and from
1d310 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 the master table
1d320 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */. v = sqlite
1d330 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
1d340 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 ;. if( v ){.
1d350 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 sqlite3BeginWri
1d360 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 teOperation(pPar
1d370 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 se, 1, iDb);.
1d380 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 sqlite3NestedPa
1d390 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 rse(pParse,.
1d3a0 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 "DELETE FROM
1d3b0 25 51 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 %Q.%s WHERE name
1d3c0 3d 25 51 20 41 4e 44 20 74 79 70 65 3d 27 69 6e =%Q AND type='in
1d3d0 64 65 78 27 22 2c 0a 20 20 20 20 20 20 20 64 62 dex'",. db
1d3e0 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 4e ->aDb[iDb].zDbSN
1d3f0 61 6d 65 2c 20 4d 41 53 54 45 52 5f 4e 41 4d 45 ame, MASTER_NAME
1d400 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a , pIndex->zName.
1d410 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 );. sqlit
1d420 65 33 43 6c 65 61 72 53 74 61 74 54 61 62 6c 65 e3ClearStatTable
1d430 73 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 s(pParse, iDb, "
1d440 69 64 78 22 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e idx", pIndex->zN
1d450 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ame);. sqlite
1d460 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 3ChangeCookie(pP
1d470 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 arse, iDb);.
1d480 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 destroyRootPage(
1d490 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2d 3e pParse, pIndex->
1d4a0 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 tnum, iDb);.
1d4b0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1d4c0 34 28 76 2c 20 4f 50 5f 44 72 6f 70 49 6e 64 65 4(v, OP_DropInde
1d4d0 78 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 49 x, iDb, 0, 0, pI
1d4e0 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b ndex->zName, 0);
1d4f0 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72 6f 70 5f . }..exit_drop_
1d500 69 6e 64 65 78 3a 0a 20 20 73 71 6c 69 74 65 33 index:. sqlite3
1d510 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 SrcListDelete(db
1d520 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a , pName);.}../*.
1d530 2a 2a 20 70 41 72 72 61 79 20 69 73 20 61 20 70 ** pArray is a p
1d540 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 ointer to an arr
1d550 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 45 ay of objects. E
1d560 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 ach object in th
1d570 65 0a 2a 2a 20 61 72 72 61 79 20 69 73 20 73 7a e.** array is sz
1d580 45 6e 74 72 79 20 62 79 74 65 73 20 69 6e 20 73 Entry bytes in s
1d590 69 7a 65 2e 20 54 68 69 73 20 72 6f 75 74 69 6e ize. This routin
1d5a0 65 20 75 73 65 73 20 73 71 6c 69 74 65 33 44 62 e uses sqlite3Db
1d5b0 52 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 74 6f 20 Realloc().** to
1d5c0 65 78 74 65 6e 64 20 74 68 65 20 61 72 72 61 79 extend the array
1d5d0 20 73 6f 20 74 68 61 74 20 74 68 65 72 65 20 69 so that there i
1d5e0 73 20 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 s space for a ne
1d5f0 77 20 6f 62 6a 65 63 74 20 61 74 20 74 68 65 20 w object at the
1d600 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 end..**.** When
1d610 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
1d620 20 63 61 6c 6c 65 64 2c 20 2a 70 6e 45 6e 74 72 called, *pnEntr
1d630 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 y contains the c
1d640 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 0a 2a urrent size of.*
1d650 2a 20 74 68 65 20 61 72 72 61 79 20 28 69 6e 20 * the array (in
1d660 65 6e 74 72 69 65 73 20 2d 20 73 6f 20 74 68 65 entries - so the
1d670 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 allocation is (
1d680 28 2a 70 6e 45 6e 74 72 79 29 20 2a 20 73 7a 45 (*pnEntry) * szE
1d690 6e 74 72 79 29 20 62 79 74 65 73 0a 2a 2a 20 69 ntry) bytes.** i
1d6a0 6e 20 74 6f 74 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 n total)..**.**
1d6b0 49 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 28 29 If the realloc()
1d6c0 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20 28 is successful (
1d6d0 69 2e 65 2e 20 69 66 20 6e 6f 20 4f 4f 4d 20 63 i.e. if no OOM c
1d6e0 6f 6e 64 69 74 69 6f 6e 20 6f 63 63 75 72 73 29 ondition occurs)
1d6f0 2c 20 74 68 65 0a 2a 2a 20 73 70 61 63 65 20 61 , the.** space a
1d700 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 llocated for the
1d710 20 6e 65 77 20 6f 62 6a 65 63 74 20 69 73 20 7a new object is z
1d720 65 72 6f 65 64 2c 20 2a 70 6e 45 6e 74 72 79 20 eroed, *pnEntry
1d730 75 70 64 61 74 65 64 20 74 6f 0a 2a 2a 20 72 65 updated to.** re
1d740 66 6c 65 63 74 20 74 68 65 20 6e 65 77 20 73 69 flect the new si
1d750 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 ze of the array
1d760 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f and a pointer to
1d770 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 the new allocat
1d780 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e ion.** returned.
1d790 20 2a 70 49 64 78 20 69 73 20 73 65 74 20 74 6f *pIdx is set to
1d7a0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
1d7b0 65 20 6e 65 77 20 61 72 72 61 79 20 65 6e 74 72 e new array entr
1d7c0 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a y in this case..
1d7d0 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
1d7e0 20 69 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 28 if the realloc(
1d7f0 29 20 66 61 69 6c 73 2c 20 2a 70 49 64 78 20 69 ) fails, *pIdx i
1d800 73 20 73 65 74 20 74 6f 20 2d 31 2c 20 2a 70 6e s set to -1, *pn
1d810 45 6e 74 72 79 20 72 65 6d 61 69 6e 73 0a 2a 2a Entry remains.**
1d820 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 61 unchanged and a
1d830 20 63 6f 70 79 20 6f 66 20 70 41 72 72 61 79 20 copy of pArray
1d840 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 76 6f 69 returned..*/.voi
1d850 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 d *sqlite3ArrayA
1d860 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74 llocate(. sqlit
1d870 65 33 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 e3 *db, /*
1d880 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f Connection to no
1d890 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 tify of malloc f
1d8a0 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 ailures */. voi
1d8b0 64 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f d *pArray, /
1d8c0 2a 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 * Array of objec
1d8d0 74 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65 ts. Might be re
1d8e0 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 allocated */. i
1d8f0 6e 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 nt szEntry,
1d900 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 /* Size of each
1d910 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61 object in the a
1d920 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 rray */. int *p
1d930 6e 45 6e 74 72 79 2c 20 20 20 20 20 2f 2a 20 4e nEntry, /* N
1d940 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63 74 73 umber of objects
1d950 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 currently in us
1d960 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 49 64 78 e */. int *pIdx
1d970 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
1d980 65 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 e the index of a
1d990 20 6e 65 77 20 73 6c 6f 74 20 68 65 72 65 20 2a new slot here *
1d9a0 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a /.){. char *z;.
1d9b0 20 20 69 6e 74 20 6e 20 3d 20 2a 70 6e 45 6e 74 int n = *pnEnt
1d9c0 72 79 3b 0a 20 20 69 66 28 20 28 6e 20 26 20 28 ry;. if( (n & (
1d9d0 6e 2d 31 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 n-1))==0 ){.
1d9e0 69 6e 74 20 73 7a 20 3d 20 28 6e 3d 3d 30 29 20 int sz = (n==0)
1d9f0 3f 20 31 20 3a 20 32 2a 6e 3b 0a 20 20 20 20 76 ? 1 : 2*n;. v
1da00 6f 69 64 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 oid *pNew = sqli
1da10 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c te3DbRealloc(db,
1da20 20 70 41 72 72 61 79 2c 20 73 7a 2a 73 7a 45 6e pArray, sz*szEn
1da30 74 72 79 29 3b 0a 20 20 20 20 69 66 28 20 70 4e try);. if( pN
1da40 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a ew==0 ){. *
1da50 70 49 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20 20 pIdx = -1;.
1da60 20 72 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a return pArray;.
1da70 20 20 20 20 7d 0a 20 20 20 20 70 41 72 72 61 79 }. pArray
1da80 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 7a = pNew;. }. z
1da90 20 3d 20 28 63 68 61 72 2a 29 70 41 72 72 61 79 = (char*)pArray
1daa0 3b 0a 20 20 6d 65 6d 73 65 74 28 26 7a 5b 6e 20 ;. memset(&z[n
1dab0 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30 2c 20 73 * szEntry], 0, s
1dac0 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70 49 64 78 zEntry);. *pIdx
1dad0 20 3d 20 6e 3b 0a 20 20 2b 2b 2a 70 6e 45 6e 74 = n;. ++*pnEnt
1dae0 72 79 3b 0a 20 20 72 65 74 75 72 6e 20 70 41 72 ry;. return pAr
1daf0 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 ray;.}../*.** Ap
1db00 70 65 6e 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 pend a new eleme
1db10 6e 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 nt to the given
1db20 49 64 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20 IdList. Create
1db30 61 20 6e 65 77 20 49 64 4c 69 73 74 20 69 66 0a a new IdList if.
1db40 2a 2a 20 6e 65 65 64 20 62 65 2e 0a 2a 2a 0a 2a ** need be..**.*
1db50 2a 20 41 20 6e 65 77 20 49 64 4c 69 73 74 20 69 * A new IdList i
1db60 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e s returned, or N
1db70 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 ULL if malloc()
1db80 66 61 69 6c 73 2e 0a 2a 2f 0a 49 64 4c 69 73 74 fails..*/.IdList
1db90 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 *sqlite3IdListA
1dba0 70 70 65 6e 64 28 73 71 6c 69 74 65 33 20 2a 64 ppend(sqlite3 *d
1dbb0 62 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 b, IdList *pList
1dbc0 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 , Token *pToken)
1dbd0 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 {. int i;. if(
1dbe0 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 pList==0 ){.
1dbf0 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 pList = sqlite3
1dc00 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
1dc10 20 73 69 7a 65 6f 66 28 49 64 4c 69 73 74 29 20 sizeof(IdList)
1dc20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 );. if( pList
1dc30 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1dc40 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d }. pList->a =
1dc50 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c sqlite3ArrayAll
1dc60 6f 63 61 74 65 28 0a 20 20 20 20 20 20 64 62 2c ocate(. db,
1dc70 0a 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c . pList->a,
1dc80 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c . sizeof(pL
1dc90 69 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 ist->a[0]),.
1dca0 20 20 26 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a 20 &pList->nId,.
1dcb0 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 69 &i. );. i
1dcc0 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 f( i<0 ){. sq
1dcd0 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 lite3IdListDelet
1dce0 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 e(db, pList);.
1dcf0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1dd00 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e pList->a[i].zN
1dd10 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d ame = sqlite3Nam
1dd20 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 eFromToken(db, p
1dd30 54 6f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e Token);. return
1dd40 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pList;.}../*.**
1dd50 20 44 65 6c 65 74 65 20 61 6e 20 49 64 4c 69 73 Delete an IdLis
1dd60 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 t..*/.void sqlit
1dd70 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 e3IdListDelete(s
1dd80 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 qlite3 *db, IdLi
1dd90 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e st *pList){. in
1dda0 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 t i;. if( pList
1ddb0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1ddc0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 for(i=0; i<pList
1ddd0 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nId; i++){.
1dde0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1ddf0 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a b, pList->a[i].z
1de00 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c Name);. }. sql
1de10 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1de20 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 List->a);. sqli
1de30 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c te3DbFree(db, pL
1de40 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ist);.}../*.** R
1de50 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 eturn the index
1de60 69 6e 20 70 4c 69 73 74 20 6f 66 20 74 68 65 20 in pList of the
1de70 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 64 identifier named
1de80 20 7a 49 64 2e 20 20 52 65 74 75 72 6e 20 2d 31 zId. Return -1
1de90 0a 2a 2a 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 .** if not found
1dea0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
1deb0 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 69 IdListIndex(IdLi
1dec0 73 74 20 2a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 st *pList, const
1ded0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 char *zName){.
1dee0 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c int i;. if( pL
1def0 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ist==0 ) return
1df00 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 -1;. for(i=0; i
1df10 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b <pList->nId; i++
1df20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
1df30 65 33 53 74 72 49 43 6d 70 28 70 4c 69 73 74 2d e3StrICmp(pList-
1df40 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 >a[i].zName, zNa
1df50 6d 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 me)==0 ) return
1df60 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 i;. }. return
1df70 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 -1;.}../*.** Exp
1df80 61 6e 64 20 74 68 65 20 73 70 61 63 65 20 61 6c and the space al
1df90 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 located for the
1dfa0 67 69 76 65 6e 20 53 72 63 4c 69 73 74 20 6f 62 given SrcList ob
1dfb0 6a 65 63 74 20 62 79 0a 2a 2a 20 63 72 65 61 74 ject by.** creat
1dfc0 69 6e 67 20 6e 45 78 74 72 61 20 6e 65 77 20 73 ing nExtra new s
1dfd0 6c 6f 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 lots beginning a
1dfe0 74 20 69 53 74 61 72 74 2e 20 20 69 53 74 61 72 t iStart. iStar
1dff0 74 20 69 73 20 7a 65 72 6f 20 62 61 73 65 64 2e t is zero based.
1e000 0a 2a 2a 20 4e 65 77 20 73 6c 6f 74 73 20 61 72 .** New slots ar
1e010 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 e zeroed..**.**
1e020 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 For example, sup
1e030 70 6f 73 65 20 61 20 53 72 63 4c 69 73 74 20 69 pose a SrcList i
1e040 6e 69 74 69 61 6c 6c 79 20 63 6f 6e 74 61 69 6e nitially contain
1e050 73 20 74 77 6f 20 65 6e 74 72 69 65 73 3a 20 41 s two entries: A
1e060 2c 42 2e 0a 2a 2a 20 54 6f 20 61 70 70 65 6e 64 ,B..** To append
1e070 20 33 20 6e 65 77 20 65 6e 74 72 69 65 73 20 6f 3 new entries o
1e080 6e 74 6f 20 74 68 65 20 65 6e 64 2c 20 64 6f 20 nto the end, do
1e090 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 this:.**.** s
1e0a0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c qlite3SrcListEnl
1e0b0 61 72 67 65 28 64 62 2c 20 70 53 72 63 6c 69 73 arge(db, pSrclis
1e0c0 74 2c 20 33 2c 20 32 29 3b 0a 2a 2a 0a 2a 2a 20 t, 3, 2);.**.**
1e0d0 41 66 74 65 72 20 74 68 65 20 63 61 6c 6c 20 61 After the call a
1e0e0 62 6f 76 65 20 69 74 20 77 6f 75 6c 64 20 63 6f bove it would co
1e0f0 6e 74 61 69 6e 3a 20 20 41 2c 20 42 2c 20 6e 69 ntain: A, B, ni
1e100 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20 l, nil, nil..**
1e110 49 66 20 74 68 65 20 69 53 74 61 72 74 20 61 72 If the iStart ar
1e120 67 75 6d 65 6e 74 20 68 61 64 20 62 65 65 6e 20 gument had been
1e130 31 20 69 6e 73 74 65 61 64 20 6f 66 20 32 2c 20 1 instead of 2,
1e140 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a then the result.
1e150 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 ** would have be
1e160 65 6e 3a 20 20 41 2c 20 6e 69 6c 2c 20 6e 69 6c en: A, nil, nil
1e170 2c 20 6e 69 6c 2c 20 42 2e 20 20 54 6f 20 70 72 , nil, B. To pr
1e180 65 70 65 6e 64 20 74 68 65 20 6e 65 77 20 73 6c epend the new sl
1e190 6f 74 73 2c 0a 2a 2a 20 74 68 65 20 69 53 74 61 ots,.** the iSta
1e1a0 72 74 20 76 61 6c 75 65 20 77 6f 75 6c 64 20 62 rt value would b
1e1b0 65 20 30 2e 20 20 54 68 65 20 72 65 73 75 6c 74 e 0. The result
1e1c0 20 74 68 65 6e 20 77 6f 75 6c 64 0a 2a 2a 20 62 then would.** b
1e1d0 65 3a 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c e: nil, nil, nil
1e1e0 2c 20 41 2c 20 42 2e 0a 2a 2a 0a 2a 2a 20 49 66 , A, B..**.** If
1e1f0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
1e200 74 69 6f 6e 20 66 61 69 6c 73 20 74 68 65 20 53 tion fails the S
1e210 72 63 4c 69 73 74 20 69 73 20 75 6e 63 68 61 6e rcList is unchan
1e220 67 65 64 2e 20 20 54 68 65 0a 2a 2a 20 64 62 2d ged. The.** db-
1e230 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c >mallocFailed fl
1e240 61 67 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 ag will be set t
1e250 6f 20 74 72 75 65 2e 0a 2a 2f 0a 53 72 63 4c 69 o true..*/.SrcLi
1e260 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 st *sqlite3SrcLi
1e270 73 74 45 6e 6c 61 72 67 65 28 0a 20 20 73 71 6c stEnlarge(. sql
1e280 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
1e290 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e /* Database conn
1e2a0 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 ection to notify
1e2b0 20 6f 66 20 4f 4f 4d 20 65 72 72 6f 72 73 20 2a of OOM errors *
1e2c0 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 /. SrcList *pSr
1e2d0 63 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 53 72 c, /* The Sr
1e2e0 63 4c 69 73 74 20 74 6f 20 62 65 20 65 6e 6c 61 cList to be enla
1e2f0 72 67 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 rged */. int nE
1e300 78 74 72 61 2c 20 20 20 20 20 20 20 20 2f 2a 20 xtra, /*
1e310 4e 75 6d 62 65 72 20 6f 66 20 6e 65 77 20 73 6c Number of new sl
1e320 6f 74 73 20 74 6f 20 61 64 64 20 74 6f 20 70 53 ots to add to pS
1e330 72 63 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 rc->a[] */. int
1e340 20 69 53 74 61 72 74 20 20 20 20 20 20 20 20 20 iStart
1e350 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 53 72 63 /* Index in pSrc
1e360 2d 3e 61 5b 5d 20 6f 66 20 66 69 72 73 74 20 6e ->a[] of first n
1e370 65 77 20 73 6c 6f 74 20 2a 2f 0a 29 7b 0a 20 20 ew slot */.){.
1e380 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a 20 53 61 6e int i;.. /* San
1e390 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 ity checking on
1e3a0 63 61 6c 6c 69 6e 67 20 70 61 72 61 6d 65 74 65 calling paramete
1e3b0 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 rs */. assert(
1e3c0 69 53 74 61 72 74 3e 3d 30 20 29 3b 0a 20 20 61 iStart>=0 );. a
1e3d0 73 73 65 72 74 28 20 6e 45 78 74 72 61 3e 3d 31 ssert( nExtra>=1
1e3e0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 );. assert( pS
1e3f0 72 63 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 rc!=0 );. asser
1e400 74 28 20 69 53 74 61 72 74 3c 3d 70 53 72 63 2d t( iStart<=pSrc-
1e410 3e 6e 53 72 63 20 29 3b 0a 0a 20 20 2f 2a 20 41 >nSrc );.. /* A
1e420 6c 6c 6f 63 61 74 65 20 61 64 64 69 74 69 6f 6e llocate addition
1e430 61 6c 20 73 70 61 63 65 20 69 66 20 6e 65 65 64 al space if need
1e440 65 64 20 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 ed */. if( (u32
1e450 29 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 )pSrc->nSrc+nExt
1e460 72 61 3e 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 ra>pSrc->nAlloc
1e470 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a ){. SrcList *
1e480 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e 41 pNew;. int nA
1e490 6c 6c 6f 63 20 3d 20 70 53 72 63 2d 3e 6e 53 72 lloc = pSrc->nSr
1e4a0 63 2a 32 2b 6e 45 78 74 72 61 3b 0a 20 20 20 20 c*2+nExtra;.
1e4b0 69 6e 74 20 6e 47 6f 74 3b 0a 20 20 20 20 70 4e int nGot;. pN
1e4c0 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 ew = sqlite3DbRe
1e4d0 61 6c 6c 6f 63 28 64 62 2c 20 70 53 72 63 2c 0a alloc(db, pSrc,.
1e4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
1e4f0 69 7a 65 6f 66 28 2a 70 53 72 63 29 20 2b 20 28 izeof(*pSrc) + (
1e500 6e 41 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 nAlloc-1)*sizeof
1e510 28 70 53 72 63 2d 3e 61 5b 30 5d 29 20 29 3b 0a (pSrc->a[0]) );.
1e520 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 if( pNew==0
1e530 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1e540 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1e550 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 d );. retur
1e560 6e 20 70 53 72 63 3b 0a 20 20 20 20 7d 0a 20 20 n pSrc;. }.
1e570 20 20 70 53 72 63 20 3d 20 70 4e 65 77 3b 0a 20 pSrc = pNew;.
1e580 20 20 20 6e 47 6f 74 20 3d 20 28 73 71 6c 69 74 nGot = (sqlit
1e590 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 e3DbMallocSize(d
1e5a0 62 2c 20 70 4e 65 77 29 20 2d 20 73 69 7a 65 6f b, pNew) - sizeo
1e5b0 66 28 2a 70 53 72 63 29 29 2f 73 69 7a 65 6f 66 f(*pSrc))/sizeof
1e5c0 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b 0a (pSrc->a[0])+1;.
1e5d0 20 20 20 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 pSrc->nAlloc
1e5e0 20 3d 20 6e 47 6f 74 3b 0a 20 20 7d 0a 0a 20 20 = nGot;. }..
1e5f0 2f 2a 20 4d 6f 76 65 20 65 78 69 73 74 69 6e 67 /* Move existing
1e600 20 73 6c 6f 74 73 20 74 68 61 74 20 63 6f 6d 65 slots that come
1e610 20 61 66 74 65 72 20 74 68 65 20 6e 65 77 6c 79 after the newly
1e620 20 69 6e 73 65 72 74 65 64 20 73 6c 6f 74 73 0a inserted slots.
1e630 20 20 2a 2a 20 6f 75 74 20 6f 66 20 74 68 65 20 ** out of the
1e640 77 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 70 way */. for(i=p
1e650 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d Src->nSrc-1; i>=
1e660 69 53 74 61 72 74 3b 20 69 2d 2d 29 7b 0a 20 20 iStart; i--){.
1e670 20 20 70 53 72 63 2d 3e 61 5b 69 2b 6e 45 78 74 pSrc->a[i+nExt
1e680 72 61 5d 20 3d 20 70 53 72 63 2d 3e 61 5b 69 5d ra] = pSrc->a[i]
1e690 3b 0a 20 20 7d 0a 20 20 70 53 72 63 2d 3e 6e 53 ;. }. pSrc->nS
1e6a0 72 63 20 2b 3d 20 6e 45 78 74 72 61 3b 0a 0a 20 rc += nExtra;..
1e6b0 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 6e 65 77 /* Zero the new
1e6c0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f ly allocated slo
1e6d0 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 ts */. memset(&
1e6e0 70 53 72 63 2d 3e 61 5b 69 53 74 61 72 74 5d 2c pSrc->a[iStart],
1e6f0 20 30 2c 20 73 69 7a 65 6f 66 28 70 53 72 63 2d 0, sizeof(pSrc-
1e700 3e 61 5b 30 5d 29 2a 6e 45 78 74 72 61 29 3b 0a >a[0])*nExtra);.
1e710 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74 3b 20 for(i=iStart;
1e720 69 3c 69 53 74 61 72 74 2b 6e 45 78 74 72 61 3b i<iStart+nExtra;
1e730 20 69 2b 2b 29 7b 0a 20 20 20 20 70 53 72 63 2d i++){. pSrc-
1e740 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 20 3d 20 >a[i].iCursor =
1e750 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 -1;. }.. /* Re
1e760 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
1e770 6f 20 74 68 65 20 65 6e 6c 61 72 67 65 64 20 53 o the enlarged S
1e780 72 63 4c 69 73 74 20 2a 2f 0a 20 20 72 65 74 75 rcList */. retu
1e790 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a rn pSrc;.}.../*.
1e7a0 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65 77 20 ** Append a new
1e7b0 74 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68 table name to th
1e7c0 65 20 67 69 76 65 6e 20 53 72 63 4c 69 73 74 2e e given SrcList.
1e7d0 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 53 Create a new S
1e7e0 72 63 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 rcList if.** nee
1e7f0 64 20 62 65 2e 20 20 41 20 6e 65 77 20 65 6e 74 d be. A new ent
1e800 72 79 20 69 73 20 63 72 65 61 74 65 64 20 69 6e ry is created in
1e810 20 74 68 65 20 53 72 63 4c 69 73 74 20 65 76 65 the SrcList eve
1e820 6e 20 69 66 20 70 54 61 62 6c 65 20 69 73 20 4e n if pTable is N
1e830 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 53 72 63 ULL..**.** A Src
1e840 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e 65 64 List is returned
1e850 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 , or NULL if the
1e860 72 65 20 69 73 20 61 6e 20 4f 4f 4d 20 65 72 72 re is an OOM err
1e870 6f 72 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 or. The returne
1e880 64 0a 2a 2a 20 53 72 63 4c 69 73 74 20 6d 69 67 d.** SrcList mig
1e890 68 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 ht be the same a
1e8a0 73 20 74 68 65 20 53 72 63 4c 69 73 74 20 74 68 s the SrcList th
1e8b0 61 74 20 77 61 73 20 69 6e 70 75 74 20 6f 72 20 at was input or
1e8c0 69 74 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 it might be.** a
1e8d0 20 6e 65 77 20 6f 6e 65 2e 20 20 49 66 20 61 6e new one. If an
1e8e0 20 4f 4f 4d 20 65 72 72 6f 72 20 64 6f 65 73 20 OOM error does
1e8f0 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65 occurs, then the
1e900 20 70 72 69 6f 72 20 76 61 6c 75 65 20 6f 66 20 prior value of
1e910 70 4c 69 73 74 0a 2a 2a 20 74 68 61 74 20 69 73 pList.** that is
1e920 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73 20 72 input to this r
1e930 6f 75 74 69 6e 65 20 69 73 20 61 75 74 6f 6d 61 outine is automa
1e940 74 69 63 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a tically freed..*
1e950 2a 0a 2a 2a 20 49 66 20 70 44 61 74 61 62 61 73 *.** If pDatabas
1e960 65 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69 e is not null, i
1e970 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 t means that the
1e980 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 6f 70 table has an op
1e990 74 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 tional.** databa
1e9a0 73 65 20 6e 61 6d 65 20 70 72 65 66 69 78 2e 20 se name prefix.
1e9b0 20 4c 69 6b 65 20 74 68 69 73 3a 20 20 22 64 61 Like this: "da
1e9c0 74 61 62 61 73 65 2e 74 61 62 6c 65 22 2e 20 20 tabase.table".
1e9d0 54 68 65 20 70 44 61 74 61 62 61 73 65 0a 2a 2a The pDatabase.**
1e9e0 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 74 points to the t
1e9f0 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 able name and th
1ea00 65 20 70 54 61 62 6c 65 20 70 6f 69 6e 74 73 20 e pTable points
1ea10 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
1ea20 6e 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 53 72 63 name..** The Src
1ea30 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 List.a[].zName f
1ea40 69 65 6c 64 20 69 73 20 66 69 6c 6c 65 64 20 77 ield is filled w
1ea50 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 6e 61 ith the table na
1ea60 6d 65 20 77 68 69 63 68 20 6d 69 67 68 74 0a 2a me which might.*
1ea70 2a 20 63 6f 6d 65 20 66 72 6f 6d 20 70 54 61 62 * come from pTab
1ea80 6c 65 20 28 69 66 20 70 44 61 74 61 62 61 73 65 le (if pDatabase
1ea90 20 69 73 20 4e 55 4c 4c 29 20 6f 72 20 66 72 6f is NULL) or fro
1eaa0 6d 20 70 44 61 74 61 62 61 73 65 2e 20 20 0a 2a m pDatabase. .*
1eab0 2a 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44 * SrcList.a[].zD
1eac0 61 74 61 62 61 73 65 20 69 73 20 66 69 6c 6c 65 atabase is fille
1ead0 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 d with the datab
1eae0 61 73 65 20 6e 61 6d 65 20 66 72 6f 6d 20 70 54 ase name from pT
1eaf0 61 62 6c 65 2c 0a 2a 2a 20 6f 72 20 77 69 74 68 able,.** or with
1eb00 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 64 61 74 61 NULL if no data
1eb10 62 61 73 65 20 69 73 20 73 70 65 63 69 66 69 65 base is specifie
1eb20 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 d..**.** In othe
1eb30 72 20 77 6f 72 64 73 2c 20 69 66 20 63 61 6c 6c r words, if call
1eb40 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
1eb50 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 * sqlite
1eb60 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44 3SrcListAppend(D
1eb70 2c 41 2c 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 ,A,B,0);.**.** T
1eb80 68 65 6e 20 42 20 69 73 20 61 20 74 61 62 6c 65 hen B is a table
1eb90 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 64 61 name and the da
1eba0 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 75 tabase name is u
1ebb0 6e 73 70 65 63 69 66 69 65 64 2e 20 20 49 66 20 nspecified. If
1ebc0 63 61 6c 6c 65 64 0a 2a 2a 20 6c 69 6b 65 20 74 called.** like t
1ebd0 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 his:.**.**
1ebe0 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 sqlite3SrcLis
1ebf0 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c 43 29 tAppend(D,A,B,C)
1ec00 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 43 20 69 ;.**.** Then C i
1ec10 73 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 s the table name
1ec20 20 61 6e 64 20 42 20 69 73 20 74 68 65 20 64 61 and B is the da
1ec30 74 61 62 61 73 65 20 6e 61 6d 65 2e 20 20 49 66 tabase name. If
1ec40 20 43 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a C is defined.**
1ec50 20 74 68 65 6e 20 73 6f 20 69 73 20 42 2e 20 20 then so is B.
1ec60 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1ec70 77 65 20 6e 65 76 65 72 20 68 61 76 65 20 61 20 we never have a
1ec80 63 61 73 65 20 77 68 65 72 65 3a 0a 2a 2a 0a 2a case where:.**.*
1ec90 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 * sqlite
1eca0 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44 3SrcListAppend(D
1ecb0 2c 41 2c 30 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 42 ,A,0,C);.**.** B
1ecc0 6f 74 68 20 70 54 61 62 6c 65 20 61 6e 64 20 70 oth pTable and p
1ecd0 44 61 74 61 62 61 73 65 20 61 72 65 20 61 73 73 Database are ass
1ece0 75 6d 65 64 20 74 6f 20 62 65 20 71 75 6f 74 65 umed to be quote
1ecf0 64 2e 20 20 54 68 65 79 20 61 72 65 20 64 65 71 d. They are deq
1ed00 75 6f 74 65 64 0a 2a 2a 20 62 65 66 6f 72 65 20 uoted.** before
1ed10 62 65 69 6e 67 20 61 64 64 65 64 20 74 6f 20 74 being added to t
1ed20 68 65 20 53 72 63 4c 69 73 74 2e 0a 2a 2f 0a 53 he SrcList..*/.S
1ed30 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
1ed40 72 63 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20 rcListAppend(.
1ed50 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
1ed60 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f /* Connectio
1ed70 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d n to notify of m
1ed80 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 2a alloc failures *
1ed90 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 /. SrcList *pLi
1eda0 73 74 2c 20 20 20 20 20 2f 2a 20 41 70 70 65 6e st, /* Appen
1edb0 64 20 74 6f 20 74 68 69 73 20 53 72 63 4c 69 73 d to this SrcLis
1edc0 74 2e 20 4e 55 4c 4c 20 63 72 65 61 74 65 73 20 t. NULL creates
1edd0 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 2a 2f a new SrcList */
1ede0 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 . Token *pTable
1edf0 2c 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 , /* Table
1ee00 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20 54 to append */. T
1ee10 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73 65 20 oken *pDatabase
1ee20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6f /* Database o
1ee30 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 f the table */.)
1ee40 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 {. struct SrcLi
1ee50 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
1ee60 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61 62 assert( pDatab
1ee70 61 73 65 3d 3d 30 20 7c 7c 20 70 54 61 62 6c 65 ase==0 || pTable
1ee80 21 3d 30 20 29 3b 20 20 2f 2a 20 43 61 6e 6e 6f !=0 ); /* Canno
1ee90 74 20 68 61 76 65 20 43 20 77 69 74 68 6f 75 74 t have C without
1eea0 20 42 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 B */. assert(
1eeb0 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 db!=0 );. if( p
1eec0 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 List==0 ){. p
1eed0 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 List = sqlite3Db
1eee0 4d 61 6c 6c 6f 63 52 61 77 4e 4e 28 64 62 2c 20 MallocRawNN(db,
1eef0 73 69 7a 65 6f 66 28 53 72 63 4c 69 73 74 29 20 sizeof(SrcList)
1ef00 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 );. if( pList
1ef10 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1ef20 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f pList->nAllo
1ef30 63 20 3d 20 31 3b 0a 20 20 20 20 70 4c 69 73 74 c = 1;. pList
1ef40 2d 3e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20 ->nSrc = 1;.
1ef50 6d 65 6d 73 65 74 28 26 70 4c 69 73 74 2d 3e 61 memset(&pList->a
1ef60 5b 30 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 [0], 0, sizeof(p
1ef70 4c 69 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 List->a[0]));.
1ef80 20 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 pList->a[0].iC
1ef90 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 7d 65 ursor = -1;. }e
1efa0 6c 73 65 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d lse{. pList =
1efb0 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 sqlite3SrcListE
1efc0 6e 6c 61 72 67 65 28 64 62 2c 20 70 4c 69 73 74 nlarge(db, pList
1efd0 2c 20 31 2c 20 70 4c 69 73 74 2d 3e 6e 53 72 63 , 1, pList->nSrc
1efe0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d );. }. if( db-
1eff0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
1f000 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c . sqlite3SrcL
1f010 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c istDelete(db, pL
1f020 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ist);. return
1f030 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 0;. }. pItem
1f040 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 = &pList->a[pLis
1f050 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66 t->nSrc-1];. if
1f060 28 20 70 44 61 74 61 62 61 73 65 20 26 26 20 70 ( pDatabase && p
1f070 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29 Database->z==0 )
1f080 7b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20 {. pDatabase
1f090 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 = 0;. }. if( p
1f0a0 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20 20 20 Database ){.
1f0b0 54 6f 6b 65 6e 20 2a 70 54 65 6d 70 20 3d 20 70 Token *pTemp = p
1f0c0 44 61 74 61 62 61 73 65 3b 0a 20 20 20 20 70 44 Database;. pD
1f0d0 61 74 61 62 61 73 65 20 3d 20 70 54 61 62 6c 65 atabase = pTable
1f0e0 3b 0a 20 20 20 20 70 54 61 62 6c 65 20 3d 20 70 ;. pTable = p
1f0f0 54 65 6d 70 3b 0a 20 20 7d 0a 20 20 70 49 74 65 Temp;. }. pIte
1f100 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 m->zName = sqlit
1f110 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
1f120 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 20 20 70 db, pTable);. p
1f130 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 Item->zDatabase
1f140 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
1f150 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 61 74 61 mToken(db, pData
1f160 62 61 73 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 base);. return
1f170 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pList;.}../*.**
1f180 41 73 73 69 67 6e 20 56 64 62 65 43 75 72 73 6f Assign VdbeCurso
1f190 72 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 73 20 r index numbers
1f1a0 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e to all tables in
1f1b0 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f 0a 76 6f a SrcList.*/.vo
1f1c0 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 id sqlite3SrcLis
1f1d0 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50 tAssignCursors(P
1f1e0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 arse *pParse, Sr
1f1f0 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 cList *pList){.
1f200 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
1f210 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
1f220 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 70 Item;. assert(p
1f230 4c 69 73 74 20 7c 7c 20 70 50 61 72 73 65 2d 3e List || pParse->
1f240 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1f250 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 );. if( pList
1f260 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 ){. for(i=0,
1f270 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 pItem=pList->a;
1f280 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 i<pList->nSrc; i
1f290 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
1f2a0 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 if( pItem->i
1f2b0 43 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65 61 Cursor>=0 ) brea
1f2c0 6b 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e k;. pItem->
1f2d0 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 iCursor = pParse
1f2e0 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 ->nTab++;.
1f2f0 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 if( pItem->pSele
1f300 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ct ){. sq
1f310 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 lite3SrcListAssi
1f320 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 gnCursors(pParse
1f330 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 , pItem->pSelect
1f340 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 7d ->pSrc);. }
1f350 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1f360 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e .** Delete an en
1f370 74 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e 63 tire SrcList inc
1f380 6c 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20 73 luding all its s
1f390 75 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a ubstructure..*/.
1f3a0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c void sqlite3SrcL
1f3b0 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 istDelete(sqlite
1f3c0 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 3 *db, SrcList *
1f3d0 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b pList){. int i;
1f3e0 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 . struct SrcLis
1f3f0 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 t_item *pItem;.
1f400 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 if( pList==0 )
1f410 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70 49 return;. for(pI
1f420 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d tem=pList->a, i=
1f430 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 0; i<pList->nSrc
1f440 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
1f450 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1f460 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 44 ee(db, pItem->zD
1f470 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71 atabase);. sq
1f480 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1f490 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pItem->zName);.
1f4a0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1f4b0 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 (db, pItem->zAli
1f4c0 61 73 29 3b 0a 20 20 20 20 69 66 28 20 70 49 74 as);. if( pIt
1f4d0 65 6d 2d 3e 66 67 2e 69 73 49 6e 64 65 78 65 64 em->fg.isIndexed
1f4e0 42 79 20 29 20 73 71 6c 69 74 65 33 44 62 46 72 By ) sqlite3DbFr
1f4f0 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 75 31 ee(db, pItem->u1
1f500 2e 7a 49 6e 64 65 78 65 64 42 79 29 3b 0a 20 20 .zIndexedBy);.
1f510 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 66 67 2e if( pItem->fg.
1f520 69 73 54 61 62 46 75 6e 63 20 29 20 73 71 6c 69 isTabFunc ) sqli
1f530 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 te3ExprListDelet
1f540 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 75 31 2e e(db, pItem->u1.
1f550 70 46 75 6e 63 41 72 67 29 3b 0a 20 20 20 20 73 pFuncArg);. s
1f560 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c qlite3DeleteTabl
1f570 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 54 61 e(db, pItem->pTa
1f580 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 b);. sqlite3S
1f590 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 electDelete(db,
1f5a0 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 3b pItem->pSelect);
1f5b0 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
1f5c0 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d Delete(db, pItem
1f5d0 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c 69 ->pOn);. sqli
1f5e0 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 te3IdListDelete(
1f5f0 64 62 2c 20 70 49 74 65 6d 2d 3e 70 55 73 69 6e db, pItem->pUsin
1f600 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 g);. }. sqlite
1f610 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 3DbFree(db, pLis
1f620 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 t);.}../*.** Thi
1f630 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
1f640 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 led by the parse
1f650 72 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20 74 r to add a new t
1f660 65 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e erm to the.** en
1f670 64 20 6f 66 20 61 20 67 72 6f 77 69 6e 67 20 46 d of a growing F
1f680 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 65 ROM clause. The
1f690 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20 69 "p" parameter i
1f6a0 73 20 74 68 65 20 70 61 72 74 20 6f 66 0a 2a 2a s the part of.**
1f6b0 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
1f6c0 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 that has alread
1f6d0 79 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63 74 y been construct
1f6e0 65 64 2e 20 20 22 70 22 20 69 73 20 4e 55 4c 4c ed. "p" is NULL
1f6f0 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 .** if this is t
1f700 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 6f 66 he first term of
1f710 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
1f720 2e 20 20 70 54 61 62 6c 65 20 61 6e 64 20 70 44 . pTable and pD
1f730 61 74 61 62 61 73 65 0a 2a 2a 20 61 72 65 20 74 atabase.** are t
1f740 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
1f750 61 62 6c 65 20 61 6e 64 20 64 61 74 61 62 61 73 able and databas
1f760 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 46 e named in the F
1f770 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 2e ROM clause term.
1f780 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 69 73 .** pDatabase is
1f790 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 64 61 74 NULL if the dat
1f7a0 61 62 61 73 65 20 6e 61 6d 65 20 71 75 61 6c 69 abase name quali
1f7b0 66 69 65 72 20 69 73 20 6d 69 73 73 69 6e 67 20 fier is missing
1f7c0 2d 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c 20 63 - the.** usual c
1f7d0 61 73 65 2e 20 20 49 66 20 74 68 65 20 74 65 72 ase. If the ter
1f7e0 6d 20 68 61 73 20 61 6e 20 61 6c 69 61 73 2c 20 m has an alias,
1f7f0 74 68 65 6e 20 70 41 6c 69 61 73 20 70 6f 69 6e then pAlias poin
1f800 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 69 ts to the.** ali
1f810 61 73 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74 68 as token. If th
1f820 65 20 74 65 72 6d 20 69 73 20 61 20 73 75 62 71 e term is a subq
1f830 75 65 72 79 2c 20 74 68 65 6e 20 70 53 75 62 71 uery, then pSubq
1f840 75 65 72 79 20 69 73 20 74 68 65 0a 2a 2a 20 53 uery is the.** S
1f850 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
1f860 74 68 61 74 20 74 68 65 20 73 75 62 71 75 65 72 that the subquer
1f870 79 20 65 6e 63 6f 64 65 73 2e 20 20 54 68 65 20 y encodes. The
1f880 70 54 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70 44 pTable and.** pD
1f890 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65 atabase paramete
1f8a0 72 73 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72 20 rs are NULL for
1f8b0 73 75 62 71 75 65 72 69 65 73 2e 20 20 54 68 65 subqueries. The
1f8c0 20 70 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67 0a pOn and pUsing.
1f8d0 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 ** parameters ar
1f8e0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
1f8f0 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e the ON and USIN
1f900 47 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a G clauses..**.**
1f910 20 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53 72 Return a new Sr
1f920 63 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63 6f cList which enco
1f930 64 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d 20 des is the FROM
1f940 77 69 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a 20 with the new.**
1f950 74 65 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a 53 term added..*/.S
1f960 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
1f970 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d rcListAppendFrom
1f980 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 Term(. Parse *p
1f990 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
1f9a0 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
1f9b0 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 xt */. SrcList
1f9c0 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
1f9d0 2f 2a 20 54 68 65 20 6c 65 66 74 20 70 61 72 74 /* The left part
1f9e0 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 of the FROM cla
1f9f0 75 73 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e use already seen
1fa00 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 */. Token *pTa
1fa10 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ble, /*
1fa20 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 Name of the tab
1fa30 6c 65 20 74 6f 20 61 64 64 20 74 6f 20 74 68 65 le to add to the
1fa40 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a FROM clause */.
1fa50 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 Token *pDataba
1fa60 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d se, /* Nam
1fa70 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1fa80 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 e containing pTa
1fa90 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a ble */. Token *
1faa0 70 41 6c 69 61 73 2c 20 20 20 20 20 20 20 20 20 pAlias,
1fab0 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61 /* The right-ha
1fac0 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 41 nd side of the A
1fad0 53 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 S subexpression
1fae0 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 */. Select *pSu
1faf0 62 71 75 65 72 79 2c 20 20 20 20 20 20 2f 2a 20 bquery, /*
1fb00 41 20 73 75 62 71 75 65 72 79 20 75 73 65 64 20 A subquery used
1fb10 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 in place of a ta
1fb20 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 ble name */. Ex
1fb30 70 72 20 2a 70 4f 6e 2c 20 20 20 20 20 20 20 20 pr *pOn,
1fb40 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20 /* The ON
1fb50 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e clause of a join
1fb60 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 55 */. IdList *pU
1fb70 73 69 6e 67 20 20 20 20 20 20 20 20 20 20 2f 2a sing /*
1fb80 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 The USING claus
1fb90 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 29 e of a join */.)
1fba0 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 {. struct SrcLi
1fbb0 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
1fbc0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1fbd0 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 pParse->db;. if
1fbe0 28 20 21 70 20 26 26 20 28 70 4f 6e 20 7c 7c 20 ( !p && (pOn ||
1fbf0 70 55 73 69 6e 67 29 20 29 7b 0a 20 20 20 20 73 pUsing) ){. s
1fc00 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1fc10 50 61 72 73 65 2c 20 22 61 20 4a 4f 49 4e 20 63 Parse, "a JOIN c
1fc20 6c 61 75 73 65 20 69 73 20 72 65 71 75 69 72 65 lause is require
1fc30 64 20 62 65 66 6f 72 65 20 25 73 22 2c 20 0a 20 d before %s", .
1fc40 20 20 20 20 20 28 70 4f 6e 20 3f 20 22 4f 4e 22 (pOn ? "ON"
1fc50 20 3a 20 22 55 53 49 4e 47 22 29 0a 20 20 20 20 : "USING").
1fc60 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 65 );. goto appe
1fc70 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a 20 nd_from_error;.
1fc80 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 }. p = sqlite3
1fc90 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 SrcListAppend(db
1fca0 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 61 , p, pTable, pDa
1fcb0 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 tabase);. if( p
1fcc0 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e ==0 || NEVER(p->
1fcd0 6e 53 72 63 3d 3d 30 29 20 29 7b 0a 20 20 20 20 nSrc==0) ){.
1fce0 67 6f 74 6f 20 61 70 70 65 6e 64 5f 66 72 6f 6d goto append_from
1fcf0 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 49 _error;. }. pI
1fd00 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e tem = &p->a[p->n
1fd10 53 72 63 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 Src-1];. assert
1fd20 28 20 70 41 6c 69 61 73 21 3d 30 20 29 3b 0a 20 ( pAlias!=0 );.
1fd30 20 69 66 28 20 70 41 6c 69 61 73 2d 3e 6e 20 29 if( pAlias->n )
1fd40 7b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 41 6c {. pItem->zAl
1fd50 69 61 73 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d ias = sqlite3Nam
1fd60 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 eFromToken(db, p
1fd70 41 6c 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70 49 Alias);. }. pI
1fd80 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 tem->pSelect = p
1fd90 53 75 62 71 75 65 72 79 3b 0a 20 20 70 49 74 65 Subquery;. pIte
1fda0 6d 2d 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20 m->pOn = pOn;.
1fdb0 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 pItem->pUsing =
1fdc0 70 55 73 69 6e 67 3b 0a 20 20 72 65 74 75 72 6e pUsing;. return
1fdd0 20 70 3b 0a 0a 20 61 70 70 65 6e 64 5f 66 72 6f p;.. append_fro
1fde0 6d 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 m_error:. asser
1fdf0 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20 73 71 6c t( p==0 );. sql
1fe00 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
1fe10 62 2c 20 70 4f 6e 29 3b 0a 20 20 73 71 6c 69 74 b, pOn);. sqlit
1fe20 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 e3IdListDelete(d
1fe30 62 2c 20 70 55 73 69 6e 67 29 3b 0a 20 20 73 71 b, pUsing);. sq
1fe40 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 lite3SelectDelet
1fe50 65 28 64 62 2c 20 70 53 75 62 71 75 65 72 79 29 e(db, pSubquery)
1fe60 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
1fe70 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e ./*.** Add an IN
1fe80 44 45 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 DEXED BY or NOT
1fe90 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 74 INDEXED clause t
1fea0 6f 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e o the most recen
1feb0 74 6c 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c tly added .** el
1fec0 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 ement of the sou
1fed0 72 63 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20 rce-list passed
1fee0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
1fef0 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 gument..*/.void
1ff00 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e sqlite3SrcListIn
1ff10 64 65 78 65 64 42 79 28 50 61 72 73 65 20 2a 70 dexedBy(Parse *p
1ff20 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a Parse, SrcList *
1ff30 70 2c 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 p, Token *pIndex
1ff40 65 64 42 79 29 7b 0a 20 20 61 73 73 65 72 74 28 edBy){. assert(
1ff50 20 70 49 6e 64 65 78 65 64 42 79 21 3d 30 20 29 pIndexedBy!=0 )
1ff60 3b 0a 20 20 69 66 28 20 70 20 26 26 20 41 4c 57 ;. if( p && ALW
1ff70 41 59 53 28 70 2d 3e 6e 53 72 63 3e 30 29 20 29 AYS(p->nSrc>0) )
1ff80 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 {. struct Src
1ff90 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1ffa0 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 = &p->a[p->nSrc
1ffb0 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 -1];. assert(
1ffc0 20 70 49 74 65 6d 2d 3e 66 67 2e 6e 6f 74 49 6e pItem->fg.notIn
1ffd0 64 65 78 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 dexed==0 );.
1ffe0 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 66 assert( pItem->f
1fff0 67 2e 69 73 49 6e 64 65 78 65 64 42 79 3d 3d 30 g.isIndexedBy==0
20000 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
20010 70 49 74 65 6d 2d 3e 66 67 2e 69 73 54 61 62 46 pItem->fg.isTabF
20020 75 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 unc==0 );. if
20030 28 20 70 49 6e 64 65 78 65 64 42 79 2d 3e 6e 3d ( pIndexedBy->n=
20040 3d 31 20 26 26 20 21 70 49 6e 64 65 78 65 64 42 =1 && !pIndexedB
20050 79 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 2f 2a y->z ){. /*
20060 20 41 20 22 4e 4f 54 20 49 4e 44 45 58 45 44 22 A "NOT INDEXED"
20070 20 63 6c 61 75 73 65 20 77 61 73 20 73 75 70 70 clause was supp
20080 6c 69 65 64 2e 20 53 65 65 20 70 61 72 73 65 2e lied. See parse.
20090 79 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 y . ** cons
200a0 74 72 75 63 74 20 22 69 6e 64 65 78 65 64 5f 6f truct "indexed_o
200b0 70 74 22 20 66 6f 72 20 64 65 74 61 69 6c 73 2e pt" for details.
200c0 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d */. pItem-
200d0 3e 66 67 2e 6e 6f 74 49 6e 64 65 78 65 64 20 3d >fg.notIndexed =
200e0 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 1;. }else{.
200f0 20 20 20 20 20 70 49 74 65 6d 2d 3e 75 31 2e 7a pItem->u1.z
20100 49 6e 64 65 78 65 64 42 79 20 3d 20 73 71 6c 69 IndexedBy = sqli
20110 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
20120 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 6e (pParse->db, pIn
20130 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20 20 20 dexedBy);.
20140 70 49 74 65 6d 2d 3e 66 67 2e 69 73 49 6e 64 65 pItem->fg.isInde
20150 78 65 64 42 79 20 3d 20 28 70 49 74 65 6d 2d 3e xedBy = (pItem->
20160 75 31 2e 7a 49 6e 64 65 78 65 64 42 79 21 3d 30 u1.zIndexedBy!=0
20170 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
20180 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 6c 69 /*.** Add the li
20190 73 74 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 st of function a
201a0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
201b0 53 72 63 4c 69 73 74 20 65 6e 74 72 79 20 66 6f SrcList entry fo
201c0 72 20 61 0a 2a 2a 20 74 61 62 6c 65 2d 76 61 6c r a.** table-val
201d0 75 65 64 2d 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f ued-function..*/
201e0 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 .void sqlite3Src
201f0 4c 69 73 74 46 75 6e 63 41 72 67 73 28 50 61 72 ListFuncArgs(Par
20200 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c se *pParse, SrcL
20210 69 73 74 20 2a 70 2c 20 45 78 70 72 4c 69 73 74 ist *p, ExprList
20220 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 66 28 20 *pList){. if(
20230 70 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 p ){. struct
20240 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
20250 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e tem = &p->a[p->n
20260 53 72 63 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 Src-1];. asse
20270 72 74 28 20 70 49 74 65 6d 2d 3e 66 67 2e 6e 6f rt( pItem->fg.no
20280 74 49 6e 64 65 78 65 64 3d 3d 30 20 29 3b 0a 20 tIndexed==0 );.
20290 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d assert( pItem
202a0 2d 3e 66 67 2e 69 73 49 6e 64 65 78 65 64 42 79 ->fg.isIndexedBy
202b0 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
202c0 74 28 20 70 49 74 65 6d 2d 3e 66 67 2e 69 73 54 t( pItem->fg.isT
202d0 61 62 46 75 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 abFunc==0 );.
202e0 20 70 49 74 65 6d 2d 3e 75 31 2e 70 46 75 6e 63 pItem->u1.pFunc
202f0 41 72 67 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 Arg = pList;.
20300 20 70 49 74 65 6d 2d 3e 66 67 2e 69 73 54 61 62 pItem->fg.isTab
20310 46 75 6e 63 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 Func = 1;. }els
20320 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 e{. sqlite3Ex
20330 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 prListDelete(pPa
20340 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b rse->db, pList);
20350 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 . }.}../*.** Wh
20360 65 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20 61 en building up a
20370 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e 20 FROM clause in
20380 74 68 65 20 70 61 72 73 65 72 2c 20 74 68 65 20 the parser, the
20390 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a join operator.**
203a0 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 74 is initially at
203b0 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65 tached to the le
203c0 66 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 74 ft operand. But
203d0 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 the code genera
203e0 74 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 74 tor.** expects t
203f0 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 he join operator
20400 20 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 69 to be on the ri
20410 67 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 ght operand. Th
20420 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 68 is routine.** Sh
20430 69 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70 ifts all join op
20440 65 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 66 erators from lef
20450 74 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20 61 t to right for a
20460 6e 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a 2a n entire FROM.**
20470 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45 clause..**.** E
20480 78 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 20 xample: Suppose
20490 74 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b 65 the join is like
204a0 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
204b0 20 20 20 20 20 20 20 41 20 6e 61 74 75 72 61 6c A natural
204c0 20 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a cross join B.**
204d0 0a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 .** The operator
204e0 20 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f is "natural cro
204f0 73 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41 ss join". The A
20500 20 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20 and B operands
20510 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e are stored.** in
20520 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e p->a[0] and p->
20530 61 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65 a[1], respective
20540 6c 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 ly. The parser
20550 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73 initially stores
20560 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 the.** operator
20570 20 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 72 with A. This r
20580 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 68 outine shifts th
20590 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72 at operator over
205a0 20 74 6f 20 42 2e 0a 2a 2f 0a 76 6f 69 64 20 73 to B..*/.void s
205b0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 qlite3SrcListShi
205c0 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 ftJoinType(SrcLi
205d0 73 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 st *p){. if( p
205e0 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
205f0 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 53 72 63 2d for(i=p->nSrc-
20600 31 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 1; i>0; i--){.
20610 20 20 20 20 70 2d 3e 61 5b 69 5d 2e 66 67 2e 6a p->a[i].fg.j
20620 6f 69 6e 74 79 70 65 20 3d 20 70 2d 3e 61 5b 69 ointype = p->a[i
20630 2d 31 5d 2e 66 67 2e 6a 6f 69 6e 74 79 70 65 3b -1].fg.jointype;
20640 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 5b . }. p->a[
20650 30 5d 2e 66 67 2e 6a 6f 69 6e 74 79 70 65 20 3d 0].fg.jointype =
20660 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0;. }.}../*.**
20670 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 Generate VDBE c
20680 6f 64 65 20 66 6f 72 20 61 20 42 45 47 49 4e 20 ode for a BEGIN
20690 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f statement..*/.vo
206a0 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 id sqlite3BeginT
206b0 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 ransaction(Parse
206c0 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 74 79 *pParse, int ty
206d0 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a pe){. sqlite3 *
206e0 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 db;. Vdbe *v;.
206f0 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 int i;.. asser
20700 74 28 20 70 50 61 72 73 65 21 3d 30 20 29 3b 0a t( pParse!=0 );.
20710 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
20720 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 b;. assert( db!
20730 3d 30 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 =0 );. if( sqli
20740 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
20750 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e rse, SQLITE_TRAN
20760 53 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22 SACTION, "BEGIN"
20770 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 , 0, 0) ){. r
20780 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d eturn;. }. v =
20790 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
207a0 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 pParse);. if( !
207b0 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 v ) return;. if
207c0 28 20 74 79 70 65 21 3d 54 4b 5f 44 45 46 45 52 ( type!=TK_DEFER
207d0 52 45 44 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 RED ){. for(i
207e0 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
207f0 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
20800 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20810 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 OP_Transaction,
20820 69 2c 20 28 74 79 70 65 3d 3d 54 4b 5f 45 58 43 i, (type==TK_EXC
20830 4c 55 53 49 56 45 29 2b 31 29 3b 0a 20 20 20 20 LUSIVE)+1);.
20840 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 sqlite3VdbeUse
20850 73 42 74 72 65 65 28 76 2c 20 69 29 3b 0a 20 20 sBtree(v, i);.
20860 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
20870 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 3VdbeAddOp0(v, O
20880 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 7d P_AutoCommit);.}
20890 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
208a0 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72 20 61 VDBE code for a
208b0 20 43 4f 4d 4d 49 54 20 73 74 61 74 65 6d 65 6e COMMIT statemen
208c0 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 t..*/.void sqlit
208d0 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 e3CommitTransact
208e0 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 ion(Parse *pPars
208f0 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a e){. Vdbe *v;..
20900 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
20910 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
20920 20 70 50 61 72 73 65 2d 3e 64 62 21 3d 30 20 29 pParse->db!=0 )
20930 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 ;. if( sqlite3A
20940 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c uthCheck(pParse,
20950 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 SQLITE_TRANSACT
20960 49 4f 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30 ION, "COMMIT", 0
20970 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , 0) ){. retu
20980 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71 rn;. }. v = sq
20990 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
209a0 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b rse);. if( v ){
209b0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
209c0 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 41 75 74 AddOp1(v, OP_Aut
209d0 6f 43 6f 6d 6d 69 74 2c 20 31 29 3b 0a 20 20 7d oCommit, 1);. }
209e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
209f0 74 65 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72 te VDBE code for
20a00 20 61 20 52 4f 4c 4c 42 41 43 4b 20 73 74 61 74 a ROLLBACK stat
20a10 65 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 ement..*/.void s
20a20 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 qlite3RollbackTr
20a30 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 ansaction(Parse
20a40 2a 70 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65 *pParse){. Vdbe
20a50 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 *v;.. assert(
20a60 70 50 61 72 73 65 21 3d 30 20 29 3b 0a 20 20 61 pParse!=0 );. a
20a70 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 ssert( pParse->d
20a80 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 73 71 b!=0 );. if( sq
20a90 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
20aa0 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 Parse, SQLITE_TR
20ab0 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c ANSACTION, "ROLL
20ac0 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29 7b 0a BACK", 0, 0) ){.
20ad0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
20ae0 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 v = sqlite3Get
20af0 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
20b00 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c if( v ){. sql
20b10 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
20b20 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c , OP_AutoCommit,
20b30 20 31 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 1, 1);. }.}../
20b40 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
20b50 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 on is called by
20b60 74 68 65 20 70 61 72 73 65 72 20 77 68 65 6e 20 the parser when
20b70 69 74 20 70 61 72 73 65 73 20 61 20 63 6f 6d 6d it parses a comm
20b80 61 6e 64 20 74 6f 20 63 72 65 61 74 65 2c 0a 2a and to create,.*
20b90 2a 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c * release or rol
20ba0 6c 62 61 63 6b 20 61 6e 20 53 51 4c 20 73 61 76 lback an SQL sav
20bb0 65 70 6f 69 6e 74 2e 20 0a 2a 2f 0a 76 6f 69 64 epoint. .*/.void
20bc0 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e sqlite3Savepoin
20bd0 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c t(Parse *pParse,
20be0 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a int op, Token *
20bf0 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a pName){. char *
20c00 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e zName = sqlite3N
20c10 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 ameFromToken(pPa
20c20 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b rse->db, pName);
20c30 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a . if( zName ){.
20c40 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 Vdbe *v = sq
20c50 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
20c60 72 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 rse);.#ifndef SQ
20c70 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
20c80 49 5a 41 54 49 4f 4e 0a 20 20 20 20 73 74 61 74 IZATION. stat
20c90 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 ic const char *
20ca0 63 6f 6e 73 74 20 61 7a 5b 5d 20 3d 20 7b 20 22 const az[] = { "
20cb0 42 45 47 49 4e 22 2c 20 22 52 45 4c 45 41 53 45 BEGIN", "RELEASE
20cc0 22 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 20 7d 3b ", "ROLLBACK" };
20cd0 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 53 41 . assert( !SA
20ce0 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 26 26 VEPOINT_BEGIN &&
20cf0 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 SAVEPOINT_RELEA
20d00 53 45 3d 3d 31 20 26 26 20 53 41 56 45 50 4f 49 SE==1 && SAVEPOI
20d10 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3d 3d 32 20 29 NT_ROLLBACK==2 )
20d20 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 ;.#endif. if(
20d30 20 21 76 20 7c 7c 20 73 71 6c 69 74 65 33 41 75 !v || sqlite3Au
20d40 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 thCheck(pParse,
20d50 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 SQLITE_SAVEPOINT
20d60 2c 20 61 7a 5b 6f 70 5d 2c 20 7a 4e 61 6d 65 2c , az[op], zName,
20d70 20 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 0) ){. sql
20d80 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73 ite3DbFree(pPars
20d90 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 e->db, zName);.
20da0 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
20db0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 }. sqlite3Vd
20dc0 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 beAddOp4(v, OP_S
20dd0 61 76 65 70 6f 69 6e 74 2c 20 6f 70 2c 20 30 2c avepoint, op, 0,
20de0 20 30 2c 20 7a 4e 61 6d 65 2c 20 50 34 5f 44 59 0, zName, P4_DY
20df0 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f NAMIC);. }.}../
20e00 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 *.** Make sure t
20e10 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
20e20 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 61 76 61 is open and ava
20e30 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 2e 20 ilable for use.
20e40 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e Return.** the n
20e50 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e umber of errors.
20e60 20 20 4c 65 61 76 65 20 61 6e 79 20 65 72 72 6f Leave any erro
20e70 72 20 6d 65 73 73 61 67 65 73 20 69 6e 20 74 68 r messages in th
20e80 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 e pParse structu
20e90 72 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 re..*/.int sqlit
20ea0 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 e3OpenTempDataba
20eb0 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 se(Parse *pParse
20ec0 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
20ed0 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
20ee0 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e if( db->aDb[1].
20ef0 70 42 74 3d 3d 30 20 26 26 20 21 70 50 61 72 73 pBt==0 && !pPars
20f00 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 e->explain ){.
20f10 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 42 74 int rc;. Bt
20f20 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 73 74 ree *pBt;. st
20f30 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 66 atic const int f
20f40 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20 20 20 lags = .
20f50 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 SQLITE_OPEN_RE
20f60 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 ADWRITE |.
20f70 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
20f80 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 CREATE |.
20f90 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 SQLITE_OPEN_E
20fa0 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 XCLUSIVE |.
20fb0 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
20fc0 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c _DELETEONCLOSE |
20fd0 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 . SQLIT
20fe0 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a E_OPEN_TEMP_DB;.
20ff0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
21000 33 42 74 72 65 65 4f 70 65 6e 28 64 62 2d 3e 70 3BtreeOpen(db->p
21010 56 66 73 2c 20 30 2c 20 64 62 2c 20 26 70 42 74 Vfs, 0, db, &pBt
21020 2c 20 30 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 , 0, flags);.
21030 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
21040 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
21050 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
21060 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f se, "unable to o
21070 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 pen a temporary
21080 64 61 74 61 62 61 73 65 20 22 0a 20 20 20 20 20 database ".
21090 20 20 20 22 66 69 6c 65 20 66 6f 72 20 73 74 6f "file for sto
210a0 72 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 ring temporary t
210b0 61 62 6c 65 73 22 29 3b 0a 20 20 20 20 20 20 70 ables");. p
210c0 50 61 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a Parse->rc = rc;.
210d0 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
210e0 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 61 44 }. db->aD
210f0 62 5b 31 5d 2e 70 42 74 20 3d 20 70 42 74 3b 0a b[1].pBt = pBt;.
21100 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
21110 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 aDb[1].pSchema )
21120 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 ;. if( SQLITE
21130 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 42 _NOMEM==sqlite3B
21140 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 treeSetPageSize(
21150 70 42 74 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67 pBt, db->nextPag
21160 65 73 69 7a 65 2c 20 2d 31 2c 20 30 29 20 29 7b esize, -1, 0) ){
21170 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 6f . sqlite3Oo
21180 6d 46 61 75 6c 74 28 64 62 29 3b 0a 20 20 20 20 mFault(db);.
21190 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
211a0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
211b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 ;.}../*.** Recor
211c0 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 d the fact that
211d0 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 the schema cooki
211e0 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62 e will need to b
211f0 65 20 76 65 72 69 66 69 65 64 0a 2a 2a 20 66 6f e verified.** fo
21200 72 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 r database iDb.
21210 20 54 68 65 20 63 6f 64 65 20 74 6f 20 61 63 74 The code to act
21220 75 61 6c 6c 79 20 76 65 72 69 66 79 20 74 68 65 ually verify the
21230 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 2a schema cookie.*
21240 2a 20 77 69 6c 6c 20 6f 63 63 75 72 20 61 74 20 * will occur at
21250 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 the end of the t
21260 6f 70 2d 6c 65 76 65 6c 20 56 44 42 45 20 61 6e op-level VDBE an
21270 64 20 77 69 6c 6c 20 62 65 20 67 65 6e 65 72 61 d will be genera
21280 74 65 64 0a 2a 2a 20 6c 61 74 65 72 2c 20 62 79 ted.** later, by
21290 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f sqlite3FinishCo
212a0 64 69 6e 67 28 29 2e 0a 2a 2f 0a 76 6f 69 64 20 ding()..*/.void
212b0 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 sqlite3CodeVerif
212c0 79 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 ySchema(Parse *p
212d0 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b Parse, int iDb){
212e0 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 . Parse *pTople
212f0 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 vel = sqlite3Par
21300 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 seToplevel(pPars
21310 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 e);.. assert( i
21320 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 70 50 61 Db>=0 && iDb<pPa
21330 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a rse->db->nDb );.
21340 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
21350 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 ->db->aDb[iDb].p
21360 42 74 21 3d 30 20 7c 7c 20 69 44 62 3d 3d 31 20 Bt!=0 || iDb==1
21370 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 );. assert( iDb
21380 3c 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 <SQLITE_MAX_ATTA
21390 43 48 45 44 2b 32 20 29 3b 0a 20 20 61 73 73 65 CHED+2 );. asse
213a0 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d rt( sqlite3Schem
213b0 61 4d 75 74 65 78 48 65 6c 64 28 70 50 61 72 73 aMutexHeld(pPars
213c0 65 2d 3e 64 62 2c 20 69 44 62 2c 20 30 29 20 29 e->db, iDb, 0) )
213d0 3b 0a 20 20 69 66 28 20 44 62 4d 61 73 6b 54 65 ;. if( DbMaskTe
213e0 73 74 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f st(pToplevel->co
213f0 6f 6b 69 65 4d 61 73 6b 2c 20 69 44 62 29 3d 3d okieMask, iDb)==
21400 30 20 29 7b 0a 20 20 20 20 44 62 4d 61 73 6b 53 0 ){. DbMaskS
21410 65 74 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f et(pToplevel->co
21420 6f 6b 69 65 4d 61 73 6b 2c 20 69 44 62 29 3b 0a okieMask, iDb);.
21430 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 if( !OMIT_TE
21440 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 MPDB && iDb==1 )
21450 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f {. sqlite3O
21460 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 penTempDatabase(
21470 70 54 6f 70 6c 65 76 65 6c 29 3b 0a 20 20 20 20 pToplevel);.
21480 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 }. }.}../*.** I
21490 66 20 61 72 67 75 6d 65 6e 74 20 7a 44 62 20 69 f argument zDb i
214a0 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 61 6c s NULL, then cal
214b0 6c 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 l sqlite3CodeVer
214c0 69 66 79 53 63 68 65 6d 61 28 29 20 66 6f 72 20 ifySchema() for
214d0 65 61 63 68 20 0a 2a 2a 20 61 74 74 61 63 68 65 each .** attache
214e0 64 20 64 61 74 61 62 61 73 65 2e 20 4f 74 68 65 d database. Othe
214f0 72 77 69 73 65 2c 20 69 6e 76 6f 6b 65 20 69 74 rwise, invoke it
21500 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 for the databas
21510 65 20 6e 61 6d 65 64 20 7a 44 62 20 6f 6e 6c 79 e named zDb only
21520 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
21530 33 43 6f 64 65 56 65 72 69 66 79 4e 61 6d 65 64 3CodeVerifyNamed
21540 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 Schema(Parse *pP
21550 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 arse, const char
21560 20 2a 7a 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 *zDb){. sqlite
21570 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
21580 64 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 db;. int i;. f
21590 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
215a0 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44 62 20 b; i++){. Db
215b0 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b *pDb = &db->aDb[
215c0 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d i];. if( pDb-
215d0 3e 70 42 74 20 26 26 20 28 21 7a 44 62 20 7c 7c >pBt && (!zDb ||
215e0 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 0==sqlite3StrIC
215f0 6d 70 28 7a 44 62 2c 20 70 44 62 2d 3e 7a 44 62 mp(zDb, pDb->zDb
21600 53 4e 61 6d 65 29 29 20 29 7b 0a 20 20 20 20 20 SName)) ){.
21610 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 sqlite3CodeVeri
21620 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c fySchema(pParse,
21630 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d i);. }. }.}
21640 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
21650 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 VDBE code that
21660 70 72 65 70 61 72 65 73 20 66 6f 72 20 64 6f 69 prepares for doi
21670 6e 67 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 ng an operation
21680 74 68 61 74 0a 2a 2a 20 6d 69 67 68 74 20 63 68 that.** might ch
21690 61 6e 67 65 20 74 68 65 20 64 61 74 61 62 61 73 ange the databas
216a0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
216b0 75 74 69 6e 65 20 73 74 61 72 74 73 20 61 20 6e utine starts a n
216c0 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ew transaction i
216d0 66 20 77 65 20 61 72 65 20 6e 6f 74 20 61 6c 72 f we are not alr
216e0 65 61 64 79 20 77 69 74 68 69 6e 0a 2a 2a 20 61 eady within.** a
216f0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 transaction. I
21700 66 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 79 f we are already
21710 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 within a transa
21720 63 74 69 6f 6e 2c 20 74 68 65 6e 20 61 20 63 68 ction, then a ch
21730 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20 69 73 20 73 eckpoint.** is s
21740 65 74 20 69 66 20 74 68 65 20 73 65 74 53 74 61 et if the setSta
21750 74 65 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 tement parameter
21760 20 69 73 20 74 72 75 65 2e 20 20 41 20 63 68 65 is true. A che
21770 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64 0a 2a ckpoint should.*
21780 2a 20 62 65 20 73 65 74 20 66 6f 72 20 6f 70 65 * be set for ope
21790 72 61 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 rations that mig
217a0 68 74 20 66 61 69 6c 20 28 64 75 65 20 74 6f 20 ht fail (due to
217b0 61 20 63 6f 6e 73 74 72 61 69 6e 74 29 20 70 61 a constraint) pa
217c0 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 77 61 79 rt of.** the way
217d0 20 74 68 72 6f 75 67 68 20 61 6e 64 20 77 68 69 through and whi
217e0 63 68 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 ch will need to
217f0 75 6e 64 6f 20 73 6f 6d 65 20 77 72 69 74 65 73 undo some writes
21800 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
21810 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 74 to.** rollback t
21820 68 65 20 77 68 6f 6c 65 20 74 72 61 6e 73 61 63 he whole transac
21830 74 69 6f 6e 2e 20 20 46 6f 72 20 6f 70 65 72 61 tion. For opera
21840 74 69 6f 6e 73 20 77 68 65 72 65 20 61 6c 6c 20 tions where all
21850 63 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 20 63 constraints.** c
21860 61 6e 20 62 65 20 63 68 65 63 6b 65 64 20 62 65 an be checked be
21870 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 fore any changes
21880 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 are made to the
21890 20 64 61 74 61 62 61 73 65 2c 20 69 74 20 69 73 database, it is
218a0 20 6e 65 76 65 72 0a 2a 2a 20 6e 65 63 65 73 73 never.** necess
218b0 61 72 79 20 74 6f 20 75 6e 64 6f 20 61 20 77 72 ary to undo a wr
218c0 69 74 65 20 61 6e 64 20 74 68 65 20 63 68 65 63 ite and the chec
218d0 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 6e 6f kpoint should no
218e0 74 20 62 65 20 73 65 74 2e 0a 2a 2f 0a 76 6f 69 t be set..*/.voi
218f0 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 d sqlite3BeginWr
21900 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 iteOperation(Par
21910 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
21920 73 65 74 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e setStatement, in
21930 74 20 69 44 62 29 7b 0a 20 20 50 61 72 73 65 20 t iDb){. Parse
21940 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c *pToplevel = sql
21950 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 ite3ParseTopleve
21960 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c l(pParse);. sql
21970 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 ite3CodeVerifySc
21980 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 hema(pParse, iDb
21990 29 3b 0a 20 20 44 62 4d 61 73 6b 53 65 74 28 70 );. DbMaskSet(p
219a0 54 6f 70 6c 65 76 65 6c 2d 3e 77 72 69 74 65 4d Toplevel->writeM
219b0 61 73 6b 2c 20 69 44 62 29 3b 0a 20 20 70 54 6f ask, iDb);. pTo
219c0 70 6c 65 76 65 6c 2d 3e 69 73 4d 75 6c 74 69 57 plevel->isMultiW
219d0 72 69 74 65 20 7c 3d 20 73 65 74 53 74 61 74 65 rite |= setState
219e0 6d 65 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ment;.}../*.** I
219f0 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 ndicate that the
21a00 20 73 74 61 74 65 6d 65 6e 74 20 63 75 72 72 65 statement curre
21a10 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 ntly under const
21a20 72 75 63 74 69 6f 6e 20 6d 69 67 68 74 20 77 72 ruction might wr
21a30 69 74 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e ite.** more than
21a40 20 6f 6e 65 20 65 6e 74 72 79 20 28 65 78 61 6d one entry (exam
21a50 70 6c 65 3a 20 64 65 6c 65 74 69 6e 67 20 6f 6e ple: deleting on
21a60 65 20 72 6f 77 20 74 68 65 6e 20 69 6e 73 65 72 e row then inser
21a70 74 69 6e 67 20 61 6e 6f 74 68 65 72 2c 0a 2a 2a ting another,.**
21a80 20 69 6e 73 65 72 74 69 6e 67 20 6d 75 6c 74 69 inserting multi
21a90 70 6c 65 20 72 6f 77 73 20 69 6e 20 61 20 74 61 ple rows in a ta
21aa0 62 6c 65 2c 20 6f 72 20 69 6e 73 65 72 74 69 6e ble, or insertin
21ab0 67 20 61 20 72 6f 77 20 61 6e 64 20 69 6e 64 65 g a row and inde
21ac0 78 20 65 6e 74 72 69 65 73 2e 29 0a 2a 2a 20 49 x entries.).** I
21ad0 66 20 61 6e 20 61 62 6f 72 74 20 6f 63 63 75 72 f an abort occur
21ae0 73 20 61 66 74 65 72 20 73 6f 6d 65 20 6f 66 20 s after some of
21af0 74 68 65 73 65 20 77 72 69 74 65 73 20 68 61 76 these writes hav
21b00 65 20 63 6f 6d 70 6c 65 74 65 64 2c 20 74 68 65 e completed, the
21b10 6e 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 n it will.** be
21b20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64 necessary to und
21b30 6f 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 64 20 o the completed
21b40 77 72 69 74 65 73 2e 0a 2a 2f 0a 76 6f 69 64 20 writes..*/.void
21b50 73 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 sqlite3MultiWrit
21b60 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 e(Parse *pParse)
21b70 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c {. Parse *pTopl
21b80 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 evel = sqlite3Pa
21b90 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 rseToplevel(pPar
21ba0 73 65 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c se);. pToplevel
21bb0 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 3d ->isMultiWrite =
21bc0 20 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 1;.}../* .** Th
21bd0 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 e code generator
21be0 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 calls this rout
21bf0 69 6e 65 20 69 66 20 69 73 20 64 69 73 63 6f 76 ine if is discov
21c00 65 72 73 20 74 68 61 74 20 69 74 20 69 73 0a 2a ers that it is.*
21c10 2a 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 62 * possible to ab
21c20 6f 72 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 ort a statement
21c30 70 72 69 6f 72 20 74 6f 20 63 6f 6d 70 6c 65 74 prior to complet
21c40 69 6f 6e 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 ion. In order t
21c50 6f 20 0a 2a 2a 20 70 65 72 66 6f 72 6d 20 74 68 o .** perform th
21c60 69 73 20 61 62 6f 72 74 20 77 69 74 68 6f 75 74 is abort without
21c70 20 63 6f 72 72 75 70 74 69 6e 67 20 74 68 65 20 corrupting the
21c80 64 61 74 61 62 61 73 65 2c 20 77 65 20 6e 65 65 database, we nee
21c90 64 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 d to make.** sur
21ca0 65 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 e that the state
21cb0 6d 65 6e 74 20 69 73 20 70 72 6f 74 65 63 74 65 ment is protecte
21cc0 64 20 62 79 20 61 20 73 74 61 74 65 6d 65 6e 74 d by a statement
21cd0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
21ce0 0a 2a 2a 20 54 65 63 68 6e 69 63 61 6c 6c 79 2c .** Technically,
21cf0 20 77 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f we only need to
21d00 20 73 65 74 20 74 68 65 20 6d 61 79 41 62 6f 72 set the mayAbor
21d10 74 20 66 6c 61 67 20 69 66 20 74 68 65 0a 2a 2a t flag if the.**
21d20 20 69 73 4d 75 6c 74 69 57 72 69 74 65 20 66 6c isMultiWrite fl
21d30 61 67 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c ag was previousl
21d40 79 20 73 65 74 2e 20 20 54 68 65 72 65 20 69 73 y set. There is
21d50 20 61 20 74 69 6d 65 20 64 65 70 65 6e 64 65 6e a time dependen
21d60 63 79 0a 2a 2a 20 73 75 63 68 20 74 68 61 74 20 cy.** such that
21d70 74 68 65 20 61 62 6f 72 74 20 6d 75 73 74 20 6f the abort must o
21d80 63 63 75 72 20 61 66 74 65 72 20 74 68 65 20 6d ccur after the m
21d90 75 6c 74 69 77 72 69 74 65 2e 20 20 54 68 69 73 ultiwrite. This
21da0 20 6d 61 6b 65 73 0a 2a 2a 20 73 6f 6d 65 20 73 makes.** some s
21db0 74 61 74 65 6d 65 6e 74 73 20 69 6e 76 6f 6c 76 tatements involv
21dc0 69 6e 67 20 74 68 65 20 52 45 50 4c 41 43 45 20 ing the REPLACE
21dd0 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 conflict resolut
21de0 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 0a 2a 2a ion algorithm.**
21df0 20 67 6f 20 61 20 6c 69 74 74 6c 65 20 66 61 73 go a little fas
21e00 74 65 72 2e 20 20 42 75 74 20 74 61 6b 69 6e 67 ter. But taking
21e10 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 advantage of th
21e20 69 73 20 74 69 6d 65 20 64 65 70 65 6e 64 65 6e is time dependen
21e30 63 79 0a 2a 2a 20 6d 61 6b 65 73 20 69 74 20 6d cy.** makes it m
21e40 6f 72 65 20 64 69 66 66 69 63 75 6c 74 20 74 6f ore difficult to
21e50 20 70 72 6f 76 65 20 74 68 61 74 20 74 68 65 20 prove that the
21e60 63 6f 64 65 20 69 73 20 63 6f 72 72 65 63 74 20 code is correct
21e70 28 69 6e 20 0a 2a 2a 20 70 61 72 74 69 63 75 6c (in .** particul
21e80 61 72 2c 20 69 74 20 70 72 65 76 65 6e 74 73 20 ar, it prevents
21e90 75 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 us from writing
21ea0 61 6e 20 65 66 66 65 63 74 69 76 65 0a 2a 2a 20 an effective.**
21eb0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
21ec0 66 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d f sqlite3AssertM
21ed0 61 79 41 62 6f 72 74 28 29 29 20 61 6e 64 20 73 ayAbort()) and s
21ee0 6f 20 77 65 20 68 61 76 65 20 63 68 6f 73 65 6e o we have chosen
21ef0 0a 2a 2a 20 74 6f 20 74 61 6b 65 20 74 68 65 20 .** to take the
21f00 73 61 66 65 20 72 6f 75 74 65 20 61 6e 64 20 73 safe route and s
21f10 6b 69 70 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 kip the optimiza
21f20 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 tion..*/.void sq
21f30 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50 61 lite3MayAbort(Pa
21f40 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 rse *pParse){.
21f50 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c Parse *pToplevel
21f60 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 = sqlite3ParseT
21f70 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b oplevel(pParse);
21f80 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6d 61 . pToplevel->ma
21f90 79 41 62 6f 72 74 20 3d 20 31 3b 0a 7d 0a 0a 2f yAbort = 1;.}../
21fa0 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f *.** Code an OP_
21fb0 48 61 6c 74 20 74 68 61 74 20 63 61 75 73 65 73 Halt that causes
21fc0 20 74 68 65 20 76 64 62 65 20 74 6f 20 72 65 74 the vdbe to ret
21fd0 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f urn an SQLITE_CO
21fe0 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 65 72 72 6f NSTRAINT.** erro
21ff0 72 2e 20 54 68 65 20 6f 6e 45 72 72 6f 72 20 70 r. The onError p
22000 61 72 61 6d 65 74 65 72 20 64 65 74 65 72 6d 69 arameter determi
22010 6e 65 73 20 77 68 69 63 68 20 28 69 66 20 61 6e nes which (if an
22020 79 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d y) of the statem
22030 65 6e 74 0a 2a 2a 20 61 6e 64 2f 6f 72 20 63 75 ent.** and/or cu
22040 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
22050 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b n is rolled back
22060 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
22070 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 3HaltConstraint(
22080 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
22090 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 , /* Parsing
220a0 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 context */. int
220b0 20 65 72 72 43 6f 64 65 2c 20 20 20 20 20 20 2f errCode, /
220c0 2a 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 * extended error
220d0 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6f code */. int o
220e0 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 nError, /*
220f0 43 6f 6e 73 74 72 61 69 6e 74 20 74 79 70 65 20 Constraint type
22100 2a 2f 0a 20 20 63 68 61 72 20 2a 70 34 2c 20 20 */. char *p4,
22110 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 /* Error
22120 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 69 38 20 message */. i8
22130 70 34 74 79 70 65 2c 20 20 20 20 20 20 20 20 2f p4type, /
22140 2a 20 50 34 5f 53 54 41 54 49 43 20 6f 72 20 50 * P4_STATIC or P
22150 34 5f 54 52 41 4e 53 49 45 4e 54 20 2a 2f 0a 20 4_TRANSIENT */.
22160 20 75 38 20 70 35 45 72 72 6d 73 67 20 20 20 20 u8 p5Errmsg
22170 20 20 20 2f 2a 20 50 35 5f 45 72 72 4d 73 67 20 /* P5_ErrMsg
22180 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 type */.){. Vdb
22190 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 e *v = sqlite3Ge
221a0 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
221b0 20 61 73 73 65 72 74 28 20 28 65 72 72 43 6f 64 assert( (errCod
221c0 65 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f e&0xff)==SQLITE_
221d0 43 4f 4e 53 54 52 41 49 4e 54 20 29 3b 0a 20 20 CONSTRAINT );.
221e0 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f if( onError==OE_
221f0 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 73 71 6c Abort ){. sql
22200 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 ite3MayAbort(pPa
22210 72 73 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 rse);. }. sqli
22220 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
22230 20 4f 50 5f 48 61 6c 74 2c 20 65 72 72 43 6f 64 OP_Halt, errCod
22240 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 70 e, onError, 0, p
22250 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 73 71 4, p4type);. sq
22260 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
22270 35 28 76 2c 20 70 35 45 72 72 6d 73 67 29 3b 0a 5(v, p5Errmsg);.
22280 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e }../*.** Code an
22290 20 4f 50 5f 48 61 6c 74 20 64 75 65 20 74 6f 20 OP_Halt due to
222a0 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41 52 UNIQUE or PRIMAR
222b0 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 Y KEY constraint
222c0 20 76 69 6f 6c 61 74 69 6f 6e 2e 0a 2a 2f 0a 76 violation..*/.v
222d0 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 69 71 75 oid sqlite3Uniqu
222e0 65 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 eConstraint(. P
222f0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
22300 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
22310 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 ext */. int onE
22320 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 43 6f rror, /* Co
22330 6e 73 74 72 61 69 6e 74 20 74 79 70 65 20 2a 2f nstraint type */
22340 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 20 . Index *pIdx
22350 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 /* The inde
22360 78 20 74 68 61 74 20 74 72 69 67 67 65 72 73 20 x that triggers
22370 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a the constraint *
22380 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 45 72 /.){. char *zEr
22390 72 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 53 74 r;. int j;. St
223a0 72 41 63 63 75 6d 20 65 72 72 4d 73 67 3b 0a 20 rAccum errMsg;.
223b0 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 Table *pTab = p
223c0 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 0a 20 20 Idx->pTable;..
223d0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 sqlite3StrAccumI
223e0 6e 69 74 28 26 65 72 72 4d 73 67 2c 20 70 50 61 nit(&errMsg, pPa
223f0 72 73 65 2d 3e 64 62 2c 20 30 2c 20 30 2c 20 32 rse->db, 0, 0, 2
22400 30 30 29 3b 0a 20 20 69 66 28 20 70 49 64 78 2d 00);. if( pIdx-
22410 3e 61 43 6f 6c 45 78 70 72 20 29 7b 0a 20 20 20 >aColExpr ){.
22420 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 sqlite3XPrintf(
22430 26 65 72 72 4d 73 67 2c 20 22 69 6e 64 65 78 20 &errMsg, "index
22440 27 25 71 27 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 '%q'", pIdx->zNa
22450 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 me);. }else{.
22460 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 for(j=0; j<pId
22470 78 2d 3e 6e 4b 65 79 43 6f 6c 3b 20 6a 2b 2b 29 x->nKeyCol; j++)
22480 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 {. char *zC
22490 6f 6c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ol;. assert
224a0 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e ( pIdx->aiColumn
224b0 5b 6a 5d 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 [j]>=0 );.
224c0 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f zCol = pTab->aCo
224d0 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e l[pIdx->aiColumn
224e0 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 [j]].zName;.
224f0 20 20 69 66 28 20 6a 20 29 20 73 71 6c 69 74 65 if( j ) sqlite
22500 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
22510 26 65 72 72 4d 73 67 2c 20 22 2c 20 22 2c 20 32 &errMsg, ", ", 2
22520 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
22530 58 50 72 69 6e 74 66 28 26 65 72 72 4d 73 67 2c XPrintf(&errMsg,
22540 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e "%s.%s", pTab->
22550 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 zName, zCol);.
22560 20 20 7d 0a 20 20 7d 0a 20 20 7a 45 72 72 20 3d }. }. zErr =
22570 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
22580 46 69 6e 69 73 68 28 26 65 72 72 4d 73 67 29 3b Finish(&errMsg);
22590 0a 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f . sqlite3HaltCo
225a0 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c nstraint(pParse,
225b0 20 0a 20 20 20 20 49 73 50 72 69 6d 61 72 79 4b . IsPrimaryK
225c0 65 79 49 6e 64 65 78 28 70 49 64 78 29 20 3f 20 eyIndex(pIdx) ?
225d0 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
225e0 54 5f 50 52 49 4d 41 52 59 4b 45 59 20 0a 20 20 T_PRIMARYKEY .
225f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22600 20 20 20 20 20 20 20 20 20 20 3a 20 53 51 4c 49 : SQLI
22610 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 55 4e TE_CONSTRAINT_UN
22620 49 51 55 45 2c 0a 20 20 20 20 6f 6e 45 72 72 6f IQUE,. onErro
22630 72 2c 20 7a 45 72 72 2c 20 50 34 5f 44 59 4e 41 r, zErr, P4_DYNA
22640 4d 49 43 2c 20 50 35 5f 43 6f 6e 73 74 72 61 69 MIC, P5_Constrai
22650 6e 74 55 6e 69 71 75 65 29 3b 0a 7d 0a 0a 0a 2f ntUnique);.}.../
22660 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f *.** Code an OP_
22670 48 61 6c 74 20 64 75 65 20 74 6f 20 6e 6f 6e 2d Halt due to non-
22680 75 6e 69 71 75 65 20 72 6f 77 69 64 2e 0a 2a 2f unique rowid..*/
22690 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 .void sqlite3Row
226a0 69 64 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 idConstraint(.
226b0 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
226c0 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
226d0 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e text */. int on
226e0 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 43 Error, /* C
226f0 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 onflict resoluti
22700 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 20 2a 2f 0a on algorithm */.
22710 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 20 20 Table *pTab
22720 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 /* The table
22730 20 77 69 74 68 20 74 68 65 20 6e 6f 6e 2d 75 6e with the non-un
22740 69 71 75 65 20 72 6f 77 69 64 20 2a 2f 20 0a 29 ique rowid */ .)
22750 7b 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a {. char *zMsg;.
22760 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 int rc;. if(
22770 70 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 pTab->iPKey>=0 )
22780 7b 0a 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c {. zMsg = sql
22790 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 ite3MPrintf(pPar
227a0 73 65 2d 3e 64 62 2c 20 22 25 73 2e 25 73 22 2c se->db, "%s.%s",
227b0 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 pTab->zName,.
227c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
227d0 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 pTab->aC
227e0 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e ol[pTab->iPKey].
227f0 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d zName);. rc =
22800 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
22810 4e 54 5f 50 52 49 4d 41 52 59 4b 45 59 3b 0a 20 NT_PRIMARYKEY;.
22820 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4d 73 67 }else{. zMsg
22830 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
22840 66 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 25 f(pParse->db, "%
22850 73 2e 72 6f 77 69 64 22 2c 20 70 54 61 62 2d 3e s.rowid", pTab->
22860 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d zName);. rc =
22870 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
22880 4e 54 5f 52 4f 57 49 44 3b 0a 20 20 7d 0a 20 20 NT_ROWID;. }.
22890 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 sqlite3HaltConst
228a0 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20 72 63 raint(pParse, rc
228b0 2c 20 6f 6e 45 72 72 6f 72 2c 20 7a 4d 73 67 2c , onError, zMsg,
228c0 20 50 34 5f 44 59 4e 41 4d 49 43 2c 0a 20 20 20 P4_DYNAMIC,.
228d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
228e0 20 20 20 20 20 50 35 5f 43 6f 6e 73 74 72 61 69 P5_Constrai
228f0 6e 74 55 6e 69 71 75 65 29 3b 0a 7d 0a 0a 2f 2a ntUnique);.}../*
22900 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 .** Check to see
22910 20 69 66 20 70 49 6e 64 65 78 20 75 73 65 73 20 if pIndex uses
22920 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 the collating se
22930 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 52 quence pColl. R
22940 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 eturn.** true if
22950 20 69 74 20 64 6f 65 73 20 61 6e 64 20 66 61 6c it does and fal
22960 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f se if it does no
22970 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 t..*/.#ifndef SQ
22980 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 LITE_OMIT_REINDE
22990 58 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c X.static int col
229a0 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e 73 lationMatch(cons
229b0 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49 t char *zColl, I
229c0 6e 64 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20 ndex *pIndex){.
229d0 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 int i;. assert
229e0 28 20 7a 43 6f 6c 6c 21 3d 30 20 29 3b 0a 20 20 ( zColl!=0 );.
229f0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 for(i=0; i<pInde
22a00 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 x->nColumn; i++)
22a10 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 {. const char
22a20 20 2a 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a *z = pIndex->az
22a30 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73 Coll[i];. ass
22a40 65 72 74 28 20 7a 21 3d 30 20 7c 7c 20 70 49 6e ert( z!=0 || pIn
22a50 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d dex->aiColumn[i]
22a60 3c 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 <0 );. if( pI
22a70 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 ndex->aiColumn[i
22a80 5d 3e 3d 30 20 26 26 20 30 3d 3d 73 71 6c 69 74 ]>=0 && 0==sqlit
22a90 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f e3StrICmp(z, zCo
22aa0 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ll) ){. ret
22ab0 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 1;. }. }
22ac0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 . return 0;.}.#
22ad0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 endif../*.** Rec
22ae0 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 ompute all indic
22af0 65 73 20 6f 66 20 70 54 61 62 20 74 68 61 74 20 es of pTab that
22b00 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e use the collatin
22b10 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c g sequence pColl
22b20 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 ..** If pColl==0
22b30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 20 then recompute
22b40 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 all indices of p
22b50 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 Tab..*/.#ifndef
22b60 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e SQLITE_OMIT_REIN
22b70 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20 DEX.static void
22b80 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50 61 72 reindexTable(Par
22b90 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c se *pParse, Tabl
22ba0 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20 63 6f e *pTab, char co
22bb0 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49 nst *zColl){. I
22bc0 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 ndex *pIndex;
22bd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e /* An
22be0 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 index associate
22bf0 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a d with pTab */..
22c00 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54 61 for(pIndex=pTa
22c10 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 b->pIndex; pInde
22c20 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65 78 x; pIndex=pIndex
22c30 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
22c40 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f ( zColl==0 || co
22c50 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f llationMatch(zCo
22c60 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0a 20 ll, pIndex) ){.
22c70 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 int iDb = s
22c80 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
22c90 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 dex(pParse->db,
22ca0 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
22cb0 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 sqlite3Beg
22cc0 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e inWriteOperation
22cd0 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 (pParse, 0, iDb)
22ce0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 ;. sqlite3R
22cf0 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 efillIndex(pPars
22d00 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a e, pIndex, -1);.
22d10 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 }. }.}.#end
22d20 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 if../*.** Recomp
22d30 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 ute all indices
22d40 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e of all tables in
22d50 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 77 all databases w
22d60 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69 here the.** indi
22d70 63 65 73 20 75 73 65 20 74 68 65 20 63 6f 6c 6c ces use the coll
22d80 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70 ating sequence p
22d90 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d Coll. If pColl=
22da0 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 =0 then recomput
22db0 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73 e.** all indices
22dc0 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a everywhere..*/.
22dd0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
22de0 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 MIT_REINDEX.stat
22df0 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78 44 ic void reindexD
22e00 61 74 61 62 61 73 65 73 28 50 61 72 73 65 20 2a atabases(Parse *
22e10 70 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f 6e pParse, char con
22e20 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62 st *zColl){. Db
22e30 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20 *pDb;
22e40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 /* A s
22e50 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 2a ingle database *
22e60 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 /. int iDb;
22e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22e80 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
22e90 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a index number */.
22ea0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
22eb0 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a pParse->db; /*
22ec0 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
22ed0 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61 nnection */. Ha
22ee0 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20 shElem *k;
22ef0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 /* For
22f00 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74 61 looping over ta
22f10 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20 bles in pDb */.
22f20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 Table *pTab;
22f30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
22f40 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 A table in the d
22f50 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20 61 73 atabase */.. as
22f60 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
22f70 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 eeHoldsAllMutexe
22f80 73 28 64 62 29 20 29 3b 20 20 2f 2a 20 4e 65 65 s(db) ); /* Nee
22f90 64 65 64 20 66 6f 72 20 73 63 68 65 6d 61 20 61 ded for schema a
22fa0 63 63 65 73 73 20 2a 2f 0a 20 20 66 6f 72 28 69 ccess */. for(i
22fb0 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44 Db=0, pDb=db->aD
22fc0 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 b; iDb<db->nDb;
22fd0 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20 iDb++, pDb++){.
22fe0 20 20 20 61 73 73 65 72 74 28 20 70 44 62 21 3d assert( pDb!=
22ff0 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73 0 );. for(k=s
23000 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 qliteHashFirst(&
23010 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 pDb->pSchema->tb
23020 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71 lHash); k; k=sq
23030 6c 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29 liteHashNext(k))
23040 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 28 {. pTab = (
23050 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 Table*)sqliteHas
23060 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 20 20 hData(k);.
23070 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61 reindexTable(pPa
23080 72 73 65 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c rse, pTab, zColl
23090 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 );. }. }.}.#
230a0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e endif../*.** Gen
230b0 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 erate code for t
230c0 68 65 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61 he REINDEX comma
230d0 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 nd..**.**
230e0 20 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 REINDEX
230f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23100 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 -- 1.**
23110 20 20 20 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c REINDEX <col
23120 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20 20 20 20 lation>
23130 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 -- 2.**
23140 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c REINDEX ?<
23150 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c database>.?<tabl
23160 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20 ename> -- 3.**
23170 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 REINDEX
23180 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e ?<database>.?<in
23190 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a dexname> -- 4.*
231a0 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 *.** Form 1 caus
231b0 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 es all indices i
231c0 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 n all attached d
231d0 61 74 61 62 61 73 65 73 20 74 6f 20 62 65 20 72 atabases to be r
231e0 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20 ebuilt..** Form
231f0 32 20 72 65 62 75 69 6c 64 73 20 61 6c 6c 20 69 2 rebuilds all i
23200 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 64 61 ndices in all da
23210 74 61 62 61 73 65 73 20 74 68 61 74 20 75 73 65 tabases that use
23220 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f the named.** co
23230 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e llating function
23240 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e 64 20 34 . Forms 3 and 4
23250 20 72 65 62 75 69 6c 64 20 74 68 65 20 6e 61 6d rebuild the nam
23260 65 64 20 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a ed index or all.
23270 2a 2a 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 ** indices assoc
23280 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e iated with the n
23290 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 amed table..*/.#
232a0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
232b0 49 54 5f 52 45 49 4e 44 45 58 0a 76 6f 69 64 20 IT_REINDEX.void
232c0 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 sqlite3Reindex(P
232d0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f arse *pParse, To
232e0 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b ken *pName1, Tok
232f0 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 43 en *pName2){. C
23300 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 ollSeq *pColl;
23310 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
23320 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
23330 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 to be reindexed
23340 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 , or NULL */. c
23350 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 har *z;
23360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
23370 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 me of a table or
23380 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 index */. cons
23390 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 t char *zDb;
233a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
233b0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
233c0 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 */. Table *pTab
233d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
233e0 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 /* A table in t
233f0 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
23400 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 Index *pIndex;
23410 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
23420 41 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 An index associa
23430 74 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f ted with pTab */
23440 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 . int iDb;
23450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
23460 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 * The database i
23470 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 ndex number */.
23480 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
23490 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 Parse->db; /*
234a0 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
234b0 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b nection */. Tok
234c0 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 en *pObjName;
234d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
234e0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 of the table or
234f0 20 69 6e 64 65 78 20 74 6f 20 62 65 20 72 65 69 index to be rei
23500 6e 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 ndexed */.. /*
23510 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 Read the databas
23520 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 e schema. If an
23530 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 error occurs, le
23540 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ave an error mes
23550 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f sage. ** and co
23560 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 de in pParse and
23570 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f return NULL. */
23580 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
23590 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 !=sqlite3ReadSch
235a0 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 ema(pParse) ){.
235b0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a return;. }..
235c0 20 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 if( pName1==0
235d0 29 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 44 61 ){. reindexDa
235e0 74 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 tabases(pParse,
235f0 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 0);. return;.
23600 20 20 7d 65 6c 73 65 20 69 66 28 20 4e 45 56 45 }else if( NEVE
23610 52 28 70 4e 61 6d 65 32 3d 3d 30 29 20 7c 7c 20 R(pName2==0) ||
23620 70 4e 61 6d 65 32 2d 3e 7a 3d 3d 30 20 29 7b 0a pName2->z==0 ){.
23630 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b char *zColl;
23640 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 . assert( pNa
23650 6d 65 31 2d 3e 7a 20 29 3b 0a 20 20 20 20 7a 43 me1->z );. zC
23660 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d oll = sqlite3Nam
23670 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 eFromToken(pPars
23680 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a e->db, pName1);.
23690 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 if( !zColl )
236a0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 70 43 6f return;. pCo
236b0 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 ll = sqlite3Find
236c0 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 CollSeq(db, ENC(
236d0 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29 3b 0a db), zColl, 0);.
236e0 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b if( pColl ){
236f0 0a 20 20 20 20 20 20 72 65 69 6e 64 65 78 44 61 . reindexDa
23700 74 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 tabases(pParse,
23710 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 zColl);. sq
23720 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
23730 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 72 65 zColl);. re
23740 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 turn;. }.
23750 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
23760 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 , zColl);. }.
23770 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f iDb = sqlite3Two
23780 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c PartName(pParse,
23790 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c pName1, pName2,
237a0 20 26 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 &pObjName);. i
237b0 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 f( iDb<0 ) retur
237c0 6e 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 n;. z = sqlite3
237d0 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 NameFromToken(db
237e0 2c 20 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 , pObjName);. i
237f0 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e f( z==0 ) return
23800 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 ;. zDb = db->aD
23810 62 5b 69 44 62 5d 2e 7a 44 62 53 4e 61 6d 65 3b b[iDb].zDbSName;
23820 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 . pTab = sqlite
23830 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 3FindTable(db, z
23840 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 70 54 , zDb);. if( pT
23850 61 62 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65 ab ){. reinde
23860 78 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 xTable(pParse, p
23870 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c Tab, 0);. sql
23880 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
23890 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
238a0 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 }. pIndex = sq
238b0 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 lite3FindIndex(d
238c0 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 73 71 b, z, zDb);. sq
238d0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
238e0 7a 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 z);. if( pIndex
238f0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
23900 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 eginWriteOperati
23910 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 on(pParse, 0, iD
23920 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 b);. sqlite3R
23930 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 efillIndex(pPars
23940 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a e, pIndex, -1);.
23950 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
23960 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
23970 67 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c g(pParse, "unabl
23980 65 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 e to identify th
23990 65 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 72 e object to be r
239a0 65 69 6e 64 65 78 65 64 22 29 3b 0a 7d 0a 23 65 eindexed");.}.#e
239b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
239c0 72 6e 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 rn a KeyInfo str
239d0 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20 61 ucture that is a
239e0 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 ppropriate for t
239f0 68 65 20 67 69 76 65 6e 20 49 6e 64 65 78 2e 0a he given Index..
23a00 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 **.** The caller
23a10 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73 should invoke s
23a20 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f 55 6e 72 qlite3KeyInfoUnr
23a30 65 66 28 29 20 6f 6e 20 74 68 65 20 72 65 74 75 ef() on the retu
23a40 72 6e 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 77 rned object.** w
23a50 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 hen it has finis
23a60 68 65 64 20 75 73 69 6e 67 20 69 74 2e 0a 2a 2f hed using it..*/
23a70 0a 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 .KeyInfo *sqlite
23a80 33 4b 65 79 49 6e 66 6f 4f 66 49 6e 64 65 78 28 3KeyInfoOfIndex(
23a90 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 Parse *pParse, I
23aa0 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 ndex *pIdx){. i
23ab0 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c nt i;. int nCol
23ac0 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e = pIdx->nColumn
23ad0 3b 0a 20 20 69 6e 74 20 6e 4b 65 79 20 3d 20 70 ;. int nKey = p
23ae0 49 64 78 2d 3e 6e 4b 65 79 43 6f 6c 3b 0a 20 20 Idx->nKeyCol;.
23af0 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 0a 20 KeyInfo *pKey;.
23b00 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 if( pParse->nEr
23b10 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 r ) return 0;.
23b20 69 66 28 20 70 49 64 78 2d 3e 75 6e 69 71 4e 6f if( pIdx->uniqNo
23b30 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4b 65 tNull ){. pKe
23b40 79 20 3d 20 73 71 6c 69 74 65 33 4b 65 79 49 6e y = sqlite3KeyIn
23b50 66 6f 41 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e foAlloc(pParse->
23b60 64 62 2c 20 6e 4b 65 79 2c 20 6e 43 6f 6c 2d 6e db, nKey, nCol-n
23b70 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Key);. }else{.
23b80 20 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 pKey = sqlite
23b90 33 4b 65 79 49 6e 66 6f 41 6c 6c 6f 63 28 70 50 3KeyInfoAlloc(pP
23ba0 61 72 73 65 2d 3e 64 62 2c 20 6e 43 6f 6c 2c 20 arse->db, nCol,
23bb0 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4b 0);. }. if( pK
23bc0 65 79 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ey ){. assert
23bd0 28 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f ( sqlite3KeyInfo
23be0 49 73 57 72 69 74 65 61 62 6c 65 28 70 4b 65 79 IsWriteable(pKey
23bf0 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ) );. for(i=0
23c00 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a ; i<nCol; i++){.
23c10 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
23c20 20 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e *zColl = pIdx->
23c30 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 azColl[i];.
23c40 20 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 pKey->aColl[i]
23c50 3d 20 7a 43 6f 6c 6c 3d 3d 73 71 6c 69 74 65 33 = zColl==sqlite3
23c60 53 74 72 42 49 4e 41 52 59 20 3f 20 30 20 3a 0a StrBINARY ? 0 :.
23c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23c80 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c sqlite3L
23c90 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 ocateCollSeq(pPa
23ca0 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 rse, zColl);.
23cb0 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 pKey->aSortOr
23cc0 64 65 72 5b 69 5d 20 3d 20 70 49 64 78 2d 3e 61 der[i] = pIdx->a
23cd0 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 SortOrder[i];.
23ce0 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 72 }. if( pPar
23cf0 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 se->nErr ){.
23d00 20 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f sqlite3KeyInfo
23d10 55 6e 72 65 66 28 70 4b 65 79 29 3b 0a 20 20 20 Unref(pKey);.
23d20 20 20 20 70 4b 65 79 20 3d 20 30 3b 0a 20 20 20 pKey = 0;.
23d30 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
23d40 70 4b 65 79 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 pKey;.}..#ifndef
23d50 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 54 45 SQLITE_OMIT_CTE
23d60 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ./* .** This rou
23d70 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 tine is invoked
23d80 6f 6e 63 65 20 70 65 72 20 43 54 45 20 62 79 20 once per CTE by
23d90 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 the parser while
23da0 20 70 61 72 73 69 6e 67 20 61 20 0a 2a 2a 20 57 parsing a .** W
23db0 49 54 48 20 63 6c 61 75 73 65 2e 20 0a 2a 2f 0a ITH clause. .*/.
23dc0 57 69 74 68 20 2a 73 71 6c 69 74 65 33 57 69 74 With *sqlite3Wit
23dd0 68 41 64 64 28 0a 20 20 50 61 72 73 65 20 2a 70 hAdd(. Parse *p
23de0 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
23df0 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
23e00 78 74 20 2a 2f 0a 20 20 57 69 74 68 20 2a 70 57 xt */. With *pW
23e10 69 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 ith,
23e20 2f 2a 20 45 78 69 73 74 69 6e 67 20 57 49 54 48 /* Existing WITH
23e30 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e 55 4c 4c clause, or NULL
23e40 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 */. Token *pNa
23e50 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a me, /*
23e60 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6d Name of the com
23e70 6d 6f 6e 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 45 mon-table */. E
23e80 78 70 72 4c 69 73 74 20 2a 70 41 72 67 6c 69 73 xprList *pArglis
23e90 74 2c 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e t, /* Option
23ea0 61 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 6c al column name l
23eb0 69 73 74 20 66 6f 72 20 74 68 65 20 74 61 62 6c ist for the tabl
23ec0 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 e */. Select *p
23ed0 51 75 65 72 79 20 20 20 20 20 20 20 20 20 20 2f Query /
23ee0 2a 20 51 75 65 72 79 20 75 73 65 64 20 74 6f 20 * Query used to
23ef0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 74 initialize the t
23f00 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c able */.){. sql
23f10 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
23f20 65 2d 3e 64 62 3b 0a 20 20 57 69 74 68 20 2a 70 e->db;. With *p
23f30 4e 65 77 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 New;. char *zNa
23f40 6d 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 me;.. /* Check
23f50 74 68 61 74 20 74 68 65 20 43 54 45 20 6e 61 6d that the CTE nam
23f60 65 20 69 73 20 75 6e 69 71 75 65 20 77 69 74 68 e is unique with
23f70 69 6e 20 74 68 69 73 20 57 49 54 48 20 63 6c 61 in this WITH cla
23f80 75 73 65 2e 20 49 66 0a 20 20 2a 2a 20 6e 6f 74 use. If. ** not
23f90 2c 20 73 74 6f 72 65 20 61 6e 20 65 72 72 6f 72 , store an error
23fa0 20 69 6e 20 74 68 65 20 50 61 72 73 65 20 73 74 in the Parse st
23fb0 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 7a 4e ructure. */. zN
23fc0 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d ame = sqlite3Nam
23fd0 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 eFromToken(pPars
23fe0 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 e->db, pName);.
23ff0 20 69 66 28 20 7a 4e 61 6d 65 20 26 26 20 70 57 if( zName && pW
24000 69 74 68 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 ith ){. int i
24010 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
24020 3c 70 57 69 74 68 2d 3e 6e 43 74 65 3b 20 69 2b <pWith->nCte; i+
24030 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 +){. if( sq
24040 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4e 61 lite3StrICmp(zNa
24050 6d 65 2c 20 70 57 69 74 68 2d 3e 61 5b 69 5d 2e me, pWith->a[i].
24060 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 zName)==0 ){.
24070 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
24080 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 64 75 rMsg(pParse, "du
24090 70 6c 69 63 61 74 65 20 57 49 54 48 20 74 61 62 plicate WITH tab
240a0 6c 65 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e le name: %s", zN
240b0 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ame);. }.
240c0 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 }. }.. if( p
240d0 57 69 74 68 20 29 7b 0a 20 20 20 20 69 6e 74 20 With ){. int
240e0 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a nByte = sizeof(*
240f0 70 57 69 74 68 29 20 2b 20 28 73 69 7a 65 6f 66 pWith) + (sizeof
24100 28 70 57 69 74 68 2d 3e 61 5b 31 5d 29 20 2a 20 (pWith->a[1]) *
24110 70 57 69 74 68 2d 3e 6e 43 74 65 29 3b 0a 20 20 pWith->nCte);.
24120 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
24130 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 57 DbRealloc(db, pW
24140 69 74 68 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d ith, nByte);. }
24150 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d else{. pNew =
24160 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
24170 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
24180 2a 70 57 69 74 68 29 29 3b 0a 20 20 7d 0a 20 20 *pWith));. }.
24190 61 73 73 65 72 74 28 20 28 70 4e 65 77 21 3d 30 assert( (pNew!=0
241a0 20 26 26 20 7a 4e 61 6d 65 21 3d 30 29 20 7c 7c && zName!=0) ||
241b0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
241c0 64 20 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e d );.. if( db->
241d0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
241e0 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c sqlite3ExprL
241f0 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 41 istDelete(db, pA
24200 72 67 6c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c rglist);. sql
24210 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 ite3SelectDelete
24220 28 64 62 2c 20 70 51 75 65 72 79 29 3b 0a 20 20 (db, pQuery);.
24230 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
24240 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 db, zName);.
24250 70 4e 65 77 20 3d 20 70 57 69 74 68 3b 0a 20 20 pNew = pWith;.
24260 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d }else{. pNew-
24270 3e 61 5b 70 4e 65 77 2d 3e 6e 43 74 65 5d 2e 70 >a[pNew->nCte].p
24280 53 65 6c 65 63 74 20 3d 20 70 51 75 65 72 79 3b Select = pQuery;
24290 0a 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 . pNew->a[pNe
242a0 77 2d 3e 6e 43 74 65 5d 2e 70 43 6f 6c 73 20 3d w->nCte].pCols =
242b0 20 70 41 72 67 6c 69 73 74 3b 0a 20 20 20 20 70 pArglist;. p
242c0 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 43 74 New->a[pNew->nCt
242d0 65 5d 2e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 e].zName = zName
242e0 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e ;. pNew->a[pN
242f0 65 77 2d 3e 6e 43 74 65 5d 2e 7a 43 74 65 45 72 ew->nCte].zCteEr
24300 72 20 3d 20 30 3b 0a 20 20 20 20 70 4e 65 77 2d r = 0;. pNew-
24310 3e 6e 43 74 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 >nCte++;. }..
24320 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a return pNew;.}..
24330 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 63 /*.** Free the c
24340 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 57 ontents of the W
24350 69 74 68 20 6f 62 6a 65 63 74 20 70 61 73 73 65 ith object passe
24360 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
24370 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 argument..*/.voi
24380 64 20 73 71 6c 69 74 65 33 57 69 74 68 44 65 6c d sqlite3WithDel
24390 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ete(sqlite3 *db,
243a0 20 57 69 74 68 20 2a 70 57 69 74 68 29 7b 0a 20 With *pWith){.
243b0 20 69 66 28 20 70 57 69 74 68 20 29 7b 0a 20 20 if( pWith ){.
243c0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
243d0 28 69 3d 30 3b 20 69 3c 70 57 69 74 68 2d 3e 6e (i=0; i<pWith->n
243e0 43 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Cte; i++){.
243f0 20 73 74 72 75 63 74 20 43 74 65 20 2a 70 43 74 struct Cte *pCt
24400 65 20 3d 20 26 70 57 69 74 68 2d 3e 61 5b 69 5d e = &pWith->a[i]
24410 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
24420 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 xprListDelete(db
24430 2c 20 70 43 74 65 2d 3e 70 43 6f 6c 73 29 3b 0a , pCte->pCols);.
24440 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c sqlite3Sel
24450 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 ectDelete(db, pC
24460 74 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 te->pSelect);.
24470 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
24480 65 28 64 62 2c 20 70 43 74 65 2d 3e 7a 4e 61 6d e(db, pCte->zNam
24490 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 e);. }. sq
244a0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
244b0 70 57 69 74 68 29 3b 0a 20 20 7d 0a 7d 0a 23 65 pWith);. }.}.#e
244c0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
244d0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 54 45 (SQLITE_OMIT_CTE
244e0 29 20 2a 2f 0a ) */.