drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 26 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** 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 the |
| 14 | ** VDBE. |
| 15 | */ |
| 16 | #include "sqliteInt.h" |
| 17 | #include "vdbeInt.h" |
| 18 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 19 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 20 | /* |
| 21 | ** Return TRUE (non-zero) of the statement supplied as an argument needs |
| 22 | ** to be recompiled. A statement needs to be recompiled whenever the |
| 23 | ** execution environment changes in a way that would alter the program |
| 24 | ** that sqlite3_prepare() generates. For example, if new functions or |
| 25 | ** collating sequences are registered or if an authorizer function is |
| 26 | ** added or changed. |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 27 | */ |
| 28 | int sqlite3_expired(sqlite3_stmt *pStmt){ |
| 29 | Vdbe *p = (Vdbe*)pStmt; |
| 30 | return p==0 || p->expired; |
| 31 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 32 | #endif |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 33 | |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 34 | /* |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 35 | ** Check on a Vdbe to make sure it has not been finalized. Log |
| 36 | ** an error and return true if it has been finalized (or is otherwise |
| 37 | ** invalid). Return false if it is ok. |
| 38 | */ |
| 39 | static int vdbeSafety(Vdbe *p){ |
| 40 | if( p->db==0 ){ |
| 41 | sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); |
| 42 | return 1; |
| 43 | }else{ |
| 44 | return 0; |
| 45 | } |
| 46 | } |
| 47 | static int vdbeSafetyNotNull(Vdbe *p){ |
| 48 | if( p==0 ){ |
| 49 | sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); |
| 50 | return 1; |
| 51 | }else{ |
| 52 | return vdbeSafety(p); |
| 53 | } |
| 54 | } |
| 55 | |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 56 | #ifndef SQLITE_OMIT_TRACE |
| 57 | /* |
| 58 | ** Invoke the profile callback. This routine is only called if we already |
| 59 | ** know that the profile callback is defined and needs to be invoked. |
| 60 | */ |
| 61 | static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ |
| 62 | sqlite3_int64 iNow; |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 63 | sqlite3_int64 iElapse; |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 64 | assert( p->startTime>0 ); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 65 | assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 ); |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 66 | assert( db->init.busy==0 ); |
| 67 | assert( p->zSql!=0 ); |
| 68 | sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 69 | iElapse = (iNow - p->startTime)*1000000; |
| 70 | if( db->xProfile ){ |
| 71 | db->xProfile(db->pProfileArg, p->zSql, iElapse); |
| 72 | } |
| 73 | if( db->mTrace & SQLITE_TRACE_PROFILE ){ |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 74 | db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 75 | } |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 76 | p->startTime = 0; |
| 77 | } |
| 78 | /* |
| 79 | ** The checkProfileCallback(DB,P) macro checks to see if a profile callback |
| 80 | ** is needed, and it invokes the callback if it is needed. |
| 81 | */ |
| 82 | # define checkProfileCallback(DB,P) \ |
| 83 | if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } |
| 84 | #else |
| 85 | # define checkProfileCallback(DB,P) /*no-op*/ |
| 86 | #endif |
| 87 | |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 88 | /* |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 89 | ** The following routine destroys a virtual machine that is created by |
| 90 | ** the sqlite3_compile() routine. The integer returned is an SQLITE_ |
| 91 | ** success/failure code that describes the result of executing the virtual |
| 92 | ** machine. |
| 93 | ** |
| 94 | ** This routine sets the error code and string returned by |
| 95 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 96 | */ |
| 97 | int sqlite3_finalize(sqlite3_stmt *pStmt){ |
| 98 | int rc; |
| 99 | if( pStmt==0 ){ |
drh | 65bafa6 | 2010-09-29 01:54:00 +0000 | [diff] [blame] | 100 | /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL |
| 101 | ** pointer is a harmless no-op. */ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 102 | rc = SQLITE_OK; |
| 103 | }else{ |
| 104 | Vdbe *v = (Vdbe*)pStmt; |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 105 | sqlite3 *db = v->db; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 106 | if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 107 | sqlite3_mutex_enter(db->mutex); |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 108 | checkProfileCallback(db, v); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 109 | rc = sqlite3VdbeFinalize(v); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 110 | rc = sqlite3ApiExit(db, rc); |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 111 | sqlite3LeaveMutexAndCloseZombie(db); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 112 | } |
| 113 | return rc; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | ** Terminate the current execution of an SQL statement and reset it |
| 118 | ** back to its starting state so that it can be reused. A success code from |
| 119 | ** the prior execution is returned. |
| 120 | ** |
| 121 | ** This routine sets the error code and string returned by |
| 122 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 123 | */ |
| 124 | int sqlite3_reset(sqlite3_stmt *pStmt){ |
| 125 | int rc; |
| 126 | if( pStmt==0 ){ |
| 127 | rc = SQLITE_OK; |
| 128 | }else{ |
| 129 | Vdbe *v = (Vdbe*)pStmt; |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 130 | sqlite3 *db = v->db; |
| 131 | sqlite3_mutex_enter(db->mutex); |
| 132 | checkProfileCallback(db, v); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 133 | rc = sqlite3VdbeReset(v); |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 134 | sqlite3VdbeRewind(v); |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 135 | assert( (rc & (db->errMask))==rc ); |
| 136 | rc = sqlite3ApiExit(db, rc); |
| 137 | sqlite3_mutex_leave(db->mutex); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 138 | } |
| 139 | return rc; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | ** Set all the parameters in the compiled SQL statement to NULL. |
| 144 | */ |
| 145 | int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ |
| 146 | int i; |
| 147 | int rc = SQLITE_OK; |
drh | 7297d1f | 2008-05-09 14:39:44 +0000 | [diff] [blame] | 148 | Vdbe *p = (Vdbe*)pStmt; |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 149 | #if SQLITE_THREADSAFE |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 150 | sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; |
| 151 | #endif |
| 152 | sqlite3_mutex_enter(mutex); |
drh | 7297d1f | 2008-05-09 14:39:44 +0000 | [diff] [blame] | 153 | for(i=0; i<p->nVar; i++){ |
| 154 | sqlite3VdbeMemRelease(&p->aVar[i]); |
| 155 | p->aVar[i].flags = MEM_Null; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 156 | } |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 157 | assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); |
dan | bda4cb8 | 2017-02-23 16:30:16 +0000 | [diff] [blame] | 158 | if( p->expmask ){ |
dan | c94b859 | 2009-10-20 07:01:24 +0000 | [diff] [blame] | 159 | p->expired = 1; |
| 160 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 161 | sqlite3_mutex_leave(mutex); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 162 | return rc; |
| 163 | } |
| 164 | |
| 165 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 166 | /**************************** sqlite3_value_ ******************************* |
| 167 | ** The following routines extract information from a Mem or sqlite3_value |
| 168 | ** structure. |
| 169 | */ |
| 170 | const void *sqlite3_value_blob(sqlite3_value *pVal){ |
| 171 | Mem *p = (Mem*)pVal; |
| 172 | if( p->flags & (MEM_Blob|MEM_Str) ){ |
drh | ff535a2 | 2016-09-20 01:46:15 +0000 | [diff] [blame] | 173 | if( ExpandBlob(p)!=SQLITE_OK ){ |
dan | a4d5ae8 | 2015-07-24 16:24:37 +0000 | [diff] [blame] | 174 | assert( p->flags==MEM_Null && p->z==0 ); |
| 175 | return 0; |
| 176 | } |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 177 | p->flags |= MEM_Blob; |
drh | 4226253 | 2010-09-08 16:30:36 +0000 | [diff] [blame] | 178 | return p->n ? p->z : 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 179 | }else{ |
| 180 | return sqlite3_value_text(pVal); |
| 181 | } |
| 182 | } |
| 183 | int sqlite3_value_bytes(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 184 | return sqlite3ValueBytes(pVal, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 185 | } |
| 186 | int sqlite3_value_bytes16(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 187 | return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 188 | } |
| 189 | double sqlite3_value_double(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 190 | return sqlite3VdbeRealValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 191 | } |
| 192 | int sqlite3_value_int(sqlite3_value *pVal){ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 193 | return (int)sqlite3VdbeIntValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 194 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 195 | sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 196 | return sqlite3VdbeIntValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 197 | } |
drh | bcdf78a | 2015-09-10 20:34:56 +0000 | [diff] [blame] | 198 | unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ |
dan | 5b6c8e4 | 2016-01-30 15:46:03 +0000 | [diff] [blame] | 199 | Mem *pMem = (Mem*)pVal; |
| 200 | return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); |
drh | bcdf78a | 2015-09-10 20:34:56 +0000 | [diff] [blame] | 201 | } |
drh | 3a96a5d | 2017-06-30 23:09:03 +0000 | [diff] [blame^] | 202 | void *sqlite3_value_pointer(sqlite3_value *pVal){ |
| 203 | Mem *p = (Mem*)pVal; |
| 204 | if( (p->flags & MEM_TypeMask)==(MEM_Null|MEM_Subtype) && p->eSubtype=='p' ){ |
| 205 | return p->u.pPtr; |
| 206 | }else{ |
| 207 | return 0; |
| 208 | } |
| 209 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 210 | const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 211 | return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 212 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 213 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 214 | const void *sqlite3_value_text16(sqlite3_value* pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 215 | return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 216 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 217 | const void *sqlite3_value_text16be(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 218 | return sqlite3ValueText(pVal, SQLITE_UTF16BE); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 219 | } |
| 220 | const void *sqlite3_value_text16le(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 221 | return sqlite3ValueText(pVal, SQLITE_UTF16LE); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 222 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 223 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 22ec134 | 2015-02-27 00:33:15 +0000 | [diff] [blame] | 224 | /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five |
| 225 | ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating |
| 226 | ** point number string BLOB NULL |
| 227 | */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 228 | int sqlite3_value_type(sqlite3_value* pVal){ |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 229 | static const u8 aType[] = { |
| 230 | SQLITE_BLOB, /* 0x00 */ |
| 231 | SQLITE_NULL, /* 0x01 */ |
| 232 | SQLITE_TEXT, /* 0x02 */ |
| 233 | SQLITE_NULL, /* 0x03 */ |
| 234 | SQLITE_INTEGER, /* 0x04 */ |
| 235 | SQLITE_NULL, /* 0x05 */ |
| 236 | SQLITE_INTEGER, /* 0x06 */ |
| 237 | SQLITE_NULL, /* 0x07 */ |
| 238 | SQLITE_FLOAT, /* 0x08 */ |
| 239 | SQLITE_NULL, /* 0x09 */ |
| 240 | SQLITE_FLOAT, /* 0x0a */ |
| 241 | SQLITE_NULL, /* 0x0b */ |
| 242 | SQLITE_INTEGER, /* 0x0c */ |
| 243 | SQLITE_NULL, /* 0x0d */ |
| 244 | SQLITE_INTEGER, /* 0x0e */ |
| 245 | SQLITE_NULL, /* 0x0f */ |
| 246 | SQLITE_BLOB, /* 0x10 */ |
| 247 | SQLITE_NULL, /* 0x11 */ |
| 248 | SQLITE_TEXT, /* 0x12 */ |
| 249 | SQLITE_NULL, /* 0x13 */ |
| 250 | SQLITE_INTEGER, /* 0x14 */ |
| 251 | SQLITE_NULL, /* 0x15 */ |
| 252 | SQLITE_INTEGER, /* 0x16 */ |
| 253 | SQLITE_NULL, /* 0x17 */ |
| 254 | SQLITE_FLOAT, /* 0x18 */ |
| 255 | SQLITE_NULL, /* 0x19 */ |
| 256 | SQLITE_FLOAT, /* 0x1a */ |
| 257 | SQLITE_NULL, /* 0x1b */ |
| 258 | SQLITE_INTEGER, /* 0x1c */ |
| 259 | SQLITE_NULL, /* 0x1d */ |
| 260 | SQLITE_INTEGER, /* 0x1e */ |
| 261 | SQLITE_NULL, /* 0x1f */ |
| 262 | }; |
mistachkin | afc14f7 | 2014-03-05 01:29:18 +0000 | [diff] [blame] | 263 | return aType[pVal->flags&MEM_AffMask]; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 264 | } |
| 265 | |
drh | 4f03f41 | 2015-05-20 21:28:32 +0000 | [diff] [blame] | 266 | /* Make a copy of an sqlite3_value object |
| 267 | */ |
| 268 | sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ |
| 269 | sqlite3_value *pNew; |
| 270 | if( pOrig==0 ) return 0; |
| 271 | pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 272 | if( pNew==0 ) return 0; |
| 273 | memset(pNew, 0, sizeof(*pNew)); |
| 274 | memcpy(pNew, pOrig, MEMCELLSIZE); |
| 275 | pNew->flags &= ~MEM_Dyn; |
| 276 | pNew->db = 0; |
| 277 | if( pNew->flags&(MEM_Str|MEM_Blob) ){ |
drh | 10ca5b4 | 2015-05-22 21:04:54 +0000 | [diff] [blame] | 278 | pNew->flags &= ~(MEM_Static|MEM_Dyn); |
| 279 | pNew->flags |= MEM_Ephem; |
| 280 | if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ |
| 281 | sqlite3ValueFree(pNew); |
| 282 | pNew = 0; |
drh | 4f03f41 | 2015-05-20 21:28:32 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | return pNew; |
| 286 | } |
| 287 | |
| 288 | /* Destroy an sqlite3_value object previously obtained from |
| 289 | ** sqlite3_value_dup(). |
| 290 | */ |
| 291 | void sqlite3_value_free(sqlite3_value *pOld){ |
| 292 | sqlite3ValueFree(pOld); |
| 293 | } |
| 294 | |
| 295 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 296 | /**************************** sqlite3_result_ ******************************* |
| 297 | ** The following routines are used by user-defined functions to specify |
| 298 | ** the function result. |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 299 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 300 | ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 301 | ** result as a string or blob but if the string or blob is too large, it |
| 302 | ** then sets the error code to SQLITE_TOOBIG |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 303 | ** |
| 304 | ** The invokeValueDestructor(P,X) routine invokes destructor function X() |
| 305 | ** on value P is not going to be used and need to be destroyed. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 306 | */ |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 307 | static void setResultStrOrError( |
| 308 | sqlite3_context *pCtx, /* Function context */ |
| 309 | const char *z, /* String pointer */ |
| 310 | int n, /* Bytes in string, or negative */ |
| 311 | u8 enc, /* Encoding of z. 0 for BLOBs */ |
| 312 | void (*xDel)(void*) /* Destructor function */ |
| 313 | ){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 314 | if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){ |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 315 | sqlite3_result_error_toobig(pCtx); |
| 316 | } |
| 317 | } |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 318 | static int invokeValueDestructor( |
| 319 | const void *p, /* Value to destroy */ |
| 320 | void (*xDel)(void*), /* The destructor */ |
| 321 | sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */ |
| 322 | ){ |
drh | fc59a95 | 2014-09-11 23:34:55 +0000 | [diff] [blame] | 323 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 324 | if( xDel==0 ){ |
| 325 | /* noop */ |
| 326 | }else if( xDel==SQLITE_TRANSIENT ){ |
| 327 | /* noop */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 328 | }else{ |
| 329 | xDel((void*)p); |
| 330 | } |
| 331 | if( pCtx ) sqlite3_result_error_toobig(pCtx); |
| 332 | return SQLITE_TOOBIG; |
| 333 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 334 | void sqlite3_result_blob( |
| 335 | sqlite3_context *pCtx, |
| 336 | const void *z, |
| 337 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 338 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 339 | ){ |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame] | 340 | assert( n>=0 ); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 341 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 342 | setResultStrOrError(pCtx, z, n, 0, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 343 | } |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 344 | void sqlite3_result_blob64( |
| 345 | sqlite3_context *pCtx, |
| 346 | const void *z, |
| 347 | sqlite3_uint64 n, |
| 348 | void (*xDel)(void *) |
| 349 | ){ |
| 350 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | fc59a95 | 2014-09-11 23:34:55 +0000 | [diff] [blame] | 351 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 352 | if( n>0x7fffffff ){ |
| 353 | (void)invokeValueDestructor(z, xDel, pCtx); |
| 354 | }else{ |
| 355 | setResultStrOrError(pCtx, z, (int)n, 0, xDel); |
| 356 | } |
| 357 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 358 | void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 359 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 360 | sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 361 | } |
| 362 | void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 363 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 364 | pCtx->isError = SQLITE_ERROR; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 365 | pCtx->fErrorOrAux = 1; |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 366 | sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 367 | } |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 368 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 369 | void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 370 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 371 | pCtx->isError = SQLITE_ERROR; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 372 | pCtx->fErrorOrAux = 1; |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 373 | sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 374 | } |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 375 | #endif |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 376 | void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 377 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 378 | sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 379 | } |
| 380 | void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 381 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 382 | sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 383 | } |
| 384 | void sqlite3_result_null(sqlite3_context *pCtx){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 385 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 386 | sqlite3VdbeMemSetNull(pCtx->pOut); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 387 | } |
drh | 3a96a5d | 2017-06-30 23:09:03 +0000 | [diff] [blame^] | 388 | void sqlite3_result_pointer(sqlite3_context *pCtx, void *pPtr){ |
| 389 | Mem *pOut = pCtx->pOut; |
| 390 | assert( sqlite3_mutex_held(pOut->db->mutex) ); |
| 391 | sqlite3VdbeMemSetNull(pOut); |
| 392 | sqlite3VdbeMemSetPointer(pOut, pPtr); |
| 393 | } |
drh | bcdf78a | 2015-09-10 20:34:56 +0000 | [diff] [blame] | 394 | void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ |
dan | 5b6c8e4 | 2016-01-30 15:46:03 +0000 | [diff] [blame] | 395 | Mem *pOut = pCtx->pOut; |
| 396 | assert( sqlite3_mutex_held(pOut->db->mutex) ); |
| 397 | pOut->eSubtype = eSubtype & 0xff; |
| 398 | pOut->flags |= MEM_Subtype; |
drh | bcdf78a | 2015-09-10 20:34:56 +0000 | [diff] [blame] | 399 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 400 | void sqlite3_result_text( |
| 401 | sqlite3_context *pCtx, |
| 402 | const char *z, |
| 403 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 404 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 405 | ){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 406 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 407 | setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 408 | } |
drh | bbf483f | 2014-09-09 20:30:24 +0000 | [diff] [blame] | 409 | void sqlite3_result_text64( |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 410 | sqlite3_context *pCtx, |
| 411 | const char *z, |
| 412 | sqlite3_uint64 n, |
| 413 | void (*xDel)(void *), |
| 414 | unsigned char enc |
| 415 | ){ |
| 416 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | fc59a95 | 2014-09-11 23:34:55 +0000 | [diff] [blame] | 417 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | 79f7af9 | 2014-10-03 16:00:51 +0000 | [diff] [blame] | 418 | if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 419 | if( n>0x7fffffff ){ |
| 420 | (void)invokeValueDestructor(z, xDel, pCtx); |
| 421 | }else{ |
| 422 | setResultStrOrError(pCtx, z, (int)n, enc, xDel); |
| 423 | } |
| 424 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 425 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 426 | void sqlite3_result_text16( |
| 427 | sqlite3_context *pCtx, |
| 428 | const void *z, |
| 429 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 430 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 431 | ){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 432 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 433 | setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 434 | } |
| 435 | void sqlite3_result_text16be( |
| 436 | sqlite3_context *pCtx, |
| 437 | const void *z, |
| 438 | int n, |
| 439 | void (*xDel)(void *) |
| 440 | ){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 441 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 442 | setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 443 | } |
| 444 | void sqlite3_result_text16le( |
| 445 | sqlite3_context *pCtx, |
| 446 | const void *z, |
| 447 | int n, |
| 448 | void (*xDel)(void *) |
| 449 | ){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 450 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 451 | setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 452 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 453 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 454 | void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 455 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 456 | sqlite3VdbeMemCopy(pCtx->pOut, pValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 457 | } |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 458 | void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 459 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 460 | sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 461 | } |
dan | a4d5ae8 | 2015-07-24 16:24:37 +0000 | [diff] [blame] | 462 | int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ |
| 463 | Mem *pOut = pCtx->pOut; |
| 464 | assert( sqlite3_mutex_held(pOut->db->mutex) ); |
drh | 6bacdc2 | 2015-07-24 17:14:03 +0000 | [diff] [blame] | 465 | if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
dan | a4d5ae8 | 2015-07-24 16:24:37 +0000 | [diff] [blame] | 466 | return SQLITE_TOOBIG; |
| 467 | } |
| 468 | sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); |
| 469 | return SQLITE_OK; |
| 470 | } |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 471 | void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ |
| 472 | pCtx->isError = errCode; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 473 | pCtx->fErrorOrAux = 1; |
drh | 983b5ee | 2015-02-13 12:05:56 +0000 | [diff] [blame] | 474 | #ifdef SQLITE_DEBUG |
drh | 96f4ad2 | 2015-03-12 21:02:36 +0000 | [diff] [blame] | 475 | if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; |
drh | 983b5ee | 2015-02-13 12:05:56 +0000 | [diff] [blame] | 476 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 477 | if( pCtx->pOut->flags & MEM_Null ){ |
| 478 | sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 479 | SQLITE_UTF8, SQLITE_STATIC); |
| 480 | } |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 481 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 482 | |
drh | a0206bc | 2007-05-08 15:15:02 +0000 | [diff] [blame] | 483 | /* Force an SQLITE_TOOBIG error. */ |
| 484 | void sqlite3_result_error_toobig(sqlite3_context *pCtx){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 485 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 486 | pCtx->isError = SQLITE_TOOBIG; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 487 | pCtx->fErrorOrAux = 1; |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 488 | sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 489 | SQLITE_UTF8, SQLITE_STATIC); |
drh | a0206bc | 2007-05-08 15:15:02 +0000 | [diff] [blame] | 490 | } |
| 491 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 492 | /* An SQLITE_NOMEM error. */ |
| 493 | void sqlite3_result_error_nomem(sqlite3_context *pCtx){ |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 494 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 495 | sqlite3VdbeMemSetNull(pCtx->pOut); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 496 | pCtx->isError = SQLITE_NOMEM_BKPT; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 497 | pCtx->fErrorOrAux = 1; |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 498 | sqlite3OomFault(pCtx->pOut->db); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 499 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 500 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 501 | /* |
| 502 | ** This function is called after a transaction has been committed. It |
| 503 | ** invokes callbacks registered with sqlite3_wal_hook() as required. |
| 504 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 505 | static int doWalCallbacks(sqlite3 *db){ |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 506 | int rc = SQLITE_OK; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 507 | #ifndef SQLITE_OMIT_WAL |
| 508 | int i; |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 509 | for(i=0; i<db->nDb; i++){ |
| 510 | Btree *pBt = db->aDb[i].pBt; |
| 511 | if( pBt ){ |
drh | ef15c6e | 2014-12-12 01:27:17 +0000 | [diff] [blame] | 512 | int nEntry; |
| 513 | sqlite3BtreeEnter(pBt); |
| 514 | nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); |
| 515 | sqlite3BtreeLeave(pBt); |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 516 | if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 517 | rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 521 | #endif |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 522 | return rc; |
| 523 | } |
| 524 | |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 525 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 526 | /* |
| 527 | ** Execute the statement pStmt, either until a row of data is ready, the |
| 528 | ** statement is completely executed or an error occurs. |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 529 | ** |
| 530 | ** This routine implements the bulk of the logic behind the sqlite_step() |
| 531 | ** API. The only thing omitted is the automatic recompile if a |
| 532 | ** schema change has occurred. That detail is handled by the |
| 533 | ** outer sqlite3_step() wrapper procedure. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 534 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 535 | static int sqlite3Step(Vdbe *p){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 536 | sqlite3 *db; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 537 | int rc; |
| 538 | |
danielk1977 | a185ddb | 2007-11-15 16:04:15 +0000 | [diff] [blame] | 539 | assert(p); |
| 540 | if( p->magic!=VDBE_MAGIC_RUN ){ |
drh | 3674bfd | 2010-04-17 12:53:19 +0000 | [diff] [blame] | 541 | /* We used to require that sqlite3_reset() be called before retrying |
drh | 602acb4 | 2011-01-17 17:42:37 +0000 | [diff] [blame] | 542 | ** sqlite3_step() after any error or after SQLITE_DONE. But beginning |
| 543 | ** with version 3.7.0, we changed this so that sqlite3_reset() would |
| 544 | ** be called automatically instead of throwing the SQLITE_MISUSE error. |
| 545 | ** This "automatic-reset" change is not technically an incompatibility, |
| 546 | ** since any application that receives an SQLITE_MISUSE is broken by |
| 547 | ** definition. |
| 548 | ** |
| 549 | ** Nevertheless, some published applications that were originally written |
| 550 | ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 551 | ** returns, and those were broken by the automatic-reset change. As a |
drh | 602acb4 | 2011-01-17 17:42:37 +0000 | [diff] [blame] | 552 | ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the |
| 553 | ** legacy behavior of returning SQLITE_MISUSE for cases where the |
| 554 | ** previous sqlite3_step() returned something other than a SQLITE_LOCKED |
| 555 | ** or SQLITE_BUSY error. |
drh | 3674bfd | 2010-04-17 12:53:19 +0000 | [diff] [blame] | 556 | */ |
drh | 602acb4 | 2011-01-17 17:42:37 +0000 | [diff] [blame] | 557 | #ifdef SQLITE_OMIT_AUTORESET |
drh | 983b5ee | 2015-02-13 12:05:56 +0000 | [diff] [blame] | 558 | if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
drh | 602acb4 | 2011-01-17 17:42:37 +0000 | [diff] [blame] | 559 | sqlite3_reset((sqlite3_stmt*)p); |
| 560 | }else{ |
| 561 | return SQLITE_MISUSE_BKPT; |
| 562 | } |
| 563 | #else |
drh | 3674bfd | 2010-04-17 12:53:19 +0000 | [diff] [blame] | 564 | sqlite3_reset((sqlite3_stmt*)p); |
drh | 602acb4 | 2011-01-17 17:42:37 +0000 | [diff] [blame] | 565 | #endif |
drh | f1bfe9a | 2007-10-17 01:44:20 +0000 | [diff] [blame] | 566 | } |
| 567 | |
dan | 14d4cc4 | 2010-01-20 14:25:37 +0000 | [diff] [blame] | 568 | /* Check that malloc() has not failed. If it has, return early. */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 569 | db = p->db; |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 570 | if( db->mallocFailed ){ |
dan | 14d4cc4 | 2010-01-20 14:25:37 +0000 | [diff] [blame] | 571 | p->rc = SQLITE_NOMEM; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 572 | return SQLITE_NOMEM_BKPT; |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 573 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 574 | |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 575 | if( p->pc<=0 && p->expired ){ |
drh | bee8065 | 2010-02-25 21:27:58 +0000 | [diff] [blame] | 576 | p->rc = SQLITE_SCHEMA; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 577 | rc = SQLITE_ERROR; |
| 578 | goto end_of_step; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 579 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 580 | if( p->pc<0 ){ |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 581 | /* If there are no other statements currently running, then |
| 582 | ** reset the interrupt flag. This prevents a call to sqlite3_interrupt |
| 583 | ** from interrupting a statement that has not yet started. |
| 584 | */ |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 585 | if( db->nVdbeActive==0 ){ |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 586 | db->u1.isInterrupted = 0; |
| 587 | } |
| 588 | |
drh | 888e16e | 2013-07-09 13:05:49 +0000 | [diff] [blame] | 589 | assert( db->nVdbeWrite>0 || db->autoCommit==0 |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 590 | || (db->nDeferredCons==0 && db->nDeferredImmCons==0) |
| 591 | ); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 592 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 593 | #ifndef SQLITE_OMIT_TRACE |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 594 | if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0) |
| 595 | && !db->init.busy && p->zSql ){ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 596 | sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 597 | }else{ |
| 598 | assert( p->startTime==0 ); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 599 | } |
| 600 | #endif |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 601 | |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 602 | db->nVdbeActive++; |
| 603 | if( p->readOnly==0 ) db->nVdbeWrite++; |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 604 | if( p->bIsReader ) db->nVdbeRead++; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 605 | p->pc = 0; |
| 606 | } |
drh | 983b5ee | 2015-02-13 12:05:56 +0000 | [diff] [blame] | 607 | #ifdef SQLITE_DEBUG |
| 608 | p->rcApp = SQLITE_OK; |
| 609 | #endif |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 610 | #ifndef SQLITE_OMIT_EXPLAIN |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 611 | if( p->explain ){ |
| 612 | rc = sqlite3VdbeList(p); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 613 | }else |
| 614 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 615 | { |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 616 | db->nVdbeExec++; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 617 | rc = sqlite3VdbeExec(p); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 618 | db->nVdbeExec--; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 619 | } |
| 620 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 621 | #ifndef SQLITE_OMIT_TRACE |
drh | 201e0c6 | 2015-07-14 14:48:50 +0000 | [diff] [blame] | 622 | /* If the statement completed successfully, invoke the profile callback */ |
| 623 | if( rc!=SQLITE_ROW ) checkProfileCallback(db, p); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 624 | #endif |
| 625 | |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 626 | if( rc==SQLITE_DONE ){ |
| 627 | assert( p->rc==SQLITE_OK ); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 628 | p->rc = doWalCallbacks(db); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 629 | if( p->rc!=SQLITE_OK ){ |
| 630 | rc = SQLITE_ERROR; |
| 631 | } |
| 632 | } |
| 633 | |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 634 | db->errCode = rc; |
danielk1977 | 357864e | 2009-03-25 15:43:08 +0000 | [diff] [blame] | 635 | if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 636 | p->rc = SQLITE_NOMEM_BKPT; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 637 | } |
danielk1977 | 357864e | 2009-03-25 15:43:08 +0000 | [diff] [blame] | 638 | end_of_step: |
| 639 | /* At this point local variable rc holds the value that should be |
| 640 | ** returned if this statement was compiled using the legacy |
| 641 | ** sqlite3_prepare() interface. According to the docs, this can only |
| 642 | ** be one of the values in the first assert() below. Variable p->rc |
| 643 | ** contains the value that would be returned if sqlite3_finalize() |
| 644 | ** were called on statement p. |
| 645 | */ |
| 646 | assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 647 | || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE |
danielk1977 | 357864e | 2009-03-25 15:43:08 +0000 | [diff] [blame] | 648 | ); |
drh | 983b5ee | 2015-02-13 12:05:56 +0000 | [diff] [blame] | 649 | assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp ); |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 650 | if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 |
| 651 | && rc!=SQLITE_ROW |
| 652 | && rc!=SQLITE_DONE |
| 653 | ){ |
| 654 | /* If this statement was prepared using saved SQL and an |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 655 | ** error has occurred, then return the error code in p->rc to the |
danielk1977 | 357864e | 2009-03-25 15:43:08 +0000 | [diff] [blame] | 656 | ** caller. Set the error code in the database handle to the same value. |
| 657 | */ |
dan | 029ead6 | 2011-10-27 15:19:58 +0000 | [diff] [blame] | 658 | rc = sqlite3VdbeTransferError(p); |
danielk1977 | 357864e | 2009-03-25 15:43:08 +0000 | [diff] [blame] | 659 | } |
| 660 | return (rc&db->errMask); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* |
| 664 | ** This is the top-level implementation of sqlite3_step(). Call |
| 665 | ** sqlite3Step() to do most of the work. If a schema error occurs, |
| 666 | ** call sqlite3Reprepare() and try again. |
| 667 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 668 | int sqlite3_step(sqlite3_stmt *pStmt){ |
drh | a6129fa | 2010-02-24 17:15:19 +0000 | [diff] [blame] | 669 | int rc = SQLITE_OK; /* Result from sqlite3Step() */ |
| 670 | int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ |
| 671 | Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ |
| 672 | int cnt = 0; /* Counter to prevent infinite loop of reprepares */ |
| 673 | sqlite3 *db; /* The database connection */ |
| 674 | |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 675 | if( vdbeSafetyNotNull(v) ){ |
| 676 | return SQLITE_MISUSE_BKPT; |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 677 | } |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 678 | db = v->db; |
| 679 | sqlite3_mutex_enter(db->mutex); |
drh | 37f58e9 | 2012-09-04 21:34:26 +0000 | [diff] [blame] | 680 | v->doingRerun = 0; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 681 | while( (rc = sqlite3Step(v))==SQLITE_SCHEMA |
drh | 2c7946a | 2014-08-19 20:27:40 +0000 | [diff] [blame] | 682 | && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ |
| 683 | int savedPc = v->pc; |
| 684 | rc2 = rc = sqlite3Reprepare(v); |
| 685 | if( rc!=SQLITE_OK) break; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 686 | sqlite3_reset(pStmt); |
drh | 5f58ae7 | 2014-08-19 20:41:36 +0000 | [diff] [blame] | 687 | if( savedPc>=0 ) v->doingRerun = 1; |
dan | 08f5f4c | 2011-06-27 19:37:23 +0000 | [diff] [blame] | 688 | assert( v->expired==0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 689 | } |
drh | a3cc007 | 2013-12-13 16:23:55 +0000 | [diff] [blame] | 690 | if( rc2!=SQLITE_OK ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 691 | /* This case occurs after failing to recompile an sql statement. |
| 692 | ** The error message from the SQL compiler has already been loaded |
| 693 | ** into the database handle. This block copies the error message |
| 694 | ** from the database handle into the statement and sets the statement |
| 695 | ** program counter to 0 to ensure that when the statement is |
| 696 | ** finalized or reset the parser error message is available via |
| 697 | ** sqlite3_errmsg() and sqlite3_errcode(). |
| 698 | */ |
| 699 | const char *zErr = (const char *)sqlite3_value_text(db->pErr); |
| 700 | sqlite3DbFree(db, v->zErrMsg); |
| 701 | if( !db->mallocFailed ){ |
| 702 | v->zErrMsg = sqlite3DbStrDup(db, zErr); |
drh | a6129fa | 2010-02-24 17:15:19 +0000 | [diff] [blame] | 703 | v->rc = rc2; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 704 | } else { |
| 705 | v->zErrMsg = 0; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 706 | v->rc = rc = SQLITE_NOMEM_BKPT; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | rc = sqlite3ApiExit(db, rc); |
| 710 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 76e8d1a | 2006-01-18 18:22:43 +0000 | [diff] [blame] | 711 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 712 | } |
| 713 | |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 714 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 715 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 716 | ** Extract the user data from a sqlite3_context structure and return a |
| 717 | ** pointer to it. |
| 718 | */ |
| 719 | void *sqlite3_user_data(sqlite3_context *p){ |
| 720 | assert( p && p->pFunc ); |
| 721 | return p->pFunc->pUserData; |
| 722 | } |
| 723 | |
| 724 | /* |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 725 | ** Extract the user data from a sqlite3_context structure and return a |
| 726 | ** pointer to it. |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 727 | ** |
| 728 | ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface |
| 729 | ** returns a copy of the pointer to the database connection (the 1st |
| 730 | ** parameter) of the sqlite3_create_function() and |
| 731 | ** sqlite3_create_function16() routines that originally registered the |
| 732 | ** application defined function. |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 733 | */ |
| 734 | sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 735 | assert( p && p->pOut ); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 736 | return p->pOut->db; |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 740 | ** Return the current time for a statement. If the current time |
| 741 | ** is requested more than once within the same run of a single prepared |
| 742 | ** statement, the exact same time is returned for each invocation regardless |
| 743 | ** of the amount of time that elapses between invocations. In other words, |
| 744 | ** the time returned is always the time of the first call. |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 745 | */ |
| 746 | sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 747 | int rc; |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 748 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 3df79a9 | 2015-03-12 23:48:27 +0000 | [diff] [blame] | 749 | sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 750 | assert( p->pVdbe!=0 ); |
| 751 | #else |
drh | 3df79a9 | 2015-03-12 23:48:27 +0000 | [diff] [blame] | 752 | sqlite3_int64 iTime = 0; |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 753 | sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 754 | #endif |
drh | 3df79a9 | 2015-03-12 23:48:27 +0000 | [diff] [blame] | 755 | if( *piTime==0 ){ |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 756 | rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); |
| 757 | if( rc ) *piTime = 0; |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 758 | } |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 759 | return *piTime; |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /* |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 763 | ** The following is the implementation of an SQL function that always |
| 764 | ** fails with an error message stating that the function is used in the |
| 765 | ** wrong context. The sqlite3_overload_function() API might construct |
| 766 | ** SQL function that use this routine so that the functions will exist |
| 767 | ** for name resolution but are actually overloaded by the xFindFunction |
| 768 | ** method of virtual tables. |
| 769 | */ |
| 770 | void sqlite3InvalidFunction( |
| 771 | sqlite3_context *context, /* The function calling context */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 772 | int NotUsed, /* Number of arguments to the function */ |
| 773 | sqlite3_value **NotUsed2 /* Value of each argument */ |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 774 | ){ |
| 775 | const char *zName = context->pFunc->zName; |
| 776 | char *zErr; |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 777 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 778 | zErr = sqlite3_mprintf( |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 779 | "unable to use function %s in the requested context", zName); |
| 780 | sqlite3_result_error(context, zErr, -1); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 781 | sqlite3_free(zErr); |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
drh | 9de4a17 | 2014-08-23 18:17:19 +0000 | [diff] [blame] | 785 | ** Create a new aggregate context for p and return a pointer to |
| 786 | ** its pMem->z element. |
| 787 | */ |
| 788 | static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ |
| 789 | Mem *pMem = p->pMem; |
| 790 | assert( (pMem->flags & MEM_Agg)==0 ); |
| 791 | if( nByte<=0 ){ |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 792 | sqlite3VdbeMemSetNull(pMem); |
drh | 9de4a17 | 2014-08-23 18:17:19 +0000 | [diff] [blame] | 793 | pMem->z = 0; |
| 794 | }else{ |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 795 | sqlite3VdbeMemClearAndResize(pMem, nByte); |
drh | 9de4a17 | 2014-08-23 18:17:19 +0000 | [diff] [blame] | 796 | pMem->flags = MEM_Agg; |
| 797 | pMem->u.pDef = p->pFunc; |
| 798 | if( pMem->z ){ |
| 799 | memset(pMem->z, 0, nByte); |
| 800 | } |
| 801 | } |
| 802 | return (void*)pMem->z; |
| 803 | } |
| 804 | |
| 805 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 806 | ** Allocate or return the aggregate context for a user function. A new |
| 807 | ** context is allocated on the first call. Subsequent calls return the |
| 808 | ** same context that was returned on prior calls. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 809 | */ |
| 810 | void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ |
drh | 2d80151 | 2016-01-14 22:19:58 +0000 | [diff] [blame] | 811 | assert( p && p->pFunc && p->pFunc->xFinalize ); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 812 | assert( sqlite3_mutex_held(p->pOut->db->mutex) ); |
drh | d68eee0 | 2009-12-11 03:44:18 +0000 | [diff] [blame] | 813 | testcase( nByte<0 ); |
drh | 9de4a17 | 2014-08-23 18:17:19 +0000 | [diff] [blame] | 814 | if( (p->pMem->flags & MEM_Agg)==0 ){ |
| 815 | return createAggContext(p, nByte); |
| 816 | }else{ |
| 817 | return (void*)p->pMem->z; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 818 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 822 | ** Return the auxiliary data pointer, if any, for the iArg'th argument to |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 823 | ** the user-function defined by pCtx. |
drh | f7fa4e7 | 2017-05-11 15:20:18 +0000 | [diff] [blame] | 824 | ** |
| 825 | ** The left-most argument is 0. |
| 826 | ** |
| 827 | ** Undocumented behavior: If iArg is negative then access a cache of |
| 828 | ** auxiliary data pointers that is available to all functions within a |
| 829 | ** single prepared statement. The iArg values must match. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 830 | */ |
| 831 | void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ |
dan | 0c54779 | 2013-07-18 17:12:08 +0000 | [diff] [blame] | 832 | AuxData *pAuxData; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 833 | |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 834 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 835 | #if SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 18bf807 | 2015-03-11 20:06:40 +0000 | [diff] [blame] | 836 | if( pCtx->pVdbe==0 ) return 0; |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 837 | #else |
| 838 | assert( pCtx->pVdbe!=0 ); |
| 839 | #endif |
drh | e694139 | 2017-05-10 19:42:52 +0000 | [diff] [blame] | 840 | for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ |
drh | f7fa4e7 | 2017-05-11 15:20:18 +0000 | [diff] [blame] | 841 | if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ |
drh | e694139 | 2017-05-10 19:42:52 +0000 | [diff] [blame] | 842 | return pAuxData->pAux; |
| 843 | } |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 844 | } |
drh | e694139 | 2017-05-10 19:42:52 +0000 | [diff] [blame] | 845 | return 0; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 849 | ** Set the auxiliary data pointer and delete function, for the iArg'th |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 850 | ** argument to the user-function defined by pCtx. Any previous value is |
| 851 | ** deleted by calling the delete function specified when it was set. |
drh | f7fa4e7 | 2017-05-11 15:20:18 +0000 | [diff] [blame] | 852 | ** |
| 853 | ** The left-most argument is 0. |
| 854 | ** |
| 855 | ** Undocumented behavior: If iArg is negative then make the data available |
| 856 | ** to all functions within the current prepared statement using iArg as an |
| 857 | ** access code. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 858 | */ |
| 859 | void sqlite3_set_auxdata( |
| 860 | sqlite3_context *pCtx, |
| 861 | int iArg, |
| 862 | void *pAux, |
| 863 | void (*xDelete)(void*) |
| 864 | ){ |
dan | 0c54779 | 2013-07-18 17:12:08 +0000 | [diff] [blame] | 865 | AuxData *pAuxData; |
| 866 | Vdbe *pVdbe = pCtx->pVdbe; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 867 | |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 868 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 869 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 18bf807 | 2015-03-11 20:06:40 +0000 | [diff] [blame] | 870 | if( pVdbe==0 ) goto failed; |
drh | a9e03b1 | 2015-03-12 06:46:52 +0000 | [diff] [blame] | 871 | #else |
| 872 | assert( pVdbe!=0 ); |
| 873 | #endif |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 874 | |
drh | e694139 | 2017-05-10 19:42:52 +0000 | [diff] [blame] | 875 | for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ |
drh | f7fa4e7 | 2017-05-11 15:20:18 +0000 | [diff] [blame] | 876 | if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ |
| 877 | break; |
| 878 | } |
dan | 0c54779 | 2013-07-18 17:12:08 +0000 | [diff] [blame] | 879 | } |
| 880 | if( pAuxData==0 ){ |
| 881 | pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); |
| 882 | if( !pAuxData ) goto failed; |
drh | e694139 | 2017-05-10 19:42:52 +0000 | [diff] [blame] | 883 | pAuxData->iAuxOp = pCtx->iOp; |
| 884 | pAuxData->iAuxArg = iArg; |
| 885 | pAuxData->pNextAux = pVdbe->pAuxData; |
dan | 0c54779 | 2013-07-18 17:12:08 +0000 | [diff] [blame] | 886 | pVdbe->pAuxData = pAuxData; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 887 | if( pCtx->fErrorOrAux==0 ){ |
| 888 | pCtx->isError = 0; |
| 889 | pCtx->fErrorOrAux = 1; |
| 890 | } |
drh | e694139 | 2017-05-10 19:42:52 +0000 | [diff] [blame] | 891 | }else if( pAuxData->xDeleteAux ){ |
| 892 | pAuxData->xDeleteAux(pAuxData->pAux); |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 893 | } |
dan | 0c54779 | 2013-07-18 17:12:08 +0000 | [diff] [blame] | 894 | |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 895 | pAuxData->pAux = pAux; |
drh | e694139 | 2017-05-10 19:42:52 +0000 | [diff] [blame] | 896 | pAuxData->xDeleteAux = xDelete; |
danielk1977 | e0fc526 | 2007-07-26 06:50:05 +0000 | [diff] [blame] | 897 | return; |
| 898 | |
| 899 | failed: |
| 900 | if( xDelete ){ |
| 901 | xDelete(pAux); |
| 902 | } |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 903 | } |
| 904 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 905 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 906 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 907 | ** Return the number of times the Step function of an aggregate has been |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 908 | ** called. |
| 909 | ** |
drh | cf85a51 | 2006-02-09 18:35:29 +0000 | [diff] [blame] | 910 | ** This function is deprecated. Do not use it for new code. It is |
| 911 | ** provide only to avoid breaking legacy code. New aggregate function |
| 912 | ** implementations should keep their own counts within their aggregate |
| 913 | ** context. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 914 | */ |
| 915 | int sqlite3_aggregate_count(sqlite3_context *p){ |
drh | 2d80151 | 2016-01-14 22:19:58 +0000 | [diff] [blame] | 916 | assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 917 | return p->pMem->n; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 918 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 919 | #endif |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 920 | |
| 921 | /* |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 922 | ** Return the number of columns in the result set for the statement pStmt. |
| 923 | */ |
| 924 | int sqlite3_column_count(sqlite3_stmt *pStmt){ |
| 925 | Vdbe *pVm = (Vdbe *)pStmt; |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 926 | return pVm ? pVm->nResColumn : 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | /* |
| 930 | ** Return the number of values available from the current row of the |
| 931 | ** currently executing statement pStmt. |
| 932 | */ |
| 933 | int sqlite3_data_count(sqlite3_stmt *pStmt){ |
| 934 | Vdbe *pVm = (Vdbe *)pStmt; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 935 | if( pVm==0 || pVm->pResultSet==0 ) return 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 936 | return pVm->nResColumn; |
| 937 | } |
| 938 | |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 939 | /* |
| 940 | ** Return a pointer to static memory containing an SQL NULL value. |
| 941 | */ |
| 942 | static const Mem *columnNullValue(void){ |
| 943 | /* Even though the Mem structure contains an element |
drh | b1a1c29 | 2014-03-05 12:47:55 +0000 | [diff] [blame] | 944 | ** of type i64, on certain architectures (x86) with certain compiler |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 945 | ** switches (-Os), gcc may align this Mem object on a 4-byte boundary |
| 946 | ** instead of an 8-byte one. This all works fine, except that when |
| 947 | ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s |
| 948 | ** that a Mem structure is located on an 8-byte boundary. To prevent |
drh | b1a1c29 | 2014-03-05 12:47:55 +0000 | [diff] [blame] | 949 | ** these assert()s from failing, when building with SQLITE_DEBUG defined |
| 950 | ** using gcc, we force nullMem to be 8-byte aligned using the magical |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 951 | ** __attribute__((aligned(8))) macro. */ |
| 952 | static const Mem nullMem |
| 953 | #if defined(SQLITE_DEBUG) && defined(__GNUC__) |
drh | b1a1c29 | 2014-03-05 12:47:55 +0000 | [diff] [blame] | 954 | __attribute__((aligned(8))) |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 955 | #endif |
drh | 035e563 | 2014-09-16 14:16:31 +0000 | [diff] [blame] | 956 | = { |
drh | 3329a63 | 2014-09-18 01:21:43 +0000 | [diff] [blame] | 957 | /* .u = */ {0}, |
drh | 8faee87 | 2015-09-19 18:08:13 +0000 | [diff] [blame] | 958 | /* .flags = */ (u16)MEM_Null, |
| 959 | /* .enc = */ (u8)0, |
| 960 | /* .eSubtype = */ (u8)0, |
| 961 | /* .n = */ (int)0, |
| 962 | /* .z = */ (char*)0, |
| 963 | /* .zMalloc = */ (char*)0, |
| 964 | /* .szMalloc = */ (int)0, |
| 965 | /* .uTemp = */ (u32)0, |
| 966 | /* .db = */ (sqlite3*)0, |
| 967 | /* .xDel = */ (void(*)(void*))0, |
drh | b1a1c29 | 2014-03-05 12:47:55 +0000 | [diff] [blame] | 968 | #ifdef SQLITE_DEBUG |
drh | 8faee87 | 2015-09-19 18:08:13 +0000 | [diff] [blame] | 969 | /* .pScopyFrom = */ (Mem*)0, |
| 970 | /* .pFiller = */ (void*)0, |
drh | b1a1c29 | 2014-03-05 12:47:55 +0000 | [diff] [blame] | 971 | #endif |
drh | 035e563 | 2014-09-16 14:16:31 +0000 | [diff] [blame] | 972 | }; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 973 | return &nullMem; |
| 974 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 975 | |
| 976 | /* |
| 977 | ** Check to see if column iCol of the given statement is valid. If |
| 978 | ** it is, return a pointer to the Mem for the value of that column. |
| 979 | ** If iCol is not valid, return a pointer to a Mem which has a value |
| 980 | ** of NULL. |
| 981 | */ |
| 982 | static Mem *columnMem(sqlite3_stmt *pStmt, int i){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 983 | Vdbe *pVm; |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 984 | Mem *pOut; |
| 985 | |
| 986 | pVm = (Vdbe *)pStmt; |
drh | 28f1701 | 2016-09-22 21:37:18 +0000 | [diff] [blame] | 987 | if( pVm==0 ) return (Mem*)columnNullValue(); |
| 988 | assert( pVm->db ); |
| 989 | sqlite3_mutex_enter(pVm->db->mutex); |
| 990 | if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 991 | pOut = &pVm->pResultSet[i]; |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 992 | }else{ |
drh | 28f1701 | 2016-09-22 21:37:18 +0000 | [diff] [blame] | 993 | sqlite3Error(pVm->db, SQLITE_RANGE); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 994 | pOut = (Mem*)columnNullValue(); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 995 | } |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 996 | return pOut; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 997 | } |
| 998 | |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 999 | /* |
| 1000 | ** This function is called after invoking an sqlite3_value_XXX function on a |
| 1001 | ** column value (i.e. a value returned by evaluating an SQL expression in the |
| 1002 | ** select list of a SELECT statement) that may cause a malloc() failure. If |
| 1003 | ** malloc() has failed, the threads mallocFailed flag is cleared and the result |
| 1004 | ** code of statement pStmt set to SQLITE_NOMEM. |
| 1005 | ** |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 1006 | ** Specifically, this is called from within: |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1007 | ** |
| 1008 | ** sqlite3_column_int() |
| 1009 | ** sqlite3_column_int64() |
| 1010 | ** sqlite3_column_text() |
| 1011 | ** sqlite3_column_text16() |
| 1012 | ** sqlite3_column_real() |
| 1013 | ** sqlite3_column_bytes() |
| 1014 | ** sqlite3_column_bytes16() |
drh | 4226253 | 2010-09-08 16:30:36 +0000 | [diff] [blame] | 1015 | ** sqiite3_column_blob() |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1016 | */ |
| 1017 | static void columnMallocFailure(sqlite3_stmt *pStmt) |
| 1018 | { |
| 1019 | /* If malloc() failed during an encoding conversion within an |
| 1020 | ** sqlite3_column_XXX API, then set the return code of the statement to |
| 1021 | ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR |
| 1022 | ** and _finalize() will return NOMEM. |
| 1023 | */ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1024 | Vdbe *p = (Vdbe *)pStmt; |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 1025 | if( p ){ |
drh | 28f1701 | 2016-09-22 21:37:18 +0000 | [diff] [blame] | 1026 | assert( p->db!=0 ); |
| 1027 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 1028 | p->rc = sqlite3ApiExit(p->db, p->rc); |
| 1029 | sqlite3_mutex_leave(p->db->mutex); |
| 1030 | } |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1033 | /**************************** sqlite3_column_ ******************************* |
| 1034 | ** The following routines are used to access elements of the current row |
| 1035 | ** in the result set. |
| 1036 | */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1037 | const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1038 | const void *val; |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1039 | val = sqlite3_value_blob( columnMem(pStmt,i) ); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 1040 | /* Even though there is no encoding conversion, value_blob() might |
| 1041 | ** need to call malloc() to expand the result of a zeroblob() |
| 1042 | ** expression. |
| 1043 | */ |
| 1044 | columnMallocFailure(pStmt); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1045 | return val; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1046 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1047 | int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1048 | int val = sqlite3_value_bytes( columnMem(pStmt,i) ); |
| 1049 | columnMallocFailure(pStmt); |
| 1050 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1051 | } |
| 1052 | int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1053 | int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); |
| 1054 | columnMallocFailure(pStmt); |
| 1055 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1056 | } |
| 1057 | double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1058 | double val = sqlite3_value_double( columnMem(pStmt,i) ); |
| 1059 | columnMallocFailure(pStmt); |
| 1060 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1061 | } |
| 1062 | int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1063 | int val = sqlite3_value_int( columnMem(pStmt,i) ); |
| 1064 | columnMallocFailure(pStmt); |
| 1065 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1066 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 1067 | sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1068 | sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); |
| 1069 | columnMallocFailure(pStmt); |
| 1070 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1071 | } |
| 1072 | const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1073 | const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); |
| 1074 | columnMallocFailure(pStmt); |
| 1075 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1076 | } |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 1077 | sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ |
danielk1977 | d0ffa1e | 2008-10-13 10:37:49 +0000 | [diff] [blame] | 1078 | Mem *pOut = columnMem(pStmt, i); |
| 1079 | if( pOut->flags&MEM_Static ){ |
| 1080 | pOut->flags &= ~MEM_Static; |
| 1081 | pOut->flags |= MEM_Ephem; |
| 1082 | } |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 1083 | columnMallocFailure(pStmt); |
danielk1977 | d0ffa1e | 2008-10-13 10:37:49 +0000 | [diff] [blame] | 1084 | return (sqlite3_value *)pOut; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 1085 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1086 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1087 | const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1088 | const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); |
| 1089 | columnMallocFailure(pStmt); |
| 1090 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1091 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1092 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1093 | int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 1094 | int iType = sqlite3_value_type( columnMem(pStmt,i) ); |
| 1095 | columnMallocFailure(pStmt); |
| 1096 | return iType; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1099 | /* |
| 1100 | ** Convert the N-th element of pStmt->pColName[] into a string using |
| 1101 | ** xFunc() then return that string. If N is out of range, return 0. |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1102 | ** |
| 1103 | ** There are up to 5 names for each column. useType determines which |
| 1104 | ** name is returned. Here are the names: |
| 1105 | ** |
| 1106 | ** 0 The column name as it should be displayed for output |
| 1107 | ** 1 The datatype name for the column |
| 1108 | ** 2 The name of the database that the column derives from |
| 1109 | ** 3 The name of the table that the column derives from |
| 1110 | ** 4 The name of the table column that the result column derives from |
| 1111 | ** |
| 1112 | ** If the result is not a simple column reference (if it is an expression |
| 1113 | ** or a constant) then useTypes 2, 3, and 4 return NULL. |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1114 | */ |
| 1115 | static const void *columnName( |
| 1116 | sqlite3_stmt *pStmt, |
| 1117 | int N, |
| 1118 | const void *(*xFunc)(Mem*), |
| 1119 | int useType |
| 1120 | ){ |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 1121 | const void *ret; |
| 1122 | Vdbe *p; |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 1123 | int n; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 1124 | sqlite3 *db; |
| 1125 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1126 | if( pStmt==0 ){ |
| 1127 | (void)SQLITE_MISUSE_BKPT; |
| 1128 | return 0; |
| 1129 | } |
| 1130 | #endif |
| 1131 | ret = 0; |
| 1132 | p = (Vdbe *)pStmt; |
| 1133 | db = p->db; |
drh | 9b4ea4a | 2009-04-10 20:32:00 +0000 | [diff] [blame] | 1134 | assert( db!=0 ); |
| 1135 | n = sqlite3_column_count(pStmt); |
| 1136 | if( N<n && N>=0 ){ |
| 1137 | N += useType*n; |
| 1138 | sqlite3_mutex_enter(db->mutex); |
| 1139 | assert( db->mallocFailed==0 ); |
| 1140 | ret = xFunc(&p->aColName[N]); |
| 1141 | /* A malloc may have failed inside of the xFunc() call. If this |
| 1142 | ** is the case, clear the mallocFailed flag and return NULL. |
| 1143 | */ |
| 1144 | if( db->mallocFailed ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1145 | sqlite3OomClear(db); |
drh | 9b4ea4a | 2009-04-10 20:32:00 +0000 | [diff] [blame] | 1146 | ret = 0; |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 1147 | } |
drh | 9b4ea4a | 2009-04-10 20:32:00 +0000 | [diff] [blame] | 1148 | sqlite3_mutex_leave(db->mutex); |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1149 | } |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 1150 | return ret; |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | ** Return the name of the Nth column of the result set returned by SQL |
| 1155 | ** statement pStmt. |
| 1156 | */ |
| 1157 | const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1158 | return columnName( |
| 1159 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1160 | } |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1161 | #ifndef SQLITE_OMIT_UTF16 |
| 1162 | const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1163 | return columnName( |
| 1164 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1165 | } |
| 1166 | #endif |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 1169 | ** Constraint: If you have ENABLE_COLUMN_METADATA then you must |
| 1170 | ** not define OMIT_DECLTYPE. |
| 1171 | */ |
| 1172 | #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) |
| 1173 | # error "Must not define both SQLITE_OMIT_DECLTYPE \ |
| 1174 | and SQLITE_ENABLE_COLUMN_METADATA" |
| 1175 | #endif |
| 1176 | |
| 1177 | #ifndef SQLITE_OMIT_DECLTYPE |
| 1178 | /* |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1179 | ** Return the column declaration type (if applicable) of the 'i'th column |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1180 | ** of the result set of SQL statement pStmt. |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1181 | */ |
| 1182 | const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1183 | return columnName( |
| 1184 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1185 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1186 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 1187 | const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1188 | return columnName( |
| 1189 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1190 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1191 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 1192 | #endif /* SQLITE_OMIT_DECLTYPE */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1193 | |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 1194 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1195 | /* |
| 1196 | ** Return the name of the database from which a result column derives. |
| 1197 | ** NULL is returned if the result column is an expression or constant or |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1198 | ** anything else which is not an unambiguous reference to a database column. |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1199 | */ |
| 1200 | const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1201 | return columnName( |
| 1202 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1203 | } |
| 1204 | #ifndef SQLITE_OMIT_UTF16 |
| 1205 | const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1206 | return columnName( |
| 1207 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1208 | } |
| 1209 | #endif /* SQLITE_OMIT_UTF16 */ |
| 1210 | |
| 1211 | /* |
| 1212 | ** Return the name of the table from which a result column derives. |
| 1213 | ** NULL is returned if the result column is an expression or constant or |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1214 | ** anything else which is not an unambiguous reference to a database column. |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1215 | */ |
| 1216 | const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1217 | return columnName( |
| 1218 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1219 | } |
| 1220 | #ifndef SQLITE_OMIT_UTF16 |
| 1221 | const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1222 | return columnName( |
| 1223 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1224 | } |
| 1225 | #endif /* SQLITE_OMIT_UTF16 */ |
| 1226 | |
| 1227 | /* |
| 1228 | ** Return the name of the table column from which a result column derives. |
| 1229 | ** NULL is returned if the result column is an expression or constant or |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1230 | ** anything else which is not an unambiguous reference to a database column. |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1231 | */ |
| 1232 | const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1233 | return columnName( |
| 1234 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1235 | } |
| 1236 | #ifndef SQLITE_OMIT_UTF16 |
| 1237 | const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1238 | return columnName( |
| 1239 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1240 | } |
| 1241 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 1242 | #endif /* SQLITE_ENABLE_COLUMN_METADATA */ |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1243 | |
| 1244 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1245 | /******************************* sqlite3_bind_ *************************** |
| 1246 | ** |
| 1247 | ** Routines used to attach values to wildcards in a compiled SQL statement. |
| 1248 | */ |
| 1249 | /* |
| 1250 | ** Unbind the value bound to variable i in virtual machine p. This is the |
| 1251 | ** the same as binding a NULL value to the column. If the "i" parameter is |
| 1252 | ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
| 1253 | ** |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1254 | ** A successful evaluation of this routine acquires the mutex on p. |
| 1255 | ** the mutex is released if any kind of error occurs. |
| 1256 | ** |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1257 | ** The error code stored in database p->db is overwritten with the return |
| 1258 | ** value in any case. |
| 1259 | */ |
| 1260 | static int vdbeUnbind(Vdbe *p, int i){ |
| 1261 | Mem *pVar; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1262 | if( vdbeSafetyNotNull(p) ){ |
| 1263 | return SQLITE_MISUSE_BKPT; |
| 1264 | } |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1265 | sqlite3_mutex_enter(p->db->mutex); |
| 1266 | if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ |
drh | 13f40da | 2014-08-22 18:00:11 +0000 | [diff] [blame] | 1267 | sqlite3Error(p->db, SQLITE_MISUSE); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1268 | sqlite3_mutex_leave(p->db->mutex); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1269 | sqlite3_log(SQLITE_MISUSE, |
| 1270 | "bind on a busy prepared statement: [%s]", p->zSql); |
| 1271 | return SQLITE_MISUSE_BKPT; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1272 | } |
| 1273 | if( i<1 || i>p->nVar ){ |
drh | 13f40da | 2014-08-22 18:00:11 +0000 | [diff] [blame] | 1274 | sqlite3Error(p->db, SQLITE_RANGE); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1275 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1276 | return SQLITE_RANGE; |
| 1277 | } |
| 1278 | i--; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1279 | pVar = &p->aVar[i]; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1280 | sqlite3VdbeMemRelease(pVar); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1281 | pVar->flags = MEM_Null; |
drh | 13f40da | 2014-08-22 18:00:11 +0000 | [diff] [blame] | 1282 | sqlite3Error(p->db, SQLITE_OK); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1283 | |
dan | 1d2ce4f | 2009-10-19 18:11:09 +0000 | [diff] [blame] | 1284 | /* If the bit corresponding to this variable in Vdbe.expmask is set, then |
| 1285 | ** binding a new value to this variable invalidates the current query plan. |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 1286 | ** |
| 1287 | ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host |
| 1288 | ** parameter in the WHERE clause might influence the choice of query plan |
| 1289 | ** for a statement, then the statement will be automatically recompiled, |
| 1290 | ** as if there had been a schema change, on the first sqlite3_step() call |
| 1291 | ** following any change to the bindings of that parameter. |
dan | 1d2ce4f | 2009-10-19 18:11:09 +0000 | [diff] [blame] | 1292 | */ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 1293 | assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); |
drh | 2996796 | 2017-03-03 21:51:40 +0000 | [diff] [blame] | 1294 | if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1295 | p->expired = 1; |
| 1296 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1297 | return SQLITE_OK; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1301 | ** Bind a text or BLOB value. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1302 | */ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1303 | static int bindText( |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1304 | sqlite3_stmt *pStmt, /* The statement to bind against */ |
| 1305 | int i, /* Index of the parameter to bind */ |
| 1306 | const void *zData, /* Pointer to the data to be bound */ |
| 1307 | int nData, /* Number of bytes of data to be bound */ |
| 1308 | void (*xDel)(void*), /* Destructor for the data */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 1309 | u8 encoding /* Encoding for the data */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1310 | ){ |
| 1311 | Vdbe *p = (Vdbe *)pStmt; |
| 1312 | Mem *pVar; |
| 1313 | int rc; |
| 1314 | |
| 1315 | rc = vdbeUnbind(p, i); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1316 | if( rc==SQLITE_OK ){ |
| 1317 | if( zData!=0 ){ |
| 1318 | pVar = &p->aVar[i-1]; |
| 1319 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); |
| 1320 | if( rc==SQLITE_OK && encoding!=0 ){ |
| 1321 | rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); |
| 1322 | } |
drh | e70d01f | 2017-05-29 22:44:18 +0000 | [diff] [blame] | 1323 | if( rc ){ |
| 1324 | sqlite3Error(p->db, rc); |
| 1325 | rc = sqlite3ApiExit(p->db, rc); |
| 1326 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1327 | } |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1328 | sqlite3_mutex_leave(p->db->mutex); |
drh | 94bb2ba | 2010-10-14 01:16:32 +0000 | [diff] [blame] | 1329 | }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ |
drh | 6fec9ee | 2010-10-12 02:13:32 +0000 | [diff] [blame] | 1330 | xDel((void*)zData); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1331 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1332 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1333 | } |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1334 | |
| 1335 | |
| 1336 | /* |
| 1337 | ** Bind a blob value to an SQL statement variable. |
| 1338 | */ |
| 1339 | int sqlite3_bind_blob( |
| 1340 | sqlite3_stmt *pStmt, |
| 1341 | int i, |
| 1342 | const void *zData, |
| 1343 | int nData, |
| 1344 | void (*xDel)(void*) |
| 1345 | ){ |
drh | 5b081d8 | 2016-02-18 01:29:12 +0000 | [diff] [blame] | 1346 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1347 | if( nData<0 ) return SQLITE_MISUSE_BKPT; |
| 1348 | #endif |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1349 | return bindText(pStmt, i, zData, nData, xDel, 0); |
| 1350 | } |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 1351 | int sqlite3_bind_blob64( |
| 1352 | sqlite3_stmt *pStmt, |
| 1353 | int i, |
| 1354 | const void *zData, |
| 1355 | sqlite3_uint64 nData, |
| 1356 | void (*xDel)(void*) |
| 1357 | ){ |
drh | fc59a95 | 2014-09-11 23:34:55 +0000 | [diff] [blame] | 1358 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 1359 | if( nData>0x7fffffff ){ |
| 1360 | return invokeValueDestructor(zData, xDel, 0); |
| 1361 | }else{ |
drh | 3329a63 | 2014-09-18 01:21:43 +0000 | [diff] [blame] | 1362 | return bindText(pStmt, i, zData, (int)nData, xDel, 0); |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1365 | int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 1366 | int rc; |
| 1367 | Vdbe *p = (Vdbe *)pStmt; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1368 | rc = vdbeUnbind(p, i); |
| 1369 | if( rc==SQLITE_OK ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1370 | sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1371 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1372 | } |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1373 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1374 | } |
| 1375 | int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 1376 | return sqlite3_bind_int64(p, i, (i64)iValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1377 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 1378 | int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1379 | int rc; |
| 1380 | Vdbe *p = (Vdbe *)pStmt; |
| 1381 | rc = vdbeUnbind(p, i); |
| 1382 | if( rc==SQLITE_OK ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1383 | sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1384 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1385 | } |
| 1386 | return rc; |
| 1387 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1388 | int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ |
| 1389 | int rc; |
| 1390 | Vdbe *p = (Vdbe*)pStmt; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1391 | rc = vdbeUnbind(p, i); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1392 | if( rc==SQLITE_OK ){ |
| 1393 | sqlite3_mutex_leave(p->db->mutex); |
| 1394 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1395 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1396 | } |
drh | 3a96a5d | 2017-06-30 23:09:03 +0000 | [diff] [blame^] | 1397 | int sqlite3_bind_pointer(sqlite3_stmt *pStmt, int i, void *pPtr){ |
| 1398 | int rc; |
| 1399 | Vdbe *p = (Vdbe*)pStmt; |
| 1400 | rc = vdbeUnbind(p, i); |
| 1401 | if( rc==SQLITE_OK ){ |
| 1402 | sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr); |
| 1403 | sqlite3_mutex_leave(p->db->mutex); |
| 1404 | } |
| 1405 | return rc; |
| 1406 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1407 | int sqlite3_bind_text( |
| 1408 | sqlite3_stmt *pStmt, |
| 1409 | int i, |
| 1410 | const char *zData, |
| 1411 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1412 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1413 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1414 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1415 | } |
drh | bbf483f | 2014-09-09 20:30:24 +0000 | [diff] [blame] | 1416 | int sqlite3_bind_text64( |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 1417 | sqlite3_stmt *pStmt, |
| 1418 | int i, |
| 1419 | const char *zData, |
| 1420 | sqlite3_uint64 nData, |
| 1421 | void (*xDel)(void*), |
| 1422 | unsigned char enc |
| 1423 | ){ |
drh | fc59a95 | 2014-09-11 23:34:55 +0000 | [diff] [blame] | 1424 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 1425 | if( nData>0x7fffffff ){ |
| 1426 | return invokeValueDestructor(zData, xDel, 0); |
| 1427 | }else{ |
drh | fc59a95 | 2014-09-11 23:34:55 +0000 | [diff] [blame] | 1428 | if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; |
drh | 3329a63 | 2014-09-18 01:21:43 +0000 | [diff] [blame] | 1429 | return bindText(pStmt, i, zData, (int)nData, xDel, enc); |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1432 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1433 | int sqlite3_bind_text16( |
| 1434 | sqlite3_stmt *pStmt, |
| 1435 | int i, |
| 1436 | const void *zData, |
| 1437 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1438 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1439 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1440 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1441 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1442 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 1443 | int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ |
| 1444 | int rc; |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 1445 | switch( sqlite3_value_type((sqlite3_value*)pValue) ){ |
drh | 29def56 | 2009-04-14 12:43:33 +0000 | [diff] [blame] | 1446 | case SQLITE_INTEGER: { |
| 1447 | rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); |
| 1448 | break; |
| 1449 | } |
| 1450 | case SQLITE_FLOAT: { |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1451 | rc = sqlite3_bind_double(pStmt, i, pValue->u.r); |
drh | 29def56 | 2009-04-14 12:43:33 +0000 | [diff] [blame] | 1452 | break; |
| 1453 | } |
| 1454 | case SQLITE_BLOB: { |
| 1455 | if( pValue->flags & MEM_Zero ){ |
| 1456 | rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); |
| 1457 | }else{ |
| 1458 | rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); |
| 1459 | } |
| 1460 | break; |
| 1461 | } |
| 1462 | case SQLITE_TEXT: { |
drh | ac80db7 | 2009-04-14 12:58:20 +0000 | [diff] [blame] | 1463 | rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, |
| 1464 | pValue->enc); |
| 1465 | break; |
| 1466 | } |
| 1467 | default: { |
| 1468 | rc = sqlite3_bind_null(pStmt, i); |
drh | 29def56 | 2009-04-14 12:43:33 +0000 | [diff] [blame] | 1469 | break; |
| 1470 | } |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 1471 | } |
| 1472 | return rc; |
| 1473 | } |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 1474 | int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ |
| 1475 | int rc; |
| 1476 | Vdbe *p = (Vdbe *)pStmt; |
| 1477 | rc = vdbeUnbind(p, i); |
| 1478 | if( rc==SQLITE_OK ){ |
| 1479 | sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1480 | sqlite3_mutex_leave(p->db->mutex); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 1481 | } |
| 1482 | return rc; |
| 1483 | } |
dan | 80c0302 | 2015-07-24 17:36:34 +0000 | [diff] [blame] | 1484 | int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ |
| 1485 | int rc; |
| 1486 | Vdbe *p = (Vdbe *)pStmt; |
| 1487 | sqlite3_mutex_enter(p->db->mutex); |
| 1488 | if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 1489 | rc = SQLITE_TOOBIG; |
| 1490 | }else{ |
| 1491 | assert( (n & 0x7FFFFFFF)==n ); |
| 1492 | rc = sqlite3_bind_zeroblob(pStmt, i, n); |
| 1493 | } |
| 1494 | rc = sqlite3ApiExit(p->db, rc); |
| 1495 | sqlite3_mutex_leave(p->db->mutex); |
| 1496 | return rc; |
| 1497 | } |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 1498 | |
| 1499 | /* |
| 1500 | ** Return the number of wildcards that can be potentially bound to. |
| 1501 | ** This routine is added to support DBD::SQLite. |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 1502 | */ |
| 1503 | int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1504 | Vdbe *p = (Vdbe*)pStmt; |
| 1505 | return p ? p->nVar : 0; |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 1506 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1507 | |
| 1508 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1509 | ** Return the name of a wildcard parameter. Return NULL if the index |
| 1510 | ** is out of range or if the wildcard is unnamed. |
| 1511 | ** |
| 1512 | ** The result is always UTF-8. |
| 1513 | */ |
| 1514 | const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ |
| 1515 | Vdbe *p = (Vdbe*)pStmt; |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 1516 | if( p==0 ) return 0; |
| 1517 | return sqlite3VListNumToName(p->pVList, i); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1518 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1519 | |
| 1520 | /* |
| 1521 | ** Given a wildcard parameter name, return the index of the variable |
| 1522 | ** with that name. If there is no variable with the given name, |
| 1523 | ** return 0. |
| 1524 | */ |
drh | 5f18a22 | 2009-11-26 14:01:53 +0000 | [diff] [blame] | 1525 | int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 1526 | if( p==0 || zName==0 ) return 0; |
| 1527 | return sqlite3VListNameToNum(p->pVList, zName, nName); |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1528 | } |
drh | 5f18a22 | 2009-11-26 14:01:53 +0000 | [diff] [blame] | 1529 | int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ |
| 1530 | return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); |
| 1531 | } |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1532 | |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 1533 | /* |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1534 | ** Transfer all bindings from the first statement over to the second. |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1535 | */ |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 1536 | int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1537 | Vdbe *pFrom = (Vdbe*)pFromStmt; |
| 1538 | Vdbe *pTo = (Vdbe*)pToStmt; |
drh | 0f5ea0b | 2009-04-09 14:02:44 +0000 | [diff] [blame] | 1539 | int i; |
| 1540 | assert( pTo->db==pFrom->db ); |
| 1541 | assert( pTo->nVar==pFrom->nVar ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1542 | sqlite3_mutex_enter(pTo->db->mutex); |
drh | 0f5ea0b | 2009-04-09 14:02:44 +0000 | [diff] [blame] | 1543 | for(i=0; i<pFrom->nVar; i++){ |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 1544 | sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1545 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1546 | sqlite3_mutex_leave(pTo->db->mutex); |
drh | 0f5ea0b | 2009-04-09 14:02:44 +0000 | [diff] [blame] | 1547 | return SQLITE_OK; |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1548 | } |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 1549 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1550 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 1551 | /* |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 1552 | ** Deprecated external interface. Internal/core SQLite code |
| 1553 | ** should call sqlite3TransferBindings. |
drh | 0f5ea0b | 2009-04-09 14:02:44 +0000 | [diff] [blame] | 1554 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1555 | ** It is misuse to call this routine with statements from different |
drh | 0f5ea0b | 2009-04-09 14:02:44 +0000 | [diff] [blame] | 1556 | ** database connections. But as this is a deprecated interface, we |
| 1557 | ** will not bother to check for that condition. |
| 1558 | ** |
| 1559 | ** If the two statements contain a different number of bindings, then |
| 1560 | ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise |
| 1561 | ** SQLITE_OK is returned. |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 1562 | */ |
| 1563 | int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
drh | 0f5ea0b | 2009-04-09 14:02:44 +0000 | [diff] [blame] | 1564 | Vdbe *pFrom = (Vdbe*)pFromStmt; |
| 1565 | Vdbe *pTo = (Vdbe*)pToStmt; |
| 1566 | if( pFrom->nVar!=pTo->nVar ){ |
| 1567 | return SQLITE_ERROR; |
| 1568 | } |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 1569 | assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 ); |
dan | bda4cb8 | 2017-02-23 16:30:16 +0000 | [diff] [blame] | 1570 | if( pTo->expmask ){ |
dan | c94b859 | 2009-10-20 07:01:24 +0000 | [diff] [blame] | 1571 | pTo->expired = 1; |
| 1572 | } |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 1573 | assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 ); |
dan | bda4cb8 | 2017-02-23 16:30:16 +0000 | [diff] [blame] | 1574 | if( pFrom->expmask ){ |
dan | c94b859 | 2009-10-20 07:01:24 +0000 | [diff] [blame] | 1575 | pFrom->expired = 1; |
| 1576 | } |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 1577 | return sqlite3TransferBindings(pFromStmt, pToStmt); |
| 1578 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1579 | #endif |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 1580 | |
| 1581 | /* |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 1582 | ** Return the sqlite3* database handle to which the prepared statement given |
| 1583 | ** in the argument belongs. This is the same database handle that was |
| 1584 | ** the first argument to the sqlite3_prepare() that was used to create |
| 1585 | ** the statement in the first place. |
| 1586 | */ |
| 1587 | sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ |
| 1588 | return pStmt ? ((Vdbe*)pStmt)->db : 0; |
| 1589 | } |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 1590 | |
| 1591 | /* |
drh | f03d9cc | 2010-11-16 23:10:25 +0000 | [diff] [blame] | 1592 | ** Return true if the prepared statement is guaranteed to not modify the |
| 1593 | ** database. |
| 1594 | */ |
| 1595 | int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ |
| 1596 | return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; |
| 1597 | } |
| 1598 | |
| 1599 | /* |
drh | 2fb6693 | 2011-11-25 17:21:47 +0000 | [diff] [blame] | 1600 | ** Return true if the prepared statement is in need of being reset. |
| 1601 | */ |
| 1602 | int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ |
| 1603 | Vdbe *v = (Vdbe*)pStmt; |
drh | 76336d5 | 2016-10-01 11:39:53 +0000 | [diff] [blame] | 1604 | return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0; |
drh | 2fb6693 | 2011-11-25 17:21:47 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | /* |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 1608 | ** Return a pointer to the next prepared statement after pStmt associated |
| 1609 | ** with database connection pDb. If pStmt is NULL, return the first |
| 1610 | ** prepared statement for the database connection. Return NULL if there |
| 1611 | ** are no more. |
| 1612 | */ |
| 1613 | sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 1614 | sqlite3_stmt *pNext; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 1615 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1616 | if( !sqlite3SafetyCheckOk(pDb) ){ |
| 1617 | (void)SQLITE_MISUSE_BKPT; |
| 1618 | return 0; |
| 1619 | } |
| 1620 | #endif |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 1621 | sqlite3_mutex_enter(pDb->mutex); |
| 1622 | if( pStmt==0 ){ |
| 1623 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 1624 | }else{ |
| 1625 | pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| 1626 | } |
| 1627 | sqlite3_mutex_leave(pDb->mutex); |
| 1628 | return pNext; |
| 1629 | } |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 1630 | |
| 1631 | /* |
| 1632 | ** Return the value of a status counter for a prepared statement |
| 1633 | */ |
| 1634 | int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 1635 | Vdbe *pVdbe = (Vdbe*)pStmt; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 1636 | u32 v; |
| 1637 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1638 | if( !pStmt ){ |
| 1639 | (void)SQLITE_MISUSE_BKPT; |
| 1640 | return 0; |
| 1641 | } |
| 1642 | #endif |
drh | 3528f6b | 2017-05-31 16:21:54 +0000 | [diff] [blame] | 1643 | if( op==SQLITE_STMTSTATUS_MEMUSED ){ |
| 1644 | sqlite3 *db = pVdbe->db; |
| 1645 | sqlite3_mutex_enter(db->mutex); |
| 1646 | v = 0; |
| 1647 | db->pnBytesFreed = (int*)&v; |
| 1648 | sqlite3VdbeClearObject(db, pVdbe); |
| 1649 | sqlite3DbFree(db, pVdbe); |
| 1650 | db->pnBytesFreed = 0; |
| 1651 | sqlite3_mutex_leave(db->mutex); |
| 1652 | }else{ |
| 1653 | v = pVdbe->aCounter[op]; |
| 1654 | if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 1655 | } |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 1656 | return (int)v; |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 1657 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1658 | |
drh | 557341e | 2016-07-23 02:07:26 +0000 | [diff] [blame] | 1659 | /* |
| 1660 | ** Return the SQL associated with a prepared statement |
| 1661 | */ |
| 1662 | const char *sqlite3_sql(sqlite3_stmt *pStmt){ |
| 1663 | Vdbe *p = (Vdbe *)pStmt; |
| 1664 | return p ? p->zSql : 0; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | ** Return the SQL associated with a prepared statement with |
| 1669 | ** bound parameters expanded. Space to hold the returned string is |
| 1670 | ** obtained from sqlite3_malloc(). The caller is responsible for |
| 1671 | ** freeing the returned string by passing it to sqlite3_free(). |
| 1672 | ** |
| 1673 | ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of |
| 1674 | ** expanded bound parameters. |
| 1675 | */ |
| 1676 | char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ |
| 1677 | #ifdef SQLITE_OMIT_TRACE |
| 1678 | return 0; |
| 1679 | #else |
| 1680 | char *z = 0; |
| 1681 | const char *zSql = sqlite3_sql(pStmt); |
| 1682 | if( zSql ){ |
| 1683 | Vdbe *p = (Vdbe *)pStmt; |
| 1684 | sqlite3_mutex_enter(p->db->mutex); |
| 1685 | z = sqlite3VdbeExpandSql(p, zSql); |
| 1686 | sqlite3_mutex_leave(p->db->mutex); |
| 1687 | } |
| 1688 | return z; |
| 1689 | #endif |
| 1690 | } |
| 1691 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1692 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1693 | /* |
dan | 93bca69 | 2011-09-14 19:41:44 +0000 | [diff] [blame] | 1694 | ** Allocate and populate an UnpackedRecord structure based on the serialized |
| 1695 | ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure |
| 1696 | ** if successful, or a NULL pointer if an OOM error is encountered. |
| 1697 | */ |
| 1698 | static UnpackedRecord *vdbeUnpackRecord( |
| 1699 | KeyInfo *pKeyInfo, |
| 1700 | int nKey, |
| 1701 | const void *pKey |
| 1702 | ){ |
dan | 93bca69 | 2011-09-14 19:41:44 +0000 | [diff] [blame] | 1703 | UnpackedRecord *pRet; /* Return value */ |
| 1704 | |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 1705 | pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); |
dan | 93bca69 | 2011-09-14 19:41:44 +0000 | [diff] [blame] | 1706 | if( pRet ){ |
drh | c8d985e | 2013-12-14 18:24:46 +0000 | [diff] [blame] | 1707 | memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1)); |
dan | 93bca69 | 2011-09-14 19:41:44 +0000 | [diff] [blame] | 1708 | sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet); |
| 1709 | } |
| 1710 | return pRet; |
| 1711 | } |
| 1712 | |
| 1713 | /* |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1714 | ** This function is called from within a pre-update callback to retrieve |
| 1715 | ** a field of the row currently being updated or deleted. |
| 1716 | */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1717 | int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ |
| 1718 | PreUpdate *p = db->pPreUpdate; |
dan | 7271d7a | 2017-01-25 18:53:27 +0000 | [diff] [blame] | 1719 | Mem *pMem; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1720 | int rc = SQLITE_OK; |
| 1721 | |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1722 | /* Test that this call is being made from within an SQLITE_DELETE or |
| 1723 | ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1724 | if( !p || p->op==SQLITE_INSERT ){ |
| 1725 | rc = SQLITE_MISUSE_BKPT; |
| 1726 | goto preupdate_old_out; |
| 1727 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 1728 | if( p->pPk ){ |
| 1729 | iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx); |
| 1730 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1731 | if( iIdx>=p->pCsr->nField || iIdx<0 ){ |
| 1732 | rc = SQLITE_RANGE; |
| 1733 | goto preupdate_old_out; |
| 1734 | } |
| 1735 | |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1736 | /* If the old.* record has not yet been loaded into memory, do so now. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1737 | if( p->pUnpacked==0 ){ |
dan | 12ca0b5 | 2011-03-21 16:17:42 +0000 | [diff] [blame] | 1738 | u32 nRec; |
| 1739 | u8 *aRec; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1740 | |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 1741 | nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); |
dan | 12ca0b5 | 2011-03-21 16:17:42 +0000 | [diff] [blame] | 1742 | aRec = sqlite3DbMallocRaw(db, nRec); |
| 1743 | if( !aRec ) goto preupdate_old_out; |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 1744 | rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); |
dan | 12ca0b5 | 2011-03-21 16:17:42 +0000 | [diff] [blame] | 1745 | if( rc==SQLITE_OK ){ |
dan | 93bca69 | 2011-09-14 19:41:44 +0000 | [diff] [blame] | 1746 | p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); |
dan | 12ca0b5 | 2011-03-21 16:17:42 +0000 | [diff] [blame] | 1747 | if( !p->pUnpacked ) rc = SQLITE_NOMEM; |
| 1748 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1749 | if( rc!=SQLITE_OK ){ |
dan | 12ca0b5 | 2011-03-21 16:17:42 +0000 | [diff] [blame] | 1750 | sqlite3DbFree(db, aRec); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1751 | goto preupdate_old_out; |
| 1752 | } |
dan | 12ca0b5 | 2011-03-21 16:17:42 +0000 | [diff] [blame] | 1753 | p->aRecord = aRec; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
dan | 7271d7a | 2017-01-25 18:53:27 +0000 | [diff] [blame] | 1756 | pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; |
| 1757 | if( iIdx==p->pTab->iPKey ){ |
| 1758 | sqlite3VdbeMemSetInt64(pMem, p->iKey1); |
| 1759 | }else if( iIdx>=p->pUnpacked->nField ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1760 | *ppValue = (sqlite3_value *)columnNullValue(); |
dan | 7271d7a | 2017-01-25 18:53:27 +0000 | [diff] [blame] | 1761 | }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ |
| 1762 | if( pMem->flags & MEM_Int ){ |
| 1763 | sqlite3VdbeMemRealify(pMem); |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 1764 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | preupdate_old_out: |
drh | e1ed0b0 | 2014-08-26 02:15:07 +0000 | [diff] [blame] | 1768 | sqlite3Error(db, rc); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1769 | return sqlite3ApiExit(db, rc); |
| 1770 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1771 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1772 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1773 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1774 | /* |
| 1775 | ** This function is called from within a pre-update callback to retrieve |
| 1776 | ** the number of columns in the row being updated, deleted or inserted. |
| 1777 | */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1778 | int sqlite3_preupdate_count(sqlite3 *db){ |
| 1779 | PreUpdate *p = db->pPreUpdate; |
dan | e437ca5 | 2011-07-11 19:45:38 +0000 | [diff] [blame] | 1780 | return (p ? p->keyinfo.nField : 0); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1781 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1782 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1783 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1784 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1785 | /* |
dan | 1e7a2d4 | 2011-03-22 18:45:29 +0000 | [diff] [blame] | 1786 | ** This function is designed to be called from within a pre-update callback |
| 1787 | ** only. It returns zero if the change that caused the callback was made |
| 1788 | ** immediately by a user SQL statement. Or, if the change was made by a |
| 1789 | ** trigger program, it returns the number of trigger programs currently |
| 1790 | ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a |
| 1791 | ** top-level trigger etc.). |
| 1792 | ** |
| 1793 | ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL |
| 1794 | ** or SET DEFAULT action is considered a trigger. |
| 1795 | */ |
| 1796 | int sqlite3_preupdate_depth(sqlite3 *db){ |
| 1797 | PreUpdate *p = db->pPreUpdate; |
| 1798 | return (p ? p->v->nFrame : 0); |
| 1799 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1800 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 1e7a2d4 | 2011-03-22 18:45:29 +0000 | [diff] [blame] | 1801 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1802 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 1e7a2d4 | 2011-03-22 18:45:29 +0000 | [diff] [blame] | 1803 | /* |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1804 | ** This function is called from within a pre-update callback to retrieve |
| 1805 | ** a field of the row currently being updated or inserted. |
| 1806 | */ |
| 1807 | int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1808 | PreUpdate *p = db->pPreUpdate; |
| 1809 | int rc = SQLITE_OK; |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1810 | Mem *pMem; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1811 | |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1812 | if( !p || p->op==SQLITE_DELETE ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1813 | rc = SQLITE_MISUSE_BKPT; |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1814 | goto preupdate_new_out; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1815 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 1816 | if( p->pPk && p->op!=SQLITE_UPDATE ){ |
| 1817 | iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx); |
| 1818 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1819 | if( iIdx>=p->pCsr->nField || iIdx<0 ){ |
| 1820 | rc = SQLITE_RANGE; |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1821 | goto preupdate_new_out; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1822 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1823 | |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1824 | if( p->op==SQLITE_INSERT ){ |
| 1825 | /* For an INSERT, memory cell p->iNewReg contains the serialized record |
| 1826 | ** that is being inserted. Deserialize it. */ |
| 1827 | UnpackedRecord *pUnpack = p->pNewUnpacked; |
| 1828 | if( !pUnpack ){ |
| 1829 | Mem *pData = &p->v->aMem[p->iNewReg]; |
drh | ff535a2 | 2016-09-20 01:46:15 +0000 | [diff] [blame] | 1830 | rc = ExpandBlob(pData); |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1831 | if( rc!=SQLITE_OK ) goto preupdate_new_out; |
dan | 93bca69 | 2011-09-14 19:41:44 +0000 | [diff] [blame] | 1832 | pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1833 | if( !pUnpack ){ |
| 1834 | rc = SQLITE_NOMEM; |
| 1835 | goto preupdate_new_out; |
| 1836 | } |
| 1837 | p->pNewUnpacked = pUnpack; |
| 1838 | } |
dan | 2a86c19 | 2017-01-25 17:44:13 +0000 | [diff] [blame] | 1839 | pMem = &pUnpack->aMem[iIdx]; |
| 1840 | if( iIdx==p->pTab->iPKey ){ |
| 1841 | sqlite3VdbeMemSetInt64(pMem, p->iKey2); |
| 1842 | }else if( iIdx>=pUnpack->nField ){ |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1843 | pMem = (sqlite3_value *)columnNullValue(); |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1844 | } |
| 1845 | }else{ |
| 1846 | /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required |
| 1847 | ** value. Make a copy of the cell contents and return a pointer to it. |
| 1848 | ** It is not safe to return a pointer to the memory cell itself as the |
| 1849 | ** caller may modify the value text encoding. |
| 1850 | */ |
| 1851 | assert( p->op==SQLITE_UPDATE ); |
| 1852 | if( !p->aNew ){ |
| 1853 | p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); |
| 1854 | if( !p->aNew ){ |
| 1855 | rc = SQLITE_NOMEM; |
| 1856 | goto preupdate_new_out; |
| 1857 | } |
| 1858 | } |
drh | 304637c | 2011-03-18 16:47:27 +0000 | [diff] [blame] | 1859 | assert( iIdx>=0 && iIdx<p->pCsr->nField ); |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1860 | pMem = &p->aNew[iIdx]; |
| 1861 | if( pMem->flags==0 ){ |
dan | e43635a | 2016-10-21 21:21:45 +0000 | [diff] [blame] | 1862 | if( iIdx==p->pTab->iPKey ){ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 1863 | sqlite3VdbeMemSetInt64(pMem, p->iKey2); |
| 1864 | }else{ |
| 1865 | rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]); |
| 1866 | if( rc!=SQLITE_OK ) goto preupdate_new_out; |
| 1867 | } |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | *ppValue = pMem; |
| 1871 | |
| 1872 | preupdate_new_out: |
drh | e1ed0b0 | 2014-08-26 02:15:07 +0000 | [diff] [blame] | 1873 | sqlite3Error(db, rc); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1874 | return sqlite3ApiExit(db, rc); |
| 1875 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1876 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
drh | 04e8a58 | 2014-11-18 21:20:57 +0000 | [diff] [blame] | 1877 | |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 1878 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 1879 | /* |
| 1880 | ** Return status data for a single loop within query pStmt. |
| 1881 | */ |
| 1882 | int sqlite3_stmt_scanstatus( |
drh | d1a1c23 | 2014-11-03 16:35:55 +0000 | [diff] [blame] | 1883 | sqlite3_stmt *pStmt, /* Prepared statement being queried */ |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 1884 | int idx, /* Index of loop to report on */ |
drh | d1a1c23 | 2014-11-03 16:35:55 +0000 | [diff] [blame] | 1885 | int iScanStatusOp, /* Which metric to return */ |
| 1886 | void *pOut /* OUT: Write the answer here */ |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 1887 | ){ |
| 1888 | Vdbe *p = (Vdbe*)pStmt; |
dan | 037b532 | 2014-11-03 11:25:32 +0000 | [diff] [blame] | 1889 | ScanStatus *pScan; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 1890 | if( idx<0 || idx>=p->nScan ) return 1; |
| 1891 | pScan = &p->aScan[idx]; |
drh | d1a1c23 | 2014-11-03 16:35:55 +0000 | [diff] [blame] | 1892 | switch( iScanStatusOp ){ |
| 1893 | case SQLITE_SCANSTAT_NLOOP: { |
| 1894 | *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; |
| 1895 | break; |
| 1896 | } |
| 1897 | case SQLITE_SCANSTAT_NVISIT: { |
| 1898 | *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; |
| 1899 | break; |
| 1900 | } |
| 1901 | case SQLITE_SCANSTAT_EST: { |
drh | 518140e | 2014-11-06 03:55:10 +0000 | [diff] [blame] | 1902 | double r = 1.0; |
| 1903 | LogEst x = pScan->nEst; |
| 1904 | while( x<100 ){ |
| 1905 | x += 10; |
| 1906 | r *= 0.5; |
| 1907 | } |
| 1908 | *(double*)pOut = r*sqlite3LogEstToInt(x); |
drh | d1a1c23 | 2014-11-03 16:35:55 +0000 | [diff] [blame] | 1909 | break; |
| 1910 | } |
| 1911 | case SQLITE_SCANSTAT_NAME: { |
dan | d72219d | 2014-11-03 16:39:37 +0000 | [diff] [blame] | 1912 | *(const char**)pOut = pScan->zName; |
drh | d1a1c23 | 2014-11-03 16:35:55 +0000 | [diff] [blame] | 1913 | break; |
| 1914 | } |
| 1915 | case SQLITE_SCANSTAT_EXPLAIN: { |
| 1916 | if( pScan->addrExplain ){ |
| 1917 | *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; |
| 1918 | }else{ |
| 1919 | *(const char**)pOut = 0; |
| 1920 | } |
| 1921 | break; |
| 1922 | } |
drh | c6652b1 | 2014-11-06 04:42:20 +0000 | [diff] [blame] | 1923 | case SQLITE_SCANSTAT_SELECTID: { |
| 1924 | if( pScan->addrExplain ){ |
| 1925 | *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; |
| 1926 | }else{ |
| 1927 | *(int*)pOut = -1; |
| 1928 | } |
| 1929 | break; |
| 1930 | } |
drh | d1a1c23 | 2014-11-03 16:35:55 +0000 | [diff] [blame] | 1931 | default: { |
| 1932 | return 1; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 1933 | } |
| 1934 | } |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 1935 | return 0; |
| 1936 | } |
| 1937 | |
| 1938 | /* |
| 1939 | ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. |
| 1940 | */ |
| 1941 | void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ |
| 1942 | Vdbe *p = (Vdbe*)pStmt; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 1943 | memset(p->anExec, 0, p->nOp * sizeof(i64)); |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 1944 | } |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 1945 | #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ |