extensions/sqlite/sqlite-source/vdbeapi.c
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
ds@1952
     1
/*
ds@1952
     2
** 2004 May 26
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
**
ds@1952
    13
** This file contains code use to implement APIs that are part of the
ds@1952
    14
** VDBE.
ds@1952
    15
*/
ds@1952
    16
#include "sqliteInt.h"
ds@1952
    17
#include "vdbeInt.h"
ds@1952
    18
ds@1952
    19
/*
ds@1952
    20
** Return TRUE (non-zero) of the statement supplied as an argument needs
ds@1952
    21
** to be recompiled.  A statement needs to be recompiled whenever the
ds@1952
    22
** execution environment changes in a way that would alter the program
ds@1952
    23
** that sqlite3_prepare() generates.  For example, if new functions or
ds@1952
    24
** collating sequences are registered or if an authorizer function is
ds@1952
    25
** added or changed.
ds@1952
    26
*/
ds@1952
    27
int sqlite3_expired(sqlite3_stmt *pStmt){
ds@1952
    28
  Vdbe *p = (Vdbe*)pStmt;
ds@1952
    29
  return p==0 || p->expired;
ds@1952
    30
}
ds@1952
    31
ds@1952
    32
/*
ds@1952
    33
** The following routine destroys a virtual machine that is created by
ds@1952
    34
** the sqlite3_compile() routine. The integer returned is an SQLITE_
ds@1952
    35
** success/failure code that describes the result of executing the virtual
ds@1952
    36
** machine.
ds@1952
    37
**
ds@1952
    38
** This routine sets the error code and string returned by
ds@1952
    39
** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
ds@1952
    40
*/
ds@1952
    41
int sqlite3_finalize(sqlite3_stmt *pStmt){
ds@1952
    42
  int rc;
ds@1952
    43
  if( pStmt==0 ){
ds@1952
    44
    rc = SQLITE_OK;
ds@1952
    45
  }else{
ds@1952
    46
    Vdbe *v = (Vdbe*)pStmt;
ds@1952
    47
    sqlite3_mutex *mutex = v->db->mutex;
ds@1952
    48
    sqlite3_mutex_enter(mutex);
ds@1952
    49
    rc = sqlite3VdbeFinalize(v);
ds@1952
    50
    sqlite3_mutex_leave(mutex);
ds@1952
    51
  }
ds@1952
    52
  return rc;
ds@1952
    53
}
ds@1952
    54
ds@1952
    55
/*
ds@1952
    56
** Terminate the current execution of an SQL statement and reset it
ds@1952
    57
** back to its starting state so that it can be reused. A success code from
ds@1952
    58
** the prior execution is returned.
ds@1952
    59
**
ds@1952
    60
** This routine sets the error code and string returned by
ds@1952
    61
** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
ds@1952
    62
*/
ds@1952
    63
int sqlite3_reset(sqlite3_stmt *pStmt){
ds@1952
    64
  int rc;
ds@1952
    65
  if( pStmt==0 ){
ds@1952
    66
    rc = SQLITE_OK;
ds@1952
    67
  }else{
ds@1952
    68
    Vdbe *v = (Vdbe*)pStmt;
ds@1952
    69
    sqlite3_mutex_enter(v->db->mutex);
ds@1952
    70
    rc = sqlite3VdbeReset(v);
ds@1952
    71
    sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
ds@1952
    72
    assert( (rc & (v->db->errMask))==rc );
ds@1952
    73
    sqlite3_mutex_leave(v->db->mutex);
ds@1952
    74
  }
ds@1952
    75
  return rc;
ds@1952
    76
}
ds@1952
    77
ds@1952
    78
/*
ds@1952
    79
** Set all the parameters in the compiled SQL statement to NULL.
ds@1952
    80
*/
ds@1952
    81
int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
ds@1952
    82
  int i;
ds@1952
    83
  int rc = SQLITE_OK;
ds@1952
    84
  Vdbe *v = (Vdbe*)pStmt;
ds@1952
    85
  sqlite3_mutex_enter(v->db->mutex);
ds@1952
    86
  for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
ds@1952
    87
    rc = sqlite3_bind_null(pStmt, i);
ds@1952
    88
  }
ds@1952
    89
  sqlite3_mutex_leave(v->db->mutex);
ds@1952
    90
  return rc;
ds@1952
    91
}
ds@1952
    92
ds@1952
    93
ds@1952
    94
/**************************** sqlite3_value_  *******************************
ds@1952
    95
** The following routines extract information from a Mem or sqlite3_value
ds@1952
    96
** structure.
ds@1952
    97
*/
ds@1952
    98
const void *sqlite3_value_blob(sqlite3_value *pVal){
ds@1952
    99
  Mem *p = (Mem*)pVal;
ds@1952
   100
  if( p->flags & (MEM_Blob|MEM_Str) ){
ds@1952
   101
    sqlite3VdbeMemExpandBlob(p);
ds@1952
   102
    p->flags &= ~MEM_Str;
ds@1952
   103
    p->flags |= MEM_Blob;
ds@1952
   104
    return p->z;
ds@1952
   105
  }else{
ds@1952
   106
    return sqlite3_value_text(pVal);
ds@1952
   107
  }
ds@1952
   108
}
ds@1952
   109
int sqlite3_value_bytes(sqlite3_value *pVal){
ds@1952
   110
  return sqlite3ValueBytes(pVal, SQLITE_UTF8);
ds@1952
   111
}
ds@1952
   112
int sqlite3_value_bytes16(sqlite3_value *pVal){
ds@1952
   113
  return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
ds@1952
   114
}
ds@1952
   115
double sqlite3_value_double(sqlite3_value *pVal){
ds@1952
   116
  return sqlite3VdbeRealValue((Mem*)pVal);
ds@1952
   117
}
ds@1952
   118
int sqlite3_value_int(sqlite3_value *pVal){
ds@1952
   119
  return sqlite3VdbeIntValue((Mem*)pVal);
ds@1952
   120
}
ds@1952
   121
sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
ds@1952
   122
  return sqlite3VdbeIntValue((Mem*)pVal);
ds@1952
   123
}
ds@1952
   124
const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
ds@1952
   125
  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
ds@1952
   126
}
ds@1952
   127
#ifndef SQLITE_OMIT_UTF16
ds@1952
   128
const void *sqlite3_value_text16(sqlite3_value* pVal){
ds@1952
   129
  return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
ds@1952
   130
}
ds@1952
   131
