OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ** 2004 May 26 |
| 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: |
| 6 ** |
| 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. |
| 10 ** |
| 11 ************************************************************************* |
| 12 ** |
| 13 ** This file contains code use to implement APIs that are part of the |
| 14 ** VDBE. |
| 15 */ |
| 16 #include "sqliteInt.h" |
| 17 #include "vdbeInt.h" |
| 18 |
| 19 #ifndef SQLITE_OMIT_DEPRECATED |
| 20 /* |
| 21 ** Return TRUE (non-zero) of the statement supplied as an argument needs |
| 22 ** to be recompiled. A statement needs to be recompiled whenever the |
| 23 ** execution environment changes in a way that would alter the program |
| 24 ** that sqlite3_prepare() generates. For example, if new functions or |
| 25 ** collating sequences are registered or if an authorizer function is |
| 26 ** added or changed. |
| 27 */ |
| 28 int sqlite3_expired(sqlite3_stmt *pStmt){ |
| 29 Vdbe *p = (Vdbe*)pStmt; |
| 30 return p==0 || p->expired; |
| 31 } |
| 32 #endif |
| 33 |
| 34 /* |
| 35 ** Check on a Vdbe to make sure it has not been finalized. Log |
| 36 ** an error and return true if it has been finalized (or is otherwise |
| 37 ** invalid). Return false if it is ok. |
| 38 */ |
| 39 static int vdbeSafety(Vdbe *p){ |
| 40 if( p->db==0 ){ |
| 41 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); |
| 42 return 1; |
| 43 }else{ |
| 44 return 0; |
| 45 } |
| 46 } |
| 47 static int vdbeSafetyNotNull(Vdbe *p){ |
| 48 if( p==0 ){ |
| 49 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); |
| 50 return 1; |
| 51 }else{ |
| 52 return vdbeSafety(p); |
| 53 } |
| 54 } |
| 55 |
| 56 /* |
| 57 ** The following routine destroys a virtual machine that is created by |
| 58 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ |
| 59 ** success/failure code that describes the result of executing the virtual |
| 60 ** machine. |
| 61 ** |
| 62 ** This routine sets the error code and string returned by |
| 63 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 64 */ |
| 65 int sqlite3_finalize(sqlite3_stmt *pStmt){ |
| 66 int rc; |
| 67 if( pStmt==0 ){ |
| 68 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL |
| 69 ** pointer is a harmless no-op. */ |
| 70 rc = SQLITE_OK; |
| 71 }else{ |
| 72 Vdbe *v = (Vdbe*)pStmt; |
| 73 sqlite3 *db = v->db; |
| 74 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; |
| 75 sqlite3_mutex_enter(db->mutex); |
| 76 rc = sqlite3VdbeFinalize(v); |
| 77 rc = sqlite3ApiExit(db, rc); |
| 78 sqlite3LeaveMutexAndCloseZombie(db); |
| 79 } |
| 80 return rc; |
| 81 } |
| 82 |
| 83 /* |
| 84 ** 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 |
| 86 ** the prior execution is returned. |
| 87 ** |
| 88 ** This routine sets the error code and string returned by |
| 89 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 90 */ |
| 91 int sqlite3_reset(sqlite3_stmt *pStmt){ |
| 92 int rc; |
| 93 if( pStmt==0 ){ |
| 94 rc = SQLITE_OK; |
| 95 }else{ |
| 96 Vdbe *v = (Vdbe*)pStmt; |
| 97 sqlite3_mutex_enter(v->db->mutex); |
| 98 rc = sqlite3VdbeReset(v); |
| 99 sqlite3VdbeRewind(v); |
| 100 assert( (rc & (v->db->errMask))==rc ); |
| 101 rc = sqlite3ApiExit(v->db, rc); |
| 102 sqlite3_mutex_leave(v->db->mutex); |
| 103 } |
| 104 return rc; |
| 105 } |
| 106 |
| 107 /* |
| 108 ** Set all the parameters in the compiled SQL statement to NULL. |
| 109 */ |
| 110 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ |
| 111 int i; |
| 112 int rc = SQLITE_OK; |
| 113 Vdbe *p = (Vdbe*)pStmt; |
| 114 #if SQLITE_THREADSAFE |
| 115 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; |
| 116 #endif |
| 117 sqlite3_mutex_enter(mutex); |
| 118 for(i=0; i<p->nVar; i++){ |
| 119 sqlite3VdbeMemRelease(&p->aVar[i]); |
| 120 p->aVar[i].flags = MEM_Null; |
| 121 } |
| 122 if( p->isPrepareV2 && p->expmask ){ |
| 123 p->expired = 1; |
| 124 } |
| 125 sqlite3_mutex_leave(mutex); |
| 126 return rc; |
| 127 } |
| 128 |
| 129 |
| 130 /**************************** sqlite3_value_ ******************************* |
| 131 ** The following routines extract information from a Mem or sqlite3_value |
| 132 ** structure. |
| 133 */ |
| 134 const void *sqlite3_value_blob(sqlite3_value *pVal){ |
| 135 Mem *p = (Mem*)pVal; |
| 136 if( p->flags & (MEM_Blob|MEM_Str) ){ |
| 137 sqlite3VdbeMemExpandBlob(p); |
| 138 p->flags |= MEM_Blob; |
| 139 return p->n ? p->z : 0; |
| 140 }else{ |
| 141 return sqlite3_value_text(pVal); |
| 142 } |
| 143 } |
| 144 int sqlite3_value_bytes(sqlite3_value *pVal){ |
| 145 return sqlite3ValueBytes(pVal, SQLITE_UTF8); |
| 146 } |
| 147 int sqlite3_value_bytes16(sqlite3_value *pVal){ |
| 148 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); |
| 149 } |
| 150 double sqlite3_value_double(sqlite3_value *pVal){ |
| 151 return sqlite3VdbeRealValue((Mem*)pVal); |
| 152 } |
| 153 int sqlite3_value_int(sqlite3_value *pVal){ |
| 154 return (int)sqlite3VdbeIntValue((Mem*)pVal); |
| 155 } |
| 156 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ |
| 157 return sqlite3VdbeIntValue((Mem*)pVal); |
| 158 } |
| 159 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ |
| 160 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); |
| 161 } |
| 162 #ifndef SQLITE_OMIT_UTF16 |
| 163 const void *sqlite3_value_text16(sqlite3_value* pVal){ |
| 164 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
| 165 } |
| 166 const void *sqlite3_value_text16be(sqlite3_value *pVal){ |
| 167 return sqlite3ValueText(pVal, SQLITE_UTF16BE); |
| 168 } |
| 169 const void *sqlite3_value_text16le(sqlite3_value *pVal){ |
| 170 return sqlite3ValueText(pVal, SQLITE_UTF16LE); |
| 171 } |
| 172 #endif /* SQLITE_OMIT_UTF16 */ |
| 173 int sqlite3_value_type(sqlite3_value* pVal){ |
| 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]; |
| 209 } |
| 210 |
| 211 /**************************** sqlite3_result_ ******************************* |
| 212 ** The following routines are used by user-defined functions to specify |
| 213 ** the function result. |
| 214 ** |
| 215 ** 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 |
| 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. |
| 221 */ |
| 222 static void setResultStrOrError( |
| 223 sqlite3_context *pCtx, /* Function context */ |
| 224 const char *z, /* String pointer */ |
| 225 int n, /* Bytes in string, or negative */ |
| 226 u8 enc, /* Encoding of z. 0 for BLOBs */ |
| 227 void (*xDel)(void*) /* Destructor function */ |
| 228 ){ |
| 229 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){ |
| 230 sqlite3_result_error_toobig(pCtx); |
| 231 } |
| 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 } |
| 249 void sqlite3_result_blob( |
| 250 sqlite3_context *pCtx, |
| 251 const void *z, |
| 252 int n, |
| 253 void (*xDel)(void *) |
| 254 ){ |
| 255 assert( n>=0 ); |
| 256 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 257 setResultStrOrError(pCtx, z, n, 0, xDel); |
| 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 } |
| 273 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ |
| 274 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 275 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); |
| 276 } |
| 277 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ |
| 278 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 279 pCtx->isError = SQLITE_ERROR; |
| 280 pCtx->fErrorOrAux = 1; |
| 281 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); |
| 282 } |
| 283 #ifndef SQLITE_OMIT_UTF16 |
| 284 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ |
| 285 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 286 pCtx->isError = SQLITE_ERROR; |
| 287 pCtx->fErrorOrAux = 1; |
| 288 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); |
| 289 } |
| 290 #endif |
| 291 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ |
| 292 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 293 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); |
| 294 } |
| 295 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ |
| 296 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 297 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); |
| 298 } |
| 299 void sqlite3_result_null(sqlite3_context *pCtx){ |
| 300 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 301 sqlite3VdbeMemSetNull(pCtx->pOut); |
| 302 } |
| 303 void sqlite3_result_text( |
| 304 sqlite3_context *pCtx, |
| 305 const char *z, |
| 306 int n, |
| 307 void (*xDel)(void *) |
| 308 ){ |
| 309 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 310 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); |
| 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 } |
| 328 #ifndef SQLITE_OMIT_UTF16 |
| 329 void sqlite3_result_text16( |
| 330 sqlite3_context *pCtx, |
| 331 const void *z, |
| 332 int n, |
| 333 void (*xDel)(void *) |
| 334 ){ |
| 335 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 336 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); |
| 337 } |
| 338 void sqlite3_result_text16be( |
| 339 sqlite3_context *pCtx, |
| 340 const void *z, |
| 341 int n, |
| 342 void (*xDel)(void *) |
| 343 ){ |
| 344 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 345 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); |
| 346 } |
| 347 void sqlite3_result_text16le( |
| 348 sqlite3_context *pCtx, |
| 349 const void *z, |
| 350 int n, |
| 351 void (*xDel)(void *) |
| 352 ){ |
| 353 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 354 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); |
| 355 } |
| 356 #endif /* SQLITE_OMIT_UTF16 */ |
| 357 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ |
| 358 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 359 sqlite3VdbeMemCopy(pCtx->pOut, pValue); |
| 360 } |
| 361 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ |
| 362 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 363 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); |
| 364 } |
| 365 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ |
| 366 pCtx->isError = errCode; |
| 367 pCtx->fErrorOrAux = 1; |
| 368 if( pCtx->pOut->flags & MEM_Null ){ |
| 369 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, |
| 370 SQLITE_UTF8, SQLITE_STATIC); |
| 371 } |
| 372 } |
| 373 |
| 374 /* Force an SQLITE_TOOBIG error. */ |
| 375 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ |
| 376 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 377 pCtx->isError = SQLITE_TOOBIG; |
| 378 pCtx->fErrorOrAux = 1; |
| 379 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, |
| 380 SQLITE_UTF8, SQLITE_STATIC); |
| 381 } |
| 382 |
| 383 /* An SQLITE_NOMEM error. */ |
| 384 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ |
| 385 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 386 sqlite3VdbeMemSetNull(pCtx->pOut); |
| 387 pCtx->isError = SQLITE_NOMEM; |
| 388 pCtx->fErrorOrAux = 1; |
| 389 pCtx->pOut->db->mallocFailed = 1; |
| 390 } |
| 391 |
| 392 /* |
| 393 ** This function is called after a transaction has been committed. It |
| 394 ** invokes callbacks registered with sqlite3_wal_hook() as required. |
| 395 */ |
| 396 static int doWalCallbacks(sqlite3 *db){ |
| 397 int rc = SQLITE_OK; |
| 398 #ifndef SQLITE_OMIT_WAL |
| 399 int i; |
| 400 for(i=0; i<db->nDb; i++){ |
| 401 Btree *pBt = db->aDb[i].pBt; |
| 402 if( pBt ){ |
| 403 int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); |
| 404 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ |
| 405 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry); |
| 406 } |
| 407 } |
| 408 } |
| 409 #endif |
| 410 return rc; |
| 411 } |
| 412 |
| 413 /* |
| 414 ** Execute the statement pStmt, either until a row of data is ready, the |
| 415 ** statement is completely executed or an error occurs. |
| 416 ** |
| 417 ** This routine implements the bulk of the logic behind the sqlite_step() |
| 418 ** API. The only thing omitted is the automatic recompile if a |
| 419 ** schema change has occurred. That detail is handled by the |
| 420 ** outer sqlite3_step() wrapper procedure. |
| 421 */ |
| 422 static int sqlite3Step(Vdbe *p){ |
| 423 sqlite3 *db; |
| 424 int rc; |
| 425 |
| 426 assert(p); |
| 427 if( p->magic!=VDBE_MAGIC_RUN ){ |
| 428 /* We used to require that sqlite3_reset() be called before retrying |
| 429 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning |
| 430 ** with version 3.7.0, we changed this so that sqlite3_reset() would |
| 431 ** be called automatically instead of throwing the SQLITE_MISUSE error. |
| 432 ** This "automatic-reset" change is not technically an incompatibility, |
| 433 ** since any application that receives an SQLITE_MISUSE is broken by |
| 434 ** definition. |
| 435 ** |
| 436 ** Nevertheless, some published applications that were originally written |
| 437 ** 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 |
| 439 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the |
| 440 ** legacy behavior of returning SQLITE_MISUSE for cases where the |
| 441 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED |
| 442 ** or SQLITE_BUSY error. |
| 443 */ |
| 444 #ifdef SQLITE_OMIT_AUTORESET |
| 445 if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){ |
| 446 sqlite3_reset((sqlite3_stmt*)p); |
| 447 }else{ |
| 448 return SQLITE_MISUSE_BKPT; |
| 449 } |
| 450 #else |
| 451 sqlite3_reset((sqlite3_stmt*)p); |
| 452 #endif |
| 453 } |
| 454 |
| 455 /* Check that malloc() has not failed. If it has, return early. */ |
| 456 db = p->db; |
| 457 if( db->mallocFailed ){ |
| 458 p->rc = SQLITE_NOMEM; |
| 459 return SQLITE_NOMEM; |
| 460 } |
| 461 |
| 462 if( p->pc<=0 && p->expired ){ |
| 463 p->rc = SQLITE_SCHEMA; |
| 464 rc = SQLITE_ERROR; |
| 465 goto end_of_step; |
| 466 } |
| 467 if( p->pc<0 ){ |
| 468 /* If there are no other statements currently running, then |
| 469 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt |
| 470 ** from interrupting a statement that has not yet started. |
| 471 */ |
| 472 if( db->nVdbeActive==0 ){ |
| 473 db->u1.isInterrupted = 0; |
| 474 } |
| 475 |
| 476 assert( db->nVdbeWrite>0 || db->autoCommit==0 |
| 477 || (db->nDeferredCons==0 && db->nDeferredImmCons==0) |
| 478 ); |
| 479 |
| 480 #ifndef SQLITE_OMIT_TRACE |
| 481 if( db->xProfile && !db->init.busy ){ |
| 482 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); |
| 483 } |
| 484 #endif |
| 485 |
| 486 db->nVdbeActive++; |
| 487 if( p->readOnly==0 ) db->nVdbeWrite++; |
| 488 if( p->bIsReader ) db->nVdbeRead++; |
| 489 p->pc = 0; |
| 490 } |
| 491 #ifndef SQLITE_OMIT_EXPLAIN |
| 492 if( p->explain ){ |
| 493 rc = sqlite3VdbeList(p); |
| 494 }else |
| 495 #endif /* SQLITE_OMIT_EXPLAIN */ |
| 496 { |
| 497 db->nVdbeExec++; |
| 498 rc = sqlite3VdbeExec(p); |
| 499 db->nVdbeExec--; |
| 500 } |
| 501 |
| 502 #ifndef SQLITE_OMIT_TRACE |
| 503 /* Invoke the profile callback if there is one |
| 504 */ |
| 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 |
| 511 |
| 512 if( rc==SQLITE_DONE ){ |
| 513 assert( p->rc==SQLITE_OK ); |
| 514 p->rc = doWalCallbacks(db); |
| 515 if( p->rc!=SQLITE_OK ){ |
| 516 rc = SQLITE_ERROR; |
| 517 } |
| 518 } |
| 519 |
| 520 db->errCode = rc; |
| 521 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ |
| 522 p->rc = SQLITE_NOMEM; |
| 523 } |
| 524 end_of_step: |
| 525 /* At this point local variable rc holds the value that should be |
| 526 ** returned if this statement was compiled using the legacy |
| 527 ** sqlite3_prepare() interface. According to the docs, this can only |
| 528 ** be one of the values in the first assert() below. Variable p->rc |
| 529 ** contains the value that would be returned if sqlite3_finalize() |
| 530 ** were called on statement p. |
| 531 */ |
| 532 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR |
| 533 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE |
| 534 ); |
| 535 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE ); |
| 536 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
| 537 /* 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 |
| 539 ** caller. Set the error code in the database handle to the same value. |
| 540 */ |
| 541 rc = sqlite3VdbeTransferError(p); |
| 542 } |
| 543 return (rc&db->errMask); |
| 544 } |
| 545 |
| 546 /* |
| 547 ** This is the top-level implementation of sqlite3_step(). Call |
| 548 ** sqlite3Step() to do most of the work. If a schema error occurs, |
| 549 ** call sqlite3Reprepare() and try again. |
| 550 */ |
| 551 int sqlite3_step(sqlite3_stmt *pStmt){ |
| 552 int rc = SQLITE_OK; /* Result from sqlite3Step() */ |
| 553 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ |
| 554 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ |
| 555 int cnt = 0; /* Counter to prevent infinite loop of reprepares */ |
| 556 sqlite3 *db; /* The database connection */ |
| 557 |
| 558 if( vdbeSafetyNotNull(v) ){ |
| 559 return SQLITE_MISUSE_BKPT; |
| 560 } |
| 561 db = v->db; |
| 562 sqlite3_mutex_enter(db->mutex); |
| 563 v->doingRerun = 0; |
| 564 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA |
| 565 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ |
| 566 int savedPc = v->pc; |
| 567 rc2 = rc = sqlite3Reprepare(v); |
| 568 if( rc!=SQLITE_OK) break; |
| 569 sqlite3_reset(pStmt); |
| 570 if( savedPc>=0 ) v->doingRerun = 1; |
| 571 assert( v->expired==0 ); |
| 572 } |
| 573 if( rc2!=SQLITE_OK ){ |
| 574 /* This case occurs after failing to recompile an sql statement. |
| 575 ** The error message from the SQL compiler has already been loaded |
| 576 ** into the database handle. This block copies the error message |
| 577 ** from the database handle into the statement and sets the statement |
| 578 ** program counter to 0 to ensure that when the statement is |
| 579 ** finalized or reset the parser error message is available via |
| 580 ** sqlite3_errmsg() and sqlite3_errcode(). |
| 581 */ |
| 582 const char *zErr = (const char *)sqlite3_value_text(db->pErr); |
| 583 assert( zErr!=0 || db->mallocFailed ); |
| 584 sqlite3DbFree(db, v->zErrMsg); |
| 585 if( !db->mallocFailed ){ |
| 586 v->zErrMsg = sqlite3DbStrDup(db, zErr); |
| 587 v->rc = rc2; |
| 588 } else { |
| 589 v->zErrMsg = 0; |
| 590 v->rc = rc = SQLITE_NOMEM; |
| 591 } |
| 592 } |
| 593 rc = sqlite3ApiExit(db, rc); |
| 594 sqlite3_mutex_leave(db->mutex); |
| 595 return rc; |
| 596 } |
| 597 |
| 598 |
| 599 /* |
| 600 ** Extract the user data from a sqlite3_context structure and return a |
| 601 ** pointer to it. |
| 602 */ |
| 603 void *sqlite3_user_data(sqlite3_context *p){ |
| 604 assert( p && p->pFunc ); |
| 605 return p->pFunc->pUserData; |
| 606 } |
| 607 |
| 608 /* |
| 609 ** Extract the user data from a sqlite3_context structure and return a |
| 610 ** pointer to it. |
| 611 ** |
| 612 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface |
| 613 ** returns a copy of the pointer to the database connection (the 1st |
| 614 ** parameter) of the sqlite3_create_function() and |
| 615 ** sqlite3_create_function16() routines that originally registered the |
| 616 ** application defined function. |
| 617 */ |
| 618 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ |
| 619 assert( p && p->pFunc ); |
| 620 return p->pOut->db; |
| 621 } |
| 622 |
| 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 /* |
| 637 ** 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 |
| 639 ** wrong context. The sqlite3_overload_function() API might construct |
| 640 ** SQL function that use this routine so that the functions will exist |
| 641 ** for name resolution but are actually overloaded by the xFindFunction |
| 642 ** method of virtual tables. |
| 643 */ |
| 644 void sqlite3InvalidFunction( |
| 645 sqlite3_context *context, /* The function calling context */ |
| 646 int NotUsed, /* Number of arguments to the function */ |
| 647 sqlite3_value **NotUsed2 /* Value of each argument */ |
| 648 ){ |
| 649 const char *zName = context->pFunc->zName; |
| 650 char *zErr; |
| 651 UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 652 zErr = sqlite3_mprintf( |
| 653 "unable to use function %s in the requested context", zName); |
| 654 sqlite3_result_error(context, zErr, -1); |
| 655 sqlite3_free(zErr); |
| 656 } |
| 657 |
| 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 /* |
| 680 ** Allocate or return the aggregate context for a user function. A new |
| 681 ** context is allocated on the first call. Subsequent calls return the |
| 682 ** same context that was returned on prior calls. |
| 683 */ |
| 684 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ |
| 685 assert( p && p->pFunc && p->pFunc->xStep ); |
| 686 assert( sqlite3_mutex_held(p->pOut->db->mutex) ); |
| 687 testcase( nByte<0 ); |
| 688 if( (p->pMem->flags & MEM_Agg)==0 ){ |
| 689 return createAggContext(p, nByte); |
| 690 }else{ |
| 691 return (void*)p->pMem->z; |
| 692 } |
| 693 } |
| 694 |
| 695 /* |
| 696 ** Return the auxiliary data pointer, if any, for the iArg'th argument to |
| 697 ** the user-function defined by pCtx. |
| 698 */ |
| 699 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ |
| 700 AuxData *pAuxData; |
| 701 |
| 702 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 703 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ |
| 704 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; |
| 705 } |
| 706 |
| 707 return (pAuxData ? pAuxData->pAux : 0); |
| 708 } |
| 709 |
| 710 /* |
| 711 ** 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 |
| 713 ** deleted by calling the delete function specified when it was set. |
| 714 */ |
| 715 void sqlite3_set_auxdata( |
| 716 sqlite3_context *pCtx, |
| 717 int iArg, |
| 718 void *pAux, |
| 719 void (*xDelete)(void*) |
| 720 ){ |
| 721 AuxData *pAuxData; |
| 722 Vdbe *pVdbe = pCtx->pVdbe; |
| 723 |
| 724 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 725 if( iArg<0 ) goto failed; |
| 726 |
| 727 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ |
| 728 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; |
| 729 } |
| 730 if( pAuxData==0 ){ |
| 731 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); |
| 732 if( !pAuxData ) goto failed; |
| 733 pAuxData->iOp = pCtx->iOp; |
| 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; |
| 740 } |
| 741 }else if( pAuxData->xDelete ){ |
| 742 pAuxData->xDelete(pAuxData->pAux); |
| 743 } |
| 744 |
| 745 pAuxData->pAux = pAux; |
| 746 pAuxData->xDelete = xDelete; |
| 747 return; |
| 748 |
| 749 failed: |
| 750 if( xDelete ){ |
| 751 xDelete(pAux); |
| 752 } |
| 753 } |
| 754 |
| 755 #ifndef SQLITE_OMIT_DEPRECATED |
| 756 /* |
| 757 ** Return the number of times the Step function of an aggregate has been |
| 758 ** called. |
| 759 ** |
| 760 ** This function is deprecated. Do not use it for new code. It is |
| 761 ** provide only to avoid breaking legacy code. New aggregate function |
| 762 ** implementations should keep their own counts within their aggregate |
| 763 ** context. |
| 764 */ |
| 765 int sqlite3_aggregate_count(sqlite3_context *p){ |
| 766 assert( p && p->pMem && p->pFunc && p->pFunc->xStep ); |
| 767 return p->pMem->n; |
| 768 } |
| 769 #endif |
| 770 |
| 771 /* |
| 772 ** Return the number of columns in the result set for the statement pStmt. |
| 773 */ |
| 774 int sqlite3_column_count(sqlite3_stmt *pStmt){ |
| 775 Vdbe *pVm = (Vdbe *)pStmt; |
| 776 return pVm ? pVm->nResColumn : 0; |
| 777 } |
| 778 |
| 779 /* |
| 780 ** Return the number of values available from the current row of the |
| 781 ** currently executing statement pStmt. |
| 782 */ |
| 783 int sqlite3_data_count(sqlite3_stmt *pStmt){ |
| 784 Vdbe *pVm = (Vdbe *)pStmt; |
| 785 if( pVm==0 || pVm->pResultSet==0 ) return 0; |
| 786 return pVm->nResColumn; |
| 787 } |
| 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 } |
| 824 |
| 825 /* |
| 826 ** 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. |
| 828 ** If iCol is not valid, return a pointer to a Mem which has a value |
| 829 ** of NULL. |
| 830 */ |
| 831 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ |
| 832 Vdbe *pVm; |
| 833 Mem *pOut; |
| 834 |
| 835 pVm = (Vdbe *)pStmt; |
| 836 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ |
| 837 sqlite3_mutex_enter(pVm->db->mutex); |
| 838 pOut = &pVm->pResultSet[i]; |
| 839 }else{ |
| 840 if( pVm && ALWAYS(pVm->db) ){ |
| 841 sqlite3_mutex_enter(pVm->db->mutex); |
| 842 sqlite3Error(pVm->db, SQLITE_RANGE); |
| 843 } |
| 844 pOut = (Mem*)columnNullValue(); |
| 845 } |
| 846 return pOut; |
| 847 } |
| 848 |
| 849 /* |
| 850 ** This function is called after invoking an sqlite3_value_XXX function on a |
| 851 ** column value (i.e. a value returned by evaluating an SQL expression in the |
| 852 ** select list of a SELECT statement) that may cause a malloc() failure. If |
| 853 ** malloc() has failed, the threads mallocFailed flag is cleared and the result |
| 854 ** code of statement pStmt set to SQLITE_NOMEM. |
| 855 ** |
| 856 ** Specifically, this is called from within: |
| 857 ** |
| 858 ** sqlite3_column_int() |
| 859 ** sqlite3_column_int64() |
| 860 ** sqlite3_column_text() |
| 861 ** sqlite3_column_text16() |
| 862 ** sqlite3_column_real() |
| 863 ** sqlite3_column_bytes() |
| 864 ** sqlite3_column_bytes16() |
| 865 ** sqiite3_column_blob() |
| 866 */ |
| 867 static void columnMallocFailure(sqlite3_stmt *pStmt) |
| 868 { |
| 869 /* If malloc() failed during an encoding conversion within an |
| 870 ** sqlite3_column_XXX API, then set the return code of the statement to |
| 871 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR |
| 872 ** and _finalize() will return NOMEM. |
| 873 */ |
| 874 Vdbe *p = (Vdbe *)pStmt; |
| 875 if( p ){ |
| 876 p->rc = sqlite3ApiExit(p->db, p->rc); |
| 877 sqlite3_mutex_leave(p->db->mutex); |
| 878 } |
| 879 } |
| 880 |
| 881 /**************************** sqlite3_column_ ******************************* |
| 882 ** The following routines are used to access elements of the current row |
| 883 ** in the result set. |
| 884 */ |
| 885 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ |
| 886 const void *val; |
| 887 val = sqlite3_value_blob( columnMem(pStmt,i) ); |
| 888 /* Even though there is no encoding conversion, value_blob() might |
| 889 ** need to call malloc() to expand the result of a zeroblob() |
| 890 ** expression. |
| 891 */ |
| 892 columnMallocFailure(pStmt); |
| 893 return val; |
| 894 } |
| 895 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ |
| 896 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); |
| 897 columnMallocFailure(pStmt); |
| 898 return val; |
| 899 } |
| 900 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ |
| 901 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); |
| 902 columnMallocFailure(pStmt); |
| 903 return val; |
| 904 } |
| 905 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ |
| 906 double val = sqlite3_value_double( columnMem(pStmt,i) ); |
| 907 columnMallocFailure(pStmt); |
| 908 return val; |
| 909 } |
| 910 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ |
| 911 int val = sqlite3_value_int( columnMem(pStmt,i) ); |
| 912 columnMallocFailure(pStmt); |
| 913 return val; |
| 914 } |
| 915 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ |
| 916 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); |
| 917 columnMallocFailure(pStmt); |
| 918 return val; |
| 919 } |
| 920 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ |
| 921 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); |
| 922 columnMallocFailure(pStmt); |
| 923 return val; |
| 924 } |
| 925 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ |
| 926 Mem *pOut = columnMem(pStmt, i); |
| 927 if( pOut->flags&MEM_Static ){ |
| 928 pOut->flags &= ~MEM_Static; |
| 929 pOut->flags |= MEM_Ephem; |
| 930 } |
| 931 columnMallocFailure(pStmt); |
| 932 return (sqlite3_value *)pOut; |
| 933 } |
| 934 #ifndef SQLITE_OMIT_UTF16 |
| 935 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ |
| 936 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); |
| 937 columnMallocFailure(pStmt); |
| 938 return val; |
| 939 } |
| 940 #endif /* SQLITE_OMIT_UTF16 */ |
| 941 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ |
| 942 int iType = sqlite3_value_type( columnMem(pStmt,i) ); |
| 943 columnMallocFailure(pStmt); |
| 944 return iType; |
| 945 } |
| 946 |
| 947 /* |
| 948 ** Convert the N-th element of pStmt->pColName[] into a string using |
| 949 ** xFunc() then return that string. If N is out of range, return 0. |
| 950 ** |
| 951 ** There are up to 5 names for each column. useType determines which |
| 952 ** name is returned. Here are the names: |
| 953 ** |
| 954 ** 0 The column name as it should be displayed for output |
| 955 ** 1 The datatype name for the column |
| 956 ** 2 The name of the database that the column derives from |
| 957 ** 3 The name of the table that the column derives from |
| 958 ** 4 The name of the table column that the result column derives from |
| 959 ** |
| 960 ** 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. |
| 962 */ |
| 963 static const void *columnName( |
| 964 sqlite3_stmt *pStmt, |
| 965 int N, |
| 966 const void *(*xFunc)(Mem*), |
| 967 int useType |
| 968 ){ |
| 969 const void *ret = 0; |
| 970 Vdbe *p = (Vdbe *)pStmt; |
| 971 int n; |
| 972 sqlite3 *db = p->db; |
| 973 |
| 974 assert( db!=0 ); |
| 975 n = sqlite3_column_count(pStmt); |
| 976 if( N<n && N>=0 ){ |
| 977 N += useType*n; |
| 978 sqlite3_mutex_enter(db->mutex); |
| 979 assert( db->mallocFailed==0 ); |
| 980 ret = xFunc(&p->aColName[N]); |
| 981 /* A malloc may have failed inside of the xFunc() call. If this |
| 982 ** is the case, clear the mallocFailed flag and return NULL. |
| 983 */ |
| 984 if( db->mallocFailed ){ |
| 985 db->mallocFailed = 0; |
| 986 ret = 0; |
| 987 } |
| 988 sqlite3_mutex_leave(db->mutex); |
| 989 } |
| 990 return ret; |
| 991 } |
| 992 |
| 993 /* |
| 994 ** Return the name of the Nth column of the result set returned by SQL |
| 995 ** statement pStmt. |
| 996 */ |
| 997 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
| 998 return columnName( |
| 999 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); |
| 1000 } |
| 1001 #ifndef SQLITE_OMIT_UTF16 |
| 1002 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
| 1003 return columnName( |
| 1004 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); |
| 1005 } |
| 1006 #endif |
| 1007 |
| 1008 /* |
| 1009 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must |
| 1010 ** not define OMIT_DECLTYPE. |
| 1011 */ |
| 1012 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) |
| 1013 # error "Must not define both SQLITE_OMIT_DECLTYPE \ |
| 1014 and SQLITE_ENABLE_COLUMN_METADATA" |
| 1015 #endif |
| 1016 |
| 1017 #ifndef SQLITE_OMIT_DECLTYPE |
| 1018 /* |
| 1019 ** Return the column declaration type (if applicable) of the 'i'th column |
| 1020 ** of the result set of SQL statement pStmt. |
| 1021 */ |
| 1022 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ |
| 1023 return columnName( |
| 1024 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); |
| 1025 } |
| 1026 #ifndef SQLITE_OMIT_UTF16 |
| 1027 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ |
| 1028 return columnName( |
| 1029 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); |
| 1030 } |
| 1031 #endif /* SQLITE_OMIT_UTF16 */ |
| 1032 #endif /* SQLITE_OMIT_DECLTYPE */ |
| 1033 |
| 1034 #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 1035 /* |
| 1036 ** Return the name of the database from which a result column derives. |
| 1037 ** NULL is returned if the result column is an expression or constant or |
| 1038 ** anything else which is not an unambiguous reference to a database column. |
| 1039 */ |
| 1040 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ |
| 1041 return columnName( |
| 1042 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); |
| 1043 } |
| 1044 #ifndef SQLITE_OMIT_UTF16 |
| 1045 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ |
| 1046 return columnName( |
| 1047 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); |
| 1048 } |
| 1049 #endif /* SQLITE_OMIT_UTF16 */ |
| 1050 |
| 1051 /* |
| 1052 ** Return the name of the table from which a result column derives. |
| 1053 ** NULL is returned if the result column is an expression or constant or |
| 1054 ** anything else which is not an unambiguous reference to a database column. |
| 1055 */ |
| 1056 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ |
| 1057 return columnName( |
| 1058 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); |
| 1059 } |
| 1060 #ifndef SQLITE_OMIT_UTF16 |
| 1061 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ |
| 1062 return columnName( |
| 1063 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); |
| 1064 } |
| 1065 #endif /* SQLITE_OMIT_UTF16 */ |
| 1066 |
| 1067 /* |
| 1068 ** Return the name of the table column from which a result column derives. |
| 1069 ** NULL is returned if the result column is an expression or constant or |
| 1070 ** anything else which is not an unambiguous reference to a database column. |
| 1071 */ |
| 1072 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ |
| 1073 return columnName( |
| 1074 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); |
| 1075 } |
| 1076 #ifndef SQLITE_OMIT_UTF16 |
| 1077 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ |
| 1078 return columnName( |
| 1079 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); |
| 1080 } |
| 1081 #endif /* SQLITE_OMIT_UTF16 */ |
| 1082 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ |
| 1083 |
| 1084 |
| 1085 /******************************* sqlite3_bind_ *************************** |
| 1086 ** |
| 1087 ** Routines used to attach values to wildcards in a compiled SQL statement. |
| 1088 */ |
| 1089 /* |
| 1090 ** Unbind the value bound to variable i in virtual machine p. This is the |
| 1091 ** the same as binding a NULL value to the column. If the "i" parameter is |
| 1092 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
| 1093 ** |
| 1094 ** A successful evaluation of this routine acquires the mutex on p. |
| 1095 ** the mutex is released if any kind of error occurs. |
| 1096 ** |
| 1097 ** The error code stored in database p->db is overwritten with the return |
| 1098 ** value in any case. |
| 1099 */ |
| 1100 static int vdbeUnbind(Vdbe *p, int i){ |
| 1101 Mem *pVar; |
| 1102 if( vdbeSafetyNotNull(p) ){ |
| 1103 return SQLITE_MISUSE_BKPT; |
| 1104 } |
| 1105 sqlite3_mutex_enter(p->db->mutex); |
| 1106 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ |
| 1107 sqlite3Error(p->db, SQLITE_MISUSE); |
| 1108 sqlite3_mutex_leave(p->db->mutex); |
| 1109 sqlite3_log(SQLITE_MISUSE, |
| 1110 "bind on a busy prepared statement: [%s]", p->zSql); |
| 1111 return SQLITE_MISUSE_BKPT; |
| 1112 } |
| 1113 if( i<1 || i>p->nVar ){ |
| 1114 sqlite3Error(p->db, SQLITE_RANGE); |
| 1115 sqlite3_mutex_leave(p->db->mutex); |
| 1116 return SQLITE_RANGE; |
| 1117 } |
| 1118 i--; |
| 1119 pVar = &p->aVar[i]; |
| 1120 sqlite3VdbeMemRelease(pVar); |
| 1121 pVar->flags = MEM_Null; |
| 1122 sqlite3Error(p->db, SQLITE_OK); |
| 1123 |
| 1124 /* If the bit corresponding to this variable in Vdbe.expmask is set, then |
| 1125 ** binding a new value to this variable invalidates the current query plan. |
| 1126 ** |
| 1127 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host |
| 1128 ** parameter in the WHERE clause might influence the choice of query plan |
| 1129 ** for a statement, then the statement will be automatically recompiled, |
| 1130 ** as if there had been a schema change, on the first sqlite3_step() call |
| 1131 ** following any change to the bindings of that parameter. |
| 1132 */ |
| 1133 if( p->isPrepareV2 && |
| 1134 ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) |
| 1135 ){ |
| 1136 p->expired = 1; |
| 1137 } |
| 1138 return SQLITE_OK; |
| 1139 } |
| 1140 |
| 1141 /* |
| 1142 ** Bind a text or BLOB value. |
| 1143 */ |
| 1144 static int bindText( |
| 1145 sqlite3_stmt *pStmt, /* The statement to bind against */ |
| 1146 int i, /* Index of the parameter to bind */ |
| 1147 const void *zData, /* Pointer to the data to be bound */ |
| 1148 int nData, /* Number of bytes of data to be bound */ |
| 1149 void (*xDel)(void*), /* Destructor for the data */ |
| 1150 u8 encoding /* Encoding for the data */ |
| 1151 ){ |
| 1152 Vdbe *p = (Vdbe *)pStmt; |
| 1153 Mem *pVar; |
| 1154 int rc; |
| 1155 |
| 1156 rc = vdbeUnbind(p, i); |
| 1157 if( rc==SQLITE_OK ){ |
| 1158 if( zData!=0 ){ |
| 1159 pVar = &p->aVar[i-1]; |
| 1160 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); |
| 1161 if( rc==SQLITE_OK && encoding!=0 ){ |
| 1162 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); |
| 1163 } |
| 1164 sqlite3Error(p->db, rc); |
| 1165 rc = sqlite3ApiExit(p->db, rc); |
| 1166 } |
| 1167 sqlite3_mutex_leave(p->db->mutex); |
| 1168 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ |
| 1169 xDel((void*)zData); |
| 1170 } |
| 1171 return rc; |
| 1172 } |
| 1173 |
| 1174 |
| 1175 /* |
| 1176 ** Bind a blob value to an SQL statement variable. |
| 1177 */ |
| 1178 int sqlite3_bind_blob( |
| 1179 sqlite3_stmt *pStmt, |
| 1180 int i, |
| 1181 const void *zData, |
| 1182 int nData, |
| 1183 void (*xDel)(void*) |
| 1184 ){ |
| 1185 return bindText(pStmt, i, zData, nData, xDel, 0); |
| 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 } |
| 1201 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 1202 int rc; |
| 1203 Vdbe *p = (Vdbe *)pStmt; |
| 1204 rc = vdbeUnbind(p, i); |
| 1205 if( rc==SQLITE_OK ){ |
| 1206 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); |
| 1207 sqlite3_mutex_leave(p->db->mutex); |
| 1208 } |
| 1209 return rc; |
| 1210 } |
| 1211 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
| 1212 return sqlite3_bind_int64(p, i, (i64)iValue); |
| 1213 } |
| 1214 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
| 1215 int rc; |
| 1216 Vdbe *p = (Vdbe *)pStmt; |
| 1217 rc = vdbeUnbind(p, i); |
| 1218 if( rc==SQLITE_OK ){ |
| 1219 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
| 1220 sqlite3_mutex_leave(p->db->mutex); |
| 1221 } |
| 1222 return rc; |
| 1223 } |
| 1224 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ |
| 1225 int rc; |
| 1226 Vdbe *p = (Vdbe*)pStmt; |
| 1227 rc = vdbeUnbind(p, i); |
| 1228 if( rc==SQLITE_OK ){ |
| 1229 sqlite3_mutex_leave(p->db->mutex); |
| 1230 } |
| 1231 return rc; |
| 1232 } |
| 1233 int sqlite3_bind_text( |
| 1234 sqlite3_stmt *pStmt, |
| 1235 int i, |
| 1236 const char *zData, |
| 1237 int nData, |
| 1238 void (*xDel)(void*) |
| 1239 ){ |
| 1240 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); |
| 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 } |
| 1258 #ifndef SQLITE_OMIT_UTF16 |
| 1259 int sqlite3_bind_text16( |
| 1260 sqlite3_stmt *pStmt, |
| 1261 int i, |
| 1262 const void *zData, |
| 1263 int nData, |
| 1264 void (*xDel)(void*) |
| 1265 ){ |
| 1266 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); |
| 1267 } |
| 1268 #endif /* SQLITE_OMIT_UTF16 */ |
| 1269 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ |
| 1270 int rc; |
| 1271 switch( sqlite3_value_type((sqlite3_value*)pValue) ){ |
| 1272 case SQLITE_INTEGER: { |
| 1273 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); |
| 1274 break; |
| 1275 } |
| 1276 case SQLITE_FLOAT: { |
| 1277 rc = sqlite3_bind_double(pStmt, i, pValue->u.r); |
| 1278 break; |
| 1279 } |
| 1280 case SQLITE_BLOB: { |
| 1281 if( pValue->flags & MEM_Zero ){ |
| 1282 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); |
| 1283 }else{ |
| 1284 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); |
| 1285 } |
| 1286 break; |
| 1287 } |
| 1288 case SQLITE_TEXT: { |
| 1289 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, |
| 1290 pValue->enc); |
| 1291 break; |
| 1292 } |
| 1293 default: { |
| 1294 rc = sqlite3_bind_null(pStmt, i); |
| 1295 break; |
| 1296 } |
| 1297 } |
| 1298 return rc; |
| 1299 } |
| 1300 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ |
| 1301 int rc; |
| 1302 Vdbe *p = (Vdbe *)pStmt; |
| 1303 rc = vdbeUnbind(p, i); |
| 1304 if( rc==SQLITE_OK ){ |
| 1305 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
| 1306 sqlite3_mutex_leave(p->db->mutex); |
| 1307 } |
| 1308 return rc; |
| 1309 } |
| 1310 |
| 1311 /* |
| 1312 ** Return the number of wildcards that can be potentially bound to. |
| 1313 ** This routine is added to support DBD::SQLite. |
| 1314 */ |
| 1315 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ |
| 1316 Vdbe *p = (Vdbe*)pStmt; |
| 1317 return p ? p->nVar : 0; |
| 1318 } |
| 1319 |
| 1320 /* |
| 1321 ** Return the name of a wildcard parameter. Return NULL if the index |
| 1322 ** is out of range or if the wildcard is unnamed. |
| 1323 ** |
| 1324 ** The result is always UTF-8. |
| 1325 */ |
| 1326 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ |
| 1327 Vdbe *p = (Vdbe*)pStmt; |
| 1328 if( p==0 || i<1 || i>p->nzVar ){ |
| 1329 return 0; |
| 1330 } |
| 1331 return p->azVar[i-1]; |
| 1332 } |
| 1333 |
| 1334 /* |
| 1335 ** Given a wildcard parameter name, return the index of the variable |
| 1336 ** with that name. If there is no variable with the given name, |
| 1337 ** return 0. |
| 1338 */ |
| 1339 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ |
| 1340 int i; |
| 1341 if( p==0 ){ |
| 1342 return 0; |
| 1343 } |
| 1344 if( zName ){ |
| 1345 for(i=0; i<p->nzVar; i++){ |
| 1346 const char *z = p->azVar[i]; |
| 1347 if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){ |
| 1348 return i+1; |
| 1349 } |
| 1350 } |
| 1351 } |
| 1352 return 0; |
| 1353 } |
| 1354 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ |
| 1355 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); |
| 1356 } |
| 1357 |
| 1358 /* |
| 1359 ** Transfer all bindings from the first statement over to the second. |
| 1360 */ |
| 1361 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
| 1362 Vdbe *pFrom = (Vdbe*)pFromStmt; |
| 1363 Vdbe *pTo = (Vdbe*)pToStmt; |
| 1364 int i; |
| 1365 assert( pTo->db==pFrom->db ); |
| 1366 assert( pTo->nVar==pFrom->nVar ); |
| 1367 sqlite3_mutex_enter(pTo->db->mutex); |
| 1368 for(i=0; i<pFrom->nVar; i++){ |
| 1369 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); |
| 1370 } |
| 1371 sqlite3_mutex_leave(pTo->db->mutex); |
| 1372 return SQLITE_OK; |
| 1373 } |
| 1374 |
| 1375 #ifndef SQLITE_OMIT_DEPRECATED |
| 1376 /* |
| 1377 ** Deprecated external interface. Internal/core SQLite code |
| 1378 ** should call sqlite3TransferBindings. |
| 1379 ** |
| 1380 ** It is misuse to call this routine with statements from different |
| 1381 ** database connections. But as this is a deprecated interface, we |
| 1382 ** will not bother to check for that condition. |
| 1383 ** |
| 1384 ** If the two statements contain a different number of bindings, then |
| 1385 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise |
| 1386 ** SQLITE_OK is returned. |
| 1387 */ |
| 1388 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
| 1389 Vdbe *pFrom = (Vdbe*)pFromStmt; |
| 1390 Vdbe *pTo = (Vdbe*)pToStmt; |
| 1391 if( pFrom->nVar!=pTo->nVar ){ |
| 1392 return SQLITE_ERROR; |
| 1393 } |
| 1394 if( pTo->isPrepareV2 && pTo->expmask ){ |
| 1395 pTo->expired = 1; |
| 1396 } |
| 1397 if( pFrom->isPrepareV2 && pFrom->expmask ){ |
| 1398 pFrom->expired = 1; |
| 1399 } |
| 1400 return sqlite3TransferBindings(pFromStmt, pToStmt); |
| 1401 } |
| 1402 #endif |
| 1403 |
| 1404 /* |
| 1405 ** Return the sqlite3* database handle to which the prepared statement given |
| 1406 ** in the argument belongs. This is the same database handle that was |
| 1407 ** the first argument to the sqlite3_prepare() that was used to create |
| 1408 ** the statement in the first place. |
| 1409 */ |
| 1410 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ |
| 1411 return pStmt ? ((Vdbe*)pStmt)->db : 0; |
| 1412 } |
| 1413 |
| 1414 /* |
| 1415 ** Return true if the prepared statement is guaranteed to not modify the |
| 1416 ** database. |
| 1417 */ |
| 1418 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ |
| 1419 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; |
| 1420 } |
| 1421 |
| 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 /* |
| 1431 ** Return a pointer to the next prepared statement after pStmt associated |
| 1432 ** with database connection pDb. If pStmt is NULL, return the first |
| 1433 ** prepared statement for the database connection. Return NULL if there |
| 1434 ** are no more. |
| 1435 */ |
| 1436 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 1437 sqlite3_stmt *pNext; |
| 1438 sqlite3_mutex_enter(pDb->mutex); |
| 1439 if( pStmt==0 ){ |
| 1440 pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 1441 }else{ |
| 1442 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| 1443 } |
| 1444 sqlite3_mutex_leave(pDb->mutex); |
| 1445 return pNext; |
| 1446 } |
| 1447 |
| 1448 /* |
| 1449 ** Return the value of a status counter for a prepared statement |
| 1450 */ |
| 1451 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 1452 Vdbe *pVdbe = (Vdbe*)pStmt; |
| 1453 u32 v = pVdbe->aCounter[op]; |
| 1454 if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 1455 return (int)v; |
| 1456 } |
OLD | NEW |