16
16
#include "sqliteInt.h"
17
17
#include "vdbeInt.h"
19
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
21
** The following structure contains pointers to the end points of a
22
** doubly-linked list of all compiled SQL statements that may be holding
23
** buffers eligible for release when the sqlite3_release_memory() interface is
24
** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
27
** Statements are added to the end of this list when sqlite3_reset() is
28
** called. They are removed either when sqlite3_step() or sqlite3_finalize()
29
** is called. When statements are added to this list, the associated
30
** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
31
** can be freed using sqlite3VdbeReleaseMemory().
33
** When statements are added or removed from this list, the mutex
34
** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
35
** already held. The LRU2 mutex is then obtained, blocking if necessary,
36
** the linked-list pointers manipulated and the LRU2 mutex relinquished.
38
struct StatementLruList {
42
static struct StatementLruList sqlite3LruStatements;
45
** Check that the list looks to be internally consistent. This is used
46
** as part of an assert() statement as follows:
48
** assert( stmtLruCheck() );
51
static int stmtLruCheck(){
53
for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
54
assert(p->pLruNext || p==sqlite3LruStatements.pLast);
55
assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
56
assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
57
assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
64
** Add vdbe p to the end of the statement lru list. It is assumed that
65
** p is not already part of the list when this is called. The lru list
66
** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
68
static void stmtLruAdd(Vdbe *p){
69
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
71
if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
72
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
76
assert( stmtLruCheck() );
78
if( !sqlite3LruStatements.pFirst ){
79
assert( !sqlite3LruStatements.pLast );
80
sqlite3LruStatements.pFirst = p;
81
sqlite3LruStatements.pLast = p;
83
assert( !sqlite3LruStatements.pLast->pLruNext );
84
p->pLruPrev = sqlite3LruStatements.pLast;
85
sqlite3LruStatements.pLast->pLruNext = p;
86
sqlite3LruStatements.pLast = p;
89
assert( stmtLruCheck() );
91
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
95
** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
96
** statement p from the least-recently-used statement list. If the
97
** statement is not currently part of the list, this call is a no-op.
99
static void stmtLruRemoveNomutex(Vdbe *p){
100
if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
101
assert( stmtLruCheck() );
103
p->pLruNext->pLruPrev = p->pLruPrev;
105
sqlite3LruStatements.pLast = p->pLruPrev;
108
p->pLruPrev->pLruNext = p->pLruNext;
110
sqlite3LruStatements.pFirst = p->pLruNext;
114
assert( stmtLruCheck() );
119
** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
120
** statement p from the least-recently-used statement list. If the
121
** statement is not currently part of the list, this call is a no-op.
123
static void stmtLruRemove(Vdbe *p){
124
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
125
stmtLruRemoveNomutex(p);
126
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
130
** Try to release n bytes of memory by freeing buffers associated
131
** with the memory registers of currently unused vdbes.
133
int sqlite3VdbeReleaseMemory(int n){
138
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
139
for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
142
/* For each statement handle in the lru list, attempt to obtain the
143
** associated database mutex. If it cannot be obtained, continue
144
** to the next statement handle. It is not possible to block on
145
** the database mutex - that could cause deadlock.
147
if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
148
nFree += sqlite3VdbeReleaseBuffers(p);
149
stmtLruRemoveNomutex(p);
150
sqlite3_mutex_leave(p->db->mutex);
153
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
159
** Call sqlite3Reprepare() on the statement. Remove it from the
160
** lru list before doing so, as Reprepare() will free all the
161
** memory register buffers anyway.
163
int vdbeReprepare(Vdbe *p){
165
return sqlite3Reprepare(p);
168
#else /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
169
#define stmtLruRemove(x)
170
#define stmtLruAdd(x)
171
#define vdbeReprepare(x) sqlite3Reprepare(x)
21
176
** Return TRUE (non-zero) of the statement supplied as an argument needs
30
185
return p==0 || p->expired;
189
** The following routine destroys a virtual machine that is created by
190
** the sqlite3_compile() routine. The integer returned is an SQLITE_
191
** success/failure code that describes the result of executing the virtual
194
** This routine sets the error code and string returned by
195
** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
197
int sqlite3_finalize(sqlite3_stmt *pStmt){
202
Vdbe *v = (Vdbe*)pStmt;
203
#ifndef SQLITE_MUTEX_NOOP
204
sqlite3_mutex *mutex = v->db->mutex;
206
sqlite3_mutex_enter(mutex);
208
rc = sqlite3VdbeFinalize(v);
209
sqlite3_mutex_leave(mutex);
215
** Terminate the current execution of an SQL statement and reset it
216
** back to its starting state so that it can be reused. A success code from
217
** the prior execution is returned.
219
** This routine sets the error code and string returned by
220
** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
222
int sqlite3_reset(sqlite3_stmt *pStmt){
227
Vdbe *v = (Vdbe*)pStmt;
228
sqlite3_mutex_enter(v->db->mutex);
229
rc = sqlite3VdbeReset(v, 1);
231
sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
232
assert( (rc & (v->db->errMask))==rc );
233
sqlite3_mutex_leave(v->db->mutex);
239
** Set all the parameters in the compiled SQL statement to NULL.
241
int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
244
Vdbe *p = (Vdbe*)pStmt;
245
#ifndef SQLITE_MUTEX_NOOP
246
sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
248
sqlite3_mutex_enter(mutex);
249
for(i=0; i<p->nVar; i++){
250
sqlite3VdbeMemRelease(&p->aVar[i]);
251
p->aVar[i].flags = MEM_Null;
253
sqlite3_mutex_leave(mutex);
33
258
/**************************** sqlite3_value_ *******************************
34
259
** The following routines extract information from a Mem or sqlite3_value
87
314
void (*xDel)(void *)
317
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
90
318
sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
92
320
void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
321
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
93
322
sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
95
324
void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
325
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
326
pCtx->isError = SQLITE_ERROR;
97
327
sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
99
329
#ifndef SQLITE_OMIT_UTF16
100
330
void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
331
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
332
pCtx->isError = SQLITE_ERROR;
102
333
sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
105
336
void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
337
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
106
338
sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
108
340
void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
341
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
109
342
sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
111
344
void sqlite3_result_null(sqlite3_context *pCtx){
345
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
112
346
sqlite3VdbeMemSetNull(&pCtx->s);
114
348
void sqlite3_result_text(
143
380
void (*xDel)(void *)
382
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
145
383
sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
147
385
#endif /* SQLITE_OMIT_UTF16 */
148
386
void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
387
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
149
388
sqlite3VdbeMemCopy(&pCtx->s, pValue);
390
void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
391
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
392
sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
394
void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
395
pCtx->isError = errCode;
398
/* Force an SQLITE_TOOBIG error. */
399
void sqlite3_result_error_toobig(sqlite3_context *pCtx){
400
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
401
pCtx->isError = SQLITE_TOOBIG;
402
sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
403
SQLITE_UTF8, SQLITE_STATIC);
406
/* An SQLITE_NOMEM error. */
407
void sqlite3_result_error_nomem(sqlite3_context *pCtx){
408
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
409
sqlite3VdbeMemSetNull(&pCtx->s);
410
pCtx->isError = SQLITE_NOMEM;
411
pCtx->s.db->mallocFailed = 1;
154
415
** Execute the statement pStmt, either until a row of data is ready, the
155
416
** statement is completely executed or an error occurs.
418
** This routine implements the bulk of the logic behind the sqlite_step()
419
** API. The only thing omitted is the automatic recompile if a
420
** schema change has occurred. That detail is handled by the
421
** outer sqlite3_step() wrapper procedure.
157
int sqlite3_step(sqlite3_stmt *pStmt){
158
Vdbe *p = (Vdbe*)pStmt;
423
static int sqlite3Step(Vdbe *p){
428
if( p->magic!=VDBE_MAGIC_RUN ){
429
return SQLITE_MISUSE;
162
432
/* Assert that malloc() has not failed */
163
assert( !sqlite3MallocFailed() );
434
assert( !db->mallocFailed );
165
if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
166
return SQLITE_MISUSE;
168
436
if( p->aborted ){
169
437
return SQLITE_ABORT;
191
459
#ifndef SQLITE_OMIT_TRACE
192
/* Invoke the trace callback if there is one
194
if( db->xTrace && !db->init.busy ){
196
assert( p->aOp[p->nOp-1].opcode==OP_Noop );
197
assert( p->aOp[p->nOp-1].p3!=0 );
198
assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
199
sqlite3SafetyOff(db);
200
db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
201
if( sqlite3SafetyOn(db) ){
202
p->rc = SQLITE_MISUSE;
203
return SQLITE_MISUSE;
206
460
if( db->xProfile && !db->init.busy ){
208
sqlite3OsCurrentTime(&rNow);
462
sqlite3OsCurrentTime(db->pVfs, &rNow);
209
463
p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
213
/* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
214
** on in debugging mode.
217
if( (db->flags & SQLITE_SqlTrace)!=0 ){
218
sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
220
#endif /* SQLITE_DEBUG */
222
467
db->activeVdbeCnt++;
225
471
#ifndef SQLITE_OMIT_EXPLAIN
226
472
if( p->explain ){
238
484
#ifndef SQLITE_OMIT_TRACE
239
485
/* Invoke the profile callback if there is one
241
if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
487
if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0
488
&& p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){
245
sqlite3OsCurrentTime(&rNow);
492
sqlite3OsCurrentTime(db->pVfs, &rNow);
246
493
elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
248
assert( p->aOp[p->nOp-1].opcode==OP_Noop );
249
assert( p->aOp[p->nOp-1].p3!=0 );
250
assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
251
db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
494
db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime);
255
498
sqlite3Error(p->db, rc, 0);
256
499
p->rc = sqlite3ApiExit(p->db, p->rc);
257
501
assert( (rc&0xff)==rc );
502
if( p->zSql && (rc&0xff)<SQLITE_ROW ){
503
/* This behavior occurs if sqlite3_prepare_v2() was used to build
504
** the prepared statement. Return error codes directly */
505
sqlite3Error(p->db, p->rc, 0);
508
/* This is for legacy sqlite3_prepare() builds and when the code
509
** is SQLITE_ROW or SQLITE_DONE */
515
** This is the top-level implementation of sqlite3_step(). Call
516
** sqlite3Step() to do most of the work. If a schema error occurs,
517
** call sqlite3Reprepare() and try again.
519
#ifdef SQLITE_OMIT_PARSER
520
int sqlite3_step(sqlite3_stmt *pStmt){
521
int rc = SQLITE_MISUSE;
525
sqlite3_mutex_enter(v->db->mutex);
527
sqlite3_mutex_leave(v->db->mutex);
532
int sqlite3_step(sqlite3_stmt *pStmt){
533
int rc = SQLITE_MISUSE;
536
Vdbe *v = (Vdbe*)pStmt;
538
sqlite3_mutex_enter(db->mutex);
539
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
541
&& vdbeReprepare(v) ){
542
sqlite3_reset(pStmt);
545
if( rc==SQLITE_SCHEMA && v->zSql && db->pErr ){
546
/* This case occurs after failing to recompile an sql statement.
547
** The error message from the SQL compiler has already been loaded
548
** into the database handle. This block copies the error message
549
** from the database handle into the statement and sets the statement
550
** program counter to 0 to ensure that when the statement is
551
** finalized or reset the parser error message is available via
552
** sqlite3_errmsg() and sqlite3_errcode().
554
const char *zErr = (const char *)sqlite3_value_text(db->pErr);
555
sqlite3_free(v->zErrMsg);
556
if( !db->mallocFailed ){
557
v->zErrMsg = sqlite3DbStrDup(db, zErr);
560
v->rc = SQLITE_NOMEM;
563
rc = sqlite3ApiExit(db, rc);
564
sqlite3_mutex_leave(db->mutex);
262
571
** Extract the user data from a sqlite3_context structure and return a
341
662
struct AuxData *pAuxData;
342
663
VdbeFunc *pVdbeFunc;
664
if( iArg<0 ) goto failed;
666
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
345
667
pVdbeFunc = pCtx->pVdbeFunc;
346
668
if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
669
int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
347
670
int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
348
pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
349
if( !pVdbeFunc ) return;
671
pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
350
675
pCtx->pVdbeFunc = pVdbeFunc;
351
memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
352
sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
676
memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
353
677
pVdbeFunc->nAux = iArg+1;
354
678
pVdbeFunc->pFunc = pCtx->pFunc;
404
734
static Mem *columnMem(sqlite3_stmt *pStmt, int i){
405
Vdbe *pVm = (Vdbe *)pStmt;
406
int vals = sqlite3_data_count(pStmt);
407
if( i>=vals || i<0 ){
408
static const Mem nullMem = {0, 0.0, "", 0, MEM_Null, MEM_Null };
409
sqlite3Error(pVm->db, SQLITE_RANGE, 0);
410
return (Mem*)&nullMem;
740
if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
741
sqlite3_mutex_enter(pVm->db->mutex);
742
vals = sqlite3_data_count(pStmt);
743
pOut = &pVm->pResultSet[i];
745
static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
747
sqlite3_mutex_enter(pVm->db->mutex);
748
sqlite3Error(pVm->db, SQLITE_RANGE, 0);
750
pOut = (Mem*)&nullMem;
412
return &pVm->pTos[(1-vals)+i];
658
1027
** Bind a text or BLOB value.
660
1029
static int bindText(
1030
sqlite3_stmt *pStmt, /* The statement to bind against */
1031
int i, /* Index of the parameter to bind */
1032
const void *zData, /* Pointer to the data to be bound */
1033
int nData, /* Number of bytes of data to be bound */
1034
void (*xDel)(void*), /* Destructor for the data */
1035
int encoding /* Encoding for the data */
668
1037
Vdbe *p = (Vdbe *)pStmt;
1042
return SQLITE_MISUSE;
1044
sqlite3_mutex_enter(p->db->mutex);
672
1045
rc = vdbeUnbind(p, i);
673
if( rc || zData==0 ){
676
pVar = &p->aVar[i-1];
677
rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
678
if( rc==SQLITE_OK && encoding!=0 ){
679
rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
682
sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
683
return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc);
1046
if( rc==SQLITE_OK && zData!=0 ){
1047
pVar = &p->aVar[i-1];
1048
rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
1049
if( rc==SQLITE_OK && encoding!=0 ){
1050
rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
1052
sqlite3Error(p->db, rc, 0);
1053
rc = sqlite3ApiExit(p->db, rc);
1055
sqlite3_mutex_leave(p->db->mutex);