const void *sqlite3_value_text16be(sqlite3_value *pVal){
ds@1952
   132
  return sqlite3ValueText(pVal, SQLITE_UTF16BE);
ds@1952
   133
}
ds@1952
   134
const void *sqlite3_value_text16le(sqlite3_value *pVal){
ds@1952
   135
  return sqlite3ValueText(pVal, SQLITE_UTF16LE);
ds@1952
   136
}
ds@1952
   137
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   138
int sqlite3_value_type(sqlite3_value* pVal){
ds@1952
   139
  return pVal->type;
ds@1952
   140
}
ds@1952
   141
ds@1952
   142
/**************************** sqlite3_result_  *******************************
ds@1952
   143
** The following routines are used by user-defined functions to specify
ds@1952
   144
** the function result.
ds@1952
   145
*/
ds@1952
   146
void sqlite3_result_blob(
ds@1952
   147
  sqlite3_context *pCtx, 
ds@1952
   148
  const void *z, 
ds@1952
   149
  int n, 
ds@1952
   150
  void (*xDel)(void *)
ds@1952
   151
){
ds@1952
   152
  assert( n>=0 );
ds@1952
   153
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   154
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
ds@1952
   155
}
ds@1952
   156
void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
ds@1952
   157
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   158
  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
ds@1952
   159
}
ds@1952
   160
void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
ds@1952
   161
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   162
  pCtx->isError = 1;
ds@1952
   163
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
ds@1952
   164
}
ds@1952
   165
#ifndef SQLITE_OMIT_UTF16
ds@1952
   166
void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
ds@1952
   167
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   168
  pCtx->isError = 1;
ds@1952
   169
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
ds@1952
   170
}
ds@1952
   171
#endif
ds@1952
   172
void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
ds@1952
   173
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   174
  sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
ds@1952
   175
}
ds@1952
   176
void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
ds@1952
   177
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   178
  sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
ds@1952
   179
}
ds@1952
   180
void sqlite3_result_null(sqlite3_context *pCtx){
ds@1952
   181
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   182
  sqlite3VdbeMemSetNull(&pCtx->s);
ds@1952
   183
}
ds@1952
   184
void sqlite3_result_text(
ds@1952
   185
  sqlite3_context *pCtx, 
ds@1952
   186
  const char *z, 
ds@1952
   187
  int n,
ds@1952
   188
  void (*xDel)(void *)
ds@1952
   189
){
ds@1952
   190
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   191
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
ds@1952
   192
}
ds@1952
   193
#ifndef SQLITE_OMIT_UTF16
ds@1952
   194
void sqlite3_result_text16(
ds@1952
   195
  sqlite3_context *pCtx, 
ds@1952
   196
  const void *z, 
ds@1952
   197
  int n, 
ds@1952
   198
  void (*xDel)(void *)
ds@1952
   199
){
ds@1952
   200
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   201
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
ds@1952
   202
}
ds@1952
   203
void sqlite3_result_text16be(
ds@1952
   204
  sqlite3_context *pCtx, 
ds@1952
   205
  const void *z, 
ds@1952
   206
  int n, 
ds@1952
   207
  void (*xDel)(void *)
ds@1952
   208
){
ds@1952
   209
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   210
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
ds@1952
   211
}
ds@1952
   212
void sqlite3_result_text16le(
ds@1952
   213
  sqlite3_context *pCtx, 
ds@1952
   214
  const void *z, 
ds@1952
   215
  int n, 
ds@1952
   216
  void (*xDel)(void *)
ds@1952
   217
){
ds@1952
   218
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   219
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
ds@1952
   220
}
ds@1952
   221
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   222
void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
ds@1952
   223
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   224
  sqlite3VdbeMemCopy(&pCtx->s, pValue);
ds@1952
   225
}
ds@1952
   226
void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
ds@1952
   227
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   228
  sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
ds@1952
   229
}
ds@1952
   230
ds@1952
   231
/* Force an SQLITE_TOOBIG error. */
ds@1952
   232
void sqlite3_result_error_toobig(sqlite3_context *pCtx){
ds@1952
   233
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   234
  sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1);
ds@1952
   235
}
ds@1952
   236
ds@1952
   237
/* An SQLITE_NOMEM error. */
ds@1952
   238
void sqlite3_result_error_nomem(sqlite3_context *pCtx){
ds@1952
   239
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   240
  sqlite3VdbeMemSetNull(&pCtx->s);
ds@1952
   241
  pCtx->isError = 1;
ds@1952
   242
  pCtx->s.db->mallocFailed = 1;
ds@1952
   243
}
ds@1952
   244
ds@1952
   245
/*
ds@1952
   246
** Execute the statement pStmt, either until a row of data is ready, the
ds@1952
   247
** statement is completely executed or an error occurs.
ds@1952
   248
**
ds@1952
   249
** This routine implements the bulk of the logic behind the sqlite_step()
ds@1952
   250
** API.  The only thing omitted is the automatic recompile if a 
ds@1952
   251
** schema change has occurred.  That detail is handled by the
ds@1952
   252
** outer sqlite3_step() wrapper procedure.
ds@1952
   253
*/
ds@1952
   254
static int sqlite3Step(Vdbe *p){
ds@1952
   255
  sqlite3 *db;
ds@1952
   256
  int rc;
ds@1952
   257
ds@1952
   258
  /* Assert that malloc() has not failed */
ds@1952
   259
  db = p->db;
ds@1952
   260
  assert( !db->mallocFailed );
ds@1952
   261
ds@1952
   262
  if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
ds@1952
   263
    return SQLITE_MISUSE;
ds@1952
   264
  }
ds@1952
   265
  if( p->aborted ){
ds@1952
   266
    return SQLITE_ABORT;
ds@1952
   267
  }
ds@1952
   268
  if( p->pc<=0 && p->expired ){
ds@1952
   269
    if( p->rc==SQLITE_OK ){
ds@1952
   270
      p->rc = SQLITE_SCHEMA;
ds@1952
   271
    }
ds@1952
   272
    rc = SQLITE_ERROR;
ds@1952
   273
    goto end_of_step;
ds@1952
   274
  }
ds@1952
   275
  if( sqlite3SafetyOn(db) ){
ds@1952
   276
    p->rc = SQLITE_MISUSE;
ds@1952
   277
    return SQLITE_MISUSE;
ds@1952
   278
  }
