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