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

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

Issue 2751253002: [sql] Import SQLite 3.17.0. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 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
« no previous file with comments | « third_party/sqlite/src/src/vdbeInt.h ('k') | third_party/sqlite/src/src/vdbeaux.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 } 53 }
54 } 54 }
55 55
56 #ifndef SQLITE_OMIT_TRACE 56 #ifndef SQLITE_OMIT_TRACE
57 /* 57 /*
58 ** Invoke the profile callback. This routine is only called if we already 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. 59 ** know that the profile callback is defined and needs to be invoked.
60 */ 60 */
61 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ 61 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
62 sqlite3_int64 iNow; 62 sqlite3_int64 iNow;
63 sqlite3_int64 iElapse;
63 assert( p->startTime>0 ); 64 assert( p->startTime>0 );
64 assert( db->xProfile!=0 ); 65 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
65 assert( db->init.busy==0 ); 66 assert( db->init.busy==0 );
66 assert( p->zSql!=0 ); 67 assert( p->zSql!=0 );
67 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 68 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
68 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000); 69 iElapse = (iNow - p->startTime)*1000000;
70 if( db->xProfile ){
71 db->xProfile(db->pProfileArg, p->zSql, iElapse);
72 }
73 if( db->mTrace & SQLITE_TRACE_PROFILE ){
74 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
75 }
69 p->startTime = 0; 76 p->startTime = 0;
70 } 77 }
71 /* 78 /*
72 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback 79 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
73 ** is needed, and it invokes the callback if it is needed. 80 ** is needed, and it invokes the callback if it is needed.
74 */ 81 */
75 # define checkProfileCallback(DB,P) \ 82 # define checkProfileCallback(DB,P) \
76 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } 83 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
77 #else 84 #else
78 # define checkProfileCallback(DB,P) /*no-op*/ 85 # define checkProfileCallback(DB,P) /*no-op*/
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 162 }
156 163
157 164
158 /**************************** sqlite3_value_ ******************************* 165 /**************************** sqlite3_value_ *******************************
159 ** The following routines extract information from a Mem or sqlite3_value 166 ** The following routines extract information from a Mem or sqlite3_value
160 ** structure. 167 ** structure.
161 */ 168 */
162 const void *sqlite3_value_blob(sqlite3_value *pVal){ 169 const void *sqlite3_value_blob(sqlite3_value *pVal){
163 Mem *p = (Mem*)pVal; 170 Mem *p = (Mem*)pVal;
164 if( p->flags & (MEM_Blob|MEM_Str) ){ 171 if( p->flags & (MEM_Blob|MEM_Str) ){
165 if( sqlite3VdbeMemExpandBlob(p)!=SQLITE_OK ){ 172 if( ExpandBlob(p)!=SQLITE_OK ){
166 assert( p->flags==MEM_Null && p->z==0 ); 173 assert( p->flags==MEM_Null && p->z==0 );
167 return 0; 174 return 0;
168 } 175 }
169 p->flags |= MEM_Blob; 176 p->flags |= MEM_Blob;
170 return p->n ? p->z : 0; 177 return p->n ? p->z : 0;
171 }else{ 178 }else{
172 return sqlite3_value_text(pVal); 179 return sqlite3_value_text(pVal);
173 } 180 }
174 } 181 }
175 int sqlite3_value_bytes(sqlite3_value *pVal){ 182 int sqlite3_value_bytes(sqlite3_value *pVal){
176 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 183 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
177 } 184 }
178 int sqlite3_value_bytes16(sqlite3_value *pVal){ 185 int sqlite3_value_bytes16(sqlite3_value *pVal){
179 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 186 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
180 } 187 }
181 double sqlite3_value_double(sqlite3_value *pVal){ 188 double sqlite3_value_double(sqlite3_value *pVal){
182 return sqlite3VdbeRealValue((Mem*)pVal); 189 return sqlite3VdbeRealValue((Mem*)pVal);
183 } 190 }
184 int sqlite3_value_int(sqlite3_value *pVal){ 191 int sqlite3_value_int(sqlite3_value *pVal){
185 return (int)sqlite3VdbeIntValue((Mem*)pVal); 192 return (int)sqlite3VdbeIntValue((Mem*)pVal);
186 } 193 }
187 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 194 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
188 return sqlite3VdbeIntValue((Mem*)pVal); 195 return sqlite3VdbeIntValue((Mem*)pVal);
189 } 196 }
190 unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ 197 unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
191 return ((Mem*)pVal)->eSubtype; 198 Mem *pMem = (Mem*)pVal;
199 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
192 } 200 }
193 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 201 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
194 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 202 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
195 } 203 }
196 #ifndef SQLITE_OMIT_UTF16 204 #ifndef SQLITE_OMIT_UTF16
197 const void *sqlite3_value_text16(sqlite3_value* pVal){ 205 const void *sqlite3_value_text16(sqlite3_value* pVal){
198 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 206 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
199 } 207 }
200 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 208 const void *sqlite3_value_text16be(sqlite3_value *pVal){
201 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 209 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 } 370 }
363 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 371 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
364 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 372 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
365 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 373 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
366 } 374 }
367 void sqlite3_result_null(sqlite3_context *pCtx){ 375 void sqlite3_result_null(sqlite3_context *pCtx){
368 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 376 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
369 sqlite3VdbeMemSetNull(pCtx->pOut); 377 sqlite3VdbeMemSetNull(pCtx->pOut);
370 } 378 }
371 void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ 379 void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
372 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 380 Mem *pOut = pCtx->pOut;
373 pCtx->pOut->eSubtype = eSubtype & 0xff; 381 assert( sqlite3_mutex_held(pOut->db->mutex) );
382 pOut->eSubtype = eSubtype & 0xff;
383 pOut->flags |= MEM_Subtype;
374 } 384 }
375 void sqlite3_result_text( 385 void sqlite3_result_text(
376 sqlite3_context *pCtx, 386 sqlite3_context *pCtx,
377 const char *z, 387 const char *z,
378 int n, 388 int n,
379 void (*xDel)(void *) 389 void (*xDel)(void *)
380 ){ 390 ){
381 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 391 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
382 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 392 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
383 } 393 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 pCtx->isError = SQLITE_TOOBIG; 471 pCtx->isError = SQLITE_TOOBIG;
462 pCtx->fErrorOrAux = 1; 472 pCtx->fErrorOrAux = 1;
463 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 473 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
464 SQLITE_UTF8, SQLITE_STATIC); 474 SQLITE_UTF8, SQLITE_STATIC);
465 } 475 }
466 476
467 /* An SQLITE_NOMEM error. */ 477 /* An SQLITE_NOMEM error. */
468 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 478 void sqlite3_result_error_nomem(sqlite3_context *pCtx){
469 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 479 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
470 sqlite3VdbeMemSetNull(pCtx->pOut); 480 sqlite3VdbeMemSetNull(pCtx->pOut);
471 pCtx->isError = SQLITE_NOMEM; 481 pCtx->isError = SQLITE_NOMEM_BKPT;
472 pCtx->fErrorOrAux = 1; 482 pCtx->fErrorOrAux = 1;
473 pCtx->pOut->db->mallocFailed = 1; 483 sqlite3OomFault(pCtx->pOut->db);
474 } 484 }
475 485
476 /* 486 /*
477 ** This function is called after a transaction has been committed. It 487 ** This function is called after a transaction has been committed. It
478 ** invokes callbacks registered with sqlite3_wal_hook() as required. 488 ** invokes callbacks registered with sqlite3_wal_hook() as required.
479 */ 489 */
480 static int doWalCallbacks(sqlite3 *db){ 490 static int doWalCallbacks(sqlite3 *db){
481 int rc = SQLITE_OK; 491 int rc = SQLITE_OK;
482 #ifndef SQLITE_OMIT_WAL 492 #ifndef SQLITE_OMIT_WAL
483 int i; 493 int i;
484 for(i=0; i<db->nDb; i++){ 494 for(i=0; i<db->nDb; i++){
485 Btree *pBt = db->aDb[i].pBt; 495 Btree *pBt = db->aDb[i].pBt;
486 if( pBt ){ 496 if( pBt ){
487 int nEntry; 497 int nEntry;
488 sqlite3BtreeEnter(pBt); 498 sqlite3BtreeEnter(pBt);
489 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 499 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
490 sqlite3BtreeLeave(pBt); 500 sqlite3BtreeLeave(pBt);
491 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ 501 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
492 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry); 502 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
493 } 503 }
494 } 504 }
495 } 505 }
496 #endif 506 #endif
497 return rc; 507 return rc;
498 } 508 }
499 509
500 510
501 /* 511 /*
502 ** Execute the statement pStmt, either until a row of data is ready, the 512 ** Execute the statement pStmt, either until a row of data is ready, the
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 } 547 }
538 #else 548 #else
539 sqlite3_reset((sqlite3_stmt*)p); 549 sqlite3_reset((sqlite3_stmt*)p);
540 #endif 550 #endif
541 } 551 }
542 552
543 /* Check that malloc() has not failed. If it has, return early. */ 553 /* Check that malloc() has not failed. If it has, return early. */
544 db = p->db; 554 db = p->db;
545 if( db->mallocFailed ){ 555 if( db->mallocFailed ){
546 p->rc = SQLITE_NOMEM; 556 p->rc = SQLITE_NOMEM;
547 return SQLITE_NOMEM; 557 return SQLITE_NOMEM_BKPT;
548 } 558 }
549 559
550 if( p->pc<=0 && p->expired ){ 560 if( p->pc<=0 && p->expired ){
551 p->rc = SQLITE_SCHEMA; 561 p->rc = SQLITE_SCHEMA;
552 rc = SQLITE_ERROR; 562 rc = SQLITE_ERROR;
553 goto end_of_step; 563 goto end_of_step;
554 } 564 }
555 if( p->pc<0 ){ 565 if( p->pc<0 ){
556 /* If there are no other statements currently running, then 566 /* If there are no other statements currently running, then
557 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 567 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
558 ** from interrupting a statement that has not yet started. 568 ** from interrupting a statement that has not yet started.
559 */ 569 */
560 if( db->nVdbeActive==0 ){ 570 if( db->nVdbeActive==0 ){
561 db->u1.isInterrupted = 0; 571 db->u1.isInterrupted = 0;
562 } 572 }
563 573
564 assert( db->nVdbeWrite>0 || db->autoCommit==0 574 assert( db->nVdbeWrite>0 || db->autoCommit==0
565 || (db->nDeferredCons==0 && db->nDeferredImmCons==0) 575 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
566 ); 576 );
567 577
568 #ifndef SQLITE_OMIT_TRACE 578 #ifndef SQLITE_OMIT_TRACE
569 if( db->xProfile && !db->init.busy && p->zSql ){ 579 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
580 && !db->init.busy && p->zSql ){
570 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 581 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
571 }else{ 582 }else{
572 assert( p->startTime==0 ); 583 assert( p->startTime==0 );
573 } 584 }
574 #endif 585 #endif
575 586
576 db->nVdbeActive++; 587 db->nVdbeActive++;
577 if( p->readOnly==0 ) db->nVdbeWrite++; 588 if( p->readOnly==0 ) db->nVdbeWrite++;
578 if( p->bIsReader ) db->nVdbeRead++; 589 if( p->bIsReader ) db->nVdbeRead++;
579 p->pc = 0; 590 p->pc = 0;
(...skipping 20 matching lines...) Expand all
600 if( rc==SQLITE_DONE ){ 611 if( rc==SQLITE_DONE ){
601 assert( p->rc==SQLITE_OK ); 612 assert( p->rc==SQLITE_OK );
602 p->rc = doWalCallbacks(db); 613 p->rc = doWalCallbacks(db);
603 if( p->rc!=SQLITE_OK ){ 614 if( p->rc!=SQLITE_OK ){
604 rc = SQLITE_ERROR; 615 rc = SQLITE_ERROR;
605 } 616 }
606 } 617 }
607 618
608 db->errCode = rc; 619 db->errCode = rc;
609 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 620 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
610 p->rc = SQLITE_NOMEM; 621 p->rc = SQLITE_NOMEM_BKPT;
611 } 622 }
612 end_of_step: 623 end_of_step:
613 /* At this point local variable rc holds the value that should be 624 /* At this point local variable rc holds the value that should be
614 ** returned if this statement was compiled using the legacy 625 ** returned if this statement was compiled using the legacy
615 ** sqlite3_prepare() interface. According to the docs, this can only 626 ** sqlite3_prepare() interface. According to the docs, this can only
616 ** be one of the values in the first assert() below. Variable p->rc 627 ** be one of the values in the first assert() below. Variable p->rc
617 ** contains the value that would be returned if sqlite3_finalize() 628 ** contains the value that would be returned if sqlite3_finalize()
618 ** were called on statement p. 629 ** were called on statement p.
619 */ 630 */
620 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 631 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 ** finalized or reset the parser error message is available via 678 ** finalized or reset the parser error message is available via
668 ** sqlite3_errmsg() and sqlite3_errcode(). 679 ** sqlite3_errmsg() and sqlite3_errcode().
669 */ 680 */
670 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 681 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
671 sqlite3DbFree(db, v->zErrMsg); 682 sqlite3DbFree(db, v->zErrMsg);
672 if( !db->mallocFailed ){ 683 if( !db->mallocFailed ){
673 v->zErrMsg = sqlite3DbStrDup(db, zErr); 684 v->zErrMsg = sqlite3DbStrDup(db, zErr);
674 v->rc = rc2; 685 v->rc = rc2;
675 } else { 686 } else {
676 v->zErrMsg = 0; 687 v->zErrMsg = 0;
677 v->rc = rc = SQLITE_NOMEM; 688 v->rc = rc = SQLITE_NOMEM_BKPT;
678 } 689 }
679 } 690 }
680 rc = sqlite3ApiExit(db, rc); 691 rc = sqlite3ApiExit(db, rc);
681 sqlite3_mutex_leave(db->mutex); 692 sqlite3_mutex_leave(db->mutex);
682 return rc; 693 return rc;
683 } 694 }
684 695
685 696
686 /* 697 /*
687 ** Extract the user data from a sqlite3_context structure and return a 698 ** Extract the user data from a sqlite3_context structure and return a
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 } 783 }
773 return (void*)pMem->z; 784 return (void*)pMem->z;
774 } 785 }
775 786
776 /* 787 /*
777 ** Allocate or return the aggregate context for a user function. A new 788 ** Allocate or return the aggregate context for a user function. A new
778 ** context is allocated on the first call. Subsequent calls return the 789 ** context is allocated on the first call. Subsequent calls return the
779 ** same context that was returned on prior calls. 790 ** same context that was returned on prior calls.
780 */ 791 */
781 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 792 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
782 assert( p && p->pFunc && p->pFunc->xStep ); 793 assert( p && p->pFunc && p->pFunc->xFinalize );
783 assert( sqlite3_mutex_held(p->pOut->db->mutex) ); 794 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
784 testcase( nByte<0 ); 795 testcase( nByte<0 );
785 if( (p->pMem->flags & MEM_Agg)==0 ){ 796 if( (p->pMem->flags & MEM_Agg)==0 ){
786 return createAggContext(p, nByte); 797 return createAggContext(p, nByte);
787 }else{ 798 }else{
788 return (void*)p->pMem->z; 799 return (void*)p->pMem->z;
789 } 800 }
790 } 801 }
791 802
792 /* 803 /*
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 /* 874 /*
864 ** Return the number of times the Step function of an aggregate has been 875 ** Return the number of times the Step function of an aggregate has been
865 ** called. 876 ** called.
866 ** 877 **
867 ** This function is deprecated. Do not use it for new code. It is 878 ** This function is deprecated. Do not use it for new code. It is
868 ** provide only to avoid breaking legacy code. New aggregate function 879 ** provide only to avoid breaking legacy code. New aggregate function
869 ** implementations should keep their own counts within their aggregate 880 ** implementations should keep their own counts within their aggregate
870 ** context. 881 ** context.
871 */ 882 */
872 int sqlite3_aggregate_count(sqlite3_context *p){ 883 int sqlite3_aggregate_count(sqlite3_context *p){
873 assert( p && p->pMem && p->pFunc && p->pFunc->xStep ); 884 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
874 return p->pMem->n; 885 return p->pMem->n;
875 } 886 }
876 #endif 887 #endif
877 888
878 /* 889 /*
879 ** Return the number of columns in the result set for the statement pStmt. 890 ** Return the number of columns in the result set for the statement pStmt.
880 */ 891 */
881 int sqlite3_column_count(sqlite3_stmt *pStmt){ 892 int sqlite3_column_count(sqlite3_stmt *pStmt){
882 Vdbe *pVm = (Vdbe *)pStmt; 893 Vdbe *pVm = (Vdbe *)pStmt;
883 return pVm ? pVm->nResColumn : 0; 894 return pVm ? pVm->nResColumn : 0;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 ** Check to see if column iCol of the given statement is valid. If 945 ** Check to see if column iCol of the given statement is valid. If
935 ** it is, return a pointer to the Mem for the value of that column. 946 ** it is, return a pointer to the Mem for the value of that column.
936 ** If iCol is not valid, return a pointer to a Mem which has a value 947 ** If iCol is not valid, return a pointer to a Mem which has a value
937 ** of NULL. 948 ** of NULL.
938 */ 949 */
939 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 950 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
940 Vdbe *pVm; 951 Vdbe *pVm;
941 Mem *pOut; 952 Mem *pOut;
942 953
943 pVm = (Vdbe *)pStmt; 954 pVm = (Vdbe *)pStmt;
944 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 955 if( pVm==0 ) return (Mem*)columnNullValue();
945 sqlite3_mutex_enter(pVm->db->mutex); 956 assert( pVm->db );
957 sqlite3_mutex_enter(pVm->db->mutex);
958 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
946 pOut = &pVm->pResultSet[i]; 959 pOut = &pVm->pResultSet[i];
947 }else{ 960 }else{
948 if( pVm && ALWAYS(pVm->db) ){ 961 sqlite3Error(pVm->db, SQLITE_RANGE);
949 sqlite3_mutex_enter(pVm->db->mutex);
950 sqlite3Error(pVm->db, SQLITE_RANGE);
951 }
952 pOut = (Mem*)columnNullValue(); 962 pOut = (Mem*)columnNullValue();
953 } 963 }
954 return pOut; 964 return pOut;
955 } 965 }
956 966
957 /* 967 /*
958 ** This function is called after invoking an sqlite3_value_XXX function on a 968 ** This function is called after invoking an sqlite3_value_XXX function on a
959 ** column value (i.e. a value returned by evaluating an SQL expression in the 969 ** column value (i.e. a value returned by evaluating an SQL expression in the
960 ** select list of a SELECT statement) that may cause a malloc() failure. If 970 ** select list of a SELECT statement) that may cause a malloc() failure. If
961 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 971 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
(...skipping 12 matching lines...) Expand all
974 */ 984 */
975 static void columnMallocFailure(sqlite3_stmt *pStmt) 985 static void columnMallocFailure(sqlite3_stmt *pStmt)
976 { 986 {
977 /* If malloc() failed during an encoding conversion within an 987 /* If malloc() failed during an encoding conversion within an
978 ** sqlite3_column_XXX API, then set the return code of the statement to 988 ** sqlite3_column_XXX API, then set the return code of the statement to
979 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 989 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
980 ** and _finalize() will return NOMEM. 990 ** and _finalize() will return NOMEM.
981 */ 991 */
982 Vdbe *p = (Vdbe *)pStmt; 992 Vdbe *p = (Vdbe *)pStmt;
983 if( p ){ 993 if( p ){
994 assert( p->db!=0 );
995 assert( sqlite3_mutex_held(p->db->mutex) );
984 p->rc = sqlite3ApiExit(p->db, p->rc); 996 p->rc = sqlite3ApiExit(p->db, p->rc);
985 sqlite3_mutex_leave(p->db->mutex); 997 sqlite3_mutex_leave(p->db->mutex);
986 } 998 }
987 } 999 }
988 1000
989 /**************************** sqlite3_column_ ******************************* 1001 /**************************** sqlite3_column_ *******************************
990 ** The following routines are used to access elements of the current row 1002 ** The following routines are used to access elements of the current row
991 ** in the result set. 1003 ** in the result set.
992 */ 1004 */
993 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 1005 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1091 n = sqlite3_column_count(pStmt); 1103 n = sqlite3_column_count(pStmt);
1092 if( N<n && N>=0 ){ 1104 if( N<n && N>=0 ){
1093 N += useType*n; 1105 N += useType*n;
1094 sqlite3_mutex_enter(db->mutex); 1106 sqlite3_mutex_enter(db->mutex);
1095 assert( db->mallocFailed==0 ); 1107 assert( db->mallocFailed==0 );
1096 ret = xFunc(&p->aColName[N]); 1108 ret = xFunc(&p->aColName[N]);
1097 /* A malloc may have failed inside of the xFunc() call. If this 1109 /* A malloc may have failed inside of the xFunc() call. If this
1098 ** is the case, clear the mallocFailed flag and return NULL. 1110 ** is the case, clear the mallocFailed flag and return NULL.
1099 */ 1111 */
1100 if( db->mallocFailed ){ 1112 if( db->mallocFailed ){
1101 db->mallocFailed = 0; 1113 sqlite3OomClear(db);
1102 ret = 0; 1114 ret = 0;
1103 } 1115 }
1104 sqlite3_mutex_leave(db->mutex); 1116 sqlite3_mutex_leave(db->mutex);
1105 } 1117 }
1106 return ret; 1118 return ret;
1107 } 1119 }
1108 1120
1109 /* 1121 /*
1110 ** Return the name of the Nth column of the result set returned by SQL 1122 ** Return the name of the Nth column of the result set returned by SQL
1111 ** statement pStmt. 1123 ** statement pStmt.
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 /* 1303 /*
1292 ** Bind a blob value to an SQL statement variable. 1304 ** Bind a blob value to an SQL statement variable.
1293 */ 1305 */
1294 int sqlite3_bind_blob( 1306 int sqlite3_bind_blob(
1295 sqlite3_stmt *pStmt, 1307 sqlite3_stmt *pStmt,
1296 int i, 1308 int i,
1297 const void *zData, 1309 const void *zData,
1298 int nData, 1310 int nData,
1299 void (*xDel)(void*) 1311 void (*xDel)(void*)
1300 ){ 1312 ){
1313 #ifdef SQLITE_ENABLE_API_ARMOR
1314 if( nData<0 ) return SQLITE_MISUSE_BKPT;
1315 #endif
1301 return bindText(pStmt, i, zData, nData, xDel, 0); 1316 return bindText(pStmt, i, zData, nData, xDel, 0);
1302 } 1317 }
1303 int sqlite3_bind_blob64( 1318 int sqlite3_bind_blob64(
1304 sqlite3_stmt *pStmt, 1319 sqlite3_stmt *pStmt,
1305 int i, 1320 int i,
1306 const void *zData, 1321 const void *zData,
1307 sqlite3_uint64 nData, 1322 sqlite3_uint64 nData,
1308 void (*xDel)(void*) 1323 void (*xDel)(void*)
1309 ){ 1324 ){
1310 assert( xDel!=SQLITE_DYNAMIC ); 1325 assert( xDel!=SQLITE_DYNAMIC );
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1448 } 1463 }
1449 1464
1450 /* 1465 /*
1451 ** Return the name of a wildcard parameter. Return NULL if the index 1466 ** Return the name of a wildcard parameter. Return NULL if the index
1452 ** is out of range or if the wildcard is unnamed. 1467 ** is out of range or if the wildcard is unnamed.
1453 ** 1468 **
1454 ** The result is always UTF-8. 1469 ** The result is always UTF-8.
1455 */ 1470 */
1456 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1471 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
1457 Vdbe *p = (Vdbe*)pStmt; 1472 Vdbe *p = (Vdbe*)pStmt;
1458 if( p==0 || i<1 || i>p->nzVar ){ 1473 if( p==0 ) return 0;
1459 return 0; 1474 return sqlite3VListNumToName(p->pVList, i);
1460 }
1461 return p->azVar[i-1];
1462 } 1475 }
1463 1476
1464 /* 1477 /*
1465 ** Given a wildcard parameter name, return the index of the variable 1478 ** Given a wildcard parameter name, return the index of the variable
1466 ** with that name. If there is no variable with the given name, 1479 ** with that name. If there is no variable with the given name,
1467 ** return 0. 1480 ** return 0.
1468 */ 1481 */
1469 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 1482 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
1470 int i; 1483 if( p==0 || zName==0 ) return 0;
1471 if( p==0 ){ 1484 return sqlite3VListNameToNum(p->pVList, zName, nName);
1472 return 0;
1473 }
1474 if( zName ){
1475 for(i=0; i<p->nzVar; i++){
1476 const char *z = p->azVar[i];
1477 if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
1478 return i+1;
1479 }
1480 }
1481 }
1482 return 0;
1483 } 1485 }
1484 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 1486 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
1485 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 1487 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
1486 } 1488 }
1487 1489
1488 /* 1490 /*
1489 ** Transfer all bindings from the first statement over to the second. 1491 ** Transfer all bindings from the first statement over to the second.
1490 */ 1492 */
1491 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1493 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
1492 Vdbe *pFrom = (Vdbe*)pFromStmt; 1494 Vdbe *pFrom = (Vdbe*)pFromStmt;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1547 */ 1549 */
1548 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 1550 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
1549 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 1551 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
1550 } 1552 }
1551 1553
1552 /* 1554 /*
1553 ** Return true if the prepared statement is in need of being reset. 1555 ** Return true if the prepared statement is in need of being reset.
1554 */ 1556 */
1555 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 1557 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
1556 Vdbe *v = (Vdbe*)pStmt; 1558 Vdbe *v = (Vdbe*)pStmt;
1557 return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN; 1559 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
1558 } 1560 }
1559 1561
1560 /* 1562 /*
1561 ** Return a pointer to the next prepared statement after pStmt associated 1563 ** Return a pointer to the next prepared statement after pStmt associated
1562 ** with database connection pDb. If pStmt is NULL, return the first 1564 ** with database connection pDb. If pStmt is NULL, return the first
1563 ** prepared statement for the database connection. Return NULL if there 1565 ** prepared statement for the database connection. Return NULL if there
1564 ** are no more. 1566 ** are no more.
1565 */ 1567 */
1566 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1568 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
1567 sqlite3_stmt *pNext; 1569 sqlite3_stmt *pNext;
(...skipping 23 matching lines...) Expand all
1591 if( !pStmt ){ 1593 if( !pStmt ){
1592 (void)SQLITE_MISUSE_BKPT; 1594 (void)SQLITE_MISUSE_BKPT;
1593 return 0; 1595 return 0;
1594 } 1596 }
1595 #endif 1597 #endif
1596 v = pVdbe->aCounter[op]; 1598 v = pVdbe->aCounter[op];
1597 if( resetFlag ) pVdbe->aCounter[op] = 0; 1599 if( resetFlag ) pVdbe->aCounter[op] = 0;
1598 return (int)v; 1600 return (int)v;
1599 } 1601 }
1600 1602
1603 /*
1604 ** Return the SQL associated with a prepared statement
1605 */
1606 const char *sqlite3_sql(sqlite3_stmt *pStmt){
1607 Vdbe *p = (Vdbe *)pStmt;
1608 return p ? p->zSql : 0;
1609 }
1610
1611 /*
1612 ** Return the SQL associated with a prepared statement with
1613 ** bound parameters expanded. Space to hold the returned string is
1614 ** obtained from sqlite3_malloc(). The caller is responsible for
1615 ** freeing the returned string by passing it to sqlite3_free().
1616 **
1617 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
1618 ** expanded bound parameters.
1619 */
1620 char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
1621 #ifdef SQLITE_OMIT_TRACE
1622 return 0;
1623 #else
1624 char *z = 0;
1625 const char *zSql = sqlite3_sql(pStmt);
1626 if( zSql ){
1627 Vdbe *p = (Vdbe *)pStmt;
1628 sqlite3_mutex_enter(p->db->mutex);
1629 z = sqlite3VdbeExpandSql(p, zSql);
1630 sqlite3_mutex_leave(p->db->mutex);
1631 }
1632 return z;
1633 #endif
1634 }
1635
1636 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1637 /*
1638 ** Allocate and populate an UnpackedRecord structure based on the serialized
1639 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
1640 ** if successful, or a NULL pointer if an OOM error is encountered.
1641 */
1642 static UnpackedRecord *vdbeUnpackRecord(
1643 KeyInfo *pKeyInfo,
1644 int nKey,
1645 const void *pKey
1646 ){
1647 UnpackedRecord *pRet; /* Return value */
1648
1649 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
1650 if( pRet ){
1651 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
1652 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
1653 }
1654 return pRet;
1655 }
1656
1657 /*
1658 ** This function is called from within a pre-update callback to retrieve
1659 ** a field of the row currently being updated or deleted.
1660 */
1661 int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
1662 PreUpdate *p = db->pPreUpdate;
1663 Mem *pMem;
1664 int rc = SQLITE_OK;
1665
1666 /* Test that this call is being made from within an SQLITE_DELETE or
1667 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
1668 if( !p || p->op==SQLITE_INSERT ){
1669 rc = SQLITE_MISUSE_BKPT;
1670 goto preupdate_old_out;
1671 }
1672 if( p->pPk ){
1673 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1674 }
1675 if( iIdx>=p->pCsr->nField || iIdx<0 ){
1676 rc = SQLITE_RANGE;
1677 goto preupdate_old_out;
1678 }
1679
1680 /* If the old.* record has not yet been loaded into memory, do so now. */
1681 if( p->pUnpacked==0 ){
1682 u32 nRec;
1683 u8 *aRec;
1684
1685 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
1686 aRec = sqlite3DbMallocRaw(db, nRec);
1687 if( !aRec ) goto preupdate_old_out;
1688 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
1689 if( rc==SQLITE_OK ){
1690 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
1691 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
1692 }
1693 if( rc!=SQLITE_OK ){
1694 sqlite3DbFree(db, aRec);
1695 goto preupdate_old_out;
1696 }
1697 p->aRecord = aRec;
1698 }
1699
1700 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
1701 if( iIdx==p->pTab->iPKey ){
1702 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
1703 }else if( iIdx>=p->pUnpacked->nField ){
1704 *ppValue = (sqlite3_value *)columnNullValue();
1705 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
1706 if( pMem->flags & MEM_Int ){
1707 sqlite3VdbeMemRealify(pMem);
1708 }
1709 }
1710
1711 preupdate_old_out:
1712 sqlite3Error(db, rc);
1713 return sqlite3ApiExit(db, rc);
1714 }
1715 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
1716
1717 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1718 /*
1719 ** This function is called from within a pre-update callback to retrieve
1720 ** the number of columns in the row being updated, deleted or inserted.
1721 */
1722 int sqlite3_preupdate_count(sqlite3 *db){
1723 PreUpdate *p = db->pPreUpdate;
1724 return (p ? p->keyinfo.nField : 0);
1725 }
1726 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
1727
1728 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1729 /*
1730 ** This function is designed to be called from within a pre-update callback
1731 ** only. It returns zero if the change that caused the callback was made
1732 ** immediately by a user SQL statement. Or, if the change was made by a
1733 ** trigger program, it returns the number of trigger programs currently
1734 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
1735 ** top-level trigger etc.).
1736 **
1737 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
1738 ** or SET DEFAULT action is considered a trigger.
1739 */
1740 int sqlite3_preupdate_depth(sqlite3 *db){
1741 PreUpdate *p = db->pPreUpdate;
1742 return (p ? p->v->nFrame : 0);
1743 }
1744 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
1745
1746 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1747 /*
1748 ** This function is called from within a pre-update callback to retrieve
1749 ** a field of the row currently being updated or inserted.
1750 */
1751 int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
1752 PreUpdate *p = db->pPreUpdate;
1753 int rc = SQLITE_OK;
1754 Mem *pMem;
1755
1756 if( !p || p->op==SQLITE_DELETE ){
1757 rc = SQLITE_MISUSE_BKPT;
1758 goto preupdate_new_out;
1759 }
1760 if( p->pPk && p->op!=SQLITE_UPDATE ){
1761 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
1762 }
1763 if( iIdx>=p->pCsr->nField || iIdx<0 ){
1764 rc = SQLITE_RANGE;
1765 goto preupdate_new_out;
1766 }
1767
1768 if( p->op==SQLITE_INSERT ){
1769 /* For an INSERT, memory cell p->iNewReg contains the serialized record
1770 ** that is being inserted. Deserialize it. */
1771 UnpackedRecord *pUnpack = p->pNewUnpacked;
1772 if( !pUnpack ){
1773 Mem *pData = &p->v->aMem[p->iNewReg];
1774 rc = ExpandBlob(pData);
1775 if( rc!=SQLITE_OK ) goto preupdate_new_out;
1776 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
1777 if( !pUnpack ){
1778 rc = SQLITE_NOMEM;
1779 goto preupdate_new_out;
1780 }
1781 p->pNewUnpacked = pUnpack;
1782 }
1783 pMem = &pUnpack->aMem[iIdx];
1784 if( iIdx==p->pTab->iPKey ){
1785 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1786 }else if( iIdx>=pUnpack->nField ){
1787 pMem = (sqlite3_value *)columnNullValue();
1788 }
1789 }else{
1790 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
1791 ** value. Make a copy of the cell contents and return a pointer to it.
1792 ** It is not safe to return a pointer to the memory cell itself as the
1793 ** caller may modify the value text encoding.
1794 */
1795 assert( p->op==SQLITE_UPDATE );
1796 if( !p->aNew ){
1797 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
1798 if( !p->aNew ){
1799 rc = SQLITE_NOMEM;
1800 goto preupdate_new_out;
1801 }
1802 }
1803 assert( iIdx>=0 && iIdx<p->pCsr->nField );
1804 pMem = &p->aNew[iIdx];
1805 if( pMem->flags==0 ){
1806 if( iIdx==p->pTab->iPKey ){
1807 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
1808 }else{
1809 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
1810 if( rc!=SQLITE_OK ) goto preupdate_new_out;
1811 }
1812 }
1813 }
1814 *ppValue = pMem;
1815
1816 preupdate_new_out:
1817 sqlite3Error(db, rc);
1818 return sqlite3ApiExit(db, rc);
1819 }
1820 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
1821
1601 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 1822 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1602 /* 1823 /*
1603 ** Return status data for a single loop within query pStmt. 1824 ** Return status data for a single loop within query pStmt.
1604 */ 1825 */
1605 int sqlite3_stmt_scanstatus( 1826 int sqlite3_stmt_scanstatus(
1606 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 1827 sqlite3_stmt *pStmt, /* Prepared statement being queried */
1607 int idx, /* Index of loop to report on */ 1828 int idx, /* Index of loop to report on */
1608 int iScanStatusOp, /* Which metric to return */ 1829 int iScanStatusOp, /* Which metric to return */
1609 void *pOut /* OUT: Write the answer here */ 1830 void *pOut /* OUT: Write the answer here */
1610 ){ 1831 ){
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1659 } 1880 }
1660 1881
1661 /* 1882 /*
1662 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 1883 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
1663 */ 1884 */
1664 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 1885 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
1665 Vdbe *p = (Vdbe*)pStmt; 1886 Vdbe *p = (Vdbe*)pStmt;
1666 memset(p->anExec, 0, p->nOp * sizeof(i64)); 1887 memset(p->anExec, 0, p->nOp * sizeof(i64));
1667 } 1888 }
1668 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ 1889 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
OLDNEW
« no previous file with comments | « third_party/sqlite/src/src/vdbeInt.h ('k') | third_party/sqlite/src/src/vdbeaux.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698