ds@1952
   279
  if( p->pc<0 ){
ds@1952
   280
    /* If there are no other statements currently running, then
ds@1952
   281
    ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
ds@1952
   282
    ** from interrupting a statement that has not yet started.
ds@1952
   283
    */
ds@1952
   284
    if( db->activeVdbeCnt==0 ){
ds@1952
   285
      db->u1.isInterrupted = 0;
ds@1952
   286
    }
ds@1952
   287
ds@1952
   288
#ifndef SQLITE_OMIT_TRACE
ds@1952
   289
    /* Invoke the trace callback if there is one
ds@1952
   290
    */
ds@1952
   291
    if( db->xTrace && !db->init.busy ){
ds@1952
   292
      assert( p->nOp>0 );
ds@1952
   293
      assert( p->aOp[p->nOp-1].opcode==OP_Noop );
ds@1952
   294
      assert( p->aOp[p->nOp-1].p3!=0 );
ds@1952
   295
      assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
ds@1952
   296
      sqlite3SafetyOff(db);
ds@1952
   297
      db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
ds@1952
   298
      if( sqlite3SafetyOn(db) ){
ds@1952
   299
        p->rc = SQLITE_MISUSE;
ds@1952
   300
        return SQLITE_MISUSE;
ds@1952
   301
      }
ds@1952
   302
    }
ds@1952
   303
    if( db->xProfile && !db->init.busy ){
ds@1952
   304
      double rNow;
ds@1952
   305
      sqlite3OsCurrentTime(db->pVfs, &rNow);
ds@1952
   306
      p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
ds@1952
   307
    }
ds@1952
   308
#endif
ds@1952
   309
ds@1952
   310
    /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
ds@1952
   311
    ** on in debugging mode.
ds@1952
   312
    */
ds@1952
   313
#ifdef SQLITE_DEBUG
ds@1952
   314
    if( (db->flags & SQLITE_SqlTrace)!=0 ){
ds@1952
   315
      sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
ds@1952
   316
    }
ds@1952
   317
#endif /* SQLITE_DEBUG */
ds@1952
   318
ds@1952
   319
    db->activeVdbeCnt++;
ds@1952
   320
    p->pc = 0;
ds@1952
   321
  }
ds@1952
   322
#ifndef SQLITE_OMIT_EXPLAIN
ds@1952
   323
  if( p->explain ){
ds@1952
   324
    rc = sqlite3VdbeList(p);
ds@1952
   325
  }else
ds@1952
   326
#endif /* SQLITE_OMIT_EXPLAIN */
ds@1952
   327
  {
ds@1952
   328
    rc = sqlite3VdbeExec(p);
ds@1952
   329
  }
ds@1952
   330
ds@1952
   331
  if( sqlite3SafetyOff(db) ){
ds@1952
   332
    rc = SQLITE_MISUSE;
ds@1952
   333
  }
ds@1952
   334
ds@1952
   335
#ifndef SQLITE_OMIT_TRACE
ds@1952
   336
  /* Invoke the profile callback if there is one
ds@1952
   337
  */
ds@1952
   338
  if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
ds@1952
   339
    double rNow;
ds@1952
   340
    u64 elapseTime;
ds@1952
   341
ds@1952
   342
    sqlite3OsCurrentTime(db->pVfs, &rNow);
ds@1952
   343
    elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
ds@1952
   344
    assert( p->nOp>0 );
ds@1952
   345
    assert( p->aOp[p->nOp-1].opcode==OP_Noop );
ds@1952
   346
    assert( p->aOp[p->nOp-1].p3!=0 );
ds@1952
   347
    assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
ds@1952
   348
    db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime);
ds@1952
   349
  }
ds@1952
   350
#endif
ds@1952
   351
ds@1952
   352
  sqlite3Error(p->db, rc, 0);
ds@1952
   353
  p->rc = sqlite3ApiExit(p->db, p->rc);
ds@1952
   354
end_of_step:
ds@1952
   355
  assert( (rc&0xff)==rc );
ds@1952
   356
  if( p->zSql && (rc&0xff)<SQLITE_ROW ){
ds@1952
   357
    /* This behavior occurs if sqlite3_prepare_v2() was used to build
ds@1952
   358
    ** the prepared statement.  Return error codes directly */
ds@1952
   359
    sqlite3Error(p->db, p->rc, 0);
ds@1952
   360
    return p->rc;
ds@1952
   361
  }else{
ds@1952
   362
    /* This is for legacy sqlite3_prepare() builds and when the code
ds@1952
   363
    ** is SQLITE_ROW or SQLITE_DONE */
ds@1952
   364
    return rc;
ds@1952
   365
  }
ds@1952
   366
}
ds@1952
   367
ds@1952
   368
/*
ds@1952
   369
** This is the top-level implementation of sqlite3_step().  Call
ds@1952
   370
** sqlite3Step() to do most of the work.  If a schema error occurs,
ds@1952
   371
** call sqlite3Reprepare() and try again.
ds@1952
   372
*/
ds@1952
   373
#ifdef SQLITE_OMIT_PARSER
ds@1952
   374
int sqlite3_step(sqlite3_stmt *pStmt){
ds@1952
   375
  int rc;
ds@1952
   376
  Vdbe *v;
ds@1952
   377
  v = (Vdbe*)pStmt;
ds@1952
   378
  sqlite3_mutex_enter(v->db->mutex);
ds@1952
   379
  rc = sqlite3Step(v);
ds@1952
   380
  sqlite3_mutex_leave(v->db->mutex);
ds@1952
   381
  return rc;
ds@1952
   382
}
ds@1952
   383
#else
ds@1952
   384
int sqlite3_step(sqlite3_stmt *pStmt){
ds@1952
   385
  int cnt = 0;
ds@1952
   386
  int rc;
ds@1952
   387
  Vdbe *v = (Vdbe*)pStmt;
ds@1952
   388
  sqlite3_mutex_enter(v->db->mutex);
ds@1952
   389
  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
ds@1952
   390
         && cnt++ < 5
ds@1952
   391
         && sqlite3Reprepare(v) ){
ds@1952
   392
    sqlite3_reset(pStmt);
ds@1952
   393
    v->expired = 0;
ds@1952
   394
  }
ds@1952
   395
  sqlite3_mutex_leave(v->db->mutex);
ds@1952
   396
  return rc;
ds@1952
   397
}
ds@1952
   398
#endif
ds@1952
   399
ds@1952
   400
/*
ds@1952
   401
** Extract the user data from a sqlite3_context structure and return a
ds@1952
   402
** pointer to it.
ds@1952
   403
*/
ds@1952
   404
void *sqlite3_user_data(sqlite3_context *p){
ds@1952
   405
  assert( p && p->pFunc );
ds@1952
   406
  return p->pFunc->pUserData;
ds@1952
   407
}
ds@1952
   408
