~ubuntu-branches/ubuntu/maverick/sqlite3/maverick-updates

« back to all changes in this revision

Viewing changes to src/vdbeapi.c

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2008-10-01 20:16:18 UTC
  • mfrom: (3.1.20 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081001201618-yfvqqj1qs29wdtcc
Tags: 3.5.9-5
Backport fix for distinct on indexes (closes: #500792).

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
*/
16
16
#include "sqliteInt.h"
17
17
#include "vdbeInt.h"
18
 
#include "os.h"
 
18
 
 
19
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 
20
/*
 
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
 
25
** mutex.
 
26
**
 
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().
 
32
**
 
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.
 
37
*/
 
38
struct StatementLruList {
 
39
  Vdbe *pFirst;
 
40
  Vdbe *pLast;
 
41
};
 
42
static struct StatementLruList sqlite3LruStatements;
 
43
 
 
44
/*
 
45
** Check that the list looks to be internally consistent. This is used
 
46
** as part of an assert() statement as follows:
 
47
**
 
48
**   assert( stmtLruCheck() );
 
49
*/
 
50
#ifndef NDEBUG
 
51
static int stmtLruCheck(){
 
52
  Vdbe *p;
 
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);
 
58
  }
 
59
  return 1;
 
60
}
 
61
#endif
 
62
 
 
63
/*
 
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.
 
67
*/
 
68
static void stmtLruAdd(Vdbe *p){
 
69
  sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
 
70
 
 
71
  if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
 
72
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
 
73
    return;
 
74
  }
 
75
 
 
76
  assert( stmtLruCheck() );
 
77
 
 
78
  if( !sqlite3LruStatements.pFirst ){
 
79
    assert( !sqlite3LruStatements.pLast );
 
80
    sqlite3LruStatements.pFirst = p;
 
81
    sqlite3LruStatements.pLast = p;
 
82
  }else{
 
83
    assert( !sqlite3LruStatements.pLast->pLruNext );
 
84
    p->pLruPrev = sqlite3LruStatements.pLast;
 
85
    sqlite3LruStatements.pLast->pLruNext = p;
 
86
    sqlite3LruStatements.pLast = p;
 
87
  }
 
88
 
 
89
  assert( stmtLruCheck() );
 
90
 
 
91
  sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
 
92
}
 
93
 
 
94
/*
 
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.
 
98
*/
 
99
static void stmtLruRemoveNomutex(Vdbe *p){
 
100
  if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
 
101
    assert( stmtLruCheck() );
 
102
    if( p->pLruNext ){
 
103
      p->pLruNext->pLruPrev = p->pLruPrev;
 
104
    }else{
 
105
      sqlite3LruStatements.pLast = p->pLruPrev;
 
106
    }
 
107
    if( p->pLruPrev ){
 
108
      p->pLruPrev->pLruNext = p->pLruNext;
 
109
    }else{
 
110
      sqlite3LruStatements.pFirst = p->pLruNext;
 
111
    }
 
112
    p->pLruNext = 0;
 
113
    p->pLruPrev = 0;
 
114
    assert( stmtLruCheck() );
 
115
  }
 
116
}
 
117
 
 
118
/*
 
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.
 
122
*/
 
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));
 
127
}
 
128
 
 
129
/*
 
130
** Try to release n bytes of memory by freeing buffers associated 
 
131
** with the memory registers of currently unused vdbes.
 
132
*/
 
133
int sqlite3VdbeReleaseMemory(int n){
 
134
  Vdbe *p;
 
135
  Vdbe *pNext;
 
136
  int nFree = 0;
 
137
 
 
138
  sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
 
139
  for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
 
140
    pNext = p->pLruNext;
 
141
 
 
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.
 
146
    */
 
147
    if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
 
148
      nFree += sqlite3VdbeReleaseBuffers(p);
 
149
      stmtLruRemoveNomutex(p);
 
150
      sqlite3_mutex_leave(p->db->mutex);
 
151
    }
 
152
  }
 
153
  sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU2));
 
154
 
 
155
  return nFree;
 
156
}
 
157
 
 
158
/*
 
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.
 
162
*/
 
163
int vdbeReprepare(Vdbe *p){
 
164
  stmtLruRemove(p);
 
165
  return sqlite3Reprepare(p);
 
166
}
 
