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

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

Issue 1610543003: [sql] Import reference version of SQLite 3.10.2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 } 46 }
47 static int vdbeSafetyNotNull(Vdbe *p){ 47 static int vdbeSafetyNotNull(Vdbe *p){
48 if( p==0 ){ 48 if( p==0 ){
49 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); 49 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
50 return 1; 50 return 1;
51 }else{ 51 }else{
52 return vdbeSafety(p); 52 return vdbeSafety(p);
53 } 53 }
54 } 54 }
55 55
56 #ifndef SQLITE_OMIT_TRACE
57 /*
58 ** Invoke the profile callback. This routine is only called if we already
59 ** know that the profile callback is defined and needs to be invoked.
60 */
61 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
62 sqlite3_int64 iNow;
63 assert( p->startTime>0 );
64 assert( db->xProfile!=0 );
65 assert( db->init.busy==0 );
66 assert( p->zSql!=0 );
67 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
68 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
69 p->startTime = 0;
70 }
71 /*
72 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
73 ** is needed, and it invokes the callback if it is needed.
74 */
75 # define checkProfileCallback(DB,P) \
76 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
77 #else
78 # define checkProfileCallback(DB,P) /*no-op*/
79 #endif
80
56 /* 81 /*
57 ** The following routine destroys a virtual machine that is created by 82 ** The following routine destroys a virtual machine that is created by
58 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 83 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
59 ** success/failure code that describes the result of executing the virtual 84 ** success/failure code that describes the result of executing the virtual
60 ** machine. 85 ** machine.
61 ** 86 **
62 ** This routine sets the error code and string returned by 87 ** This routine sets the error code and string returned by
63 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 88 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
64 */ 89 */
65 int sqlite3_finalize(sqlite3_stmt *pStmt){ 90 int sqlite3_finalize(sqlite3_stmt *pStmt){
66 int rc; 91 int rc;
67 if( pStmt==0 ){ 92 if( pStmt==0 ){
68 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 93 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
69 ** pointer is a harmless no-op. */ 94 ** pointer is a harmless no-op. */
70 rc = SQLITE_OK; 95 rc = SQLITE_OK;
71 }else{ 96 }else{
72 Vdbe *v = (Vdbe*)pStmt; 97 Vdbe *v = (Vdbe*)pStmt;
73 sqlite3 *db = v->db; 98 sqlite3 *db = v->db;
74 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 99 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
75 sqlite3_mutex_enter(db->mutex); 100 sqlite3_mutex_enter(db->mutex);
101 checkProfileCallback(db, v);
76 rc = sqlite3VdbeFinalize(v); 102 rc = sqlite3VdbeFinalize(v);
77 rc = sqlite3ApiExit(db, rc); 103 rc = sqlite3ApiExit(db, rc);
78 sqlite3LeaveMutexAndCloseZombie(db); 104 sqlite3LeaveMutexAndCloseZombie(db);
79 } 105 }
80 return rc; 106 return rc;
81 } 107 }
82 108
83 /* 109 /*
84 ** Terminate the current execution of an SQL statement and reset it 110 ** Terminate the current execution of an SQL statement and reset it
85 ** back to its starting state so that it can be reused. A success code from 111 ** back to its starting state so that it can be reused. A success code from
86 ** the prior execution is returned. 112 ** the prior execution is returned.
87 ** 113 **
88 ** This routine sets the error code and string returned by 114 ** This routine sets the error code and string returned by
89 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 115 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
90 */ 116 */
91 int sqlite3_reset(sqlite3_stmt *pStmt){ 117 int sqlite3_reset(sqlite3_stmt *pStmt){
92 int rc; 118 int rc;
93 if( pStmt==0 ){ 119 if( pStmt==0 ){
94 rc = SQLITE_OK; 120 rc = SQLITE_OK;
95 }else{ 121 }else{
96 Vdbe *v = (Vdbe*)pStmt; 122 Vdbe *v = (Vdbe*)pStmt;
97 sqlite3_mutex_enter(v->db->mutex); 123 sqlite3 *db = v->db;
124 sqlite3_mutex_enter(db->mutex);
125 checkProfileCallback(db, v);
98 rc = sqlite3VdbeReset(v); 126 rc = sqlite3VdbeReset(v);
99 sqlite3VdbeRewind(v); 127 sqlite3VdbeRewind(v);
100 assert( (rc & (v->db->errMask))==rc ); 128 assert( (rc & (db->errMask))==rc );
101 rc = sqlite3ApiExit(v->db, rc); 129 rc = sqlite3ApiExit(db, rc);
102 sqlite3_mutex_leave(v->db->mutex); 130 sqlite3_mutex_leave(db->mutex);
103 } 131 }
104 return rc; 132 return rc;
105 } 133 }
106 134
107 /* 135 /*
108 ** Set all the parameters in the compiled SQL statement to NULL. 136 ** Set all the parameters in the compiled SQL statement to NULL.
109 */ 137 */
110 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 138 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
111 int i; 139 int i;
112 int rc = SQLITE_OK; 140 int rc = SQLITE_OK;
(...skipping 14 matching lines...) Expand all
127 } 155 }
128 156
129 157
130 /**************************** sqlite3_value_ ******************************* 158 /**************************** sqlite3_value_ *******************************
131 ** The following routines extract information from a Mem or sqlite3_value 159 ** The following routines extract information from a Mem or sqlite3_value
132 ** structure. 160 ** structure.
133 */ 161 */
134 const void *sqlite3_value_blob(sqlite3_value *pVal){ 162 const void *sqlite3_value_blob(sqlite3_value *pVal){
135 Mem *p = (Mem*)pVal; 163 Mem *p = (Mem*)pVal;
136 if( p->flags & (MEM_Blob|MEM_Str) ){ 164 if( p->flags & (MEM_Blob|MEM_Str) ){
137 sqlite3VdbeMemExpandBlob(p); 165 if( sqlite3VdbeMemExpandBlob(p)!=SQLITE_OK ){
166 assert( p->flags==MEM_Null && p->z==0 );
167 return 0;
168 }
138 p->flags |= MEM_Blob; 169 p->flags |= MEM_Blob;
139 return p->n ? p->z : 0; 170 return p->n ? p->z : 0;
140 }else{ 171 }else{
141 return sqlite3_value_text(pVal); 172 return sqlite3_value_text(pVal);
142 } 173 }
143 } 174 }
144 int sqlite3_value_bytes(sqlite3_value *pVal){ 175 int sqlite3_value_bytes(sqlite3_value *pVal){
145 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 176 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
146 } 177 }
147 int sqlite3_value_bytes16(sqlite3_value *pVal){ 178 int sqlite3_value_bytes16(sqlite3_value *pVal){
148 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 179 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
149 } 180 }
150 double sqlite3_value_double(sqlite3_value *pVal){ 181 double sqlite3_value_double(sqlite3_value *pVal){
151 return sqlite3VdbeRealValue((Mem*)pVal); 182 return sqlite3VdbeRealValue((Mem*)pVal);
152 } 183 }
153 int sqlite3_value_int(sqlite3_value *pVal){ 184 int sqlite3_value_int(sqlite3_value *pVal){
154 return (int)sqlite3VdbeIntValue((Mem*)pVal); 185 return (int)sqlite3VdbeIntValue((Mem*)pVal);
155 } 186 }
156 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 187 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
157 return sqlite3VdbeIntValue((Mem*)pVal); 188 return sqlite3VdbeIntValue((Mem*)pVal);
158 } 189 }
190 unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
191 return ((Mem*)pVal)->eSubtype;
192 }
159 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 193 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
160 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 194 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
161 } 195 }
162 #ifndef SQLITE_OMIT_UTF16 196 #ifndef SQLITE_OMIT_UTF16
163 const void *sqlite3_value_text16(sqlite3_value* pVal){ 197 const void *sqlite3_value_text16(sqlite3_value* pVal){
164 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 198 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
165 } 199 }
166 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 200 const void *sqlite3_value_text16be(sqlite3_value *pVal){
167 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 201 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
168 } 202 }
169 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 203 const void *sqlite3_value_text16le(sqlite3_value *pVal){
170 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 204 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
171 } 205 }
172 #endif /* SQLITE_OMIT_UTF16 */ 206 #endif /* SQLITE_OMIT_UTF16 */
207 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
208 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
209 ** point number string BLOB NULL
210 */
173 int sqlite3_value_type(sqlite3_value* pVal){ 211 int sqlite3_value_type(sqlite3_value* pVal){
174 static const u8 aType[] = { 212 static const u8 aType[] = {
175 SQLITE_BLOB, /* 0x00 */ 213 SQLITE_BLOB, /* 0x00 */
176 SQLITE_NULL, /* 0x01 */ 214 SQLITE_NULL, /* 0x01 */
177 SQLITE_TEXT, /* 0x02 */ 215 SQLITE_TEXT, /* 0x02 */
178 SQLITE_NULL, /* 0x03 */ 216 SQLITE_NULL, /* 0x03 */
179 SQLITE_INTEGER, /* 0x04 */ 217 SQLITE_INTEGER, /* 0x04 */
180 SQLITE_NULL, /* 0x05 */ 218 SQLITE_NULL, /* 0x05 */
181 SQLITE_INTEGER, /* 0x06 */ 219 SQLITE_INTEGER, /* 0x06 */
182 SQLITE_NULL, /* 0x07 */ 220 SQLITE_NULL, /* 0x07 */
(...skipping 18 matching lines...) Expand all
201 SQLITE_FLOAT, /* 0x1a */ 239 SQLITE_FLOAT, /* 0x1a */
202 SQLITE_NULL, /* 0x1b */ 240 SQLITE_NULL, /* 0x1b */
203 SQLITE_INTEGER, /* 0x1c */ 241 SQLITE_INTEGER, /* 0x1c */
204 SQLITE_NULL, /* 0x1d */ 242 SQLITE_NULL, /* 0x1d */
205 SQLITE_INTEGER, /* 0x1e */ 243 SQLITE_INTEGER, /* 0x1e */
206 SQLITE_NULL, /* 0x1f */ 244 SQLITE_NULL, /* 0x1f */
207 }; 245 };
208 return aType[pVal->flags&MEM_AffMask]; 246 return aType[pVal->flags&MEM_AffMask];
209 } 247 }
210 248
249 /* Make a copy of an sqlite3_value object
250 */
251 sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
252 sqlite3_value *pNew;
253 if( pOrig==0 ) return 0;
254 pNew = sqlite3_malloc( sizeof(*pNew) );
255 if( pNew==0 ) return 0;
256 memset(pNew, 0, sizeof(*pNew));
257 memcpy(pNew, pOrig, MEMCELLSIZE);
258 pNew->flags &= ~MEM_Dyn;
259 pNew->db = 0;
260 if( pNew->flags&(MEM_Str|MEM_Blob) ){
261 pNew->flags &= ~(MEM_Static|MEM_Dyn);
262 pNew->flags |= MEM_Ephem;
263 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
264 sqlite3ValueFree(pNew);
265 pNew = 0;
266 }
267 }
268 return pNew;
269 }
270
271 /* Destroy an sqlite3_value object previously obtained from
272 ** sqlite3_value_dup().
273 */
274 void sqlite3_value_free(sqlite3_value *pOld){
275 sqlite3ValueFree(pOld);
276 }
277
278
211 /**************************** sqlite3_result_ ******************************* 279 /**************************** sqlite3_result_ *******************************
212 ** The following routines are used by user-defined functions to specify 280 ** The following routines are used by user-defined functions to specify
213 ** the function result. 281 ** the function result.
214 ** 282 **
215 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the 283 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
216 ** result as a string or blob but if the string or blob is too large, it 284 ** result as a string or blob but if the string or blob is too large, it
217 ** then sets the error code to SQLITE_TOOBIG 285 ** then sets the error code to SQLITE_TOOBIG
218 ** 286 **
219 ** The invokeValueDestructor(P,X) routine invokes destructor function X() 287 ** The invokeValueDestructor(P,X) routine invokes destructor function X()
220 ** on value P is not going to be used and need to be destroyed. 288 ** on value P is not going to be used and need to be destroyed.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); 361 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
294 } 362 }
295 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 363 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
296 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 364 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
297 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 365 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
298 } 366 }
299 void sqlite3_result_null(sqlite3_context *pCtx){ 367 void sqlite3_result_null(sqlite3_context *pCtx){
300 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 368 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
301 sqlite3VdbeMemSetNull(pCtx->pOut); 369 sqlite3VdbeMemSetNull(pCtx->pOut);
302 } 370 }
371 void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
372 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
373 pCtx->pOut->eSubtype = eSubtype & 0xff;
374 }
303 void sqlite3_result_text( 375 void sqlite3_result_text(
304 sqlite3_context *pCtx, 376 sqlite3_context *pCtx,
305 const char *z, 377 const char *z,
306 int n, 378 int n,
307 void (*xDel)(void *) 379 void (*xDel)(void *)
308 ){ 380 ){
309 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 381 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
310 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 382 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
311 } 383 }
312 void sqlite3_result_text64( 384 void sqlite3_result_text64(
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 } 427 }
356 #endif /* SQLITE_OMIT_UTF16 */ 428 #endif /* SQLITE_OMIT_UTF16 */
357 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 429 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
358 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 430 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
359 sqlite3VdbeMemCopy(pCtx->pOut, pValue); 431 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
360 } 432 }
361 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 433 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
362 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 434 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
363 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); 435 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
364 } 436 }
437 int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
438 Mem *pOut = pCtx->pOut;
439 assert( sqlite3_mutex_held(pOut->db->mutex) );
440 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
441 return SQLITE_TOOBIG;
442 }
443 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
444 return SQLITE_OK;
445 }
365 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 446 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
366 pCtx->isError = errCode; 447 pCtx->isError = errCode;
367 pCtx->fErrorOrAux = 1; 448 pCtx->fErrorOrAux = 1;
449 #ifdef SQLITE_DEBUG
450 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
451 #endif
368 if( pCtx->pOut->flags & MEM_Null ){ 452 if( pCtx->pOut->flags & MEM_Null ){
369 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 453 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
370 SQLITE_UTF8, SQLITE_STATIC); 454 SQLITE_UTF8, SQLITE_STATIC);
371 } 455 }
372 } 456 }
373 457
374 /* Force an SQLITE_TOOBIG error. */ 458 /* Force an SQLITE_TOOBIG error. */
375 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 459 void sqlite3_result_error_toobig(sqlite3_context *pCtx){
376 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 460 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
377 pCtx->isError = SQLITE_TOOBIG; 461 pCtx->isError = SQLITE_TOOBIG;
(...skipping 15 matching lines...) Expand all
393 ** This function is called after a transaction has been committed. It 477 ** This function is called after a transaction has been committed. It
394 ** invokes callbacks registered with sqlite3_wal_hook() as required. 478 ** invokes callbacks registered with sqlite3_wal_hook() as required.
395 */ 479 */
396 static int doWalCallbacks(sqlite3 *db){ 480 static int doWalCallbacks(sqlite3 *db){
397 int rc = SQLITE_OK; 481 int rc = SQLITE_OK;
398 #ifndef SQLITE_OMIT_WAL 482 #ifndef SQLITE_OMIT_WAL
399 int i; 483 int i;
400 for(i=0; i<db->nDb; i++){ 484 for(i=0; i<db->nDb; i++){
401 Btree *pBt = db->aDb[i].pBt; 485 Btree *pBt = db->aDb[i].pBt;
402 if( pBt ){ 486 if( pBt ){
403 int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 487 int nEntry;
488 sqlite3BtreeEnter(pBt);
489 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
490 sqlite3BtreeLeave(pBt);
404 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ 491 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
405 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry); 492 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
406 } 493 }
407 } 494 }
408 } 495 }
409 #endif 496 #endif
410 return rc; 497 return rc;
411 } 498 }
412 499
500
413 /* 501 /*
414 ** Execute the statement pStmt, either until a row of data is ready, the 502 ** Execute the statement pStmt, either until a row of data is ready, the
415 ** statement is completely executed or an error occurs. 503 ** statement is completely executed or an error occurs.
416 ** 504 **
417 ** This routine implements the bulk of the logic behind the sqlite_step() 505 ** This routine implements the bulk of the logic behind the sqlite_step()
418 ** API. The only thing omitted is the automatic recompile if a 506 ** API. The only thing omitted is the automatic recompile if a
419 ** schema change has occurred. That detail is handled by the 507 ** schema change has occurred. That detail is handled by the
420 ** outer sqlite3_step() wrapper procedure. 508 ** outer sqlite3_step() wrapper procedure.
421 */ 509 */
422 static int sqlite3Step(Vdbe *p){ 510 static int sqlite3Step(Vdbe *p){
(...skipping 12 matching lines...) Expand all
435 ** 523 **
436 ** Nevertheless, some published applications that were originally written 524 ** Nevertheless, some published applications that were originally written
437 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 525 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
438 ** returns, and those were broken by the automatic-reset change. As a 526 ** returns, and those were broken by the automatic-reset change. As a
439 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 527 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
440 ** legacy behavior of returning SQLITE_MISUSE for cases where the 528 ** legacy behavior of returning SQLITE_MISUSE for cases where the
441 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 529 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
442 ** or SQLITE_BUSY error. 530 ** or SQLITE_BUSY error.
443 */ 531 */
444 #ifdef SQLITE_OMIT_AUTORESET 532 #ifdef SQLITE_OMIT_AUTORESET
445 if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){ 533 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
446 sqlite3_reset((sqlite3_stmt*)p); 534 sqlite3_reset((sqlite3_stmt*)p);
447 }else{ 535 }else{
448 return SQLITE_MISUSE_BKPT; 536 return SQLITE_MISUSE_BKPT;
449 } 537 }
450 #else 538 #else
451 sqlite3_reset((sqlite3_stmt*)p); 539 sqlite3_reset((sqlite3_stmt*)p);
452 #endif 540 #endif
453 } 541 }
454 542
455 /* Check that malloc() has not failed. If it has, return early. */ 543 /* Check that malloc() has not failed. If it has, return early. */
(...skipping 15 matching lines...) Expand all
471 */ 559 */
472 if( db->nVdbeActive==0 ){ 560 if( db->nVdbeActive==0 ){
473 db->u1.isInterrupted = 0; 561 db->u1.isInterrupted = 0;
474 } 562 }
475 563
476 assert( db->nVdbeWrite>0 || db->autoCommit==0 564 assert( db->nVdbeWrite>0 || db->autoCommit==0
477 || (db->nDeferredCons==0 && db->nDeferredImmCons==0) 565 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
478 ); 566 );
479 567
480 #ifndef SQLITE_OMIT_TRACE 568 #ifndef SQLITE_OMIT_TRACE
481 if( db->xProfile && !db->init.busy ){ 569 if( db->xProfile && !db->init.busy && p->zSql ){
482 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 570 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
571 }else{
572 assert( p->startTime==0 );
483 } 573 }
484 #endif 574 #endif
485 575
486 db->nVdbeActive++; 576 db->nVdbeActive++;
487 if( p->readOnly==0 ) db->nVdbeWrite++; 577 if( p->readOnly==0 ) db->nVdbeWrite++;
488 if( p->bIsReader ) db->nVdbeRead++; 578 if( p->bIsReader ) db->nVdbeRead++;
489 p->pc = 0; 579 p->pc = 0;
490 } 580 }
581 #ifdef SQLITE_DEBUG
582 p->rcApp = SQLITE_OK;
583 #endif
491 #ifndef SQLITE_OMIT_EXPLAIN 584 #ifndef SQLITE_OMIT_EXPLAIN
492 if( p->explain ){ 585 if( p->explain ){
493 rc = sqlite3VdbeList(p); 586 rc = sqlite3VdbeList(p);
494 }else 587 }else
495 #endif /* SQLITE_OMIT_EXPLAIN */ 588 #endif /* SQLITE_OMIT_EXPLAIN */
496 { 589 {
497 db->nVdbeExec++; 590 db->nVdbeExec++;
498 rc = sqlite3VdbeExec(p); 591 rc = sqlite3VdbeExec(p);
499 db->nVdbeExec--; 592 db->nVdbeExec--;
500 } 593 }
501 594
502 #ifndef SQLITE_OMIT_TRACE 595 #ifndef SQLITE_OMIT_TRACE
503 /* Invoke the profile callback if there is one 596 /* If the statement completed successfully, invoke the profile callback */
504 */ 597 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
505 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
506 sqlite3_int64 iNow;
507 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
508 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
509 }
510 #endif 598 #endif
511 599
512 if( rc==SQLITE_DONE ){ 600 if( rc==SQLITE_DONE ){
513 assert( p->rc==SQLITE_OK ); 601 assert( p->rc==SQLITE_OK );
514 p->rc = doWalCallbacks(db); 602 p->rc = doWalCallbacks(db);
515 if( p->rc!=SQLITE_OK ){ 603 if( p->rc!=SQLITE_OK ){
516 rc = SQLITE_ERROR; 604 rc = SQLITE_ERROR;
517 } 605 }
518 } 606 }
519 607
520 db->errCode = rc; 608 db->errCode = rc;
521 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 609 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
522 p->rc = SQLITE_NOMEM; 610 p->rc = SQLITE_NOMEM;
523 } 611 }
524 end_of_step: 612 end_of_step:
525 /* At this point local variable rc holds the value that should be 613 /* At this point local variable rc holds the value that should be
526 ** returned if this statement was compiled using the legacy 614 ** returned if this statement was compiled using the legacy
527 ** sqlite3_prepare() interface. According to the docs, this can only 615 ** sqlite3_prepare() interface. According to the docs, this can only
528 ** be one of the values in the first assert() below. Variable p->rc 616 ** be one of the values in the first assert() below. Variable p->rc
529 ** contains the value that would be returned if sqlite3_finalize() 617 ** contains the value that would be returned if sqlite3_finalize()
530 ** were called on statement p. 618 ** were called on statement p.
531 */ 619 */
532 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 620 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
533 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE 621 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
534 ); 622 );
535 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE ); 623 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
536 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 624 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
537 /* If this statement was prepared using sqlite3_prepare_v2(), and an 625 /* If this statement was prepared using sqlite3_prepare_v2(), and an
538 ** error has occurred, then return the error code in p->rc to the 626 ** error has occurred, then return the error code in p->rc to the
539 ** caller. Set the error code in the database handle to the same value. 627 ** caller. Set the error code in the database handle to the same value.
540 */ 628 */
541 rc = sqlite3VdbeTransferError(p); 629 rc = sqlite3VdbeTransferError(p);
542 } 630 }
543 return (rc&db->errMask); 631 return (rc&db->errMask);
544 } 632 }
545 633
(...skipping 27 matching lines...) Expand all
573 if( rc2!=SQLITE_OK ){ 661 if( rc2!=SQLITE_OK ){
574 /* This case occurs after failing to recompile an sql statement. 662 /* This case occurs after failing to recompile an sql statement.
575 ** The error message from the SQL compiler has already been loaded 663 ** The error message from the SQL compiler has already been loaded
576 ** into the database handle. This block copies the error message 664 ** into the database handle. This block copies the error message
577 ** from the database handle into the statement and sets the statement 665 ** from the database handle into the statement and sets the statement
578 ** program counter to 0 to ensure that when the statement is 666 ** program counter to 0 to ensure that when the statement is
579 ** finalized or reset the parser error message is available via 667 ** finalized or reset the parser error message is available via
580 ** sqlite3_errmsg() and sqlite3_errcode(). 668 ** sqlite3_errmsg() and sqlite3_errcode().
581 */ 669 */
582 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 670 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
583 assert( zErr!=0 || db->mallocFailed );
584 sqlite3DbFree(db, v->zErrMsg); 671 sqlite3DbFree(db, v->zErrMsg);
585 if( !db->mallocFailed ){ 672 if( !db->mallocFailed ){
586 v->zErrMsg = sqlite3DbStrDup(db, zErr); 673 v->zErrMsg = sqlite3DbStrDup(db, zErr);
587 v->rc = rc2; 674 v->rc = rc2;
588 } else { 675 } else {
589 v->zErrMsg = 0; 676 v->zErrMsg = 0;
590 v->rc = rc = SQLITE_NOMEM; 677 v->rc = rc = SQLITE_NOMEM;
591 } 678 }
592 } 679 }
593 rc = sqlite3ApiExit(db, rc); 680 rc = sqlite3ApiExit(db, rc);
(...skipping 15 matching lines...) Expand all
609 ** Extract the user data from a sqlite3_context structure and return a 696 ** Extract the user data from a sqlite3_context structure and return a
610 ** pointer to it. 697 ** pointer to it.
611 ** 698 **
612 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 699 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
613 ** returns a copy of the pointer to the database connection (the 1st 700 ** returns a copy of the pointer to the database connection (the 1st
614 ** parameter) of the sqlite3_create_function() and 701 ** parameter) of the sqlite3_create_function() and
615 ** sqlite3_create_function16() routines that originally registered the 702 ** sqlite3_create_function16() routines that originally registered the
616 ** application defined function. 703 ** application defined function.
617 */ 704 */
618 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 705 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
619 assert( p && p->pFunc ); 706 assert( p && p->pOut );
620 return p->pOut->db; 707 return p->pOut->db;
621 } 708 }
622 709
623 /* 710 /*
624 ** Return the current time for a statement 711 ** Return the current time for a statement. If the current time
712 ** is requested more than once within the same run of a single prepared
713 ** statement, the exact same time is returned for each invocation regardless
714 ** of the amount of time that elapses between invocations. In other words,
715 ** the time returned is always the time of the first call.
625 */ 716 */
626 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ 717 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
627 Vdbe *v = p->pVdbe;
628 int rc; 718 int rc;
629 if( v->iCurrentTime==0 ){ 719 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
630 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, &v->iCurrentTime); 720 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
631 if( rc ) v->iCurrentTime = 0; 721 assert( p->pVdbe!=0 );
722 #else
723 sqlite3_int64 iTime = 0;
724 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
725 #endif
726 if( *piTime==0 ){
727 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
728 if( rc ) *piTime = 0;
632 } 729 }
633 return v->iCurrentTime; 730 return *piTime;
634 } 731 }
635 732
636 /* 733 /*
637 ** The following is the implementation of an SQL function that always 734 ** The following is the implementation of an SQL function that always
638 ** fails with an error message stating that the function is used in the 735 ** fails with an error message stating that the function is used in the
639 ** wrong context. The sqlite3_overload_function() API might construct 736 ** wrong context. The sqlite3_overload_function() API might construct
640 ** SQL function that use this routine so that the functions will exist 737 ** SQL function that use this routine so that the functions will exist
641 ** for name resolution but are actually overloaded by the xFindFunction 738 ** for name resolution but are actually overloaded by the xFindFunction
642 ** method of virtual tables. 739 ** method of virtual tables.
643 */ 740 */
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 } 790 }
694 791
695 /* 792 /*
696 ** Return the auxiliary data pointer, if any, for the iArg'th argument to 793 ** Return the auxiliary data pointer, if any, for the iArg'th argument to
697 ** the user-function defined by pCtx. 794 ** the user-function defined by pCtx.
698 */ 795 */
699 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 796 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
700 AuxData *pAuxData; 797 AuxData *pAuxData;
701 798
702 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 799 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
800 #if SQLITE_ENABLE_STAT3_OR_STAT4
801 if( pCtx->pVdbe==0 ) return 0;
802 #else
803 assert( pCtx->pVdbe!=0 );
804 #endif
703 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ 805 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
704 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; 806 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
705 } 807 }
706 808
707 return (pAuxData ? pAuxData->pAux : 0); 809 return (pAuxData ? pAuxData->pAux : 0);
708 } 810 }
709 811
710 /* 812 /*
711 ** Set the auxiliary data pointer and delete function, for the iArg'th 813 ** Set the auxiliary data pointer and delete function, for the iArg'th
712 ** argument to the user-function defined by pCtx. Any previous value is 814 ** argument to the user-function defined by pCtx. Any previous value is
713 ** deleted by calling the delete function specified when it was set. 815 ** deleted by calling the delete function specified when it was set.
714 */ 816 */
715 void sqlite3_set_auxdata( 817 void sqlite3_set_auxdata(
716 sqlite3_context *pCtx, 818 sqlite3_context *pCtx,
717 int iArg, 819 int iArg,
718 void *pAux, 820 void *pAux,
719 void (*xDelete)(void*) 821 void (*xDelete)(void*)
720 ){ 822 ){
721 AuxData *pAuxData; 823 AuxData *pAuxData;
722 Vdbe *pVdbe = pCtx->pVdbe; 824 Vdbe *pVdbe = pCtx->pVdbe;
723 825
724 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 826 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
725 if( iArg<0 ) goto failed; 827 if( iArg<0 ) goto failed;
828 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
829 if( pVdbe==0 ) goto failed;
830 #else
831 assert( pVdbe!=0 );
832 #endif
726 833
727 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ 834 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
728 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; 835 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
729 } 836 }
730 if( pAuxData==0 ){ 837 if( pAuxData==0 ){
731 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); 838 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
732 if( !pAuxData ) goto failed; 839 if( !pAuxData ) goto failed;
733 pAuxData->iOp = pCtx->iOp; 840 pAuxData->iOp = pCtx->iOp;
734 pAuxData->iArg = iArg; 841 pAuxData->iArg = iArg;
735 pAuxData->pNext = pVdbe->pAuxData; 842 pAuxData->pNext = pVdbe->pAuxData;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 ** that a Mem structure is located on an 8-byte boundary. To prevent 905 ** 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 906 ** 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 907 ** using gcc, we force nullMem to be 8-byte aligned using the magical
801 ** __attribute__((aligned(8))) macro. */ 908 ** __attribute__((aligned(8))) macro. */
802 static const Mem nullMem 909 static const Mem nullMem
803 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 910 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
804 __attribute__((aligned(8))) 911 __attribute__((aligned(8)))
805 #endif 912 #endif
806 = { 913 = {
807 /* .u = */ {0}, 914 /* .u = */ {0},
808 /* .flags = */ MEM_Null, 915 /* .flags = */ (u16)MEM_Null,
809 /* .enc = */ 0, 916 /* .enc = */ (u8)0,
810 /* .n = */ 0, 917 /* .eSubtype = */ (u8)0,
811 /* .z = */ 0, 918 /* .n = */ (int)0,
812 /* .zMalloc = */ 0, 919 /* .z = */ (char*)0,
813 /* .szMalloc = */ 0, 920 /* .zMalloc = */ (char*)0,
814 /* .iPadding1 = */ 0, 921 /* .szMalloc = */ (int)0,
815 /* .db = */ 0, 922 /* .uTemp = */ (u32)0,
816 /* .xDel = */ 0, 923 /* .db = */ (sqlite3*)0,
924 /* .xDel = */ (void(*)(void*))0,
817 #ifdef SQLITE_DEBUG 925 #ifdef SQLITE_DEBUG
818 /* .pScopyFrom = */ 0, 926 /* .pScopyFrom = */ (Mem*)0,
819 /* .pFiller = */ 0, 927 /* .pFiller = */ (void*)0,
820 #endif 928 #endif
821 }; 929 };
822 return &nullMem; 930 return &nullMem;
823 } 931 }
824 932
825 /* 933 /*
826 ** Check to see if column iCol of the given statement is valid. If 934 ** Check to see if column iCol of the given statement is valid. If
827 ** it is, return a pointer to the Mem for the value of that column. 935 ** it is, return a pointer to the Mem for the value of that column.
828 ** If iCol is not valid, return a pointer to a Mem which has a value 936 ** If iCol is not valid, return a pointer to a Mem which has a value
829 ** of NULL. 937 ** of NULL.
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 ** 1067 **
960 ** If the result is not a simple column reference (if it is an expression 1068 ** If the result is not a simple column reference (if it is an expression
961 ** or a constant) then useTypes 2, 3, and 4 return NULL. 1069 ** or a constant) then useTypes 2, 3, and 4 return NULL.
962 */ 1070 */
963 static const void *columnName( 1071 static const void *columnName(
964 sqlite3_stmt *pStmt, 1072 sqlite3_stmt *pStmt,
965 int N, 1073 int N,
966 const void *(*xFunc)(Mem*), 1074 const void *(*xFunc)(Mem*),
967 int useType 1075 int useType
968 ){ 1076 ){
969 const void *ret = 0; 1077 const void *ret;
970 Vdbe *p = (Vdbe *)pStmt; 1078 Vdbe *p;
971 int n; 1079 int n;
972 sqlite3 *db = p->db; 1080 sqlite3 *db;
973 1081 #ifdef SQLITE_ENABLE_API_ARMOR
1082 if( pStmt==0 ){
1083 (void)SQLITE_MISUSE_BKPT;
1084 return 0;
1085 }
1086 #endif
1087 ret = 0;
1088 p = (Vdbe *)pStmt;
1089 db = p->db;
974 assert( db!=0 ); 1090 assert( db!=0 );
975 n = sqlite3_column_count(pStmt); 1091 n = sqlite3_column_count(pStmt);
976 if( N<n && N>=0 ){ 1092 if( N<n && N>=0 ){
977 N += useType*n; 1093 N += useType*n;
978 sqlite3_mutex_enter(db->mutex); 1094 sqlite3_mutex_enter(db->mutex);
979 assert( db->mallocFailed==0 ); 1095 assert( db->mallocFailed==0 );
980 ret = xFunc(&p->aColName[N]); 1096 ret = xFunc(&p->aColName[N]);
981 /* A malloc may have failed inside of the xFunc() call. If this 1097 /* A malloc may have failed inside of the xFunc() call. If this
982 ** is the case, clear the mallocFailed flag and return NULL. 1098 ** is the case, clear the mallocFailed flag and return NULL.
983 */ 1099 */
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
1300 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 1416 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
1301 int rc; 1417 int rc;
1302 Vdbe *p = (Vdbe *)pStmt; 1418 Vdbe *p = (Vdbe *)pStmt;
1303 rc = vdbeUnbind(p, i); 1419 rc = vdbeUnbind(p, i);
1304 if( rc==SQLITE_OK ){ 1420 if( rc==SQLITE_OK ){
1305 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1421 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
1306 sqlite3_mutex_leave(p->db->mutex); 1422 sqlite3_mutex_leave(p->db->mutex);
1307 } 1423 }
1308 return rc; 1424 return rc;
1309 } 1425 }
1426 int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
1427 int rc;
1428 Vdbe *p = (Vdbe *)pStmt;
1429 sqlite3_mutex_enter(p->db->mutex);
1430 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
1431 rc = SQLITE_TOOBIG;
1432 }else{
1433 assert( (n & 0x7FFFFFFF)==n );
1434 rc = sqlite3_bind_zeroblob(pStmt, i, n);
1435 }
1436 rc = sqlite3ApiExit(p->db, rc);
1437 sqlite3_mutex_leave(p->db->mutex);
1438 return rc;
1439 }
1310 1440
1311 /* 1441 /*
1312 ** Return the number of wildcards that can be potentially bound to. 1442 ** Return the number of wildcards that can be potentially bound to.
1313 ** This routine is added to support DBD::SQLite. 1443 ** This routine is added to support DBD::SQLite.
1314 */ 1444 */
1315 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 1445 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
1316 Vdbe *p = (Vdbe*)pStmt; 1446 Vdbe *p = (Vdbe*)pStmt;
1317 return p ? p->nVar : 0; 1447 return p ? p->nVar : 0;
1318 } 1448 }
1319 1449
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1428 } 1558 }
1429 1559
1430 /* 1560 /*
1431 ** Return a pointer to the next prepared statement after pStmt associated 1561 ** Return a pointer to the next prepared statement after pStmt associated
1432 ** with database connection pDb. If pStmt is NULL, return the first 1562 ** with database connection pDb. If pStmt is NULL, return the first
1433 ** prepared statement for the database connection. Return NULL if there 1563 ** prepared statement for the database connection. Return NULL if there
1434 ** are no more. 1564 ** are no more.
1435 */ 1565 */
1436 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1566 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1437 sqlite3_stmt *pNext; 1567 sqlite3_stmt *pNext;
1568 #ifdef SQLITE_ENABLE_API_ARMOR
1569 if( !sqlite3SafetyCheckOk(pDb) ){
1570 (void)SQLITE_MISUSE_BKPT;
1571 return 0;
1572 }
1573 #endif
1438 sqlite3_mutex_enter(pDb->mutex); 1574 sqlite3_mutex_enter(pDb->mutex);
1439 if( pStmt==0 ){ 1575 if( pStmt==0 ){
1440 pNext = (sqlite3_stmt*)pDb->pVdbe; 1576 pNext = (sqlite3_stmt*)pDb->pVdbe;
1441 }else{ 1577 }else{
1442 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1578 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
1443 } 1579 }
1444 sqlite3_mutex_leave(pDb->mutex); 1580 sqlite3_mutex_leave(pDb->mutex);
1445 return pNext; 1581 return pNext;
1446 } 1582 }
1447 1583
1448 /* 1584 /*
1449 ** Return the value of a status counter for a prepared statement 1585 ** Return the value of a status counter for a prepared statement
1450 */ 1586 */
1451 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1587 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
1452 Vdbe *pVdbe = (Vdbe*)pStmt; 1588 Vdbe *pVdbe = (Vdbe*)pStmt;
1453 u32 v = pVdbe->aCounter[op]; 1589 u32 v;
1590 #ifdef SQLITE_ENABLE_API_ARMOR
1591 if( !pStmt ){
1592 (void)SQLITE_MISUSE_BKPT;
1593 return 0;
1594 }
1595 #endif
1596 v = pVdbe->aCounter[op];
1454 if( resetFlag ) pVdbe->aCounter[op] = 0; 1597 if( resetFlag ) pVdbe->aCounter[op] = 0;
1455 return (int)v; 1598 return (int)v;
1456 } 1599 }
1600
1601 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1602 /*
1603 ** Return status data for a single loop within query pStmt.
1604 */
1605 int sqlite3_stmt_scanstatus(
1606 sqlite3_stmt *pStmt, /* Prepared statement being queried */
1607 int idx, /* Index of loop to report on */
1608 int iScanStatusOp, /* Which metric to return */
1609 void *pOut /* OUT: Write the answer here */
1610 ){
1611 Vdbe *p = (Vdbe*)pStmt;
1612 ScanStatus *pScan;
1613 if( idx<0 || idx>=p->nScan ) return 1;
1614 pScan = &p->aScan[idx];
1615 switch( iScanStatusOp ){
1616 case SQLITE_SCANSTAT_NLOOP: {
1617 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
1618 break;
1619 }
1620 case SQLITE_SCANSTAT_NVISIT: {
1621 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
1622 break;
1623 }
1624 case SQLITE_SCANSTAT_EST: {
1625 double r = 1.0;
1626 LogEst x = pScan->nEst;
1627 while( x<100 ){
1628 x += 10;
1629 r *= 0.5;
1630 }
1631 *(double*)pOut = r*sqlite3LogEstToInt(x);
1632 break;
1633 }
1634 case SQLITE_SCANSTAT_NAME: {
1635 *(const char**)pOut = pScan->zName;
1636 break;
1637 }
1638 case SQLITE_SCANSTAT_EXPLAIN: {
1639 if( pScan->addrExplain ){
1640 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
1641 }else{
1642 *(const char**)pOut = 0;
1643 }
1644 break;
1645 }
1646 case SQLITE_SCANSTAT_SELECTID: {
1647 if( pScan->addrExplain ){
1648 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
1649 }else{
1650 *(int*)pOut = -1;
1651 }
1652 break;
1653 }
1654 default: {
1655 return 1;
1656 }
1657 }
1658 return 0;
1659 }
1660
1661 /*
1662 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
1663 */
1664 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
1665 Vdbe *p = (Vdbe*)pStmt;
1666 memset(p->anExec, 0, p->nOp * sizeof(i64));
1667 }
1668 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3100200/src/vdbeInt.h ('k') | third_party/sqlite/sqlite-src-3100200/src/vdbeaux.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698