ds@1952
   409
/*
ds@1952
   410
** The following is the implementation of an SQL function that always
ds@1952
   411
** fails with an error message stating that the function is used in the
ds@1952
   412
** wrong context.  The sqlite3_overload_function() API might construct
ds@1952
   413
** SQL function that use this routine so that the functions will exist
ds@1952
   414
** for name resolution but are actually overloaded by the xFindFunction
ds@1952
   415
** method of virtual tables.
ds@1952
   416
*/
ds@1952
   417
void sqlite3InvalidFunction(
ds@1952
   418
  sqlite3_context *context,  /* The function calling context */
ds@1952
   419
  int argc,                  /* Number of arguments to the function */
ds@1952
   420
  sqlite3_value **argv       /* Value of each argument */
ds@1952
   421
){
ds@1952
   422
  const char *zName = context->pFunc->zName;
ds@1952
   423
  char *zErr;
ds@1952
   424
  zErr = sqlite3MPrintf(0,
ds@1952
   425
      "unable to use function %s in the requested context", zName);
ds@1952
   426
  sqlite3_result_error(context, zErr, -1);
ds@1952
   427
  sqlite3_free(zErr);
ds@1952
   428
}
ds@1952
   429
ds@1952
   430
/*
ds@1952
   431
** Allocate or return the aggregate context for a user function.  A new
ds@1952
   432
** context is allocated on the first call.  Subsequent calls return the
ds@1952
   433
** same context that was returned on prior calls.
ds@1952
   434
*/
ds@1952
   435
void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
ds@1952
   436
  Mem *pMem;
ds@1952
   437
  assert( p && p->pFunc && p->pFunc->xStep );
ds@1952
   438
  assert( sqlite3_mutex_held(p->s.db->mutex) );
ds@1952
   439
  pMem = p->pMem;
ds@1952
   440
  if( (pMem->flags & MEM_Agg)==0 ){
ds@1952
   441
    if( nByte==0 ){
ds@1952
   442
      assert( pMem->flags==MEM_Null );
ds@1952
   443
      pMem->z = 0;
ds@1952
   444
    }else{
ds@1952
   445
      pMem->flags = MEM_Agg;
ds@1952
   446
      pMem->xDel = sqlite3_free;
ds@1952
   447
      pMem->u.pDef = p->pFunc;
ds@1952
   448
      if( nByte<=NBFS ){
ds@1952
   449
        pMem->z = pMem->zShort;
ds@1952
   450
        memset(pMem->z, 0, nByte);
ds@1952
   451
      }else{
ds@1952
   452
        pMem->z = sqlite3DbMallocZero(p->s.db, nByte);
ds@1952
   453
      }
ds@1952
   454
    }
ds@1952
   455
  }
ds@1952
   456
  return (void*)pMem->z;
ds@1952
   457
}
ds@1952
   458
ds@1952
   459
/*
ds@1952
   460
** Return the auxilary data pointer, if any, for the iArg'th argument to
ds@1952
   461
** the user-function defined by pCtx.
ds@1952
   462
*/
ds@1952
   463
void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
ds@1952
   464
  VdbeFunc *pVdbeFunc;
ds@1952
   465
ds@1952
   466
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   467
  pVdbeFunc = pCtx->pVdbeFunc;
ds@1952
   468
  if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
ds@1952
   469
    return 0;
ds@1952
   470
  }
ds@1952
   471
  return pVdbeFunc->apAux[iArg].pAux;
ds@1952
   472
}
ds@1952
   473
ds@1952
   474
/*
ds@1952
   475
** Set the auxilary data pointer and delete function, for the iArg'th
ds@1952
   476
** argument to the user-function defined by pCtx. Any previous value is
ds@1952
   477
** deleted by calling the delete function specified when it was set.
ds@1952
   478
*/
ds@1952
   479
void sqlite3_set_auxdata(
ds@1952
   480
  sqlite3_context *pCtx, 
ds@1952
   481
  int iArg, 
ds@1952
   482
  void *pAux, 
ds@1952
   483
  void (*xDelete)(void*)
ds@1952
   484
){
ds@1952
   485
  struct AuxData *pAuxData;
ds@1952
   486
  VdbeFunc *pVdbeFunc;
ds@1952
   487
  if( iArg<0 ) goto failed;
ds@1952
   488
ds@1952
   489
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
ds@1952
   490
  pVdbeFunc = pCtx->pVdbeFunc;
ds@1952
   491
  if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
ds@1952
   492
    int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
ds@1952
   493
    int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
ds@1952
   494
    pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
ds@1952
   495
    if( !pVdbeFunc ){
ds@1952
   496
      goto failed;
ds@1952
   497
    }
ds@1952
   498
    pCtx->pVdbeFunc = pVdbeFunc;
ds@1952
   499
    memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
ds@1952
   500
    pVdbeFunc->nAux = iArg+1;
ds@1952
   501
    pVdbeFunc->pFunc = pCtx->pFunc;
ds@1952
   502
  }
ds@1952
   503
ds@1952
   504
  pAuxData = &pVdbeFunc->apAux[iArg];
ds@1952
   505
  if( pAuxData->pAux && pAuxData->xDelete ){
ds@1952
   506
    pAuxData->xDelete(pAuxData->pAux);
ds@1952
   507
  }
ds@1952
   508
  pAuxData->pAux = pAux;
ds@1952
   509
  pAuxData->xDelete = xDelete;
ds@1952
   510
  return;
ds@1952
   511
ds@1952
   512
failed:
ds@1952
   513
  if( xDelete ){
ds@1952
   514
    xDelete(pAux);
ds@1952
   515
  }
ds@1952
   516
}
ds@1952
   517
ds@1952
   518
/*
ds@1952
   519
** Return the number of times the Step function of a aggregate has been 
ds@1952
   520
** called.
ds@1952
   521
**
ds@1952
   522
** This function is deprecated.  Do not use it for new code.  It is
ds@1952
   523
** provide only to avoid breaking legacy code.  New aggregate function
ds@1952
   524
** implementations should keep their own counts within their aggregate
ds@1952
   525
** context.
ds@1952
   526
*/
ds@1952
   527
int sqlite3_aggregate_count(sqlite3_context *p){
ds@1952
   528
  assert( p && p->pFunc && p->pFunc->xStep );
ds@1952
   529
  return p->pMem->n;
ds@1952
   530
}
ds@1952
   531
ds@1952
   532
/*
ds@1952
   533
** Return the number of columns in the result set for the statement pStmt.
ds@1952
   534
*/
ds@1952
   535
