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