annotate sqlite/delete.c @ 1622:55011fbbd8cb trunk

[svn] - detect what $(SHARED_SUFFIX) should be. - detect if we are using Apple-GCC.
author nenolod
date Wed, 06 Sep 2006 12:57:50 -0700
parents b6b61becdf4e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1434
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
2 ** 2001 September 15
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
3 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
4 ** The author disclaims copyright to this source code. In place of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
5 ** a legal notice, here is a blessing:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
6 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
7 ** May you do good and not evil.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
8 ** May you find forgiveness for yourself and forgive others.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
9 ** May you share freely, never taking more than you give.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
10 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
11 *************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
12 ** This file contains C code routines that are called by the parser
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
13 ** in order to generate code for DELETE FROM statements.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
14 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
15 ** $Id: delete.c,v 1.122 2006/02/24 02:53:50 drh Exp $
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
16 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
17 #include "sqliteInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
18
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
19 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
20 ** Look up every table that is named in pSrc. If any table is not found,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
21 ** add an error message to pParse->zErrMsg and return NULL. If all tables
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
22 ** are found, return a pointer to the last table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
23 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
24 Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
25 Table *pTab = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
26 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
27 struct SrcList_item *pItem;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
28 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
29 pTab = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
30 sqlite3DeleteTable(pParse->db, pItem->pTab);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
31 pItem->pTab = pTab;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
32 if( pTab ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
33 pTab->nRef++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
34 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
35 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
36 return pTab;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
37 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
38
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
39 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
40 ** Check to make sure the given table is writable. If it is not
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
41 ** writable, generate an error message and return 1. If it is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
42 ** writable return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
43 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
44 int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
45 if( pTab->readOnly && (pParse->db->flags & SQLITE_WriteSchema)==0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
46 && pParse->nested==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
47 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
48 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
49 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
50 #ifndef SQLITE_OMIT_VIEW
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
51 if( !viewOk && pTab->pSelect ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
52 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
53 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
54 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
55 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
56 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
57 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
58
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
59 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
60 ** Generate code that will open a table for reading.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
61 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
62 void sqlite3OpenTable(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
63 Parse *p, /* Generate code into this VDBE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
64 int iCur, /* The cursor number of the table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
65 int iDb, /* The database index in sqlite3.aDb[] */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
66 Table *pTab, /* The table to be opened */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
67 int opcode /* OP_OpenRead or OP_OpenWrite */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
68 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
69 Vdbe *v = sqlite3GetVdbe(p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
70 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
71 sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite), pTab->zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
72 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
73 VdbeComment((v, "# %s", pTab->zName));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
74 sqlite3VdbeAddOp(v, opcode, iCur, pTab->tnum);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
75 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
76 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
77
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
78
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
79 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
80 ** Generate code for a DELETE FROM statement.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
81 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
82 ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
83 ** \________/ \________________/
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
84 ** pTabList pWhere
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
85 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
86 void sqlite3DeleteFrom(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
87 Parse *pParse, /* The parser context */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
88 SrcList *pTabList, /* The table from which we should delete things */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
89 Expr *pWhere /* The WHERE clause. May be null */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
90 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
91 Vdbe *v; /* The virtual database engine */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
92 Table *pTab; /* The table from which records will be deleted */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
93 const char *zDb; /* Name of database holding pTab */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
94 int end, addr = 0; /* A couple addresses of generated code */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
95 int i; /* Loop counter */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
96 WhereInfo *pWInfo; /* Information about the WHERE clause */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
97 Index *pIdx; /* For looping over indices of the table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
98 int iCur; /* VDBE Cursor number for pTab */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
99 sqlite3 *db; /* Main database structure */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
100 AuthContext sContext; /* Authorization context */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
101 int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
102 NameContext sNC; /* Name context to resolve expressions in */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
103 int iDb;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
104
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
105 #ifndef SQLITE_OMIT_TRIGGER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
106 int isView; /* True if attempting to delete from a view */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
107 int triggers_exist = 0; /* True if any triggers exist */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
108 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
109
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
110 sContext.pParse = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
111 if( pParse->nErr || sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
112 goto delete_from_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
113 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
114 db = pParse->db;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
115 assert( pTabList->nSrc==1 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
116
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
117 /* Locate the table which we want to delete. This table has to be
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
118 ** put in an SrcList structure because some of the subroutines we
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
119 ** will be calling are designed to work with multiple tables and expect
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
120 ** an SrcList* parameter instead of just a Table* parameter.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
121 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
122 pTab = sqlite3SrcListLookup(pParse, pTabList);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
123 if( pTab==0 ) goto delete_from_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
124
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
125 /* Figure out if we have any triggers and if the table being
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
126 ** deleted from is a view
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
127 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
128 #ifndef SQLITE_OMIT_TRIGGER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
129 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
130 isView = pTab->pSelect!=0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
131 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
132 # define triggers_exist 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
133 # define isView 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
134 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
135 #ifdef SQLITE_OMIT_VIEW
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
136 # undef isView
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
137 # define isView 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
138 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
139
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
140 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
141 goto delete_from_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
142 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
143 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
144 assert( iDb<db->nDb );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
145 zDb = db->aDb[iDb].zName;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
146 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
147 goto delete_from_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
148 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
149
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
150 /* If pTab is really a view, make sure it has been initialized.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
151 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
152 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
153 goto delete_from_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
154 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
155
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
156 /* Allocate a cursor used to store the old.* data for a trigger.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
157 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
158 if( triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
159 oldIdx = pParse->nTab++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
160 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
161
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
162 /* Resolve the column names in the WHERE clause.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
163 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
164 assert( pTabList->nSrc==1 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
165 iCur = pTabList->a[0].iCursor = pParse->nTab++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
166 memset(&sNC, 0, sizeof(sNC));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
167 sNC.pParse = pParse;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
168 sNC.pSrcList = pTabList;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
169 if( sqlite3ExprResolveNames(&sNC, pWhere) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
170 goto delete_from_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
171 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
172
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
173 /* Start the view context
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
174 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
175 if( isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
176 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
177 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
178
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
179 /* Begin generating code.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
180 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
181 v = sqlite3GetVdbe(pParse);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
182 if( v==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
183 goto delete_from_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
184 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
185 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
186 sqlite3BeginWriteOperation(pParse, triggers_exist, iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
187
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
188 /* If we are trying to delete from a view, realize that view into
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
189 ** a ephemeral table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
190 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
191 if( isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
192 Select *pView = sqlite3SelectDup(pTab->pSelect);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
193 sqlite3Select(pParse, pView, SRT_VirtualTab, iCur, 0, 0, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
194 sqlite3SelectDelete(pView);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
195 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
196
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
197 /* Initialize the counter of the number of rows deleted, if
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
198 ** we are counting rows.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
199 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
200 if( db->flags & SQLITE_CountRows ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
201 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
202 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
203
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
204 /* Special case: A DELETE without a WHERE clause deletes everything.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
205 ** It is easier just to erase the whole table. Note, however, that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
206 ** this means that the row change count will be incorrect.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
207 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
208 if( pWhere==0 && !triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
209 if( db->flags & SQLITE_CountRows ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
210 /* If counting rows deleted, just count the total number of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
211 ** entries in the table. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
212 int endOfLoop = sqlite3VdbeMakeLabel(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
213 int addr2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
214 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
215 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
216 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
217 sqlite3VdbeAddOp(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
218 addr2 = sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
219 sqlite3VdbeAddOp(v, OP_Next, iCur, addr2);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
220 sqlite3VdbeResolveLabel(v, endOfLoop);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
221 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
222 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
223 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
224 sqlite3VdbeAddOp(v, OP_Clear, pTab->tnum, iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
225 if( !pParse->nested ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
226 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
227 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
228 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
229 assert( pIdx->pSchema==pTab->pSchema );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
230 sqlite3VdbeAddOp(v, OP_Clear, pIdx->tnum, iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
231 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
232 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
233 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
234
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
235 /* The usual case: There is a WHERE clause so we have to scan through
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
236 ** the table and pick which records to delete.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
237 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
238 else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
239 /* Begin the database scan
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
240 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
241 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
242 if( pWInfo==0 ) goto delete_from_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
243
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
244 /* Remember the rowid of every item to be deleted.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
245 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
246 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
247 sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
248 if( db->flags & SQLITE_CountRows ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
249 sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
250 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
251
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
252 /* End the database scan loop.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
253 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
254 sqlite3WhereEnd(pWInfo);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
255
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
256 /* Open the pseudo-table used to store OLD if there are triggers.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
257 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
258 if( triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
259 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
260 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
261 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
262
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
263 /* Delete every item whose key was written to the list during the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
264 ** database scan. We have to delete items after the scan is complete
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
265 ** because deleting an item can change the scan order.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
266 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
267 end = sqlite3VdbeMakeLabel(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
268
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
269 /* This is the beginning of the delete loop when there are
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
270 ** row triggers.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
271 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
272 if( triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
273 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, end);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
274 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
275 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
276 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
277 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
278 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
279 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
280 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
281 sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
282 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
283 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
284 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
285
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
286 (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_BEFORE, pTab,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
287 -1, oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
288 addr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
289 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
290
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
291 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
292 /* Open cursors for the table we are deleting from and all its
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
293 ** indices. If there are row triggers, this happens inside the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
294 ** OP_FifoRead loop because the cursor have to all be closed
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
295 ** before the trigger fires. If there are no row triggers, the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
296 ** cursors are opened only once on the outside the loop.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
297 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
298 sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
299
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
300 /* This is the beginning of the delete loop when there are no
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
301 ** row triggers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
302 if( !triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
303 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, end);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
304 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
305
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
306 /* Delete the row */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
307 sqlite3GenerateRowDelete(db, v, pTab, iCur, pParse->nested==0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
308 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
309
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
310 /* If there are row triggers, close all cursors then invoke
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
311 ** the AFTER triggers
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
312 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
313 if( triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
314 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
315 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
316 sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
317 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
318 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
319 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
320 (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_AFTER, pTab, -1,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
321 oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
322 addr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
323 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
324
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
325 /* End of the delete loop */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
326 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
327 sqlite3VdbeResolveLabel(v, end);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
328
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
329 /* Close the cursors after the loop if there are no row triggers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
330 if( !triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
331 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
332 sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
333 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
334 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
335 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
336 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
337
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
338 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
339 ** Return the number of rows that were deleted. If this routine is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
340 ** generating code because of a call to sqlite3NestedParse(), do not
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
341 ** invoke the callback function.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
342 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
343 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
344 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
345 sqlite3VdbeSetNumCols(v, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
346 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
347 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
348
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
349 delete_from_cleanup:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
350 sqlite3AuthContextPop(&sContext);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
351 sqlite3SrcListDelete(pTabList);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
352 sqlite3ExprDelete(pWhere);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
353 return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
354 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
355
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
356 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
357 ** This routine generates VDBE code that causes a single row of a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
358 ** single table to be deleted.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
359 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
360 ** The VDBE must be in a particular state when this routine is called.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
361 ** These are the requirements:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
362 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
363 ** 1. A read/write cursor pointing to pTab, the table containing the row
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
364 ** to be deleted, must be opened as cursor number "base".
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
365 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
366 ** 2. Read/write cursors for all indices of pTab must be open as
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
367 ** cursor number base+i for the i-th index.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
368 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
369 ** 3. The record number of the row to be deleted must be on the top
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
370 ** of the stack.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
371 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
372 ** This routine pops the top of the stack to remove the record number
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
373 ** and then generates code to remove both the table record and all index
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
374 ** entries that point to that record.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
375 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
376 void sqlite3GenerateRowDelete(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
377 sqlite3 *db, /* The database containing the index */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
378 Vdbe *v, /* Generate code into this VDBE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
379 Table *pTab, /* Table containing the row to be deleted */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
380 int iCur, /* Cursor number for the table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
381 int count /* Increment the row change counter */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
382 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
383 int addr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
384 addr = sqlite3VdbeAddOp(v, OP_NotExists, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
385 sqlite3GenerateRowIndexDelete(v, pTab, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
386 sqlite3VdbeAddOp(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
387 if( count ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
388 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
389 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
390 sqlite3VdbeJumpHere(v, addr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
391 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
392
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
393 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
394 ** This routine generates VDBE code that causes the deletion of all
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
395 ** index entries associated with a single row of a single table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
396 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
397 ** The VDBE must be in a particular state when this routine is called.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
398 ** These are the requirements:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
399 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
400 ** 1. A read/write cursor pointing to pTab, the table containing the row
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
401 ** to be deleted, must be opened as cursor number "iCur".
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
402 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
403 ** 2. Read/write cursors for all indices of pTab must be open as
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
404 ** cursor number iCur+i for the i-th index.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
405 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
406 ** 3. The "iCur" cursor must be pointing to the row that is to be
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
407 ** deleted.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
408 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
409 void sqlite3GenerateRowIndexDelete(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
410 Vdbe *v, /* Generate code into this VDBE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
411 Table *pTab, /* Table containing the row to be deleted */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
412 int iCur, /* Cursor number for the table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
413 char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
414 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
415 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
416 Index *pIdx;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
417
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
418 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
419 if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
420 sqlite3GenerateIndexKey(v, pIdx, iCur);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
421 sqlite3VdbeAddOp(v, OP_IdxDelete, iCur+i, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
422 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
423 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
424
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
425 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
426 ** Generate code that will assemble an index key and put it on the top
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
427 ** of the tack. The key with be for index pIdx which is an index on pTab.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
428 ** iCur is the index of a cursor open on the pTab table and pointing to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
429 ** the entry that needs indexing.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
430 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
431 void sqlite3GenerateIndexKey(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
432 Vdbe *v, /* Generate code into this VDBE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
433 Index *pIdx, /* The index for which to generate a key */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
434 int iCur /* Cursor number for the pIdx->pTable table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
435 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
436 int j;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
437 Table *pTab = pIdx->pTable;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
438
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
439 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
440 for(j=0; j<pIdx->nColumn; j++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
441 int idx = pIdx->aiColumn[j];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
442 if( idx==pTab->iPKey ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
443 sqlite3VdbeAddOp(v, OP_Dup, j, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
444 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
445 sqlite3VdbeAddOp(v, OP_Column, iCur, idx);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
446 sqlite3ColumnDefault(v, pTab, idx);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
447 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
448 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
449 sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
450 sqlite3IndexAffinityStr(v, pIdx);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
451 }