int sqlite3_column_count(sqlite3_stmt *pStmt){
ds@1952
   536
  Vdbe *pVm = (Vdbe *)pStmt;
ds@1952
   537
  return pVm ? pVm->nResColumn : 0;
ds@1952
   538
}
ds@1952
   539
ds@1952
   540
/*
ds@1952
   541
** Return the number of values available from the current row of the
ds@1952
   542
** currently executing statement pStmt.
ds@1952
   543
*/
ds@1952
   544
int sqlite3_data_count(sqlite3_stmt *pStmt){
ds@1952
   545
  Vdbe *pVm = (Vdbe *)pStmt;
ds@1952
   546
  if( pVm==0 || !pVm->resOnStack ) return 0;
ds@1952
   547
  return pVm->nResColumn;
ds@1952
   548
}
ds@1952
   549
ds@1952
   550
ds@1952
   551
/*
ds@1952
   552
** Check to see if column iCol of the given statement is valid.  If
ds@1952
   553
** it is, return a pointer to the Mem for the value of that column.
ds@1952
   554
** If iCol is not valid, return a pointer to a Mem which has a value
ds@1952
   555
** of NULL.
ds@1952
   556
*/
ds@1952
   557
static Mem *columnMem(sqlite3_stmt *pStmt, int i){
ds@1952
   558
  Vdbe *pVm;
ds@1952
   559
  int vals;
ds@1952
   560
  Mem *pOut;
ds@1952
   561
ds@1952
   562
  pVm = (Vdbe *)pStmt;
ds@1952
   563
  if( pVm && pVm->resOnStack && i<pVm->nResColumn && i>=0 ){
ds@1952
   564
    sqlite3_mutex_enter(pVm->db->mutex);
ds@1952
   565
    vals = sqlite3_data_count(pStmt);
ds@1952
   566
    pOut = &pVm->pTos[(1-vals)+i];
ds@1952
   567
  }else{
ds@1952
   568
    static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL };
ds@1952
   569
    if( pVm->db ){
ds@1952
   570
      sqlite3_mutex_enter(pVm->db->mutex);
ds@1952
   571
      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
ds@1952
   572
    }
ds@1952
   573
    pOut = (Mem*)&nullMem;
ds@1952
   574
  }
ds@1952
   575
  return pOut;
ds@1952
   576
}
ds@1952
   577
ds@1952
   578
/*
ds@1952
   579
** This function is called after invoking an sqlite3_value_XXX function on a 
ds@1952
   580
** column value (i.e. a value returned by evaluating an SQL expression in the
ds@1952
   581
** select list of a SELECT statement) that may cause a malloc() failure. If 
ds@1952
   582
** malloc() has failed, the threads mallocFailed flag is cleared and the result
ds@1952
   583
** code of statement pStmt set to SQLITE_NOMEM.
ds@1952
   584
**
ds@1952
   585
** Specifically, this is called from within:
ds@1952
   586
**
ds@1952
   587
**     sqlite3_column_int()
ds@1952
   588
**     sqlite3_column_int64()
ds@1952
   589
**     sqlite3_column_text()
ds@1952
   590
**     sqlite3_column_text16()
ds@1952
   591
**     sqlite3_column_real()
ds@1952
   592
**     sqlite3_column_bytes()
ds@1952
   593
**     sqlite3_column_bytes16()
ds@1952
   594
**
ds@1952
   595
** But not for sqlite3_column_blob(), which never calls malloc().
ds@1952
   596
*/
ds@1952
   597
static void columnMallocFailure(sqlite3_stmt *pStmt)
ds@1952
   598
{
ds@1952
   599
  /* If malloc() failed during an encoding conversion within an
ds@1952
   600
  ** sqlite3_column_XXX API, then set the return code of the statement to
ds@1952
   601
  ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
ds@1952
   602
  ** and _finalize() will return NOMEM.
ds@1952
   603
  */
ds@1952
   604
  Vdbe *p = (Vdbe *)pStmt;
ds@1952
   605
  if( p ){
ds@1952
   606
    p->rc = sqlite3ApiExit(p->db, p->rc);
ds@1952
   607
    sqlite3_mutex_leave(p->db->mutex);
ds@1952
   608
  }
ds@1952
   609
}
ds@1952
   610
ds@1952
   611
/**************************** sqlite3_column_  *******************************
ds@1952
   612
** The following routines are used to access elements of the current row
ds@1952
   613
** in the result set.
ds@1952
   614
*/
ds@1952
   615
const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
ds@1952
   616
  const void *val;
ds@1952
   617
  val = sqlite3_value_blob( columnMem(pStmt,i) );
ds@1952
   618
  /* Even though there is no encoding conversion, value_blob() might
ds@1952
   619
  ** need to call malloc() to expand the result of a zeroblob() 
ds@1952
   620
  ** expression. 
ds@1952
   621
  */
ds@1952
   622
  columnMallocFailure(pStmt);
ds@1952
   623
  return val;
ds@1952
   624
}
ds@1952
   625
int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
ds@1952
   626
  int val = sqlite3_value_bytes( columnMem(pStmt,i) );
ds@1952
   627
  columnMallocFailure(pStmt);
ds@1952
   628
  return val;
ds@1952
   629
}
ds@1952
   630
int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
ds@1952
   631
  int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
ds@1952
   632
  columnMallocFailure(pStmt);
ds@1952
   633
  return val;
ds@1952
   634
}
ds@1952
   635
double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
ds@1952
   636
  double val = sqlite3_value_double( columnMem(pStmt,i) );
ds@1952
   637
  columnMallocFailure(pStmt);
ds@1952
   638
  return val;
ds@1952
   639
}
ds@1952
   640
int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
ds@1952
   641
  int val = sqlite3_value_int( columnMem(pStmt,i) );
ds@1952
   642
  columnMallocFailure(pStmt);
ds@1952
   643
  return val;
ds@1952
   644
}
ds@1952
   645
sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
ds@1952
   646
  sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
ds@1952
   647
  columnMallocFailure(pStmt);
ds@1952
   648
  return val;
ds@1952
   649
}
ds@1952
   650
const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
ds@1952
   651
  const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
ds@1952
   652
  columnMallocFailure(pStmt);
ds@1952
   653
  return val;
ds@1952
   654
}
ds@1952
   655
sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
ds@1952
   656
  sqlite3_value *pOut = columnMem(pStmt, i);
ds@1952
   657
  columnMallocFailure(pStmt);
ds@1952
   658
  return pOut;
