Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
vdbeapi.c2605 linesDownload Raw Back to src
1/*2** 2004 May 263**4** The author disclaims copyright to this source code.  In place of5** a legal notice, here is a blessing:6**7**    May you do good and not evil.8**    May you find forgiveness for yourself and forgive others.9**    May you share freely, never taking more than you give.10**11*************************************************************************12**13** This file contains code use to implement APIs that are part of the14** VDBE.15*/16#include "sqliteInt.h"17#include "vdbeInt.h"18#include "opcodes.h"19 20#ifndef SQLITE_OMIT_DEPRECATED21/*22** Return TRUE (non-zero) of the statement supplied as an argument needs23** to be recompiled.  A statement needs to be recompiled whenever the24** execution environment changes in a way that would alter the program25** that sqlite3_prepare() generates.  For example, if new functions or26** collating sequences are registered or if an authorizer function is27** added or changed.28*/29int sqlite3_expired(sqlite3_stmt *pStmt){30  Vdbe *p = (Vdbe*)pStmt;31  return p==0 || p->expired;32}33#endif34 35/*36** Check on a Vdbe to make sure it has not been finalized.  Log37** an error and return true if it has been finalized (or is otherwise38** invalid).  Return false if it is ok.39*/40static int vdbeSafety(Vdbe *p){41  if( p->db==0 ){42    sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");43    return 1;44  }else{45    return 0;46  }47}48static int vdbeSafetyNotNull(Vdbe *p){49  if( p==0 ){50    sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");51    return 1;52  }else{53    return vdbeSafety(p);54  }55}56 57#ifndef SQLITE_OMIT_TRACE58/*59** Invoke the profile callback.  This routine is only called if we already60** know that the profile callback is defined and needs to be invoked.61*/62static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){63  sqlite3_int64 iNow;64  sqlite3_int64 iElapse;65  assert( p->startTime>0 );66  assert( db->init.busy==0 );67  assert( p->zSql!=0 );68  sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);69  iElapse = (iNow - p->startTime)*1000000;70#ifndef SQLITE_OMIT_DEPRECATED71  if( db->xProfile ){72    db->xProfile(db->pProfileArg, p->zSql, iElapse);73  }74#endif75  if( db->mTrace & SQLITE_TRACE_PROFILE ){76    db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);77  }78  p->startTime = 0;79}80/*81** The checkProfileCallback(DB,P) macro checks to see if a profile callback82** is needed, and it invokes the callback if it is needed.83*/84# define checkProfileCallback(DB,P) \85   if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }86#else87# define checkProfileCallback(DB,P)  /*no-op*/88#endif89 90/*91** The following routine destroys a virtual machine that is created by92** the sqlite3_compile() routine. The integer returned is an SQLITE_93** success/failure code that describes the result of executing the virtual94** machine.95**96** This routine sets the error code and string returned by97** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().98*/99int sqlite3_finalize(sqlite3_stmt *pStmt){100  int rc;101  if( pStmt==0 ){102    /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL103    ** pointer is a harmless no-op. */104    rc = SQLITE_OK;105  }else{106    Vdbe *v = (Vdbe*)pStmt;107    sqlite3 *db = v->db;108    if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;109    sqlite3_mutex_enter(db->mutex);110    checkProfileCallback(db, v);111    assert( v->eVdbeState>=VDBE_READY_STATE );112    rc = sqlite3VdbeReset(v);113    sqlite3VdbeDelete(v);114    rc = sqlite3ApiExit(db, rc);115    sqlite3LeaveMutexAndCloseZombie(db);116  }117  return rc;118}119 120/*121** Terminate the current execution of an SQL statement and reset it122** back to its starting state so that it can be reused. A success code from123** the prior execution is returned.124**125** This routine sets the error code and string returned by126** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().127*/128int sqlite3_reset(sqlite3_stmt *pStmt){129  int rc;130  if( pStmt==0 ){131    rc = SQLITE_OK;132  }else{133    Vdbe *v = (Vdbe*)pStmt;134    sqlite3 *db = v->db;135    sqlite3_mutex_enter(db->mutex);136    checkProfileCallback(db, v);137    rc = sqlite3VdbeReset(v);138    sqlite3VdbeRewind(v);139    assert( (rc & (db->errMask))==rc );140    rc = sqlite3ApiExit(db, rc);141    sqlite3_mutex_leave(db->mutex);142  }143  return rc;144}145 146/*147** Set all the parameters in the compiled SQL statement to NULL.148*/149int sqlite3_clear_bindings(sqlite3_stmt *pStmt){150  int i;151  int rc = SQLITE_OK;152  Vdbe *p = (Vdbe*)pStmt;153#if SQLITE_THREADSAFE154  sqlite3_mutex *mutex;155#endif156#ifdef SQLITE_ENABLE_API_ARMOR157  if( pStmt==0 ){158    return SQLITE_MISUSE_BKPT;159  }160#endif161#if SQLITE_THREADSAFE162  mutex = p->db->mutex;163#endif164  sqlite3_mutex_enter(mutex);165  for(i=0; i<p->nVar; i++){166    sqlite3VdbeMemRelease(&p->aVar[i]);167    p->aVar[i].flags = MEM_Null;168  }169  assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );170  if( p->expmask ){171    p->expired = 1;172  }173  sqlite3_mutex_leave(mutex);174  return rc;175}176 177 178/**************************** sqlite3_value_  *******************************179** The following routines extract information from a Mem or sqlite3_value180** structure.181*/182const void *sqlite3_value_blob(sqlite3_value *pVal){183  Mem *p = (Mem*)pVal;184  if( p->flags & (MEM_Blob|MEM_Str) ){185    if( ExpandBlob(p)!=SQLITE_OK ){186      assert( p->flags==MEM_Null && p->z==0 );187      return 0;188    }189    p->flags |= MEM_Blob;190    return p->n ? p->z : 0;191  }else{192    return sqlite3_value_text(pVal);193  }194}195int sqlite3_value_bytes(sqlite3_value *pVal){196  return sqlite3ValueBytes(pVal, SQLITE_UTF8);197}198int sqlite3_value_bytes16(sqlite3_value *pVal){199  return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);200}201double sqlite3_value_double(sqlite3_value *pVal){202  return sqlite3VdbeRealValue((Mem*)pVal);203}204int sqlite3_value_int(sqlite3_value *pVal){205  return (int)sqlite3VdbeIntValue((Mem*)pVal);206}207sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){208  return sqlite3VdbeIntValue((Mem*)pVal);209}210unsigned int sqlite3_value_subtype(sqlite3_value *pVal){211  Mem *pMem = (Mem*)pVal;212  return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);213}214void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){215  Mem *p = (Mem*)pVal;216  if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==217                 (MEM_Null|MEM_Term|MEM_Subtype)218   && zPType!=0219   && p->eSubtype=='p'220   && strcmp(p->u.zPType, zPType)==0221  ){222    return (void*)p->z;223  }else{224    return 0;225  }226}227const unsigned char *sqlite3_value_text(sqlite3_value *pVal){228  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);229}230#ifndef SQLITE_OMIT_UTF16231const void *sqlite3_value_text16(sqlite3_value* pVal){232  return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);233}234const void *sqlite3_value_text16be(sqlite3_value *pVal){235  return sqlite3ValueText(pVal, SQLITE_UTF16BE);236}237const void *sqlite3_value_text16le(sqlite3_value *pVal){238  return sqlite3ValueText(pVal, SQLITE_UTF16LE);239}240#endif /* SQLITE_OMIT_UTF16 */241/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five242** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating243** point number string BLOB NULL244*/245int sqlite3_value_type(sqlite3_value* pVal){246  static const u8 aType[] = {247     SQLITE_BLOB,     /* 0x00 (not possible) */248     SQLITE_NULL,     /* 0x01 NULL */249     SQLITE_TEXT,     /* 0x02 TEXT */250     SQLITE_NULL,     /* 0x03 (not possible) */251     SQLITE_INTEGER,  /* 0x04 INTEGER */252     SQLITE_NULL,     /* 0x05 (not possible) */253     SQLITE_INTEGER,  /* 0x06 INTEGER + TEXT */254     SQLITE_NULL,     /* 0x07 (not possible) */255     SQLITE_FLOAT,    /* 0x08 FLOAT */256     SQLITE_NULL,     /* 0x09 (not possible) */257     SQLITE_FLOAT,    /* 0x0a FLOAT + TEXT */258     SQLITE_NULL,     /* 0x0b (not possible) */259     SQLITE_INTEGER,  /* 0x0c (not possible) */260     SQLITE_NULL,     /* 0x0d (not possible) */261     SQLITE_INTEGER,  /* 0x0e (not possible) */262     SQLITE_NULL,     /* 0x0f (not possible) */263     SQLITE_BLOB,     /* 0x10 BLOB */264     SQLITE_NULL,     /* 0x11 (not possible) */265     SQLITE_TEXT,     /* 0x12 (not possible) */266     SQLITE_NULL,     /* 0x13 (not possible) */267     SQLITE_INTEGER,  /* 0x14 INTEGER + BLOB */268     SQLITE_NULL,     /* 0x15 (not possible) */269     SQLITE_INTEGER,  /* 0x16 (not possible) */270     SQLITE_NULL,     /* 0x17 (not possible) */271     SQLITE_FLOAT,    /* 0x18 FLOAT + BLOB */272     SQLITE_NULL,     /* 0x19 (not possible) */273     SQLITE_FLOAT,    /* 0x1a (not possible) */274     SQLITE_NULL,     /* 0x1b (not possible) */275     SQLITE_INTEGER,  /* 0x1c (not possible) */276     SQLITE_NULL,     /* 0x1d (not possible) */277     SQLITE_INTEGER,  /* 0x1e (not possible) */278     SQLITE_NULL,     /* 0x1f (not possible) */279     SQLITE_FLOAT,    /* 0x20 INTREAL */280     SQLITE_NULL,     /* 0x21 (not possible) */281     SQLITE_FLOAT,    /* 0x22 INTREAL + TEXT */282     SQLITE_NULL,     /* 0x23 (not possible) */283     SQLITE_FLOAT,    /* 0x24 (not possible) */284     SQLITE_NULL,     /* 0x25 (not possible) */285     SQLITE_FLOAT,    /* 0x26 (not possible) */286     SQLITE_NULL,     /* 0x27 (not possible) */287     SQLITE_FLOAT,    /* 0x28 (not possible) */288     SQLITE_NULL,     /* 0x29 (not possible) */289     SQLITE_FLOAT,    /* 0x2a (not possible) */290     SQLITE_NULL,     /* 0x2b (not possible) */291     SQLITE_FLOAT,    /* 0x2c (not possible) */292     SQLITE_NULL,     /* 0x2d (not possible) */293     SQLITE_FLOAT,    /* 0x2e (not possible) */294     SQLITE_NULL,     /* 0x2f (not possible) */295     SQLITE_BLOB,     /* 0x30 (not possible) */296     SQLITE_NULL,     /* 0x31 (not possible) */297     SQLITE_TEXT,     /* 0x32 (not possible) */298     SQLITE_NULL,     /* 0x33 (not possible) */299     SQLITE_FLOAT,    /* 0x34 (not possible) */300     SQLITE_NULL,     /* 0x35 (not possible) */301     SQLITE_FLOAT,    /* 0x36 (not possible) */302     SQLITE_NULL,     /* 0x37 (not possible) */303     SQLITE_FLOAT,    /* 0x38 (not possible) */304     SQLITE_NULL,     /* 0x39 (not possible) */305     SQLITE_FLOAT,    /* 0x3a (not possible) */306     SQLITE_NULL,     /* 0x3b (not possible) */307     SQLITE_FLOAT,    /* 0x3c (not possible) */308     SQLITE_NULL,     /* 0x3d (not possible) */309     SQLITE_FLOAT,    /* 0x3e (not possible) */310     SQLITE_NULL,     /* 0x3f (not possible) */311  };312#ifdef SQLITE_DEBUG313  {314    int eType = SQLITE_BLOB;315    if( pVal->flags & MEM_Null ){316      eType = SQLITE_NULL;317    }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){318      eType = SQLITE_FLOAT;319    }else if( pVal->flags & MEM_Int ){320      eType = SQLITE_INTEGER;321    }else if( pVal->flags & MEM_Str ){322      eType = SQLITE_TEXT;323    }324    assert( eType == aType[pVal->flags&MEM_AffMask] );325  }326#endif327  return aType[pVal->flags&MEM_AffMask];328}329int sqlite3_value_encoding(sqlite3_value *pVal){330  return pVal->enc;331}332 333/* Return true if a parameter to xUpdate represents an unchanged column */334int sqlite3_value_nochange(sqlite3_value *pVal){335  return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);336}337 338/* Return true if a parameter value originated from an sqlite3_bind() */339int sqlite3_value_frombind(sqlite3_value *pVal){340  return (pVal->flags&MEM_FromBind)!=0;341}342 343/* Make a copy of an sqlite3_value object344*/345sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){346  sqlite3_value *pNew;347  if( pOrig==0 ) return 0;348  pNew = sqlite3_malloc( sizeof(*pNew) );349  if( pNew==0 ) return 0;350  memset(pNew, 0, sizeof(*pNew));351  memcpy(pNew, pOrig, MEMCELLSIZE);352  pNew->flags &= ~MEM_Dyn;353  pNew->db = 0;354  if( pNew->flags&(MEM_Str|MEM_Blob) ){355    pNew->flags &= ~(MEM_Static|MEM_Dyn);356    pNew->flags |= MEM_Ephem;357    if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){358      sqlite3ValueFree(pNew);359      pNew = 0;360    }361  }else if( pNew->flags & MEM_Null ){362    /* Do not duplicate pointer values */363    pNew->flags &= ~(MEM_Term|MEM_Subtype);364  }365  return pNew;366}367 368/* Destroy an sqlite3_value object previously obtained from369** sqlite3_value_dup().370*/371void sqlite3_value_free(sqlite3_value *pOld){372  sqlite3ValueFree(pOld);373}374 375 376/**************************** sqlite3_result_  *******************************377** The following routines are used by user-defined functions to specify378** the function result.379**380** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the381** result as a string or blob.  Appropriate errors are set if the string/blob382** is too big or if an OOM occurs.383**384** The invokeValueDestructor(P,X) routine invokes destructor function X()385** on value P if P is not going to be used and need to be destroyed.386*/387static void setResultStrOrError(388  sqlite3_context *pCtx,  /* Function context */389  const char *z,          /* String pointer */390  int n,                  /* Bytes in string, or negative */391  u8 enc,                 /* Encoding of z.  0 for BLOBs */392  void (*xDel)(void*)     /* Destructor function */393){394  Mem *pOut = pCtx->pOut;395  int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);396  if( rc ){397    if( rc==SQLITE_TOOBIG ){398      sqlite3_result_error_toobig(pCtx);399    }else{400      /* The only errors possible from sqlite3VdbeMemSetStr are401      ** SQLITE_TOOBIG and SQLITE_NOMEM */402      assert( rc==SQLITE_NOMEM );403      sqlite3_result_error_nomem(pCtx);404    }405    return;406  }407  sqlite3VdbeChangeEncoding(pOut, pCtx->enc);408  if( sqlite3VdbeMemTooBig(pOut) ){409    sqlite3_result_error_toobig(pCtx);410  }411}412static int invokeValueDestructor(413  const void *p,             /* Value to destroy */414  void (*xDel)(void*),       /* The destructor */415  sqlite3_context *pCtx      /* Set a SQLITE_TOOBIG error if not NULL */416){417  assert( xDel!=SQLITE_DYNAMIC );418  if( xDel==0 ){419    /* noop */420  }else if( xDel==SQLITE_TRANSIENT ){421    /* noop */422  }else{423    xDel((void*)p);424  }425#ifdef SQLITE_ENABLE_API_ARMOR426  if( pCtx!=0 ){427    sqlite3_result_error_toobig(pCtx);428  }429#else430  assert( pCtx!=0 );431  sqlite3_result_error_toobig(pCtx);432#endif433  return SQLITE_TOOBIG;434}435void sqlite3_result_blob(436  sqlite3_context *pCtx,437  const void *z,438  int n,439  void (*xDel)(void *)440){441#ifdef SQLITE_ENABLE_API_ARMOR442  if( pCtx==0 || n<0 ){443    invokeValueDestructor(z, xDel, pCtx);444    return;445  }446#endif447  assert( n>=0 );448  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );449  setResultStrOrError(pCtx, z, n, 0, xDel);450}451void sqlite3_result_blob64(452  sqlite3_context *pCtx,453  const void *z,454  sqlite3_uint64 n,455  void (*xDel)(void *)456){457  assert( xDel!=SQLITE_DYNAMIC );458#ifdef SQLITE_ENABLE_API_ARMOR459  if( pCtx==0 ){460    invokeValueDestructor(z, xDel, 0);461    return;462  }463#endif464  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );465  if( n>0x7fffffff ){466    (void)invokeValueDestructor(z, xDel, pCtx);467  }else{468    setResultStrOrError(pCtx, z, (int)n, 0, xDel);469  }470}471void sqlite3_result_double(sqlite3_context *pCtx, double rVal){472#ifdef SQLITE_ENABLE_API_ARMOR473  if( pCtx==0 ) return;474#endif475  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );476  sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);477}478void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){479#ifdef SQLITE_ENABLE_API_ARMOR480  if( pCtx==0 ) return;481#endif482  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );483  pCtx->isError = SQLITE_ERROR;484  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);485}486#ifndef SQLITE_OMIT_UTF16487void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){488#ifdef SQLITE_ENABLE_API_ARMOR489  if( pCtx==0 ) return;490#endif491  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );492  pCtx->isError = SQLITE_ERROR;493  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);494}495#endif496void sqlite3_result_int(sqlite3_context *pCtx, int iVal){497#ifdef SQLITE_ENABLE_API_ARMOR498  if( pCtx==0 ) return;499#endif500  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );501  sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);502}503void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){504#ifdef SQLITE_ENABLE_API_ARMOR505  if( pCtx==0 ) return;506#endif507  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );508  sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);509}510void sqlite3_result_null(sqlite3_context *pCtx){511#ifdef SQLITE_ENABLE_API_ARMOR512  if( pCtx==0 ) return;513#endif514  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );515  sqlite3VdbeMemSetNull(pCtx->pOut);516}517void sqlite3_result_pointer(518  sqlite3_context *pCtx,519  void *pPtr,520  const char *zPType,521  void (*xDestructor)(void*)522){523  Mem *pOut;524#ifdef SQLITE_ENABLE_API_ARMOR525  if( pCtx==0 ){526    invokeValueDestructor(pPtr, xDestructor, 0);527    return;528  }529#endif530  pOut = pCtx->pOut;531  assert( sqlite3_mutex_held(pOut->db->mutex) );532  sqlite3VdbeMemRelease(pOut);533  pOut->flags = MEM_Null;534  sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);535}536void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){537  Mem *pOut;538#ifdef SQLITE_ENABLE_API_ARMOR539  if( pCtx==0 ) return;540#endif541#if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0542  if( pCtx->pFunc!=0543   && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0544  ){545    char zErr[200];546    sqlite3_snprintf(sizeof(zErr), zErr,547                     "misuse of sqlite3_result_subtype() by %s()", 548                     pCtx->pFunc->zName);549    sqlite3_result_error(pCtx, zErr, -1);550    return;551  }552#endif /* SQLITE_STRICT_SUBTYPE */553  pOut = pCtx->pOut;554  assert( sqlite3_mutex_held(pOut->db->mutex) );555  pOut->eSubtype = eSubtype & 0xff;556  pOut->flags |= MEM_Subtype;557}558void sqlite3_result_text(559  sqlite3_context *pCtx,560  const char *z,561  int n,562  void (*xDel)(void *)563){564#ifdef SQLITE_ENABLE_API_ARMOR565  if( pCtx==0 ){566    invokeValueDestructor(z, xDel, 0);567    return;568  }569#endif570  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );571  setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);572}573void sqlite3_result_text64(574  sqlite3_context *pCtx,575  const char *z,576  sqlite3_uint64 n,577  void (*xDel)(void *),578  unsigned char enc579){580#ifdef SQLITE_ENABLE_API_ARMOR581  if( pCtx==0 ){582    invokeValueDestructor(z, xDel, 0);583    return;584  }585#endif586  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );587  assert( xDel!=SQLITE_DYNAMIC );588  if( enc!=SQLITE_UTF8 ){589    if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;590    n &= ~(u64)1;591  }592  if( n>0x7fffffff ){593    (void)invokeValueDestructor(z, xDel, pCtx);594  }else{595    setResultStrOrError(pCtx, z, (int)n, enc, xDel);596    sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut);597  }598}599#ifndef SQLITE_OMIT_UTF16600void sqlite3_result_text16(601  sqlite3_context *pCtx,602  const void *z,603  int n,604  void (*xDel)(void *)605){606  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );607  setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE, xDel);608}609void sqlite3_result_text16be(610  sqlite3_context *pCtx,611  const void *z,612  int n,613  void (*xDel)(void *)614){615  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );616  setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE, xDel);617}618void sqlite3_result_text16le(619  sqlite3_context *pCtx,620  const void *z,621  int n,622  void (*xDel)(void *)623){624  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );625  setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE, xDel);626}627#endif /* SQLITE_OMIT_UTF16 */628void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){629  Mem *pOut;630 631#ifdef SQLITE_ENABLE_API_ARMOR632  if( pCtx==0 ) return;633  if( pValue==0 ){634    sqlite3_result_null(pCtx);635    return;636  }637#endif638  pOut = pCtx->pOut;639  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );640  sqlite3VdbeMemCopy(pOut, pValue);641  sqlite3VdbeChangeEncoding(pOut, pCtx->enc);642  if( sqlite3VdbeMemTooBig(pOut) ){643    sqlite3_result_error_toobig(pCtx);644  }645}646void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){647  sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0);648}649int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){650  Mem *pOut;651 652#ifdef SQLITE_ENABLE_API_ARMOR653  if( pCtx==0 ) return SQLITE_MISUSE_BKPT;654#endif655  pOut = pCtx->pOut;656  assert( sqlite3_mutex_held(pOut->db->mutex) );657  if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){658    sqlite3_result_error_toobig(pCtx);659    return SQLITE_TOOBIG;660  }661#ifndef SQLITE_OMIT_INCRBLOB662  sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);663  return SQLITE_OK;664#else665  return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);666#endif667}668void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){669#ifdef SQLITE_ENABLE_API_ARMOR670  if( pCtx==0 ) return;671#endif672  pCtx->isError = errCode ? errCode : -1;673#ifdef SQLITE_DEBUG674  if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;675#endif676  if( pCtx->pOut->flags & MEM_Null ){677    setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,678                        SQLITE_STATIC);679  }680}681 682/* Force an SQLITE_TOOBIG error. */683void sqlite3_result_error_toobig(sqlite3_context *pCtx){684#ifdef SQLITE_ENABLE_API_ARMOR685  if( pCtx==0 ) return;686#endif687  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );688  pCtx->isError = SQLITE_TOOBIG;689  sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,690                       SQLITE_UTF8, SQLITE_STATIC);691}692 693/* An SQLITE_NOMEM error. */694void sqlite3_result_error_nomem(sqlite3_context *pCtx){695#ifdef SQLITE_ENABLE_API_ARMOR696  if( pCtx==0 ) return;697#endif698  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );699  sqlite3VdbeMemSetNull(pCtx->pOut);700  pCtx->isError = SQLITE_NOMEM_BKPT;701  sqlite3OomFault(pCtx->pOut->db);702}703 704#ifndef SQLITE_UNTESTABLE705/* Force the INT64 value currently stored as the result to be706** a MEM_IntReal value.  See the SQLITE_TESTCTRL_RESULT_INTREAL707** test-control.708*/709void sqlite3ResultIntReal(sqlite3_context *pCtx){710  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );711  if( pCtx->pOut->flags & MEM_Int ){712    pCtx->pOut->flags &= ~MEM_Int;713    pCtx->pOut->flags |= MEM_IntReal;714  }715}716#endif717 718 719/*720** This function is called after a transaction has been committed. It721** invokes callbacks registered with sqlite3_wal_hook() as required.722*/723static int doWalCallbacks(sqlite3 *db){724  int rc = SQLITE_OK;725#ifndef SQLITE_OMIT_WAL726  int i;727  for(i=0; i<db->nDb; i++){728    Btree *pBt = db->aDb[i].pBt;729    if( pBt ){730      int nEntry;731      sqlite3BtreeEnter(pBt);732      nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));733      sqlite3BtreeLeave(pBt);734      if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){735        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);736      }737    }738  }739#else740  UNUSED_PARAMETER(db);741#endif742  return rc;743}744 745 746/*747** Execute the statement pStmt, either until a row of data is ready, the748** statement is completely executed or an error occurs.749**750** This routine implements the bulk of the logic behind the sqlite_step()751** API.  The only thing omitted is the automatic recompile if a752** schema change has occurred.  That detail is handled by the753** outer sqlite3_step() wrapper procedure.754*/755static int sqlite3Step(Vdbe *p){756  sqlite3 *db;757  int rc;758 759  assert(p);760  db = p->db;761  if( p->eVdbeState!=VDBE_RUN_STATE ){762    restart_step:763    if( p->eVdbeState==VDBE_READY_STATE ){764      if( p->expired ){765        p->rc = SQLITE_SCHEMA;766        rc = SQLITE_ERROR;767        if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){768          /* If this statement was prepared using saved SQL and an769          ** error has occurred, then return the error code in p->rc to the770          ** caller. Set the error code in the database handle to the same771          ** value.772          */773          rc = sqlite3VdbeTransferError(p);774        }775        goto end_of_step;776      }777 778      /* If there are no other statements currently running, then779      ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt780      ** from interrupting a statement that has not yet started.781      */782      if( db->nVdbeActive==0 ){783        AtomicStore(&db->u1.isInterrupted, 0);784      }785 786      assert( db->nVdbeWrite>0 || db->autoCommit==0787          || ((db->nDeferredCons + db->nDeferredImmCons)==0)788      );789 790#ifndef SQLITE_OMIT_TRACE791      if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0792          && !db->init.busy && p->zSql ){793        sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);794      }else{795        assert( p->startTime==0 );796      }797#endif798 799      db->nVdbeActive++;800      if( p->readOnly==0 ) db->nVdbeWrite++;801      if( p->bIsReader ) db->nVdbeRead++;802      p->pc = 0;803      p->eVdbeState = VDBE_RUN_STATE;804    }else805 806    if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){807      /* We used to require that sqlite3_reset() be called before retrying808      ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning809      ** with version 3.7.0, we changed this so that sqlite3_reset() would810      ** be called automatically instead of throwing the SQLITE_MISUSE error.811      ** This "automatic-reset" change is not technically an incompatibility,812      ** since any application that receives an SQLITE_MISUSE is broken by813      ** definition.814      **815      ** Nevertheless, some published applications that were originally written816      ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE817      ** returns, and those were broken by the automatic-reset change.  As a818      ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the819      ** legacy behavior of returning SQLITE_MISUSE for cases where the820      ** previous sqlite3_step() returned something other than a SQLITE_LOCKED821      ** or SQLITE_BUSY error.822      */823#ifdef SQLITE_OMIT_AUTORESET824      if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){825        sqlite3_reset((sqlite3_stmt*)p);826      }else{827        return SQLITE_MISUSE_BKPT;828      }829#else830      sqlite3_reset((sqlite3_stmt*)p);831#endif832      assert( p->eVdbeState==VDBE_READY_STATE );833      goto restart_step;834    }835  }836 837#ifdef SQLITE_DEBUG838  p->rcApp = SQLITE_OK;839#endif840#ifndef SQLITE_OMIT_EXPLAIN841  if( p->explain ){842    rc = sqlite3VdbeList(p);843  }else844#endif /* SQLITE_OMIT_EXPLAIN */845  {846    db->nVdbeExec++;847    rc = sqlite3VdbeExec(p);848    db->nVdbeExec--;849  }850 851  if( rc==SQLITE_ROW ){852    assert( p->rc==SQLITE_OK );853    assert( db->mallocFailed==0 );854    db->errCode = SQLITE_ROW;855    return SQLITE_ROW;856  }else{857#ifndef SQLITE_OMIT_TRACE858    /* If the statement completed successfully, invoke the profile callback */859    checkProfileCallback(db, p);860#endif861    p->pResultRow = 0;862    if( rc==SQLITE_DONE && db->autoCommit ){863      assert( p->rc==SQLITE_OK );864      p->rc = doWalCallbacks(db);865      if( p->rc!=SQLITE_OK ){866        rc = SQLITE_ERROR;867      }868    }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){869      /* If this statement was prepared using saved SQL and an870      ** error has occurred, then return the error code in p->rc to the871      ** caller. Set the error code in the database handle to the same value.872      */873      rc = sqlite3VdbeTransferError(p);874    }875  }876 877  db->errCode = rc;878  if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){879    p->rc = SQLITE_NOMEM_BKPT;880    if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;881  }882end_of_step:883  /* There are only a limited number of result codes allowed from the884  ** statements prepared using the legacy sqlite3_prepare() interface */885  assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0886       || rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR887       || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE888  );889  return (rc&db->errMask);890}891 892/*893** This is the top-level implementation of sqlite3_step().  Call894** sqlite3Step() to do most of the work.  If a schema error occurs,895** call sqlite3Reprepare() and try again.896*/897int sqlite3_step(sqlite3_stmt *pStmt){898  int rc = SQLITE_OK;      /* Result from sqlite3Step() */899  Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */900  int cnt = 0;             /* Counter to prevent infinite loop of reprepares */901  sqlite3 *db;             /* The database connection */902 903  if( vdbeSafetyNotNull(v) ){904    return SQLITE_MISUSE_BKPT;905  }906  db = v->db;907  sqlite3_mutex_enter(db->mutex);908  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA909         && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){910    int savedPc = v->pc;911    rc = sqlite3Reprepare(v);912    if( rc!=SQLITE_OK ){913      /* This case occurs after failing to recompile an sql statement.914      ** The error message from the SQL compiler has already been loaded915      ** into the database handle. This block copies the error message916      ** from the database handle into the statement and sets the statement917      ** program counter to 0 to ensure that when the statement is918      ** finalized or reset the parser error message is available via919      ** sqlite3_errmsg() and sqlite3_errcode().920      */921      const char *zErr = (const char *)sqlite3_value_text(db->pErr);922      sqlite3DbFree(db, v->zErrMsg);923      if( !db->mallocFailed ){924        v->zErrMsg = sqlite3DbStrDup(db, zErr);925        v->rc = rc = sqlite3ApiExit(db, rc);926      } else {927        v->zErrMsg = 0;928        v->rc = rc = SQLITE_NOMEM_BKPT;929      }930      break;931    }932    sqlite3_reset(pStmt);933    if( savedPc>=0 ){934      /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and935      ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has936      ** already been done once on a prior invocation that failed due to937      ** SQLITE_SCHEMA.   tag-20220401a  */938      v->minWriteFileFormat = 254;939    }940    assert( v->expired==0 );941  }942  sqlite3_mutex_leave(db->mutex);943  return rc;944}945 946 947/*948** Extract the user data from a sqlite3_context structure and return a949** pointer to it.950*/951void *sqlite3_user_data(sqlite3_context *p){952#ifdef SQLITE_ENABLE_API_ARMOR953  if( p==0 ) return 0;954#endif955  assert( p && p->pFunc );956  return p->pFunc->pUserData;957}958 959/*960** Extract the user data from a sqlite3_context structure and return a961** pointer to it.962**963** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface964** returns a copy of the pointer to the database connection (the 1st965** parameter) of the sqlite3_create_function() and966** sqlite3_create_function16() routines that originally registered the967** application defined function.968*/969sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){970#ifdef SQLITE_ENABLE_API_ARMOR971  if( p==0 ) return 0;972#else973  assert( p && p->pOut );974#endif975  return p->pOut->db;976}977 978/*979** If this routine is invoked from within an xColumn method of a virtual980** table, then it returns true if and only if the the call is during an981** UPDATE operation and the value of the column will not be modified982** by the UPDATE.983**984** If this routine is called from any context other than within the985** xColumn method of a virtual table, then the return value is meaningless986** and arbitrary.987**988** Virtual table implements might use this routine to optimize their989** performance by substituting a NULL result, or some other light-weight990** value, as a signal to the xUpdate routine that the column is unchanged.991*/992int sqlite3_vtab_nochange(sqlite3_context *p){993#ifdef SQLITE_ENABLE_API_ARMOR994  if( p==0 ) return 0;995#else996  assert( p );997#endif998  return sqlite3_value_nochange(p->pOut);999}1000 1001/*1002** The destructor function for a ValueList object.  This needs to be1003** a separate function, unknowable to the application, to ensure that1004** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not1005** preceded by activation of IN processing via sqlite3_vtab_int() do not1006** try to access a fake ValueList object inserted by a hostile extension.1007*/1008void sqlite3VdbeValueListFree(void *pToDelete){1009  sqlite3_free(pToDelete);1010}1011 1012/*1013** Implementation of sqlite3_vtab_in_first() (if bNext==0) and1014** sqlite3_vtab_in_next() (if bNext!=0).1015*/1016static int valueFromValueList(1017  sqlite3_value *pVal,        /* Pointer to the ValueList object */1018  sqlite3_value **ppOut,      /* Store the next value from the list here */1019  int bNext                   /* 1 for _next(). 0 for _first() */1020){1021  int rc;1022  ValueList *pRhs;1023 1024  *ppOut = 0;1025  if( pVal==0 ) return SQLITE_MISUSE_BKPT;1026  if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){1027    return SQLITE_ERROR;1028  }else{1029    assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==1030                 (MEM_Null|MEM_Term|MEM_Subtype) );1031    assert( pVal->eSubtype=='p' );1032    assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 );1033    pRhs = (ValueList*)pVal->z;1034  }1035  if( bNext ){1036    rc = sqlite3BtreeNext(pRhs->pCsr, 0);1037  }else{1038    int dummy = 0;1039    rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);1040    assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );1041    if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;1042  }1043  if( rc==SQLITE_OK ){1044    u32 sz;       /* Size of current row in bytes */1045    Mem sMem;     /* Raw content of current row */1046    memset(&sMem, 0, sizeof(sMem));1047    sz = sqlite3BtreePayloadSize(pRhs->pCsr);1048    rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);1049    if( rc==SQLITE_OK ){1050      u8 *zBuf = (u8*)sMem.z;1051      u32 iSerial;1052      sqlite3_value *pOut = pRhs->pOut;1053      int iOff = 1 + getVarint32(&zBuf[1], iSerial);1054      sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut);1055      pOut->enc = ENC(pOut->db);1056      if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){1057        rc = SQLITE_NOMEM;1058      }else{1059        *ppOut = pOut;1060      }1061    }1062    sqlite3VdbeMemRelease(&sMem);1063  }1064  return rc;1065}1066 1067/*1068** Set the iterator value pVal to point to the first value in the set.1069** Set (*ppOut) to point to this value before returning.1070*/1071int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){1072  return valueFromValueList(pVal, ppOut, 0);1073}1074 1075/*1076** Set the iterator value pVal to point to the next value in the set.1077** Set (*ppOut) to point to this value before returning.1078*/1079int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){1080  return valueFromValueList(pVal, ppOut, 1);1081}1082 1083/*1084** Return the current time for a statement.  If the current time1085** is requested more than once within the same run of a single prepared1086** statement, the exact same time is returned for each invocation regardless1087** of the amount of time that elapses between invocations.  In other words,1088** the time returned is always the time of the first call.1089*/1090sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){1091  int rc;1092#ifndef SQLITE_ENABLE_STAT41093  sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;1094  assert( p->pVdbe!=0 );1095#else1096  sqlite3_int64 iTime = 0;1097  sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;1098#endif1099  if( *piTime==0 ){1100    rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);1101    if( rc ) *piTime = 0;1102  }1103  return *piTime;1104}1105 1106/*1107** Create a new aggregate context for p and return a pointer to1108** its pMem->z element.1109*/1110static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){1111  Mem *pMem = p->pMem;1112  assert( (pMem->flags & MEM_Agg)==0 );1113  if( nByte<=0 ){1114    sqlite3VdbeMemSetNull(pMem);1115    pMem->z = 0;1116  }else{1117    sqlite3VdbeMemClearAndResize(pMem, nByte);1118    pMem->flags = MEM_Agg;1119    pMem->u.pDef = p->pFunc;1120    if( pMem->z ){1121      memset(pMem->z, 0, nByte);1122    }1123  }1124  return (void*)pMem->z;1125}1126 1127/*1128** Allocate or return the aggregate context for a user function.  A new1129** context is allocated on the first call.  Subsequent calls return the1130** same context that was returned on prior calls.1131*/1132void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){1133  assert( p && p->pFunc && p->pFunc->xFinalize );1134  assert( sqlite3_mutex_held(p->pOut->db->mutex) );1135  testcase( nByte<0 );1136  if( (p->pMem->flags & MEM_Agg)==0 ){1137    return createAggContext(p, nByte);1138  }else{1139    return (void*)p->pMem->z;1140  }1141}1142 1143/*1144** Return the auxiliary data pointer, if any, for the iArg'th argument to1145** the user-function defined by pCtx.1146**1147** The left-most argument is 0.1148**1149** Undocumented behavior:  If iArg is negative then access a cache of1150** auxiliary data pointers that is available to all functions within a1151** single prepared statement.  The iArg values must match.1152*/1153void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){1154  AuxData *pAuxData;1155 1156#ifdef SQLITE_ENABLE_API_ARMOR1157  if( pCtx==0 ) return 0;1158#endif1159  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );1160#if SQLITE_ENABLE_STAT41161  if( pCtx->pVdbe==0 ) return 0;1162#else1163  assert( pCtx->pVdbe!=0 );1164#endif1165  for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){1166    if(  pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){1167      return pAuxData->pAux;1168    }1169  }1170  return 0;1171}1172 1173/*1174** Set the auxiliary data pointer and delete function, for the iArg'th1175** argument to the user-function defined by pCtx. Any previous value is1176** deleted by calling the delete function specified when it was set.1177**1178** The left-most argument is 0.1179**1180** Undocumented behavior:  If iArg is negative then make the data available1181** to all functions within the current prepared statement using iArg as an1182** access code.1183*/1184void sqlite3_set_auxdata(1185  sqlite3_context *pCtx,1186  int iArg,1187  void *pAux,1188  void (*xDelete)(void*)1189){1190  AuxData *pAuxData;1191  Vdbe *pVdbe;1192 1193#ifdef SQLITE_ENABLE_API_ARMOR1194  if( pCtx==0 ) return;1195#endif1196  pVdbe= pCtx->pVdbe;1197  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );1198#ifdef SQLITE_ENABLE_STAT41199  if( pVdbe==0 ) goto failed;1200#else

Showing the first 1,200 of 2605 lines. Download the file for the rest.