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