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; |
| 47 | Realify(pMem, flagsToEnc(pMem->flags)); |
| 48 | return pMem->r; |
| 49 | } |
| 50 | int sqlite3_value_int(sqlite3_value *pVal){ |
| 51 | Mem *pMem = (Mem *)pVal; |
| 52 | Integerify(pMem, flagsToEnc(pMem->flags)); |
| 53 | return (int)pVal->i; |
| 54 | } |
| 55 | long long int sqlite3_value_int64(sqlite3_value *pVal){ |
| 56 | Mem *pMem = (Mem *)pVal; |
| 57 | Integerify(pMem, flagsToEnc(pMem->flags)); |
| 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 | */ |
| 70 | SetEncoding(pVal, MEM_Utf8|MEM_Term); |
| 71 | }else if( !(pVal->flags&MEM_Blob) ){ |
| 72 | /* Otherwise, unless this is a blob, convert it to a UTF-8 string */ |
| 73 | Stringify(pVal, TEXT_Utf8); |
| 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 | */ |
| 88 | SetEncoding(pVal, encToFlags(TEXT_Utf16)|MEM_Term); |
| 89 | }else if( !(pVal->flags&MEM_Blob) ){ |
| 90 | /* Otherwise, unless this is a blob, convert it to a UTF-16 string */ |
| 91 | Stringify(pVal, TEXT_Utf16); |
| 92 | } |
| 93 | |
| 94 | return (const void *)(pVal->z); |
| 95 | } |
| 96 | int sqlite3_value_type(sqlite3_value* pVal){ |
| 97 | int f = ((Mem *)pVal)->flags; |
| 98 | if( f&MEM_Null ){ |
| 99 | return SQLITE3_NULL; |
| 100 | } |
| 101 | if( f&MEM_Int ){ |
| 102 | return SQLITE3_INTEGER; |
| 103 | } |
| 104 | if( f&MEM_Real ){ |
| 105 | return SQLITE3_FLOAT; |
| 106 | } |
| 107 | if( f&MEM_Str ){ |
| 108 | return SQLITE3_TEXT; |
| 109 | } |
| 110 | if( f&MEM_Blob ){ |
| 111 | return SQLITE3_BLOB; |
| 112 | } |
| 113 | assert(0); |
| 114 | } |
| 115 | |
| 116 | /**************************** sqlite3_result_ ******************************* |
| 117 | ** The following routines are used by user-defined functions to specify |
| 118 | ** the function result. |
| 119 | */ |
| 120 | void sqlite3_result_blob( |
| 121 | sqlite3_context *pCtx, |
| 122 | const void *z, |
| 123 | int n, |
| 124 | int eCopy |
| 125 | ){ |
| 126 | assert( n>0 ); |
| 127 | MemSetStr(&pCtx->s, z, n, 0, eCopy); |
| 128 | } |
| 129 | void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ |
| 130 | sqlite3VdbeMemSetDouble(&pCtx->s, rVal); |
| 131 | } |
| 132 | void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ |
| 133 | pCtx->isError = 1; |
| 134 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf8, 1); |
| 135 | } |
| 136 | void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ |
| 137 | pCtx->isError = 1; |
| 138 | sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf16, 1); |
| 139 | } |
| 140 | void sqlite3_result_int32(sqlite3_context *pCtx, int iVal){ |
| 141 | sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); |
| 142 | } |
| 143 | void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ |
| 144 | sqlite3VdbeMemSetInt64(&pCtx->s, iVal); |
| 145 | } |
| 146 | void sqlite3_result_null(sqlite3_context *pCtx){ |
| 147 | sqilte3VdbeMemSetNull(&pCtx->s); |
| 148 | } |
| 149 | void sqlite3_result_text( |
| 150 | sqlite3_context *pCtx, |
| 151 | const char *z, |
| 152 | int n, |
| 153 | int eCopy |
| 154 | ){ |
| 155 | MemSetStr(&pCtx->s, z, n, TEXT_Utf8, eCopy); |
| 156 | } |
| 157 | void sqlite3_result_text16( |
| 158 | sqlite3_context *pCtx, |
| 159 | const void *z, |
| 160 | int n, |
| 161 | int eCopy |
| 162 | ){ |
| 163 | MemSetStr(&pCtx->s, z, n, TEXT_Utf16, eCopy); |
| 164 | } |
| 165 | void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ |
| 166 | sqlite3VdbeMemCopy(&pCtx->s, pValue); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /* |
| 171 | ** Execute the statement pStmt, either until a row of data is ready, the |
| 172 | ** statement is completely executed or an error occurs. |
| 173 | */ |
| 174 | int sqlite3_step(sqlite3_stmt *pStmt){ |
| 175 | Vdbe *p = (Vdbe*)pStmt; |
| 176 | sqlite *db; |
| 177 | int rc; |
| 178 | |
| 179 | if( p->magic!=VDBE_MAGIC_RUN ){ |
| 180 | return SQLITE_MISUSE; |
| 181 | } |
| 182 | db = p->db; |
| 183 | if( sqlite3SafetyOn(db) ){ |
| 184 | p->rc = SQLITE_MISUSE; |
| 185 | return SQLITE_MISUSE; |
| 186 | } |
| 187 | if( p->explain ){ |
| 188 | rc = sqlite3VdbeList(p); |
| 189 | }else{ |
| 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 | /* |
| 202 | ** Return the number of columns in the result set for the statement pStmt. |
| 203 | */ |
| 204 | int sqlite3_column_count(sqlite3_stmt *pStmt){ |
| 205 | Vdbe *pVm = (Vdbe *)pStmt; |
| 206 | return pVm->nResColumn; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | ** Return the number of values available from the current row of the |
| 211 | ** currently executing statement pStmt. |
| 212 | */ |
| 213 | int sqlite3_data_count(sqlite3_stmt *pStmt){ |
| 214 | Vdbe *pVm = (Vdbe *)pStmt; |
| 215 | if( !pVm->resOnStack ) return 0; |
| 216 | return pVm->nResColumn; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /* |
| 221 | ** Check to see if column iCol of the given statement is valid. If |
| 222 | ** it is, return a pointer to the Mem for the value of that column. |
| 223 | ** If iCol is not valid, return a pointer to a Mem which has a value |
| 224 | ** of NULL. |
| 225 | */ |
| 226 | static Mem *columnMem(sqlite3_stmt *pStmt, int i){ |
| 227 | Vdbe *pVm = (Vdbe *)pStmt; |
| 228 | int vals = sqlite3_data_count(pStmt); |
| 229 | if( i>=vals || i<0 ){ |
| 230 | static Mem nullMem; |
| 231 | if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; } |
| 232 | sqlite3Error(pVm->db, SQLITE_RANGE, 0); |
| 233 | return &nullMem; |
| 234 | } |
| 235 | return &pVm->pTos[(1-vals)+i]; |
| 236 | } |
| 237 | |
| 238 | /**************************** sqlite3_column_ ******************************* |
| 239 | ** The following routines are used to access elements of the current row |
| 240 | ** in the result set. |
| 241 | */ |
| 242 | int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ |
| 243 | return sqlite3_value_bytes( columnMem(pStmt,i) ); |
| 244 | } |
| 245 | int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ |
| 246 | return sqlite3_value_bytes16( columnMem(pStmt,i) ); |
| 247 | } |
| 248 | double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ |
| 249 | return sqlite3_value_double( columnMem(pStmt,i) ); |
| 250 | } |
| 251 | int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ |
| 252 | return sqlite3_value_int( columnMem(pStmt,i) ); |
| 253 | } |
| 254 | long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ |
| 255 | return sqlite3_value_int64( columnMem(pStmt,i) ); |
| 256 | } |
| 257 | const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ |
| 258 | return sqlite3_value_text( columnMem(pStmt,i) ); |
| 259 | } |
| 260 | const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ |
| 261 | return sqlite3_value_text16( columnMem(pStmt,i) ); |
| 262 | } |
| 263 | int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ |
| 264 | return sqlite3_value_type( columnMem(pStmt,i) ); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /* |
| 269 | ** Return the name of the Nth column of the result set returned by SQL |
| 270 | ** statement pStmt. |
| 271 | */ |
| 272 | const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
| 273 | Vdbe *p = (Vdbe *)pStmt; |
| 274 | Mem *pColName; |
| 275 | |
| 276 | if( N>=sqlite3_column_count(pStmt) || N<0 ){ |
| 277 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | pColName = &(p->aColName[N]); |
| 282 | return sqlite3_value_text(pColName); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | ** Return the name of the 'i'th column of the result set of SQL statement |
| 287 | ** pStmt, encoded as UTF-16. |
| 288 | */ |
| 289 | const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
| 290 | Vdbe *p = (Vdbe *)pStmt; |
| 291 | Mem *pColName; |
| 292 | |
| 293 | if( N>=sqlite3_column_count(pStmt) || N<0 ){ |
| 294 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | pColName = &(p->aColName[N]); |
| 299 | return sqlite3_value_text16(pColName); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /* |
| 304 | ** This routine returns either the column name, or declaration type (see |
| 305 | ** sqlite3_column_decltype16() ) of the 'i'th column of the result set of |
| 306 | ** SQL statement pStmt. The returned string is UTF-16 encoded. |
| 307 | ** |
| 308 | ** The declaration type is returned if 'decltype' is true, otherwise |
| 309 | ** the column name. |
| 310 | */ |
| 311 | static const void *columnName16(sqlite3_stmt *pStmt, int i, int decltype){ |
| 312 | Vdbe *p = (Vdbe *)pStmt; |
| 313 | |
| 314 | if( i>=sqlite3_column_count(pStmt) || i<0 ){ |
| 315 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | if( decltype ){ |
| 320 | i += p->nResColumn; |
| 321 | } |
| 322 | |
| 323 | if( !p->azColName16 ){ |
| 324 | p->azColName16 = (void **)sqliteMalloc(sizeof(void *)*p->nResColumn*2); |
| 325 | if( !p->azColName16 ){ |
| 326 | sqlite3Error(p->db, SQLITE_NOMEM, 0); |
| 327 | return 0; |
| 328 | } |
| 329 | } |
| 330 | if( !p->azColName16[i] ){ |
| 331 | if( SQLITE3_BIGENDIAN ){ |
| 332 | p->azColName16[i] = sqlite3utf8to16be(p->azColName[i], -1); |
| 333 | } |
| 334 | if( !p->azColName16[i] ){ |
| 335 | sqlite3Error(p->db, SQLITE_NOMEM, 0); |
| 336 | return 0; |
| 337 | } |
| 338 | } |
| 339 | return p->azColName16[i]; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | ** Return the column declaration type (if applicable) of the 'i'th column |
| 344 | ** of the result set of SQL statement pStmt, encoded as UTF-8. |
| 345 | */ |
| 346 | const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int i){ |
| 347 | Vdbe *p = (Vdbe *)pStmt; |
| 348 | |
| 349 | if( i>=sqlite3_column_count(pStmt) || i<0 ){ |
| 350 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | return p->azColName[i+p->nResColumn]; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | ** Return the column declaration type (if applicable) of the 'i'th column |
| 359 | ** of the result set of SQL statement pStmt, encoded as UTF-16. |
| 360 | */ |
| 361 | const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int i){ |
| 362 | return columnName16(pStmt, i, 1); |
| 363 | } |
| 364 | |
| 365 | /******************************* sqlite3_bind_ *************************** |
| 366 | ** |
| 367 | ** Routines used to attach values to wildcards in a compiled SQL statement. |
| 368 | */ |
| 369 | /* |
| 370 | ** Unbind the value bound to variable i in virtual machine p. This is the |
| 371 | ** the same as binding a NULL value to the column. If the "i" parameter is |
| 372 | ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
| 373 | ** |
| 374 | ** The error code stored in database p->db is overwritten with the return |
| 375 | ** value in any case. |
| 376 | */ |
| 377 | static int vdbeUnbind(Vdbe *p, int i){ |
| 378 | Mem *pVar; |
| 379 | if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){ |
| 380 | sqlite3Error(p->db, SQLITE_MISUSE, 0); |
| 381 | return SQLITE_MISUSE; |
| 382 | } |
| 383 | if( i<1 || i>p->nVar ){ |
| 384 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 385 | return SQLITE_RANGE; |
| 386 | } |
| 387 | i--; |
| 388 | pVar = &p->apVar[i]; |
| 389 | if( pVar->flags&MEM_Dyn ){ |
| 390 | sqliteFree(pVar->z); |
| 391 | } |
| 392 | pVar->flags = MEM_Null; |
| 393 | sqlite3Error(p->db, SQLITE_OK, 0); |
| 394 | return SQLITE_OK; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | ** Bind a blob value to an SQL statement variable. |
| 399 | */ |
| 400 | int sqlite3_bind_blob( |
| 401 | sqlite3_stmt *p, |
| 402 | int i, |
| 403 | const void *zData, |
| 404 | int nData, |
| 405 | int eCopy |
| 406 | ){ |
| 407 | Vdbe *p = (Vdbe *)pStmt; |
| 408 | Mem *pVar; |
| 409 | int rc; |
| 410 | |
| 411 | rc = vdbeUnbind(p, i); |
| 412 | if( rc ){ |
| 413 | return rc; |
| 414 | } |
| 415 | pVar = &p->apVar[i-1]; |
| 416 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy); |
| 417 | return rc; |
| 418 | } |
| 419 | int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 420 | int rc; |
| 421 | Vdbe *p = (Vdbe *)pStmt; |
| 422 | Mem *pVar; |
| 423 | rc = vdbeUnbind(p, i); |
| 424 | if( rc==SQLITE_OK ){ |
| 425 | sqlite3VdbeMemSetReal(&p->apVar[i-1], rValue); |
| 426 | } |
| 427 | return SQLITE_OK; |
| 428 | } |
| 429 | int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
| 430 | return sqlite3_bind_int64(p, i, (long long int)iValue); |
| 431 | } |
| 432 | int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){ |
| 433 | int rc; |
| 434 | Vdbe *p = (Vdbe *)pStmt; |
| 435 | rc = vdbeUnbind(p, i); |
| 436 | if( rc==SQLITE_OK ){ |
| 437 | sqlite3VdbeMemSetInt(&p->apVar[i-1], iValue); |
| 438 | } |
| 439 | return rc; |
| 440 | } |
| 441 | int sqlite3_bind_null(sqlite3_stmt* p, int i){ |
| 442 | return vdbeUnbind((Vdbe *)p, i); |
| 443 | } |
| 444 | int sqlite3_bind_text( |
| 445 | sqlite3_stmt *pStmt, |
| 446 | int i, |
| 447 | const char *zData, |
| 448 | int nData, |
| 449 | int eCopy |
| 450 | ){ |
| 451 | Vdbe *p = (Vdbe *)pStmt; |
| 452 | Mem *pVar; |
| 453 | int rc; |
| 454 | |
| 455 | rc = vdbeUnbind(p, i); |
| 456 | if( rc ){ |
| 457 | return rc; |
| 458 | } |
| 459 | pVar = &p->apVar[i-1]; |
| 460 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, TEXT_Utf8, eCopy); |
| 461 | if( rc ){ |
| 462 | return rc; |
| 463 | } |
| 464 | rc = sqlite3VdbeSetEncoding(pVar, p->db->enc); |
| 465 | return rc; |
| 466 | } |
| 467 | int sqlite3_bind_text16( |
| 468 | sqlite3_stmt *pStmt, |
| 469 | int i, |
| 470 | const void *zData, |
| 471 | int nData, |
| 472 | int eCopy |
| 473 | ){ |
| 474 | Vdbe *p = (Vdbe *)pStmt; |
| 475 | Mem *pVar; |
| 476 | int rc; |
| 477 | |
| 478 | rc = vdbeUnbind(p, i); |
| 479 | if( rc ){ |
| 480 | return rc; |
| 481 | } |
| 482 | Mem *pVar = &p->apVar[i-1]; |
| 483 | |
| 484 | /* There may or may not be a byte order mark at the start of the UTF-16. |
| 485 | ** Either way set 'txt_enc' to the TEXT_Utf16* value indicating the |
| 486 | ** actual byte order used by this string. If the string does happen |
| 487 | ** to contain a BOM, then move zData so that it points to the first |
| 488 | ** byte after the BOM. |
| 489 | */ |
| 490 | txt_enc = sqlite3UtfReadBom(zData, nData); |
| 491 | if( txt_enc ){ |
| 492 | zData = (void *)(((u8 *)zData) + 2); |
| 493 | nData -= 2; |
| 494 | }else{ |
| 495 | txt_enc = SQLITE3_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le; |
| 496 | } |
| 497 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy); |
| 498 | if( rc ){ |
| 499 | return rc; |
| 500 | } |
| 501 | rc = sqlite3VdbeSetEncoding(pVar, p->db->enc); |
| 502 | return rc; |
| 503 | } |