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 | |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 19 | /* |
| 20 | ** Return TRUE (non-zero) of the statement supplied as an argument needs |
| 21 | ** to be recompiled. A statement needs to be recompiled whenever the |
| 22 | ** execution environment changes in a way that would alter the program |
| 23 | ** that sqlite3_prepare() generates. For example, if new functions or |
| 24 | ** collating sequences are registered or if an authorizer function is |
| 25 | ** added or changed. |
| 26 | ** |
| 27 | ***** EXPERIMENTAL ****** |
| 28 | */ |
| 29 | int sqlite3_expired(sqlite3_stmt *pStmt){ |
| 30 | Vdbe *p = (Vdbe*)pStmt; |
| 31 | return p==0 || p->expired; |
| 32 | } |
| 33 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 34 | /**************************** sqlite3_value_ ******************************* |
| 35 | ** The following routines extract information from a Mem or sqlite3_value |
| 36 | ** structure. |
| 37 | */ |
| 38 | const void *sqlite3_value_blob(sqlite3_value *pVal){ |
| 39 | Mem *p = (Mem*)pVal; |
| 40 | if( p->flags & (MEM_Blob|MEM_Str) ){ |
| 41 | return p->z; |
| 42 | }else{ |
| 43 | return sqlite3_value_text(pVal); |
| 44 | } |
| 45 | } |
| 46 | int sqlite3_value_bytes(sqlite3_value *pVal){ |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 47 | return sqlite3ValueBytes(pVal, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 48 | } |
| 49 | int sqlite3_value_bytes16(sqlite3_value *pVal){ |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 50 | return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 51 | } |
| 52 | double sqlite3_value_double(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 53 | return sqlite3VdbeRealValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 54 | } |
| 55 | int sqlite3_value_int(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 56 | return sqlite3VdbeIntValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 57 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 58 | sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 59 | return sqlite3VdbeIntValue((Mem*)pVal); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 60 | } |
| 61 | const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 62 | return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 63 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 64 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 65 | const void *sqlite3_value_text16(sqlite3_value* pVal){ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 66 | return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 67 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 68 | const void *sqlite3_value_text16be(sqlite3_value *pVal){ |
| 69 | return sqlite3ValueText(pVal, SQLITE_UTF16BE); |
| 70 | } |
| 71 | const void *sqlite3_value_text16le(sqlite3_value *pVal){ |
| 72 | return sqlite3ValueText(pVal, SQLITE_UTF16LE); |
| 73 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 74 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 75 | int sqlite3_value_type(sqlite3_value* pVal){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 76 | return pVal->type; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /**************************** sqlite3_result_ ******************************* |
| 80 | ** The following routines are used by user-defined functions to specify |
| 81 | ** the function result. |
| 82 | */ |
| 83 | void sqlite3_result_blob( |
| 84 | sqlite3_context *pCtx, |
| 85 | const void *z, |
| 86 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 87 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 88 | ){ |
| 89 | assert( n>0 ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 90 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 91 | } |
| 92 | void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ |
| 93 | sqlite3VdbeMemSetDouble(&pCtx->s, rVal); |
| 94 | } |
| 95 | void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ |
| 96 | pCtx->isError = 1; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 97 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 98 | } |
| 99 | void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ |
| 100 | pCtx->isError = 1; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 101 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 102 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 103 | void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 104 | sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); |
| 105 | } |
| 106 | void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ |
| 107 | sqlite3VdbeMemSetInt64(&pCtx->s, iVal); |
| 108 | } |
| 109 | void sqlite3_result_null(sqlite3_context *pCtx){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 110 | sqlite3VdbeMemSetNull(&pCtx->s); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 111 | } |
| 112 | void sqlite3_result_text( |
| 113 | sqlite3_context *pCtx, |
| 114 | const char *z, |
| 115 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 116 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 117 | ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 118 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 119 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 120 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 121 | void sqlite3_result_text16( |
| 122 | sqlite3_context *pCtx, |
| 123 | const void *z, |
| 124 | int n, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 125 | void (*xDel)(void *) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 126 | ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 127 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); |
| 128 | } |
| 129 | void sqlite3_result_text16be( |
| 130 | sqlite3_context *pCtx, |
| 131 | const void *z, |
| 132 | int n, |
| 133 | void (*xDel)(void *) |
| 134 | ){ |
| 135 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); |
| 136 | } |
| 137 | void sqlite3_result_text16le( |
| 138 | sqlite3_context *pCtx, |
| 139 | const void *z, |
| 140 | int n, |
| 141 | void (*xDel)(void *) |
| 142 | ){ |
| 143 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 144 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 145 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 146 | void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ |
| 147 | sqlite3VdbeMemCopy(&pCtx->s, pValue); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | ** Execute the statement pStmt, either until a row of data is ready, the |
| 153 | ** statement is completely executed or an error occurs. |
| 154 | */ |
| 155 | int sqlite3_step(sqlite3_stmt *pStmt){ |
| 156 | Vdbe *p = (Vdbe*)pStmt; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 157 | sqlite3 *db; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 158 | int rc; |
| 159 | |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 160 | if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 161 | return SQLITE_MISUSE; |
| 162 | } |
drh | 91b48aa | 2004-06-30 11:14:18 +0000 | [diff] [blame] | 163 | if( p->aborted ){ |
| 164 | return SQLITE_ABORT; |
| 165 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame^] | 166 | if( p->pc<=0 && p->expired ){ |
| 167 | if( p->rc==SQLITE_OK ){ |
| 168 | p->rc = SQLITE_SCHEMA; |
| 169 | } |
| 170 | return SQLITE_ERROR; |
| 171 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 172 | db = p->db; |
| 173 | if( sqlite3SafetyOn(db) ){ |
| 174 | p->rc = SQLITE_MISUSE; |
| 175 | return SQLITE_MISUSE; |
| 176 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 177 | if( p->pc<0 ){ |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 178 | /* Invoke the trace callback if there is one |
| 179 | */ |
| 180 | if( (db = p->db)->xTrace && !db->init.busy ){ |
| 181 | assert( p->nOp>0 ); |
| 182 | assert( p->aOp[p->nOp-1].opcode==OP_Noop ); |
| 183 | assert( p->aOp[p->nOp-1].p3!=0 ); |
| 184 | assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); |
| 185 | sqlite3SafetyOff(db); |
| 186 | db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); |
| 187 | if( sqlite3SafetyOn(db) ){ |
| 188 | p->rc = SQLITE_MISUSE; |
| 189 | return SQLITE_MISUSE; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned |
| 194 | ** on in debugging mode. |
| 195 | */ |
| 196 | #ifdef SQLITE_DEBUG |
| 197 | if( (db->flags & SQLITE_SqlTrace)!=0 ){ |
| 198 | sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); |
| 199 | } |
| 200 | #endif /* SQLITE_DEBUG */ |
| 201 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 202 | db->activeVdbeCnt++; |
| 203 | p->pc = 0; |
| 204 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 205 | #ifndef SQLITE_OMIT_EXPLAIN |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 206 | if( p->explain ){ |
| 207 | rc = sqlite3VdbeList(p); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 208 | }else |
| 209 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 210 | { |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 211 | rc = sqlite3VdbeExec(p); |
| 212 | } |
| 213 | |
| 214 | if( sqlite3SafetyOff(db) ){ |
| 215 | rc = SQLITE_MISUSE; |
| 216 | } |
| 217 | |
| 218 | sqlite3Error(p->db, rc, p->zErrMsg); |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 223 | ** Extract the user data from a sqlite3_context structure and return a |
| 224 | ** pointer to it. |
| 225 | */ |
| 226 | void *sqlite3_user_data(sqlite3_context *p){ |
| 227 | assert( p && p->pFunc ); |
| 228 | return p->pFunc->pUserData; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | ** Allocate or return the aggregate context for a user function. A new |
| 233 | ** context is allocated on the first call. Subsequent calls return the |
| 234 | ** same context that was returned on prior calls. |
| 235 | ** |
| 236 | ** This routine is defined here in vdbe.c because it depends on knowing |
| 237 | ** the internals of the sqlite3_context structure which is only defined in |
| 238 | ** this source file. |
| 239 | */ |
| 240 | void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ |
| 241 | assert( p && p->pFunc && p->pFunc->xStep ); |
| 242 | if( p->pAgg==0 ){ |
| 243 | if( nByte<=NBFS ){ |
| 244 | p->pAgg = (void*)p->s.z; |
| 245 | memset(p->pAgg, 0, nByte); |
| 246 | }else{ |
| 247 | p->pAgg = sqliteMalloc( nByte ); |
| 248 | } |
| 249 | } |
| 250 | return p->pAgg; |
| 251 | } |
| 252 | |
| 253 | /* |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 254 | ** Return the auxilary data pointer, if any, for the iArg'th argument to |
| 255 | ** the user-function defined by pCtx. |
| 256 | */ |
| 257 | void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ |
| 258 | VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc; |
| 259 | if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ |
| 260 | return 0; |
| 261 | } |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 262 | return pVdbeFunc->apAux[iArg].pAux; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
| 266 | ** Set the auxilary data pointer and delete function, for the iArg'th |
| 267 | ** argument to the user-function defined by pCtx. Any previous value is |
| 268 | ** deleted by calling the delete function specified when it was set. |
| 269 | */ |
| 270 | void sqlite3_set_auxdata( |
| 271 | sqlite3_context *pCtx, |
| 272 | int iArg, |
| 273 | void *pAux, |
| 274 | void (*xDelete)(void*) |
| 275 | ){ |
| 276 | struct AuxData *pAuxData; |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 277 | VdbeFunc *pVdbeFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 278 | if( iArg<0 ) return; |
| 279 | |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 280 | pVdbeFunc = pCtx->pVdbeFunc; |
| 281 | if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ |
| 282 | int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; |
| 283 | pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc); |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 284 | if( !pVdbeFunc ) return; |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 285 | memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, |
| 286 | sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux)); |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 287 | pVdbeFunc->nAux = iArg+1; |
| 288 | pVdbeFunc->pFunc = pCtx->pFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 289 | } |
| 290 | |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 291 | pAuxData = &pVdbeFunc->apAux[iArg]; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 292 | if( pAuxData->pAux && pAuxData->xDelete ){ |
| 293 | pAuxData->xDelete(pAuxData->pAux); |
| 294 | } |
| 295 | pAuxData->pAux = pAux; |
| 296 | pAuxData->xDelete = xDelete; |
| 297 | } |
| 298 | |
| 299 | /* |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 300 | ** Return the number of times the Step function of a aggregate has been |
| 301 | ** called. |
| 302 | ** |
| 303 | ** This routine is defined here in vdbe.c because it depends on knowing |
| 304 | ** the internals of the sqlite3_context structure which is only defined in |
| 305 | ** this source file. |
| 306 | */ |
| 307 | int sqlite3_aggregate_count(sqlite3_context *p){ |
| 308 | assert( p && p->pFunc && p->pFunc->xStep ); |
| 309 | return p->cnt; |
| 310 | } |
| 311 | |
| 312 | /* |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 313 | ** Return the number of columns in the result set for the statement pStmt. |
| 314 | */ |
| 315 | int sqlite3_column_count(sqlite3_stmt *pStmt){ |
| 316 | Vdbe *pVm = (Vdbe *)pStmt; |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 317 | return pVm ? pVm->nResColumn : 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* |
| 321 | ** Return the number of values available from the current row of the |
| 322 | ** currently executing statement pStmt. |
| 323 | */ |
| 324 | int sqlite3_data_count(sqlite3_stmt *pStmt){ |
| 325 | Vdbe *pVm = (Vdbe *)pStmt; |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 326 | if( pVm==0 || !pVm->resOnStack ) return 0; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 327 | return pVm->nResColumn; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /* |
| 332 | ** Check to see if column iCol of the given statement is valid. If |
| 333 | ** it is, return a pointer to the Mem for the value of that column. |
| 334 | ** If iCol is not valid, return a pointer to a Mem which has a value |
| 335 | ** of NULL. |
| 336 | */ |
| 337 | static Mem *columnMem(sqlite3_stmt *pStmt, int i){ |
| 338 | Vdbe *pVm = (Vdbe *)pStmt; |
| 339 | int vals = sqlite3_data_count(pStmt); |
| 340 | if( i>=vals || i<0 ){ |
| 341 | static Mem nullMem; |
| 342 | if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; } |
| 343 | sqlite3Error(pVm->db, SQLITE_RANGE, 0); |
| 344 | return &nullMem; |
| 345 | } |
| 346 | return &pVm->pTos[(1-vals)+i]; |
| 347 | } |
| 348 | |
| 349 | /**************************** sqlite3_column_ ******************************* |
| 350 | ** The following routines are used to access elements of the current row |
| 351 | ** in the result set. |
| 352 | */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 353 | const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ |
| 354 | return sqlite3_value_blob( columnMem(pStmt,i) ); |
| 355 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 356 | int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ |
| 357 | return sqlite3_value_bytes( columnMem(pStmt,i) ); |
| 358 | } |
| 359 | int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ |
| 360 | return sqlite3_value_bytes16( columnMem(pStmt,i) ); |
| 361 | } |
| 362 | double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ |
| 363 | return sqlite3_value_double( columnMem(pStmt,i) ); |
| 364 | } |
| 365 | int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ |
| 366 | return sqlite3_value_int( columnMem(pStmt,i) ); |
| 367 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 368 | sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 369 | return sqlite3_value_int64( columnMem(pStmt,i) ); |
| 370 | } |
| 371 | const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ |
| 372 | return sqlite3_value_text( columnMem(pStmt,i) ); |
| 373 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 374 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 375 | const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ |
| 376 | return sqlite3_value_text16( columnMem(pStmt,i) ); |
| 377 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 378 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 379 | int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ |
| 380 | return sqlite3_value_type( columnMem(pStmt,i) ); |
| 381 | } |
| 382 | |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 383 | /* |
| 384 | ** Convert the N-th element of pStmt->pColName[] into a string using |
| 385 | ** xFunc() then return that string. If N is out of range, return 0. |
| 386 | ** If useType is 1, then use the second set of N elements (the datatype |
| 387 | ** names) instead of the first set. |
| 388 | */ |
| 389 | static const void *columnName( |
| 390 | sqlite3_stmt *pStmt, |
| 391 | int N, |
| 392 | const void *(*xFunc)(Mem*), |
| 393 | int useType |
| 394 | ){ |
| 395 | Vdbe *p = (Vdbe *)pStmt; |
| 396 | int n = sqlite3_column_count(pStmt); |
| 397 | |
| 398 | if( p==0 || N>=n || N<0 ){ |
| 399 | return 0; |
| 400 | } |
| 401 | if( useType ){ |
| 402 | N += n; |
| 403 | } |
| 404 | return xFunc(&p->aColName[N]); |
| 405 | } |
| 406 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | ** Return the name of the Nth column of the result set returned by SQL |
| 410 | ** statement pStmt. |
| 411 | */ |
| 412 | const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 413 | return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 417 | ** Return the column declaration type (if applicable) of the 'i'th column |
| 418 | ** of the result set of SQL statement pStmt, encoded as UTF-8. |
| 419 | */ |
| 420 | const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ |
| 421 | return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1); |
| 422 | } |
| 423 | |
| 424 | #ifndef SQLITE_OMIT_UTF16 |
| 425 | /* |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 426 | ** Return the name of the 'i'th column of the result set of SQL statement |
| 427 | ** pStmt, encoded as UTF-16. |
| 428 | */ |
| 429 | const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 430 | return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 431 | } |
| 432 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 433 | /* |
| 434 | ** Return the column declaration type (if applicable) of the 'i'th column |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 435 | ** of the result set of SQL statement pStmt, encoded as UTF-16. |
| 436 | */ |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 437 | const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 438 | return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 439 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 440 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 441 | |
| 442 | /******************************* sqlite3_bind_ *************************** |
| 443 | ** |
| 444 | ** Routines used to attach values to wildcards in a compiled SQL statement. |
| 445 | */ |
| 446 | /* |
| 447 | ** Unbind the value bound to variable i in virtual machine p. This is the |
| 448 | ** the same as binding a NULL value to the column. If the "i" parameter is |
| 449 | ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
| 450 | ** |
| 451 | ** The error code stored in database p->db is overwritten with the return |
| 452 | ** value in any case. |
| 453 | */ |
| 454 | static int vdbeUnbind(Vdbe *p, int i){ |
| 455 | Mem *pVar; |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 456 | if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 457 | sqlite3Error(p->db, SQLITE_MISUSE, 0); |
| 458 | return SQLITE_MISUSE; |
| 459 | } |
| 460 | if( i<1 || i>p->nVar ){ |
| 461 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 462 | return SQLITE_RANGE; |
| 463 | } |
| 464 | i--; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 465 | pVar = &p->aVar[i]; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 466 | sqlite3VdbeMemRelease(pVar); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 467 | pVar->flags = MEM_Null; |
| 468 | sqlite3Error(p->db, SQLITE_OK, 0); |
| 469 | return SQLITE_OK; |
| 470 | } |
| 471 | |
| 472 | /* |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 473 | ** Bind a text or BLOB value. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 474 | */ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 475 | static int bindText( |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 476 | sqlite3_stmt *pStmt, |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 477 | int i, |
| 478 | const void *zData, |
| 479 | int nData, |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 480 | void (*xDel)(void*), |
| 481 | int encoding |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 482 | ){ |
| 483 | Vdbe *p = (Vdbe *)pStmt; |
| 484 | Mem *pVar; |
| 485 | int rc; |
| 486 | |
| 487 | rc = vdbeUnbind(p, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 488 | if( rc || zData==0 ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 489 | return rc; |
| 490 | } |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 491 | pVar = &p->aVar[i-1]; |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 492 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); |
| 493 | if( rc ){ |
| 494 | return rc; |
| 495 | } |
| 496 | if( rc==SQLITE_OK && encoding!=0 ){ |
| 497 | rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc); |
| 498 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 499 | return rc; |
| 500 | } |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 501 | |
| 502 | |
| 503 | /* |
| 504 | ** Bind a blob value to an SQL statement variable. |
| 505 | */ |
| 506 | int sqlite3_bind_blob( |
| 507 | sqlite3_stmt *pStmt, |
| 508 | int i, |
| 509 | const void *zData, |
| 510 | int nData, |
| 511 | void (*xDel)(void*) |
| 512 | ){ |
| 513 | return bindText(pStmt, i, zData, nData, xDel, 0); |
| 514 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 515 | int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 516 | int rc; |
| 517 | Vdbe *p = (Vdbe *)pStmt; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 518 | rc = vdbeUnbind(p, i); |
| 519 | if( rc==SQLITE_OK ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 520 | sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 521 | } |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 522 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 523 | } |
| 524 | int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 525 | return sqlite3_bind_int64(p, i, (i64)iValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 526 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 527 | int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 528 | int rc; |
| 529 | Vdbe *p = (Vdbe *)pStmt; |
| 530 | rc = vdbeUnbind(p, i); |
| 531 | if( rc==SQLITE_OK ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 532 | sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 533 | } |
| 534 | return rc; |
| 535 | } |
| 536 | int sqlite3_bind_null(sqlite3_stmt* p, int i){ |
| 537 | return vdbeUnbind((Vdbe *)p, i); |
| 538 | } |
| 539 | int sqlite3_bind_text( |
| 540 | sqlite3_stmt *pStmt, |
| 541 | int i, |
| 542 | const char *zData, |
| 543 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 544 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 545 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 546 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 547 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 548 | #ifndef SQLITE_OMIT_UTF16 |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 549 | int sqlite3_bind_text16( |
| 550 | sqlite3_stmt *pStmt, |
| 551 | int i, |
| 552 | const void *zData, |
| 553 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 554 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 555 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame] | 556 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 557 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 558 | #endif /* SQLITE_OMIT_UTF16 */ |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | ** Return the number of wildcards that can be potentially bound to. |
| 562 | ** This routine is added to support DBD::SQLite. |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 563 | */ |
| 564 | int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 565 | Vdbe *p = (Vdbe*)pStmt; |
| 566 | return p ? p->nVar : 0; |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 567 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 568 | |
| 569 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 570 | ** Create a mapping from variable numbers to variable names |
| 571 | ** in the Vdbe.azVar[] array, if such a mapping does not already |
| 572 | ** exist. |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 573 | */ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 574 | static void createVarMap(Vdbe *p){ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 575 | if( !p->okVar ){ |
| 576 | int j; |
| 577 | Op *pOp; |
| 578 | for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ |
| 579 | if( pOp->opcode==OP_Variable ){ |
| 580 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
| 581 | p->azVar[pOp->p1-1] = pOp->p3; |
| 582 | } |
| 583 | } |
| 584 | p->okVar = 1; |
| 585 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
| 589 | ** Return the name of a wildcard parameter. Return NULL if the index |
| 590 | ** is out of range or if the wildcard is unnamed. |
| 591 | ** |
| 592 | ** The result is always UTF-8. |
| 593 | */ |
| 594 | const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ |
| 595 | Vdbe *p = (Vdbe*)pStmt; |
| 596 | if( p==0 || i<1 || i>p->nVar ){ |
| 597 | return 0; |
| 598 | } |
| 599 | createVarMap(p); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 600 | return p->azVar[i-1]; |
| 601 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 602 | |
| 603 | /* |
| 604 | ** Given a wildcard parameter name, return the index of the variable |
| 605 | ** with that name. If there is no variable with the given name, |
| 606 | ** return 0. |
| 607 | */ |
| 608 | int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ |
| 609 | Vdbe *p = (Vdbe*)pStmt; |
| 610 | int i; |
| 611 | if( p==0 ){ |
| 612 | return 0; |
| 613 | } |
| 614 | createVarMap(p); |
drh | 6a8903c | 2004-12-10 18:00:04 +0000 | [diff] [blame] | 615 | if( zName ){ |
| 616 | for(i=0; i<p->nVar; i++){ |
| 617 | const char *z = p->azVar[i]; |
| 618 | if( z && strcmp(z,zName)==0 ){ |
| 619 | return i+1; |
| 620 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | return 0; |
| 624 | } |