0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 31 39 39 39 2c 20 32 30 30 30 20 44 (c) 1999, 2000 D
0020: 2e 20 52 69 63 68 61 72 64 20 48 69 70 70 0a 2a . Richard Hipp.*
0030: 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 67 72 61 *.** This progra
0040: 6d 20 69 73 20 66 72 65 65 20 73 6f 66 74 77 61 m is free softwa
0050: 72 65 3b 20 79 6f 75 20 63 61 6e 20 72 65 64 69 re; you can redi
0060: 73 74 72 69 62 75 74 65 20 69 74 20 61 6e 64 2f stribute it and/
0070: 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 69 74 20 or.** modify it
0080: 75 6e 64 65 72 20 74 68 65 20 74 65 72 6d 73 20 under the terms
0090: 6f 66 20 74 68 65 20 47 4e 55 20 47 65 6e 65 72 of the GNU Gener
00a0: 61 6c 20 50 75 62 6c 69 63 0a 2a 2a 20 4c 69 63 al Public.** Lic
00b0: 65 6e 73 65 20 61 73 20 70 75 62 6c 69 73 68 65 ense as publishe
00c0: 64 20 62 79 20 74 68 65 20 46 72 65 65 20 53 6f d by the Free So
00d0: 66 74 77 61 72 65 20 46 6f 75 6e 64 61 74 69 6f ftware Foundatio
00e0: 6e 3b 20 65 69 74 68 65 72 0a 2a 2a 20 76 65 72 n; either.** ver
00f0: 73 69 6f 6e 20 32 20 6f 66 20 74 68 65 20 4c 69 sion 2 of the Li
0100: 63 65 6e 73 65 2c 20 6f 72 20 28 61 74 20 79 6f cense, or (at yo
0110: 75 72 20 6f 70 74 69 6f 6e 29 20 61 6e 79 20 6c ur option) any l
0120: 61 74 65 72 20 76 65 72 73 69 6f 6e 2e 0a 2a 2a ater version..**
0130: 0a 2a 2a 20 54 68 69 73 20 70 72 6f 67 72 61 6d .** This program
0140: 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64 20 is distributed
0150: 69 6e 20 74 68 65 20 68 6f 70 65 20 74 68 61 74 in the hope that
0160: 20 69 74 20 77 69 6c 6c 20 62 65 20 75 73 65 66 it will be usef
0170: 75 6c 2c 0a 2a 2a 20 62 75 74 20 57 49 54 48 4f ul,.** but WITHO
0180: 55 54 20 41 4e 59 20 57 41 52 52 41 4e 54 59 3b UT ANY WARRANTY;
0190: 20 77 69 74 68 6f 75 74 20 65 76 65 6e 20 74 68 without even th
01a0: 65 20 69 6d 70 6c 69 65 64 20 77 61 72 72 61 6e e implied warran
01b0: 74 79 20 6f 66 0a 2a 2a 20 4d 45 52 43 48 41 4e ty of.** MERCHAN
01c0: 54 41 42 49 4c 49 54 59 20 6f 72 20 46 49 54 4e TABILITY or FITN
01d0: 45 53 53 20 46 4f 52 20 41 20 50 41 52 54 49 43 ESS FOR A PARTIC
01e0: 55 4c 41 52 20 50 55 52 50 4f 53 45 2e 20 20 53 ULAR PURPOSE. S
01f0: 65 65 20 74 68 65 20 47 4e 55 0a 2a 2a 20 47 65 ee the GNU.** Ge
0200: 6e 65 72 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 neral Public Lic
0210: 65 6e 73 65 20 66 6f 72 20 6d 6f 72 65 20 64 65 ense for more de
0220: 74 61 69 6c 73 2e 0a 2a 2a 20 0a 2a 2a 20 59 6f tails..** .** Yo
0230: 75 20 73 68 6f 75 6c 64 20 68 61 76 65 20 72 65 u should have re
0240: 63 65 69 76 65 64 20 61 20 63 6f 70 79 20 6f 66 ceived a copy of
0250: 20 74 68 65 20 47 4e 55 20 47 65 6e 65 72 61 6c the GNU General
0260: 20 50 75 62 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e Public.** Licen
0270: 73 65 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 se along with th
0280: 69 73 20 6c 69 62 72 61 72 79 3b 20 69 66 20 6e is library; if n
0290: 6f 74 2c 20 77 72 69 74 65 20 74 6f 20 74 68 65 ot, write to the
02a0: 0a 2a 2a 20 46 72 65 65 20 53 6f 66 74 77 61 72 .** Free Softwar
02b0: 65 20 46 6f 75 6e 64 61 74 69 6f 6e 2c 20 49 6e e Foundation, In
02c0: 63 2e 2c 20 35 39 20 54 65 6d 70 6c 65 20 50 6c c., 59 Temple Pl
02d0: 61 63 65 20 2d 20 53 75 69 74 65 20 33 33 30 2c ace - Suite 330,
02e0: 0a 2a 2a 20 42 6f 73 74 6f 6e 2c 20 4d 41 20 20 .** Boston, MA
02f0: 30 32 31 31 31 2d 31 33 30 37 2c 20 55 53 41 2e 02111-1307, USA.
0300: 0a 2a 2a 0a 2a 2a 20 41 75 74 68 6f 72 20 63 6f .**.** Author co
0310: 6e 74 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f ntact informatio
0320: 6e 3a 0a 2a 2a 20 20 20 64 72 68 40 68 77 61 63 n:.** drh@hwac
0330: 69 2e 63 6f 6d 0a 2a 2a 20 20 20 68 74 74 70 3a i.com.** http:
0340: 2f 2f 77 77 77 2e 68 77 61 63 69 2e 63 6f 6d 2f //www.hwaci.com/
0350: 64 72 68 2f 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a drh/.**.********
0360: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0370: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0380: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0390: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03a0: 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
03b0: 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 ontains C code r
03c0: 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 outines that are
03d0: 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 called by the p
03e0: 61 72 73 65 72 0a 2a 2a 20 77 68 65 6e 20 73 79 arser.** when sy
03f0: 6e 74 61 78 20 72 75 6c 65 73 20 61 72 65 20 72 ntax rules are r
0400: 65 64 75 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 educed..**.** $I
0410: 64 3a 20 62 75 69 6c 64 2e 63 2c 76 20 31 2e 33 d: build.c,v 1.3
0420: 20 32 30 30 30 2f 30 35 2f 32 39 20 32 33 3a 33 2000/05/29 23:3
0430: 30 3a 35 31 20 64 72 68 20 45 78 70 20 24 0a 2a 0:51 drh Exp $.*
0440: 2f 0a 23 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 /.#include "sqli
0450: 74 65 49 6e 74 2e 68 22 0a 0a 2f 2a 0a 2a 2a 20 teInt.h"../*.**
0460: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
0470: 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20 73 called after a s
0480: 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d ingle SQL statem
0490: 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 ent has been.**
04a0: 70 61 72 73 65 64 20 61 6e 64 20 77 65 20 77 61 parsed and we wa
04b0: 6e 74 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 nt to execute th
04c0: 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d e code to implem
04d0: 65 6e 74 20 0a 2a 2a 20 74 68 65 20 73 74 61 74 ent .** the stat
04e0: 65 6d 65 6e 74 2e 20 20 50 72 69 6f 72 20 61 63 ement. Prior ac
04f0: 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 73 68 tion routines sh
0500: 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 ould have alread
0510: 79 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 y.** constructed
0520: 20 56 44 42 45 20 63 6f 64 65 20 74 6f 20 64 6f VDBE code to do
0530: 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 the work of the
0540: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a SQL statement..
0550: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
0560: 6a 75 73 74 20 68 61 73 20 74 6f 20 65 78 65 63 just has to exec
0570: 75 74 65 20 74 68 65 20 56 44 42 45 20 63 6f 64 ute the VDBE cod
0580: 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 e..**.** Note th
0590: 61 74 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f at if an error o
05a0: 63 63 75 72 72 65 64 2c 20 69 74 20 6d 69 67 68 ccurred, it migh
05b0: 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 t be the case th
05c0: 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 45 20 63 6f at.** no VDBE co
05d0: 64 65 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 de was generated
05e0: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
05f0: 45 78 65 63 28 50 61 72 73 65 20 2a 70 50 61 72 Exec(Parse *pPar
0600: 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 se){. if( pPars
0610: 65 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 e->pVdbe ){.
0620: 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c if( pParse->expl
0630: 61 69 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ain ){. sql
0640: 69 74 65 56 64 62 65 4c 69 73 74 28 70 50 61 72 iteVdbeList(pPar
0650: 73 65 2d 3e 70 56 64 62 65 2c 20 70 50 61 72 73 se->pVdbe, pPars
0660: 65 2d 3e 78 43 61 6c 6c 62 61 63 6b 2c 20 70 50 e->xCallback, pP
0670: 61 72 73 65 2d 3e 70 41 72 67 2c 20 0a 20 20 20 arse->pArg, .
0680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0690: 20 20 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d &pParse->zErrM
06a0: 73 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a sg);. }else{.
06b0: 20 20 20 20 20 20 46 49 4c 45 20 2a 74 72 61 63 FILE *trac
06c0: 65 20 3d 20 28 70 50 61 72 73 65 2d 3e 64 62 2d e = (pParse->db-
06d0: 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
06e0: 56 64 62 65 54 72 61 63 65 29 21 3d 30 20 3f 20 VdbeTrace)!=0 ?
06f0: 73 74 64 65 72 72 20 3a 20 30 3b 0a 20 20 20 20 stderr : 0;.
0700: 20 20 73 71 6c 69 74 65 56 64 62 65 54 72 61 63 sqliteVdbeTrac
0710: 65 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c e(pParse->pVdbe,
0720: 20 74 72 61 63 65 29 3b 0a 20 20 20 20 20 20 73 trace);. s
0730: 71 6c 69 74 65 56 64 62 65 45 78 65 63 28 70 50 qliteVdbeExec(pP
0740: 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 70 50 61 arse->pVdbe, pPa
0750: 72 73 65 2d 3e 78 43 61 6c 6c 62 61 63 6b 2c 20 rse->xCallback,
0760: 70 50 61 72 73 65 2d 3e 70 41 72 67 2c 20 0a 20 pParse->pArg, .
0770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0780: 20 20 20 20 26 70 50 61 72 73 65 2d 3e 7a 45 72 &pParse->zEr
0790: 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 rMsg);. }.
07a0: 20 73 71 6c 69 74 65 56 64 62 65 44 65 6c 65 74 sqliteVdbeDelet
07b0: 65 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 e(pParse->pVdbe)
07c0: 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 56 ;. pParse->pV
07d0: 64 62 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a dbe = 0;. }.}..
07e0: 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 /*.** Construct
07f0: 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e a new expression
0800: 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 72 6e node and return
0810: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
0820: 2e 0a 2a 2f 0a 45 78 70 72 20 2a 73 71 6c 69 74 ..*/.Expr *sqlit
0830: 65 45 78 70 72 28 69 6e 74 20 6f 70 2c 20 45 78 eExpr(int op, Ex
0840: 70 72 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 pr *pLeft, Expr
0850: 2a 70 52 69 67 68 74 2c 20 54 6f 6b 65 6e 20 2a *pRight, Token *
0860: 70 54 6f 6b 65 6e 29 7b 0a 20 20 45 78 70 72 20 pToken){. Expr
0870: 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 *pNew;. pNew =
0880: 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 sqliteMalloc( si
0890: 7a 65 6f 66 28 45 78 70 72 29 20 29 3b 0a 20 20 zeof(Expr) );.
08a0: 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 if( pNew==0 ) re
08b0: 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e turn 0;. pNew->
08c0: 6f 70 20 3d 20 6f 70 3b 0a 20 20 70 4e 65 77 2d op = op;. pNew-
08d0: 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a >pLeft = pLeft;.
08e0: 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d pNew->pRight =
08f0: 20 70 52 69 67 68 74 3b 0a 20 20 69 66 28 20 70 pRight;. if( p
0900: 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 70 4e 65 Token ){. pNe
0910: 77 2d 3e 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b w->token = *pTok
0920: 65 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 en;. }else{.
0930: 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d pNew->token.z =
0940: 20 22 22 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 74 "";. pNew->t
0950: 6f 6b 65 6e 2e 6e 20 3d 20 30 3b 0a 20 20 7d 0a oken.n = 0;. }.
0960: 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
0970: 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 ../*.** Construc
0980: 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 t a new expressi
0990: 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 on node for a fu
09a0: 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 nction with mult
09b0: 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 iple.** argument
09c0: 73 2e 0a 2a 2f 0a 45 78 70 72 20 2a 73 71 6c 69 s..*/.Expr *sqli
09d0: 74 65 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 45 teExprFunction(E
09e0: 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 xprList *pList,
09f0: 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a Token *pToken){.
0a00: 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 Expr *pNew;.
0a10: 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 4d 61 6c pNew = sqliteMal
0a20: 6c 6f 63 28 20 73 69 7a 65 6f 66 28 45 78 70 72 loc( sizeof(Expr
0a30: 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d ) );. if( pNew=
0a40: 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
0a50: 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 46 pNew->op = TK_F
0a60: 55 4e 43 54 49 4f 4e 3b 0a 20 20 70 4e 65 77 2d UNCTION;. pNew-
0a70: 3e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a >pList = pList;.
0a80: 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a if( pToken ){.
0a90: 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 20 pNew->token
0aa0: 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 7d 65 6c = *pToken;. }el
0ab0: 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 74 6f se{. pNew->to
0ac0: 6b 65 6e 2e 7a 20 3d 20 22 22 3b 0a 20 20 20 20 ken.z = "";.
0ad0: 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20 pNew->token.n =
0ae0: 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
0af0: 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 pNew;.}../*.** R
0b00: 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 ecursively delet
0b10: 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 e an expression
0b20: 74 72 65 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 tree..*/.void sq
0b30: 6c 69 74 65 45 78 70 72 44 65 6c 65 74 65 28 45 liteExprDelete(E
0b40: 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 xpr *p){. if( p
0b50: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
0b60: 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 29 20 73 if( p->pLeft ) s
0b70: 71 6c 69 74 65 45 78 70 72 44 65 6c 65 74 65 28 qliteExprDelete(
0b80: 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 p->pLeft);. if(
0b90: 20 70 2d 3e 70 52 69 67 68 74 20 29 20 73 71 6c p->pRight ) sql
0ba0: 69 74 65 45 78 70 72 44 65 6c 65 74 65 28 70 2d iteExprDelete(p-
0bb0: 3e 70 52 69 67 68 74 29 3b 0a 20 20 73 71 6c 69 >pRight);. sqli
0bc0: 74 65 46 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a teFree(p);.}../*
0bd0: 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 .** Locate the i
0be0: 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 n-memory structu
0bf0: 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 re that describe
0c00: 73 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 61 74 20 s the.** format
0c10: 6f 66 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 of a particular
0c20: 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 67 database table g
0c30: 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 0a 2a 2a iven the name.**
0c40: 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e 20 of that table.
0c50: 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 Return NULL if
0c60: 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 54 61 not found..*/.Ta
0c70: 62 6c 65 20 2a 73 71 6c 69 74 65 46 69 6e 64 54 ble *sqliteFindT
0c80: 61 62 6c 65 28 73 71 6c 69 74 65 20 2a 64 62 2c able(sqlite *db,
0c90: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 char *zName){.
0ca0: 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 0a Table *pTable;.
0cb0: 20 20 69 6e 74 20 68 3b 0a 0a 20 20 68 20 3d 20 int h;.. h =
0cc0: 73 71 6c 69 74 65 48 61 73 68 4e 6f 43 61 73 65 sqliteHashNoCase
0cd0: 28 7a 4e 61 6d 65 2c 20 30 29 20 25 20 4e 5f 48 (zName, 0) % N_H
0ce0: 41 53 48 3b 0a 20 20 66 6f 72 28 70 54 61 62 6c ASH;. for(pTabl
0cf0: 65 3d 64 62 2d 3e 61 70 54 62 6c 48 61 73 68 5b e=db->apTblHash[
0d00: 68 5d 3b 20 70 54 61 62 6c 65 3b 20 70 54 61 62 h]; pTable; pTab
0d10: 6c 65 3d 70 54 61 62 6c 65 2d 3e 70 48 61 73 68 le=pTable->pHash
0d20: 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
0d30: 65 53 74 72 49 43 6d 70 28 70 54 61 62 6c 65 2d eStrICmp(pTable-
0d40: 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d >zName, zName)==
0d50: 30 20 29 20 72 65 74 75 72 6e 20 70 54 61 62 6c 0 ) return pTabl
0d60: 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
0d70: 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 0;.}../*.** Loca
0d80: 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 te the in-memory
0d90: 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 structure that
0da0: 64 65 73 63 72 69 62 65 73 20 74 68 65 0a 2a 2a describes the.**
0db0: 20 66 6f 72 6d 61 74 20 6f 66 20 61 20 70 61 72 format of a par
0dc0: 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20 74 61 ticular index ta
0dd0: 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 ble given the na
0de0: 6d 65 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 me.** of that ta
0df0: 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c ble. Return NUL
0e00: 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a L if not found..
0e10: 2a 2f 0a 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 */.Index *sqlite
0e20: 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65 FindIndex(sqlite
0e30: 20 2a 64 62 2c 20 63 68 61 72 20 2a 7a 4e 61 6d *db, char *zNam
0e40: 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 3b 0a e){. Index *p;.
0e50: 20 20 69 6e 74 20 68 3b 0a 0a 20 20 68 20 3d 20 int h;.. h =
0e60: 73 71 6c 69 74 65 48 61 73 68 4e 6f 43 61 73 65 sqliteHashNoCase
0e70: 28 7a 4e 61 6d 65 2c 20 30 29 20 25 20 4e 5f 48 (zName, 0) % N_H
0e80: 41 53 48 3b 0a 20 20 66 6f 72 28 70 3d 64 62 2d ASH;. for(p=db-
0e90: 3e 61 70 49 64 78 48 61 73 68 5b 68 5d 3b 20 70 >apIdxHash[h]; p
0ea0: 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29 7b 0a 20 ; p=p->pHash){.
0eb0: 20 20 20 69 66 28 20 73 71 6c 69 74 65 53 74 72 if( sqliteStr
0ec0: 49 43 6d 70 28 70 2d 3e 7a 4e 61 6d 65 2c 20 7a ICmp(p->zName, z
0ed0: 4e 61 6d 65 29 3d 3d 30 20 29 20 72 65 74 75 72 Name)==0 ) retur
0ee0: 6e 20 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n p;. }. retur
0ef0: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n 0;.}../*.** Re
0f00: 6d 6f 76 65 20 74 68 65 20 67 69 76 65 6e 20 69 move the given i
0f10: 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e ndex from the in
0f20: 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 2c 20 dex hash table,
0f30: 61 6e 64 20 66 72 65 65 0a 2a 2a 20 69 74 73 20 and free.** its
0f40: 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 memory structure
0f50: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 s..**.** The ind
0f60: 65 78 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 ex is removed fr
0f70: 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
0f80: 68 61 73 68 20 74 61 62 6c 65 2c 20 62 75 74 20 hash table, but
0f90: 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 75 6e 6c it is.** not unl
0fa0: 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68 65 20 74 inked from the t
0fb0: 61 62 6c 65 20 74 68 61 74 20 69 73 20 62 65 69 able that is bei
0fc0: 6e 67 20 69 6e 64 65 78 65 64 2e 20 20 55 6e 6c ng indexed. Unl
0fd0: 69 6e 6b 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 74 inking.** from t
0fe0: 68 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 he table must be
0ff0: 20 64 6f 6e 65 20 62 79 20 74 68 65 20 63 61 6c done by the cal
1000: 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a ling function..*
1010: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
1020: 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65 78 28 liteDeleteIndex(
1030: 73 71 6c 69 74 65 20 2a 64 62 2c 20 49 6e 64 65 sqlite *db, Inde
1040: 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20 20 69 6e x *pIndex){. in
1050: 74 20 68 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 t h;. if( pInde
1060: 78 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 x->zName ){.
1070: 68 20 3d 20 73 71 6c 69 74 65 48 61 73 68 4e 6f h = sqliteHashNo
1080: 43 61 73 65 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 Case(pIndex->zNa
1090: 6d 65 2c 20 30 29 20 25 20 4e 5f 48 41 53 48 3b me, 0) % N_HASH;
10a0: 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 70 49 . if( db->apI
10b0: 64 78 48 61 73 68 5b 68 5d 3d 3d 70 49 6e 64 65 dxHash[h]==pInde
10c0: 78 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 x ){. db->a
10d0: 70 49 64 78 48 61 73 68 5b 68 5d 20 3d 20 70 49 pIdxHash[h] = pI
10e0: 6e 64 65 78 2d 3e 70 48 61 73 68 3b 0a 20 20 20 ndex->pHash;.
10f0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e }else{. In
1100: 64 65 78 20 2a 70 3b 0a 20 20 20 20 20 20 66 6f dex *p;. fo
1110: 72 28 70 3d 64 62 2d 3e 61 70 49 64 78 48 61 73 r(p=db->apIdxHas
1120: 68 5b 68 5d 3b 20 70 20 26 26 20 70 2d 3e 70 48 h[h]; p && p->pH
1130: 61 73 68 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70 ash!=pIndex; p=p
1140: 2d 3e 70 48 61 73 68 29 7b 7d 0a 20 20 20 20 20 ->pHash){}.
1150: 20 69 66 28 20 70 20 26 26 20 70 2d 3e 70 48 61 if( p && p->pHa
1160: 73 68 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 sh==pIndex ){.
1170: 20 20 20 20 20 20 70 2d 3e 70 48 61 73 68 20 3d p->pHash =
1180: 20 70 49 6e 64 65 78 2d 3e 70 48 61 73 68 3b 0a pIndex->pHash;.
1190: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
11a0: 7d 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 }. sqliteFree(p
11b0: 49 6e 64 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Index);.}../*.**
11c0: 20 52 65 6d 6f 76 65 20 74 68 65 20 6d 65 6d 6f Remove the memo
11d0: 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75 72 ry data structur
11e0: 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
11f0: 74 68 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 th the given.**
1200: 74 61 62 6c 65 2e 20 20 4e 6f 20 63 68 61 6e 67 table. No chang
1210: 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 64 es are made to d
1220: 69 73 6b 20 62 79 20 74 68 69 73 20 72 6f 75 74 isk by this rout
1230: 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ine..**.** This
1240: 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 64 65 6c routine just del
1250: 65 74 65 73 20 74 68 65 20 64 61 74 61 20 73 74 etes the data st
1260: 72 75 63 74 75 72 65 2e 20 20 49 74 20 64 6f 65 ructure. It doe
1270: 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 s not unlink.**
1280: 74 68 65 20 74 61 62 6c 65 20 64 61 74 61 20 73 the table data s
1290: 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 tructure from th
12a0: 65 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 42 e hash table. B
12b0: 75 74 20 64 6f 65 73 20 69 74 20 64 65 73 74 72 ut does it destr
12c0: 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74 72 oy.** memory str
12d0: 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20 69 uctures of the i
12e0: 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 ndices associate
12f0: 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 d with the table
1300: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
1310: 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71 6c 69 DeleteTable(sqli
1320: 74 65 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 te *db, Table *p
1330: 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b Table){. int i;
1340: 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
1350: 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 69 66 28 20 , *pNext;. if(
1360: 70 54 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 pTable==0 ) retu
1370: 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 rn;. for(i=0; i
1380: 3c 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 <pTable->nCol; i
1390: 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54 61 ++){. if( pTa
13a0: 62 6c 65 2d 3e 61 7a 43 6f 6c 5b 69 5d 20 29 20 ble->azCol[i] )
13b0: 73 71 6c 69 74 65 46 72 65 65 28 70 54 61 62 6c sqliteFree(pTabl
13c0: 65 2d 3e 61 7a 43 6f 6c 5b 69 5d 29 3b 0a 20 20 e->azCol[i]);.
13d0: 7d 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 20 3d }. for(pIndex =
13e0: 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b pTable->pIndex;
13f0: 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d pIndex; pIndex=
1400: 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 pNext){. pNex
1410: 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 t = pIndex->pNex
1420: 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 44 65 6c t;. sqliteDel
1430: 65 74 65 49 6e 64 65 78 28 64 62 2c 20 70 49 6e eteIndex(db, pIn
1440: 64 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 dex);. }. sqli
1450: 74 65 46 72 65 65 28 70 54 61 62 6c 65 2d 3e 61 teFree(pTable->a
1460: 7a 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 46 zCol);. sqliteF
1470: 72 65 65 28 70 54 61 62 6c 65 29 3b 0a 7d 0a 0a ree(pTable);.}..
1480: 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 /*.** Construct
1490: 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 75 73 the name of a us
14a0: 65 72 20 74 61 62 6c 65 20 66 72 6f 6d 20 61 20 er table from a
14b0: 74 6f 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 token..**.** Spa
14c0: 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e ce to hold the n
14d0: 61 6d 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ame is obtained
14e0: 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f from sqliteMallo
14f0: 63 28 29 20 61 6e 64 20 6d 75 73 74 0a 2a 2a 20 c() and must.**
1500: 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 be freed by the
1510: 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
1520: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
1530: 20 2a 73 71 6c 69 74 65 54 61 62 6c 65 4e 61 6d *sqliteTableNam
1540: 65 46 72 6f 6d 54 6f 6b 65 6e 28 54 6f 6b 65 6e eFromToken(Token
1550: 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 *pName){. char
1560: 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 73 *zName = 0;. s
1570: 71 6c 69 74 65 53 65 74 4e 53 74 72 69 6e 67 28 qliteSetNString(
1580: 26 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 7a &zName, pName->z
1590: 2c 20 70 4e 61 6d 65 2d 3e 6e 2c 20 30 29 3b 0a , pName->n, 0);.
15a0: 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a return zName;.
15b0: 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 63 }../*.** Begin c
15c0: 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20 6e 65 onstructing a ne
15d0: 77 20 74 61 62 6c 65 20 72 65 70 72 65 73 65 6e w table represen
15e0: 74 61 74 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72 79 tation in memory
15f0: 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 74 68 . This is.** th
1600: 65 20 66 69 72 73 74 20 6f 66 20 73 65 76 65 72 e first of sever
1610: 61 6c 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e al action routin
1620: 65 73 20 74 68 61 74 20 67 65 74 20 63 61 6c 6c es that get call
1630: 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 0a 2a ed in response.*
1640: 2a 20 74 6f 20 61 20 43 52 45 41 54 45 20 54 41 * to a CREATE TA
1650: 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a BLE statement..*
1660: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 53 74 61 /.void sqliteSta
1670: 72 74 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 rtTable(Parse *p
1680: 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 53 Parse, Token *pS
1690: 74 61 72 74 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 tart, Token *pNa
16a0: 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 me){. Table *pT
16b0: 61 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e able;. char *zN
16c0: 61 6d 65 3b 0a 0a 20 20 70 50 61 72 73 65 2d 3e ame;.. pParse->
16d0: 73 46 69 72 73 74 54 6f 6b 65 6e 20 3d 20 2a 70 sFirstToken = *p
16e0: 53 74 61 72 74 3b 0a 20 20 7a 4e 61 6d 65 20 3d Start;. zName =
16f0: 20 73 71 6c 69 74 65 54 61 62 6c 65 4e 61 6d 65 sqliteTableName
1700: 46 72 6f 6d 54 6f 6b 65 6e 28 70 4e 61 6d 65 29 FromToken(pName)
1710: 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c ;. pTable = sql
1720: 69 74 65 46 69 6e 64 54 61 62 6c 65 28 70 50 61 iteFindTable(pPa
1730: 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b rse->db, zName);
1740: 0a 20 20 69 66 28 20 70 54 61 62 6c 65 21 3d 30 . if( pTable!=0
1750: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 53 65 ){. sqliteSe
1760: 74 4e 53 74 72 69 6e 67 28 26 70 50 61 72 73 65 tNString(&pParse
1770: 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 74 61 62 6c ->zErrMsg, "tabl
1780: 65 20 5c 22 22 2c 20 30 2c 20 70 4e 61 6d 65 2d e \"", 0, pName-
1790: 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 2c 0a 20 20 >z, pName->n,.
17a0: 20 20 20 20 20 20 22 5c 22 20 61 6c 72 65 61 64 "\" alread
17b0: 79 20 65 78 69 73 74 73 22 2c 20 30 2c 20 30 29 y exists", 0, 0)
17c0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 ;. sqliteFree
17d0: 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 50 61 (zName);. pPa
17e0: 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 rse->nErr++;.
17f0: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
1800: 66 28 20 73 71 6c 69 74 65 46 69 6e 64 49 6e 64 f( sqliteFindInd
1810: 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a ex(pParse->db, z
1820: 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 73 71 6c Name) ){. sql
1830: 69 74 65 53 65 74 53 74 72 69 6e 67 28 26 70 50 iteSetString(&pP
1840: 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 arse->zErrMsg, "
1850: 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 there is already
1860: 20 61 6e 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 an index named
1870: 5c 22 22 2c 20 0a 20 20 20 20 20 20 20 7a 4e 61 \"", . zNa
1880: 6d 65 2c 20 22 5c 22 22 2c 20 30 29 3b 0a 20 20 me, "\"", 0);.
1890: 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 4e 61 sqliteFree(zNa
18a0: 6d 65 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d me);. pParse-
18b0: 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 72 65 74 >nErr++;. ret
18c0: 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c urn;. }. pTabl
18d0: 65 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 e = sqliteMalloc
18e0: 28 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 20 ( sizeof(Table)
18f0: 29 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d );. if( pTable=
1900: 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1910: 53 65 74 53 74 72 69 6e 67 28 26 70 50 61 72 73 SetString(&pPars
1920: 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 6f 75 74 e->zErrMsg, "out
1930: 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20 30 29 3b of memory", 0);
1940: 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 . pParse->nEr
1950: 72 2b 2b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b r++;. return;
1960: 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a . }. pTable->z
1970: 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 Name = zName;.
1980: 70 54 61 62 6c 65 2d 3e 70 48 61 73 68 20 3d 20 pTable->pHash =
1990: 30 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 0;. pTable->nCo
19a0: 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62 6c 65 2d l = 0;. pTable-
19b0: 3e 61 7a 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 >azCol = 0;. pT
19c0: 61 62 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 30 able->pIndex = 0
19d0: 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
19e0: 70 4e 65 77 54 61 62 6c 65 20 29 20 73 71 6c 69 pNewTable ) sqli
19f0: 74 65 44 65 6c 65 74 65 54 61 62 6c 65 28 70 50 teDeleteTable(pP
1a00: 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 arse->db, pParse
1a10: 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 ->pNewTable);.
1a20: 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
1a30: 65 20 3d 20 70 54 61 62 6c 65 3b 0a 7d 0a 0a 2f e = pTable;.}../
1a40: 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 63 *.** Add a new c
1a50: 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20 74 61 62 olumn to the tab
1a60: 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 le currently bei
1a70: 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a ng constructed..
1a80: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 41 64 */.void sqliteAd
1a90: 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 dColumn(Parse *p
1aa0: 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e Parse, Token *pN
1ab0: 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 ame){. Table *p
1ac0: 3b 0a 20 20 63 68 61 72 20 2a 2a 70 7a 3b 0a 20 ;. char **pz;.
1ad0: 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 if( (p = pParse
1ae0: 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 ->pNewTable)==0
1af0: 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
1b00: 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d (p->nCol & 0x7)=
1b10: 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 61 7a 43 =0 ){. p->azC
1b20: 6f 6c 20 3d 20 73 71 6c 69 74 65 52 65 61 6c 6c ol = sqliteReall
1b30: 6f 63 28 20 70 2d 3e 61 7a 43 6f 6c 2c 20 28 70 oc( p->azCol, (p
1b40: 2d 3e 6e 43 6f 6c 2b 39 29 2a 73 69 7a 65 6f 66 ->nCol+9)*sizeof
1b50: 28 70 2d 3e 61 7a 43 6f 6c 5b 30 5d 29 29 3b 0a (p->azCol[0]));.
1b60: 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 7a 43 }. if( p->azC
1b70: 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e ol==0 ){. p->
1b80: 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 nCol = 0;. re
1b90: 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 7a 20 3d turn;. }. pz =
1ba0: 20 26 70 2d 3e 61 7a 43 6f 6c 5b 70 2d 3e 6e 43 &p->azCol[p->nC
1bb0: 6f 6c 2b 2b 5d 3b 0a 20 20 2a 70 7a 20 3d 20 30 ol++];. *pz = 0
1bc0: 3b 0a 20 20 73 71 6c 69 74 65 53 65 74 4e 53 74 ;. sqliteSetNSt
1bd0: 72 69 6e 67 28 70 7a 2c 20 70 4e 61 6d 65 2d 3e ring(pz, pName->
1be0: 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 2c 20 30 29 3b z, pName->n, 0);
1bf0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
1c00: 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1c10: 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 66 to report the f
1c20: 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 65 inal ")" that te
1c30: 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 52 rminates.** a CR
1c40: 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
1c50: 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
1c60: 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 table structure
1c70: 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 is added to the
1c80: 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 internal hash ta
1c90: 62 6c 65 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 6e bles. .**.** An
1ca0: 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 74 entry for the t
1cb0: 61 62 6c 65 20 69 73 20 6d 61 64 65 20 69 6e 20 able is made in
1cc0: 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 the master table
1cd0: 2c 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 69 6e 69 , unless .** ini
1ce0: 74 46 6c 61 67 3d 3d 31 2e 20 20 57 68 65 6e 20 tFlag==1. When
1cf0: 69 6e 69 74 46 6c 61 67 3d 3d 31 2c 20 69 74 20 initFlag==1, it
1d00: 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 61 means we are rea
1d10: 64 69 6e 67 20 74 68 65 0a 2a 2a 20 6d 61 73 74 ding the.** mast
1d20: 65 72 20 74 61 62 6c 65 20 62 65 63 61 75 73 65 er table because
1d30: 20 77 65 20 6a 75 73 74 20 63 6f 6e 6e 65 63 74 we just connect
1d40: 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ed to the databa
1d50: 73 65 2c 20 73 6f 20 0a 2a 2a 20 74 68 65 20 65 se, so .** the e
1d60: 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20 74 61 ntry for this ta
1d70: 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 ble already exis
1d80: 74 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 ts in the master
1d90: 20 74 61 62 6c 65 2e 0a 2a 2a 20 57 65 20 64 6f table..** We do
1da0: 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 63 72 65 not want to cre
1db0: 61 74 65 20 69 74 20 61 67 61 69 6e 2e 0a 2a 2f ate it again..*/
1dc0: 0a 76 6f 69 64 20 73 71 6c 69 74 65 45 6e 64 54 .void sqliteEndT
1dd0: 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 able(Parse *pPar
1de0: 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 se, Token *pEnd)
1df0: 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 {. Table *p;.
1e00: 69 6e 74 20 68 3b 0a 0a 20 20 69 66 28 20 70 50 int h;.. if( pP
1e10: 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 arse->nErr ) ret
1e20: 75 72 6e 3b 0a 0a 20 20 2f 2a 20 41 64 64 20 74 urn;.. /* Add t
1e30: 68 65 20 74 61 62 6c 65 20 74 6f 20 74 68 65 20 he table to the
1e40: 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 in-memory repres
1e50: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
1e60: 64 61 74 61 62 61 73 65 0a 20 20 2a 2f 0a 20 20 database. */.
1e70: 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d if( (p = pParse-
1e80: 3e 70 4e 65 77 54 61 62 6c 65 29 21 3d 30 20 26 >pNewTable)!=0 &
1e90: 26 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 & pParse->explai
1ea0: 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 68 20 3d 20 n==0 ){. h =
1eb0: 73 71 6c 69 74 65 48 61 73 68 4e 6f 43 61 73 65 sqliteHashNoCase
1ec0: 28 70 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 25 20 (p->zName, 0) %
1ed0: 4e 5f 48 41 53 48 3b 0a 20 20 20 20 70 2d 3e 70 N_HASH;. p->p
1ee0: 48 61 73 68 20 3d 20 70 50 61 72 73 65 2d 3e 64 Hash = pParse->d
1ef0: 62 2d 3e 61 70 54 62 6c 48 61 73 68 5b 68 5d 3b b->apTblHash[h];
1f00: 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d . pParse->db-
1f10: 3e 61 70 54 62 6c 48 61 73 68 5b 68 5d 20 3d 20 >apTblHash[h] =
1f20: 70 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 p;. pParse->p
1f30: 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 NewTable = 0;.
1f40: 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 }.. /* If not i
1f50: 6e 69 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 nitializing, the
1f60: 6e 20 63 72 65 61 74 65 20 74 68 65 20 74 61 62 n create the tab
1f70: 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f le on disk.. */
1f80: 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e . if( !pParse->
1f90: 69 6e 69 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 initFlag ){.
1fa0: 73 74 61 74 69 63 20 56 64 62 65 4f 70 20 61 64 static VdbeOp ad
1fb0: 64 54 61 62 6c 65 5b 5d 20 3d 20 7b 0a 20 20 20 dTable[] = {.
1fc0: 20 20 20 7b 20 4f 50 5f 4f 70 65 6e 2c 20 20 20 { OP_Open,
1fd0: 20 20 20 20 20 30 2c 20 30 2c 20 4d 41 53 54 45 0, 0, MASTE
1fe0: 52 5f 4e 41 4d 45 20 7d 2c 0a 20 20 20 20 20 20 R_NAME },.
1ff0: 7b 20 4f 50 5f 4e 65 77 2c 20 20 20 20 20 20 20 { OP_New,
2000: 20 20 30 2c 20 30 2c 20 30 7d 2c 0a 20 20 20 20 0, 0, 0},.
2010: 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 2c 20 20 { OP_String,
2020: 20 20 20 20 30 2c 20 30 2c 20 22 74 61 62 6c 65 0, 0, "table
2030: 22 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b " },. {
2040: 20 4f 50 5f 53 74 72 69 6e 67 2c 20 20 20 20 20 OP_String,
2050: 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 0, 0, 0},
2060: 20 20 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 /* 3 */.
2070: 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 2c { OP_String,
2080: 20 20 20 20 20 20 30 2c 20 30 2c 20 30 7d 2c 20 0, 0, 0},
2090: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34 20 /* 4
20a0: 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 */. { OP_St
20b0: 72 69 6e 67 2c 20 20 20 20 20 20 30 2c 20 30 2c ring, 0, 0,
20c0: 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20 0},
20d0: 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 7b 20 /* 5 */. {
20e0: 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 20 OP_MakeRecord,
20f0: 34 2c 20 30 2c 20 30 7d 2c 0a 20 20 20 20 20 20 4, 0, 0},.
2100: 7b 20 4f 50 5f 50 75 74 2c 20 20 20 20 20 20 20 { OP_Put,
2110: 20 20 30 2c 20 30 2c 20 30 7d 2c 0a 20 20 20 20 0, 0, 0},.
2120: 20 20 7b 20 4f 50 5f 43 6c 6f 73 65 2c 20 20 20 { OP_Close,
2130: 20 20 20 20 30 2c 20 30 2c 20 30 7d 2c 0a 20 20 0, 0, 0},.
2140: 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6e 2c 20 };. int n,
2150: 62 61 73 65 3b 0a 20 20 20 20 56 64 62 65 20 2a base;. Vdbe *
2160: 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
2170: 65 3b 0a 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 e;.. if( v==0
2180: 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 70 50 ){. v = pP
2190: 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 73 71 arse->pVdbe = sq
21a0: 6c 69 74 65 56 64 62 65 43 72 65 61 74 65 28 70 liteVdbeCreate(p
21b0: 50 61 72 73 65 2d 3e 64 62 2d 3e 70 42 65 29 3b Parse->db->pBe);
21c0: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 76 . }. if( v
21d0: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
21e0: 20 20 6e 20 3d 20 28 69 6e 74 29 70 45 6e 64 2d n = (int)pEnd-
21f0: 3e 7a 20 2d 20 28 69 6e 74 29 70 50 61 72 73 65 >z - (int)pParse
2200: 2d 3e 73 46 69 72 73 74 54 6f 6b 65 6e 2e 7a 20 ->sFirstToken.z
2210: 2b 20 31 3b 0a 20 20 20 20 62 61 73 65 20 3d 20 + 1;. base =
2220: 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 4c sqliteVdbeAddOpL
2230: 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 ist(v, ArraySize
2240: 28 61 64 64 54 61 62 6c 65 29 2c 20 61 64 64 54 (addTable), addT
2250: 61 62 6c 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 able);. sqlit
2260: 65 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c eVdbeChangeP3(v,
2270: 20 62 61 73 65 2b 33 2c 20 70 2d 3e 7a 4e 61 6d base+3, p->zNam
2280: 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 e, 0);. sqlit
2290: 65 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c eVdbeChangeP3(v,
22a0: 20 62 61 73 65 2b 34 2c 20 70 2d 3e 7a 4e 61 6d base+4, p->zNam
22b0: 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 e, 0);. sqlit
22c0: 65 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c eVdbeChangeP3(v,
22d0: 20 62 61 73 65 2b 35 2c 20 70 50 61 72 73 65 2d base+5, pParse-
22e0: 3e 73 46 69 72 73 74 54 6f 6b 65 6e 2e 7a 2c 20 >sFirstToken.z,
22f0: 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a n);. }.}../*.**
2300: 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20 Given a token,
2310: 6c 6f 6f 6b 20 75 70 20 61 20 74 61 62 6c 65 20 look up a table
2320: 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20 with that name.
2330: 20 49 66 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 6c If not found, l
2340: 65 61 76 65 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 eave.** an error
2350: 20 66 6f 72 20 74 68 65 20 70 61 72 73 65 72 20 for the parser
2360: 74 6f 20 66 69 6e 64 20 61 6e 64 20 72 65 74 75 to find and retu
2370: 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 rn NULL..*/.stat
2380: 69 63 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 ic Table *sqlite
2390: 54 61 62 6c 65 46 72 6f 6d 54 6f 6b 65 6e 28 50 TableFromToken(P
23a0: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f arse *pParse, To
23b0: 6b 65 6e 20 2a 70 54 6f 6b 29 7b 0a 20 20 63 68 ken *pTok){. ch
23c0: 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 ar *zName = sqli
23d0: 74 65 54 61 62 6c 65 4e 61 6d 65 46 72 6f 6d 54 teTableNameFromT
23e0: 6f 6b 65 6e 28 70 54 6f 6b 29 3b 0a 20 20 54 61 oken(pTok);. Ta
23f0: 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 ble *pTab = sqli
2400: 74 65 46 69 6e 64 54 61 62 6c 65 28 70 50 61 72 teFindTable(pPar
2410: 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a se->db, zName);.
2420: 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 4e 61 sqliteFree(zNa
2430: 6d 65 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d me);. if( pTab=
2440: 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
2450: 53 65 74 4e 53 74 72 69 6e 67 28 26 70 50 61 72 SetNString(&pPar
2460: 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 6e 6f se->zErrMsg, "no
2470: 20 73 75 63 68 20 74 61 62 6c 65 3a 20 22 2c 20 such table: ",
2480: 30 2c 20 0a 20 20 20 20 20 20 20 20 70 54 6f 6b 0, . pTok
2490: 2d 3e 7a 2c 20 70 54 6f 6b 2d 3e 6e 2c 20 30 29 ->z, pTok->n, 0)
24a0: 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 ;. pParse->nE
24b0: 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rr++;. }. retu
24c0: 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pTab;.}../*.*
24d0: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
24e0: 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 6f 20 74 s called to do t
24f0: 68 65 20 77 6f 72 6b 20 6f 66 20 61 20 44 52 4f he work of a DRO
2500: 50 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e P TABLE statemen
2510: 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 t..*/.void sqlit
2520: 65 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 eDropTable(Parse
2530: 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 *pParse, Token
2540: 2a 70 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 *pName){. Table
2550: 20 2a 70 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 *pTable;. int
2560: 68 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 h;. Vdbe *v;.
2570: 69 6e 74 20 62 61 73 65 3b 0a 0a 20 20 70 54 61 int base;.. pTa
2580: 62 6c 65 20 3d 20 73 71 6c 69 74 65 54 61 62 6c ble = sqliteTabl
2590: 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 eFromToken(pPars
25a0: 65 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 e, pName);. if(
25b0: 20 70 54 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 pTable==0 ) ret
25c0: 75 72 6e 3b 0a 20 20 69 66 28 20 70 54 61 62 6c urn;. if( pTabl
25d0: 65 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 e->readOnly ){.
25e0: 20 20 20 73 71 6c 69 74 65 53 65 74 53 74 72 69 sqliteSetStri
25f0: 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 ng(&pParse->zErr
2600: 4d 73 67 2c 20 22 74 61 62 6c 65 20 5c 22 22 2c Msg, "table \"",
2610: 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 pTable->zName,
2620: 0a 20 20 20 20 20 20 20 22 5c 22 20 6d 61 79 20 . "\" may
2630: 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 2c not be dropped",
2640: 20 30 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 0);. pParse-
2650: 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 72 65 74 >nErr++;. ret
2660: 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 urn;. }.. /* G
2670: 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
2680: 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 remove the table
2690: 20 61 6e 64 20 69 74 73 20 72 65 66 65 72 65 6e and its referen
26a0: 63 65 20 69 6e 20 73 79 73 5f 6d 61 73 74 65 72 ce in sys_master
26b0: 20 2a 2f 0a 20 20 76 20 3d 20 70 50 61 72 73 65 */. v = pParse
26c0: 2d 3e 70 56 64 62 65 3b 0a 20 20 69 66 28 20 76 ->pVdbe;. if( v
26d0: 3d 3d 30 20 29 7b 0a 20 20 20 20 76 20 3d 20 70 ==0 ){. v = p
26e0: 50 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 73 Parse->pVdbe = s
26f0: 71 6c 69 74 65 56 64 62 65 43 72 65 61 74 65 28 qliteVdbeCreate(
2700: 70 50 61 72 73 65 2d 3e 64 62 2d 3e 70 42 65 29 pParse->db->pBe)
2710: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 20 29 7b ;. }. if( v ){
2720: 0a 20 20 20 20 73 74 61 74 69 63 20 56 64 62 65 . static Vdbe
2730: 4f 70 20 64 72 6f 70 54 61 62 6c 65 5b 5d 20 3d Op dropTable[] =
2740: 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4f 70 {. { OP_Op
2750: 65 6e 2c 20 20 20 20 20 20 20 30 2c 20 30 2c 20 en, 0, 0,
2760: 20 20 20 20 20 20 20 4d 41 53 54 45 52 5f 4e 41 MASTER_NA
2770: 4d 45 20 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 ME },. { OP
2780: 5f 4c 69 73 74 4f 70 65 6e 2c 20 20 20 30 2c 20 _ListOpen, 0,
2790: 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 0, 0},.
27a0: 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 2c { OP_String,
27b0: 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 0, 0,
27c0: 20 20 30 7d 2c 20 2f 2a 20 32 20 2a 2f 0a 20 20 0}, /* 2 */.
27d0: 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20 { OP_Next,
27e0: 20 20 20 20 20 30 2c 20 41 44 44 52 28 31 30 29 0, ADDR(10)
27f0: 2c 20 30 7d 2c 20 2f 2a 20 33 20 2a 2f 0a 20 20 , 0}, /* 3 */.
2800: 20 20 20 20 7b 20 4f 50 5f 44 75 70 2c 20 20 20 { OP_Dup,
2810: 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 0, 0,
2820: 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 0},. { OP
2830: 5f 46 69 65 6c 64 2c 20 20 20 20 20 20 30 2c 20 _Field, 0,
2840: 32 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 2, 0},.
2850: 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 { OP_Ne,
2860: 20 20 20 20 20 30 2c 20 41 44 44 52 28 33 29 2c 0, ADDR(3),
2870: 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 0},. { OP
2880: 5f 4b 65 79 2c 20 20 20 20 20 20 20 20 30 2c 20 _Key, 0,
2890: 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 0, 0},.
28a0: 20 20 20 20 7b 20 4f 50 5f 4c 69 73 74 57 72 69 { OP_ListWri
28b0: 74 65 2c 20 20 30 2c 20 30 2c 20 20 20 20 20 20 te, 0, 0,
28c0: 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 0},. { OP
28d0: 5f 47 6f 74 6f 2c 20 20 20 20 20 20 20 30 2c 20 _Goto, 0,
28e0: 41 44 44 52 28 33 29 2c 20 20 30 7d 2c 0a 20 20 ADDR(3), 0},.
28f0: 20 20 20 20 7b 20 4f 50 5f 4c 69 73 74 52 65 77 { OP_ListRew
2900: 69 6e 64 2c 20 30 2c 20 30 2c 20 20 20 20 20 20 ind, 0, 0,
2910: 20 20 30 7d 2c 20 2f 2a 20 31 30 20 2a 2f 0a 20 0}, /* 10 */.
2920: 20 20 20 20 20 7b 20 4f 50 5f 4c 69 73 74 52 65 { OP_ListRe
2930: 61 64 2c 20 20 20 30 2c 20 41 44 44 52 28 31 34 ad, 0, ADDR(14
2940: 29 2c 20 30 7d 2c 20 2f 2a 20 31 31 20 2a 2f 0a ), 0}, /* 11 */.
2950: 20 20 20 20 20 20 7b 20 4f 50 5f 44 65 6c 65 74 { OP_Delet
2960: 65 2c 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 e, 0, 0,
2970: 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 0},. {
2980: 4f 50 5f 47 6f 74 6f 2c 20 20 20 20 20 20 20 30 OP_Goto, 0
2990: 2c 20 41 44 44 52 28 31 31 29 2c 20 30 7d 2c 0a , ADDR(11), 0},.
29a0: 20 20 20 20 20 20 7b 20 4f 50 5f 44 65 73 74 72 { OP_Destr
29b0: 6f 79 2c 20 20 20 20 30 2c 20 30 2c 20 20 20 20 oy, 0, 0,
29c0: 20 20 20 20 30 7d 2c 20 2f 2a 20 31 34 20 2a 2f 0}, /* 14 */
29d0: 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43 6c 6f 73 . { OP_Clos
29e0: 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 20 20 e, 0, 0,
29f0: 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 0},. };.
2a00: 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b Index *pIdx;
2a10: 0a 20 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69 . base = sqli
2a20: 74 65 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 teVdbeAddOpList(
2a30: 76 2c 20 41 72 72 61 79 53 69 7a 65 28 64 72 6f v, ArraySize(dro
2a40: 70 54 61 62 6c 65 29 2c 20 64 72 6f 70 54 61 62 pTable), dropTab
2a50: 6c 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 le);. sqliteV
2a60: 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 62 dbeChangeP3(v, b
2a70: 61 73 65 2b 32 2c 20 70 54 61 62 6c 65 2d 3e 7a ase+2, pTable->z
2a80: 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 Name, 0);. sq
2a90: 6c 69 74 65 56 64 62 65 43 68 61 6e 67 65 50 33 liteVdbeChangeP3
2aa0: 28 76 2c 20 62 61 73 65 2b 31 34 2c 20 70 54 61 (v, base+14, pTa
2ab0: 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a ble->zName, 0);.
2ac0: 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 for(pIdx=pTa
2ad0: 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 ble->pIndex; pId
2ae0: 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e x; pIdx=pIdx->pN
2af0: 65 78 74 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ext){. sqli
2b00: 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f teVdbeAddOp(v, O
2b10: 50 5f 44 65 73 74 72 6f 79 2c 20 30 2c 20 30 2c P_Destroy, 0, 0,
2b20: 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 pIdx->zName, 0)
2b30: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
2b40: 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 74 61 62 * Remove the tab
2b50: 6c 65 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 le structure and
2b60: 20 66 72 65 65 20 69 74 73 20 6d 65 6d 6f 72 79 free its memory
2b70: 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 .. **. ** Exce
2b80: 70 74 69 6f 6e 3a 20 69 66 20 74 68 65 20 53 51 ption: if the SQ
2b90: 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 67 61 L statement bega
2ba0: 6e 20 77 69 74 68 20 74 68 65 20 45 58 50 4c 41 n with the EXPLA
2bb0: 49 4e 20 6b 65 79 77 6f 72 64 2c 0a 20 20 2a 2a IN keyword,. **
2bc0: 20 74 68 65 6e 20 6e 6f 20 63 68 61 6e 67 65 73 then no changes
2bd0: 20 61 72 65 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a are made.. */.
2be0: 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 65 if( !pParse->e
2bf0: 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 68 20 xplain ){. h
2c00: 3d 20 73 71 6c 69 74 65 48 61 73 68 4e 6f 43 61 = sqliteHashNoCa
2c10: 73 65 28 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 se(pTable->zName
2c20: 2c 20 30 29 20 25 20 4e 5f 48 41 53 48 3b 0a 20 , 0) % N_HASH;.
2c30: 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 if( pParse->d
2c40: 62 2d 3e 61 70 54 62 6c 48 61 73 68 5b 68 5d 3d b->apTblHash[h]=
2c50: 3d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 =pTable ){.
2c60: 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 70 54 pParse->db->apT
2c70: 62 6c 48 61 73 68 5b 68 5d 20 3d 20 70 54 61 62 blHash[h] = pTab
2c80: 6c 65 2d 3e 70 48 61 73 68 3b 0a 20 20 20 20 7d le->pHash;. }
2c90: 65 6c 73 65 7b 0a 20 20 20 20 20 20 54 61 62 6c else{. Tabl
2ca0: 65 20 2a 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 e *p;. for(
2cb0: 70 3d 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 70 p=pParse->db->ap
2cc0: 54 62 6c 48 61 73 68 5b 68 5d 3b 20 70 20 26 26 TblHash[h]; p &&
2cd0: 20 70 2d 3e 70 48 61 73 68 21 3d 70 54 61 62 6c p->pHash!=pTabl
2ce0: 65 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29 7b 7d e; p=p->pHash){}
2cf0: 0a 20 20 20 20 20 20 69 66 28 20 70 20 26 26 20 . if( p &&
2d00: 70 2d 3e 70 48 61 73 68 3d 3d 70 54 61 62 6c 65 p->pHash==pTable
2d10: 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 ){. p->p
2d20: 48 61 73 68 20 3d 20 70 54 61 62 6c 65 2d 3e 70 Hash = pTable->p
2d30: 48 61 73 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Hash;. }.
2d40: 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 44 65 }. sqliteDe
2d50: 6c 65 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 leteTable(pParse
2d60: 2d 3e 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 20 ->db, pTable);.
2d70: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 }.}../*.** Crea
2d80: 74 65 20 61 20 6e 65 77 20 69 6e 64 65 78 20 66 te a new index f
2d90: 6f 72 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 2e or an SQL table.
2da0: 20 20 70 49 6e 64 65 78 20 69 73 20 74 68 65 20 pIndex is the
2db0: 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 name of the inde
2dc0: 78 20 0a 2a 2a 20 61 6e 64 20 70 54 61 62 6c 65 x .** and pTable
2dd0: 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
2de0: 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 the table that i
2df0: 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e s to be indexed.
2e00: 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a 2a 20 Both will .**
2e10: 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20 70 72 be NULL for a pr
2e20: 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 6e 20 74 imary key. In t
2e30: 68 61 74 20 63 61 73 65 2c 20 75 73 65 20 70 50 hat case, use pP
2e40: 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 arse->pNewTable
2e50: 61 73 20 74 68 65 20 0a 2a 2a 20 74 61 62 6c 65 as the .** table
2e60: 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 0a to be indexed..
2e70: 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73 20 61 **.** pList is a
2e80: 20 6c 69 73 74 20 6f 66 20 66 69 65 6c 64 73 20 list of fields
2e90: 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 to be indexed.
2ea0: 70 4c 69 73 74 20 77 69 6c 6c 20 62 65 20 4e 55 pList will be NU
2eb0: 4c 4c 20 69 66 20 74 68 65 0a 2a 2a 20 6d 6f 73 LL if the.** mos
2ec0: 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 t recently added
2ed0: 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61 field of the ta
2ee0: 62 6c 65 20 69 73 20 6c 61 62 65 6c 65 64 20 61 ble is labeled a
2ef0: 73 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 s the primary ke
2f00: 79 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 y..*/.void sqlit
2f10: 65 43 72 65 61 74 65 49 6e 64 65 78 28 0a 20 20 eCreateIndex(.
2f20: 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
2f30: 20 2f 2a 20 41 6c 6c 20 69 6e 66 6f 72 6d 61 74 /* All informat
2f40: 69 6f 6e 20 61 62 6f 75 74 20 74 68 69 73 20 70 ion about this p
2f50: 61 72 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 arse */. Token
2f60: 2a 70 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 *pName, /* Na
2f70: 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e me of the index.
2f80: 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f May be NULL */
2f90: 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 . Token *pTable
2fa0: 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 , /* Name of t
2fb0: 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 64 65 he table to inde
2fc0: 78 2e 20 20 55 73 65 20 70 50 61 72 73 65 2d 3e x. Use pParse->
2fd0: 70 4e 65 77 54 61 62 6c 65 20 69 66 20 30 20 2a pNewTable if 0 *
2fe0: 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 /. IdList *pLis
2ff0: 74 2c 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f t, /* A list o
3000: 66 20 66 69 65 6c 64 73 20 74 6f 20 62 65 20 69 f fields to be i
3010: 6e 64 65 78 65 64 20 2a 2f 0a 20 20 54 6f 6b 65 ndexed */. Toke
3020: 6e 20 2a 70 53 74 61 72 74 2c 20 20 20 2f 2a 20 n *pStart, /*
3030: 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e The CREATE token
3040: 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 20 43 that begins a C
3050: 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
3060: 65 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e ement */. Token
3070: 20 2a 70 45 6e 64 20 20 20 20 20 20 2f 2a 20 54 *pEnd /* T
3080: 68 65 20 22 29 22 20 74 68 61 74 20 63 6c 6f 73 he ")" that clos
3090: 65 73 20 74 68 65 20 43 52 45 41 54 45 20 49 4e es the CREATE IN
30a0: 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f DEX statement */
30b0: 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 .){. Table *pTa
30c0: 62 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 b; /* Table
30d0: 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f to be indexed */
30e0: 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
30f0: 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 ; /* The index
3100: 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 2a to be created *
3110: 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 /. char *zName
3120: 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c = 0;. int i, j,
3130: 20 68 3b 0a 20 20 54 6f 6b 65 6e 20 6e 75 6c 6c h;. Token null
3140: 49 64 3b 20 20 20 20 2f 2a 20 46 61 6b 65 20 74 Id; /* Fake t
3150: 6f 6b 65 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74 oken for an empt
3160: 79 20 49 44 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 y ID list */..
3170: 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 /*. ** Find the
3180: 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 table that is t
3190: 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 52 o be indexed. R
31a0: 65 74 75 72 6e 20 65 61 72 6c 79 20 69 66 20 6e eturn early if n
31b0: 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 ot found.. */.
31c0: 20 69 66 28 20 70 54 61 62 6c 65 21 3d 30 20 29 if( pTable!=0 )
31d0: 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 20 73 71 {. pTab = sq
31e0: 6c 69 74 65 54 61 62 6c 65 46 72 6f 6d 54 6f 6b liteTableFromTok
31f0: 65 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c en(pParse, pTabl
3200: 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 e);. }else{.
3210: 20 70 54 61 62 20 3d 20 20 70 50 61 72 73 65 2d pTab = pParse-
3220: 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 7d 0a >pNewTable;. }.
3230: 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c if( pTab==0 ||
3240: 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 pParse->nErr )
3250: 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
3260: 5f 69 6e 64 65 78 3b 0a 20 20 69 66 28 20 70 54 _index;. if( pT
3270: 61 62 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a ab->readOnly ){.
3280: 20 20 20 20 73 71 6c 69 74 65 53 65 74 53 74 72 sqliteSetStr
3290: 69 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 ing(&pParse->zEr
32a0: 72 4d 73 67 2c 20 22 74 61 62 6c 65 20 22 2c 20 rMsg, "table ",
32b0: 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 pTab->zName, .
32c0: 20 20 20 20 22 20 6d 61 79 20 6e 6f 74 20 68 61 " may not ha
32d0: 76 65 20 6e 65 77 20 69 6e 64 69 63 65 73 20 61 ve new indices a
32e0: 64 64 65 64 22 2c 20 30 29 3b 0a 20 20 20 20 70 dded", 0);. p
32f0: 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 Parse->nErr++;.
3300: 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
3310: 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 0a ate_index;. }..
3320: 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 /*. ** Find t
3330: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 he name of the i
3340: 6e 64 65 78 2e 20 20 4d 61 6b 65 20 73 75 72 65 ndex. Make sure
3350: 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 61 6c there is not al
3360: 72 65 61 64 79 20 61 6e 6f 74 68 65 72 0a 20 20 ready another.
3370: 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c ** index or tabl
3380: 65 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 e with the same
3390: 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 name.. */. if(
33a0: 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e pName ){. zN
33b0: 61 6d 65 20 3d 20 73 71 6c 69 74 65 54 61 62 6c ame = sqliteTabl
33c0: 65 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 eNameFromToken(p
33d0: 4e 61 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Name);. }else{.
33e0: 20 20 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 zName = 0;.
33f0: 20 20 20 73 71 6c 69 74 65 53 65 74 53 74 72 69 sqliteSetStri
3400: 6e 67 28 26 7a 4e 61 6d 65 2c 20 70 54 61 62 2d ng(&zName, pTab-
3410: 3e 7a 4e 61 6d 65 2c 20 22 5f 5f 70 72 69 6d 61 >zName, "__prima
3420: 72 79 5f 6b 65 79 22 2c 20 30 29 3b 0a 20 20 7d ry_key", 0);. }
3430: 0a 20 20 69 66 28 20 73 71 6c 69 74 65 46 69 6e . if( sqliteFin
3440: 64 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 dIndex(pParse->d
3450: 62 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 b, zName) ){.
3460: 20 73 71 6c 69 74 65 53 65 74 53 74 72 69 6e 67 sqliteSetString
3470: 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 (&pParse->zErrMs
3480: 67 2c 20 22 69 6e 64 65 78 20 5c 22 22 2c 20 7a g, "index \"", z
3490: 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 22 5c Name, . "\
34a0: 22 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 " already exists
34b0: 22 2c 20 30 29 3b 0a 20 20 20 20 70 50 61 72 73 ", 0);. pPars
34c0: 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 e->nErr++;. g
34d0: 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
34e0: 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28 index;. }. if(
34f0: 20 73 71 6c 69 74 65 46 69 6e 64 54 61 62 6c 65 sqliteFindTable
3500: 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 (pParse->db, zNa
3510: 6d 65 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 me) ){. sqlit
3520: 65 53 65 74 53 74 72 69 6e 67 28 26 70 50 61 72 eSetString(&pPar
3530: 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 74 68 se->zErrMsg, "th
3540: 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 ere is already a
3550: 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 5c 22 22 table named \""
3560: 2c 0a 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 ,. zName,
3570: 22 5c 22 22 2c 20 30 29 3b 0a 20 20 20 20 70 50 "\"", 0);. pP
3580: 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 arse->nErr++;.
3590: 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 goto exit_crea
35a0: 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 0a 20 te_index;. }..
35b0: 20 2f 2a 20 49 66 20 70 4c 69 73 74 3d 3d 30 2c /* If pList==0,
35c0: 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 72 it means this r
35d0: 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 outine was calle
35e0: 64 20 74 6f 20 6d 61 6b 65 20 61 20 70 72 69 6d d to make a prim
35f0: 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20 6f 75 74 ary. ** key out
3600: 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 of the last fie
3610: 6c 64 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 ld added to the
3620: 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 table under cons
3630: 74 72 75 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 truction.. ** S
3640: 6f 20 63 72 65 61 74 65 20 61 20 66 61 6b 65 20 o create a fake
3650: 6c 69 73 74 20 74 6f 20 73 69 6d 75 6c 61 74 65 list to simulate
3660: 20 74 68 69 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 this.. */. if
3670: 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 ( pList==0 ){.
3680: 20 20 6e 75 6c 6c 49 64 2e 7a 20 3d 20 70 54 61 nullId.z = pTa
3690: 62 2d 3e 61 7a 43 6f 6c 5b 70 54 61 62 2d 3e 6e b->azCol[pTab->n
36a0: 43 6f 6c 2d 31 5d 3b 0a 20 20 20 20 6e 75 6c 6c Col-1];. null
36b0: 49 64 2e 6e 20 3d 20 73 74 72 6c 65 6e 28 6e 75 Id.n = strlen(nu
36c0: 6c 6c 49 64 2e 7a 29 3b 0a 20 20 20 20 70 4c 69 llId.z);. pLi
36d0: 73 74 20 3d 20 73 71 6c 69 74 65 49 64 4c 69 73 st = sqliteIdLis
36e0: 74 41 70 70 65 6e 64 28 30 2c 20 26 6e 75 6c 6c tAppend(0, &null
36f0: 49 64 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 Id);. if( pLi
3700: 73 74 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 st==0 ) goto exi
3710: 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
3720: 20 20 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 }.. /* . **
3730: 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 Allocate the ind
3740: 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 0a 20 ex structure. .
3750: 20 2a 2f 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 */. pIndex = s
3760: 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a qliteMalloc( siz
3770: 65 6f 66 28 49 6e 64 65 78 29 20 2b 20 73 74 72 eof(Index) + str
3780: 6c 65 6e 28 7a 4e 61 6d 65 29 20 2b 20 0a 20 20 len(zName) + .
3790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
37a0: 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 sizeof(int
37b0: 29 2a 70 4c 69 73 74 2d 3e 6e 49 64 20 29 3b 0a )*pList->nId );.
37c0: 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 if( pIndex==0
37d0: 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 53 65 74 ){. sqliteSet
37e0: 53 74 72 69 6e 67 28 26 70 50 61 72 73 65 2d 3e String(&pParse->
37f0: 7a 45 72 72 4d 73 67 2c 20 22 6f 75 74 20 6f 66 zErrMsg, "out of
3800: 20 6d 65 6d 6f 72 79 22 2c 20 30 29 3b 0a 20 20 memory", 0);.
3810: 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b pParse->nErr++
3820: 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f ;. goto exit_
3830: 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
3840: 7d 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69 46 69 }. pIndex->aiFi
3850: 65 6c 64 20 3d 20 28 69 6e 74 2a 29 26 70 49 6e eld = (int*)&pIn
3860: 64 65 78 5b 31 5d 3b 0a 20 20 70 49 6e 64 65 78 dex[1];. pIndex
3870: 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a ->zName = (char*
3880: 29 26 70 49 6e 64 65 78 2d 3e 61 69 46 69 65 6c )&pIndex->aiFiel
3890: 64 5b 70 4c 69 73 74 2d 3e 6e 49 64 5d 3b 0a 20 d[pList->nId];.
38a0: 20 73 74 72 63 70 79 28 70 49 6e 64 65 78 2d 3e strcpy(pIndex->
38b0: 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 zName, zName);.
38c0: 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 20 pIndex->pTable
38d0: 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 65 78 = pTab;. pIndex
38e0: 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4c 69 73 74 ->nField = pList
38f0: 2d 3e 6e 49 64 3b 0a 0a 20 20 2f 2a 20 53 63 61 ->nId;.. /* Sca
3900: 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 n the names of t
3910: 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 he fields of the
3920: 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 table to be ind
3930: 65 78 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f exed and. ** lo
3940: 61 64 20 74 68 65 20 66 69 65 6c 64 20 69 6e 64 ad the field ind
3950: 69 63 65 73 20 69 6e 74 6f 20 74 68 65 20 49 6e ices into the In
3960: 64 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 20 dex structure.
3970: 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 0a Report an error.
3980: 20 20 2a 2a 20 69 66 20 61 6e 79 20 66 69 65 6c ** if any fiel
3990: 64 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a d is not found..
39a0: 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
39b0: 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b i<pList->nId; i+
39c0: 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b +){. for(j=0;
39d0: 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a j<pTab->nCol; j
39e0: 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 ++){. if( s
39f0: 71 6c 69 74 65 53 74 72 49 43 6d 70 28 70 4c 69 qliteStrICmp(pLi
3a00: 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 st->a[i].zName,
3a10: 70 54 61 62 2d 3e 61 7a 43 6f 6c 5b 6a 5d 29 3d pTab->azCol[j])=
3a20: 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
3a30: 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 }. if( j>=pTa
3a40: 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 b->nCol ){.
3a50: 20 73 71 6c 69 74 65 53 65 74 53 74 72 69 6e 67 sqliteSetString
3a60: 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 (&pParse->zErrMs
3a70: 67 2c 20 22 74 61 62 6c 65 20 22 2c 20 70 54 61 g, "table ", pTa
3a80: 62 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 b->zName, .
3a90: 20 20 20 22 20 68 61 73 20 6e 6f 20 66 69 65 6c " has no fiel
3aa0: 64 20 6e 61 6d 65 64 20 22 2c 20 70 4c 69 73 74 d named ", pList
3ab0: 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 30 29 ->a[i].zName, 0)
3ac0: 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e ;. pParse->
3ad0: 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 73 71 nErr++;. sq
3ae0: 6c 69 74 65 46 72 65 65 28 70 49 6e 64 65 78 29 liteFree(pIndex)
3af0: 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 ;. goto exi
3b00: 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
3b10: 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 }. pIndex
3b20: 2d 3e 61 69 46 69 65 6c 64 5b 69 5d 20 3d 20 6a ->aiField[i] = j
3b30: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b ;. }.. /* Link
3b40: 20 74 68 65 20 6e 65 77 20 49 6e 64 65 78 20 73 the new Index s
3b50: 74 72 75 63 74 75 72 65 20 74 6f 20 69 74 73 20 tructure to its
3b60: 74 61 62 6c 65 20 61 6e 64 20 74 6f 20 74 68 65 table and to the
3b70: 20 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e 2d 6d other. ** in-m
3b80: 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 73 emory database s
3b90: 74 72 75 63 74 75 72 65 73 2e 0a 20 20 2a 2f 0a tructures.. */.
3ba0: 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 if( pParse->ex
3bb0: 70 6c 61 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 plain==0 ){.
3bc0: 68 20 3d 20 73 71 6c 69 74 65 48 61 73 68 4e 6f h = sqliteHashNo
3bd0: 43 61 73 65 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 Case(pIndex->zNa
3be0: 6d 65 2c 20 30 29 20 25 20 4e 5f 48 41 53 48 3b me, 0) % N_HASH;
3bf0: 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 48 61 . pIndex->pHa
3c00: 73 68 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d sh = pParse->db-
3c10: 3e 61 70 49 64 78 48 61 73 68 5b 68 5d 3b 0a 20 >apIdxHash[h];.
3c20: 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 pParse->db->a
3c30: 70 49 64 78 48 61 73 68 5b 68 5d 20 3d 20 70 49 pIdxHash[h] = pI
3c40: 6e 64 65 78 3b 0a 20 20 20 20 70 49 6e 64 65 78 ndex;. pIndex
3c50: 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e ->pNext = pTab->
3c60: 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61 62 pIndex;. pTab
3c70: 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 ->pIndex = pInde
3c80: 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 x;. }.. /* If
3c90: 74 68 65 20 69 6e 69 74 46 6c 61 67 20 69 73 20 the initFlag is
3ca0: 30 20 74 68 65 6e 20 63 72 65 61 74 65 20 74 68 0 then create th
3cb0: 65 20 69 6e 64 65 78 20 6f 6e 20 64 69 73 6b 2e e index on disk.
3cc0: 20 20 54 68 69 73 0a 20 20 2a 2a 20 69 6e 76 6f This. ** invo
3cd0: 6c 76 65 73 20 77 72 69 74 69 6e 67 20 74 68 65 lves writing the
3ce0: 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 index into the
3cf0: 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 master table and
3d00: 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 0a filling in the.
3d10: 20 20 2a 2a 20 69 6e 64 65 78 20 77 69 74 68 20 ** index with
3d20: 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c the current tabl
3d30: 65 20 63 6f 6e 74 65 6e 74 73 2e 0a 20 20 2a 2a e contents.. **
3d40: 0a 20 20 2a 2a 20 54 68 65 20 69 6e 69 74 46 6c . ** The initFl
3d50: 61 67 20 69 73 20 30 20 77 68 65 6e 20 74 68 65 ag is 0 when the
3d60: 20 75 73 65 72 20 66 69 72 73 74 20 65 6e 74 65 user first ente
3d70: 72 73 20 61 20 43 52 45 41 54 45 20 49 4e 44 45 rs a CREATE INDE
3d80: 58 20 0a 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64 2e X . ** command.
3d90: 20 20 54 68 65 20 69 6e 69 74 46 6c 61 67 20 69 The initFlag i
3da0: 73 20 31 20 77 68 65 6e 20 61 20 64 61 74 61 62 s 1 when a datab
3db0: 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 61 6e ase is opened an
3dc0: 64 20 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 49 d . ** CREATE I
3dd0: 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 73 20 NDEX statements
3de0: 61 72 65 20 72 65 61 64 20 6f 75 74 20 6f 66 20 are read out of
3df0: 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 the master table
3e00: 2e 20 20 49 6e 0a 20 20 2a 2a 20 74 68 65 20 6c . In. ** the l
3e10: 61 74 74 65 72 20 63 61 73 65 20 74 68 65 20 69 atter case the i
3e20: 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69 ndex already exi
3e30: 73 74 73 20 6f 6e 20 64 69 73 6b 2c 20 77 68 69 sts on disk, whi
3e40: 63 68 20 69 73 20 77 68 79 0a 20 20 2a 2a 20 77 ch is why. ** w
3e50: 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 e don't want to
3e60: 72 65 63 72 65 61 74 65 20 69 74 2e 0a 20 20 2a recreate it.. *
3e70: 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e /. if( pParse->
3e80: 69 6e 69 74 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 initFlag==0 ){.
3e90: 20 20 20 73 74 61 74 69 63 20 56 64 62 65 4f 70 static VdbeOp
3ea0: 20 61 64 64 54 61 62 6c 65 5b 5d 20 3d 20 7b 0a addTable[] = {.
3eb0: 20 20 20 20 20 20 7b 20 4f 50 5f 4f 70 65 6e 2c { OP_Open,
3ec0: 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 4d 41 0, 0, MA
3ed0: 53 54 45 52 5f 4e 41 4d 45 7d 2c 0a 20 20 20 20 STER_NAME},.
3ee0: 20 20 7b 20 4f 50 5f 4e 65 77 2c 20 20 20 20 20 { OP_New,
3ef0: 20 20 20 20 30 2c 20 30 2c 20 30 7d 2c 0a 20 20 0, 0, 0},.
3f00: 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 2c { OP_String,
3f10: 20 20 20 20 20 20 30 2c 20 30 2c 20 22 69 6e 64 0, 0, "ind
3f20: 65 78 22 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 ex"},. { OP
3f30: 5f 53 74 72 69 6e 67 2c 20 20 20 20 20 20 30 2c _String, 0,
3f40: 20 30 2c 20 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0, 0}, /* 3 */
3f50: 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 . { OP_Stri
3f60: 6e 67 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 ng, 0, 0, 0
3f70: 7d 2c 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 }, /* 4 */.
3f80: 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 2c 20 20 { OP_String,
3f90: 20 20 20 20 30 2c 20 30 2c 20 30 7d 2c 20 20 2f 0, 0, 0}, /
3fa0: 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f * 5 */. { O
3fb0: 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 20 34 P_MakeRecord, 4
3fc0: 2c 20 30 2c 20 30 7d 2c 0a 20 20 20 20 20 20 7b , 0, 0},. {
3fd0: 20 4f 50 5f 50 75 74 2c 20 20 20 20 20 20 20 20 OP_Put,
3fe0: 20 30 2c 20 30 2c 20 30 7d 2c 0a 20 20 20 20 20 0, 0, 0},.
3ff0: 20 7b 20 4f 50 5f 43 6c 6f 73 65 2c 20 20 20 20 { OP_Close,
4000: 20 20 20 30 2c 20 30 2c 20 30 7d 2c 0a 20 20 20 0, 0, 0},.
4010: 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 };. int n;.
4020: 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 Vdbe *v = pPa
4030: 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20 rse->pVdbe;.
4040: 69 6e 74 20 6c 62 6c 31 2c 20 6c 62 6c 32 3b 0a int lbl1, lbl2;.
4050: 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20 20 20 20 int i;..
4060: 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 if( v==0 ){.
4070: 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 v = pParse->pV
4080: 64 62 65 20 3d 20 73 71 6c 69 74 65 56 64 62 65 dbe = sqliteVdbe
4090: 43 72 65 61 74 65 28 70 50 61 72 73 65 2d 3e 64 Create(pParse->d
40a0: 62 2d 3e 70 42 65 29 3b 0a 20 20 20 20 7d 0a 20 b->pBe);. }.
40b0: 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f if( v==0 ) go
40c0: 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
40d0: 6e 64 65 78 3b 0a 20 20 20 20 69 66 28 20 70 53 ndex;. if( pS
40e0: 74 61 72 74 20 26 26 20 70 45 6e 64 20 29 7b 0a tart && pEnd ){.
40f0: 20 20 20 20 20 20 69 6e 74 20 62 61 73 65 3b 0a int base;.
4100: 20 20 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 70 n = (int)p
4110: 45 6e 64 2d 3e 7a 20 2d 20 28 69 6e 74 29 70 53 End->z - (int)pS
4120: 74 61 72 74 2d 3e 7a 20 2b 20 31 3b 0a 20 20 20 tart->z + 1;.
4130: 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69 74 65 base = sqlite
4140: 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c VdbeAddOpList(v,
4150: 20 41 72 72 61 79 53 69 7a 65 28 61 64 64 54 61 ArraySize(addTa
4160: 62 6c 65 29 2c 20 61 64 64 54 61 62 6c 65 29 3b ble), addTable);
4170: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 56 64 62 . sqliteVdb
4180: 65 43 68 61 6e 67 65 50 33 28 76 2c 20 62 61 73 eChangeP3(v, bas
4190: 65 2b 33 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 e+3, pIndex->zNa
41a0: 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 me, 0);. sq
41b0: 6c 69 74 65 56 64 62 65 43 68 61 6e 67 65 50 33 liteVdbeChangeP3
41c0: 28 76 2c 20 62 61 73 65 2b 34 2c 20 70 54 61 62 (v, base+4, pTab
41d0: 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 ->zName, 0);.
41e0: 20 20 20 73 71 6c 69 74 65 56 64 62 65 43 68 61 sqliteVdbeCha
41f0: 6e 67 65 50 33 28 76 2c 20 62 61 73 65 2b 35 2c ngeP3(v, base+5,
4200: 20 70 53 74 61 72 74 2d 3e 7a 2c 20 6e 29 3b 0a pStart->z, n);.
4210: 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
4220: 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f VdbeAddOp(v, OP_
4230: 4f 70 65 6e 2c 20 30 2c 20 30 2c 20 70 54 61 62 Open, 0, 0, pTab
4240: 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 ->zName, 0);.
4250: 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 sqliteVdbeAddOp
4260: 28 76 2c 20 4f 50 5f 4f 70 65 6e 2c 20 31 2c 20 (v, OP_Open, 1,
4270: 30 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0, pIndex->zName
4280: 2c 20 30 29 3b 0a 20 20 20 20 6c 62 6c 31 20 3d , 0);. lbl1 =
4290: 20 73 71 6c 69 74 65 56 64 62 65 4d 61 6b 65 4c sqliteVdbeMakeL
42a0: 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 6c 62 6c abel(v);. lbl
42b0: 32 20 3d 20 73 71 6c 69 74 65 56 64 62 65 4d 61 2 = sqliteVdbeMa
42c0: 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 keLabel(v);.
42d0: 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 sqliteVdbeAddOp(
42e0: 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 30 2c 20 6c v, OP_Next, 0, l
42f0: 62 6c 32 2c 20 30 2c 20 6c 62 6c 31 29 3b 0a 20 bl2, 0, lbl1);.
4300: 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 sqliteVdbeAdd
4310: 4f 70 28 76 2c 20 4f 50 5f 4b 65 79 2c 20 30 2c Op(v, OP_Key, 0,
4320: 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 66 0, 0, 0);. f
4330: 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78 or(i=0; i<pIndex
4340: 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b 2b 29 7b 0a ->nField; i++){.
4350: 20 20 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 sqliteVdbe
4360: 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46 69 65 6c AddOp(v, OP_Fiel
4370: 64 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e 61 69 d, 0, pIndex->ai
4380: 46 69 65 6c 64 5b 69 5d 2c 20 30 2c 20 30 29 3b Field[i], 0, 0);
4390: 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
43a0: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
43b0: 5f 4d 61 6b 65 4b 65 79 2c 20 70 49 6e 64 65 78 _MakeKey, pIndex
43c0: 2d 3e 6e 46 69 65 6c 64 2c 20 30 2c 20 30 2c 20 ->nField, 0, 0,
43d0: 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 64 0);. sqliteVd
43e0: 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 75 beAddOp(v, OP_Pu
43f0: 74 49 64 78 2c 20 31 2c 20 30 2c 20 30 2c 20 30 tIdx, 1, 0, 0, 0
4400: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 64 62 );. sqliteVdb
4410: 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 eAddOp(v, OP_Got
4420: 6f 2c 20 30 2c 20 6c 62 6c 31 2c 20 30 2c 20 30 o, 0, lbl1, 0, 0
4430: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 64 62 );. sqliteVdb
4440: 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 6f eAddOp(v, OP_Noo
4450: 70 2c 20 30 2c 20 30 2c 20 30 2c 20 6c 62 6c 32 p, 0, 0, 0, lbl2
4460: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 64 62 );. sqliteVdb
4470: 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f eAddOp(v, OP_Clo
4480: 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b se, 0, 0, 0, 0);
4490: 0a 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 . sqliteVdbeA
44a0: 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 ddOp(v, OP_Close
44b0: 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 , 1, 0, 0, 0);.
44c0: 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c 61 69 6d }.. /* Reclaim
44d0: 20 6d 65 6d 6f 72 79 20 6f 6e 20 61 6e 20 45 58 memory on an EX
44e0: 50 4c 41 49 4e 20 63 61 6c 6c 2e 0a 20 20 2a 2f PLAIN call.. */
44f0: 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 . if( pParse->e
4500: 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 73 71 xplain ){. sq
4510: 6c 69 74 65 46 72 65 65 28 70 49 6e 64 65 78 29 liteFree(pIndex)
4520: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 ;. }.. /* Clea
4530: 6e 20 75 70 20 62 65 66 6f 72 65 20 65 78 69 74 n up before exit
4540: 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72 65 61 ing */.exit_crea
4550: 74 65 5f 69 6e 64 65 78 3a 0a 20 20 73 71 6c 69 te_index:. sqli
4560: 74 65 49 64 4c 69 73 74 44 65 6c 65 74 65 28 70 teIdListDelete(p
4570: 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 46 List);. sqliteF
4580: 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 ree(zName);. re
4590: 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 turn;.}../*.** T
45a0: 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
45b0: 20 64 72 6f 70 20 61 6e 20 65 78 69 73 74 69 6e drop an existin
45c0: 67 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 0a 2a g named index..*
45d0: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 44 72 6f /.void sqliteDro
45e0: 70 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 pIndex(Parse *pP
45f0: 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 arse, Token *pNa
4600: 6d 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 me){. Index *pI
4610: 6e 64 65 78 3b 0a 20 20 63 68 61 72 20 2a 7a 4e ndex;. char *zN
4620: 61 6d 65 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a ame;. Vdbe *v;.
4630: 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 . zName = sqlit
4640: 65 54 61 62 6c 65 4e 61 6d 65 46 72 6f 6d 54 6f eTableNameFromTo
4650: 6b 65 6e 28 70 4e 61 6d 65 29 3b 0a 20 20 70 49 ken(pName);. pI
4660: 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 46 69 6e ndex = sqliteFin
4670: 64 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 dIndex(pParse->d
4680: 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c b, zName);. sql
4690: 69 74 65 46 72 65 65 28 7a 4e 61 6d 65 29 3b 0a iteFree(zName);.
46a0: 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 if( pIndex==0
46b0: 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 53 65 74 ){. sqliteSet
46c0: 4e 53 74 72 69 6e 67 28 26 70 50 61 72 73 65 2d NString(&pParse-
46d0: 3e 7a 45 72 72 4d 73 67 2c 20 22 6e 6f 20 73 75 >zErrMsg, "no su
46e0: 63 68 20 69 6e 64 65 78 3a 20 5c 22 22 2c 20 30 ch index: \"", 0
46f0: 2c 20 0a 20 20 20 20 20 20 20 20 70 4e 61 6d 65 , . pName
4700: 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 2c 20 22 ->z, pName->n, "
4710: 5c 22 22 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 \"", 1, 0);.
4720: 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a pParse->nErr++;.
4730: 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
4740: 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 . /* Generate c
4750: 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 ode to remove th
4760: 65 20 69 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d e index and from
4770: 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c the master tabl
4780: 65 20 2a 2f 0a 20 20 76 20 3d 20 70 50 61 72 73 e */. v = pPars
4790: 65 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74 e->pVdbe = sqlit
47a0: 65 56 64 62 65 43 72 65 61 74 65 28 70 50 61 72 eVdbeCreate(pPar
47b0: 73 65 2d 3e 64 62 2d 3e 70 42 65 29 3b 0a 20 20 se->db->pBe);.
47c0: 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 74 61 if( v ){. sta
47d0: 74 69 63 20 56 64 62 65 4f 70 20 64 72 6f 70 49 tic VdbeOp dropI
47e0: 6e 64 65 78 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 ndex[] = {.
47f0: 20 7b 20 4f 50 5f 4f 70 65 6e 2c 20 20 20 20 20 { OP_Open,
4800: 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 4d 41 0, 0, MA
4810: 53 54 45 52 5f 4e 41 4d 45 7d 2c 0a 20 20 20 20 STER_NAME},.
4820: 20 20 7b 20 4f 50 5f 4c 69 73 74 4f 70 65 6e 2c { OP_ListOpen,
4830: 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 30 0, 0, 0
4840: 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 },. { OP_St
4850: 72 69 6e 67 2c 20 20 20 20 20 30 2c 20 30 2c 20 ring, 0, 0,
4860: 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 32 20 2a 0}, /* 2 *
4870: 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 /. { OP_Nex
4880: 74 2c 20 20 20 20 20 20 20 30 2c 20 41 44 44 52 t, 0, ADDR
4890: 28 39 29 2c 20 30 7d 2c 20 2f 2a 20 33 20 2a 2f (9), 0}, /* 3 */
48a0: 0a 20 20 20 20 20 20 7b 20 4f 50 5f 44 75 70 2c . { OP_Dup,
48b0: 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 20 20 0, 0,
48c0: 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 0},. {
48d0: 4f 50 5f 46 69 65 6c 64 2c 20 20 20 20 20 20 30 OP_Field, 0
48e0: 2c 20 31 2c 20 20 20 20 20 20 20 30 7d 2c 0a 20 , 1, 0},.
48f0: 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 { OP_Ne,
4900: 20 20 20 20 20 20 30 2c 20 41 44 44 52 28 33 29 0, ADDR(3)
4910: 2c 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 , 0},. { OP
4920: 5f 4b 65 79 2c 20 20 20 20 20 20 20 20 30 2c 20 _Key, 0,
4930: 30 2c 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 0, 0},.
4940: 20 20 20 7b 20 4f 50 5f 44 65 6c 65 74 65 2c 20 { OP_Delete,
4950: 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 0, 0,
4960: 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 44 0},. { OP_D
4970: 65 73 74 72 6f 79 2c 20 20 20 20 30 2c 20 30 2c estroy, 0, 0,
4980: 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 39 20 0}, /* 9
4990: 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43 6c */. { OP_Cl
49a0: 6f 73 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 ose, 0, 0,
49b0: 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0},. };
49c0: 0a 20 20 20 20 69 6e 74 20 62 61 73 65 3b 0a 0a . int base;..
49d0: 20 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69 74 base = sqlit
49e0: 65 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 eVdbeAddOpList(v
49f0: 2c 20 41 72 72 61 79 53 69 7a 65 28 64 72 6f 70 , ArraySize(drop
4a00: 49 6e 64 65 78 29 2c 20 64 72 6f 70 49 6e 64 65 Index), dropInde
4a10: 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 64 x);. sqliteVd
4a20: 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 62 61 beChangeP3(v, ba
4a30: 73 65 2b 32 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e se+2, pIndex->zN
4a40: 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c ame, 0);. sql
4a50: 69 74 65 56 64 62 65 43 68 61 6e 67 65 50 33 28 iteVdbeChangeP3(
4a60: 76 2c 20 62 61 73 65 2b 39 2c 20 70 49 6e 64 65 v, base+9, pInde
4a70: 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 x->zName, 0);.
4a80: 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 }.. /* Remove t
4a90: 68 65 20 69 6e 64 65 78 20 73 74 72 75 63 74 75 he index structu
4aa0: 72 65 20 61 6e 64 20 66 72 65 65 20 69 74 73 20 re and free its
4ab0: 6d 65 6d 6f 72 79 2e 20 20 45 78 63 65 70 74 20 memory. Except
4ac0: 69 66 20 74 68 65 0a 20 20 2a 2a 20 45 58 50 4c if the. ** EXPL
4ad0: 41 49 4e 20 6b 65 79 77 6f 72 64 20 69 73 20 70 AIN keyword is p
4ae0: 72 65 73 65 6e 74 2c 20 6e 6f 20 63 68 61 6e 67 resent, no chang
4af0: 65 73 20 61 72 65 20 6d 61 64 65 2e 0a 20 20 2a es are made.. *
4b00: 2f 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d /. if( !pParse-
4b10: 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 >explain ){.
4b20: 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 if( pIndex->pTab
4b30: 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e 64 le->pIndex==pInd
4b40: 65 78 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 ex ){. pInd
4b50: 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 ex->pTable->pInd
4b60: 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 ex = pIndex->pNe
4b70: 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 xt;. }else{.
4b80: 20 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 Index *p;.
4b90: 20 20 20 20 20 66 6f 72 28 70 3d 70 49 6e 64 65 for(p=pInde
4ba0: 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 x->pTable->pInde
4bb0: 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 x; p && p->pNext
4bc0: 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70 2d 3e 70 !=pIndex; p=p->p
4bd0: 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20 20 69 66 Next){}. if
4be0: 28 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d ( p && p->pNext=
4bf0: 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 =pIndex ){.
4c00: 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49 p->pNext = pI
4c10: 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 ndex->pNext;.
4c20: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
4c30: 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65 78 qliteDeleteIndex
4c40: 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 6e (pParse->db, pIn
4c50: 64 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a dex);. }.}../*.
4c60: 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 ** Add a new ele
4c70: 6d 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64 20 ment to the end
4c80: 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e of an expression
4c90: 20 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73 74 list. If pList
4ca0: 20 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 is.** initially
4cb0: 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 NULL, then crea
4cc0: 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 te a new express
4cd0: 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 45 78 70 ion list..*/.Exp
4ce0: 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 45 78 70 rList *sqliteExp
4cf0: 72 4c 69 73 74 41 70 70 65 6e 64 28 45 78 70 72 rListAppend(Expr
4d00: 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 45 78 70 List *pList, Exp
4d10: 72 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 r *pExpr, Token
4d20: 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 *pName){. int i
4d30: 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 ;. if( pList==0
4d40: 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 ){. pList =
4d50: 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 sqliteMalloc( si
4d60: 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 zeof(ExprList) )
4d70: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 ;. }. if( pLis
4d80: 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b t==0 ) return 0;
4d90: 0a 20 20 69 66 28 20 28 70 4c 69 73 74 2d 3e 6e . if( (pList->n
4da0: 45 78 70 72 20 26 20 37 29 3d 3d 30 20 29 7b 0a Expr & 7)==0 ){.
4db0: 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 int n = pLis
4dc0: 74 2d 3e 6e 45 78 70 72 20 2b 20 38 3b 0a 20 20 t->nExpr + 8;.
4dd0: 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 73 71 6c pList->a = sql
4de0: 69 74 65 52 65 61 6c 6c 6f 63 28 70 4c 69 73 74 iteRealloc(pList
4df0: 2d 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28 70 4c ->a, n*sizeof(pL
4e00: 69 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 ist->a[0]));.
4e10: 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 3d 3d 30 if( pList->a==0
4e20: 20 29 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 2d ){. pList-
4e30: 3e 6e 45 78 70 72 20 3d 20 30 3b 0a 20 20 20 20 >nExpr = 0;.
4e40: 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a return pList;.
4e50: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 20 3d 20 }. }. i =
4e60: 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 3b 0a pList->nExpr++;.
4e70: 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 pList->a[i].pE
4e80: 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 70 xpr = pExpr;. p
4e90: 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 List->a[i].zName
4ea0: 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 61 6d = 0;. if( pNam
4eb0: 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 53 e ){. sqliteS
4ec0: 65 74 4e 53 74 72 69 6e 67 28 26 70 4c 69 73 74 etNString(&pList
4ed0: 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70 4e ->a[i].zName, pN
4ee0: 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e ame->z, pName->n
4ef0: 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , 0);. }. retu
4f00: 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a rn pList;.}../*.
4f10: 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 ** Delete an ent
4f20: 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c ire expression l
4f30: 69 73 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ist..*/.void sql
4f40: 69 74 65 45 78 70 72 4c 69 73 74 44 65 6c 65 74 iteExprListDelet
4f50: 65 28 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 e(ExprList *pLis
4f60: 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 t){. int i;. i
4f70: 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 f( pList==0 ) re
4f80: 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b turn;. for(i=0;
4f90: 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b i<pList->nExpr;
4fa0: 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 i++){. sqlit
4fb0: 65 45 78 70 72 44 65 6c 65 74 65 28 70 4c 69 73 eExprDelete(pLis
4fc0: 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a t->a[i].pExpr);.
4fd0: 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 sqliteFree(p
4fe0: 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 List->a[i].zName
4ff0: 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 );. }. sqliteF
5000: 72 65 65 28 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 ree(pList->a);.
5010: 20 73 71 6c 69 74 65 46 72 65 65 28 70 4c 69 73 sqliteFree(pLis
5020: 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 t);.}../*.** App
5030: 65 6e 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e end a new elemen
5040: 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 49 t to the given I
5050: 64 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20 61 dList. Create a
5060: 20 6e 65 77 20 49 64 4c 69 73 74 20 69 66 0a 2a new IdList if.*
5070: 2a 20 6e 65 65 64 20 62 65 2e 0a 2a 2f 0a 49 64 * need be..*/.Id
5080: 4c 69 73 74 20 2a 73 71 6c 69 74 65 49 64 4c 69 List *sqliteIdLi
5090: 73 74 41 70 70 65 6e 64 28 49 64 4c 69 73 74 20 stAppend(IdList
50a0: 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 *pList, Token *p
50b0: 54 6f 6b 65 6e 29 7b 0a 20 20 69 66 28 20 70 4c Token){. if( pL
50c0: 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c ist==0 ){. pL
50d0: 69 73 74 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c ist = sqliteMall
50e0: 6f 63 28 20 73 69 7a 65 6f 66 28 49 64 4c 69 73 oc( sizeof(IdLis
50f0: 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c t) );. if( pL
5100: 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ist==0 ) return
5110: 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 4c 0;. }. if( (pL
5120: 69 73 74 2d 3e 6e 49 64 20 26 20 37 29 3d 3d 30 ist->nId & 7)==0
5130: 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 ){. pList->a
5140: 20 3d 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 = sqliteRealloc
5150: 28 70 4c 69 73 74 2d 3e 61 2c 20 28 70 4c 69 73 (pList->a, (pLis
5160: 74 2d 3e 6e 49 64 2b 38 29 2a 73 69 7a 65 6f 66 t->nId+8)*sizeof
5170: 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 20 29 3b (pList->a[0]) );
5180: 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e . if( pList->
5190: 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c a==0 ){. pL
51a0: 69 73 74 2d 3e 6e 49 64 20 3d 20 30 3b 0a 20 20 ist->nId = 0;.
51b0: 20 20 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 return pList
51c0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 ;. }. }. me
51d0: 6d 73 65 74 28 26 70 4c 69 73 74 2d 3e 61 5b 70 mset(&pList->a[p
51e0: 4c 69 73 74 2d 3e 6e 49 64 5d 2c 20 30 2c 20 73 List->nId], 0, s
51f0: 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 izeof(pList->a[0
5200: 5d 29 29 3b 0a 20 20 69 66 28 20 70 54 6f 6b 65 ]));. if( pToke
5210: 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 53 n ){. sqliteS
5220: 65 74 4e 53 74 72 69 6e 67 28 26 70 4c 69 73 74 etNString(&pList
5230: 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 49 64 5d 2e ->a[pList->nId].
5240: 7a 4e 61 6d 65 2c 20 70 54 6f 6b 65 6e 2d 3e 7a zName, pToken->z
5250: 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 2c 20 30 29 3b , pToken->n, 0);
5260: 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 6e 49 . }. pList->nI
5270: 64 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c d++;. return pL
5280: 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 ist;.}../*.** Ad
5290: 64 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 d an alias to th
52a0: 65 20 6c 61 73 74 20 69 64 65 6e 74 69 66 69 65 e last identifie
52b0: 72 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 69 r on the given i
52c0: 64 65 6e 74 69 66 69 65 72 20 6c 69 73 74 2e 0a dentifier list..
52d0: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 49 64 */.void sqliteId
52e0: 4c 69 73 74 41 64 64 41 6c 69 61 73 28 49 64 4c ListAddAlias(IdL
52f0: 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 ist *pList, Toke
5300: 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 69 66 n *pToken){. if
5310: 28 20 70 4c 69 73 74 20 26 26 20 70 4c 69 73 74 ( pList && pList
5320: 2d 3e 6e 49 64 3e 30 20 29 7b 0a 20 20 20 20 69 ->nId>0 ){. i
5330: 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 49 nt i = pList->nI
5340: 64 20 2d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 d - 1;. sqlit
5350: 65 53 65 74 4e 53 74 72 69 6e 67 28 26 70 4c 69 eSetNString(&pLi
5360: 73 74 2d 3e 61 5b 69 5d 2e 7a 41 6c 69 61 73 2c st->a[i].zAlias,
5370: 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b pToken->z, pTok
5380: 65 6e 2d 3e 6e 2c 20 30 29 3b 0a 20 20 7d 0a 7d en->n, 0);. }.}
5390: 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
53a0: 6e 20 65 6e 74 69 72 65 20 49 64 4c 69 73 74 0a n entire IdList.
53b0: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 49 64 */.void sqliteId
53c0: 4c 69 73 74 44 65 6c 65 74 65 28 49 64 4c 69 73 ListDelete(IdLis
53d0: 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 t *pList){. int
53e0: 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d i;. if( pList=
53f0: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 =0 ) return;. f
5400: 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d or(i=0; i<pList-
5410: 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nId; i++){.
5420: 73 71 6c 69 74 65 46 72 65 65 28 70 4c 69 73 74 sqliteFree(pList
5430: 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 ->a[i].zName);.
5440: 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 4c sqliteFree(pL
5450: 69 73 74 2d 3e 61 5b 69 5d 2e 7a 41 6c 69 61 73 ist->a[i].zAlias
5460: 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 );. }. sqliteF
5470: 72 65 65 28 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 ree(pList->a);.
5480: 20 73 71 6c 69 74 65 46 72 65 65 28 70 4c 69 73 sqliteFree(pLis
5490: 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 t);.}../*.** Thi
54a0: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
54b0: 6c 20 74 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 l to handle SQL
54c0: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
54d0: 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 form:.**.**
54e0: 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c insert into TABL
54f0: 45 20 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65 E (IDLIST) value
5500: 73 28 45 58 50 52 4c 49 53 54 29 0a 2a 2a 0a 2a s(EXPRLIST).**.*
5510: 2a 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 * The parameters
5520: 20 61 72 65 20 74 68 65 20 74 61 62 6c 65 20 6e are the table n
5530: 61 6d 65 20 61 6e 64 20 74 68 65 20 65 78 70 72 ame and the expr
5540: 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a ession list..*/.
5550: 76 6f 69 64 20 73 71 6c 69 74 65 49 6e 73 65 72 void sqliteInser
5560: 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 t(. Parse *pPar
5570: 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 se, /* Pa
5580: 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a rser context */.
5590: 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e Token *pTableN
55a0: 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 ame, /* Name
55b0: 6f 66 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 of table into wh
55c0: 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 ich we are inser
55d0: 74 69 6e 67 20 2a 2f 0a 20 20 45 78 70 72 4c 69 ting */. ExprLi
55e0: 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 st *pList,
55f0: 2f 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 /* List of value
5600: 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 s to be inserted
5610: 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 46 */. IdList *pF
5620: 69 65 6c 64 20 20 20 20 20 20 20 20 2f 2a 20 46 ield /* F
5630: 69 65 6c 64 20 6e 61 6d 65 20 63 6f 72 72 65 73 ield name corres
5640: 70 6f 6e 64 69 6e 67 20 74 6f 20 70 4c 69 73 74 ponding to pList
5650: 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c . Might be NULL
5660: 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a */.){. Table *
5670: 70 54 61 62 3b 0a 20 20 63 68 61 72 20 2a 7a 54 pTab;. char *zT
5680: 61 62 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a ab;. int i, j;.
5690: 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 7a 54 Vdbe *v;.. zT
56a0: 61 62 20 3d 20 73 71 6c 69 74 65 54 61 62 6c 65 ab = sqliteTable
56b0: 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 54 NameFromToken(pT
56c0: 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 70 54 61 ableName);. pTa
56d0: 62 20 3d 20 73 71 6c 69 74 65 46 69 6e 64 54 61 b = sqliteFindTa
56e0: 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ble(pParse->db,
56f0: 7a 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 46 zTab);. sqliteF
5700: 72 65 65 28 7a 54 61 62 29 3b 0a 20 20 69 66 28 ree(zTab);. if(
5710: 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 pTab==0 ){.
5720: 73 71 6c 69 74 65 53 65 74 4e 53 74 72 69 6e 67 sqliteSetNString
5730: 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 (&pParse->zErrMs
5740: 67 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c g, "no such tabl
5750: 65 3a 20 5c 22 22 2c 20 30 2c 20 0a 20 20 20 20 e: \"", 0, .
5760: 20 20 20 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e pTableName->
5770: 7a 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 6e z, pTableName->n
5780: 2c 20 22 5c 22 22 2c 20 31 2c 20 30 29 3b 0a 20 , "\"", 1, 0);.
5790: 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b pParse->nErr+
57a0: 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 +;. goto inse
57b0: 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a rt_cleanup;. }.
57c0: 20 20 69 66 28 20 70 54 61 62 2d 3e 72 65 61 64 if( pTab->read
57d0: 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 Only ){. sqli
57e0: 74 65 53 65 74 53 74 72 69 6e 67 28 26 70 50 61 teSetString(&pPa
57f0: 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 74 rse->zErrMsg, "t
5800: 61 62 6c 65 20 5c 22 22 2c 20 70 54 61 62 2d 3e able \"", pTab->
5810: 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 22 zName,. "
5820: 5c 22 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f \" may not be mo
5830: 64 69 66 69 65 64 22 2c 20 30 29 3b 0a 20 20 20 dified", 0);.
5840: 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b pParse->nErr++;
5850: 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 . goto insert
5860: 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 _cleanup;. }.
5870: 69 66 28 20 70 46 69 65 6c 64 3d 3d 30 20 26 26 if( pField==0 &&
5880: 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70 pList->nExpr!=p
5890: 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 Tab->nCol ){.
58a0: 20 63 68 61 72 20 7a 4e 75 6d 31 5b 33 30 5d 3b char zNum1[30];
58b0: 0a 20 20 20 20 63 68 61 72 20 7a 4e 75 6d 32 5b . char zNum2[
58c0: 33 30 5d 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 30];. sprintf
58d0: 28 7a 4e 75 6d 31 2c 22 25 64 22 2c 20 70 4c 69 (zNum1,"%d", pLi
58e0: 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 st->nExpr);.
58f0: 73 70 72 69 6e 74 66 28 7a 4e 75 6d 32 2c 22 25 sprintf(zNum2,"%
5900: 64 22 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b d", pTab->nCol);
5910: 0a 20 20 20 20 73 71 6c 69 74 65 53 65 74 53 74 . sqliteSetSt
5920: 72 69 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 ring(&pParse->zE
5930: 72 72 4d 73 67 2c 20 22 74 61 62 6c 65 20 22 2c rrMsg, "table ",
5940: 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 pTab->zName,.
5950: 20 20 20 20 20 22 20 68 61 73 20 22 2c 20 7a 4e " has ", zN
5960: 75 6d 32 2c 20 22 20 63 6f 6c 75 6d 6e 73 20 62 um2, " columns b
5970: 75 74 20 6f 6e 6c 79 20 22 2c 0a 20 20 20 20 20 ut only ",.
5980: 20 20 7a 4e 75 6d 31 2c 20 22 20 76 61 6c 75 65 zNum1, " value
5990: 73 20 77 65 72 65 20 73 75 70 70 6c 69 65 64 22 s were supplied"
59a0: 2c 20 30 29 3b 0a 20 20 20 20 70 50 61 72 73 65 , 0);. pParse
59b0: 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f ->nErr++;. go
59c0: 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 to insert_cleanu
59d0: 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 p;. }. if( pFi
59e0: 65 6c 64 21 3d 30 20 26 26 20 70 4c 69 73 74 2d eld!=0 && pList-
59f0: 3e 6e 45 78 70 72 21 3d 70 46 69 65 6c 64 2d 3e >nExpr!=pField->
5a00: 6e 49 64 20 29 7b 0a 20 20 20 20 63 68 61 72 20 nId ){. char
5a10: 7a 4e 75 6d 31 5b 33 30 5d 3b 0a 20 20 20 20 63 zNum1[30];. c
5a20: 68 61 72 20 7a 4e 75 6d 32 5b 33 30 5d 3b 0a 20 har zNum2[30];.
5a30: 20 20 20 73 70 72 69 6e 74 66 28 7a 4e 75 6d 31 sprintf(zNum1
5a40: 2c 22 25 64 22 2c 20 70 4c 69 73 74 2d 3e 6e 45 ,"%d", pList->nE
5a50: 78 70 72 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 xpr);. sprint
5a60: 66 28 7a 4e 75 6d 32 2c 22 25 64 22 2c 20 70 54 f(zNum2,"%d", pT
5a70: 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 ab->nCol);. s
5a80: 71 6c 69 74 65 53 65 74 53 74 72 69 6e 67 28 26 qliteSetString(&
5a90: 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c pParse->zErrMsg,
5aa0: 20 7a 4e 75 6d 31 2c 20 22 20 76 61 6c 75 65 73 zNum1, " values
5ab0: 20 66 6f 72 20 22 2c 0a 20 20 20 20 20 20 20 7a for ",. z
5ac0: 4e 75 6d 32 2c 20 22 20 63 6f 6c 75 6d 6e 73 22 Num2, " columns"
5ad0: 2c 20 30 29 3b 0a 20 20 20 20 70 50 61 72 73 65 , 0);. pParse
5ae0: 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f ->nErr++;. go
5af0: 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 to insert_cleanu
5b00: 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 p;. }. if( pFi
5b10: 65 6c 64 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 eld ){. for(i
5b20: 3d 30 3b 20 69 3c 70 46 69 65 6c 64 2d 3e 6e 49 =0; i<pField->nI
5b30: 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 d; i++){. p
5b40: 46 69 65 6c 64 2d 3e 61 5b 69 5d 2e 69 64 78 20 Field->a[i].idx
5b50: 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = -1;. }.
5b60: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 69 65 6c for(i=0; i<pFiel
5b70: 64 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 d->nId; i++){.
5b80: 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 for(j=0; j<p
5b90: 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b Tab->nCol; j++){
5ba0: 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
5bb0: 69 74 65 53 74 72 49 43 6d 70 28 70 46 69 65 6c iteStrICmp(pFiel
5bc0: 64 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70 d->a[i].zName, p
5bd0: 54 61 62 2d 3e 61 7a 43 6f 6c 5b 6a 5d 29 3d 3d Tab->azCol[j])==
5be0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 0 ){. p
5bf0: 46 69 65 6c 64 2d 3e 61 5b 69 5d 2e 69 64 78 20 Field->a[i].idx
5c00: 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 = j;. b
5c10: 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
5c20: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
5c30: 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 ( j>=pTab->nCol
5c40: 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
5c50: 65 53 65 74 53 74 72 69 6e 67 28 26 70 50 61 72 eSetString(&pPar
5c60: 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 74 61 se->zErrMsg, "ta
5c70: 62 6c 65 20 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 ble ", pTab->zNa
5c80: 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 22 me,. "
5c90: 20 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e has no column n
5ca0: 61 6d 65 64 20 22 2c 20 70 46 69 65 6c 64 2d 3e amed ", pField->
5cb0: 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a a[i].zName, 0);.
5cc0: 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
5cd0: 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nErr++;.
5ce0: 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 goto insert_clea
5cf0: 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nup;. }.
5d00: 20 7d 0a 20 20 7d 0a 20 20 76 20 3d 20 70 50 61 }. }. v = pPa
5d10: 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c rse->pVdbe = sql
5d20: 69 74 65 56 64 62 65 43 72 65 61 74 65 28 70 50 iteVdbeCreate(pP
5d30: 61 72 73 65 2d 3e 64 62 2d 3e 70 42 65 29 3b 0a arse->db->pBe);.
5d40: 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 49 if( v ){. I
5d50: 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 ndex *pIdx;.
5d60: 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 sqliteVdbeAddOp(
5d70: 76 2c 20 4f 50 5f 4f 70 65 6e 2c 20 30 2c 20 30 v, OP_Open, 0, 0
5d80: 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 , pTab->zName, 0
5d90: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 64 62 );. sqliteVdb
5da0: 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 77 eAddOp(v, OP_New
5db0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 , 0, 0, 0, 0);.
5dc0: 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e if( pTab->pIn
5dd0: 64 65 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c dex ){. sql
5de0: 69 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 iteVdbeAddOp(v,
5df0: 4f 50 5f 44 75 70 2c 20 30 2c 20 30 2c 20 30 2c OP_Dup, 0, 0, 0,
5e00: 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 0);. }. f
5e10: 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e or(i=0; i<pTab->
5e20: 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCol; i++){.
5e30: 20 20 69 66 28 20 70 46 69 65 6c 64 3d 3d 30 20 if( pField==0
5e40: 29 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 69 ){. j = i
5e50: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
5e60: 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 for(j=0;
5e70: 6a 3c 70 46 69 65 6c 64 2d 3e 6e 49 64 3b 20 6a j<pField->nId; j
5e80: 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ++){. i
5e90: 66 28 20 70 46 69 65 6c 64 2d 3e 61 5b 6a 5d 2e f( pField->a[j].
5ea0: 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a idx==i ) break;.
5eb0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
5ec0: 7d 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 65 }. if( pFie
5ed0: 6c 64 20 26 26 20 6a 3e 3d 70 46 69 65 6c 64 2d ld && j>=pField-
5ee0: 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20 20 20 20 >nId ){.
5ef0: 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 sqliteVdbeAddOp(
5f00: 76 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20 30 2c v, OP_String, 0,
5f10: 20 30 2c 20 22 22 2c 20 30 29 3b 0a 20 20 20 20 0, "", 0);.
5f20: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
5f30: 20 73 71 6c 69 74 65 45 78 70 72 43 6f 64 65 28 sqliteExprCode(
5f40: 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 pParse, pList->a
5f50: 5b 6a 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 [j].pExpr);.
5f60: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
5f70: 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c liteVdbeAddOp(v,
5f80: 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 OP_MakeRecord,
5f90: 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 30 2c 20 30 pTab->nCol, 0, 0
5fa0: 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 0);. sqlite
5fb0: 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f VdbeAddOp(v, OP_
5fc0: 50 75 74 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 Put, 0, 0, 0, 0)
5fd0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 ;. sqliteVdbe
5fe0: 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 AddOp(v, OP_Clos
5ff0: 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a e, 0, 0, 0, 0);.
6000: 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 for(pIdx=pTa
6010: 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b b->pIndex; pIdx;
6020: 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 pIdx=pIdx->pNex
6030: 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 t){. if( pI
6040: 64 78 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 dx->pNext ){.
6050: 20 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 sqliteVdbeA
6060: 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 ddOp(v, OP_Dup,
6070: 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 0, 0, 0, 0);.
6080: 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
6090: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
60a0: 5f 4f 70 65 6e 2c 20 30 2c 20 30 2c 20 70 49 64 _Open, 0, 0, pId
60b0: 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 x->zName, 0);.
60c0: 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
60d0: 49 64 78 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b 2b Idx->nField; i++
60e0: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 ){. int i
60f0: 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 46 69 65 dx = pIdx->aiFie
6100: 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 69 ld[i];. i
6110: 66 28 20 70 46 69 65 6c 64 3d 3d 30 20 29 7b 0a f( pField==0 ){.
6120: 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20 69 64 j = id
6130: 78 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 x;. }else
6140: 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 {. for(
6150: 6a 3d 30 3b 20 6a 3c 70 46 69 65 6c 64 2d 3e 6e j=0; j<pField->n
6160: 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 Id; j++){.
6170: 20 20 20 20 20 20 69 66 28 20 70 46 69 65 6c 64 if( pField
6180: 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 64 78 20 ->a[j].idx==idx
6190: 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ) break;.
61a0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
61b0: 20 20 20 20 20 20 20 69 66 28 20 70 46 69 65 6c if( pFiel
61c0: 64 20 26 26 20 6a 3e 3d 70 46 69 65 6c 64 2d 3e d && j>=pField->
61d0: 6e 49 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 nId ){.
61e0: 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 sqliteVdbeAddOp
61f0: 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20 30 (v, OP_String, 0
6200: 2c 20 30 2c 20 22 22 2c 20 30 29 3b 0a 20 20 20 , 0, "", 0);.
6210: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
6220: 20 20 20 20 20 20 73 71 6c 69 74 65 45 78 70 72 sqliteExpr
6230: 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 Code(pParse, pLi
6240: 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 29 3b st->a[j].pExpr);
6250: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
6260: 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 56 }. sqliteV
6270: 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d dbeAddOp(v, OP_M
6280: 61 6b 65 4b 65 79 2c 20 70 49 64 78 2d 3e 6e 46 akeKey, pIdx->nF
6290: 69 65 6c 64 2c 20 30 2c 20 30 2c 20 30 29 3b 0a ield, 0, 0, 0);.
62a0: 20 20 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 sqliteVdbe
62b0: 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 75 74 49 AddOp(v, OP_PutI
62c0: 64 78 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b dx, 0, 0, 0, 0);
62d0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 56 64 62 . sqliteVdb
62e0: 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f eAddOp(v, OP_Clo
62f0: 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b se, 0, 0, 0, 0);
6300: 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 69 6e 73 65 . }. }..inse
6310: 72 74 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 rt_cleanup:. sq
6320: 6c 69 74 65 45 78 70 72 4c 69 73 74 44 65 6c 65 liteExprListDele
6330: 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c te(pList);. sql
6340: 69 74 65 49 64 4c 69 73 74 44 65 6c 65 74 65 28 iteIdListDelete(
6350: 70 46 69 65 6c 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pField);.}../*.*
6360: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
6370: 61 6c 6b 73 20 61 6e 20 65 78 70 72 65 73 73 69 alks an expressi
6380: 6f 6e 20 74 72 65 65 20 61 6e 64 20 72 65 73 6f on tree and reso
6390: 6c 76 65 73 20 72 65 66 65 72 65 6e 63 65 73 20 lves references
63a0: 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 66 69 65 6c to.** table fiel
63b0: 64 73 2e 20 20 4e 6f 64 65 73 20 6f 66 20 74 68 ds. Nodes of th
63c0: 65 20 66 6f 72 6d 20 49 44 2e 49 44 20 6f 72 20 e form ID.ID or
63d0: 49 44 20 72 65 73 6f 6c 76 65 20 69 6e 74 6f 20 ID resolve into
63e0: 61 6e 0a 2a 2a 20 69 6e 64 65 78 20 74 6f 20 74 an.** index to t
63f0: 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 he table in the
6400: 74 61 62 6c 65 20 6c 69 73 74 20 61 6e 64 20 61 table list and a
6410: 20 66 69 65 6c 64 20 6f 66 66 73 65 74 2e 20 20 field offset.
6420: 54 68 65 20 6f 70 63 6f 64 65 0a 2a 2a 20 66 6f The opcode.** fo
6430: 72 20 73 75 63 68 20 6e 6f 64 65 73 20 69 73 20 r such nodes is
6440: 63 68 61 6e 67 65 64 20 74 6f 20 54 4b 5f 46 49 changed to TK_FI
6450: 45 4c 44 2e 20 20 54 68 65 20 69 54 61 62 6c 65 ELD. The iTable
6460: 20 76 61 6c 75 65 20 69 73 20 63 68 61 6e 67 65 value is change
6470: 64 0a 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65 d.** to the inde
6480: 78 20 6f 66 20 74 68 65 20 72 65 66 65 72 65 6e x of the referen
6490: 63 65 64 20 74 61 62 6c 65 20 69 6e 20 70 54 61 ced table in pTa
64a0: 62 4c 69 73 74 2c 20 61 6e 64 20 74 68 65 20 69 bList, and the i
64b0: 46 69 65 6c 64 20 76 61 6c 75 65 0a 2a 2a 20 69 Field value.** i
64c0: 73 20 63 68 61 6e 67 65 64 20 74 6f 20 74 68 65 s changed to the
64d0: 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 66 69 index of the fi
64e0: 65 6c 64 20 6f 66 20 74 68 65 20 72 65 66 65 72 eld of the refer
64f0: 65 6e 63 65 64 20 74 61 62 6c 65 2e 0a 2a 2a 0a enced table..**.
6500: 2a 2a 20 55 6e 6b 6e 6f 77 6e 20 66 69 65 6c 64 ** Unknown field
6510: 73 20 6f 72 20 74 61 62 6c 65 73 20 70 72 6f 76 s or tables prov
6520: 6f 6b 65 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 oke an error. T
6530: 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
6540: 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 rns.** the numbe
6550: 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e r of errors seen
6560: 20 61 6e 64 20 6c 65 61 76 65 73 20 61 6e 20 65 and leaves an e
6570: 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6f 6e 20 rror message on
6580: 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e pParse->zErrMsg.
6590: 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 45 78 .*/.int sqliteEx
65a0: 70 72 52 65 73 6f 6c 76 65 49 64 73 28 50 61 72 prResolveIds(Par
65b0: 73 65 20 2a 70 50 61 72 73 65 2c 20 49 64 4c 69 se *pParse, IdLi
65c0: 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 45 78 st *pTabList, Ex
65d0: 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 66 pr *pExpr){. if
65e0: 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 ( pExpr==0 ) ret
65f0: 75 72 6e 20 30 3b 0a 20 20 73 77 69 74 63 68 28 urn 0;. switch(
6600: 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 pExpr->op ){.
6610: 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64 65 6e /* A lone iden
6620: 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 63 61 tifier */. ca
6630: 73 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 se TK_ID: {.
6640: 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 int cnt = 0;
6650: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 /* Number of ma
6660: 74 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20 69 tches */. i
6670: 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 2f 2a nt i; /*
6680: 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
6690: 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 20 3d . char *z =
66a0: 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b pExpr->token.z;
66b0: 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 . int n = p
66c0: 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 Expr->token.n;.
66d0: 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
66e0: 70 54 61 62 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 pTabList->nId; i
66f0: 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 ++){. int
6700: 20 6a 3b 0a 20 20 20 20 20 20 20 20 54 61 62 6c j;. Tabl
6710: 65 20 2a 70 54 61 62 20 3d 20 70 54 61 62 4c 69 e *pTab = pTabLi
6720: 73 74 2d 3e 61 5b 69 5d 2e 70 54 61 62 3b 0a 20 st->a[i].pTab;.
6730: 20 20 20 20 20 20 20 69 66 28 20 70 54 61 62 3d if( pTab=
6740: 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 =0 ) continue;.
6750: 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 for(j=0;
6760: 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b j<pTab->nCol; j+
6770: 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
6780: 28 20 73 71 6c 69 74 65 53 74 72 4e 49 43 6d 70 ( sqliteStrNICmp
6790: 28 70 54 61 62 2d 3e 61 7a 43 6f 6c 5b 6a 5d 2c (pTab->azCol[j],
67a0: 20 7a 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 z, n)==0 ){.
67b0: 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a cnt++;.
67c0: 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
67d0: 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 3b 0a 20 r->iTable = i;.
67e0: 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
67f0: 2d 3e 69 46 69 65 6c 64 20 3d 20 6a 3b 0a 20 20 ->iField = j;.
6800: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
6810: 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
6820: 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 7b 0a if( cnt==0 ){.
6830: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 53 65 sqliteSe
6840: 74 4e 53 74 72 69 6e 67 28 26 70 50 61 72 73 65 tNString(&pParse
6850: 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 75 6e 6b 6e ->zErrMsg, "unkn
6860: 6f 77 6e 20 66 69 65 6c 64 20 6e 61 6d 65 3a 20 own field name:
6870: 5c 22 22 2c 20 2d 31 2c 20 20 0a 20 20 20 20 20 \"", -1, .
6880: 20 20 20 20 20 70 45 78 70 72 2d 3e 74 6f 6b 65 pExpr->toke
6890: 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 n.z, pExpr->toke
68a0: 6e 2e 6e 2c 20 22 5c 22 22 2c 20 2d 31 2c 20 30 n.n, "\"", -1, 0
68b0: 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 );. pPars
68c0: 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 e->nErr++;.
68d0: 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
68e0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 6e 74 }else if( cnt
68f0: 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 >1 ){. sq
6900: 6c 69 74 65 53 65 74 4e 53 74 72 69 6e 67 28 26 liteSetNString(&
6910: 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c pParse->zErrMsg,
6920: 20 22 61 6d 62 69 67 75 6f 75 73 20 66 69 65 6c "ambiguous fiel
6930: 64 20 6e 61 6d 65 3a 20 5c 22 22 2c 20 2d 31 2c d name: \"", -1,
6940: 20 20 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 . pEx
6950: 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 pr->token.z, pEx
6960: 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 22 5c 22 pr->token.n, "\"
6970: 22 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20 ", -1, 0);.
6980: 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b pParse->nErr+
6990: 2b 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 +;. retur
69a0: 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 n 1;. }.
69b0: 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
69c0: 4b 5f 46 49 45 4c 44 3b 0a 20 20 20 20 20 20 62 K_FIELD;. b
69d0: 72 65 61 6b 3b 20 0a 20 20 20 20 7d 0a 20 20 0a reak; . }. .
69e0: 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e /* A table n
69f0: 61 6d 65 20 61 6e 64 20 66 69 65 6c 64 20 6e 61 ame and field na
6a00: 6d 65 3a 20 20 49 44 2e 49 44 20 2a 2f 0a 20 20 me: ID.ID */.
6a10: 20 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a 20 7b case TK_DOT: {
6a20: 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d . int cnt =
6a30: 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 0; /* Number
6a40: 6f 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20 20 of matches */.
6a50: 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 int i;
6a60: 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
6a70: 65 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 er */. Expr
6a80: 20 2a 70 4c 65 66 74 2c 20 2a 70 52 69 67 68 74 *pLeft, *pRight
6a90: 3b 20 20 20 20 2f 2a 20 4c 65 66 74 20 61 6e 64 ; /* Left and
6aa0: 20 72 69 67 68 74 20 73 75 62 62 72 61 6e 63 68 right subbranch
6ab0: 65 73 20 6f 66 20 74 68 65 20 65 78 70 72 20 2a es of the expr *
6ac0: 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 /. int n;
6ad0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ae0: 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 6e /* Length of an
6af0: 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 identifier */.
6b00: 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 char *z;
6b10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6b20: 20 54 65 78 74 20 6f 66 20 61 6e 20 69 64 65 6e Text of an iden
6b30: 74 69 66 69 65 72 20 2a 2f 0a 0a 20 20 20 20 20 tifier */..
6b40: 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e pLeft = pExpr->
6b50: 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 70 52 69 pLeft;. pRi
6b60: 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 ght = pExpr->pRi
6b70: 67 68 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ght;. asser
6b80: 74 28 20 70 4c 65 66 74 20 26 26 20 70 4c 65 66 t( pLeft && pLef
6b90: 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a t->op==TK_ID );.
6ba0: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 52 assert( pR
6bb0: 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e ight && pRight->
6bc0: 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20 20 20 op==TK_ID );.
6bd0: 20 20 20 6e 20 3d 20 70 52 69 67 68 74 2d 3e 74 n = pRight->t
6be0: 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 7a 20 oken.n;. z
6bf0: 3d 20 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e = pRight->token.
6c00: 7a 3b 20 20 20 20 20 20 0a 20 20 20 20 20 20 66 z; . f
6c10: 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 or(i=0; i<pTabLi
6c20: 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 st->nId; i++){.
6c30: 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 int j;.
6c40: 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 char *zTab
6c50: 3b 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 ;. Table
6c60: 2a 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 *pTab = pTabList
6c70: 2d 3e 61 5b 69 5d 2e 70 54 61 62 3b 0a 20 20 20 ->a[i].pTab;.
6c80: 20 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 if( pTab==0
6c90: 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
6ca0: 20 20 20 20 20 69 66 28 20 70 54 61 62 4c 69 73 if( pTabLis
6cb0: 74 2d 3e 61 5b 69 5d 2e 7a 41 6c 69 61 73 20 29 t->a[i].zAlias )
6cc0: 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 61 62 {. zTab
6cd0: 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 = pTabList->a[i
6ce0: 5d 2e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 ].zAlias;.
6cf0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
6d00: 20 20 20 7a 54 61 62 20 3d 20 70 54 61 62 2d 3e zTab = pTab->
6d10: 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d zName;. }
6d20: 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
6d30: 69 74 65 53 74 72 4e 49 43 6d 70 28 7a 54 61 62 iteStrNICmp(zTab
6d40: 2c 20 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2e 7a , pLeft->token.z
6d50: 2c 20 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2e 6e , pLeft->token.n
6d60: 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b )!=0 ) continue;
6d70: 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 . for(j=0
6d80: 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 ; j<pTab->nCol;
6d90: 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 j++){.
6da0: 69 66 28 20 73 71 6c 69 74 65 53 74 72 4e 49 43 if( sqliteStrNIC
6db0: 6d 70 28 70 54 61 62 2d 3e 61 7a 43 6f 6c 5b 6a mp(pTab->azCol[j
6dc0: 5d 2c 20 7a 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 ], z, n)==0 ){.
6dd0: 20 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b cnt++
6de0: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 ;. pE
6df0: 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 3b xpr->iTable = i;
6e00: 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 . pEx
6e10: 70 72 2d 3e 69 46 69 65 6c 64 20 3d 20 6a 3b 0a pr->iField = j;.
6e20: 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
6e30: 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
6e40: 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 if( cnt==0 )
6e50: 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
6e60: 53 65 74 4e 53 74 72 69 6e 67 28 26 70 50 61 72 SetNString(&pPar
6e70: 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 75 6e se->zErrMsg, "un
6e80: 6b 6e 6f 77 6e 20 66 69 65 6c 64 20 6e 61 6d 65 known field name
6e90: 3a 20 5c 22 22 2c 20 2d 31 2c 20 20 0a 20 20 20 : \"", -1, .
6ea0: 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 74 6f pLeft->to
6eb0: 6b 65 6e 2e 7a 2c 20 70 4c 65 66 74 2d 3e 74 6f ken.z, pLeft->to
6ec0: 6b 65 6e 2e 6e 2c 20 22 2e 22 2c 20 31 2c 20 7a ken.n, ".", 1, z
6ed0: 2c 20 6e 2c 20 22 5c 22 22 2c 20 31 2c 20 30 29 , n, "\"", 1, 0)
6ee0: 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 ;. pParse
6ef0: 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 ->nErr++;.
6f00: 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
6f10: 20 20 7d 65 6c 73 65 20 69 66 28 20 63 6e 74 3e }else if( cnt>
6f20: 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 1 ){. sql
6f30: 69 74 65 53 65 74 4e 53 74 72 69 6e 67 28 26 70 iteSetNString(&p
6f40: 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 Parse->zErrMsg,
6f50: 22 61 6d 62 69 67 75 6f 75 73 20 66 69 65 6c 64 "ambiguous field
6f60: 20 6e 61 6d 65 3a 20 5c 22 22 2c 20 2d 31 2c 20 name: \"", -1,
6f70: 20 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 . pExp
6f80: 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 r->token.z, pExp
6f90: 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 22 2e 22 2c r->token.n, ".",
6fa0: 20 31 2c 20 7a 2c 20 6e 2c 20 22 5c 22 22 2c 20 1, z, n, "\"",
6fb0: 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 1, 0);. p
6fc0: 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 Parse->nErr++;.
6fd0: 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b return 1;
6fe0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
6ff0: 71 6c 69 74 65 45 78 70 72 44 65 6c 65 74 65 28 qliteExprDelete(
7000: 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 70 45 pLeft);. pE
7010: 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a xpr->pLeft = 0;.
7020: 20 20 20 20 20 20 73 71 6c 69 74 65 45 78 70 72 sqliteExpr
7030: 44 65 6c 65 74 65 28 70 52 69 67 68 74 29 3b 0a Delete(pRight);.
7040: 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 pExpr->pRi
7050: 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 ght = 0;. p
7060: 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 46 49 Expr->op = TK_FI
7070: 45 4c 44 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ELD;. break
7080: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
7090: 46 6f 72 20 61 6c 6c 20 65 6c 73 65 2c 20 6a 75 For all else, ju
70a0: 73 74 20 72 65 63 75 72 73 69 76 65 6c 79 20 77 st recursively w
70b0: 61 6c 6b 20 74 68 65 20 74 72 65 65 20 2a 2f 0a alk the tree */.
70c0: 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
70d0: 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e if( pExpr->
70e0: 70 4c 65 66 74 20 0a 20 20 20 20 20 20 20 20 20 pLeft .
70f0: 20 20 20 26 26 20 73 71 6c 69 74 65 45 78 70 72 && sqliteExpr
7100: 52 65 73 6f 6c 76 65 49 64 73 28 70 50 61 72 73 ResolveIds(pPars
7110: 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 45 78 e, pTabList, pEx
7120: 70 72 2d 3e 70 4c 65 66 74 29 20 29 7b 0a 20 20 pr->pLeft) ){.
7130: 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
7140: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
7150: 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 ( pExpr->pRight
7160: 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 . &&
7170: 73 71 6c 69 74 65 45 78 70 72 52 65 73 6f 6c 76 sqliteExprResolv
7180: 65 49 64 73 28 70 50 61 72 73 65 2c 20 70 54 61 eIds(pParse, pTa
7190: 62 4c 69 73 74 2c 20 70 45 78 70 72 2d 3e 70 52 bList, pExpr->pR
71a0: 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 20 ight) ){.
71b0: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
71c0: 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 }. if( pEx
71d0: 70 72 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 pr->pList ){.
71e0: 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
71f0: 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c ExprList *pL
7200: 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 ist = pExpr->pLi
7210: 73 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 st;. for(
7220: 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 i=0; i<pList->nE
7230: 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
7240: 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 45 if( sqliteE
7250: 78 70 72 52 65 73 6f 6c 76 65 49 64 73 28 70 50 xprResolveIds(pP
7260: 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 arse, pTabList,
7270: 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 pList->a[i].pExp
7280: 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 r) ){.
7290: 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
72a0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
72b0: 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
72c0: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
72d0: 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 }../*.** Process
72e0: 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d a SELECT statem
72f0: 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ent..*/.void sql
7300: 69 74 65 53 65 6c 65 63 74 28 0a 20 20 50 61 72 iteSelect(. Par
7310: 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
7320: 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 /* The parse
7330: 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 r context */. E
7340: 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c xprList *pEList,
7350: 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 /* List of
7360: 20 66 69 65 6c 64 73 20 74 6f 20 65 78 74 72 61 fields to extra
7370: 63 74 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 ct. NULL means
7380: 22 2a 22 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 "*" */. IdList
7390: 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 20 20 *pTabList,
73a0: 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 /* List of table
73b0: 73 20 74 6f 20 73 65 6c 65 63 74 20 66 72 6f 6d s to select from
73c0: 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 */. Expr *pWhe
73d0: 72 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 re, /*
73e0: 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 The WHERE clause
73f0: 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a . May be NULL *
7400: 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f /. ExprList *pO
7410: 72 64 65 72 42 79 20 20 20 20 20 2f 2a 20 54 68 rderBy /* Th
7420: 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
7430: 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 e. May be NULL
7440: 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a */.){. int i, j
7450: 3b 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 ;. WhereInfo *p
7460: 57 49 6e 66 6f 3b 0a 20 20 56 64 62 65 20 2a 76 WInfo;. Vdbe *v
7470: 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d ;.. if( pParse-
7480: 3e 6e 45 72 72 3e 30 20 29 20 67 6f 74 6f 20 73 >nErr>0 ) goto s
7490: 65 6c 65 63 74 5f 63 6c 65 61 6e 75 70 3b 0a 0a elect_cleanup;..
74a0: 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65 /* Look up eve
74b0: 72 79 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 ry table in the
74c0: 74 61 62 6c 65 20 6c 69 73 74 2e 0a 20 20 2a 2f table list.. */
74d0: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 . for(i=0; i<pT
74e0: 61 62 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b abList->nId; i++
74f0: 29 7b 0a 20 20 20 20 70 54 61 62 4c 69 73 74 2d ){. pTabList-
7500: 3e 61 5b 69 5d 2e 70 54 61 62 20 3d 20 73 71 6c >a[i].pTab = sql
7510: 69 74 65 46 69 6e 64 54 61 62 6c 65 28 70 50 61 iteFindTable(pPa
7520: 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 4c 69 73 rse->db, pTabLis
7530: 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a t->a[i].zName);.
7540: 20 20 20 20 69 66 28 20 70 54 61 62 4c 69 73 74 if( pTabList
7550: 2d 3e 61 5b 69 5d 2e 70 54 61 62 3d 3d 30 20 29 ->a[i].pTab==0 )
7560: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 53 65 {. sqliteSe
7570: 74 53 74 72 69 6e 67 28 26 70 50 61 72 73 65 2d tString(&pParse-
7580: 3e 7a 45 72 72 4d 73 67 2c 20 22 75 6e 6b 6e 6f >zErrMsg, "unkno
7590: 77 6e 20 74 61 62 6c 65 20 5c 22 22 2c 20 0a 20 wn table \"", .
75a0: 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74 pTabList
75b0: 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 22 5c ->a[i].zName, "\
75c0: 22 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50 "", 0);. pP
75d0: 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 arse->nErr++;.
75e0: 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f goto select_
75f0: 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 cleanup;. }.
7600: 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
7610: 6c 69 73 74 20 6f 66 20 66 69 65 6c 64 73 20 74 list of fields t
7620: 6f 20 72 65 74 72 69 65 76 65 20 69 73 20 22 2a o retrieve is "*
7630: 22 20 74 68 65 6e 20 72 65 70 6c 61 63 65 20 69 " then replace i
7640: 74 20 77 69 74 68 0a 20 20 2a 2a 20 61 20 6c 69 t with. ** a li
7650: 73 74 20 6f 66 20 61 6c 6c 20 66 69 65 6c 64 73 st of all fields
7660: 20 66 72 6f 6d 20 61 6c 6c 20 74 61 62 6c 65 73 from all tables
7670: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c .. */. if( pEL
7680: 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66 6f ist==0 ){. fo
7690: 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 r(i=0; i<pTabLis
76a0: 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 t->nId; i++){.
76b0: 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 Table *pTab
76c0: 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d = pTabList->a[i]
76d0: 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 66 6f 72 .pTab;. for
76e0: 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 (j=0; j<pTab->nC
76f0: 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ol; j++){.
7700: 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 Expr *pExpr =
7710: 73 71 6c 69 74 65 45 78 70 72 28 54 4b 5f 46 49 sqliteExpr(TK_FI
7720: 45 4c 44 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 ELD, 0, 0, 0);.
7730: 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 pExpr->iT
7740: 61 62 6c 65 20 3d 20 69 3b 0a 20 20 20 20 20 20 able = i;.
7750: 20 20 70 45 78 70 72 2d 3e 69 46 69 65 6c 64 20 pExpr->iField
7760: 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 70 45 4c = j;. pEL
7770: 69 73 74 20 3d 20 73 71 6c 69 74 65 45 78 70 72 ist = sqliteExpr
7780: 4c 69 73 74 41 70 70 65 6e 64 28 70 45 4c 69 73 ListAppend(pELis
7790: 74 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 t, pExpr, 0);.
77a0: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
77b0: 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 . /* Resolve th
77c0: 65 20 66 69 65 6c 64 20 6e 61 6d 65 73 20 69 6e e field names in
77d0: 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73 all the express
77e0: 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 ions.. */. for
77f0: 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e (i=0; i<pEList->
7800: 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
7810: 20 69 66 28 20 73 71 6c 69 74 65 45 78 70 72 52 if( sqliteExprR
7820: 65 73 6f 6c 76 65 49 64 73 28 70 50 61 72 73 65 esolveIds(pParse
7830: 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 , pTabList, pELi
7840: 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 st->a[i].pExpr)
7850: 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 ){. goto se
7860: 6c 65 63 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 lect_cleanup;.
7870: 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 57 }. }. if( pW
7880: 68 65 72 65 20 26 26 20 73 71 6c 69 74 65 45 78 here && sqliteEx
7890: 70 72 52 65 73 6f 6c 76 65 49 64 73 28 70 50 61 prResolveIds(pPa
78a0: 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 rse, pTabList, p
78b0: 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 67 6f Where) ){. go
78c0: 74 6f 20 73 65 6c 65 63 74 5f 63 6c 65 61 6e 75 to select_cleanu
78d0: 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 72 p;. }. if( pOr
78e0: 64 65 72 42 79 20 29 7b 0a 20 20 20 20 66 6f 72 derBy ){. for
78f0: 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 (i=0; i<pOrderBy
7900: 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 ->nExpr; i++){.
7910: 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 45 if( sqliteE
7920: 78 70 72 52 65 73 6f 6c 76 65 49 64 73 28 70 50 xprResolveIds(pP
7930: 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 arse, pTabList,
7940: 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 pOrderBy->a[i].p
7950: 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 Expr) ){.
7960: 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 63 6c 65 goto select_cle
7970: 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 anup;. }.
7980: 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 }. }.. /* Be
7990: 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 gin generating c
79a0: 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 ode.. */. v =
79b0: 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
79c0: 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 if( v==0 ){.
79d0: 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 v = pParse->pVd
79e0: 62 65 20 3d 20 73 71 6c 69 74 65 56 64 62 65 43 be = sqliteVdbeC
79f0: 72 65 61 74 65 28 70 50 61 72 73 65 2d 3e 64 62 reate(pParse->db
7a00: 2d 3e 70 42 65 29 3b 0a 20 20 7d 0a 20 20 69 66 ->pBe);. }. if
7a10: 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 ( v==0 ) goto se
7a20: 6c 65 63 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 lect_cleanup;.
7a30: 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a if( pOrderBy ){.
7a40: 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 sqliteVdbeAd
7a50: 64 4f 70 28 76 2c 20 4f 50 5f 53 6f 72 74 4f 70 dOp(v, OP_SortOp
7a60: 65 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b en, 0, 0, 0, 0);
7a70: 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 64 65 6e . }... /* Iden
7a80: 74 69 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 tify column name
7a90: 73 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 56 s. */. sqliteV
7aa0: 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 dbeAddOp(v, OP_C
7ab0: 6f 6c 75 6d 6e 43 6f 75 6e 74 2c 20 70 45 4c 69 olumnCount, pELi
7ac0: 73 74 2d 3e 6e 45 78 70 72 2c 20 30 2c 20 30 2c st->nExpr, 0, 0,
7ad0: 20 30 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 0);. for(i=0;
7ae0: 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b i<pEList->nExpr;
7af0: 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 i++){. Expr
7b00: 2a 70 3b 0a 20 20 20 20 69 66 28 20 70 45 4c 69 *p;. if( pELi
7b10: 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29 st->a[i].zName )
7b20: 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e {. char *zN
7b30: 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b ame = pEList->a[
7b40: 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 i].zName;.
7b50: 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 int addr = sqlit
7b60: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
7b70: 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 69 2c 20 _ColumnName, i,
7b80: 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 0, zName, 0);.
7b90: 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 5b 30 5d if( zName[0]
7ba0: 3d 3d 27 5c 27 27 20 7c 7c 20 7a 4e 61 6d 65 5b =='\'' || zName[
7bb0: 30 5d 3d 3d 27 22 27 20 29 7b 0a 20 20 20 20 20 0]=='"' ){.
7bc0: 20 20 20 73 71 6c 69 74 65 56 64 62 65 44 65 71 sqliteVdbeDeq
7bd0: 75 6f 74 65 50 33 28 76 2c 20 61 64 64 72 29 3b uoteP3(v, addr);
7be0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 . }. c
7bf0: 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 ontinue;. }.
7c00: 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61 p = pEList->a
7c10: 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 [i].pExpr;. i
7c20: 66 28 20 70 2d 3e 6f 70 21 3d 54 4b 5f 46 49 45 f( p->op!=TK_FIE
7c30: 4c 44 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 LD ){. char
7c40: 20 7a 4e 61 6d 65 5b 33 30 5d 3b 0a 20 20 20 20 zName[30];.
7c50: 20 20 73 70 72 69 6e 74 66 28 7a 4e 61 6d 65 2c sprintf(zName,
7c60: 20 22 66 69 65 6c 64 25 64 22 2c 20 69 2b 31 29 "field%d", i+1)
7c70: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 56 64 ;. sqliteVd
7c80: 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f beAddOp(v, OP_Co
7c90: 6c 75 6d 6e 4e 61 6d 65 2c 20 69 2c 20 30 2c 20 lumnName, i, 0,
7ca0: 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d zName, 0);. }
7cb0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 else{. if(
7cc0: 70 54 61 62 4c 69 73 74 2d 3e 6e 49 64 3e 31 20 pTabList->nId>1
7cd0: 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 ){. char
7ce0: 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 *zName = 0;.
7cf0: 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 Table *pTab
7d00: 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 2d = pTabList->a[p-
7d10: 3e 69 54 61 62 6c 65 5d 2e 70 54 61 62 3b 0a 20 >iTable].pTab;.
7d20: 20 20 20 20 20 20 20 73 71 6c 69 74 65 53 65 74 sqliteSet
7d30: 53 74 72 69 6e 67 28 26 7a 4e 61 6d 65 2c 20 70 String(&zName, p
7d40: 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 2e 22 2c Tab->zName, ".",
7d50: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
7d60: 20 70 54 61 62 2d 3e 61 7a 43 6f 6c 5b 70 2d 3e pTab->azCol[p->
7d70: 69 46 69 65 6c 64 5d 2c 20 30 29 3b 0a 20 20 20 iField], 0);.
7d80: 20 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 sqliteVdbeA
7d90: 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d ddOp(v, OP_Colum
7da0: 6e 4e 61 6d 65 2c 20 69 2c 20 30 2c 20 7a 4e 61 nName, i, 0, zNa
7db0: 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 me, 0);.
7dc0: 73 71 6c 69 74 65 46 72 65 65 28 7a 4e 61 6d 65 sqliteFree(zName
7dd0: 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
7de0: 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 Table *p
7df0: 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e Tab = pTabList->
7e00: 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 a[0].pTab;.
7e10: 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 sqliteVdbeAdd
7e20: 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 4e Op(v, OP_ColumnN
7e30: 61 6d 65 2c 20 69 2c 20 30 2c 20 70 54 61 62 2d ame, i, 0, pTab-
7e40: 3e 61 7a 43 6f 6c 5b 70 2d 3e 69 46 69 65 6c 64 >azCol[p->iField
7e50: 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ], 0);. }.
7e60: 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 }. }.. /* B
7e70: 65 67 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 egin the databas
7e80: 65 20 73 63 61 6e 0a 20 20 2a 2f 20 20 0a 20 20 e scan. */ .
7e90: 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 57 pWInfo = sqliteW
7ea0: 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 hereBegin(pParse
7eb0: 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65 , pTabList, pWhe
7ec0: 72 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 57 re, 0);. if( pW
7ed0: 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73 Info==0 ) goto s
7ee0: 65 6c 65 63 74 5f 63 6c 65 61 6e 75 70 3b 0a 0a elect_cleanup;..
7ef0: 20 20 2f 2a 20 50 75 6c 6c 20 74 68 65 20 72 65 /* Pull the re
7f00: 71 75 65 73 74 65 64 20 66 69 65 6c 64 73 2e 0a quested fields..
7f10: 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
7f20: 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b i<pEList->nExpr;
7f30: 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 i++){. sqlit
7f40: 65 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 eExprCode(pParse
7f50: 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 , pEList->a[i].p
7f60: 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 Expr);. }. .
7f70: 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e /* If there is n
7f80: 6f 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 o ORDER BY claus
7f90: 65 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 69 e, then we can i
7fa0: 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 nvoke the callba
7fb0: 63 6b 0a 20 20 2a 2a 20 72 69 67 68 74 20 61 77 ck. ** right aw
7fc0: 61 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 ay. If there is
7fd0: 20 61 6e 20 4f 52 44 45 52 20 42 59 2c 20 74 68 an ORDER BY, th
7fe0: 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 70 75 en we need to pu
7ff0: 74 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 20 t the. ** data
8000: 69 6e 74 6f 20 61 6e 20 61 70 70 72 6f 70 72 69 into an appropri
8010: 61 74 65 20 73 6f 72 74 65 72 20 72 65 63 6f 72 ate sorter recor
8020: 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f d.. */. if( pO
8030: 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 rderBy==0 ){.
8040: 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 sqliteVdbeAddOp
8050: 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c (v, OP_Callback,
8060: 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 pEList->nExpr,
8070: 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 0, 0, 0);. }els
8080: 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 6f e{. char *zSo
8090: 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 73 71 6c rtOrder;. sql
80a0: 69 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 iteVdbeAddOp(v,
80b0: 4f 50 5f 53 6f 72 74 4d 61 6b 65 52 65 63 2c 20 OP_SortMakeRec,
80c0: 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 30 pEList->nExpr, 0
80d0: 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7a 53 6f , 0, 0);. zSo
80e0: 72 74 4f 72 64 65 72 20 3d 20 73 71 6c 69 74 65 rtOrder = sqlite
80f0: 4d 61 6c 6c 6f 63 28 20 70 4f 72 64 65 72 42 79 Malloc( pOrderBy
8100: 2d 3e 6e 45 78 70 72 20 2b 20 31 20 29 3b 0a 20 ->nExpr + 1 );.
8110: 20 20 20 69 66 28 20 7a 53 6f 72 74 4f 72 64 65 if( zSortOrde
8120: 72 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 r==0 ) goto sele
8130: 63 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 ct_cleanup;.
8140: 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 for(i=0; i<pOrde
8150: 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 rBy->nExpr; i++)
8160: 7b 0a 20 20 20 20 20 20 7a 53 6f 72 74 4f 72 64 {. zSortOrd
8170: 65 72 5b 69 5d 20 3d 20 70 4f 72 64 65 72 42 79 er[i] = pOrderBy
8180: 2d 3e 61 5b 69 5d 2e 69 64 78 20 3f 20 27 2d 27 ->a[i].idx ? '-'
8190: 20 3a 20 27 2b 27 3b 0a 20 20 20 20 20 20 73 71 : '+';. sq
81a0: 6c 69 74 65 45 78 70 72 43 6f 64 65 28 70 50 61 liteExprCode(pPa
81b0: 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 61 rse, pOrderBy->a
81c0: 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 [i].pExpr);.
81d0: 7d 0a 20 20 20 20 7a 53 6f 72 74 4f 72 64 65 72 }. zSortOrder
81e0: 5b 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 [pOrderBy->nExpr
81f0: 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 ] = 0;. sqlit
8200: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
8210: 5f 53 6f 72 74 4d 61 6b 65 4b 65 79 2c 20 70 4f _SortMakeKey, pO
8220: 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 2c 20 30 rderBy->nExpr, 0
8230: 2c 20 7a 53 6f 72 74 4f 72 64 65 72 2c 20 30 29 , zSortOrder, 0)
8240: 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 ;. sqliteVdbe
8250: 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53 6f 72 74 AddOp(v, OP_Sort
8260: 50 75 74 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 Put, 0, 0, 0, 0)
8270: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 64 20 ;. }.. /* End
8280: 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61 the database sca
8290: 6e 20 6c 6f 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73 n loop.. */. s
82a0: 71 6c 69 74 65 57 68 65 72 65 45 6e 64 28 70 57 qliteWhereEnd(pW
82b0: 49 6e 66 6f 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 Info);.. /* If
82c0: 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 there is an ORDE
82d0: 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 R BY clause, the
82e0: 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 73 6f 72 n we need to sor
82f0: 74 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 t the results.
8300: 2a 2a 20 61 6e 64 20 73 65 6e 64 20 74 68 65 6d ** and send them
8310: 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
8320: 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20 2a one by one.. *
8330: 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 /. if( pOrderBy
8340: 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 6e 64 20 ){. int end
8350: 3d 20 73 71 6c 69 74 65 56 64 62 65 4d 61 6b 65 = sqliteVdbeMake
8360: 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 69 6e Label(v);. in
8370: 74 20 61 64 64 72 3b 0a 20 20 20 20 73 71 6c 69 t addr;. sqli
8380: 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f teVdbeAddOp(v, O
8390: 50 5f 53 6f 72 74 2c 20 30 2c 20 30 2c 20 30 2c P_Sort, 0, 0, 0,
83a0: 20 30 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 0);. addr =
83b0: 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 sqliteVdbeAddOp(
83c0: 76 2c 20 4f 50 5f 53 6f 72 74 4e 65 78 74 2c 20 v, OP_SortNext,
83d0: 30 2c 20 65 6e 64 2c 20 30 2c 20 30 29 3b 0a 20 0, end, 0, 0);.
83e0: 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 sqliteVdbeAdd
83f0: 4f 70 28 76 2c 20 4f 50 5f 53 6f 72 74 43 61 6c Op(v, OP_SortCal
8400: 6c 62 61 63 6b 2c 20 70 45 4c 69 73 74 2d 3e 6e lback, pEList->n
8410: 45 78 70 72 2c 20 30 2c 20 30 2c 20 30 29 3b 0a Expr, 0, 0, 0);.
8420: 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 sqliteVdbeAd
8430: 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 dOp(v, OP_Goto,
8440: 30 2c 20 61 64 64 72 2c 20 30 2c 20 30 29 3b 0a 0, addr, 0, 0);.
8450: 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 sqliteVdbeAd
8460: 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 6f 70 2c 20 dOp(v, OP_Noop,
8470: 30 2c 20 30 2c 20 30 2c 20 65 6e 64 29 3b 0a 20 0, 0, 0, end);.
8480: 20 7d 0a 0a 20 20 2f 2a 20 41 6c 77 61 79 73 20 }.. /* Always
8490: 65 78 65 63 75 74 65 20 74 68 65 20 66 6f 6c 6c execute the foll
84a0: 6f 77 69 6e 67 20 63 6f 64 65 20 62 65 66 6f 72 owing code befor
84b0: 65 20 65 78 69 74 69 6e 67 2c 20 69 6e 20 6f 72 e exiting, in or
84c0: 64 65 72 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 der to. ** rele
84d0: 61 73 65 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 ase resources..
84e0: 20 2a 2f 0a 73 65 6c 65 63 74 5f 63 6c 65 61 6e */.select_clean
84f0: 75 70 3a 0a 20 20 73 71 6c 69 74 65 45 78 70 72 up:. sqliteExpr
8500: 4c 69 73 74 44 65 6c 65 74 65 28 70 45 4c 69 73 ListDelete(pELis
8510: 74 29 3b 0a 20 20 73 71 6c 69 74 65 49 64 4c 69 t);. sqliteIdLi
8520: 73 74 44 65 6c 65 74 65 28 70 54 61 62 4c 69 73 stDelete(pTabLis
8530: 74 29 3b 0a 20 20 73 71 6c 69 74 65 45 78 70 72 t);. sqliteExpr
8540: 44 65 6c 65 74 65 28 70 57 68 65 72 65 29 3b 0a Delete(pWhere);.
8550: 20 20 73 71 6c 69 74 65 45 78 70 72 4c 69 73 74 sqliteExprList
8560: 44 65 6c 65 74 65 28 70 4f 72 64 65 72 42 79 29 Delete(pOrderBy)
8570: 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f ;. return;.}../
8580: 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 44 *.** Process a D
8590: 45 4c 45 54 45 20 46 52 4f 4d 20 73 74 61 74 65 ELETE FROM state
85a0: 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ment..*/.void sq
85b0: 6c 69 74 65 44 65 6c 65 74 65 46 72 6f 6d 28 0a liteDeleteFrom(.
85c0: 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
85d0: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
85e0: 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a parser context *
85f0: 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c /. Token *pTabl
8600: 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 68 eName, /* Th
8610: 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 e table from whi
8620: 63 68 20 77 65 20 73 68 6f 75 6c 64 20 64 65 6c ch we should del
8630: 65 74 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 ete things */.
8640: 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 Expr *pWhere
8650: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 /* The WH
8660: 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 ERE clause. May
8670: 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 be null */.){.
8680: 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 Vdbe *v;
8690: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 /* The v
86a0: 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 irtual database
86b0: 65 6e 67 69 6e 65 20 2a 2f 0a 20 20 54 61 62 6c engine */. Tabl
86c0: 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 e *pTab;
86d0: 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
86e0: 66 72 6f 6d 20 77 68 69 63 68 20 72 65 63 6f 72 from which recor
86f0: 64 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 ds will be delet
8700: 65 64 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a ed */. IdList *
8710: 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20 20 2f pTabList; /
8720: 2a 20 41 6e 20 49 44 20 6c 69 73 74 20 68 6f 6c * An ID list hol
8730: 64 69 6e 67 20 70 54 61 62 20 61 6e 64 20 6e 6f ding pTab and no
8740: 74 68 69 6e 67 20 65 6c 73 65 20 2a 2f 0a 20 20 thing else */.
8750: 69 6e 74 20 65 6e 64 2c 20 61 64 64 72 3b 20 20 int end, addr;
8760: 20 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 75 70 /* A coup
8770: 6c 65 20 61 64 64 72 65 73 73 65 73 20 6f 66 20 le addresses of
8780: 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a generated code *
8790: 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 /. int i;
87a0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
87b0: 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
87c0: 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 WhereInfo *pWInf
87d0: 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d o; /* Inform
87e0: 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
87f0: 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a WHERE clause */.
8800: 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 Index *pIdx;
8810: 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
8820: 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 looping over ind
8830: 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c ices of the tabl
8840: 65 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 e */.. /* Locat
8850: 65 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63 e the table whic
8860: 68 20 77 65 20 77 61 6e 74 20 74 6f 20 75 70 64 h we want to upd
8870: 61 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c 65 ate. This table
8880: 20 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 has to be. **
8890: 70 75 74 20 69 6e 20 61 6e 20 49 64 4c 69 73 74 put in an IdList
88a0: 20 73 74 72 75 63 74 75 72 65 20 62 65 63 61 75 structure becau
88b0: 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 73 se some of the s
88c0: 75 62 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 0a ubroutines will.
88d0: 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c ** will be cal
88e0: 6c 69 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65 ling are designe
88f0: 64 20 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d d to work with m
8900: 75 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 ultiple tables a
8910: 6e 64 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61 nd expect. ** a
8920: 6e 20 49 64 4c 69 73 74 2a 20 70 61 72 61 6d 65 n IdList* parame
8930: 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a ter instead of j
8940: 75 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72 ust a Table* par
8950: 61 6d 65 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 ameger.. */. p
8960: 54 61 62 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 TabList = sqlite
8970: 49 64 4c 69 73 74 41 70 70 65 6e 64 28 30 2c 20 IdListAppend(0,
8980: 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 66 pTableName);. f
8990: 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 or(i=0; i<pTabLi
89a0: 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 st->nId; i++){.
89b0: 20 20 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 pTabList->a[i
89c0: 5d 2e 70 54 61 62 20 3d 20 73 71 6c 69 74 65 46 ].pTab = sqliteF
89d0: 69 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2d indTable(pParse-
89e0: 3e 64 62 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 >db, pTabList->a
89f0: 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 [i].zName);.
8a00: 69 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b if( pTabList->a[
8a10: 69 5d 2e 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 i].pTab==0 ){.
8a20: 20 20 20 20 73 71 6c 69 74 65 53 65 74 53 74 72 sqliteSetStr
8a30: 69 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 ing(&pParse->zEr
8a40: 72 4d 73 67 2c 20 22 75 6e 6b 6e 6f 77 6e 20 74 rMsg, "unknown t
8a50: 61 62 6c 65 20 5c 22 22 2c 20 0a 20 20 20 20 20 able \"", .
8a60: 20 20 20 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b pTabList->a[
8a70: 69 5d 2e 7a 4e 61 6d 65 2c 20 22 5c 22 22 2c 20 i].zName, "\"",
8a80: 30 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 0);. pParse
8a90: 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 ->nErr++;.
8aa0: 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d goto delete_from
8ab0: 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a _cleanup;. }.
8ac0: 20 20 20 20 69 66 28 20 70 54 61 62 4c 69 73 74 if( pTabList
8ad0: 2d 3e 61 5b 69 5d 2e 70 54 61 62 2d 3e 72 65 61 ->a[i].pTab->rea
8ae0: 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 dOnly ){. s
8af0: 71 6c 69 74 65 53 65 74 53 74 72 69 6e 67 28 26 qliteSetString(&
8b00: 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c pParse->zErrMsg,
8b10: 20 22 74 61 62 6c 65 20 5c 22 22 2c 20 70 54 61 "table \"", pTa
8b20: 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d bList->a[i].zNam
8b30: 65 2c 0a 20 20 20 20 20 20 20 20 22 5c 22 20 6d e,. "\" m
8b40: 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 ay not be modifi
8b50: 65 64 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 ed", 0);. p
8b60: 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 Parse->nErr++;.
8b70: 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 goto delete
8b80: 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 _from_cleanup;.
8b90: 20 20 20 7d 0a 20 20 7d 0a 20 20 70 54 61 62 20 }. }. pTab
8ba0: 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d = pTabList->a[0]
8bb0: 2e 70 54 61 62 3b 0a 0a 20 20 2f 2a 20 52 65 73 .pTab;.. /* Res
8bc0: 6f 6c 76 65 20 74 68 65 20 66 69 65 6c 64 20 6e olve the field n
8bd0: 61 6d 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20 ames in all the
8be0: 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20 20 2a expressions.. *
8bf0: 2f 0a 20 20 69 66 28 20 70 57 68 65 72 65 20 26 /. if( pWhere &
8c00: 26 20 73 71 6c 69 74 65 45 78 70 72 52 65 73 6f & sqliteExprReso
8c10: 6c 76 65 49 64 73 28 70 50 61 72 73 65 2c 20 70 lveIds(pParse, p
8c20: 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 29 TabList, pWhere)
8c30: 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c ){. goto del
8c40: 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 ete_from_cleanup
8c50: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 ;. }.. /* Begi
8c60: 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 n generating cod
8c70: 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 70 50 e.. */. v = pP
8c80: 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 arse->pVdbe;. i
8c90: 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 76 f( v==0 ){. v
8ca0: 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
8cb0: 20 3d 20 73 71 6c 69 74 65 56 64 62 65 43 72 65 = sqliteVdbeCre
8cc0: 61 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e ate(pParse->db->
8cd0: 70 42 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 pBe);. }. if(
8ce0: 76 3d 3d 30 20 29 20 67 6f 74 6f 20 64 65 6c 65 v==0 ) goto dele
8cf0: 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b te_from_cleanup;
8d00: 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 .. /* Begin the
8d10: 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 database scan.
8d20: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 56 64 62 65 */. sqliteVdbe
8d30: 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4c 69 73 74 AddOp(v, OP_List
8d40: 4f 70 65 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 Open, 0, 0, 0, 0
8d50: 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 );. pWInfo = sq
8d60: 6c 69 74 65 57 68 65 72 65 42 65 67 69 6e 28 70 liteWhereBegin(p
8d70: 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c Parse, pTabList,
8d80: 20 70 57 68 65 72 65 2c 20 31 29 3b 0a 20 20 69 pWhere, 1);. i
8d90: 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 f( pWInfo==0 ) g
8da0: 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f oto delete_from_
8db0: 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 52 cleanup;.. /* R
8dc0: 65 6d 65 6d 62 65 72 20 74 68 65 20 69 6e 64 65 emember the inde
8dd0: 78 20 6f 66 20 65 76 65 72 79 20 69 74 65 6d 20 x of every item
8de0: 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 20 to be deleted..
8df0: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 56 64 62 65 */. sqliteVdbe
8e00: 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4c 69 73 74 AddOp(v, OP_List
8e10: 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 2c 20 Write, 0, 0, 0,
8e20: 30 29 3b 0a 0a 20 20 2f 2a 20 45 6e 64 20 74 68 0);.. /* End th
8e30: 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20 e database scan
8e40: 6c 6f 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c loop.. */. sql
8e50: 69 74 65 57 68 65 72 65 45 6e 64 28 70 57 49 6e iteWhereEnd(pWIn
8e60: 66 6f 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 fo);.. /* Delet
8e70: 65 20 65 76 65 72 79 20 69 74 65 6d 20 69 64 65 e every item ide
8e80: 6e 74 69 66 69 65 64 20 69 6e 20 74 68 65 20 6c ntified in the l
8e90: 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 ist.. */. sqli
8ea0: 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f teVdbeAddOp(v, O
8eb0: 50 5f 4c 69 73 74 52 65 77 69 6e 64 2c 20 30 2c P_ListRewind, 0,
8ec0: 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 66 6f 72 0, 0, 0);. for
8ed0: 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d (i=1, pIdx=pTab-
8ee0: 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 >pIndex; pIdx; i
8ef0: 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 ++, pIdx=pIdx->p
8f00: 4e 65 78 74 29 7b 0a 20 20 20 20 73 71 6c 69 74 Next){. sqlit
8f10: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
8f20: 5f 4f 70 65 6e 2c 20 69 2c 20 30 2c 20 70 49 64 _Open, i, 0, pId
8f30: 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 x->zName, 0);.
8f40: 7d 0a 20 20 65 6e 64 20 3d 20 73 71 6c 69 74 65 }. end = sqlite
8f50: 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 VdbeMakeLabel(v)
8f60: 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 ;. addr = sqlit
8f70: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
8f80: 5f 4c 69 73 74 52 65 61 64 2c 20 30 2c 20 65 6e _ListRead, 0, en
8f90: 64 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 d, 0, 0);. if(
8fa0: 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 29 7b 0a pTab->pIndex ){.
8fb0: 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 sqliteVdbeAd
8fc0: 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 30 dOp(v, OP_Dup, 0
8fd0: 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 , 0, 0, 0);.
8fe0: 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 sqliteVdbeAddOp(
8ff0: 76 2c 20 4f 50 5f 46 65 74 63 68 2c 20 30 2c 20 v, OP_Fetch, 0,
9000: 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 66 6f 0, 0, 0);. fo
9010: 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 r(i=1, pIdx=pTab
9020: 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 ->pIndex; pIdx;
9030: 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e i++, pIdx=pIdx->
9040: 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e pNext){. in
9050: 74 20 6a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 t j;. sqlit
9060: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
9070: 5f 44 75 70 2c 20 30 2c 20 30 2c 20 30 2c 20 30 _Dup, 0, 0, 0, 0
9080: 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 );. for(j=0
9090: 3b 20 6a 3c 70 49 64 78 2d 3e 6e 46 69 65 6c 64 ; j<pIdx->nField
90a0: 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; j++){.
90b0: 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 sqliteVdbeAddOp(
90c0: 76 2c 20 4f 50 5f 46 69 65 6c 64 2c 20 30 2c 20 v, OP_Field, 0,
90d0: 70 49 64 78 2d 3e 61 69 46 69 65 6c 64 5b 6a 5d pIdx->aiField[j]
90e0: 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d , 0, 0);. }
90f0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 56 64 62 . sqliteVdb
9100: 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b eAddOp(v, OP_Mak
9110: 65 4b 65 79 2c 20 70 49 64 78 2d 3e 6e 46 69 65 eKey, pIdx->nFie
9120: 6c 64 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 ld, 0, 0, 0);.
9130: 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 sqliteVdbeAd
9140: 64 4f 70 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 dOp(v, OP_Delete
9150: 49 64 78 2c 20 69 2c 20 30 2c 20 30 2c 20 30 29 Idx, i, 0, 0, 0)
9160: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
9170: 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c liteVdbeAddOp(v,
9180: 20 4f 50 5f 44 65 6c 65 74 65 2c 20 30 2c 20 30 OP_Delete, 0, 0
9190: 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 , 0, 0);. sqlit
91a0: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
91b0: 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2c 20 _Goto, 0, addr,
91c0: 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 56 0, 0);. sqliteV
91d0: 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4c dbeAddOp(v, OP_L
91e0: 69 73 74 43 6c 6f 73 65 2c 20 30 2c 20 30 2c 20 istClose, 0, 0,
91f0: 30 2c 20 65 6e 64 29 3b 0a 0a 64 65 6c 65 74 65 0, end);..delete
9200: 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a 0a 20 _from_cleanup:.
9210: 20 73 71 6c 69 74 65 49 64 4c 69 73 74 44 65 6c sqliteIdListDel
9220: 65 74 65 28 70 54 61 62 4c 69 73 74 29 3b 0a 20 ete(pTabList);.
9230: 20 73 71 6c 69 74 65 45 78 70 72 44 65 6c 65 74 sqliteExprDelet
9240: 65 28 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 e(pWhere);. ret
9250: 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 urn;.}../*.** Pr
9260: 6f 63 65 73 73 20 61 6e 20 55 50 44 41 54 45 20 ocess an UPDATE
9270: 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 76 6f statement..*/.vo
9280: 69 64 20 73 71 6c 69 74 65 55 70 64 61 74 65 28 id sqliteUpdate(
9290: 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
92a0: 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 , /* The
92b0: 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 parser context
92c0: 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 */. Token *pTab
92d0: 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 leName, /* T
92e0: 68 65 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 he table in whic
92f0: 68 20 77 65 20 73 68 6f 75 6c 64 20 63 68 61 6e h we should chan
9300: 67 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45 ge things */. E
9310: 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 xprList *pChange
9320: 73 2c 20 20 20 20 2f 2a 20 54 68 69 6e 67 73 20 s, /* Things
9330: 74 6f 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f to be changed */
9340: 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 . Expr *pWhere
9350: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
9360: 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 WHERE clause.
9370: 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 May be null */.)
9380: 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 {. int i, j;
9390: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
93a0: 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 op counters */.
93b0: 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 Table *pTab;
93c0: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
93d0: 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61 74 able to be updat
93e0: 65 64 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a ed */. IdList *
93f0: 70 54 61 62 4c 69 73 74 20 3d 20 30 3b 20 20 2f pTabList = 0; /
9400: 2a 20 4c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e * List containin
9410: 67 20 6f 6e 6c 79 20 70 54 61 62 20 2a 2f 0a 20 g only pTab */.
9420: 20 69 6e 74 20 65 6e 64 2c 20 61 64 64 72 3b 20 int end, addr;
9430: 20 20 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 75 /* A cou
9440: 70 6c 65 20 6f 66 20 61 64 64 72 65 73 73 65 73 ple of addresses
9450: 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61 74 65 in the generate
9460: 64 20 63 6f 64 65 20 2a 2f 0a 20 20 57 68 65 72 d code */. Wher
9470: 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 eInfo *pWInfo;
9480: 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
9490: 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 52 n about the WHER
94a0: 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 56 64 E clause */. Vd
94b0: 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 be *v;
94c0: 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 /* The virt
94d0: 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 ual database eng
94e0: 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a ine */. Index *
94f0: 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 pIdx;
9500: 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f /* For looping o
9510: 76 65 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 ver indices */.
9520: 20 69 6e 74 20 6e 49 64 78 3b 20 20 20 20 20 20 int nIdx;
9530: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
9540: 72 20 6f 66 20 69 6e 64 69 63 65 73 20 74 68 61 r of indices tha
9550: 74 20 6e 65 65 64 20 75 70 64 61 74 69 6e 67 20 t need updating
9560: 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 2a 61 70 49 */. Index **apI
9570: 64 78 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 dx = 0; /* A
9580: 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 64 69 63 n array of indic
9590: 65 73 20 74 68 61 74 20 6e 65 65 64 20 75 70 64 es that need upd
95a0: 61 74 69 6e 67 20 74 6f 6f 20 2a 2f 0a 20 20 69 ating too */. i
95b0: 6e 74 20 2a 61 58 52 65 66 20 3d 20 30 3b 20 20 nt *aXRef = 0;
95c0: 20 20 20 20 20 20 2f 2a 20 61 58 52 65 66 5b 69 /* aXRef[i
95d0: 5d 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 ] is the index i
95e0: 6e 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 5d 20 n pChanges->a[]
95f0: 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 of the.
9600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9610: 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e ** an expression
9620: 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 66 69 for the i-th fi
9630: 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 eld of the table
9640: 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ..
9650: 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 58 ** aX
9660: 52 65 66 5b 69 5d 3d 3d 2d 31 20 69 66 20 74 68 Ref[i]==-1 if th
9670: 65 20 69 2d 74 68 20 66 69 65 6c 64 20 69 73 20 e i-th field is
9680: 6e 6f 74 20 63 68 61 6e 67 65 64 2e 20 2a 2f 0a not changed. */.
9690: 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 . /* Locate the
96a0: 20 74 61 62 6c 65 20 77 68 69 63 68 20 77 65 20 table which we
96b0: 77 61 6e 74 20 74 6f 20 75 70 64 61 74 65 2e 20 want to update.
96c0: 20 54 68 69 73 20 74 61 62 6c 65 20 68 61 73 20 This table has
96d0: 74 6f 20 62 65 0a 20 20 2a 2a 20 70 75 74 20 69 to be. ** put i
96e0: 6e 20 61 6e 20 49 64 4c 69 73 74 20 73 74 72 75 n an IdList stru
96f0: 63 74 75 72 65 20 62 65 63 61 75 73 65 20 73 6f cture because so
9700: 6d 65 20 6f 66 20 74 68 65 20 73 75 62 72 6f 75 me of the subrou
9710: 74 69 6e 65 73 20 77 69 6c 6c 0a 20 20 2a 2a 20 tines will. **
9720: 77 69 6c 6c 20 62 65 20 63 61 6c 6c 69 6e 67 20 will be calling
9730: 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 are designed to
9740: 77 6f 72 6b 20 77 69 74 68 20 6d 75 6c 74 69 70 work with multip
9750: 6c 65 20 74 61 62 6c 65 73 20 61 6e 64 20 65 78 le tables and ex
9760: 70 65 63 74 0a 20 20 2a 2a 20 61 6e 20 49 64 4c pect. ** an IdL
9770: 69 73 74 2a 20 70 61 72 61 6d 65 74 65 72 20 69 ist* parameter i
9780: 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74 20 61 nstead of just a
9790: 20 54 61 62 6c 65 2a 20 70 61 72 61 6d 65 67 65 Table* paramege
97a0: 72 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62 4c 69 r.. */. pTabLi
97b0: 73 74 20 3d 20 73 71 6c 69 74 65 49 64 4c 69 73 st = sqliteIdLis
97c0: 74 41 70 70 65 6e 64 28 30 2c 20 70 54 61 62 6c tAppend(0, pTabl
97d0: 65 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d eName);. for(i=
97e0: 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 0; i<pTabList->n
97f0: 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 54 Id; i++){. pT
9800: 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54 61 abList->a[i].pTa
9810: 62 20 3d 20 73 71 6c 69 74 65 46 69 6e 64 54 61 b = sqliteFindTa
9820: 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ble(pParse->db,
9830: 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a pTabList->a[i].z
9840: 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 Name);. if( p
9850: 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54 TabList->a[i].pT
9860: 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 ab==0 ){. s
9870: 71 6c 69 74 65 53 65 74 53 74 72 69 6e 67 28 26 qliteSetString(&
9880: 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c pParse->zErrMsg,
9890: 20 22 75 6e 6b 6e 6f 77 6e 20 74 61 62 6c 65 20 "unknown table
98a0: 5c 22 22 2c 20 0a 20 20 20 20 20 20 20 20 20 70 \"", . p
98b0: 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e TabList->a[i].zN
98c0: 61 6d 65 2c 20 22 5c 22 22 2c 20 30 29 3b 0a 20 ame, "\"", 0);.
98d0: 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 pParse->nEr
98e0: 72 2b 2b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 r++;. goto
98f0: 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a update_cleanup;.
9900: 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 54 }. if( pT
9910: 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54 61 abList->a[i].pTa
9920: 62 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 b->readOnly ){.
9930: 20 20 20 20 20 73 71 6c 69 74 65 53 65 74 53 74 sqliteSetSt
9940: 72 69 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 ring(&pParse->zE
9950: 72 72 4d 73 67 2c 20 22 74 61 62 6c 65 20 5c 22 rrMsg, "table \"
9960: 22 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 ", pTabList->a[i
9970: 5d 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 ].zName,.
9980: 20 22 5c 22 20 6d 61 79 20 6e 6f 74 20 62 65 20 "\" may not be
9990: 6d 6f 64 69 66 69 65 64 22 2c 20 30 29 3b 0a 20 modified", 0);.
99a0: 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 pParse->nEr
99b0: 72 2b 2b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 r++;. goto
99c0: 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a update_cleanup;.
99d0: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 54 61 62 }. }. pTab
99e0: 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 = pTabList->a[0
99f0: 5d 2e 70 54 61 62 3b 0a 20 20 61 58 52 65 66 20 ].pTab;. aXRef
9a00: 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 = sqliteMalloc(
9a10: 73 69 7a 65 6f 66 28 69 6e 74 29 20 2a 20 70 54 sizeof(int) * pT
9a20: 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 69 66 ab->nCol );. if
9a30: 28 20 61 58 52 65 66 3d 3d 30 20 29 20 67 6f 74 ( aXRef==0 ) got
9a40: 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 o update_cleanup
9a50: 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
9a60: 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 20 Tab->nCol; i++)
9a70: 61 58 52 65 66 5b 69 5d 20 3d 20 2d 31 3b 0a 0a aXRef[i] = -1;..
9a80: 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 /* Resolve the
9a90: 20 66 69 65 6c 64 20 6e 61 6d 65 73 20 69 6e 20 field names in
9aa0: 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 all the expressi
9ab0: 6f 6e 73 20 69 6e 20 62 6f 74 68 20 74 68 65 0a ons in both the.
9ac0: 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 ** WHERE claus
9ad0: 65 20 61 6e 64 20 69 6e 20 74 68 65 20 6e 65 77 e and in the new
9ae0: 20 76 61 6c 75 65 73 2e 20 20 41 6c 73 6f 20 66 values. Also f
9af0: 69 6e 64 20 74 68 65 20 66 69 65 6c 64 20 69 6e ind the field in
9b00: 64 65 78 0a 20 20 2a 2a 20 66 6f 72 20 65 61 63 dex. ** for eac
9b10: 68 20 66 69 65 6c 64 20 74 6f 20 62 65 20 75 70 h field to be up
9b20: 64 61 74 65 64 20 69 6e 20 74 68 65 20 70 43 68 dated in the pCh
9b30: 61 6e 67 65 73 20 61 72 72 61 79 2e 0a 20 20 2a anges array.. *
9b40: 2f 0a 20 20 69 66 28 20 70 57 68 65 72 65 20 26 /. if( pWhere &
9b50: 26 20 73 71 6c 69 74 65 45 78 70 72 52 65 73 6f & sqliteExprReso
9b60: 6c 76 65 49 64 73 28 70 50 61 72 73 65 2c 20 70 lveIds(pParse, p
9b70: 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 29 TabList, pWhere)
9b80: 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 ){. goto upd
9b90: 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d ate_cleanup;. }
9ba0: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 . for(i=0; i<pC
9bb0: 68 61 6e 67 65 73 2d 3e 6e 45 78 70 72 3b 20 69 hanges->nExpr; i
9bc0: 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c ++){. if( sql
9bd0: 69 74 65 45 78 70 72 52 65 73 6f 6c 76 65 49 64 iteExprResolveId
9be0: 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 s(pParse, pTabLi
9bf0: 73 74 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b st, pChanges->a[
9c00: 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 i].pExpr) ){.
9c10: 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 goto update_c
9c20: 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 leanup;. }.
9c30: 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 for(j=0; j<pTa
9c40: 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 b->nCol; j++){.
9c50: 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 if( strcmp(
9c60: 70 54 61 62 2d 3e 61 7a 43 6f 6c 5b 6a 5d 2c 20 pTab->azCol[j],
9c70: 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a pChanges->a[i].z
9c80: 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Name)==0 ){.
9c90: 20 20 20 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b pChanges->a[
9ca0: 69 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20 20 20 i].idx = j;.
9cb0: 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 69 aXRef[j] = i
9cc0: 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
9cd0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
9ce0: 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e if( j>=pTab->
9cf0: 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 nCol ){. sq
9d00: 6c 69 74 65 53 65 74 53 74 72 69 6e 67 28 26 70 liteSetString(&p
9d10: 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 Parse->zErrMsg,
9d20: 22 6e 6f 20 73 75 63 68 20 66 69 65 6c 64 3a 20 "no such field:
9d30: 5c 22 22 2c 20 0a 20 20 20 20 20 20 20 20 20 70 \"", . p
9d40: 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e Changes->a[i].zN
9d50: 61 6d 65 2c 20 22 5c 22 22 2c 20 30 29 3b 0a 20 ame, "\"", 0);.
9d60: 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 pParse->nEr
9d70: 72 2b 2b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 r++;. goto
9d80: 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a update_cleanup;.
9d90: 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
9da0: 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 Allocate memory
9db0: 66 6f 72 20 74 68 65 20 61 72 72 61 79 20 61 70 for the array ap
9dc0: 49 64 78 5b 5d 20 61 6e 64 20 66 69 6c 6c 20 69 Idx[] and fill i
9dd0: 74 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 65 76 t pointers to ev
9de0: 65 72 79 0a 20 20 2a 2a 20 69 6e 64 65 78 20 74 ery. ** index t
9df0: 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 hat needs to be
9e00: 75 70 64 61 74 65 64 2e 20 20 49 6e 64 69 63 65 updated. Indice
9e10: 73 20 6f 6e 6c 79 20 6e 65 65 64 20 75 70 64 61 s only need upda
9e20: 74 69 6e 67 20 69 66 20 74 68 65 69 72 0a 20 20 ting if their.
9e30: 2a 2a 20 6b 65 79 20 69 6e 63 6c 75 64 65 73 20 ** key includes
9e40: 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 65 6c 64 one of the field
9e50: 73 20 6e 61 6d 65 64 20 69 6e 20 70 43 68 61 6e s named in pChan
9e60: 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 ges.. */. for(
9e70: 6e 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 nIdx=0, pIdx=pTa
9e80: 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b b->pIndex; pIdx;
9e90: 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 pIdx=pIdx->pNex
9ea0: 74 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b t){. for(i=0;
9eb0: 20 69 3c 70 49 64 78 2d 3e 6e 46 69 65 6c 64 3b i<pIdx->nField;
9ec0: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
9ed0: 20 61 58 52 65 66 5b 70 49 64 78 2d 3e 61 69 46 aXRef[pIdx->aiF
9ee0: 69 65 6c 64 5b 69 5d 5d 3e 3d 30 20 29 20 62 72 ield[i]]>=0 ) br
9ef0: 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
9f00: 66 28 20 69 3c 70 49 64 78 2d 3e 6e 46 69 65 6c f( i<pIdx->nFiel
9f10: 64 20 29 20 6e 49 64 78 2b 2b 3b 0a 20 20 7d 0a d ) nIdx++;. }.
9f20: 20 20 61 70 49 64 78 20 3d 20 73 71 6c 69 74 65 apIdx = sqlite
9f30: 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 49 Malloc( sizeof(I
9f40: 6e 64 65 78 2a 29 20 2a 20 6e 49 64 78 20 29 3b ndex*) * nIdx );
9f50: 0a 20 20 69 66 28 20 61 70 49 64 78 3d 3d 30 20 . if( apIdx==0
9f60: 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c ) goto update_cl
9f70: 65 61 6e 75 70 3b 0a 20 20 66 6f 72 28 6e 49 64 eanup;. for(nId
9f80: 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e x=0, pIdx=pTab->
9f90: 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 pIndex; pIdx; pI
9fa0: 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b dx=pIdx->pNext){
9fb0: 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
9fc0: 70 49 64 78 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b pIdx->nField; i+
9fd0: 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 58 +){. if( aX
9fe0: 52 65 66 5b 70 49 64 78 2d 3e 61 69 46 69 65 6c Ref[pIdx->aiFiel
9ff0: 64 5b 69 5d 5d 3e 3d 30 20 29 20 62 72 65 61 6b d[i]]>=0 ) break
a000: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
a010: 69 3c 70 49 64 78 2d 3e 6e 46 69 65 6c 64 20 29 i<pIdx->nField )
a020: 20 61 70 49 64 78 5b 6e 49 64 78 2b 2b 5d 20 3d apIdx[nIdx++] =
a030: 20 70 49 64 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a pIdx;. }.. /*
a040: 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e Begin generatin
a050: 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 g code.. */. v
a060: 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
a070: 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a ;. if( v==0 ){.
a080: 20 20 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e v = pParse->
a090: 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 56 64 pVdbe = sqliteVd
a0a0: 62 65 43 72 65 61 74 65 28 70 50 61 72 73 65 2d beCreate(pParse-
a0b0: 3e 64 62 2d 3e 70 42 65 29 3b 0a 20 20 7d 0a 20 >db->pBe);. }.
a0c0: 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f if( v==0 ) goto
a0d0: 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b update_cleanup;
a0e0: 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 .. /* Begin the
a0f0: 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 database scan.
a100: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 56 64 62 65 */. sqliteVdbe
a110: 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4c 69 73 74 AddOp(v, OP_List
a120: 4f 70 65 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 Open, 0, 0, 0, 0
a130: 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 );. pWInfo = sq
a140: 6c 69 74 65 57 68 65 72 65 42 65 67 69 6e 28 70 liteWhereBegin(p
a150: 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c Parse, pTabList,
a160: 20 70 57 68 65 72 65 2c 20 31 29 3b 0a 20 20 69 pWhere, 1);. i
a170: 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 f( pWInfo==0 ) g
a180: 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e oto update_clean
a190: 75 70 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 up;.. /* Rememb
a1a0: 65 72 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 er the index of
a1b0: 65 76 65 72 79 20 69 74 65 6d 20 74 6f 20 62 65 every item to be
a1c0: 20 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 updated.. */.
a1d0: 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 sqliteVdbeAddOp
a1e0: 28 76 2c 20 4f 50 5f 4c 69 73 74 57 72 69 74 65 (v, OP_ListWrite
a1f0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 0a , 0, 0, 0, 0);..
a200: 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74 /* End the dat
a210: 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e abase scan loop.
a220: 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 57 68 . */. sqliteWh
a230: 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a ereEnd(pWInfo);.
a240: 0a 20 20 2f 2a 20 52 65 77 69 6e 64 20 74 68 65 . /* Rewind the
a250: 20 6c 69 73 74 20 6f 66 20 72 65 63 6f 72 64 73 list of records
a260: 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 that need to be
a270: 20 75 70 64 61 74 65 64 20 61 6e 64 0a 20 20 2a updated and. *
a280: 2a 20 6f 70 65 6e 20 65 76 65 72 79 20 69 6e 64 * open every ind
a290: 65 78 20 74 68 61 74 20 6e 65 65 64 73 20 75 70 ex that needs up
a2a0: 64 61 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 73 dating.. */. s
a2b0: 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 76 qliteVdbeAddOp(v
a2c0: 2c 20 4f 50 5f 4c 69 73 74 52 65 77 69 6e 64 2c , OP_ListRewind,
a2d0: 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0, 0, 0, 0);.
a2e0: 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 64 78 3b for(i=0; i<nIdx;
a2f0: 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 i++){. sqlit
a300: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
a310: 5f 4f 70 65 6e 2c 20 69 2b 31 2c 20 30 2c 20 61 _Open, i+1, 0, a
a320: 70 49 64 78 5b 69 5d 2d 3e 7a 4e 61 6d 65 2c 20 pIdx[i]->zName,
a330: 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 0);. }.. /* Lo
a340: 6f 70 20 6f 76 65 72 20 65 76 65 72 79 20 72 65 op over every re
a350: 63 6f 72 64 20 74 68 61 74 20 6e 65 65 64 73 20 cord that needs
a360: 75 70 64 61 74 69 6e 67 2e 20 20 57 65 20 68 61 updating. We ha
a370: 76 65 20 74 6f 20 6c 6f 61 64 0a 20 20 2a 2a 20 ve to load. **
a380: 74 68 65 20 6f 6c 64 20 64 61 74 61 20 66 6f 72 the old data for
a390: 20 65 61 63 68 20 72 65 63 6f 72 64 20 74 6f 20 each record to
a3a0: 62 65 20 75 70 64 61 74 65 64 20 62 65 63 61 75 be updated becau
a3b0: 73 65 20 73 6f 6d 65 20 66 69 65 6c 64 73 0a 20 se some fields.
a3c0: 20 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 63 68 ** might not ch
a3d0: 61 6e 67 65 20 61 6e 64 20 77 65 20 77 69 6c 6c ange and we will
a3e0: 20 6e 65 65 64 20 74 6f 20 63 6f 70 79 20 74 68 need to copy th
a3f0: 65 20 6f 6c 64 20 76 61 6c 75 65 2c 20 74 68 65 e old value, the
a400: 72 65 66 6f 72 65 2e 0a 20 20 2a 2a 20 41 6c 73 refore.. ** Als
a410: 6f 2c 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 o, the old data
a420: 69 73 20 6e 65 65 64 65 64 20 74 6f 20 64 65 6c is needed to del
a430: 65 74 65 20 74 68 65 20 6f 6c 64 20 69 6e 64 65 ete the old inde
a440: 78 20 65 6e 74 69 72 65 73 2e 0a 20 20 2a 2f 0a x entires.. */.
a450: 20 20 65 6e 64 20 3d 20 73 71 6c 69 74 65 56 64 end = sqliteVd
a460: 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a beMakeLabel(v);.
a470: 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 56 addr = sqliteV
a480: 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4c dbeAddOp(v, OP_L
a490: 69 73 74 52 65 61 64 2c 20 30 2c 20 65 6e 64 2c istRead, 0, end,
a4a0: 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 0, 0);. sqlite
a4b0: 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f VdbeAddOp(v, OP_
a4c0: 44 75 70 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 Dup, 0, 0, 0, 0)
a4d0: 3b 0a 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 ;. sqliteVdbeAd
a4e0: 64 4f 70 28 76 2c 20 4f 50 5f 46 65 74 63 68 2c dOp(v, OP_Fetch,
a4f0: 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 0a 20 0, 0, 0, 0);..
a500: 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6f /* Delete the o
a510: 6c 64 20 69 6e 64 69 63 65 73 20 66 6f 72 20 74 ld indices for t
a520: 68 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f 72 he current recor
a530: 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d d.. */. for(i=
a540: 30 3b 20 69 3c 6e 49 64 78 3b 20 69 2b 2b 29 7b 0; i<nIdx; i++){
a550: 0a 20 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 . sqliteVdbeA
a560: 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 ddOp(v, OP_Dup,
a570: 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 0, 0, 0, 0);.
a580: 20 70 49 64 78 20 3d 20 61 70 49 64 78 5b 69 5d pIdx = apIdx[i]
a590: 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a ;. for(j=0; j
a5a0: 3c 70 49 64 78 2d 3e 6e 46 69 65 6c 64 3b 20 6a <pIdx->nField; j
a5b0: 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
a5c0: 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 eVdbeAddOp(v, OP
a5d0: 5f 46 69 65 6c 64 2c 20 30 2c 20 70 49 64 78 2d _Field, 0, pIdx-
a5e0: 3e 61 69 46 69 65 6c 64 5b 6a 5d 2c 20 30 2c 20 >aiField[j], 0,
a5f0: 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 0);. }. sq
a600: 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c liteVdbeAddOp(v,
a610: 20 4f 50 5f 4d 61 6b 65 4b 65 79 2c 20 70 49 64 OP_MakeKey, pId
a620: 78 2d 3e 6e 46 69 65 6c 64 2c 20 30 2c 20 30 2c x->nField, 0, 0,
a630: 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 0);. sqliteV
a640: 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 dbeAddOp(v, OP_D
a650: 65 6c 65 74 65 49 64 78 2c 20 69 2b 31 2c 20 30 eleteIdx, i+1, 0
a660: 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 , 0, 0);. }..
a670: 2f 2a 20 43 6f 6d 70 75 74 65 20 61 20 63 6f 6d /* Compute a com
a680: 70 6c 65 74 65 6c 79 20 6e 65 77 20 64 61 74 61 pletely new data
a690: 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72 64 for this record
a6a0: 2e 20 20 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 . . */. for(i
a6b0: 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c =0; i<pTab->nCol
a6c0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6a 20 3d 20 ; i++){. j =
a6d0: 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 69 66 aXRef[i];. if
a6e0: 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 ( j<0 ){. s
a6f0: 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 76 qliteVdbeAddOp(v
a700: 2c 20 4f 50 5f 46 69 65 6c 64 2c 20 30 2c 20 69 , OP_Field, 0, i
a710: 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c , 0, 0);. }el
a720: 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
a730: 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c ExprCode(pParse,
a740: 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e pChanges->a[j].
a750: 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 pExpr);. }.
a760: 7d 0a 0a 20 20 2f 2a 20 49 6e 73 65 72 74 20 6e }.. /* Insert n
a770: 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 ew index entries
a780: 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 that correspond
a790: 20 74 6f 20 74 68 65 20 6e 65 77 20 64 61 74 61 to the new data
a7a0: 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b . */. for(i=0;
a7b0: 20 69 3c 6e 49 64 78 3b 20 69 2b 2b 29 7b 0a 20 i<nIdx; i++){.
a7c0: 20 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 sqliteVdbeAdd
a7d0: 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 70 54 Op(v, OP_Dup, pT
a7e0: 61 62 2d 3e 6e 43 6f 6c 2c 20 30 2c 20 30 2c 20 ab->nCol, 0, 0,
a7f0: 30 29 3b 20 2f 2a 20 54 68 65 20 4b 45 59 20 2a 0); /* The KEY *
a800: 2f 0a 20 20 20 20 70 49 64 78 20 3d 20 61 70 49 /. pIdx = apI
a810: 64 78 5b 69 5d 3b 0a 20 20 20 20 66 6f 72 28 6a dx[i];. for(j
a820: 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 46 69 65 =0; j<pIdx->nFie
a830: 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ld; j++){.
a840: 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 sqliteVdbeAddOp(
a850: 76 2c 20 4f 50 5f 44 75 70 2c 20 6a 2b 70 54 61 v, OP_Dup, j+pTa
a860: 62 2d 3e 6e 43 6f 6c 2d 70 49 64 78 2d 3e 61 69 b->nCol-pIdx->ai
a870: 46 69 65 6c 64 5b 6a 5d 2c 20 30 2c 20 30 2c 20 Field[j], 0, 0,
a880: 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 0);. }. sq
a890: 6c 69 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c liteVdbeAddOp(v,
a8a0: 20 4f 50 5f 4d 61 6b 65 4b 65 79 2c 20 70 49 64 OP_MakeKey, pId
a8b0: 78 2d 3e 6e 46 69 65 6c 64 2c 20 30 2c 20 30 2c x->nField, 0, 0,
a8c0: 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 56 0);. sqliteV
a8d0: 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 dbeAddOp(v, OP_P
a8e0: 75 74 49 64 78 2c 20 69 2b 31 2c 20 30 2c 20 30 utIdx, i+1, 0, 0
a8f0: 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 , 0);. }.. /*
a900: 57 72 69 74 65 20 74 68 65 20 6e 65 77 20 64 61 Write the new da
a910: 74 61 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 ta back into the
a920: 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a database.. */.
a930: 20 20 73 71 6c 69 74 65 56 64 62 65 41 64 64 4f sqliteVdbeAddO
a940: 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f p(v, OP_MakeReco
a950: 72 64 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 rd, pTab->nCol,
a960: 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 0, 0, 0);. sqli
a970: 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f teVdbeAddOp(v, O
a980: 50 5f 50 75 74 2c 20 30 2c 20 30 2c 20 30 2c 20 P_Put, 0, 0, 0,
a990: 30 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 65 61 74 0);.. /* Repeat
a9a0: 20 74 68 65 20 61 62 6f 76 65 20 77 69 74 68 20 the above with
a9b0: 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64 20 the next record
a9c0: 74 6f 20 62 65 20 75 70 64 61 74 65 64 2c 20 75 to be updated, u
a9d0: 6e 74 69 6c 0a 20 20 2a 2a 20 61 6c 6c 20 72 65 ntil. ** all re
a9e0: 63 6f 72 64 20 73 65 6c 65 63 74 65 64 20 62 79 cord selected by
a9f0: 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
aa00: 65 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 e have been upda
aa10: 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 ted.. */. sqli
aa20: 74 65 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f teVdbeAddOp(v, O
aa30: 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2c P_Goto, 0, addr,
aa40: 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 0, 0);. sqlite
aa50: 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f VdbeAddOp(v, OP_
aa60: 4c 69 73 74 43 6c 6f 73 65 2c 20 30 2c 20 30 2c ListClose, 0, 0,
aa70: 20 30 2c 20 65 6e 64 29 3b 0a 0a 75 70 64 61 74 0, end);..updat
aa80: 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c e_cleanup:. sql
aa90: 69 74 65 46 72 65 65 28 61 70 49 64 78 29 3b 0a iteFree(apIdx);.
aaa0: 20 20 73 71 6c 69 74 65 46 72 65 65 28 61 58 52 sqliteFree(aXR
aab0: 65 66 29 3b 0a 20 20 73 71 6c 69 74 65 49 64 4c ef);. sqliteIdL
aac0: 69 73 74 44 65 6c 65 74 65 28 70 54 61 62 4c 69 istDelete(pTabLi
aad0: 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 45 78 70 st);. sqliteExp
aae0: 72 4c 69 73 74 44 65 6c 65 74 65 28 70 43 68 61 rListDelete(pCha
aaf0: 6e 67 65 73 29 3b 0a 20 20 73 71 6c 69 74 65 45 nges);. sqliteE
ab00: 78 70 72 44 65 6c 65 74 65 28 70 57 68 65 72 65 xprDelete(pWhere
ab10: 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a );. return;.}.