ds@1952
   659
}
ds@1952
   660
#ifndef SQLITE_OMIT_UTF16
ds@1952
   661
const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
ds@1952
   662
  const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
ds@1952
   663
  columnMallocFailure(pStmt);
ds@1952
   664
  return val;
ds@1952
   665
}
ds@1952
   666
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   667
int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
ds@1952
   668
  int iType = sqlite3_value_type( columnMem(pStmt,i) );
ds@1952
   669
  columnMallocFailure(pStmt);
ds@1952
   670
  return iType;
ds@1952
   671
}
ds@1952
   672
ds@1952
   673
/* The following function is experimental and subject to change or
ds@1952
   674
** removal */
ds@1952
   675
/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
ds@1952
   676
**  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
ds@1952
   677
**}
ds@1952
   678
*/
ds@1952
   679
ds@1952
   680
/*
ds@1952
   681
** Convert the N-th element of pStmt->pColName[] into a string using
ds@1952
   682
** xFunc() then return that string.  If N is out of range, return 0.
ds@1952
   683
**
ds@1952
   684
** There are up to 5 names for each column.  useType determines which
ds@1952
   685
** name is returned.  Here are the names:
ds@1952
   686
**
ds@1952
   687
**    0      The column name as it should be displayed for output
ds@1952
   688
**    1      The datatype name for the column
ds@1952
   689
**    2      The name of the database that the column derives from
ds@1952
   690
**    3      The name of the table that the column derives from
ds@1952
   691
**    4      The name of the table column that the result column derives from
ds@1952
   692
**
ds@1952
   693
** If the result is not a simple column reference (if it is an expression
ds@1952
   694
** or a constant) then useTypes 2, 3, and 4 return NULL.
ds@1952
   695
*/
ds@1952
   696
static const void *columnName(
ds@1952
   697
  sqlite3_stmt *pStmt,
ds@1952
   698
  int N,
ds@1952
   699
  const void *(*xFunc)(Mem*),
ds@1952
   700
  int useType
ds@1952
   701
){
ds@1952
   702
  const void *ret = 0;
ds@1952
   703
  Vdbe *p = (Vdbe *)pStmt;
ds@1952
   704
  int n;
ds@1952
   705
  
ds@1952
   706
ds@1952
   707
  if( p!=0 ){
ds@1952
   708
    n = sqlite3_column_count(pStmt);
ds@1952
   709
    if( N<n && N>=0 ){
ds@1952
   710
      N += useType*n;
ds@1952
   711
      sqlite3_mutex_enter(p->db->mutex);
ds@1952
   712
      ret = xFunc(&p->aColName[N]);
ds@1952
   713
ds@1952
   714
      /* A malloc may have failed inside of the xFunc() call. If this
ds@1952
   715
      ** is the case, clear the mallocFailed flag and return NULL.
ds@1952
   716
      */
ds@1952
   717
      if( p->db && p->db->mallocFailed ){
ds@1952
   718
        p->db->mallocFailed = 0;
ds@1952
   719
        ret = 0;
ds@1952
   720
      }
ds@1952
   721
      sqlite3_mutex_leave(p->db->mutex);
ds@1952
   722
    }
ds@1952
   723
  }
ds@1952
   724
  return ret;
ds@1952
   725
}
ds@1952
   726
ds@1952
   727
/*
ds@1952
   728
** Return the name of the Nth column of the result set returned by SQL
ds@1952
   729
** statement pStmt.
ds@1952
   730
*/
ds@1952
   731
const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
ds@1952
   732
  return columnName(
ds@1952
   733
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
ds@1952
   734
}
ds@1952
   735
#ifndef SQLITE_OMIT_UTF16
ds@1952
   736
const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
ds@1952
   737
  return columnName(
ds@1952
   738
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
ds@1952
   739
}
ds@1952
   740
#endif
ds@1952
   741
ds@1952
   742
/*
ds@1952
   743
** Return the column declaration type (if applicable) of the 'i'th column
ds@1952
   744
** of the result set of SQL statement pStmt.
ds@1952
   745
*/
ds@1952
   746
const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
ds@1952
   747
  return columnName(
ds@1952
   748
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
ds@1952
   749
}
ds@1952
   750
#ifndef SQLITE_OMIT_UTF16
ds@1952
   751
const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
ds@1952
   752
  return columnName(
ds@1952
   753
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
ds@1952
   754
}
ds@1952
   755
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   756
ds@1952
   757
#ifdef SQLITE_ENABLE_COLUMN_METADATA
ds@1952
   758
/*
ds@1952
   759
** Return the name of the database from which a result column derives.
ds@1952
   760
** NULL is returned if the result column is an expression or constant or
ds@1952
   761
** anything else which is not an unabiguous reference to a database column.
ds@1952
   762
*/
ds@1952
   763
const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
ds@1952
   764
  return columnName(
ds@1952
   765
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
ds@1952
   766
}
ds@1952
   767
#ifndef SQLITE_OMIT_UTF16
ds@1952
   768
const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
ds@1952
   769
  return columnName(
ds@1952
   770
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
ds@1952
   771
}
ds@1952
   772
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   773
ds@1952
   774
/*
ds@1952
   775
** Return the name of the table from which a result column derives.
ds@1952
   776
** NULL is returned if the result column is an expression or constant or
ds@1952
   777
** anything else which is not an unabiguous reference to a database column.
ds@1952
   778
*/
ds@1952
   779
const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
ds@1952
   780
  return columnName(
ds@1952
   781
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
ds@1952
   782
}
ds@1952
   783
#ifndef SQLITE_OMIT_UTF16
ds@1952
   784
const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
ds@1952
   785
  return columnName(
ds@1952
   786
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
ds@1952
   787
}
ds@1952
   788
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   789
ds@1952
   790
/*
ds@1952
   791
** Return the name of the table column from which a result column derives.
ds@1952
   792
** NULL is returned if the result column is an expression or constant or
ds@1952
   793
** anything else which is not an unabiguous reference to a database column.
ds@1952
   794
*/
ds@1952
   795
const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
ds@1952
   796
  return columnName(
ds@1952
   797
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
ds@1952
   798
}
ds@1952
   799
#ifndef SQLITE_OMIT_UTF16
ds@1952
   800
const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
ds@1952
   801
  return columnName(
ds@1952
   802
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
ds@1952
   803
}
ds@1952
   804
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   805
#endif /* SQLITE_ENABLE_COLUMN_METADATA */
ds@1952
   806
ds@1952
   807
ds@1952
   808
