annotate sqlite/insert.c @ 1434:b6b61becdf4e trunk

[svn] - add sqlite/ directory
author nenolod
date Thu, 27 Jul 2006 22:41:31 -0700
parents
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 ** to handle INSERT statements in SQLite.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
14 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
15 ** $Id: insert.c,v 1.164 2006/03/15 16:26:10 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 ** Set P3 of the most recently inserted opcode to a column affinity
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
21 ** string for index pIdx. A column affinity string has one character
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
22 ** for each column in the table, according to the affinity of the column:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
23 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
24 ** Character Column affinity
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
25 ** ------------------------------
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
26 ** 'a' TEXT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
27 ** 'b' NONE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
28 ** 'c' NUMERIC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
29 ** 'd' INTEGER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
30 ** 'e' REAL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
31 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
32 void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
33 if( !pIdx->zColAff ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
34 /* The first time a column affinity string for a particular index is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
35 ** required, it is allocated and populated here. It is then stored as
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
36 ** a member of the Index structure for subsequent use.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
37 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
38 ** The column affinity string will eventually be deleted by
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
39 ** sqliteDeleteIndex() when the Index structure itself is cleaned
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
40 ** up.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
41 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
42 int n;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
43 Table *pTab = pIdx->pTable;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
44 pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
45 if( !pIdx->zColAff ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
46 return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
47 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
48 for(n=0; n<pIdx->nColumn; n++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
49 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
50 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
51 pIdx->zColAff[pIdx->nColumn] = '\0';
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
52 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
53
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
54 sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
55 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
56
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
57 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
58 ** Set P3 of the most recently inserted opcode to a column affinity
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
59 ** string for table pTab. A column affinity string has one character
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
60 ** for each column indexed by the index, according to the affinity of the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
61 ** column:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
62 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
63 ** Character Column affinity
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
64 ** ------------------------------
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
65 ** 'a' TEXT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
66 ** 'b' NONE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
67 ** 'c' NUMERIC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
68 ** 'd' INTEGER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
69 ** 'e' REAL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
70 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
71 void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
72 /* The first time a column affinity string for a particular table
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
73 ** is required, it is allocated and populated here. It is then
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
74 ** stored as a member of the Table structure for subsequent use.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
75 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
76 ** The column affinity string will eventually be deleted by
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
77 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
78 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
79 if( !pTab->zColAff ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
80 char *zColAff;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
81 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
82
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
83 zColAff = (char *)sqliteMalloc(pTab->nCol+1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
84 if( !zColAff ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
85 return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
86 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
87
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
88 for(i=0; i<pTab->nCol; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
89 zColAff[i] = pTab->aCol[i].affinity;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
90 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
91 zColAff[pTab->nCol] = '\0';
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
92
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
93 pTab->zColAff = zColAff;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
94 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
95
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
96 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
97 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
98
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
99 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
100 ** Return non-zero if SELECT statement p opens the table with rootpage
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
101 ** iTab in database iDb. This is used to see if a statement of the form
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
102 ** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
103 ** table for the results of the SELECT.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
104 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
105 ** No checking is done for sub-selects that are part of expressions.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
106 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
107 static int selectReadsTable(Select *p, Schema *pSchema, int iTab){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
108 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
109 struct SrcList_item *pItem;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
110 if( p->pSrc==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
111 for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
112 if( pItem->pSelect ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
113 if( selectReadsTable(pItem->pSelect, pSchema, iTab) ) return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
114 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
115 if( pItem->pTab->pSchema==pSchema && pItem->pTab->tnum==iTab ) return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
116 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
117 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
118 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
119 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
120
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
121 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
122 ** This routine is call to handle SQL of the following forms:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
123 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
124 ** insert into TABLE (IDLIST) values(EXPRLIST)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
125 ** insert into TABLE (IDLIST) select
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
126 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
127 ** The IDLIST following the table name is always optional. If omitted,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
128 ** then a list of all columns for the table is substituted. The IDLIST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
129 ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
130 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
131 ** The pList parameter holds EXPRLIST in the first form of the INSERT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
132 ** statement above, and pSelect is NULL. For the second form, pList is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
133 ** NULL and pSelect is a pointer to the select statement used to generate
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
134 ** data for the insert.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
135 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
136 ** The code generated follows one of three templates. For a simple
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
137 ** select with data coming from a VALUES clause, the code executes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
138 ** once straight down through. The template looks like this:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
139 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
140 ** open write cursor to <table> and its indices
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
141 ** puts VALUES clause expressions onto the stack
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
142 ** write the resulting record into <table>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
143 ** cleanup
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
144 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
145 ** If the statement is of the form
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
146 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
147 ** INSERT INTO <table> SELECT ...
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
148 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
149 ** And the SELECT clause does not read from <table> at any time, then
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
150 ** the generated code follows this template:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
151 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
152 ** goto B
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
153 ** A: setup for the SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
154 ** loop over the tables in the SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
155 ** gosub C
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
156 ** end loop
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
157 ** cleanup after the SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
158 ** goto D
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
159 ** B: open write cursor to <table> and its indices
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
160 ** goto A
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
161 ** C: insert the select result into <table>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
162 ** return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
163 ** D: cleanup
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
164 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
165 ** The third template is used if the insert statement takes its
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
166 ** values from a SELECT but the data is being inserted into a table
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
167 ** that is also read as part of the SELECT. In the third form,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
168 ** we have to use a intermediate table to store the results of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
169 ** the select. The template is like this:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
170 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
171 ** goto B
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
172 ** A: setup for the SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
173 ** loop over the tables in the SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
174 ** gosub C
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
175 ** end loop
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
176 ** cleanup after the SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
177 ** goto D
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
178 ** C: insert the select result into the intermediate table
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
179 ** return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
180 ** B: open a cursor to an intermediate table
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
181 ** goto A
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
182 ** D: open write cursor to <table> and its indices
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
183 ** loop over the intermediate table
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
184 ** transfer values form intermediate table into <table>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
185 ** end the loop
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
186 ** cleanup
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
187 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
188 void sqlite3Insert(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
189 Parse *pParse, /* Parser context */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
190 SrcList *pTabList, /* Name of table into which we are inserting */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
191 ExprList *pList, /* List of values to be inserted */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
192 Select *pSelect, /* A SELECT statement to use as the data source */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
193 IdList *pColumn, /* Column names corresponding to IDLIST. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
194 int onError /* How to handle constraint errors */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
195 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
196 Table *pTab; /* The table to insert into */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
197 char *zTab; /* Name of the table into which we are inserting */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
198 const char *zDb; /* Name of the database holding this table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
199 int i, j, idx; /* Loop counters */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
200 Vdbe *v; /* Generate code into this virtual machine */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
201 Index *pIdx; /* For looping over indices of the table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
202 int nColumn; /* Number of columns in the data */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
203 int base = 0; /* VDBE Cursor number for pTab */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
204 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
205 sqlite3 *db; /* The main database structure */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
206 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
207 int endOfLoop; /* Label for the end of the insertion loop */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
208 int useTempTable = 0; /* Store SELECT results in intermediate table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
209 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
210 int iSelectLoop = 0; /* Address of code that implements the SELECT */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
211 int iCleanup = 0; /* Address of the cleanup code */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
212 int iInsertBlock = 0; /* Address of the subroutine used to insert data */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
213 int iCntMem = 0; /* Memory cell used for the row counter */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
214 int newIdx = -1; /* Cursor for the NEW table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
215 Db *pDb; /* The database containing table being inserted into */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
216 int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
217 int iDb;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
218
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
219 #ifndef SQLITE_OMIT_TRIGGER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
220 int isView; /* True if attempting to insert into a view */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
221 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
222 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
223
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
224 #ifndef SQLITE_OMIT_AUTOINCREMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
225 int counterRowid = 0; /* Memory cell holding rowid of autoinc counter */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
226 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
227
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
228 if( pParse->nErr || sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
229 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
230 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
231 db = pParse->db;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
232
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
233 /* Locate the table into which we will be inserting new information.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
234 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
235 assert( pTabList->nSrc==1 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
236 zTab = pTabList->a[0].zName;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
237 if( zTab==0 ) goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
238 pTab = sqlite3SrcListLookup(pParse, pTabList);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
239 if( pTab==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
240 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
241 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
242 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
243 assert( iDb<db->nDb );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
244 pDb = &db->aDb[iDb];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
245 zDb = pDb->zName;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
246 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
247 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
248 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
249
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
250 /* Figure out if we have any triggers and if the table being
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
251 ** inserted into is a view
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
252 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
253 #ifndef SQLITE_OMIT_TRIGGER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
254 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
255 isView = pTab->pSelect!=0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
256 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
257 # define triggers_exist 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
258 # define isView 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
259 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
260 #ifdef SQLITE_OMIT_VIEW
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
261 # undef isView
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
262 # define isView 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
263 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
264
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
265 /* Ensure that:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
266 * (a) the table is not read-only,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
267 * (b) that if it is a view then ON INSERT triggers exist
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
268 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
269 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
270 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
271 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
272 assert( pTab!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
273
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
274 /* If pTab is really a view, make sure it has been initialized.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
275 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
276 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
277 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
278 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
279
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
280 /* Allocate a VDBE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
281 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
282 v = sqlite3GetVdbe(pParse);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
283 if( v==0 ) goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
284 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
285 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
286
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
287 /* if there are row triggers, allocate a temp table for new.* references. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
288 if( triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
289 newIdx = pParse->nTab++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
290 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
291
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
292 #ifndef SQLITE_OMIT_AUTOINCREMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
293 /* If this is an AUTOINCREMENT table, look up the sequence number in the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
294 ** sqlite_sequence table and store it in memory cell counterMem. Also
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
295 ** remember the rowid of the sqlite_sequence table entry in memory cell
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
296 ** counterRowid.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
297 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
298 if( pTab->autoInc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
299 int iCur = pParse->nTab;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
300 int addr = sqlite3VdbeCurrentAddr(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
301 counterRowid = pParse->nMem++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
302 counterMem = pParse->nMem++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
303 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
304 sqlite3VdbeAddOp(v, OP_Rewind, iCur, addr+13);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
305 sqlite3VdbeAddOp(v, OP_Column, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
306 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
307 sqlite3VdbeAddOp(v, OP_Ne, 0x100, addr+12);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
308 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
309 sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
310 sqlite3VdbeAddOp(v, OP_Column, iCur, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
311 sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
312 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+13);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
313 sqlite3VdbeAddOp(v, OP_Next, iCur, addr+4);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
314 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
315 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
316 #endif /* SQLITE_OMIT_AUTOINCREMENT */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
317
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
318 /* Figure out how many columns of data are supplied. If the data
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
319 ** is coming from a SELECT statement, then this step also generates
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
320 ** all the code to implement the SELECT statement and invoke a subroutine
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
321 ** to process each row of the result. (Template 2.) If the SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
322 ** statement uses the the table that is being inserted into, then the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
323 ** subroutine is also coded here. That subroutine stores the SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
324 ** results in a temporary table. (Template 3.)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
325 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
326 if( pSelect ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
327 /* Data is coming from a SELECT. Generate code to implement that SELECT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
328 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
329 int rc, iInitCode;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
330 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
331 iSelectLoop = sqlite3VdbeCurrentAddr(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
332 iInsertBlock = sqlite3VdbeMakeLabel(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
333
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
334 /* Resolve the expressions in the SELECT statement and execute it. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
335 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
336 if( rc || pParse->nErr || sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
337 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
338 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
339
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
340 iCleanup = sqlite3VdbeMakeLabel(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
341 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
342 assert( pSelect->pEList );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
343 nColumn = pSelect->pEList->nExpr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
344
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
345 /* Set useTempTable to TRUE if the result of the SELECT statement
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
346 ** should be written into a temporary table. Set to FALSE if each
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
347 ** row of the SELECT can be written directly into the result table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
348 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
349 ** A temp table must be used if the table being updated is also one
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
350 ** of the tables being read by the SELECT statement. Also use a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
351 ** temp table in the case of row triggers.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
352 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
353 if( triggers_exist || selectReadsTable(pSelect,pTab->pSchema,pTab->tnum) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
354 useTempTable = 1;
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 if( useTempTable ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
358 /* Generate the subroutine that SELECT calls to process each row of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
359 ** the result. Store the result in a temporary table
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
360 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
361 srcTab = pParse->nTab++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
362 sqlite3VdbeResolveLabel(v, iInsertBlock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
363 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
364 sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
365 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
366 sqlite3VdbeAddOp(v, OP_Insert, srcTab, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
367 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
368
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
369 /* The following code runs first because the GOTO at the very top
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
370 ** of the program jumps to it. Create the temporary table, then jump
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
371 ** back up and execute the SELECT code above.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
372 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
373 sqlite3VdbeJumpHere(v, iInitCode);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
374 sqlite3VdbeAddOp(v, OP_OpenVirtual, srcTab, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
375 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
376 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
377 sqlite3VdbeResolveLabel(v, iCleanup);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
378 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
379 sqlite3VdbeJumpHere(v, iInitCode);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
380 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
381 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
382 /* This is the case if the data for the INSERT is coming from a VALUES
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
383 ** clause
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
384 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
385 NameContext sNC;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
386 memset(&sNC, 0, sizeof(sNC));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
387 sNC.pParse = pParse;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
388 assert( pList!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
389 srcTab = -1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
390 useTempTable = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
391 assert( pList );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
392 nColumn = pList->nExpr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
393 for(i=0; i<nColumn; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
394 if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
395 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
396 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
397 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
398 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
399
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
400 /* Make sure the number of columns in the source data matches the number
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
401 ** of columns to be inserted into the table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
402 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
403 if( pColumn==0 && nColumn!=pTab->nCol ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
404 sqlite3ErrorMsg(pParse,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
405 "table %S has %d columns but %d values were supplied",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
406 pTabList, 0, pTab->nCol, nColumn);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
407 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
408 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
409 if( pColumn!=0 && nColumn!=pColumn->nId ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
410 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
411 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
412 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
413
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
414 /* If the INSERT statement included an IDLIST term, then make sure
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
415 ** all elements of the IDLIST really are columns of the table and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
416 ** remember the column indices.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
417 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
418 ** If the table has an INTEGER PRIMARY KEY column and that column
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
419 ** is named in the IDLIST, then record in the keyColumn variable
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
420 ** the index into IDLIST of the primary key column. keyColumn is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
421 ** the index of the primary key as it appears in IDLIST, not as
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
422 ** is appears in the original table. (The index of the primary
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
423 ** key in the original table is pTab->iPKey.)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
424 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
425 if( pColumn ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
426 for(i=0; i<pColumn->nId; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
427 pColumn->a[i].idx = -1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
428 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
429 for(i=0; i<pColumn->nId; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
430 for(j=0; j<pTab->nCol; j++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
431 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
432 pColumn->a[i].idx = j;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
433 if( j==pTab->iPKey ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
434 keyColumn = i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
435 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
436 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
437 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
438 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
439 if( j>=pTab->nCol ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
440 if( sqlite3IsRowid(pColumn->a[i].zName) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
441 keyColumn = i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
442 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
443 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
444 pTabList, 0, pColumn->a[i].zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
445 pParse->nErr++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
446 goto insert_cleanup;
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 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
450 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
451
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
452 /* If there is no IDLIST term but the table has an integer primary
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
453 ** key, the set the keyColumn variable to the primary key column index
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
454 ** in the original table definition.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
455 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
456 if( pColumn==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
457 keyColumn = pTab->iPKey;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
458 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
459
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
460 /* Open the temp table for FOR EACH ROW triggers
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
461 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
462 if( triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
463 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
464 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
465 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
466
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
467 /* Initialize the count of rows to be inserted
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
468 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
469 if( db->flags & SQLITE_CountRows ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
470 iCntMem = pParse->nMem++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
471 sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
472 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
473
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
474 /* Open tables and indices if there are no row triggers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
475 if( !triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
476 base = pParse->nTab;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
477 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
478 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
479
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
480 /* If the data source is a temporary table, then we have to create
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
481 ** a loop because there might be multiple rows of data. If the data
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
482 ** source is a subroutine call from the SELECT statement, then we need
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
483 ** to launch the SELECT statement processing.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
484 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
485 if( useTempTable ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
486 iBreak = sqlite3VdbeMakeLabel(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
487 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
488 iCont = sqlite3VdbeCurrentAddr(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
489 }else if( pSelect ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
490 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
491 sqlite3VdbeResolveLabel(v, iInsertBlock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
492 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
493
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
494 /* Run the BEFORE and INSTEAD OF triggers, if there are any
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
495 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
496 endOfLoop = sqlite3VdbeMakeLabel(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
497 if( triggers_exist & TRIGGER_BEFORE ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
498
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
499 /* build the NEW.* reference row. Note that if there is an INTEGER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
500 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
501 ** translated into a unique ID for the row. But on a BEFORE trigger,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
502 ** we do not know what the unique ID will be (because the insert has
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
503 ** not happened yet) so we substitute a rowid of -1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
504 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
505 if( keyColumn<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
506 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
507 }else if( useTempTable ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
508 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
509 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
510 assert( pSelect==0 ); /* Otherwise useTempTable is true */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
511 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
512 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
513 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
514 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
515 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
516 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
517
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
518 /* Create the new column data
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
519 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
520 for(i=0; i<pTab->nCol; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
521 if( pColumn==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
522 j = i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
523 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
524 for(j=0; j<pColumn->nId; j++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
525 if( pColumn->a[j].idx==i ) break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
526 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
527 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
528 if( pColumn && j>=pColumn->nId ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
529 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
530 }else if( useTempTable ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
531 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
532 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
533 assert( pSelect==0 ); /* Otherwise useTempTable is true */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
534 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
535 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
536 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
537 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
538
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
539 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
540 ** do not attempt any conversions before assembling the record.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
541 ** If this is a real table, attempt conversions as required by the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
542 ** table column affinities.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
543 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
544 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
545 sqlite3TableAffinityStr(v, pTab);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
546 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
547 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
548
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
549 /* Fire BEFORE or INSTEAD OF triggers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
550 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
551 newIdx, -1, onError, endOfLoop) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
552 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
553 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
554 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
555
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
556 /* If any triggers exists, the opening of tables and indices is deferred
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
557 ** until now.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
558 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
559 if( triggers_exist && !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
560 base = pParse->nTab;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
561 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
562 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
563
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
564 /* Push the record number for the new entry onto the stack. The
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
565 ** record number is a randomly generate integer created by NewRowid
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
566 ** except when the table has an INTEGER PRIMARY KEY column, in which
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
567 ** case the record number is the same as that column.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
568 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
569 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
570 if( keyColumn>=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
571 if( useTempTable ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
572 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
573 }else if( pSelect ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
574 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
575 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
576 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
577 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
578 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
579 ** to generate a unique primary key value.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
580 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
581 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
582 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
583 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
584 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
585 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
586 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
587 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
588 #ifndef SQLITE_OMIT_AUTOINCREMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
589 if( pTab->autoInc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
590 sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
591 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
592 #endif /* SQLITE_OMIT_AUTOINCREMENT */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
593
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
594 /* Push onto the stack, data for all columns of the new entry, beginning
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
595 ** with the first column.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
596 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
597 for(i=0; i<pTab->nCol; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
598 if( i==pTab->iPKey ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
599 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
600 ** Whenever this column is read, the record number will be substituted
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
601 ** in its place. So will fill this column with a NULL to avoid
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
602 ** taking up data space with information that will never be used. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
603 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
604 continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
605 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
606 if( pColumn==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
607 j = i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
608 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
609 for(j=0; j<pColumn->nId; j++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
610 if( pColumn->a[j].idx==i ) break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
611 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
612 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
613 if( pColumn && j>=pColumn->nId ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
614 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
615 }else if( useTempTable ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
616 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
617 }else if( pSelect ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
618 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
619 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
620 sqlite3ExprCode(pParse, pList->a[j].pExpr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
621 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
622 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
623
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
624 /* Generate code to check constraints and generate index keys and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
625 ** do the insertion.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
626 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
627 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
628 0, onError, endOfLoop);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
629 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
630 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
631 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
632
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
633 /* Update the count of rows that are inserted
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
634 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
635 if( (db->flags & SQLITE_CountRows)!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
636 sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
637 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
638
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
639 if( triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
640 /* Close all tables opened */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
641 if( !isView ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
642 sqlite3VdbeAddOp(v, OP_Close, base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
643 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
644 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
645 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
646 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
647
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
648 /* Code AFTER triggers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
649 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
650 newIdx, -1, onError, endOfLoop) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
651 goto insert_cleanup;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
652 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
653 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
654
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
655 /* The bottom of the loop, if the data source is a SELECT statement
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
656 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
657 sqlite3VdbeResolveLabel(v, endOfLoop);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
658 if( useTempTable ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
659 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
660 sqlite3VdbeResolveLabel(v, iBreak);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
661 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
662 }else if( pSelect ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
663 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
664 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
665 sqlite3VdbeResolveLabel(v, iCleanup);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
666 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
667
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
668 if( !triggers_exist ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
669 /* Close all tables opened */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
670 sqlite3VdbeAddOp(v, OP_Close, base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
671 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
672 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
673 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
674 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
675
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
676 #ifndef SQLITE_OMIT_AUTOINCREMENT
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
677 /* Update the sqlite_sequence table by storing the content of the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
678 ** counter value in memory counterMem back into the sqlite_sequence
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
679 ** table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
680 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
681 if( pTab->autoInc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
682 int iCur = pParse->nTab;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
683 int addr = sqlite3VdbeCurrentAddr(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
684 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
685 sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
686 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+7);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
687 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
688 sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
689 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
690 sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
691 sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
692 sqlite3VdbeAddOp(v, OP_Insert, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
693 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
694 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
695 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
696
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
697 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
698 ** Return the number of rows inserted. If this routine is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
699 ** generating code because of a call to sqlite3NestedParse(), do not
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
700 ** invoke the callback function.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
701 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
702 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
703 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
704 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
705 sqlite3VdbeSetNumCols(v, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
706 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
707 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
708
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
709 insert_cleanup:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
710 sqlite3SrcListDelete(pTabList);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
711 sqlite3ExprListDelete(pList);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
712 sqlite3SelectDelete(pSelect);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
713 sqlite3IdListDelete(pColumn);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
714 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
715
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
716 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
717 ** Generate code to do a constraint check prior to an INSERT or an UPDATE.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
718 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
719 ** When this routine is called, the stack contains (from bottom to top)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
720 ** the following values:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
721 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
722 ** 1. The rowid of the row to be updated before the update. This
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
723 ** value is omitted unless we are doing an UPDATE that involves a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
724 ** change to the record number.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
725 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
726 ** 2. The rowid of the row after the update.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
727 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
728 ** 3. The data in the first column of the entry after the update.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
729 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
730 ** i. Data from middle columns...
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
731 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
732 ** N. The data in the last column of the entry after the update.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
733 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
734 ** The old rowid shown as entry (1) above is omitted unless both isUpdate
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
735 ** and rowidChng are 1. isUpdate is true for UPDATEs and false for
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
736 ** INSERTs and rowidChng is true if the record number is being changed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
737 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
738 ** The code generated by this routine pushes additional entries onto
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
739 ** the stack which are the keys for new index entries for the new record.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
740 ** The order of index keys is the same as the order of the indices on
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
741 ** the pTable->pIndex list. A key is only created for index i if
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
742 ** aIdxUsed!=0 and aIdxUsed[i]!=0.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
743 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
744 ** This routine also generates code to check constraints. NOT NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
745 ** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
746 ** then the appropriate action is performed. There are five possible
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
747 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
748 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
749 ** Constraint type Action What Happens
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
750 ** --------------- ---------- ----------------------------------------
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
751 ** any ROLLBACK The current transaction is rolled back and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
752 ** sqlite3_exec() returns immediately with a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
753 ** return code of SQLITE_CONSTRAINT.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
754 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
755 ** any ABORT Back out changes from the current command
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
756 ** only (do not do a complete rollback) then
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
757 ** cause sqlite3_exec() to return immediately
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
758 ** with SQLITE_CONSTRAINT.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
759 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
760 ** any FAIL Sqlite_exec() returns immediately with a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
761 ** return code of SQLITE_CONSTRAINT. The
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
762 ** transaction is not rolled back and any
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
763 ** prior changes are retained.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
764 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
765 ** any IGNORE The record number and data is popped from
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
766 ** the stack and there is an immediate jump
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
767 ** to label ignoreDest.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
768 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
769 ** NOT NULL REPLACE The NULL value is replace by the default
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
770 ** value for that column. If the default value
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
771 ** is NULL, the action is the same as ABORT.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
772 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
773 ** UNIQUE REPLACE The other row that conflicts with the row
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
774 ** being inserted is removed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
775 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
776 ** CHECK REPLACE Illegal. The results in an exception.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
777 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
778 ** Which action to take is determined by the overrideError parameter.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
779 ** Or if overrideError==OE_Default, then the pParse->onError parameter
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
780 ** is used. Or if pParse->onError==OE_Default then the onError value
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
781 ** for the constraint is used.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
782 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
783 ** The calling routine must open a read/write cursor for pTab with
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
784 ** cursor number "base". All indices of pTab must also have open
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
785 ** read/write cursors with cursor number base+i for the i-th cursor.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
786 ** Except, if there is no possibility of a REPLACE action then
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
787 ** cursors do not need to be open for indices where aIdxUsed[i]==0.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
788 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
789 ** If the isUpdate flag is true, it means that the "base" cursor is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
790 ** initially pointing to an entry that is being updated. The isUpdate
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
791 ** flag causes extra code to be generated so that the "base" cursor
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
792 ** is still pointing at the same entry after the routine returns.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
793 ** Without the isUpdate flag, the "base" cursor might be moved.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
794 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
795 void sqlite3GenerateConstraintChecks(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
796 Parse *pParse, /* The parser context */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
797 Table *pTab, /* the table into which we are inserting */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
798 int base, /* Index of a read/write cursor pointing at pTab */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
799 char *aIdxUsed, /* Which indices are used. NULL means all are used */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
800 int rowidChng, /* True if the record number will change */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
801 int isUpdate, /* True for UPDATE, False for INSERT */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
802 int overrideError, /* Override onError to this if not OE_Default */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
803 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
804 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
805 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
806 Vdbe *v;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
807 int nCol;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
808 int onError;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
809 int addr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
810 int extra;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
811 int iCur;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
812 Index *pIdx;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
813 int seenReplace = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
814 int jumpInst1=0, jumpInst2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
815 int hasTwoRowids = (isUpdate && rowidChng);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
816
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
817 v = sqlite3GetVdbe(pParse);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
818 assert( v!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
819 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
820 nCol = pTab->nCol;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
821
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
822 /* Test all NOT NULL constraints.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
823 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
824 for(i=0; i<nCol; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
825 if( i==pTab->iPKey ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
826 continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
827 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
828 onError = pTab->aCol[i].notNull;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
829 if( onError==OE_None ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
830 if( overrideError!=OE_Default ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
831 onError = overrideError;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
832 }else if( onError==OE_Default ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
833 onError = OE_Abort;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
834 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
835 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
836 onError = OE_Abort;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
837 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
838 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
839 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
840 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
841 || onError==OE_Ignore || onError==OE_Replace );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
842 switch( onError ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
843 case OE_Rollback:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
844 case OE_Abort:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
845 case OE_Fail: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
846 char *zMsg = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
847 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
848 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
849 " may not be NULL", (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
850 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
851 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
852 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
853 case OE_Ignore: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
854 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
855 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
856 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
857 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
858 case OE_Replace: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
859 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
860 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
861 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
862 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
863 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
864 sqlite3VdbeJumpHere(v, addr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
865 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
866
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
867 /* Test all CHECK constraints
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
868 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
869 #ifndef SQLITE_OMIT_CHECK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
870 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
871 int allOk = sqlite3VdbeMakeLabel(v);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
872 assert( pParse->ckOffset==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
873 pParse->ckOffset = nCol;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
874 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
875 assert( pParse->ckOffset==nCol );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
876 pParse->ckOffset = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
877 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
878 if( onError==OE_Ignore || onError==OE_Replace ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
879 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
880 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
881 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
882 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
883 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
884 sqlite3VdbeResolveLabel(v, allOk);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
885 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
886 #endif /* !defined(SQLITE_OMIT_CHECK) */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
887
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
888 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
889 ** of the new record does not previously exist. Except, if this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
890 ** is an UPDATE and the primary key is not changing, that is OK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
891 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
892 if( rowidChng ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
893 onError = pTab->keyConf;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
894 if( overrideError!=OE_Default ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
895 onError = overrideError;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
896 }else if( onError==OE_Default ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
897 onError = OE_Abort;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
898 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
899
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
900 if( isUpdate ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
901 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
902 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
903 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
904 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
905 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
906 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
907 switch( onError ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
908 default: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
909 onError = OE_Abort;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
910 /* Fall thru into the next case */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
911 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
912 case OE_Rollback:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
913 case OE_Abort:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
914 case OE_Fail: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
915 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
916 "PRIMARY KEY must be unique", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
917 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
918 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
919 case OE_Replace: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
920 sqlite3GenerateRowIndexDelete(v, pTab, base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
921 if( isUpdate ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
922 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
923 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
924 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
925 seenReplace = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
926 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
927 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
928 case OE_Ignore: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
929 assert( seenReplace==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
930 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
931 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
932 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
933 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
934 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
935 sqlite3VdbeJumpHere(v, jumpInst2);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
936 if( isUpdate ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
937 sqlite3VdbeJumpHere(v, jumpInst1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
938 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
939 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
940 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
941 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
942
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
943 /* Test all UNIQUE constraints by creating entries for each UNIQUE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
944 ** index and making sure that duplicate entries do not already exist.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
945 ** Add the new records to the indices as we go.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
946 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
947 extra = -1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
948 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
949 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
950 extra++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
951
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
952 /* Create a key for accessing the index entry */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
953 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
954 for(i=0; i<pIdx->nColumn; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
955 int idx = pIdx->aiColumn[i];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
956 if( idx==pTab->iPKey ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
957 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
958 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
959 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
960 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
961 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
962 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
963 sqlite3IndexAffinityStr(v, pIdx);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
964
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
965 /* Find out what action to take in case there is an indexing conflict */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
966 onError = pIdx->onError;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
967 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
968 if( overrideError!=OE_Default ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
969 onError = overrideError;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
970 }else if( onError==OE_Default ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
971 onError = OE_Abort;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
972 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
973 if( seenReplace ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
974 if( onError==OE_Ignore ) onError = OE_Replace;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
975 else if( onError==OE_Fail ) onError = OE_Abort;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
976 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
977
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
978
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
979 /* Check to see if the new index entry will be unique */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
980 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
981 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
982
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
983 /* Generate code that executes if the new index entry is not unique */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
984 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
985 || onError==OE_Ignore || onError==OE_Replace );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
986 switch( onError ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
987 case OE_Rollback:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
988 case OE_Abort:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
989 case OE_Fail: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
990 int j, n1, n2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
991 char zErrMsg[200];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
992 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
993 n1 = strlen(zErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
994 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
995 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
996 n2 = strlen(zCol);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
997 if( j>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
998 strcpy(&zErrMsg[n1], ", ");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
999 n1 += 2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1000 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1001 if( n1+n2>sizeof(zErrMsg)-30 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1002 strcpy(&zErrMsg[n1], "...");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1003 n1 += 3;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1004 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1005 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1006 strcpy(&zErrMsg[n1], zCol);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1007 n1 += n2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1008 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1009 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1010 strcpy(&zErrMsg[n1],
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1011 pIdx->nColumn>1 ? " are not unique" : " is not unique");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1012 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1013 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1014 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1015 case OE_Ignore: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1016 assert( seenReplace==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1017 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1018 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1019 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1020 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1021 case OE_Replace: {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1022 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1023 if( isUpdate ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1024 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1025 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1026 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1027 seenReplace = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1028 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1029 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1030 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1031 #if NULL_DISTINCT_FOR_UNIQUE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1032 sqlite3VdbeJumpHere(v, jumpInst1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1033 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1034 sqlite3VdbeJumpHere(v, jumpInst2);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1035 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1036 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1037
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1038 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1039 ** This routine generates code to finish the INSERT or UPDATE operation
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1040 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1041 ** The stack must contain keys for all active indices followed by data
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1042 ** and the rowid for the new entry. This routine creates the new
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1043 ** entries in all indices and in the main table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1044 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1045 ** The arguments to this routine should be the same as the first six
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1046 ** arguments to sqlite3GenerateConstraintChecks.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1047 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1048 void sqlite3CompleteInsertion(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1049 Parse *pParse, /* The parser context */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1050 Table *pTab, /* the table into which we are inserting */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1051 int base, /* Index of a read/write cursor pointing at pTab */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1052 char *aIdxUsed, /* Which indices are used. NULL means all are used */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1053 int rowidChng, /* True if the record number will change */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1054 int isUpdate, /* True for UPDATE, False for INSERT */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1055 int newIdx /* Index of NEW table for triggers. -1 if none */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1056 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1057 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1058 Vdbe *v;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1059 int nIdx;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1060 Index *pIdx;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1061 int pik_flags;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1062
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1063 v = sqlite3GetVdbe(pParse);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1064 assert( v!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1065 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1066 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1067 for(i=nIdx-1; i>=0; i--){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1068 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1069 sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1070 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1071 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1072 sqlite3TableAffinityStr(v, pTab);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1073 #ifndef SQLITE_OMIT_TRIGGER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1074 if( newIdx>=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1075 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1076 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1077 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1078 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1079 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1080 if( pParse->nested ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1081 pik_flags = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1082 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1083 pik_flags = OPFLAG_NCHANGE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1084 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1085 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1086 sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1087 if( !pParse->nested ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1088 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1089 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1090
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1091 if( isUpdate && rowidChng ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1092 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1093 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1094 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1095
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1096 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1097 ** Generate code that will open cursors for a table and for all
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1098 ** indices of that table. The "base" parameter is the cursor number used
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1099 ** for the table. Indices are opened on subsequent cursors.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1100 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1101 void sqlite3OpenTableAndIndices(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1102 Parse *pParse, /* Parsing context */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1103 Table *pTab, /* Table to be opened */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1104 int base, /* Cursor number assigned to the table */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1105 int op /* OP_OpenRead or OP_OpenWrite */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1106 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1107 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1108 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1109 Index *pIdx;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1110 Vdbe *v = sqlite3GetVdbe(pParse);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1111 assert( v!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1112 sqlite3OpenTable(pParse, base, iDb, pTab, op);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1113 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1114 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1115 assert( pIdx->pSchema==pTab->pSchema );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1116 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1117 VdbeComment((v, "# %s", pIdx->zName));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1118 sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1119 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1120 if( pParse->nTab<=base+i ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1121 pParse->nTab = base+i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1122 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1123 }