167
 
 
168
#else       /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
 
169
  #define stmtLruRemove(x)
 
170
  #define stmtLruAdd(x)
 
171
  #define vdbeReprepare(x) sqlite3Reprepare(x)
 
172
#endif
 
173
 
19
174
 
20
175
/*
21
176
** Return TRUE (non-zero) of the statement supplied as an argument needs
30
185
  return p==0 || p->expired;
31
186
}
32
187
 
 
188
/*
 
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
 
192
** machine.
 
193
**
 
194
** This routine sets the error code and string returned by
 
195
** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
 
196
*/
 
197
int sqlite3_finalize(sqlite3_stmt *pStmt){
 
198
  int rc;
 
199
  if( pStmt==0 ){
 
200
    rc = SQLITE_OK;
 
201
  }else{
 
202
    Vdbe *v = (Vdbe*)pStmt;
 
203
#ifndef SQLITE_MUTEX_NOOP
 
204
    sqlite3_mutex *mutex = v->db->mutex;
 
205
#endif
 
206
    sqlite3_mutex_enter(mutex);
 
207
    stmtLruRemove(v);
 
208
    rc = sqlite3VdbeFinalize(v);
 
209
    sqlite3_mutex_leave(mutex);
 
210
  }
 
211
  return rc;
 
212
}
 
213
 
 
214
/*
 
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.
 
218
**
 
219
** This routine sets the error code and string returned by
 
220
** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
 
221
*/
 
222
int sqlite3_reset(sqlite3_stmt *pStmt){
 
223
  int rc;
 
224
  if( pStmt==0 ){
 
225
    rc = SQLITE_OK;
 
226
  }else{
 
227
    Vdbe *v = (Vdbe*)pStmt;
 
228
    sqlite3_mutex_enter(v->db->mutex);
 
229
    rc = sqlite3VdbeReset(v, 1);
 
230
    stmtLruAdd(v);
 
231
    sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
 
232
    assert( (rc & (v->db->errMask))==rc );
 
233
    sqlite3_mutex_leave(v->db->mutex);
 
234
  }
 
235
  return rc;
 
236
}
 
237
 
 
238
/*
 
239
** Set all the parameters in the compiled SQL statement to NULL.
 
240
*/
 
241
int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
 
242
  int i;
 
243
  int rc = SQLITE_OK;
 
244
  Vdbe *p = (Vdbe*)pStmt;
 
245
#ifndef SQLITE_MUTEX_NOOP
 
246
  sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
 
247
#endif
 
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;
 
252
  }
 
253
  sqlite3_mutex_leave(mutex);
 
254
  return rc;
 
255
}
 
256
 
 
257
 
33
258
/**************************** sqlite3_value_  *******************************
34
259
** The following routines extract information from a Mem or sqlite3_value
35
260
** structure.
37
262
const void *sqlite3_value_blob(sqlite3_value *pVal){
38
263
  Mem *p = (Mem*)pVal;
39
264
  if( p->flags & (MEM_Blob|MEM_Str) ){
 
265
    sqlite3VdbeMemExpandBlob(p);
 
266
    p->flags &= ~MEM_Str;
 
267
    p->flags |= MEM_Blob;
40
268
    return p->z;
41
269
  }else{
42
270
    return sqlite3_value_text(pVal);
74
302
int sqlite3_value_type(sqlite3_value* pVal){
75
303
  return pVal->type;
76
304
}
77
 
/* sqlite3_value_numeric_type() defined in vdbe.c */
78
305
 
