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