drh | 4f26d6c | 2004-05-26 23:25:30 | [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" |
dan | 2adb309 | 2022-12-06 18:48:06 | [diff] [blame] | 18 | #include "opcodes.h" |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 19 | |
shane | eec556d | 2008-10-12 00:27:53 | [diff] [blame] | 20 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | d89bd00 | 2005-01-22 03:03:54 | [diff] [blame] | 21 | /* |
| 22 | ** Return TRUE (non-zero) of the statement supplied as an argument needs |
| 23 | ** to be recompiled. A statement needs to be recompiled whenever the |
| 24 | ** execution environment changes in a way that would alter the program |
| 25 | ** that sqlite3_prepare() generates. For example, if new functions or |
| 26 | ** collating sequences are registered or if an authorizer function is |
| 27 | ** added or changed. |
drh | d89bd00 | 2005-01-22 03:03:54 | [diff] [blame] | 28 | */ |
| 29 | int sqlite3_expired(sqlite3_stmt *pStmt){ |
| 30 | Vdbe *p = (Vdbe*)pStmt; |
| 31 | return p==0 || p->expired; |
| 32 | } |
shane | eec556d | 2008-10-12 00:27:53 | [diff] [blame] | 33 | #endif |
drh | d89bd00 | 2005-01-22 03:03:54 | [diff] [blame] | 34 | |
drh | e30f442 | 2007-08-21 16:15:55 | [diff] [blame] | 35 | /* |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 36 | ** Check on a Vdbe to make sure it has not been finalized. Log |
| 37 | ** an error and return true if it has been finalized (or is otherwise |
| 38 | ** invalid). Return false if it is ok. |
| 39 | */ |
| 40 | static int vdbeSafety(Vdbe *p){ |
| 41 | if( p->db==0 ){ |
| 42 | sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); |
| 43 | return 1; |
| 44 | }else{ |
| 45 | return 0; |
| 46 | } |
| 47 | } |
| 48 | static int vdbeSafetyNotNull(Vdbe *p){ |
| 49 | if( p==0 ){ |
| 50 | sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); |
| 51 | return 1; |
| 52 | }else{ |
| 53 | return vdbeSafety(p); |
| 54 | } |
| 55 | } |
| 56 | |
drh | 201e0c6 | 2015-07-14 14:48:50 | [diff] [blame] | 57 | #ifndef SQLITE_OMIT_TRACE |
| 58 | /* |
| 59 | ** Invoke the profile callback. This routine is only called if we already |
| 60 | ** know that the profile callback is defined and needs to be invoked. |
| 61 | */ |
| 62 | static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ |
| 63 | sqlite3_int64 iNow; |
drh | 3d2a529 | 2016-07-13 22:55:01 | [diff] [blame] | 64 | sqlite3_int64 iElapse; |
drh | 201e0c6 | 2015-07-14 14:48:50 | [diff] [blame] | 65 | assert( p->startTime>0 ); |
drh | 201e0c6 | 2015-07-14 14:48:50 | [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 | [diff] [blame] | 69 | iElapse = (iNow - p->startTime)*1000000; |
drh | 07891f0 | 2019-04-14 00:40:29 | [diff] [blame] | 70 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 3d2a529 | 2016-07-13 22:55:01 | [diff] [blame] | 71 | if( db->xProfile ){ |
| 72 | db->xProfile(db->pProfileArg, p->zSql, iElapse); |
| 73 | } |
drh | 04c6747 | 2018-12-04 14:33:02 | [diff] [blame] | 74 | #endif |
drh | 3d2a529 | 2016-07-13 22:55:01 | [diff] [blame] | 75 | if( db->mTrace & SQLITE_TRACE_PROFILE ){ |
drh | 08b9208 | 2020-08-10 14:18:00 | [diff] [blame] | 76 | db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); |
drh | 3d2a529 | 2016-07-13 22:55:01 | [diff] [blame] | 77 | } |
drh | 201e0c6 | 2015-07-14 14:48:50 | [diff] [blame] | 78 | p->startTime = 0; |
| 79 | } |
| 80 | /* |
| 81 | ** The checkProfileCallback(DB,P) macro checks to see if a profile callback |
| 82 | ** is needed, and it invokes the callback if it is needed. |
| 83 | */ |
| 84 | # define checkProfileCallback(DB,P) \ |
| 85 | if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } |
| 86 | #else |
| 87 | # define checkProfileCallback(DB,P) /*no-op*/ |
| 88 | #endif |
| 89 | |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 90 | /* |
drh | e30f442 | 2007-08-21 16:15:55 | [diff] [blame] | 91 | ** The following routine destroys a virtual machine that is created by |
| 92 | ** the sqlite3_compile() routine. The integer returned is an SQLITE_ |
| 93 | ** success/failure code that describes the result of executing the virtual |
| 94 | ** machine. |
| 95 | ** |
| 96 | ** This routine sets the error code and string returned by |
| 97 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 98 | */ |
| 99 | int sqlite3_finalize(sqlite3_stmt *pStmt){ |
| 100 | int rc; |
| 101 | if( pStmt==0 ){ |
drh | 65bafa6 | 2010-09-29 01:54:00 | [diff] [blame] | 102 | /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL |
| 103 | ** pointer is a harmless no-op. */ |
drh | e30f442 | 2007-08-21 16:15:55 | [diff] [blame] | 104 | rc = SQLITE_OK; |
| 105 | }else{ |
| 106 | Vdbe *v = (Vdbe*)pStmt; |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 107 | sqlite3 *db = v->db; |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 108 | if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; |
drh | 4245c40 | 2012-06-02 14:32:21 | [diff] [blame] | 109 | sqlite3_mutex_enter(db->mutex); |
drh | 201e0c6 | 2015-07-14 14:48:50 | [diff] [blame] | 110 | checkProfileCallback(db, v); |
drh | 2b294b5 | 2022-06-30 22:46:28 | [diff] [blame] | 111 | assert( v->eVdbeState>=VDBE_READY_STATE ); |
| 112 | rc = sqlite3VdbeReset(v); |
| 113 | sqlite3VdbeDelete(v); |
danielk1977 | 238746a | 2009-03-19 18:51:06 | [diff] [blame] | 114 | rc = sqlite3ApiExit(db, rc); |
drh | 4245c40 | 2012-06-02 14:32:21 | [diff] [blame] | 115 | sqlite3LeaveMutexAndCloseZombie(db); |
drh | e30f442 | 2007-08-21 16:15:55 | [diff] [blame] | 116 | } |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | ** Terminate the current execution of an SQL statement and reset it |
| 122 | ** back to its starting state so that it can be reused. A success code from |
| 123 | ** the prior execution is returned. |
| 124 | ** |
| 125 | ** This routine sets the error code and string returned by |
| 126 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 127 | */ |
| 128 | int sqlite3_reset(sqlite3_stmt *pStmt){ |
| 129 | int rc; |
| 130 | if( pStmt==0 ){ |
| 131 | rc = SQLITE_OK; |
| 132 | }else{ |
| 133 | Vdbe *v = (Vdbe*)pStmt; |
drh | 201e0c6 | 2015-07-14 14:48:50 | [diff] [blame] | 134 | sqlite3 *db = v->db; |
| 135 | sqlite3_mutex_enter(db->mutex); |
| 136 | checkProfileCallback(db, v); |
drh | c890fec | 2008-08-01 20:10:08 | [diff] [blame] | 137 | rc = sqlite3VdbeReset(v); |
drh | 124c0b4 | 2011-06-01 18:15:55 | [diff] [blame] | 138 | sqlite3VdbeRewind(v); |
drh | 201e0c6 | 2015-07-14 14:48:50 | [diff] [blame] | 139 | assert( (rc & (db->errMask))==rc ); |
| 140 | rc = sqlite3ApiExit(db, rc); |
| 141 | sqlite3_mutex_leave(db->mutex); |
drh | e30f442 | 2007-08-21 16:15:55 | [diff] [blame] | 142 | } |
| 143 | return rc; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | ** Set all the parameters in the compiled SQL statement to NULL. |
| 148 | */ |
| 149 | int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ |
| 150 | int i; |
| 151 | int rc = SQLITE_OK; |
drh | 7297d1f | 2008-05-09 14:39:44 | [diff] [blame] | 152 | Vdbe *p = (Vdbe*)pStmt; |
drh | 18472fa | 2008-10-07 15:25:48 | [diff] [blame] | 153 | #if SQLITE_THREADSAFE |
stephan | 4598b6e | 2023-10-22 13:09:37 | [diff] [blame] | 154 | sqlite3_mutex *mutex; |
| 155 | #endif |
| 156 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 157 | if( pStmt==0 ){ |
| 158 | return SQLITE_MISUSE_BKPT; |
| 159 | } |
| 160 | #endif |
| 161 | #if SQLITE_THREADSAFE |
| 162 | mutex = p->db->mutex; |
drh | 7e8b848 | 2008-01-23 03:03:05 | [diff] [blame] | 163 | #endif |
| 164 | sqlite3_mutex_enter(mutex); |
drh | 7297d1f | 2008-05-09 14:39:44 | [diff] [blame] | 165 | for(i=0; i<p->nVar; i++){ |
| 166 | sqlite3VdbeMemRelease(&p->aVar[i]); |
| 167 | p->aVar[i].flags = MEM_Null; |
drh | e30f442 | 2007-08-21 16:15:55 | [diff] [blame] | 168 | } |
drh | 2c2f392 | 2017-06-01 00:54:35 | [diff] [blame] | 169 | assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); |
dan | bda4cb8 | 2017-02-23 16:30:16 | [diff] [blame] | 170 | if( p->expmask ){ |
dan | c94b859 | 2009-10-20 07:01:24 | [diff] [blame] | 171 | p->expired = 1; |
| 172 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 | [diff] [blame] | 173 | sqlite3_mutex_leave(mutex); |
drh | e30f442 | 2007-08-21 16:15:55 | [diff] [blame] | 174 | return rc; |
| 175 | } |
| 176 | |
| 177 | |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 178 | /**************************** sqlite3_value_ ******************************* |
| 179 | ** The following routines extract information from a Mem or sqlite3_value |
| 180 | ** structure. |
| 181 | */ |
| 182 | const void *sqlite3_value_blob(sqlite3_value *pVal){ |
| 183 | Mem *p = (Mem*)pVal; |
| 184 | if( p->flags & (MEM_Blob|MEM_Str) ){ |
drh | ff535a2 | 2016-09-20 01:46:15 | [diff] [blame] | 185 | if( ExpandBlob(p)!=SQLITE_OK ){ |
dan | a4d5ae8 | 2015-07-24 16:24:37 | [diff] [blame] | 186 | assert( p->flags==MEM_Null && p->z==0 ); |
| 187 | return 0; |
| 188 | } |
drh | 1f0feef | 2007-05-15 13:27:07 | [diff] [blame] | 189 | p->flags |= MEM_Blob; |
drh | 4226253 | 2010-09-08 16:30:36 | [diff] [blame] | 190 | return p->n ? p->z : 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 191 | }else{ |
| 192 | return sqlite3_value_text(pVal); |
| 193 | } |
| 194 | } |
| 195 | int sqlite3_value_bytes(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 196 | return sqlite3ValueBytes(pVal, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 197 | } |
| 198 | int sqlite3_value_bytes16(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 199 | return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 200 | } |
| 201 | double sqlite3_value_double(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 | [diff] [blame] | 202 | return sqlite3VdbeRealValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 203 | } |
| 204 | int sqlite3_value_int(sqlite3_value *pVal){ |
drh | b27b7f5 | 2008-12-10 18:03:45 | [diff] [blame] | 205 | return (int)sqlite3VdbeIntValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 206 | } |
drh | efad999 | 2004-06-22 12:13:55 | [diff] [blame] | 207 | sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 | [diff] [blame] | 208 | return sqlite3VdbeIntValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 209 | } |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 210 | unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ |
dan | 5b6c8e4 | 2016-01-30 15:46:03 | [diff] [blame] | 211 | Mem *pMem = (Mem*)pVal; |
| 212 | return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 213 | } |
drh | ae3ec3f | 2017-07-17 00:40:19 | [diff] [blame] | 214 | void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){ |
drh | 3a96a5d | 2017-06-30 23:09:03 | [diff] [blame] | 215 | Mem *p = (Mem*)pVal; |
drh | a0024e6 | 2017-07-27 15:53:24 | [diff] [blame] | 216 | if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == |
| 217 | (MEM_Null|MEM_Term|MEM_Subtype) |
drh | ae3ec3f | 2017-07-17 00:40:19 | [diff] [blame] | 218 | && zPType!=0 |
drh | a0024e6 | 2017-07-27 15:53:24 | [diff] [blame] | 219 | && p->eSubtype=='p' |
drh | 2293006 | 2017-07-27 03:48:02 | [diff] [blame] | 220 | && strcmp(p->u.zPType, zPType)==0 |
drh | ae3ec3f | 2017-07-17 00:40:19 | [diff] [blame] | 221 | ){ |
drh | 2293006 | 2017-07-27 03:48:02 | [diff] [blame] | 222 | return (void*)p->z; |
drh | 3a96a5d | 2017-06-30 23:09:03 | [diff] [blame] | 223 | }else{ |
| 224 | return 0; |
| 225 | } |
| 226 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 227 | const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 228 | return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 229 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 230 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 231 | const void *sqlite3_value_text16(sqlite3_value* pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 232 | return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 233 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 234 | const void *sqlite3_value_text16be(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 235 | return sqlite3ValueText(pVal, SQLITE_UTF16BE); |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 236 | } |
| 237 | const void *sqlite3_value_text16le(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 238 | return sqlite3ValueText(pVal, SQLITE_UTF16LE); |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 239 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 240 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 22ec134 | 2015-02-27 00:33:15 | [diff] [blame] | 241 | /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five |
| 242 | ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating |
| 243 | ** point number string BLOB NULL |
| 244 | */ |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 245 | int sqlite3_value_type(sqlite3_value* pVal){ |
drh | 1b27b8c | 2014-02-10 03:21:57 | [diff] [blame] | 246 | static const u8 aType[] = { |
drh | f2566c4 | 2019-05-03 17:08:16 | [diff] [blame] | 247 | SQLITE_BLOB, /* 0x00 (not possible) */ |
| 248 | SQLITE_NULL, /* 0x01 NULL */ |
| 249 | SQLITE_TEXT, /* 0x02 TEXT */ |
| 250 | SQLITE_NULL, /* 0x03 (not possible) */ |
| 251 | SQLITE_INTEGER, /* 0x04 INTEGER */ |
| 252 | SQLITE_NULL, /* 0x05 (not possible) */ |
| 253 | SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */ |
| 254 | SQLITE_NULL, /* 0x07 (not possible) */ |
| 255 | SQLITE_FLOAT, /* 0x08 FLOAT */ |
| 256 | SQLITE_NULL, /* 0x09 (not possible) */ |
| 257 | SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */ |
| 258 | SQLITE_NULL, /* 0x0b (not possible) */ |
| 259 | SQLITE_INTEGER, /* 0x0c (not possible) */ |
| 260 | SQLITE_NULL, /* 0x0d (not possible) */ |
| 261 | SQLITE_INTEGER, /* 0x0e (not possible) */ |
| 262 | SQLITE_NULL, /* 0x0f (not possible) */ |
| 263 | SQLITE_BLOB, /* 0x10 BLOB */ |
| 264 | SQLITE_NULL, /* 0x11 (not possible) */ |
| 265 | SQLITE_TEXT, /* 0x12 (not possible) */ |
| 266 | SQLITE_NULL, /* 0x13 (not possible) */ |
| 267 | SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */ |
| 268 | SQLITE_NULL, /* 0x15 (not possible) */ |
| 269 | SQLITE_INTEGER, /* 0x16 (not possible) */ |
| 270 | SQLITE_NULL, /* 0x17 (not possible) */ |
| 271 | SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */ |
| 272 | SQLITE_NULL, /* 0x19 (not possible) */ |
| 273 | SQLITE_FLOAT, /* 0x1a (not possible) */ |
| 274 | SQLITE_NULL, /* 0x1b (not possible) */ |
| 275 | SQLITE_INTEGER, /* 0x1c (not possible) */ |
| 276 | SQLITE_NULL, /* 0x1d (not possible) */ |
| 277 | SQLITE_INTEGER, /* 0x1e (not possible) */ |
| 278 | SQLITE_NULL, /* 0x1f (not possible) */ |
| 279 | SQLITE_FLOAT, /* 0x20 INTREAL */ |
| 280 | SQLITE_NULL, /* 0x21 (not possible) */ |
drh | cd0f540 | 2023-04-04 18:55:31 | [diff] [blame] | 281 | SQLITE_FLOAT, /* 0x22 INTREAL + TEXT */ |
drh | f2566c4 | 2019-05-03 17:08:16 | [diff] [blame] | 282 | SQLITE_NULL, /* 0x23 (not possible) */ |
| 283 | SQLITE_FLOAT, /* 0x24 (not possible) */ |
| 284 | SQLITE_NULL, /* 0x25 (not possible) */ |
| 285 | SQLITE_FLOAT, /* 0x26 (not possible) */ |
| 286 | SQLITE_NULL, /* 0x27 (not possible) */ |
| 287 | SQLITE_FLOAT, /* 0x28 (not possible) */ |
| 288 | SQLITE_NULL, /* 0x29 (not possible) */ |
| 289 | SQLITE_FLOAT, /* 0x2a (not possible) */ |
| 290 | SQLITE_NULL, /* 0x2b (not possible) */ |
| 291 | SQLITE_FLOAT, /* 0x2c (not possible) */ |
| 292 | SQLITE_NULL, /* 0x2d (not possible) */ |
| 293 | SQLITE_FLOAT, /* 0x2e (not possible) */ |
| 294 | SQLITE_NULL, /* 0x2f (not possible) */ |
| 295 | SQLITE_BLOB, /* 0x30 (not possible) */ |
| 296 | SQLITE_NULL, /* 0x31 (not possible) */ |
| 297 | SQLITE_TEXT, /* 0x32 (not possible) */ |
| 298 | SQLITE_NULL, /* 0x33 (not possible) */ |
| 299 | SQLITE_FLOAT, /* 0x34 (not possible) */ |
| 300 | SQLITE_NULL, /* 0x35 (not possible) */ |
| 301 | SQLITE_FLOAT, /* 0x36 (not possible) */ |
| 302 | SQLITE_NULL, /* 0x37 (not possible) */ |
| 303 | SQLITE_FLOAT, /* 0x38 (not possible) */ |
| 304 | SQLITE_NULL, /* 0x39 (not possible) */ |
| 305 | SQLITE_FLOAT, /* 0x3a (not possible) */ |
| 306 | SQLITE_NULL, /* 0x3b (not possible) */ |
| 307 | SQLITE_FLOAT, /* 0x3c (not possible) */ |
| 308 | SQLITE_NULL, /* 0x3d (not possible) */ |
| 309 | SQLITE_FLOAT, /* 0x3e (not possible) */ |
| 310 | SQLITE_NULL, /* 0x3f (not possible) */ |
drh | 1b27b8c | 2014-02-10 03:21:57 | [diff] [blame] | 311 | }; |
drh | de7109e | 2019-05-02 17:45:52 | [diff] [blame] | 312 | #ifdef SQLITE_DEBUG |
| 313 | { |
| 314 | int eType = SQLITE_BLOB; |
| 315 | if( pVal->flags & MEM_Null ){ |
| 316 | eType = SQLITE_NULL; |
drh | 169f077 | 2019-05-02 21:36:26 | [diff] [blame] | 317 | }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){ |
drh | de7109e | 2019-05-02 17:45:52 | [diff] [blame] | 318 | eType = SQLITE_FLOAT; |
drh | 169f077 | 2019-05-02 21:36:26 | [diff] [blame] | 319 | }else if( pVal->flags & MEM_Int ){ |
| 320 | eType = SQLITE_INTEGER; |
drh | de7109e | 2019-05-02 17:45:52 | [diff] [blame] | 321 | }else if( pVal->flags & MEM_Str ){ |
| 322 | eType = SQLITE_TEXT; |
| 323 | } |
| 324 | assert( eType == aType[pVal->flags&MEM_AffMask] ); |
| 325 | } |
| 326 | #endif |
mistachkin | afc14f7 | 2014-03-05 01:29:18 | [diff] [blame] | 327 | return aType[pVal->flags&MEM_AffMask]; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 328 | } |
drh | 47996ea | 2022-10-12 12:49:29 | [diff] [blame] | 329 | int sqlite3_value_encoding(sqlite3_value *pVal){ |
| 330 | return pVal->enc; |
| 331 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 332 | |
drh | ce2fbd1 | 2018-01-12 21:00:14 | [diff] [blame] | 333 | /* Return true if a parameter to xUpdate represents an unchanged column */ |
| 334 | int sqlite3_value_nochange(sqlite3_value *pVal){ |
| 335 | return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero); |
| 336 | } |
| 337 | |
drh | 57b1a3e | 2019-03-29 11:13:37 | [diff] [blame] | 338 | /* Return true if a parameter value originated from an sqlite3_bind() */ |
| 339 | int sqlite3_value_frombind(sqlite3_value *pVal){ |
| 340 | return (pVal->flags&MEM_FromBind)!=0; |
| 341 | } |
| 342 | |
drh | 4f03f41 | 2015-05-20 21:28:32 | [diff] [blame] | 343 | /* Make a copy of an sqlite3_value object |
| 344 | */ |
| 345 | sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ |
| 346 | sqlite3_value *pNew; |
| 347 | if( pOrig==0 ) return 0; |
| 348 | pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 349 | if( pNew==0 ) return 0; |
| 350 | memset(pNew, 0, sizeof(*pNew)); |
| 351 | memcpy(pNew, pOrig, MEMCELLSIZE); |
| 352 | pNew->flags &= ~MEM_Dyn; |
| 353 | pNew->db = 0; |
| 354 | if( pNew->flags&(MEM_Str|MEM_Blob) ){ |
drh | 10ca5b4 | 2015-05-22 21:04:54 | [diff] [blame] | 355 | pNew->flags &= ~(MEM_Static|MEM_Dyn); |
| 356 | pNew->flags |= MEM_Ephem; |
| 357 | if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ |
| 358 | sqlite3ValueFree(pNew); |
| 359 | pNew = 0; |
drh | 4f03f41 | 2015-05-20 21:28:32 | [diff] [blame] | 360 | } |
drh | 8366529 | 2022-03-14 23:50:38 | [diff] [blame] | 361 | }else if( pNew->flags & MEM_Null ){ |
| 362 | /* Do not duplicate pointer values */ |
| 363 | pNew->flags &= ~(MEM_Term|MEM_Subtype); |
drh | 4f03f41 | 2015-05-20 21:28:32 | [diff] [blame] | 364 | } |
| 365 | return pNew; |
| 366 | } |
| 367 | |
| 368 | /* Destroy an sqlite3_value object previously obtained from |
| 369 | ** sqlite3_value_dup(). |
| 370 | */ |
| 371 | void sqlite3_value_free(sqlite3_value *pOld){ |
| 372 | sqlite3ValueFree(pOld); |
| 373 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 374 | |
drh | 4f03f41 | 2015-05-20 21:28:32 | [diff] [blame] | 375 | |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 376 | /**************************** sqlite3_result_ ******************************* |
| 377 | ** The following routines are used by user-defined functions to specify |
| 378 | ** the function result. |
drh | 4c8555f | 2009-06-25 01:47:11 | [diff] [blame] | 379 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 380 | ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the |
drh | dbe349d | 2021-10-13 13:00:34 | [diff] [blame] | 381 | ** result as a string or blob. Appropriate errors are set if the string/blob |
| 382 | ** is too big or if an OOM occurs. |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 383 | ** |
| 384 | ** The invokeValueDestructor(P,X) routine invokes destructor function X() |
drh | 42b49a3 | 2023-10-17 18:28:27 | [diff] [blame] | 385 | ** on value P if P is not going to be used and need to be destroyed. |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 386 | */ |
drh | 4c8555f | 2009-06-25 01:47:11 | [diff] [blame] | 387 | static void setResultStrOrError( |
| 388 | sqlite3_context *pCtx, /* Function context */ |
| 389 | const char *z, /* String pointer */ |
| 390 | int n, /* Bytes in string, or negative */ |
| 391 | u8 enc, /* Encoding of z. 0 for BLOBs */ |
| 392 | void (*xDel)(void*) /* Destructor function */ |
| 393 | ){ |
drh | fb92e07 | 2022-03-29 01:43:09 | [diff] [blame] | 394 | Mem *pOut = pCtx->pOut; |
| 395 | int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel); |
drh | dbe349d | 2021-10-13 13:00:34 | [diff] [blame] | 396 | if( rc ){ |
| 397 | if( rc==SQLITE_TOOBIG ){ |
| 398 | sqlite3_result_error_toobig(pCtx); |
| 399 | }else{ |
| 400 | /* The only errors possible from sqlite3VdbeMemSetStr are |
| 401 | ** SQLITE_TOOBIG and SQLITE_NOMEM */ |
| 402 | assert( rc==SQLITE_NOMEM ); |
| 403 | sqlite3_result_error_nomem(pCtx); |
| 404 | } |
drh | fb92e07 | 2022-03-29 01:43:09 | [diff] [blame] | 405 | return; |
| 406 | } |
drh | 659fdb4 | 2022-04-01 15:31:58 | [diff] [blame] | 407 | sqlite3VdbeChangeEncoding(pOut, pCtx->enc); |
drh | c55b62d | 2022-03-29 22:57:00 | [diff] [blame] | 408 | if( sqlite3VdbeMemTooBig(pOut) ){ |
| 409 | sqlite3_result_error_toobig(pCtx); |
drh | 4c8555f | 2009-06-25 01:47:11 | [diff] [blame] | 410 | } |
| 411 | } |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 412 | static int invokeValueDestructor( |
| 413 | const void *p, /* Value to destroy */ |
| 414 | void (*xDel)(void*), /* The destructor */ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 415 | sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if not NULL */ |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 416 | ){ |
drh | fc59a95 | 2014-09-11 23:34:55 | [diff] [blame] | 417 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 418 | if( xDel==0 ){ |
| 419 | /* noop */ |
| 420 | }else if( xDel==SQLITE_TRANSIENT ){ |
| 421 | /* noop */ |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 422 | }else{ |
| 423 | xDel((void*)p); |
| 424 | } |
drh | 42b49a3 | 2023-10-17 18:28:27 | [diff] [blame] | 425 | #ifdef SQLITE_ENABLE_API_ARMOR |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 426 | if( pCtx!=0 ){ |
| 427 | sqlite3_result_error_toobig(pCtx); |
| 428 | } |
drh | 42b49a3 | 2023-10-17 18:28:27 | [diff] [blame] | 429 | #else |
| 430 | assert( pCtx!=0 ); |
drh | d622855 | 2021-06-09 14:45:02 | [diff] [blame] | 431 | sqlite3_result_error_toobig(pCtx); |
drh | 42b49a3 | 2023-10-17 18:28:27 | [diff] [blame] | 432 | #endif |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 433 | return SQLITE_TOOBIG; |
| 434 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 435 | void sqlite3_result_blob( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 436 | sqlite3_context *pCtx, |
| 437 | const void *z, |
| 438 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 439 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 440 | ){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 441 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 442 | if( pCtx==0 || n<0 ){ |
| 443 | invokeValueDestructor(z, xDel, pCtx); |
| 444 | return; |
| 445 | } |
| 446 | #endif |
drh | 5708d2d | 2005-06-22 10:53:59 | [diff] [blame] | 447 | assert( n>=0 ); |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 448 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 4c8555f | 2009-06-25 01:47:11 | [diff] [blame] | 449 | setResultStrOrError(pCtx, z, n, 0, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 450 | } |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 451 | void sqlite3_result_blob64( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 452 | sqlite3_context *pCtx, |
| 453 | const void *z, |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 454 | sqlite3_uint64 n, |
| 455 | void (*xDel)(void *) |
| 456 | ){ |
drh | fc59a95 | 2014-09-11 23:34:55 | [diff] [blame] | 457 | assert( xDel!=SQLITE_DYNAMIC ); |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 458 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 459 | if( pCtx==0 ){ |
| 460 | invokeValueDestructor(z, xDel, 0); |
| 461 | return; |
| 462 | } |
| 463 | #endif |
| 464 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 465 | if( n>0x7fffffff ){ |
| 466 | (void)invokeValueDestructor(z, xDel, pCtx); |
| 467 | }else{ |
| 468 | setResultStrOrError(pCtx, z, (int)n, 0, xDel); |
| 469 | } |
| 470 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 471 | void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 472 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 473 | if( pCtx==0 ) return; |
| 474 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 475 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 476 | sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 477 | } |
| 478 | void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 479 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 480 | if( pCtx==0 ) return; |
| 481 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 482 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 69544ec | 2008-02-06 14:11:34 | [diff] [blame] | 483 | pCtx->isError = SQLITE_ERROR; |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 484 | sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 485 | } |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 | [diff] [blame] | 486 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 487 | void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 488 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 489 | if( pCtx==0 ) return; |
| 490 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 491 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 69544ec | 2008-02-06 14:11:34 | [diff] [blame] | 492 | pCtx->isError = SQLITE_ERROR; |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 493 | sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 494 | } |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 | [diff] [blame] | 495 | #endif |
drh | f447950 | 2004-05-27 03:12:53 | [diff] [blame] | 496 | void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 497 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 498 | if( pCtx==0 ) return; |
| 499 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 500 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 501 | sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 502 | } |
| 503 | void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 504 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 505 | if( pCtx==0 ) return; |
| 506 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 507 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 508 | sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 509 | } |
| 510 | void sqlite3_result_null(sqlite3_context *pCtx){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 511 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 512 | if( pCtx==0 ) return; |
| 513 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 514 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 515 | sqlite3VdbeMemSetNull(pCtx->pOut); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 516 | } |
drh | 2293006 | 2017-07-27 03:48:02 | [diff] [blame] | 517 | void sqlite3_result_pointer( |
| 518 | sqlite3_context *pCtx, |
| 519 | void *pPtr, |
| 520 | const char *zPType, |
| 521 | void (*xDestructor)(void*) |
| 522 | ){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 523 | Mem *pOut; |
| 524 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 525 | if( pCtx==0 ){ |
| 526 | invokeValueDestructor(pPtr, xDestructor, 0); |
| 527 | return; |
| 528 | } |
| 529 | #endif |
| 530 | pOut = pCtx->pOut; |
drh | 3a96a5d | 2017-06-30 23:09:03 | [diff] [blame] | 531 | assert( sqlite3_mutex_held(pOut->db->mutex) ); |
drh | 526740b | 2017-08-24 13:55:46 | [diff] [blame] | 532 | sqlite3VdbeMemRelease(pOut); |
| 533 | pOut->flags = MEM_Null; |
drh | 2293006 | 2017-07-27 03:48:02 | [diff] [blame] | 534 | sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor); |
drh | 3a96a5d | 2017-06-30 23:09:03 | [diff] [blame] | 535 | } |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 536 | void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 537 | Mem *pOut; |
| 538 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 539 | if( pCtx==0 ) return; |
| 540 | #endif |
drh | 6eb381f | 2023-11-09 12:58:03 | [diff] [blame] | 541 | #if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0 |
drh | b10c3d3 | 2023-11-09 15:01:56 | [diff] [blame] | 542 | if( pCtx->pFunc!=0 |
| 543 | && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0 |
| 544 | ){ |
drh | 6eb381f | 2023-11-09 12:58:03 | [diff] [blame] | 545 | char zErr[200]; |
| 546 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 547 | "misuse of sqlite3_result_subtype() by %s()", |
| 548 | pCtx->pFunc->zName); |
| 549 | sqlite3_result_error(pCtx, zErr, -1); |
| 550 | return; |
| 551 | } |
| 552 | #endif /* SQLITE_STRICT_SUBTYPE */ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 553 | pOut = pCtx->pOut; |
dan | 5b6c8e4 | 2016-01-30 15:46:03 | [diff] [blame] | 554 | assert( sqlite3_mutex_held(pOut->db->mutex) ); |
| 555 | pOut->eSubtype = eSubtype & 0xff; |
| 556 | pOut->flags |= MEM_Subtype; |
drh | bcdf78a | 2015-09-10 20:34:56 | [diff] [blame] | 557 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 558 | void sqlite3_result_text( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 559 | sqlite3_context *pCtx, |
| 560 | const char *z, |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 561 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 562 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 563 | ){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 564 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 565 | if( pCtx==0 ){ |
| 566 | invokeValueDestructor(z, xDel, 0); |
| 567 | return; |
| 568 | } |
| 569 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 570 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 4c8555f | 2009-06-25 01:47:11 | [diff] [blame] | 571 | setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 572 | } |
drh | bbf483f | 2014-09-09 20:30:24 | [diff] [blame] | 573 | void sqlite3_result_text64( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 574 | sqlite3_context *pCtx, |
| 575 | const char *z, |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 576 | sqlite3_uint64 n, |
| 577 | void (*xDel)(void *), |
| 578 | unsigned char enc |
| 579 | ){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 580 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 581 | if( pCtx==0 ){ |
| 582 | invokeValueDestructor(z, xDel, 0); |
| 583 | return; |
| 584 | } |
| 585 | #endif |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 586 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | fc59a95 | 2014-09-11 23:34:55 | [diff] [blame] | 587 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | d2603ad | 2022-12-02 17:52:52 | [diff] [blame] | 588 | if( enc!=SQLITE_UTF8 ){ |
| 589 | if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; |
| 590 | n &= ~(u64)1; |
| 591 | } |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 592 | if( n>0x7fffffff ){ |
| 593 | (void)invokeValueDestructor(z, xDel, pCtx); |
| 594 | }else{ |
| 595 | setResultStrOrError(pCtx, z, (int)n, enc, xDel); |
drh | 569700a | 2023-07-21 18:09:07 | [diff] [blame] | 596 | sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut); |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 597 | } |
| 598 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 599 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 600 | void sqlite3_result_text16( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 601 | sqlite3_context *pCtx, |
| 602 | const void *z, |
| 603 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 604 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 605 | ){ |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 606 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | d2603ad | 2022-12-02 17:52:52 | [diff] [blame] | 607 | setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE, xDel); |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 608 | } |
| 609 | void sqlite3_result_text16be( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 610 | sqlite3_context *pCtx, |
| 611 | const void *z, |
| 612 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 613 | void (*xDel)(void *) |
| 614 | ){ |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 615 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | d2603ad | 2022-12-02 17:52:52 | [diff] [blame] | 616 | setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE, xDel); |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 617 | } |
| 618 | void sqlite3_result_text16le( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 619 | sqlite3_context *pCtx, |
| 620 | const void *z, |
| 621 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 622 | void (*xDel)(void *) |
| 623 | ){ |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 624 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | d2603ad | 2022-12-02 17:52:52 | [diff] [blame] | 625 | setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 626 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 627 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 628 | void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 629 | Mem *pOut; |
| 630 | |
| 631 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 632 | if( pCtx==0 ) return; |
| 633 | if( pValue==0 ){ |
| 634 | sqlite3_result_null(pCtx); |
| 635 | return; |
| 636 | } |
| 637 | #endif |
| 638 | pOut = pCtx->pOut; |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 639 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | fb92e07 | 2022-03-29 01:43:09 | [diff] [blame] | 640 | sqlite3VdbeMemCopy(pOut, pValue); |
drh | 659fdb4 | 2022-04-01 15:31:58 | [diff] [blame] | 641 | sqlite3VdbeChangeEncoding(pOut, pCtx->enc); |
drh | fb92e07 | 2022-03-29 01:43:09 | [diff] [blame] | 642 | if( sqlite3VdbeMemTooBig(pOut) ){ |
| 643 | sqlite3_result_error_toobig(pCtx); |
| 644 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 645 | } |
drh | b026e05 | 2007-05-02 01:34:31 | [diff] [blame] | 646 | void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ |
drh | fb92e07 | 2022-03-29 01:43:09 | [diff] [blame] | 647 | sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0); |
drh | b026e05 | 2007-05-02 01:34:31 | [diff] [blame] | 648 | } |
dan | a4d5ae8 | 2015-07-24 16:24:37 | [diff] [blame] | 649 | int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 650 | Mem *pOut; |
| 651 | |
| 652 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 653 | if( pCtx==0 ) return SQLITE_MISUSE_BKPT; |
| 654 | #endif |
| 655 | pOut = pCtx->pOut; |
dan | a4d5ae8 | 2015-07-24 16:24:37 | [diff] [blame] | 656 | assert( sqlite3_mutex_held(pOut->db->mutex) ); |
drh | 6bacdc2 | 2015-07-24 17:14:03 | [diff] [blame] | 657 | if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | fb92e07 | 2022-03-29 01:43:09 | [diff] [blame] | 658 | sqlite3_result_error_toobig(pCtx); |
dan | a4d5ae8 | 2015-07-24 16:24:37 | [diff] [blame] | 659 | return SQLITE_TOOBIG; |
| 660 | } |
dan | a32536b | 2021-11-08 19:35:26 | [diff] [blame] | 661 | #ifndef SQLITE_OMIT_INCRBLOB |
dan | a4d5ae8 | 2015-07-24 16:24:37 | [diff] [blame] | 662 | sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); |
| 663 | return SQLITE_OK; |
dan | a32536b | 2021-11-08 19:35:26 | [diff] [blame] | 664 | #else |
| 665 | return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); |
| 666 | #endif |
dan | a4d5ae8 | 2015-07-24 16:24:37 | [diff] [blame] | 667 | } |
drh | 69544ec | 2008-02-06 14:11:34 | [diff] [blame] | 668 | void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 669 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 670 | if( pCtx==0 ) return; |
| 671 | #endif |
drh | f09ac0b | 2018-01-23 03:44:06 | [diff] [blame] | 672 | pCtx->isError = errCode ? errCode : -1; |
drh | 983b5ee | 2015-02-13 12:05:56 | [diff] [blame] | 673 | #ifdef SQLITE_DEBUG |
drh | 96f4ad2 | 2015-03-12 21:02:36 | [diff] [blame] | 674 | if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; |
drh | 983b5ee | 2015-02-13 12:05:56 | [diff] [blame] | 675 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 676 | if( pCtx->pOut->flags & MEM_Null ){ |
drh | fb92e07 | 2022-03-29 01:43:09 | [diff] [blame] | 677 | setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8, |
| 678 | SQLITE_STATIC); |
drh | 51c7d86 | 2009-04-27 18:46:06 | [diff] [blame] | 679 | } |
drh | 69544ec | 2008-02-06 14:11:34 | [diff] [blame] | 680 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 681 | |
drh | a0206bc | 2007-05-08 15:15:02 | [diff] [blame] | 682 | /* Force an SQLITE_TOOBIG error. */ |
| 683 | void sqlite3_result_error_toobig(sqlite3_context *pCtx){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 684 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 685 | if( pCtx==0 ) return; |
| 686 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 687 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 688 | pCtx->isError = SQLITE_TOOBIG; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 689 | sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 690 | SQLITE_UTF8, SQLITE_STATIC); |
drh | a0206bc | 2007-05-08 15:15:02 | [diff] [blame] | 691 | } |
| 692 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 | [diff] [blame] | 693 | /* An SQLITE_NOMEM error. */ |
| 694 | void sqlite3_result_error_nomem(sqlite3_context *pCtx){ |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 695 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 696 | if( pCtx==0 ) return; |
| 697 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 698 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 699 | sqlite3VdbeMemSetNull(pCtx->pOut); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 700 | pCtx->isError = SQLITE_NOMEM_BKPT; |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 701 | sqlite3OomFault(pCtx->pOut->db); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 | [diff] [blame] | 702 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 703 | |
drh | 0c8f403 | 2019-05-03 21:17:28 | [diff] [blame] | 704 | #ifndef SQLITE_UNTESTABLE |
| 705 | /* Force the INT64 value currently stored as the result to be |
| 706 | ** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL |
| 707 | ** test-control. |
| 708 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 709 | void sqlite3ResultIntReal(sqlite3_context *pCtx){ |
drh | 0c8f403 | 2019-05-03 21:17:28 | [diff] [blame] | 710 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 711 | if( pCtx->pOut->flags & MEM_Int ){ |
| 712 | pCtx->pOut->flags &= ~MEM_Int; |
| 713 | pCtx->pOut->flags |= MEM_IntReal; |
| 714 | } |
| 715 | } |
| 716 | #endif |
| 717 | |
| 718 | |
dan | 5cf5353 | 2010-05-01 16:40:20 | [diff] [blame] | 719 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 720 | ** This function is called after a transaction has been committed. It |
dan | 5cf5353 | 2010-05-01 16:40:20 | [diff] [blame] | 721 | ** invokes callbacks registered with sqlite3_wal_hook() as required. |
| 722 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 | [diff] [blame] | 723 | static int doWalCallbacks(sqlite3 *db){ |
dan | 8d22a17 | 2010-04-19 18:03:51 | [diff] [blame] | 724 | int rc = SQLITE_OK; |
dan | 5cf5353 | 2010-05-01 16:40:20 | [diff] [blame] | 725 | #ifndef SQLITE_OMIT_WAL |
| 726 | int i; |
dan | 8d22a17 | 2010-04-19 18:03:51 | [diff] [blame] | 727 | for(i=0; i<db->nDb; i++){ |
| 728 | Btree *pBt = db->aDb[i].pBt; |
| 729 | if( pBt ){ |
drh | ef15c6e | 2014-12-12 01:27:17 | [diff] [blame] | 730 | int nEntry; |
| 731 | sqlite3BtreeEnter(pBt); |
| 732 | nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); |
| 733 | sqlite3BtreeLeave(pBt); |
drh | 59533aa | 2017-08-02 18:28:26 | [diff] [blame] | 734 | if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){ |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 735 | rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry); |
dan | 8d22a17 | 2010-04-19 18:03:51 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 | [diff] [blame] | 739 | #endif |
dan | 8d22a17 | 2010-04-19 18:03:51 | [diff] [blame] | 740 | return rc; |
| 741 | } |
| 742 | |
drh | 201e0c6 | 2015-07-14 14:48:50 | [diff] [blame] | 743 | |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 744 | /* |
| 745 | ** Execute the statement pStmt, either until a row of data is ready, the |
| 746 | ** statement is completely executed or an error occurs. |
drh | b900aaf | 2006-11-09 00:24:53 | [diff] [blame] | 747 | ** |
| 748 | ** This routine implements the bulk of the logic behind the sqlite_step() |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 749 | ** API. The only thing omitted is the automatic recompile if a |
drh | b900aaf | 2006-11-09 00:24:53 | [diff] [blame] | 750 | ** schema change has occurred. That detail is handled by the |
| 751 | ** outer sqlite3_step() wrapper procedure. |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 752 | */ |
drh | b900aaf | 2006-11-09 00:24:53 | [diff] [blame] | 753 | static int sqlite3Step(Vdbe *p){ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 754 | sqlite3 *db; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 755 | int rc; |
| 756 | |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 757 | assert(p); |
drh | 35e9e35 | 2022-04-01 19:13:39 | [diff] [blame] | 758 | db = p->db; |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 759 | if( p->eVdbeState!=VDBE_RUN_STATE ){ |
| 760 | restart_step: |
| 761 | if( p->eVdbeState==VDBE_READY_STATE ){ |
| 762 | if( p->expired ){ |
| 763 | p->rc = SQLITE_SCHEMA; |
| 764 | rc = SQLITE_ERROR; |
| 765 | if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 766 | /* If this statement was prepared using saved SQL and an |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 767 | ** error has occurred, then return the error code in p->rc to the |
| 768 | ** caller. Set the error code in the database handle to the same |
| 769 | ** value. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 770 | */ |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 771 | rc = sqlite3VdbeTransferError(p); |
| 772 | } |
| 773 | goto end_of_step; |
| 774 | } |
drh | 15ca1df | 2006-07-26 13:43:30 | [diff] [blame] | 775 | |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 776 | /* If there are no other statements currently running, then |
| 777 | ** reset the interrupt flag. This prevents a call to sqlite3_interrupt |
| 778 | ** from interrupting a statement that has not yet started. |
| 779 | */ |
| 780 | if( db->nVdbeActive==0 ){ |
| 781 | AtomicStore(&db->u1.isInterrupted, 0); |
| 782 | } |
| 783 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 784 | assert( db->nVdbeWrite>0 || db->autoCommit==0 |
dan | 467e20a | 2025-01-20 18:26:58 | [diff] [blame] | 785 | || ((db->nDeferredCons + db->nDeferredImmCons)==0) |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 786 | ); |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 787 | |
drh | 19e2d37 | 2005-08-29 23:00:03 | [diff] [blame] | 788 | #ifndef SQLITE_OMIT_TRACE |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 789 | if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 |
| 790 | && !db->init.busy && p->zSql ){ |
| 791 | sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); |
| 792 | }else{ |
| 793 | assert( p->startTime==0 ); |
| 794 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 | [diff] [blame] | 795 | #endif |
drh | c16a03b | 2004-09-15 13:38:10 | [diff] [blame] | 796 | |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 797 | db->nVdbeActive++; |
| 798 | if( p->readOnly==0 ) db->nVdbeWrite++; |
| 799 | if( p->bIsReader ) db->nVdbeRead++; |
| 800 | p->pc = 0; |
| 801 | p->eVdbeState = VDBE_RUN_STATE; |
| 802 | }else |
| 803 | |
drh | 17c4865 | 2022-04-01 17:01:57 | [diff] [blame] | 804 | if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){ |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 805 | /* We used to require that sqlite3_reset() be called before retrying |
| 806 | ** sqlite3_step() after any error or after SQLITE_DONE. But beginning |
| 807 | ** with version 3.7.0, we changed this so that sqlite3_reset() would |
| 808 | ** be called automatically instead of throwing the SQLITE_MISUSE error. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 809 | ** This "automatic-reset" change is not technically an incompatibility, |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 810 | ** since any application that receives an SQLITE_MISUSE is broken by |
| 811 | ** definition. |
| 812 | ** |
| 813 | ** Nevertheless, some published applications that were originally written |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 814 | ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 815 | ** returns, and those were broken by the automatic-reset change. As a |
| 816 | ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 817 | ** legacy behavior of returning SQLITE_MISUSE for cases where the |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 818 | ** previous sqlite3_step() returned something other than a SQLITE_LOCKED |
| 819 | ** or SQLITE_BUSY error. |
| 820 | */ |
| 821 | #ifdef SQLITE_OMIT_AUTORESET |
| 822 | if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 823 | sqlite3_reset((sqlite3_stmt*)p); |
| 824 | }else{ |
| 825 | return SQLITE_MISUSE_BKPT; |
| 826 | } |
| 827 | #else |
| 828 | sqlite3_reset((sqlite3_stmt*)p); |
| 829 | #endif |
| 830 | assert( p->eVdbeState==VDBE_READY_STATE ); |
| 831 | goto restart_step; |
| 832 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 | [diff] [blame] | 833 | } |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 834 | |
drh | 983b5ee | 2015-02-13 12:05:56 | [diff] [blame] | 835 | #ifdef SQLITE_DEBUG |
| 836 | p->rcApp = SQLITE_OK; |
| 837 | #endif |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 838 | #ifndef SQLITE_OMIT_EXPLAIN |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 839 | if( p->explain ){ |
| 840 | rc = sqlite3VdbeList(p); |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 841 | }else |
| 842 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 843 | { |
drh | 4f7d3a5 | 2013-06-27 23:54:02 | [diff] [blame] | 844 | db->nVdbeExec++; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 845 | rc = sqlite3VdbeExec(p); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 | [diff] [blame] | 846 | db->nVdbeExec--; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 847 | } |
| 848 | |
drh | 7e62146 | 2022-03-30 17:56:27 | [diff] [blame] | 849 | if( rc==SQLITE_ROW ){ |
| 850 | assert( p->rc==SQLITE_OK ); |
| 851 | assert( db->mallocFailed==0 ); |
| 852 | db->errCode = SQLITE_ROW; |
| 853 | return SQLITE_ROW; |
| 854 | }else{ |
drh | 19e2d37 | 2005-08-29 23:00:03 | [diff] [blame] | 855 | #ifndef SQLITE_OMIT_TRACE |
drh | b7de827 | 2018-12-04 13:51:26 | [diff] [blame] | 856 | /* If the statement completed successfully, invoke the profile callback */ |
| 857 | checkProfileCallback(db, p); |
drh | 19e2d37 | 2005-08-29 23:00:03 | [diff] [blame] | 858 | #endif |
drh | cce70d5 | 2022-12-22 19:12:48 | [diff] [blame] | 859 | p->pResultRow = 0; |
drh | b7de827 | 2018-12-04 13:51:26 | [diff] [blame] | 860 | if( rc==SQLITE_DONE && db->autoCommit ){ |
| 861 | assert( p->rc==SQLITE_OK ); |
| 862 | p->rc = doWalCallbacks(db); |
| 863 | if( p->rc!=SQLITE_OK ){ |
| 864 | rc = SQLITE_ERROR; |
| 865 | } |
drh | 5cbb442 | 2020-06-29 13:12:42 | [diff] [blame] | 866 | }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 867 | /* If this statement was prepared using saved SQL and an |
drh | 5cbb442 | 2020-06-29 13:12:42 | [diff] [blame] | 868 | ** error has occurred, then return the error code in p->rc to the |
| 869 | ** caller. Set the error code in the database handle to the same value. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 870 | */ |
drh | 5cbb442 | 2020-06-29 13:12:42 | [diff] [blame] | 871 | rc = sqlite3VdbeTransferError(p); |
dan | 8d22a17 | 2010-04-19 18:03:51 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
drh | 80cc85b | 2008-07-23 21:07:25 | [diff] [blame] | 875 | db->errCode = rc; |
danielk1977 | 357864e | 2009-03-25 15:43:08 | [diff] [blame] | 876 | if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 877 | p->rc = SQLITE_NOMEM_BKPT; |
drh | 5cbb442 | 2020-06-29 13:12:42 | [diff] [blame] | 878 | if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc; |
drh | b900aaf | 2006-11-09 00:24:53 | [diff] [blame] | 879 | } |
danielk1977 | 357864e | 2009-03-25 15:43:08 | [diff] [blame] | 880 | end_of_step: |
drh | 5cbb442 | 2020-06-29 13:12:42 | [diff] [blame] | 881 | /* There are only a limited number of result codes allowed from the |
| 882 | ** statements prepared using the legacy sqlite3_prepare() interface */ |
| 883 | assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 884 | || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR |
drh | cbd8db3 | 2015-08-20 17:18:32 | [diff] [blame] | 885 | || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE |
danielk1977 | 357864e | 2009-03-25 15:43:08 | [diff] [blame] | 886 | ); |
danielk1977 | 357864e | 2009-03-25 15:43:08 | [diff] [blame] | 887 | return (rc&db->errMask); |
drh | b900aaf | 2006-11-09 00:24:53 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /* |
| 891 | ** This is the top-level implementation of sqlite3_step(). Call |
| 892 | ** sqlite3Step() to do most of the work. If a schema error occurs, |
| 893 | ** call sqlite3Reprepare() and try again. |
| 894 | */ |
drh | b900aaf | 2006-11-09 00:24:53 | [diff] [blame] | 895 | int sqlite3_step(sqlite3_stmt *pStmt){ |
drh | a6129fa | 2010-02-24 17:15:19 | [diff] [blame] | 896 | int rc = SQLITE_OK; /* Result from sqlite3Step() */ |
drh | a6129fa | 2010-02-24 17:15:19 | [diff] [blame] | 897 | Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ |
| 898 | int cnt = 0; /* Counter to prevent infinite loop of reprepares */ |
| 899 | sqlite3 *db; /* The database connection */ |
| 900 | |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 901 | if( vdbeSafetyNotNull(v) ){ |
| 902 | return SQLITE_MISUSE_BKPT; |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 | [diff] [blame] | 903 | } |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 904 | db = v->db; |
| 905 | sqlite3_mutex_enter(db->mutex); |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 906 | while( (rc = sqlite3Step(v))==SQLITE_SCHEMA |
drh | 2c7946a | 2014-08-19 20:27:40 | [diff] [blame] | 907 | && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ |
| 908 | int savedPc = v->pc; |
drh | 754ee28 | 2017-08-02 19:04:37 | [diff] [blame] | 909 | rc = sqlite3Reprepare(v); |
| 910 | if( rc!=SQLITE_OK ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 911 | /* This case occurs after failing to recompile an sql statement. |
| 912 | ** The error message from the SQL compiler has already been loaded |
| 913 | ** into the database handle. This block copies the error message |
drh | 754ee28 | 2017-08-02 19:04:37 | [diff] [blame] | 914 | ** from the database handle into the statement and sets the statement |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 915 | ** program counter to 0 to ensure that when the statement is |
drh | 754ee28 | 2017-08-02 19:04:37 | [diff] [blame] | 916 | ** finalized or reset the parser error message is available via |
| 917 | ** sqlite3_errmsg() and sqlite3_errcode(). |
| 918 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 919 | const char *zErr = (const char *)sqlite3_value_text(db->pErr); |
drh | 754ee28 | 2017-08-02 19:04:37 | [diff] [blame] | 920 | sqlite3DbFree(db, v->zErrMsg); |
| 921 | if( !db->mallocFailed ){ |
| 922 | v->zErrMsg = sqlite3DbStrDup(db, zErr); |
| 923 | v->rc = rc = sqlite3ApiExit(db, rc); |
| 924 | } else { |
| 925 | v->zErrMsg = 0; |
| 926 | v->rc = rc = SQLITE_NOMEM_BKPT; |
| 927 | } |
| 928 | break; |
| 929 | } |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 930 | sqlite3_reset(pStmt); |
drh | a24832b | 2022-04-01 19:04:13 | [diff] [blame] | 931 | if( savedPc>=0 ){ |
| 932 | /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and |
drh | 2591cfb | 2022-06-22 14:25:12 | [diff] [blame] | 933 | ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has |
| 934 | ** already been done once on a prior invocation that failed due to |
| 935 | ** SQLITE_SCHEMA. tag-20220401a */ |
drh | a24832b | 2022-04-01 19:04:13 | [diff] [blame] | 936 | v->minWriteFileFormat = 254; |
| 937 | } |
dan | 08f5f4c | 2011-06-27 19:37:23 | [diff] [blame] | 938 | assert( v->expired==0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 939 | } |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 940 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 76e8d1a | 2006-01-18 18:22:43 | [diff] [blame] | 941 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 942 | } |
| 943 | |
drh | 95a7b3e | 2013-09-16 12:57:19 | [diff] [blame] | 944 | |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 945 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 946 | ** Extract the user data from a sqlite3_context structure and return a |
| 947 | ** pointer to it. |
| 948 | */ |
| 949 | void *sqlite3_user_data(sqlite3_context *p){ |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 950 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 951 | if( p==0 ) return 0; |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 952 | #endif |
drh | f5e487d | 2023-10-19 20:46:22 | [diff] [blame] | 953 | assert( p && p->pFunc ); |
drh | d8f2635 | 2023-09-30 16:50:17 | [diff] [blame] | 954 | return p->pFunc->pUserData; |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /* |
drh | fa4a4b9 | 2008-03-19 21:45:51 | [diff] [blame] | 958 | ** Extract the user data from a sqlite3_context structure and return a |
| 959 | ** pointer to it. |
drh | 9f129f4 | 2010-08-31 15:27:32 | [diff] [blame] | 960 | ** |
| 961 | ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface |
| 962 | ** returns a copy of the pointer to the database connection (the 1st |
| 963 | ** parameter) of the sqlite3_create_function() and |
| 964 | ** sqlite3_create_function16() routines that originally registered the |
| 965 | ** application defined function. |
drh | fa4a4b9 | 2008-03-19 21:45:51 | [diff] [blame] | 966 | */ |
| 967 | sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 968 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 969 | if( p==0 ) return 0; |
| 970 | #else |
drh | a46a4a6 | 2015-09-08 21:12:53 | [diff] [blame] | 971 | assert( p && p->pOut ); |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 972 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 973 | return p->pOut->db; |
drh | fa4a4b9 | 2008-03-19 21:45:51 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /* |
drh | 6f390be | 2018-01-11 17:04:26 | [diff] [blame] | 977 | ** If this routine is invoked from within an xColumn method of a virtual |
| 978 | ** table, then it returns true if and only if the the call is during an |
| 979 | ** UPDATE operation and the value of the column will not be modified |
| 980 | ** by the UPDATE. |
| 981 | ** |
| 982 | ** If this routine is called from any context other than within the |
| 983 | ** xColumn method of a virtual table, then the return value is meaningless |
| 984 | ** and arbitrary. |
| 985 | ** |
| 986 | ** Virtual table implements might use this routine to optimize their |
| 987 | ** performance by substituting a NULL result, or some other light-weight |
| 988 | ** value, as a signal to the xUpdate routine that the column is unchanged. |
| 989 | */ |
| 990 | int sqlite3_vtab_nochange(sqlite3_context *p){ |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 991 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 992 | if( p==0 ) return 0; |
| 993 | #else |
drh | 6f390be | 2018-01-11 17:04:26 | [diff] [blame] | 994 | assert( p ); |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 995 | #endif |
drh | ce2fbd1 | 2018-01-12 21:00:14 | [diff] [blame] | 996 | return sqlite3_value_nochange(p->pOut); |
drh | 6f390be | 2018-01-11 17:04:26 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* |
drh | b604f14 | 2023-01-25 16:56:24 | [diff] [blame] | 1000 | ** The destructor function for a ValueList object. This needs to be |
| 1001 | ** a separate function, unknowable to the application, to ensure that |
| 1002 | ** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1003 | ** preceded by activation of IN processing via sqlite3_vtab_int() do not |
drh | b604f14 | 2023-01-25 16:56:24 | [diff] [blame] | 1004 | ** try to access a fake ValueList object inserted by a hostile extension. |
| 1005 | */ |
| 1006 | void sqlite3VdbeValueListFree(void *pToDelete){ |
| 1007 | sqlite3_free(pToDelete); |
| 1008 | } |
| 1009 | |
| 1010 | /* |
drh | 0fe7e7d | 2022-02-01 14:58:29 | [diff] [blame] | 1011 | ** Implementation of sqlite3_vtab_in_first() (if bNext==0) and |
| 1012 | ** sqlite3_vtab_in_next() (if bNext!=0). |
| 1013 | */ |
drh | 30e314e | 2022-02-02 14:36:58 | [diff] [blame] | 1014 | static int valueFromValueList( |
| 1015 | sqlite3_value *pVal, /* Pointer to the ValueList object */ |
| 1016 | sqlite3_value **ppOut, /* Store the next value from the list here */ |
| 1017 | int bNext /* 1 for _next(). 0 for _first() */ |
| 1018 | ){ |
drh | b30298d | 2022-02-01 21:59:43 | [diff] [blame] | 1019 | int rc; |
drh | 30e314e | 2022-02-02 14:36:58 | [diff] [blame] | 1020 | ValueList *pRhs; |
| 1021 | |
drh | 0fe7e7d | 2022-02-01 14:58:29 | [diff] [blame] | 1022 | *ppOut = 0; |
drh | 5a05a68 | 2023-09-05 15:03:23 | [diff] [blame] | 1023 | if( pVal==0 ) return SQLITE_MISUSE_BKPT; |
drh | 3c12ebd | 2023-01-26 02:18:53 | [diff] [blame] | 1024 | if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){ |
drh | b604f14 | 2023-01-25 16:56:24 | [diff] [blame] | 1025 | return SQLITE_ERROR; |
| 1026 | }else{ |
| 1027 | assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == |
| 1028 | (MEM_Null|MEM_Term|MEM_Subtype) ); |
| 1029 | assert( pVal->eSubtype=='p' ); |
| 1030 | assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 ); |
| 1031 | pRhs = (ValueList*)pVal->z; |
| 1032 | } |
drh | b30298d | 2022-02-01 21:59:43 | [diff] [blame] | 1033 | if( bNext ){ |
drh | 30e314e | 2022-02-02 14:36:58 | [diff] [blame] | 1034 | rc = sqlite3BtreeNext(pRhs->pCsr, 0); |
drh | b30298d | 2022-02-01 21:59:43 | [diff] [blame] | 1035 | }else{ |
| 1036 | int dummy = 0; |
drh | 30e314e | 2022-02-02 14:36:58 | [diff] [blame] | 1037 | rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy); |
drh | 3d7a69e | 2022-02-02 16:24:01 | [diff] [blame] | 1038 | assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) ); |
| 1039 | if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE; |
drh | b30298d | 2022-02-01 21:59:43 | [diff] [blame] | 1040 | } |
| 1041 | if( rc==SQLITE_OK ){ |
drh | 30e314e | 2022-02-02 14:36:58 | [diff] [blame] | 1042 | u32 sz; /* Size of current row in bytes */ |
| 1043 | Mem sMem; /* Raw content of current row */ |
| 1044 | memset(&sMem, 0, sizeof(sMem)); |
| 1045 | sz = sqlite3BtreePayloadSize(pRhs->pCsr); |
| 1046 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem); |
| 1047 | if( rc==SQLITE_OK ){ |
| 1048 | u8 *zBuf = (u8*)sMem.z; |
| 1049 | u32 iSerial; |
| 1050 | sqlite3_value *pOut = pRhs->pOut; |
| 1051 | int iOff = 1 + getVarint32(&zBuf[1], iSerial); |
| 1052 | sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut); |
drh | 38d1e44 | 2022-02-02 15:10:45 | [diff] [blame] | 1053 | pOut->enc = ENC(pOut->db); |
drh | 30e314e | 2022-02-02 14:36:58 | [diff] [blame] | 1054 | if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){ |
| 1055 | rc = SQLITE_NOMEM; |
| 1056 | }else{ |
| 1057 | *ppOut = pOut; |
| 1058 | } |
| 1059 | } |
| 1060 | sqlite3VdbeMemRelease(&sMem); |
drh | 0fe7e7d | 2022-02-01 14:58:29 | [diff] [blame] | 1061 | } |
| 1062 | return rc; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | ** Set the iterator value pVal to point to the first value in the set. |
| 1067 | ** Set (*ppOut) to point to this value before returning. |
| 1068 | */ |
| 1069 | int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){ |
drh | 30e314e | 2022-02-02 14:36:58 | [diff] [blame] | 1070 | return valueFromValueList(pVal, ppOut, 0); |
drh | 0fe7e7d | 2022-02-01 14:58:29 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | ** Set the iterator value pVal to point to the next value in the set. |
| 1075 | ** Set (*ppOut) to point to this value before returning. |
| 1076 | */ |
| 1077 | int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){ |
drh | 30e314e | 2022-02-02 14:36:58 | [diff] [blame] | 1078 | return valueFromValueList(pVal, ppOut, 1); |
drh | 0fe7e7d | 2022-02-01 14:58:29 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* |
drh | a9e03b1 | 2015-03-12 06:46:52 | [diff] [blame] | 1082 | ** Return the current time for a statement. If the current time |
| 1083 | ** is requested more than once within the same run of a single prepared |
| 1084 | ** statement, the exact same time is returned for each invocation regardless |
| 1085 | ** of the amount of time that elapses between invocations. In other words, |
| 1086 | ** the time returned is always the time of the first call. |
drh | 95a7b3e | 2013-09-16 12:57:19 | [diff] [blame] | 1087 | */ |
drh | 601e4d4 | 2023-02-08 20:29:48 | [diff] [blame] | 1088 | sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ |
drh | 95a7b3e | 2013-09-16 12:57:19 | [diff] [blame] | 1089 | int rc; |
drh | 175b8f0 | 2019-08-08 15:24:17 | [diff] [blame] | 1090 | #ifndef SQLITE_ENABLE_STAT4 |
drh | 601e4d4 | 2023-02-08 20:29:48 | [diff] [blame] | 1091 | sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; |
drh | a9e03b1 | 2015-03-12 06:46:52 | [diff] [blame] | 1092 | assert( p->pVdbe!=0 ); |
| 1093 | #else |
drh | 3df79a9 | 2015-03-12 23:48:27 | [diff] [blame] | 1094 | sqlite3_int64 iTime = 0; |
drh | 601e4d4 | 2023-02-08 20:29:48 | [diff] [blame] | 1095 | sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; |
drh | a9e03b1 | 2015-03-12 06:46:52 | [diff] [blame] | 1096 | #endif |
drh | 3df79a9 | 2015-03-12 23:48:27 | [diff] [blame] | 1097 | if( *piTime==0 ){ |
drh | 601e4d4 | 2023-02-08 20:29:48 | [diff] [blame] | 1098 | rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); |
drh | d4af882 | 2023-02-07 21:55:14 | [diff] [blame] | 1099 | if( rc ) *piTime = 0; |
| 1100 | } |
| 1101 | return *piTime; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
drh | 9de4a17 | 2014-08-23 18:17:19 | [diff] [blame] | 1105 | ** Create a new aggregate context for p and return a pointer to |
| 1106 | ** its pMem->z element. |
| 1107 | */ |
| 1108 | static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ |
| 1109 | Mem *pMem = p->pMem; |
| 1110 | assert( (pMem->flags & MEM_Agg)==0 ); |
| 1111 | if( nByte<=0 ){ |
drh | 0725cab | 2014-09-17 14:52:46 | [diff] [blame] | 1112 | sqlite3VdbeMemSetNull(pMem); |
drh | 9de4a17 | 2014-08-23 18:17:19 | [diff] [blame] | 1113 | pMem->z = 0; |
| 1114 | }else{ |
drh | 322f285 | 2014-09-19 00:43:39 | [diff] [blame] | 1115 | sqlite3VdbeMemClearAndResize(pMem, nByte); |
drh | 9de4a17 | 2014-08-23 18:17:19 | [diff] [blame] | 1116 | pMem->flags = MEM_Agg; |
| 1117 | pMem->u.pDef = p->pFunc; |
| 1118 | if( pMem->z ){ |
| 1119 | memset(pMem->z, 0, nByte); |
| 1120 | } |
| 1121 | } |
| 1122 | return (void*)pMem->z; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 1126 | ** Allocate or return the aggregate context for a user function. A new |
| 1127 | ** context is allocated on the first call. Subsequent calls return the |
| 1128 | ** same context that was returned on prior calls. |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 1129 | */ |
| 1130 | void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ |
drh | 2d80151 | 2016-01-14 22:19:58 | [diff] [blame] | 1131 | assert( p && p->pFunc && p->pFunc->xFinalize ); |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 1132 | assert( sqlite3_mutex_held(p->pOut->db->mutex) ); |
drh | d68eee0 | 2009-12-11 03:44:18 | [diff] [blame] | 1133 | testcase( nByte<0 ); |
drh | 9de4a17 | 2014-08-23 18:17:19 | [diff] [blame] | 1134 | if( (p->pMem->flags & MEM_Agg)==0 ){ |
| 1135 | return createAggContext(p, nByte); |
| 1136 | }else{ |
| 1137 | return (void*)p->pMem->z; |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 1138 | } |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 1142 | ** Return the auxiliary data pointer, if any, for the iArg'th argument to |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1143 | ** the user-function defined by pCtx. |
drh | f7fa4e7 | 2017-05-11 15:20:18 | [diff] [blame] | 1144 | ** |
| 1145 | ** The left-most argument is 0. |
| 1146 | ** |
| 1147 | ** Undocumented behavior: If iArg is negative then access a cache of |
| 1148 | ** auxiliary data pointers that is available to all functions within a |
| 1149 | ** single prepared statement. The iArg values must match. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1150 | */ |
| 1151 | void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ |
dan | 0c54779 | 2013-07-18 17:12:08 | [diff] [blame] | 1152 | AuxData *pAuxData; |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 1153 | |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 1154 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1155 | if( pCtx==0 ) return 0; |
| 1156 | #endif |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 1157 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 175b8f0 | 2019-08-08 15:24:17 | [diff] [blame] | 1158 | #if SQLITE_ENABLE_STAT4 |
dan | 18bf807 | 2015-03-11 20:06:40 | [diff] [blame] | 1159 | if( pCtx->pVdbe==0 ) return 0; |
drh | a9e03b1 | 2015-03-12 06:46:52 | [diff] [blame] | 1160 | #else |
| 1161 | assert( pCtx->pVdbe!=0 ); |
| 1162 | #endif |
drh | e694139 | 2017-05-10 19:42:52 | [diff] [blame] | 1163 | for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ |
drh | f7fa4e7 | 2017-05-11 15:20:18 | [diff] [blame] | 1164 | if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ |
drh | e694139 | 2017-05-10 19:42:52 | [diff] [blame] | 1165 | return pAuxData->pAux; |
| 1166 | } |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1167 | } |
drh | e694139 | 2017-05-10 19:42:52 | [diff] [blame] | 1168 | return 0; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 1172 | ** Set the auxiliary data pointer and delete function, for the iArg'th |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1173 | ** argument to the user-function defined by pCtx. Any previous value is |
| 1174 | ** deleted by calling the delete function specified when it was set. |
drh | f7fa4e7 | 2017-05-11 15:20:18 | [diff] [blame] | 1175 | ** |
| 1176 | ** The left-most argument is 0. |
| 1177 | ** |
| 1178 | ** Undocumented behavior: If iArg is negative then make the data available |
| 1179 | ** to all functions within the current prepared statement using iArg as an |
| 1180 | ** access code. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1181 | */ |
| 1182 | void sqlite3_set_auxdata( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1183 | sqlite3_context *pCtx, |
| 1184 | int iArg, |
| 1185 | void *pAux, |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1186 | void (*xDelete)(void*) |
| 1187 | ){ |
dan | 0c54779 | 2013-07-18 17:12:08 | [diff] [blame] | 1188 | AuxData *pAuxData; |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 1189 | Vdbe *pVdbe; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1190 | |
stephan | 7a54b12 | 2023-10-14 14:53:18 | [diff] [blame] | 1191 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1192 | if( pCtx==0 ) return; |
| 1193 | #endif |
| 1194 | pVdbe= pCtx->pVdbe; |
drh | 9bd038f | 2014-08-27 14:14:06 | [diff] [blame] | 1195 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
drh | 175b8f0 | 2019-08-08 15:24:17 | [diff] [blame] | 1196 | #ifdef SQLITE_ENABLE_STAT4 |
dan | 18bf807 | 2015-03-11 20:06:40 | [diff] [blame] | 1197 | if( pVdbe==0 ) goto failed; |
drh | a9e03b1 | 2015-03-12 06:46:52 | [diff] [blame] | 1198 | #else |
| 1199 | assert( pVdbe!=0 ); |
| 1200 | #endif |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1201 | |
drh | e694139 | 2017-05-10 19:42:52 | [diff] [blame] | 1202 | for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ |
drh | f7fa4e7 | 2017-05-11 15:20:18 | [diff] [blame] | 1203 | if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ |
| 1204 | break; |
| 1205 | } |
dan | 0c54779 | 2013-07-18 17:12:08 | [diff] [blame] | 1206 | } |
| 1207 | if( pAuxData==0 ){ |
| 1208 | pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); |
| 1209 | if( !pAuxData ) goto failed; |
drh | e694139 | 2017-05-10 19:42:52 | [diff] [blame] | 1210 | pAuxData->iAuxOp = pCtx->iOp; |
| 1211 | pAuxData->iAuxArg = iArg; |
| 1212 | pAuxData->pNextAux = pVdbe->pAuxData; |
dan | 0c54779 | 2013-07-18 17:12:08 | [diff] [blame] | 1213 | pVdbe->pAuxData = pAuxData; |
drh | f09ac0b | 2018-01-23 03:44:06 | [diff] [blame] | 1214 | if( pCtx->isError==0 ) pCtx->isError = -1; |
drh | e694139 | 2017-05-10 19:42:52 | [diff] [blame] | 1215 | }else if( pAuxData->xDeleteAux ){ |
| 1216 | pAuxData->xDeleteAux(pAuxData->pAux); |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1217 | } |
dan | 0c54779 | 2013-07-18 17:12:08 | [diff] [blame] | 1218 | |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1219 | pAuxData->pAux = pAux; |
drh | e694139 | 2017-05-10 19:42:52 | [diff] [blame] | 1220 | pAuxData->xDeleteAux = xDelete; |
danielk1977 | e0fc526 | 2007-07-26 06:50:05 | [diff] [blame] | 1221 | return; |
| 1222 | |
| 1223 | failed: |
| 1224 | if( xDelete ){ |
| 1225 | xDelete(pAux); |
| 1226 | } |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1227 | } |
| 1228 | |
shane | eec556d | 2008-10-12 00:27:53 | [diff] [blame] | 1229 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | 682f68b | 2004-06-05 10:22:17 | [diff] [blame] | 1230 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1231 | ** Return the number of times the Step function of an aggregate has been |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 1232 | ** called. |
| 1233 | ** |
drh | cf85a51 | 2006-02-09 18:35:29 | [diff] [blame] | 1234 | ** This function is deprecated. Do not use it for new code. It is |
| 1235 | ** provide only to avoid breaking legacy code. New aggregate function |
| 1236 | ** implementations should keep their own counts within their aggregate |
| 1237 | ** context. |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 1238 | */ |
| 1239 | int sqlite3_aggregate_count(sqlite3_context *p){ |
drh | 2d80151 | 2016-01-14 22:19:58 | [diff] [blame] | 1240 | assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); |
drh | abfcea2 | 2005-09-06 20:36:48 | [diff] [blame] | 1241 | return p->pMem->n; |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 1242 | } |
shane | eec556d | 2008-10-12 00:27:53 | [diff] [blame] | 1243 | #endif |
drh | eb2e176 | 2004-05-27 01:53:56 | [diff] [blame] | 1244 | |
| 1245 | /* |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1246 | ** Return the number of columns in the result set for the statement pStmt. |
| 1247 | */ |
| 1248 | int sqlite3_column_count(sqlite3_stmt *pStmt){ |
| 1249 | Vdbe *pVm = (Vdbe *)pStmt; |
drh | d1db37a | 2023-07-18 17:29:05 | [diff] [blame] | 1250 | if( pVm==0 ) return 0; |
| 1251 | return pVm->nResColumn; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /* |
| 1255 | ** Return the number of values available from the current row of the |
| 1256 | ** currently executing statement pStmt. |
| 1257 | */ |
| 1258 | int sqlite3_data_count(sqlite3_stmt *pStmt){ |
| 1259 | Vdbe *pVm = (Vdbe *)pStmt; |
drh | edc2713 | 2022-12-22 18:44:39 | [diff] [blame] | 1260 | if( pVm==0 || pVm->pResultRow==0 ) return 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1261 | return pVm->nResColumn; |
| 1262 | } |
| 1263 | |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 1264 | /* |
| 1265 | ** Return a pointer to static memory containing an SQL NULL value. |
| 1266 | */ |
| 1267 | static const Mem *columnNullValue(void){ |
| 1268 | /* Even though the Mem structure contains an element |
drh | b1a1c29 | 2014-03-05 12:47:55 | [diff] [blame] | 1269 | ** of type i64, on certain architectures (x86) with certain compiler |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 1270 | ** switches (-Os), gcc may align this Mem object on a 4-byte boundary |
| 1271 | ** instead of an 8-byte one. This all works fine, except that when |
| 1272 | ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s |
| 1273 | ** that a Mem structure is located on an 8-byte boundary. To prevent |
drh | b1a1c29 | 2014-03-05 12:47:55 | [diff] [blame] | 1274 | ** these assert()s from failing, when building with SQLITE_DEBUG defined |
| 1275 | ** using gcc, we force nullMem to be 8-byte aligned using the magical |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 1276 | ** __attribute__((aligned(8))) macro. */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1277 | static const Mem nullMem |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 1278 | #if defined(SQLITE_DEBUG) && defined(__GNUC__) |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1279 | __attribute__((aligned(8))) |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 1280 | #endif |
drh | 035e563 | 2014-09-16 14:16:31 | [diff] [blame] | 1281 | = { |
drh | 3329a63 | 2014-09-18 01:21:43 | [diff] [blame] | 1282 | /* .u = */ {0}, |
drh | c9373e8 | 2022-02-28 03:25:13 | [diff] [blame] | 1283 | /* .z = */ (char*)0, |
| 1284 | /* .n = */ (int)0, |
drh | 8faee87 | 2015-09-19 18:08:13 | [diff] [blame] | 1285 | /* .flags = */ (u16)MEM_Null, |
| 1286 | /* .enc = */ (u8)0, |
| 1287 | /* .eSubtype = */ (u8)0, |
drh | c9373e8 | 2022-02-28 03:25:13 | [diff] [blame] | 1288 | /* .db = */ (sqlite3*)0, |
drh | 8faee87 | 2015-09-19 18:08:13 | [diff] [blame] | 1289 | /* .szMalloc = */ (int)0, |
| 1290 | /* .uTemp = */ (u32)0, |
drh | c9373e8 | 2022-02-28 03:25:13 | [diff] [blame] | 1291 | /* .zMalloc = */ (char*)0, |
drh | 8faee87 | 2015-09-19 18:08:13 | [diff] [blame] | 1292 | /* .xDel = */ (void(*)(void*))0, |
drh | b1a1c29 | 2014-03-05 12:47:55 | [diff] [blame] | 1293 | #ifdef SQLITE_DEBUG |
drh | 8faee87 | 2015-09-19 18:08:13 | [diff] [blame] | 1294 | /* .pScopyFrom = */ (Mem*)0, |
drh | 58773a5 | 2018-06-12 13:52:23 | [diff] [blame] | 1295 | /* .mScopyFlags= */ 0, |
drh | 8e7a168 | 2025-01-21 16:30:55 | [diff] [blame] | 1296 | /* .bScopy = */ 0, |
drh | 58773a5 | 2018-06-12 13:52:23 | [diff] [blame] | 1297 | #endif |
drh | 035e563 | 2014-09-16 14:16:31 | [diff] [blame] | 1298 | }; |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 1299 | return &nullMem; |
| 1300 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1301 | |
| 1302 | /* |
| 1303 | ** Check to see if column iCol of the given statement is valid. If |
| 1304 | ** it is, return a pointer to the Mem for the value of that column. |
| 1305 | ** If iCol is not valid, return a pointer to a Mem which has a value |
| 1306 | ** of NULL. |
| 1307 | */ |
| 1308 | static Mem *columnMem(sqlite3_stmt *pStmt, int i){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1309 | Vdbe *pVm; |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1310 | Mem *pOut; |
| 1311 | |
| 1312 | pVm = (Vdbe *)pStmt; |
drh | 28f1701 | 2016-09-22 21:37:18 | [diff] [blame] | 1313 | if( pVm==0 ) return (Mem*)columnNullValue(); |
| 1314 | assert( pVm->db ); |
| 1315 | sqlite3_mutex_enter(pVm->db->mutex); |
drh | edc2713 | 2022-12-22 18:44:39 | [diff] [blame] | 1316 | if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){ |
| 1317 | pOut = &pVm->pResultRow[i]; |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1318 | }else{ |
drh | 28f1701 | 2016-09-22 21:37:18 | [diff] [blame] | 1319 | sqlite3Error(pVm->db, SQLITE_RANGE); |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 1320 | pOut = (Mem*)columnNullValue(); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1321 | } |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1322 | return pOut; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1323 | } |
| 1324 | |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1325 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1326 | ** This function is called after invoking an sqlite3_value_XXX function on a |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1327 | ** column value (i.e. a value returned by evaluating an SQL expression in the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1328 | ** select list of a SELECT statement) that may cause a malloc() failure. If |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1329 | ** malloc() has failed, the threads mallocFailed flag is cleared and the result |
| 1330 | ** code of statement pStmt set to SQLITE_NOMEM. |
| 1331 | ** |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1332 | ** Specifically, this is called from within: |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1333 | ** |
| 1334 | ** sqlite3_column_int() |
| 1335 | ** sqlite3_column_int64() |
| 1336 | ** sqlite3_column_text() |
| 1337 | ** sqlite3_column_text16() |
drh | 6e53f67 | 2024-12-10 12:32:34 | [diff] [blame] | 1338 | ** sqlite3_column_double() |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1339 | ** sqlite3_column_bytes() |
| 1340 | ** sqlite3_column_bytes16() |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1341 | ** sqlite3_column_blob() |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1342 | */ |
| 1343 | static void columnMallocFailure(sqlite3_stmt *pStmt) |
| 1344 | { |
| 1345 | /* If malloc() failed during an encoding conversion within an |
| 1346 | ** sqlite3_column_XXX API, then set the return code of the statement to |
| 1347 | ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR |
| 1348 | ** and _finalize() will return NOMEM. |
| 1349 | */ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 | [diff] [blame] | 1350 | Vdbe *p = (Vdbe *)pStmt; |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1351 | if( p ){ |
drh | 28f1701 | 2016-09-22 21:37:18 | [diff] [blame] | 1352 | assert( p->db!=0 ); |
| 1353 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1354 | p->rc = sqlite3ApiExit(p->db, p->rc); |
| 1355 | sqlite3_mutex_leave(p->db->mutex); |
| 1356 | } |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1357 | } |
| 1358 | |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1359 | /**************************** sqlite3_column_ ******************************* |
| 1360 | ** The following routines are used to access elements of the current row |
| 1361 | ** in the result set. |
| 1362 | */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 | [diff] [blame] | 1363 | const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1364 | const void *val; |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1365 | val = sqlite3_value_blob( columnMem(pStmt,i) ); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 | [diff] [blame] | 1366 | /* Even though there is no encoding conversion, value_blob() might |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1367 | ** need to call malloc() to expand the result of a zeroblob() |
| 1368 | ** expression. |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 | [diff] [blame] | 1369 | */ |
| 1370 | columnMallocFailure(pStmt); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1371 | return val; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 | [diff] [blame] | 1372 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1373 | int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1374 | int val = sqlite3_value_bytes( columnMem(pStmt,i) ); |
| 1375 | columnMallocFailure(pStmt); |
| 1376 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1377 | } |
| 1378 | int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1379 | int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); |
| 1380 | columnMallocFailure(pStmt); |
| 1381 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1382 | } |
| 1383 | double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1384 | double val = sqlite3_value_double( columnMem(pStmt,i) ); |
| 1385 | columnMallocFailure(pStmt); |
| 1386 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1387 | } |
| 1388 | int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1389 | int val = sqlite3_value_int( columnMem(pStmt,i) ); |
| 1390 | columnMallocFailure(pStmt); |
| 1391 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1392 | } |
drh | efad999 | 2004-06-22 12:13:55 | [diff] [blame] | 1393 | sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1394 | sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); |
| 1395 | columnMallocFailure(pStmt); |
| 1396 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1397 | } |
| 1398 | const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1399 | const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); |
| 1400 | columnMallocFailure(pStmt); |
| 1401 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1402 | } |
drh | d1e4733 | 2005-06-26 17:55:33 | [diff] [blame] | 1403 | sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ |
danielk1977 | d0ffa1e | 2008-10-13 10:37:49 | [diff] [blame] | 1404 | Mem *pOut = columnMem(pStmt, i); |
| 1405 | if( pOut->flags&MEM_Static ){ |
| 1406 | pOut->flags &= ~MEM_Static; |
| 1407 | pOut->flags |= MEM_Ephem; |
| 1408 | } |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1409 | columnMallocFailure(pStmt); |
danielk1977 | d0ffa1e | 2008-10-13 10:37:49 | [diff] [blame] | 1410 | return (sqlite3_value *)pOut; |
drh | d1e4733 | 2005-06-26 17:55:33 | [diff] [blame] | 1411 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1412 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1413 | const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 | [diff] [blame] | 1414 | const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); |
| 1415 | columnMallocFailure(pStmt); |
| 1416 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1417 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1418 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1419 | int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1420 | int iType = sqlite3_value_type( columnMem(pStmt,i) ); |
| 1421 | columnMallocFailure(pStmt); |
| 1422 | return iType; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1423 | } |
| 1424 | |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1425 | /* |
drh | d1db37a | 2023-07-18 17:29:05 | [diff] [blame] | 1426 | ** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN. |
| 1427 | */ |
| 1428 | static const char * const azExplainColNames8[] = { |
| 1429 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */ |
| 1430 | "id", "parent", "notused", "detail" /* EQP */ |
| 1431 | }; |
| 1432 | static const u16 azExplainColNames16data[] = { |
| 1433 | /* 0 */ 'a', 'd', 'd', 'r', 0, |
| 1434 | /* 5 */ 'o', 'p', 'c', 'o', 'd', 'e', 0, |
| 1435 | /* 12 */ 'p', '1', 0, |
| 1436 | /* 15 */ 'p', '2', 0, |
| 1437 | /* 18 */ 'p', '3', 0, |
| 1438 | /* 21 */ 'p', '4', 0, |
| 1439 | /* 24 */ 'p', '5', 0, |
| 1440 | /* 27 */ 'c', 'o', 'm', 'm', 'e', 'n', 't', 0, |
| 1441 | /* 35 */ 'i', 'd', 0, |
| 1442 | /* 38 */ 'p', 'a', 'r', 'e', 'n', 't', 0, |
| 1443 | /* 45 */ 'n', 'o', 't', 'u', 's', 'e', 'd', 0, |
| 1444 | /* 53 */ 'd', 'e', 't', 'a', 'i', 'l', 0 |
| 1445 | }; |
| 1446 | static const u8 iExplainColNames16[] = { |
| 1447 | 0, 5, 12, 15, 18, 21, 24, 27, |
| 1448 | 35, 38, 45, 53 |
| 1449 | }; |
| 1450 | |
| 1451 | /* |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1452 | ** Convert the N-th element of pStmt->pColName[] into a string using |
| 1453 | ** xFunc() then return that string. If N is out of range, return 0. |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1454 | ** |
| 1455 | ** There are up to 5 names for each column. useType determines which |
| 1456 | ** name is returned. Here are the names: |
| 1457 | ** |
| 1458 | ** 0 The column name as it should be displayed for output |
| 1459 | ** 1 The datatype name for the column |
| 1460 | ** 2 The name of the database that the column derives from |
| 1461 | ** 3 The name of the table that the column derives from |
| 1462 | ** 4 The name of the table column that the result column derives from |
| 1463 | ** |
| 1464 | ** If the result is not a simple column reference (if it is an expression |
| 1465 | ** or a constant) then useTypes 2, 3, and 4 return NULL. |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1466 | */ |
| 1467 | static const void *columnName( |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1468 | sqlite3_stmt *pStmt, /* The statement */ |
| 1469 | int N, /* Which column to get the name for */ |
| 1470 | int useUtf16, /* True to return the name as UTF16 */ |
| 1471 | int useType /* What type of name */ |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1472 | ){ |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 1473 | const void *ret; |
| 1474 | Vdbe *p; |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1475 | int n; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 1476 | sqlite3 *db; |
| 1477 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1478 | if( pStmt==0 ){ |
| 1479 | (void)SQLITE_MISUSE_BKPT; |
| 1480 | return 0; |
| 1481 | } |
| 1482 | #endif |
drh | d1db37a | 2023-07-18 17:29:05 | [diff] [blame] | 1483 | if( N<0 ) return 0; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 1484 | ret = 0; |
| 1485 | p = (Vdbe *)pStmt; |
| 1486 | db = p->db; |
drh | 9b4ea4a | 2009-04-10 20:32:00 | [diff] [blame] | 1487 | assert( db!=0 ); |
drh | d1db37a | 2023-07-18 17:29:05 | [diff] [blame] | 1488 | sqlite3_mutex_enter(db->mutex); |
| 1489 | |
| 1490 | if( p->explain ){ |
| 1491 | if( useType>0 ) goto columnName_end; |
| 1492 | n = p->explain==1 ? 8 : 4; |
| 1493 | if( N>=n ) goto columnName_end; |
| 1494 | if( useUtf16 ){ |
| 1495 | int i = iExplainColNames16[N + 8*p->explain - 8]; |
| 1496 | ret = (void*)&azExplainColNames16data[i]; |
| 1497 | }else{ |
| 1498 | ret = (void*)azExplainColNames8[N + 8*p->explain - 8]; |
| 1499 | } |
| 1500 | goto columnName_end; |
| 1501 | } |
| 1502 | n = p->nResColumn; |
| 1503 | if( N<n ){ |
drh | 93b4c3b | 2023-04-27 23:59:51 | [diff] [blame] | 1504 | u8 prior_mallocFailed = db->mallocFailed; |
drh | 9b4ea4a | 2009-04-10 20:32:00 | [diff] [blame] | 1505 | N += useType*n; |
dan | 4474e86 | 2019-03-04 07:15:57 | [diff] [blame] | 1506 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1507 | if( useUtf16 ){ |
| 1508 | ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]); |
dan | 4474e86 | 2019-03-04 07:15:57 | [diff] [blame] | 1509 | }else |
| 1510 | #endif |
| 1511 | { |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1512 | ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]); |
| 1513 | } |
| 1514 | /* A malloc may have failed inside of the _text() call. If this |
drh | 9b4ea4a | 2009-04-10 20:32:00 | [diff] [blame] | 1515 | ** is the case, clear the mallocFailed flag and return NULL. |
| 1516 | */ |
drh | 93b4c3b | 2023-04-27 23:59:51 | [diff] [blame] | 1517 | assert( db->mallocFailed==0 || db->mallocFailed==1 ); |
| 1518 | if( db->mallocFailed > prior_mallocFailed ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 1519 | sqlite3OomClear(db); |
drh | 9b4ea4a | 2009-04-10 20:32:00 | [diff] [blame] | 1520 | ret = 0; |
drh | 32bc3f6 | 2007-08-21 20:25:39 | [diff] [blame] | 1521 | } |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1522 | } |
drh | d1db37a | 2023-07-18 17:29:05 | [diff] [blame] | 1523 | columnName_end: |
| 1524 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 | [diff] [blame] | 1525 | return ret; |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1526 | } |
| 1527 | |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1528 | /* |
| 1529 | ** Return the name of the Nth column of the result set returned by SQL |
| 1530 | ** statement pStmt. |
| 1531 | */ |
| 1532 | const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1533 | return columnName(pStmt, N, 0, COLNAME_NAME); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1534 | } |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1535 | #ifndef SQLITE_OMIT_UTF16 |
| 1536 | const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1537 | return columnName(pStmt, N, 1, COLNAME_NAME); |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1538 | } |
| 1539 | #endif |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1540 | |
| 1541 | /* |
drh | 3f91357 | 2008-03-22 01:07:17 | [diff] [blame] | 1542 | ** Constraint: If you have ENABLE_COLUMN_METADATA then you must |
| 1543 | ** not define OMIT_DECLTYPE. |
| 1544 | */ |
| 1545 | #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) |
| 1546 | # error "Must not define both SQLITE_OMIT_DECLTYPE \ |
| 1547 | and SQLITE_ENABLE_COLUMN_METADATA" |
| 1548 | #endif |
| 1549 | |
| 1550 | #ifndef SQLITE_OMIT_DECLTYPE |
| 1551 | /* |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1552 | ** Return the column declaration type (if applicable) of the 'i'th column |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1553 | ** of the result set of SQL statement pStmt. |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1554 | */ |
| 1555 | const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1556 | return columnName(pStmt, N, 0, COLNAME_DECLTYPE); |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1557 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1558 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 76d505b | 2004-05-28 13:13:02 | [diff] [blame] | 1559 | const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1560 | return columnName(pStmt, N, 1, COLNAME_DECLTYPE); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1561 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1562 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 3f91357 | 2008-03-22 01:07:17 | [diff] [blame] | 1563 | #endif /* SQLITE_OMIT_DECLTYPE */ |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1564 | |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 | [diff] [blame] | 1565 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1566 | /* |
| 1567 | ** Return the name of the database from which a result column derives. |
| 1568 | ** NULL is returned if the result column is an expression or constant or |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 1569 | ** anything else which is not an unambiguous reference to a database column. |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1570 | */ |
| 1571 | const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1572 | return columnName(pStmt, N, 0, COLNAME_DATABASE); |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1573 | } |
| 1574 | #ifndef SQLITE_OMIT_UTF16 |
| 1575 | const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1576 | return columnName(pStmt, N, 1, COLNAME_DATABASE); |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1577 | } |
| 1578 | #endif /* SQLITE_OMIT_UTF16 */ |
| 1579 | |
| 1580 | /* |
| 1581 | ** Return the name of the table from which a result column derives. |
| 1582 | ** NULL is returned if the result column is an expression or constant or |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 1583 | ** anything else which is not an unambiguous reference to a database column. |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1584 | */ |
| 1585 | const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1586 | return columnName(pStmt, N, 0, COLNAME_TABLE); |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1587 | } |
| 1588 | #ifndef SQLITE_OMIT_UTF16 |
| 1589 | const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1590 | return columnName(pStmt, N, 1, COLNAME_TABLE); |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1591 | } |
| 1592 | #endif /* SQLITE_OMIT_UTF16 */ |
| 1593 | |
| 1594 | /* |
| 1595 | ** Return the name of the table column from which a result column derives. |
| 1596 | ** NULL is returned if the result column is an expression or constant or |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 1597 | ** anything else which is not an unambiguous reference to a database column. |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1598 | */ |
| 1599 | const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1600 | return columnName(pStmt, N, 0, COLNAME_COLUMN); |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1601 | } |
| 1602 | #ifndef SQLITE_OMIT_UTF16 |
| 1603 | const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ |
drh | 8cf9289 | 2019-02-20 18:13:57 | [diff] [blame] | 1604 | return columnName(pStmt, N, 1, COLNAME_COLUMN); |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1605 | } |
| 1606 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 | [diff] [blame] | 1607 | #endif /* SQLITE_ENABLE_COLUMN_METADATA */ |
drh | e590fbd | 2005-05-20 19:36:01 | [diff] [blame] | 1608 | |
| 1609 | |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1610 | /******************************* sqlite3_bind_ *************************** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1611 | ** |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1612 | ** Routines used to attach values to wildcards in a compiled SQL statement. |
| 1613 | */ |
| 1614 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1615 | ** Unbind the value bound to variable i in virtual machine p. This is the |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1616 | ** the same as binding a NULL value to the column. If the "i" parameter is |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1617 | ** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK. |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1618 | ** |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1619 | ** A successful evaluation of this routine acquires the mutex on p. |
| 1620 | ** the mutex is released if any kind of error occurs. |
| 1621 | ** |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1622 | ** The error code stored in database p->db is overwritten with the return |
| 1623 | ** value in any case. |
drh | 87dce45 | 2024-09-17 21:42:04 | [diff] [blame] | 1624 | ** |
| 1625 | ** (tag-20240917-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK, |
| 1626 | ** that means all of the the following will be true: |
| 1627 | ** |
| 1628 | ** p!=0 |
| 1629 | ** p->pVar!=0 |
| 1630 | ** i>0 |
| 1631 | ** i<=p->nVar |
| 1632 | ** |
| 1633 | ** An assert() is normally added after vdbeUnbind() to help static analyzers |
| 1634 | ** realize this. |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1635 | */ |
drh | 403f002 | 2022-08-03 19:37:25 | [diff] [blame] | 1636 | static int vdbeUnbind(Vdbe *p, unsigned int i){ |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1637 | Mem *pVar; |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 1638 | if( vdbeSafetyNotNull(p) ){ |
| 1639 | return SQLITE_MISUSE_BKPT; |
| 1640 | } |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1641 | sqlite3_mutex_enter(p->db->mutex); |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 1642 | if( p->eVdbeState!=VDBE_READY_STATE ){ |
drh | 5a05a68 | 2023-09-05 15:03:23 | [diff] [blame] | 1643 | sqlite3Error(p->db, SQLITE_MISUSE_BKPT); |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1644 | sqlite3_mutex_leave(p->db->mutex); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1645 | sqlite3_log(SQLITE_MISUSE, |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 1646 | "bind on a busy prepared statement: [%s]", p->zSql); |
| 1647 | return SQLITE_MISUSE_BKPT; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1648 | } |
mistachkin | e284893 | 2022-08-05 05:30:07 | [diff] [blame] | 1649 | if( i>=(unsigned int)p->nVar ){ |
drh | 13f40da | 2014-08-22 18:00:11 | [diff] [blame] | 1650 | sqlite3Error(p->db, SQLITE_RANGE); |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1651 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1652 | return SQLITE_RANGE; |
| 1653 | } |
drh | 290c194 | 2004-08-21 17:54:45 | [diff] [blame] | 1654 | pVar = &p->aVar[i]; |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1655 | sqlite3VdbeMemRelease(pVar); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1656 | pVar->flags = MEM_Null; |
drh | 368bfe8 | 2018-12-11 12:20:41 | [diff] [blame] | 1657 | p->db->errCode = SQLITE_OK; |
dan | 937d0de | 2009-10-15 18:35:38 | [diff] [blame] | 1658 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1659 | /* If the bit corresponding to this variable in Vdbe.expmask is set, then |
dan | 1d2ce4f | 2009-10-19 18:11:09 | [diff] [blame] | 1660 | ** binding a new value to this variable invalidates the current query plan. |
drh | a704400 | 2010-09-14 18:22:59 | [diff] [blame] | 1661 | ** |
drh | bbf6d43 | 2020-05-15 15:03:51 | [diff] [blame] | 1662 | ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host |
drh | a704400 | 2010-09-14 18:22:59 | [diff] [blame] | 1663 | ** parameter in the WHERE clause might influence the choice of query plan |
| 1664 | ** for a statement, then the statement will be automatically recompiled, |
| 1665 | ** as if there had been a schema change, on the first sqlite3_step() call |
| 1666 | ** following any change to the bindings of that parameter. |
dan | 1d2ce4f | 2009-10-19 18:11:09 | [diff] [blame] | 1667 | */ |
drh | 2c2f392 | 2017-06-01 00:54:35 | [diff] [blame] | 1668 | assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); |
drh | 2996796 | 2017-03-03 21:51:40 | [diff] [blame] | 1669 | if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){ |
dan | 937d0de | 2009-10-15 18:35:38 | [diff] [blame] | 1670 | p->expired = 1; |
| 1671 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1672 | return SQLITE_OK; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1676 | ** Bind a text or BLOB value. |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1677 | */ |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1678 | static int bindText( |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 1679 | sqlite3_stmt *pStmt, /* The statement to bind against */ |
| 1680 | int i, /* Index of the parameter to bind */ |
| 1681 | const void *zData, /* Pointer to the data to be bound */ |
drh | d622855 | 2021-06-09 14:45:02 | [diff] [blame] | 1682 | i64 nData, /* Number of bytes of data to be bound */ |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 1683 | void (*xDel)(void*), /* Destructor for the data */ |
drh | b27b7f5 | 2008-12-10 18:03:45 | [diff] [blame] | 1684 | u8 encoding /* Encoding for the data */ |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1685 | ){ |
| 1686 | Vdbe *p = (Vdbe *)pStmt; |
| 1687 | Mem *pVar; |
| 1688 | int rc; |
| 1689 | |
drh | 403f002 | 2022-08-03 19:37:25 | [diff] [blame] | 1690 | rc = vdbeUnbind(p, (u32)(i-1)); |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1691 | if( rc==SQLITE_OK ){ |
drh | 87dce45 | 2024-09-17 21:42:04 | [diff] [blame] | 1692 | assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1693 | if( zData!=0 ){ |
| 1694 | pVar = &p->aVar[i-1]; |
| 1695 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); |
| 1696 | if( rc==SQLITE_OK && encoding!=0 ){ |
| 1697 | rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); |
| 1698 | } |
drh | e70d01f | 2017-05-29 22:44:18 | [diff] [blame] | 1699 | if( rc ){ |
| 1700 | sqlite3Error(p->db, rc); |
| 1701 | rc = sqlite3ApiExit(p->db, rc); |
| 1702 | } |
drh | 2764170 | 2007-08-22 02:56:42 | [diff] [blame] | 1703 | } |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1704 | sqlite3_mutex_leave(p->db->mutex); |
drh | 94bb2ba | 2010-10-14 01:16:32 | [diff] [blame] | 1705 | }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ |
drh | 6fec9ee | 2010-10-12 02:13:32 | [diff] [blame] | 1706 | xDel((void*)zData); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1707 | } |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 1708 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1709 | } |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1710 | |
| 1711 | |
| 1712 | /* |
| 1713 | ** Bind a blob value to an SQL statement variable. |
| 1714 | */ |
| 1715 | int sqlite3_bind_blob( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1716 | sqlite3_stmt *pStmt, |
| 1717 | int i, |
| 1718 | const void *zData, |
| 1719 | int nData, |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1720 | void (*xDel)(void*) |
| 1721 | ){ |
drh | 5b081d8 | 2016-02-18 01:29:12 | [diff] [blame] | 1722 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1723 | if( nData<0 ) return SQLITE_MISUSE_BKPT; |
| 1724 | #endif |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1725 | return bindText(pStmt, i, zData, nData, xDel, 0); |
| 1726 | } |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 1727 | int sqlite3_bind_blob64( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1728 | sqlite3_stmt *pStmt, |
| 1729 | int i, |
| 1730 | const void *zData, |
| 1731 | sqlite3_uint64 nData, |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 1732 | void (*xDel)(void*) |
| 1733 | ){ |
drh | fc59a95 | 2014-09-11 23:34:55 | [diff] [blame] | 1734 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | d622855 | 2021-06-09 14:45:02 | [diff] [blame] | 1735 | return bindText(pStmt, i, zData, nData, xDel, 0); |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 1736 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1737 | int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 1738 | int rc; |
| 1739 | Vdbe *p = (Vdbe *)pStmt; |
drh | 403f002 | 2022-08-03 19:37:25 | [diff] [blame] | 1740 | rc = vdbeUnbind(p, (u32)(i-1)); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1741 | if( rc==SQLITE_OK ){ |
drh | 87dce45 | 2024-09-17 21:42:04 | [diff] [blame] | 1742 | assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ |
drh | 290c194 | 2004-08-21 17:54:45 | [diff] [blame] | 1743 | sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1744 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1745 | } |
danielk1977 | f461889 | 2004-06-28 13:09:11 | [diff] [blame] | 1746 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1747 | } |
| 1748 | int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
drh | efad999 | 2004-06-22 12:13:55 | [diff] [blame] | 1749 | return sqlite3_bind_int64(p, i, (i64)iValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1750 | } |
drh | efad999 | 2004-06-22 12:13:55 | [diff] [blame] | 1751 | int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1752 | int rc; |
| 1753 | Vdbe *p = (Vdbe *)pStmt; |
drh | 403f002 | 2022-08-03 19:37:25 | [diff] [blame] | 1754 | rc = vdbeUnbind(p, (u32)(i-1)); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1755 | if( rc==SQLITE_OK ){ |
drh | 87dce45 | 2024-09-17 21:42:04 | [diff] [blame] | 1756 | assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ |
drh | 290c194 | 2004-08-21 17:54:45 | [diff] [blame] | 1757 | sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1758 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1759 | } |
| 1760 | return rc; |
| 1761 | } |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 1762 | int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ |
| 1763 | int rc; |
| 1764 | Vdbe *p = (Vdbe*)pStmt; |
drh | 403f002 | 2022-08-03 19:37:25 | [diff] [blame] | 1765 | rc = vdbeUnbind(p, (u32)(i-1)); |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1766 | if( rc==SQLITE_OK ){ |
drh | 87dce45 | 2024-09-17 21:42:04 | [diff] [blame] | 1767 | assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1768 | sqlite3_mutex_leave(p->db->mutex); |
| 1769 | } |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 1770 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1771 | } |
drh | 2293006 | 2017-07-27 03:48:02 | [diff] [blame] | 1772 | int sqlite3_bind_pointer( |
| 1773 | sqlite3_stmt *pStmt, |
| 1774 | int i, |
| 1775 | void *pPtr, |
| 1776 | const char *zPTtype, |
| 1777 | void (*xDestructor)(void*) |
| 1778 | ){ |
drh | 3a96a5d | 2017-06-30 23:09:03 | [diff] [blame] | 1779 | int rc; |
| 1780 | Vdbe *p = (Vdbe*)pStmt; |
drh | 403f002 | 2022-08-03 19:37:25 | [diff] [blame] | 1781 | rc = vdbeUnbind(p, (u32)(i-1)); |
drh | 3a96a5d | 2017-06-30 23:09:03 | [diff] [blame] | 1782 | if( rc==SQLITE_OK ){ |
drh | 87dce45 | 2024-09-17 21:42:04 | [diff] [blame] | 1783 | assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ |
drh | 2293006 | 2017-07-27 03:48:02 | [diff] [blame] | 1784 | sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor); |
drh | 3a96a5d | 2017-06-30 23:09:03 | [diff] [blame] | 1785 | sqlite3_mutex_leave(p->db->mutex); |
drh | 34fcf36 | 2017-07-27 16:42:36 | [diff] [blame] | 1786 | }else if( xDestructor ){ |
| 1787 | xDestructor(pPtr); |
drh | 3a96a5d | 2017-06-30 23:09:03 | [diff] [blame] | 1788 | } |
| 1789 | return rc; |
| 1790 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1791 | int sqlite3_bind_text( |
| 1792 | sqlite3_stmt *pStmt, |
| 1793 | int i, |
| 1794 | const char *zData, |
| 1795 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1796 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1797 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 | [diff] [blame] | 1798 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1799 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1800 | int sqlite3_bind_text64( |
| 1801 | sqlite3_stmt *pStmt, |
| 1802 | int i, |
| 1803 | const char *zData, |
| 1804 | sqlite3_uint64 nData, |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 1805 | void (*xDel)(void*), |
| 1806 | unsigned char enc |
| 1807 | ){ |
drh | fc59a95 | 2014-09-11 23:34:55 | [diff] [blame] | 1808 | assert( xDel!=SQLITE_DYNAMIC ); |
drh | d2603ad | 2022-12-02 17:52:52 | [diff] [blame] | 1809 | if( enc!=SQLITE_UTF8 ){ |
| 1810 | if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; |
drh | 29c7c8b | 2025-04-06 10:22:26 | [diff] [blame] | 1811 | nData &= ~(u64)1; |
drh | d2603ad | 2022-12-02 17:52:52 | [diff] [blame] | 1812 | } |
drh | d622855 | 2021-06-09 14:45:02 | [diff] [blame] | 1813 | return bindText(pStmt, i, zData, nData, xDel, enc); |
drh | da4ca9d | 2014-09-09 17:27:35 | [diff] [blame] | 1814 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1815 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1816 | int sqlite3_bind_text16( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1817 | sqlite3_stmt *pStmt, |
| 1818 | int i, |
| 1819 | const void *zData, |
| 1820 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1821 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1822 | ){ |
drh | d2603ad | 2022-12-02 17:52:52 | [diff] [blame] | 1823 | return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 | [diff] [blame] | 1824 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 1825 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 | [diff] [blame] | 1826 | int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ |
| 1827 | int rc; |
drh | 1b27b8c | 2014-02-10 03:21:57 | [diff] [blame] | 1828 | switch( sqlite3_value_type((sqlite3_value*)pValue) ){ |
drh | 29def56 | 2009-04-14 12:43:33 | [diff] [blame] | 1829 | case SQLITE_INTEGER: { |
| 1830 | rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); |
| 1831 | break; |
| 1832 | } |
| 1833 | case SQLITE_FLOAT: { |
dan | 63a4733 | 2022-02-11 16:10:18 | [diff] [blame] | 1834 | assert( pValue->flags & (MEM_Real|MEM_IntReal) ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1835 | rc = sqlite3_bind_double(pStmt, i, |
dan | 63a4733 | 2022-02-11 16:10:18 | [diff] [blame] | 1836 | (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i |
| 1837 | ); |
drh | 29def56 | 2009-04-14 12:43:33 | [diff] [blame] | 1838 | break; |
| 1839 | } |
| 1840 | case SQLITE_BLOB: { |
| 1841 | if( pValue->flags & MEM_Zero ){ |
| 1842 | rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); |
| 1843 | }else{ |
| 1844 | rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); |
| 1845 | } |
| 1846 | break; |
| 1847 | } |
| 1848 | case SQLITE_TEXT: { |
drh | ac80db7 | 2009-04-14 12:58:20 | [diff] [blame] | 1849 | rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, |
| 1850 | pValue->enc); |
| 1851 | break; |
| 1852 | } |
| 1853 | default: { |
| 1854 | rc = sqlite3_bind_null(pStmt, i); |
drh | 29def56 | 2009-04-14 12:43:33 | [diff] [blame] | 1855 | break; |
| 1856 | } |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 | [diff] [blame] | 1857 | } |
| 1858 | return rc; |
| 1859 | } |
drh | b026e05 | 2007-05-02 01:34:31 | [diff] [blame] | 1860 | int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ |
| 1861 | int rc; |
| 1862 | Vdbe *p = (Vdbe *)pStmt; |
drh | 403f002 | 2022-08-03 19:37:25 | [diff] [blame] | 1863 | rc = vdbeUnbind(p, (u32)(i-1)); |
drh | b026e05 | 2007-05-02 01:34:31 | [diff] [blame] | 1864 | if( rc==SQLITE_OK ){ |
drh | 87dce45 | 2024-09-17 21:42:04 | [diff] [blame] | 1865 | assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ |
dan | a32536b | 2021-11-08 19:35:26 | [diff] [blame] | 1866 | #ifndef SQLITE_OMIT_INCRBLOB |
drh | b026e05 | 2007-05-02 01:34:31 | [diff] [blame] | 1867 | sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
dan | a32536b | 2021-11-08 19:35:26 | [diff] [blame] | 1868 | #else |
| 1869 | rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
| 1870 | #endif |
drh | 488e7b6 | 2008-10-06 12:46:43 | [diff] [blame] | 1871 | sqlite3_mutex_leave(p->db->mutex); |
drh | b026e05 | 2007-05-02 01:34:31 | [diff] [blame] | 1872 | } |
| 1873 | return rc; |
| 1874 | } |
dan | 80c0302 | 2015-07-24 17:36:34 | [diff] [blame] | 1875 | int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ |
| 1876 | int rc; |
| 1877 | Vdbe *p = (Vdbe *)pStmt; |
stephan | c76e450 | 2023-10-14 16:29:36 | [diff] [blame] | 1878 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1879 | if( p==0 ) return SQLITE_MISUSE_BKPT; |
| 1880 | #endif |
dan | 80c0302 | 2015-07-24 17:36:34 | [diff] [blame] | 1881 | sqlite3_mutex_enter(p->db->mutex); |
| 1882 | if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 1883 | rc = SQLITE_TOOBIG; |
| 1884 | }else{ |
| 1885 | assert( (n & 0x7FFFFFFF)==n ); |
| 1886 | rc = sqlite3_bind_zeroblob(pStmt, i, n); |
| 1887 | } |
| 1888 | rc = sqlite3ApiExit(p->db, rc); |
| 1889 | sqlite3_mutex_leave(p->db->mutex); |
| 1890 | return rc; |
| 1891 | } |
drh | 75f6a03 | 2004-07-15 14:15:00 | [diff] [blame] | 1892 | |
| 1893 | /* |
| 1894 | ** Return the number of wildcards that can be potentially bound to. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1895 | ** This routine is added to support DBD::SQLite. |
drh | 75f6a03 | 2004-07-15 14:15:00 | [diff] [blame] | 1896 | */ |
| 1897 | int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ |
drh | 92febd9 | 2004-08-20 18:34:20 | [diff] [blame] | 1898 | Vdbe *p = (Vdbe*)pStmt; |
| 1899 | return p ? p->nVar : 0; |
drh | 75f6a03 | 2004-07-15 14:15:00 | [diff] [blame] | 1900 | } |
drh | 895d747 | 2004-08-20 16:02:39 | [diff] [blame] | 1901 | |
| 1902 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 | [diff] [blame] | 1903 | ** Return the name of a wildcard parameter. Return NULL if the index |
| 1904 | ** is out of range or if the wildcard is unnamed. |
| 1905 | ** |
| 1906 | ** The result is always UTF-8. |
| 1907 | */ |
| 1908 | const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ |
| 1909 | Vdbe *p = (Vdbe*)pStmt; |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1910 | if( p==0 ) return 0; |
| 1911 | return sqlite3VListNumToName(p->pVList, i); |
drh | 895d747 | 2004-08-20 16:02:39 | [diff] [blame] | 1912 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 | [diff] [blame] | 1913 | |
| 1914 | /* |
| 1915 | ** Given a wildcard parameter name, return the index of the variable |
| 1916 | ** with that name. If there is no variable with the given name, |
| 1917 | ** return 0. |
| 1918 | */ |
drh | 5f18a22 | 2009-11-26 14:01:53 | [diff] [blame] | 1919 | int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1920 | if( p==0 || zName==0 ) return 0; |
| 1921 | return sqlite3VListNameToNum(p->pVList, zName, nName); |
drh | fa6bc00 | 2004-09-07 16:19:52 | [diff] [blame] | 1922 | } |
drh | 5f18a22 | 2009-11-26 14:01:53 | [diff] [blame] | 1923 | int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ |
| 1924 | return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); |
| 1925 | } |
drh | f8db1bc | 2005-04-22 02:38:37 | [diff] [blame] | 1926 | |
drh | 51942bc | 2005-06-12 22:01:42 | [diff] [blame] | 1927 | /* |
drh | f8db1bc | 2005-04-22 02:38:37 | [diff] [blame] | 1928 | ** Transfer all bindings from the first statement over to the second. |
drh | f8db1bc | 2005-04-22 02:38:37 | [diff] [blame] | 1929 | */ |
shane | 145834a | 2008-09-04 12:03:42 | [diff] [blame] | 1930 | int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
drh | f8db1bc | 2005-04-22 02:38:37 | [diff] [blame] | 1931 | Vdbe *pFrom = (Vdbe*)pFromStmt; |
| 1932 | Vdbe *pTo = (Vdbe*)pToStmt; |
drh | 0f5ea0b | 2009-04-09 14:02:44 | [diff] [blame] | 1933 | int i; |
| 1934 | assert( pTo->db==pFrom->db ); |
| 1935 | assert( pTo->nVar==pFrom->nVar ); |
drh | 2764170 | 2007-08-22 02:56:42 | [diff] [blame] | 1936 | sqlite3_mutex_enter(pTo->db->mutex); |
drh | 0f5ea0b | 2009-04-09 14:02:44 | [diff] [blame] | 1937 | for(i=0; i<pFrom->nVar; i++){ |
drh | 643167f | 2008-01-22 21:30:53 | [diff] [blame] | 1938 | sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); |
drh | f8db1bc | 2005-04-22 02:38:37 | [diff] [blame] | 1939 | } |
drh | 2764170 | 2007-08-22 02:56:42 | [diff] [blame] | 1940 | sqlite3_mutex_leave(pTo->db->mutex); |
drh | 0f5ea0b | 2009-04-09 14:02:44 | [diff] [blame] | 1941 | return SQLITE_OK; |
drh | f8db1bc | 2005-04-22 02:38:37 | [diff] [blame] | 1942 | } |
drh | 51942bc | 2005-06-12 22:01:42 | [diff] [blame] | 1943 | |
shane | eec556d | 2008-10-12 00:27:53 | [diff] [blame] | 1944 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 51942bc | 2005-06-12 22:01:42 | [diff] [blame] | 1945 | /* |
shane | 145834a | 2008-09-04 12:03:42 | [diff] [blame] | 1946 | ** Deprecated external interface. Internal/core SQLite code |
| 1947 | ** should call sqlite3TransferBindings. |
drh | 0f5ea0b | 2009-04-09 14:02:44 | [diff] [blame] | 1948 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 1949 | ** It is misuse to call this routine with statements from different |
drh | 0f5ea0b | 2009-04-09 14:02:44 | [diff] [blame] | 1950 | ** database connections. But as this is a deprecated interface, we |
| 1951 | ** will not bother to check for that condition. |
| 1952 | ** |
| 1953 | ** If the two statements contain a different number of bindings, then |
| 1954 | ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise |
| 1955 | ** SQLITE_OK is returned. |
shane | 145834a | 2008-09-04 12:03:42 | [diff] [blame] | 1956 | */ |
| 1957 | int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
drh | 0f5ea0b | 2009-04-09 14:02:44 | [diff] [blame] | 1958 | Vdbe *pFrom = (Vdbe*)pFromStmt; |
| 1959 | Vdbe *pTo = (Vdbe*)pToStmt; |
| 1960 | if( pFrom->nVar!=pTo->nVar ){ |
| 1961 | return SQLITE_ERROR; |
| 1962 | } |
drh | 2c2f392 | 2017-06-01 00:54:35 | [diff] [blame] | 1963 | assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 ); |
dan | bda4cb8 | 2017-02-23 16:30:16 | [diff] [blame] | 1964 | if( pTo->expmask ){ |
dan | c94b859 | 2009-10-20 07:01:24 | [diff] [blame] | 1965 | pTo->expired = 1; |
| 1966 | } |
drh | 2c2f392 | 2017-06-01 00:54:35 | [diff] [blame] | 1967 | assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 ); |
dan | bda4cb8 | 2017-02-23 16:30:16 | [diff] [blame] | 1968 | if( pFrom->expmask ){ |
dan | c94b859 | 2009-10-20 07:01:24 | [diff] [blame] | 1969 | pFrom->expired = 1; |
| 1970 | } |
shane | 145834a | 2008-09-04 12:03:42 | [diff] [blame] | 1971 | return sqlite3TransferBindings(pFromStmt, pToStmt); |
| 1972 | } |
shane | eec556d | 2008-10-12 00:27:53 | [diff] [blame] | 1973 | #endif |
shane | 145834a | 2008-09-04 12:03:42 | [diff] [blame] | 1974 | |
| 1975 | /* |
drh | 51942bc | 2005-06-12 22:01:42 | [diff] [blame] | 1976 | ** Return the sqlite3* database handle to which the prepared statement given |
| 1977 | ** in the argument belongs. This is the same database handle that was |
| 1978 | ** the first argument to the sqlite3_prepare() that was used to create |
| 1979 | ** the statement in the first place. |
| 1980 | */ |
| 1981 | sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ |
| 1982 | return pStmt ? ((Vdbe*)pStmt)->db : 0; |
| 1983 | } |
drh | bb5a9c3 | 2008-06-19 02:52:25 | [diff] [blame] | 1984 | |
| 1985 | /* |
drh | f03d9cc | 2010-11-16 23:10:25 | [diff] [blame] | 1986 | ** Return true if the prepared statement is guaranteed to not modify the |
| 1987 | ** database. |
| 1988 | */ |
| 1989 | int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ |
| 1990 | return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; |
| 1991 | } |
| 1992 | |
| 1993 | /* |
drh | 39c5c4a | 2019-03-06 14:53:27 | [diff] [blame] | 1994 | ** Return 1 if the statement is an EXPLAIN and return 2 if the |
| 1995 | ** statement is an EXPLAIN QUERY PLAN |
| 1996 | */ |
| 1997 | int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){ |
| 1998 | return pStmt ? ((Vdbe*)pStmt)->explain : 0; |
| 1999 | } |
| 2000 | |
| 2001 | /* |
drh | e393f6e | 2023-07-15 16:48:14 | [diff] [blame] | 2002 | ** Set the explain mode for a statement. |
| 2003 | */ |
| 2004 | int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){ |
| 2005 | Vdbe *v = (Vdbe*)pStmt; |
| 2006 | int rc; |
stephan | eb62ccd | 2023-10-14 20:01:55 | [diff] [blame] | 2007 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2008 | if( pStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 2009 | #endif |
drh | e393f6e | 2023-07-15 16:48:14 | [diff] [blame] | 2010 | sqlite3_mutex_enter(v->db->mutex); |
drh | 592ae28 | 2023-08-14 12:20:44 | [diff] [blame] | 2011 | if( ((int)v->explain)==eMode ){ |
drh | 0c6b869 | 2023-07-29 15:31:48 | [diff] [blame] | 2012 | rc = SQLITE_OK; |
| 2013 | }else if( eMode<0 || eMode>2 ){ |
| 2014 | rc = SQLITE_ERROR; |
| 2015 | }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){ |
| 2016 | rc = SQLITE_ERROR; |
| 2017 | }else if( v->eVdbeState!=VDBE_READY_STATE ){ |
| 2018 | rc = SQLITE_BUSY; |
| 2019 | }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){ |
drh | d1db37a | 2023-07-18 17:29:05 | [diff] [blame] | 2020 | /* No reprepare necessary */ |
| 2021 | v->explain = eMode; |
| 2022 | rc = SQLITE_OK; |
| 2023 | }else{ |
drh | d1db37a | 2023-07-18 17:29:05 | [diff] [blame] | 2024 | v->explain = eMode; |
| 2025 | rc = sqlite3Reprepare(v); |
drh | fb546c0 | 2023-07-29 17:05:35 | [diff] [blame] | 2026 | v->haveEqpOps = eMode==2; |
drh | d1db37a | 2023-07-18 17:29:05 | [diff] [blame] | 2027 | } |
| 2028 | if( v->explain ){ |
| 2029 | v->nResColumn = 12 - 4*v->explain; |
| 2030 | }else{ |
| 2031 | v->nResColumn = v->nResAlloc; |
| 2032 | } |
drh | e393f6e | 2023-07-15 16:48:14 | [diff] [blame] | 2033 | sqlite3_mutex_leave(v->db->mutex); |
| 2034 | return rc; |
| 2035 | } |
| 2036 | |
| 2037 | /* |
drh | 2fb6693 | 2011-11-25 17:21:47 | [diff] [blame] | 2038 | ** Return true if the prepared statement is in need of being reset. |
| 2039 | */ |
| 2040 | int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ |
| 2041 | Vdbe *v = (Vdbe*)pStmt; |
drh | 99a2182 | 2022-03-31 21:15:09 | [diff] [blame] | 2042 | return v!=0 && v->eVdbeState==VDBE_RUN_STATE; |
drh | 2fb6693 | 2011-11-25 17:21:47 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | /* |
drh | bb5a9c3 | 2008-06-19 02:52:25 | [diff] [blame] | 2046 | ** Return a pointer to the next prepared statement after pStmt associated |
| 2047 | ** with database connection pDb. If pStmt is NULL, return the first |
| 2048 | ** prepared statement for the database connection. Return NULL if there |
| 2049 | ** are no more. |
| 2050 | */ |
| 2051 | sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 2052 | sqlite3_stmt *pNext; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 2053 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2054 | if( !sqlite3SafetyCheckOk(pDb) ){ |
| 2055 | (void)SQLITE_MISUSE_BKPT; |
| 2056 | return 0; |
| 2057 | } |
| 2058 | #endif |
drh | bb5a9c3 | 2008-06-19 02:52:25 | [diff] [blame] | 2059 | sqlite3_mutex_enter(pDb->mutex); |
| 2060 | if( pStmt==0 ){ |
| 2061 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 2062 | }else{ |
drh | e5928b1 | 2022-08-23 20:11:01 | [diff] [blame] | 2063 | pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext; |
drh | bb5a9c3 | 2008-06-19 02:52:25 | [diff] [blame] | 2064 | } |
| 2065 | sqlite3_mutex_leave(pDb->mutex); |
| 2066 | return pNext; |
| 2067 | } |
drh | d1d3848 | 2008-10-07 23:46:38 | [diff] [blame] | 2068 | |
| 2069 | /* |
| 2070 | ** Return the value of a status counter for a prepared statement |
| 2071 | */ |
| 2072 | int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 2073 | Vdbe *pVdbe = (Vdbe*)pStmt; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 2074 | u32 v; |
| 2075 | #ifdef SQLITE_ENABLE_API_ARMOR |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2076 | if( !pStmt |
dan | 68cf69e | 2018-03-14 08:27:39 | [diff] [blame] | 2077 | || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter))) |
| 2078 | ){ |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 2079 | (void)SQLITE_MISUSE_BKPT; |
| 2080 | return 0; |
| 2081 | } |
| 2082 | #endif |
drh | 3528f6b | 2017-05-31 16:21:54 | [diff] [blame] | 2083 | if( op==SQLITE_STMTSTATUS_MEMUSED ){ |
| 2084 | sqlite3 *db = pVdbe->db; |
| 2085 | sqlite3_mutex_enter(db->mutex); |
| 2086 | v = 0; |
| 2087 | db->pnBytesFreed = (int*)&v; |
drh | 376860b | 2022-08-22 15:18:37 | [diff] [blame] | 2088 | assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); |
| 2089 | db->lookaside.pEnd = db->lookaside.pStart; |
drh | 1c84863 | 2022-04-04 01:12:11 | [diff] [blame] | 2090 | sqlite3VdbeDelete(pVdbe); |
drh | 3528f6b | 2017-05-31 16:21:54 | [diff] [blame] | 2091 | db->pnBytesFreed = 0; |
drh | 376860b | 2022-08-22 15:18:37 | [diff] [blame] | 2092 | db->lookaside.pEnd = db->lookaside.pTrueEnd; |
drh | 3528f6b | 2017-05-31 16:21:54 | [diff] [blame] | 2093 | sqlite3_mutex_leave(db->mutex); |
| 2094 | }else{ |
| 2095 | v = pVdbe->aCounter[op]; |
| 2096 | if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 2097 | } |
drh | 9b47ee3 | 2013-08-20 03:13:51 | [diff] [blame] | 2098 | return (int)v; |
drh | d1d3848 | 2008-10-07 23:46:38 | [diff] [blame] | 2099 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2100 | |
drh | 557341e | 2016-07-23 02:07:26 | [diff] [blame] | 2101 | /* |
| 2102 | ** Return the SQL associated with a prepared statement |
| 2103 | */ |
| 2104 | const char *sqlite3_sql(sqlite3_stmt *pStmt){ |
| 2105 | Vdbe *p = (Vdbe *)pStmt; |
| 2106 | return p ? p->zSql : 0; |
| 2107 | } |
| 2108 | |
| 2109 | /* |
| 2110 | ** Return the SQL associated with a prepared statement with |
| 2111 | ** bound parameters expanded. Space to hold the returned string is |
| 2112 | ** obtained from sqlite3_malloc(). The caller is responsible for |
| 2113 | ** freeing the returned string by passing it to sqlite3_free(). |
| 2114 | ** |
| 2115 | ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of |
| 2116 | ** expanded bound parameters. |
| 2117 | */ |
| 2118 | char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ |
| 2119 | #ifdef SQLITE_OMIT_TRACE |
| 2120 | return 0; |
| 2121 | #else |
| 2122 | char *z = 0; |
| 2123 | const char *zSql = sqlite3_sql(pStmt); |
| 2124 | if( zSql ){ |
| 2125 | Vdbe *p = (Vdbe *)pStmt; |
| 2126 | sqlite3_mutex_enter(p->db->mutex); |
| 2127 | z = sqlite3VdbeExpandSql(p, zSql); |
| 2128 | sqlite3_mutex_leave(p->db->mutex); |
| 2129 | } |
| 2130 | return z; |
| 2131 | #endif |
| 2132 | } |
| 2133 | |
mistachkin | 8bee11a | 2018-10-29 17:53:23 | [diff] [blame] | 2134 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 2135 | /* |
| 2136 | ** Return the normalized SQL associated with a prepared statement. |
| 2137 | */ |
| 2138 | const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){ |
| 2139 | Vdbe *p = (Vdbe *)pStmt; |
drh | 707821f | 2018-12-05 13:39:06 | [diff] [blame] | 2140 | if( p==0 ) return 0; |
drh | 1a6c2b1 | 2018-12-10 20:01:40 | [diff] [blame] | 2141 | if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){ |
drh | 1f169fe | 2018-12-06 01:08:58 | [diff] [blame] | 2142 | sqlite3_mutex_enter(p->db->mutex); |
drh | 1a6c2b1 | 2018-12-10 20:01:40 | [diff] [blame] | 2143 | p->zNormSql = sqlite3Normalize(p, p->zSql); |
drh | 1f169fe | 2018-12-06 01:08:58 | [diff] [blame] | 2144 | sqlite3_mutex_leave(p->db->mutex); |
drh | 707821f | 2018-12-05 13:39:06 | [diff] [blame] | 2145 | } |
| 2146 | return p->zNormSql; |
mistachkin | 8bee11a | 2018-10-29 17:53:23 | [diff] [blame] | 2147 | } |
| 2148 | #endif /* SQLITE_ENABLE_NORMALIZE */ |
| 2149 | |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2150 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2151 | /* |
dan | 93bca69 | 2011-09-14 19:41:44 | [diff] [blame] | 2152 | ** Allocate and populate an UnpackedRecord structure based on the serialized |
| 2153 | ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure |
| 2154 | ** if successful, or a NULL pointer if an OOM error is encountered. |
| 2155 | */ |
| 2156 | static UnpackedRecord *vdbeUnpackRecord( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2157 | KeyInfo *pKeyInfo, |
| 2158 | int nKey, |
dan | 93bca69 | 2011-09-14 19:41:44 | [diff] [blame] | 2159 | const void *pKey |
| 2160 | ){ |
dan | 93bca69 | 2011-09-14 19:41:44 | [diff] [blame] | 2161 | UnpackedRecord *pRet; /* Return value */ |
| 2162 | |
drh | a582b01 | 2016-12-21 19:45:54 | [diff] [blame] | 2163 | pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); |
dan | 93bca69 | 2011-09-14 19:41:44 | [diff] [blame] | 2164 | if( pRet ){ |
drh | a485ad1 | 2017-08-02 22:43:14 | [diff] [blame] | 2165 | memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1)); |
drh | 8658a8d | 2025-06-02 13:54:33 | [diff] [blame] | 2166 | sqlite3VdbeRecordUnpack(nKey, pKey, pRet); |
dan | 93bca69 | 2011-09-14 19:41:44 | [diff] [blame] | 2167 | } |
| 2168 | return pRet; |
| 2169 | } |
| 2170 | |
| 2171 | /* |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2172 | ** This function is called from within a pre-update callback to retrieve |
| 2173 | ** a field of the row currently being updated or deleted. |
| 2174 | */ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2175 | int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ |
stephan | 7dc0cc4 | 2023-10-13 12:48:35 | [diff] [blame] | 2176 | PreUpdate *p; |
dan | 7271d7a | 2017-01-25 18:53:27 | [diff] [blame] | 2177 | Mem *pMem; |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2178 | int rc = SQLITE_OK; |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2179 | int iStore = 0; |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2180 | |
stephan | 7dc0cc4 | 2023-10-13 12:48:35 | [diff] [blame] | 2181 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2182 | if( db==0 || ppValue==0 ){ |
| 2183 | return SQLITE_MISUSE_BKPT; |
| 2184 | } |
| 2185 | #endif |
| 2186 | p = db->pPreUpdate; |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2187 | /* Test that this call is being made from within an SQLITE_DELETE or |
| 2188 | ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2189 | if( !p || p->op==SQLITE_INSERT ){ |
| 2190 | rc = SQLITE_MISUSE_BKPT; |
| 2191 | goto preupdate_old_out; |
| 2192 | } |
dan | cb9a364 | 2017-01-30 19:44:53 | [diff] [blame] | 2193 | if( p->pPk ){ |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2194 | iStore = sqlite3TableColumnToIndex(p->pPk, iIdx); |
drh | 66cd200 | 2025-06-24 15:58:32 | [diff] [blame] | 2195 | }else if( iIdx >= p->pTab->nCol ){ |
| 2196 | rc = SQLITE_MISUSE_BKPT; |
| 2197 | goto preupdate_old_out; |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2198 | }else{ |
| 2199 | iStore = sqlite3TableColumnToStorage(p->pTab, iIdx); |
dan | cb9a364 | 2017-01-30 19:44:53 | [diff] [blame] | 2200 | } |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2201 | if( iStore>=p->pCsr->nField || iStore<0 ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2202 | rc = SQLITE_RANGE; |
| 2203 | goto preupdate_old_out; |
| 2204 | } |
| 2205 | |
dan | 7271d7a | 2017-01-25 18:53:27 | [diff] [blame] | 2206 | if( iIdx==p->pTab->iPKey ){ |
dan | 4bf24c8 | 2024-11-04 16:59:02 | [diff] [blame] | 2207 | *ppValue = pMem = &p->oldipk; |
dan | 7271d7a | 2017-01-25 18:53:27 | [diff] [blame] | 2208 | sqlite3VdbeMemSetInt64(pMem, p->iKey1); |
dan | 4bf24c8 | 2024-11-04 16:59:02 | [diff] [blame] | 2209 | }else{ |
| 2210 | |
| 2211 | /* If the old.* record has not yet been loaded into memory, do so now. */ |
| 2212 | if( p->pUnpacked==0 ){ |
| 2213 | u32 nRec; |
| 2214 | u8 *aRec; |
| 2215 | |
| 2216 | assert( p->pCsr->eCurType==CURTYPE_BTREE ); |
| 2217 | nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); |
| 2218 | aRec = sqlite3DbMallocRaw(db, nRec); |
| 2219 | if( !aRec ) goto preupdate_old_out; |
| 2220 | rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); |
| 2221 | if( rc==SQLITE_OK ){ |
drh | b6e8f65 | 2025-03-14 19:07:11 | [diff] [blame] | 2222 | p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec); |
dan | 4bf24c8 | 2024-11-04 16:59:02 | [diff] [blame] | 2223 | if( !p->pUnpacked ) rc = SQLITE_NOMEM; |
dan | 38b31a9 | 2024-09-18 15:02:27 | [diff] [blame] | 2224 | } |
dan | 4bf24c8 | 2024-11-04 16:59:02 | [diff] [blame] | 2225 | if( rc!=SQLITE_OK ){ |
| 2226 | sqlite3DbFree(db, aRec); |
| 2227 | goto preupdate_old_out; |
dan | 38b31a9 | 2024-09-18 15:02:27 | [diff] [blame] | 2228 | } |
dan | 4bf24c8 | 2024-11-04 16:59:02 | [diff] [blame] | 2229 | p->aRecord = aRec; |
dan | 38b31a9 | 2024-09-18 15:02:27 | [diff] [blame] | 2230 | } |
dan | 4bf24c8 | 2024-11-04 16:59:02 | [diff] [blame] | 2231 | |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2232 | pMem = *ppValue = &p->pUnpacked->aMem[iStore]; |
| 2233 | if( iStore>=p->pUnpacked->nField ){ |
dan | 4bf24c8 | 2024-11-04 16:59:02 | [diff] [blame] | 2234 | /* This occurs when the table has been extended using ALTER TABLE |
| 2235 | ** ADD COLUMN. The value to return is the default value of the column. */ |
| 2236 | Column *pCol = &p->pTab->aCol[iIdx]; |
| 2237 | if( pCol->iDflt>0 ){ |
| 2238 | if( p->apDflt==0 ){ |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 2239 | int nByte; |
| 2240 | assert( sizeof(sqlite3_value*)*UMXV(p->pTab->nCol) < 0x7fffffff ); |
| 2241 | nByte = sizeof(sqlite3_value*)*p->pTab->nCol; |
dan | 4bf24c8 | 2024-11-04 16:59:02 | [diff] [blame] | 2242 | p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte); |
| 2243 | if( p->apDflt==0 ) goto preupdate_old_out; |
| 2244 | } |
| 2245 | if( p->apDflt[iIdx]==0 ){ |
| 2246 | sqlite3_value *pVal = 0; |
| 2247 | Expr *pDflt; |
| 2248 | assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) ); |
| 2249 | pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; |
| 2250 | rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal); |
| 2251 | if( rc==SQLITE_OK && pVal==0 ){ |
| 2252 | rc = SQLITE_CORRUPT_BKPT; |
| 2253 | } |
| 2254 | p->apDflt[iIdx] = pVal; |
| 2255 | } |
| 2256 | *ppValue = p->apDflt[iIdx]; |
| 2257 | }else{ |
| 2258 | *ppValue = (sqlite3_value *)columnNullValue(); |
| 2259 | } |
| 2260 | }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ |
| 2261 | if( pMem->flags & (MEM_Int|MEM_IntReal) ){ |
| 2262 | testcase( pMem->flags & MEM_Int ); |
| 2263 | testcase( pMem->flags & MEM_IntReal ); |
| 2264 | sqlite3VdbeMemRealify(pMem); |
| 2265 | } |
dan | 319eeb7 | 2011-03-19 08:38:50 | [diff] [blame] | 2266 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | preupdate_old_out: |
drh | e1ed0b0 | 2014-08-26 02:15:07 | [diff] [blame] | 2270 | sqlite3Error(db, rc); |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2271 | return sqlite3ApiExit(db, rc); |
| 2272 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2273 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2274 | |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2275 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2276 | /* |
| 2277 | ** This function is called from within a pre-update callback to retrieve |
| 2278 | ** the number of columns in the row being updated, deleted or inserted. |
| 2279 | */ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2280 | int sqlite3_preupdate_count(sqlite3 *db){ |
stephan | 7dc0cc4 | 2023-10-13 12:48:35 | [diff] [blame] | 2281 | PreUpdate *p; |
| 2282 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2283 | p = db!=0 ? db->pPreUpdate : 0; |
| 2284 | #else |
| 2285 | p = db->pPreUpdate; |
| 2286 | #endif |
drh | b6e8f65 | 2025-03-14 19:07:11 | [diff] [blame] | 2287 | return (p ? p->pKeyinfo->nKeyField : 0); |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2288 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2289 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2290 | |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2291 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2292 | /* |
dan | 1e7a2d4 | 2011-03-22 18:45:29 | [diff] [blame] | 2293 | ** This function is designed to be called from within a pre-update callback |
| 2294 | ** only. It returns zero if the change that caused the callback was made |
| 2295 | ** immediately by a user SQL statement. Or, if the change was made by a |
| 2296 | ** trigger program, it returns the number of trigger programs currently |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2297 | ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a |
dan | 1e7a2d4 | 2011-03-22 18:45:29 | [diff] [blame] | 2298 | ** top-level trigger etc.). |
| 2299 | ** |
| 2300 | ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL |
| 2301 | ** or SET DEFAULT action is considered a trigger. |
| 2302 | */ |
| 2303 | int sqlite3_preupdate_depth(sqlite3 *db){ |
stephan | 7dc0cc4 | 2023-10-13 12:48:35 | [diff] [blame] | 2304 | PreUpdate *p; |
| 2305 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2306 | p = db!=0 ? db->pPreUpdate : 0; |
| 2307 | #else |
| 2308 | p = db->pPreUpdate; |
| 2309 | #endif |
dan | 1e7a2d4 | 2011-03-22 18:45:29 | [diff] [blame] | 2310 | return (p ? p->v->nFrame : 0); |
| 2311 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2312 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 1e7a2d4 | 2011-03-22 18:45:29 | [diff] [blame] | 2313 | |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2314 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 1e7a2d4 | 2011-03-22 18:45:29 | [diff] [blame] | 2315 | /* |
dan | a23a873 | 2021-04-21 20:52:17 | [diff] [blame] | 2316 | ** This function is designed to be called from within a pre-update callback |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2317 | ** only. |
dan | a23a873 | 2021-04-21 20:52:17 | [diff] [blame] | 2318 | */ |
| 2319 | int sqlite3_preupdate_blobwrite(sqlite3 *db){ |
stephan | 7dc0cc4 | 2023-10-13 12:48:35 | [diff] [blame] | 2320 | PreUpdate *p; |
| 2321 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2322 | p = db!=0 ? db->pPreUpdate : 0; |
| 2323 | #else |
| 2324 | p = db->pPreUpdate; |
| 2325 | #endif |
dan | a23a873 | 2021-04-21 20:52:17 | [diff] [blame] | 2326 | return (p ? p->iBlobWrite : -1); |
| 2327 | } |
| 2328 | #endif |
| 2329 | |
| 2330 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 2331 | /* |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2332 | ** This function is called from within a pre-update callback to retrieve |
| 2333 | ** a field of the row currently being updated or inserted. |
| 2334 | */ |
| 2335 | int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ |
stephan | 7dc0cc4 | 2023-10-13 12:48:35 | [diff] [blame] | 2336 | PreUpdate *p; |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2337 | int rc = SQLITE_OK; |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2338 | Mem *pMem; |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2339 | int iStore = 0; |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2340 | |
stephan | 7dc0cc4 | 2023-10-13 12:48:35 | [diff] [blame] | 2341 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2342 | if( db==0 || ppValue==0 ){ |
| 2343 | return SQLITE_MISUSE_BKPT; |
| 2344 | } |
| 2345 | #endif |
| 2346 | p = db->pPreUpdate; |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2347 | if( !p || p->op==SQLITE_DELETE ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2348 | rc = SQLITE_MISUSE_BKPT; |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2349 | goto preupdate_new_out; |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2350 | } |
dan | cb9a364 | 2017-01-30 19:44:53 | [diff] [blame] | 2351 | if( p->pPk && p->op!=SQLITE_UPDATE ){ |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2352 | iStore = sqlite3TableColumnToIndex(p->pPk, iIdx); |
drh | a67c712 | 2025-06-24 18:27:59 | [diff] [blame] | 2353 | }else if( iIdx >= p->pTab->nCol ){ |
| 2354 | return SQLITE_MISUSE_BKPT; |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2355 | }else{ |
| 2356 | iStore = sqlite3TableColumnToStorage(p->pTab, iIdx); |
dan | cb9a364 | 2017-01-30 19:44:53 | [diff] [blame] | 2357 | } |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2358 | |
| 2359 | if( iStore>=p->pCsr->nField || iStore<0 ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2360 | rc = SQLITE_RANGE; |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2361 | goto preupdate_new_out; |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2362 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2363 | |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2364 | if( p->op==SQLITE_INSERT ){ |
| 2365 | /* For an INSERT, memory cell p->iNewReg contains the serialized record |
| 2366 | ** that is being inserted. Deserialize it. */ |
| 2367 | UnpackedRecord *pUnpack = p->pNewUnpacked; |
| 2368 | if( !pUnpack ){ |
| 2369 | Mem *pData = &p->v->aMem[p->iNewReg]; |
drh | ff535a2 | 2016-09-20 01:46:15 | [diff] [blame] | 2370 | rc = ExpandBlob(pData); |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2371 | if( rc!=SQLITE_OK ) goto preupdate_new_out; |
drh | b6e8f65 | 2025-03-14 19:07:11 | [diff] [blame] | 2372 | pUnpack = vdbeUnpackRecord(p->pKeyinfo, pData->n, pData->z); |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2373 | if( !pUnpack ){ |
| 2374 | rc = SQLITE_NOMEM; |
| 2375 | goto preupdate_new_out; |
| 2376 | } |
| 2377 | p->pNewUnpacked = pUnpack; |
| 2378 | } |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2379 | pMem = &pUnpack->aMem[iStore]; |
dan | 2a86c19 | 2017-01-25 17:44:13 | [diff] [blame] | 2380 | if( iIdx==p->pTab->iPKey ){ |
| 2381 | sqlite3VdbeMemSetInt64(pMem, p->iKey2); |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2382 | }else if( iStore>=pUnpack->nField ){ |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2383 | pMem = (sqlite3_value *)columnNullValue(); |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2384 | } |
| 2385 | }else{ |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2386 | /* For an UPDATE, memory cell (p->iNewReg+1+iStore) contains the required |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2387 | ** value. Make a copy of the cell contents and return a pointer to it. |
| 2388 | ** It is not safe to return a pointer to the memory cell itself as the |
| 2389 | ** caller may modify the value text encoding. |
| 2390 | */ |
| 2391 | assert( p->op==SQLITE_UPDATE ); |
| 2392 | if( !p->aNew ){ |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 2393 | assert( sizeof(Mem)*UMXV(p->pCsr->nField) < 0x7fffffff ); |
| 2394 | p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem)*p->pCsr->nField); |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2395 | if( !p->aNew ){ |
| 2396 | rc = SQLITE_NOMEM; |
| 2397 | goto preupdate_new_out; |
| 2398 | } |
| 2399 | } |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2400 | assert( iStore>=0 && iStore<p->pCsr->nField ); |
| 2401 | pMem = &p->aNew[iStore]; |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2402 | if( pMem->flags==0 ){ |
dan | e43635a | 2016-10-21 21:21:45 | [diff] [blame] | 2403 | if( iIdx==p->pTab->iPKey ){ |
dan | 319eeb7 | 2011-03-19 08:38:50 | [diff] [blame] | 2404 | sqlite3VdbeMemSetInt64(pMem, p->iKey2); |
| 2405 | }else{ |
dan | 9dcf3d0 | 2025-01-28 19:03:37 | [diff] [blame] | 2406 | rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iStore]); |
dan | 319eeb7 | 2011-03-19 08:38:50 | [diff] [blame] | 2407 | if( rc!=SQLITE_OK ) goto preupdate_new_out; |
| 2408 | } |
dan | 37db03b | 2011-03-16 19:59:18 | [diff] [blame] | 2409 | } |
| 2410 | } |
| 2411 | *ppValue = pMem; |
| 2412 | |
| 2413 | preupdate_new_out: |
drh | e1ed0b0 | 2014-08-26 02:15:07 | [diff] [blame] | 2414 | sqlite3Error(db, rc); |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2415 | return sqlite3ApiExit(db, rc); |
| 2416 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2417 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
drh | 04e8a58 | 2014-11-18 21:20:57 | [diff] [blame] | 2418 | |
dan | 6f9702e | 2014-11-01 20:38:06 | [diff] [blame] | 2419 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 04489b6 | 2014-10-31 20:11:32 | [diff] [blame] | 2420 | /* |
| 2421 | ** Return status data for a single loop within query pStmt. |
| 2422 | */ |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2423 | int sqlite3_stmt_scanstatus_v2( |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2424 | sqlite3_stmt *pStmt, /* Prepared statement being queried */ |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2425 | int iScan, /* Index of loop to report on */ |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2426 | int iScanStatusOp, /* Which metric to return */ |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2427 | int flags, |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2428 | void *pOut /* OUT: Write the answer here */ |
dan | 04489b6 | 2014-10-31 20:11:32 | [diff] [blame] | 2429 | ){ |
| 2430 | Vdbe *p = (Vdbe*)pStmt; |
stephan | eb62ccd | 2023-10-14 20:01:55 | [diff] [blame] | 2431 | VdbeOp *aOp; |
| 2432 | int nOp; |
drh | 54b81e3 | 2023-04-01 15:51:21 | [diff] [blame] | 2433 | ScanStatus *pScan = 0; |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2434 | int idx; |
| 2435 | |
stephan | eb62ccd | 2023-10-14 20:01:55 | [diff] [blame] | 2436 | #ifdef SQLITE_ENABLE_API_ARMOR |
stephan | b866f98 | 2023-10-17 02:15:49 | [diff] [blame] | 2437 | if( p==0 || pOut==0 |
| 2438 | || iScanStatusOp<SQLITE_SCANSTAT_NLOOP |
| 2439 | || iScanStatusOp>SQLITE_SCANSTAT_NCYCLE ){ |
| 2440 | return 1; |
| 2441 | } |
stephan | eb62ccd | 2023-10-14 20:01:55 | [diff] [blame] | 2442 | #endif |
| 2443 | aOp = p->aOp; |
| 2444 | nOp = p->nOp; |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2445 | if( p->pFrame ){ |
| 2446 | VdbeFrame *pFrame; |
| 2447 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 2448 | aOp = pFrame->aOp; |
| 2449 | nOp = pFrame->nOp; |
| 2450 | } |
| 2451 | |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2452 | if( iScan<0 ){ |
| 2453 | int ii; |
| 2454 | if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){ |
| 2455 | i64 res = 0; |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2456 | for(ii=0; ii<nOp; ii++){ |
| 2457 | res += aOp[ii].nCycle; |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2458 | } |
| 2459 | *(i64*)pOut = res; |
| 2460 | return 0; |
| 2461 | } |
| 2462 | return 1; |
| 2463 | } |
| 2464 | if( flags & SQLITE_SCANSTAT_COMPLEX ){ |
| 2465 | idx = iScan; |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2466 | }else{ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2467 | /* If the COMPLEX flag is clear, then this function must ignore any |
dan | 7f4b066 | 2022-12-07 20:09:54 | [diff] [blame] | 2468 | ** ScanStatus structures with ScanStatus.addrLoop set to 0. */ |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2469 | for(idx=0; idx<p->nScan; idx++){ |
| 2470 | pScan = &p->aScan[idx]; |
dan | a3d0c15 | 2022-12-05 18:19:56 | [diff] [blame] | 2471 | if( pScan->zName ){ |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2472 | iScan--; |
| 2473 | if( iScan<0 ) break; |
| 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | if( idx>=p->nScan ) return 1; |
dan | eb5bd4d | 2024-03-25 18:30:15 | [diff] [blame] | 2478 | assert( pScan==0 || pScan==&p->aScan[idx] ); |
| 2479 | pScan = &p->aScan[idx]; |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2480 | |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2481 | switch( iScanStatusOp ){ |
| 2482 | case SQLITE_SCANSTAT_NLOOP: { |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2483 | if( pScan->addrLoop>0 ){ |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2484 | *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec; |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2485 | }else{ |
| 2486 | *(sqlite3_int64*)pOut = -1; |
| 2487 | } |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2488 | break; |
| 2489 | } |
| 2490 | case SQLITE_SCANSTAT_NVISIT: { |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2491 | if( pScan->addrVisit>0 ){ |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2492 | *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec; |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2493 | }else{ |
| 2494 | *(sqlite3_int64*)pOut = -1; |
| 2495 | } |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2496 | break; |
| 2497 | } |
| 2498 | case SQLITE_SCANSTAT_EST: { |
drh | 518140e | 2014-11-06 03:55:10 | [diff] [blame] | 2499 | double r = 1.0; |
| 2500 | LogEst x = pScan->nEst; |
| 2501 | while( x<100 ){ |
| 2502 | x += 10; |
| 2503 | r *= 0.5; |
| 2504 | } |
| 2505 | *(double*)pOut = r*sqlite3LogEstToInt(x); |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2506 | break; |
| 2507 | } |
| 2508 | case SQLITE_SCANSTAT_NAME: { |
dan | d72219d | 2014-11-03 16:39:37 | [diff] [blame] | 2509 | *(const char**)pOut = pScan->zName; |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2510 | break; |
| 2511 | } |
| 2512 | case SQLITE_SCANSTAT_EXPLAIN: { |
| 2513 | if( pScan->addrExplain ){ |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2514 | *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z; |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2515 | }else{ |
| 2516 | *(const char**)pOut = 0; |
| 2517 | } |
| 2518 | break; |
| 2519 | } |
drh | c6652b1 | 2014-11-06 04:42:20 | [diff] [blame] | 2520 | case SQLITE_SCANSTAT_SELECTID: { |
| 2521 | if( pScan->addrExplain ){ |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2522 | *(int*)pOut = aOp[ pScan->addrExplain ].p1; |
drh | c6652b1 | 2014-11-06 04:42:20 | [diff] [blame] | 2523 | }else{ |
| 2524 | *(int*)pOut = -1; |
| 2525 | } |
| 2526 | break; |
| 2527 | } |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2528 | case SQLITE_SCANSTAT_PARENTID: { |
| 2529 | if( pScan->addrExplain ){ |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2530 | *(int*)pOut = aOp[ pScan->addrExplain ].p2; |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2531 | }else{ |
| 2532 | *(int*)pOut = -1; |
| 2533 | } |
| 2534 | break; |
| 2535 | } |
| 2536 | case SQLITE_SCANSTAT_NCYCLE: { |
dan | ad23a47 | 2022-12-03 21:24:26 | [diff] [blame] | 2537 | i64 res = 0; |
| 2538 | if( pScan->aAddrRange[0]==0 ){ |
| 2539 | res = -1; |
| 2540 | }else{ |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2541 | int ii; |
dan | f6f01f1 | 2022-12-03 18:16:25 | [diff] [blame] | 2542 | for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){ |
| 2543 | int iIns = pScan->aAddrRange[ii]; |
| 2544 | int iEnd = pScan->aAddrRange[ii+1]; |
| 2545 | if( iIns==0 ) break; |
dan | ad23a47 | 2022-12-03 21:24:26 | [diff] [blame] | 2546 | if( iIns>0 ){ |
| 2547 | while( iIns<=iEnd ){ |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2548 | res += aOp[iIns].nCycle; |
dan | ad23a47 | 2022-12-03 21:24:26 | [diff] [blame] | 2549 | iIns++; |
| 2550 | } |
| 2551 | }else{ |
| 2552 | int iOp; |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2553 | for(iOp=0; iOp<nOp; iOp++){ |
| 2554 | Op *pOp = &aOp[iOp]; |
dan | ad23a47 | 2022-12-03 21:24:26 | [diff] [blame] | 2555 | if( pOp->p1!=iEnd ) continue; |
dan | 2adb309 | 2022-12-06 18:48:06 | [diff] [blame] | 2556 | if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){ |
dan | ad23a47 | 2022-12-03 21:24:26 | [diff] [blame] | 2557 | continue; |
| 2558 | } |
dan | 7b56e97 | 2023-03-29 18:54:01 | [diff] [blame] | 2559 | res += aOp[iOp].nCycle; |
dan | ad23a47 | 2022-12-03 21:24:26 | [diff] [blame] | 2560 | } |
dan | f6f01f1 | 2022-12-03 18:16:25 | [diff] [blame] | 2561 | } |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2562 | } |
| 2563 | } |
| 2564 | *(i64*)pOut = res; |
| 2565 | break; |
| 2566 | } |
drh | d1a1c23 | 2014-11-03 16:35:55 | [diff] [blame] | 2567 | default: { |
| 2568 | return 1; |
dan | 6f9702e | 2014-11-01 20:38:06 | [diff] [blame] | 2569 | } |
| 2570 | } |
dan | 04489b6 | 2014-10-31 20:11:32 | [diff] [blame] | 2571 | return 0; |
| 2572 | } |
| 2573 | |
| 2574 | /* |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2575 | ** Return status data for a single loop within query pStmt. |
| 2576 | */ |
| 2577 | int sqlite3_stmt_scanstatus( |
| 2578 | sqlite3_stmt *pStmt, /* Prepared statement being queried */ |
| 2579 | int iScan, /* Index of loop to report on */ |
| 2580 | int iScanStatusOp, /* Which metric to return */ |
| 2581 | void *pOut /* OUT: Write the answer here */ |
| 2582 | ){ |
| 2583 | return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut); |
| 2584 | } |
| 2585 | |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 2586 | /* |
dan | 04489b6 | 2014-10-31 20:11:32 | [diff] [blame] | 2587 | ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. |
| 2588 | */ |
| 2589 | void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ |
| 2590 | Vdbe *p = (Vdbe*)pStmt; |
dan | 7f4b066 | 2022-12-07 20:09:54 | [diff] [blame] | 2591 | int ii; |
stephan | eb62ccd | 2023-10-14 20:01:55 | [diff] [blame] | 2592 | for(ii=0; p!=0 && ii<p->nOp; ii++){ |
dan | 7f4b066 | 2022-12-07 20:09:54 | [diff] [blame] | 2593 | Op *pOp = &p->aOp[ii]; |
| 2594 | pOp->nExec = 0; |
| 2595 | pOp->nCycle = 0; |
| 2596 | } |
dan | 04489b6 | 2014-10-31 20:11:32 | [diff] [blame] | 2597 | } |
dan | 6f9702e | 2014-11-01 20:38:06 | [diff] [blame] | 2598 | #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ |