Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: third_party/sqlite/sqlite-src-3080704/src/vdbeapi.c

Issue 883353008: [sql] Import reference version of SQLite 3.8.7.4. (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Hold back encoding change which is messing up patch. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2004 May 26 2 ** 2004 May 26
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 */ 64 */
65 int sqlite3_finalize(sqlite3_stmt *pStmt){ 65 int sqlite3_finalize(sqlite3_stmt *pStmt){
66 int rc; 66 int rc;
67 if( pStmt==0 ){ 67 if( pStmt==0 ){
68 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 68 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
69 ** pointer is a harmless no-op. */ 69 ** pointer is a harmless no-op. */
70 rc = SQLITE_OK; 70 rc = SQLITE_OK;
71 }else{ 71 }else{
72 Vdbe *v = (Vdbe*)pStmt; 72 Vdbe *v = (Vdbe*)pStmt;
73 sqlite3 *db = v->db; 73 sqlite3 *db = v->db;
74 #if SQLITE_THREADSAFE
75 sqlite3_mutex *mutex;
76 #endif
77 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 74 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
78 #if SQLITE_THREADSAFE 75 sqlite3_mutex_enter(db->mutex);
79 mutex = v->db->mutex;
80 #endif
81 sqlite3_mutex_enter(mutex);
82 rc = sqlite3VdbeFinalize(v); 76 rc = sqlite3VdbeFinalize(v);
83 rc = sqlite3ApiExit(db, rc); 77 rc = sqlite3ApiExit(db, rc);
84 sqlite3_mutex_leave(mutex); 78 sqlite3LeaveMutexAndCloseZombie(db);
85 } 79 }
86 return rc; 80 return rc;
87 } 81 }
88 82
89 /* 83 /*
90 ** Terminate the current execution of an SQL statement and reset it 84 ** Terminate the current execution of an SQL statement and reset it
91 ** back to its starting state so that it can be reused. A success code from 85 ** back to its starting state so that it can be reused. A success code from
92 ** the prior execution is returned. 86 ** the prior execution is returned.
93 ** 87 **
94 ** This routine sets the error code and string returned by 88 ** This routine sets the error code and string returned by
95 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 89 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
96 */ 90 */
97 int sqlite3_reset(sqlite3_stmt *pStmt){ 91 int sqlite3_reset(sqlite3_stmt *pStmt){
98 int rc; 92 int rc;
99 if( pStmt==0 ){ 93 if( pStmt==0 ){
100 rc = SQLITE_OK; 94 rc = SQLITE_OK;
101 }else{ 95 }else{
102 Vdbe *v = (Vdbe*)pStmt; 96 Vdbe *v = (Vdbe*)pStmt;
103 sqlite3_mutex_enter(v->db->mutex); 97 sqlite3_mutex_enter(v->db->mutex);
104 rc = sqlite3VdbeReset(v); 98 rc = sqlite3VdbeReset(v);
105 sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0); 99 sqlite3VdbeRewind(v);
106 assert( (rc & (v->db->errMask))==rc ); 100 assert( (rc & (v->db->errMask))==rc );
107 rc = sqlite3ApiExit(v->db, rc); 101 rc = sqlite3ApiExit(v->db, rc);
108 sqlite3_mutex_leave(v->db->mutex); 102 sqlite3_mutex_leave(v->db->mutex);
109 } 103 }
110 return rc; 104 return rc;
111 } 105 }
112 106
113 /* 107 /*
114 ** Set all the parameters in the compiled SQL statement to NULL. 108 ** Set all the parameters in the compiled SQL statement to NULL.
115 */ 109 */
(...skipping 18 matching lines...) Expand all
134 128
135 129
136 /**************************** sqlite3_value_ ******************************* 130 /**************************** sqlite3_value_ *******************************
137 ** The following routines extract information from a Mem or sqlite3_value 131 ** The following routines extract information from a Mem or sqlite3_value
138 ** structure. 132 ** structure.
139 */ 133 */
140 const void *sqlite3_value_blob(sqlite3_value *pVal){ 134 const void *sqlite3_value_blob(sqlite3_value *pVal){
141 Mem *p = (Mem*)pVal; 135 Mem *p = (Mem*)pVal;
142 if( p->flags & (MEM_Blob|MEM_Str) ){ 136 if( p->flags & (MEM_Blob|MEM_Str) ){
143 sqlite3VdbeMemExpandBlob(p); 137 sqlite3VdbeMemExpandBlob(p);
144 p->flags &= ~MEM_Str;
145 p->flags |= MEM_Blob; 138 p->flags |= MEM_Blob;
146 return p->n ? p->z : 0; 139 return p->n ? p->z : 0;
147 }else{ 140 }else{
148 return sqlite3_value_text(pVal); 141 return sqlite3_value_text(pVal);
149 } 142 }
150 } 143 }
151 int sqlite3_value_bytes(sqlite3_value *pVal){ 144 int sqlite3_value_bytes(sqlite3_value *pVal){
152 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 145 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
153 } 146 }
154 int sqlite3_value_bytes16(sqlite3_value *pVal){ 147 int sqlite3_value_bytes16(sqlite3_value *pVal){
(...skipping 16 matching lines...) Expand all
171 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 164 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
172 } 165 }
173 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 166 const void *sqlite3_value_text16be(sqlite3_value *pVal){
174 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 167 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
175 } 168 }
176 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 169 const void *sqlite3_value_text16le(sqlite3_value *pVal){
177 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 170 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
178 } 171 }
179 #endif /* SQLITE_OMIT_UTF16 */ 172 #endif /* SQLITE_OMIT_UTF16 */
180 int sqlite3_value_type(sqlite3_value* pVal){ 173 int sqlite3_value_type(sqlite3_value* pVal){
181 return pVal->type; 174 static const u8 aType[] = {
175 SQLITE_BLOB, /* 0x00 */
176 SQLITE_NULL, /* 0x01 */
177 SQLITE_TEXT, /* 0x02 */
178 SQLITE_NULL, /* 0x03 */
179 SQLITE_INTEGER, /* 0x04 */
180 SQLITE_NULL, /* 0x05 */
181 SQLITE_INTEGER, /* 0x06 */
182 SQLITE_NULL, /* 0x07 */
183 SQLITE_FLOAT, /* 0x08 */
184 SQLITE_NULL, /* 0x09 */
185 SQLITE_FLOAT, /* 0x0a */
186 SQLITE_NULL, /* 0x0b */
187 SQLITE_INTEGER, /* 0x0c */
188 SQLITE_NULL, /* 0x0d */
189 SQLITE_INTEGER, /* 0x0e */
190 SQLITE_NULL, /* 0x0f */
191 SQLITE_BLOB, /* 0x10 */
192 SQLITE_NULL, /* 0x11 */
193 SQLITE_TEXT, /* 0x12 */
194 SQLITE_NULL, /* 0x13 */
195 SQLITE_INTEGER, /* 0x14 */
196 SQLITE_NULL, /* 0x15 */
197 SQLITE_INTEGER, /* 0x16 */
198 SQLITE_NULL, /* 0x17 */
199 SQLITE_FLOAT, /* 0x18 */
200 SQLITE_NULL, /* 0x19 */
201 SQLITE_FLOAT, /* 0x1a */
202 SQLITE_NULL, /* 0x1b */
203 SQLITE_INTEGER, /* 0x1c */
204 SQLITE_NULL, /* 0x1d */
205 SQLITE_INTEGER, /* 0x1e */
206 SQLITE_NULL, /* 0x1f */
207 };
208 return aType[pVal->flags&MEM_AffMask];
182 } 209 }
183 210
184 /**************************** sqlite3_result_ ******************************* 211 /**************************** sqlite3_result_ *******************************
185 ** The following routines are used by user-defined functions to specify 212 ** The following routines are used by user-defined functions to specify
186 ** the function result. 213 ** the function result.
187 ** 214 **
188 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the 215 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
189 ** result as a string or blob but if the string or blob is too large, it 216 ** result as a string or blob but if the string or blob is too large, it
190 ** then sets the error code to SQLITE_TOOBIG 217 ** then sets the error code to SQLITE_TOOBIG
218 **
219 ** The invokeValueDestructor(P,X) routine invokes destructor function X()
220 ** on value P is not going to be used and need to be destroyed.
191 */ 221 */
192 static void setResultStrOrError( 222 static void setResultStrOrError(
193 sqlite3_context *pCtx, /* Function context */ 223 sqlite3_context *pCtx, /* Function context */
194 const char *z, /* String pointer */ 224 const char *z, /* String pointer */
195 int n, /* Bytes in string, or negative */ 225 int n, /* Bytes in string, or negative */
196 u8 enc, /* Encoding of z. 0 for BLOBs */ 226 u8 enc, /* Encoding of z. 0 for BLOBs */
197 void (*xDel)(void*) /* Destructor function */ 227 void (*xDel)(void*) /* Destructor function */
198 ){ 228 ){
199 if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){ 229 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
200 sqlite3_result_error_toobig(pCtx); 230 sqlite3_result_error_toobig(pCtx);
201 } 231 }
202 } 232 }
233 static int invokeValueDestructor(
234 const void *p, /* Value to destroy */
235 void (*xDel)(void*), /* The destructor */
236 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
237 ){
238 assert( xDel!=SQLITE_DYNAMIC );
239 if( xDel==0 ){
240 /* noop */
241 }else if( xDel==SQLITE_TRANSIENT ){
242 /* noop */
243 }else{
244 xDel((void*)p);
245 }
246 if( pCtx ) sqlite3_result_error_toobig(pCtx);
247 return SQLITE_TOOBIG;
248 }
203 void sqlite3_result_blob( 249 void sqlite3_result_blob(
204 sqlite3_context *pCtx, 250 sqlite3_context *pCtx,
205 const void *z, 251 const void *z,
206 int n, 252 int n,
207 void (*xDel)(void *) 253 void (*xDel)(void *)
208 ){ 254 ){
209 assert( n>=0 ); 255 assert( n>=0 );
210 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 256 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
211 setResultStrOrError(pCtx, z, n, 0, xDel); 257 setResultStrOrError(pCtx, z, n, 0, xDel);
212 } 258 }
259 void sqlite3_result_blob64(
260 sqlite3_context *pCtx,
261 const void *z,
262 sqlite3_uint64 n,
263 void (*xDel)(void *)
264 ){
265 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
266 assert( xDel!=SQLITE_DYNAMIC );
267 if( n>0x7fffffff ){
268 (void)invokeValueDestructor(z, xDel, pCtx);
269 }else{
270 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
271 }
272 }
213 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 273 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
214 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 274 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
215 sqlite3VdbeMemSetDouble(&pCtx->s, rVal); 275 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
216 } 276 }
217 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 277 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
218 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 278 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
219 pCtx->isError = SQLITE_ERROR; 279 pCtx->isError = SQLITE_ERROR;
220 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 280 pCtx->fErrorOrAux = 1;
281 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
221 } 282 }
222 #ifndef SQLITE_OMIT_UTF16 283 #ifndef SQLITE_OMIT_UTF16
223 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 284 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
224 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 285 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
225 pCtx->isError = SQLITE_ERROR; 286 pCtx->isError = SQLITE_ERROR;
226 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 287 pCtx->fErrorOrAux = 1;
288 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
227 } 289 }
228 #endif 290 #endif
229 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 291 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
230 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 292 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
231 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 293 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
232 } 294 }
233 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 295 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
234 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 296 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
235 sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 297 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
236 } 298 }
237 void sqlite3_result_null(sqlite3_context *pCtx){ 299 void sqlite3_result_null(sqlite3_context *pCtx){
238 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 300 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
239 sqlite3VdbeMemSetNull(&pCtx->s); 301 sqlite3VdbeMemSetNull(pCtx->pOut);
240 } 302 }
241 void sqlite3_result_text( 303 void sqlite3_result_text(
242 sqlite3_context *pCtx, 304 sqlite3_context *pCtx,
243 const char *z, 305 const char *z,
244 int n, 306 int n,
245 void (*xDel)(void *) 307 void (*xDel)(void *)
246 ){ 308 ){
247 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 309 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
248 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 310 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
249 } 311 }
312 void sqlite3_result_text64(
313 sqlite3_context *pCtx,
314 const char *z,
315 sqlite3_uint64 n,
316 void (*xDel)(void *),
317 unsigned char enc
318 ){
319 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
320 assert( xDel!=SQLITE_DYNAMIC );
321 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
322 if( n>0x7fffffff ){
323 (void)invokeValueDestructor(z, xDel, pCtx);
324 }else{
325 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
326 }
327 }
250 #ifndef SQLITE_OMIT_UTF16 328 #ifndef SQLITE_OMIT_UTF16
251 void sqlite3_result_text16( 329 void sqlite3_result_text16(
252 sqlite3_context *pCtx, 330 sqlite3_context *pCtx,
253 const void *z, 331 const void *z,
254 int n, 332 int n,
255 void (*xDel)(void *) 333 void (*xDel)(void *)
256 ){ 334 ){
257 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 335 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
258 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); 336 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
259 } 337 }
260 void sqlite3_result_text16be( 338 void sqlite3_result_text16be(
261 sqlite3_context *pCtx, 339 sqlite3_context *pCtx,
262 const void *z, 340 const void *z,
263 int n, 341 int n,
264 void (*xDel)(void *) 342 void (*xDel)(void *)
265 ){ 343 ){
266 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 344 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
267 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); 345 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
268 } 346 }
269 void sqlite3_result_text16le( 347 void sqlite3_result_text16le(
270 sqlite3_context *pCtx, 348 sqlite3_context *pCtx,
271 const void *z, 349 const void *z,
272 int n, 350 int n,
273 void (*xDel)(void *) 351 void (*xDel)(void *)
274 ){ 352 ){
275 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 353 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
276 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); 354 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
277 } 355 }
278 #endif /* SQLITE_OMIT_UTF16 */ 356 #endif /* SQLITE_OMIT_UTF16 */
279 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 357 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
280 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 358 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
281 sqlite3VdbeMemCopy(&pCtx->s, pValue); 359 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
282 } 360 }
283 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 361 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
284 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 362 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
285 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n); 363 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
286 } 364 }
287 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 365 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
288 pCtx->isError = errCode; 366 pCtx->isError = errCode;
289 if( pCtx->s.flags & MEM_Null ){ 367 pCtx->fErrorOrAux = 1;
290 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 368 if( pCtx->pOut->flags & MEM_Null ){
369 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
291 SQLITE_UTF8, SQLITE_STATIC); 370 SQLITE_UTF8, SQLITE_STATIC);
292 } 371 }
293 } 372 }
294 373
295 /* Force an SQLITE_TOOBIG error. */ 374 /* Force an SQLITE_TOOBIG error. */
296 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 375 void sqlite3_result_error_toobig(sqlite3_context *pCtx){
297 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 376 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
298 pCtx->isError = SQLITE_TOOBIG; 377 pCtx->isError = SQLITE_TOOBIG;
299 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 378 pCtx->fErrorOrAux = 1;
379 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
300 SQLITE_UTF8, SQLITE_STATIC); 380 SQLITE_UTF8, SQLITE_STATIC);
301 } 381 }
302 382
303 /* An SQLITE_NOMEM error. */ 383 /* An SQLITE_NOMEM error. */
304 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 384 void sqlite3_result_error_nomem(sqlite3_context *pCtx){
305 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 385 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
306 sqlite3VdbeMemSetNull(&pCtx->s); 386 sqlite3VdbeMemSetNull(pCtx->pOut);
307 pCtx->isError = SQLITE_NOMEM; 387 pCtx->isError = SQLITE_NOMEM;
308 pCtx->s.db->mallocFailed = 1; 388 pCtx->fErrorOrAux = 1;
389 pCtx->pOut->db->mallocFailed = 1;
309 } 390 }
310 391
311 /* 392 /*
312 ** This function is called after a transaction has been committed. It 393 ** This function is called after a transaction has been committed. It
313 ** invokes callbacks registered with sqlite3_wal_hook() as required. 394 ** invokes callbacks registered with sqlite3_wal_hook() as required.
314 */ 395 */
315 static int doWalCallbacks(sqlite3 *db){ 396 static int doWalCallbacks(sqlite3 *db){
316 int rc = SQLITE_OK; 397 int rc = SQLITE_OK;
317 #ifndef SQLITE_OMIT_WAL 398 #ifndef SQLITE_OMIT_WAL
318 int i; 399 int i;
(...skipping 28 matching lines...) Expand all
347 /* We used to require that sqlite3_reset() be called before retrying 428 /* We used to require that sqlite3_reset() be called before retrying
348 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning 429 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
349 ** with version 3.7.0, we changed this so that sqlite3_reset() would 430 ** with version 3.7.0, we changed this so that sqlite3_reset() would
350 ** be called automatically instead of throwing the SQLITE_MISUSE error. 431 ** be called automatically instead of throwing the SQLITE_MISUSE error.
351 ** This "automatic-reset" change is not technically an incompatibility, 432 ** This "automatic-reset" change is not technically an incompatibility,
352 ** since any application that receives an SQLITE_MISUSE is broken by 433 ** since any application that receives an SQLITE_MISUSE is broken by
353 ** definition. 434 ** definition.
354 ** 435 **
355 ** Nevertheless, some published applications that were originally written 436 ** Nevertheless, some published applications that were originally written
356 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 437 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
357 ** returns, and the so were broken by the automatic-reset change. As a 438 ** returns, and those were broken by the automatic-reset change. As a
358 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 439 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
359 ** legacy behavior of returning SQLITE_MISUSE for cases where the 440 ** legacy behavior of returning SQLITE_MISUSE for cases where the
360 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 441 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
361 ** or SQLITE_BUSY error. 442 ** or SQLITE_BUSY error.
362 */ 443 */
363 #ifdef SQLITE_OMIT_AUTORESET 444 #ifdef SQLITE_OMIT_AUTORESET
364 if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){ 445 if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
365 sqlite3_reset((sqlite3_stmt*)p); 446 sqlite3_reset((sqlite3_stmt*)p);
366 }else{ 447 }else{
367 return SQLITE_MISUSE_BKPT; 448 return SQLITE_MISUSE_BKPT;
(...skipping 13 matching lines...) Expand all
381 if( p->pc<=0 && p->expired ){ 462 if( p->pc<=0 && p->expired ){
382 p->rc = SQLITE_SCHEMA; 463 p->rc = SQLITE_SCHEMA;
383 rc = SQLITE_ERROR; 464 rc = SQLITE_ERROR;
384 goto end_of_step; 465 goto end_of_step;
385 } 466 }
386 if( p->pc<0 ){ 467 if( p->pc<0 ){
387 /* If there are no other statements currently running, then 468 /* If there are no other statements currently running, then
388 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 469 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
389 ** from interrupting a statement that has not yet started. 470 ** from interrupting a statement that has not yet started.
390 */ 471 */
391 if( db->activeVdbeCnt==0 ){ 472 if( db->nVdbeActive==0 ){
392 db->u1.isInterrupted = 0; 473 db->u1.isInterrupted = 0;
393 } 474 }
394 475
395 assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 ); 476 assert( db->nVdbeWrite>0 || db->autoCommit==0
477 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
478 );
396 479
397 #ifndef SQLITE_OMIT_TRACE 480 #ifndef SQLITE_OMIT_TRACE
398 if( db->xProfile && !db->init.busy ){ 481 if( db->xProfile && !db->init.busy ){
399 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 482 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
400 } 483 }
401 #endif 484 #endif
402 485
403 db->activeVdbeCnt++; 486 db->nVdbeActive++;
404 if( p->readOnly==0 ) db->writeVdbeCnt++; 487 if( p->readOnly==0 ) db->nVdbeWrite++;
488 if( p->bIsReader ) db->nVdbeRead++;
405 p->pc = 0; 489 p->pc = 0;
406 } 490 }
407 #ifndef SQLITE_OMIT_EXPLAIN 491 #ifndef SQLITE_OMIT_EXPLAIN
408 if( p->explain ){ 492 if( p->explain ){
409 rc = sqlite3VdbeList(p); 493 rc = sqlite3VdbeList(p);
410 }else 494 }else
411 #endif /* SQLITE_OMIT_EXPLAIN */ 495 #endif /* SQLITE_OMIT_EXPLAIN */
412 { 496 {
413 db->vdbeExecCnt++; 497 db->nVdbeExec++;
414 rc = sqlite3VdbeExec(p); 498 rc = sqlite3VdbeExec(p);
415 db->vdbeExecCnt--; 499 db->nVdbeExec--;
416 } 500 }
417 501
418 #ifndef SQLITE_OMIT_TRACE 502 #ifndef SQLITE_OMIT_TRACE
419 /* Invoke the profile callback if there is one 503 /* Invoke the profile callback if there is one
420 */ 504 */
421 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){ 505 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
422 sqlite3_int64 iNow; 506 sqlite3_int64 iNow;
423 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 507 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
424 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000); 508 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
425 } 509 }
(...skipping 18 matching lines...) Expand all
444 ** be one of the values in the first assert() below. Variable p->rc 528 ** be one of the values in the first assert() below. Variable p->rc
445 ** contains the value that would be returned if sqlite3_finalize() 529 ** contains the value that would be returned if sqlite3_finalize()
446 ** were called on statement p. 530 ** were called on statement p.
447 */ 531 */
448 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 532 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
449 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE 533 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
450 ); 534 );
451 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE ); 535 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
452 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 536 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
453 /* If this statement was prepared using sqlite3_prepare_v2(), and an 537 /* If this statement was prepared using sqlite3_prepare_v2(), and an
454 ** error has occured, then return the error code in p->rc to the 538 ** error has occurred, then return the error code in p->rc to the
455 ** caller. Set the error code in the database handle to the same value. 539 ** caller. Set the error code in the database handle to the same value.
456 */ 540 */
457 rc = db->errCode = p->rc; 541 rc = sqlite3VdbeTransferError(p);
458 } 542 }
459 return (rc&db->errMask); 543 return (rc&db->errMask);
460 } 544 }
461 545
462 /* 546 /*
463 ** This is the top-level implementation of sqlite3_step(). Call 547 ** This is the top-level implementation of sqlite3_step(). Call
464 ** sqlite3Step() to do most of the work. If a schema error occurs, 548 ** sqlite3Step() to do most of the work. If a schema error occurs,
465 ** call sqlite3Reprepare() and try again. 549 ** call sqlite3Reprepare() and try again.
466 */ 550 */
467 int sqlite3_step(sqlite3_stmt *pStmt){ 551 int sqlite3_step(sqlite3_stmt *pStmt){
468 int rc = SQLITE_OK; /* Result from sqlite3Step() */ 552 int rc = SQLITE_OK; /* Result from sqlite3Step() */
469 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ 553 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */
470 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 554 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
471 int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 555 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
472 sqlite3 *db; /* The database connection */ 556 sqlite3 *db; /* The database connection */
473 557
474 if( vdbeSafetyNotNull(v) ){ 558 if( vdbeSafetyNotNull(v) ){
475 return SQLITE_MISUSE_BKPT; 559 return SQLITE_MISUSE_BKPT;
476 } 560 }
477 db = v->db; 561 db = v->db;
478 sqlite3_mutex_enter(db->mutex); 562 sqlite3_mutex_enter(db->mutex);
563 v->doingRerun = 0;
479 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 564 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
480 && cnt++ < 5 565 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
481 && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){ 566 int savedPc = v->pc;
567 rc2 = rc = sqlite3Reprepare(v);
568 if( rc!=SQLITE_OK) break;
482 sqlite3_reset(pStmt); 569 sqlite3_reset(pStmt);
483 v->expired = 0; 570 if( savedPc>=0 ) v->doingRerun = 1;
571 assert( v->expired==0 );
484 } 572 }
485 if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){ 573 if( rc2!=SQLITE_OK ){
486 /* This case occurs after failing to recompile an sql statement. 574 /* This case occurs after failing to recompile an sql statement.
487 ** The error message from the SQL compiler has already been loaded 575 ** The error message from the SQL compiler has already been loaded
488 ** into the database handle. This block copies the error message 576 ** into the database handle. This block copies the error message
489 ** from the database handle into the statement and sets the statement 577 ** from the database handle into the statement and sets the statement
490 ** program counter to 0 to ensure that when the statement is 578 ** program counter to 0 to ensure that when the statement is
491 ** finalized or reset the parser error message is available via 579 ** finalized or reset the parser error message is available via
492 ** sqlite3_errmsg() and sqlite3_errcode(). 580 ** sqlite3_errmsg() and sqlite3_errcode().
493 */ 581 */
494 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 582 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
583 assert( zErr!=0 || db->mallocFailed );
495 sqlite3DbFree(db, v->zErrMsg); 584 sqlite3DbFree(db, v->zErrMsg);
496 if( !db->mallocFailed ){ 585 if( !db->mallocFailed ){
497 v->zErrMsg = sqlite3DbStrDup(db, zErr); 586 v->zErrMsg = sqlite3DbStrDup(db, zErr);
498 v->rc = rc2; 587 v->rc = rc2;
499 } else { 588 } else {
500 v->zErrMsg = 0; 589 v->zErrMsg = 0;
501 v->rc = rc = SQLITE_NOMEM; 590 v->rc = rc = SQLITE_NOMEM;
502 } 591 }
503 } 592 }
504 rc = sqlite3ApiExit(db, rc); 593 rc = sqlite3ApiExit(db, rc);
505 sqlite3_mutex_leave(db->mutex); 594 sqlite3_mutex_leave(db->mutex);
506 return rc; 595 return rc;
507 } 596 }
508 597
598
509 /* 599 /*
510 ** Extract the user data from a sqlite3_context structure and return a 600 ** Extract the user data from a sqlite3_context structure and return a
511 ** pointer to it. 601 ** pointer to it.
512 */ 602 */
513 void *sqlite3_user_data(sqlite3_context *p){ 603 void *sqlite3_user_data(sqlite3_context *p){
514 assert( p && p->pFunc ); 604 assert( p && p->pFunc );
515 return p->pFunc->pUserData; 605 return p->pFunc->pUserData;
516 } 606 }
517 607
518 /* 608 /*
519 ** Extract the user data from a sqlite3_context structure and return a 609 ** Extract the user data from a sqlite3_context structure and return a
520 ** pointer to it. 610 ** pointer to it.
521 ** 611 **
522 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 612 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
523 ** returns a copy of the pointer to the database connection (the 1st 613 ** returns a copy of the pointer to the database connection (the 1st
524 ** parameter) of the sqlite3_create_function() and 614 ** parameter) of the sqlite3_create_function() and
525 ** sqlite3_create_function16() routines that originally registered the 615 ** sqlite3_create_function16() routines that originally registered the
526 ** application defined function. 616 ** application defined function.
527 */ 617 */
528 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 618 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
529 assert( p && p->pFunc ); 619 assert( p && p->pFunc );
530 return p->s.db; 620 return p->pOut->db;
531 } 621 }
532 622
533 /* 623 /*
624 ** Return the current time for a statement
625 */
626 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
627 Vdbe *v = p->pVdbe;
628 int rc;
629 if( v->iCurrentTime==0 ){
630 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, &v->iCurrentTime);
631 if( rc ) v->iCurrentTime = 0;
632 }
633 return v->iCurrentTime;
634 }
635
636 /*
534 ** The following is the implementation of an SQL function that always 637 ** The following is the implementation of an SQL function that always
535 ** fails with an error message stating that the function is used in the 638 ** fails with an error message stating that the function is used in the
536 ** wrong context. The sqlite3_overload_function() API might construct 639 ** wrong context. The sqlite3_overload_function() API might construct
537 ** SQL function that use this routine so that the functions will exist 640 ** SQL function that use this routine so that the functions will exist
538 ** for name resolution but are actually overloaded by the xFindFunction 641 ** for name resolution but are actually overloaded by the xFindFunction
539 ** method of virtual tables. 642 ** method of virtual tables.
540 */ 643 */
541 void sqlite3InvalidFunction( 644 void sqlite3InvalidFunction(
542 sqlite3_context *context, /* The function calling context */ 645 sqlite3_context *context, /* The function calling context */
543 int NotUsed, /* Number of arguments to the function */ 646 int NotUsed, /* Number of arguments to the function */
544 sqlite3_value **NotUsed2 /* Value of each argument */ 647 sqlite3_value **NotUsed2 /* Value of each argument */
545 ){ 648 ){
546 const char *zName = context->pFunc->zName; 649 const char *zName = context->pFunc->zName;
547 char *zErr; 650 char *zErr;
548 UNUSED_PARAMETER2(NotUsed, NotUsed2); 651 UNUSED_PARAMETER2(NotUsed, NotUsed2);
549 zErr = sqlite3_mprintf( 652 zErr = sqlite3_mprintf(
550 "unable to use function %s in the requested context", zName); 653 "unable to use function %s in the requested context", zName);
551 sqlite3_result_error(context, zErr, -1); 654 sqlite3_result_error(context, zErr, -1);
552 sqlite3_free(zErr); 655 sqlite3_free(zErr);
553 } 656 }
554 657
555 /* 658 /*
659 ** Create a new aggregate context for p and return a pointer to
660 ** its pMem->z element.
661 */
662 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
663 Mem *pMem = p->pMem;
664 assert( (pMem->flags & MEM_Agg)==0 );
665 if( nByte<=0 ){
666 sqlite3VdbeMemSetNull(pMem);
667 pMem->z = 0;
668 }else{
669 sqlite3VdbeMemClearAndResize(pMem, nByte);
670 pMem->flags = MEM_Agg;
671 pMem->u.pDef = p->pFunc;
672 if( pMem->z ){
673 memset(pMem->z, 0, nByte);
674 }
675 }
676 return (void*)pMem->z;
677 }
678
679 /*
556 ** Allocate or return the aggregate context for a user function. A new 680 ** Allocate or return the aggregate context for a user function. A new
557 ** context is allocated on the first call. Subsequent calls return the 681 ** context is allocated on the first call. Subsequent calls return the
558 ** same context that was returned on prior calls. 682 ** same context that was returned on prior calls.
559 */ 683 */
560 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 684 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
561 Mem *pMem;
562 assert( p && p->pFunc && p->pFunc->xStep ); 685 assert( p && p->pFunc && p->pFunc->xStep );
563 assert( sqlite3_mutex_held(p->s.db->mutex) ); 686 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
564 pMem = p->pMem;
565 testcase( nByte<0 ); 687 testcase( nByte<0 );
566 if( (pMem->flags & MEM_Agg)==0 ){ 688 if( (p->pMem->flags & MEM_Agg)==0 ){
567 if( nByte<=0 ){ 689 return createAggContext(p, nByte);
568 sqlite3VdbeMemReleaseExternal(pMem); 690 }else{
569 pMem->flags = MEM_Null; 691 return (void*)p->pMem->z;
570 pMem->z = 0;
571 }else{
572 sqlite3VdbeMemGrow(pMem, nByte, 0);
573 pMem->flags = MEM_Agg;
574 pMem->u.pDef = p->pFunc;
575 if( pMem->z ){
576 memset(pMem->z, 0, nByte);
577 }
578 }
579 } 692 }
580 return (void*)pMem->z;
581 } 693 }
582 694
583 /* 695 /*
584 ** Return the auxilary data pointer, if any, for the iArg'th argument to 696 ** Return the auxiliary data pointer, if any, for the iArg'th argument to
585 ** the user-function defined by pCtx. 697 ** the user-function defined by pCtx.
586 */ 698 */
587 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 699 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
588 VdbeFunc *pVdbeFunc; 700 AuxData *pAuxData;
589 701
590 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 702 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
591 pVdbeFunc = pCtx->pVdbeFunc; 703 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
592 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 704 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
593 return 0;
594 } 705 }
595 return pVdbeFunc->apAux[iArg].pAux; 706
707 return (pAuxData ? pAuxData->pAux : 0);
596 } 708 }
597 709
598 /* 710 /*
599 ** Set the auxilary data pointer and delete function, for the iArg'th 711 ** Set the auxiliary data pointer and delete function, for the iArg'th
600 ** argument to the user-function defined by pCtx. Any previous value is 712 ** argument to the user-function defined by pCtx. Any previous value is
601 ** deleted by calling the delete function specified when it was set. 713 ** deleted by calling the delete function specified when it was set.
602 */ 714 */
603 void sqlite3_set_auxdata( 715 void sqlite3_set_auxdata(
604 sqlite3_context *pCtx, 716 sqlite3_context *pCtx,
605 int iArg, 717 int iArg,
606 void *pAux, 718 void *pAux,
607 void (*xDelete)(void*) 719 void (*xDelete)(void*)
608 ){ 720 ){
609 struct AuxData *pAuxData; 721 AuxData *pAuxData;
610 VdbeFunc *pVdbeFunc; 722 Vdbe *pVdbe = pCtx->pVdbe;
723
724 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
611 if( iArg<0 ) goto failed; 725 if( iArg<0 ) goto failed;
612 726
613 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 727 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
614 pVdbeFunc = pCtx->pVdbeFunc; 728 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
615 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 729 }
616 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0); 730 if( pAuxData==0 ){
617 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 731 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
618 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc); 732 if( !pAuxData ) goto failed;
619 if( !pVdbeFunc ){ 733 pAuxData->iOp = pCtx->iOp;
620 goto failed; 734 pAuxData->iArg = iArg;
735 pAuxData->pNext = pVdbe->pAuxData;
736 pVdbe->pAuxData = pAuxData;
737 if( pCtx->fErrorOrAux==0 ){
738 pCtx->isError = 0;
739 pCtx->fErrorOrAux = 1;
621 } 740 }
622 pCtx->pVdbeFunc = pVdbeFunc; 741 }else if( pAuxData->xDelete ){
623 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux)); 742 pAuxData->xDelete(pAuxData->pAux);
624 pVdbeFunc->nAux = iArg+1;
625 pVdbeFunc->pFunc = pCtx->pFunc;
626 } 743 }
627 744
628 pAuxData = &pVdbeFunc->apAux[iArg];
629 if( pAuxData->pAux && pAuxData->xDelete ){
630 pAuxData->xDelete(pAuxData->pAux);
631 }
632 pAuxData->pAux = pAux; 745 pAuxData->pAux = pAux;
633 pAuxData->xDelete = xDelete; 746 pAuxData->xDelete = xDelete;
634 return; 747 return;
635 748
636 failed: 749 failed:
637 if( xDelete ){ 750 if( xDelete ){
638 xDelete(pAux); 751 xDelete(pAux);
639 } 752 }
640 } 753 }
641 754
642 #ifndef SQLITE_OMIT_DEPRECATED 755 #ifndef SQLITE_OMIT_DEPRECATED
643 /* 756 /*
644 ** Return the number of times the Step function of a aggregate has been 757 ** Return the number of times the Step function of an aggregate has been
645 ** called. 758 ** called.
646 ** 759 **
647 ** This function is deprecated. Do not use it for new code. It is 760 ** This function is deprecated. Do not use it for new code. It is
648 ** provide only to avoid breaking legacy code. New aggregate function 761 ** provide only to avoid breaking legacy code. New aggregate function
649 ** implementations should keep their own counts within their aggregate 762 ** implementations should keep their own counts within their aggregate
650 ** context. 763 ** context.
651 */ 764 */
652 int sqlite3_aggregate_count(sqlite3_context *p){ 765 int sqlite3_aggregate_count(sqlite3_context *p){
653 assert( p && p->pMem && p->pFunc && p->pFunc->xStep ); 766 assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
654 return p->pMem->n; 767 return p->pMem->n;
(...skipping 11 matching lines...) Expand all
666 /* 779 /*
667 ** Return the number of values available from the current row of the 780 ** Return the number of values available from the current row of the
668 ** currently executing statement pStmt. 781 ** currently executing statement pStmt.
669 */ 782 */
670 int sqlite3_data_count(sqlite3_stmt *pStmt){ 783 int sqlite3_data_count(sqlite3_stmt *pStmt){
671 Vdbe *pVm = (Vdbe *)pStmt; 784 Vdbe *pVm = (Vdbe *)pStmt;
672 if( pVm==0 || pVm->pResultSet==0 ) return 0; 785 if( pVm==0 || pVm->pResultSet==0 ) return 0;
673 return pVm->nResColumn; 786 return pVm->nResColumn;
674 } 787 }
675 788
789 /*
790 ** Return a pointer to static memory containing an SQL NULL value.
791 */
792 static const Mem *columnNullValue(void){
793 /* Even though the Mem structure contains an element
794 ** of type i64, on certain architectures (x86) with certain compiler
795 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
796 ** instead of an 8-byte one. This all works fine, except that when
797 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
798 ** that a Mem structure is located on an 8-byte boundary. To prevent
799 ** these assert()s from failing, when building with SQLITE_DEBUG defined
800 ** using gcc, we force nullMem to be 8-byte aligned using the magical
801 ** __attribute__((aligned(8))) macro. */
802 static const Mem nullMem
803 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
804 __attribute__((aligned(8)))
805 #endif
806 = {
807 /* .u = */ {0},
808 /* .flags = */ MEM_Null,
809 /* .enc = */ 0,
810 /* .n = */ 0,
811 /* .z = */ 0,
812 /* .zMalloc = */ 0,
813 /* .szMalloc = */ 0,
814 /* .iPadding1 = */ 0,
815 /* .db = */ 0,
816 /* .xDel = */ 0,
817 #ifdef SQLITE_DEBUG
818 /* .pScopyFrom = */ 0,
819 /* .pFiller = */ 0,
820 #endif
821 };
822 return &nullMem;
823 }
676 824
677 /* 825 /*
678 ** Check to see if column iCol of the given statement is valid. If 826 ** Check to see if column iCol of the given statement is valid. If
679 ** it is, return a pointer to the Mem for the value of that column. 827 ** it is, return a pointer to the Mem for the value of that column.
680 ** If iCol is not valid, return a pointer to a Mem which has a value 828 ** If iCol is not valid, return a pointer to a Mem which has a value
681 ** of NULL. 829 ** of NULL.
682 */ 830 */
683 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 831 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
684 Vdbe *pVm; 832 Vdbe *pVm;
685 Mem *pOut; 833 Mem *pOut;
686 834
687 pVm = (Vdbe *)pStmt; 835 pVm = (Vdbe *)pStmt;
688 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 836 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
689 sqlite3_mutex_enter(pVm->db->mutex); 837 sqlite3_mutex_enter(pVm->db->mutex);
690 pOut = &pVm->pResultSet[i]; 838 pOut = &pVm->pResultSet[i];
691 }else{ 839 }else{
692 /* If the value passed as the second argument is out of range, return
693 ** a pointer to the following static Mem object which contains the
694 ** value SQL NULL. Even though the Mem structure contains an element
695 ** of type i64, on certain architecture (x86) with certain compiler
696 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
697 ** instead of an 8-byte one. This all works fine, except that when
698 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
699 ** that a Mem structure is located on an 8-byte boundary. To prevent
700 ** this assert() from failing, when building with SQLITE_DEBUG defined
701 ** using gcc, force nullMem to be 8-byte aligned using the magical
702 ** __attribute__((aligned(8))) macro. */
703 static const Mem nullMem
704 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
705 __attribute__((aligned(8)))
706 #endif
707 = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
708 #ifdef SQLITE_DEBUG
709 0, 0, /* pScopyFrom, pFiller */
710 #endif
711 0, 0 };
712
713 if( pVm && ALWAYS(pVm->db) ){ 840 if( pVm && ALWAYS(pVm->db) ){
714 sqlite3_mutex_enter(pVm->db->mutex); 841 sqlite3_mutex_enter(pVm->db->mutex);
715 sqlite3Error(pVm->db, SQLITE_RANGE, 0); 842 sqlite3Error(pVm->db, SQLITE_RANGE);
716 } 843 }
717 pOut = (Mem*)&nullMem; 844 pOut = (Mem*)columnNullValue();
718 } 845 }
719 return pOut; 846 return pOut;
720 } 847 }
721 848
722 /* 849 /*
723 ** This function is called after invoking an sqlite3_value_XXX function on a 850 ** This function is called after invoking an sqlite3_value_XXX function on a
724 ** column value (i.e. a value returned by evaluating an SQL expression in the 851 ** column value (i.e. a value returned by evaluating an SQL expression in the
725 ** select list of a SELECT statement) that may cause a malloc() failure. If 852 ** select list of a SELECT statement) that may cause a malloc() failure. If
726 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 853 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
727 ** code of statement pStmt set to SQLITE_NOMEM. 854 ** code of statement pStmt set to SQLITE_NOMEM.
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 columnMallocFailure(pStmt); 937 columnMallocFailure(pStmt);
811 return val; 938 return val;
812 } 939 }
813 #endif /* SQLITE_OMIT_UTF16 */ 940 #endif /* SQLITE_OMIT_UTF16 */
814 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 941 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
815 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 942 int iType = sqlite3_value_type( columnMem(pStmt,i) );
816 columnMallocFailure(pStmt); 943 columnMallocFailure(pStmt);
817 return iType; 944 return iType;
818 } 945 }
819 946
820 /* The following function is experimental and subject to change or
821 ** removal */
822 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
823 ** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
824 **}
825 */
826
827 /* 947 /*
828 ** Convert the N-th element of pStmt->pColName[] into a string using 948 ** Convert the N-th element of pStmt->pColName[] into a string using
829 ** xFunc() then return that string. If N is out of range, return 0. 949 ** xFunc() then return that string. If N is out of range, return 0.
830 ** 950 **
831 ** There are up to 5 names for each column. useType determines which 951 ** There are up to 5 names for each column. useType determines which
832 ** name is returned. Here are the names: 952 ** name is returned. Here are the names:
833 ** 953 **
834 ** 0 The column name as it should be displayed for output 954 ** 0 The column name as it should be displayed for output
835 ** 1 The datatype name for the column 955 ** 1 The datatype name for the column
836 ** 2 The name of the database that the column derives from 956 ** 2 The name of the database that the column derives from
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
908 return columnName( 1028 return columnName(
909 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 1029 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
910 } 1030 }
911 #endif /* SQLITE_OMIT_UTF16 */ 1031 #endif /* SQLITE_OMIT_UTF16 */
912 #endif /* SQLITE_OMIT_DECLTYPE */ 1032 #endif /* SQLITE_OMIT_DECLTYPE */
913 1033
914 #ifdef SQLITE_ENABLE_COLUMN_METADATA 1034 #ifdef SQLITE_ENABLE_COLUMN_METADATA
915 /* 1035 /*
916 ** Return the name of the database from which a result column derives. 1036 ** Return the name of the database from which a result column derives.
917 ** NULL is returned if the result column is an expression or constant or 1037 ** NULL is returned if the result column is an expression or constant or
918 ** anything else which is not an unabiguous reference to a database column. 1038 ** anything else which is not an unambiguous reference to a database column.
919 */ 1039 */
920 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 1040 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
921 return columnName( 1041 return columnName(
922 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 1042 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
923 } 1043 }
924 #ifndef SQLITE_OMIT_UTF16 1044 #ifndef SQLITE_OMIT_UTF16
925 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 1045 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
926 return columnName( 1046 return columnName(
927 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 1047 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
928 } 1048 }
929 #endif /* SQLITE_OMIT_UTF16 */ 1049 #endif /* SQLITE_OMIT_UTF16 */
930 1050
931 /* 1051 /*
932 ** Return the name of the table from which a result column derives. 1052 ** Return the name of the table from which a result column derives.
933 ** NULL is returned if the result column is an expression or constant or 1053 ** NULL is returned if the result column is an expression or constant or
934 ** anything else which is not an unabiguous reference to a database column. 1054 ** anything else which is not an unambiguous reference to a database column.
935 */ 1055 */
936 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 1056 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
937 return columnName( 1057 return columnName(
938 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 1058 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
939 } 1059 }
940 #ifndef SQLITE_OMIT_UTF16 1060 #ifndef SQLITE_OMIT_UTF16
941 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 1061 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
942 return columnName( 1062 return columnName(
943 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 1063 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
944 } 1064 }
945 #endif /* SQLITE_OMIT_UTF16 */ 1065 #endif /* SQLITE_OMIT_UTF16 */
946 1066
947 /* 1067 /*
948 ** Return the name of the table column from which a result column derives. 1068 ** Return the name of the table column from which a result column derives.
949 ** NULL is returned if the result column is an expression or constant or 1069 ** NULL is returned if the result column is an expression or constant or
950 ** anything else which is not an unabiguous reference to a database column. 1070 ** anything else which is not an unambiguous reference to a database column.
951 */ 1071 */
952 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 1072 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
953 return columnName( 1073 return columnName(
954 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 1074 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
955 } 1075 }
956 #ifndef SQLITE_OMIT_UTF16 1076 #ifndef SQLITE_OMIT_UTF16
957 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 1077 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
958 return columnName( 1078 return columnName(
959 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 1079 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
960 } 1080 }
(...skipping 16 matching lines...) Expand all
977 ** The error code stored in database p->db is overwritten with the return 1097 ** The error code stored in database p->db is overwritten with the return
978 ** value in any case. 1098 ** value in any case.
979 */ 1099 */
980 static int vdbeUnbind(Vdbe *p, int i){ 1100 static int vdbeUnbind(Vdbe *p, int i){
981 Mem *pVar; 1101 Mem *pVar;
982 if( vdbeSafetyNotNull(p) ){ 1102 if( vdbeSafetyNotNull(p) ){
983 return SQLITE_MISUSE_BKPT; 1103 return SQLITE_MISUSE_BKPT;
984 } 1104 }
985 sqlite3_mutex_enter(p->db->mutex); 1105 sqlite3_mutex_enter(p->db->mutex);
986 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 1106 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
987 sqlite3Error(p->db, SQLITE_MISUSE, 0); 1107 sqlite3Error(p->db, SQLITE_MISUSE);
988 sqlite3_mutex_leave(p->db->mutex); 1108 sqlite3_mutex_leave(p->db->mutex);
989 sqlite3_log(SQLITE_MISUSE, 1109 sqlite3_log(SQLITE_MISUSE,
990 "bind on a busy prepared statement: [%s]", p->zSql); 1110 "bind on a busy prepared statement: [%s]", p->zSql);
991 return SQLITE_MISUSE_BKPT; 1111 return SQLITE_MISUSE_BKPT;
992 } 1112 }
993 if( i<1 || i>p->nVar ){ 1113 if( i<1 || i>p->nVar ){
994 sqlite3Error(p->db, SQLITE_RANGE, 0); 1114 sqlite3Error(p->db, SQLITE_RANGE);
995 sqlite3_mutex_leave(p->db->mutex); 1115 sqlite3_mutex_leave(p->db->mutex);
996 return SQLITE_RANGE; 1116 return SQLITE_RANGE;
997 } 1117 }
998 i--; 1118 i--;
999 pVar = &p->aVar[i]; 1119 pVar = &p->aVar[i];
1000 sqlite3VdbeMemRelease(pVar); 1120 sqlite3VdbeMemRelease(pVar);
1001 pVar->flags = MEM_Null; 1121 pVar->flags = MEM_Null;
1002 sqlite3Error(p->db, SQLITE_OK, 0); 1122 sqlite3Error(p->db, SQLITE_OK);
1003 1123
1004 /* If the bit corresponding to this variable in Vdbe.expmask is set, then 1124 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
1005 ** binding a new value to this variable invalidates the current query plan. 1125 ** binding a new value to this variable invalidates the current query plan.
1006 ** 1126 **
1007 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host 1127 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
1008 ** parameter in the WHERE clause might influence the choice of query plan 1128 ** parameter in the WHERE clause might influence the choice of query plan
1009 ** for a statement, then the statement will be automatically recompiled, 1129 ** for a statement, then the statement will be automatically recompiled,
1010 ** as if there had been a schema change, on the first sqlite3_step() call 1130 ** as if there had been a schema change, on the first sqlite3_step() call
1011 ** following any change to the bindings of that parameter. 1131 ** following any change to the bindings of that parameter.
1012 */ 1132 */
(...skipping 21 matching lines...) Expand all
1034 int rc; 1154 int rc;
1035 1155
1036 rc = vdbeUnbind(p, i); 1156 rc = vdbeUnbind(p, i);
1037 if( rc==SQLITE_OK ){ 1157 if( rc==SQLITE_OK ){
1038 if( zData!=0 ){ 1158 if( zData!=0 ){
1039 pVar = &p->aVar[i-1]; 1159 pVar = &p->aVar[i-1];
1040 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 1160 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1041 if( rc==SQLITE_OK && encoding!=0 ){ 1161 if( rc==SQLITE_OK && encoding!=0 ){
1042 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 1162 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1043 } 1163 }
1044 sqlite3Error(p->db, rc, 0); 1164 sqlite3Error(p->db, rc);
1045 rc = sqlite3ApiExit(p->db, rc); 1165 rc = sqlite3ApiExit(p->db, rc);
1046 } 1166 }
1047 sqlite3_mutex_leave(p->db->mutex); 1167 sqlite3_mutex_leave(p->db->mutex);
1048 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 1168 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
1049 xDel((void*)zData); 1169 xDel((void*)zData);
1050 } 1170 }
1051 return rc; 1171 return rc;
1052 } 1172 }
1053 1173
1054 1174
1055 /* 1175 /*
1056 ** Bind a blob value to an SQL statement variable. 1176 ** Bind a blob value to an SQL statement variable.
1057 */ 1177 */
1058 int sqlite3_bind_blob( 1178 int sqlite3_bind_blob(
1059 sqlite3_stmt *pStmt, 1179 sqlite3_stmt *pStmt,
1060 int i, 1180 int i,
1061 const void *zData, 1181 const void *zData,
1062 int nData, 1182 int nData,
1063 void (*xDel)(void*) 1183 void (*xDel)(void*)
1064 ){ 1184 ){
1065 return bindText(pStmt, i, zData, nData, xDel, 0); 1185 return bindText(pStmt, i, zData, nData, xDel, 0);
1066 } 1186 }
1187 int sqlite3_bind_blob64(
1188 sqlite3_stmt *pStmt,
1189 int i,
1190 const void *zData,
1191 sqlite3_uint64 nData,
1192 void (*xDel)(void*)
1193 ){
1194 assert( xDel!=SQLITE_DYNAMIC );
1195 if( nData>0x7fffffff ){
1196 return invokeValueDestructor(zData, xDel, 0);
1197 }else{
1198 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
1199 }
1200 }
1067 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 1201 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
1068 int rc; 1202 int rc;
1069 Vdbe *p = (Vdbe *)pStmt; 1203 Vdbe *p = (Vdbe *)pStmt;
1070 rc = vdbeUnbind(p, i); 1204 rc = vdbeUnbind(p, i);
1071 if( rc==SQLITE_OK ){ 1205 if( rc==SQLITE_OK ){
1072 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 1206 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
1073 sqlite3_mutex_leave(p->db->mutex); 1207 sqlite3_mutex_leave(p->db->mutex);
1074 } 1208 }
1075 return rc; 1209 return rc;
1076 } 1210 }
(...skipping 21 matching lines...) Expand all
1098 } 1232 }
1099 int sqlite3_bind_text( 1233 int sqlite3_bind_text(
1100 sqlite3_stmt *pStmt, 1234 sqlite3_stmt *pStmt,
1101 int i, 1235 int i,
1102 const char *zData, 1236 const char *zData,
1103 int nData, 1237 int nData,
1104 void (*xDel)(void*) 1238 void (*xDel)(void*)
1105 ){ 1239 ){
1106 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 1240 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
1107 } 1241 }
1242 int sqlite3_bind_text64(
1243 sqlite3_stmt *pStmt,
1244 int i,
1245 const char *zData,
1246 sqlite3_uint64 nData,
1247 void (*xDel)(void*),
1248 unsigned char enc
1249 ){
1250 assert( xDel!=SQLITE_DYNAMIC );
1251 if( nData>0x7fffffff ){
1252 return invokeValueDestructor(zData, xDel, 0);
1253 }else{
1254 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
1255 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
1256 }
1257 }
1108 #ifndef SQLITE_OMIT_UTF16 1258 #ifndef SQLITE_OMIT_UTF16
1109 int sqlite3_bind_text16( 1259 int sqlite3_bind_text16(
1110 sqlite3_stmt *pStmt, 1260 sqlite3_stmt *pStmt,
1111 int i, 1261 int i,
1112 const void *zData, 1262 const void *zData,
1113 int nData, 1263 int nData,
1114 void (*xDel)(void*) 1264 void (*xDel)(void*)
1115 ){ 1265 ){
1116 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 1266 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
1117 } 1267 }
1118 #endif /* SQLITE_OMIT_UTF16 */ 1268 #endif /* SQLITE_OMIT_UTF16 */
1119 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 1269 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
1120 int rc; 1270 int rc;
1121 switch( pValue->type ){ 1271 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
1122 case SQLITE_INTEGER: { 1272 case SQLITE_INTEGER: {
1123 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 1273 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
1124 break; 1274 break;
1125 } 1275 }
1126 case SQLITE_FLOAT: { 1276 case SQLITE_FLOAT: {
1127 rc = sqlite3_bind_double(pStmt, i, pValue->r); 1277 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
1128 break; 1278 break;
1129 } 1279 }
1130 case SQLITE_BLOB: { 1280 case SQLITE_BLOB: {
1131 if( pValue->flags & MEM_Zero ){ 1281 if( pValue->flags & MEM_Zero ){
1132 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 1282 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
1133 }else{ 1283 }else{
1134 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 1284 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
1135 } 1285 }
1136 break; 1286 break;
1137 } 1287 }
(...skipping 23 matching lines...) Expand all
1161 /* 1311 /*
1162 ** Return the number of wildcards that can be potentially bound to. 1312 ** Return the number of wildcards that can be potentially bound to.
1163 ** This routine is added to support DBD::SQLite. 1313 ** This routine is added to support DBD::SQLite.
1164 */ 1314 */
1165 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 1315 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
1166 Vdbe *p = (Vdbe*)pStmt; 1316 Vdbe *p = (Vdbe*)pStmt;
1167 return p ? p->nVar : 0; 1317 return p ? p->nVar : 0;
1168 } 1318 }
1169 1319
1170 /* 1320 /*
1171 ** Create a mapping from variable numbers to variable names
1172 ** in the Vdbe.azVar[] array, if such a mapping does not already
1173 ** exist.
1174 */
1175 static void createVarMap(Vdbe *p){
1176 if( !p->okVar ){
1177 int j;
1178 Op *pOp;
1179 sqlite3_mutex_enter(p->db->mutex);
1180 /* The race condition here is harmless. If two threads call this
1181 ** routine on the same Vdbe at the same time, they both might end
1182 ** up initializing the Vdbe.azVar[] array. That is a little extra
1183 ** work but it results in the same answer.
1184 */
1185 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
1186 if( pOp->opcode==OP_Variable ){
1187 assert( pOp->p1>0 && pOp->p1<=p->nVar );
1188 p->azVar[pOp->p1-1] = pOp->p4.z;
1189 }
1190 }
1191 p->okVar = 1;
1192 sqlite3_mutex_leave(p->db->mutex);
1193 }
1194 }
1195
1196 /*
1197 ** Return the name of a wildcard parameter. Return NULL if the index 1321 ** Return the name of a wildcard parameter. Return NULL if the index
1198 ** is out of range or if the wildcard is unnamed. 1322 ** is out of range or if the wildcard is unnamed.
1199 ** 1323 **
1200 ** The result is always UTF-8. 1324 ** The result is always UTF-8.
1201 */ 1325 */
1202 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1326 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1203 Vdbe *p = (Vdbe*)pStmt; 1327 Vdbe *p = (Vdbe*)pStmt;
1204 if( p==0 || i<1 || i>p->nVar ){ 1328 if( p==0 || i<1 || i>p->nzVar ){
1205 return 0; 1329 return 0;
1206 } 1330 }
1207 createVarMap(p);
1208 return p->azVar[i-1]; 1331 return p->azVar[i-1];
1209 } 1332 }
1210 1333
1211 /* 1334 /*
1212 ** Given a wildcard parameter name, return the index of the variable 1335 ** Given a wildcard parameter name, return the index of the variable
1213 ** with that name. If there is no variable with the given name, 1336 ** with that name. If there is no variable with the given name,
1214 ** return 0. 1337 ** return 0.
1215 */ 1338 */
1216 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 1339 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
1217 int i; 1340 int i;
1218 if( p==0 ){ 1341 if( p==0 ){
1219 return 0; 1342 return 0;
1220 } 1343 }
1221 createVarMap(p);
1222 if( zName ){ 1344 if( zName ){
1223 for(i=0; i<p->nVar; i++){ 1345 for(i=0; i<p->nzVar; i++){
1224 const char *z = p->azVar[i]; 1346 const char *z = p->azVar[i];
1225 if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){ 1347 if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
1226 return i+1; 1348 return i+1;
1227 } 1349 }
1228 } 1350 }
1229 } 1351 }
1230 return 0; 1352 return 0;
1231 } 1353 }
1232 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 1354 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1233 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 1355 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
(...skipping 14 matching lines...) Expand all
1248 } 1370 }
1249 sqlite3_mutex_leave(pTo->db->mutex); 1371 sqlite3_mutex_leave(pTo->db->mutex);
1250 return SQLITE_OK; 1372 return SQLITE_OK;
1251 } 1373 }
1252 1374
1253 #ifndef SQLITE_OMIT_DEPRECATED 1375 #ifndef SQLITE_OMIT_DEPRECATED
1254 /* 1376 /*
1255 ** Deprecated external interface. Internal/core SQLite code 1377 ** Deprecated external interface. Internal/core SQLite code
1256 ** should call sqlite3TransferBindings. 1378 ** should call sqlite3TransferBindings.
1257 ** 1379 **
1258 ** Is is misuse to call this routine with statements from different 1380 ** It is misuse to call this routine with statements from different
1259 ** database connections. But as this is a deprecated interface, we 1381 ** database connections. But as this is a deprecated interface, we
1260 ** will not bother to check for that condition. 1382 ** will not bother to check for that condition.
1261 ** 1383 **
1262 ** If the two statements contain a different number of bindings, then 1384 ** If the two statements contain a different number of bindings, then
1263 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 1385 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
1264 ** SQLITE_OK is returned. 1386 ** SQLITE_OK is returned.
1265 */ 1387 */
1266 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1388 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1267 Vdbe *pFrom = (Vdbe*)pFromStmt; 1389 Vdbe *pFrom = (Vdbe*)pFromStmt;
1268 Vdbe *pTo = (Vdbe*)pToStmt; 1390 Vdbe *pTo = (Vdbe*)pToStmt;
(...skipping 22 matching lines...) Expand all
1291 1413
1292 /* 1414 /*
1293 ** Return true if the prepared statement is guaranteed to not modify the 1415 ** Return true if the prepared statement is guaranteed to not modify the
1294 ** database. 1416 ** database.
1295 */ 1417 */
1296 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 1418 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1297 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 1419 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1298 } 1420 }
1299 1421
1300 /* 1422 /*
1423 ** Return true if the prepared statement is in need of being reset.
1424 */
1425 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
1426 Vdbe *v = (Vdbe*)pStmt;
1427 return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN;
1428 }
1429
1430 /*
1301 ** Return a pointer to the next prepared statement after pStmt associated 1431 ** Return a pointer to the next prepared statement after pStmt associated
1302 ** with database connection pDb. If pStmt is NULL, return the first 1432 ** with database connection pDb. If pStmt is NULL, return the first
1303 ** prepared statement for the database connection. Return NULL if there 1433 ** prepared statement for the database connection. Return NULL if there
1304 ** are no more. 1434 ** are no more.
1305 */ 1435 */
1306 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1436 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1307 sqlite3_stmt *pNext; 1437 sqlite3_stmt *pNext;
1308 sqlite3_mutex_enter(pDb->mutex); 1438 sqlite3_mutex_enter(pDb->mutex);
1309 if( pStmt==0 ){ 1439 if( pStmt==0 ){
1310 pNext = (sqlite3_stmt*)pDb->pVdbe; 1440 pNext = (sqlite3_stmt*)pDb->pVdbe;
1311 }else{ 1441 }else{
1312 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1442 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1313 } 1443 }
1314 sqlite3_mutex_leave(pDb->mutex); 1444 sqlite3_mutex_leave(pDb->mutex);
1315 return pNext; 1445 return pNext;
1316 } 1446 }
1317 1447
1318 /* 1448 /*
1319 ** Return the value of a status counter for a prepared statement 1449 ** Return the value of a status counter for a prepared statement
1320 */ 1450 */
1321 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1451 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1322 Vdbe *pVdbe = (Vdbe*)pStmt; 1452 Vdbe *pVdbe = (Vdbe*)pStmt;
1323 int v = pVdbe->aCounter[op-1]; 1453 u32 v = pVdbe->aCounter[op];
1324 if( resetFlag ) pVdbe->aCounter[op-1] = 0; 1454 if( resetFlag ) pVdbe->aCounter[op] = 0;
1325 return v; 1455 return (int)v;
1326 } 1456 }
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3080704/src/vdbeInt.h ('k') | third_party/sqlite/sqlite-src-3080704/src/vdbeaux.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698