79
306
/**************************** sqlite3_result_  *******************************
80
307
** The following routines are used by user-defined functions to specify
87
314
  void (*xDel)(void *)
88
315
){
89
316
  assert( n>=0 );
 
317
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
90
318
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
91
319
}
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);
94
323
}
95
324
void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
96
 
  pCtx->isError = 1;
 
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);
98
328
}
99
329
#ifndef SQLITE_OMIT_UTF16
100
330
void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
101
 
  pCtx->isError = 1;
 
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);
103
334
}
104
335
#endif
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);
107
339
}
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);
110
343
}
111
344
void sqlite3_result_null(sqlite3_context *pCtx){
 
345
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
112
346
  sqlite3VdbeMemSetNull(&pCtx->s);
113
347
}
114
348
void sqlite3_result_text(
117
351
  int n,
118
352
  void (*xDel)(void *)
119
353
){
 
354
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
120
355
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
121
356
}
122
357
#ifndef SQLITE_OMIT_UTF16
126
361
  int n, 
127
362
  void (*xDel)(void *)
128
363
){
 
364
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
129
365
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
130
366
}
131
367
void sqlite3_result_text16be(
134
370
  int n, 
135
371
  void (*xDel)(void *)
136
372
){
 
373
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
137
374
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
138
375
}
139
376
void sqlite3_result_text16le(
142
379
  int n, 
143
380
  void (*xDel)(void *)
144
381
){
 
382
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
145
383
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
146
384
}
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);
150
389
}
151
 
 
 
390
void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
 
391
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 
392
  sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
 
393
}
 
394
void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
 
395
  pCtx->isError = errCode;
 
396
}
 
397
 
 
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);
 
404
}
 
405
 
 
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;
 
412
}
152
413
 
153
414
/*
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.
 
417
**
 
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.
156
422
*/
157
 
int sqlite3_step(sqlite3_stmt *pStmt){
158
 
  Vdbe *p = (Vdbe*)pStmt;
 
423
static int sqlite3Step(Vdbe *p){
159
424
  sqlite3 *db;
160
425
  int rc;
161
426
 
 
427
  assert(p);
 
428
  if( p->magic!=VDBE_MAGIC_RUN ){
 
429
    return SQLITE_MISUSE;
 
430
  }
 
431
 
162
432
  /* Assert that malloc() has not failed */
163
 
  assert( !sqlite3MallocFailed() );
 
433
  db = p->db;
 
434
  assert( !db->mallocFailed );
164
435
 
165
 
  if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
166
 
    return SQLITE_MISUSE;
167
 
  }
168
436
  if( p->aborted ){
169
437
    return SQLITE_ABORT;
170
438
  }
172
440
    if( p->rc==SQLITE_OK ){
173
441
      p->rc = SQLITE_SCHEMA;
174
442
    }
175
 
    return SQLITE_ERROR;
 
443
    rc = SQLITE_ERROR;
 
444
    goto end_of_step;
176
445
  }
177
 
  db = p->db;
178
446
  if( sqlite3SafetyOn(db) ){
179
447
    p->rc = SQLITE_MISUSE;
180
448
    return SQLITE_MISUSE;
189
457
    }
190
458
 
191
459
#ifndef SQLITE_OMIT_TRACE
192
 
    /* Invoke the trace callback if there is one
193
 
    */
194
 
    if( db->xTrace && !db->init.busy ){
195
 
      assert( p->nOp>0 );
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;
204
 
      }
205
 
    }
206
460
    if( db->xProfile && !db->init.busy ){
207
461
      double rNow;
208
 
      sqlite3OsCurrentTime(&rNow);
 
462
      sqlite3OsCurrentTime(db->pVfs, &rNow);
209
463
      p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
210
464
    }
211
465
#endif
212
466
 
213
 
    /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
214
 
    ** on in debugging mode.
215
 
    */
216
 
#ifdef SQLITE_DEBUG
217
 
    if( (db->flags & SQLITE_SqlTrace)!=0 ){
218
 
      sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
219
 
    }
220
 
#endif /* SQLITE_DEBUG */
221
 
 
222
467
    db->activeVdbeCnt++;
223
468
    p->pc = 0;
 
469
    stmtLruRemove(p);
224
470
  }
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
240
486
  */
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 ){
242
489
    double rNow;
243
490
    u64 elapseTime;
244
491
 
245
 
    sqlite3OsCurrentTime(&rNow);
 
492
    sqlite3OsCurrentTime(db->pVfs, &rNow);
246
493
    elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
247
 
    assert( p->nOp>0 );
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);
252
495
  }
253
496
#endif
254
497
 
255
498
  sqlite3Error(p->db, rc, 0);
256
499
  p->rc = sqlite3ApiExit(p->db, p->rc);
 
500
end_of_step:
257
501
  assert( (rc&0xff)==rc );
258
 
  return rc;
