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 | |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 353 | /* |
| 354 | ** Convert the N-th element of pStmt->pColName[] into a string using |
| 355 | ** xFunc() then return that string. If N is out of range, return 0. |
| 356 | ** If useType is 1, then use the second set of N elements (the datatype |
| 357 | ** names) instead of the first set. |
| 358 | */ |
| 359 | static const void *columnName( |
| 360 | sqlite3_stmt *pStmt, |
| 361 | int N, |
| 362 | const void *(*xFunc)(Mem*), |
| 363 | int useType |
| 364 | ){ |
| 365 | Vdbe *p = (Vdbe *)pStmt; |
| 366 | int n = sqlite3_column_count(pStmt); |
| 367 | |
| 368 | if( p==0 || N>=n || N<0 ){ |
| 369 | return 0; |
| 370 | } |
| 371 | if( useType ){ |
| 372 | N += n; |
| 373 | } |
| 374 | return xFunc(&p->aColName[N]); |
| 375 | } |
| 376 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | ** Return the name of the Nth column of the result set returned by SQL |
| 380 | ** statement pStmt. |
| 381 | */ |
| 382 | const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 383 | return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* |
| 387 | ** Return the name of the 'i'th column of the result set of SQL statement |
| 388 | ** pStmt, encoded as UTF-16. |
| 389 | */ |
| 390 | const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 391 | return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 392 | } |
| 393 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 394 | /* |
| 395 | ** Return the column declaration type (if applicable) of the 'i'th column |
| 396 | ** of the result set of SQL statement pStmt, encoded as UTF-8. |
| 397 | */ |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 398 | const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 399 | return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1); |
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){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 407 | return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /******************************* sqlite3_bind_ *************************** |
| 411 | ** |
| 412 | ** Routines used to attach values to wildcards in a compiled SQL statement. |
| 413 | */ |
| 414 | /* |
| 415 | ** Unbind the value bound to variable i in virtual machine p. This is the |
| 416 | ** the same as binding a NULL value to the column. If the "i" parameter is |
| 417 | ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
| 418 | ** |
| 419 | ** The error code stored in database p->db is overwritten with the return |
| 420 | ** value in any case. |
| 421 | */ |
| 422 | static int vdbeUnbind(Vdbe *p, int i){ |
| 423 | Mem *pVar; |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 424 | if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 425 | sqlite3Error(p->db, SQLITE_MISUSE, 0); |
| 426 | return SQLITE_MISUSE; |
| 427 | } |
| 428 | if( i<1 || i>p->nVar ){ |
| 429 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 430 | return SQLITE_RANGE; |
| 431 | } |
| 432 | i--; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 433 | pVar = &p->aVar[i]; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 434 | sqlite3VdbeMemRelease(pVar); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 435 | pVar->flags = MEM_Null; |
| 436 | sqlite3Error(p->db, SQLITE_OK, 0); |
| 437 | return SQLITE_OK; |
| 438 | } |
| 439 | |
| 440 | /* |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 441 | ** Bind a text or BLOB value. |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 442 | */ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 443 | static int bindText( |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 444 | sqlite3_stmt *pStmt, |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 445 | int i, |
| 446 | const void *zData, |
| 447 | int nData, |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 448 | void (*xDel)(void*), |
| 449 | int encoding |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 450 | ){ |
| 451 | Vdbe *p = (Vdbe *)pStmt; |
| 452 | Mem *pVar; |
| 453 | int rc; |
| 454 | |
| 455 | rc = vdbeUnbind(p, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 456 | if( rc || zData==0 ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 457 | return rc; |
| 458 | } |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 459 | pVar = &p->aVar[i-1]; |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 460 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); |
| 461 | if( rc ){ |
| 462 | return rc; |
| 463 | } |
| 464 | if( rc==SQLITE_OK && encoding!=0 ){ |
| 465 | rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc); |
| 466 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 467 | return rc; |
| 468 | } |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 469 | |
| 470 | |
| 471 | /* |
| 472 | ** Bind a blob value to an SQL statement variable. |
| 473 | */ |
| 474 | int sqlite3_bind_blob( |
| 475 | sqlite3_stmt *pStmt, |
| 476 | int i, |
| 477 | const void *zData, |
| 478 | int nData, |
| 479 | void (*xDel)(void*) |
| 480 | ){ |
| 481 | return bindText(pStmt, i, zData, nData, xDel, 0); |
| 482 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 483 | int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 484 | int rc; |
| 485 | Vdbe *p = (Vdbe *)pStmt; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 486 | rc = vdbeUnbind(p, i); |
| 487 | if( rc==SQLITE_OK ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 488 | sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 489 | } |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 490 | return rc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 491 | } |
| 492 | int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 493 | return sqlite3_bind_int64(p, i, (i64)iValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 494 | } |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 495 | int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 496 | int rc; |
| 497 | Vdbe *p = (Vdbe *)pStmt; |
| 498 | rc = vdbeUnbind(p, i); |
| 499 | if( rc==SQLITE_OK ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 500 | sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 501 | } |
| 502 | return rc; |
| 503 | } |
| 504 | int sqlite3_bind_null(sqlite3_stmt* p, int i){ |
| 505 | return vdbeUnbind((Vdbe *)p, i); |
| 506 | } |
| 507 | int sqlite3_bind_text( |
| 508 | sqlite3_stmt *pStmt, |
| 509 | int i, |
| 510 | const char *zData, |
| 511 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 512 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 513 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 514 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 515 | } |
| 516 | int sqlite3_bind_text16( |
| 517 | sqlite3_stmt *pStmt, |
| 518 | int i, |
| 519 | const void *zData, |
| 520 | int nData, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 521 | void (*xDel)(void*) |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 522 | ){ |
drh | 5e2517e | 2004-09-24 23:59:12 +0000 | [diff] [blame^] | 523 | return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 524 | } |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | ** Return the number of wildcards that can be potentially bound to. |
| 528 | ** This routine is added to support DBD::SQLite. |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 529 | */ |
| 530 | int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 531 | Vdbe *p = (Vdbe*)pStmt; |
| 532 | return p ? p->nVar : 0; |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 533 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 534 | |
| 535 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 536 | ** Create a mapping from variable numbers to variable names |
| 537 | ** in the Vdbe.azVar[] array, if such a mapping does not already |
| 538 | ** exist. |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 539 | */ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 540 | static void createVarMap(Vdbe *p){ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 541 | if( !p->okVar ){ |
| 542 | int j; |
| 543 | Op *pOp; |
| 544 | for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ |
| 545 | if( pOp->opcode==OP_Variable ){ |
| 546 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
| 547 | p->azVar[pOp->p1-1] = pOp->p3; |
| 548 | } |
| 549 | } |
| 550 | p->okVar = 1; |
| 551 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* |
| 555 | ** Return the name of a wildcard parameter. Return NULL if the index |
| 556 | ** is out of range or if the wildcard is unnamed. |
| 557 | ** |
| 558 | ** The result is always UTF-8. |
| 559 | */ |
| 560 | const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ |
| 561 | Vdbe *p = (Vdbe*)pStmt; |
| 562 | if( p==0 || i<1 || i>p->nVar ){ |
| 563 | return 0; |
| 564 | } |
| 565 | createVarMap(p); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 566 | return p->azVar[i-1]; |
| 567 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 568 | |
| 569 | /* |
| 570 | ** Given a wildcard parameter name, return the index of the variable |
| 571 | ** with that name. If there is no variable with the given name, |
| 572 | ** return 0. |
| 573 | */ |
| 574 | int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ |
| 575 | Vdbe *p = (Vdbe*)pStmt; |
| 576 | int i; |
| 577 | if( p==0 ){ |
| 578 | return 0; |
| 579 | } |
| 580 | createVarMap(p); |
| 581 | for(i=0; i<p->nVar; i++){ |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 582 | const char *z = p->azVar[i]; |
| 583 | if( z && strcmp(z,zName)==0 ){ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 584 | return i+1; |
| 585 | } |
| 586 | } |
| 587 | return 0; |
| 588 | } |