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. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame^] | 16 | ** $Id: vdbeapi.c,v 1.155 2009/03/19 18:51:07 danielk1977 Exp $ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
| 19 | #include "vdbeInt.h" |
| 20 | |
danielk1977 | 67e3da7 | 2008-08-21 12:19:44 +0000 | [diff] [blame] | 21 | #if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 22 | /* |
| 23 | ** The following structure contains pointers to the end points of a |
| 24 | ** doubly-linked list of all compiled SQL statements that may be holding |
| 25 | ** buffers eligible for release when the sqlite3_release_memory() interface is |
| 26 | ** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2 |
| 27 | ** mutex. |
| 28 | ** |
| 29 | ** Statements are added to the end of this list when sqlite3_reset() is |
| 30 | ** called. They are removed either when sqlite3_step() or sqlite3_finalize() |
| 31 | ** is called. When statements are added to this list, the associated |
| 32 | ** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that |
| 33 | ** can be freed using sqlite3VdbeReleaseMemory(). |
| 34 | ** |
| 35 | ** When statements are added or removed from this list, the mutex |
| 36 | ** associated with the Vdbe being added or removed (Vdbe.db->mutex) is |
| 37 | ** already held. The LRU2 mutex is then obtained, blocking if necessary, |
| 38 | ** the linked-list pointers manipulated and the LRU2 mutex relinquished. |
| 39 | */ |
| 40 | struct StatementLruList { |
| 41 | Vdbe *pFirst; |
| 42 | Vdbe *pLast; |
| 43 | }; |
| 44 | static struct StatementLruList sqlite3LruStatements; |
| 45 | |
| 46 | /* |
| 47 | ** Check that the list looks to be internally consistent. This is used |
| 48 | ** as part of an assert() statement as follows: |
| 49 | ** |
| 50 | ** assert( stmtLruCheck() ); |
| 51 | */ |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 52 | #ifndef NDEBUG |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 53 | static int stmtLruCheck(){ |
| 54 | Vdbe *p; |
| 55 | for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){ |
| 56 | assert(p->pLruNext || p==sqlite3LruStatements.pLast); |
| 57 | assert(!p->pLruNext || p->pLruNext->pLruPrev==p); |
| 58 | assert(p->pLruPrev || p==sqlite3LruStatements.pFirst); |
| 59 | assert(!p->pLruPrev || p->pLruPrev->pLruNext==p); |
| 60 | } |
| 61 | return 1; |
| 62 | } |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 63 | #endif |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | ** Add vdbe p to the end of the statement lru list. It is assumed that |
| 67 | ** p is not already part of the list when this is called. The lru list |
| 68 | ** is protected by the SQLITE_MUTEX_STATIC_LRU mutex. |
| 69 | */ |
| 70 | static void stmtLruAdd(Vdbe *p){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 71 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 72 | |
| 73 | if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 74 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
| 78 | assert( stmtLruCheck() ); |
| 79 | |
| 80 | if( !sqlite3LruStatements.pFirst ){ |
| 81 | assert( !sqlite3LruStatements.pLast ); |
| 82 | sqlite3LruStatements.pFirst = p; |
| 83 | sqlite3LruStatements.pLast = p; |
| 84 | }else{ |
| 85 | assert( !sqlite3LruStatements.pLast->pLruNext ); |
| 86 | p->pLruPrev = sqlite3LruStatements.pLast; |
| 87 | sqlite3LruStatements.pLast->pLruNext = p; |
| 88 | sqlite3LruStatements.pLast = p; |
| 89 | } |
| 90 | |
| 91 | assert( stmtLruCheck() ); |
| 92 | |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 93 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* |
| 97 | ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove |
| 98 | ** statement p from the least-recently-used statement list. If the |
| 99 | ** statement is not currently part of the list, this call is a no-op. |
| 100 | */ |
| 101 | static void stmtLruRemoveNomutex(Vdbe *p){ |
| 102 | if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){ |
| 103 | assert( stmtLruCheck() ); |
| 104 | if( p->pLruNext ){ |
| 105 | p->pLruNext->pLruPrev = p->pLruPrev; |
| 106 | }else{ |
| 107 | sqlite3LruStatements.pLast = p->pLruPrev; |
| 108 | } |
| 109 | if( p->pLruPrev ){ |
| 110 | p->pLruPrev->pLruNext = p->pLruNext; |
| 111 | }else{ |
| 112 | sqlite3LruStatements.pFirst = p->pLruNext; |
| 113 | } |
| 114 | p->pLruNext = 0; |
| 115 | p->pLruPrev = 0; |
| 116 | assert( stmtLruCheck() ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove |
| 122 | ** statement p from the least-recently-used statement list. If the |
| 123 | ** statement is not currently part of the list, this call is a no-op. |
| 124 | */ |
| 125 | static void stmtLruRemove(Vdbe *p){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 126 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 127 | stmtLruRemoveNomutex(p); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 128 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | ** Try to release n bytes of memory by freeing buffers associated |
| 133 | ** with the memory registers of currently unused vdbes. |
| 134 | */ |
| 135 | int sqlite3VdbeReleaseMemory(int n){ |
| 136 | Vdbe *p; |
| 137 | Vdbe *pNext; |
| 138 | int nFree = 0; |
| 139 | |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 140 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 141 | for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){ |
| 142 | pNext = p->pLruNext; |
| 143 | |
| 144 | /* For each statement handle in the lru list, attempt to obtain the |
| 145 | ** associated database mutex. If it cannot be obtained, continue |
| 146 | ** to the next statement handle. It is not possible to block on |
| 147 | ** the database mutex - that could cause deadlock. |
| 148 | */ |
| 149 | if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){ |
| 150 | nFree += sqlite3VdbeReleaseBuffers(p); |
| 151 | stmtLruRemoveNomutex(p); |
| 152 | sqlite3_mutex_leave(p->db->mutex); |
| 153 | } |
| 154 | } |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 155 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2)); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 156 | |
| 157 | return nFree; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | ** Call sqlite3Reprepare() on the statement. Remove it from the |
| 162 | ** lru list before doing so, as Reprepare() will free all the |
| 163 | ** memory register buffers anyway. |
| 164 | */ |
| 165 | int vdbeReprepare(Vdbe *p){ |
| 166 | stmtLruRemove(p); |
| 167 | return sqlite3Reprepare(p); |
| 168 | } |
| 169 | |
| 170 | #else /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 171 | #define stmtLruRemove(x) |
| 172 | #define stmtLruAdd(x) |
| 173 | #define vdbeReprepare(x) sqlite3Reprepare(x) |
| 174 | #endif |
| 175 | |
| 176 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 177 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 178 | /* |
| 179 | ** Return TRUE (non-zero) of the statement supplied as an argument needs |
| 180 | ** to be recompiled. A statement needs to be recompiled whenever the |
| 181 | ** execution environment changes in a way that would alter the program |
| 182 | ** that sqlite3_prepare() generates. For example, if new functions or |
| 183 | ** collating sequences are registered or if an authorizer function is |
| 184 | ** added or changed. |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 185 | */ |
| 186 | int sqlite3_expired(sqlite3_stmt *pStmt){ |
| 187 | Vdbe *p = (Vdbe*)pStmt; |
| 188 | return p==0 || p->expired; |
| 189 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 190 | #endif |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 191 | |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 192 | /* |
| 193 | ** The following routine destroys a virtual machine that is created by |
| 194 | ** the sqlite3_compile() routine. The integer returned is an SQLITE_ |
| 195 | ** success/failure code that describes the result of executing the virtual |
| 196 | ** machine. |
| 197 | ** |
| 198 | ** This routine sets the error code and string returned by |
| 199 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 200 | */ |
| 201 | int sqlite3_finalize(sqlite3_stmt *pStmt){ |
| 202 | int rc; |
| 203 | if( pStmt==0 ){ |
| 204 | rc = SQLITE_OK; |
| 205 | }else{ |
| 206 | Vdbe *v = (Vdbe*)pStmt; |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame^] | 207 | sqlite3 *db = v->db; |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 208 | #if SQLITE_THREADSAFE |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 209 | sqlite3_mutex *mutex = v->db->mutex; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 210 | #endif |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 211 | sqlite3_mutex_enter(mutex); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 212 | stmtLruRemove(v); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 213 | rc = sqlite3VdbeFinalize(v); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame^] | 214 | rc = sqlite3ApiExit(db, rc); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 215 | sqlite3_mutex_leave(mutex); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 216 | } |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | ** Terminate the current execution of an SQL statement and reset it |
| 222 | ** back to its starting state so that it can be reused. A success code from |
| 223 | ** the prior execution is returned. |
| 224 | ** |
| 225 | ** This routine sets the error code and string returned by |
| 226 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 227 | */ |
| 228 | int sqlite3_reset(sqlite3_stmt *pStmt){ |
| 229 | int rc; |
| 230 | if( pStmt==0 ){ |
| 231 | rc = SQLITE_OK; |
| 232 | }else{ |
| 233 | Vdbe *v = (Vdbe*)pStmt; |
| 234 | sqlite3_mutex_enter(v->db->mutex); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 235 | rc = sqlite3VdbeReset(v); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 236 | stmtLruAdd(v); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 237 | sqlite3VdbeMakeReady(v, -1, 0, 0, 0); |
| 238 | assert( (rc & (v->db->errMask))==rc ); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame^] | 239 | rc = sqlite3ApiExit(v->db, rc); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 240 | sqlite3_mutex_leave(v->db->mutex); |
| 241 | } |
| 242 | return rc; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | ** Set all the parameters in the compiled SQL statement to NULL. |
| 247 | */ |
| 248 | int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ |
| 249 | int i; |
| 250 | int rc = SQLITE_OK; |
drh | 7297d1f | 2008-05-09 14:39:44 +0000 | [diff] [blame] | 251 | Vdbe *p = (Vdbe*)pStmt; |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 252 | #if SQLITE_THREADSAFE |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 253 | sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; |
| 254 | #endif |
| 255 | sqlite3_mutex_enter(mutex); |
drh | 7297d1f | 2008-05-09 14:39:44 +0000 | [diff] [blame] | 256 | for(i=0; i<p->nVar; i++){ |
| 257 | sqlite3VdbeMemRelease(&p->aVar[i]); |
| 258 | p->aVar[i].flags = MEM_Null; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 259 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 260 | sqlite3_mutex_leave(mutex); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 261 | return rc; |
| 262 | } |
| 263 | |
| 264 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 265 | /**************************** sqlite3_value_ ******************************* |
| 266 | ** The following routines extract information from a Mem or sqlite3_value |
| 267 | ** structure. |
| 268 | */ |
| 269 | const void *sqlite3_value_blob(sqlite3_value *pVal){ |
| 270 | Mem *p = (Mem*)pVal; |
| 271 | if( p->flags & (MEM_Blob|MEM_Str) ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 272 | sqlite3VdbeMemExpandBlob(p); |
drh | 1f0feef | 2007-05-15 13:27:07 +0000 | [diff] [blame] | 273 | p->flags &= ~MEM_Str; |
| 274 | p->flags |= MEM_Blob; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 275 | return p->z; |
| 276 | }else{ |
| 277 | return sqlite3_value_text(pVal); |
| 278 | } |
| 279 | } |
| 280 | int sqlite3_value_bytes(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 281 | return sqlite3ValueBytes(pVal, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 282 | } |
| 283 | int sqlite3_value_bytes16(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 284 | return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 285 | } |
| 286 | double sqlite3_value_double(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 287 | return sqlite3VdbeRealValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 288 | } |
| 289 | int sqlite3_value_int(sqlite3_value *pVal){ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 290 | return (int)sqlite3VdbeIntValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 291 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 292 | sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 293 | return sqlite3VdbeIntValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 294 | } |
| 295 | const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 296 | return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 297 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 298 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 299 | const void *sqlite3_value_text16(sqlite3_value* pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 300 | return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 301 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 302 | const void *sqlite3_value_text16be(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 303 | return sqlite3ValueText(pVal, SQLITE_UTF16BE); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 304 | } |
| 305 | const void *sqlite3_value_text16le(sqlite3_value *pVal){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 306 | return sqlite3ValueText(pVal, SQLITE_UTF16LE); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 307 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 308 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 309 | int sqlite3_value_type(sqlite3_value* pVal){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 310 | return pVal->type; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /**************************** sqlite3_result_ ******************************* |
| 314 | ** The following routines are used by user-defined functions to specify |
| 315 | ** the function result. |
| 316 | */ |
| 317 | void sqlite3_result_blob( |
| 318 | sqlite3_context *pCtx, |
| 319 | const void *z, |
| 320 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 321 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 322 | ){ |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame] | 323 | assert( n>=0 ); |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 324 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 325 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 326 | } |
| 327 | void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 328 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 329 | sqlite3VdbeMemSetDouble(&pCtx->s, rVal); |
| 330 | } |
| 331 | void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 332 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 333 | pCtx->isError = SQLITE_ERROR; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 334 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 335 | } |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 336 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 337 | void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 338 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 339 | pCtx->isError = SQLITE_ERROR; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 340 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 341 | } |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 342 | #endif |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 343 | void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 344 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 345 | sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); |
| 346 | } |
| 347 | void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 348 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 349 | sqlite3VdbeMemSetInt64(&pCtx->s, iVal); |
| 350 | } |
| 351 | void sqlite3_result_null(sqlite3_context *pCtx){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 352 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 353 | sqlite3VdbeMemSetNull(&pCtx->s); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 354 | } |
| 355 | void sqlite3_result_text( |
| 356 | sqlite3_context *pCtx, |
| 357 | const char *z, |
| 358 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 359 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 360 | ){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 361 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 362 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 363 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 364 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 365 | void sqlite3_result_text16( |
| 366 | sqlite3_context *pCtx, |
| 367 | const void *z, |
| 368 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 369 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 370 | ){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 371 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 372 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 373 | } |
| 374 | void sqlite3_result_text16be( |
| 375 | sqlite3_context *pCtx, |
| 376 | const void *z, |
| 377 | int n, |
| 378 | void (*xDel)(void *) |
| 379 | ){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 380 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 381 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 382 | } |
| 383 | void sqlite3_result_text16le( |
| 384 | sqlite3_context *pCtx, |
| 385 | const void *z, |
| 386 | int n, |
| 387 | void (*xDel)(void *) |
| 388 | ){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 389 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 390 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 391 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 392 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 393 | void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 394 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 395 | sqlite3VdbeMemCopy(&pCtx->s, pValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 396 | } |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 397 | void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 398 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 399 | sqlite3VdbeMemSetZeroBlob(&pCtx->s, n); |
| 400 | } |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 401 | void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ |
| 402 | pCtx->isError = errCode; |
| 403 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 404 | |
drh | a0206bc | 2007-05-08 15:15:02 +0000 | [diff] [blame] | 405 | /* Force an SQLITE_TOOBIG error. */ |
| 406 | void sqlite3_result_error_toobig(sqlite3_context *pCtx){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 407 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 408 | pCtx->isError = SQLITE_TOOBIG; |
| 409 | sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, |
| 410 | SQLITE_UTF8, SQLITE_STATIC); |
drh | a0206bc | 2007-05-08 15:15:02 +0000 | [diff] [blame] | 411 | } |
| 412 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 413 | /* An SQLITE_NOMEM error. */ |
| 414 | void sqlite3_result_error_nomem(sqlite3_context *pCtx){ |
| 415 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
| 416 | sqlite3VdbeMemSetNull(&pCtx->s); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 417 | pCtx->isError = SQLITE_NOMEM; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 418 | pCtx->s.db->mallocFailed = 1; |
| 419 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | ** Execute the statement pStmt, either until a row of data is ready, the |
| 423 | ** statement is completely executed or an error occurs. |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 424 | ** |
| 425 | ** This routine implements the bulk of the logic behind the sqlite_step() |
| 426 | ** API. The only thing omitted is the automatic recompile if a |
| 427 | ** schema change has occurred. That detail is handled by the |
| 428 | ** outer sqlite3_step() wrapper procedure. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 429 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 430 | static int sqlite3Step(Vdbe *p){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 431 | sqlite3 *db; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 432 | int rc; |
| 433 | |
danielk1977 | a185ddb | 2007-11-15 16:04:15 +0000 | [diff] [blame] | 434 | assert(p); |
| 435 | if( p->magic!=VDBE_MAGIC_RUN ){ |
drh | f1bfe9a | 2007-10-17 01:44:20 +0000 | [diff] [blame] | 436 | return SQLITE_MISUSE; |
| 437 | } |
| 438 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 439 | /* Assert that malloc() has not failed */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 440 | db = p->db; |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 441 | if( db->mallocFailed ){ |
| 442 | return SQLITE_NOMEM; |
| 443 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 444 | |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 445 | if( p->pc<=0 && p->expired ){ |
| 446 | if( p->rc==SQLITE_OK ){ |
| 447 | p->rc = SQLITE_SCHEMA; |
| 448 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 449 | rc = SQLITE_ERROR; |
| 450 | goto end_of_step; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 451 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 452 | if( sqlite3SafetyOn(db) ){ |
| 453 | p->rc = SQLITE_MISUSE; |
| 454 | return SQLITE_MISUSE; |
| 455 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 456 | if( p->pc<0 ){ |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 457 | /* If there are no other statements currently running, then |
| 458 | ** reset the interrupt flag. This prevents a call to sqlite3_interrupt |
| 459 | ** from interrupting a statement that has not yet started. |
| 460 | */ |
| 461 | if( db->activeVdbeCnt==0 ){ |
| 462 | db->u1.isInterrupted = 0; |
| 463 | } |
| 464 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 465 | #ifndef SQLITE_OMIT_TRACE |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 466 | if( db->xProfile && !db->init.busy ){ |
| 467 | double rNow; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 468 | sqlite3OsCurrentTime(db->pVfs, &rNow); |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 469 | p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 470 | } |
| 471 | #endif |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 472 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 473 | db->activeVdbeCnt++; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 474 | if( p->readOnly==0 ) db->writeVdbeCnt++; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 475 | p->pc = 0; |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 476 | stmtLruRemove(p); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 477 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 478 | #ifndef SQLITE_OMIT_EXPLAIN |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 479 | if( p->explain ){ |
| 480 | rc = sqlite3VdbeList(p); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 481 | }else |
| 482 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 483 | { |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 484 | rc = sqlite3VdbeExec(p); |
| 485 | } |
| 486 | |
| 487 | if( sqlite3SafetyOff(db) ){ |
| 488 | rc = SQLITE_MISUSE; |
| 489 | } |
| 490 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 491 | #ifndef SQLITE_OMIT_TRACE |
| 492 | /* Invoke the profile callback if there is one |
| 493 | */ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 494 | if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 495 | double rNow; |
| 496 | u64 elapseTime; |
| 497 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 498 | sqlite3OsCurrentTime(db->pVfs, &rNow); |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 499 | elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0); |
| 500 | elapseTime -= p->startTime; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 501 | db->xProfile(db->pProfileArg, p->zSql, elapseTime); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 502 | } |
| 503 | #endif |
| 504 | |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 505 | db->errCode = rc; |
| 506 | /*sqlite3Error(p->db, rc, 0);*/ |
danielk1977 | 76e8d1a | 2006-01-18 18:22:43 +0000 | [diff] [blame] | 507 | p->rc = sqlite3ApiExit(p->db, p->rc); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 508 | end_of_step: |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 509 | assert( (rc&0xff)==rc ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 510 | if( p->isPrepareV2 && (rc&0xff)<SQLITE_ROW ){ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 511 | /* This behavior occurs if sqlite3_prepare_v2() was used to build |
| 512 | ** the prepared statement. Return error codes directly */ |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 513 | p->db->errCode = p->rc; |
| 514 | /* sqlite3Error(p->db, p->rc, 0); */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 515 | return p->rc; |
| 516 | }else{ |
| 517 | /* This is for legacy sqlite3_prepare() builds and when the code |
| 518 | ** is SQLITE_ROW or SQLITE_DONE */ |
| 519 | return rc; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | ** This is the top-level implementation of sqlite3_step(). Call |
| 525 | ** sqlite3Step() to do most of the work. If a schema error occurs, |
| 526 | ** call sqlite3Reprepare() and try again. |
| 527 | */ |
| 528 | #ifdef SQLITE_OMIT_PARSER |
| 529 | int sqlite3_step(sqlite3_stmt *pStmt){ |
danielk1977 | a185ddb | 2007-11-15 16:04:15 +0000 | [diff] [blame] | 530 | int rc = SQLITE_MISUSE; |
| 531 | if( pStmt ){ |
| 532 | Vdbe *v; |
| 533 | v = (Vdbe*)pStmt; |
| 534 | sqlite3_mutex_enter(v->db->mutex); |
| 535 | rc = sqlite3Step(v); |
| 536 | sqlite3_mutex_leave(v->db->mutex); |
| 537 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 538 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 539 | } |
| 540 | #else |
| 541 | int sqlite3_step(sqlite3_stmt *pStmt){ |
danielk1977 | a185ddb | 2007-11-15 16:04:15 +0000 | [diff] [blame] | 542 | int rc = SQLITE_MISUSE; |
| 543 | if( pStmt ){ |
| 544 | int cnt = 0; |
| 545 | Vdbe *v = (Vdbe*)pStmt; |
| 546 | sqlite3 *db = v->db; |
| 547 | sqlite3_mutex_enter(db->mutex); |
| 548 | while( (rc = sqlite3Step(v))==SQLITE_SCHEMA |
| 549 | && cnt++ < 5 |
danielk1977 | 65a2ea1 | 2009-03-19 07:58:31 +0000 | [diff] [blame] | 550 | && (rc = vdbeReprepare(v))==SQLITE_OK ){ |
danielk1977 | a185ddb | 2007-11-15 16:04:15 +0000 | [diff] [blame] | 551 | sqlite3_reset(pStmt); |
| 552 | v->expired = 0; |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 553 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 554 | if( rc==SQLITE_SCHEMA && v->isPrepareV2 && db->pErr ){ |
danielk1977 | a185ddb | 2007-11-15 16:04:15 +0000 | [diff] [blame] | 555 | /* This case occurs after failing to recompile an sql statement. |
| 556 | ** The error message from the SQL compiler has already been loaded |
| 557 | ** into the database handle. This block copies the error message |
| 558 | ** from the database handle into the statement and sets the statement |
| 559 | ** program counter to 0 to ensure that when the statement is |
| 560 | ** finalized or reset the parser error message is available via |
| 561 | ** sqlite3_errmsg() and sqlite3_errcode(). |
| 562 | */ |
| 563 | const char *zErr = (const char *)sqlite3_value_text(db->pErr); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 564 | sqlite3DbFree(db, v->zErrMsg); |
danielk1977 | a185ddb | 2007-11-15 16:04:15 +0000 | [diff] [blame] | 565 | if( !db->mallocFailed ){ |
| 566 | v->zErrMsg = sqlite3DbStrDup(db, zErr); |
| 567 | } else { |
| 568 | v->zErrMsg = 0; |
| 569 | v->rc = SQLITE_NOMEM; |
| 570 | } |
| 571 | } |
| 572 | rc = sqlite3ApiExit(db, rc); |
| 573 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 574 | } |
danielk1977 | 76e8d1a | 2006-01-18 18:22:43 +0000 | [diff] [blame] | 575 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 576 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 577 | #endif |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 578 | |
| 579 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 580 | ** Extract the user data from a sqlite3_context structure and return a |
| 581 | ** pointer to it. |
| 582 | */ |
| 583 | void *sqlite3_user_data(sqlite3_context *p){ |
| 584 | assert( p && p->pFunc ); |
| 585 | return p->pFunc->pUserData; |
| 586 | } |
| 587 | |
| 588 | /* |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 589 | ** Extract the user data from a sqlite3_context structure and return a |
| 590 | ** pointer to it. |
| 591 | */ |
| 592 | sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ |
| 593 | assert( p && p->pFunc ); |
| 594 | return p->s.db; |
| 595 | } |
| 596 | |
| 597 | /* |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 598 | ** The following is the implementation of an SQL function that always |
| 599 | ** fails with an error message stating that the function is used in the |
| 600 | ** wrong context. The sqlite3_overload_function() API might construct |
| 601 | ** SQL function that use this routine so that the functions will exist |
| 602 | ** for name resolution but are actually overloaded by the xFindFunction |
| 603 | ** method of virtual tables. |
| 604 | */ |
| 605 | void sqlite3InvalidFunction( |
| 606 | sqlite3_context *context, /* The function calling context */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 607 | int NotUsed, /* Number of arguments to the function */ |
| 608 | sqlite3_value **NotUsed2 /* Value of each argument */ |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 609 | ){ |
| 610 | const char *zName = context->pFunc->zName; |
| 611 | char *zErr; |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 612 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 613 | zErr = sqlite3MPrintf(0, |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 614 | "unable to use function %s in the requested context", zName); |
| 615 | sqlite3_result_error(context, zErr, -1); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 616 | sqlite3_free(zErr); |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 620 | ** Allocate or return the aggregate context for a user function. A new |
| 621 | ** context is allocated on the first call. Subsequent calls return the |
| 622 | ** same context that was returned on prior calls. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 623 | */ |
| 624 | void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 625 | Mem *pMem; |
drh | 825c662 | 2005-09-08 19:01:05 +0000 | [diff] [blame] | 626 | assert( p && p->pFunc && p->pFunc->xStep ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 627 | assert( sqlite3_mutex_held(p->s.db->mutex) ); |
| 628 | pMem = p->pMem; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 629 | if( (pMem->flags & MEM_Agg)==0 ){ |
| 630 | if( nByte==0 ){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 631 | sqlite3VdbeMemReleaseExternal(pMem); |
| 632 | pMem->flags = MEM_Null; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 633 | pMem->z = 0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 634 | }else{ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 635 | sqlite3VdbeMemGrow(pMem, nByte, 0); |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 636 | pMem->flags = MEM_Agg; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 637 | pMem->u.pDef = p->pFunc; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 638 | if( pMem->z ){ |
| 639 | memset(pMem->z, 0, nByte); |
| 640 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 643 | return (void*)pMem->z; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 647 | ** Return the auxilary data pointer, if any, for the iArg'th argument to |
| 648 | ** the user-function defined by pCtx. |
| 649 | */ |
| 650 | void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 651 | VdbeFunc *pVdbeFunc; |
| 652 | |
| 653 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
| 654 | pVdbeFunc = pCtx->pVdbeFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 655 | if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ |
| 656 | return 0; |
| 657 | } |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 658 | return pVdbeFunc->apAux[iArg].pAux; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | ** Set the auxilary data pointer and delete function, for the iArg'th |
| 663 | ** argument to the user-function defined by pCtx. Any previous value is |
| 664 | ** deleted by calling the delete function specified when it was set. |
| 665 | */ |
| 666 | void sqlite3_set_auxdata( |
| 667 | sqlite3_context *pCtx, |
| 668 | int iArg, |
| 669 | void *pAux, |
| 670 | void (*xDelete)(void*) |
| 671 | ){ |
| 672 | struct AuxData *pAuxData; |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 673 | VdbeFunc *pVdbeFunc; |
danielk1977 | e0fc526 | 2007-07-26 06:50:05 +0000 | [diff] [blame] | 674 | if( iArg<0 ) goto failed; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 675 | |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 676 | assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 677 | pVdbeFunc = pCtx->pVdbeFunc; |
| 678 | if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 679 | int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0); |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 680 | int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 681 | pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 682 | if( !pVdbeFunc ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 683 | goto failed; |
| 684 | } |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 685 | pCtx->pVdbeFunc = pVdbeFunc; |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 686 | memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux)); |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 687 | pVdbeFunc->nAux = iArg+1; |
| 688 | pVdbeFunc->pFunc = pCtx->pFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 689 | } |
| 690 | |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 691 | pAuxData = &pVdbeFunc->apAux[iArg]; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 692 | if( pAuxData->pAux && pAuxData->xDelete ){ |
| 693 | pAuxData->xDelete(pAuxData->pAux); |
| 694 | } |
| 695 | pAuxData->pAux = pAux; |
| 696 | pAuxData->xDelete = xDelete; |
danielk1977 | e0fc526 | 2007-07-26 06:50:05 +0000 | [diff] [blame] | 697 | return; |
| 698 | |
| 699 | failed: |
| 700 | if( xDelete ){ |
| 701 | xDelete(pAux); |
| 702 | } |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 703 | } |
| 704 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 705 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 706 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 707 | ** Return the number of times the Step function of a aggregate has been |
| 708 | ** called. |
| 709 | ** |
drh | cf85a51 | 2006-02-09 18:35:29 +0000 | [diff] [blame] | 710 | ** This function is deprecated. Do not use it for new code. It is |
| 711 | ** provide only to avoid breaking legacy code. New aggregate function |
| 712 | ** implementations should keep their own counts within their aggregate |
| 713 | ** context. |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 714 | */ |
| 715 | int sqlite3_aggregate_count(sqlite3_context *p){ |
shane | e34c647 | 2009-03-05 04:23:47 +0000 | [diff] [blame] | 716 | assert( p && p->pMem && p->pFunc && p->pFunc->xStep ); |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 717 | return p->pMem->n; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 718 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 719 | #endif |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 720 | |
| 721 | /* |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 722 | ** Return the number of columns in the result set for the statement pStmt. |
| 723 | */ |
| 724 | int sqlite3_column_count(sqlite3_stmt *pStmt){ |
| 725 | Vdbe *pVm = (Vdbe *)pStmt; |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 726 | return pVm ? pVm->nResColumn : 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* |
| 730 | ** Return the number of values available from the current row of the |
| 731 | ** currently executing statement pStmt. |
| 732 | */ |
| 733 | int sqlite3_data_count(sqlite3_stmt *pStmt){ |
| 734 | Vdbe *pVm = (Vdbe *)pStmt; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 735 | if( pVm==0 || pVm->pResultSet==0 ) return 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 736 | return pVm->nResColumn; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | /* |
| 741 | ** Check to see if column iCol of the given statement is valid. If |
| 742 | ** it is, return a pointer to the Mem for the value of that column. |
| 743 | ** If iCol is not valid, return a pointer to a Mem which has a value |
| 744 | ** of NULL. |
| 745 | */ |
| 746 | static Mem *columnMem(sqlite3_stmt *pStmt, int i){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 747 | Vdbe *pVm; |
| 748 | int vals; |
| 749 | Mem *pOut; |
| 750 | |
| 751 | pVm = (Vdbe *)pStmt; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 752 | if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 753 | sqlite3_mutex_enter(pVm->db->mutex); |
| 754 | vals = sqlite3_data_count(pStmt); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 755 | pOut = &pVm->pResultSet[i]; |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 756 | }else{ |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 757 | /* ((double)0) In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 758 | static const Mem nullMem = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 }; |
shane | e34c647 | 2009-03-05 04:23:47 +0000 | [diff] [blame] | 759 | if( pVm && pVm->db ){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 760 | sqlite3_mutex_enter(pVm->db->mutex); |
| 761 | sqlite3Error(pVm->db, SQLITE_RANGE, 0); |
| 762 | } |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 763 | pOut = (Mem*)&nullMem; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 764 | } |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 765 | return pOut; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 766 | } |
| 767 | |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 768 | /* |
| 769 | ** This function is called after invoking an sqlite3_value_XXX function on a |
| 770 | ** column value (i.e. a value returned by evaluating an SQL expression in the |
| 771 | ** select list of a SELECT statement) that may cause a malloc() failure. If |
| 772 | ** malloc() has failed, the threads mallocFailed flag is cleared and the result |
| 773 | ** code of statement pStmt set to SQLITE_NOMEM. |
| 774 | ** |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 775 | ** Specifically, this is called from within: |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 776 | ** |
| 777 | ** sqlite3_column_int() |
| 778 | ** sqlite3_column_int64() |
| 779 | ** sqlite3_column_text() |
| 780 | ** sqlite3_column_text16() |
| 781 | ** sqlite3_column_real() |
| 782 | ** sqlite3_column_bytes() |
| 783 | ** sqlite3_column_bytes16() |
| 784 | ** |
| 785 | ** But not for sqlite3_column_blob(), which never calls malloc(). |
| 786 | */ |
| 787 | static void columnMallocFailure(sqlite3_stmt *pStmt) |
| 788 | { |
| 789 | /* If malloc() failed during an encoding conversion within an |
| 790 | ** sqlite3_column_XXX API, then set the return code of the statement to |
| 791 | ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR |
| 792 | ** and _finalize() will return NOMEM. |
| 793 | */ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 794 | Vdbe *p = (Vdbe *)pStmt; |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 795 | if( p ){ |
| 796 | p->rc = sqlite3ApiExit(p->db, p->rc); |
| 797 | sqlite3_mutex_leave(p->db->mutex); |
| 798 | } |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 799 | } |
| 800 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 801 | /**************************** sqlite3_column_ ******************************* |
| 802 | ** The following routines are used to access elements of the current row |
| 803 | ** in the result set. |
| 804 | */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 805 | const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 806 | const void *val; |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 807 | val = sqlite3_value_blob( columnMem(pStmt,i) ); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 808 | /* Even though there is no encoding conversion, value_blob() might |
| 809 | ** need to call malloc() to expand the result of a zeroblob() |
| 810 | ** expression. |
| 811 | */ |
| 812 | columnMallocFailure(pStmt); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 813 | return val; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 814 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 815 | int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 816 | int val = sqlite3_value_bytes( columnMem(pStmt,i) ); |
| 817 | columnMallocFailure(pStmt); |
| 818 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 819 | } |
| 820 | int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 821 | int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); |
| 822 | columnMallocFailure(pStmt); |
| 823 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 824 | } |
| 825 | double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 826 | double val = sqlite3_value_double( columnMem(pStmt,i) ); |
| 827 | columnMallocFailure(pStmt); |
| 828 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 829 | } |
| 830 | int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 831 | int val = sqlite3_value_int( columnMem(pStmt,i) ); |
| 832 | columnMallocFailure(pStmt); |
| 833 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 834 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 835 | sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 836 | sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); |
| 837 | columnMallocFailure(pStmt); |
| 838 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 839 | } |
| 840 | const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 841 | const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); |
| 842 | columnMallocFailure(pStmt); |
| 843 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 844 | } |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 845 | sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ |
danielk1977 | d0ffa1e | 2008-10-13 10:37:49 +0000 | [diff] [blame] | 846 | Mem *pOut = columnMem(pStmt, i); |
| 847 | if( pOut->flags&MEM_Static ){ |
| 848 | pOut->flags &= ~MEM_Static; |
| 849 | pOut->flags |= MEM_Ephem; |
| 850 | } |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 851 | columnMallocFailure(pStmt); |
danielk1977 | d0ffa1e | 2008-10-13 10:37:49 +0000 | [diff] [blame] | 852 | return (sqlite3_value *)pOut; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 853 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 854 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 855 | const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 856 | const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); |
| 857 | columnMallocFailure(pStmt); |
| 858 | return val; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 859 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 860 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 861 | int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 862 | int iType = sqlite3_value_type( columnMem(pStmt,i) ); |
| 863 | columnMallocFailure(pStmt); |
| 864 | return iType; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 865 | } |
| 866 | |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 867 | /* The following function is experimental and subject to change or |
| 868 | ** removal */ |
| 869 | /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ |
| 870 | ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); |
| 871 | **} |
| 872 | */ |
| 873 | |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 874 | /* |
| 875 | ** Convert the N-th element of pStmt->pColName[] into a string using |
| 876 | ** xFunc() then return that string. If N is out of range, return 0. |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 877 | ** |
| 878 | ** There are up to 5 names for each column. useType determines which |
| 879 | ** name is returned. Here are the names: |
| 880 | ** |
| 881 | ** 0 The column name as it should be displayed for output |
| 882 | ** 1 The datatype name for the column |
| 883 | ** 2 The name of the database that the column derives from |
| 884 | ** 3 The name of the table that the column derives from |
| 885 | ** 4 The name of the table column that the result column derives from |
| 886 | ** |
| 887 | ** If the result is not a simple column reference (if it is an expression |
| 888 | ** or a constant) then useTypes 2, 3, and 4 return NULL. |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 889 | */ |
| 890 | static const void *columnName( |
| 891 | sqlite3_stmt *pStmt, |
| 892 | int N, |
| 893 | const void *(*xFunc)(Mem*), |
| 894 | int useType |
| 895 | ){ |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 896 | const void *ret = 0; |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 897 | Vdbe *p = (Vdbe *)pStmt; |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 898 | int n; |
| 899 | |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 900 | |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 901 | if( p!=0 ){ |
| 902 | n = sqlite3_column_count(pStmt); |
| 903 | if( N<n && N>=0 ){ |
| 904 | N += useType*n; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 905 | sqlite3_mutex_enter(p->db->mutex); |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 906 | ret = xFunc(&p->aColName[N]); |
| 907 | |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 908 | /* A malloc may have failed inside of the xFunc() call. If this |
| 909 | ** is the case, clear the mallocFailed flag and return NULL. |
| 910 | */ |
| 911 | if( p->db && p->db->mallocFailed ){ |
| 912 | p->db->mallocFailed = 0; |
| 913 | ret = 0; |
| 914 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 915 | sqlite3_mutex_leave(p->db->mutex); |
drh | 32bc3f6 | 2007-08-21 20:25:39 +0000 | [diff] [blame] | 916 | } |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 917 | } |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 918 | return ret; |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 919 | } |
| 920 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 921 | /* |
| 922 | ** Return the name of the Nth column of the result set returned by SQL |
| 923 | ** statement pStmt. |
| 924 | */ |
| 925 | const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 926 | return columnName( |
| 927 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 928 | } |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 929 | #ifndef SQLITE_OMIT_UTF16 |
| 930 | const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 931 | return columnName( |
| 932 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 933 | } |
| 934 | #endif |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 935 | |
| 936 | /* |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 937 | ** Constraint: If you have ENABLE_COLUMN_METADATA then you must |
| 938 | ** not define OMIT_DECLTYPE. |
| 939 | */ |
| 940 | #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) |
| 941 | # error "Must not define both SQLITE_OMIT_DECLTYPE \ |
| 942 | and SQLITE_ENABLE_COLUMN_METADATA" |
| 943 | #endif |
| 944 | |
| 945 | #ifndef SQLITE_OMIT_DECLTYPE |
| 946 | /* |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 947 | ** Return the column declaration type (if applicable) of the 'i'th column |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 948 | ** of the result set of SQL statement pStmt. |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 949 | */ |
| 950 | const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 951 | return columnName( |
| 952 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 953 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 954 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 955 | const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 956 | return columnName( |
| 957 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 958 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 959 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 960 | #endif /* SQLITE_OMIT_DECLTYPE */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 961 | |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 962 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 963 | /* |
| 964 | ** Return the name of the database from which a result column derives. |
| 965 | ** NULL is returned if the result column is an expression or constant or |
| 966 | ** anything else which is not an unabiguous reference to a database column. |
| 967 | */ |
| 968 | const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 969 | return columnName( |
| 970 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 971 | } |
| 972 | #ifndef SQLITE_OMIT_UTF16 |
| 973 | const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 974 | return columnName( |
| 975 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 976 | } |
| 977 | #endif /* SQLITE_OMIT_UTF16 */ |
| 978 | |
| 979 | /* |
| 980 | ** Return the name of the table from which a result column derives. |
| 981 | ** NULL is returned if the result column is an expression or constant or |
| 982 | ** anything else which is not an unabiguous reference to a database column. |
| 983 | */ |
| 984 | const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 985 | return columnName( |
| 986 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 987 | } |
| 988 | #ifndef SQLITE_OMIT_UTF16 |
| 989 | const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 990 | return columnName( |
| 991 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 992 | } |
| 993 | #endif /* SQLITE_OMIT_UTF16 */ |
| 994 | |
| 995 | /* |
| 996 | ** Return the name of the table column from which a result column derives. |
| 997 | ** NULL is returned if the result column is an expression or constant or |
| 998 | ** anything else which is not an unabiguous reference to a database column. |
| 999 | */ |
| 1000 | const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1001 | return columnName( |
| 1002 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1003 | } |
| 1004 | #ifndef SQLITE_OMIT_UTF16 |
| 1005 | const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1006 | return columnName( |
| 1007 | pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1008 | } |
| 1009 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 1010 | #endif /* SQLITE_ENABLE_COLUMN_METADATA */ |
drh | e590fbd | 2005-05-20 19:36:01 +0000 | [diff] [blame] | 1011 | |
| 1012 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1013 | /******************************* sqlite3_bind_ *************************** |
| 1014 | ** |
| 1015 | ** Routines used to attach values to wildcards in a compiled SQL statement. |
| 1016 | */ |
| 1017 | /* |
| 1018 | ** Unbind the value bound to variable i in virtual machine p. This is the |
| 1019 | ** the same as binding a NULL value to the column. If the "i" parameter is |
| 1020 | ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
| 1021 | ** |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1022 | ** A successful evaluation of this routine acquires the mutex on p. |
| 1023 | ** the mutex is released if any kind of error occurs. |
| 1024 | ** |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1025 | ** The error code stored in database p->db is overwritten with the return |
| 1026 | ** value in any case. |
| 1027 | */ |
| 1028 | static int vdbeUnbind(Vdbe *p, int i){ |
| 1029 | Mem *pVar; |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1030 | if( p==0 ) return SQLITE_MISUSE; |
| 1031 | sqlite3_mutex_enter(p->db->mutex); |
| 1032 | if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ |
| 1033 | sqlite3Error(p->db, SQLITE_MISUSE, 0); |
| 1034 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1035 | return SQLITE_MISUSE; |
| 1036 | } |
| 1037 | if( i<1 || i>p->nVar ){ |
| 1038 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1039 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1040 | return SQLITE_RANGE; |
| 1041 | } |
| 1042 | i--; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1043 | pVar = &p->aVar[i]; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1044 | sqlite3VdbeMemRelease(pVar); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1045 | pVar->flags = MEM_Null; |
| 1046 | sqlite3Error(p->db, SQLITE_OK, 0); |
| 1047 | return SQLITE_OK; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1051 | ** Bind a text or BLOB value. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1052 | */ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1053 | static int bindText( |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1054 | sqlite3_stmt *pStmt, /* The statement to bind against */ |
| 1055 | int i, /* Index of the parameter to bind */ |
| 1056 | const void *zData, /* Pointer to the data to be bound */ |
| 1057 | int nData, /* Number of bytes of data to be bound */ |
| 1058 | void (*xDel)(void*), /* Destructor for the data */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 1059 | u8 encoding /* Encoding for the data */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1060 | ){ |
| 1061 | Vdbe *p = (Vdbe *)pStmt; |
| 1062 | Mem *pVar; |
| 1063 | int rc; |
| 1064 | |
| 1065 | rc = vdbeUnbind(p, i); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1066 | if( rc==SQLITE_OK ){ |
| 1067 | if( zData!=0 ){ |
| 1068 | pVar = &p->aVar[i-1]; |
| 1069 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); |
| 1070 | if( rc==SQLITE_OK && encoding!=0 ){ |
| 1071 | rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); |
| 1072 | } |
| 1073 | sqlite3Error(p->db, rc, 0); |
| 1074 | rc = sqlite3ApiExit(p->db, rc); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1075 | } |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1076 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1077 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1078 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1079 | } |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1080 | |
| 1081 | |
| 1082 | /* |
| 1083 | ** Bind a blob value to an SQL statement variable. |
| 1084 | */ |
| 1085 | int sqlite3_bind_blob( |
| 1086 | sqlite3_stmt *pStmt, |
| 1087 | int i, |
| 1088 | const void *zData, |
| 1089 | int nData, |
| 1090 | void (*xDel)(void*) |
| 1091 | ){ |
| 1092 | return bindText(pStmt, i, zData, nData, xDel, 0); |
| 1093 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1094 | int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 1095 | int rc; |
| 1096 | Vdbe *p = (Vdbe *)pStmt; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1097 | rc = vdbeUnbind(p, i); |
| 1098 | if( rc==SQLITE_OK ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1099 | sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1100 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1101 | } |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 1102 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1103 | } |
| 1104 | int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 1105 | return sqlite3_bind_int64(p, i, (i64)iValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1106 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 1107 | int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1108 | int rc; |
| 1109 | Vdbe *p = (Vdbe *)pStmt; |
| 1110 | rc = vdbeUnbind(p, i); |
| 1111 | if( rc==SQLITE_OK ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1112 | sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1113 | sqlite3_mutex_leave(p->db->mutex); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1114 | } |
| 1115 | return rc; |
| 1116 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1117 | int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ |
| 1118 | int rc; |
| 1119 | Vdbe *p = (Vdbe*)pStmt; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1120 | rc = vdbeUnbind(p, i); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1121 | if( rc==SQLITE_OK ){ |
| 1122 | sqlite3_mutex_leave(p->db->mutex); |
| 1123 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1124 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1125 | } |
| 1126 | int sqlite3_bind_text( |
| 1127 | sqlite3_stmt *pStmt, |
| 1128 | int i, |
| 1129 | const char *zData, |
| 1130 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1131 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1132 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1133 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1134 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1135 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1136 | int sqlite3_bind_text16( |
| 1137 | sqlite3_stmt *pStmt, |
| 1138 | int i, |
| 1139 | const void *zData, |
| 1140 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1141 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1142 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 1143 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1144 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1145 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 1146 | int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ |
| 1147 | int rc; |
| 1148 | Vdbe *p = (Vdbe *)pStmt; |
| 1149 | rc = vdbeUnbind(p, i); |
| 1150 | if( rc==SQLITE_OK ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1151 | rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue); |
danielk1977 | 1e740c6 | 2008-05-16 15:24:58 +0000 | [diff] [blame] | 1152 | if( rc==SQLITE_OK ){ |
| 1153 | rc = sqlite3VdbeChangeEncoding(&p->aVar[i-1], ENC(p->db)); |
| 1154 | } |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1155 | sqlite3_mutex_leave(p->db->mutex); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 1156 | } |
danielk1977 | 0bfbaa8 | 2008-10-07 14:06:11 +0000 | [diff] [blame] | 1157 | rc = sqlite3ApiExit(p->db, rc); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 1158 | return rc; |
| 1159 | } |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 1160 | int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ |
| 1161 | int rc; |
| 1162 | Vdbe *p = (Vdbe *)pStmt; |
| 1163 | rc = vdbeUnbind(p, i); |
| 1164 | if( rc==SQLITE_OK ){ |
| 1165 | sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
drh | 488e7b6 | 2008-10-06 12:46:43 +0000 | [diff] [blame] | 1166 | sqlite3_mutex_leave(p->db->mutex); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 1167 | } |
| 1168 | return rc; |
| 1169 | } |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 1170 | |
| 1171 | /* |
| 1172 | ** Return the number of wildcards that can be potentially bound to. |
| 1173 | ** This routine is added to support DBD::SQLite. |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 1174 | */ |
| 1175 | int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1176 | Vdbe *p = (Vdbe*)pStmt; |
| 1177 | return p ? p->nVar : 0; |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 1178 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1179 | |
| 1180 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1181 | ** Create a mapping from variable numbers to variable names |
| 1182 | ** in the Vdbe.azVar[] array, if such a mapping does not already |
| 1183 | ** exist. |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1184 | */ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1185 | static void createVarMap(Vdbe *p){ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1186 | if( !p->okVar ){ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1187 | sqlite3_mutex_enter(p->db->mutex); |
| 1188 | if( !p->okVar ){ |
| 1189 | int j; |
| 1190 | Op *pOp; |
| 1191 | for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ |
| 1192 | if( pOp->opcode==OP_Variable ){ |
| 1193 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1194 | p->azVar[pOp->p1-1] = pOp->p4.z; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1195 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1196 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1197 | p->okVar = 1; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1198 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1199 | sqlite3_mutex_leave(p->db->mutex); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1200 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | ** Return the name of a wildcard parameter. Return NULL if the index |
| 1205 | ** is out of range or if the wildcard is unnamed. |
| 1206 | ** |
| 1207 | ** The result is always UTF-8. |
| 1208 | */ |
| 1209 | const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ |
| 1210 | Vdbe *p = (Vdbe*)pStmt; |
| 1211 | if( p==0 || i<1 || i>p->nVar ){ |
| 1212 | return 0; |
| 1213 | } |
| 1214 | createVarMap(p); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1215 | return p->azVar[i-1]; |
| 1216 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1217 | |
| 1218 | /* |
| 1219 | ** Given a wildcard parameter name, return the index of the variable |
| 1220 | ** with that name. If there is no variable with the given name, |
| 1221 | ** return 0. |
| 1222 | */ |
| 1223 | int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ |
| 1224 | Vdbe *p = (Vdbe*)pStmt; |
| 1225 | int i; |
| 1226 | if( p==0 ){ |
| 1227 | return 0; |
| 1228 | } |
| 1229 | createVarMap(p); |
drh | 6a8903c | 2004-12-10 18:00:04 +0000 | [diff] [blame] | 1230 | if( zName ){ |
| 1231 | for(i=0; i<p->nVar; i++){ |
| 1232 | const char *z = p->azVar[i]; |
| 1233 | if( z && strcmp(z,zName)==0 ){ |
| 1234 | return i+1; |
| 1235 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | return 0; |
| 1239 | } |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1240 | |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 1241 | /* |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1242 | ** Transfer all bindings from the first statement over to the second. |
| 1243 | ** If the two statements contain a different number of bindings, then |
| 1244 | ** an SQLITE_ERROR is returned. |
| 1245 | */ |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 1246 | int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1247 | Vdbe *pFrom = (Vdbe*)pFromStmt; |
| 1248 | Vdbe *pTo = (Vdbe*)pToStmt; |
| 1249 | int i, rc = SQLITE_OK; |
| 1250 | if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1251 | || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) |
| 1252 | || pTo->db!=pFrom->db ){ |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1253 | return SQLITE_MISUSE; |
| 1254 | } |
| 1255 | if( pFrom->nVar!=pTo->nVar ){ |
| 1256 | return SQLITE_ERROR; |
| 1257 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1258 | sqlite3_mutex_enter(pTo->db->mutex); |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1259 | for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 1260 | sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1261 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1262 | sqlite3_mutex_leave(pTo->db->mutex); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1263 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1264 | return rc; |
| 1265 | } |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 1266 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1267 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 1268 | /* |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 1269 | ** Deprecated external interface. Internal/core SQLite code |
| 1270 | ** should call sqlite3TransferBindings. |
| 1271 | */ |
| 1272 | int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
| 1273 | return sqlite3TransferBindings(pFromStmt, pToStmt); |
| 1274 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1275 | #endif |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 1276 | |
| 1277 | /* |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 1278 | ** Return the sqlite3* database handle to which the prepared statement given |
| 1279 | ** in the argument belongs. This is the same database handle that was |
| 1280 | ** the first argument to the sqlite3_prepare() that was used to create |
| 1281 | ** the statement in the first place. |
| 1282 | */ |
| 1283 | sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ |
| 1284 | return pStmt ? ((Vdbe*)pStmt)->db : 0; |
| 1285 | } |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 1286 | |
| 1287 | /* |
| 1288 | ** Return a pointer to the next prepared statement after pStmt associated |
| 1289 | ** with database connection pDb. If pStmt is NULL, return the first |
| 1290 | ** prepared statement for the database connection. Return NULL if there |
| 1291 | ** are no more. |
| 1292 | */ |
| 1293 | sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 1294 | sqlite3_stmt *pNext; |
| 1295 | sqlite3_mutex_enter(pDb->mutex); |
| 1296 | if( pStmt==0 ){ |
| 1297 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 1298 | }else{ |
| 1299 | pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| 1300 | } |
| 1301 | sqlite3_mutex_leave(pDb->mutex); |
| 1302 | return pNext; |
| 1303 | } |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 1304 | |
| 1305 | /* |
| 1306 | ** Return the value of a status counter for a prepared statement |
| 1307 | */ |
| 1308 | int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 1309 | Vdbe *pVdbe = (Vdbe*)pStmt; |
| 1310 | int v = pVdbe->aCounter[op-1]; |
| 1311 | if( resetFlag ) pVdbe->aCounter[op-1] = 0; |
| 1312 | return v; |
| 1313 | } |