259
 
}
 
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);
 
506
    return p->rc;
 
507
  }else{
 
508
    /* This is for legacy sqlite3_prepare() builds and when the code
 
509
    ** is SQLITE_ROW or SQLITE_DONE */
 
510
    return rc;
 
511
  }
 
512
}
 
513
 
 
514
/*
 
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.
 
518
*/
 
519
#ifdef SQLITE_OMIT_PARSER
 
520
int sqlite3_step(sqlite3_stmt *pStmt){
 
521
  int rc = SQLITE_MISUSE;
 
522
  if( pStmt ){
 
523
    Vdbe *v;
 
524
    v = (Vdbe*)pStmt;
 
525
    sqlite3_mutex_enter(v->db->mutex);
 
526
    rc = sqlite3Step(v);
 
527
    sqlite3_mutex_leave(v->db->mutex);
 
528
  }
 
529
  return rc;
 
530
}
 
531
#else
 
532
int sqlite3_step(sqlite3_stmt *pStmt){
 
533
  int rc = SQLITE_MISUSE;
 
534
  if( pStmt ){
 
535
    int cnt = 0;
 
536
    Vdbe *v = (Vdbe*)pStmt;
 
537
    sqlite3 *db = v->db;
 
538
    sqlite3_mutex_enter(db->mutex);
 
539
    while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
 
540
           && cnt++ < 5
 
541
           && vdbeReprepare(v) ){
 
542
      sqlite3_reset(pStmt);
 
543
      v->expired = 0;
 
544
    }
 
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().
 
553
      */
 
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);
 
558
      } else {
 
559
        v->zErrMsg = 0;
 
560
        v->rc = SQLITE_NOMEM;
 
561
      }
 
562
    }
 
563
    rc = sqlite3ApiExit(db, rc);
 
564
    sqlite3_mutex_leave(db->mutex);
 
565
  }
 
566
  return rc;
 
567
}
 
568
#endif
260
569
 
261
570
/*
262
571
** Extract the user data from a sqlite3_context structure and return a
268
577
}
269
578
 
270
579
/*
 
580
** Extract the user data from a sqlite3_context structure and return a
 
581
** pointer to it.
 
582
*/
 
583
sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
 
584
  assert( p && p->pFunc );
 
585
  return p->s.db;
 
586
}
 
587
 
 
588
/*
271
589
** The following is the implementation of an SQL function that always
272
590
** fails with an error message stating that the function is used in the
273
591
** wrong context.  The sqlite3_overload_function() API might construct
282
600
){
283
601
  const char *zName = context->pFunc->zName;
284
602
  char *zErr;
285
 
  zErr = sqlite3MPrintf(
 
603
  zErr = sqlite3MPrintf(0,
286
604
      "unable to use function %s in the requested context", zName);
287
605
  sqlite3_result_error(context, zErr, -1);
288
 
  sqliteFree(zErr);
 
606
  sqlite3_free(zErr);
289
607
}
290
608
 
291
609
/*
294
612
** same context that was returned on prior calls.
295
613
*/
296
614
void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
297
 
  Mem *pMem = p->pMem;
 
615
  Mem *pMem;
298
616
  assert( p && p->pFunc && p->pFunc->xStep );
 
617
  assert( sqlite3_mutex_held(p->s.db->mutex) );
 
618
  pMem = p->pMem;
299
619
  if( (pMem->flags & MEM_Agg)==0 ){
300
620
    if( nByte==0 ){
301
 
      assert( pMem->flags==MEM_Null );
 
621
      sqlite3VdbeMemReleaseExternal(pMem);
 
622
      pMem->flags = MEM_Null;
302
623
      pMem->z = 0;
303
624
    }else{
 
625
      sqlite3VdbeMemGrow(pMem, nByte, 0);
304
626
      pMem->flags = MEM_Agg;
305
 
      pMem->xDel = sqlite3FreeX;
306
 
      *(FuncDef**)&pMem->i = p->pFunc;
307
 
      if( nByte<=NBFS ){
308
 
        pMem->z = pMem->zShort;
 
627
      pMem->u.pDef = p->pFunc;
 
628
      if( pMem->z ){
309
629
        memset(pMem->z, 0, nByte);
310
 
      }else{
311
 
        pMem->z = sqliteMalloc( nByte );
312
630
      }
313
631
    }
314
632
  }
320
638
** the user-function defined by pCtx.
321
639
*/
322
640
void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
323
 
  VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
 
641
  VdbeFunc *pVdbeFunc;
 
642
 
 
643
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 
644
  pVdbeFunc = pCtx->pVdbeFunc;
324
645
  if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
325
646
    return 0;
326
647
  }
