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