/******************************* sqlite3_bind_  ***************************
ds@1952
   809
** 
ds@1952
   810
** Routines used to attach values to wildcards in a compiled SQL statement.
ds@1952
   811
*/
ds@1952
   812
/*
ds@1952
   813
** Unbind the value bound to variable i in virtual machine p. This is the 
ds@1952
   814
** the same as binding a NULL value to the column. If the "i" parameter is
ds@1952
   815
** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
ds@1952
   816
**
ds@1952
   817
** The error code stored in database p->db is overwritten with the return
ds@1952
   818
** value in any case.
ds@1952
   819
*/
ds@1952
   820
static int vdbeUnbind(Vdbe *p, int i){
ds@1952
   821
  Mem *pVar;
ds@1952
   822
  if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
ds@1952
   823
    if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
ds@1952
   824
    return SQLITE_MISUSE;
ds@1952
   825
  }
ds@1952
   826
  if( i<1 || i>p->nVar ){
ds@1952
   827
    sqlite3Error(p->db, SQLITE_RANGE, 0);
ds@1952
   828
    return SQLITE_RANGE;
ds@1952
   829
  }
ds@1952
   830
  i--;
ds@1952
   831
  pVar = &p->aVar[i];
ds@1952
   832
  sqlite3VdbeMemRelease(pVar);
ds@1952
   833
  pVar->flags = MEM_Null;
ds@1952
   834
  sqlite3Error(p->db, SQLITE_OK, 0);
ds@1952
   835
  return SQLITE_OK;
ds@1952
   836
}
ds@1952
   837
ds@1952
   838
/*
ds@1952
   839
** Bind a text or BLOB value.
ds@1952
   840
*/
ds@1952
   841
static int bindText(
ds@1952
   842
  sqlite3_stmt *pStmt,   /* The statement to bind against */
ds@1952
   843
  int i,                 /* Index of the parameter to bind */
ds@1952
   844
  const void *zData,     /* Pointer to the data to be bound */
ds@1952
   845
  int nData,             /* Number of bytes of data to be bound */
ds@1952
   846
  void (*xDel)(void*),   /* Destructor for the data */
ds@1952
   847
  int encoding           /* Encoding for the data */
ds@1952
   848
){
ds@1952
   849
  Vdbe *p = (Vdbe *)pStmt;
ds@1952
   850
  Mem *pVar;
ds@1952
   851
  int rc;
ds@1952
   852
ds@1952
   853
  if( p==0 ){
ds@1952
   854
    return SQLITE_MISUSE;
ds@1952
   855
  }
ds@1952
   856
  sqlite3_mutex_enter(p->db->mutex);
ds@1952
   857
  rc = vdbeUnbind(p, i);
ds@1952
   858
  if( rc==SQLITE_OK && zData!=0 ){
ds@1952
   859
    pVar = &p->aVar[i-1];
ds@1952
   860
    rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
ds@1952
   861
    if( rc==SQLITE_OK && encoding!=0 ){
ds@1952
   862
      rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
ds@1952
   863
    }
ds@1952
   864
    sqlite3Error(p->db, rc, 0);
ds@1952
   865
    rc = sqlite3ApiExit(p->db, rc);
ds@1952
   866
  }
ds@1952
   867
  sqlite3_mutex_leave(p->db->mutex);
ds@1952
   868
  return rc;
ds@1952
   869
}
ds@1952
   870
ds@1952
   871
ds@1952
   872
/*
ds@1952
   873
** Bind a blob value to an SQL statement variable.
ds@1952
   874
*/
ds@1952
   875
int sqlite3_bind_blob(
ds@1952
   876
  sqlite3_stmt *pStmt, 
ds@1952
   877
  int i, 
ds@1952
   878
  const void *zData, 
ds@1952
   879
  int nData, 
ds@1952
   880
  void (*xDel)(void*)
ds@1952
   881
){
ds@1952
   882
  return bindText(pStmt, i, zData, nData, xDel, 0);
ds@1952
   883
}
ds@1952
   884
int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
ds@1952
   885
  int rc;
ds@1952
   886
  Vdbe *p = (Vdbe *)pStmt;
ds@1952
   887
  sqlite3_mutex_enter(p->db->mutex);
ds@1952
   888
  rc = vdbeUnbind(p, i);
ds@1952
   889
  if( rc==SQLITE_OK ){
ds@1952
   890
    sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
ds@1952
   891
  }
ds@1952
   892
  sqlite3_mutex_leave(p->db->mutex);
ds@1952
   893
  return rc;
ds@1952
   894
}
ds@1952
   895
int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
ds@1952
   896
  return sqlite3_bind_int64(p, i, (i64)iValue);
ds@1952
   897
}
ds@1952
   898
int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
ds@1952
   899
  int rc;
ds@1952
   900
  Vdbe *p = (Vdbe *)pStmt;
ds@1952
   901
  sqlite3_mutex_enter(p->db->mutex);
ds@1952
   902
  rc = vdbeUnbind(p, i);
ds@1952
   903
  if( rc==SQLITE_OK ){
ds@1952
   904
    sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
ds@1952
   905
  }
ds@1952
   906
  sqlite3_mutex_leave(p->db->mutex);
ds@1952
   907
  return rc;
ds@1952
   908
}
ds@1952
   909
int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
ds@1952
   910
  int rc;
ds@1952
   911
  Vdbe *p = (Vdbe*)pStmt;
ds@1952
   912
  sqlite3_mutex_enter(p->db->mutex);
ds@1952
   913
  rc = vdbeUnbind(p, i);
ds@1952
   914
  sqlite3_mutex_leave(p->db->mutex);
ds@1952
   915
  return rc;
ds@1952
   916
}
ds@1952
   917
int sqlite3_bind_text( 
ds@1952
   918
  sqlite3_stmt *pStmt, 
ds@1952
   919
  int i, 
ds@1952
   920
  const char *zData, 
ds@1952
   921
  int nData, 
ds@1952
   922
  void (*xDel)(void*)
ds@1952
   923
){
ds@1952
   924
  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
ds@1952
   925
}
ds@1952
   926
#ifndef SQLITE_OMIT_UTF16
ds@1952
   927
int sqlite3_bind_text16(
ds@1952
   928
  sqlite3_stmt *pStmt, 
ds@1952
   929
  int i, 
ds@1952
   930
  const void *zData, 
ds@1952
   931
  int nData, 
ds@1952
   932
  void (*xDel)(void*)
ds@1952
   933
){
ds@1952
   934
  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
ds@1952
   935
}
ds@1952
   936
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   937
int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
ds@1952
   938
  int rc;