340
661
){
341
662
  struct AuxData *pAuxData;
342
663
  VdbeFunc *pVdbeFunc;
343
 
  if( iArg<0 ) return;
 
664
  if( iArg<0 ) goto failed;
344
665
 
 
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);
 
672
    if( !pVdbeFunc ){
 
673
      goto failed;
 
674
    }
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;
355
679
  }
360
684
  }
361
685
  pAuxData->pAux = pAux;
362
686
  pAuxData->xDelete = xDelete;
 
687
  return;
 
688
 
 
689
failed:
 
690
  if( xDelete ){
 
691
    xDelete(pAux);
 
692
  }
363
693
}
364
694
 
365
695
/*
390
720
*/
391
721
int sqlite3_data_count(sqlite3_stmt *pStmt){
392
722
  Vdbe *pVm = (Vdbe *)pStmt;
393
 
  if( pVm==0 || !pVm->resOnStack ) return 0;
 
723
  if( pVm==0 || pVm->pResultSet==0 ) return 0;
394
724
  return pVm->nResColumn;
395
725
}
396
726
 
402
732
** of NULL.
403
733
*/
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;
 
735
  Vdbe *pVm;
 
736
  int vals;
 
737
  Mem *pOut;
 
738
 
 
739
  pVm = (Vdbe *)pStmt;
 
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];
 
744
  }else{
 
745
    static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
 
746
    if( pVm->db ){
 
747
      sqlite3_mutex_enter(pVm->db->mutex);
 
748
      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
 
749
    }
 
750
    pOut = (Mem*)&nullMem;
411
751
  }
412
 
  return &pVm->pTos[(1-vals)+i];
 
752
  return pOut;
413
753
}
414
754
 
415
755
/*
419
759
** malloc() has failed, the threads mallocFailed flag is cleared and the result
420
760
** code of statement pStmt set to SQLITE_NOMEM.
421
761
**
422
 
** Specificly, this is called from within:
 
762
** Specifically, this is called from within:
423
763
**
424
764
**     sqlite3_column_int()
425
765
**     sqlite3_column_int64()
439
779
  ** and _finalize() will return NOMEM.
440
780
  */
441
781
  Vdbe *p = (Vdbe *)pStmt;
442
 
  p->rc = sqlite3ApiExit(0, p->rc);
 
782
  if( p ){
 
783
    p->rc = sqlite3ApiExit(p->db, p->rc);
 
784
    sqlite3_mutex_leave(p->db->mutex);
 
785
  }
443
786
}
444
787
 
445
788
/**************************** sqlite3_column_  *******************************
448
791
*/
449
792
const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
450
793
  const void *val;
451
 
  sqlite3MallocDisallow();
452
794
  val = sqlite3_value_blob( columnMem(pStmt,i) );
453
 
  sqlite3MallocAllow();
 
795
  /* Even though there is no encoding conversion, value_blob() might
 
796
  ** need to call malloc() to expand the result of a zeroblob() 
 
797
  ** expression. 
 
798
  */
 
799
  columnMallocFailure(pStmt);
454
800
  return val;
455
801
}
456
802
int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
484
830
  return val;
485
831
}
486
832
sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
487
 
  return columnMem(pStmt, i);
 
833
  sqlite3_value *pOut = columnMem(pStmt, i);
 
834
  columnMallocFailure(pStmt);
 
835
  return pOut;
488
836
}
489
837
#ifndef SQLITE_OMIT_UTF16
490
838
const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
494
842
}
495
843
#endif /* SQLITE_OMIT_UTF16 */
496
844
int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
497
 
  return sqlite3_value_type( columnMem(pStmt,i) );
 
845
  int iType = sqlite3_value_type( columnMem(pStmt,i) );
 
846
  columnMallocFailure(pStmt);
 
