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