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