847
  return iType;
498
848
}
499
849
 
500
850
/* The following function is experimental and subject to change or
526
876
  const void *(*xFunc)(Mem*),
527
877
  int useType
528
878
){
529
 
  const void *ret;
 
879
  const void *ret = 0;
530
880
  Vdbe *p = (Vdbe *)pStmt;
531
 
  int n = sqlite3_column_count(pStmt);
532
 
 
533
 
  if( p==0 || N>=n || N<0 ){
534
 
    return 0;
 
881
  int n;
 
882
  
 
883
 
 
884
  if( p!=0 ){
 
885
    n = sqlite3_column_count(pStmt);
 
886
    if( N<n && N>=0 ){
 
887
      N += useType*n;
 
888
      sqlite3_mutex_enter(p->db->mutex);
 
889
      ret = xFunc(&p->aColName[N]);
 
890
 
 
891
      /* A malloc may have failed inside of the xFunc() call. If this
 
892
      ** is the case, clear the mallocFailed flag and return NULL.
 
893
      */
 
894
      if( p->db && p->db->mallocFailed ){
 
895
        p->db->mallocFailed = 0;
 
896
        ret = 0;
 
897
      }
 
898
      sqlite3_mutex_leave(p->db->mutex);
 
899
    }
535
900
  }
536
 
  N += useType*n;
537
 
  ret = xFunc(&p->aColName[N]);
538
 
 
539
 
  /* A malloc may have failed inside of the xFunc() call. If this is the case,
540
 
  ** clear the mallocFailed flag and return NULL.
541
 
  */
542
 
  sqlite3ApiExit(0, 0);
543
901
  return ret;
544
902
}
545
903
 
559
917
#endif
560
918
 
561
919
/*
 
920
** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
 
921
** not define OMIT_DECLTYPE.
 
922
*/
 
923
#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
 
924
# error "Must not define both SQLITE_OMIT_DECLTYPE \
 
925
         and SQLITE_ENABLE_COLUMN_METADATA"
 
926
#endif
 
927
 
 
928
#ifndef SQLITE_OMIT_DECLTYPE
 
929
/*
562
930
** Return the column declaration type (if applicable) of the 'i'th column
563
931
** of the result set of SQL statement pStmt.
564
932
*/
572
940
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
573
941
}
574
942
#endif /* SQLITE_OMIT_UTF16 */
 
943
#endif /* SQLITE_OMIT_DECLTYPE */
575
944
 
576
945
#ifdef SQLITE_ENABLE_COLUMN_METADATA
577
946
/*
658
1027
** Bind a text or BLOB value.
659
1028
*/
660
1029
static int bindText(
661
 
  sqlite3_stmt *pStmt, 
662
 
  int i, 
663
 
  const void *zData, 
664
 
  int nData, 
665
 
  void (*xDel)(void*),
666
 
  int encoding
 
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 */
667
1036
){
668
1037
  Vdbe *p = (Vdbe *)pStmt;
669
1038
  Mem *pVar;
670
1039
  int rc;
671
1040
 
 
1041
  if( p==0 ){
 
1042
    return SQLITE_MISUSE;
 
1043
  }
 
1044
  sqlite3_mutex_enter(p->db->mutex);
672
1045
  rc = vdbeUnbind(p, i);
673
 
  if( rc || zData==0 ){
674
 
    return rc;
675
 
  }
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));
680
 
  }
681
 
 
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));
 
1051
    }
 
1052
    sqlite3Error(p->db, rc, 0);
 
1053
    rc = sqlite3ApiExit(p->db, rc);
 
1054
  }
 
1055
  sqlite3_mutex_leave(p->db->mutex);
 
1056
  return rc;
684
1057
}
685
1058
 
686
1059
 
699
1072
int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
700
1073
  int rc;
701
1074
  Vdbe *p = (Vdbe *)pStmt;
 
1075
  sqlite3_mutex_enter(p->db->mutex);
702
1076
  rc = vdbeUnbind(p, i);
703
1077
  if( rc==SQLITE_OK ){
704
1078
    sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
705
1079
  }
 
1080
  sqlite3_mutex_leave(p->db->mutex);
706
1081
  return rc;
