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