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 53 79 73 74 65 6d 54 61 62 6c 65 28 50 61 isSystemTable(Pa
0410: 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e rse *pParse, con
0420: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b st char *zName){
0430: 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 . if( 0==sqlite
0440: 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 3StrNICmp(zName,
0450: 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 "sqlite_", 7) )
0460: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
0470: 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 orMsg(pParse, "t
0480: 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 able %s may not
0490: 62 65 20 61 6c 74 65 72 65 64 22 2c 20 7a 4e 61 be altered", zNa
04a0: 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 me);. return
04b0: 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
04c0: 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 0;.}../*.** Gene
04d0: 72 61 74 65 20 63 6f 64 65 20 74 6f 20 76 65 72 rate code to ver
04e0: 69 66 79 20 74 68 61 74 20 74 68 65 20 73 63 68 ify that the sch
04f0: 65 6d 61 73 20 6f 66 20 64 61 74 61 62 61 73 65 emas of database
0500: 20 7a 44 62 20 61 6e 64 2c 20 69 66 0a 2a 2a 20 zDb and, if.**
0510: 62 54 65 6d 70 20 69 73 20 6e 6f 74 20 74 72 75 bTemp is not tru
0520: 65 2c 20 64 61 74 61 62 61 73 65 20 22 74 65 6d e, database "tem
0530: 70 22 2c 20 63 61 6e 20 73 74 69 6c 6c 20 62 65 p", can still be
0540: 20 70 61 72 73 65 64 2e 20 54 68 69 73 20 69 73 parsed. This is
0550: 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 74 68 .** called at th
0560: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 67 65 6e e end of the gen
0570: 65 72 61 74 69 6f 6e 20 6f 66 20 61 6e 20 41 4c eration of an AL
0580: 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 52 45 TER TABLE ... RE
0590: 4e 41 4d 45 20 2e 2e 2e 0a 2a 2a 20 73 74 61 74 NAME ....** stat
05a0: 65 6d 65 6e 74 20 74 6f 20 65 6e 73 75 72 65 20 ement to ensure
05b0: 74 68 61 74 20 74 68 65 20 6f 70 65 72 61 74 69 that the operati
05c0: 6f 6e 20 68 61 73 20 6e 6f 74 20 72 65 6e 64 65 on has not rende
05d0: 72 65 64 20 61 6e 79 20 73 63 68 65 6d 61 0a 2a red any schema.*
05e0: 2a 20 6f 62 6a 65 63 74 73 20 75 6e 75 73 61 62 * objects unusab
05f0: 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
0600: 69 64 20 72 65 6e 61 6d 65 54 65 73 74 53 63 68 id renameTestSch
0610: 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 ema(Parse *pPars
0620: 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
0630: 44 62 2c 20 69 6e 74 20 62 54 65 6d 70 29 7b 0a Db, int bTemp){.
0640: 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 sqlite3NestedP
0650: 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 arse(pParse, .
0660: 20 20 20 20 22 53 45 4c 45 43 54 20 31 20 22 0a "SELECT 1 ".
0670: 20 20 20 20 20 20 22 46 52 4f 4d 20 5c 22 25 77 "FROM \"%w
0680: 5c 22 2e 25 73 20 22 0a 20 20 20 20 20 20 22 57 \".%s ". "W
0690: 48 45 52 45 20 6e 61 6d 65 20 4e 4f 54 20 4c 49 HERE name NOT LI
06a0: 4b 45 20 27 73 71 6c 69 74 65 5f 25 25 27 22 0a KE 'sqlite_%%'".
06b0: 20 20 20 20 20 20 22 20 41 4e 44 20 73 71 6c 20 " AND sql
06c0: 4e 4f 54 20 4c 49 4b 45 20 27 63 72 65 61 74 65 NOT LIKE 'create
06d0: 20 76 69 72 74 75 61 6c 25 25 27 22 0a 20 20 20 virtual%%'".
06e0: 20 20 20 22 20 41 4e 44 20 73 71 6c 69 74 65 5f " AND sqlite_
06f0: 72 65 6e 61 6d 65 5f 74 65 73 74 28 25 51 2c 20 rename_test(%Q,
0700: 73 71 6c 2c 20 74 79 70 65 2c 20 6e 61 6d 65 2c sql, type, name,
0710: 20 25 64 29 3d 4e 55 4c 4c 20 22 2c 0a 20 20 20 %d)=NULL ",.
0720: 20 20 20 7a 44 62 2c 20 4d 41 53 54 45 52 5f 4e zDb, MASTER_N
0730: 41 4d 45 2c 20 0a 20 20 20 20 20 20 7a 44 62 2c AME, . zDb,
0740: 20 62 54 65 6d 70 0a 20 20 29 3b 0a 0a 20 20 69 bTemp. );.. i
0750: 66 28 20 62 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 f( bTemp==0 ){.
0760: 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 sqlite3Nested
0770: 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 Parse(pParse, .
0780: 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 31 "SELECT 1
0790: 20 22 0a 20 20 20 20 20 20 20 20 22 46 52 4f 4d ". "FROM
07a0: 20 74 65 6d 70 2e 25 73 20 22 0a 20 20 20 20 20 temp.%s ".
07b0: 20 20 20 22 57 48 45 52 45 20 6e 61 6d 65 20 4e "WHERE name N
07c0: 4f 54 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f OT LIKE 'sqlite_
07d0: 25 25 27 22 0a 20 20 20 20 20 20 20 20 22 20 41 %%'". " A
07e0: 4e 44 20 73 71 6c 20 4e 4f 54 20 4c 49 4b 45 20 ND sql NOT LIKE
07f0: 27 63 72 65 61 74 65 20 76 69 72 74 75 61 6c 25 'create virtual%
0800: 25 27 22 0a 20 20 20 20 20 20 20 20 22 20 41 4e %'". " AN
0810: 44 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f D sqlite_rename_
0820: 74 65 73 74 28 25 51 2c 20 73 71 6c 2c 20 74 79 test(%Q, sql, ty
0830: 70 65 2c 20 6e 61 6d 65 2c 20 31 29 3d 4e 55 4c pe, name, 1)=NUL
0840: 4c 20 22 2c 0a 20 20 20 20 20 20 20 20 4d 41 53 L ",. MAS
0850: 54 45 52 5f 4e 41 4d 45 2c 20 7a 44 62 20 0a 20 TER_NAME, zDb .
0860: 20 20 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a );. }.}../*.
0870: 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
0880: 20 74 6f 20 72 65 6c 6f 61 64 20 74 68 65 20 73 to reload the s
0890: 63 68 65 6d 61 20 66 6f 72 20 64 61 74 61 62 61 chema for databa
08a0: 73 65 20 69 44 62 2e 20 41 6e 64 2c 20 69 66 20 se iDb. And, if
08b0: 69 44 62 21 3d 31 2c 20 66 6f 72 0a 2a 2a 20 74 iDb!=1, for.** t
08c0: 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 he temp database
08d0: 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 as well..*/.sta
08e0: 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 52 tic void renameR
08f0: 65 6c 6f 61 64 53 63 68 65 6d 61 28 50 61 72 73 eloadSchema(Pars
0900: 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
0910: 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d Db){. Vdbe *v =
0920: 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a pParse->pVdbe;.
0930: 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 if( v ){. s
0940: 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b qlite3ChangeCook
0950: 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b ie(pParse, iDb);
0960: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
0970: 41 64 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70 AddParseSchemaOp
0980: 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 30 29 (pParse, iDb, 0)
0990: 3b 0a 20 20 20 20 69 66 28 20 69 44 62 21 3d 31 ;. if( iDb!=1
09a0: 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 ) sqlite3VdbeAd
09b0: 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70 28 70 dParseSchemaOp(p
09c0: 50 61 72 73 65 2c 20 31 2c 20 30 29 3b 0a 20 20 Parse, 1, 0);.
09d0: 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
09e0: 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c ate code to impl
09f0: 65 6d 65 6e 74 20 74 68 65 20 22 41 4c 54 45 52 ement the "ALTER
0a00: 20 54 41 42 4c 45 20 78 78 78 20 52 45 4e 41 4d TABLE xxx RENAM
0a10: 45 20 54 4f 20 79 79 79 22 20 0a 2a 2a 20 63 6f E TO yyy" .** co
0a20: 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a 76 6f 69 64 20 mmand. .*/.void
0a30: 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 sqlite3AlterRena
0a40: 6d 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 meTable(. Parse
0a50: 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
0a60: 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 /* Parser c
0a70: 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53 72 63 ontext. */. Src
0a80: 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 List *pSrc,
0a90: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 /* The ta
0aa0: 62 6c 65 20 74 6f 20 72 65 6e 61 6d 65 2e 20 2a ble to rename. *
0ab0: 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
0ac0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
0ad0: 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e The new table n
0ae0: 61 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 ame. */.){. int
0af0: 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 iDb;
0b00: 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
0b10: 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 se that contains
0b20: 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
0b30: 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 char *zDb;
0b40: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
0b50: 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 44 e of database iD
0b60: 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 b */. Table *pT
0b70: 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ab;
0b80: 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 /* Table being
0b90: 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 63 68 61 renamed */. cha
0ba0: 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 r *zName = 0;
0bb0: 20 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74 /* NULL-t
0bc0: 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f erminated versio
0bd0: 6e 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20 0a 20 n of pName */ .
0be0: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
0bf0: 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 Parse->db; /* Da
0c00: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
0c10: 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 4e n */. int nTabN
0c20: 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
0c30: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 54 /* Number of UT
0c40: 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 20 69 F-8 characters i
0c50: 6e 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a 20 20 n zTabName */.
0c60: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 const char *zTab
0c70: 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f 72 69 Name; /* Ori
0c80: 67 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20 74 68 ginal name of th
0c90: 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 e table */. Vdb
0ca0: 65 20 2a 76 3b 0a 20 20 56 54 61 62 6c 65 20 2a e *v;. VTable *
0cb0: 70 56 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 pVTab = 0;
0cc0: 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f 20 69 66 /* Non-zero if
0cd0: 20 74 68 69 73 20 69 73 20 61 20 76 2d 74 61 62 this is a v-tab
0ce0: 20 77 69 74 68 20 61 6e 20 78 52 65 6e 61 6d 65 with an xRename
0cf0: 28 29 20 2a 2f 0a 20 20 75 33 32 20 73 61 76 65 () */. u32 save
0d00: 64 44 62 46 6c 61 67 73 3b 20 20 20 20 20 20 20 dDbFlags;
0d10: 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 /* Saved value
0d20: 20 6f 66 20 64 62 2d 3e 6d 44 62 46 6c 61 67 73 of db->mDbFlags
0d30: 20 2a 2f 0a 0a 20 20 73 61 76 65 64 44 62 46 6c */.. savedDbFl
0d40: 61 67 73 20 3d 20 64 62 2d 3e 6d 44 62 46 6c 61 ags = db->mDbFla
0d50: 67 73 3b 20 20 0a 20 20 69 66 28 20 4e 45 56 45 gs; . if( NEVE
0d60: 52 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c R(db->mallocFail
0d70: 65 64 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f ed) ) goto exit_
0d80: 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 rename_table;.
0d90: 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 assert( pSrc->nS
0da0: 72 63 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 rc==1 );. asser
0db0: 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
0dc0: 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 oldsAllMutexes(p
0dd0: 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 0a 20 Parse->db) );..
0de0: 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c pTab = sqlite3L
0df0: 6f 63 61 74 65 54 61 62 6c 65 49 74 65 6d 28 70 ocateTableItem(p
0e00: 50 61 72 73 65 2c 20 30 2c 20 26 70 53 72 63 2d Parse, 0, &pSrc-
0e10: 3e 61 5b 30 5d 29 3b 0a 20 20 69 66 28 20 21 70 >a[0]);. if( !p
0e20: 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f Tab ) goto exit_
0e30: 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 rename_table;.
0e40: 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
0e50: 65 6d 61 54 6f 49 6e 64 65 78 32 28 70 50 61 72 emaToIndex2(pPar
0e60: 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 se->db, pTab->pS
0e70: 63 68 65 6d 61 2c 20 30 29 3b 0a 20 20 7a 44 62 chema, 0);. zDb
0e80: 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
0e90: 7a 44 62 53 4e 61 6d 65 3b 0a 20 20 64 62 2d 3e zDbSName;. db->
0ea0: 6d 44 62 46 6c 61 67 73 20 7c 3d 20 44 42 46 4c mDbFlags |= DBFL
0eb0: 41 47 5f 50 72 65 66 65 72 42 75 69 6c 74 69 6e AG_PreferBuiltin
0ec0: 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e 55 ;.. /* Get a NU
0ed0: 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64 20 76 65 LL terminated ve
0ee0: 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 rsion of the new
0ef0: 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a table name. */.
0f00: 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 zName = sqlite
0f10: 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
0f20: 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 b, pName);. if(
0f30: 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 65 !zName ) goto e
0f40: 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
0f50: 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 ;.. /* Check th
0f60: 61 74 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e at a table or in
0f70: 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d 65 dex named 'zName
0f80: 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 ' does not alrea
0f90: 64 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 6e dy exist. ** in
0fa0: 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 49 database iDb. I
0fb0: 66 20 73 6f 2c 20 74 68 69 73 20 69 73 20 61 6e f so, this is an
0fc0: 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 error.. */. i
0fd0: 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 f( sqlite3FindTa
0fe0: 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a ble(db, zName, z
0ff0: 44 62 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 69 Db) || sqlite3Fi
1000: 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d ndIndex(db, zNam
1010: 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 e, zDb) ){. s
1020: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1030: 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 Parse, .
1040: 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 "there is alread
1050: 79 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 y another table
1060: 6f 72 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 or index with th
1070: 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e is name: %s", zN
1080: 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ame);. goto e
1090: 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
10a0: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
10b0: 20 73 75 72 65 20 69 74 20 69 73 20 6e 6f 74 20 sure it is not
10c0: 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 62 a system table b
10d0: 65 69 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f 72 eing altered, or
10e0: 20 61 20 72 65 73 65 72 76 65 64 20 6e 61 6d 65 a reserved name
10f0: 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 74 . ** that the t
1100: 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 72 65 able is being re
1110: 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20 named to.. */.
1120: 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
1130: 69 73 53 79 73 74 65 6d 54 61 62 6c 65 28 70 50 isSystemTable(pP
1140: 61 72 73 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d arse, pTab->zNam
1150: 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 e) ){. goto e
1160: 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
1170: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 ;. }. if( SQLI
1180: 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 TE_OK!=sqlite3Ch
1190: 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 eckObjectName(pP
11a0: 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 20 arse, zName) ){
11b0: 67 6f 74 6f 0a 20 20 20 20 65 78 69 74 5f 72 65 goto. exit_re
11c0: 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a name_table;. }.
11d0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
11e0: 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 OMIT_VIEW. if(
11f0: 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b pTab->pSelect ){
1200: 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1210: 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 rMsg(pParse, "vi
1220: 65 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 ew %s may not be
1230: 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d altered", pTab-
1240: 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 >zName);. got
1250: 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
1260: 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ble;. }.#endif.
1270: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1280: 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
1290: 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 ON. /* Invoke t
12a0: 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e he authorization
12b0: 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 callback. */.
12c0: 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 if( sqlite3AuthC
12d0: 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c heck(pParse, SQL
12e0: 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c ITE_ALTER_TABLE,
12f0: 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d zDb, pTab->zNam
1300: 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 e, 0) ){. got
1310: 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
1320: 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ble;. }.#endif.
1330: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1340: 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1350: 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 E. if( sqlite3V
1360: 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 iewGetColumnName
1370: 73 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 s(pParse, iDb, p
1380: 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f Tab) ){. goto
1390: 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 exit_rename_tab
13a0: 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 le;. }. if( Is
13b0: 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b Virtual(pTab) ){
13c0: 0a 20 20 20 20 70 56 54 61 62 20 3d 20 73 71 6c . pVTab = sql
13d0: 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 ite3GetVTable(db
13e0: 2c 20 2d 31 2c 20 70 54 61 62 29 3b 0a 20 20 20 , -1, pTab);.
13f0: 20 69 66 28 20 70 56 54 61 62 2d 3e 70 56 74 61 if( pVTab->pVta
1400: 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e b->pModule->xRen
1410: 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ame==0 ){.
1420: 70 56 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 7d pVTab = 0;. }
1430: 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
1440: 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 * Begin a transa
1450: 63 74 69 6f 6e 20 66 6f 72 20 64 61 74 61 62 61 ction for databa
1460: 73 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54 68 se iDb. . ** Th
1470: 65 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63 en modify the sc
1480: 68 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69 6e hema cookie (sin
1490: 63 65 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 ce the ALTER TAB
14a0: 4c 45 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0a LE modifies the.
14b0: 20 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f 70 ** schema). Op
14c0: 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 en a statement t
14d0: 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 ransaction if th
14e0: 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 e table is a vir
14f0: 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 2e tual. ** table.
1500: 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 . */. v = sqli
1510: 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
1520: 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 e);. if( v==0 )
1530: 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f {. goto exit_
1540: 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 rename_table;.
1550: 7d 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f }.. /* figure o
1560: 75 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d ut how many UTF-
1570: 38 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 8 characters are
1580: 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a in zName */. z
1590: 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e TabName = pTab->
15a0: 7a 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d zName;. nTabNam
15b0: 65 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 e = sqlite3Utf8C
15c0: 68 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c harLen(zTabName,
15d0: 20 2d 31 29 3b 0a 0a 20 20 2f 2a 20 52 65 77 72 -1);.. /* Rewr
15e0: 69 74 65 20 61 6c 6c 20 43 52 45 41 54 45 20 54 ite all CREATE T
15f0: 41 42 4c 45 2c 20 49 4e 44 45 58 2c 20 54 52 49 ABLE, INDEX, TRI
1600: 47 47 45 52 20 6f 72 20 56 49 45 57 20 73 74 61 GGER or VIEW sta
1610: 74 65 6d 65 6e 74 73 20 69 6e 0a 20 20 2a 2a 20 tements in. **
1620: 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20 75 73 the schema to us
1630: 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 e the new table
1640: 6e 61 6d 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 name. */. sqli
1650: 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 te3NestedParse(p
1660: 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 55 Parse, . "U
1670: 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 25 73 20 PDATE \"%w\".%s
1680: 53 45 54 20 22 0a 20 20 20 20 20 20 22 73 71 6c SET ". "sql
1690: 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 = sqlite_rename
16a0: 5f 74 61 62 6c 65 28 25 51 2c 20 74 79 70 65 2c _table(%Q, type,
16b0: 20 6e 61 6d 65 2c 20 73 71 6c 2c 20 25 51 2c 20 name, sql, %Q,
16c0: 25 51 2c 20 25 64 29 20 22 0a 20 20 20 20 20 20 %Q, %d) ".
16d0: 22 57 48 45 52 45 20 28 74 79 70 65 21 3d 27 69 "WHERE (type!='i
16e0: 6e 64 65 78 27 20 4f 52 20 74 62 6c 5f 6e 61 6d ndex' OR tbl_nam
16f0: 65 3d 25 51 20 43 4f 4c 4c 41 54 45 20 6e 6f 63 e=%Q COLLATE noc
1700: 61 73 65 29 22 0a 20 20 20 20 20 20 22 41 4e 44 ase)". "AND
1710: 20 20 20 6e 61 6d 65 20 4e 4f 54 20 4c 49 4b 45 name NOT LIKE
1720: 20 27 73 71 6c 69 74 65 5f 25 25 27 22 0a 20 20 'sqlite_%%'".
1730: 20 20 20 20 2c 20 7a 44 62 2c 20 4d 41 53 54 45 , zDb, MASTE
1740: 52 5f 4e 41 4d 45 2c 20 7a 44 62 2c 20 7a 54 61 R_NAME, zDb, zTa
1750: 62 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 28 69 bName, zName, (i
1760: 44 62 3d 3d 31 29 2c 20 7a 54 61 62 4e 61 6d 65 Db==1), zTabName
1770: 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 . );.. /* Upda
1780: 74 65 20 74 68 65 20 74 62 6c 5f 6e 61 6d 65 20 te the tbl_name
1790: 61 6e 64 20 6e 61 6d 65 20 63 6f 6c 75 6d 6e 73 and name columns
17a0: 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 6d of the sqlite_m
17b0: 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a aster table. **
17c0: 20 61 73 20 72 65 71 75 69 72 65 64 2e 20 20 2a as required. *
17d0: 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 /. sqlite3Neste
17e0: 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 dParse(pParse,.
17f0: 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e "UPDATE %Q.
1800: 25 73 20 53 45 54 20 22 0a 20 20 20 20 20 20 20 %s SET ".
1810: 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 "tbl_name = %
1820: 51 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 Q, ". "
1830: 6e 61 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 20 name = CASE ".
1840: 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 "WHEN
1850: 74 79 70 65 3d 27 74 61 62 6c 65 27 20 54 48 45 type='table' THE
1860: 4e 20 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 N %Q ".
1870: 20 20 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49 "WHEN name LI
1880: 4b 45 20 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 KE 'sqlite_autoi
1890: 6e 64 65 78 25 25 27 20 41 4e 44 20 74 79 70 65 ndex%%' AND type
18a0: 3d 27 69 6e 64 65 78 27 20 54 48 45 4e 20 22 0a ='index' THEN ".
18b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 22 27 73 "'s
18c0: 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f qlite_autoindex_
18d0: 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 ' || %Q || subst
18e0: 72 28 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22 0a r(name,%d+18) ".
18f0: 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c 53 "ELS
1900: 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20 20 E name END ".
1910: 20 20 20 22 57 48 45 52 45 20 74 62 6c 5f 6e 61 "WHERE tbl_na
1920: 6d 65 3d 25 51 20 43 4f 4c 4c 41 54 45 20 6e 6f me=%Q COLLATE no
1930: 63 61 73 65 20 41 4e 44 20 22 0a 20 20 20 20 20 case AND ".
1940: 20 20 20 20 20 22 28 74 79 70 65 3d 27 74 61 62 "(type='tab
1950: 6c 65 27 20 4f 52 20 74 79 70 65 3d 27 69 6e 64 le' OR type='ind
1960: 65 78 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69 ex' OR type='tri
1970: 67 67 65 72 27 29 3b 22 2c 20 0a 20 20 20 20 20 gger');", .
1980: 20 7a 44 62 2c 20 4d 41 53 54 45 52 5f 4e 41 4d zDb, MASTER_NAM
1990: 45 2c 20 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c E, . zName,
19a0: 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 0a zName, zName, .
19b0: 20 20 20 20 20 20 6e 54 61 62 4e 61 6d 65 2c 20 nTabName,
19c0: 7a 54 61 62 4e 61 6d 65 0a 20 20 29 3b 0a 0a 23 zTabName. );..#
19d0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
19e0: 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 IT_AUTOINCREMENT
19f0: 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 71 6c . /* If the sql
1a00: 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 ite_sequence tab
1a10: 6c 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 69 le exists in thi
1a20: 73 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e s database, then
1a30: 20 75 70 64 61 74 65 20 0a 20 20 2a 2a 20 69 74 update . ** it
1a40: 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 74 61 with the new ta
1a50: 62 6c 65 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 ble name.. */.
1a60: 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 if( sqlite3Find
1a70: 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 Table(db, "sqlit
1a80: 65 5f 73 65 71 75 65 6e 63 65 22 2c 20 7a 44 62 e_sequence", zDb
1a90: 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
1aa0: 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 NestedParse(pPar
1ab0: 73 65 2c 0a 20 20 20 20 20 20 20 20 22 55 50 44 se,. "UPD
1ac0: 41 54 45 20 5c 22 25 77 5c 22 2e 73 71 6c 69 74 ATE \"%w\".sqlit
1ad0: 65 5f 73 65 71 75 65 6e 63 65 20 73 65 74 20 6e e_sequence set n
1ae0: 61 6d 65 20 3d 20 25 51 20 57 48 45 52 45 20 6e ame = %Q WHERE n
1af0: 61 6d 65 20 3d 20 25 51 22 2c 0a 20 20 20 20 20 ame = %Q",.
1b00: 20 20 20 7a 44 62 2c 20 7a 4e 61 6d 65 2c 20 70 zDb, zName, p
1b10: 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d Tab->zName);. }
1b20: 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 .#endif.. /* If
1b30: 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
1b40: 20 72 65 6e 61 6d 65 64 20 69 73 20 6e 6f 74 20 renamed is not
1b50: 69 74 73 65 6c 66 20 70 61 72 74 20 6f 66 20 74 itself part of t
1b60: 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 he temp database
1b70: 2c 0a 20 20 2a 2a 20 65 64 69 74 20 76 69 65 77 ,. ** edit view
1b80: 20 61 6e 64 20 74 72 69 67 67 65 72 20 64 65 66 and trigger def
1b90: 69 6e 69 74 69 6f 6e 73 20 77 69 74 68 69 6e 20 initions within
1ba0: 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 the temp databas
1bb0: 65 20 0a 20 20 2a 2a 20 61 73 20 72 65 71 75 69 e . ** as requi
1bc0: 72 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 69 red. */. if( i
1bd0: 44 62 21 3d 31 20 29 7b 0a 20 20 20 20 73 71 6c Db!=1 ){. sql
1be0: 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
1bf0: 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
1c00: 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65 5f "UPDATE sqlite_
1c10: 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54 20 temp_master SET
1c20: 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 73 ". "s
1c30: 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 ql = sqlite_rena
1c40: 6d 65 5f 74 61 62 6c 65 28 25 51 2c 20 74 79 70 me_table(%Q, typ
1c50: 65 2c 20 6e 61 6d 65 2c 20 73 71 6c 2c 20 25 51 e, name, sql, %Q
1c60: 2c 20 25 51 2c 20 31 29 2c 20 22 0a 20 20 20 20 , %Q, 1), ".
1c70: 20 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d "tbl_nam
1c80: 65 20 3d 20 22 0a 20 20 20 20 20 20 20 20 20 20 e = ".
1c90: 20 20 20 20 22 43 41 53 45 20 57 48 45 4e 20 74 "CASE WHEN t
1ca0: 62 6c 5f 6e 61 6d 65 3d 25 51 20 43 4f 4c 4c 41 bl_name=%Q COLLA
1cb0: 54 45 20 6e 6f 63 61 73 65 20 41 4e 44 20 22 0a TE nocase AND ".
1cc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 "
1cd0: 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f sqlite_
1ce0: 72 65 6e 61 6d 65 5f 74 65 73 74 28 25 51 2c 20 rename_test(%Q,
1cf0: 73 71 6c 2c 20 74 79 70 65 2c 20 6e 61 6d 65 2c sql, type, name,
1d00: 20 31 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 1) ".
1d10: 20 20 20 20 22 54 48 45 4e 20 25 51 20 45 4c 53 "THEN %Q ELS
1d20: 45 20 74 62 6c 5f 6e 61 6d 65 20 45 4e 44 20 22 E tbl_name END "
1d30: 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48 . "WH
1d40: 45 52 45 20 74 79 70 65 20 49 4e 20 28 27 76 69 ERE type IN ('vi
1d50: 65 77 27 2c 20 27 74 72 69 67 67 65 72 27 29 22 ew', 'trigger')"
1d60: 0a 20 20 20 20 20 20 20 20 2c 20 7a 44 62 2c 20 . , zDb,
1d70: 7a 54 61 62 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c zTabName, zName,
1d80: 20 7a 54 61 62 4e 61 6d 65 2c 20 7a 44 62 2c 20 zTabName, zDb,
1d90: 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f zName);. }.. /
1da0: 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 76 * If this is a v
1db0: 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 69 6e irtual table, in
1dc0: 76 6f 6b 65 20 74 68 65 20 78 52 65 6e 61 6d 65 voke the xRename
1dd0: 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 0a 20 () function if.
1de0: 20 2a 2a 20 6f 6e 65 20 69 73 20 64 65 66 69 6e ** one is defin
1df0: 65 64 2e 20 54 68 65 20 78 52 65 6e 61 6d 65 28 ed. The xRename(
1e00: 29 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 ) callback will
1e10: 6d 6f 64 69 66 79 20 74 68 65 20 6e 61 6d 65 73 modify the names
1e20: 0a 20 20 2a 2a 20 6f 66 20 61 6e 79 20 72 65 73 . ** of any res
1e30: 6f 75 72 63 65 73 20 75 73 65 64 20 62 79 20 74 ources used by t
1e40: 68 65 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 he v-table imple
1e50: 6d 65 6e 74 61 74 69 6f 6e 20 28 69 6e 63 6c 75 mentation (inclu
1e60: 64 69 6e 67 20 6f 74 68 65 72 0a 20 20 2a 2a 20 ding other. **
1e70: 53 51 4c 69 74 65 20 74 61 62 6c 65 73 29 20 74 SQLite tables) t
1e80: 68 61 74 20 61 72 65 20 69 64 65 6e 74 69 66 69 hat are identifi
1e90: 65 64 20 62 79 20 74 68 65 20 6e 61 6d 65 20 6f ed by the name o
1ea0: 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 f the virtual ta
1eb0: 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 ble.. */.#ifnde
1ec0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1ed0: 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 RTUALTABLE. if(
1ee0: 20 70 56 54 61 62 20 29 7b 0a 20 20 20 20 69 6e pVTab ){. in
1ef0: 74 20 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e t i = ++pParse->
1f00: 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 nMem;. sqlite
1f10: 33 56 64 62 65 4c 6f 61 64 53 74 72 69 6e 67 28 3VdbeLoadString(
1f20: 76 2c 20 69 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 v, i, zName);.
1f30: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f40: 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d Op4(v, OP_VRenam
1f50: 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73 e, i, 0, 0,(cons
1f60: 74 20 63 68 61 72 2a 29 70 56 54 61 62 2c 20 50 t char*)pVTab, P
1f70: 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 73 71 6c 4_VTAB);. sql
1f80: 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 ite3MayAbort(pPa
1f90: 72 73 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 rse);. }.#endif
1fa0: 0a 0a 20 20 72 65 6e 61 6d 65 52 65 6c 6f 61 64 .. renameReload
1fb0: 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 Schema(pParse, i
1fc0: 44 62 29 3b 0a 20 20 72 65 6e 61 6d 65 54 65 73 Db);. renameTes
1fd0: 74 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 tSchema(pParse,
1fe0: 7a 44 62 2c 20 69 44 62 3d 3d 31 29 3b 0a 0a 65 zDb, iDb==1);..e
1ff0: 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
2000: 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 :. sqlite3SrcLi
2010: 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 stDelete(db, pSr
2020: 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 c);. sqlite3DbF
2030: 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a ree(db, zName);.
2040: 20 20 64 62 2d 3e 6d 44 62 46 6c 61 67 73 20 3d db->mDbFlags =
2050: 20 73 61 76 65 64 44 62 46 6c 61 67 73 3b 0a 7d savedDbFlags;.}
2060: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
2070: 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
2080: 61 66 74 65 72 20 61 6e 20 22 41 4c 54 45 52 20 after an "ALTER
2090: 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 TABLE ... ADD" s
20a0: 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 tatement.** has
20b0: 62 65 65 6e 20 70 61 72 73 65 64 2e 20 41 72 67 been parsed. Arg
20c0: 75 6d 65 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f ument pColDef co
20d0: 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 ntains the text
20e0: 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 6f of the new.** co
20f0: 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e lumn definition.
2100: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 .**.** The Table
2110: 20 73 74 72 75 63 74 75 72 65 20 70 50 61 72 73 structure pPars
2120: 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 77 61 73 e->pNewTable was
2130: 20 65 78 74 65 6e 64 65 64 20 74 6f 20 69 6e 63 extended to inc
2140: 6c 75 64 65 0a 2a 2a 20 74 68 65 20 6e 65 77 20 lude.** the new
2150: 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67 20 70 61 column during pa
2160: 72 73 69 6e 67 2e 0a 2a 2f 0a 76 6f 69 64 20 73 rsing..*/.void s
2170: 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 qlite3AlterFinis
2180: 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 hAddColumn(Parse
2190: 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 *pParse, Token
21a0: 2a 70 43 6f 6c 44 65 66 29 7b 0a 20 20 54 61 62 *pColDef){. Tab
21b0: 6c 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 le *pNew;
21c0: 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
21d0: 66 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 f pParse->pNewTa
21e0: 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ble */. Table *
21f0: 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 pTab;
2200: 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e /* Table bein
2210: 67 20 61 6c 74 65 72 65 64 20 2a 2f 0a 20 20 69 g altered */. i
2220: 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
2230: 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
2240: 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 base number */.
2250: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
2260: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 ; /* Da
2270: 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 tabase name */.
2280: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 const char *zTa
2290: 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 b; /* Ta
22a0: 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 ble name */. ch
22b0: 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 ar *zCol;
22c0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d /* Null-
22d0: 74 65 72 6d 69 6e 61 74 65 64 20 63 6f 6c 75 6d terminated colum
22e0: 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a n definition */.
22f0: 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 Column *pCol;
2300: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
2310: 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f he new column */
2320: 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 . Expr *pDflt;
2330: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2340: 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f Default value fo
2350: 72 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e r the new column
2360: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
2370: 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
2380: 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
2390: 63 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a 20 connection; */.
23a0: 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 Vdbe *v;
23b0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
23c0: 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 e prepared state
23d0: 6d 65 6e 74 20 75 6e 64 65 72 20 63 6f 6e 73 74 ment under const
23e0: 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 ruction */. int
23f0: 20 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 r1;
2400: 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 /* Tempor
2410: 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a 2f ary registers */
2420: 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d .. db = pParse-
2430: 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 >db;. if( pPars
2440: 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d e->nErr || db->m
2450: 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 allocFailed ) re
2460: 74 75 72 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70 turn;. pNew = p
2470: 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
2480: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 ;. assert( pNew
2490: 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 );.. assert( s
24a0: 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
24b0: 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 AllMutexes(db) )
24c0: 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 ;. iDb = sqlite
24d0: 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 32 28 3SchemaToIndex2(
24e0: 64 62 2c 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d db, pNew->pSchem
24f0: 61 2c 20 30 29 3b 0a 20 20 7a 44 62 20 3d 20 64 a, 0);. zDb = d
2500: 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 b->aDb[iDb].zDbS
2510: 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 26 Name;. zTab = &
2520: 70 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b pNew->zName[16];
2530: 20 20 2f 2a 20 53 6b 69 70 20 74 68 65 20 22 73 /* Skip the "s
2540: 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 qlite_altertab_"
2550: 20 70 72 65 66 69 78 20 6f 6e 20 74 68 65 20 6e prefix on the n
2560: 61 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c 20 3d 20 ame */. pCol =
2570: 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 &pNew->aCol[pNew
2580: 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66 ->nCol-1];. pDf
2590: 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 lt = pCol->pDflt
25a0: 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 ;. pTab = sqlit
25b0: 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 e3FindTable(db,
25c0: 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20 20 61 73 zTab, zDb);. as
25d0: 73 65 72 74 28 20 70 54 61 62 20 29 3b 0a 0a 23 sert( pTab );..#
25e0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
25f0: 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
2600: 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 . /* Invoke the
2610: 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 authorization c
2620: 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 allback. */. if
2630: 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 ( sqlite3AuthChe
2640: 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ck(pParse, SQLIT
2650: 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a E_ALTER_TABLE, z
2660: 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c Db, pTab->zName,
2670: 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 0) ){. retur
2680: 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 n;. }.#endif..
2690: 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 /* If the defau
26a0: 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
26b0: 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77 61 73 20 new column was
26c0: 73 70 65 63 69 66 69 65 64 20 77 69 74 68 20 61 specified with a
26d0: 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61 6c 20 4e . ** literal N
26e0: 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 70 44 ULL, then set pD
26f0: 66 6c 74 20 74 6f 20 30 2e 20 54 68 69 73 20 73 flt to 0. This s
2700: 69 6d 70 6c 69 66 69 65 73 20 63 68 65 63 6b 69 implifies checki
2710: 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61 6e 20 53 ng. ** for an S
2720: 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 QL NULL default
2730: 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 below.. */. as
2740: 73 65 72 74 28 20 70 44 66 6c 74 3d 3d 30 20 7c sert( pDflt==0 |
2750: 7c 20 70 44 66 6c 74 2d 3e 6f 70 3d 3d 54 4b 5f | pDflt->op==TK_
2760: 53 50 41 4e 20 29 3b 0a 20 20 69 66 28 20 70 44 SPAN );. if( pD
2770: 66 6c 74 20 26 26 20 70 44 66 6c 74 2d 3e 70 4c flt && pDflt->pL
2780: 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c eft->op==TK_NULL
2790: 20 29 7b 0a 20 20 20 20 70 44 66 6c 74 20 3d 20 ){. pDflt =
27a0: 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 0;. }.. /* Che
27b0: 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 ck that the new
27c0: 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70 column is not sp
27d0: 65 63 69 66 69 65 64 20 61 73 20 50 52 49 4d 41 ecified as PRIMA
27e0: 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 RY KEY or UNIQUE
27f0: 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 .. ** If there
2800: 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f is a NOT NULL co
2810: 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e 20 74 nstraint, then t
2820: 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 he default value
2830: 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f for the. ** co
2840: 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 lumn must not be
2850: 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 NULL.. */. if
2860: 28 20 70 43 6f 6c 2d 3e 63 6f 6c 46 6c 61 67 73 ( pCol->colFlags
2870: 20 26 20 43 4f 4c 46 4c 41 47 5f 50 52 49 4d 4b & COLFLAG_PRIMK
2880: 45 59 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 EY ){. sqlite
2890: 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
28a0: 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 , "Cannot add a
28b0: 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 PRIMARY KEY colu
28c0: 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e mn");. return
28d0: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 77 ;. }. if( pNew
28e0: 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 ->pIndex ){.
28f0: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
2900: 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 pParse, "Cannot
2910: 61 64 64 20 61 20 55 4e 49 51 55 45 20 63 6f 6c add a UNIQUE col
2920: 75 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 umn");. retur
2930: 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 64 62 n;. }. if( (db
2940: 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 ->flags&SQLITE_F
2950: 6f 72 65 69 67 6e 4b 65 79 73 29 20 26 26 20 70 oreignKeys) && p
2960: 4e 65 77 2d 3e 70 46 4b 65 79 20 26 26 20 70 44 New->pFKey && pD
2970: 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 flt ){. sqlit
2980: 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
2990: 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61 6e e, . "Can
29a0: 6e 6f 74 20 61 64 64 20 61 20 52 45 46 45 52 45 not add a REFERE
29b0: 4e 43 45 53 20 63 6f 6c 75 6d 6e 20 77 69 74 68 NCES column with
29c0: 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c non-NULL defaul
29d0: 74 20 76 61 6c 75 65 22 29 3b 0a 20 20 20 20 72 t value");. r
29e0: 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 eturn;. }. if(
29f0: 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 pCol->notNull &
2a00: 26 20 21 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 & !pDflt ){.
2a10: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
2a20: 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
2a30: 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 4e "Cannot add a N
2a40: 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77 OT NULL column w
2a50: 69 74 68 20 64 65 66 61 75 6c 74 20 76 61 6c 75 ith default valu
2a60: 65 20 4e 55 4c 4c 22 29 3b 0a 20 20 20 20 72 65 e NULL");. re
2a70: 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 turn;. }.. /*
2a80: 45 6e 73 75 72 65 20 74 68 65 20 64 65 66 61 75 Ensure the defau
2a90: 6c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 lt expression is
2aa0: 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 something that
2ab0: 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d sqlite3ValueFrom
2ac0: 45 78 70 72 28 29 0a 20 20 2a 2a 20 63 61 6e 20 Expr(). ** can
2ad0: 68 61 6e 64 6c 65 20 28 69 2e 65 2e 20 6e 6f 74 handle (i.e. not
2ae0: 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 20 65 74 CURRENT_TIME et
2af0: 63 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 c.). */. if( p
2b00: 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 Dflt ){. sqli
2b10: 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 te3_value *pVal
2b20: 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b = 0;. int rc;
2b30: 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
2b40: 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 3ValueFromExpr(d
2b50: 62 2c 20 70 44 66 6c 74 2c 20 53 51 4c 49 54 45 b, pDflt, SQLITE
2b60: 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 41 46 _UTF8, SQLITE_AF
2b70: 46 5f 42 4c 4f 42 2c 20 26 70 56 61 6c 29 3b 0a F_BLOB, &pVal);.
2b80: 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
2b90: 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d SQLITE_OK || rc=
2ba0: 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
2bb0: 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
2bc0: 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2bd0: 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c assert( db->mall
2be0: 6f 63 46 61 69 6c 65 64 20 3d 3d 20 31 20 29 3b ocFailed == 1 );
2bf0: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
2c00: 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 56 }. if( !pV
2c10: 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 al ){. sqli
2c20: 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
2c30: 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 se, "Cannot add
2c40: 61 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f a column with no
2c50: 6e 2d 63 6f 6e 73 74 61 6e 74 20 64 65 66 61 75 n-constant defau
2c60: 6c 74 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 lt");. retu
2c70: 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 rn;. }. sq
2c80: 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 lite3ValueFree(p
2c90: 56 61 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Val);. }.. /*
2ca0: 4d 6f 64 69 66 79 20 74 68 65 20 43 52 45 41 54 Modify the CREAT
2cb0: 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
2cc0: 74 2e 20 2a 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 t. */. zCol = s
2cd0: 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
2ce0: 64 62 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 44 db, (char*)pColD
2cf0: 65 66 2d 3e 7a 2c 20 70 43 6f 6c 44 65 66 2d 3e ef->z, pColDef->
2d00: 6e 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6c 20 29 n);. if( zCol )
2d10: 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 6e 64 {. char *zEnd
2d20: 20 3d 20 26 7a 43 6f 6c 5b 70 43 6f 6c 44 65 66 = &zCol[pColDef
2d30: 2d 3e 6e 2d 31 5d 3b 0a 20 20 20 20 75 33 32 20 ->n-1];. u32
2d40: 73 61 76 65 64 44 62 46 6c 61 67 73 20 3d 20 64 savedDbFlags = d
2d50: 62 2d 3e 6d 44 62 46 6c 61 67 73 3b 0a 20 20 20 b->mDbFlags;.
2d60: 20 77 68 69 6c 65 28 20 7a 45 6e 64 3e 7a 43 6f while( zEnd>zCo
2d70: 6c 20 26 26 20 28 2a 7a 45 6e 64 3d 3d 27 3b 27 l && (*zEnd==';'
2d80: 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 73 70 61 || sqlite3Isspa
2d90: 63 65 28 2a 7a 45 6e 64 29 29 20 29 7b 0a 20 20 ce(*zEnd)) ){.
2da0: 20 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c *zEnd-- = '\
2db0: 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 0';. }. db
2dc0: 2d 3e 6d 44 62 46 6c 61 67 73 20 7c 3d 20 44 42 ->mDbFlags |= DB
2dd0: 46 4c 41 47 5f 50 72 65 66 65 72 42 75 69 6c 74 FLAG_PreferBuilt
2de0: 69 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e in;. sqlite3N
2df0: 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 estedParse(pPars
2e00: 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 e, . "UPD
2e10: 41 54 45 20 5c 22 25 77 5c 22 2e 25 73 20 53 45 ATE \"%w\".%s SE
2e20: 54 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 73 T ". "s
2e30: 71 6c 20 3d 20 73 75 62 73 74 72 28 73 71 6c 2c ql = substr(sql,
2e40: 31 2c 25 64 29 20 7c 7c 20 27 2c 20 27 20 7c 7c 1,%d) || ', ' ||
2e50: 20 25 51 20 7c 7c 20 73 75 62 73 74 72 28 73 71 %Q || substr(sq
2e60: 6c 2c 25 64 29 20 22 0a 20 20 20 20 20 20 20 20 l,%d) ".
2e70: 22 57 48 45 52 45 20 74 79 70 65 20 3d 20 27 74 "WHERE type = 't
2e80: 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65 20 3d able' AND name =
2e90: 20 25 51 22 2c 20 0a 20 20 20 20 20 20 7a 44 62 %Q", . zDb
2ea0: 2c 20 4d 41 53 54 45 52 5f 4e 41 4d 45 2c 20 70 , MASTER_NAME, p
2eb0: 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 New->addColOffse
2ec0: 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 t, zCol, pNew->a
2ed0: 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20 ddColOffset+1,.
2ee0: 20 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b zTab. );
2ef0: 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
2f00: 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 ee(db, zCol);.
2f10: 20 20 64 62 2d 3e 6d 44 62 46 6c 61 67 73 20 3d db->mDbFlags =
2f20: 20 73 61 76 65 64 44 62 46 6c 61 67 73 3b 0a 20 savedDbFlags;.
2f30: 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 }.. /* Make su
2f40: 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 re the schema ve
2f50: 72 73 69 6f 6e 20 69 73 20 61 74 20 6c 65 61 73 rsion is at leas
2f60: 74 20 33 2e 20 20 42 75 74 20 64 6f 20 6e 6f 74 t 3. But do not
2f70: 20 75 70 67 72 61 64 65 0a 20 20 2a 2a 20 66 72 upgrade. ** fr
2f80: 6f 6d 20 6c 65 73 73 20 74 68 61 6e 20 33 20 74 om less than 3 t
2f90: 6f 20 34 2c 20 61 73 20 74 68 61 74 20 77 69 6c o 4, as that wil
2fa0: 6c 20 63 6f 72 72 75 70 74 20 61 6e 79 20 70 72 l corrupt any pr
2fb0: 65 65 78 69 73 74 69 6e 67 20 44 45 53 43 0a 20 eexisting DESC.
2fc0: 20 2a 2a 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a ** index.. */.
2fd0: 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 v = sqlite3Get
2fe0: 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
2ff0: 69 66 28 20 76 20 29 7b 0a 20 20 20 20 72 31 20 if( v ){. r1
3000: 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 = sqlite3GetTemp
3010: 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 Reg(pParse);.
3020: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
3030: 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f p3(v, OP_ReadCoo
3040: 6b 69 65 2c 20 69 44 62 2c 20 72 31 2c 20 42 54 kie, iDb, r1, BT
3050: 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 29 REE_FILE_FORMAT)
3060: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
3070: 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 eUsesBtree(v, iD
3080: 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 b);. sqlite3V
3090: 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
30a0: 41 64 64 49 6d 6d 2c 20 72 31 2c 20 2d 32 29 3b AddImm, r1, -2);
30b0: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
30c0: 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 AddOp2(v, OP_IfP
30d0: 6f 73 2c 20 72 31 2c 20 73 71 6c 69 74 65 33 56 os, r1, sqlite3V
30e0: 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 dbeCurrentAddr(v
30f0: 29 2b 32 29 3b 0a 20 20 20 20 56 64 62 65 43 6f )+2);. VdbeCo
3100: 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 73 verage(v);. s
3110: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
3120: 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 (v, OP_SetCookie
3130: 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 46 49 4c , iDb, BTREE_FIL
3140: 45 5f 46 4f 52 4d 41 54 2c 20 33 29 3b 0a 20 20 E_FORMAT, 3);.
3150: 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
3160: 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
3170: 72 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 r1);. }.. /* R
3180: 65 6c 6f 61 64 20 74 68 65 20 74 61 62 6c 65 20 eload the table
3190: 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 definition */.
31a0: 72 65 6e 61 6d 65 52 65 6c 6f 61 64 53 63 68 65 renameReloadSche
31b0: 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b ma(pParse, iDb);
31c0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
31d0: 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
31e0: 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 d by the parser
31f0: 61 66 74 65 72 20 74 68 65 20 74 61 62 6c 65 2d after the table-
3200: 6e 61 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 name in.** an "A
3210: 4c 54 45 52 20 54 41 42 4c 45 20 3c 74 61 62 6c LTER TABLE <tabl
3220: 65 2d 6e 61 6d 65 3e 20 41 44 44 22 20 73 74 61 e-name> ADD" sta
3230: 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 tement is parsed
3240: 2e 20 41 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 . Argument .** p
3250: 53 72 63 20 69 73 20 74 68 65 20 66 75 6c 6c 2d Src is the full-
3260: 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
3270: 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e e being altered.
3280: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
3290: 69 6e 65 20 6d 61 6b 65 73 20 61 20 28 70 61 72 ine makes a (par
32a0: 74 69 61 6c 29 20 63 6f 70 79 20 6f 66 20 74 68 tial) copy of th
32b0: 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 e Table structur
32c0: 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 e.** for the tab
32d0: 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 le being altered
32e0: 20 61 6e 64 20 73 65 74 73 20 50 61 72 73 65 2e and sets Parse.
32f0: 70 4e 65 77 54 61 62 6c 65 20 74 6f 20 70 6f 69 pNewTable to poi
3300: 6e 74 0a 2a 2a 20 74 6f 20 69 74 2e 20 52 6f 75 nt.** to it. Rou
3310: 74 69 6e 65 73 20 63 61 6c 6c 65 64 20 62 79 20 tines called by
3320: 74 68 65 20 70 61 72 73 65 72 20 61 73 20 74 68 the parser as th
3330: 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 e column definit
3340: 69 6f 6e 0a 2a 2a 20 69 73 20 70 61 72 73 65 64 ion.** is parsed
3350: 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 33 41 64 (i.e. sqlite3Ad
3360: 64 43 6f 6c 75 6d 6e 28 29 29 20 61 64 64 20 74 dColumn()) add t
3370: 68 65 20 6e 65 77 20 43 6f 6c 75 6d 6e 20 64 61 he new Column da
3380: 74 61 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63 6f ta to .** the co
3390: 70 79 2e 20 54 68 65 20 63 6f 70 79 20 6f 66 20 py. The copy of
33a0: 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 the Table struct
33b0: 75 72 65 20 69 73 20 64 65 6c 65 74 65 64 20 62 ure is deleted b
33c0: 79 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a y tokenize.c .**
33d0: 20 61 66 74 65 72 20 70 61 72 73 69 6e 67 20 69 after parsing i
33e0: 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a s finished..**.*
33f0: 2a 20 52 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 * Routine sqlite
3400: 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 3AlterFinishAddC
3410: 6f 6c 75 6d 6e 28 29 20 77 69 6c 6c 20 62 65 20 olumn() will be
3420: 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 called to comple
3430: 74 65 0a 2a 2a 20 63 6f 64 69 6e 67 20 74 68 65 te.** coding the
3440: 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e "ALTER TABLE ..
3450: 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 . ADD" statement
3460: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
3470: 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 3AlterBeginAddCo
3480: 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 lumn(Parse *pPar
3490: 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 se, SrcList *pSr
34a0: 63 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 c){. Table *pNe
34b0: 77 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 w;. Table *pTab
34c0: 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 ;. int iDb;. i
34d0: 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 41 6c 6c nt i;. int nAll
34e0: 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 oc;. sqlite3 *d
34f0: 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
3500: 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 74 68 . /* Look up th
3510: 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c e table being al
3520: 74 65 72 65 64 2e 20 2a 2f 0a 20 20 61 73 73 65 tered. */. asse
3530: 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 rt( pParse->pNew
3540: 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 Table==0 );. as
3550: 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
3560: 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 eeHoldsAllMutexe
3570: 73 28 64 62 29 20 29 3b 0a 20 20 69 66 28 20 64 s(db) );. if( d
3580: 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
3590: 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 ) goto exit_begi
35a0: 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 n_add_column;.
35b0: 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f pTab = sqlite3Lo
35c0: 63 61 74 65 54 61 62 6c 65 49 74 65 6d 28 70 50 cateTableItem(pP
35d0: 61 72 73 65 2c 20 30 2c 20 26 70 53 72 63 2d 3e arse, 0, &pSrc->
35e0: 61 5b 30 5d 29 3b 0a 20 20 69 66 28 20 21 70 54 a[0]);. if( !pT
35f0: 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 ab ) goto exit_b
3600: 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b egin_add_column;
3610: 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
3620: 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
3630: 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 LE. if( IsVirtu
3640: 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 al(pTab) ){.
3650: 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
3660: 70 50 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c pParse, "virtual
3670: 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 tables may not
3680: 62 65 20 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 be altered");.
3690: 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 goto exit_begi
36a0: 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 n_add_column;.
36b0: 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d }.#endif.. /* M
36c0: 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 69 73 ake sure this is
36d0: 20 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 not an attempt
36e0: 74 6f 20 41 4c 54 45 52 20 61 20 76 69 65 77 2e to ALTER a view.
36f0: 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e */. if( pTab->
3700: 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 pSelect ){. s
3710: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
3720: 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 Parse, "Cannot a
3730: 64 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 dd a column to a
3740: 20 76 69 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 view");. got
3750: 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 o exit_begin_add
3760: 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 69 _column;. }. i
3770: 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 69 73 f( SQLITE_OK!=is
3780: 53 79 73 74 65 6d 54 61 62 6c 65 28 70 50 61 72 SystemTable(pPar
3790: 73 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 se, pTab->zName)
37a0: 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 ){. goto exi
37b0: 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 t_begin_add_colu
37c0: 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 mn;. }.. asser
37d0: 74 28 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f t( pTab->addColO
37e0: 66 66 73 65 74 3e 30 20 29 3b 0a 20 20 69 44 62 ffset>0 );. iDb
37f0: 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 = sqlite3Schema
3800: 54 6f 49 6e 64 65 78 32 28 64 62 2c 20 70 54 61 ToIndex2(db, pTa
3810: 62 2d 3e 70 53 63 68 65 6d 61 2c 20 30 29 3b 0a b->pSchema, 0);.
3820: 0a 20 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79 . /* Put a copy
3830: 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 of the Table st
3840: 72 75 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e ruct in Parse.pN
3850: 65 77 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a ewTable for the.
3860: 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43 ** sqlite3AddC
3870: 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e olumn() function
3880: 20 61 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20 and friends to
3890: 6d 6f 64 69 66 79 2e 20 20 42 75 74 20 6d 6f 64 modify. But mod
38a0: 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d ify. ** the nam
38b0: 65 20 62 79 20 61 64 64 69 6e 67 20 61 6e 20 22 e by adding an "
38c0: 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f sqlite_altertab_
38d0: 22 20 70 72 65 66 69 78 2e 20 20 42 79 20 61 64 " prefix. By ad
38e0: 64 69 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 ding this. ** p
38f0: 72 65 66 69 78 2c 20 77 65 20 69 6e 73 75 72 65 refix, we insure
3900: 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65 20 77 that the name w
3910: 69 6c 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 ill not collide
3920: 77 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 with an existing
3930: 0a 20 20 2a 2a 20 74 61 62 6c 65 20 62 65 63 61 . ** table beca
3940: 75 73 65 20 75 73 65 72 20 74 61 62 6c 65 20 61 use user table a
3950: 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 re not allowed t
3960: 6f 20 68 61 76 65 20 74 68 65 20 22 73 71 6c 69 o have the "sqli
3970: 74 65 5f 22 0a 20 20 2a 2a 20 70 72 65 66 69 78 te_". ** prefix
3980: 20 6f 6e 20 74 68 65 69 72 20 6e 61 6d 65 2e 0a on their name..
3990: 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54 */. pNew = (T
39a0: 61 62 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 4d able*)sqlite3DbM
39b0: 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
39c0: 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 zeof(Table));.
39d0: 69 66 28 20 21 70 4e 65 77 20 29 20 67 6f 74 6f if( !pNew ) goto
39e0: 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f exit_begin_add_
39f0: 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65 column;. pParse
3a00: 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e ->pNewTable = pN
3a10: 65 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e 54 61 62 ew;. pNew->nTab
3a20: 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d Ref = 1;. pNew-
3a30: 3e 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 >nCol = pTab->nC
3a40: 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e ol;. assert( pN
3a50: 65 77 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 ew->nCol>0 );.
3a60: 6e 41 6c 6c 6f 63 20 3d 20 28 28 28 70 4e 65 77 nAlloc = (((pNew
3a70: 2d 3e 6e 43 6f 6c 2d 31 29 2f 38 29 2a 38 29 2b ->nCol-1)/8)*8)+
3a80: 38 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 41 6c 8;. assert( nAl
3a90: 6c 6f 63 3e 3d 70 4e 65 77 2d 3e 6e 43 6f 6c 20 loc>=pNew->nCol
3aa0: 26 26 20 6e 41 6c 6c 6f 63 25 38 3d 3d 30 20 26 && nAlloc%8==0 &
3ab0: 26 20 6e 41 6c 6c 6f 63 2d 70 4e 65 77 2d 3e 6e & nAlloc-pNew->n
3ac0: 43 6f 6c 3c 38 20 29 3b 0a 20 20 70 4e 65 77 2d Col<8 );. pNew-
3ad0: 3e 61 43 6f 6c 20 3d 20 28 43 6f 6c 75 6d 6e 2a >aCol = (Column*
3ae0: 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 )sqlite3DbMalloc
3af0: 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
3b00: 43 6f 6c 75 6d 6e 29 2a 6e 41 6c 6c 6f 63 29 3b Column)*nAlloc);
3b10: 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d . pNew->zName =
3b20: 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
3b30: 64 62 2c 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 db, "sqlite_alte
3b40: 72 74 61 62 5f 25 73 22 2c 20 70 54 61 62 2d 3e rtab_%s", pTab->
3b50: 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 zName);. if( !p
3b60: 4e 65 77 2d 3e 61 43 6f 6c 20 7c 7c 20 21 70 4e New->aCol || !pN
3b70: 65 77 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 ew->zName ){.
3b80: 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c assert( db->mal
3b90: 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 locFailed );.
3ba0: 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e goto exit_begin
3bb0: 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d _add_column;. }
3bc0: 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e . memcpy(pNew->
3bd0: 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c aCol, pTab->aCol
3be0: 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 , sizeof(Column)
3bf0: 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 *pNew->nCol);.
3c00: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 77 2d for(i=0; i<pNew-
3c10: 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nCol; i++){.
3c20: 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 Column *pCol =
3c30: 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a &pNew->aCol[i];.
3c40: 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 pCol->zName
3c50: 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
3c60: 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d p(db, pCol->zNam
3c70: 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 e);. pCol->zC
3c80: 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f oll = 0;. pCo
3c90: 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 l->pDflt = 0;.
3ca0: 7d 0a 20 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d }. pNew->pSchem
3cb0: 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d a = db->aDb[iDb]
3cc0: 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 4e 65 77 .pSchema;. pNew
3cd0: 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d ->addColOffset =
3ce0: 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 pTab->addColOff
3cf0: 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e 54 61 set;. pNew->nTa
3d00: 62 52 65 66 20 3d 20 31 3b 0a 0a 65 78 69 74 5f bRef = 1;..exit_
3d10: 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e begin_add_column
3d20: 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 :. sqlite3SrcLi
3d30: 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 stDelete(db, pSr
3d40: 63 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a c);. return;.}.
3d50: 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 ./*.** Parameter
3d60: 20 70 54 61 62 20 69 73 20 74 68 65 20 73 75 62 pTab is the sub
3d70: 6a 65 63 74 20 6f 66 20 61 6e 20 41 4c 54 45 52 ject of an ALTER
3d80: 20 54 41 42 4c 45 20 2e 2e 2e 20 52 45 4e 41 4d TABLE ... RENAM
3d90: 45 20 43 4f 4c 55 4d 4e 0a 2a 2a 20 63 6f 6d 6d E COLUMN.** comm
3da0: 61 6e 64 2e 20 54 68 69 73 20 66 75 6e 63 74 69 and. This functi
3db0: 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 on checks if the
3dc0: 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 65 77 table is a view
3dd0: 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 or virtual.** t
3de0: 61 62 6c 65 20 28 63 6f 6c 75 6d 6e 73 20 6f 66 able (columns of
3df0: 20 76 69 65 77 73 20 6f 72 20 76 69 72 74 75 61 views or virtua
3e00: 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 l tables may not
3e10: 20 62 65 20 72 65 6e 61 6d 65 64 29 2e 20 49 66 be renamed). If
3e20: 20 73 6f 2c 0a 2a 2a 20 69 74 20 6c 6f 61 64 73 so,.** it loads
3e30: 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
3e40: 65 20 69 6e 74 6f 20 70 50 61 72 73 65 20 61 6e e into pParse an
3e50: 64 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 d returns non-ze
3e60: 72 6f 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 66 ro..**.** Or, if
3e70: 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76 pTab is not a v
3e80: 69 65 77 20 6f 72 20 76 69 72 74 75 61 6c 20 74 iew or virtual t
3e90: 61 62 6c 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 able, zero is re
3ea0: 74 75 72 6e 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 turned..*/.#if !
3eb0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
3ec0: 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 MIT_VIEW) || !de
3ed0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
3ee0: 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a T_VIRTUALTABLE).
3ef0: 73 74 61 74 69 63 20 69 6e 74 20 69 73 52 65 61 static int isRea
3f00: 6c 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 lTable(Parse *pP
3f10: 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 arse, Table *pTa
3f20: 62 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 b){. const char
3f30: 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 23 69 66 *zType = 0;.#if
3f40: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
3f50: 5f 56 49 45 57 0a 20 20 69 66 28 20 70 54 61 62 _VIEW. if( pTab
3f60: 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 ->pSelect ){.
3f70: 20 7a 54 79 70 65 20 3d 20 22 76 69 65 77 22 3b zType = "view";
3f80: 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e . }.#endif.#ifn
3f90: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
3fa0: 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 VIRTUALTABLE. i
3fb0: 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 f( IsVirtual(pTa
3fc0: 62 29 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 b) ){. zType
3fd0: 3d 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 = "virtual table
3fe0: 22 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 ";. }.#endif.
3ff0: 69 66 28 20 7a 54 79 70 65 20 29 7b 0a 20 20 20 if( zType ){.
4000: 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
4010: 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 (. pParse
4020: 2c 20 22 63 61 6e 6e 6f 74 20 72 65 6e 61 6d 65 , "cannot rename
4030: 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 25 73 20 5c columns of %s \
4040: 22 25 73 5c 22 22 2c 20 7a 54 79 70 65 2c 20 70 "%s\"", zType, p
4050: 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 Tab->zName. )
4060: 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
4070: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
4080: 7d 0a 23 65 6c 73 65 20 2f 2a 20 21 64 65 66 69 }.#else /* !defi
4090: 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
40a0: 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 VIEW) || !define
40b0: 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
40c0: 52 54 55 41 4c 54 41 42 4c 45 29 20 2a 2f 0a 23 RTUALTABLE) */.#
40d0: 20 64 65 66 69 6e 65 20 69 73 52 65 61 6c 54 61 define isRealTa
40e0: 62 6c 65 28 78 2c 79 29 20 28 30 29 0a 23 65 6e ble(x,y) (0).#en
40f0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c dif../*.** Handl
4100: 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 es the following
4110: 20 70 61 72 73 65 72 20 72 65 64 75 63 74 69 6f parser reductio
4120: 6e 3a 0a 2a 2a 0a 2a 2a 20 20 63 6d 64 20 3a 3a n:.**.** cmd ::
4130: 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 70 53 = ALTER TABLE pS
4140: 72 63 20 52 45 4e 41 4d 45 20 43 4f 4c 55 4d 4e rc RENAME COLUMN
4150: 20 70 4f 6c 64 20 54 4f 20 70 4e 65 77 0a 2a 2f pOld TO pNew.*/
4160: 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 .void sqlite3Alt
4170: 65 72 52 65 6e 61 6d 65 43 6f 6c 75 6d 6e 28 0a erRenameColumn(.
4180: 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
4190: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
41a0: 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
41b0: 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 text */. SrcLis
41c0: 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 t *pSrc,
41d0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 /* Tab
41e0: 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 le being altered
41f0: 2e 20 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 . pSrc->nSrc==1
4200: 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4f 6c */. Token *pOl
4210: 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
4220: 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
4230: 20 63 6f 6c 75 6d 6e 20 62 65 69 6e 67 20 63 68 column being ch
4240: 61 6e 67 65 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e anged */. Token
4250: 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20 20 *pNew
4260: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
4270: 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f w column name */
4280: 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 .){. sqlite3 *d
4290: 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 b = pParse->db;
42a0: 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
42b0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
42c0: 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 Table *pTab;
42d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
42e0: 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 /* Table being
42f0: 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e updated */. in
4300: 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 t iCol;
4310: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4320: 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e Index of column
4330: 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a being renamed *
4340: 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 6c 64 20 3d /. char *zOld =
4350: 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
4360: 20 20 20 20 2f 2a 20 4f 6c 64 20 63 6f 6c 75 6d /* Old colum
4370: 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 n name */. char
4380: 20 2a 7a 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 *zNew = 0;
4390: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
43a0: 65 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a ew column name *
43b0: 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
43c0: 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 zDb;
43d0: 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 /* Name of s
43e0: 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 chema containing
43f0: 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
4400: 69 6e 74 20 69 53 63 68 65 6d 61 3b 20 20 20 20 int iSchema;
4410: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4420: 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 /* Index of the
4430: 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 schema */. int
4440: 62 51 75 6f 74 65 3b 20 20 20 20 20 20 20 20 20 bQuote;
4450: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
4460: 72 75 65 20 74 6f 20 71 75 6f 74 65 20 74 68 65 rue to quote the
4470: 20 6e 65 77 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 new name */..
4480: 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 /* Locate the ta
4490: 62 6c 65 20 74 6f 20 62 65 20 61 6c 74 65 72 65 ble to be altere
44a0: 64 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 d */. pTab = sq
44b0: 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
44c0: 49 74 65 6d 28 70 50 61 72 73 65 2c 20 30 2c 20 Item(pParse, 0,
44d0: 26 70 53 72 63 2d 3e 61 5b 30 5d 29 3b 0a 20 20 &pSrc->a[0]);.
44e0: 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f if( !pTab ) goto
44f0: 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 63 6f 6c exit_rename_col
4500: 75 6d 6e 3b 0a 0a 20 20 2f 2a 20 43 61 6e 6e 6f umn;.. /* Canno
4510: 74 20 61 6c 74 65 72 20 61 20 73 79 73 74 65 6d t alter a system
4520: 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 table */. if(
4530: 53 51 4c 49 54 45 5f 4f 4b 21 3d 69 73 53 79 73 SQLITE_OK!=isSys
4540: 74 65 6d 54 61 62 6c 65 28 70 50 61 72 73 65 2c temTable(pParse,
4550: 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 29 20 pTab->zName) )
4560: 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 goto exit_rename
4570: 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 53 _column;. if( S
4580: 51 4c 49 54 45 5f 4f 4b 21 3d 69 73 52 65 61 6c QLITE_OK!=isReal
4590: 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 Table(pParse, pT
45a0: 61 62 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f ab) ) goto exit_
45b0: 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 3b 0a 0a rename_column;..
45c0: 20 20 2f 2a 20 57 68 69 63 68 20 73 63 68 65 6d /* Which schem
45d0: 61 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c a holds the tabl
45e0: 65 20 74 6f 20 62 65 20 61 6c 74 65 72 65 64 20 e to be altered
45f0: 2a 2f 20 20 0a 20 20 69 53 63 68 65 6d 61 20 3d */ . iSchema =
4600: 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
4610: 49 6e 64 65 78 32 28 64 62 2c 20 70 54 61 62 2d Index2(db, pTab-
4620: 3e 70 53 63 68 65 6d 61 2c 20 30 29 3b 0a 20 20 >pSchema, 0);.
4630: 61 73 73 65 72 74 28 20 69 53 63 68 65 6d 61 3e assert( iSchema>
4640: 3d 30 20 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 =0 );. zDb = db
4650: 2d 3e 61 44 62 5b 69 53 63 68 65 6d 61 5d 2e 7a ->aDb[iSchema].z
4660: 44 62 53 4e 61 6d 65 3b 0a 0a 23 69 66 6e 64 65 DbSName;..#ifnde
4670: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
4680: 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a THORIZATION. /*
4690: 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 Invoke the auth
46a0: 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 orization callba
46b0: 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c ck. */. if( sql
46c0: 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
46d0: 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 arse, SQLITE_ALT
46e0: 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 ER_TABLE, zDb, p
46f0: 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 Tab->zName, 0) )
4700: 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f {. goto exit_
4710: 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 3b 0a 20 rename_column;.
4720: 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
4730: 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 6c Make sure the ol
4740: 64 20 6e 61 6d 65 20 72 65 61 6c 6c 79 20 69 73 d name really is
4750: 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 a column name i
4760: 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 n the table to b
4770: 65 0a 20 20 2a 2a 20 61 6c 74 65 72 65 64 2e 20 e. ** altered.
4780: 20 53 65 74 20 69 43 6f 6c 20 74 6f 20 62 65 20 Set iCol to be
4790: 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
47a0: 20 63 6f 6c 75 6d 6e 20 62 65 69 6e 67 20 72 65 column being re
47b0: 6e 61 6d 65 64 20 2a 2f 0a 20 20 7a 4f 6c 64 20 named */. zOld
47c0: 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
47d0: 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4f 6c 64 29 mToken(db, pOld)
47e0: 3b 0a 20 20 69 66 28 20 21 7a 4f 6c 64 20 29 20 ;. if( !zOld )
47f0: 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 goto exit_rename
4800: 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 66 6f 72 28 69 _column;. for(i
4810: 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 Col=0; iCol<pTab
4820: 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b ->nCol; iCol++){
4830: 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 . if( 0==sqli
4840: 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d te3StrICmp(pTab-
4850: 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d >aCol[iCol].zNam
4860: 65 2c 20 7a 4f 6c 64 29 20 29 20 62 72 65 61 6b e, zOld) ) break
4870: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c ;. }. if( iCol
4880: 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a ==pTab->nCol ){.
4890: 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
48a0: 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 Msg(pParse, "no
48b0: 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 such column: \"%
48c0: 73 5c 22 22 2c 20 7a 4f 6c 64 29 3b 0a 20 20 20 s\"", zOld);.
48d0: 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d goto exit_renam
48e0: 65 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 e_column;. }..
48f0: 20 2f 2a 20 44 6f 20 74 68 65 20 72 65 6e 61 6d /* Do the renam
4900: 65 20 6f 70 65 72 61 74 69 6f 6e 20 75 73 69 6e e operation usin
4910: 67 20 61 20 72 65 63 75 72 73 69 76 65 20 55 50 g a recursive UP
4920: 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 74 DATE statement t
4930: 68 61 74 0a 20 20 2a 2a 20 75 73 65 73 20 74 68 hat. ** uses th
4940: 65 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f e sqlite_rename_
4950: 63 6f 6c 75 6d 6e 28 29 20 53 51 4c 20 66 75 6e column() SQL fun
4960: 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 70 75 74 65 ction to compute
4970: 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 43 52 the new. ** CR
4980: 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 74 EATE statement t
4990: 65 78 74 20 66 6f 72 20 74 68 65 20 73 71 6c 69 ext for the sqli
49a0: 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e te_master table.
49b0: 0a 20 20 2a 2f 0a 20 20 7a 4e 65 77 20 3d 20 73 . */. zNew = s
49c0: 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
49d0: 6b 65 6e 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 ken(db, pNew);.
49e0: 20 69 66 28 20 21 7a 4e 65 77 20 29 20 67 6f 74 if( !zNew ) got
49f0: 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 63 6f o exit_rename_co
4a00: 6c 75 6d 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 lumn;. assert(
4a10: 70 4e 65 77 2d 3e 6e 3e 30 20 29 3b 0a 20 20 62 pNew->n>0 );. b
4a20: 51 75 6f 74 65 20 3d 20 73 71 6c 69 74 65 33 49 Quote = sqlite3I
4a30: 73 71 75 6f 74 65 28 70 4e 65 77 2d 3e 7a 5b 30 squote(pNew->z[0
4a40: 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 ]);. sqlite3Nes
4a50: 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
4a60: 20 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 . "UPDATE
4a70: 5c 22 25 77 5c 22 2e 25 73 20 53 45 54 20 22 0a \"%w\".%s SET ".
4a80: 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 71 6c "sql = sql
4a90: 69 74 65 5f 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d ite_rename_colum
4aa0: 6e 28 73 71 6c 2c 20 74 79 70 65 2c 20 6e 61 6d n(sql, type, nam
4ab0: 65 2c 20 25 51 2c 20 25 51 2c 20 25 64 2c 20 25 e, %Q, %Q, %d, %
4ac0: 51 2c 20 25 64 2c 20 25 64 29 20 22 0a 20 20 20 Q, %d, %d) ".
4ad0: 20 20 20 22 57 48 45 52 45 20 6e 61 6d 65 20 4e "WHERE name N
4ae0: 4f 54 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f OT LIKE 'sqlite_
4af0: 25 25 27 20 41 4e 44 20 28 74 79 70 65 20 21 3d %%' AND (type !=
4b00: 20 27 69 6e 64 65 78 27 20 4f 52 20 74 62 6c 5f 'index' OR tbl_
4b10: 6e 61 6d 65 20 3d 20 25 51 29 22 0a 20 20 20 20 name = %Q)".
4b20: 20 20 22 20 41 4e 44 20 73 71 6c 20 4e 4f 54 20 " AND sql NOT
4b30: 4c 49 4b 45 20 27 63 72 65 61 74 65 20 76 69 72 LIKE 'create vir
4b40: 74 75 61 6c 25 25 27 22 2c 0a 20 20 20 20 20 20 tual%%'",.
4b50: 7a 44 62 2c 20 4d 41 53 54 45 52 5f 4e 41 4d 45 zDb, MASTER_NAME
4b60: 2c 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 70 54 , . zDb, pT
4b70: 61 62 2d 3e 7a 4e 61 6d 65 2c 20 69 43 6f 6c 2c ab->zName, iCol,
4b80: 20 7a 4e 65 77 2c 20 62 51 75 6f 74 65 2c 20 69 zNew, bQuote, i
4b90: 53 63 68 65 6d 61 3d 3d 31 2c 0a 20 20 20 20 20 Schema==1,.
4ba0: 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 29 pTab->zName. )
4bb0: 3b 0a 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 ;.. sqlite3Nest
4bc0: 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 edParse(pParse,
4bd0: 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 74 . "UPDATE t
4be0: 65 6d 70 2e 25 73 20 53 45 54 20 22 0a 20 20 20 emp.%s SET ".
4bf0: 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 "sql = sqlite
4c00: 5f 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 28 73 _rename_column(s
4c10: 71 6c 2c 20 74 79 70 65 2c 20 6e 61 6d 65 2c 20 ql, type, name,
4c20: 25 51 2c 20 25 51 2c 20 25 64 2c 20 25 51 2c 20 %Q, %Q, %d, %Q,
4c30: 25 64 2c 20 31 29 20 22 0a 20 20 20 20 20 20 22 %d, 1) ". "
4c40: 57 48 45 52 45 20 74 79 70 65 20 49 4e 20 28 27 WHERE type IN ('
4c50: 74 72 69 67 67 65 72 27 2c 20 27 76 69 65 77 27 trigger', 'view'
4c60: 29 22 2c 0a 20 20 20 20 20 20 4d 41 53 54 45 52 )",. MASTER
4c70: 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 7a 44 _NAME, . zD
4c80: 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 b, pTab->zName,
4c90: 69 43 6f 6c 2c 20 7a 4e 65 77 2c 20 62 51 75 6f iCol, zNew, bQuo
4ca0: 74 65 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 44 72 te. );.. /* Dr
4cb0: 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 op and reload th
4cc0: 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
4cd0: 61 2e 20 2a 2f 0a 20 20 72 65 6e 61 6d 65 52 65 a. */. renameRe
4ce0: 6c 6f 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 loadSchema(pPars
4cf0: 65 2c 20 69 53 63 68 65 6d 61 29 3b 0a 20 20 72 e, iSchema);. r
4d00: 65 6e 61 6d 65 54 65 73 74 53 63 68 65 6d 61 28 enameTestSchema(
4d10: 70 50 61 72 73 65 2c 20 7a 44 62 2c 20 69 53 63 pParse, zDb, iSc
4d20: 68 65 6d 61 3d 3d 31 29 3b 0a 0a 20 65 78 69 74 hema==1);.. exit
4d30: 5f 72 65 6e 61 6d 65 5f 63 6f 6c 75 6d 6e 3a 0a _rename_column:.
4d40: 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 sqlite3SrcList
4d50: 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 Delete(db, pSrc)
4d60: 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
4d70: 65 28 64 62 2c 20 7a 4f 6c 64 29 3b 0a 20 20 73 e(db, zOld);. s
4d80: 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
4d90: 20 7a 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e zNew);. return
4da0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 ;.}../*.** Each
4db0: 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 6f 62 6a 65 RenameToken obje
4dc0: 63 74 20 6d 61 70 73 20 61 6e 20 65 6c 65 6d 65 ct maps an eleme
4dd0: 6e 74 20 6f 66 20 74 68 65 20 70 61 72 73 65 20 nt of the parse
4de0: 74 72 65 65 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 tree into.** the
4df0: 20 74 6f 6b 65 6e 20 74 68 61 74 20 67 65 6e 65 token that gene
4e00: 72 61 74 65 64 20 74 68 61 74 20 65 6c 65 6d 65 rated that eleme
4e10: 6e 74 2e 20 20 54 68 65 20 70 61 72 73 65 20 74 nt. The parse t
4e20: 72 65 65 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20 6d ree element.** m
4e30: 69 67 68 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a ight be one of:.
4e40: 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20 41 20 70 **.** * A p
4e50: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 45 78 70 ointer to an Exp
4e60: 72 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 r that represent
4e70: 73 20 61 6e 20 49 44 0a 2a 2a 20 20 20 20 20 2a s an ID.** *
4e80: 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 The name of a
4e90: 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 table column in
4ea0: 43 6f 6c 75 6d 6e 2e 7a 4e 61 6d 65 0a 2a 2a 0a Column.zName.**.
4eb0: 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 52 65 6e ** A list of Ren
4ec0: 61 6d 65 54 6f 6b 65 6e 20 6f 62 6a 65 63 74 73 ameToken objects
4ed0: 20 63 61 6e 20 62 65 20 63 6f 6e 73 74 72 75 63 can be construc
4ee0: 74 65 64 20 64 75 72 69 6e 67 20 70 61 72 73 69 ted during parsi
4ef0: 6e 67 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 ng..** Each new
4f00: 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 object is create
4f10: 64 20 62 79 20 73 71 6c 69 74 65 33 52 65 6e 61 d by sqlite3Rena
4f20: 6d 65 54 6f 6b 65 6e 4d 61 70 28 29 2e 0a 2a 2a meTokenMap()..**
4f30: 20 41 73 20 74 68 65 20 70 61 72 73 65 20 74 72 As the parse tr
4f40: 65 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 ee is transforme
4f50: 64 2c 20 74 68 65 20 73 71 6c 69 74 65 33 52 65 d, the sqlite3Re
4f60: 6e 61 6d 65 54 6f 6b 65 6e 52 65 6d 61 70 28 29 nameTokenRemap()
4f70: 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 75 .** routine is u
4f80: 73 65 64 20 74 6f 20 6b 65 65 70 20 74 68 65 20 sed to keep the
4f90: 6d 61 70 70 69 6e 67 20 63 75 72 72 65 6e 74 2e mapping current.
4fa0: 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 .**.** After the
4fb0: 20 70 61 72 73 65 20 66 69 6e 69 73 68 65 73 2c parse finishes,
4fc0: 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 renameTokenFind
4fd0: 28 29 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 () routine can b
4fe0: 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 6f e used.** to loo
4ff0: 6b 20 75 70 20 74 68 65 20 61 63 74 75 61 6c 20 k up the actual
5000: 74 6f 6b 65 6e 20 76 61 6c 75 65 20 74 68 61 74 token value that
5010: 20 63 72 65 61 74 65 64 20 73 6f 6d 65 20 65 6c created some el
5020: 65 6d 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 20 ement in.** the
5030: 70 61 72 73 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 parse tree..*/.s
5040: 74 72 75 63 74 20 52 65 6e 61 6d 65 54 6f 6b 65 truct RenameToke
5050: 6e 20 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 20 20 n {. void *p;
5060: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5070: 50 61 72 73 65 20 74 72 65 65 20 65 6c 65 6d 65 Parse tree eleme
5080: 6e 74 20 63 72 65 61 74 65 64 20 62 79 20 74 6f nt created by to
5090: 6b 65 6e 20 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e ken t */. Token
50a0: 20 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
50b0: 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 /* The token t
50c0: 68 61 74 20 63 72 65 61 74 65 64 20 70 61 72 73 hat created pars
50d0: 65 20 74 72 65 65 20 65 6c 65 6d 65 6e 74 20 70 e tree element p
50e0: 20 2a 2f 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 */. RenameToke
50f0: 6e 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 n *pNext; /*
5100: 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f Next is a list o
5110: 66 20 61 6c 6c 20 52 65 6e 61 6d 65 54 6f 6b 65 f all RenameToke
5120: 6e 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a n objects */.};.
5130: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 ./*.** The conte
5140: 78 74 20 6f 66 20 61 6e 20 41 4c 54 45 52 20 54 xt of an ALTER T
5150: 41 42 4c 45 20 52 45 4e 41 4d 45 20 43 4f 4c 55 ABLE RENAME COLU
5160: 4d 4e 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 MN operation tha
5170: 74 20 67 65 74 73 20 70 61 73 73 65 64 0a 2a 2a t gets passed.**
5180: 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68 65 20 57 down into the W
5190: 61 6c 6b 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 alker..*/.typede
51a0: 66 20 73 74 72 75 63 74 20 52 65 6e 61 6d 65 43 f struct RenameC
51b0: 74 78 20 52 65 6e 61 6d 65 43 74 78 3b 0a 73 74 tx RenameCtx;.st
51c0: 72 75 63 74 20 52 65 6e 61 6d 65 43 74 78 20 7b ruct RenameCtx {
51d0: 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a . RenameToken *
51e0: 70 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 pList;
51f0: 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 6f /* List of to
5200: 6b 65 6e 73 20 74 6f 20 6f 76 65 72 77 72 69 74 kens to overwrit
5210: 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 69 73 74 e */. int nList
5220: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5230: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5240: 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 70 4c of tokens in pL
5250: 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f ist */. int iCo
5260: 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
5270: 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
5280: 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 62 65 69 6e x of column bein
5290: 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 54 g renamed */. T
52a0: 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 able *pTab;
52b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
52c0: 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 41 4c * Table being AL
52d0: 54 45 52 65 64 20 2a 2f 20 0a 20 20 63 6f 6e 73 TERed */ . cons
52e0: 74 20 63 68 61 72 20 2a 7a 4f 6c 64 3b 20 20 20 t char *zOld;
52f0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
5300: 6c 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a ld column name *
5310: 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c /.};..#ifdef SQL
5320: 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ITE_DEBUG./*.**
5330: 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
5340: 20 6f 6e 6c 79 20 66 6f 72 20 64 65 62 75 67 67 only for debugg
5350: 69 6e 67 2e 20 49 74 20 70 65 72 66 6f 72 6d 73 ing. It performs
5360: 20 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a 2a two tasks:.**.*
5370: 2a 20 20 20 31 2e 20 43 68 65 63 6b 73 20 74 68 * 1. Checks th
5380: 61 74 20 70 6f 69 6e 74 65 72 20 70 50 74 72 20 at pointer pPtr
5390: 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 does not already
53a0: 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 0a appear in the .
53b0: 2a 2a 20 20 20 20 20 20 72 65 6e 61 6d 65 2d 74 ** rename-t
53c0: 6f 6b 65 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a oken list..**.**
53d0: 20 20 20 32 2e 20 44 65 72 65 66 65 72 65 6e 63 2. Dereferenc
53e0: 65 73 20 65 61 63 68 20 70 6f 69 6e 74 65 72 20 es each pointer
53f0: 69 6e 20 74 68 65 20 72 65 6e 61 6d 65 2d 74 6f in the rename-to
5400: 6b 65 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 ken list..**.**
5410: 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20 6d 6f The second is mo
5420: 73 74 20 65 66 66 65 63 74 69 76 65 20 77 68 65 st effective whe
5430: 6e 20 64 65 62 75 67 67 69 6e 67 20 75 6e 64 65 n debugging unde
5440: 72 20 76 61 6c 67 72 69 6e 64 20 6f 72 0a 2a 2a r valgrind or.**
5450: 20 61 64 64 72 65 73 73 2d 73 61 6e 69 74 69 7a address-sanitiz
5460: 65 72 20 6f 72 20 73 69 6d 69 6c 61 72 2e 20 49 er or similar. I
5470: 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 70 f any of these p
5480: 6f 69 6e 74 65 72 73 20 6e 6f 20 6c 6f 6e 67 65 ointers no longe
5490: 72 20 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 76 r .** point to v
54a0: 61 6c 69 64 20 6f 62 6a 65 63 74 73 2c 20 61 6e alid objects, an
54b0: 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 72 61 exception is ra
54c0: 69 73 65 64 20 62 79 20 74 68 65 20 6d 65 6d 6f ised by the memo
54d0: 72 79 2d 63 68 65 63 6b 69 6e 67 20 0a 2a 2a 20 ry-checking .**
54e0: 74 6f 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tool..**.** The
54f0: 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 69 73 point of this is
5500: 20 74 6f 20 70 72 65 76 65 6e 74 20 63 6f 6d 70 to prevent comp
5510: 61 72 69 73 6f 6e 73 20 6f 66 20 69 6e 76 61 6c arisons of inval
5520: 69 64 20 70 6f 69 6e 74 65 72 20 76 61 6c 75 65 id pointer value
5530: 73 2e 0a 2a 2a 20 45 76 65 6e 20 74 68 6f 75 67 s..** Even thoug
5540: 68 20 74 68 69 73 20 61 6c 77 61 79 73 20 73 65 h this always se
5550: 65 6d 73 20 74 6f 20 77 6f 72 6b 2c 20 69 74 20 ems to work, it
5560: 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 63 63 is undefined acc
5570: 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 0a 2a 2a ording to the.**
5580: 20 43 20 73 74 61 6e 64 61 72 64 2e 20 45 78 61 C standard. Exa
5590: 6d 70 6c 65 20 6f 66 20 75 6e 64 65 66 69 6e 65 mple of undefine
55a0: 64 20 63 6f 6d 70 61 72 69 73 6f 6e 3a 0a 2a 2a d comparison:.**
55b0: 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f .** sqlite3_
55c0: 66 72 65 65 28 78 29 3b 0a 2a 2a 20 20 20 20 20 free(x);.**
55d0: 69 66 28 20 78 3d 3d 79 20 29 20 2e 2e 2e 0a 2a if( x==y ) ....*
55e0: 2a 0a 2a 2a 20 54 65 63 68 6e 69 63 61 6c 6c 79 *.** Technically
55f0: 2c 20 61 73 20 78 20 6e 6f 20 6c 6f 6e 67 65 72 , as x no longer
5600: 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 61 20 76 points into a v
5610: 61 6c 69 64 20 6f 62 6a 65 63 74 20 6f 72 20 74 alid object or t
5620: 6f 20 74 68 65 20 62 79 74 65 0a 2a 2a 20 66 6f o the byte.** fo
5630: 6c 6c 6f 77 69 6e 67 20 61 20 76 61 6c 69 64 20 llowing a valid
5640: 6f 62 6a 65 63 74 2c 20 69 74 20 6d 61 79 20 6e object, it may n
5650: 6f 74 20 62 65 20 75 73 65 64 20 69 6e 20 63 6f ot be used in co
5660: 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 mparison operati
5670: 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ons..*/.static v
5680: 6f 69 64 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 43 oid renameTokenC
5690: 68 65 63 6b 41 6c 6c 28 50 61 72 73 65 20 2a 70 heckAll(Parse *p
56a0: 50 61 72 73 65 2c 20 76 6f 69 64 20 2a 70 50 74 Parse, void *pPt
56b0: 72 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 r){. if( pParse
56c0: 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 70 50 61 ->nErr==0 && pPa
56d0: 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 rse->db->mallocF
56e0: 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 ailed==0 ){.
56f0: 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a 70 3b 0a RenameToken *p;.
5700: 20 20 20 20 75 38 20 69 20 3d 20 30 3b 0a 20 20 u8 i = 0;.
5710: 20 20 66 6f 72 28 70 3d 70 50 61 72 73 65 2d 3e for(p=pParse->
5720: 70 52 65 6e 61 6d 65 3b 20 70 3b 20 70 3d 70 2d pRename; p; p=p-
5730: 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 >pNext){. i
5740: 66 28 20 70 2d 3e 70 20 29 7b 0a 20 20 20 20 20 f( p->p ){.
5750: 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 21 assert( p->p!
5760: 3d 70 50 74 72 20 29 3b 0a 20 20 20 20 20 20 20 =pPtr );.
5770: 20 69 20 2b 3d 20 2a 28 75 38 2a 29 28 70 2d 3e i += *(u8*)(p->
5780: 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
5790: 7d 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 }. }.}.#else.#
57a0: 64 65 66 69 6e 65 20 72 65 6e 61 6d 65 54 6f 6b define renameTok
57b0: 65 6e 43 68 65 63 6b 41 6c 6c 28 78 2c 79 29 0a enCheckAll(x,y).
57c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 #endif../*.** Ad
57d0: 64 20 61 20 6e 65 77 20 52 65 6e 61 6d 65 54 6f d a new RenameTo
57e0: 6b 65 6e 20 6f 62 6a 65 63 74 20 6d 61 70 70 69 ken object mappi
57f0: 6e 67 20 70 61 72 73 65 20 74 72 65 65 20 65 6c ng parse tree el
5800: 65 6d 65 6e 74 20 70 50 74 72 20 69 6e 74 6f 0a ement pPtr into.
5810: 2a 2a 20 74 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e ** token *pToken
5820: 20 74 6f 20 74 68 65 20 50 61 72 73 65 20 6f 62 to the Parse ob
5830: 6a 65 63 74 20 63 75 72 72 65 6e 74 6c 79 20 75 ject currently u
5840: 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
5850: 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 n..**.** Return
5860: 61 20 63 6f 70 79 20 6f 66 20 70 50 74 72 2e 0a a copy of pPtr..
5870: 2a 2f 0a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 */.void *sqlite3
5880: 52 65 6e 61 6d 65 54 6f 6b 65 6e 4d 61 70 28 50 RenameTokenMap(P
5890: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 76 6f arse *pParse, vo
58a0: 69 64 20 2a 70 50 74 72 2c 20 54 6f 6b 65 6e 20 id *pPtr, Token
58b0: 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 52 65 6e 61 *pToken){. Rena
58c0: 6d 65 54 6f 6b 65 6e 20 2a 70 4e 65 77 3b 0a 20 meToken *pNew;.
58d0: 20 61 73 73 65 72 74 28 20 70 50 74 72 20 7c 7c assert( pPtr ||
58e0: 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
58f0: 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 locFailed );. r
5900: 65 6e 61 6d 65 54 6f 6b 65 6e 43 68 65 63 6b 41 enameTokenCheckA
5910: 6c 6c 28 70 50 61 72 73 65 2c 20 70 50 74 72 29 ll(pParse, pPtr)
5920: 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 ;. pNew = sqlit
5930: 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 e3DbMallocZero(p
5940: 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f Parse->db, sizeo
5950: 66 28 52 65 6e 61 6d 65 54 6f 6b 65 6e 29 29 3b f(RenameToken));
5960: 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 . if( pNew ){.
5970: 20 20 20 70 4e 65 77 2d 3e 70 20 3d 20 70 50 74 pNew->p = pPt
5980: 72 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 74 20 3d r;. pNew->t =
5990: 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 70 4e *pToken;. pN
59a0: 65 77 2d 3e 70 4e 65 78 74 20 3d 20 70 50 61 72 ew->pNext = pPar
59b0: 73 65 2d 3e 70 52 65 6e 61 6d 65 3b 0a 20 20 20 se->pRename;.
59c0: 20 70 50 61 72 73 65 2d 3e 70 52 65 6e 61 6d 65 pParse->pRename
59d0: 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 = pNew;. }..
59e0: 72 65 74 75 72 6e 20 70 50 74 72 3b 0a 7d 0a 0a return pPtr;.}..
59f0: 2f 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 /*.** It is assu
5a00: 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 20 69 med that there i
5a10: 73 20 61 6c 72 65 61 64 79 20 61 20 52 65 6e 61 s already a Rena
5a20: 6d 65 54 6f 6b 65 6e 20 6f 62 6a 65 63 74 20 61 meToken object a
5a30: 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
5a40: 68 20 70 61 72 73 65 20 74 72 65 65 20 65 6c 65 h parse tree ele
5a50: 6d 65 6e 74 20 70 46 72 6f 6d 2e 20 54 68 69 73 ment pFrom. This
5a60: 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 61 70 73 function remaps
5a70: 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 the associated
5a80: 74 6f 6b 65 6e 0a 2a 2a 20 74 6f 20 70 61 72 73 token.** to pars
5a90: 65 20 74 72 65 65 20 65 6c 65 6d 65 6e 74 20 70 e tree element p
5aa0: 54 6f 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 To..*/.void sqli
5ab0: 74 65 33 52 65 6e 61 6d 65 54 6f 6b 65 6e 52 65 te3RenameTokenRe
5ac0: 6d 61 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 map(Parse *pPars
5ad0: 65 2c 20 76 6f 69 64 20 2a 70 54 6f 2c 20 76 6f e, void *pTo, vo
5ae0: 69 64 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 52 65 id *pFrom){. Re
5af0: 6e 61 6d 65 54 6f 6b 65 6e 20 2a 70 3b 0a 20 20 nameToken *p;.
5b00: 72 65 6e 61 6d 65 54 6f 6b 65 6e 43 68 65 63 6b renameTokenCheck
5b10: 41 6c 6c 28 70 50 61 72 73 65 2c 20 70 54 6f 29 All(pParse, pTo)
5b20: 3b 0a 20 20 66 6f 72 28 70 3d 70 50 61 72 73 65 ;. for(p=pParse
5b30: 2d 3e 70 52 65 6e 61 6d 65 3b 20 70 3b 20 70 3d ->pRename; p; p=
5b40: 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 p->pNext){. i
5b50: 66 28 20 70 2d 3e 70 3d 3d 70 46 72 6f 6d 20 29 f( p->p==pFrom )
5b60: 7b 0a 20 20 20 20 20 20 70 2d 3e 70 20 3d 20 70 {. p->p = p
5b70: 54 6f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b To;. break;
5b80: 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
5b90: 0a 2a 2a 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 .** Walker callb
5ba0: 61 63 6b 20 75 73 65 64 20 62 79 20 73 71 6c 69 ack used by sqli
5bb0: 74 65 33 52 65 6e 61 6d 65 45 78 70 72 55 6e 6d te3RenameExprUnm
5bc0: 61 70 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ap()..*/.static
5bd0: 69 6e 74 20 72 65 6e 61 6d 65 55 6e 6d 61 70 45 int renameUnmapE
5be0: 78 70 72 43 62 28 57 61 6c 6b 65 72 20 2a 70 57 xprCb(Walker *pW
5bf0: 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 alker, Expr *pEx
5c00: 70 72 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 pr){. Parse *pP
5c10: 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e arse = pWalker->
5c20: 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 pParse;. sqlite
5c30: 33 52 65 6e 61 6d 65 54 6f 6b 65 6e 52 65 6d 61 3RenameTokenRema
5c40: 70 28 70 50 61 72 73 65 2c 20 30 2c 20 28 76 6f p(pParse, 0, (vo
5c50: 69 64 2a 29 70 45 78 70 72 29 3b 0a 20 20 72 65 id*)pExpr);. re
5c60: 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
5c70: 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f e;.}../*.** Remo
5c80: 76 65 20 61 6c 6c 20 6e 6f 64 65 73 20 74 68 61 ve all nodes tha
5c90: 74 20 61 72 65 20 70 61 72 74 20 6f 66 20 65 78 t are part of ex
5ca0: 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 66 pression pExpr f
5cb0: 72 6f 6d 20 74 68 65 20 72 65 6e 61 6d 65 20 6c rom the rename l
5cc0: 69 73 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ist..*/.void sql
5cd0: 69 74 65 33 52 65 6e 61 6d 65 45 78 70 72 55 6e ite3RenameExprUn
5ce0: 6d 61 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 map(Parse *pPars
5cf0: 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b e, Expr *pExpr){
5d00: 0a 20 20 57 61 6c 6b 65 72 20 73 57 61 6c 6b 65 . Walker sWalke
5d10: 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 57 61 r;. memset(&sWa
5d20: 6c 6b 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 lker, 0, sizeof(
5d30: 57 61 6c 6b 65 72 29 29 3b 0a 20 20 73 57 61 6c Walker));. sWal
5d40: 6b 65 72 2e 70 50 61 72 73 65 20 3d 20 70 50 61 ker.pParse = pPa
5d50: 72 73 65 3b 0a 20 20 73 57 61 6c 6b 65 72 2e 78 rse;. sWalker.x
5d60: 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 ExprCallback = r
5d70: 65 6e 61 6d 65 55 6e 6d 61 70 45 78 70 72 43 62 enameUnmapExprCb
5d80: 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 ;. sqlite3WalkE
5d90: 78 70 72 28 26 73 57 61 6c 6b 65 72 2c 20 70 45 xpr(&sWalker, pE
5da0: 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 xpr);.}../*.** R
5db0: 65 6d 6f 76 65 20 61 6c 6c 20 6e 6f 64 65 73 20 emove all nodes
5dc0: 74 68 61 74 20 61 72 65 20 70 61 72 74 20 6f 66 that are part of
5dd0: 20 65 78 70 72 65 73 73 69 6f 6e 2d 6c 69 73 74 expression-list
5de0: 20 70 45 4c 69 73 74 20 66 72 6f 6d 20 74 68 65 pEList from the
5df0: 20 0a 2a 2a 20 72 65 6e 61 6d 65 20 6c 69 73 74 .** rename list
5e00: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
5e10: 33 52 65 6e 61 6d 65 45 78 70 72 6c 69 73 74 55 3RenameExprlistU
5e20: 6e 6d 61 70 28 50 61 72 73 65 20 2a 70 50 61 72 nmap(Parse *pPar
5e30: 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 45 se, ExprList *pE
5e40: 4c 69 73 74 29 7b 0a 20 20 69 66 28 20 70 45 4c List){. if( pEL
5e50: 69 73 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 ist ){. int i
5e60: 3b 0a 20 20 20 20 57 61 6c 6b 65 72 20 73 57 61 ;. Walker sWa
5e70: 6c 6b 65 72 3b 0a 20 20 20 20 6d 65 6d 73 65 74 lker;. memset
5e80: 28 26 73 57 61 6c 6b 65 72 2c 20 30 2c 20 73 69 (&sWalker, 0, si
5e90: 7a 65 6f 66 28 57 61 6c 6b 65 72 29 29 3b 0a 20 zeof(Walker));.
5ea0: 20 20 20 73 57 61 6c 6b 65 72 2e 70 50 61 72 73 sWalker.pPars
5eb0: 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 e = pParse;.
5ec0: 73 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c sWalker.xExprCal
5ed0: 6c 62 61 63 6b 20 3d 20 72 65 6e 61 6d 65 55 6e lback = renameUn
5ee0: 6d 61 70 45 78 70 72 43 62 3b 0a 20 20 20 20 73 mapExprCb;. s
5ef0: 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 qlite3WalkExprLi
5f00: 73 74 28 26 73 57 61 6c 6b 65 72 2c 20 70 45 4c st(&sWalker, pEL
5f10: 69 73 74 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d ist);. for(i=
5f20: 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 0; i<pEList->nEx
5f30: 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 pr; i++){.
5f40: 73 71 6c 69 74 65 33 52 65 6e 61 6d 65 54 6f 6b sqlite3RenameTok
5f50: 65 6e 52 65 6d 61 70 28 70 50 61 72 73 65 2c 20 enRemap(pParse,
5f60: 30 2c 20 28 76 6f 69 64 2a 29 70 45 4c 69 73 74 0, (void*)pEList
5f70: 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 ->a[i].zName);.
5f80: 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
5f90: 2a 20 46 72 65 65 20 74 68 65 20 6c 69 73 74 20 * Free the list
5fa0: 6f 66 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 6f of RenameToken o
5fb0: 62 6a 65 63 74 73 20 67 69 76 65 6e 20 69 6e 20 bjects given in
5fc0: 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
5fd0: 65 6e 74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ent.*/.static vo
5fe0: 69 64 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 72 id renameTokenFr
5ff0: 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ee(sqlite3 *db,
6000: 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a 70 54 6f RenameToken *pTo
6010: 6b 65 6e 29 7b 0a 20 20 52 65 6e 61 6d 65 54 6f ken){. RenameTo
6020: 6b 65 6e 20 2a 70 4e 65 78 74 3b 0a 20 20 52 65 ken *pNext;. Re
6030: 6e 61 6d 65 54 6f 6b 65 6e 20 2a 70 3b 0a 20 20 nameToken *p;.
6040: 66 6f 72 28 70 3d 70 54 6f 6b 65 6e 3b 20 70 3b for(p=pToken; p;
6050: 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 p=pNext){. p
6060: 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b Next = p->pNext;
6070: 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
6080: 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d ee(db, p);. }.}
6090: 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 74 ../*.** Search t
60a0: 68 65 20 50 61 72 73 65 20 6f 62 6a 65 63 74 20 he Parse object
60b0: 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
60c0: 72 73 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 rst argument for
60d0: 20 61 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 0a 2a a RenameToken.*
60e0: 2a 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 * object associa
60f0: 74 65 64 20 77 69 74 68 20 70 61 72 73 65 20 74 ted with parse t
6100: 72 65 65 20 65 6c 65 6d 65 6e 74 20 70 50 74 72 ree element pPtr
6110: 2e 20 49 66 20 66 6f 75 6e 64 2c 20 72 65 6d 6f . If found, remo
6120: 76 65 20 69 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 ve it.** from th
6130: 65 20 50 61 72 73 65 20 6f 62 6a 65 63 74 20 61 e Parse object a
6140: 6e 64 20 61 64 64 20 69 74 20 74 6f 20 74 68 65 nd add it to the
6150: 20 6c 69 73 74 20 6d 61 69 6e 74 61 69 6e 65 64 list maintained
6160: 20 62 79 20 74 68 65 0a 2a 2a 20 52 65 6e 61 6d by the.** Renam
6170: 65 43 74 78 20 6f 62 6a 65 63 74 20 70 61 73 73 eCtx object pass
6180: 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
6190: 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 argument..*/.st
61a0: 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 atic void rename
61b0: 54 6f 6b 65 6e 46 69 6e 64 28 50 61 72 73 65 20 TokenFind(Parse
61c0: 2a 70 50 61 72 73 65 2c 20 73 74 72 75 63 74 20 *pParse, struct
61d0: 52 65 6e 61 6d 65 43 74 78 20 2a 70 43 74 78 2c RenameCtx *pCtx,
61e0: 20 76 6f 69 64 20 2a 70 50 74 72 29 7b 0a 20 20 void *pPtr){.
61f0: 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a 2a 70 70 RenameToken **pp
6200: 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 74 72 ;. assert( pPtr
6210: 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70 70 3d !=0 );. for(pp=
6220: 26 70 50 61 72 73 65 2d 3e 70 52 65 6e 61 6d 65 &pParse->pRename
6230: 3b 20 28 2a 70 70 29 3b 20 70 70 3d 26 28 2a 70 ; (*pp); pp=&(*p
6240: 70 29 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 p)->pNext){.
6250: 69 66 28 20 28 2a 70 70 29 2d 3e 70 3d 3d 70 50 if( (*pp)->p==pP
6260: 74 72 20 29 7b 0a 20 20 20 20 20 20 52 65 6e 61 tr ){. Rena
6270: 6d 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 meToken *pToken
6280: 3d 20 2a 70 70 3b 0a 20 20 20 20 20 20 2a 70 70 = *pp;. *pp
6290: 20 3d 20 70 54 6f 6b 65 6e 2d 3e 70 4e 65 78 74 = pToken->pNext
62a0: 3b 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e 2d 3e ;. pToken->
62b0: 70 4e 65 78 74 20 3d 20 70 43 74 78 2d 3e 70 4c pNext = pCtx->pL
62c0: 69 73 74 3b 0a 20 20 20 20 20 20 70 43 74 78 2d ist;. pCtx-
62d0: 3e 70 4c 69 73 74 20 3d 20 70 54 6f 6b 65 6e 3b >pList = pToken;
62e0: 0a 20 20 20 20 20 20 70 43 74 78 2d 3e 6e 4c 69 . pCtx->nLi
62f0: 73 74 2b 2b 3b 0a 20 20 20 20 20 20 62 72 65 61 st++;. brea
6300: 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a k;. }. }.}..
6310: 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 /*.** This is a
6320: 57 61 6c 6b 65 72 20 73 65 6c 65 63 74 20 63 61 Walker select ca
6330: 6c 6c 62 61 63 6b 2e 20 49 74 20 64 6f 65 73 20 llback. It does
6340: 6e 6f 74 68 69 6e 67 2e 20 49 74 20 69 73 20 6f nothing. It is o
6350: 6e 6c 79 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 nly required.**
6360: 62 65 63 61 75 73 65 20 77 69 74 68 6f 75 74 20 because without
6370: 61 20 64 75 6d 6d 79 20 63 61 6c 6c 62 61 63 6b a dummy callback
6380: 2c 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 , sqlite3WalkExp
6390: 72 28 29 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 r() and similar
63a0: 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65 73 63 65 6e do not.** descen
63b0: 64 20 69 6e 74 6f 20 73 75 62 2d 73 65 6c 65 63 d into sub-selec
63c0: 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f t statements..*/
63d0: 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6e 61 .static int rena
63e0: 6d 65 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 43 62 meColumnSelectCb
63f0: 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
6400: 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 , Select *p){.
6410: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
6420: 28 70 57 61 6c 6b 65 72 29 3b 0a 20 20 55 4e 55 (pWalker);. UNU
6430: 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 SED_PARAMETER(p)
6440: 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 ;. return WRC_C
6450: 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ontinue;.}../*.*
6460: 2a 20 54 68 69 73 20 69 73 20 61 20 57 61 6c 6b * This is a Walk
6470: 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 er expression ca
6480: 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f llback..**.** Fo
6490: 72 20 65 76 65 72 79 20 54 4b 5f 43 4f 4c 55 4d r every TK_COLUM
64a0: 4e 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 N node in the ex
64b0: 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2c 20 73 pression tree, s
64c0: 65 61 72 63 68 20 74 6f 20 73 65 65 0a 2a 2a 20 earch to see.**
64d0: 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 62 65 if the column be
64e0: 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 69 ing references i
64f0: 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 62 65 69 s the column bei
6500: 6e 67 20 72 65 6e 61 6d 65 64 20 62 79 20 61 6e ng renamed by an
6510: 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 .** ALTER TABLE
6520: 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 69 statement. If i
6530: 74 20 69 73 2c 20 74 68 65 6e 20 61 74 74 61 63 t is, then attac
6540: 68 20 69 74 73 20 61 73 73 6f 63 69 61 74 65 64 h its associated
6550: 0a 2a 2a 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 .** RenameToken
6560: 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 6c 69 object to the li
6570: 73 74 20 6f 66 20 52 65 6e 61 6d 65 54 6f 6b 65 st of RenameToke
6580: 6e 20 6f 62 6a 65 63 74 73 20 62 65 69 6e 67 0a n objects being.
6590: 2a 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 20 69 ** constructed i
65a0: 6e 20 52 65 6e 61 6d 65 43 74 78 20 6f 62 6a 65 n RenameCtx obje
65b0: 63 74 20 61 74 20 70 57 61 6c 6b 65 72 2d 3e 75 ct at pWalker->u
65c0: 2e 70 52 65 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 .pRename..*/.sta
65d0: 74 69 63 20 69 6e 74 20 72 65 6e 61 6d 65 43 6f tic int renameCo
65e0: 6c 75 6d 6e 45 78 70 72 43 62 28 57 61 6c 6b 65 lumnExprCb(Walke
65f0: 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 r *pWalker, Expr
6600: 20 2a 70 45 78 70 72 29 7b 0a 20 20 52 65 6e 61 *pExpr){. Rena
6610: 6d 65 43 74 78 20 2a 70 20 3d 20 70 57 61 6c 6b meCtx *p = pWalk
6620: 65 72 2d 3e 75 2e 70 52 65 6e 61 6d 65 3b 0a 20 er->u.pRename;.
6630: 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d if( pExpr->op==
6640: 54 4b 5f 54 52 49 47 47 45 52 20 0a 20 20 20 26 TK_TRIGGER . &
6650: 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e & pExpr->iColumn
6660: 3d 3d 70 2d 3e 69 43 6f 6c 20 0a 20 20 20 26 26 ==p->iCol . &&
6670: 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 pWalker->pParse
6680: 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3d 3d 70 ->pTriggerTab==p
6690: 2d 3e 70 54 61 62 0a 20 20 29 7b 0a 20 20 20 20 ->pTab. ){.
66a0: 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 renameTokenFind(
66b0: 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 2c pWalker->pParse,
66c0: 20 70 2c 20 28 76 6f 69 64 2a 29 70 45 78 70 72 p, (void*)pExpr
66d0: 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 );. }else if( p
66e0: 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c Expr->op==TK_COL
66f0: 55 4d 4e 20 0a 20 20 20 26 26 20 70 45 78 70 72 UMN . && pExpr
6700: 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 2d 3e 69 43 ->iColumn==p->iC
6710: 6f 6c 20 0a 20 20 20 26 26 20 70 2d 3e 70 54 61 ol . && p->pTa
6720: 62 3d 3d 70 45 78 70 72 2d 3e 79 2e 70 54 61 62 b==pExpr->y.pTab
6730: 0a 20 20 29 7b 0a 20 20 20 20 72 65 6e 61 6d 65 . ){. rename
6740: 54 6f 6b 65 6e 46 69 6e 64 28 70 57 61 6c 6b 65 TokenFind(pWalke
6750: 72 2d 3e 70 50 61 72 73 65 2c 20 70 2c 20 28 76 r->pParse, p, (v
6760: 6f 69 64 2a 29 70 45 78 70 72 29 3b 0a 20 20 7d oid*)pExpr);. }
6770: 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f . return WRC_Co
6780: 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ntinue;.}../*.**
6790: 20 54 68 65 20 52 65 6e 61 6d 65 43 74 78 20 63 The RenameCtx c
67a0: 6f 6e 74 61 69 6e 73 20 61 20 6c 69 73 74 20 6f ontains a list o
67b0: 66 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 72 65 f tokens that re
67c0: 66 65 72 65 6e 63 65 20 61 20 63 6f 6c 75 6d 6e ference a column
67d0: 20 74 68 61 74 0a 2a 2a 20 69 73 20 62 65 69 6e that.** is bein
67e0: 67 20 72 65 6e 61 6d 65 64 20 62 79 20 61 6e 20 g renamed by an
67f0: 41 4c 54 45 52 20 54 41 42 4c 45 20 73 74 61 74 ALTER TABLE stat
6800: 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 ement. Return t
6810: 68 65 20 22 6c 61 73 74 22 0a 2a 2a 20 52 65 6e he "last".** Ren
6820: 61 6d 65 54 6f 6b 65 6e 20 69 6e 20 74 68 65 20 ameToken in the
6830: 52 65 6e 61 6d 65 43 74 78 20 61 6e 64 20 72 65 RenameCtx and re
6840: 6d 6f 76 65 20 74 68 61 74 20 52 65 6e 61 6d 65 move that Rename
6850: 54 6f 6b 65 6e 20 66 72 6f 6d 20 74 68 65 0a 2a Token from the.*
6860: 2a 20 52 65 6e 61 6d 65 43 6f 6e 74 65 78 74 2e * RenameContext.
6870: 20 20 22 4c 61 73 74 22 20 6d 65 61 6e 73 20 74 "Last" means t
6880: 68 65 20 6c 61 73 74 20 52 65 6e 61 6d 65 54 6f he last RenameTo
6890: 6b 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 ken encountered
68a0: 77 68 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 70 75 when.** the inpu
68b0: 74 20 53 51 4c 20 69 73 20 70 61 72 73 65 64 20 t SQL is parsed
68c0: 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 from left to rig
68d0: 68 74 2e 20 20 52 65 70 65 61 74 65 64 20 63 61 ht. Repeated ca
68e0: 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 lls to this rout
68f0: 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61 6c ine.** return al
6900: 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 74 6f l column name to
6910: 6b 65 6e 73 20 69 6e 20 74 68 65 20 6f 72 64 65 kens in the orde
6920: 72 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20 r that they are
6930: 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2a 20 69 encountered.** i
6940: 6e 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d n the SQL statem
6950: 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 ent..*/.static R
6960: 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a 72 65 6e 61 enameToken *rena
6970: 6d 65 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 4e 65 78 meColumnTokenNex
6980: 74 28 52 65 6e 61 6d 65 43 74 78 20 2a 70 43 74 t(RenameCtx *pCt
6990: 78 29 7b 0a 20 20 52 65 6e 61 6d 65 54 6f 6b 65 x){. RenameToke
69a0: 6e 20 2a 70 42 65 73 74 20 3d 20 70 43 74 78 2d n *pBest = pCtx-
69b0: 3e 70 4c 69 73 74 3b 0a 20 20 52 65 6e 61 6d 65 >pList;. Rename
69c0: 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 0a 20 Token *pToken;.
69d0: 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 2a 2a 70 RenameToken **p
69e0: 70 3b 0a 0a 20 20 66 6f 72 28 70 54 6f 6b 65 6e p;.. for(pToken
69f0: 3d 70 42 65 73 74 2d 3e 70 4e 65 78 74 3b 20 70 =pBest->pNext; p
6a00: 54 6f 6b 65 6e 3b 20 70 54 6f 6b 65 6e 3d 70 54 Token; pToken=pT
6a10: 6f 6b 65 6e 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 oken->pNext){.
6a20: 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 74 2e if( pToken->t.
6a30: 7a 3e 70 42 65 73 74 2d 3e 74 2e 7a 20 29 20 70 z>pBest->t.z ) p
6a40: 42 65 73 74 20 3d 20 70 54 6f 6b 65 6e 3b 0a 20 Best = pToken;.
6a50: 20 7d 0a 20 20 66 6f 72 28 70 70 3d 26 70 43 74 }. for(pp=&pCt
6a60: 78 2d 3e 70 4c 69 73 74 3b 20 2a 70 70 21 3d 70 x->pList; *pp!=p
6a70: 42 65 73 74 3b 20 70 70 3d 26 28 2a 70 70 29 2d Best; pp=&(*pp)-
6a80: 3e 70 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d >pNext);. *pp =
6a90: 20 70 42 65 73 74 2d 3e 70 4e 65 78 74 3b 0a 0a pBest->pNext;..
6aa0: 20 20 72 65 74 75 72 6e 20 70 42 65 73 74 3b 0a return pBest;.
6ab0: 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f }../*.** An erro
6ac0: 72 20 6f 63 63 75 72 65 64 20 77 68 69 6c 65 20 r occured while
6ad0: 70 61 72 73 69 6e 67 20 6f 72 20 6f 74 68 65 72 parsing or other
6ae0: 77 69 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 wise processing
6af0: 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 6f 62 a database.** ob
6b00: 6a 65 63 74 20 28 65 69 74 68 65 72 20 70 50 61 ject (either pPa
6b10: 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20 rse->pNewTable,
6b20: 70 4e 65 77 49 6e 64 65 78 20 6f 72 20 70 4e 65 pNewIndex or pNe
6b30: 77 54 72 69 67 67 65 72 29 20 61 73 20 70 61 72 wTrigger) as par
6b40: 74 20 6f 66 20 61 6e 0a 2a 2a 20 41 4c 54 45 52 t of an.** ALTER
6b50: 20 54 41 42 4c 45 20 52 45 4e 41 4d 45 20 43 4f TABLE RENAME CO
6b60: 4c 55 4d 4e 20 70 72 6f 67 72 61 6d 2e 20 54 68 LUMN program. Th
6b70: 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
6b80: 65 6d 69 74 74 65 64 20 62 79 20 74 68 65 0a 2a emitted by the.*
6b90: 2a 20 73 75 62 2d 72 6f 75 74 69 6e 65 20 69 73 * sub-routine is
6ba0: 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 currently store
6bb0: 64 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 d in pParse->zEr
6bc0: 72 4d 73 67 2e 20 54 68 69 73 20 66 75 6e 63 74 rMsg. This funct
6bd0: 69 6f 6e 0a 2a 2a 20 61 64 64 73 20 63 6f 6e 74 ion.** adds cont
6be0: 65 78 74 20 74 6f 20 74 68 65 20 65 72 72 6f 72 ext to the error
6bf0: 20 6d 65 73 73 61 67 65 20 61 6e 64 20 74 68 65 message and the
6c00: 6e 20 73 74 6f 72 65 73 20 69 74 20 69 6e 20 70 n stores it in p
6c10: 43 74 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 Ctx..*/.static v
6c20: 6f 69 64 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e oid renameColumn
6c30: 50 61 72 73 65 45 72 72 6f 72 28 0a 20 20 73 71 ParseError(. sq
6c40: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
6c50: 43 74 78 2c 20 0a 20 20 69 6e 74 20 62 50 6f 73 Ctx, . int bPos
6c60: 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c t,. sqlite3_val
6c70: 75 65 20 2a 70 54 79 70 65 2c 0a 20 20 73 71 6c ue *pType,. sql
6c80: 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 4f 62 6a ite3_value *pObj
6c90: 65 63 74 2c 0a 20 20 50 61 72 73 65 20 2a 70 50 ect,. Parse *pP
6ca0: 61 72 73 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 arse.){. const
6cb0: 63 68 61 72 20 2a 7a 54 20 3d 20 28 63 6f 6e 73 char *zT = (cons
6cc0: 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f t char*)sqlite3_
6cd0: 76 61 6c 75 65 5f 74 65 78 74 28 70 54 79 70 65 value_text(pType
6ce0: 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 );. const char
6cf0: 2a 7a 4e 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 *zN = (const cha
6d00: 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r*)sqlite3_value
6d10: 5f 74 65 78 74 28 70 4f 62 6a 65 63 74 29 3b 0a _text(pObject);.
6d20: 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 0a 20 char *zErr;..
6d30: 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f zErr = sqlite3_
6d40: 6d 70 72 69 6e 74 66 28 22 65 72 72 6f 72 20 69 mprintf("error i
6d50: 6e 20 25 73 20 25 73 25 73 3a 20 25 73 22 2c 20 n %s %s%s: %s",
6d60: 0a 20 20 20 20 20 20 7a 54 2c 20 7a 4e 2c 20 28 . zT, zN, (
6d70: 62 50 6f 73 74 20 3f 20 22 20 61 66 74 65 72 20 bPost ? " after
6d80: 72 65 6e 61 6d 65 22 20 3a 20 22 22 29 2c 0a 20 rename" : ""),.
6d90: 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 pParse->zEr
6da0: 72 4d 73 67 0a 20 20 29 3b 0a 20 20 73 71 6c 69 rMsg. );. sqli
6db0: 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
6dc0: 28 70 43 74 78 2c 20 7a 45 72 72 2c 20 2d 31 29 (pCtx, zErr, -1)
6dd0: 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
6de0: 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a (zErr);.}../*.**
6df0: 20 46 6f 72 20 65 61 63 68 20 6e 61 6d 65 20 69 For each name i
6e00: 6e 20 74 68 65 20 74 68 65 20 65 78 70 72 65 73 n the the expres
6e10: 73 69 6f 6e 2d 6c 69 73 74 20 70 45 4c 69 73 74 sion-list pEList
6e20: 20 28 69 2e 65 2e 20 65 61 63 68 0a 2a 2a 20 70 (i.e. each.** p
6e30: 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d EList->a[i].zNam
6e40: 65 29 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 e) that matches
6e50: 74 68 65 20 73 74 72 69 6e 67 20 69 6e 20 7a 4f the string in zO
6e60: 6c 64 2c 20 65 78 74 72 61 63 74 20 74 68 65 20 ld, extract the
6e70: 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e .** correspondin
6e80: 67 20 72 65 6e 61 6d 65 2d 74 6f 6b 65 6e 20 66 g rename-token f
6e90: 72 6f 6d 20 50 61 72 73 65 20 6f 62 6a 65 63 74 rom Parse object
6ea0: 20 70 50 61 72 73 65 20 61 6e 64 20 61 64 64 20 pParse and add
6eb0: 69 74 0a 2a 2a 20 74 6f 20 74 68 65 20 52 65 6e it.** to the Ren
6ec0: 61 6d 65 43 74 78 20 70 43 74 78 2e 0a 2a 2f 0a ameCtx pCtx..*/.
6ed0: 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 static void rena
6ee0: 6d 65 43 6f 6c 75 6d 6e 45 6c 69 73 74 4e 61 6d meColumnElistNam
6ef0: 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 es(. Parse *pPa
6f00: 72 73 65 2c 20 0a 20 20 52 65 6e 61 6d 65 43 74 rse, . RenameCt
6f10: 78 20 2a 70 43 74 78 2c 20 0a 20 20 45 78 70 72 x *pCtx, . Expr
6f20: 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 0a 20 List *pEList, .
6f30: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 6c const char *zOl
6f40: 64 0a 29 7b 0a 20 20 69 66 28 20 70 45 4c 69 73 d.){. if( pELis
6f50: 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a t ){. int i;.
6f60: 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
6f70: 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b EList->nExpr; i+
6f80: 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a +){. char *
6f90: 7a 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e zName = pEList->
6fa0: 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 a[i].zName;.
6fb0: 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 if( 0==sqlite3
6fc0: 5f 73 74 72 69 63 6d 70 28 7a 4e 61 6d 65 2c 20 _stricmp(zName,
6fd0: 7a 4f 6c 64 29 20 29 7b 0a 20 20 20 20 20 20 20 zOld) ){.
6fe0: 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 renameTokenFind
6ff0: 28 70 50 61 72 73 65 2c 20 70 43 74 78 2c 20 28 (pParse, pCtx, (
7000: 76 6f 69 64 2a 29 7a 4e 61 6d 65 29 3b 0a 20 20 void*)zName);.
7010: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
7020: 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 }../*.** For eac
7030: 68 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 74 68 h name in the th
7040: 65 20 69 64 2d 6c 69 73 74 20 70 49 64 4c 69 73 e id-list pIdLis
7050: 74 20 28 69 2e 65 2e 20 65 61 63 68 20 70 49 64 t (i.e. each pId
7060: 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 List->a[i].zName
7070: 29 20 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 ) .** that match
7080: 65 73 20 74 68 65 20 73 74 72 69 6e 67 20 69 6e es the string in
7090: 20 7a 4f 6c 64 2c 20 65 78 74 72 61 63 74 20 74 zOld, extract t
70a0: 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
70b0: 20 72 65 6e 61 6d 65 2d 74 6f 6b 65 6e 20 0a 2a rename-token .*
70c0: 2a 20 66 72 6f 6d 20 50 61 72 73 65 20 6f 62 6a * from Parse obj
70d0: 65 63 74 20 70 50 61 72 73 65 20 61 6e 64 20 61 ect pParse and a
70e0: 64 64 20 69 74 20 74 6f 20 74 68 65 20 52 65 6e dd it to the Ren
70f0: 61 6d 65 43 74 78 20 70 43 74 78 2e 0a 2a 2f 0a ameCtx pCtx..*/.
7100: 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 static void rena
7110: 6d 65 43 6f 6c 75 6d 6e 49 64 6c 69 73 74 4e 61 meColumnIdlistNa
7120: 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 mes(. Parse *pP
7130: 61 72 73 65 2c 20 0a 20 20 52 65 6e 61 6d 65 43 arse, . RenameC
7140: 74 78 20 2a 70 43 74 78 2c 20 0a 20 20 49 64 4c tx *pCtx, . IdL
7150: 69 73 74 20 2a 70 49 64 4c 69 73 74 2c 20 0a 20 ist *pIdList, .
7160: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 6c const char *zOl
7170: 64 0a 29 7b 0a 20 20 69 66 28 20 70 49 64 4c 69 d.){. if( pIdLi
7180: 73 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b st ){. int i;
7190: 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
71a0: 70 49 64 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b pIdList->nId; i+
71b0: 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a +){. char *
71c0: 7a 4e 61 6d 65 20 3d 20 70 49 64 4c 69 73 74 2d zName = pIdList-
71d0: 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 >a[i].zName;.
71e0: 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 if( 0==sqlite
71f0: 33 5f 73 74 72 69 63 6d 70 28 7a 4e 61 6d 65 2c 3_stricmp(zName,
7200: 20 7a 4f 6c 64 29 20 29 7b 0a 20 20 20 20 20 20 zOld) ){.
7210: 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e renameTokenFin
7220: 64 28 70 50 61 72 73 65 2c 20 70 43 74 78 2c 20 d(pParse, pCtx,
7230: 28 76 6f 69 64 2a 29 7a 4e 61 6d 65 29 3b 0a 20 (void*)zName);.
7240: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
7250: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 .}../*.** Parse
7260: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
7270: 74 20 7a 53 71 6c 20 75 73 69 6e 67 20 50 61 72 t zSql using Par
7280: 73 65 20 6f 62 6a 65 63 74 20 28 2a 70 29 2e 20 se object (*p).
7290: 54 68 65 20 50 61 72 73 65 20 6f 62 6a 65 63 74 The Parse object
72a0: 0a 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a .** is initializ
72b0: 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 ed by this funct
72c0: 69 6f 6e 20 62 65 66 6f 72 65 20 69 74 20 69 73 ion before it is
72d0: 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 used..*/.static
72e0: 20 69 6e 74 20 72 65 6e 61 6d 65 50 61 72 73 65 int renameParse
72f0: 53 71 6c 28 0a 20 20 50 61 72 73 65 20 2a 70 2c Sql(. Parse *p,
7300: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7310: 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
7320: 20 74 6f 20 75 73 65 20 66 6f 72 20 50 61 72 73 to use for Pars
7330: 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f e object */. co
7340: 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 nst char *zDb,
7350: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7360: 20 4e 61 6d 65 20 6f 66 20 73 63 68 65 6d 61 20 Name of schema
7370: 53 51 4c 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a SQL belongs to *
7380: 2f 0a 20 20 69 6e 74 20 62 54 61 62 6c 65 2c 20 /. int bTable,
7390: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73a0: 20 20 20 20 2f 2a 20 31 20 2d 3e 20 52 45 4e 41 /* 1 -> RENA
73b0: 4d 45 20 54 41 42 4c 45 2c 20 30 20 2d 3e 20 52 ME TABLE, 0 -> R
73c0: 45 4e 41 4d 45 20 43 4f 4c 55 4d 4e 20 2a 2f 0a ENAME COLUMN */.
73d0: 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
73e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73f0: 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 /* Database ha
7400: 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
7410: 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 char *zSql,
7420: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c /* SQL
7430: 20 74 6f 20 70 61 72 73 65 20 2a 2f 0a 20 20 69 to parse */. i
7440: 6e 74 20 62 54 65 6d 70 20 20 20 20 20 20 20 20 nt bTemp
7450: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
7460: 2a 20 54 72 75 65 20 69 66 20 53 51 4c 20 69 73 * True if SQL is
7470: 20 66 72 6f 6d 20 74 65 6d 70 20 73 63 68 65 6d from temp schem
7480: 61 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 a */.){. int rc
7490: 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d ;. char *zErr =
74a0: 20 30 3b 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 0;.. db->init.
74b0: 69 44 62 20 3d 20 62 54 65 6d 70 20 3f 20 31 20 iDb = bTemp ? 1
74c0: 3a 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e : sqlite3FindDbN
74d0: 61 6d 65 28 64 62 2c 20 7a 44 62 29 3b 0a 0a 20 ame(db, zDb);..
74e0: 20 2f 2a 20 50 61 72 73 65 20 74 68 65 20 53 51 /* Parse the SQ
74f0: 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 L statement pass
7500: 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
7510: 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 6e 6f 20 argument. If no
7520: 65 72 72 6f 72 0a 20 20 2a 2a 20 6f 63 63 75 72 error. ** occur
7530: 73 20 61 6e 64 20 74 68 65 20 70 61 72 73 65 20 s and the parse
7540: 64 6f 65 73 20 6e 6f 74 20 72 65 73 75 6c 74 20 does not result
7550: 69 6e 20 61 20 6e 65 77 20 74 61 62 6c 65 2c 20 in a new table,
7560: 69 6e 64 65 78 20 6f 72 0a 20 20 2a 2a 20 74 72 index or. ** tr
7570: 69 67 67 65 72 20 6f 62 6a 65 63 74 2c 20 74 68 igger object, th
7580: 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 e database must
7590: 62 65 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 be corrupt. */.
75a0: 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 memset(p, 0, si
75b0: 7a 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20 zeof(Parse));.
75c0: 70 2d 3e 65 50 61 72 73 65 4d 6f 64 65 20 3d 20 p->eParseMode =
75d0: 28 62 54 61 62 6c 65 20 3f 20 50 41 52 53 45 5f (bTable ? PARSE_
75e0: 4d 4f 44 45 5f 52 45 4e 41 4d 45 5f 54 41 42 4c MODE_RENAME_TABL
75f0: 45 20 3a 20 50 41 52 53 45 5f 4d 4f 44 45 5f 52 E : PARSE_MODE_R
7600: 45 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 20 ENAME_COLUMN);.
7610: 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 p->db = db;. p
7620: 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70 20 3d 20 31 ->nQueryLoop = 1
7630: 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
7640: 52 75 6e 50 61 72 73 65 72 28 70 2c 20 7a 53 71 RunParser(p, zSq
7650: 6c 2c 20 26 7a 45 72 72 29 3b 0a 20 20 61 73 73 l, &zErr);. ass
7660: 65 72 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d ert( p->zErrMsg=
7670: 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
7680: 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
7690: 20 7a 45 72 72 3d 3d 30 20 29 3b 0a 20 20 61 73 zErr==0 );. as
76a0: 73 65 72 74 28 20 28 30 21 3d 70 2d 3e 70 4e 65 sert( (0!=p->pNe
76b0: 77 54 61 62 6c 65 29 20 2b 20 28 30 21 3d 70 2d wTable) + (0!=p-
76c0: 3e 70 4e 65 77 49 6e 64 65 78 29 20 2b 20 28 30 >pNewIndex) + (0
76d0: 21 3d 70 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 !=p->pNewTrigger
76e0: 29 3c 32 20 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 )<2 );. p->zErr
76f0: 4d 73 67 20 3d 20 7a 45 72 72 3b 0a 20 20 69 66 Msg = zErr;. if
7700: 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
7710: 65 64 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 ed ) rc = SQLITE
7720: 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63 _NOMEM;. if( rc
7730: 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 0a 20 20 20 ==SQLITE_OK .
7740: 26 26 20 70 2d 3e 70 4e 65 77 54 61 62 6c 65 3d && p->pNewTable=
7750: 3d 30 20 26 26 20 70 2d 3e 70 4e 65 77 49 6e 64 =0 && p->pNewInd
7760: 65 78 3d 3d 30 20 26 26 20 70 2d 3e 70 4e 65 77 ex==0 && p->pNew
7770: 54 72 69 67 67 65 72 3d 3d 30 20 0a 20 20 29 7b Trigger==0 . ){
7780: 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
7790: 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
77a0: 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }..#ifdef SQLIT
77b0: 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 45 6e 73 E_DEBUG. /* Ens
77c0: 75 72 65 20 74 68 61 74 20 61 6c 6c 20 6d 61 70 ure that all map
77d0: 70 69 6e 67 73 20 69 6e 20 74 68 65 20 50 61 72 pings in the Par
77e0: 73 65 2e 70 52 65 6e 61 6d 65 20 6c 69 73 74 20 se.pRename list
77f0: 72 65 61 6c 6c 79 20 64 6f 20 6d 61 70 20 74 6f really do map to
7800: 0a 20 20 2a 2a 20 61 20 70 61 72 74 20 6f 66 20 . ** a part of
7810: 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 the input string
7820: 2e 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d . */. if( rc==
7830: 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
7840: 20 69 6e 74 20 6e 53 71 6c 20 3d 20 73 71 6c 69 int nSql = sqli
7850: 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 53 71 6c te3Strlen30(zSql
7860: 29 3b 0a 20 20 20 20 52 65 6e 61 6d 65 54 6f 6b );. RenameTok
7870: 65 6e 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 en *pToken;.
7880: 66 6f 72 28 70 54 6f 6b 65 6e 3d 70 2d 3e 70 52 for(pToken=p->pR
7890: 65 6e 61 6d 65 3b 20 70 54 6f 6b 65 6e 3b 20 70 ename; pToken; p
78a0: 54 6f 6b 65 6e 3d 70 54 6f 6b 65 6e 2d 3e 70 4e Token=pToken->pN
78b0: 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ext){. asse
78c0: 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 74 2e 7a 3e rt( pToken->t.z>
78d0: 3d 7a 53 71 6c 20 26 26 20 26 70 54 6f 6b 65 6e =zSql && &pToken
78e0: 2d 3e 74 2e 7a 5b 70 54 6f 6b 65 6e 2d 3e 74 2e ->t.z[pToken->t.
78f0: 6e 5d 3c 3d 26 7a 53 71 6c 5b 6e 53 71 6c 5d 20 n]<=&zSql[nSql]
7900: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e );. }. }.#en
7910: 64 69 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e dif.. db->init.
7920: 69 44 62 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 iDb = 0;. retur
7930: 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
7940: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 64 69 his function edi
7950: 74 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ts SQL statement
7960: 20 7a 53 71 6c 2c 20 72 65 70 6c 61 63 69 6e 67 zSql, replacing
7970: 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 64 65 6e each token iden
7980: 74 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 tified.** by the
7990: 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 70 52 65 linked list pRe
79a0: 6e 61 6d 65 20 77 69 74 68 20 74 68 65 20 74 65 name with the te
79b0: 78 74 20 6f 66 20 7a 4e 65 77 2e 20 49 66 20 61 xt of zNew. If a
79c0: 72 67 75 6d 65 6e 74 20 62 51 75 6f 74 65 20 69 rgument bQuote i
79d0: 73 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 6e 20 s.** true, then
79e0: 7a 4e 65 77 20 69 73 20 61 6c 77 61 79 73 20 71 zNew is always q
79f0: 75 6f 74 65 64 20 66 69 72 73 74 2e 20 49 66 20 uoted first. If
7a00: 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c no error occurs,
7a10: 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 the result.** i
7a20: 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 63 6f s loaded into co
7a30: 6e 74 65 78 74 20 6f 62 6a 65 63 74 20 70 43 74 ntext object pCt
7a40: 78 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 2e x as the result.
7a50: 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 66 20 61 6e .**.** Or, if an
7a60: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 28 69 error occurs (i
7a70: 2e 65 2e 20 61 6e 20 4f 4f 4d 20 63 6f 6e 64 69 .e. an OOM condi
7a80: 74 69 6f 6e 29 2c 20 61 6e 20 65 72 72 6f 72 20 tion), an error
7a90: 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a 20 70 43 is left in.** pC
7aa0: 74 78 20 61 6e 64 20 61 6e 20 53 51 4c 69 74 65 tx and an SQLite
7ab0: 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 error code retu
7ac0: 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rned..*/.static
7ad0: 69 6e 74 20 72 65 6e 61 6d 65 45 64 69 74 53 71 int renameEditSq
7ae0: 6c 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e l(. sqlite3_con
7af0: 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20 20 20 text *pCtx,
7b00: 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 72 /* Return r
7b10: 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 esult here */.
7b20: 52 65 6e 61 6d 65 43 74 78 20 2a 70 52 65 6e 61 RenameCtx *pRena
7b30: 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 me,
7b40: 2f 2a 20 52 65 6e 61 6d 65 20 63 6f 6e 74 65 78 /* Rename contex
7b50: 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 t */. const cha
7b60: 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 r *zSql,
7b70: 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 /* SQL st
7b80: 61 74 65 6d 65 6e 74 20 74 6f 20 65 64 69 74 20 atement to edit
7b90: 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
7ba0: 2a 7a 4e 65 77 2c 20 20 20 20 20 20 20 20 20 20 *zNew,
7bb0: 20 20 20 20 20 2f 2a 20 4e 65 77 20 74 6f 6b 65 /* New toke
7bc0: 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 n text */. int
7bd0: 62 51 75 6f 74 65 20 20 20 20 20 20 20 20 20 20 bQuote
7be0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
7bf0: 72 75 65 20 74 6f 20 61 6c 77 61 79 73 20 71 75 rue to always qu
7c00: 6f 74 65 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a ote token */.){.
7c10: 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 73 71 6c int nNew = sql
7c20: 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 65 ite3Strlen30(zNe
7c30: 77 29 3b 0a 20 20 69 6e 74 20 6e 53 71 6c 20 3d w);. int nSql =
7c40: 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
7c50: 28 7a 53 71 6c 29 3b 0a 20 20 73 71 6c 69 74 65 (zSql);. sqlite
7c60: 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 3 *db = sqlite3_
7c70: 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
7c80: 65 28 70 43 74 78 29 3b 0a 20 20 69 6e 74 20 72 e(pCtx);. int r
7c90: 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
7ca0: 20 63 68 61 72 20 2a 7a 51 75 6f 74 3b 0a 20 20 char *zQuot;.
7cb0: 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 69 6e char *zOut;. in
7cc0: 74 20 6e 51 75 6f 74 3b 0a 0a 20 20 2f 2a 20 53 t nQuot;.. /* S
7cd0: 65 74 20 7a 51 75 6f 74 20 74 6f 20 70 6f 69 6e et zQuot to poin
7ce0: 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f t to a buffer co
7cf0: 6e 74 61 69 6e 69 6e 67 20 61 20 71 75 6f 74 65 ntaining a quote
7d00: 64 20 63 6f 70 79 20 6f 66 20 74 68 65 20 0a 20 d copy of the .
7d10: 20 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 20 7a ** identifier z
7d20: 4e 65 77 2e 20 49 66 20 74 68 65 20 63 6f 72 72 New. If the corr
7d30: 65 73 70 6f 6e 64 69 6e 67 20 69 64 65 6e 74 69 esponding identi
7d40: 66 69 65 72 20 69 6e 20 74 68 65 20 6f 72 69 67 fier in the orig
7d50: 69 6e 61 6c 20 0a 20 20 2a 2a 20 41 4c 54 45 52 inal . ** ALTER
7d60: 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
7d70: 20 77 61 73 20 71 75 6f 74 65 64 20 28 62 51 75 was quoted (bQu
7d80: 6f 74 65 3d 3d 31 29 2c 20 74 68 65 6e 20 73 65 ote==1), then se
7d90: 74 20 7a 4e 65 77 20 74 6f 0a 20 20 2a 2a 20 70 t zNew to. ** p
7da0: 6f 69 6e 74 20 74 6f 20 7a 51 75 6f 74 20 73 6f oint to zQuot so
7db0: 20 74 68 61 74 20 61 6c 6c 20 73 75 62 73 74 69 that all substi
7dc0: 74 75 74 69 6f 6e 73 20 61 72 65 20 6d 61 64 65 tutions are made
7dd0: 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 using the. **
7de0: 71 75 6f 74 65 64 20 76 65 72 73 69 6f 6e 20 6f quoted version o
7df0: 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e f the new column
7e00: 20 6e 61 6d 65 2e 20 20 2a 2f 0a 20 20 7a 51 75 name. */. zQu
7e10: 6f 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 ot = sqlite3MPri
7e20: 6e 74 66 28 64 62 2c 20 22 5c 22 25 77 5c 22 22 ntf(db, "\"%w\""
7e30: 2c 20 7a 4e 65 77 29 3b 0a 20 20 69 66 28 20 7a , zNew);. if( z
7e40: 51 75 6f 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 Quot==0 ){. r
7e50: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
7e60: 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 EM;. }else{.
7e70: 20 6e 51 75 6f 74 20 3d 20 73 71 6c 69 74 65 33 nQuot = sqlite3
7e80: 53 74 72 6c 65 6e 33 30 28 7a 51 75 6f 74 29 3b Strlen30(zQuot);
7e90: 0a 20 20 7d 0a 20 20 69 66 28 20 62 51 75 6f 74 . }. if( bQuot
7ea0: 65 20 29 7b 0a 20 20 20 20 7a 4e 65 77 20 3d 20 e ){. zNew =
7eb0: 7a 51 75 6f 74 3b 0a 20 20 20 20 6e 4e 65 77 20 zQuot;. nNew
7ec0: 3d 20 6e 51 75 6f 74 3b 0a 20 20 7d 0a 0a 20 20 = nQuot;. }..
7ed0: 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 /* At this point
7ee0: 20 70 52 65 6e 61 6d 65 2d 3e 70 4c 69 73 74 20 pRename->pList
7ef0: 63 6f 6e 74 61 69 6e 73 20 61 20 6c 69 73 74 20 contains a list
7f00: 6f 66 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 6f of RenameToken o
7f10: 62 6a 65 63 74 73 0a 20 20 2a 2a 20 63 6f 72 72 bjects. ** corr
7f20: 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 61 6c 6c esponding to all
7f30: 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 69 tokens in the i
7f40: 6e 70 75 74 20 53 51 4c 20 74 68 61 74 20 6d 75 nput SQL that mu
7f50: 73 74 20 62 65 20 72 65 70 6c 61 63 65 64 0a 20 st be replaced.
7f60: 20 2a 2a 20 77 69 74 68 20 74 68 65 20 6e 65 77 ** with the new
7f70: 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 20 41 6c column name. Al
7f80: 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 69 l that remains i
7f90: 73 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 61 s to construct a
7fa0: 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 nd. ** return t
7fb0: 68 65 20 65 64 69 74 65 64 20 53 51 4c 20 73 74 he edited SQL st
7fc0: 72 69 6e 67 2e 20 2a 2f 0a 20 20 61 73 73 65 72 ring. */. asser
7fd0: 74 28 20 6e 51 75 6f 74 3e 3d 6e 4e 65 77 20 29 t( nQuot>=nNew )
7fe0: 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 ;. zOut = sqlit
7ff0: 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 e3DbMallocZero(d
8000: 62 2c 20 6e 53 71 6c 20 2b 20 70 52 65 6e 61 6d b, nSql + pRenam
8010: 65 2d 3e 6e 4c 69 73 74 2a 6e 51 75 6f 74 20 2b e->nList*nQuot +
8020: 20 31 29 3b 0a 20 20 69 66 28 20 7a 4f 75 74 20 1);. if( zOut
8030: 29 7b 0a 20 20 20 20 69 6e 74 20 6e 4f 75 74 20 ){. int nOut
8040: 3d 20 6e 53 71 6c 3b 0a 20 20 20 20 6d 65 6d 63 = nSql;. memc
8050: 70 79 28 7a 4f 75 74 2c 20 7a 53 71 6c 2c 20 6e py(zOut, zSql, n
8060: 53 71 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 Sql);. while(
8070: 20 70 52 65 6e 61 6d 65 2d 3e 70 4c 69 73 74 20 pRename->pList
8080: 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4f 66 ){. int iOf
8090: 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
80a0: 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f /* Offset o
80b0: 66 20 74 6f 6b 65 6e 20 74 6f 20 72 65 70 6c 61 f token to repla
80c0: 63 65 20 69 6e 20 7a 4f 75 74 20 2a 2f 0a 20 20 ce in zOut */.
80d0: 20 20 20 20 52 65 6e 61 6d 65 54 6f 6b 65 6e 20 RenameToken
80e0: 2a 70 42 65 73 74 20 3d 20 72 65 6e 61 6d 65 43 *pBest = renameC
80f0: 6f 6c 75 6d 6e 54 6f 6b 65 6e 4e 65 78 74 28 70 olumnTokenNext(p
8100: 52 65 6e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 Rename);..
8110: 75 33 32 20 6e 52 65 70 6c 61 63 65 3b 0a 20 20 u32 nReplace;.
8120: 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
8130: 7a 52 65 70 6c 61 63 65 3b 0a 20 20 20 20 20 20 zReplace;.
8140: 69 66 28 20 73 71 6c 69 74 65 33 49 73 49 64 43 if( sqlite3IsIdC
8150: 68 61 72 28 2a 70 42 65 73 74 2d 3e 74 2e 7a 29 har(*pBest->t.z)
8160: 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 52 65 70 ){. nRep
8170: 6c 61 63 65 20 3d 20 6e 4e 65 77 3b 0a 20 20 20 lace = nNew;.
8180: 20 20 20 20 20 7a 52 65 70 6c 61 63 65 20 3d 20 zReplace =
8190: 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 zNew;. }els
81a0: 65 7b 0a 20 20 20 20 20 20 20 20 6e 52 65 70 6c e{. nRepl
81b0: 61 63 65 20 3d 20 6e 51 75 6f 74 3b 0a 20 20 20 ace = nQuot;.
81c0: 20 20 20 20 20 7a 52 65 70 6c 61 63 65 20 3d 20 zReplace =
81d0: 7a 51 75 6f 74 3b 0a 20 20 20 20 20 20 7d 0a 0a zQuot;. }..
81e0: 20 20 20 20 20 20 69 4f 66 66 20 3d 20 70 42 65 iOff = pBe
81f0: 73 74 2d 3e 74 2e 7a 20 2d 20 7a 53 71 6c 3b 0a st->t.z - zSql;.
8200: 20 20 20 20 20 20 69 66 28 20 70 42 65 73 74 2d if( pBest-
8210: 3e 74 2e 6e 21 3d 6e 52 65 70 6c 61 63 65 20 29 >t.n!=nReplace )
8220: 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 6d 6f 76 {. memmov
8230: 65 28 26 7a 4f 75 74 5b 69 4f 66 66 20 2b 20 6e e(&zOut[iOff + n
8240: 52 65 70 6c 61 63 65 5d 2c 20 26 7a 4f 75 74 5b Replace], &zOut[
8250: 69 4f 66 66 20 2b 20 70 42 65 73 74 2d 3e 74 2e iOff + pBest->t.
8260: 6e 5d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 n], .
8270: 20 6e 4f 75 74 20 2d 20 28 69 4f 66 66 20 2b 20 nOut - (iOff +
8280: 70 42 65 73 74 2d 3e 74 2e 6e 29 0a 20 20 20 20 pBest->t.n).
8290: 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 6e );. n
82a0: 4f 75 74 20 2b 3d 20 6e 52 65 70 6c 61 63 65 20 Out += nReplace
82b0: 2d 20 70 42 65 73 74 2d 3e 74 2e 6e 3b 0a 20 20 - pBest->t.n;.
82c0: 20 20 20 20 20 20 7a 4f 75 74 5b 6e 4f 75 74 5d zOut[nOut]
82d0: 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 7d = '\0';. }
82e0: 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a . memcpy(&z
82f0: 4f 75 74 5b 69 4f 66 66 5d 2c 20 7a 52 65 70 6c Out[iOff], zRepl
8300: 61 63 65 2c 20 6e 52 65 70 6c 61 63 65 29 3b 0a ace, nReplace);.
8310: 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
8320: 72 65 65 28 64 62 2c 20 70 42 65 73 74 29 3b 0a ree(db, pBest);.
8330: 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 }.. sqlit
8340: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 e3_result_text(p
8350: 43 74 78 2c 20 7a 4f 75 74 2c 20 2d 31 2c 20 53 Ctx, zOut, -1, S
8360: 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
8370: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
8380: 72 65 65 28 64 62 2c 20 7a 4f 75 74 29 3b 0a 20 ree(db, zOut);.
8390: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
83a0: 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
83b0: 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 }.. sqlite3_fr
83c0: 65 65 28 7a 51 75 6f 74 29 3b 0a 20 20 72 65 74 ee(zQuot);. ret
83d0: 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
83e0: 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 73 79 6d Resolve all sym
83f0: 62 6f 6c 73 20 69 6e 20 74 68 65 20 74 72 69 67 bols in the trig
8400: 67 65 72 20 61 74 20 70 50 61 72 73 65 2d 3e 70 ger at pParse->p
8410: 4e 65 77 54 72 69 67 67 65 72 2c 20 61 73 73 75 NewTrigger, assu
8420: 6d 69 6e 67 0a 2a 2a 20 69 74 20 77 61 73 20 72 ming.** it was r
8430: 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73 63 68 ead from the sch
8440: 65 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 ema of database
8450: 7a 44 62 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 zDb. Return SQLI
8460: 54 45 5f 4f 4b 20 69 66 20 0a 2a 2a 20 73 75 63 TE_OK if .** suc
8470: 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 72 77 69 cessful. Otherwi
8480: 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 53 51 se, return an SQ
8490: 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 Lite error code
84a0: 61 6e 64 20 6c 65 61 76 65 20 61 6e 20 65 72 72 and leave an err
84b0: 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69 6e or.** message in
84c0: 20 74 68 65 20 50 61 72 73 65 20 6f 62 6a 65 63 the Parse objec
84d0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
84e0: 20 72 65 6e 61 6d 65 52 65 73 6f 6c 76 65 54 72 renameResolveTr
84f0: 69 67 67 65 72 28 50 61 72 73 65 20 2a 70 50 61 igger(Parse *pPa
8500: 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 rse, const char
8510: 2a 7a 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 *zDb){. sqlite3
8520: 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
8530: 62 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e b;. Trigger *pN
8540: 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 ew = pParse->pNe
8550: 77 54 72 69 67 67 65 72 3b 0a 20 20 54 72 69 67 wTrigger;. Trig
8560: 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 3b 0a gerStep *pStep;.
8570: 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e NameContext sN
8580: 43 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 C;. int rc = SQ
8590: 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 6d 65 6d 73 LITE_OK;.. mems
85a0: 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 et(&sNC, 0, size
85b0: 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e of(sNC));. sNC.
85c0: 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b pParse = pParse;
85d0: 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d . assert( pNew-
85e0: 3e 70 54 61 62 53 63 68 65 6d 61 20 29 3b 0a 20 >pTabSchema );.
85f0: 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 pParse->pTrigge
8600: 72 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 rTab = sqlite3Fi
8610: 6e 64 54 61 62 6c 65 28 64 62 2c 20 70 4e 65 77 ndTable(db, pNew
8620: 2d 3e 74 61 62 6c 65 2c 20 0a 20 20 20 20 20 20 ->table, .
8630: 64 62 2d 3e 61 44 62 5b 73 71 6c 69 74 65 33 53 db->aDb[sqlite3S
8640: 63 68 65 6d 61 54 6f 49 6e 64 65 78 32 28 64 62 chemaToIndex2(db
8650: 2c 20 70 4e 65 77 2d 3e 70 54 61 62 53 63 68 65 , pNew->pTabSche
8660: 6d 61 2c 20 30 29 5d 2e 7a 44 62 53 4e 61 6d 65 ma, 0)].zDbSName
8670: 0a 20 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e . );. pParse->
8680: 65 54 72 69 67 67 65 72 4f 70 20 3d 20 70 4e 65 eTriggerOp = pNe
8690: 77 2d 3e 6f 70 3b 0a 20 20 2f 2a 20 41 4c 57 41 w->op;. /* ALWA
86a0: 59 53 28 29 20 62 65 63 61 75 73 65 20 69 66 20 YS() because if
86b0: 74 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 65 the table of the
86c0: 20 74 72 69 67 67 65 72 20 64 6f 65 73 20 6e 6f trigger does no
86d0: 74 20 65 78 69 73 74 2c 20 74 68 65 0a 20 20 2a t exist, the. *
86e0: 2a 20 65 72 72 6f 72 20 77 6f 75 6c 64 20 68 61 * error would ha
86f0: 76 65 20 62 65 65 6e 20 68 69 74 20 62 65 66 6f ve been hit befo
8700: 72 65 20 74 68 69 73 20 70 6f 69 6e 74 20 2a 2f re this point */
8710: 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 . if( ALWAYS(pP
8720: 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 arse->pTriggerTa
8730: 62 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 b) ){. int iD
8740: 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
8750: 61 54 6f 49 6e 64 65 78 32 28 64 62 2c 20 70 50 aToIndex2(db, pP
8760: 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 arse->pTriggerTa
8770: 62 2d 3e 70 53 63 68 65 6d 61 2c 20 30 29 3b 0a b->pSchema, 0);.
8780: 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
8790: 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d ViewGetColumnNam
87a0: 65 73 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 es(pParse, iDb,
87b0: 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 pParse->pTrigger
87c0: 54 61 62 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Tab);. }.. /*
87d0: 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 73 20 Resolve symbols
87e0: 69 6e 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a in WHEN clause *
87f0: 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
8800: 54 45 5f 4f 4b 20 26 26 20 70 4e 65 77 2d 3e 70 TE_OK && pNew->p
8810: 57 68 65 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d When ){. rc =
8820: 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
8830: 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 xprNames(&sNC, p
8840: 4e 65 77 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 7d New->pWhen);. }
8850: 0a 0a 20 20 66 6f 72 28 70 53 74 65 70 3d 70 4e .. for(pStep=pN
8860: 65 77 2d 3e 73 74 65 70 5f 6c 69 73 74 3b 20 72 ew->step_list; r
8870: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
8880: 70 53 74 65 70 3b 20 70 53 74 65 70 3d 70 53 74 pStep; pStep=pSt
8890: 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 ep->pNext){.
88a0: 69 66 28 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 if( pStep->pSele
88b0: 63 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ct ){. sqli
88c0: 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 te3SelectPrep(pP
88d0: 61 72 73 65 2c 20 70 53 74 65 70 2d 3e 70 53 65 arse, pStep->pSe
88e0: 6c 65 63 74 2c 20 26 73 4e 43 29 3b 0a 20 20 20 lect, &sNC);.
88f0: 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e if( pParse->n
8900: 45 72 72 20 29 20 72 63 20 3d 20 70 50 61 72 73 Err ) rc = pPars
8910: 65 2d 3e 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 e->rc;. }.
8920: 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
8930: 4f 4b 20 26 26 20 70 53 74 65 70 2d 3e 7a 54 61 OK && pStep->zTa
8940: 72 67 65 74 20 29 7b 0a 20 20 20 20 20 20 54 61 rget ){. Ta
8950: 62 6c 65 20 2a 70 54 61 72 67 65 74 20 3d 20 73 ble *pTarget = s
8960: 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c qlite3LocateTabl
8970: 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 74 e(pParse, 0, pSt
8980: 65 70 2d 3e 7a 54 61 72 67 65 74 2c 20 7a 44 62 ep->zTarget, zDb
8990: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 );. if( pTa
89a0: 72 67 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 rget==0 ){.
89b0: 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
89c0: 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 65 6c 73 RROR;. }els
89d0: 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 e{. int i
89e0: 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
89f0: 6d 61 54 6f 49 6e 64 65 78 32 28 64 62 2c 20 70 maToIndex2(db, p
8a00: 54 61 72 67 65 74 2d 3e 70 53 63 68 65 6d 61 2c Target->pSchema,
8a10: 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0);. if(
8a20: 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 SQLITE_OK==(rc
8a30: 3d 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 = sqlite3ViewGet
8a40: 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 ColumnNames(pPar
8a50: 73 65 2c 20 69 44 62 2c 20 70 54 61 72 67 65 74 se, iDb, pTarget
8a60: 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 )) ){.
8a70: 53 72 63 4c 69 73 74 20 73 53 72 63 3b 0a 20 20 SrcList sSrc;.
8a80: 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 memset(&
8a90: 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 sSrc, 0, sizeof(
8aa0: 73 53 72 63 29 29 3b 0a 20 20 20 20 20 20 20 20 sSrc));.
8ab0: 20 20 73 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b sSrc.nSrc = 1;
8ac0: 0a 20 20 20 20 20 20 20 20 20 20 73 53 72 63 2e . sSrc.
8ad0: 61 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 70 53 74 a[0].zName = pSt
8ae0: 65 70 2d 3e 7a 54 61 72 67 65 74 3b 0a 20 20 20 ep->zTarget;.
8af0: 20 20 20 20 20 20 20 73 53 72 63 2e 61 5b 30 5d sSrc.a[0]
8b00: 2e 70 54 61 62 20 3d 20 70 54 61 72 67 65 74 3b .pTab = pTarget;
8b10: 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e 70 . sNC.p
8b20: 53 72 63 4c 69 73 74 20 3d 20 26 73 53 72 63 3b SrcList = &sSrc;
8b30: 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
8b40: 53 74 65 70 2d 3e 70 57 68 65 72 65 20 29 7b 0a Step->pWhere ){.
8b50: 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
8b60: 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
8b70: 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 xprNames(&sNC, p
8b80: 53 74 65 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 Step->pWhere);.
8b90: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
8ba0: 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
8bb0: 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
8bc0: 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
8bd0: 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4c 69 73 e3ResolveExprLis
8be0: 74 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 53 74 tNames(&sNC, pSt
8bf0: 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a ep->pExprList);.
8c00: 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
8c10: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
8c20: 53 74 65 70 2d 3e 70 55 70 73 65 72 74 20 7c 7c Step->pUpsert ||
8c30: 20 28 21 70 53 74 65 70 2d 3e 70 57 68 65 72 65 (!pStep->pWhere
8c40: 20 26 26 20 21 70 53 74 65 70 2d 3e 70 45 78 70 && !pStep->pExp
8c50: 72 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 20 20 rList) );.
8c60: 20 20 20 20 69 66 28 20 70 53 74 65 70 2d 3e 70 if( pStep->p
8c70: 55 70 73 65 72 74 20 29 7b 0a 20 20 20 20 20 20 Upsert ){.
8c80: 20 20 20 20 20 20 55 70 73 65 72 74 20 2a 70 55 Upsert *pU
8c90: 70 73 65 72 74 20 3d 20 70 53 74 65 70 2d 3e 70 psert = pStep->p
8ca0: 55 70 73 65 72 74 3b 0a 20 20 20 20 20 20 20 20 Upsert;.
8cb0: 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
8cc0: 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
8cd0: 20 20 20 20 20 20 20 20 20 70 55 70 73 65 72 74 pUpsert
8ce0: 2d 3e 70 55 70 73 65 72 74 53 72 63 20 3d 20 26 ->pUpsertSrc = &
8cf0: 73 53 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 sSrc;.
8d00: 20 20 73 4e 43 2e 75 4e 43 2e 70 55 70 73 65 72 sNC.uNC.pUpser
8d10: 74 20 3d 20 70 55 70 73 65 72 74 3b 0a 20 20 20 t = pUpsert;.
8d20: 20 20 20 20 20 20 20 20 20 73 4e 43 2e 6e 63 46 sNC.ncF
8d30: 6c 61 67 73 20 3d 20 4e 43 5f 55 55 70 73 65 72 lags = NC_UUpser
8d40: 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 t;. r
8d50: 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 6f 6c c = sqlite3Resol
8d60: 76 65 45 78 70 72 4c 69 73 74 4e 61 6d 65 73 28 veExprListNames(
8d70: 26 73 4e 43 2c 20 70 55 70 73 65 72 74 2d 3e 70 &sNC, pUpsert->p
8d80: 55 70 73 65 72 74 54 61 72 67 65 74 29 3b 0a 20 UpsertTarget);.
8d90: 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
8da0: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
8db0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78 Ex
8dc0: 70 72 4c 69 73 74 20 2a 70 55 70 73 65 72 74 53 prList *pUpsertS
8dd0: 65 74 20 3d 20 70 55 70 73 65 72 74 2d 3e 70 55 et = pUpsert->pU
8de0: 70 73 65 72 74 53 65 74 3b 0a 20 20 20 20 20 20 psertSet;.
8df0: 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
8e00: 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4c ite3ResolveExprL
8e10: 69 73 74 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 istNames(&sNC, p
8e20: 55 70 73 65 72 74 53 65 74 29 3b 0a 20 20 20 20 UpsertSet);.
8e30: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
8e40: 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
8e50: 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
8e60: 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
8e70: 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
8e80: 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 55 70 73 Names(&sNC, pUps
8e90: 65 72 74 2d 3e 70 55 70 73 65 72 74 57 68 65 72 ert->pUpsertWher
8ea0: 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
8eb0: 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 }. if
8ec0: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
8ed0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
8ee0: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 rc = sqlite3Res
8ef0: 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 olveExprNames(&s
8f00: 4e 43 2c 20 70 55 70 73 65 72 74 2d 3e 70 55 70 NC, pUpsert->pUp
8f10: 73 65 72 74 54 61 72 67 65 74 57 68 65 72 65 29 sertTargetWhere)
8f20: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
8f30: 20 20 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e sNC.
8f40: 6e 63 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 ncFlags = 0;.
8f50: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
8f60: 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
8f70: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
8f80: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b ;.}../*.** Invok
8f90: 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 e sqlite3WalkExp
8fa0: 72 28 29 20 6f 72 20 73 71 6c 69 74 65 33 57 61 r() or sqlite3Wa
8fb0: 6c 6b 53 65 6c 65 63 74 28 29 20 6f 6e 20 61 6c lkSelect() on al
8fc0: 6c 20 53 65 6c 65 63 74 20 6f 72 20 45 78 70 72 l Select or Expr
8fd0: 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 74 68 61 74 .** objects that
8fe0: 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 are part of the
8ff0: 20 74 72 69 67 67 65 72 20 70 61 73 73 65 64 20 trigger passed
9000: 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
9010: 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 gument..*/.stati
9020: 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 57 61 6c c void renameWal
9030: 6b 54 72 69 67 67 65 72 28 57 61 6c 6b 65 72 20 kTrigger(Walker
9040: 2a 70 57 61 6c 6b 65 72 2c 20 54 72 69 67 67 65 *pWalker, Trigge
9050: 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 r *pTrigger){.
9060: 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 TriggerStep *pSt
9070: 65 70 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 ep;.. /* Find t
9080: 6f 6b 65 6e 73 20 74 6f 20 65 64 69 74 20 69 6e okens to edit in
9090: 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a WHEN clause */.
90a0: 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 sqlite3WalkExp
90b0: 72 28 70 57 61 6c 6b 65 72 2c 20 70 54 72 69 67 r(pWalker, pTrig
90c0: 67 65 72 2d 3e 70 57 68 65 6e 29 3b 0a 0a 20 20 ger->pWhen);..
90d0: 2f 2a 20 46 69 6e 64 20 74 6f 6b 65 6e 73 20 74 /* Find tokens t
90e0: 6f 20 65 64 69 74 20 69 6e 20 74 72 69 67 67 65 o edit in trigge
90f0: 72 20 73 74 65 70 73 20 2a 2f 0a 20 20 66 6f 72 r steps */. for
9100: 28 70 53 74 65 70 3d 70 54 72 69 67 67 65 72 2d (pStep=pTrigger-
9110: 3e 73 74 65 70 5f 6c 69 73 74 3b 20 70 53 74 65 >step_list; pSte
9120: 70 3b 20 70 53 74 65 70 3d 70 53 74 65 70 2d 3e p; pStep=pStep->
9130: 70 4e 65 78 74 29 7b 0a 20 20 20 20 73 71 6c 69 pNext){. sqli
9140: 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 te3WalkSelect(pW
9150: 61 6c 6b 65 72 2c 20 70 53 74 65 70 2d 3e 70 53 alker, pStep->pS
9160: 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 elect);. sqli
9170: 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
9180: 6b 65 72 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 ker, pStep->pWhe
9190: 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 re);. sqlite3
91a0: 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 WalkExprList(pWa
91b0: 6c 6b 65 72 2c 20 70 53 74 65 70 2d 3e 70 45 78 lker, pStep->pEx
91c0: 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 69 66 28 prList);. if(
91d0: 20 70 53 74 65 70 2d 3e 70 55 70 73 65 72 74 20 pStep->pUpsert
91e0: 29 7b 0a 20 20 20 20 20 20 55 70 73 65 72 74 20 ){. Upsert
91f0: 2a 70 55 70 73 65 72 74 20 3d 20 70 53 74 65 70 *pUpsert = pStep
9200: 2d 3e 70 55 70 73 65 72 74 3b 0a 20 20 20 20 20 ->pUpsert;.
9210: 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
9220: 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 55 List(pWalker, pU
9230: 70 73 65 72 74 2d 3e 70 55 70 73 65 72 74 54 61 psert->pUpsertTa
9240: 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c rget);. sql
9250: 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 ite3WalkExprList
9260: 28 70 57 61 6c 6b 65 72 2c 20 70 55 70 73 65 72 (pWalker, pUpser
9270: 74 2d 3e 70 55 70 73 65 72 74 53 65 74 29 3b 0a t->pUpsertSet);.
9280: 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c sqlite3Wal
9290: 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 kExpr(pWalker, p
92a0: 55 70 73 65 72 74 2d 3e 70 55 70 73 65 72 74 57 Upsert->pUpsertW
92b0: 68 65 72 65 29 3b 0a 20 20 20 20 20 20 73 71 6c here);. sql
92c0: 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
92d0: 6c 6b 65 72 2c 20 70 55 70 73 65 72 74 2d 3e 70 lker, pUpsert->p
92e0: 55 70 73 65 72 74 54 61 72 67 65 74 57 68 65 72 UpsertTargetWher
92f0: 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a e);. }. }.}.
9300: 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 ./*.** Free the
9310: 63 6f 6e 74 65 6e 74 73 20 6f 66 20 50 61 72 73 contents of Pars
9320: 65 20 6f 62 6a 65 63 74 20 28 2a 70 50 61 72 73 e object (*pPars
9330: 65 29 2e 20 44 6f 20 6e 6f 74 20 66 72 65 65 20 e). Do not free
9340: 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6f 63 the memory.** oc
9350: 63 75 70 69 65 64 20 62 79 20 74 68 65 20 50 61 cupied by the Pa
9360: 72 73 65 20 6f 62 6a 65 63 74 20 69 74 73 65 6c rse object itsel
9370: 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 f..*/.static voi
9380: 64 20 72 65 6e 61 6d 65 50 61 72 73 65 43 6c 65 d renameParseCle
9390: 61 6e 75 70 28 50 61 72 73 65 20 2a 70 50 61 72 anup(Parse *pPar
93a0: 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a se){. sqlite3 *
93b0: 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
93c0: 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 . if( pParse->p
93d0: 56 64 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 Vdbe ){. sqli
93e0: 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 te3VdbeFinalize(
93f0: 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a pParse->pVdbe);.
9400: 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 6c }. sqlite3Del
9410: 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 50 61 eteTable(db, pPa
9420: 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b rse->pNewTable);
9430: 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 . if( pParse->p
9440: 4e 65 77 49 6e 64 65 78 20 29 20 73 71 6c 69 74 NewIndex ) sqlit
9450: 65 33 46 72 65 65 49 6e 64 65 78 28 64 62 2c 20 e3FreeIndex(db,
9460: 70 50 61 72 73 65 2d 3e 70 4e 65 77 49 6e 64 65 pParse->pNewInde
9470: 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c x);. sqlite3Del
9480: 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 eteTrigger(db, p
9490: 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 Parse->pNewTrigg
94a0: 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 er);. sqlite3Db
94b0: 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d Free(db, pParse-
94c0: 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 65 6e >zErrMsg);. ren
94d0: 61 6d 65 54 6f 6b 65 6e 46 72 65 65 28 64 62 2c ameTokenFree(db,
94e0: 20 70 50 61 72 73 65 2d 3e 70 52 65 6e 61 6d 65 pParse->pRename
94f0: 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 );. sqlite3Pars
9500: 65 72 52 65 73 65 74 28 70 50 61 72 73 65 29 3b erReset(pParse);
9510: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 66 75 .}../*.** SQL fu
9520: 6e 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 nction:.**.**
9530: 20 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f sqlite_rename_
9540: 63 6f 6c 75 6d 6e 28 7a 53 71 6c 2c 20 69 43 6f column(zSql, iCo
9550: 6c 2c 20 62 51 75 6f 74 65 2c 20 7a 4e 65 77 2c l, bQuote, zNew,
9560: 20 7a 54 61 62 6c 65 2c 20 7a 4f 6c 64 29 0a 2a zTable, zOld).*
9570: 2a 0a 2a 2a 20 20 20 30 2e 20 7a 53 71 6c 3a 20 *.** 0. zSql:
9580: 20 20 20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e SQL statemen
9590: 74 20 74 6f 20 72 65 77 72 69 74 65 0a 2a 2a 20 t to rewrite.**
95a0: 20 20 31 2e 20 74 79 70 65 3a 20 20 20 20 20 54 1. type: T
95b0: 79 70 65 20 6f 66 20 6f 62 6a 65 63 74 20 28 22 ype of object ("
95c0: 74 61 62 6c 65 22 2c 20 22 76 69 65 77 22 20 65 table", "view" e
95d0: 74 63 2e 29 0a 2a 2a 20 20 20 32 2e 20 6f 62 6a tc.).** 2. obj
95e0: 65 63 74 3a 20 20 20 4e 61 6d 65 20 6f 66 20 6f ect: Name of o
95f0: 62 6a 65 63 74 0a 2a 2a 20 20 20 33 2e 20 44 61 bject.** 3. Da
9600: 74 61 62 61 73 65 3a 20 44 61 74 61 62 61 73 65 tabase: Database
9610: 20 6e 61 6d 65 20 28 65 2e 67 2e 20 22 6d 61 69 name (e.g. "mai
9620: 6e 22 29 0a 2a 2a 20 20 20 34 2e 20 54 61 62 6c n").** 4. Tabl
9630: 65 3a 20 20 20 20 54 61 62 6c 65 20 6e 61 6d 65 e: Table name
9640: 0a 2a 2a 20 20 20 35 2e 20 69 43 6f 6c 3a 20 20 .** 5. iCol:
9650: 20 20 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 Index of colu
9660: 6d 6e 20 74 6f 20 72 65 6e 61 6d 65 0a 2a 2a 20 mn to rename.**
9670: 20 20 36 2e 20 7a 4e 65 77 3a 20 20 20 20 20 4e 6. zNew: N
9680: 65 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 2a ew column name.*
9690: 2a 20 20 20 37 2e 20 62 51 75 6f 74 65 3a 20 20 * 7. bQuote:
96a0: 20 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 Non-zero if the
96b0: 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 new column name
96c0: 20 73 68 6f 75 6c 64 20 62 65 20 71 75 6f 74 65 should be quote
96d0: 64 2e 0a 2a 2a 20 20 20 38 2e 20 62 54 65 6d 70 d..** 8. bTemp
96e0: 3a 20 20 20 20 54 72 75 65 20 69 66 20 7a 53 71 : True if zSq
96f0: 6c 20 63 6f 6d 65 73 20 66 72 6f 6d 20 74 65 6d l comes from tem
9700: 70 20 73 63 68 65 6d 61 0a 2a 2a 0a 2a 2a 20 44 p schema.**.** D
9710: 6f 20 61 20 63 6f 6c 75 6d 6e 20 72 65 6e 61 6d o a column renam
9720: 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 74 e operation on t
9730: 68 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d he CREATE statem
9740: 65 6e 74 20 67 69 76 65 6e 20 69 6e 20 7a 53 71 ent given in zSq
9750: 6c 2e 0a 2a 2a 20 54 68 65 20 69 43 6f 6c 2d 74 l..** The iCol-t
9760: 68 20 63 6f 6c 75 6d 6e 20 28 6c 65 66 74 2d 6d h column (left-m
9770: 6f 73 74 20 69 73 20 30 29 20 6f 66 20 74 61 62 ost is 0) of tab
9780: 6c 65 20 7a 54 61 62 6c 65 20 69 73 20 72 65 6e le zTable is ren
9790: 61 6d 65 64 20 66 72 6f 6d 20 7a 43 6f 6c 0a 2a amed from zCol.*
97a0: 2a 20 69 6e 74 6f 20 7a 4e 65 77 2e 20 20 54 68 * into zNew. Th
97b0: 65 20 6e 61 6d 65 20 73 68 6f 75 6c 64 20 62 65 e name should be
97c0: 20 71 75 6f 74 65 64 20 69 66 20 62 51 75 6f 74 quoted if bQuot
97d0: 65 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a e is true..**.**
97e0: 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
97f0: 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c s used internall
9800: 79 20 62 79 20 74 68 65 20 41 4c 54 45 52 20 54 y by the ALTER T
9810: 41 42 4c 45 20 52 45 4e 41 4d 45 20 43 4f 4c 55 ABLE RENAME COLU
9820: 4d 4e 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 20 54 MN command..** T
9830: 68 6f 75 67 68 20 61 63 63 65 73 73 69 62 6c 65 hough accessible
9840: 20 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 to application
9850: 63 6f 64 65 2c 20 69 74 20 69 73 20 6e 6f 74 20 code, it is not
9860: 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 intended for use
9870: 20 62 79 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 by.** applicati
9880: 6f 6e 73 2e 20 20 54 68 65 20 65 78 69 73 74 61 ons. The exista
9890: 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 nce of this func
98a0: 74 69 6f 6e 2c 20 61 6e 64 20 74 68 65 20 77 61 tion, and the wa
98b0: 79 20 69 74 20 77 6f 72 6b 73 2c 0a 2a 2a 20 69 y it works,.** i
98c0: 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 s subject to cha
98d0: 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 nge without noti
98e0: 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 ce..**.** If any
98f0: 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 of the paramete
9900: 72 73 20 61 72 65 20 6f 75 74 2d 6f 66 2d 62 6f rs are out-of-bo
9910: 75 6e 64 73 2c 20 74 68 65 6e 20 73 69 6d 70 6c unds, then simpl
9920: 79 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a y return NULL..*
9930: 2a 20 41 6e 20 6f 75 74 2d 6f 66 2d 62 6f 75 6e * An out-of-boun
9940: 64 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 6e ds parameter can
9950: 20 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68 65 6e only occur when
9960: 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
9970: 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 69 73 20 66 calls.** this f
9980: 75 6e 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 unction directly
9990: 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 . The parameter
99a0: 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 s will always be
99b0: 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 77 68 65 well-formed whe
99c0: 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e n.** this routin
99d0: 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 e is invoked by
99e0: 74 68 65 20 62 79 74 65 63 6f 64 65 20 66 6f 72 the bytecode for
99f0: 20 61 20 6c 65 67 69 74 69 6d 61 74 65 20 41 4c a legitimate AL
9a00: 54 45 52 20 54 41 42 4c 45 0a 2a 2a 20 73 74 61 TER TABLE.** sta
9a10: 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 tement..*/.stati
9a20: 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 43 6f 6c c void renameCol
9a30: 75 6d 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 umnFunc(. sqlit
9a40: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
9a50: 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
9a60: 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
9a70: 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
9a80: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
9a90: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
9aa0: 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
9ab0: 3b 0a 20 20 52 65 6e 61 6d 65 43 74 78 20 73 43 ;. RenameCtx sC
9ac0: 74 78 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 tx;. const char
9ad0: 20 2a 7a 53 71 6c 20 3d 20 28 63 6f 6e 73 74 20 *zSql = (const
9ae0: 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 char*)sqlite3_va
9af0: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
9b00: 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 );. const char
9b10: 2a 7a 44 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 *zDb = (const ch
9b20: 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 ar*)sqlite3_valu
9b30: 65 5f 74 65 78 74 28 61 72 67 76 5b 33 5d 29 3b e_text(argv[3]);
9b40: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
9b50: 54 61 62 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 Table = (const c
9b60: 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
9b70: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 34 5d 29 ue_text(argv[4])
9b80: 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 73 ;. int iCol = s
9b90: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
9ba0: 28 61 72 67 76 5b 35 5d 29 3b 0a 20 20 63 6f 6e (argv[5]);. con
9bb0: 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 st char *zNew =
9bc0: 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c (const char*)sql
9bd0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
9be0: 61 72 67 76 5b 36 5d 29 3b 0a 20 20 69 6e 74 20 argv[6]);. int
9bf0: 62 51 75 6f 74 65 20 3d 20 73 71 6c 69 74 65 33 bQuote = sqlite3
9c00: 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b _value_int(argv[
9c10: 37 5d 29 3b 0a 20 20 69 6e 74 20 62 54 65 6d 70 7]);. int bTemp
9c20: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
9c30: 5f 69 6e 74 28 61 72 67 76 5b 38 5d 29 3b 0a 20 _int(argv[8]);.
9c40: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 6c const char *zOl
9c50: 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 d;. int rc;. P
9c60: 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 57 arse sParse;. W
9c70: 61 6c 6b 65 72 20 73 57 61 6c 6b 65 72 3b 0a 20 alker sWalker;.
9c80: 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 Index *pIdx;.
9c90: 69 6e 74 20 69 3b 0a 20 20 54 61 62 6c 65 20 2a int i;. Table *
9ca0: 70 54 61 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 pTab;.#ifndef SQ
9cb0: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
9cc0: 49 5a 41 54 49 4f 4e 0a 20 20 73 71 6c 69 74 65 IZATION. sqlite
9cd0: 33 5f 78 61 75 74 68 20 78 41 75 74 68 20 3d 20 3_xauth xAuth =
9ce0: 64 62 2d 3e 78 41 75 74 68 3b 0a 23 65 6e 64 69 db->xAuth;.#endi
9cf0: 66 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 f.. UNUSED_PARA
9d00: 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
9d10: 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20 if( zSql==0 )
9d20: 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 7a 54 return;. if( zT
9d30: 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e able==0 ) return
9d40: 3b 0a 20 20 69 66 28 20 7a 4e 65 77 3d 3d 30 20 ;. if( zNew==0
9d50: 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
9d60: 69 43 6f 6c 3c 30 20 29 20 72 65 74 75 72 6e 3b iCol<0 ) return;
9d70: 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
9d80: 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 70 nterAll(db);. p
9d90: 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e Tab = sqlite3Fin
9da0: 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 6c dTable(db, zTabl
9db0: 65 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 70 e, zDb);. if( p
9dc0: 54 61 62 3d 3d 30 20 7c 7c 20 69 43 6f 6c 3e 3d Tab==0 || iCol>=
9dd0: 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 pTab->nCol ){.
9de0: 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
9df0: 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
9e00: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 4f return;. }. zO
9e10: 6c 64 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b ld = pTab->aCol[
9e20: 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 6d iCol].zName;. m
9e30: 65 6d 73 65 74 28 26 73 43 74 78 2c 20 30 2c 20 emset(&sCtx, 0,
9e40: 73 69 7a 65 6f 66 28 73 43 74 78 29 29 3b 0a 20 sizeof(sCtx));.
9e50: 20 73 43 74 78 2e 69 43 6f 6c 20 3d 20 28 28 69 sCtx.iCol = ((i
9e60: 43 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 Col==pTab->iPKey
9e70: 29 20 3f 20 2d 31 20 3a 20 69 43 6f 6c 29 3b 0a ) ? -1 : iCol);.
9e80: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
9e90: 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
9ea0: 4f 4e 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d ON. db->xAuth =
9eb0: 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 63 20 0;.#endif. rc
9ec0: 3d 20 72 65 6e 61 6d 65 50 61 72 73 65 53 71 6c = renameParseSql
9ed0: 28 26 73 50 61 72 73 65 2c 20 7a 44 62 2c 20 30 (&sParse, zDb, 0
9ee0: 2c 20 64 62 2c 20 7a 53 71 6c 2c 20 62 54 65 6d , db, zSql, bTem
9ef0: 70 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 p);.. /* Find t
9f00: 6f 6b 65 6e 73 20 74 68 61 74 20 6e 65 65 64 20 okens that need
9f10: 74 6f 20 62 65 20 72 65 70 6c 61 63 65 64 2e 20 to be replaced.
9f20: 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 57 61 */. memset(&sWa
9f30: 6c 6b 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 lker, 0, sizeof(
9f40: 57 61 6c 6b 65 72 29 29 3b 0a 20 20 73 57 61 6c Walker));. sWal
9f50: 6b 65 72 2e 70 50 61 72 73 65 20 3d 20 26 73 50 ker.pParse = &sP
9f60: 61 72 73 65 3b 0a 20 20 73 57 61 6c 6b 65 72 2e arse;. sWalker.
9f70: 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 xExprCallback =
9f80: 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 45 78 70 72 renameColumnExpr
9f90: 43 62 3b 0a 20 20 73 57 61 6c 6b 65 72 2e 78 53 Cb;. sWalker.xS
9fa0: 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 electCallback =
9fb0: 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 53 65 6c 65 renameColumnSele
9fc0: 63 74 43 62 3b 0a 20 20 73 57 61 6c 6b 65 72 2e ctCb;. sWalker.
9fd0: 75 2e 70 52 65 6e 61 6d 65 20 3d 20 26 73 43 74 u.pRename = &sCt
9fe0: 78 3b 0a 0a 20 20 73 43 74 78 2e 70 54 61 62 20 x;.. sCtx.pTab
9ff0: 3d 20 70 54 61 62 3b 0a 20 20 69 66 28 20 72 63 = pTab;. if( rc
a000: 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
a010: 74 6f 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 46 to renameColumnF
a020: 75 6e 63 5f 64 6f 6e 65 3b 0a 20 20 69 66 28 20 unc_done;. if(
a030: 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 sParse.pNewTable
a040: 20 29 7b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a ){. Select *
a050: 70 53 65 6c 65 63 74 20 3d 20 73 50 61 72 73 65 pSelect = sParse
a060: 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 70 53 65 6c .pNewTable->pSel
a070: 65 63 74 3b 0a 20 20 20 20 69 66 28 20 70 53 65 ect;. if( pSe
a080: 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 73 50 lect ){. sP
a090: 61 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 arse.rc = SQLITE
a0a0: 5f 4f 4b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 _OK;. sqlit
a0b0: 65 33 53 65 6c 65 63 74 50 72 65 70 28 26 73 50 e3SelectPrep(&sP
a0c0: 61 72 73 65 2c 20 73 50 61 72 73 65 2e 70 4e 65 arse, sParse.pNe
a0d0: 77 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 2c wTable->pSelect,
a0e0: 20 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 0);. rc =
a0f0: 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 (db->mallocFaile
a100: 64 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d d ? SQLITE_NOMEM
a110: 20 3a 20 73 50 61 72 73 65 2e 72 63 29 3b 0a 20 : sParse.rc);.
a120: 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
a130: 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
a140: 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c sqlite3WalkSel
a150: 65 63 74 28 26 73 57 61 6c 6b 65 72 2c 20 70 53 ect(&sWalker, pS
a160: 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 7d 0a elect);. }.
a170: 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
a180: 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 72 LITE_OK ) goto r
a190: 65 6e 61 6d 65 43 6f 6c 75 6d 6e 46 75 6e 63 5f enameColumnFunc_
a1a0: 64 6f 6e 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b done;. }else{
a1b0: 0a 20 20 20 20 20 20 2f 2a 20 41 20 72 65 67 75 . /* A regu
a1c0: 6c 61 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 lar table */.
a1d0: 20 20 20 69 6e 74 20 62 46 4b 4f 6e 6c 79 20 3d int bFKOnly =
a1e0: 20 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 sqlite3_stricmp
a1f0: 28 7a 54 61 62 6c 65 2c 20 73 50 61 72 73 65 2e (zTable, sParse.
a200: 70 4e 65 77 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 pNewTable->zName
a210: 29 3b 0a 20 20 20 20 20 20 46 4b 65 79 20 2a 70 );. FKey *p
a220: 46 4b 65 79 3b 0a 20 20 20 20 20 20 61 73 73 65 FKey;. asse
a230: 72 74 28 20 73 50 61 72 73 65 2e 70 4e 65 77 54 rt( sParse.pNewT
a240: 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 able->pSelect==0
a250: 20 29 3b 0a 20 20 20 20 20 20 73 43 74 78 2e 70 );. sCtx.p
a260: 54 61 62 20 3d 20 73 50 61 72 73 65 2e 70 4e 65 Tab = sParse.pNe
a270: 77 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 69 66 wTable;. if
a280: 28 20 62 46 4b 4f 6e 6c 79 3d 3d 30 20 29 7b 0a ( bFKOnly==0 ){.
a290: 20 20 20 20 20 20 20 20 72 65 6e 61 6d 65 54 6f renameTo
a2a0: 6b 65 6e 46 69 6e 64 28 0a 20 20 20 20 20 20 20 kenFind(.
a2b0: 20 20 20 20 20 26 73 50 61 72 73 65 2c 20 26 73 &sParse, &s
a2c0: 43 74 78 2c 20 28 76 6f 69 64 2a 29 73 50 61 72 Ctx, (void*)sPar
a2d0: 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 se.pNewTable->aC
a2e0: 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 0a 20 ol[iCol].zName.
a2f0: 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
a300: 20 20 69 66 28 20 73 43 74 78 2e 69 43 6f 6c 3c if( sCtx.iCol<
a310: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
a320: 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 26 enameTokenFind(&
a330: 73 50 61 72 73 65 2c 20 26 73 43 74 78 2c 20 28 sParse, &sCtx, (
a340: 76 6f 69 64 2a 29 26 73 50 61 72 73 65 2e 70 4e void*)&sParse.pN
a350: 65 77 54 61 62 6c 65 2d 3e 69 50 4b 65 79 29 3b ewTable->iPKey);
a360: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a370: 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 sqlite3WalkEx
a380: 70 72 4c 69 73 74 28 26 73 57 61 6c 6b 65 72 2c prList(&sWalker,
a390: 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c sParse.pNewTabl
a3a0: 65 2d 3e 70 43 68 65 63 6b 29 3b 0a 20 20 20 20 e->pCheck);.
a3b0: 20 20 20 20 66 6f 72 28 70 49 64 78 3d 73 50 61 for(pIdx=sPa
a3c0: 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 70 rse.pNewTable->p
a3d0: 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 Index; pIdx; pId
a3e0: 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a x=pIdx->pNext){.
a3f0: 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
a400: 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 26 73 3WalkExprList(&s
a410: 57 61 6c 6b 65 72 2c 20 70 49 64 78 2d 3e 61 43 Walker, pIdx->aC
a420: 6f 6c 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 olExpr);.
a430: 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
a440: 20 20 66 6f 72 28 70 46 4b 65 79 3d 73 50 61 72 for(pFKey=sPar
a450: 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 70 46 se.pNewTable->pF
a460: 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 Key; pFKey; pFKe
a470: 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 y=pFKey->pNextFr
a480: 6f 6d 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 om){. for
a490: 28 69 3d 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e (i=0; i<pFKey->n
a4a0: 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Col; i++){.
a4b0: 20 20 20 20 20 69 66 28 20 62 46 4b 4f 6e 6c 79 if( bFKOnly
a4c0: 3d 3d 30 20 26 26 20 70 46 4b 65 79 2d 3e 61 43 ==0 && pFKey->aC
a4d0: 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 3d 3d 69 43 6f ol[i].iFrom==iCo
a4e0: 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 l ){.
a4f0: 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 renameTokenFind
a500: 28 26 73 50 61 72 73 65 2c 20 26 73 43 74 78 2c (&sParse, &sCtx,
a510: 20 28 76 6f 69 64 2a 29 26 70 46 4b 65 79 2d 3e (void*)&pFKey->
a520: 61 43 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 aCol[i]);.
a530: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
a540: 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 if( 0==sqlite3_s
a550: 74 72 69 63 6d 70 28 70 46 4b 65 79 2d 3e 7a 54 tricmp(pFKey->zT
a560: 6f 2c 20 7a 54 61 62 6c 65 29 0a 20 20 20 20 20 o, zTable).
a570: 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 && 0==sqli
a580: 74 65 33 5f 73 74 72 69 63 6d 70 28 70 46 4b 65 te3_stricmp(pFKe
a590: 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 2c y->aCol[i].zCol,
a5a0: 20 7a 4f 6c 64 29 0a 20 20 20 20 20 20 20 20 20 zOld).
a5b0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a5c0: 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 renameTokenFind(
a5d0: 26 73 50 61 72 73 65 2c 20 26 73 43 74 78 2c 20 &sParse, &sCtx,
a5e0: 28 76 6f 69 64 2a 29 70 46 4b 65 79 2d 3e 61 43 (void*)pFKey->aC
a5f0: 6f 6c 5b 69 5d 2e 7a 43 6f 6c 29 3b 0a 20 20 20 ol[i].zCol);.
a600: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a610: 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
a620: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 50 61 . }else if( sPa
a630: 72 73 65 2e 70 4e 65 77 49 6e 64 65 78 20 29 7b rse.pNewIndex ){
a640: 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b . sqlite3Walk
a650: 45 78 70 72 4c 69 73 74 28 26 73 57 61 6c 6b 65 ExprList(&sWalke
a660: 72 2c 20 73 50 61 72 73 65 2e 70 4e 65 77 49 6e r, sParse.pNewIn
a670: 64 65 78 2d 3e 61 43 6f 6c 45 78 70 72 29 3b 0a dex->aColExpr);.
a680: 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 sqlite3WalkE
a690: 78 70 72 28 26 73 57 61 6c 6b 65 72 2c 20 73 50 xpr(&sWalker, sP
a6a0: 61 72 73 65 2e 70 4e 65 77 49 6e 64 65 78 2d 3e arse.pNewIndex->
a6b0: 70 50 61 72 74 49 64 78 57 68 65 72 65 29 3b 0a pPartIdxWhere);.
a6c0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
a6d0: 41 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 A trigger */.
a6e0: 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 TriggerStep *pS
a6f0: 74 65 70 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 tep;. rc = re
a700: 6e 61 6d 65 52 65 73 6f 6c 76 65 54 72 69 67 67 nameResolveTrigg
a710: 65 72 28 26 73 50 61 72 73 65 2c 20 28 62 54 65 er(&sParse, (bTe
a720: 6d 70 20 3f 20 30 20 3a 20 7a 44 62 29 29 3b 0a mp ? 0 : zDb));.
a730: 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
a740: 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 72 65 6e TE_OK ) goto ren
a750: 61 6d 65 43 6f 6c 75 6d 6e 46 75 6e 63 5f 64 6f ameColumnFunc_do
a760: 6e 65 3b 0a 0a 20 20 20 20 66 6f 72 28 70 53 74 ne;.. for(pSt
a770: 65 70 3d 73 50 61 72 73 65 2e 70 4e 65 77 54 72 ep=sParse.pNewTr
a780: 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74 igger->step_list
a790: 3b 20 70 53 74 65 70 3b 20 70 53 74 65 70 3d 70 ; pStep; pStep=p
a7a0: 53 74 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 Step->pNext){.
a7b0: 20 20 20 20 69 66 28 20 70 53 74 65 70 2d 3e 7a if( pStep->z
a7c0: 54 61 72 67 65 74 20 29 7b 20 0a 20 20 20 20 20 Target ){ .
a7d0: 20 20 20 54 61 62 6c 65 20 2a 70 54 61 72 67 65 Table *pTarge
a7e0: 74 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 t = sqlite3Locat
a7f0: 65 54 61 62 6c 65 28 26 73 50 61 72 73 65 2c 20 eTable(&sParse,
a800: 30 2c 20 70 53 74 65 70 2d 3e 7a 54 61 72 67 65 0, pStep->zTarge
a810: 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 t, zDb);.
a820: 20 69 66 28 20 70 54 61 72 67 65 74 3d 3d 70 54 if( pTarget==pT
a830: 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ab ){.
a840: 69 66 28 20 70 53 74 65 70 2d 3e 70 55 70 73 65 if( pStep->pUpse
a850: 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 rt ){.
a860: 20 20 45 78 70 72 4c 69 73 74 20 2a 70 55 70 73 ExprList *pUps
a870: 65 72 74 53 65 74 20 3d 20 70 53 74 65 70 2d 3e ertSet = pStep->
a880: 70 55 70 73 65 72 74 2d 3e 70 55 70 73 65 72 74 pUpsert->pUpsert
a890: 53 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 Set;.
a8a0: 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 45 6c 69 renameColumnEli
a8b0: 73 74 4e 61 6d 65 73 28 26 73 50 61 72 73 65 2c stNames(&sParse,
a8c0: 20 26 73 43 74 78 2c 20 70 55 70 73 65 72 74 53 &sCtx, pUpsertS
a8d0: 65 74 2c 20 7a 4f 6c 64 29 3b 0a 20 20 20 20 20 et, zOld);.
a8e0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
a8f0: 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 49 64 6c renameColumnIdl
a900: 69 73 74 4e 61 6d 65 73 28 26 73 50 61 72 73 65 istNames(&sParse
a910: 2c 20 26 73 43 74 78 2c 20 70 53 74 65 70 2d 3e , &sCtx, pStep->
a920: 70 49 64 4c 69 73 74 2c 20 7a 4f 6c 64 29 3b 0a pIdList, zOld);.
a930: 20 20 20 20 20 20 20 20 20 20 72 65 6e 61 6d 65 rename
a940: 43 6f 6c 75 6d 6e 45 6c 69 73 74 4e 61 6d 65 73 ColumnElistNames
a950: 28 26 73 50 61 72 73 65 2c 20 26 73 43 74 78 2c (&sParse, &sCtx,
a960: 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 pStep->pExprLis
a970: 74 2c 20 7a 4f 6c 64 29 3b 0a 20 20 20 20 20 20 t, zOld);.
a980: 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
a990: 7d 0a 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 }... /* Find
a9a0: 74 6f 6b 65 6e 73 20 74 6f 20 65 64 69 74 20 69 tokens to edit i
a9b0: 6e 20 55 50 44 41 54 45 20 4f 46 20 63 6c 61 75 n UPDATE OF clau
a9c0: 73 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 50 se */. if( sP
a9d0: 61 72 73 65 2e 70 54 72 69 67 67 65 72 54 61 62 arse.pTriggerTab
a9e0: 3d 3d 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 ==pTab ){.
a9f0: 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 49 64 6c 69 renameColumnIdli
aa00: 73 74 4e 61 6d 65 73 28 26 73 50 61 72 73 65 2c stNames(&sParse,
aa10: 20 26 73 43 74 78 2c 73 50 61 72 73 65 2e 70 4e &sCtx,sParse.pN
aa20: 65 77 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 ewTrigger->pColu
aa30: 6d 6e 73 2c 7a 4f 6c 64 29 3b 0a 20 20 20 20 7d mns,zOld);. }
aa40: 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 6f .. /* Find to
aa50: 6b 65 6e 73 20 74 6f 20 65 64 69 74 20 69 6e 20 kens to edit in
aa60: 76 61 72 69 6f 75 73 20 65 78 70 72 65 73 73 69 various expressi
aa70: 6f 6e 73 20 61 6e 64 20 73 65 6c 65 63 74 73 20 ons and selects
aa80: 2a 2f 0a 20 20 20 20 72 65 6e 61 6d 65 57 61 6c */. renameWal
aa90: 6b 54 72 69 67 67 65 72 28 26 73 57 61 6c 6b 65 kTrigger(&sWalke
aaa0: 72 2c 20 73 50 61 72 73 65 2e 70 4e 65 77 54 72 r, sParse.pNewTr
aab0: 69 67 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 61 igger);. }.. a
aac0: 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
aad0: 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 72 E_OK );. rc = r
aae0: 65 6e 61 6d 65 45 64 69 74 53 71 6c 28 63 6f 6e enameEditSql(con
aaf0: 74 65 78 74 2c 20 26 73 43 74 78 2c 20 7a 53 71 text, &sCtx, zSq
ab00: 6c 2c 20 7a 4e 65 77 2c 20 62 51 75 6f 74 65 29 l, zNew, bQuote)
ab10: 3b 0a 0a 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 46 ;..renameColumnF
ab20: 75 6e 63 5f 64 6f 6e 65 3a 0a 20 20 69 66 28 20 unc_done:. if(
ab30: 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
ab40: 0a 20 20 20 20 69 66 28 20 73 50 61 72 73 65 2e . if( sParse.
ab50: 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 zErrMsg ){.
ab60: 20 72 65 6e 61 6d 65 43 6f 6c 75 6d 6e 50 61 72 renameColumnPar
ab70: 73 65 45 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c seError(context,
ab80: 20 30 2c 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 0, argv[1], arg
ab90: 76 5b 32 5d 2c 20 26 73 50 61 72 73 65 29 3b 0a v[2], &sParse);.
aba0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
abb0: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
abc0: 65 72 72 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65 error_code(conte
abd0: 78 74 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 xt, rc);. }.
abe0: 20 7d 0a 0a 20 20 72 65 6e 61 6d 65 50 61 72 73 }.. renamePars
abf0: 65 43 6c 65 61 6e 75 70 28 26 73 50 61 72 73 65 eCleanup(&sParse
ac00: 29 3b 0a 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e );. renameToken
ac10: 46 72 65 65 28 64 62 2c 20 73 43 74 78 2e 70 4c Free(db, sCtx.pL
ac20: 69 73 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ist);.#ifndef SQ
ac30: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
ac40: 49 5a 41 54 49 4f 4e 0a 20 20 64 62 2d 3e 78 41 IZATION. db->xA
ac50: 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 23 65 6e uth = xAuth;.#en
ac60: 64 69 66 0a 20 20 73 71 6c 69 74 65 33 42 74 72 dif. sqlite3Btr
ac70: 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
ac80: 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 65 72 20 }../*.** Walker
ac90: 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6c 6c 62 expression callb
aca0: 61 63 6b 20 75 73 65 64 20 62 79 20 22 52 45 4e ack used by "REN
acb0: 41 4d 45 20 54 41 42 4c 45 22 2e 20 0a 2a 2f 0a AME TABLE". .*/.
acc0: 73 74 61 74 69 63 20 69 6e 74 20 72 65 6e 61 6d static int renam
acd0: 65 54 61 62 6c 65 45 78 70 72 43 62 28 57 61 6c eTableExprCb(Wal
ace0: 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 ker *pWalker, Ex
acf0: 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 52 65 pr *pExpr){. Re
ad00: 6e 61 6d 65 43 74 78 20 2a 70 20 3d 20 70 57 61 nameCtx *p = pWa
ad10: 6c 6b 65 72 2d 3e 75 2e 70 52 65 6e 61 6d 65 3b lker->u.pRename;
ad20: 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 . if( pExpr->op
ad30: 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 ==TK_COLUMN && p
ad40: 2d 3e 70 54 61 62 3d 3d 70 45 78 70 72 2d 3e 79 ->pTab==pExpr->y
ad50: 2e 70 54 61 62 20 29 7b 0a 20 20 20 20 72 65 6e .pTab ){. ren
ad60: 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 70 57 61 ameTokenFind(pWa
ad70: 6c 6b 65 72 2d 3e 70 50 61 72 73 65 2c 20 70 2c lker->pParse, p,
ad80: 20 28 76 6f 69 64 2a 29 26 70 45 78 70 72 2d 3e (void*)&pExpr->
ad90: 79 2e 70 54 61 62 29 3b 0a 20 20 7d 0a 20 20 72 y.pTab);. }. r
ada0: 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
adb0: 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c ue;.}../*.** Wal
adc0: 6b 65 72 20 73 65 6c 65 63 74 20 63 61 6c 6c 62 ker select callb
add0: 61 63 6b 20 75 73 65 64 20 62 79 20 22 52 45 4e ack used by "REN
ade0: 41 4d 45 20 54 41 42 4c 45 22 2e 20 0a 2a 2f 0a AME TABLE". .*/.
adf0: 73 74 61 74 69 63 20 69 6e 74 20 72 65 6e 61 6d static int renam
ae00: 65 54 61 62 6c 65 53 65 6c 65 63 74 43 62 28 57 eTableSelectCb(W
ae10: 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 alker *pWalker,
ae20: 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 Select *pSelect)
ae30: 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 52 65 6e {. int i;. Ren
ae40: 61 6d 65 43 74 78 20 2a 70 20 3d 20 70 57 61 6c ameCtx *p = pWal
ae50: 6b 65 72 2d 3e 75 2e 70 52 65 6e 61 6d 65 3b 0a ker->u.pRename;.
ae60: 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 SrcList *pSrc
ae70: 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b = pSelect->pSrc;
ae80: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 . for(i=0; i<pS
ae90: 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a rc->nSrc; i++){.
aea0: 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
aeb0: 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d st_item *pItem =
aec0: 20 26 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 &pSrc->a[i];.
aed0: 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 54 61 if( pItem->pTa
aee0: 62 3d 3d 70 2d 3e 70 54 61 62 20 29 7b 0a 20 20 b==p->pTab ){.
aef0: 20 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 renameTokenF
af00: 69 6e 64 28 70 57 61 6c 6b 65 72 2d 3e 70 50 61 ind(pWalker->pPa
af10: 72 73 65 2c 20 70 2c 20 70 49 74 65 6d 2d 3e 7a rse, p, pItem->z
af20: 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Name);. }. }
af30: 0a 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 .. return WRC_C
af40: 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 0a 2f 2a 0a ontinue;.}.../*.
af50: 2a 2a 20 54 68 69 73 20 43 20 66 75 6e 63 74 69 ** This C functi
af60: 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e on implements an
af70: 20 53 51 4c 20 75 73 65 72 20 66 75 6e 63 74 69 SQL user functi
af80: 6f 6e 20 74 68 61 74 20 69 73 20 75 73 65 64 20 on that is used
af90: 62 79 20 53 51 4c 20 63 6f 64 65 0a 2a 2a 20 67 by SQL code.** g
afa0: 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 enerated by the
afb0: 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 ALTER TABLE ...
afc0: 52 45 4e 41 4d 45 20 63 6f 6d 6d 61 6e 64 20 74 RENAME command t
afd0: 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 65 66 o modify the def
afe0: 69 6e 69 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 6e inition.** of an
aff0: 79 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f y foreign key co
b000: 6e 73 74 72 61 69 6e 74 73 20 74 68 61 74 20 75 nstraints that u
b010: 73 65 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 se the table bei
b020: 6e 67 20 72 65 6e 61 6d 65 64 20 61 73 20 74 68 ng renamed as th
b030: 65 20 0a 2a 2a 20 70 61 72 65 6e 74 20 74 61 62 e .** parent tab
b040: 6c 65 2e 20 49 74 20 69 73 20 70 61 73 73 65 64 le. It is passed
b050: 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 three arguments
b060: 3a 0a 2a 2a 0a 2a 2a 20 20 20 30 3a 20 54 68 65 :.**.** 0: The
b070: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
b080: 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 62 ning the table b
b090: 65 69 6e 67 20 72 65 6e 61 6d 65 64 2e 0a 2a 2a eing renamed..**
b0a0: 20 20 20 31 2e 20 74 79 70 65 3a 20 20 20 20 20 1. type:
b0b0: 54 79 70 65 20 6f 66 20 6f 62 6a 65 63 74 20 28 Type of object (
b0c0: 22 74 61 62 6c 65 22 2c 20 22 76 69 65 77 22 20 "table", "view"
b0d0: 65 74 63 2e 29 0a 2a 2a 20 20 20 32 2e 20 6f 62 etc.).** 2. ob
b0e0: 6a 65 63 74 3a 20 20 20 4e 61 6d 65 20 6f 66 20 ject: Name of
b0f0: 6f 62 6a 65 63 74 0a 2a 2a 20 20 20 33 3a 20 54 object.** 3: T
b100: 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 he complete text
b110: 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 73 of the schema s
b120: 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 6d tatement being m
b130: 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20 34 3a odified,.** 4:
b140: 20 54 68 65 20 6f 6c 64 20 6e 61 6d 65 20 6f 66 The old name of
b150: 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
b160: 20 72 65 6e 61 6d 65 64 2c 20 61 6e 64 0a 2a 2a renamed, and.**
b170: 20 20 20 35 3a 20 54 68 65 20 6e 65 77 20 6e 61 5: The new na
b180: 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
b190: 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 2e 0a 2a being renamed..*
b1a0: 2a 20 20 20 36 3a 20 54 72 75 65 20 69 66 20 74 * 6: True if t
b1b0: 68 65 20 73 63 68 65 6d 61 20 73 74 61 74 65 6d he schema statem
b1c0: 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 74 ent comes from t
b1d0: 68 65 20 74 65 6d 70 20 64 62 2e 0a 2a 2a 0a 2a he temp db..**.*
b1e0: 2a 20 49 74 20 72 65 74 75 72 6e 73 20 74 68 65 * It returns the
b1f0: 20 6e 65 77 20 73 63 68 65 6d 61 20 73 74 61 74 new schema stat
b200: 65 6d 65 6e 74 2e 20 46 6f 72 20 65 78 61 6d 70 ement. For examp
b210: 6c 65 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 le:.**.** sqlite
b220: 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 6d _rename_table('m
b230: 61 69 6e 27 2c 20 27 43 52 45 41 54 45 20 54 41 ain', 'CREATE TA
b240: 42 4c 45 20 74 31 28 61 20 52 45 46 45 52 45 4e BLE t1(a REFEREN
b250: 43 45 53 20 74 32 29 27 2c 27 74 32 27 2c 27 74 CES t2)','t2','t
b260: 33 27 2c 30 29 0a 2a 2a 20 20 20 20 20 20 20 2d 3',0).** -
b270: 3e 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 > 'CREATE TABLE
b280: 74 31 28 61 20 52 45 46 45 52 45 4e 43 45 53 20 t1(a REFERENCES
b290: 74 33 29 27 0a 2a 2f 0a 73 74 61 74 69 63 20 76 t3)'.*/.static v
b2a0: 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 oid renameTableF
b2b0: 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
b2c0: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
b2d0: 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a . int NotUsed,.
b2e0: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
b2f0: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 **argv.){. sqli
b300: 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 te3 *db = sqlite
b310: 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
b320: 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
b330: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 const char *zDb
b340: 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 = (const char*)s
b350: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
b360: 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f t(argv[0]);. co
b370: 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 nst char *zInput
b380: 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 = (const char*)
b390: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
b3a0: 78 74 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 63 xt(argv[3]);. c
b3b0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 6c 64 20 onst char *zOld
b3c0: 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 = (const char*)s
b3d0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
b3e0: 74 28 61 72 67 76 5b 34 5d 29 3b 0a 20 20 63 6f t(argv[4]);. co
b3f0: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d nst char *zNew =
b400: 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 (const char*)sq
b410: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
b420: 28 61 72 67 76 5b 35 5d 29 3b 0a 20 20 69 6e 74 (argv[5]);. int
b430: 20 62 54 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 bTemp = sqlite3
b440: 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b _value_int(argv[
b450: 36 5d 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 6]);. UNUSED_PA
b460: 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
b470: 3b 0a 0a 20 20 69 66 28 20 7a 49 6e 70 75 74 20 ;.. if( zInput
b480: 26 26 20 7a 4f 6c 64 20 26 26 20 7a 4e 65 77 20 && zOld && zNew
b490: 29 7b 0a 20 20 20 20 50 61 72 73 65 20 73 50 61 ){. Parse sPa
b4a0: 72 73 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b rse;. int rc;
b4b0: 0a 20 20 20 20 69 6e 74 20 62 51 75 6f 74 65 20 . int bQuote
b4c0: 3d 20 31 3b 0a 20 20 20 20 52 65 6e 61 6d 65 43 = 1;. RenameC
b4d0: 74 78 20 73 43 74 78 3b 0a 20 20 20 20 57 61 6c tx sCtx;. Wal
b4e0: 6b 65 72 20 73 57 61 6c 6b 65 72 3b 0a 0a 23 69 ker sWalker;..#i
b4f0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
b500: 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a T_AUTHORIZATION.
b510: 20 20 20 20 73 71 6c 69 74 65 33 5f 78 61 75 74 sqlite3_xaut
b520: 68 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 h xAuth = db->xA
b530: 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41 75 uth;. db->xAu
b540: 74 68 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a th = 0;.#endif..
b550: 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
b560: 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 0a 20 EnterAll(db);..
b570: 20 20 20 6d 65 6d 73 65 74 28 26 73 43 74 78 2c memset(&sCtx,
b580: 20 30 2c 20 73 69 7a 65 6f 66 28 52 65 6e 61 6d 0, sizeof(Renam
b590: 65 43 74 78 29 29 3b 0a 20 20 20 20 73 43 74 78 eCtx));. sCtx
b5a0: 2e 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 .pTab = sqlite3F
b5b0: 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4f 6c indTable(db, zOl
b5c0: 64 2c 20 7a 44 62 29 3b 0a 20 20 20 20 6d 65 6d d, zDb);. mem
b5d0: 73 65 74 28 26 73 57 61 6c 6b 65 72 2c 20 30 2c set(&sWalker, 0,
b5e0: 20 73 69 7a 65 6f 66 28 57 61 6c 6b 65 72 29 29 sizeof(Walker))
b5f0: 3b 0a 20 20 20 20 73 57 61 6c 6b 65 72 2e 70 50 ;. sWalker.pP
b600: 61 72 73 65 20 3d 20 26 73 50 61 72 73 65 3b 0a arse = &sParse;.
b610: 20 20 20 20 73 57 61 6c 6b 65 72 2e 78 45 78 70 sWalker.xExp
b620: 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 6e 61 rCallback = rena
b630: 6d 65 54 61 62 6c 65 45 78 70 72 43 62 3b 0a 20 meTableExprCb;.
b640: 20 20 20 73 57 61 6c 6b 65 72 2e 78 53 65 6c 65 sWalker.xSele
b650: 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 6e ctCallback = ren
b660: 61 6d 65 54 61 62 6c 65 53 65 6c 65 63 74 43 62 ameTableSelectCb
b670: 3b 0a 20 20 20 20 73 57 61 6c 6b 65 72 2e 75 2e ;. sWalker.u.
b680: 70 52 65 6e 61 6d 65 20 3d 20 26 73 43 74 78 3b pRename = &sCtx;
b690: 0a 0a 20 20 20 20 72 63 20 3d 20 72 65 6e 61 6d .. rc = renam
b6a0: 65 50 61 72 73 65 53 71 6c 28 26 73 50 61 72 73 eParseSql(&sPars
b6b0: 65 2c 20 7a 44 62 2c 20 31 2c 20 64 62 2c 20 7a e, zDb, 1, db, z
b6c0: 49 6e 70 75 74 2c 20 62 54 65 6d 70 29 3b 0a 0a Input, bTemp);..
b6d0: 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
b6e0: 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 TE_OK ){. i
b6f0: 6e 74 20 69 73 4c 65 67 61 63 79 20 3d 20 28 64 nt isLegacy = (d
b700: 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 b->flags & SQLIT
b710: 45 5f 4c 65 67 61 63 79 41 6c 74 65 72 29 3b 0a E_LegacyAlter);.
b720: 20 20 20 20 20 20 69 66 28 20 73 50 61 72 73 65 if( sParse
b730: 2e 70 4e 65 77 54 61 62 6c 65 20 29 7b 0a 20 20 .pNewTable ){.
b740: 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 Table *pTa
b750: 62 20 3d 20 73 50 61 72 73 65 2e 70 4e 65 77 54 b = sParse.pNewT
b760: 61 62 6c 65 3b 0a 0a 20 20 20 20 20 20 20 20 69 able;.. i
b770: 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 f( pTab->pSelect
b780: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
b790: 28 20 69 73 4c 65 67 61 63 79 3d 3d 30 20 29 7b ( isLegacy==0 ){
b7a0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 4e 61 6d . Nam
b7b0: 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 eContext sNC;.
b7c0: 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 memset
b7d0: 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 (&sNC, 0, sizeof
b7e0: 28 73 4e 43 29 29 3b 0a 20 20 20 20 20 20 20 20 (sNC));.
b7f0: 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d sNC.pParse =
b800: 20 26 73 50 61 72 73 65 3b 0a 0a 20 20 20 20 20 &sParse;..
b810: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 sqlite3Se
b820: 6c 65 63 74 50 72 65 70 28 26 73 50 61 72 73 65 lectPrep(&sParse
b830: 2c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c , pTab->pSelect,
b840: 20 26 73 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 &sNC);.
b850: 20 20 20 20 69 66 28 20 73 50 61 72 73 65 2e 6e if( sParse.n
b860: 45 72 72 20 29 20 72 63 20 3d 20 73 50 61 72 73 Err ) rc = sPars
b870: 65 2e 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 e.rc;.
b880: 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c sqlite3WalkSel
b890: 65 63 74 28 26 73 57 61 6c 6b 65 72 2c 20 70 54 ect(&sWalker, pT
b8a0: 61 62 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 ab->pSelect);.
b8b0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
b8c0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
b8d0: 20 20 20 2f 2a 20 4d 6f 64 69 66 79 20 61 6e 79 /* Modify any
b8e0: 20 46 4b 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 FK definitions
b8f0: 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
b900: 6e 65 77 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 69 new table. */.#i
b910: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
b920: 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 T_FOREIGN_KEY.
b930: 20 20 20 20 20 20 20 20 69 66 28 20 69 73 4c 65 if( isLe
b940: 67 61 63 79 3d 3d 30 20 7c 7c 20 28 64 62 2d 3e gacy==0 || (db->
b950: 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 flags & SQLITE_F
b960: 6f 72 65 69 67 6e 4b 65 79 73 29 20 29 7b 0a 20 oreignKeys) ){.
b970: 20 20 20 20 20 20 20 20 20 20 20 46 4b 65 79 20 FKey
b980: 2a 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 *pFKey;.
b990: 20 20 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 for(pFKey=pT
b9a0: 61 62 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 ab->pFKey; pFKey
b9b0: 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 ; pFKey=pFKey->p
b9c0: 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 NextFrom){.
b9d0: 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
b9e0: 69 74 65 33 5f 73 74 72 69 63 6d 70 28 70 46 4b ite3_stricmp(pFK
b9f0: 65 79 2d 3e 7a 54 6f 2c 20 7a 4f 6c 64 29 3d 3d ey->zTo, zOld)==
ba00: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
ba10: 20 20 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e renameToken
ba20: 46 69 6e 64 28 26 73 50 61 72 73 65 2c 20 26 73 Find(&sParse, &s
ba30: 43 74 78 2c 20 28 76 6f 69 64 2a 29 70 46 4b 65 Ctx, (void*)pFKe
ba40: 79 2d 3e 7a 54 6f 29 3b 0a 20 20 20 20 20 20 20 y->zTo);.
ba50: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
ba60: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
ba70: 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 }.#endif..
ba80: 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 /* If this
ba90: 69 73 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 is the table bei
baa0: 6e 67 20 61 6c 74 65 72 65 64 2c 20 66 69 78 20 ng altered, fix
bab0: 61 6e 79 20 74 61 62 6c 65 20 72 65 66 73 20 69 any table refs i
bac0: 6e 20 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 n CHECK.
bad0: 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 ** expressions
bae0: 2e 20 41 6c 73 6f 20 75 70 64 61 74 65 20 74 68 . Also update th
baf0: 65 20 6e 61 6d 65 20 74 68 61 74 20 61 70 70 65 e name that appe
bb00: 61 72 73 20 72 69 67 68 74 20 61 66 74 65 72 20 ars right after
bb10: 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a the. **
bb20: 20 22 43 52 45 41 54 45 20 5b 56 49 52 54 55 41 "CREATE [VIRTUA
bb30: 4c 5d 20 54 41 42 4c 45 22 20 62 69 74 2e 20 2a L] TABLE" bit. *
bb40: 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 /. if(
bb50: 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 sqlite3_stricmp(
bb60: 7a 4f 6c 64 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d zOld, pTab->zNam
bb70: 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 e)==0 ){.
bb80: 20 20 20 20 20 73 43 74 78 2e 70 54 61 62 20 3d sCtx.pTab =
bb90: 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 pTab;.
bba0: 20 20 20 69 66 28 20 69 73 4c 65 67 61 63 79 3d if( isLegacy=
bbb0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
bbc0: 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 sqlite3WalkE
bbd0: 78 70 72 4c 69 73 74 28 26 73 57 61 6c 6b 65 72 xprList(&sWalker
bbe0: 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 29 3b , pTab->pCheck);
bbf0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
bc00: 20 20 20 20 20 20 20 20 20 20 20 72 65 6e 61 6d renam
bc10: 65 54 6f 6b 65 6e 46 69 6e 64 28 26 73 50 61 72 eTokenFind(&sPar
bc20: 73 65 2c 20 26 73 43 74 78 2c 20 70 54 61 62 2d se, &sCtx, pTab-
bc30: 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 >zName);.
bc40: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
bc50: 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 65 6c }.. el
bc60: 73 65 20 69 66 28 20 73 50 61 72 73 65 2e 70 4e se if( sParse.pN
bc70: 65 77 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 ewIndex ){.
bc80: 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 renameTokenFi
bc90: 6e 64 28 26 73 50 61 72 73 65 2c 20 26 73 43 74 nd(&sParse, &sCt
bca0: 78 2c 20 73 50 61 72 73 65 2e 70 4e 65 77 49 6e x, sParse.pNewIn
bcb0: 64 65 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 dex->zName);.
bcc0: 20 20 20 20 20 69 66 28 20 69 73 4c 65 67 61 63 if( isLegac
bcd0: 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 y==0 ){.
bce0: 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 sqlite3WalkExp
bcf0: 72 28 26 73 57 61 6c 6b 65 72 2c 20 73 50 61 72 r(&sWalker, sPar
bd00: 73 65 2e 70 4e 65 77 49 6e 64 65 78 2d 3e 70 50 se.pNewIndex->pP
bd10: 61 72 74 49 64 78 57 68 65 72 65 29 3b 0a 20 20 artIdxWhere);.
bd20: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
bd30: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
bd40: 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 OMIT_TRIGGER.
bd50: 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 else{.
bd60: 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 Trigger *pTrigg
bd70: 65 72 20 3d 20 73 50 61 72 73 65 2e 70 4e 65 77 er = sParse.pNew
bd80: 54 72 69 67 67 65 72 3b 0a 20 20 20 20 20 20 20 Trigger;.
bd90: 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 TriggerStep *pS
bda0: 74 65 70 3b 0a 20 20 20 20 20 20 20 20 69 66 28 tep;. if(
bdb0: 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72 69 0==sqlite3_stri
bdc0: 63 6d 70 28 73 50 61 72 73 65 2e 70 4e 65 77 54 cmp(sParse.pNewT
bdd0: 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20 7a rigger->table, z
bde0: 4f 6c 64 29 20 0a 20 20 20 20 20 20 20 20 20 20 Old) .
bdf0: 20 20 26 26 20 73 43 74 78 2e 70 54 61 62 2d 3e && sCtx.pTab->
be00: 70 53 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 pSchema==pTrigge
be10: 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 0a 20 20 r->pTabSchema.
be20: 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
be30: 20 20 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 6e renameToken
be40: 46 69 6e 64 28 26 73 50 61 72 73 65 2c 20 26 73 Find(&sParse, &s
be50: 43 74 78 2c 20 73 50 61 72 73 65 2e 70 4e 65 77 Ctx, sParse.pNew
be60: 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b Trigger->table);
be70: 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 . }..
be80: 20 20 20 20 69 66 28 20 69 73 4c 65 67 61 63 79 if( isLegacy
be90: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
bea0: 20 72 63 20 3d 20 72 65 6e 61 6d 65 52 65 73 6f rc = renameReso
beb0: 6c 76 65 54 72 69 67 67 65 72 28 26 73 50 61 72 lveTrigger(&sPar
bec0: 73 65 2c 20 62 54 65 6d 70 20 3f 20 30 20 3a 20 se, bTemp ? 0 :
bed0: 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 zDb);.
bee0: 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
bef0: 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
bf00: 20 72 65 6e 61 6d 65 57 61 6c 6b 54 72 69 67 67 renameWalkTrigg
bf10: 65 72 28 26 73 57 61 6c 6b 65 72 2c 20 70 54 72 er(&sWalker, pTr
bf20: 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 igger);.
bf30: 20 20 20 20 66 6f 72 28 70 53 74 65 70 3d 70 54 for(pStep=pT
bf40: 72 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 rigger->step_lis
bf50: 74 3b 20 70 53 74 65 70 3b 20 70 53 74 65 70 3d t; pStep; pStep=
bf60: 70 53 74 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 pStep->pNext){.
bf70: 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 if(
bf80: 20 70 53 74 65 70 2d 3e 7a 54 61 72 67 65 74 20 pStep->zTarget
bf90: 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74 && 0==sqlite3_st
bfa0: 72 69 63 6d 70 28 70 53 74 65 70 2d 3e 7a 54 61 ricmp(pStep->zTa
bfb0: 72 67 65 74 2c 20 7a 4f 6c 64 29 20 29 7b 0a 20 rget, zOld) ){.
bfc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 r
bfd0: 65 6e 61 6d 65 54 6f 6b 65 6e 46 69 6e 64 28 26 enameTokenFind(&
bfe0: 73 50 61 72 73 65 2c 20 26 73 43 74 78 2c 20 70 sParse, &sCtx, p
bff0: 53 74 65 70 2d 3e 7a 54 61 72 67 65 74 29 3b 0a Step->zTarget);.
c000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }.
c010: 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
c020: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
c030: 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 }. }.#end
c040: 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 if. }.. if
c050: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
c060: 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 ){. rc = re
c070: 6e 61 6d 65 45 64 69 74 53 71 6c 28 63 6f 6e 74 nameEditSql(cont
c080: 65 78 74 2c 20 26 73 43 74 78 2c 20 7a 49 6e 70 ext, &sCtx, zInp
c090: 75 74 2c 20 7a 4e 65 77 2c 20 62 51 75 6f 74 65 ut, zNew, bQuote
c0a0: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
c0b0: 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
c0c0: 7b 0a 20 20 20 20 20 20 69 66 28 20 73 50 61 72 {. if( sPar
c0d0: 73 65 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 se.zErrMsg ){.
c0e0: 20 20 20 20 20 20 72 65 6e 61 6d 65 43 6f 6c 75 renameColu
c0f0: 6d 6e 50 61 72 73 65 45 72 72 6f 72 28 63 6f 6e mnParseError(con
c100: 74 65 78 74 2c 20 30 2c 20 61 72 67 76 5b 31 5d text, 0, argv[1]
c110: 2c 20 61 72 67 76 5b 32 5d 2c 20 26 73 50 61 72 , argv[2], &sPar
c120: 73 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 se);. }else
c130: 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
c140: 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
c150: 6f 64 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 ode(context, rc)
c160: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
c170: 0a 20 20 20 20 72 65 6e 61 6d 65 50 61 72 73 65 . renameParse
c180: 43 6c 65 61 6e 75 70 28 26 73 50 61 72 73 65 29 Cleanup(&sParse)
c190: 3b 0a 20 20 20 20 72 65 6e 61 6d 65 54 6f 6b 65 ;. renameToke
c1a0: 6e 46 72 65 65 28 64 62 2c 20 73 43 74 78 2e 70 nFree(db, sCtx.p
c1b0: 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 List);. sqlit
c1c0: 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
c1d0: 64 62 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c db);.#ifndef SQL
c1e0: 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 ITE_OMIT_AUTHORI
c1f0: 5a 41 54 49 4f 4e 0a 20 20 20 20 64 62 2d 3e 78 ZATION. db->x
c200: 41 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 23 65 Auth = xAuth;.#e
c210: 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ndif. }.. retu
c220: 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 rn;.}../*.** An
c230: 53 51 4c 20 75 73 65 72 20 66 75 6e 63 74 69 6f SQL user functio
c240: 6e 20 74 68 61 74 20 63 68 65 63 6b 73 20 74 68 n that checks th
c250: 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 at there are no
c260: 70 61 72 73 65 20 6f 72 20 73 79 6d 62 6f 6c 0a parse or symbol.
c270: 2a 2a 20 72 65 73 6f 6c 75 74 69 6f 6e 20 70 72 ** resolution pr
c280: 6f 62 6c 65 6d 73 20 69 6e 20 61 20 43 52 45 41 oblems in a CREA
c290: 54 45 20 54 52 49 47 47 45 52 7c 54 41 42 4c 45 TE TRIGGER|TABLE
c2a0: 7c 56 49 45 57 7c 49 4e 44 45 58 20 73 74 61 74 |VIEW|INDEX stat
c2b0: 65 6d 65 6e 74 2e 0a 2a 2a 20 41 66 74 65 72 20 ement..** After
c2c0: 61 6e 20 41 4c 54 45 52 20 54 41 42 4c 45 20 2e an ALTER TABLE .
c2d0: 2e 20 52 45 4e 41 4d 45 20 6f 70 65 72 61 74 69 . RENAME operati
c2e0: 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 on is performed
c2f0: 61 6e 64 20 74 68 65 20 73 63 68 65 6d 61 0a 2a and the schema.*
c300: 2a 20 72 65 6c 6f 61 64 65 64 2c 20 74 68 69 73 * reloaded, this
c310: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
c320: 6c 65 64 20 6f 6e 20 65 61 63 68 20 53 51 4c 20 led on each SQL
c330: 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 statement in the
c340: 20 73 63 68 65 6d 61 0a 2a 2a 20 74 6f 20 65 6e schema.** to en
c350: 73 75 72 65 20 74 68 61 74 20 69 74 20 69 73 20 sure that it is
c360: 73 74 69 6c 6c 20 75 73 61 62 6c 65 2e 0a 2a 2a still usable..**
c370: 0a 2a 2a 20 20 20 30 3a 20 44 61 74 61 62 61 73 .** 0: Databas
c380: 65 20 6e 61 6d 65 20 28 22 6d 61 69 6e 22 2c 20 e name ("main",
c390: 22 74 65 6d 70 22 20 65 74 63 2e 29 2e 0a 2a 2a "temp" etc.)..**
c3a0: 20 20 20 31 3a 20 53 51 4c 20 73 74 61 74 65 6d 1: SQL statem
c3b0: 65 6e 74 2e 0a 2a 2a 20 20 20 32 3a 20 4f 62 6a ent..** 2: Obj
c3c0: 65 63 74 20 74 79 70 65 20 28 22 76 69 65 77 22 ect type ("view"
c3d0: 2c 20 22 74 61 62 6c 65 22 2c 20 22 74 72 69 67 , "table", "trig
c3e0: 67 65 72 22 20 6f 72 20 22 69 6e 64 65 78 22 29 ger" or "index")
c3f0: 2e 0a 2a 2a 20 20 20 33 3a 20 4f 62 6a 65 63 74 ..** 3: Object
c400: 20 6e 61 6d 65 2e 0a 2a 2a 20 20 20 34 3a 20 54 name..** 4: T
c410: 72 75 65 20 69 66 20 6f 62 6a 65 63 74 20 69 73 rue if object is
c420: 20 66 72 6f 6d 20 74 65 6d 70 20 73 63 68 65 6d from temp schem
c430: 61 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 a..**.** Unless
c440: 69 74 20 66 69 6e 64 73 20 61 6e 20 65 72 72 6f it finds an erro
c450: 72 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e r, this function
c460: 20 6e 6f 72 6d 61 6c 6c 79 20 72 65 74 75 72 6e normally return
c470: 73 20 4e 55 4c 4c 2e 20 48 6f 77 65 76 65 72 2c s NULL. However,
c480: 20 69 74 0a 2a 2a 20 72 65 74 75 72 6e 73 20 69 it.** returns i
c490: 6e 74 65 67 65 72 20 76 61 6c 75 65 20 31 20 69 nteger value 1 i
c4a0: 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 f:.**.** * the
c4b0: 20 53 51 4c 20 61 72 67 75 6d 65 6e 74 20 63 72 SQL argument cr
c4c0: 65 61 74 65 73 20 61 20 74 72 69 67 67 65 72 2c eates a trigger,
c4d0: 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20 and.** * the
c4e0: 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 74 table that the t
c4f0: 72 69 67 67 65 72 20 69 73 20 61 74 74 61 63 68 rigger is attach
c500: 65 64 20 74 6f 20 69 73 20 69 6e 20 64 61 74 61 ed to is in data
c510: 62 61 73 65 20 7a 44 62 2e 0a 2a 2f 0a 73 74 61 base zDb..*/.sta
c520: 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 tic void renameT
c530: 61 62 6c 65 54 65 73 74 28 0a 20 20 73 71 6c 69 ableTest(. sqli
c540: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
c550: 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 text,. int NotU
c560: 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 sed,. sqlite3_v
c570: 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
c580: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 sqlite3 *db = s
c590: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
c5a0: 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
c5b0: 29 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 );. char const
c5c0: 2a 7a 44 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 *zDb = (const ch
c5d0: 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 ar*)sqlite3_valu
c5e0: 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
c5f0: 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a . char const *z
c600: 49 6e 70 75 74 20 3d 20 28 63 6f 6e 73 74 20 63 Input = (const c
c610: 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
c620: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 ue_text(argv[1])
c630: 3b 0a 20 20 69 6e 74 20 62 54 65 6d 70 20 3d 20 ;. int bTemp =
c640: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
c650: 74 28 61 72 67 76 5b 34 5d 29 3b 0a 20 20 69 6e t(argv[4]);. in
c660: 74 20 69 73 4c 65 67 61 63 79 20 3d 20 28 64 62 t isLegacy = (db
c670: 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
c680: 5f 4c 65 67 61 63 79 41 6c 74 65 72 29 3b 0a 0a _LegacyAlter);..
c690: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
c6a0: 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
c6b0: 4e 0a 20 20 73 71 6c 69 74 65 33 5f 78 61 75 74 N. sqlite3_xaut
c6c0: 68 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 h xAuth = db->xA
c6d0: 75 74 68 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68 uth;. db->xAuth
c6e0: 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 = 0;.#endif..
c6f0: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
c700: 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 (NotUsed);. if(
c710: 20 7a 44 62 20 26 26 20 7a 49 6e 70 75 74 20 29 zDb && zInput )
c720: 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 {. int rc;.
c730: 20 20 50 61 72 73 65 20 73 50 61 72 73 65 3b 0a Parse sParse;.
c740: 20 20 20 20 72 63 20 3d 20 72 65 6e 61 6d 65 50 rc = renameP
c750: 61 72 73 65 53 71 6c 28 26 73 50 61 72 73 65 2c arseSql(&sParse,
c760: 20 7a 44 62 2c 20 31 2c 20 64 62 2c 20 7a 49 6e zDb, 1, db, zIn
c770: 70 75 74 2c 20 62 54 65 6d 70 29 3b 0a 20 20 20 put, bTemp);.
c780: 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
c790: 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 OK ){. if(
c7a0: 69 73 4c 65 67 61 63 79 3d 3d 30 20 26 26 20 73 isLegacy==0 && s
c7b0: 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 20 Parse.pNewTable
c7c0: 26 26 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 && sParse.pNewTa
c7d0: 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a ble->pSelect ){.
c7e0: 20 20 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 NameCont
c7f0: 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 20 ext sNC;.
c800: 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c memset(&sNC, 0,
c810: 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 sizeof(sNC));.
c820: 20 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 sNC.pPars
c830: 65 20 3d 20 26 73 50 61 72 73 65 3b 0a 20 20 20 e = &sParse;.
c840: 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
c850: 63 74 50 72 65 70 28 26 73 50 61 72 73 65 2c 20 ctPrep(&sParse,
c860: 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 sParse.pNewTable
c870: 2d 3e 70 53 65 6c 65 63 74 2c 20 26 73 4e 43 29 ->pSelect, &sNC)
c880: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 50 ;. if( sP
c890: 61 72 73 65 2e 6e 45 72 72 20 29 20 72 63 20 3d arse.nErr ) rc =
c8a0: 20 73 50 61 72 73 65 2e 72 63 3b 0a 20 20 20 20 sParse.rc;.
c8b0: 20 20 7d 0a 0a 20 20 20 20 20 20 65 6c 73 65 20 }.. else
c8c0: 69 66 28 20 73 50 61 72 73 65 2e 70 4e 65 77 54 if( sParse.pNewT
c8d0: 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 rigger ){.
c8e0: 20 20 69 66 28 20 69 73 4c 65 67 61 63 79 3d 3d if( isLegacy==
c8f0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
c900: 63 20 3d 20 72 65 6e 61 6d 65 52 65 73 6f 6c 76 c = renameResolv
c910: 65 54 72 69 67 67 65 72 28 26 73 50 61 72 73 65 eTrigger(&sParse
c920: 2c 20 62 54 65 6d 70 20 3f 20 30 20 3a 20 7a 44 , bTemp ? 0 : zD
c930: 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 b);. }.
c940: 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
c950: 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
c960: 20 20 20 20 20 69 6e 74 20 69 31 20 3d 20 73 71 int i1 = sq
c970: 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
c980: 65 78 32 28 64 62 2c 20 73 50 61 72 73 65 2e 70 ex2(db, sParse.p
c990: 4e 65 77 54 72 69 67 67 65 72 2d 3e 70 54 61 62 NewTrigger->pTab
c9a0: 53 63 68 65 6d 61 2c 20 30 29 3b 0a 20 20 20 20 Schema, 0);.
c9b0: 20 20 20 20 20 20 69 6e 74 20 69 32 20 3d 20 73 int i2 = s
c9c0: 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 qlite3FindDbName
c9d0: 28 64 62 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 (db, zDb);.
c9e0: 20 20 20 20 20 69 66 28 20 69 31 3d 3d 69 32 20 if( i1==i2
c9f0: 29 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 ) sqlite3_result
ca00: 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 31 29 _int(context, 1)
ca10: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
ca20: 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 }. }.. i
ca30: 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
ca40: 20 29 7b 0a 20 20 20 20 20 20 72 65 6e 61 6d 65 ){. rename
ca50: 43 6f 6c 75 6d 6e 50 61 72 73 65 45 72 72 6f 72 ColumnParseError
ca60: 28 63 6f 6e 74 65 78 74 2c 20 31 2c 20 61 72 67 (context, 1, arg
ca70: 76 5b 32 5d 2c 20 61 72 67 76 5b 33 5d 2c 20 26 v[2], argv[3], &
ca80: 73 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 sParse);. }.
ca90: 20 20 20 72 65 6e 61 6d 65 50 61 72 73 65 43 6c renameParseCl
caa0: 65 61 6e 75 70 28 26 73 50 61 72 73 65 29 3b 0a eanup(&sParse);.
cab0: 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c }..#ifndef SQL
cac0: 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 ITE_OMIT_AUTHORI
cad0: 5a 41 54 49 4f 4e 0a 20 20 64 62 2d 3e 78 41 75 ZATION. db->xAu
cae0: 74 68 20 3d 20 78 41 75 74 68 3b 0a 23 65 6e 64 th = xAuth;.#end
caf0: 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 if.}../*.** Regi
cb00: 73 74 65 72 20 62 75 69 6c 74 2d 69 6e 20 66 75 ster built-in fu
cb10: 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 nctions used to
cb20: 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 41 help implement A
cb30: 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2f 0a 76 6f LTER TABLE.*/.vo
cb40: 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 id sqlite3AlterF
cb50: 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a unctions(void){.
cb60: 20 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 static FuncDef
cb70: 20 61 41 6c 74 65 72 54 61 62 6c 65 46 75 6e 63 aAlterTableFunc
cb80: 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 46 55 4e 43 s[] = {. FUNC
cb90: 54 49 4f 4e 28 73 71 6c 69 74 65 5f 72 65 6e 61 TION(sqlite_rena
cba0: 6d 65 5f 63 6f 6c 75 6d 6e 2c 20 20 39 2c 20 30 me_column, 9, 0
cbb0: 2c 20 30 2c 20 72 65 6e 61 6d 65 43 6f 6c 75 6d , 0, renameColum
cbc0: 6e 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43 nFunc),. FUNC
cbd0: 54 49 4f 4e 28 73 71 6c 69 74 65 5f 72 65 6e 61 TION(sqlite_rena
cbe0: 6d 65 5f 74 61 62 6c 65 2c 20 20 37 2c 20 30 2c me_table, 7, 0,
cbf0: 20 30 2c 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 0, renameTableF
cc00: 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 unc),. FUNCTI
cc10: 4f 4e 28 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 ON(sqlite_rename
cc20: 5f 74 65 73 74 2c 20 20 35 2c 20 30 2c 20 30 2c _test, 5, 0, 0,
cc30: 20 72 65 6e 61 6d 65 54 61 62 6c 65 54 65 73 74 renameTableTest
cc40: 29 2c 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 ),. };. sqlite
cc50: 33 49 6e 73 65 72 74 42 75 69 6c 74 69 6e 46 75 3InsertBuiltinFu
cc60: 6e 63 73 28 61 41 6c 74 65 72 54 61 62 6c 65 46 ncs(aAlterTableF
cc70: 75 6e 63 73 2c 20 41 72 72 61 79 53 69 7a 65 28 uncs, ArraySize(
cc80: 61 41 6c 74 65 72 54 61 62 6c 65 46 75 6e 63 73 aAlterTableFuncs
cc90: 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a ));.}.#endif /*
cca0: 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 SQLITE_ALTER_TA
ccb0: 42 4c 45 20 2a 2f 0a BLE */.