707
1082
}
708
1083
int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
711
1086
int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
712
1087
  int rc;
713
1088
  Vdbe *p = (Vdbe *)pStmt;
 
1089
  sqlite3_mutex_enter(p->db->mutex);
714
1090
  rc = vdbeUnbind(p, i);
715
1091
  if( rc==SQLITE_OK ){
716
1092
    sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
717
1093
  }
 
1094
  sqlite3_mutex_leave(p->db->mutex);
718
1095
  return rc;
719
1096
}
720
 
int sqlite3_bind_null(sqlite3_stmt* p, int i){
721
 
  return vdbeUnbind((Vdbe *)p, i);
 
1097
int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
 
1098
  int rc;
 
1099
  Vdbe *p = (Vdbe*)pStmt;
 
1100
  sqlite3_mutex_enter(p->db->mutex);
 
1101
  rc = vdbeUnbind(p, i);
 
1102
  sqlite3_mutex_leave(p->db->mutex);
 
1103
  return rc;
722
1104
}
723
1105
int sqlite3_bind_text( 
724
1106
  sqlite3_stmt *pStmt, 
743
1125
int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
744
1126
  int rc;
745
1127
  Vdbe *p = (Vdbe *)pStmt;
746
 
  rc = vdbeUnbind(p, i);
747
 
  if( rc==SQLITE_OK ){
748
 
    sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
749
 
  }
 
1128
  sqlite3_mutex_enter(p->db->mutex);
 
1129
  rc = vdbeUnbind(p, i);
 
1130
  if( rc==SQLITE_OK ){
 
1131
    rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
 
1132
  }
 
1133
  rc = sqlite3ApiExit(p->db, rc);
 
1134
  sqlite3_mutex_leave(p->db->mutex);
 
1135
  return rc;
 
1136
}
 
1137
int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
 
1138
  int rc;
 
1139
  Vdbe *p = (Vdbe *)pStmt;
 
1140
  sqlite3_mutex_enter(p->db->mutex);
 
1141
  rc = vdbeUnbind(p, i);
 
1142
  if( rc==SQLITE_OK ){
 
1143
    sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
 
1144
  }
 
1145
  sqlite3_mutex_leave(p->db->mutex);
750
1146
  return rc;
751
1147
}
752
1148
 
766
1162
*/
767
1163
static void createVarMap(Vdbe *p){
768
1164
  if( !p->okVar ){
769
 
    int j;
770
 
    Op *pOp;
771
 
    for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
772
 
      if( pOp->opcode==OP_Variable ){
773
 
        assert( pOp->p1>0 && pOp->p1<=p->nVar );
774
 
        p->azVar[pOp->p1-1] = pOp->p3;
 
1165
    sqlite3_mutex_enter(p->db->mutex);
 
1166
    if( !p->okVar ){
 
1167
      int j;
 
1168
      Op *pOp;
 
1169
      for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
 
1170
        if( pOp->opcode==OP_Variable ){
 
1171
          assert( pOp->p1>0 && pOp->p1<=p->nVar );
 
1172
          p->azVar[pOp->p1-1] = pOp->p4.z;
 
1173
        }
775
1174
      }
 
1175
      p->okVar = 1;
776
1176
    }
777
 
    p->okVar = 1;
 
1177
    sqlite3_mutex_leave(p->db->mutex);
778
1178
  }
779
1179
}
780
1180
 
826
1226
  Vdbe *pTo = (Vdbe*)pToStmt;
827
1227
  int i, rc = SQLITE_OK;
828
1228
  if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
829
 
    || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
 
1229
    || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
 
1230
    || pTo->db!=pFrom->db ){
830
1231
    return SQLITE_MISUSE;
831
1232
  }
832
1233
  if( pFrom->nVar!=pTo->nVar ){
833
1234
    return SQLITE_ERROR;
834
1235
  }
 
1236
  sqlite3_mutex_enter(pTo->db->mutex);
835
1237
  for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
836
 
    sqlite3MallocDisallow();
837
 
    rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
838
 
    sqlite3MallocAllow();
 
1238
    sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
839
1239
  }
 
1240
  sqlite3_mutex_leave(pTo->db->mutex);
840
1241
  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
841
1242
  return rc;
842
1243
}