0000: 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75 /*.** 2005 Febru
0010: 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ary 15.**.** The
0020: 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
0030: 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
0040: 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
0050: 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
0060: 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
0070: 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
0080: 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
0090: 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
00a0: 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
00b0: 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
00c0: 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
00d0: 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
00e0: 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
00f0: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
0100: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
0110: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
0120: 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou 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 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
0180: 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f le contains C co
0190: 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 de routines that
01a0: 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 used to generat
01b0: 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74 e VDBE code.** t
01c0: 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 hat implements t
01d0: 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 he ALTER TABLE c
01e0: 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 23 69 6e 63 6c ommand..*/.#incl
01f0: 75 64 65 20 22 73 71 6c 69 74 65 49 6e 74 2e 68 ude "sqliteInt.h
0200: 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 "../*.** The cod
0210: 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f e in this file o
0220: 6e 6c 79 20 65 78 69 73 74 73 20 69 66 20 77 65 nly exists if we
0230: 20 61 72 65 20 6e 6f 74 20 6f 6d 69 74 74 69 6e are not omittin
0240: 67 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 g the.** ALTER T
0250: 41 42 4c 45 20 6c 6f 67 69 63 20 66 72 6f 6d 20 ABLE logic from
0260: 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 the build..*/.#i
0270: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
0280: 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 0a 2f 2a T_ALTERTABLE../*
0290: 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4e .** Parameter zN
02a0: 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ame is the name
02b0: 6f 66 20 61 20 74 61 62 6c 65 20 74 68 61 74 20 of a table that
02c0: 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20 61 is about to be a
02d0: 6c 74 65 72 65 64 0a 2a 2a 20 28 65 69 74 68 65 ltered.** (eithe
02e0: 72 20 77 69 74 68 20 41 4c 54 45 52 20 54 41 42 r with ALTER TAB
02f0: 4c 45 20 2e 2e 2e 20 52 45 4e 41 4d 45 20 54 4f LE ... RENAME TO
0300: 20 6f 72 20 41 4c 54 45 52 20 54 41 42 4c 45 20 or ALTER TABLE
0310: 2e 2e 2e 20 41 44 44 20 43 4f 4c 55 4d 4e 29 2e ... ADD COLUMN).
0320: 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 .** If the table
0330: 20 69 73 20 61 20 73 79 73 74 65 6d 20 74 61 62 is a system tab
0340: 6c 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f le, this functio
0350: 6e 20 6c 65 61 76 65 73 20 61 6e 20 65 72 72 6f n leaves an erro
0360: 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e 20 r message.** in
0370: 70 50 61 72 73 65 2d 3e 7a 45 72 72 20 28 73 79 pParse->zErr (sy
0380: 73 74 65 6d 20 74 61 62 6c 65 73 20 6d 61 79 20 stem tables may
0390: 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 29 20 not be altered)
03a0: 61 6e 64 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d and returns non-
03b0: 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 zero..**.** Or,
03c0: 69 66 20 7a 4e 61 6d 65 20 69 73 20 6e 6f 74 20 if zName is not
03d0: 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 2c 20 a system table,
03e0: 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 zero is returned
03f0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
0400: 69 73 41 6c 74 65 72 61 62 6c 65 54 61 62 6c 65 isAlterableTable
0410: 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
0420: 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 Table *pTab){.
0430: 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 if( 0==sqlite3St
0440: 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 rNICmp(pTab->zNa
0450: 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 me, "sqlite_", 7
0460: 29 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ) .#ifndef SQLIT
0470: 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
0480: 42 4c 45 0a 20 20 20 7c 7c 20 28 20 28 70 54 61 BLE. || ( (pTa
0490: 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 b->tabFlags & TF
04a0: 5f 53 68 61 64 6f 77 29 20 0a 20 20 20 20 20 26 _Shadow) . &
04b0: 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 & (pParse->db->f
04c0: 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 44 65 lags & SQLITE_De
04d0: 66 65 6e 73 69 76 65 29 0a 20 20 20 20 20 26 26 fensive). &&
04e0: 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 56 64 pParse->db->nVd
04f0: 62 65 45 78 65 63 3d 3d 30 0a 20 20 20 29 0a 23 beExec==0. ).#
0500: 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20 73 endif. ){. s
0510: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
0520: 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 Parse, "table %s
0530: 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 may not be alte
0540: 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d red", pTab->zNam
0550: 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 e);. return 1
0560: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
0570: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 ;.}../*.** Gener
0580: 61 74 65 20 63 6f 64 65 20 74 6f 20 76 65 72 69 ate code to veri
0590: 66 79 20 74 68 61 74 20 74 68 65 20 73 63 68 65 fy that the sche
05a0: 6d 61 73 20 6f 66 20 64 61 74 61 62 61 73 65 20 mas of database
05b0: 7a 44 62 20 61 6e 64 2c 20 69 66 0a 2a 2a 20 62 zDb and, if.** b
05c0: 54 65 6d 70 20 69 73 20 6e 6f 74 20 74 72 75 65 Temp is not true
05d0: 2c 20 64 61 74 61 62 61 73 65 20 22 74 65 6d 70 , database "temp
05e0: 22 2c 20 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 ", can still be
05f0: 70 61 72 73 65 64 2e 20 54 68 69 73 20 69 73 0a parsed. This is.
0600: 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 74 68 65 ** called at the
0610: 20 65 6e 64 20 6f 66 20 74 68 65 20 67 65 6e 65 end of the gene
0620: 72 61 74 69 6f 6e 20 6f 66 20 61 6e 20 41 4c 54 ration of an ALT
0630: 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 52 45 4e ER TABLE ... REN
0640: 41 4d 45 20 2e 2e 2e 0a 2a 2a 20 73 74 61 74 65 AME ....** state
0650: 6d 65 6e 74 20 74 6f 20 65 6e 73 75 72 65 20 74 ment to ensure t
0660: 68 61 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f hat the operatio
0670: 6e 20 68 61 73 20 6e 6f 74 20 72 65 6e 64 65 72 n has not render
0680: 65 64 20 61 6e 79 20 73 63 68 65 6d 61 0a 2a 2a ed any schema.**
0690: 20 6f 62 6a 65 63 74 73 20 75 6e 75 73 61 62 6c objects unusabl
06a0: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
06b0: 64 20 72 65 6e 61 6d 65 54 65 73 74 53 63 68 65 d renameTestSche
06c0: 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ma(Parse *pParse
06d0: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 , const char *zD
06e0: 62 2c 20 69 6e 74 20 62 54 65 6d 70 29 7b 0a 20 b, int bTemp){.
06f0: 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 sqlite3NestedPa
0700: 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 rse(pParse, .
0710: 20 20 20 22 53 45 4c 45 43 54 20 31 20 22 0a 20 "SELECT 1 ".
0720: 20 20 20 20 20 22 46 52 4f 4d 20 5c 22 25 77 5c "FROM \"%w\
0730: 22 2e 25 73 20 22 0a 20 20 20 20 20 20 22 57 48 ".%s ". "WH
0740: 45 52 45 20 6e 61 6d 65 20 4e 4f 54 20 4c 49 4b ERE name NOT LIK
0750: 45 20 27 73 71 6c 69 74 65 58 5f 25 25 27 20 45 E 'sqliteX_%%' E
0760: 53 43 41 50 45 20 27 58 27 22 0a 20 20 20 20 20 SCAPE 'X'".
0770: 20 22 20 41 4e 44 20 73 71 6c 20 4e 4f 54 20 4c " AND sql NOT L
0780: 49 4b 45 20 27 63 72 65 61 74 65 20 76 69 72 74 IKE 'create virt
0790: 75 61 6c 25 25 27 22 0a 20 20 20 20 20 20 22 20 ual%%'". "
07a0: 41 4e 44 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d AND sqlite_renam
07b0: 65 5f 74 65 73 74 28 25 51 2c 20 73 71 6c 2c 20 e_test(%Q, sql,
07c0: 74 79 70 65 2c 20 6e 61 6d 65 2c 20 25 64 29 3d type, name, %d)=
07d0: 4e 55 4c 4c 20 22 2c 0a 20 20 20 20 20 20 7a 44 NULL ",. zD
07e0: 62 2c 20 4d 41 53 54 45 52 5f 4e 41 4d 45 2c 20 b, MASTER_NAME,
07f0: 0a 20 20 20 20 20 20 7a 44 62 2c 20 62 54 65 6d . zDb, bTem
0800: 70 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 62 54 p. );.. if( bT
0810: 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 emp==0 ){. sq
0820: 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
0830: 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 (pParse, .
0840: 20 20 22 53 45 4c 45 43 54 20 31 20 22 0a 20 20 "SELECT 1 ".
0850: 20 20 20 20 20 20 22 46 52 4f 4d 20 74 65 6d 70 "FROM temp
0860: 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 22 57 .%s ". "W
0870: 48 45 52 45 20 6e 61 6d 65 20 4e 4f 54 20 4c 49 HERE name NOT LI
0880: 4b 45 20 27 73 71 6c 69 74 65 58 5f 25 25 27 20 KE 'sqliteX_%%'
0890: 45 53 43 41 50 45 20 27 58 27 22 0a 20 20 20 20 ESCAPE 'X'".
08a0: 20 20 20 20 22 20 41 4e 44 20 73 71 6c 20 4e 4f " AND sql NO
08b0: 54 20 4c 49 4b 45 20 27 63 72 65 61 74 65 20 76 T LIKE 'create v
08c0: 69 72 74 75 61 6c 25 25 27 22 0a 20 20 20 20 20 irtual%%'".
08d0: 20 20 20 22 20 41 4e 44 20 73 71 6c 69 74 65 5f " AND sqlite_
08e0: 72 65 6e 61 6d 65 5f 74 65 73 74 28 25 51 2c 20 rename_test(%Q,
08f0: 73 71 6c 2c 20 74 79 70 65 2c 20 6e 61 6d 65 2c sql, type, name,
0900: 20 31 29 3d 4e 55 4c 4c 20 22 2c 0a 20 20 20 20 1)=NULL ",.
0910: 20 20 20 20 4d 41 53 54 45 52 5f 4e 41 4d 45 2c MASTER_NAME,
0920: 20 7a 44 62 20 0a 20 20 20 20 29 3b 0a 20 20 7d zDb . );. }
0930: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
0940: 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6c 6f 61 te code to reloa
0950: 64 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 d the schema for
0960: 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 41 database iDb. A
0970: 6e 64 2c 20 69 66 20 69 44 62 21 3d 31 2c 20 66 nd, if iDb!=1, f
0980: 6f 72 0a 2a 2a 20 74 68 65 20 74 65 6d 70 20 64 or.** the temp d
0990: 61 74 61 62 61 73 65 20 61 73 20 77 65 6c 6c 2e atabase as well.
09a0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
09b0: 72 65 6e 61 6d 65 52 65 6c 6f 61 64 53 63 68 65 renameReloadSche
09c0: 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ma(Parse *pParse
09d0: 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 , int iDb){. Vd
09e0: 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e be *v = pParse->
09f0: 70 56 64 62 65 3b 0a 20 20 69 66 28 20 76 20 29 pVdbe;. if( v )
0a00: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 {. sqlite3Cha
0a10: 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 ngeCookie(pParse
0a20: 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 , iDb);. sqli
0a30: 74 65 33 56 64 62 65 41 64 64 50 61 72 73 65 53 te3VdbeAddParseS
0a40: 63 68 65 6d 61 4f 70 28 70 50 61 72 73 65 2d 3e chemaOp(pParse->
0a50: 70 56 64 62 65 2c 20 69 44 62 2c 20 30 29 3b 0a pVdbe, iDb, 0);.
0a60: 20 20 20 20 69 66 28 20 69 44 62 21 3d 31 20 29 if( iDb!=1 )
0a70: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 50 sqlite3VdbeAddP
0a80: 61 72 73 65 53 63 68 65 6d 61 4f 70 28 70 50 61 arseSchemaOp(pPa
0a90: 72 73 65 2d 3e 70 56 64 62 65 2c 20 31 2c 20 30 rse->pVdbe, 1, 0
0aa0: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
0ab0: 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
0ac0: 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 22 implement the "
0ad0: 41 4c 54 45 52 20 54 41 42 4c 45 20 78 78 78 20 ALTER TABLE xxx
0ae0: 52 45 4e 41 4d 45 20 54 4f 20 79 79 79 22 20 0a RENAME TO yyy" .
0af0: 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a ** command. .*/.
0b00: 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 void sqlite3Alte
0b10: 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20 rRenameTable(.
0b20: 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
0b30: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
0b40: 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a ser context. */.
0b50: 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c SrcList *pSrc,
0b60: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
0b70: 68 65 20 74 61 62 6c 65 20 74 6f 20 72 65 6e 61 he table to rena
0b80: 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a me. */. Token *
0b90: 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 pName
0ba0: 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 /* The new ta
0bb0: 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a ble name. */.){.
0bc0: 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
0bd0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
0be0: 61 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e atabase that con
0bf0: 74 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 20 tains the table
0c00: 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 */. char *zDb;
0c10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
0c20: 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 * Name of databa
0c30: 73 65 20 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c se iDb */. Tabl
0c40: 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 e *pTab;
0c50: 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 /* Table b
0c60: 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a eing renamed */.
0c70: 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 char *zName =
0c80: 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 0; /* N
0c90: 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20 76 ULL-terminated v
0ca0: 65 72 73 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20 ersion of pName
0cb0: 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */ . sqlite3 *d
0cc0: 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 b = pParse->db;
0cd0: 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e /* Database conn
0ce0: 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ection */. int
0cf0: 6e 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 20 20 nTabName;
0d00: 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
0d10: 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 of UTF-8 charact
0d20: 65 72 73 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20 ers in zTabName
0d30: 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
0d40: 2a 7a 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f *zTabName; /
0d50: 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20 * Original name
0d60: 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a of the table */.
0d70: 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 56 54 61 Vdbe *v;. VTa
0d80: 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 30 3b 20 ble *pVTab = 0;
0d90: 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 /* Non-ze
0da0: 72 6f 20 69 66 20 74 68 69 73 20 69 73 20 61 20 ro if this is a
0db0: 76 2d 74 61 62 20 77 69 74 68 20 61 6e 20 78 52 v-tab with an xR
0dc0: 65 6e 61 6d 65 28 29 20 2a 2f 0a 20 20 75 33 32 ename() */. u32
0dd0: 20 73 61 76 65 64 44 62 46 6c 61 67 73 3b 20 20 savedDbFlags;
0de0: 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 /* Saved
0df0: 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6d 44 62 value of db->mDb
0e00: 46 6c 61 67 73 20 2a 2f 0a 0a 20 20 73 61 76 65 Flags */.. save
0e10: 64 44 62 46 6c 61 67 73 20 3d 20 64 62 2d 3e 6d dDbFlags = db->m
0e20: 44 62 46 6c 61 67 73 3b 20 20 0a 20 20 69 66 28 DbFlags; . if(
0e30: 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61 6c 6c 6f NEVER(db->mallo
0e40: 63 46 61 69 6c 65 64 29 20 29 20 67 6f 74 6f 20 cFailed) ) goto
0e50: 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c exit_rename_tabl
0e60: 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 e;. assert( pSr
0e70: 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 c->nSrc==1 );.
0e80: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
0e90: 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
0ea0: 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 xes(pParse->db)
0eb0: 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71 6c );.. pTab = sql
0ec0: 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 49 ite3LocateTableI
0ed0: 74 65 6d 28 70 50 61 72 73 65 2c 20 30 2c 20 26 tem(pParse, 0, &
0ee0: 70 53 72 63 2d 3e 61 5b 30 5d 29 3b 0a 20 20 69 pSrc->a[0]);. i
0ef0: 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 f( !pTab ) goto
0f00: 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c exit_rename_tabl
0f10: 65 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 e;. iDb = sqlit
0f20: 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
0f30: 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 pParse->db, pTab
0f40: 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 ->pSchema);. zD
0f50: 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d b = db->aDb[iDb]
0f60: 2e 7a 44 62 53 4e 61 6d 65 3b 0a 20 20 64 62 2d .zDbSName;. db-
0f70: 3e 6d 44 62 46 6c 61 67 73 20 7c 3d 20 44 42 46 >mDbFlags |= DBF
0f80: 4c 41 47 5f 50 72 65 66 65 72 42 75 69 6c 74 69 LAG_PreferBuilti
0f90: 6e 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e n;.. /* Get a N
0fa0: 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64 20 76 ULL terminated v
0fb0: 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 ersion of the ne
0fc0: 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f w table name. */
0fd0: 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 . zName = sqlit
0fe0: 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
0ff0: 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 db, pName);. if
1000: 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 ( !zName ) goto
1010: 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c exit_rename_tabl
1020: 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 e;.. /* Check t
1030: 68 61 74 20 61 20 74 61 62 6c 65 20 6f 72 20 69 hat a table or i
1040: 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d ndex named 'zNam
1050: 65 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 e' does not alre
1060: 61 64 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 ady exist. ** i
1070: 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 n database iDb.
1080: 49 66 20 73 6f 2c 20 74 68 69 73 20 69 73 20 61 If so, this is a
1090: 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 n error.. */.
10a0: 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 if( sqlite3FindT
10b0: 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 able(db, zName,
10c0: 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 zDb) || sqlite3F
10d0: 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 indIndex(db, zNa
10e0: 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 me, zDb) ){.
10f0: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1100: 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
1110: 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 "there is alrea
1120: 64 79 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 dy another table
1130: 20 6f 72 20 69 6e 64 65 78 20 77 69 74 68 20 74 or index with t
1140: 68 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a his name: %s", z
1150: 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Name);. goto
1160: 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c exit_rename_tabl
1170: 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b e;. }.. /* Mak
1180: 65 20 73 75 72 65 20 69 74 20 69 73 20 6e 6f 74 e sure it is not
1190: 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 a system table
11a0: 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f being altered, o
11b0: 72 20 61 20 72 65 73 65 72 76 65 64 20 6e 61 6d r a reserved nam
11c0: 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 e. ** that the
11d0: 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 72 table is being r
11e0: 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a enamed to.. */.
11f0: 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
1200: 3d 69 73 41 6c 74 65 72 61 62 6c 65 54 61 62 6c =isAlterableTabl
1210: 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 e(pParse, pTab)
1220: 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 ){. goto exit
1230: 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 _rename_table;.
1240: 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f }. if( SQLITE_
1250: 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b OK!=sqlite3Check
1260: 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 ObjectName(pPars
1270: 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 20 67 6f 74 e, zName) ){ got
1280: 6f 0a 20 20 20 20 65 78 69 74 5f 72 65 6e 61 6d o. exit_renam
1290: 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 e_table;. }..#i
12a0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
12b0: 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70 54 61 T_VIEW. if( pTa
12c0: 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 b->pSelect ){.
12d0: 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
12e0: 67 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 g(pParse, "view
12f0: 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c %s may not be al
1300: 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e tered", pTab->zN
1310: 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ame);. goto e
1320: 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
1330: 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ;. }.#endif..#i
1340: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1350: 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a T_AUTHORIZATION.
1360: 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 /* Invoke the
1370: 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 authorization ca
1380: 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 llback. */. if(
1390: 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
13a0: 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 k(pParse, SQLITE
13b0: 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44 _ALTER_TABLE, zD
13c0: 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 b, pTab->zName,
13d0: 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 0) ){. goto e
13e0: 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
13f0: 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ;. }.#endif..#i
1400: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1410: 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
1420: 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 if( sqlite3View
1430: 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 GetColumnNames(p
1440: 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a Parse, pTab) ){.
1450: 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 goto exit_re
1460: 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a name_table;. }.
1470: 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 if( IsVirtual(
1480: 70 54 61 62 29 20 29 7b 0a 20 20 20 20 70 56 54 pTab) ){. pVT
1490: 61 62 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 ab = sqlite3GetV
14a0: 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b Table(db, pTab);
14b0: 0a 20 20 20 20 69 66 28 20 70 56 54 61 62 2d 3e . if( pVTab->
14c0: 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e pVtab->pModule->
14d0: 78 52 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 xRename==0 ){.
14e0: 20 20 20 20 70 56 54 61 62 20 3d 20 30 3b 0a 20 pVTab = 0;.
14f0: 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
1500: 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 . /* Begin a tr
1510: 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 64 61 ansaction for da
1520: 74 61 62 61 73 65 20 69 44 62 2e 20 54 68 65 6e tabase iDb. Then
1530: 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63 68 65 modify the sche
1540: 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 2a 2a 20 28 ma cookie. ** (
1550: 73 69 6e 63 65 20 74 68 65 20 41 4c 54 45 52 20 since the ALTER
1560: 54 41 42 4c 45 20 6d 6f 64 69 66 69 65 73 20 74 TABLE modifies t
1570: 68 65 20 73 63 68 65 6d 61 29 2e 20 43 61 6c 6c he schema). Call
1580: 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 sqlite3MayAbort
1590: 28 29 2c 0a 20 20 2a 2a 20 61 73 20 74 68 65 20 (),. ** as the
15a0: 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 73 scalar functions
15b0: 20 28 65 2e 67 2e 20 73 71 6c 69 74 65 5f 72 65 (e.g. sqlite_re
15c0: 6e 61 6d 65 5f 74 61 62 6c 65 28 29 29 20 69 6e name_table()) in
15d0: 76 6f 6b 65 64 20 62 79 20 74 68 65 20 0a 20 20 voked by the .
15e0: 2a 2a 20 6e 65 73 74 65 64 20 53 51 4c 20 6d 61 ** nested SQL ma
15f0: 79 20 72 61 69 73 65 20 61 6e 20 65 78 63 65 70 y raise an excep
1600: 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 76 20 3d 20 tion. */. v =
1610: 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
1620: 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d Parse);. if( v=
1630: 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 =0 ){. goto e
1640: 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
1650: 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d ;. }. sqlite3M
1660: 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b ayAbort(pParse);
1670: 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f 75 .. /* figure ou
1680: 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d 38 t how many UTF-8
1690: 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 characters are
16a0: 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54 in zName */. zT
16b0: 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a abName = pTab->z
16c0: 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d 65 Name;. nTabName
16d0: 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 = sqlite3Utf8Ch
16e0: 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c 20 arLen(zTabName,
16f0: 2d 31 29 3b 0a 0a 20 20 2f 2a 20 52 65 77 72 69 -1);.. /* Rewri
1700: 74 65 20 61 6c 6c 20 43 52 45 41 54 45 20 54 41 te all CREATE TA
1710: 42 4c 45 2c 20 49 4e 44 45 58 2c 20 54 52 49 47 BLE, INDEX, TRIG
1720: 47 45 52 20 6f 72 20 56 49 45 57 20 73 74 61 74 GER or VIEW stat
1730: 65 6d 65 6e 74 73 20 69 6e 0a 20 20 2a 2a 20 74 ements in. ** t
1740: 68 65 20 73 63 68 65 6d 61 20 74 6f 20 75 73 65 he schema to use
1750: 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e the new table n
1760: 61 6d 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ame. */. sqlit
1770: 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
1780: 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 55 50 arse, . "UP
1790: 44 41 54 45 20 5c 22 25 77 5c 22 2e 25 73 20 53 DATE \"%w\".%s S
17a0: 45 54 20 22 0a 20 20 20 20 20 20 22 73 71 6c 20 ET ". "sql
17b0: 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f = sqlite_rename_
17c0: 74 61 62 6c 65 28 25 51 2c 20 74 79 70 65 2c 20 table(%Q, type,
17d0: 6e 61 6d 65 2c 20 73 71 6c 2c 20 25 51 2c 20 25 name, sql, %Q, %
17e0: 51 2c 20 25 64 29 20 22 0a 20 20 20 20 20 20 22 Q, %d) ". "
17f0: 57 48 45 52 45 20 28 74 79 70 65 21 3d 27 69 6e WHERE (type!='in
1800: 64 65 78 27 20 4f 52 20 74 62 6c 5f 6e 61 6d 65 dex' OR tbl_name
1810: 3d 25 51 20 43 4f 4c 4c 41 54 45 20 6e 6f 63 61 =%Q COLLATE noca
1820: 73 65 29 22 0a 20 20 20 20 20 20 22 41 4e 44 20 se)". "AND
1830: 20 20 6e 61 6d 65 20 4e 4f 54 20 4c 49 4b 45 20 name NOT LIKE
1840: 27 73 71 6c 69 74 65 58 5f 25 25 27 20 45 53 43 'sqliteX_%%' ESC
1850: 41 50 45 20 27 58 27 22 0a 20 20 20 20 20 20 2c APE 'X'". ,
1860: 20 7a 44 62 2c 20 4d 41 53 54 45 52 5f 4e 41 4d zDb, MASTER_NAM
1870: 45 2c 20 7a 44 62 2c 20 7a 54 61 62 4e 61 6d 65 E, zDb, zTabName
1880: 2c 20 7a 4e 61 6d 65 2c 20 28 69 44 62 3d 3d 31 , zName, (iDb==1
1890: 29 2c 20 7a 54 61 62 4e 61 6d 65 0a 20 20 29 3b ), zTabName. );
18a0: 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 .. /* Update th
18b0: 65 20 74 62 6c 5f 6e 61 6d 65 20 61 6e 64 20 6e e tbl_name and n
18c0: 61 6d 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 ame columns of t
18d0: 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 he sqlite_master
18e0: 20 74 61 62 6c 65 0a 20 20 2a 2a 20 61 73 20 72 table. ** as r
18f0: 65 71 75 69 72 65 64 2e 20 20 2a 2f 0a 20 20 73 equired. */. s
1900: 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 qlite3NestedPars
1910: 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 e(pParse,.
1920: 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 "UPDATE %Q.%s SE
1930: 54 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 74 T ". "t
1940: 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 2c 20 22 0a bl_name = %Q, ".
1950: 20 20 20 20 20 20 20 20 20 20 22 6e 61 6d 65 20 "name
1960: 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20 = CASE ".
1970: 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 3d "WHEN type=
1980: 27 74 61 62 6c 65 27 20 54 48 45 4e 20 25 51 20 'table' THEN %Q
1990: 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 ". "W
19a0: 48 45 4e 20 6e 61 6d 65 20 4c 49 4b 45 20 27 73 HEN name LIKE 's
19b0: 71 6c 69 74 65 58 5f 61 75 74 6f 69 6e 64 65 78 qliteX_autoindex
19c0: 25 25 27 20 45 53 43 41 50 45 20 27 58 27 20 22 %%' ESCAPE 'X' "
19d0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20 . "
19e0: 20 20 20 41 4e 44 20 74 79 70 65 3d 27 69 6e 64 AND type='ind
19f0: 65 78 27 20 54 48 45 4e 20 22 0a 20 20 20 20 20 ex' THEN ".
1a00: 20 20 20 20 20 20 20 20 22 27 73 71 6c 69 74 65 "'sqlite
1a10: 5f 61 75 74 6f 69 6e 64 65 78 5f 27 20 7c 7c 20 _autoindex_' ||
1a20: 25 51 20 7c 7c 20 73 75 62 73 74 72 28 6e 61 6d %Q || substr(nam
1a30: 65 2c 25 64 2b 31 38 29 20 22 0a 20 20 20 20 20 e,%d+18) ".
1a40: 20 20 20 20 20 20 20 22 45 4c 53 45 20 6e 61 6d "ELSE nam
1a50: 65 20 45 4e 44 20 22 0a 20 20 20 20 20 20 22 57 e END ". "W
1a60: 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 HERE tbl_name=%Q
1a70: 20 43 4f 4c 4c 41 54 45 20 6e 6f 63 61 73 65 20 COLLATE nocase
1a80: 41 4e 44 20 22 0a 20 20 20 20 20 20 20 20 20 20 AND ".
1a90: 22 28 74 79 70 65 3d 27 74 61 62 6c 65 27 20 4f "(type='table' O
1aa0: 52 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 4f R type='index' O
1ab0: 52 20 74 79 70 65 3d 27 74 72 69 67 67 65 72 27 R type='trigger'
1ac0: 29 3b 22 2c 20 0a 20 20 20 20 20 20 7a 44 62 2c );", . zDb,
1ad0: 20 4d 41 53 54 45 52 5f 4e 41 4d 45 2c 20 0a 20 MASTER_NAME, .
1ae0: 20 20 20 20 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d zName, zNam
1af0: 65 2c 20 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 e, zName, .
1b00: 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e nTabName, zTabN
1b10: 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65 ame. );..#ifnde
1b20: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1b30: 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a TOINCREMENT. /*
1b40: 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 If the sqlite_s
1b50: 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65 78 equence table ex
1b60: 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 61 74 ists in this dat
1b70: 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64 61 abase, then upda
1b80: 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74 68 te . ** it with
1b90: 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e the new table n
1ba0: 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ame.. */. if(
1bb0: 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 sqlite3FindTable
1bc0: 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 (db, "sqlite_seq
1bd0: 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a uence", zDb) ){.
1be0: 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 sqlite3Neste
1bf0: 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 dParse(pParse,.
1c00: 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c "UPDATE \
1c10: 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65 71 "%w\".sqlite_seq
1c20: 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20 3d uence set name =
1c30: 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20 3d %Q WHERE name =
1c40: 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a 44 %Q",. zD
1c50: 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e b, zName, pTab->
1c60: 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 zName);. }.#end
1c70: 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 if.. /* If the
1c80: 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 table being rena
1c90: 6d 65 64 20 69 73 20 6e 6f 74 20 69 74 73 65 6c med is not itsel
1ca0: 66 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 65 f part of the te
1cb0: 6d 70 20 64 61 74 61 62 61 73 65 2c 0a 20 20 2a mp database,. *
1cc0: 2a 20 65 64 69 74 20 76 69 65 77 20 61 6e 64 20 * edit view and
1cd0: 74 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 trigger definiti
1ce0: 6f 6e 73 20 77 69 74 68 69 6e 20 74 68 65 20 74 ons within the t
1cf0: 65 6d 70 20 64 61 74 61 62 61 73 65 20 0a 20 20 emp database .
1d00: 2a 2a 20 61 73 20 72 65 71 75 69 72 65 64 2e 20 ** as required.
1d10: 20 2a 2f 0a 20 20 69 66 28 20 69 44 62 21 3d 31 */. if( iDb!=1
1d20: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e ){. sqlite3N
1d30: 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 estedParse(pPars
1d40: 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 e, . "UPD
1d50: 41 54 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f ATE sqlite_temp_
1d60: 6d 61 73 74 65 72 20 53 45 54 20 22 0a 20 20 20 master SET ".
1d70: 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 "sql =
1d80: 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 sqlite_rename_ta
1d90: 62 6c 65 28 25 51 2c 20 74 79 70 65 2c 20 6e 61 ble(%Q, type, na
1da0: 6d 65 2c 20 73 71 6c 2c 20 25 51 2c 20 25 51 2c me, sql, %Q, %Q,
1db0: 20 31 29 2c 20 22 0a 20 20 20 20 20 20 20 20 20 1), ".
1dc0: 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 22 "tbl_name = "
1dd0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 . "
1de0: 43 41 53 45 20 57 48 45 4e 20 74 62 6c 5f 6e 61 CASE WHEN tbl_na
1df0: 6d 65 3d 25 51 20 43 4f 4c 4c 41 54 45 20 6e 6f me=%Q COLLATE no
1e00: 63 61 73 65 20 41 4e 44 20 22 0a 20 20 20 20 20 case AND ".
1e10: 20 20 20 20 20 20 20 20 20 22 20 20 20 20 20 20 "
1e20: 20 20 20 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d sqlite_renam
1e30: 65 5f 74 65 73 74 28 25 51 2c 20 73 71 6c 2c 20 e_test(%Q, sql,
1e40: 74 79 70 65 2c 20 6e 61 6d 65 2c 20 31 29 20 22 type, name, 1) "
1e50: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 . "
1e60: 54 48 45 4e 20 25 51 20 45 4c 53 45 20 74 62 6c THEN %Q ELSE tbl
1e70: 5f 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20 20 20 _name END ".
1e80: 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20 74 "WHERE t
1e90: 79 70 65 20 49 4e 20 28 27 76 69 65 77 27 2c 20 ype IN ('view',
1ea0: 27 74 72 69 67 67 65 72 27 29 22 0a 20 20 20 20 'trigger')".
1eb0: 20 20 20 20 2c 20 7a 44 62 2c 20 7a 54 61 62 4e , zDb, zTabN
1ec0: 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 54 61 62 ame, zName, zTab
1ed0: 4e 61 6d 65 2c 20 7a 44 62 2c 20 7a 4e 61 6d 65 Name, zDb, zName
1ee0: 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
1ef0: 74 68 69 73 20 69 73 20 61 20 76 69 72 74 75 61 this is a virtua
1f00: 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b 65 20 l table, invoke
1f10: 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20 66 75 the xRename() fu
1f20: 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a 20 6f nction if. ** o
1f30: 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e 20 54 ne is defined. T
1f40: 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63 61 6c he xRename() cal
1f50: 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64 69 66 lback will modif
1f60: 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a y the names. **
1f70: 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72 63 65 of any resource
1f80: 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 2d s used by the v-
1f90: 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 table implementa
1fa0: 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 tion (including
1fb0: 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c 69 74 other. ** SQLit
1fc0: 65 20 74 61 62 6c 65 73 29 20 74 68 61 74 20 61 e tables) that a
1fd0: 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 re identified by
1fe0: 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1ff0: 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a virtual table..
2000: 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
2010: 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
2020: 54 41 42 4c 45 0a 20 20 69 66 28 20 70 56 54 61 TABLE. if( pVTa
2030: 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d b ){. int i =
2040: 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
2050: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
2060: 4c 6f 61 64 53 74 72 69 6e 67 28 76 2c 20 69 2c LoadString(v, i,
2070: 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c zName);. sql
2080: 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
2090: 2c 20 4f 50 5f 56 52 65 6e 61 6d 65 2c 20 69 2c , OP_VRename, i,
20a0: 20 30 2c 20 30 2c 28 63 6f 6e 73 74 20 63 68 61 0, 0,(const cha
20b0: 72 2a 29 70 56 54 61 62 2c 20 50 34 5f 56 54 41 r*)pVTab, P4_VTA
20c0: 42 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a B);. }.#endif..
20d0: 20 20 72 65 6e 61 6d 65 52 65 6c 6f 61 64 53 63 renameReloadSc
20e0: 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 hema(pParse, iDb
20f0: 29 3b 0a 20 20 72 65 6e 61 6d 65 54 65 73 74 53 );. renameTestS
2100: 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 7a 44 chema(pParse, zD
2110: 62 2c 20 69 44 62 3d 3d 31 29 3b 0a 0a 65 78 69 b, iDb==1);..exi
2120: 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a 0a t_rename_table:.
2130: 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 sqlite3SrcList
2140: 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 Delete(db, pSrc)
2150: 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
2160: 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 e(db, zName);.
2170: 64 62 2d 3e 6d 44 62 46 6c 61 67 73 20 3d 20 73 db->mDbFlags = s
2180: 61 76 65 64 44 62 46 6c 61 67 73 3b 0a 7d 0a 0a avedDbFlags;.}..
2190: 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
21a0: 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 ion is called af
21b0: 74 65 72 20 61 6e 20 22 41 4c 54 45 52 20 54 41 ter an "ALTER TA
21c0: 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 61 BLE ... ADD" sta
21d0: 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65 tement.** has be
21e0: 65 6e 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d en parsed. Argum
21f0: 65 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f 6e 74 ent pColDef cont
2200: 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66 ains the text of
2210: 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 the new.** colu
2220: 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a mn definition..*
2230: 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 *.** The Table s
2240: 74 72 75 63 74 75 72 65 20 70 50 61 72 73 65 2d tructure pParse-
2250: 3e 70 4e 65 77 54 61 62 6c 65 20 77 61 73 20 65 >pNewTable was e
2260: 78 74 65 6e 64 65 64 20 74 6f 20 69 6e 63 6c 75 xtended to inclu
2270: 64 65 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 6f de.** the new co
2280: 6c 75 6d 6e 20 64 75 72 69 6e 67 20 70 61 72 73 lumn during pars
2290: 69 6e 67 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ing..*/.void sql
22a0: 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 ite3AlterFinishA
22b0: 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a ddColumn(Parse *
22c0: 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 pParse, Token *p
22d0: 43 6f 6c 44 65 66 29 7b 0a 20 20 54 61 62 6c 65 ColDef){. Table
22e0: 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 *pNew;
22f0: 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 /* Copy of
2300: 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
2310: 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 e */. Table *pT
2320: 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ab;
2330: 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 /* Table being
2340: 61 6c 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 altered */. int
2350: 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 iDb;
2360: 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
2370: 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 se number */. c
2380: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 onst char *zDb;
2390: 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
23a0: 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 base name */. c
23b0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 3b onst char *zTab;
23c0: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c /* Tabl
23d0: 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 e name */. char
23e0: 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 *zCol;
23f0: 20 20 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d 74 65 /* Null-te
2400: 72 6d 69 6e 61 74 65 64 20 63 6f 6c 75 6d 6e 20 rminated column
2410: 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 definition */.
2420: 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20 20 Column *pCol;
2430: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
2440: 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 new column */.
2450: 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 Expr *pDflt;
2460: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
2470: 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 fault value for
2480: 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a the new column *
2490: 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b /. sqlite3 *db;
24a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
24b0: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
24c0: 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a 20 20 56 nnection; */. V
24d0: 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 dbe *v;
24e0: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
24f0: 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2500: 6e 74 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 nt under constru
2510: 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 ction */. int r
2520: 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
2530: 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 /* Temporar
2540: 79 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 0a y registers */..
2550: 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
2560: 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d b;. if( pParse-
2570: 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c >nErr || db->mal
2580: 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 locFailed ) retu
2590: 72 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70 50 61 rn;. pNew = pPa
25a0: 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
25b0: 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 20 29 assert( pNew )
25c0: 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
25d0: 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c ite3BtreeHoldsAl
25e0: 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a lMutexes(db) );.
25f0: 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
2600: 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
2610: 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 29 3b pNew->pSchema);
2620: 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 . zDb = db->aDb
2630: 5b 69 44 62 5d 2e 7a 44 62 53 4e 61 6d 65 3b 0a [iDb].zDbSName;.
2640: 20 20 7a 54 61 62 20 3d 20 26 70 4e 65 77 2d 3e zTab = &pNew->
2650: 7a 4e 61 6d 65 5b 31 36 5d 3b 20 20 2f 2a 20 53 zName[16]; /* S
2660: 6b 69 70 20 74 68 65 20 22 73 71 6c 69 74 65 5f kip the "sqlite_
2670: 61 6c 74 65 72 74 61 62 5f 22 20 70 72 65 66 69 altertab_" prefi
2680: 78 20 6f 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f x on the name */
2690: 0a 20 20 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d . pCol = &pNew-
26a0: 3e 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43 6f 6c >aCol[pNew->nCol
26b0: 2d 31 5d 3b 0a 20 20 70 44 66 6c 74 20 3d 20 70 -1];. pDflt = p
26c0: 43 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20 70 54 Col->pDflt;. pT
26d0: 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 ab = sqlite3Find
26e0: 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 Table(db, zTab,
26f0: 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 zDb);. assert(
2700: 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64 65 66 pTab );..#ifndef
2710: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
2720: 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 HORIZATION. /*
2730: 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f Invoke the autho
2740: 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 rization callbac
2750: 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 k. */. if( sqli
2760: 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
2770: 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 rse, SQLITE_ALTE
2780: 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 R_TABLE, zDb, pT
2790: 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b ab->zName, 0) ){
27a0: 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
27b0: 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 .#endif.. /* If
27c0: 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c the default val
27d0: 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 63 ue for the new c
27e0: 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63 69 66 olumn was specif
27f0: 69 65 64 20 77 69 74 68 20 61 20 0a 20 20 2a 2a ied with a . **
2800: 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c 20 74 literal NULL, t
2810: 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20 74 6f hen set pDflt to
2820: 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c 69 66 0. This simplif
2830: 69 65 73 20 63 68 65 63 6b 69 6e 67 0a 20 20 2a ies checking. *
2840: 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e 55 4c * for an SQL NUL
2850: 4c 20 64 65 66 61 75 6c 74 20 62 65 6c 6f 77 2e L default below.
2860: 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
2870: 70 44 66 6c 74 3d 3d 30 20 7c 7c 20 70 44 66 6c pDflt==0 || pDfl
2880: 74 2d 3e 6f 70 3d 3d 54 4b 5f 53 50 41 4e 20 29 t->op==TK_SPAN )
2890: 3b 0a 20 20 69 66 28 20 70 44 66 6c 74 20 26 26 ;. if( pDflt &&
28a0: 20 70 44 66 6c 74 2d 3e 70 4c 65 66 74 2d 3e 6f pDflt->pLeft->o
28b0: 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 p==TK_NULL ){.
28c0: 20 20 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d pDflt = 0;. }
28d0: 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
28e0: 74 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e t the new column
28f0: 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 is not specifie
2900: 64 20 61 73 20 50 52 49 4d 41 52 59 20 4b 45 59 d as PRIMARY KEY
2910: 20 6f 72 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a or UNIQUE.. **
2920: 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 4e If there is a N
2930: 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 OT NULL constrai
2940: 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 64 65 66 nt, then the def
2950: 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 ault value for t
2960: 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d he. ** column m
2970: 75 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e ust not be NULL.
2980: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c . */. if( pCol
2990: 2d 3e 63 6f 6c 46 6c 61 67 73 20 26 20 43 4f 4c ->colFlags & COL
29a0: 46 4c 41 47 5f 50 52 49 4d 4b 45 59 20 29 7b 0a FLAG_PRIMKEY ){.
29b0: 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
29c0: 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e Msg(pParse, "Can
29d0: 6e 6f 74 20 61 64 64 20 61 20 50 52 49 4d 41 52 not add a PRIMAR
29e0: 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a Y KEY column");.
29f0: 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
2a00: 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 if( pNew->pInd
2a10: 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ex ){. sqlite
2a20: 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
2a30: 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 , "Cannot add a
2a40: 55 4e 49 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b UNIQUE column");
2a50: 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
2a60: 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 . if( (db->flag
2a70: 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e s&SQLITE_Foreign
2a80: 4b 65 79 73 29 20 26 26 20 70 4e 65 77 2d 3e 70 Keys) && pNew->p
2a90: 46 4b 65 79 20 26 26 20 70 44 66 6c 74 20 29 7b FKey && pDflt ){
2aa0: 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
2ab0: 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 rMsg(pParse, .
2ac0: 20 20 20 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 "Cannot ad
2ad0: 64 20 61 20 52 45 46 45 52 45 4e 43 45 53 20 63 d a REFERENCES c
2ae0: 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 4e olumn with non-N
2af0: 55 4c 4c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 ULL default valu
2b00: 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b e");. return;
2b10: 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 2d . }. if( pCol-
2b20: 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44 66 >notNull && !pDf
2b30: 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 lt ){. sqlite
2b40: 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
2b50: 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61 6e 6e , . "Cann
2b60: 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e 55 4c ot add a NOT NUL
2b70: 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64 65 L column with de
2b80: 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c 4c fault value NULL
2b90: 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ");. return;.
2ba0: 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 }.. /* Ensure
2bb0: 20 74 68 65 20 64 65 66 61 75 6c 74 20 65 78 70 the default exp
2bc0: 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65 74 ression is somet
2bd0: 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69 74 65 hing that sqlite
2be0: 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29 3ValueFromExpr()
2bf0: 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c 65 . ** can handle
2c00: 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52 45 (i.e. not CURRE
2c10: 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20 20 NT_TIME etc.).
2c20: 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20 29 */. if( pDflt )
2c30: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 {. sqlite3_va
2c40: 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 20 lue *pVal = 0;.
2c50: 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 int rc;. r
2c60: 63 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 c = sqlite3Value
2c70: 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 66 FromExpr(db, pDf
2c80: 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c lt, SQLITE_UTF8,
2c90: 20 53 51 4c 49 54 45 5f 41 46 46 5f 42 4c 4f 42 SQLITE_AFF_BLOB
2ca0: 2c 20 26 70 56 61 6c 29 3b 0a 20 20 20 20 61 73 , &pVal);. as
2cb0: 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
2cc0: 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 _OK || rc==SQLIT
2cd0: 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 69 E_NOMEM );. i
2ce0: 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
2cf0: 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
2d00: 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
2d10: 65 64 20 3d 3d 20 31 20 29 3b 0a 20 20 20 20 20 ed == 1 );.
2d20: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
2d30: 20 20 20 69 66 28 20 21 70 56 61 6c 20 29 7b 0a if( !pVal ){.
2d40: 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
2d50: 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 orMsg(pParse, "C
2d60: 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 annot add a colu
2d70: 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 mn with non-cons
2d80: 74 61 6e 74 20 64 65 66 61 75 6c 74 22 29 3b 0a tant default");.
2d90: 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
2da0: 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
2db0: 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a alueFree(pVal);.
2dc0: 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 }.. /* Modify
2dd0: 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c the CREATE TABL
2de0: 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a E statement. */.
2df0: 20 20 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 zCol = sqlite3
2e00: 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 DbStrNDup(db, (c
2e10: 68 61 72 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c har*)pColDef->z,
2e20: 20 70 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 pColDef->n);.
2e30: 69 66 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 if( zCol ){.
2e40: 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 char *zEnd = &zC
2e50: 6f 6c 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d ol[pColDef->n-1]
2e60: 3b 0a 20 20 20 20 75 33 32 20 73 61 76 65 64 44 ;. u32 savedD
2e70: 62 46 6c 61 67 73 20 3d 20 64 62 2d 3e 6d 44 62 bFlags = db->mDb
2e80: 46 6c 61 67 73 3b 0a 20 20 20 20 77 68 69 6c 65 Flags;. while
2e90: 28 20 7a 45 6e 64 3e 7a 43 6f 6c 20 26 26 20 28 ( zEnd>zCol && (
2ea0: 2a 7a 45 6e 64 3d 3d 27 3b 27 20 7c 7c 20 73 71 *zEnd==';' || sq
2eb0: 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 45 lite3Isspace(*zE
2ec0: 6e 64 29 29 20 29 7b 0a 20 20 20 20 20 20 2a 7a nd)) ){. *z
2ed0: 45 6e 64 2d 2d 20 3d 20 27 5c 30 27 3b 0a 20 20 End-- = '\0';.
2ee0: 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d 44 62 46 }. db->mDbF
2ef0: 6c 61 67 73 20 7c 3d 20 44 42 46 4c 41 47 5f 50 lags |= DBFLAG_P
2f00: 72 65 66 65 72 42 75 69 6c 74 69 6e 3b 0a 20 20 referBuiltin;.
2f10: 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 sqlite3NestedP
2f20: 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 arse(pParse, .
2f30: 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 "UPDATE \"
2f40: 25 77 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20 %w\".%s SET ".
2f50: 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 "sql = s
2f60: 75 62 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20 ubstr(sql,1,%d)
2f70: 7c 7c 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c || ', ' || %Q ||
2f80: 20 73 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20 substr(sql,%d)
2f90: 22 0a 20 20 20 20 20 20 20 20 22 57 48 45 52 45 ". "WHERE
2fa0: 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 type = 'table'
2fb0: 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20 AND name = %Q",
2fc0: 0a 20 20 20 20 20 20 7a 44 62 2c 20 4d 41 53 54 . zDb, MAST
2fd0: 45 52 5f 4e 41 4d 45 2c 20 70 4e 65 77 2d 3e 61 ER_NAME, pNew->a
2fe0: 64 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43 6f ddColOffset, zCo
2ff0: 6c 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f l, pNew->addColO
3000: 66 66 73 65 74 2b 31 2c 0a 20 20 20 20 20 20 7a ffset+1,. z
3010: 54 61 62 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 Tab. );. s
3020: 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
3030: 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 64 62 2d 3e zCol);. db->
3040: 6d 44 62 46 6c 61 67 73 20 3d 20 73 61 76 65 64 mDbFlags = saved
3050: 44 62 46 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 DbFlags;. }..
3060: 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
3070: 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 schema version
3080: 69 73 20 61 74 20 6c 65 61 73 74 20 33 2e 20 20 is at least 3.
3090: 42 75 74 20 64 6f 20 6e 6f 74 20 75 70 67 72 61 But do not upgra
30a0: 64 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 6c 65 73 de. ** from les
30b0: 73 20 74 68 61 6e 20 33 20 74 6f 20 34 2c 20 61 s than 3 to 4, a
30c0: 73 20 74 68 61 74 20 77 69 6c 6c 20 63 6f 72 72 s that will corr
30d0: 75 70 74 20 61 6e 79 20 70 72 65 65 78 69 73 74 upt any preexist
30e0: 69 6e 67 20 44 45 53 43 0a 20 20 2a 2a 20 69 6e ing DESC. ** in
30f0: 64 65 78 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 dex.. */. v =
3100: 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
3110: 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 Parse);. if( v
3120: 29 7b 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69 ){. r1 = sqli
3130: 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
3140: 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 arse);. sqlit
3150: 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
3160: 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 OP_ReadCookie, i
3170: 44 62 2c 20 72 31 2c 20 42 54 52 45 45 5f 46 49 Db, r1, BTREE_FI
3180: 4c 45 5f 46 4f 52 4d 41 54 29 3b 0a 20 20 20 20 LE_FORMAT);.
3190: 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 sqlite3VdbeUsesB
31a0: 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 tree(v, iDb);.
31b0: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
31c0: 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d Op2(v, OP_AddImm
31d0: 2c 20 72 31 2c 20 2d 32 29 3b 0a 20 20 20 20 73 , r1, -2);. s
31e0: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
31f0: 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 72 31 (v, OP_IfPos, r1
3200: 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 , sqlite3VdbeCur
3210: 72 65 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a rentAddr(v)+2);.
3220: 20 20 20 20 56 64 62 65 43 6f 76 65 72 61 67 65 VdbeCoverage
3230: 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 (v);. sqlite3
3240: 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
3250: 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c _SetCookie, iDb,
3260: 20 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d BTREE_FILE_FORM
3270: 41 54 2c 20 33 29 3b 0a 20 20 20 20 73 71 6c 69 AT, 3);. sqli
3280: 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
3290: 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 g(pParse, r1);.
32a0: 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 }.. /* Reload
32b0: 74 68 65 20 74 61 62 6c 65 20 64 65 66 69 6e 69 the table defini
32c0: 74 69 6f 6e 20 2a 2f 0a 20 20 72 65 6e 61 6d 65 tion */. rename
32d0: 52 65 6c 6f 61 64 53 63 68 65 6d 61 28 70 50 61 ReloadSchema(pPa
32e0: 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a rse, iDb);.}../*
32f0: 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
3300: 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 n is called by t
3310: 68 65 20 70 61 72 73 65 72 20 61 66 74 65 72 20 he parser after
3320: 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 the table-name i
3330: 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 n.** an "ALTER T
3340: 41 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 ABLE <table-name
3350: 3e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 > ADD" statement
3360: 20 69 73 20 70 61 72 73 65 64 2e 20 41 72 67 75 is parsed. Argu
3370: 6d 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 ment .** pSrc is
3380: 20 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f the full-name o
3390: 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e f the table bein
33a0: 67 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a g altered..**.**
33b0: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 This routine ma
33c0: 6b 65 73 20 61 20 28 70 61 72 74 69 61 6c 29 20 kes a (partial)
33d0: 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c copy of the Tabl
33e0: 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 e structure.** f
33f0: 6f 72 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 or the table bei
3400: 6e 67 20 61 6c 74 65 72 65 64 20 61 6e 64 20 73 ng altered and s
3410: 65 74 73 20 50 61 72 73 65 2e 70 4e 65 77 54 61 ets Parse.pNewTa
3420: 62 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 ble to point.**
3430: 74 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 to it. Routines
3440: 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 called by the pa
3450: 72 73 65 72 20 61 73 20 74 68 65 20 63 6f 6c 75 rser as the colu
3460: 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a mn definition.**
3470: 20 69 73 20 70 61 72 73 65 64 20 28 69 2e 65 2e is parsed (i.e.
3480: 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d sqlite3AddColum
3490: 6e 28 29 29 20 61 64 64 20 74 68 65 20 6e 65 77 n()) add the new
34a0: 20 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 Column data to
34b0: 0a 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20 54 68 .** the copy. Th
34c0: 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 e copy of the Ta
34d0: 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 ble structure is
34e0: 20 64 65 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 deleted by toke
34f0: 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 nize.c .** after
3500: 20 70 61 72 73 69 6e 67 20 69 73 20 66 69 6e 69 parsing is fini
3510: 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 shed..**.** Rout
3520: 69 6e 65 20 73 71 6c 69 74 65 33 41 6c 74 65 72 ine sqlite3Alter
3530: 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 FinishAddColumn(
3540: 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 ) will be called
3550: 20 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 to complete.**
3560: 63 6f 64 69 6e 67 20 74 68 65 20 22 41 4c 54 45 coding the "ALTE
3570: 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 R TABLE ... ADD"
3580: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 76 statement..*/.v
3590: 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 oid sqlite3Alter
35a0: 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 BeginAddColumn(P
35b0: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 arse *pParse, Sr
35c0: 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 cList *pSrc){.
35d0: 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 54 Table *pNew;. T
35e0: 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 6e able *pTab;. in
35f0: 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a t iDb;. int i;.
3600: 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 int nAlloc;.
3610: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
3620: 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 arse->db;.. /*
3630: 4c 6f 6f 6b 20 75 70 20 74 68 65 20 74 61 62 6c Look up the tabl
3640: 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e e being altered.
3650: 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
3660: 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d arse->pNewTable=
3670: 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
3680: 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
3690: 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 sAllMutexes(db)
36a0: 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c );. if( db->mal
36b0: 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f locFailed ) goto
36c0: 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f exit_begin_add_
36d0: 63 6f 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20 3d column;. pTab =
36e0: 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 sqlite3LocateTa
36f0: 62 6c 65 49 74 65 6d 28 70 50 61 72 73 65 2c 20 bleItem(pParse,
3700: 30 2c 20 26 70 53 72 63 2d 3e 61 5b 30 5d 29 3b 0, &pSrc->a[0]);
3710: 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 20 67 . if( !pTab ) g
3720: 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 oto exit_begin_a
3730: 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66 6e dd_column;..#ifn
3740: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
3750: 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 VIRTUALTABLE. i
3760: 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 f( IsVirtual(pTa
3770: 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 b) ){. sqlite
3780: 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
3790: 2c 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 , "virtual table
37a0: 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 s may not be alt
37b0: 65 72 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f ered");. goto
37c0: 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f exit_begin_add_
37d0: 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e 64 column;. }.#end
37e0: 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 if.. /* Make su
37f0: 72 65 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 re this is not a
3800: 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 41 4c 54 n attempt to ALT
3810: 45 52 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 ER a view. */.
3820: 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 if( pTab->pSelec
3830: 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
3840: 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
3850: 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 "Cannot add a c
3860: 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69 65 77 22 olumn to a view"
3870: 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 );. goto exit
3880: 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d _begin_add_colum
3890: 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c n;. }. if( SQL
38a0: 49 54 45 5f 4f 4b 21 3d 69 73 41 6c 74 65 72 61 ITE_OK!=isAltera
38b0: 62 6c 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c bleTable(pParse,
38c0: 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f pTab) ){. go
38d0: 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 to exit_begin_ad
38e0: 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 d_column;. }..
38f0: 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 61 assert( pTab->a
3900: 64 64 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29 3b ddColOffset>0 );
3910: 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 . iDb = sqlite3
3920: 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
3930: 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 , pTab->pSchema)
3940: 3b 0a 0a 20 20 2f 2a 20 50 75 74 20 61 20 63 6f ;.. /* Put a co
3950: 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 py of the Table
3960: 73 74 72 75 63 74 20 69 6e 20 50 61 72 73 65 2e struct in Parse.
3970: 70 4e 65 77 54 61 62 6c 65 20 66 6f 72 20 74 68 pNewTable for th
3980: 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 64 e. ** sqlite3Ad
3990: 64 43 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74 69 dColumn() functi
39a0: 6f 6e 20 61 6e 64 20 66 72 69 65 6e 64 73 20 74 on and friends t
39b0: 6f 20 6d 6f 64 69 66 79 2e 20 20 42 75 74 20 6d o modify. But m
39c0: 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 6e odify. ** the n
39d0: 61 6d 65 20 62 79 20 61 64 64 69 6e 67 20 61 6e ame by adding an
39e0: 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 "sqlite_alterta
39f0: 62 5f 22 20 70 72 65 66 69 78 2e 20 20 42 79 20 b_" prefix. By
3a00: 61 64 64 69 6e 67 20 74 68 69 73 0a 20 20 2a 2a adding this. **
3a10: 20 70 72 65 66 69 78 2c 20 77 65 20 69 6e 73 75 prefix, we insu
3a20: 72 65 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65 re that the name
3a30: 20 77 69 6c 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 will not collid
3a40: 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 69 e with an existi
3a50: 6e 67 0a 20 20 2a 2a 20 74 61 62 6c 65 20 62 65 ng. ** table be
3a60: 63 61 75 73 65 20 75 73 65 72 20 74 61 62 6c 65 cause user table
3a70: 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 are not allowed
3a80: 20 74 6f 20 68 61 76 65 20 74 68 65 20 22 73 71 to have the "sq
3a90: 6c 69 74 65 5f 22 0a 20 20 2a 2a 20 70 72 65 66 lite_". ** pref
3aa0: 69 78 20 6f 6e 20 74 68 65 69 72 20 6e 61 6d 65 ix on their name
3ab0: 2e 0a 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 .. */. pNew =
3ac0: 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 33 44 (Table*)sqlite3D
3ad0: 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
3ae0: 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a sizeof(Table));.
3af0: 20 20 69 66 28 20 21 70 4e 65 77 20 29 20 67 6f if( !pNew ) go
3b00: 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 to exit_begin_ad
3b10: 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 d_column;. pPar
3b20: 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 se->pNewTable =
3b30: 70 4e 65 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e 54 pNew;. pNew->nT
3b40: 61 62 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 abRef = 1;. pNe
3b50: 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e w->nCol = pTab->
3b60: 6e 43 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 nCol;. assert(
3b70: 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a pNew->nCol>0 );.
3b80: 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 28 28 70 4e nAlloc = (((pN
3b90: 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f 38 29 2a 38 ew->nCol-1)/8)*8
3ba0: 29 2b 38 3b 0a 20 20 61 73 73 65 72 74 28 20 6e )+8;. assert( n
3bb0: 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d 3e 6e 43 6f Alloc>=pNew->nCo
3bc0: 6c 20 26 26 20 6e 41 6c 6c 6f 63 25 38 3d 3d 30 l && nAlloc%8==0
3bd0: 20 26 26 20 6e 41 6c 6c 6f 63 2d 70 4e 65 77 2d && nAlloc-pNew-
3be0: 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20 20 70 4e 65 >nCol<8 );. pNe
3bf0: 77 2d 3e 61 43 6f 6c 20 3d 20 28 43 6f 6c 75 6d w->aCol = (Colum
3c00: 6e 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c n*)sqlite3DbMall
3c10: 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
3c20: 66 28 43 6f 6c 75 6d 6e 29 2a 6e 41 6c 6c 6f 63 f(Column)*nAlloc
3c30: 29 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 );. pNew->zName
3c40: 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
3c50: 66 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 61 6c f(db, "sqlite_al
3c60: 74 65 72 74 61 62 5f 25 73 22 2c 20 70 54 61 62 tertab_%s", pTab
3c70: 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 ->zName);. if(
3c80: 21 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c 7c 20 21 !pNew->aCol || !
3c90: 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 pNew->zName ){.
3ca0: 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d assert( db->m
3cb0: 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
3cc0: 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 goto exit_beg
3cd0: 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 in_add_column;.
3ce0: 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 }. memcpy(pNew
3cf0: 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 ->aCol, pTab->aC
3d00: 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d ol, sizeof(Colum
3d10: 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a n)*pNew->nCol);.
3d20: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 for(i=0; i<pNe
3d30: 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 w->nCol; i++){.
3d40: 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 Column *pCol
3d50: 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d = &pNew->aCol[i]
3d60: 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d ;. pCol->zNam
3d70: 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 e = sqlite3DbStr
3d80: 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e Dup(db, pCol->zN
3d90: 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e ame);. pCol->
3da0: 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 zColl = 0;. p
3db0: 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 3b 0a Col->pDflt = 0;.
3dc0: 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 53 63 68 }. pNew->pSch
3dd0: 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 ema = db->aDb[iD
3de0: 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 4e b].pSchema;. pN
3df0: 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 ew->addColOffset
3e00: 20 3d 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f = pTab->addColO
3e10: 66 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e ffset;. pNew->n
3e20: 54 61 62 52 65 66 20 3d 20 31 3b 0a 0a 65 78 69 TabRef = 1;..exi
3e30: 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 t_begin_add_colu
3e40: 6d 6e 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 mn:. sqlite3Src
3e50: 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
3e60: 53 72 63 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a Src);. return;.
3e70: 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 }../*.** Paramet
3e80: 65 72 20 70 54 61 62 20 69 73 20 74 68 65 20 73 er pTab is the s
3e90: 75 62 6a 65 63 74 20 6f 66 20 61 6e 20 41 4c 54 ubject of an ALT
3ea0: 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 52 45 4e ER TABLE ... REN
3eb0: 41 4d 45 20 43 4f 4c 55 4d 4e 0a 2a 2a 20 63 6f AME COLUMN.** co
3ec0: 6d 6d 61 6e 64 2e 20 54 68 69 73 20 66 75 6e 63 mmand. This func
3ed0: 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74 tion checks if t
3ee0: 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 he table is a vi
3ef0: 65 77 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a ew or virtual.**
3f00: 20 74 61 62 6c 65 20 28 63 6f 6c 75 6d 6e 73 20 table (columns
3f10: 6f 66 20 76 69 65 77 73 20 6f 72 20 76 69 72 74 of views or virt
3f20: 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e ual tables may n
3f30: 6f 74 20 62 65 20 72 65 6e 61 6d 65 64 29 2e 20 ot be renamed).
3f40: 49 66 20 73 6f 2c 0a 2a 2a 20 69 74 20 6c 6f 61 If so,.** it loa
3f50: 64 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ds an error mess
3f60: 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73 65 20 age into pParse
3f70: 61 6e 64 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d and returns non-
3f80: 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 zero..**.** Or,
3f90: 69 66 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 if pTab is not a
3fa0: 20 76 69 65 77 20 6f 72 20 76 69 72 74 75 61 6c view or virtual
3fb0: 20 74 61 62 6c 65 2c 20 7a 65 72 6f 20 69 73 20 table, zero is
3fc0: 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 23 69 66 returned..*/.#if
3fd0: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
3fe0: 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 _OMIT_VIEW) || !
3ff0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
4000: 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
4010: 29 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 52 ).static int isR
4020: 65 61 6c 54 61 62 6c 65 28 50 61 72 73 65 20 2a ealTable(Parse *
4030: 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 pParse, Table *p
4040: 54 61 62 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 Tab){. const ch
4050: 61 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 23 ar *zType = 0;.#
4060: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
4070: 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70 54 IT_VIEW. if( pT
4080: 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 ab->pSelect ){.
4090: 20 20 20 7a 54 79 70 65 20 3d 20 22 76 69 65 77 zType = "view
40a0: 22 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 ";. }.#endif.#i
40b0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
40c0: 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
40d0: 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 if( IsVirtual(p
40e0: 54 61 62 29 20 29 7b 0a 20 20 20 20 7a 54 79 70 Tab) ){. zTyp
40f0: 65 20 3d 20 22 76 69 72 74 75 61 6c 20 74 61 62 e = "virtual tab
4100: 6c 65 22 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a le";. }.#endif.
4110: 20 20 69 66 28 20 7a 54 79 70 65 20 29 7b 0a 20 if( zType ){.
4120: 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
4130: 73 67 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 sg(. pPar
4140: 73 65 2c 20 22 63 61 6e 6e 6f 74 20 72 65 6e 61 se, "cannot rena
4150: 6d 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 25 73 me columns of %s
4160: 20 5c 22 25 73 5c 22 22 2c 20 7a 54 79 70 65 2c \"%s\"", zType,
4170: 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 pTab->zName.
4180: 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 );. return 1
4190: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
41a0: 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 21 64 65 ;.}.#else /* !de
41b0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
41c0: 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 T_VIEW) || !defi
41d0: 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
41e0: 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20 2a 2f VIRTUALTABLE) */
41f0: 0a 23 20 64 65 66 69 6e 65 20 69 73 52 65 61 6c .# define isReal
4200: 54 61 62 6c 65 28 78 2c 79 29 20 28 30 29 0a 23 Table(x,y) (0).#
4210: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 48 61 6e endif../*.** Han
4220: 64 6c 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 dles the followi
4230: 6e 67 20 70 61 72 73 65 72 20 72 65 64 75 63 74 ng parser reduct
4240: 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 63 6d 64 20 ion:.**.** cmd
4250: 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 ::= ALTER TABLE
4260: 70 53 72 63 20 52 45 4e 41 4d 45 20 43 4f 4c 55 pSrc RENAME COLU
4270: 4d 4e 20 70 4f 6c 64 20 54 4f 20 70 4e 65 77 0a MN pOld TO pNew.
4280: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 41 */.void sqlite3A
4290: 6c 74 65 72 52 65 6e 61 6d 65 43 6f 6c 75 6d 6e lterRenameColumn
42a0: 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
42b0: 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
42c0: 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
42d0: 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c ontext */. SrcL
42e0: 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 ist *pSrc,
42f0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
4300: 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 able being alter
4310: 65 64 2e 20 20 70 53 72 63 2d 3e 6e 53 72 63 3d ed. pSrc->nSrc=
4320: 3d 31 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 =1 */. Token *p
4330: 4f 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 Old,
4340: 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
4350: 6f 66 20 63 6f 6c 75 6d 6e 20 62 65 69 6e 67 20 of column being
4360: 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 54 6f 6b changed */. Tok
4370: 65 6e 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 en *pNew
4380: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4390: 4e 65 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 New column name
43a0: 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 */.){. sqlite3
43b0: 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
43c0: 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 ; /* Datab
43d0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
43e0: 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b /. Table *pTab;
43f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4400: 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 /* Table bei
4410: 6e 67 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 ng updated */.
4420: 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 int iCol;
4430: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4440: 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 /* Index of colu
4450: 6d 6e 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 mn being renamed
4460: 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 6c 64 */. char *zOld
4470: 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
4480: 20 20 20 20 20 20 2f 2a 20 4f 6c 64 20 63 6f 6c /* Old col
4490: 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 umn name */. ch
44a0: 61 72 20 2a 7a 4e 65 77 20 3d 20 30 3b 20 20 20 ar *zNew = 0;
44b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
44c0: 20 4e 65 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 New column name
44d0: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
44e0: 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 *zDb;
44f0: 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
4500: 20 73 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 schema containi
4510: 6e 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a ng the table */.
4520: 20 20 69 6e 74 20 69 53 63 68 65 6d 61 3b 20 20 int iSchema;
4530: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4540: 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 /* Index of th
4550: 65 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e e schema */. in
4560: 74 20 62 51 75 6f 74 65 3b 20 20 20 20 20 20 20 t bQuote;
4570: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4580: 20 54 72 75 65 20 74 6f 20 71 75 6f 74 65 20 74 True to quote t
4590: 68 65 20 6e 65 77 20 6e 61 6d 65 20 2a 2f 0a 20 he new name */.
45a0: 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 Vdbe *v;
45b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45c0: 20 2f 2a 20 56 44 42 45 20 68 61 6e 64 6c 65 20 /* VDBE handle
45d0: 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 66 43 */. int regRefC
45e0: 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nt;
45f0: 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
4600: 20 66 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 72 for number of r
4610: 65 66 73 20 74 6f 20 63 6f 6c 75 6d 6e 20 2a 2f efs to column */
4620: 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 .. /* Locate th
4630: 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 6c e table to be al
4640: 74 65 72 65 64 20 2a 2f 0a 20 20 70 54 61 62 20 tered */. pTab
4650: 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 = sqlite3LocateT
4660: 61 62 6c 65 49 74 65 6d 28 70 50 61 72 73 65 2c ableItem(pParse,
4670: 20 30 2c 20 26 70 53 72 63 2d 3e 61 5b 30 5d 29 0, &pSrc->a[0])
4680: 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 20 ;. if( !pTab )
4690: 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 goto exit_rename
46a0: 5f 63 6f 6c 75 6d 6e 3b 0a 0a 20 20 2f 2a 20 43 _column;.. /* C
46b0: 61 6e 6e 6f 74 20 61 6c 74 65 72 20 61 20 73 79 annot alter a sy
46c0: 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 20 20 stem table */.
46d0: 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 69 if( SQLITE_OK!=i
46e0: 73 41 6c 74 65 72 61 62 6c 65 54 61 62 6c 65 28 sAlterableTable(
46f0: 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 20 pParse, pTab) )
4700: 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 goto exit_rename
4710: 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 53 _column;. if( S
4720: 51 4c 49 54 45 5f 4f 4b 21 3d 69 73 52 65 61 6c QLITE_OK!=isReal
4730: 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 Table(pParse, pT
4740: 61 62 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f ab) ) goto exit_
4750: 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 3b 0a 0a rename_column;..
4760: 20 20 2f 2a 20 57 68 69 63 68 20 73 63 68 65 6d /* Which schem
4770: 61 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c a holds the tabl
4780: 65 20 74 6f 20 62 65 20 61 6c 74 65 72 65 64 20 e to be altered
4790: 2a 2f 20 20 0a 20 20 69 53 63 68 65 6d 61 20 3d */ . iSchema =
47a0: 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
47b0: 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e Index(db, pTab->
47c0: 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 pSchema);. asse
47d0: 72 74 28 20 69 53 63 68 65 6d 61 3e 3d 30 20 29 rt( iSchema>=0 )
47e0: 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 ;. zDb = db->aD
47f0: 62 5b 69 53 63 68 65 6d 61 5d 2e 7a 44 62 53 4e b[iSchema].zDbSN
4800: 61 6d 65 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 ame;..#ifndef SQ
4810: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
4820: 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 IZATION. /* Inv
4830: 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a oke the authoriz
4840: 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 ation callback.
4850: 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 */. if( sqlite3
4860: 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
4870: 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 , SQLITE_ALTER_T
4880: 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d ABLE, zDb, pTab-
4890: 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 >zName, 0) ){.
48a0: 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 goto exit_rena
48b0: 6d 65 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 me_column;. }.#
48c0: 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 endif.. /* Make
48d0: 20 73 75 72 65 20 74 68 65 20 6f 6c 64 20 6e 61 sure the old na
48e0: 6d 65 20 72 65 61 6c 6c 79 20 69 73 20 61 20 63 me really is a c
48f0: 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 6e 20 74 68 olumn name in th
4900: 65 20 74 61 62 6c 65 20 74 6f 20 62 65 0a 20 20 e table to be.
4910: 2a 2a 20 61 6c 74 65 72 65 64 2e 20 20 53 65 74 ** altered. Set
4920: 20 69 43 6f 6c 20 74 6f 20 62 65 20 74 68 65 20 iCol to be the
4930: 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c index of the col
4940: 75 6d 6e 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 umn being rename
4950: 64 20 2a 2f 0a 20 20 7a 4f 6c 64 20 3d 20 73 71 d */. zOld = sq
4960: 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
4970: 65 6e 28 64 62 2c 20 70 4f 6c 64 29 3b 0a 20 20 en(db, pOld);.
4980: 69 66 28 20 21 7a 4f 6c 64 20 29 20 67 6f 74 6f if( !zOld ) goto
4990: 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 63 6f 6c exit_rename_col
49a0: 75 6d 6e 3b 0a 20 20 66 6f 72 28 69 43 6f 6c 3d umn;. for(iCol=
49b0: 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 0; iCol<pTab->nC
49c0: 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 ol; iCol++){.
49d0: 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 if( 0==sqlite3S
49e0: 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f trICmp(pTab->aCo
49f0: 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a l[iCol].zName, z
4a00: 4f 6c 64 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 Old) ) break;.
4a10: 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 }. if( iCol==pT
4a20: 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 ab->nCol ){.
4a30: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
4a40: 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 pParse, "no such
4a50: 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 column: \"%s\""
4a60: 2c 20 7a 4f 6c 64 29 3b 0a 20 20 20 20 67 6f 74 , zOld);. got
4a70: 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 63 6f o exit_rename_co
4a80: 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 lumn;. }.. /*
4a90: 41 6c 6c 6f 63 61 74 65 20 61 20 72 65 67 69 73 Allocate a regis
4aa0: 74 65 72 20 74 6f 20 63 6f 75 6e 74 20 74 68 65 ter to count the
4ab0: 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 77 72 69 number of rewri
4ac0: 74 74 65 6e 20 63 6f 6c 75 6d 6e 20 72 65 66 73 tten column refs
4ad0: 2e 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 . */. v = sqlit
4ae0: 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
4af0: 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 );. if( v==0 )
4b00: 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 goto exit_rename
4b10: 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 72 65 67 52 65 _column;. regRe
4b20: 66 43 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d fCnt = ++pParse-
4b30: 3e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 >nMem;. sqlite3
4b40: 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
4b50: 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 _Integer, 0, reg
4b60: 52 65 66 43 6e 74 29 3b 0a 0a 20 20 2f 2a 20 44 RefCnt);.. /* D
4b70: 6f 20 74 68 65 20 72 65 6e 61 6d 65 20 6f 70 65 o the rename ope
4b80: 72 61 74 69 6f 6e 20 75 73 69 6e 67 20 61 6e 20 ration using an
4b90: 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 UPDATE statement
4ba0: 20 74 68 61 74 20 75 73 65 73 20 74 68 65 0a 20 that uses the.
4bb0: 20 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d ** sqlite_renam
4bc0: 65 5f 63 6f 6c 75 6d 6e 28 29 20 53 51 4c 20 66 e_column() SQL f
4bd0: 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 70 75 unction to compu
4be0: 74 65 20 74 68 65 20 6e 65 77 20 43 52 45 41 54 te the new CREAT
4bf0: 45 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e E . ** statemen
4c00: 74 20 74 65 78 74 20 66 6f 72 20 74 68 65 20 73 t text for the s
4c10: 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
4c20: 6c 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 le. */. sqlite
4c30: 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 3MayAbort(pParse
4c40: 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 );. zNew = sqli
4c50: 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
4c60: 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 69 66 (db, pNew);. if
4c70: 28 20 21 7a 4e 65 77 20 29 20 67 6f 74 6f 20 65 ( !zNew ) goto e
4c80: 78 69 74 5f 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d xit_rename_colum
4c90: 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 n;. assert( pNe
4ca0: 77 2d 3e 6e 3e 30 20 29 3b 0a 20 20 62 51 75 6f w->n>0 );. bQuo
4cb0: 74 65 20 3d 20 73 71 6c 69 74 65 33 49 73 71 75 te = sqlite3Isqu
4cc0: 6f 74 65 28 70 4e 65 77 2d 3e 7a 5b 30 5d 29 3b ote(pNew->z[0]);
4cd0: 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 . sqlite3Nested
4ce0: 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 Parse(pParse, .
4cf0: 20 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 "UPDATE \"%
4d00: 77 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20 20 w\".%s SET ".
4d10: 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 "sql = sqlite
4d20: 5f 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 28 73 _rename_column(s
4d30: 71 6c 2c 20 74 79 70 65 2c 20 6e 61 6d 65 2c 20 ql, type, name,
4d40: 25 51 2c 20 25 51 2c 20 25 64 2c 20 25 51 2c 20 %Q, %Q, %d, %Q,
4d50: 25 64 2c 20 25 64 2c 20 25 64 29 20 22 0a 20 20 %d, %d, %d) ".
4d60: 20 20 20 20 22 57 48 45 52 45 20 6e 61 6d 65 20 "WHERE name
4d70: 4e 4f 54 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 NOT LIKE 'sqlite
4d80: 58 5f 25 25 27 20 45 53 43 41 50 45 20 27 58 27 X_%%' ESCAPE 'X'
4d90: 20 22 0a 20 20 20 20 20 20 22 20 41 4e 44 20 28 ". " AND (
4da0: 74 79 70 65 20 21 3d 20 27 69 6e 64 65 78 27 20 type != 'index'
4db0: 4f 52 20 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 OR tbl_name = %Q
4dc0: 29 22 0a 20 20 20 20 20 20 22 20 41 4e 44 20 73 )". " AND s
4dd0: 71 6c 20 4e 4f 54 20 4c 49 4b 45 20 27 63 72 65 ql NOT LIKE 'cre
4de0: 61 74 65 20 76 69 72 74 75 61 6c 25 25 27 22 2c ate virtual%%'",
4df0: 0a 20 20 20 20 20 20 7a 44 62 2c 20 4d 41 53 54 . zDb, MAST
4e00: 45 52 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 ER_NAME, .
4e10: 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 zDb, pTab->zName
4e20: 2c 20 69 43 6f 6c 2c 20 7a 4e 65 77 2c 20 62 51 , iCol, zNew, bQ
4e30: 75 6f 74 65 2c 20 69 53 63 68 65 6d 61 3d 3d 31 uote, iSchema==1
4e40: 2c 20 72 65 67 52 65 66 43 6e 74 2c 0a 20 20 20 , regRefCnt,.
4e50: 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 pTab->zName.
4e60: 20 29 3b 0a 0a 20 20 69 66 28 20 69 53 63 68 65 );.. if( iSche
4e70: 6d 61 21 3d 31 20 29 7b 0a 20 20 20 20 73 71 6c ma!=1 ){. sql
4e80: 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
4e90: 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
4ea0: 20 22 55 50 44 41 54 45 20 74 65 6d 70 2e 25 73 "UPDATE temp.%s
4eb0: 20 53 45 54 20 22 0a 20 20 20 20 20 20 20 20 22 SET ". "
4ec0: 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e sql = sqlite_ren
4ed0: 61 6d 65 5f 63 6f 6c 75 6d 6e 28 73 71 6c 2c 20 ame_column(sql,
4ee0: 74 79 70 65 2c 20 6e 61 6d 65 2c 20 25 51 2c 20 type, name, %Q,
4ef0: 25 51 2c 20 25 64 2c 20 25 51 2c 20 25 64 2c 20 %Q, %d, %Q, %d,
4f00: 31 2c 20 25 64 29 22 0a 20 20 20 20 20 20 20 20 1, %d)".
4f10: 22 57 48 45 52 45 20 74 79 70 65 20 49 4e 20 28 "WHERE type IN (
4f20: 27 74 72 69 67 67 65 72 27 2c 20 27 76 69 65 77 'trigger', 'view
4f30: 27 29 22 2c 0a 20 20 20 20 20 20 20 20 4d 41 53 ')",. MAS
4f40: 54 45 52 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 TER_NAME, .
4f50: 20 20 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e zDb, pTab->zN
4f60: 61 6d 65 2c 20 69 43 6f 6c 2c 20 7a 4e 65 77 2c ame, iCol, zNew,
4f70: 20 62 51 75 6f 74 65 2c 20 72 65 67 52 65 66 43 bQuote, regRefC
4f80: 6e 74 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 nt. );. }..
4f90: 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 65 6c /* Drop and rel
4fa0: 6f 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 oad the database
4fb0: 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65 schema. */. re
4fc0: 6e 61 6d 65 52 65 6c 6f 61 64 53 63 68 65 6d 61 nameReloadSchema
4fd0: 28 70 50 61 72 73 65 2c 20 69 53 63 68 65 6d 61 (pParse, iSchema
4fe0: 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 77 20 61 72 72 );.. /* Now arr
4ff0: 61 6e 67 65 20 66 6f 72 20 74 68 65 20 73 71 6c ange for the sql
5000: 69 74 65 5f 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d ite_rename_colum
5010: 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 n() function to
5020: 62 65 20 63 61 6c 6c 65 64 20 61 0a 20 20 2a 2a be called a. **
5030: 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 6f 6e 20 second time on
5040: 74 68 65 20 61 6c 72 65 61 64 79 20 72 65 77 72 the already rewr
5050: 69 74 74 65 6e 20 73 63 68 65 6d 61 2e 20 54 68 itten schema. Th
5060: 69 73 20 73 65 72 76 65 73 20 74 77 6f 20 70 75 is serves two pu
5070: 72 70 6f 73 65 73 3a 0a 20 20 2a 2a 20 28 61 29 rposes:. ** (a)
5080: 20 74 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74 to check that t
5090: 68 65 20 73 63 68 65 6d 61 20 63 61 6e 20 73 74 he schema can st
50a0: 69 6c 6c 20 62 65 20 70 61 72 73 65 64 2c 20 61 ill be parsed, a
50b0: 6e 64 20 28 62 29 20 74 6f 20 64 65 63 72 65 6d nd (b) to decrem
50c0: 65 6e 74 0a 20 20 2a 2a 20 72 65 67 69 73 74 65 ent. ** registe
50d0: 72 20 72 65 67 52 65 66 43 6e 74 20 6f 6e 63 65 r regRefCnt once
50e0: 20 66 6f 72 20 65 61 63 68 20 72 65 66 65 72 65 for each refere
50f0: 6e 63 65 20 74 6f 20 74 68 65 20 72 65 6e 61 6d nce to the renam
5100: 65 64 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 ed column in the
5110: 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 28 74 68 . ** schema (th
5120: 65 20 63 61 6c 6c 73 20 61 62 6f 76 65 20 69 6e e calls above in
5130: 63 72 65 6d 65 6e 74 65 64 20 72 65 67 52 65 66 cremented regRef
5140: 43 6e 74 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 Cnt once for eac
5150: 68 20 73 75 63 68 20 72 65 66 65 72 65 6e 63 65 h such reference
5160: 0a 20 20 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 . ** prior to r
5170: 65 6e 61 6d 69 6e 67 29 2e 20 49 66 20 72 65 67 enaming). If reg
5180: 69 73 74 65 72 20 72 65 67 52 65 66 43 6e 74 20 ister regRefCnt
5190: 69 73 20 6e 6f 74 20 6c 65 66 74 20 73 65 74 20 is not left set
51a0: 74 6f 20 30 2c 20 74 68 65 6e 20 0a 20 20 2a 2a to 0, then . **
51b0: 20 72 65 6e 61 6d 69 6e 67 20 74 68 65 20 63 6f renaming the co
51c0: 6c 75 6d 6e 20 68 61 73 20 6c 65 66 74 20 74 68 lumn has left th
51d0: 65 20 73 63 68 65 6d 61 20 77 69 74 68 20 65 69 e schema with ei
51e0: 74 68 65 72 20 6d 6f 72 65 20 6f 72 20 66 65 77 ther more or few
51f0: 65 72 0a 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 er. ** referenc
5200: 65 73 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e es to the column
5210: 20 74 68 61 6e 20 74 68 65 72 65 20 77 61 73 20 than there was
5220: 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
5230: 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20 6f 70 of the . ** op
5240: 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 69 73 eration. This is
5250: 20 61 6e 20 65 72 72 6f 72 2c 20 73 6f 20 72 65 an error, so re
5260: 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 6d 65 turn an error me
5270: 73 73 61 67 65 20 61 6e 64 20 53 51 4c 49 54 45 ssage and SQLITE
5280: 5f 41 42 4f 52 54 0a 20 20 2a 2a 20 74 68 65 20 _ABORT. ** the
5290: 73 74 61 74 65 6d 65 6e 74 2e 20 20 2a 2f 20 0a statement. */ .
52a0: 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 sqlite3NestedP
52b0: 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 arse(pParse, .
52c0: 20 20 20 20 22 53 45 4c 45 43 54 20 31 20 22 0a "SELECT 1 ".
52d0: 20 20 20 20 20 20 22 46 52 4f 4d 20 5c 22 25 77 "FROM \"%w
52e0: 5c 22 2e 25 73 20 22 0a 20 20 20 20 20 20 22 57 \".%s ". "W
52f0: 48 45 52 45 20 6e 61 6d 65 20 4e 4f 54 20 4c 49 HERE name NOT LI
5300: 4b 45 20 27 73 71 6c 69 74 65 58 5f 25 25 27 20 KE 'sqliteX_%%'
5310: 45 53 43 41 50 45 20 27 58 27 22 0a 20 20 20 20 ESCAPE 'X'".
5320: 20 20 22 20 20 41 4e 44 20 73 71 6c 20 4e 4f 54 " AND sql NOT
5330: 20 4c 49 4b 45 20 27 63 72 65 61 74 65 20 76 69 LIKE 'create vi
5340: 72 74 75 61 6c 25 25 27 22 0a 20 20 20 20 20 20 rtual%%'".
5350: 22 20 20 41 4e 44 20 28 74 79 70 65 20 21 3d 20 " AND (type !=
5360: 27 69 6e 64 65 78 27 20 4f 52 20 74 62 6c 5f 6e 'index' OR tbl_n
5370: 61 6d 65 20 3d 20 25 51 29 22 0a 20 20 20 20 20 ame = %Q)".
5380: 20 22 20 20 41 4e 44 20 73 71 6c 69 74 65 5f 72 " AND sqlite_r
5390: 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 28 73 71 6c ename_column(sql
53a0: 2c 74 79 70 65 2c 6e 61 6d 65 2c 25 51 2c 25 51 ,type,name,%Q,%Q
53b0: 2c 25 64 2c 25 51 2c 25 64 2c 25 64 2c 25 64 29 ,%d,%Q,%d,%d,%d)
53c0: 3d 4e 55 4c 4c 22 2c 0a 20 20 20 20 20 20 7a 44 =NULL",. zD
53d0: 62 2c 20 4d 41 53 54 45 52 5f 4e 41 4d 45 2c 20 b, MASTER_NAME,
53e0: 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 . pTab->zNa
53f0: 6d 65 2c 0a 20 20 20 20 20 20 7a 44 62 2c 20 70 me,. zDb, p
5400: 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 69 43 6f 6c Tab->zName, iCol
5410: 2c 20 7a 4e 65 77 2c 20 62 51 75 6f 74 65 2c 20 , zNew, bQuote,
5420: 69 53 63 68 65 6d 61 3d 3d 31 2c 20 72 65 67 52 iSchema==1, regR
5430: 65 66 43 6e 74 2a 2d 31 0a 20 20 29 3b 0a 20 20 efCnt*-1. );.
5440: 69 66 28 20 69 53 63 68 65 6d 61 21 3d 31 20 29 if( iSchema!=1 )
5450: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 {. sqlite3Nes
5460: 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
5470: 20 0a 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 . "SELEC
5480: 54 20 31 20 22 0a 20 20 20 20 20 20 20 20 22 46 T 1 ". "F
5490: 52 4f 4d 20 74 65 6d 70 2e 25 73 20 22 0a 20 20 ROM temp.%s ".
54a0: 20 20 20 20 20 20 22 57 48 45 52 45 20 74 79 70 "WHERE typ
54b0: 65 20 49 4e 20 28 27 74 72 69 67 67 65 72 27 2c e IN ('trigger',
54c0: 20 27 76 69 65 77 27 29 22 0a 20 20 20 20 20 20 'view')".
54d0: 20 20 22 20 20 41 4e 44 20 73 71 6c 69 74 65 5f " AND sqlite_
54e0: 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 28 73 71 rename_column(sq
54f0: 6c 2c 74 79 70 65 2c 6e 61 6d 65 2c 25 51 2c 25 l,type,name,%Q,%
5500: 51 2c 25 64 2c 25 51 2c 25 64 2c 31 2c 25 64 29 Q,%d,%Q,%d,1,%d)
5510: 3d 4e 55 4c 4c 22 2c 0a 20 20 20 20 20 20 20 20 =NULL",.
5520: 4d 41 53 54 45 52 5f 4e 41 4d 45 2c 0a 20 20 20 MASTER_NAME,.
5530: 20 20 20 20 20 7a 44 62 2c 20 70 54 61 62 2d 3e zDb, pTab->
5540: 7a 4e 61 6d 65 2c 20 69 43 6f 6c 2c 20 7a 4e 65 zName, iCol, zNe
5550: 77 2c 20 62 51 75 6f 74 65 2c 20 72 65 67 52 65 w, bQuote, regRe
5560: 66 43 6e 74 2a 2d 31 0a 20 20 20 20 29 3b 0a 20 fCnt*-1. );.
5570: 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }.. sqlite3Vdb
5580: 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 eAddOp2(v, OP_If
5590: 4e 6f 74 2c 20 72 65 67 52 65 66 43 6e 74 2c 20 Not, regRefCnt,
55a0: 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
55b0: 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 ntAddr(v)+2);.
55c0: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
55d0: 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 2(v, OP_Halt, SQ
55e0: 4c 49 54 45 5f 45 52 52 4f 52 2c 20 4f 45 5f 41 LITE_ERROR, OE_A
55f0: 62 6f 72 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 bort);. sqlite3
5600: 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 VdbeChangeP4(v,
5610: 2d 31 2c 20 0a 20 20 20 20 20 20 22 73 63 68 65 -1, . "sche
5620: 6d 61 20 63 6f 6e 74 61 69 6e 73 20 61 6d 62 69 ma contains ambi
5630: 67 75 6f 75 73 20 64 6f 75 62 6c 65 2d 71 75 6f guous double-quo
5640: 74 65 64 20 73 74 72 69 6e 67 22 2c 20 50 34 5f ted string", P4_
5650: 53 54 41 54 49 43 0a 20 20 29 3b 0a 0a 20 65 78 STATIC. );.. ex
5660: 69 74 5f 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e it_rename_column
5670: 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 :. sqlite3SrcLi
5680: 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 stDelete(db, pSr
5690: 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 c);. sqlite3DbF
56a0: 72 65 65 28 64 62 2c 20 7a 4f 6c 64 29 3b 0a 20 ree(db, zOld);.
56b0: 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
56c0: 62 2c 20 7a 4e 65 77 29 3b 0a 20 20 72 65 74 75 b, zNew);. retu
56d0: 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 rn;.}../*.** Eac
56e0: 68 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 6f 62 h RenameToken ob
56f0: 6a 65 63 74 20 6d 61 70 73 20 61 6e 20 65 6c 65 ject maps an ele
5700: 6d 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 73 ment of the pars
5710: 65 20 74 72 65 65 20 69 6e 74 6f 0a 2a 2a 20 74 e tree into.** t
5720: 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 67 65 he token that ge
5730: 6e 65 72 61 74 65 64 20 74 68 61 74 20 65 6c 65 nerated that ele
5740: 6d 65 6e 74 2e 20 20 54 68 65 20 70 61 72 73 65 ment. The parse
5750: 20 74 72 65 65 20 65 6c 65 6d 65 6e 74 0a 2a 2a tree element.**
5760: 20 6d 69 67 68 74 20 62 65 20 6f 6e 65 20 6f 66 might be one of
5770: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20 41 :.**.** * A
5780: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 45 pointer to an E
5790: 78 70 72 20 74 68 61 74 20 72 65 70 72 65 73 65 xpr that represe
57a0: 6e 74 73 20 61 6e 20 49 44 0a 2a 2a 20 20 20 20 nts an ID.**
57b0: 20 2a 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 * The name of
57c0: 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 69 a table column i
57d0: 6e 20 43 6f 6c 75 6d 6e 2e 7a 4e 61 6d 65 0a 2a n Column.zName.*
57e0: 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 52 *.** A list of R
57f0: 65 6e 61 6d 65 54 6f 6b 65 6e 20 6f 62 6a 65 63 enameToken objec
5800: 74 73 20 63 61 6e 20 62 65 20 63 6f 6e 73 74 72 ts can be constr
5810: 75 63 74 65 64 20 64 75 72 69 6e 67 20 70 61 72 ucted during par
5820: 73 69 6e 67 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 sing..** Each ne
5830: 77 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 w object is crea
5840: 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 52 65 ted by sqlite3Re
5850: 6e 61 6d 65 54 6f 6b 65 6e 4d 61 70 28 29 2e 0a nameTokenMap()..
5860: 2a 2a 20 41 73 20 74 68 65 20 70 61 72 73 65 20 ** As the parse
5870: 74 72 65 65 20 69 73 20 74 72 61 6e 73 66 6f 72 tree is transfor
5880: 6d 65 64 2c 20 74 68 65 20 73 71 6c 69 74 65 33 med, the sqlite3
5890: 52 65 6e 61 6d 65 54 6f 6b 65 6e 52 65 6d 61 70 RenameTokenRemap
58a0: 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 ().** routine is
58b0: 20 75 73 65 64 20 74 6f 20 6b 65 65 70 20 74 68 used to keep th
58c0: 65 20 6d 61 70 70 69 6e 67 20 63 75 72 72 65 6e e mapping curren
58d0: 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 t..**.** After t
58e0: 68 65 20 70 61 72 73 65 20 66 69 6e 69 73 68 65 he parse finishe
58f0: 73 2c 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 s, renameTokenFi
5900: 6e 64 28 29 20 72 6f 75 74 69 6e 65 20 63 61 6e nd() routine can
5910: 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c be used.** to l
5920: 6f 6f 6b 20 75 70 20 74 68 65 20 61 63 74 75 61 ook up the actua
5930: 6c 20 74 6f 6b 65 6e 20 76 61 6c 75 65 20 74 68 l token value th
5940: 61 74 20 63 72 65 61 74 65 64 20 73 6f 6d 65 20 at created some
5950: 65 6c 65 6d 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 element in.** th
5960: 65 20 70 61 72 73 65 20 74 72 65 65 2e 0a 2a 2f e parse tree..*/
5970: 0a 73 74 72 75 63 74 20 52 65 6e 61 6d 65 54 6f .struct RenameTo
5980: 6b 65 6e 20 7b 0a 20 20 76 6f 69 64 20 2a 70 3b ken {. void *p;
5990: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
59a0: 2a 20 50 61 72 73 65 20 74 72 65 65 20 65 6c 65 * Parse tree ele
59b0: 6d 65 6e 74 20 63 72 65 61 74 65 64 20 62 79 20 ment created by
59c0: 74 6f 6b 65 6e 20 74 20 2a 2f 0a 20 20 54 6f 6b token t */. Tok
59d0: 65 6e 20 74 3b 20 20 20 20 20 20 20 20 20 20 20 en t;
59e0: 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e /* The token
59f0: 20 74 68 61 74 20 63 72 65 61 74 65 64 20 70 61 that created pa
5a00: 72 73 65 20 74 72 65 65 20 65 6c 65 6d 65 6e 74 rse tree element
5a10: 20 70 20 2a 2f 0a 20 20 52 65 6e 61 6d 65 54 6f p */. RenameTo
5a20: 6b 65 6e 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f ken *pNext; /
5a30: 2a 20 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 * Next is a list
5a40: 20 6f 66 20 61 6c 6c 20 52 65 6e 61 6d 65 54 6f of all RenameTo
5a50: 6b 65 6e 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d ken objects */.}
5a60: 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e ;../*.** The con
5a70: 74 65 78 74 20 6f 66 20 61 6e 20 41 4c 54 45 52 text of an ALTER
5a80: 20 54 41 42 4c 45 20 52 45 4e 41 4d 45 20 43 4f TABLE RENAME CO
5a90: 4c 55 4d 4e 20 6f 70 65 72 61 74 69 6f 6e 20 74 LUMN operation t
5aa0: 68 61 74 20 67 65 74 73 20 70 61 73 73 65 64 0a hat gets passed.
5ab0: 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68 65 ** down into the
5ac0: 20 57 61 6c 6b 65 72 2e 0a 2a 2f 0a 74 79 70 65 Walker..*/.type
5ad0: 64 65 66 20 73 74 72 75 63 74 20 52 65 6e 61 6d def struct Renam
5ae0: 65 43 74 78 20 52 65 6e 61 6d 65 43 74 78 3b 0a eCtx RenameCtx;.
5af0: 73 74 72 75 63 74 20 52 65 6e 61 6d 65 43 74 78 struct RenameCtx
5b00: 20 7b 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 6e {. RenameToken
5b10: 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20 20 20 *pList;
5b20: 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
5b30: 74 6f 6b 65 6e 73 20 74 6f 20 6f 76 65 72 77 72 tokens to overwr
5b40: 69 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 69 ite */. int nLi
5b50: 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 st;
5b60: 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5b70: 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 er of tokens in
5b80: 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 pList */. int i
5b90: 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Col;
5ba0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
5bb0: 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 62 65 dex of column be
5bc0: 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 ing renamed */.
5bd0: 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 Table *pTab;
5be0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bf0: 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 /* Table being
5c00: 41 4c 54 45 52 65 64 20 2a 2f 20 0a 20 20 63 6f ALTERed */ . co
5c10: 6e 73 74 20 63 68 61 72 20 2a 7a 4f 6c 64 3b 20 nst char *zOld;
5c20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5c30: 20 4f 6c 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 Old column name
5c40: 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 */.};..#ifdef S
5c50: 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a QLITE_DEBUG./*.*
5c60: 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
5c70: 69 73 20 6f 6e 6c 79 20 66 6f 72 20 64 65 62 75 is only for debu
5c80: 67 67 69 6e 67 2e 20 49 74 20 70 65 72 66 6f 72 gging. It perfor
5c90: 6d 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a ms two tasks:.**
5ca0: 0a 2a 2a 20 20 20 31 2e 20 43 68 65 63 6b 73 20 .** 1. Checks
5cb0: 74 68 61 74 20 70 6f 69 6e 74 65 72 20 70 50 74 that pointer pPt
5cc0: 72 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 r does not alrea
5cd0: 64 79 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 dy appear in the
5ce0: 20 0a 2a 2a 20 20 20 20 20 20 72 65 6e 61 6d 65 .** rename
5cf0: 2d 74 6f 6b 65 6e 20 6c 69 73 74 2e 0a 2a 2a 0a -token list..**.
5d00: 2a 2a 20 20 20 32 2e 20 44 65 72 65 66 65 72 65 ** 2. Derefere
5d10: 6e 63 65 73 20 65 61 63 68 20 70 6f 69 6e 74 65 nces each pointe
5d20: 72 20 69 6e 20 74 68 65 20 72 65 6e 61 6d 65 2d r in the rename-
5d30: 74 6f 6b 65 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a token list..**.*
5d40: 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20 * The second is
5d50: 6d 6f 73 74 20 65 66 66 65 63 74 69 76 65 20 77 most effective w
5d60: 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20 75 6e hen debugging un
5d70: 64 65 72 20 76 61 6c 67 72 69 6e 64 20 6f 72 0a der valgrind or.
5d80: 2a 2a 20 61 64 64 72 65 73 73 2d 73 61 6e 69 74 ** address-sanit
5d90: 69 7a 65 72 20 6f 72 20 73 69 6d 69 6c 61 72 2e izer or similar.
5da0: 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 If any of these
5db0: 20 70 6f 69 6e 74 65 72 73 20 6e 6f 20 6c 6f 6e pointers no lon
5dc0: 67 65 72 20 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f ger .** point to
5dd0: 20 76 61 6c 69 64 20 6f 62 6a 65 63 74 73 2c 20 valid objects,
5de0: 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 an exception is
5df0: 72 61 69 73 65 64 20 62 79 20 74 68 65 20 6d 65 raised by the me
5e00: 6d 6f 72 79 2d 63 68 65 63 6b 69 6e 67 20 0a 2a mory-checking .*
5e10: 2a 20 74 6f 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 * tool..**.** Th
5e20: 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 e point of this
5e30: 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 63 6f is to prevent co
5e40: 6d 70 61 72 69 73 6f 6e 73 20 6f 66 20 69 6e 76 mparisons of inv
5e50: 61 6c 69 64 20 70 6f 69 6e 74 65 72 20 76 61 6c alid pointer val
5e60: 75 65 73 2e 0a 2a 2a 20 45 76 65 6e 20 74 68 6f ues..** Even tho
5e70: 75 67 68 20 74 68 69 73 20 61 6c 77 61 79 73 20 ugh this always
5e80: 73 65 65 6d 73 20 74 6f 20 77 6f 72 6b 2c 20 69 seems to work, i
5e90: 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 t is undefined a
5ea0: 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 0a ccording to the.
5eb0: 2a 2a 20 43 20 73 74 61 6e 64 61 72 64 2e 20 45 ** C standard. E
5ec0: 78 61 6d 70 6c 65 20 6f 66 20 75 6e 64 65 66 69 xample of undefi
5ed0: 6e 65 64 20 63 6f 6d 70 61 72 69 73 6f 6e 3a 0a ned comparison:.
5ee0: 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 **.** sqlite
5ef0: 33 5f 66 72 65 65 28 78 29 3b 0a 2a 2a 20 20 20 3_free(x);.**
5f00: 20 20 69 66 28 20 78 3d 3d 79 20 29 20 2e 2e 2e if( x==y ) ...
5f10: 0a 2a 2a 0a 2a 2a 20 54 65 63 68 6e 69 63 61 6c .**.** Technical
5f20: 6c 79 2c 20 61 73 20 78 20 6e 6f 20 6c 6f 6e 67 ly, as x no long
5f30: 65 72 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 61 er points into a
5f40: 20 76 61 6c 69 64 20 6f 62 6a 65 63 74 20 6f 72 valid object or
5f50: 20 74 6f 20 74 68 65 20 62 79 74 65 0a 2a 2a 20 to the byte.**
5f60: 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 76 61 6c 69 following a vali
5f70: 64 20 6f 62 6a 65 63 74 2c 20 69 74 20 6d 61 79 d object, it may
5f80: 20 6e 6f 74 20 62 65 20 75 73 65 64 20 69 6e 20 not be used in
5f90: 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 comparison opera
5fa0: 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 tions..*/.static
5fb0: 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 6f 6b 65 void renameToke
5fc0: 6e 43 68 65 63 6b 41 6c 6c 28 50 61 72 73 65 20 nCheckAll(Parse
5fd0: 2a 70 50 61 72 73 65 2c 20 76 6f 69 64 20 2a 70 *pParse, void *p
5fe0: 50 74 72 29 7b 0a 20 20 69 66 28 20 70 50 61 72 Ptr){. if( pPar
5ff0: 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 70 se->nErr==0 && p
6000: 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
6010: 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 cFailed==0 ){.
6020: 20 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a 70 RenameToken *p
6030: 3b 0a 20 20 20 20 75 38 20 69 20 3d 20 30 3b 0a ;. u8 i = 0;.
6040: 20 20 20 20 66 6f 72 28 70 3d 70 50 61 72 73 65 for(p=pParse
6050: 2d 3e 70 52 65 6e 61 6d 65 3b 20 70 3b 20 70 3d ->pRename; p; p=
6060: 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 p->pNext){.
6070: 20 69 66 28 20 70 2d 3e 70 20 29 7b 0a 20 20 20 if( p->p ){.
6080: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
6090: 70 21 3d 70 50 74 72 20 29 3b 0a 20 20 20 20 20 p!=pPtr );.
60a0: 20 20 20 69 20 2b 3d 20 2a 28 75 38 2a 29 28 70 i += *(u8*)(p
60b0: 2d 3e 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ->p);. }.
60c0: 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a }. }.}.#else.
60d0: 23 20 64 65 66 69 6e 65 20 72 65 6e 61 6d 65 54 # define renameT
60e0: 6f 6b 65 6e 43 68 65 63 6b 41 6c 6c 28 78 2c 79 okenCheckAll(x,y
60f0: 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
6100: 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68 Remember that th
6110: 65 20 70 61 72 73 65 72 20 74 72 65 65 20 65 6c e parser tree el
6120: 65 6d 65 6e 74 20 70 50 74 72 20 77 61 73 20 63 ement pPtr was c
6130: 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 reated using.**
6140: 74 68 65 20 74 6f 6b 65 6e 20 70 54 6f 6b 65 6e the token pToken
6150: 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 ..**.** In other
6160: 20 77 6f 72 64 73 2c 20 63 6f 6e 73 74 72 75 63 words, construc
6170: 74 20 61 20 6e 65 77 20 52 65 6e 61 6d 65 54 6f t a new RenameTo
6180: 6b 65 6e 20 6f 62 6a 65 63 74 20 61 6e 64 20 61 ken object and a
6190: 64 64 20 69 74 0a 2a 2a 20 74 6f 20 74 68 65 20 dd it.** to the
61a0: 6c 69 73 74 20 6f 66 20 52 65 6e 61 6d 65 54 6f list of RenameTo
61b0: 6b 65 6e 20 6f 62 6a 65 63 74 73 20 63 75 72 72 ken objects curr
61c0: 65 6e 74 6c 79 20 62 65 69 6e 67 20 62 75 69 6c ently being buil
61d0: 74 20 75 70 0a 2a 2a 20 69 6e 20 70 50 61 72 73 t up.** in pPars
61e0: 65 2d 3e 70 52 65 6e 61 6d 65 2e 0a 2a 2a 0a 2a e->pRename..**.*
61f0: 2a 20 54 68 65 20 70 50 74 72 20 61 72 67 75 6d * The pPtr argum
6200: 65 6e 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 ent is returned
6210: 73 6f 20 74 68 61 74 20 74 68 69 73 20 72 6f 75 so that this rou
6220: 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 tine can be used
6230: 0a 2a 2a 20 77 69 74 68 20 74 61 69 6c 20 72 65 .** with tail re
6240: 63 75 72 73 69 6f 6e 20 69 6e 20 74 6f 6b 65 6e cursion in token
6250: 45 78 70 72 28 29 20 72 6f 75 74 69 6e 65 2c 20 Expr() routine,
6260: 66 6f 72 20 61 20 73 6d 61 6c 6c 20 70 65 72 66 for a small perf
6270: 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6d 70 72 6f ormance.** impro
6280: 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 vement..*/.void
6290: 2a 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 54 6f *sqlite3RenameTo
62a0: 6b 65 6e 4d 61 70 28 50 61 72 73 65 20 2a 70 50 kenMap(Parse *pP
62b0: 61 72 73 65 2c 20 76 6f 69 64 20 2a 70 50 74 72 arse, void *pPtr
62c0: 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 , Token *pToken)
62d0: 7b 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 {. RenameToken
62e0: 2a 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74 28 *pNew;. assert(
62f0: 20 70 50 74 72 20 7c 7c 20 70 50 61 72 73 65 2d pPtr || pParse-
6300: 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
6310: 64 20 29 3b 0a 20 20 72 65 6e 61 6d 65 54 6f 6b d );. renameTok
6320: 65 6e 43 68 65 63 6b 41 6c 6c 28 70 50 61 72 73 enCheckAll(pPars
6330: 65 2c 20 70 50 74 72 29 3b 0a 20 20 70 4e 65 77 e, pPtr);. pNew
6340: 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
6350: 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 ocZero(pParse->d
6360: 62 2c 20 73 69 7a 65 6f 66 28 52 65 6e 61 6d 65 b, sizeof(Rename
6370: 54 6f 6b 65 6e 29 29 3b 0a 20 20 69 66 28 20 70 Token));. if( p
6380: 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d New ){. pNew-
6390: 3e 70 20 3d 20 70 50 74 72 3b 0a 20 20 20 20 70 >p = pPtr;. p
63a0: 4e 65 77 2d 3e 74 20 3d 20 2a 70 54 6f 6b 65 6e New->t = *pToken
63b0: 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 ;. pNew->pNex
63c0: 74 20 3d 20 70 50 61 72 73 65 2d 3e 70 52 65 6e t = pParse->pRen
63d0: 61 6d 65 3b 0a 20 20 20 20 70 50 61 72 73 65 2d ame;. pParse-
63e0: 3e 70 52 65 6e 61 6d 65 20 3d 20 70 4e 65 77 3b >pRename = pNew;
63f0: 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 . }.. return p
6400: 50 74 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 74 Ptr;.}../*.** It
6410: 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
6420: 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 there is alread
6430: 79 20 61 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 y a RenameToken
6440: 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 object associate
6450: 64 0a 2a 2a 20 77 69 74 68 20 70 61 72 73 65 20 d.** with parse
6460: 74 72 65 65 20 65 6c 65 6d 65 6e 74 20 70 46 72 tree element pFr
6470: 6f 6d 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f om. This functio
6480: 6e 20 72 65 6d 61 70 73 20 74 68 65 20 61 73 73 n remaps the ass
6490: 6f 63 69 61 74 65 64 20 74 6f 6b 65 6e 0a 2a 2a ociated token.**
64a0: 20 74 6f 20 70 61 72 73 65 20 74 72 65 65 20 65 to parse tree e
64b0: 6c 65 6d 65 6e 74 20 70 54 6f 2e 0a 2a 2f 0a 76 lement pTo..*/.v
64c0: 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6e 61 6d oid sqlite3Renam
64d0: 65 54 6f 6b 65 6e 52 65 6d 61 70 28 50 61 72 73 eTokenRemap(Pars
64e0: 65 20 2a 70 50 61 72 73 65 2c 20 76 6f 69 64 20 e *pParse, void
64f0: 2a 70 54 6f 2c 20 76 6f 69 64 20 2a 70 46 72 6f *pTo, void *pFro
6500: 6d 29 7b 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 m){. RenameToke
6510: 6e 20 2a 70 3b 0a 20 20 72 65 6e 61 6d 65 54 6f n *p;. renameTo
6520: 6b 65 6e 43 68 65 63 6b 41 6c 6c 28 70 50 61 72 kenCheckAll(pPar
6530: 73 65 2c 20 70 54 6f 29 3b 0a 20 20 66 6f 72 28 se, pTo);. for(
6540: 70 3d 70 50 61 72 73 65 2d 3e 70 52 65 6e 61 6d p=pParse->pRenam
6550: 65 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 e; p; p=p->pNext
6560: 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 3d ){. if( p->p=
6570: 3d 70 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 =pFrom ){.
6580: 70 2d 3e 70 20 3d 20 70 54 6f 3b 0a 20 20 20 20 p->p = pTo;.
6590: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
65a0: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b }.}../*.** Walk
65b0: 65 72 20 63 61 6c 6c 62 61 63 6b 20 75 73 65 64 er callback used
65c0: 20 62 79 20 73 71 6c 69 74 65 33 52 65 6e 61 6d by sqlite3Renam
65d0: 65 45 78 70 72 55 6e 6d 61 70 28 29 2e 0a 2a 2f eExprUnmap()..*/
65e0: 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6e 61 .static int rena
65f0: 6d 65 55 6e 6d 61 70 45 78 70 72 43 62 28 57 61 meUnmapExprCb(Wa
6600: 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 lker *pWalker, E
6610: 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 50 xpr *pExpr){. P
6620: 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 arse *pParse = p
6630: 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a Walker->pParse;.
6640: 20 20 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 54 sqlite3RenameT
6650: 6f 6b 65 6e 52 65 6d 61 70 28 70 50 61 72 73 65 okenRemap(pParse
6660: 2c 20 30 2c 20 28 76 6f 69 64 2a 29 70 45 78 70 , 0, (void*)pExp
6670: 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 r);. return WRC
6680: 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a _Continue;.}../*
6690: 0a 2a 2a 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 .** Walker callb
66a0: 61 63 6b 20 75 73 65 64 20 62 79 20 73 71 6c 69 ack used by sqli
66b0: 74 65 33 52 65 6e 61 6d 65 45 78 70 72 55 6e 6d te3RenameExprUnm
66c0: 61 70 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ap()..*/.static
66d0: 69 6e 74 20 72 65 6e 61 6d 65 55 6e 6d 61 70 53 int renameUnmapS
66e0: 65 6c 65 63 74 43 62 28 57 61 6c 6b 65 72 20 2a electCb(Walker *
66f0: 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 pWalker, Select
6700: 2a 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 *p){. Parse *pP
6710: 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e arse = pWalker->
6720: 70 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 3b pParse;. int i;
6730: 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d . if( ALWAYS(p-
6740: 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 >pEList) ){.
6750: 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 ExprList *pList
6760: 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 = p->pEList;.
6770: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 for(i=0; i<pLis
6780: 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a t->nExpr; i++){.
6790: 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d if( pList-
67a0: 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29 7b 0a 20 >a[i].zName ){.
67b0: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
67c0: 6e 61 6d 65 54 6f 6b 65 6e 52 65 6d 61 70 28 70 nameTokenRemap(p
67d0: 50 61 72 73 65 2c 20 30 2c 20 28 76 6f 69 64 2a Parse, 0, (void*
67e0: 29 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 )pList->a[i].zNa
67f0: 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 me);. }.
6800: 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 41 4c 57 }. }. if( ALW
6810: 41 59 53 28 70 2d 3e 70 53 72 63 29 20 29 7b 20 AYS(p->pSrc) ){
6820: 20 2f 2a 20 45 76 65 72 79 20 53 65 6c 65 63 74 /* Every Select
6830: 20 61 73 20 61 20 53 72 63 4c 69 73 74 2c 20 65 as a SrcList, e
6840: 76 65 6e 20 69 66 20 69 74 20 69 73 20 65 6d 70 ven if it is emp
6850: 74 79 20 2a 2f 0a 20 20 20 20 53 72 63 4c 69 73 ty */. SrcLis
6860: 74 20 2a 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 t *pSrc = p->pSr
6870: 63 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 c;. for(i=0;
6880: 69 3c 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b i<pSrc->nSrc; i+
6890: 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 +){. sqlite
68a0: 33 52 65 6e 61 6d 65 54 6f 6b 65 6e 52 65 6d 61 3RenameTokenRema
68b0: 70 28 70 50 61 72 73 65 2c 20 30 2c 20 28 76 6f p(pParse, 0, (vo
68c0: 69 64 2a 29 70 53 72 63 2d 3e 61 5b 69 5d 2e 7a id*)pSrc->a[i].z
68d0: 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Name);. }. }
68e0: 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f . return WRC_Co
68f0: 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ntinue;.}../*.**
6900: 20 52 65 6d 6f 76 65 20 61 6c 6c 20 6e 6f 64 65 Remove all node
6910: 73 20 74 68 61 74 20 61 72 65 20 70 61 72 74 20 s that are part
6920: 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 of expression pE
6930: 78 70 72 20 66 72 6f 6d 20 74 68 65 20 72 65 6e xpr from the ren
6940: 61 6d 65 20 6c 69 73 74 2e 0a 2a 2f 0a 76 6f 69 ame list..*/.voi
6950: 64 20 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 45 d sqlite3RenameE
6960: 78 70 72 55 6e 6d 61 70 28 50 61 72 73 65 20 2a xprUnmap(Parse *
6970: 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 pParse, Expr *pE
6980: 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 73 xpr){. Walker s
6990: 57 61 6c 6b 65 72 3b 0a 20 20 6d 65 6d 73 65 74 Walker;. memset
69a0: 28 26 73 57 61 6c 6b 65 72 2c 20 30 2c 20 73 69 (&sWalker, 0, si
69b0: 7a 65 6f 66 28 57 61 6c 6b 65 72 29 29 3b 0a 20 zeof(Walker));.
69c0: 20 73 57 61 6c 6b 65 72 2e 70 50 61 72 73 65 20 sWalker.pParse
69d0: 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 57 61 6c = pParse;. sWal
69e0: 6b 65 72 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 ker.xExprCallbac
69f0: 6b 20 3d 20 72 65 6e 61 6d 65 55 6e 6d 61 70 45 k = renameUnmapE
6a00: 78 70 72 43 62 3b 0a 20 20 73 57 61 6c 6b 65 72 xprCb;. sWalker
6a10: 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b .xSelectCallback
6a20: 20 3d 20 72 65 6e 61 6d 65 55 6e 6d 61 70 53 65 = renameUnmapSe
6a30: 6c 65 63 74 43 62 3b 0a 20 20 73 71 6c 69 74 65 lectCb;. sqlite
6a40: 33 57 61 6c 6b 45 78 70 72 28 26 73 57 61 6c 6b 3WalkExpr(&sWalk
6a50: 65 72 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f er, pExpr);.}../
6a60: 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 *.** Remove all
6a70: 6e 6f 64 65 73 20 74 68 61 74 20 61 72 65 20 70 nodes that are p
6a80: 61 72 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f art of expressio
6a90: 6e 2d 6c 69 73 74 20 70 45 4c 69 73 74 20 66 72 n-list pEList fr
6aa0: 6f 6d 20 74 68 65 20 0a 2a 2a 20 72 65 6e 61 6d om the .** renam
6ab0: 65 20 6c 69 73 74 2e 0a 2a 2f 0a 76 6f 69 64 20 e list..*/.void
6ac0: 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 45 78 70 sqlite3RenameExp
6ad0: 72 6c 69 73 74 55 6e 6d 61 70 28 50 61 72 73 65 rlistUnmap(Parse
6ae0: 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c 69 *pParse, ExprLi
6af0: 73 74 20 2a 70 45 4c 69 73 74 29 7b 0a 20 20 69 st *pEList){. i
6b00: 66 28 20 70 45 4c 69 73 74 20 29 7b 0a 20 20 20 f( pEList ){.
6b10: 20 69 6e 74 20 69 3b 0a 20 20 20 20 57 61 6c 6b int i;. Walk
6b20: 65 72 20 73 57 61 6c 6b 65 72 3b 0a 20 20 20 20 er sWalker;.
6b30: 6d 65 6d 73 65 74 28 26 73 57 61 6c 6b 65 72 2c memset(&sWalker,
6b40: 20 30 2c 20 73 69 7a 65 6f 66 28 57 61 6c 6b 65 0, sizeof(Walke
6b50: 72 29 29 3b 0a 20 20 20 20 73 57 61 6c 6b 65 72 r));. sWalker
6b60: 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
6b70: 3b 0a 20 20 20 20 73 57 61 6c 6b 65 72 2e 78 45 ;. sWalker.xE
6b80: 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 xprCallback = re
6b90: 6e 61 6d 65 55 6e 6d 61 70 45 78 70 72 43 62 3b nameUnmapExprCb;
6ba0: 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b . sqlite3Walk
6bb0: 45 78 70 72 4c 69 73 74 28 26 73 57 61 6c 6b 65 ExprList(&sWalke
6bc0: 72 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 r, pEList);.
6bd0: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 for(i=0; i<pELis
6be0: 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a t->nExpr; i++){.
6bf0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6e sqlite3Ren
6c00: 61 6d 65 54 6f 6b 65 6e 52 65 6d 61 70 28 70 50 ameTokenRemap(pP
6c10: 61 72 73 65 2c 20 30 2c 20 28 76 6f 69 64 2a 29 arse, 0, (void*)
6c20: 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 pEList->a[i].zNa
6c30: 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d me);. }. }.}
6c40: 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 ../*.** Free the
6c50: 20 6c 69 73 74 20 6f 66 20 52 65 6e 61 6d 65 54 list of RenameT
6c60: 6f 6b 65 6e 20 6f 62 6a 65 63 74 73 20 67 69 76 oken objects giv
6c70: 65 6e 20 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 en in the second
6c80: 20 61 72 67 75 6d 65 6e 74 0a 2a 2f 0a 73 74 61 argument.*/.sta
6c90: 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 tic void renameT
6ca0: 6f 6b 65 6e 46 72 65 65 28 73 71 6c 69 74 65 33 okenFree(sqlite3
6cb0: 20 2a 64 62 2c 20 52 65 6e 61 6d 65 54 6f 6b 65 *db, RenameToke
6cc0: 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 52 65 n *pToken){. Re
6cd0: 6e 61 6d 65 54 6f 6b 65 6e 20 2a 70 4e 65 78 74 nameToken *pNext
6ce0: 3b 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 ;. RenameToken
6cf0: 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 54 6f 6b *p;. for(p=pTok
6d00: 65 6e 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b en; p; p=pNext){
6d10: 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e . pNext = p->
6d20: 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 pNext;. sqlit
6d30: 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b e3DbFree(db, p);
6d40: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
6d50: 61 72 63 68 20 74 68 65 20 50 61 72 73 65 20 6f arch the Parse o
6d60: 62 6a 65 63 74 20 70 61 73 73 65 64 20 61 73 20 bject passed as
6d70: 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
6d80: 6e 74 20 66 6f 72 20 61 20 52 65 6e 61 6d 65 54 nt for a RenameT
6d90: 6f 6b 65 6e 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 oken.** object a
6da0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 ssociated with p
6db0: 61 72 73 65 20 74 72 65 65 20 65 6c 65 6d 65 6e arse tree elemen
6dc0: 74 20 70 50 74 72 2e 20 49 66 20 66 6f 75 6e 64 t pPtr. If found
6dd0: 2c 20 72 65 6d 6f 76 65 20 69 74 0a 2a 2a 20 66 , remove it.** f
6de0: 72 6f 6d 20 74 68 65 20 50 61 72 73 65 20 6f 62 rom the Parse ob
6df0: 6a 65 63 74 20 61 6e 64 20 61 64 64 20 69 74 20 ject and add it
6e00: 74 6f 20 74 68 65 20 6c 69 73 74 20 6d 61 69 6e to the list main
6e10: 74 61 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a tained by the.**
6e20: 20 52 65 6e 61 6d 65 43 74 78 20 6f 62 6a 65 63 RenameCtx objec
6e30: 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 t passed as the
6e40: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e second argument.
6e50: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
6e60: 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 renameTokenFind(
6e70: 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 73 Parse *pParse, s
6e80: 74 72 75 63 74 20 52 65 6e 61 6d 65 43 74 78 20 truct RenameCtx
6e90: 2a 70 43 74 78 2c 20 76 6f 69 64 20 2a 70 50 74 *pCtx, void *pPt
6ea0: 72 29 7b 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 r){. RenameToke
6eb0: 6e 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 n **pp;. assert
6ec0: 28 20 70 50 74 72 21 3d 30 20 29 3b 0a 20 20 66 ( pPtr!=0 );. f
6ed0: 6f 72 28 70 70 3d 26 70 50 61 72 73 65 2d 3e 70 or(pp=&pParse->p
6ee0: 52 65 6e 61 6d 65 3b 20 28 2a 70 70 29 3b 20 70 Rename; (*pp); p
6ef0: 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 p=&(*pp)->pNext)
6f00: 7b 0a 20 20 20 20 69 66 28 20 28 2a 70 70 29 2d {. if( (*pp)-
6f10: 3e 70 3d 3d 70 50 74 72 20 29 7b 0a 20 20 20 20 >p==pPtr ){.
6f20: 20 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a 70 RenameToken *p
6f30: 54 6f 6b 65 6e 20 3d 20 2a 70 70 3b 0a 20 20 20 Token = *pp;.
6f40: 20 20 20 2a 70 70 20 3d 20 70 54 6f 6b 65 6e 2d *pp = pToken-
6f50: 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 54 >pNext;. pT
6f60: 6f 6b 65 6e 2d 3e 70 4e 65 78 74 20 3d 20 70 43 oken->pNext = pC
6f70: 74 78 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20 tx->pList;.
6f80: 20 70 43 74 78 2d 3e 70 4c 69 73 74 20 3d 20 70 pCtx->pList = p
6f90: 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70 43 74 Token;. pCt
6fa0: 78 2d 3e 6e 4c 69 73 74 2b 2b 3b 0a 20 20 20 20 x->nList++;.
6fb0: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
6fc0: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 74 65 72 }.}../*.** Iter
6fd0: 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 20 ate through the
6fe0: 53 65 6c 65 63 74 20 6f 62 6a 65 63 74 73 20 74 Select objects t
6ff0: 68 61 74 20 61 72 65 20 70 61 72 74 20 6f 66 20 hat are part of
7000: 57 49 54 48 20 63 6c 61 75 73 65 73 20 61 74 74 WITH clauses att
7010: 61 63 68 65 64 0a 2a 2a 20 74 6f 20 73 65 6c 65 ached.** to sele
7020: 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70 53 65 ct statement pSe
7030: 6c 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lect..*/.static
7040: 76 6f 69 64 20 72 65 6e 61 6d 65 57 61 6c 6b 57 void renameWalkW
7050: 69 74 68 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c ith(Walker *pWal
7060: 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 ker, Select *pSe
7070: 6c 65 63 74 29 7b 0a 20 20 69 66 28 20 70 53 65 lect){. if( pSe
7080: 6c 65 63 74 2d 3e 70 57 69 74 68 20 29 7b 0a 20 lect->pWith ){.
7090: 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
70a0: 72 28 69 3d 30 3b 20 69 3c 70 53 65 6c 65 63 74 r(i=0; i<pSelect
70b0: 2d 3e 70 57 69 74 68 2d 3e 6e 43 74 65 3b 20 69 ->pWith->nCte; i
70c0: 2b 2b 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 ++){. Selec
70d0: 74 20 2a 70 20 3d 20 70 53 65 6c 65 63 74 2d 3e t *p = pSelect->
70e0: 70 57 69 74 68 2d 3e 61 5b 69 5d 2e 70 53 65 6c pWith->a[i].pSel
70f0: 65 63 74 3b 0a 20 20 20 20 20 20 4e 61 6d 65 43 ect;. NameC
7100: 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 ontext sNC;.
7110: 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 memset(&sNC, 0
7120: 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a , sizeof(sNC));.
7130: 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 sNC.pParse
7140: 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 = pWalker->pPar
7150: 73 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se;. sqlite
7160: 33 53 65 6c 65 63 74 50 72 65 70 28 73 4e 43 2e 3SelectPrep(sNC.
7170: 70 50 61 72 73 65 2c 20 70 2c 20 26 73 4e 43 29 pParse, p, &sNC)
7180: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 ;. sqlite3W
7190: 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 alkSelect(pWalke
71a0: 72 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d r, p);. }. }
71b0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 .}../*.** This i
71c0: 73 20 61 20 57 61 6c 6b 65 72 20 73 65 6c 65 63 s a Walker selec
71d0: 74 20 63 61 6c 6c 62 61 63 6b 2e 20 49 74 20 64 t callback. It d
71e0: 6f 65 73 20 6e 6f 74 68 69 6e 67 2e 20 49 74 20 oes nothing. It
71f0: 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 is only required
7200: 0a 2a 2a 20 62 65 63 61 75 73 65 20 77 69 74 68 .** because with
7210: 6f 75 74 20 61 20 64 75 6d 6d 79 20 63 61 6c 6c out a dummy call
7220: 62 61 63 6b 2c 20 73 71 6c 69 74 65 33 57 61 6c back, sqlite3Wal
7230: 6b 45 78 70 72 28 29 20 61 6e 64 20 73 69 6d 69 kExpr() and simi
7240: 6c 61 72 20 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65 lar do not.** de
7250: 73 63 65 6e 64 20 69 6e 74 6f 20 73 75 62 2d 73 scend into sub-s
7260: 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73 elect statements
7270: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
7280: 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 53 65 6c 65 renameColumnSele
7290: 63 74 43 62 28 57 61 6c 6b 65 72 20 2a 70 57 61 ctCb(Walker *pWa
72a0: 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 lker, Select *p)
72b0: 7b 0a 20 20 72 65 6e 61 6d 65 57 61 6c 6b 57 69 {. renameWalkWi
72c0: 74 68 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a th(pWalker, p);.
72d0: 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e return WRC_Con
72e0: 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tinue;.}../*.**
72f0: 54 68 69 73 20 69 73 20 61 20 57 61 6c 6b 65 72 This is a Walker
7300: 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6c 6c expression call
7310: 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 back..**.** For
7320: 65 76 65 72 79 20 54 4b 5f 43 4f 4c 55 4d 4e 20 every TK_COLUMN
7330: 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 node in the expr
7340: 65 73 73 69 6f 6e 20 74 72 65 65 2c 20 73 65 61 ession tree, sea
7350: 72 63 68 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66 rch to see.** if
7360: 20 74 68 65 20 63 6f 6c 75 6d 6e 20 62 65 69 6e the column bein
7370: 67 20 72 65 66 65 72 65 6e 63 65 73 20 69 73 20 g references is
7380: 74 68 65 20 63 6f 6c 75 6d 6e 20 62 65 69 6e 67 the column being
7390: 20 72 65 6e 61 6d 65 64 20 62 79 20 61 6e 0a 2a renamed by an.*
73a0: 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 73 74 * ALTER TABLE st
73b0: 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 atement. If it
73c0: 69 73 2c 20 74 68 65 6e 20 61 74 74 61 63 68 20 is, then attach
73d0: 69 74 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a its associated.*
73e0: 2a 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 6f 62 * RenameToken ob
73f0: 6a 65 63 74 20 74 6f 20 74 68 65 20 6c 69 73 74 ject to the list
7400: 20 6f 66 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 of RenameToken
7410: 6f 62 6a 65 63 74 73 20 62 65 69 6e 67 0a 2a 2a objects being.**
7420: 20 63 6f 6e 73 74 72 75 63 74 65 64 20 69 6e 20 constructed in
7430: 52 65 6e 61 6d 65 43 74 78 20 6f 62 6a 65 63 74 RenameCtx object
7440: 20 61 74 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 at pWalker->u.p
7450: 52 65 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 Rename..*/.stati
7460: 63 20 69 6e 74 20 72 65 6e 61 6d 65 43 6f 6c 75 c int renameColu
7470: 6d 6e 45 78 70 72 43 62 28 57 61 6c 6b 65 72 20 mnExprCb(Walker
7480: 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a *pWalker, Expr *
7490: 70 45 78 70 72 29 7b 0a 20 20 52 65 6e 61 6d 65 pExpr){. Rename
74a0: 43 74 78 20 2a 70 20 3d 20 70 57 61 6c 6b 65 72 Ctx *p = pWalker
74b0: 2d 3e 75 2e 70 52 65 6e 61 6d 65 3b 0a 20 20 69 ->u.pRename;. i
74c0: 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b f( pExpr->op==TK
74d0: 5f 54 52 49 47 47 45 52 20 0a 20 20 20 26 26 20 _TRIGGER . &&
74e0: 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d pExpr->iColumn==
74f0: 70 2d 3e 69 43 6f 6c 20 0a 20 20 20 26 26 20 70 p->iCol . && p
7500: 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 2d 3e Walker->pParse->
7510: 70 54 72 69 67 67 65 72 54 61 62 3d 3d 70 2d 3e pTriggerTab==p->
7520: 70 54 61 62 0a 20 20 29 7b 0a 20 20 20 20 72 65 pTab. ){. re
7530: 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 70 57 nameTokenFind(pW
7540: 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 2c 20 70 alker->pParse, p
7550: 2c 20 28 76 6f 69 64 2a 29 70 45 78 70 72 29 3b , (void*)pExpr);
7560: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 45 78 . }else if( pEx
7570: 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d pr->op==TK_COLUM
7580: 4e 20 0a 20 20 20 26 26 20 70 45 78 70 72 2d 3e N . && pExpr->
7590: 69 43 6f 6c 75 6d 6e 3d 3d 70 2d 3e 69 43 6f 6c iColumn==p->iCol
75a0: 20 0a 20 20 20 26 26 20 70 2d 3e 70 54 61 62 3d . && p->pTab=
75b0: 3d 70 45 78 70 72 2d 3e 79 2e 70 54 61 62 0a 20 =pExpr->y.pTab.
75c0: 20 29 7b 0a 20 20 20 20 72 65 6e 61 6d 65 54 6f ){. renameTo
75d0: 6b 65 6e 46 69 6e 64 28 70 57 61 6c 6b 65 72 2d kenFind(pWalker-
75e0: 3e 70 50 61 72 73 65 2c 20 70 2c 20 28 76 6f 69 >pParse, p, (voi
75f0: 64 2a 29 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20 d*)pExpr);. }.
7600: 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 return WRC_Cont
7610: 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 inue;.}../*.** T
7620: 68 65 20 52 65 6e 61 6d 65 43 74 78 20 63 6f 6e he RenameCtx con
7630: 74 61 69 6e 73 20 61 20 6c 69 73 74 20 6f 66 20 tains a list of
7640: 74 6f 6b 65 6e 73 20 74 68 61 74 20 72 65 66 65 tokens that refe
7650: 72 65 6e 63 65 20 61 20 63 6f 6c 75 6d 6e 20 74 rence a column t
7660: 68 61 74 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 hat.** is being
7670: 72 65 6e 61 6d 65 64 20 62 79 20 61 6e 20 41 4c renamed by an AL
7680: 54 45 52 20 54 41 42 4c 45 20 73 74 61 74 65 6d TER TABLE statem
7690: 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 ent. Return the
76a0: 20 22 6c 61 73 74 22 0a 2a 2a 20 52 65 6e 61 6d "last".** Renam
76b0: 65 54 6f 6b 65 6e 20 69 6e 20 74 68 65 20 52 65 eToken in the Re
76c0: 6e 61 6d 65 43 74 78 20 61 6e 64 20 72 65 6d 6f nameCtx and remo
76d0: 76 65 20 74 68 61 74 20 52 65 6e 61 6d 65 54 6f ve that RenameTo
76e0: 6b 65 6e 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 ken from the.**
76f0: 52 65 6e 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 RenameContext.
7700: 22 4c 61 73 74 22 20 6d 65 61 6e 73 20 74 68 65 "Last" means the
7710: 20 6c 61 73 74 20 52 65 6e 61 6d 65 54 6f 6b 65 last RenameToke
7720: 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 n encountered wh
7730: 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 en.** the input
7740: 53 51 4c 20 69 73 20 70 61 72 73 65 64 20 66 72 SQL is parsed fr
7750: 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 om left to right
7760: 2e 20 20 52 65 70 65 61 74 65 64 20 63 61 6c 6c . Repeated call
7770: 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e s to this routin
7780: 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61 6c 6c 20 e.** return all
7790: 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 74 6f 6b 65 column name toke
77a0: 6e 73 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 ns in the order
77b0: 74 68 61 74 20 74 68 65 79 20 61 72 65 20 65 6e that they are en
77c0: 63 6f 75 6e 74 65 72 65 64 0a 2a 2a 20 69 6e 20 countered.** in
77d0: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
77e0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 65 6e t..*/.static Ren
77f0: 61 6d 65 54 6f 6b 65 6e 20 2a 72 65 6e 61 6d 65 ameToken *rename
7800: 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 4e 65 78 74 28 ColumnTokenNext(
7810: 52 65 6e 61 6d 65 43 74 78 20 2a 70 43 74 78 29 RenameCtx *pCtx)
7820: 7b 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 {. RenameToken
7830: 2a 70 42 65 73 74 20 3d 20 70 43 74 78 2d 3e 70 *pBest = pCtx->p
7840: 4c 69 73 74 3b 0a 20 20 52 65 6e 61 6d 65 54 6f List;. RenameTo
7850: 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 52 ken *pToken;. R
7860: 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a 2a 70 70 3b enameToken **pp;
7870: 0a 0a 20 20 66 6f 72 28 70 54 6f 6b 65 6e 3d 70 .. for(pToken=p
7880: 42 65 73 74 2d 3e 70 4e 65 78 74 3b 20 70 54 6f Best->pNext; pTo
7890: 6b 65 6e 3b 20 70 54 6f 6b 65 6e 3d 70 54 6f 6b ken; pToken=pTok
78a0: 65 6e 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 en->pNext){.
78b0: 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 74 2e 7a 3e if( pToken->t.z>
78c0: 70 42 65 73 74 2d 3e 74 2e 7a 20 29 20 70 42 65 pBest->t.z ) pBe
78d0: 73 74 20 3d 20 70 54 6f 6b 65 6e 3b 0a 20 20 7d st = pToken;. }
78e0: 0a 20 20 66 6f 72 28 70 70 3d 26 70 43 74 78 2d . for(pp=&pCtx-
78f0: 3e 70 4c 69 73 74 3b 20 2a 70 70 21 3d 70 42 65 >pList; *pp!=pBe
7900: 73 74 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 st; pp=&(*pp)->p
7910: 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 70 Next);. *pp = p
7920: 42 65 73 74 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 Best->pNext;..
7930: 72 65 74 75 72 6e 20 70 42 65 73 74 3b 0a 7d 0a return pBest;.}.
7940: 0a 2f 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 ./*.** An error
7950: 6f 63 63 75 72 65 64 20 77 68 69 6c 65 20 70 61 occured while pa
7960: 72 73 69 6e 67 20 6f 72 20 6f 74 68 65 72 77 69 rsing or otherwi
7970: 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 61 20 se processing a
7980: 64 61 74 61 62 61 73 65 0a 2a 2a 20 6f 62 6a 65 database.** obje
7990: 63 74 20 28 65 69 74 68 65 72 20 70 50 61 72 73 ct (either pPars
79a0: 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20 70 4e e->pNewTable, pN
79b0: 65 77 49 6e 64 65 78 20 6f 72 20 70 4e 65 77 54 ewIndex or pNewT
79c0: 72 69 67 67 65 72 29 20 61 73 20 70 61 72 74 20 rigger) as part
79d0: 6f 66 20 61 6e 0a 2a 2a 20 41 4c 54 45 52 20 54 of an.** ALTER T
79e0: 41 42 4c 45 20 52 45 4e 41 4d 45 20 43 4f 4c 55 ABLE RENAME COLU
79f0: 4d 4e 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 20 MN program. The
7a00: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 6d error message em
7a10: 69 74 74 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 itted by the.**
7a20: 73 75 62 2d 72 6f 75 74 69 6e 65 20 69 73 20 63 sub-routine is c
7a30: 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 urrently stored
7a40: 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d in pParse->zErrM
7a50: 73 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f sg. This functio
7a60: 6e 0a 2a 2a 20 61 64 64 73 20 63 6f 6e 74 65 78 n.** adds contex
7a70: 74 20 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d t to the error m
7a80: 65 73 73 61 67 65 20 61 6e 64 20 74 68 65 6e 20 essage and then
7a90: 73 74 6f 72 65 73 20 69 74 20 69 6e 20 70 43 74 stores it in pCt
7aa0: 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 x..*/.static voi
7ab0: 64 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 50 61 d renameColumnPa
7ac0: 72 73 65 45 72 72 6f 72 28 0a 20 20 73 71 6c 69 rseError(. sqli
7ad0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
7ae0: 78 2c 20 0a 20 20 69 6e 74 20 62 50 6f 73 74 2c x, . int bPost,
7af0: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
7b00: 20 2a 70 54 79 70 65 2c 0a 20 20 73 71 6c 69 74 *pType,. sqlit
7b10: 65 33 5f 76 61 6c 75 65 20 2a 70 4f 62 6a 65 63 e3_value *pObjec
7b20: 74 2c 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 t,. Parse *pPar
7b30: 73 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 se.){. const ch
7b40: 61 72 20 2a 7a 54 20 3d 20 28 63 6f 6e 73 74 20 ar *zT = (const
7b50: 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 char*)sqlite3_va
7b60: 6c 75 65 5f 74 65 78 74 28 70 54 79 70 65 29 3b lue_text(pType);
7b70: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
7b80: 4e 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a N = (const char*
7b90: 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
7ba0: 65 78 74 28 70 4f 62 6a 65 63 74 29 3b 0a 20 20 ext(pObject);.
7bb0: 63 68 61 72 20 2a 7a 45 72 72 3b 0a 0a 20 20 7a char *zErr;.. z
7bc0: 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 Err = sqlite3_mp
7bd0: 72 69 6e 74 66 28 22 65 72 72 6f 72 20 69 6e 20 rintf("error in
7be0: 25 73 20 25 73 25 73 3a 20 25 73 22 2c 20 0a 20 %s %s%s: %s", .
7bf0: 20 20 20 20 20 7a 54 2c 20 7a 4e 2c 20 28 62 50 zT, zN, (bP
7c00: 6f 73 74 20 3f 20 22 20 61 66 74 65 72 20 72 65 ost ? " after re
7c10: 6e 61 6d 65 22 20 3a 20 22 22 29 2c 0a 20 20 20 name" : ""),.
7c20: 20 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d pParse->zErrM
7c30: 73 67 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 sg. );. sqlite
7c40: 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 3_result_error(p
7c50: 43 74 78 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a Ctx, zErr, -1);.
7c60: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
7c70: 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 Err);.}../*.** F
7c80: 6f 72 20 65 61 63 68 20 6e 61 6d 65 20 69 6e 20 or each name in
7c90: 74 68 65 20 74 68 65 20 65 78 70 72 65 73 73 69 the the expressi
7ca0: 6f 6e 2d 6c 69 73 74 20 70 45 4c 69 73 74 20 28 on-list pEList (
7cb0: 69 2e 65 2e 20 65 61 63 68 0a 2a 2a 20 70 45 4c i.e. each.** pEL
7cc0: 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 ist->a[i].zName)
7cd0: 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 that matches th
7ce0: 65 20 73 74 72 69 6e 67 20 69 6e 20 7a 4f 6c 64 e string in zOld
7cf0: 2c 20 65 78 74 72 61 63 74 20 74 68 65 20 0a 2a , extract the .*
7d00: 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 * corresponding
7d10: 72 65 6e 61 6d 65 2d 74 6f 6b 65 6e 20 66 72 6f rename-token fro
7d20: 6d 20 50 61 72 73 65 20 6f 62 6a 65 63 74 20 70 m Parse object p
7d30: 50 61 72 73 65 20 61 6e 64 20 61 64 64 20 69 74 Parse and add it
7d40: 0a 2a 2a 20 74 6f 20 74 68 65 20 52 65 6e 61 6d .** to the Renam
7d50: 65 43 74 78 20 70 43 74 78 2e 0a 2a 2f 0a 73 74 eCtx pCtx..*/.st
7d60: 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 atic void rename
7d70: 43 6f 6c 75 6d 6e 45 6c 69 73 74 4e 61 6d 65 73 ColumnElistNames
7d80: 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
7d90: 65 2c 20 0a 20 20 52 65 6e 61 6d 65 43 74 78 20 e, . RenameCtx
7da0: 2a 70 43 74 78 2c 20 0a 20 20 45 78 70 72 4c 69 *pCtx, . ExprLi
7db0: 73 74 20 2a 70 45 4c 69 73 74 2c 20 0a 20 20 63 st *pEList, . c
7dc0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 6c 64 0a onst char *zOld.
7dd0: 29 7b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 ){. if( pEList
7de0: 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
7df0: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c for(i=0; i<pEL
7e00: 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 ist->nExpr; i++)
7e10: 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e {. char *zN
7e20: 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b ame = pEList->a[
7e30: 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 i].zName;.
7e40: 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 if( 0==sqlite3_s
7e50: 74 72 69 63 6d 70 28 7a 4e 61 6d 65 2c 20 7a 4f tricmp(zName, zO
7e60: 6c 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ld) ){. r
7e70: 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 70 enameTokenFind(p
7e80: 50 61 72 73 65 2c 20 70 43 74 78 2c 20 28 76 6f Parse, pCtx, (vo
7e90: 69 64 2a 29 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 id*)zName);.
7ea0: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
7eb0: 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 ./*.** For each
7ec0: 6e 61 6d 65 20 69 6e 20 74 68 65 20 74 68 65 20 name in the the
7ed0: 69 64 2d 6c 69 73 74 20 70 49 64 4c 69 73 74 20 id-list pIdList
7ee0: 28 69 2e 65 2e 20 65 61 63 68 20 70 49 64 4c 69 (i.e. each pIdLi
7ef0: 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 st->a[i].zName)
7f00: 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 .** that matches
7f10: 20 74 68 65 20 73 74 72 69 6e 67 20 69 6e 20 7a the string in z
7f20: 4f 6c 64 2c 20 65 78 74 72 61 63 74 20 74 68 65 Old, extract the
7f30: 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 corresponding r
7f40: 65 6e 61 6d 65 2d 74 6f 6b 65 6e 20 0a 2a 2a 20 ename-token .**
7f50: 66 72 6f 6d 20 50 61 72 73 65 20 6f 62 6a 65 63 from Parse objec
7f60: 74 20 70 50 61 72 73 65 20 61 6e 64 20 61 64 64 t pParse and add
7f70: 20 69 74 20 74 6f 20 74 68 65 20 52 65 6e 61 6d it to the Renam
7f80: 65 43 74 78 20 70 43 74 78 2e 0a 2a 2f 0a 73 74 eCtx pCtx..*/.st
7f90: 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 atic void rename
7fa0: 43 6f 6c 75 6d 6e 49 64 6c 69 73 74 4e 61 6d 65 ColumnIdlistName
7fb0: 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 s(. Parse *pPar
7fc0: 73 65 2c 20 0a 20 20 52 65 6e 61 6d 65 43 74 78 se, . RenameCtx
7fd0: 20 2a 70 43 74 78 2c 20 0a 20 20 49 64 4c 69 73 *pCtx, . IdLis
7fe0: 74 20 2a 70 49 64 4c 69 73 74 2c 20 0a 20 20 63 t *pIdList, . c
7ff0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 6c 64 0a onst char *zOld.
8000: 29 7b 0a 20 20 69 66 28 20 70 49 64 4c 69 73 74 ){. if( pIdList
8010: 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
8020: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 for(i=0; i<pI
8030: 64 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 dList->nId; i++)
8040: 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e {. char *zN
8050: 61 6d 65 20 3d 20 70 49 64 4c 69 73 74 2d 3e 61 ame = pIdList->a
8060: 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 [i].zName;.
8070: 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 5f if( 0==sqlite3_
8080: 73 74 72 69 63 6d 70 28 7a 4e 61 6d 65 2c 20 7a stricmp(zName, z
8090: 4f 6c 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 Old) ){.
80a0: 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 renameTokenFind(
80b0: 70 50 61 72 73 65 2c 20 70 43 74 78 2c 20 28 76 pParse, pCtx, (v
80c0: 6f 69 64 2a 29 7a 4e 61 6d 65 29 3b 0a 20 20 20 oid*)zName);.
80d0: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d }. }. }.}
80e0: 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 68 ../*.** Parse th
80f0: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
8100: 7a 53 71 6c 20 75 73 69 6e 67 20 50 61 72 73 65 zSql using Parse
8110: 20 6f 62 6a 65 63 74 20 28 2a 70 29 2e 20 54 68 object (*p). Th
8120: 65 20 50 61 72 73 65 20 6f 62 6a 65 63 74 0a 2a e Parse object.*
8130: 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 * is initialized
8140: 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
8150: 6e 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 75 n before it is u
8160: 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 sed..*/.static i
8170: 6e 74 20 72 65 6e 61 6d 65 50 61 72 73 65 53 71 nt renameParseSq
8180: 6c 28 0a 20 20 50 61 72 73 65 20 2a 70 2c 20 20 l(. Parse *p,
8190: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
81a0: 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 74 /* Memory t
81b0: 6f 20 75 73 65 20 66 6f 72 20 50 61 72 73 65 20 o use for Parse
81c0: 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 object */. cons
81d0: 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 t char *zDb,
81e0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
81f0: 61 6d 65 20 6f 66 20 73 63 68 65 6d 61 20 53 51 ame of schema SQ
8200: 4c 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a L belongs to */.
8210: 20 20 69 6e 74 20 62 54 61 62 6c 65 2c 20 20 20 int bTable,
8220: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8230: 20 20 2f 2a 20 31 20 2d 3e 20 52 45 4e 41 4d 45 /* 1 -> RENAME
8240: 20 54 41 42 4c 45 2c 20 30 20 2d 3e 20 52 45 4e TABLE, 0 -> REN
8250: 41 4d 45 20 43 4f 4c 55 4d 4e 20 2a 2f 0a 20 20 AME COLUMN */.
8260: 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
8270: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8280: 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 /* Database hand
8290: 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 le */. const ch
82a0: 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 ar *zSql,
82b0: 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 /* SQL t
82c0: 6f 20 70 61 72 73 65 20 2a 2f 0a 20 20 69 6e 74 o parse */. int
82d0: 20 62 54 65 6d 70 20 20 20 20 20 20 20 20 20 20 bTemp
82e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
82f0: 54 72 75 65 20 69 66 20 53 51 4c 20 69 73 20 66 True if SQL is f
8300: 72 6f 6d 20 74 65 6d 70 20 73 63 68 65 6d 61 20 rom temp schema
8310: 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
8320: 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 char *zErr = 0
8330: 3b 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 69 44 ;.. db->init.iD
8340: 62 20 3d 20 62 54 65 6d 70 20 3f 20 31 20 3a 20 b = bTemp ? 1 :
8350: 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d sqlite3FindDbNam
8360: 65 28 64 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 2f e(db, zDb);.. /
8370: 2a 20 50 61 72 73 65 20 74 68 65 20 53 51 4c 20 * Parse the SQL
8380: 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 statement passed
8390: 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
83a0: 67 75 6d 65 6e 74 2e 20 49 66 20 6e 6f 20 65 72 gument. If no er
83b0: 72 6f 72 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20 ror. ** occurs
83c0: 61 6e 64 20 74 68 65 20 70 61 72 73 65 20 64 6f and the parse do
83d0: 65 73 20 6e 6f 74 20 72 65 73 75 6c 74 20 69 6e es not result in
83e0: 20 61 20 6e 65 77 20 74 61 62 6c 65 2c 20 69 6e a new table, in
83f0: 64 65 78 20 6f 72 0a 20 20 2a 2a 20 74 72 69 67 dex or. ** trig
8400: 67 65 72 20 6f 62 6a 65 63 74 2c 20 74 68 65 20 ger object, the
8410: 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 database must be
8420: 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 6d corrupt. */. m
8430: 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 emset(p, 0, size
8440: 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20 70 2d of(Parse));. p-
8450: 3e 65 50 61 72 73 65 4d 6f 64 65 20 3d 20 28 62 >eParseMode = (b
8460: 54 61 62 6c 65 20 3f 20 50 41 52 53 45 5f 4d 4f Table ? PARSE_MO
8470: 44 45 5f 52 45 4e 41 4d 45 5f 54 41 42 4c 45 20 DE_RENAME_TABLE
8480: 3a 20 50 41 52 53 45 5f 4d 4f 44 45 5f 52 45 4e : PARSE_MODE_REN
8490: 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 20 20 70 AME_COLUMN);. p
84a0: 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e ->db = db;. p->
84b0: 6e 51 75 65 72 79 4c 6f 6f 70 20 3d 20 31 3b 0a nQueryLoop = 1;.
84c0: 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 75 rc = sqlite3Ru
84d0: 6e 50 61 72 73 65 72 28 70 2c 20 7a 53 71 6c 2c nParser(p, zSql,
84e0: 20 26 7a 45 72 72 29 3b 0a 20 20 61 73 73 65 72 &zErr);. asser
84f0: 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 t( p->zErrMsg==0
8500: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 );. assert( rc
8510: 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 7a !=SQLITE_OK || z
8520: 45 72 72 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 7a Err==0 );. p->z
8530: 45 72 72 4d 73 67 20 3d 20 7a 45 72 72 3b 0a 20 ErrMsg = zErr;.
8540: 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
8550: 61 69 6c 65 64 20 29 20 72 63 20 3d 20 53 51 4c ailed ) rc = SQL
8560: 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 ITE_NOMEM;. if(
8570: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 0a rc==SQLITE_OK .
8580: 20 20 20 26 26 20 70 2d 3e 70 4e 65 77 54 61 62 && p->pNewTab
8590: 6c 65 3d 3d 30 20 26 26 20 70 2d 3e 70 4e 65 77 le==0 && p->pNew
85a0: 49 6e 64 65 78 3d 3d 30 20 26 26 20 70 2d 3e 70 Index==0 && p->p
85b0: 4e 65 77 54 72 69 67 67 65 72 3d 3d 30 20 0a 20 NewTrigger==0 .
85c0: 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
85d0: 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
85e0: 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 ;. }..#ifdef SQ
85f0: 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 LITE_DEBUG. /*
8600: 45 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 Ensure that all
8610: 6d 61 70 70 69 6e 67 73 20 69 6e 20 74 68 65 20 mappings in the
8620: 50 61 72 73 65 2e 70 52 65 6e 61 6d 65 20 6c 69 Parse.pRename li
8630: 73 74 20 72 65 61 6c 6c 79 20 64 6f 20 6d 61 70 st really do map
8640: 20 74 6f 0a 20 20 2a 2a 20 61 20 70 61 72 74 20 to. ** a part
8650: 6f 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 of the input str
8660: 69 6e 67 2e 20 20 2a 2f 0a 20 20 69 66 28 20 72 ing. */. if( r
8670: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
8680: 20 20 20 20 69 6e 74 20 6e 53 71 6c 20 3d 20 73 int nSql = s
8690: 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
86a0: 53 71 6c 29 3b 0a 20 20 20 20 52 65 6e 61 6d 65 Sql);. Rename
86b0: 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 0a 20 Token *pToken;.
86c0: 20 20 20 66 6f 72 28 70 54 6f 6b 65 6e 3d 70 2d for(pToken=p-
86d0: 3e 70 52 65 6e 61 6d 65 3b 20 70 54 6f 6b 65 6e >pRename; pToken
86e0: 3b 20 70 54 6f 6b 65 6e 3d 70 54 6f 6b 65 6e 2d ; pToken=pToken-
86f0: 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 >pNext){. a
8700: 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 74 ssert( pToken->t
8710: 2e 7a 3e 3d 7a 53 71 6c 20 26 26 20 26 70 54 6f .z>=zSql && &pTo
8720: 6b 65 6e 2d 3e 74 2e 7a 5b 70 54 6f 6b 65 6e 2d ken->t.z[pToken-
8730: 3e 74 2e 6e 5d 3c 3d 26 7a 53 71 6c 5b 6e 53 71 >t.n]<=&zSql[nSq
8740: 6c 5d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a l] );. }. }.
8750: 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 69 6e #endif.. db->in
8760: 69 74 2e 69 44 62 20 3d 20 30 3b 0a 20 20 72 65 it.iDb = 0;. re
8770: 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
8780: 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
8790: 65 64 69 74 73 20 53 51 4c 20 73 74 61 74 65 6d edits SQL statem
87a0: 65 6e 74 20 7a 53 71 6c 2c 20 72 65 70 6c 61 63 ent zSql, replac
87b0: 69 6e 67 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 ing each token i
87c0: 64 65 6e 74 69 66 69 65 64 0a 2a 2a 20 62 79 20 dentified.** by
87d0: 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 the linked list
87e0: 70 52 65 6e 61 6d 65 20 77 69 74 68 20 74 68 65 pRename with the
87f0: 20 74 65 78 74 20 6f 66 20 7a 4e 65 77 2e 20 49 text of zNew. I
8800: 66 20 61 72 67 75 6d 65 6e 74 20 62 51 75 6f 74 f argument bQuot
8810: 65 20 69 73 0a 2a 2a 20 74 72 75 65 2c 20 74 68 e is.** true, th
8820: 65 6e 20 7a 4e 65 77 20 69 73 20 61 6c 77 61 79 en zNew is alway
8830: 73 20 71 75 6f 74 65 64 20 66 69 72 73 74 2e 20 s quoted first.
8840: 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 If no error occu
8850: 72 73 2c 20 74 68 65 20 72 65 73 75 6c 74 0a 2a rs, the result.*
8860: 2a 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f * is loaded into
8870: 20 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 20 context object
8880: 70 43 74 78 20 61 73 20 74 68 65 20 72 65 73 75 pCtx as the resu
8890: 6c 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 66 lt..**.** Or, if
88a0: 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
88b0: 20 28 69 2e 65 2e 20 61 6e 20 4f 4f 4d 20 63 6f (i.e. an OOM co
88c0: 6e 64 69 74 69 6f 6e 29 2c 20 61 6e 20 65 72 72 ndition), an err
88d0: 6f 72 20 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a or is left in.**
88e0: 20 70 43 74 78 20 61 6e 64 20 61 6e 20 53 51 4c pCtx and an SQL
88f0: 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 ite error code r
8900: 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
8910: 69 63 20 69 6e 74 20 72 65 6e 61 6d 65 45 64 69 ic int renameEdi
8920: 74 53 71 6c 28 0a 20 20 73 71 6c 69 74 65 33 5f tSql(. sqlite3_
8930: 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 context *pCtx,
8940: 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
8950: 6e 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f n result here */
8960: 0a 20 20 52 65 6e 61 6d 65 43 74 78 20 2a 70 52 . RenameCtx *pR
8970: 65 6e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 ename,
8980: 20 20 20 2f 2a 20 52 65 6e 61 6d 65 20 63 6f 6e /* Rename con
8990: 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 text */. const
89a0: 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 char *zSql,
89b0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c /* SQL
89c0: 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 64 statement to ed
89d0: 69 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 it */. const ch
89e0: 61 72 20 2a 7a 4e 65 77 2c 20 20 20 20 20 20 20 ar *zNew,
89f0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 74 /* New t
8a00: 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69 oken text */. i
8a10: 6e 74 20 62 51 75 6f 74 65 20 20 20 20 20 20 20 nt bQuote
8a20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
8a30: 2a 20 54 72 75 65 20 74 6f 20 61 6c 77 61 79 73 * True to always
8a40: 20 71 75 6f 74 65 20 74 6f 6b 65 6e 20 2a 2f 0a quote token */.
8a50: 29 7b 0a 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 ){. int nNew =
8a60: 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
8a70: 7a 4e 65 77 29 3b 0a 20 20 69 6e 74 20 6e 53 71 zNew);. int nSq
8a80: 6c 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 l = sqlite3Strle
8a90: 6e 33 30 28 7a 53 71 6c 29 3b 0a 20 20 73 71 6c n30(zSql);. sql
8aa0: 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 ite3 *db = sqlit
8ab0: 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
8ac0: 6e 64 6c 65 28 70 43 74 78 29 3b 0a 20 20 69 6e ndle(pCtx);. in
8ad0: 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
8ae0: 3b 0a 20 20 63 68 61 72 20 2a 7a 51 75 6f 74 3b ;. char *zQuot;
8af0: 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 . char *zOut;.
8b00: 20 69 6e 74 20 6e 51 75 6f 74 3b 0a 0a 20 20 2f int nQuot;.. /
8b10: 2a 20 53 65 74 20 7a 51 75 6f 74 20 74 6f 20 70 * Set zQuot to p
8b20: 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 oint to a buffer
8b30: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 71 75 containing a qu
8b40: 6f 74 65 64 20 63 6f 70 79 20 6f 66 20 74 68 65 oted copy of the
8b50: 20 0a 20 20 2a 2a 20 69 64 65 6e 74 69 66 69 65 . ** identifie
8b60: 72 20 7a 4e 65 77 2e 20 49 66 20 74 68 65 20 63 r zNew. If the c
8b70: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 64 65 orresponding ide
8b80: 6e 74 69 66 69 65 72 20 69 6e 20 74 68 65 20 6f ntifier in the o
8b90: 72 69 67 69 6e 61 6c 20 0a 20 20 2a 2a 20 41 4c riginal . ** AL
8ba0: 54 45 52 20 54 41 42 4c 45 20 73 74 61 74 65 6d TER TABLE statem
8bb0: 65 6e 74 20 77 61 73 20 71 75 6f 74 65 64 20 28 ent was quoted (
8bc0: 62 51 75 6f 74 65 3d 3d 31 29 2c 20 74 68 65 6e bQuote==1), then
8bd0: 20 73 65 74 20 7a 4e 65 77 20 74 6f 0a 20 20 2a set zNew to. *
8be0: 2a 20 70 6f 69 6e 74 20 74 6f 20 7a 51 75 6f 74 * point to zQuot
8bf0: 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 73 75 62 so that all sub
8c00: 73 74 69 74 75 74 69 6f 6e 73 20 61 72 65 20 6d stitutions are m
8c10: 61 64 65 20 75 73 69 6e 67 20 74 68 65 0a 20 20 ade using the.
8c20: 2a 2a 20 71 75 6f 74 65 64 20 76 65 72 73 69 6f ** quoted versio
8c30: 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c n of the new col
8c40: 75 6d 6e 20 6e 61 6d 65 2e 20 20 2a 2f 0a 20 20 umn name. */.
8c50: 7a 51 75 6f 74 20 3d 20 73 71 6c 69 74 65 33 4d zQuot = sqlite3M
8c60: 50 72 69 6e 74 66 28 64 62 2c 20 22 5c 22 25 77 Printf(db, "\"%w
8c70: 5c 22 22 2c 20 7a 4e 65 77 29 3b 0a 20 20 69 66 \"", zNew);. if
8c80: 28 20 7a 51 75 6f 74 3d 3d 30 20 29 7b 0a 20 20 ( zQuot==0 ){.
8c90: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
8ca0: 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a NOMEM;. }else{.
8cb0: 20 20 20 20 6e 51 75 6f 74 20 3d 20 73 71 6c 69 nQuot = sqli
8cc0: 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 51 75 6f te3Strlen30(zQuo
8cd0: 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 62 51 t);. }. if( bQ
8ce0: 75 6f 74 65 20 29 7b 0a 20 20 20 20 7a 4e 65 77 uote ){. zNew
8cf0: 20 3d 20 7a 51 75 6f 74 3b 0a 20 20 20 20 6e 4e = zQuot;. nN
8d00: 65 77 20 3d 20 6e 51 75 6f 74 3b 0a 20 20 7d 0a ew = nQuot;. }.
8d10: 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f . /* At this po
8d20: 69 6e 74 20 70 52 65 6e 61 6d 65 2d 3e 70 4c 69 int pRename->pLi
8d30: 73 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 6c 69 st contains a li
8d40: 73 74 20 6f 66 20 52 65 6e 61 6d 65 54 6f 6b 65 st of RenameToke
8d50: 6e 20 6f 62 6a 65 63 74 73 0a 20 20 2a 2a 20 63 n objects. ** c
8d60: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
8d70: 61 6c 6c 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 all tokens in th
8d80: 65 20 69 6e 70 75 74 20 53 51 4c 20 74 68 61 74 e input SQL that
8d90: 20 6d 75 73 74 20 62 65 20 72 65 70 6c 61 63 65 must be replace
8da0: 64 0a 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 d. ** with the
8db0: 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e new column name.
8dc0: 20 41 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e All that remain
8dd0: 73 20 69 73 20 74 6f 20 63 6f 6e 73 74 72 75 63 s is to construc
8de0: 74 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 t and. ** retur
8df0: 6e 20 74 68 65 20 65 64 69 74 65 64 20 53 51 4c n the edited SQL
8e00: 20 73 74 72 69 6e 67 2e 20 2a 2f 0a 20 20 61 73 string. */. as
8e10: 73 65 72 74 28 20 6e 51 75 6f 74 3e 3d 6e 4e 65 sert( nQuot>=nNe
8e20: 77 20 29 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 w );. zOut = sq
8e30: 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
8e40: 6f 28 64 62 2c 20 6e 53 71 6c 20 2b 20 70 52 65 o(db, nSql + pRe
8e50: 6e 61 6d 65 2d 3e 6e 4c 69 73 74 2a 6e 51 75 6f name->nList*nQuo
8e60: 74 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 7a 4f t + 1);. if( zO
8e70: 75 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 4f ut ){. int nO
8e80: 75 74 20 3d 20 6e 53 71 6c 3b 0a 20 20 20 20 6d ut = nSql;. m
8e90: 65 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 53 71 6c emcpy(zOut, zSql
8ea0: 2c 20 6e 53 71 6c 29 3b 0a 20 20 20 20 77 68 69 , nSql);. whi
8eb0: 6c 65 28 20 70 52 65 6e 61 6d 65 2d 3e 70 4c 69 le( pRename->pLi
8ec0: 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 st ){. int
8ed0: 69 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 iOff;
8ee0: 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
8ef0: 74 20 6f 66 20 74 6f 6b 65 6e 20 74 6f 20 72 65 t of token to re
8f00: 70 6c 61 63 65 20 69 6e 20 7a 4f 75 74 20 2a 2f place in zOut */
8f10: 0a 20 20 20 20 20 20 52 65 6e 61 6d 65 54 6f 6b . RenameTok
8f20: 65 6e 20 2a 70 42 65 73 74 20 3d 20 72 65 6e 61 en *pBest = rena
8f30: 6d 65 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 4e 65 78 meColumnTokenNex
8f40: 74 28 70 52 65 6e 61 6d 65 29 3b 0a 0a 20 20 20 t(pRename);..
8f50: 20 20 20 75 33 32 20 6e 52 65 70 6c 61 63 65 3b u32 nReplace;
8f60: 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
8f70: 72 20 2a 7a 52 65 70 6c 61 63 65 3b 0a 20 20 20 r *zReplace;.
8f80: 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 if( sqlite3Is
8f90: 49 64 43 68 61 72 28 2a 70 42 65 73 74 2d 3e 74 IdChar(*pBest->t
8fa0: 2e 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6e .z) ){. n
8fb0: 52 65 70 6c 61 63 65 20 3d 20 6e 4e 65 77 3b 0a Replace = nNew;.
8fc0: 20 20 20 20 20 20 20 20 7a 52 65 70 6c 61 63 65 zReplace
8fd0: 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d = zNew;. }
8fe0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 52 else{. nR
8ff0: 65 70 6c 61 63 65 20 3d 20 6e 51 75 6f 74 3b 0a eplace = nQuot;.
9000: 20 20 20 20 20 20 20 20 7a 52 65 70 6c 61 63 65 zReplace
9010: 20 3d 20 7a 51 75 6f 74 3b 0a 20 20 20 20 20 20 = zQuot;.
9020: 7d 0a 0a 20 20 20 20 20 20 69 4f 66 66 20 3d 20 }.. iOff =
9030: 70 42 65 73 74 2d 3e 74 2e 7a 20 2d 20 7a 53 71 pBest->t.z - zSq
9040: 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 65 l;. if( pBe
9050: 73 74 2d 3e 74 2e 6e 21 3d 6e 52 65 70 6c 61 63 st->t.n!=nReplac
9060: 65 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d e ){. mem
9070: 6d 6f 76 65 28 26 7a 4f 75 74 5b 69 4f 66 66 20 move(&zOut[iOff
9080: 2b 20 6e 52 65 70 6c 61 63 65 5d 2c 20 26 7a 4f + nReplace], &zO
9090: 75 74 5b 69 4f 66 66 20 2b 20 70 42 65 73 74 2d ut[iOff + pBest-
90a0: 3e 74 2e 6e 5d 2c 20 0a 20 20 20 20 20 20 20 20 >t.n], .
90b0: 20 20 20 20 6e 4f 75 74 20 2d 20 28 69 4f 66 66 nOut - (iOff
90c0: 20 2b 20 70 42 65 73 74 2d 3e 74 2e 6e 29 0a 20 + pBest->t.n).
90d0: 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
90e0: 20 20 6e 4f 75 74 20 2b 3d 20 6e 52 65 70 6c 61 nOut += nRepla
90f0: 63 65 20 2d 20 70 42 65 73 74 2d 3e 74 2e 6e 3b ce - pBest->t.n;
9100: 0a 20 20 20 20 20 20 20 20 7a 4f 75 74 5b 6e 4f . zOut[nO
9110: 75 74 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 ut] = '\0';.
9120: 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 }. memcpy
9130: 28 26 7a 4f 75 74 5b 69 4f 66 66 5d 2c 20 7a 52 (&zOut[iOff], zR
9140: 65 70 6c 61 63 65 2c 20 6e 52 65 70 6c 61 63 65 eplace, nReplace
9150: 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
9160: 44 62 46 72 65 65 28 64 62 2c 20 70 42 65 73 74 DbFree(db, pBest
9170: 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 );. }.. sq
9180: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
9190: 74 28 70 43 74 78 2c 20 7a 4f 75 74 2c 20 2d 31 t(pCtx, zOut, -1
91a0: 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
91b0: 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 NT);. sqlite3
91c0: 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 75 74 29 DbFree(db, zOut)
91d0: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
91e0: 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
91f0: 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 ;. }.. sqlite3
9200: 5f 66 72 65 65 28 7a 51 75 6f 74 29 3b 0a 20 20 _free(zQuot);.
9210: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
9220: 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 .** Resolve all
9230: 73 79 6d 62 6f 6c 73 20 69 6e 20 74 68 65 20 74 symbols in the t
9240: 72 69 67 67 65 72 20 61 74 20 70 50 61 72 73 65 rigger at pParse
9250: 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 2c 20 61 ->pNewTrigger, a
9260: 73 73 75 6d 69 6e 67 0a 2a 2a 20 69 74 20 77 61 ssuming.** it wa
9270: 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 s read from the
9280: 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61 schema of databa
9290: 73 65 20 7a 44 62 2e 20 52 65 74 75 72 6e 20 53 se zDb. Return S
92a0: 51 4c 49 54 45 5f 4f 4b 20 69 66 20 0a 2a 2a 20 QLITE_OK if .**
92b0: 73 75 63 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 successful. Othe
92c0: 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e rwise, return an
92d0: 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f SQLite error co
92e0: 64 65 20 61 6e 64 20 6c 65 61 76 65 20 61 6e 20 de and leave an
92f0: 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 error.** message
9300: 20 69 6e 20 74 68 65 20 50 61 72 73 65 20 6f 62 in the Parse ob
9310: 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ject..*/.static
9320: 69 6e 74 20 72 65 6e 61 6d 65 52 65 73 6f 6c 76 int renameResolv
9330: 65 54 72 69 67 67 65 72 28 50 61 72 73 65 20 2a eTrigger(Parse *
9340: 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 pParse, const ch
9350: 61 72 20 2a 7a 44 62 29 7b 0a 20 20 73 71 6c 69 ar *zDb){. sqli
9360: 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
9370: 2d 3e 64 62 3b 0a 20 20 54 72 69 67 67 65 72 20 ->db;. Trigger
9380: 2a 70 4e 65 77 20 3d 20 70 50 61 72 73 65 2d 3e *pNew = pParse->
9390: 70 4e 65 77 54 72 69 67 67 65 72 3b 0a 20 20 54 pNewTrigger;. T
93a0: 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 riggerStep *pSte
93b0: 70 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 p;. NameContext
93c0: 20 73 4e 43 3b 0a 20 20 69 6e 74 20 72 63 20 3d sNC;. int rc =
93d0: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 6d SQLITE_OK;.. m
93e0: 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 emset(&sNC, 0, s
93f0: 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 izeof(sNC));. s
9400: 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 NC.pParse = pPar
9410: 73 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e se;. assert( pN
9420: 65 77 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 29 ew->pTabSchema )
9430: 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 54 72 69 ;. pParse->pTri
9440: 67 67 65 72 54 61 62 20 3d 20 73 71 6c 69 74 65 ggerTab = sqlite
9450: 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 70 3FindTable(db, p
9460: 4e 65 77 2d 3e 74 61 62 6c 65 2c 20 0a 20 20 20 New->table, .
9470: 20 20 20 64 62 2d 3e 61 44 62 5b 73 71 6c 69 74 db->aDb[sqlit
9480: 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
9490: 64 62 2c 20 70 4e 65 77 2d 3e 70 54 61 62 53 63 db, pNew->pTabSc
94a0: 68 65 6d 61 29 5d 2e 7a 44 62 53 4e 61 6d 65 0a hema)].zDbSName.
94b0: 20 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 65 );. pParse->e
94c0: 54 72 69 67 67 65 72 4f 70 20 3d 20 70 4e 65 77 TriggerOp = pNew
94d0: 2d 3e 6f 70 3b 0a 20 20 2f 2a 20 41 4c 57 41 59 ->op;. /* ALWAY
94e0: 53 28 29 20 62 65 63 61 75 73 65 20 69 66 20 74 S() because if t
94f0: 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 he table of the
9500: 74 72 69 67 67 65 72 20 64 6f 65 73 20 6e 6f 74 trigger does not
9510: 20 65 78 69 73 74 2c 20 74 68 65 0a 20 20 2a 2a exist, the. **
9520: 20 65 72 72 6f 72 20 77 6f 75 6c 64 20 68 61 76 error would hav
9530: 65 20 62 65 65 6e 20 68 69 74 20 62 65 66 6f 72 e been hit befor
9540: 65 20 74 68 69 73 20 70 6f 69 6e 74 20 2a 2f 0a e this point */.
9550: 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 61 if( ALWAYS(pPa
9560: 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 rse->pTriggerTab
9570: 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 ) ){. rc = sq
9580: 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 lite3ViewGetColu
9590: 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 mnNames(pParse,
95a0: 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 pParse->pTrigger
95b0: 54 61 62 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Tab);. }.. /*
95c0: 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 73 20 Resolve symbols
95d0: 69 6e 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a in WHEN clause *
95e0: 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
95f0: 54 45 5f 4f 4b 20 26 26 20 70 4e 65 77 2d 3e 70 TE_OK && pNew->p
9600: 57 68 65 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d When ){. rc =
9610: 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
9620: 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 xprNames(&sNC, p
9630: 4e 65 77 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 7d New->pWhen);. }
9640: 0a 0a 20 20 66 6f 72 28 70 53 74 65 70 3d 70 4e .. for(pStep=pN
9650: 65 77 2d 3e 73 74 65 70 5f 6c 69 73 74 3b 20 72 ew->step_list; r
9660: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
9670: 70 53 74 65 70 3b 20 70 53 74 65 70 3d 70 53 74 pStep; pStep=pSt
9680: 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 ep->pNext){.
9690: 69 66 28 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 if( pStep->pSele
96a0: 63 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ct ){. sqli
96b0: 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 te3SelectPrep(pP
96c0: 61 72 73 65 2c 20 70 53 74 65 70 2d 3e 70 53 65 arse, pStep->pSe
96d0: 6c 65 63 74 2c 20 26 73 4e 43 29 3b 0a 20 20 20 lect, &sNC);.
96e0: 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e if( pParse->n
96f0: 45 72 72 20 29 20 72 63 20 3d 20 70 50 61 72 73 Err ) rc = pPars
9700: 65 2d 3e 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 e->rc;. }.
9710: 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
9720: 4f 4b 20 26 26 20 70 53 74 65 70 2d 3e 7a 54 61 OK && pStep->zTa
9730: 72 67 65 74 20 29 7b 0a 20 20 20 20 20 20 54 61 rget ){. Ta
9740: 62 6c 65 20 2a 70 54 61 72 67 65 74 20 3d 20 73 ble *pTarget = s
9750: 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c qlite3LocateTabl
9760: 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 74 e(pParse, 0, pSt
9770: 65 70 2d 3e 7a 54 61 72 67 65 74 2c 20 7a 44 62 ep->zTarget, zDb
9780: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 );. if( pTa
9790: 72 67 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 rget==0 ){.
97a0: 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
97b0: 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 65 6c 73 RROR;. }els
97c0: 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d e if( SQLITE_OK=
97d0: 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 69 =(rc = sqlite3Vi
97e0: 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 ewGetColumnNames
97f0: 28 70 50 61 72 73 65 2c 20 70 54 61 72 67 65 74 (pParse, pTarget
9800: 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 53 72 )) ){. Sr
9810: 63 4c 69 73 74 20 73 53 72 63 3b 0a 20 20 20 20 cList sSrc;.
9820: 20 20 20 20 6d 65 6d 73 65 74 28 26 73 53 72 63 memset(&sSrc
9830: 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 53 72 63 , 0, sizeof(sSrc
9840: 29 29 3b 0a 20 20 20 20 20 20 20 20 73 53 72 63 ));. sSrc
9850: 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 .nSrc = 1;.
9860: 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 sSrc.a[0].zNa
9870: 6d 65 20 3d 20 70 53 74 65 70 2d 3e 7a 54 61 72 me = pStep->zTar
9880: 67 65 74 3b 0a 20 20 20 20 20 20 20 20 73 53 72 get;. sSr
9890: 63 2e 61 5b 30 5d 2e 70 54 61 62 20 3d 20 70 54 c.a[0].pTab = pT
98a0: 61 72 67 65 74 3b 0a 20 20 20 20 20 20 20 20 73 arget;. s
98b0: 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73 NC.pSrcList = &s
98c0: 53 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 Src;. if(
98d0: 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65 20 29 pStep->pWhere )
98e0: 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
98f0: 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
9900: 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 xprNames(&sNC, p
9910: 53 74 65 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 Step->pWhere);.
9920: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9930: 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
9940: 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
9950: 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 6f rc = sqlite3Reso
9960: 6c 76 65 45 78 70 72 4c 69 73 74 4e 61 6d 65 73 lveExprListNames
9970: 28 26 73 4e 43 2c 20 70 53 74 65 70 2d 3e 70 45 (&sNC, pStep->pE
9980: 78 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 xprList);.
9990: 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 }. asse
99a0: 72 74 28 20 21 70 53 74 65 70 2d 3e 70 55 70 73 rt( !pStep->pUps
99b0: 65 72 74 20 7c 7c 20 28 21 70 53 74 65 70 2d 3e ert || (!pStep->
99c0: 70 57 68 65 72 65 20 26 26 20 21 70 53 74 65 70 pWhere && !pStep
99d0: 2d 3e 70 45 78 70 72 4c 69 73 74 29 20 29 3b 0a ->pExprList) );.
99e0: 20 20 20 20 20 20 20 20 69 66 28 20 70 53 74 65 if( pSte
99f0: 70 2d 3e 70 55 70 73 65 72 74 20 29 7b 0a 20 20 p->pUpsert ){.
9a00: 20 20 20 20 20 20 20 20 55 70 73 65 72 74 20 2a Upsert *
9a10: 70 55 70 73 65 72 74 20 3d 20 70 53 74 65 70 2d pUpsert = pStep-
9a20: 3e 70 55 70 73 65 72 74 3b 0a 20 20 20 20 20 20 >pUpsert;.
9a30: 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
9a40: 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
9a50: 20 20 20 20 20 20 20 70 55 70 73 65 72 74 2d 3e pUpsert->
9a60: 70 55 70 73 65 72 74 53 72 63 20 3d 20 26 73 53 pUpsertSrc = &sS
9a70: 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e rc;. sN
9a80: 43 2e 75 4e 43 2e 70 55 70 73 65 72 74 20 3d 20 C.uNC.pUpsert =
9a90: 70 55 70 73 65 72 74 3b 0a 20 20 20 20 20 20 20 pUpsert;.
9aa0: 20 20 20 73 4e 43 2e 6e 63 46 6c 61 67 73 20 3d sNC.ncFlags =
9ab0: 20 4e 43 5f 55 55 70 73 65 72 74 3b 0a 20 20 20 NC_UUpsert;.
9ac0: 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
9ad0: 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4c 69 te3ResolveExprLi
9ae0: 73 74 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 55 stNames(&sNC, pU
9af0: 70 73 65 72 74 2d 3e 70 55 70 73 65 72 74 54 61 psert->pUpsertTa
9b00: 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 20 rget);.
9b10: 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
9b20: 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
9b30: 20 20 45 78 70 72 4c 69 73 74 20 2a 70 55 70 73 ExprList *pUps
9b40: 65 72 74 53 65 74 20 3d 20 70 55 70 73 65 72 74 ertSet = pUpsert
9b50: 2d 3e 70 55 70 73 65 72 74 53 65 74 3b 0a 20 20 ->pUpsertSet;.
9b60: 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
9b70: 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
9b80: 72 4c 69 73 74 4e 61 6d 65 73 28 26 73 4e 43 2c rListNames(&sNC,
9b90: 20 70 55 70 73 65 72 74 53 65 74 29 3b 0a 20 20 pUpsertSet);.
9ba0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9bb0: 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
9bc0: 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
9bd0: 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
9be0: 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
9bf0: 73 28 26 73 4e 43 2c 20 70 55 70 73 65 72 74 2d s(&sNC, pUpsert-
9c00: 3e 70 55 70 73 65 72 74 57 68 65 72 65 29 3b 0a >pUpsertWhere);.
9c10: 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
9c20: 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
9c30: 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
9c40: 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
9c50: 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
9c60: 6d 65 73 28 26 73 4e 43 2c 20 70 55 70 73 65 72 mes(&sNC, pUpser
9c70: 74 2d 3e 70 55 70 73 65 72 74 54 61 72 67 65 74 t->pUpsertTarget
9c80: 57 68 65 72 65 29 3b 0a 20 20 20 20 20 20 20 20 Where);.
9c90: 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 4e }. sN
9ca0: 43 2e 6e 63 46 6c 61 67 73 20 3d 20 30 3b 0a 20 C.ncFlags = 0;.
9cb0: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9cc0: 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 sNC.pSrcList =
9cd0: 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
9ce0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
9cf0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b ;.}../*.** Invok
9d00: 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 e sqlite3WalkExp
9d10: 72 28 29 20 6f 72 20 73 71 6c 69 74 65 33 57 61 r() or sqlite3Wa
9d20: 6c 6b 53 65 6c 65 63 74 28 29 20 6f 6e 20 61 6c lkSelect() on al
9d30: 6c 20 53 65 6c 65 63 74 20 6f 72 20 45 78 70 72 l Select or Expr
9d40: 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 74 68 61 74 .** objects that
9d50: 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 are part of the
9d60: 20 74 72 69 67 67 65 72 20 70 61 73 73 65 64 20 trigger passed
9d70: 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
9d80: 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 gument..*/.stati
9d90: 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 57 61 6c c void renameWal
9da0: 6b 54 72 69 67 67 65 72 28 57 61 6c 6b 65 72 20 kTrigger(Walker
9db0: 2a 70 57 61 6c 6b 65 72 2c 20 54 72 69 67 67 65 *pWalker, Trigge
9dc0: 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 r *pTrigger){.
9dd0: 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 TriggerStep *pSt
9de0: 65 70 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 ep;.. /* Find t
9df0: 6f 6b 65 6e 73 20 74 6f 20 65 64 69 74 20 69 6e okens to edit in
9e00: 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a WHEN clause */.
9e10: 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 sqlite3WalkExp
9e20: 72 28 70 57 61 6c 6b 65 72 2c 20 70 54 72 69 67 r(pWalker, pTrig
9e30: 67 65 72 2d 3e 70 57 68 65 6e 29 3b 0a 0a 20 20 ger->pWhen);..
9e40: 2f 2a 20 46 69 6e 64 20 74 6f 6b 65 6e 73 20 74 /* Find tokens t
9e50: 6f 20 65 64 69 74 20 69 6e 20 74 72 69 67 67 65 o edit in trigge
9e60: 72 20 73 74 65 70 73 20 2a 2f 0a 20 20 66 6f 72 r steps */. for
9e70: 28 70 53 74 65 70 3d 70 54 72 69 67 67 65 72 2d (pStep=pTrigger-
9e80: 3e 73 74 65 70 5f 6c 69 73 74 3b 20 70 53 74 65 >step_list; pSte
9e90: 70 3b 20 70 53 74 65 70 3d 70 53 74 65 70 2d 3e p; pStep=pStep->
9ea0: 70 4e 65 78 74 29 7b 0a 20 20 20 20 73 71 6c 69 pNext){. sqli
9eb0: 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 te3WalkSelect(pW
9ec0: 61 6c 6b 65 72 2c 20 70 53 74 65 70 2d 3e 70 53 alker, pStep->pS
9ed0: 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 elect);. sqli
9ee0: 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
9ef0: 6b 65 72 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 ker, pStep->pWhe
9f00: 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 re);. sqlite3
9f10: 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 WalkExprList(pWa
9f20: 6c 6b 65 72 2c 20 70 53 74 65 70 2d 3e 70 45 78 lker, pStep->pEx
9f30: 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 69 66 28 prList);. if(
9f40: 20 70 53 74 65 70 2d 3e 70 55 70 73 65 72 74 20 pStep->pUpsert
9f50: 29 7b 0a 20 20 20 20 20 20 55 70 73 65 72 74 20 ){. Upsert
9f60: 2a 70 55 70 73 65 72 74 20 3d 20 70 53 74 65 70 *pUpsert = pStep
9f70: 2d 3e 70 55 70 73 65 72 74 3b 0a 20 20 20 20 20 ->pUpsert;.
9f80: 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
9f90: 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 55 List(pWalker, pU
9fa0: 70 73 65 72 74 2d 3e 70 55 70 73 65 72 74 54 61 psert->pUpsertTa
9fb0: 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c rget);. sql
9fc0: 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 ite3WalkExprList
9fd0: 28 70 57 61 6c 6b 65 72 2c 20 70 55 70 73 65 72 (pWalker, pUpser
9fe0: 74 2d 3e 70 55 70 73 65 72 74 53 65 74 29 3b 0a t->pUpsertSet);.
9ff0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c sqlite3Wal
a000: 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 kExpr(pWalker, p
a010: 55 70 73 65 72 74 2d 3e 70 55 70 73 65 72 74 57 Upsert->pUpsertW
a020: 68 65 72 65 29 3b 0a 20 20 20 20 20 20 73 71 6c here);. sql
a030: 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
a040: 6c 6b 65 72 2c 20 70 55 70 73 65 72 74 2d 3e 70 lker, pUpsert->p
a050: 55 70 73 65 72 74 54 61 72 67 65 74 57 68 65 72 UpsertTargetWher
a060: 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a e);. }. }.}.
a070: 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 ./*.** Free the
a080: 63 6f 6e 74 65 6e 74 73 20 6f 66 20 50 61 72 73 contents of Pars
a090: 65 20 6f 62 6a 65 63 74 20 28 2a 70 50 61 72 73 e object (*pPars
a0a0: 65 29 2e 20 44 6f 20 6e 6f 74 20 66 72 65 65 20 e). Do not free
a0b0: 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6f 63 the memory.** oc
a0c0: 63 75 70 69 65 64 20 62 79 20 74 68 65 20 50 61 cupied by the Pa
a0d0: 72 73 65 20 6f 62 6a 65 63 74 20 69 74 73 65 6c rse object itsel
a0e0: 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 f..*/.static voi
a0f0: 64 20 72 65 6e 61 6d 65 50 61 72 73 65 43 6c 65 d renameParseCle
a100: 61 6e 75 70 28 50 61 72 73 65 20 2a 70 50 61 72 anup(Parse *pPar
a110: 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a se){. sqlite3 *
a120: 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
a130: 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a . Index *pIdx;.
a140: 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 56 if( pParse->pV
a150: 64 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 dbe ){. sqlit
a160: 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 70 e3VdbeFinalize(p
a170: 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 Parse->pVdbe);.
a180: 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 }. sqlite3Dele
a190: 74 65 54 61 62 6c 65 28 64 62 2c 20 70 50 61 72 teTable(db, pPar
a1a0: 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a se->pNewTable);.
a1b0: 20 20 77 68 69 6c 65 28 20 28 70 49 64 78 20 3d while( (pIdx =
a1c0: 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 49 6e 64 pParse->pNewInd
a1d0: 65 78 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 50 ex)!=0 ){. pP
a1e0: 61 72 73 65 2d 3e 70 4e 65 77 49 6e 64 65 78 20 arse->pNewIndex
a1f0: 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b 0a 20 = pIdx->pNext;.
a200: 20 20 20 73 71 6c 69 74 65 33 46 72 65 65 49 6e sqlite3FreeIn
a210: 64 65 78 28 64 62 2c 20 70 49 64 78 29 3b 0a 20 dex(db, pIdx);.
a220: 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 }. sqlite3Dele
a230: 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 50 teTrigger(db, pP
a240: 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 arse->pNewTrigge
a250: 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 r);. sqlite3DbF
a260: 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e ree(db, pParse->
a270: 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 65 6e 61 zErrMsg);. rena
a280: 6d 65 54 6f 6b 65 6e 46 72 65 65 28 64 62 2c 20 meTokenFree(db,
a290: 70 50 61 72 73 65 2d 3e 70 52 65 6e 61 6d 65 29 pParse->pRename)
a2a0: 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 ;. sqlite3Parse
a2b0: 72 52 65 73 65 74 28 70 50 61 72 73 65 29 3b 0a rReset(pParse);.
a2c0: 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e }../*.** SQL fun
a2d0: 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ction:.**.**
a2e0: 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 63 sqlite_rename_c
a2f0: 6f 6c 75 6d 6e 28 7a 53 71 6c 2c 20 69 43 6f 6c olumn(zSql, iCol
a300: 2c 20 62 51 75 6f 74 65 2c 20 7a 4e 65 77 2c 20 , bQuote, zNew,
a310: 7a 54 61 62 6c 65 2c 20 7a 4f 6c 64 29 0a 2a 2a zTable, zOld).**
a320: 0a 2a 2a 20 20 20 30 2e 20 7a 53 71 6c 3a 20 20 .** 0. zSql:
a330: 20 20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 SQL statement
a340: 20 74 6f 20 72 65 77 72 69 74 65 0a 2a 2a 20 20 to rewrite.**
a350: 20 31 2e 20 74 79 70 65 3a 20 20 20 20 20 54 79 1. type: Ty
a360: 70 65 20 6f 66 20 6f 62 6a 65 63 74 20 28 22 74 pe of object ("t
a370: 61 62 6c 65 22 2c 20 22 76 69 65 77 22 20 65 74 able", "view" et
a380: 63 2e 29 0a 2a 2a 20 20 20 32 2e 20 6f 62 6a 65 c.).** 2. obje
a390: 63 74 3a 20 20 20 4e 61 6d 65 20 6f 66 20 6f 62 ct: Name of ob
a3a0: 6a 65 63 74 0a 2a 2a 20 20 20 33 2e 20 44 61 74 ject.** 3. Dat
a3b0: 61 62 61 73 65 3a 20 44 61 74 61 62 61 73 65 20 abase: Database
a3c0: 6e 61 6d 65 20 28 65 2e 67 2e 20 22 6d 61 69 6e name (e.g. "main
a3d0: 22 29 0a 2a 2a 20 20 20 34 2e 20 54 61 62 6c 65 ").** 4. Table
a3e0: 3a 20 20 20 20 54 61 62 6c 65 20 6e 61 6d 65 0a : Table name.
a3f0: 2a 2a 20 20 20 35 2e 20 69 43 6f 6c 3a 20 20 20 ** 5. iCol:
a400: 20 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d Index of colum
a410: 6e 20 74 6f 20 72 65 6e 61 6d 65 0a 2a 2a 20 20 n to rename.**
a420: 20 36 2e 20 7a 4e 65 77 3a 20 20 20 20 20 4e 65 6. zNew: Ne
a430: 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 2a 2a w column name.**
a440: 20 20 20 37 2e 20 62 51 75 6f 74 65 3a 20 20 20 7. bQuote:
a450: 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 Non-zero if the
a460: 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 new column name
a470: 73 68 6f 75 6c 64 20 62 65 20 71 75 6f 74 65 64 should be quoted
a480: 2e 0a 2a 2a 20 20 20 38 2e 20 62 54 65 6d 70 3a ..** 8. bTemp:
a490: 20 20 20 20 54 72 75 65 20 69 66 20 7a 53 71 6c True if zSql
a4a0: 20 63 6f 6d 65 73 20 66 72 6f 6d 20 74 65 6d 70 comes from temp
a4b0: 20 73 63 68 65 6d 61 0a 2a 2a 20 20 20 39 2e 20 schema.** 9.
a4c0: 69 52 65 67 3a 20 20 20 20 20 52 65 67 69 73 74 iReg: Regist
a4d0: 65 72 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 er to increment
a4e0: 66 6f 72 20 65 61 63 68 20 72 65 77 72 69 74 74 for each rewritt
a4f0: 65 6e 20 63 6f 6c 75 6d 6e 20 72 65 66 2e 20 4f en column ref. O
a500: 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 r,.**
a510: 20 20 20 20 20 69 66 20 74 68 69 73 20 70 61 72 if this par
a520: 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 ameter is negati
a530: 76 65 2c 20 2d 31 20 74 69 6d 65 73 20 74 68 65 ve, -1 times the
a540: 20 69 64 20 6f 66 20 61 20 72 65 67 69 73 74 65 id of a registe
a550: 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 r.**
a560: 20 20 20 20 74 6f 20 64 65 63 72 65 6d 65 6e 74 to decrement
a570: 20 66 6f 72 20 65 61 63 68 20 72 65 77 72 69 74 for each rewrit
a580: 74 65 6e 20 63 6f 6c 75 6d 6e 20 72 65 66 2e 0a ten column ref..
a590: 2a 2a 0a 2a 2a 20 44 6f 20 61 20 63 6f 6c 75 6d **.** Do a colum
a5a0: 6e 20 72 65 6e 61 6d 65 20 6f 70 65 72 61 74 69 n rename operati
a5b0: 6f 6e 20 6f 6e 20 74 68 65 20 43 52 45 41 54 45 on on the CREATE
a5c0: 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e statement given
a5d0: 20 69 6e 20 7a 53 71 6c 2e 0a 2a 2a 20 54 68 65 in zSql..** The
a5e0: 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 iCol-th column
a5f0: 28 6c 65 66 74 2d 6d 6f 73 74 20 69 73 20 30 29 (left-most is 0)
a600: 20 6f 66 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 of table zTable
a610: 20 69 73 20 72 65 6e 61 6d 65 64 20 66 72 6f 6d is renamed from
a620: 20 7a 43 6f 6c 0a 2a 2a 20 69 6e 74 6f 20 7a 4e zCol.** into zN
a630: 65 77 2e 20 20 54 68 65 20 6e 61 6d 65 20 73 68 ew. The name sh
a640: 6f 75 6c 64 20 62 65 20 71 75 6f 74 65 64 20 69 ould be quoted i
a650: 66 20 62 51 75 6f 74 65 20 69 73 20 74 72 75 65 f bQuote is true
a660: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
a670: 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e ction is used in
a680: 74 65 72 6e 61 6c 6c 79 20 62 79 20 74 68 65 20 ternally by the
a690: 41 4c 54 45 52 20 54 41 42 4c 45 20 52 45 4e 41 ALTER TABLE RENA
a6a0: 4d 45 20 43 4f 4c 55 4d 4e 20 63 6f 6d 6d 61 6e ME COLUMN comman
a6b0: 64 2e 0a 2a 2a 20 49 74 20 69 73 20 6f 6e 6c 79 d..** It is only
a6c0: 20 61 63 63 65 73 73 69 62 6c 65 20 74 6f 20 53 accessible to S
a6d0: 51 4c 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 QL created using
a6e0: 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 sqlite3NestedPa
a6f0: 72 73 65 28 29 2e 20 20 49 74 20 69 73 0a 2a 2a rse(). It is.**
a700: 20 6e 6f 74 20 72 65 61 63 68 61 62 6c 65 20 66 not reachable f
a710: 72 6f 6d 20 6f 72 64 69 6e 61 72 79 20 53 51 4c rom ordinary SQL
a720: 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73 71 6c passed into sql
a730: 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 2e 0a ite3_prepare()..
a740: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
a750: 65 6e 61 6d 65 43 6f 6c 75 6d 6e 46 75 6e 63 28 enameColumnFunc(
a760: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
a770: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
a780: 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
a790: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
a7a0: 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 gv.){. sqlite3
a7b0: 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f *db = sqlite3_co
a7c0: 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
a7d0: 63 6f 6e 74 65 78 74 29 3b 0a 20 20 52 65 6e 61 context);. Rena
a7e0: 6d 65 43 74 78 20 73 43 74 78 3b 0a 20 20 63 6f meCtx sCtx;. co
a7f0: 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d nst char *zSql =
a800: 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 (const char*)sq
a810: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
a820: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e (argv[0]);. con
a830: 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 28 st char *zDb = (
a840: 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 const char*)sqli
a850: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
a860: 72 67 76 5b 33 5d 29 3b 0a 20 20 63 6f 6e 73 74 rgv[3]);. const
a870: 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 20 3d 20 char *zTable =
a880: 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c (const char*)sql
a890: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
a8a0: 61 72 67 76 5b 34 5d 29 3b 0a 20 20 69 6e 74 20 argv[4]);. int
a8b0: 69 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 iCol = sqlite3_v
a8c0: 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 35 5d alue_int(argv[5]
a8d0: 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 );. const char
a8e0: 2a 7a 4e 65 77 20 3d 20 28 63 6f 6e 73 74 20 63 *zNew = (const c
a8f0: 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
a900: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 36 5d 29 ue_text(argv[6])
a910: 3b 0a 20 20 69 6e 74 20 62 51 75 6f 74 65 20 3d ;. int bQuote =
a920: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
a930: 6e 74 28 61 72 67 76 5b 37 5d 29 3b 0a 20 20 69 nt(argv[7]);. i
a940: 6e 74 20 62 54 65 6d 70 20 3d 20 73 71 6c 69 74 nt bTemp = sqlit
a950: 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 e3_value_int(arg
a960: 76 5b 38 5d 29 3b 0a 20 20 69 6e 74 20 72 65 67 v[8]);. int reg
a970: 52 65 66 43 6e 74 20 3d 20 73 71 6c 69 74 65 33 RefCnt = sqlite3
a980: 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b _value_int(argv[
a990: 39 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 9]);. const cha
a9a0: 72 20 2a 7a 4f 6c 64 3b 0a 20 20 69 6e 74 20 72 r *zOld;. int r
a9b0: 63 3b 0a 20 20 50 61 72 73 65 20 73 50 61 72 73 c;. Parse sPars
a9c0: 65 3b 0a 20 20 57 61 6c 6b 65 72 20 73 57 61 6c e;. Walker sWal
a9d0: 6b 65 72 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 ker;. Index *pI
a9e0: 64 78 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 54 dx;. int i;. T
a9f0: 61 62 6c 65 20 2a 70 54 61 62 3b 0a 23 69 66 6e able *pTab;.#ifn
aa00: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
aa10: 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 AUTHORIZATION.
aa20: 73 71 6c 69 74 65 33 5f 78 61 75 74 68 20 78 41 sqlite3_xauth xA
aa30: 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 68 3b uth = db->xAuth;
aa40: 0a 23 65 6e 64 69 66 0a 0a 20 20 55 4e 55 53 45 .#endif.. UNUSE
aa50: 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
aa60: 73 65 64 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c sed);. if( zSql
aa70: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
aa80: 69 66 28 20 7a 54 61 62 6c 65 3d 3d 30 20 29 20 if( zTable==0 )
aa90: 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 7a 4e return;. if( zN
aaa0: 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a ew==0 ) return;.
aab0: 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 72 if( iCol<0 ) r
aac0: 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 eturn;. sqlite3
aad0: 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 BtreeEnterAll(db
aae0: 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 );. pTab = sqli
aaf0: 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c te3FindTable(db,
ab00: 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 zTable, zDb);.
ab10: 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20 if( pTab==0 ||
ab20: 69 43 6f 6c 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c iCol>=pTab->nCol
ab30: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
ab40: 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 treeLeaveAll(db)
ab50: 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
ab60: 7d 0a 20 20 7a 4f 6c 64 20 3d 20 70 54 61 62 2d }. zOld = pTab-
ab70: 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d >aCol[iCol].zNam
ab80: 65 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 43 74 e;. memset(&sCt
ab90: 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 74 x, 0, sizeof(sCt
aba0: 78 29 29 3b 0a 20 20 73 43 74 78 2e 69 43 6f 6c x));. sCtx.iCol
abb0: 20 3d 20 28 28 69 43 6f 6c 3d 3d 70 54 61 62 2d = ((iCol==pTab-
abc0: 3e 69 50 4b 65 79 29 20 3f 20 2d 31 20 3a 20 69 >iPKey) ? -1 : i
abd0: 43 6f 6c 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 Col);..#ifndef S
abe0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
abf0: 52 49 5a 41 54 49 4f 4e 0a 20 20 64 62 2d 3e 78 RIZATION. db->x
ac00: 41 75 74 68 20 3d 20 30 3b 0a 23 65 6e 64 69 66 Auth = 0;.#endif
ac10: 0a 20 20 72 63 20 3d 20 72 65 6e 61 6d 65 50 61 . rc = renamePa
ac20: 72 73 65 53 71 6c 28 26 73 50 61 72 73 65 2c 20 rseSql(&sParse,
ac30: 7a 44 62 2c 20 30 2c 20 64 62 2c 20 7a 53 71 6c zDb, 0, db, zSql
ac40: 2c 20 62 54 65 6d 70 29 3b 0a 0a 20 20 2f 2a 20 , bTemp);.. /*
ac50: 46 69 6e 64 20 74 6f 6b 65 6e 73 20 74 68 61 74 Find tokens that
ac60: 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 70 6c need to be repl
ac70: 61 63 65 64 2e 20 2a 2f 0a 20 20 6d 65 6d 73 65 aced. */. memse
ac80: 74 28 26 73 57 61 6c 6b 65 72 2c 20 30 2c 20 73 t(&sWalker, 0, s
ac90: 69 7a 65 6f 66 28 57 61 6c 6b 65 72 29 29 3b 0a izeof(Walker));.
aca0: 20 20 73 57 61 6c 6b 65 72 2e 70 50 61 72 73 65 sWalker.pParse
acb0: 20 3d 20 26 73 50 61 72 73 65 3b 0a 20 20 73 57 = &sParse;. sW
acc0: 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c 6c 62 alker.xExprCallb
acd0: 61 63 6b 20 3d 20 72 65 6e 61 6d 65 43 6f 6c 75 ack = renameColu
ace0: 6d 6e 45 78 70 72 43 62 3b 0a 20 20 73 57 61 6c mnExprCb;. sWal
acf0: 6b 65 72 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 ker.xSelectCallb
ad00: 61 63 6b 20 3d 20 72 65 6e 61 6d 65 43 6f 6c 75 ack = renameColu
ad10: 6d 6e 53 65 6c 65 63 74 43 62 3b 0a 20 20 73 57 mnSelectCb;. sW
ad20: 61 6c 6b 65 72 2e 75 2e 70 52 65 6e 61 6d 65 20 alker.u.pRename
ad30: 3d 20 26 73 43 74 78 3b 0a 0a 20 20 73 43 74 78 = &sCtx;.. sCtx
ad40: 2e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 .pTab = pTab;.
ad50: 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
ad60: 4b 20 29 20 67 6f 74 6f 20 72 65 6e 61 6d 65 43 K ) goto renameC
ad70: 6f 6c 75 6d 6e 46 75 6e 63 5f 64 6f 6e 65 3b 0a olumnFunc_done;.
ad80: 20 20 69 66 28 20 73 50 61 72 73 65 2e 70 4e 65 if( sParse.pNe
ad90: 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 53 65 wTable ){. Se
ada0: 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 lect *pSelect =
adb0: 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 sParse.pNewTable
adc0: 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 69 ->pSelect;. i
add0: 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 f( pSelect ){.
ade0: 20 20 20 20 73 50 61 72 73 65 2e 72 63 20 3d 20 sParse.rc =
adf0: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
ae00: 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 sqlite3SelectPr
ae10: 65 70 28 26 73 50 61 72 73 65 2c 20 73 50 61 72 ep(&sParse, sPar
ae20: 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 70 53 se.pNewTable->pS
ae30: 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 20 20 elect, 0);.
ae40: 20 72 63 20 3d 20 28 64 62 2d 3e 6d 61 6c 6c 6f rc = (db->mallo
ae50: 63 46 61 69 6c 65 64 20 3f 20 53 51 4c 49 54 45 cFailed ? SQLITE
ae60: 5f 4e 4f 4d 45 4d 20 3a 20 73 50 61 72 73 65 2e _NOMEM : sParse.
ae70: 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 rc);. if( r
ae80: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
ae90: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 sqlite3W
aea0: 61 6c 6b 53 65 6c 65 63 74 28 26 73 57 61 6c 6b alkSelect(&sWalk
aeb0: 65 72 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 er, pSelect);.
aec0: 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
aed0: 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
aee0: 67 6f 74 6f 20 72 65 6e 61 6d 65 43 6f 6c 75 6d goto renameColum
aef0: 6e 46 75 6e 63 5f 64 6f 6e 65 3b 0a 20 20 20 20 nFunc_done;.
af00: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
af10: 41 20 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20 A regular table
af20: 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 62 46 4b */. int bFK
af30: 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65 33 5f 73 Only = sqlite3_s
af40: 74 72 69 63 6d 70 28 7a 54 61 62 6c 65 2c 20 73 tricmp(zTable, s
af50: 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d Parse.pNewTable-
af60: 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 46 >zName);. F
af70: 4b 65 79 20 2a 70 46 4b 65 79 3b 0a 20 20 20 20 Key *pFKey;.
af80: 20 20 61 73 73 65 72 74 28 20 73 50 61 72 73 65 assert( sParse
af90: 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 70 53 65 6c .pNewTable->pSel
afa0: 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ect==0 );.
afb0: 73 43 74 78 2e 70 54 61 62 20 3d 20 73 50 61 72 sCtx.pTab = sPar
afc0: 73 65 2e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 se.pNewTable;.
afd0: 20 20 20 20 69 66 28 20 62 46 4b 4f 6e 6c 79 3d if( bFKOnly=
afe0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
aff0: 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 0a 20 nameTokenFind(.
b000: 20 20 20 20 20 20 20 20 20 20 20 26 73 50 61 72 &sPar
b010: 73 65 2c 20 26 73 43 74 78 2c 20 28 76 6f 69 64 se, &sCtx, (void
b020: 2a 29 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 *)sParse.pNewTab
b030: 6c 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a le->aCol[iCol].z
b040: 4e 61 6d 65 0a 20 20 20 20 20 20 20 20 29 3b 0a Name. );.
b050: 20 20 20 20 20 20 20 20 69 66 28 20 73 43 74 78 if( sCtx
b060: 2e 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 .iCol<0 ){.
b070: 20 20 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e renameToken
b080: 46 69 6e 64 28 26 73 50 61 72 73 65 2c 20 26 73 Find(&sParse, &s
b090: 43 74 78 2c 20 28 76 6f 69 64 2a 29 26 73 50 61 Ctx, (void*)&sPa
b0a0: 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 69 rse.pNewTable->i
b0b0: 50 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d PKey);. }
b0c0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
b0d0: 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 26 73 57 WalkExprList(&sW
b0e0: 61 6c 6b 65 72 2c 20 73 50 61 72 73 65 2e 70 4e alker, sParse.pN
b0f0: 65 77 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29 ewTable->pCheck)
b100: 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 49 ;. for(pI
b110: 64 78 3d 73 50 61 72 73 65 2e 70 4e 65 77 54 61 dx=sParse.pNewTa
b120: 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 ble->pIndex; pId
b130: 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e x; pIdx=pIdx->pN
b140: 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ext){.
b150: 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c sqlite3WalkExprL
b160: 69 73 74 28 26 73 57 61 6c 6b 65 72 2c 20 70 49 ist(&sWalker, pI
b170: 64 78 2d 3e 61 43 6f 6c 45 78 70 72 29 3b 0a 20 dx->aColExpr);.
b180: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
b190: 20 66 6f 72 28 70 49 64 78 3d 73 50 61 72 73 65 for(pIdx=sParse
b1a0: 2e 70 4e 65 77 49 6e 64 65 78 3b 20 70 49 64 78 .pNewIndex; pIdx
b1b0: 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 ; pIdx=pIdx->pNe
b1c0: 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 xt){. s
b1d0: 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 qlite3WalkExprLi
b1e0: 73 74 28 26 73 57 61 6c 6b 65 72 2c 20 70 49 64 st(&sWalker, pId
b1f0: 78 2d 3e 61 43 6f 6c 45 78 70 72 29 3b 0a 20 20 x->aColExpr);.
b200: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
b210: 0a 20 20 20 20 20 20 66 6f 72 28 70 46 4b 65 79 . for(pFKey
b220: 3d 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c =sParse.pNewTabl
b230: 65 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b e->pFKey; pFKey;
b240: 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e pFKey=pFKey->pN
b250: 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 extFrom){.
b260: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b for(i=0; i<pFK
b270: 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a ey->nCol; i++){.
b280: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 62 46 if( bF
b290: 4b 4f 6e 6c 79 3d 3d 30 20 26 26 20 70 46 4b 65 KOnly==0 && pFKe
b2a0: 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d y->aCol[i].iFrom
b2b0: 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 ==iCol ){.
b2c0: 20 20 20 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 renameToke
b2d0: 6e 46 69 6e 64 28 26 73 50 61 72 73 65 2c 20 26 nFind(&sParse, &
b2e0: 73 43 74 78 2c 20 28 76 6f 69 64 2a 29 26 70 46 sCtx, (void*)&pF
b2f0: 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 29 3b 0a 20 Key->aCol[i]);.
b300: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
b310: 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 if( 0==sqli
b320: 74 65 33 5f 73 74 72 69 63 6d 70 28 70 46 4b 65 te3_stricmp(pFKe
b330: 79 2d 3e 7a 54 6f 2c 20 7a 54 61 62 6c 65 29 0a y->zTo, zTable).
b340: 20 20 20 20 20 20 20 20 20 20 20 26 26 20 30 3d && 0=
b350: 3d 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 =sqlite3_stricmp
b360: 28 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e (pFKey->aCol[i].
b370: 7a 43 6f 6c 2c 20 7a 4f 6c 64 29 0a 20 20 20 20 zCol, zOld).
b380: 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
b390: 20 20 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e renameToken
b3a0: 46 69 6e 64 28 26 73 50 61 72 73 65 2c 20 26 73 Find(&sParse, &s
b3b0: 43 74 78 2c 20 28 76 6f 69 64 2a 29 70 46 4b 65 Ctx, (void*)pFKe
b3c0: 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 29 y->aCol[i].zCol)
b3d0: 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
b3e0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
b3f0: 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
b400: 28 20 73 50 61 72 73 65 2e 70 4e 65 77 49 6e 64 ( sParse.pNewInd
b410: 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ex ){. sqlite
b420: 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 26 73 3WalkExprList(&s
b430: 57 61 6c 6b 65 72 2c 20 73 50 61 72 73 65 2e 70 Walker, sParse.p
b440: 4e 65 77 49 6e 64 65 78 2d 3e 61 43 6f 6c 45 78 NewIndex->aColEx
b450: 70 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 pr);. sqlite3
b460: 57 61 6c 6b 45 78 70 72 28 26 73 57 61 6c 6b 65 WalkExpr(&sWalke
b470: 72 2c 20 73 50 61 72 73 65 2e 70 4e 65 77 49 6e r, sParse.pNewIn
b480: 64 65 78 2d 3e 70 50 61 72 74 49 64 78 57 68 65 dex->pPartIdxWhe
b490: 72 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 re);. }else{.
b4a0: 20 20 2f 2a 20 41 20 74 72 69 67 67 65 72 20 2a /* A trigger *
b4b0: 2f 0a 20 20 20 20 54 72 69 67 67 65 72 53 74 65 /. TriggerSte
b4c0: 70 20 2a 70 53 74 65 70 3b 0a 20 20 20 20 72 63 p *pStep;. rc
b4d0: 20 3d 20 72 65 6e 61 6d 65 52 65 73 6f 6c 76 65 = renameResolve
b4e0: 54 72 69 67 67 65 72 28 26 73 50 61 72 73 65 2c Trigger(&sParse,
b4f0: 20 28 62 54 65 6d 70 20 3f 20 30 20 3a 20 7a 44 (bTemp ? 0 : zD
b500: 62 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 b));. if( rc!
b510: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
b520: 6f 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 46 75 o renameColumnFu
b530: 6e 63 5f 64 6f 6e 65 3b 0a 0a 20 20 20 20 66 6f nc_done;.. fo
b540: 72 28 70 53 74 65 70 3d 73 50 61 72 73 65 2e 70 r(pStep=sParse.p
b550: 4e 65 77 54 72 69 67 67 65 72 2d 3e 73 74 65 70 NewTrigger->step
b560: 5f 6c 69 73 74 3b 20 70 53 74 65 70 3b 20 70 53 _list; pStep; pS
b570: 74 65 70 3d 70 53 74 65 70 2d 3e 70 4e 65 78 74 tep=pStep->pNext
b580: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 53 74 ){. if( pSt
b590: 65 70 2d 3e 7a 54 61 72 67 65 74 20 29 7b 20 0a ep->zTarget ){ .
b5a0: 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 Table *p
b5b0: 54 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 Target = sqlite3
b5c0: 4c 6f 63 61 74 65 54 61 62 6c 65 28 26 73 50 61 LocateTable(&sPa
b5d0: 72 73 65 2c 20 30 2c 20 70 53 74 65 70 2d 3e 7a rse, 0, pStep->z
b5e0: 54 61 72 67 65 74 2c 20 7a 44 62 29 3b 0a 20 20 Target, zDb);.
b5f0: 20 20 20 20 20 20 69 66 28 20 70 54 61 72 67 65 if( pTarge
b600: 74 3d 3d 70 54 61 62 20 29 7b 0a 20 20 20 20 20 t==pTab ){.
b610: 20 20 20 20 20 69 66 28 20 70 53 74 65 70 2d 3e if( pStep->
b620: 70 55 70 73 65 72 74 20 29 7b 0a 20 20 20 20 20 pUpsert ){.
b630: 20 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 ExprList
b640: 2a 70 55 70 73 65 72 74 53 65 74 20 3d 20 70 53 *pUpsertSet = pS
b650: 74 65 70 2d 3e 70 55 70 73 65 72 74 2d 3e 70 55 tep->pUpsert->pU
b660: 70 73 65 72 74 53 65 74 3b 0a 20 20 20 20 20 20 psertSet;.
b670: 20 20 20 20 20 20 72 65 6e 61 6d 65 43 6f 6c 75 renameColu
b680: 6d 6e 45 6c 69 73 74 4e 61 6d 65 73 28 26 73 50 mnElistNames(&sP
b690: 61 72 73 65 2c 20 26 73 43 74 78 2c 20 70 55 70 arse, &sCtx, pUp
b6a0: 73 65 72 74 53 65 74 2c 20 7a 4f 6c 64 29 3b 0a sertSet, zOld);.
b6b0: 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
b6c0: 20 20 20 20 20 20 72 65 6e 61 6d 65 43 6f 6c 75 renameColu
b6d0: 6d 6e 49 64 6c 69 73 74 4e 61 6d 65 73 28 26 73 mnIdlistNames(&s
b6e0: 50 61 72 73 65 2c 20 26 73 43 74 78 2c 20 70 53 Parse, &sCtx, pS
b6f0: 74 65 70 2d 3e 70 49 64 4c 69 73 74 2c 20 7a 4f tep->pIdList, zO
b700: 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 ld);. r
b710: 65 6e 61 6d 65 43 6f 6c 75 6d 6e 45 6c 69 73 74 enameColumnElist
b720: 4e 61 6d 65 73 28 26 73 50 61 72 73 65 2c 20 26 Names(&sParse, &
b730: 73 43 74 78 2c 20 70 53 74 65 70 2d 3e 70 45 78 sCtx, pStep->pEx
b740: 70 72 4c 69 73 74 2c 20 7a 4f 6c 64 29 3b 0a 20 prList, zOld);.
b750: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
b760: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 . }.. /* F
b770: 69 6e 64 20 74 6f 6b 65 6e 73 20 74 6f 20 65 64 ind tokens to ed
b780: 69 74 20 69 6e 20 55 50 44 41 54 45 20 4f 46 20 it in UPDATE OF
b790: 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 69 66 clause */. if
b7a0: 28 20 73 50 61 72 73 65 2e 70 54 72 69 67 67 65 ( sParse.pTrigge
b7b0: 72 54 61 62 3d 3d 70 54 61 62 20 29 7b 0a 20 20 rTab==pTab ){.
b7c0: 20 20 20 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e renameColumn
b7d0: 49 64 6c 69 73 74 4e 61 6d 65 73 28 26 73 50 61 IdlistNames(&sPa
b7e0: 72 73 65 2c 20 26 73 43 74 78 2c 73 50 61 72 73 rse, &sCtx,sPars
b7f0: 65 2e 70 4e 65 77 54 72 69 67 67 65 72 2d 3e 70 e.pNewTrigger->p
b800: 43 6f 6c 75 6d 6e 73 2c 7a 4f 6c 64 29 3b 0a 20 Columns,zOld);.
b810: 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e }.. /* Fin
b820: 64 20 74 6f 6b 65 6e 73 20 74 6f 20 65 64 69 74 d tokens to edit
b830: 20 69 6e 20 76 61 72 69 6f 75 73 20 65 78 70 72 in various expr
b840: 65 73 73 69 6f 6e 73 20 61 6e 64 20 73 65 6c 65 essions and sele
b850: 63 74 73 20 2a 2f 0a 20 20 20 20 72 65 6e 61 6d cts */. renam
b860: 65 57 61 6c 6b 54 72 69 67 67 65 72 28 26 73 57 eWalkTrigger(&sW
b870: 61 6c 6b 65 72 2c 20 73 50 61 72 73 65 2e 70 4e alker, sParse.pN
b880: 65 77 54 72 69 67 67 65 72 29 3b 0a 20 20 7d 0a ewTrigger);. }.
b890: 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
b8a0: 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 QLITE_OK );. if
b8b0: 28 20 72 65 67 52 65 66 43 6e 74 21 3d 30 20 29 ( regRefCnt!=0 )
b8c0: 7b 0a 20 20 20 20 69 6e 74 20 6d 75 6c 20 3d 20 {. int mul =
b8d0: 28 72 65 67 52 65 66 43 6e 74 3c 30 20 3f 20 2d (regRefCnt<0 ? -
b8e0: 31 20 3a 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 1 : 1);. sqli
b8f0: 74 65 33 56 64 62 65 49 6e 63 72 52 65 67 28 63 te3VdbeIncrReg(c
b900: 6f 6e 74 65 78 74 2c 20 72 65 67 52 65 66 43 6e ontext, regRefCn
b910: 74 2a 6d 75 6c 2c 20 73 43 74 78 2e 6e 4c 69 73 t*mul, sCtx.nLis
b920: 74 2a 6d 75 6c 29 3b 0a 20 20 7d 0a 20 20 72 63 t*mul);. }. rc
b930: 20 3d 20 72 65 6e 61 6d 65 45 64 69 74 53 71 6c = renameEditSql
b940: 28 63 6f 6e 74 65 78 74 2c 20 26 73 43 74 78 2c (context, &sCtx,
b950: 20 7a 53 71 6c 2c 20 7a 4e 65 77 2c 20 62 51 75 zSql, zNew, bQu
b960: 6f 74 65 29 3b 0a 0a 72 65 6e 61 6d 65 43 6f 6c ote);..renameCol
b970: 75 6d 6e 46 75 6e 63 5f 64 6f 6e 65 3a 0a 20 20 umnFunc_done:.
b980: 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
b990: 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 50 61 K ){. if( sPa
b9a0: 72 73 65 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 rse.zErrMsg ){.
b9b0: 20 20 20 20 20 72 65 6e 61 6d 65 43 6f 6c 75 6d renameColum
b9c0: 6e 50 61 72 73 65 45 72 72 6f 72 28 63 6f 6e 74 nParseError(cont
b9d0: 65 78 74 2c 20 72 65 67 52 65 66 43 6e 74 3c 30 ext, regRefCnt<0
b9e0: 2c 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 76 5b , argv[1], argv[
b9f0: 32 5d 2c 20 26 73 50 61 72 73 65 29 3b 0a 20 20 2], &sParse);.
ba00: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
ba10: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
ba20: 72 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65 78 74 ror_code(context
ba30: 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d , rc);. }. }
ba40: 0a 0a 20 20 72 65 6e 61 6d 65 50 61 72 73 65 43 .. renameParseC
ba50: 6c 65 61 6e 75 70 28 26 73 50 61 72 73 65 29 3b leanup(&sParse);
ba60: 0a 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 72 . renameTokenFr
ba70: 65 65 28 64 62 2c 20 73 43 74 78 2e 70 4c 69 73 ee(db, sCtx.pLis
ba80: 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 t);.#ifndef SQLI
ba90: 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
baa0: 41 54 49 4f 4e 0a 20 20 64 62 2d 3e 78 41 75 74 ATION. db->xAut
bab0: 68 20 3d 20 78 41 75 74 68 3b 0a 23 65 6e 64 69 h = xAuth;.#endi
bac0: 66 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 f. sqlite3Btree
bad0: 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 7d 0a LeaveAll(db);.}.
bae0: 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 65 72 20 65 78 ./*.** Walker ex
baf0: 70 72 65 73 73 69 6f 6e 20 63 61 6c 6c 62 61 63 pression callbac
bb00: 6b 20 75 73 65 64 20 62 79 20 22 52 45 4e 41 4d k used by "RENAM
bb10: 45 20 54 41 42 4c 45 22 2e 20 0a 2a 2f 0a 73 74 E TABLE". .*/.st
bb20: 61 74 69 63 20 69 6e 74 20 72 65 6e 61 6d 65 54 atic int renameT
bb30: 61 62 6c 65 45 78 70 72 43 62 28 57 61 6c 6b 65 ableExprCb(Walke
bb40: 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 r *pWalker, Expr
bb50: 20 2a 70 45 78 70 72 29 7b 0a 20 20 52 65 6e 61 *pExpr){. Rena
bb60: 6d 65 43 74 78 20 2a 70 20 3d 20 70 57 61 6c 6b meCtx *p = pWalk
bb70: 65 72 2d 3e 75 2e 70 52 65 6e 61 6d 65 3b 0a 20 er->u.pRename;.
bb80: 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d if( pExpr->op==
bb90: 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 2d 3e TK_COLUMN && p->
bba0: 70 54 61 62 3d 3d 70 45 78 70 72 2d 3e 79 2e 70 pTab==pExpr->y.p
bbb0: 54 61 62 20 29 7b 0a 20 20 20 20 72 65 6e 61 6d Tab ){. renam
bbc0: 65 54 6f 6b 65 6e 46 69 6e 64 28 70 57 61 6c 6b eTokenFind(pWalk
bbd0: 65 72 2d 3e 70 50 61 72 73 65 2c 20 70 2c 20 28 er->pParse, p, (
bbe0: 76 6f 69 64 2a 29 26 70 45 78 70 72 2d 3e 79 2e void*)&pExpr->y.
bbf0: 70 54 61 62 29 3b 0a 20 20 7d 0a 20 20 72 65 74 pTab);. }. ret
bc00: 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 urn WRC_Continue
bc10: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 65 ;.}../*.** Walke
bc20: 72 20 73 65 6c 65 63 74 20 63 61 6c 6c 62 61 63 r select callbac
bc30: 6b 20 75 73 65 64 20 62 79 20 22 52 45 4e 41 4d k used by "RENAM
bc40: 45 20 54 41 42 4c 45 22 2e 20 0a 2a 2f 0a 73 74 E TABLE". .*/.st
bc50: 61 74 69 63 20 69 6e 74 20 72 65 6e 61 6d 65 54 atic int renameT
bc60: 61 62 6c 65 53 65 6c 65 63 74 43 62 28 57 61 6c ableSelectCb(Wal
bc70: 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 ker *pWalker, Se
bc80: 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a lect *pSelect){.
bc90: 20 20 69 6e 74 20 69 3b 0a 20 20 52 65 6e 61 6d int i;. Renam
bca0: 65 43 74 78 20 2a 70 20 3d 20 70 57 61 6c 6b 65 eCtx *p = pWalke
bcb0: 72 2d 3e 75 2e 70 52 65 6e 61 6d 65 3b 0a 20 20 r->u.pRename;.
bcc0: 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 SrcList *pSrc =
bcd0: 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 pSelect->pSrc;.
bce0: 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a if( pSrc==0 ){.
bcf0: 20 20 20 20 61 73 73 65 72 74 28 20 70 57 61 6c assert( pWal
bd00: 6b 65 72 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d ker->pParse->db-
bd10: 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
bd20: 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f . return WRC_
bd30: 41 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 66 6f 72 Abort;. }. for
bd40: 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 (i=0; i<pSrc->nS
bd50: 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 rc; i++){. st
bd60: 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
bd70: 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 m *pItem = &pSrc
bd80: 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 ->a[i];. if(
bd90: 70 49 74 65 6d 2d 3e 70 54 61 62 3d 3d 70 2d 3e pItem->pTab==p->
bda0: 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 72 65 pTab ){. re
bdb0: 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 70 57 nameTokenFind(pW
bdc0: 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 2c 20 70 alker->pParse, p
bdd0: 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b , pItem->zName);
bde0: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 6e . }. }. ren
bdf0: 61 6d 65 57 61 6c 6b 57 69 74 68 28 70 57 61 6c ameWalkWith(pWal
be00: 6b 65 72 2c 20 70 53 65 6c 65 63 74 29 3b 0a 0a ker, pSelect);..
be10: 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e return WRC_Con
be20: 74 69 6e 75 65 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a tinue;.}.../*.**
be30: 20 54 68 69 73 20 43 20 66 75 6e 63 74 69 6f 6e This C function
be40: 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 53 implements an S
be50: 51 4c 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e QL user function
be60: 20 74 68 61 74 20 69 73 20 75 73 65 64 20 62 79 that is used by
be70: 20 53 51 4c 20 63 6f 64 65 0a 2a 2a 20 67 65 6e SQL code.** gen
be80: 65 72 61 74 65 64 20 62 79 20 74 68 65 20 41 4c erated by the AL
be90: 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 52 45 TER TABLE ... RE
bea0: 4e 41 4d 45 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 NAME command to
beb0: 6d 6f 64 69 66 79 20 74 68 65 20 64 65 66 69 6e modify the defin
bec0: 69 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 6e 79 20 ition.** of any
bed0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
bee0: 74 72 61 69 6e 74 73 20 74 68 61 74 20 75 73 65 traints that use
bef0: 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
bf00: 20 72 65 6e 61 6d 65 64 20 61 73 20 74 68 65 20 renamed as the
bf10: 0a 2a 2a 20 70 61 72 65 6e 74 20 74 61 62 6c 65 .** parent table
bf20: 2e 20 49 74 20 69 73 20 70 61 73 73 65 64 20 74 . It is passed t
bf30: 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 0a hree arguments:.
bf40: 2a 2a 0a 2a 2a 20 20 20 30 3a 20 54 68 65 20 64 **.** 0: The d
bf50: 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 atabase containi
bf60: 6e 67 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 ng the table bei
bf70: 6e 67 20 72 65 6e 61 6d 65 64 2e 0a 2a 2a 20 20 ng renamed..**
bf80: 20 31 2e 20 74 79 70 65 3a 20 20 20 20 20 54 79 1. type: Ty
bf90: 70 65 20 6f 66 20 6f 62 6a 65 63 74 20 28 22 74 pe of object ("t
bfa0: 61 62 6c 65 22 2c 20 22 76 69 65 77 22 20 65 74 able", "view" et
bfb0: 63 2e 29 0a 2a 2a 20 20 20 32 2e 20 6f 62 6a 65 c.).** 2. obje
bfc0: 63 74 3a 20 20 20 4e 61 6d 65 20 6f 66 20 6f 62 ct: Name of ob
bfd0: 6a 65 63 74 0a 2a 2a 20 20 20 33 3a 20 54 68 65 ject.** 3: The
bfe0: 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f complete text o
bff0: 66 20 74 68 65 20 73 63 68 65 6d 61 20 73 74 61 f the schema sta
c000: 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 6d 6f 64 tement being mod
c010: 69 66 69 65 64 2c 0a 2a 2a 20 20 20 34 3a 20 54 ified,.** 4: T
c020: 68 65 20 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 he old name of t
c030: 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 he table being r
c040: 65 6e 61 6d 65 64 2c 20 61 6e 64 0a 2a 2a 20 20 enamed, and.**
c050: 20 35 3a 20 54 68 65 20 6e 65 77 20 6e 61 6d 65 5: The new name
c060: 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 of the table be
c070: 69 6e 67 20 72 65 6e 61 6d 65 64 2e 0a 2a 2a 20 ing renamed..**
c080: 20 20 36 3a 20 54 72 75 65 20 69 66 20 74 68 65 6: True if the
c090: 20 73 63 68 65 6d 61 20 73 74 61 74 65 6d 65 6e schema statemen
c0a0: 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 74 68 65 t comes from the
c0b0: 20 74 65 6d 70 20 64 62 2e 0a 2a 2a 0a 2a 2a 20 temp db..**.**
c0c0: 49 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e It returns the n
c0d0: 65 77 20 73 63 68 65 6d 61 20 73 74 61 74 65 6d ew schema statem
c0e0: 65 6e 74 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 ent. For example
c0f0: 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 :.**.** sqlite_r
c100: 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 6d 61 69 ename_table('mai
c110: 6e 27 2c 20 27 43 52 45 41 54 45 20 54 41 42 4c n', 'CREATE TABL
c120: 45 20 74 31 28 61 20 52 45 46 45 52 45 4e 43 45 E t1(a REFERENCE
c130: 53 20 74 32 29 27 2c 27 74 32 27 2c 27 74 33 27 S t2)','t2','t3'
c140: 2c 30 29 0a 2a 2a 20 20 20 20 20 20 20 2d 3e 20 ,0).** ->
c150: 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 'CREATE TABLE t1
c160: 28 61 20 52 45 46 45 52 45 4e 43 45 53 20 74 33 (a REFERENCES t3
c170: 29 27 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )'.*/.static voi
c180: 64 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e d renameTableFun
c190: 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
c1a0: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
c1b0: 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 int NotUsed,.
c1c0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
c1d0: 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 argv.){. sqlite
c1e0: 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 3 *db = sqlite3_
c1f0: 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
c200: 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 6f e(context);. co
c210: 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 nst char *zDb =
c220: 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c (const char*)sql
c230: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
c240: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e 73 argv[0]);. cons
c250: 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 20 3d t char *zInput =
c260: 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 (const char*)sq
c270: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
c280: 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 63 6f 6e (argv[3]);. con
c290: 73 74 20 63 68 61 72 20 2a 7a 4f 6c 64 20 3d 20 st char *zOld =
c2a0: 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c (const char*)sql
c2b0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
c2c0: 61 72 67 76 5b 34 5d 29 3b 0a 20 20 63 6f 6e 73 argv[4]);. cons
c2d0: 74 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 28 t char *zNew = (
c2e0: 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 const char*)sqli
c2f0: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
c300: 72 67 76 5b 35 5d 29 3b 0a 20 20 69 6e 74 20 62 rgv[5]);. int b
c310: 54 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 Temp = sqlite3_v
c320: 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 36 5d alue_int(argv[6]
c330: 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
c340: 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
c350: 0a 20 20 69 66 28 20 7a 49 6e 70 75 74 20 26 26 . if( zInput &&
c360: 20 7a 4f 6c 64 20 26 26 20 7a 4e 65 77 20 29 7b zOld && zNew ){
c370: 0a 20 20 20 20 50 61 72 73 65 20 73 50 61 72 73 . Parse sPars
c380: 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 e;. int rc;.
c390: 20 20 20 69 6e 74 20 62 51 75 6f 74 65 20 3d 20 int bQuote =
c3a0: 31 3b 0a 20 20 20 20 52 65 6e 61 6d 65 43 74 78 1;. RenameCtx
c3b0: 20 73 43 74 78 3b 0a 20 20 20 20 57 61 6c 6b 65 sCtx;. Walke
c3c0: 72 20 73 57 61 6c 6b 65 72 3b 0a 0a 23 69 66 6e r sWalker;..#ifn
c3d0: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
c3e0: 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 AUTHORIZATION.
c3f0: 20 20 73 71 6c 69 74 65 33 5f 78 61 75 74 68 20 sqlite3_xauth
c400: 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 xAuth = db->xAut
c410: 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68 h;. db->xAuth
c420: 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 = 0;.#endif..
c430: 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
c440: 74 65 72 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 20 terAll(db);..
c450: 20 6d 65 6d 73 65 74 28 26 73 43 74 78 2c 20 30 memset(&sCtx, 0
c460: 2c 20 73 69 7a 65 6f 66 28 52 65 6e 61 6d 65 43 , sizeof(RenameC
c470: 74 78 29 29 3b 0a 20 20 20 20 73 43 74 78 2e 70 tx));. sCtx.p
c480: 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e Tab = sqlite3Fin
c490: 64 54 61 62 6c 65 28 64 62 2c 20 7a 4f 6c 64 2c dTable(db, zOld,
c4a0: 20 7a 44 62 29 3b 0a 20 20 20 20 6d 65 6d 73 65 zDb);. memse
c4b0: 74 28 26 73 57 61 6c 6b 65 72 2c 20 30 2c 20 73 t(&sWalker, 0, s
c4c0: 69 7a 65 6f 66 28 57 61 6c 6b 65 72 29 29 3b 0a izeof(Walker));.
c4d0: 20 20 20 20 73 57 61 6c 6b 65 72 2e 70 50 61 72 sWalker.pPar
c4e0: 73 65 20 3d 20 26 73 50 61 72 73 65 3b 0a 20 20 se = &sParse;.
c4f0: 20 20 73 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 sWalker.xExprC
c500: 61 6c 6c 62 61 63 6b 20 3d 20 72 65 6e 61 6d 65 allback = rename
c510: 54 61 62 6c 65 45 78 70 72 43 62 3b 0a 20 20 20 TableExprCb;.
c520: 20 73 57 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74 sWalker.xSelect
c530: 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 6e 61 6d Callback = renam
c540: 65 54 61 62 6c 65 53 65 6c 65 63 74 43 62 3b 0a eTableSelectCb;.
c550: 20 20 20 20 73 57 61 6c 6b 65 72 2e 75 2e 70 52 sWalker.u.pR
c560: 65 6e 61 6d 65 20 3d 20 26 73 43 74 78 3b 0a 0a ename = &sCtx;..
c570: 20 20 20 20 72 63 20 3d 20 72 65 6e 61 6d 65 50 rc = renameP
c580: 61 72 73 65 53 71 6c 28 26 73 50 61 72 73 65 2c arseSql(&sParse,
c590: 20 7a 44 62 2c 20 31 2c 20 64 62 2c 20 7a 49 6e zDb, 1, db, zIn
c5a0: 70 75 74 2c 20 62 54 65 6d 70 29 3b 0a 0a 20 20 put, bTemp);..
c5b0: 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
c5c0: 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 _OK ){. int
c5d0: 20 69 73 4c 65 67 61 63 79 20 3d 20 28 64 62 2d isLegacy = (db-
c5e0: 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
c5f0: 4c 65 67 61 63 79 41 6c 74 65 72 29 3b 0a 20 20 LegacyAlter);.
c600: 20 20 20 20 69 66 28 20 73 50 61 72 73 65 2e 70 if( sParse.p
c610: 4e 65 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 NewTable ){.
c620: 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 Table *pTab
c630: 3d 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 = sParse.pNewTab
c640: 6c 65 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 le;.. if(
c650: 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 pTab->pSelect )
c660: 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
c670: 69 73 4c 65 67 61 63 79 3d 3d 30 20 29 7b 0a 20 isLegacy==0 ){.
c680: 20 20 20 20 20 20 20 20 20 20 20 4e 61 6d 65 43 NameC
c690: 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 ontext sNC;.
c6a0: 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 memset(&
c6b0: 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 sNC, 0, sizeof(s
c6c0: 4e 43 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 NC));.
c6d0: 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 26 sNC.pParse = &
c6e0: 73 50 61 72 73 65 3b 0a 0a 20 20 20 20 20 20 20 sParse;..
c6f0: 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
c700: 63 74 50 72 65 70 28 26 73 50 61 72 73 65 2c 20 ctPrep(&sParse,
c710: 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c 20 26 pTab->pSelect, &
c720: 73 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 sNC);.
c730: 20 20 69 66 28 20 73 50 61 72 73 65 2e 6e 45 72 if( sParse.nEr
c740: 72 20 29 20 72 63 20 3d 20 73 50 61 72 73 65 2e r ) rc = sParse.
c750: 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 rc;.
c760: 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
c770: 74 28 26 73 57 61 6c 6b 65 72 2c 20 70 54 61 62 t(&sWalker, pTab
c780: 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 ->pSelect);.
c790: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
c7a0: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
c7b0: 20 2f 2a 20 4d 6f 64 69 66 79 20 61 6e 79 20 46 /* Modify any F
c7c0: 4b 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 74 6f K definitions to
c7d0: 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 point to the ne
c7e0: 77 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 69 66 6e w table. */.#ifn
c7f0: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
c800: 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 FOREIGN_KEY.
c810: 20 20 20 20 20 20 69 66 28 20 69 73 4c 65 67 61 if( isLega
c820: 63 79 3d 3d 30 20 7c 7c 20 28 64 62 2d 3e 66 6c cy==0 || (db->fl
c830: 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 6f 72 ags & SQLITE_For
c840: 65 69 67 6e 4b 65 79 73 29 20 29 7b 0a 20 20 20 eignKeys) ){.
c850: 20 20 20 20 20 20 20 20 20 46 4b 65 79 20 2a 70 FKey *p
c860: 46 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 FKey;.
c870: 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 for(pFKey=pTab
c880: 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 ->pFKey; pFKey;
c890: 70 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 pFKey=pFKey->pNe
c8a0: 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 20 xtFrom){.
c8b0: 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
c8c0: 65 33 5f 73 74 72 69 63 6d 70 28 70 46 4b 65 79 e3_stricmp(pFKey
c8d0: 2d 3e 7a 54 6f 2c 20 7a 4f 6c 64 29 3d 3d 30 20 ->zTo, zOld)==0
c8e0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
c8f0: 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 renameTokenFi
c900: 6e 64 28 26 73 50 61 72 73 65 2c 20 26 73 43 74 nd(&sParse, &sCt
c910: 78 2c 20 28 76 6f 69 64 2a 29 70 46 4b 65 79 2d x, (void*)pFKey-
c920: 3e 7a 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 >zTo);.
c930: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
c940: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
c950: 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 20 .#endif..
c960: 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 /* If this is
c970: 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
c980: 20 61 6c 74 65 72 65 64 2c 20 66 69 78 20 61 6e altered, fix an
c990: 79 20 74 61 62 6c 65 20 72 65 66 73 20 69 6e 20 y table refs in
c9a0: 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 20 20 CHECK.
c9b0: 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 ** expressions.
c9c0: 41 6c 73 6f 20 75 70 64 61 74 65 20 74 68 65 20 Also update the
c9d0: 6e 61 6d 65 20 74 68 61 74 20 61 70 70 65 61 72 name that appear
c9e0: 73 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68 s right after th
c9f0: 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 22 e. ** "
ca00: 43 52 45 41 54 45 20 5b 56 49 52 54 55 41 4c 5d CREATE [VIRTUAL]
ca10: 20 54 41 42 4c 45 22 20 62 69 74 2e 20 2a 2f 0a TABLE" bit. */.
ca20: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
ca30: 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 7a 4f lite3_stricmp(zO
ca40: 6c 64 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 ld, pTab->zName)
ca50: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
ca60: 20 20 20 73 43 74 78 2e 70 54 61 62 20 3d 20 70 sCtx.pTab = p
ca70: 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 Tab;.
ca80: 20 69 66 28 20 69 73 4c 65 67 61 63 79 3d 3d 30 if( isLegacy==0
ca90: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
caa0: 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 sqlite3WalkExp
cab0: 72 4c 69 73 74 28 26 73 57 61 6c 6b 65 72 2c 20 rList(&sWalker,
cac0: 70 54 61 62 2d 3e 70 43 68 65 63 6b 29 3b 0a 20 pTab->pCheck);.
cad0: 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
cae0: 20 20 20 20 20 20 20 20 20 72 65 6e 61 6d 65 54 renameT
caf0: 6f 6b 65 6e 46 69 6e 64 28 26 73 50 61 72 73 65 okenFind(&sParse
cb00: 2c 20 26 73 43 74 78 2c 20 70 54 61 62 2d 3e 7a , &sCtx, pTab->z
cb10: 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Name);.
cb20: 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
cb30: 20 20 20 7d 0a 0a 20 20 20 20 20 20 65 6c 73 65 }.. else
cb40: 20 69 66 28 20 73 50 61 72 73 65 2e 70 4e 65 77 if( sParse.pNew
cb50: 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 Index ){.
cb60: 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 renameTokenFind
cb70: 28 26 73 50 61 72 73 65 2c 20 26 73 43 74 78 2c (&sParse, &sCtx,
cb80: 20 73 50 61 72 73 65 2e 70 4e 65 77 49 6e 64 65 sParse.pNewInde
cb90: 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 x->zName);.
cba0: 20 20 20 69 66 28 20 69 73 4c 65 67 61 63 79 3d if( isLegacy=
cbb0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
cbc0: 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
cbd0: 26 73 57 61 6c 6b 65 72 2c 20 73 50 61 72 73 65 &sWalker, sParse
cbe0: 2e 70 4e 65 77 49 6e 64 65 78 2d 3e 70 50 61 72 .pNewIndex->pPar
cbf0: 74 49 64 78 57 68 65 72 65 29 3b 0a 20 20 20 20 tIdxWhere);.
cc00: 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 }. }..#
cc10: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
cc20: 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 IT_TRIGGER.
cc30: 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 54 else{. T
cc40: 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 rigger *pTrigger
cc50: 20 3d 20 73 50 61 72 73 65 2e 70 4e 65 77 54 72 = sParse.pNewTr
cc60: 69 67 67 65 72 3b 0a 20 20 20 20 20 20 20 20 54 igger;. T
cc70: 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 riggerStep *pSte
cc80: 70 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 p;. if( 0
cc90: 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d ==sqlite3_stricm
cca0: 70 28 73 50 61 72 73 65 2e 70 4e 65 77 54 72 69 p(sParse.pNewTri
ccb0: 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20 7a 4f 6c gger->table, zOl
ccc0: 64 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 d) .
ccd0: 26 26 20 73 43 74 78 2e 70 54 61 62 2d 3e 70 53 && sCtx.pTab->pS
cce0: 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d chema==pTrigger-
ccf0: 3e 70 54 61 62 53 63 68 65 6d 61 0a 20 20 20 20 >pTabSchema.
cd00: 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
cd10: 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 renameTokenFi
cd20: 6e 64 28 26 73 50 61 72 73 65 2c 20 26 73 43 74 nd(&sParse, &sCt
cd30: 78 2c 20 73 50 61 72 73 65 2e 70 4e 65 77 54 72 x, sParse.pNewTr
cd40: 69 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 igger->table);.
cd50: 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
cd60: 20 20 69 66 28 20 69 73 4c 65 67 61 63 79 3d 3d if( isLegacy==
cd70: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
cd80: 63 20 3d 20 72 65 6e 61 6d 65 52 65 73 6f 6c 76 c = renameResolv
cd90: 65 54 72 69 67 67 65 72 28 26 73 50 61 72 73 65 eTrigger(&sParse
cda0: 2c 20 62 54 65 6d 70 20 3f 20 30 20 3a 20 7a 44 , bTemp ? 0 : zD
cdb0: 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 b);. if
cdc0: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
cdd0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 ){. r
cde0: 65 6e 61 6d 65 57 61 6c 6b 54 72 69 67 67 65 72 enameWalkTrigger
cdf0: 28 26 73 57 61 6c 6b 65 72 2c 20 70 54 72 69 67 (&sWalker, pTrig
ce00: 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ger);.
ce10: 20 20 66 6f 72 28 70 53 74 65 70 3d 70 54 72 69 for(pStep=pTri
ce20: 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74 3b gger->step_list;
ce30: 20 70 53 74 65 70 3b 20 70 53 74 65 70 3d 70 53 pStep; pStep=pS
ce40: 74 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 tep->pNext){.
ce50: 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 if( p
ce60: 53 74 65 70 2d 3e 7a 54 61 72 67 65 74 20 26 26 Step->zTarget &&
ce70: 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72 69 0==sqlite3_stri
ce80: 63 6d 70 28 70 53 74 65 70 2d 3e 7a 54 61 72 67 cmp(pStep->zTarg
ce90: 65 74 2c 20 7a 4f 6c 64 29 20 29 7b 0a 20 20 20 et, zOld) ){.
cea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e ren
ceb0: 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 26 73 50 ameTokenFind(&sP
cec0: 61 72 73 65 2c 20 26 73 43 74 78 2c 20 70 53 74 arse, &sCtx, pSt
ced0: 65 70 2d 3e 7a 54 61 72 67 65 74 29 3b 0a 20 20 ep->zTarget);.
cee0: 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
cef0: 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
cf00: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
cf10: 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
cf20: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
cf30: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
cf40: 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 6e 61 . rc = rena
cf50: 6d 65 45 64 69 74 53 71 6c 28 63 6f 6e 74 65 78 meEditSql(contex
cf60: 74 2c 20 26 73 43 74 78 2c 20 7a 49 6e 70 75 74 t, &sCtx, zInput
cf70: 2c 20 7a 4e 65 77 2c 20 62 51 75 6f 74 65 29 3b , zNew, bQuote);
cf80: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
cf90: 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
cfa0: 20 20 20 20 20 20 69 66 28 20 73 50 61 72 73 65 if( sParse
cfb0: 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 .zErrMsg ){.
cfc0: 20 20 20 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e renameColumn
cfd0: 50 61 72 73 65 45 72 72 6f 72 28 63 6f 6e 74 65 ParseError(conte
cfe0: 78 74 2c 20 30 2c 20 61 72 67 76 5b 31 5d 2c 20 xt, 0, argv[1],
cff0: 61 72 67 76 5b 32 5d 2c 20 26 73 50 61 72 73 65 argv[2], &sParse
d000: 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
d010: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
d020: 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 result_error_cod
d030: 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a e(context, rc);.
d040: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
d050: 20 20 20 72 65 6e 61 6d 65 50 61 72 73 65 43 6c renameParseCl
d060: 65 61 6e 75 70 28 26 73 50 61 72 73 65 29 3b 0a eanup(&sParse);.
d070: 20 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 renameTokenF
d080: 72 65 65 28 64 62 2c 20 73 43 74 78 2e 70 4c 69 ree(db, sCtx.pLi
d090: 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 st);. sqlite3
d0a0: 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 BtreeLeaveAll(db
d0b0: 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
d0c0: 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 E_OMIT_AUTHORIZA
d0d0: 54 49 4f 4e 0a 20 20 20 20 64 62 2d 3e 78 41 75 TION. db->xAu
d0e0: 74 68 20 3d 20 78 41 75 74 68 3b 0a 23 65 6e 64 th = xAuth;.#end
d0f0: 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e if. }.. return
d100: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 ;.}../*.** An SQ
d110: 4c 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 L user function
d120: 74 68 61 74 20 63 68 65 63 6b 73 20 74 68 61 74 that checks that
d130: 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 there are no pa
d140: 72 73 65 20 6f 72 20 73 79 6d 62 6f 6c 0a 2a 2a rse or symbol.**
d150: 20 72 65 73 6f 6c 75 74 69 6f 6e 20 70 72 6f 62 resolution prob
d160: 6c 65 6d 73 20 69 6e 20 61 20 43 52 45 41 54 45 lems in a CREATE
d170: 20 54 52 49 47 47 45 52 7c 54 41 42 4c 45 7c 56 TRIGGER|TABLE|V
d180: 49 45 57 7c 49 4e 44 45 58 20 73 74 61 74 65 6d IEW|INDEX statem
d190: 65 6e 74 2e 0a 2a 2a 20 41 66 74 65 72 20 61 6e ent..** After an
d1a0: 20 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 20 ALTER TABLE ..
d1b0: 52 45 4e 41 4d 45 20 6f 70 65 72 61 74 69 6f 6e RENAME operation
d1c0: 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 61 6e is performed an
d1d0: 64 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20 d the schema.**
d1e0: 72 65 6c 6f 61 64 65 64 2c 20 74 68 69 73 20 66 reloaded, this f
d1f0: 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
d200: 64 20 6f 6e 20 65 61 63 68 20 53 51 4c 20 73 74 d on each SQL st
d210: 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 73 atement in the s
d220: 63 68 65 6d 61 0a 2a 2a 20 74 6f 20 65 6e 73 75 chema.** to ensu
d230: 72 65 20 74 68 61 74 20 69 74 20 69 73 20 73 74 re that it is st
d240: 69 6c 6c 20 75 73 61 62 6c 65 2e 0a 2a 2a 0a 2a ill usable..**.*
d250: 2a 20 20 20 30 3a 20 44 61 74 61 62 61 73 65 20 * 0: Database
d260: 6e 61 6d 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 name ("main", "t
d270: 65 6d 70 22 20 65 74 63 2e 29 2e 0a 2a 2a 20 20 emp" etc.)..**
d280: 20 31 3a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 1: SQL statemen
d290: 74 2e 0a 2a 2a 20 20 20 32 3a 20 4f 62 6a 65 63 t..** 2: Objec
d2a0: 74 20 74 79 70 65 20 28 22 76 69 65 77 22 2c 20 t type ("view",
d2b0: 22 74 61 62 6c 65 22 2c 20 22 74 72 69 67 67 65 "table", "trigge
d2c0: 72 22 20 6f 72 20 22 69 6e 64 65 78 22 29 2e 0a r" or "index")..
d2d0: 2a 2a 20 20 20 33 3a 20 4f 62 6a 65 63 74 20 6e ** 3: Object n
d2e0: 61 6d 65 2e 0a 2a 2a 20 20 20 34 3a 20 54 72 75 ame..** 4: Tru
d2f0: 65 20 69 66 20 6f 62 6a 65 63 74 20 69 73 20 66 e if object is f
d300: 72 6f 6d 20 74 65 6d 70 20 73 63 68 65 6d 61 2e rom temp schema.
d310: 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 69 74 .**.** Unless it
d320: 20 66 69 6e 64 73 20 61 6e 20 65 72 72 6f 72 2c finds an error,
d330: 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6e this function n
d340: 6f 72 6d 61 6c 6c 79 20 72 65 74 75 72 6e 73 20 ormally returns
d350: 4e 55 4c 4c 2e 20 48 6f 77 65 76 65 72 2c 20 69 NULL. However, i
d360: 74 0a 2a 2a 20 72 65 74 75 72 6e 73 20 69 6e 74 t.** returns int
d370: 65 67 65 72 20 76 61 6c 75 65 20 31 20 69 66 3a eger value 1 if:
d380: 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 53 .**.** * the S
d390: 51 4c 20 61 72 67 75 6d 65 6e 74 20 63 72 65 61 QL argument crea
d3a0: 74 65 73 20 61 20 74 72 69 67 67 65 72 2c 20 61 tes a trigger, a
d3b0: 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20 74 61 nd.** * the ta
d3c0: 62 6c 65 20 74 68 61 74 20 74 68 65 20 74 72 69 ble that the tri
d3d0: 67 67 65 72 20 69 73 20 61 74 74 61 63 68 65 64 gger is attached
d3e0: 20 74 6f 20 69 73 20 69 6e 20 64 61 74 61 62 61 to is in databa
d3f0: 73 65 20 7a 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 se zDb..*/.stati
d400: 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 c void renameTab
d410: 6c 65 54 65 73 74 28 0a 20 20 73 71 6c 69 74 65 leTest(. sqlite
d420: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
d430: 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
d440: 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
d450: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 ue **argv.){. s
d460: 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c qlite3 *db = sql
d470: 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
d480: 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b handle(context);
d490: 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a . char const *z
d4a0: 44 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 Db = (const char
d4b0: 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *)sqlite3_value_
d4c0: 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 text(argv[0]);.
d4d0: 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 49 6e char const *zIn
d4e0: 70 75 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 put = (const cha
d4f0: 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r*)sqlite3_value
d500: 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a _text(argv[1]);.
d510: 20 20 69 6e 74 20 62 54 65 6d 70 20 3d 20 73 71 int bTemp = sq
d520: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 lite3_value_int(
d530: 61 72 67 76 5b 34 5d 29 3b 0a 20 20 69 6e 74 20 argv[4]);. int
d540: 69 73 4c 65 67 61 63 79 20 3d 20 28 64 62 2d 3e isLegacy = (db->
d550: 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4c flags & SQLITE_L
d560: 65 67 61 63 79 41 6c 74 65 72 29 3b 0a 0a 23 69 egacyAlter);..#i
d570: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
d580: 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a T_AUTHORIZATION.
d590: 20 20 73 71 6c 69 74 65 33 5f 78 61 75 74 68 20 sqlite3_xauth
d5a0: 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 xAuth = db->xAut
d5b0: 68 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d h;. db->xAuth =
d5c0: 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 55 4e 0;.#endif.. UN
d5d0: 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
d5e0: 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 7a otUsed);. if( z
d5f0: 44 62 20 26 26 20 7a 49 6e 70 75 74 20 29 7b 0a Db && zInput ){.
d600: 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 int rc;.
d610: 50 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 Parse sParse;.
d620: 20 20 72 63 20 3d 20 72 65 6e 61 6d 65 50 61 72 rc = renamePar
d630: 73 65 53 71 6c 28 26 73 50 61 72 73 65 2c 20 7a seSql(&sParse, z
d640: 44 62 2c 20 31 2c 20 64 62 2c 20 7a 49 6e 70 75 Db, 1, db, zInpu
d650: 74 2c 20 62 54 65 6d 70 29 3b 0a 20 20 20 20 69 t, bTemp);. i
d660: 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
d670: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 ){. if( is
d680: 4c 65 67 61 63 79 3d 3d 30 20 26 26 20 73 50 61 Legacy==0 && sPa
d690: 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 20 26 26 rse.pNewTable &&
d6a0: 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c sParse.pNewTabl
d6b0: 65 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 e->pSelect ){.
d6c0: 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 NameContex
d6d0: 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 20 20 6d t sNC;. m
d6e0: 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 emset(&sNC, 0, s
d6f0: 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 izeof(sNC));.
d700: 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 sNC.pParse
d710: 3d 20 26 73 50 61 72 73 65 3b 0a 20 20 20 20 20 = &sParse;.
d720: 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
d730: 50 72 65 70 28 26 73 50 61 72 73 65 2c 20 73 50 Prep(&sParse, sP
d740: 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e arse.pNewTable->
d750: 70 53 65 6c 65 63 74 2c 20 26 73 4e 43 29 3b 0a pSelect, &sNC);.
d760: 20 20 20 20 20 20 20 20 69 66 28 20 73 50 61 72 if( sPar
d770: 73 65 2e 6e 45 72 72 20 29 20 72 63 20 3d 20 73 se.nErr ) rc = s
d780: 50 61 72 73 65 2e 72 63 3b 0a 20 20 20 20 20 20 Parse.rc;.
d790: 7d 0a 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 }.. else if
d7a0: 28 20 73 50 61 72 73 65 2e 70 4e 65 77 54 72 69 ( sParse.pNewTri
d7b0: 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 gger ){.
d7c0: 69 66 28 20 69 73 4c 65 67 61 63 79 3d 3d 30 20 if( isLegacy==0
d7d0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
d7e0: 3d 20 72 65 6e 61 6d 65 52 65 73 6f 6c 76 65 54 = renameResolveT
d7f0: 72 69 67 67 65 72 28 26 73 50 61 72 73 65 2c 20 rigger(&sParse,
d800: 62 54 65 6d 70 20 3f 20 30 20 3a 20 7a 44 62 29 bTemp ? 0 : zDb)
d810: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
d820: 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
d830: 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
d840: 20 20 20 69 6e 74 20 69 31 20 3d 20 73 71 6c 69 int i1 = sqli
d850: 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
d860: 28 64 62 2c 20 73 50 61 72 73 65 2e 70 4e 65 77 (db, sParse.pNew
d870: 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 Trigger->pTabSch
d880: 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ema);.
d890: 69 6e 74 20 69 32 20 3d 20 73 71 6c 69 74 65 33 int i2 = sqlite3
d8a0: 46 69 6e 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a FindDbName(db, z
d8b0: 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 Db);. i
d8c0: 66 28 20 69 31 3d 3d 69 32 20 29 20 73 71 6c 69 f( i1==i2 ) sqli
d8d0: 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 te3_result_int(c
d8e0: 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 20 20 ontext, 1);.
d8f0: 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
d900: 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21 }.. if( rc!
d910: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
d920: 20 20 20 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e renameColumn
d930: 50 61 72 73 65 45 72 72 6f 72 28 63 6f 6e 74 65 ParseError(conte
d940: 78 74 2c 20 31 2c 20 61 72 67 76 5b 32 5d 2c 20 xt, 1, argv[2],
d950: 61 72 67 76 5b 33 5d 2c 20 26 73 50 61 72 73 65 argv[3], &sParse
d960: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6e );. }. ren
d970: 61 6d 65 50 61 72 73 65 43 6c 65 61 6e 75 70 28 ameParseCleanup(
d980: 26 73 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 23 &sParse);. }..#
d990: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
d9a0: 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
d9b0: 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 . db->xAuth = x
d9c0: 41 75 74 68 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a Auth;.#endif.}..
d9d0: 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 62 /*.** Register b
d9e0: 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e uilt-in function
d9f0: 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 s used to help i
da00: 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52 20 54 mplement ALTER T
da10: 41 42 4c 45 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ABLE.*/.void sql
da20: 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f ite3AlterFunctio
da30: 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ns(void){. stat
da40: 69 63 20 46 75 6e 63 44 65 66 20 61 41 6c 74 65 ic FuncDef aAlte
da50: 72 54 61 62 6c 65 46 75 6e 63 73 5b 5d 20 3d 20 rTableFuncs[] =
da60: 7b 0a 20 20 20 20 49 4e 54 45 52 4e 41 4c 5f 46 {. INTERNAL_F
da70: 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 72 UNCTION(sqlite_r
da80: 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 2c 20 31 30 ename_column, 10
da90: 2c 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 46 75 , renameColumnFu
daa0: 6e 63 29 2c 0a 20 20 20 20 49 4e 54 45 52 4e 41 nc),. INTERNA
dab0: 4c 5f 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 L_FUNCTION(sqlit
dac0: 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 2c 20 e_rename_table,
dad0: 20 37 2c 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 7, renameTableF
dae0: 75 6e 63 29 2c 0a 20 20 20 20 49 4e 54 45 52 4e unc),. INTERN
daf0: 41 4c 5f 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 AL_FUNCTION(sqli
db00: 74 65 5f 72 65 6e 61 6d 65 5f 74 65 73 74 2c 20 te_rename_test,
db10: 20 20 35 2c 20 72 65 6e 61 6d 65 54 61 62 6c 65 5, renameTable
db20: 54 65 73 74 29 2c 0a 20 20 7d 3b 0a 20 20 73 71 Test),. };. sq
db30: 6c 69 74 65 33 49 6e 73 65 72 74 42 75 69 6c 74 lite3InsertBuilt
db40: 69 6e 46 75 6e 63 73 28 61 41 6c 74 65 72 54 61 inFuncs(aAlterTa
db50: 62 6c 65 46 75 6e 63 73 2c 20 41 72 72 61 79 53 bleFuncs, ArrayS
db60: 69 7a 65 28 61 41 6c 74 65 72 54 61 62 6c 65 46 ize(aAlterTableF
db70: 75 6e 63 73 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 uncs));.}.#endif
db80: 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c 54 45 /* SQLITE_ALTE
db90: 52 5f 54 41 42 4c 45 20 2a 2f 0a R_TABLE */.