ds@1952
   939
  Vdbe *p = (Vdbe *)pStmt;
ds@1952
   940
  sqlite3_mutex_enter(p->db->mutex);
ds@1952
   941
  rc = vdbeUnbind(p, i);
ds@1952
   942
  if( rc==SQLITE_OK ){
ds@1952
   943
    rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
ds@1952
   944
  }
ds@1952
   945
  sqlite3_mutex_leave(p->db->mutex);
ds@1952
   946
  return rc;
ds@1952
   947
}
ds@1952
   948
int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
ds@1952
   949
  int rc;
ds@1952
   950
  Vdbe *p = (Vdbe *)pStmt;
ds@1952
   951
  sqlite3_mutex_enter(p->db->mutex);
ds@1952
   952
  rc = vdbeUnbind(p, i);
ds@1952
   953
  if( rc==SQLITE_OK ){
ds@1952
   954
    sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
ds@1952
   955
  }
ds@1952
   956
  sqlite3_mutex_leave(p->db->mutex);
ds@1952
   957
  return rc;
ds@1952
   958
}
ds@1952
   959
ds@1952
   960
/*
ds@1952
   961
** Return the number of wildcards that can be potentially bound to.
ds@1952
   962
** This routine is added to support DBD::SQLite.  
ds@1952
   963
*/
ds@1952
   964
int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
ds@1952
   965
  Vdbe *p = (Vdbe*)pStmt;
ds@1952
   966
  return p ? p->nVar : 0;
ds@1952
   967
}
ds@1952
   968
ds@1952
   969
/*
ds@1952
   970
** Create a mapping from variable numbers to variable names
ds@1952
   971
** in the Vdbe.azVar[] array, if such a mapping does not already
ds@1952
   972
** exist.
ds@1952
   973
*/
ds@1952
   974
static void createVarMap(Vdbe *p){
ds@1952
   975
  if( !p->okVar ){
ds@1952
   976
    sqlite3_mutex_enter(p->db->mutex);
ds@1952
   977
    if( !p->okVar ){
ds@1952
   978
      int j;
ds@1952
   979
      Op *pOp;
ds@1952
   980
      for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
ds@1952
   981
        if( pOp->opcode==OP_Variable ){
ds@1952
   982
          assert( pOp->p1>0 && pOp->p1<=p->nVar );
ds@1952
   983
          p->azVar[pOp->p1-1] = pOp->p3;
ds@1952
   984
        }
ds@1952
   985
      }
ds@1952
   986
      p->okVar = 1;
ds@1952
   987
    }
ds@1952
   988
    sqlite3_mutex_leave(p->db->mutex);
ds@1952
   989
  }
ds@1952
   990
}
ds@1952
   991
ds@1952
   992
/*
ds@1952
   993
** Return the name of a wildcard parameter.  Return NULL if the index
ds@1952
   994
** is out of range or if the wildcard is unnamed.
ds@1952
   995
**
ds@1952
   996
** The result is always UTF-8.
ds@1952
   997
*/
ds@1952
   998
const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
ds@1952
   999
  Vdbe *p = (Vdbe*)pStmt;
ds@1952
  1000
  if( p==0 || i<1 || i>p->nVar ){
ds@1952
  1001
    return 0;
ds@1952
  1002
  }
ds@1952
  1003
  createVarMap(p);
ds@1952
  1004
  return p->azVar[i-1];
ds@1952
  1005
}
ds@1952
  1006
ds@1952
  1007
/*
ds@1952
  1008
** Given a wildcard parameter name, return the index of the variable
ds@1952
  1009
** with that name.  If there is no variable with the given name,
ds@1952
  1010
** return 0.
ds@1952
  1011
*/
ds@1952
  1012
int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
ds@1952
  1013
  Vdbe *p = (Vdbe*)pStmt;
ds@1952
  1014
  int i;
ds@1952
  1015
  if( p==0 ){
ds@1952
  1016
    return 0;
ds@1952
  1017
  }
ds@1952
  1018
  createVarMap(p); 
ds@1952
  1019
  if( zName ){
ds@1952
  1020
    for(i=0; i<p->nVar; i++){
ds@1952
  1021
      const char *z = p->azVar[i];
ds@1952
  1022
      if( z && strcmp(z,zName)==0 ){
ds@1952
  1023
        return i+1;
ds@1952
  1024
      }
ds@1952
  1025
    }
ds@1952
  1026
  }
ds@1952
  1027
  return 0;
ds@1952
  1028
}
ds@1952
  1029
ds@1952
  1030
/*
ds@1952
  1031
** Transfer all bindings from the first statement over to the second.
ds@1952
  1032
** If the two statements contain a different number of bindings, then
ds@1952
  1033
** an SQLITE_ERROR is returned.
ds@1952
  1034
*/
ds@1952
  1035
int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
ds@1952
  1036
  Vdbe *pFrom = (Vdbe*)pFromStmt;
ds@1952
  1037
  Vdbe *pTo = (Vdbe*)pToStmt;
ds@1952
  1038
  int i, rc = SQLITE_OK;
ds@1952
  1039
  if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
ds@1952
  1040
    || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
ds@1952
  1041
    || pTo->db!=pFrom->db ){
ds@1952
  1042
    return SQLITE_MISUSE;
ds@1952
  1043
  }
ds@1952
  1044
  if( pFrom->nVar!=pTo->nVar ){
ds@1952
  1045
    return SQLITE_ERROR;
ds@1952
  1046
  }
ds@1952
  1047
  sqlite3_mutex_enter(pTo->db->mutex);
ds@1952
  1048
  for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
ds@1952
  1049
    sqlite3MallocDisallow();
ds@1952
  1050
    rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
ds@1952
  1051
    sqlite3MallocAllow();
ds@1952
  1052
  }
ds@1952
  1053
  sqlite3_mutex_leave(pTo->db->mutex);
ds@1952
  1054
  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
ds@1952
  1055
  return rc;
ds@1952
  1056
}
ds@1952
  1057
ds@1952
  1058
/*
ds@1952
  1059
** Return the sqlite3* database handle to which the prepared statement given
ds@1952
  1060
** in the argument belongs.  This is the same database handle that was
ds@1952
  1061
** the first argument to the sqlite3_prepare() that was used to create
ds@1952
  1062
** the statement in the first place.
ds@1952
  1063
*/
ds@1952
  1064
sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
ds@1952
  1065
  return pStmt ? ((Vdbe*)pStmt)->db : 0;
ds@1952
  1066
}