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 | /* |
| 221 | ** Return the number of times the Step function of a aggregate has been |
| 222 | ** called. |
| 223 | ** |
| 224 | ** This routine is defined here in vdbe.c because it depends on knowing |
| 225 | ** the internals of the sqlite3_context structure which is only defined in |
| 226 | ** this source file. |
| 227 | */ |
| 228 | int sqlite3_aggregate_count(sqlite3_context *p){ |
| 229 | assert( p && p->pFunc && p->pFunc->xStep ); |
| 230 | return p->cnt; |
| 231 | } |
| 232 | |
| 233 | /* |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 234 | ** Return the number of columns in the result set for the statement pStmt. |
| 235 | */ |
| 236 | int sqlite3_column_count(sqlite3_stmt *pStmt){ |
| 237 | Vdbe *pVm = (Vdbe *)pStmt; |
| 238 | return pVm->nResColumn; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | ** Return the number of values available from the current row of the |
| 243 | ** currently executing statement pStmt. |
| 244 | */ |
| 245 | int sqlite3_data_count(sqlite3_stmt *pStmt){ |
| 246 | Vdbe *pVm = (Vdbe *)pStmt; |
| 247 | if( !pVm->resOnStack ) return 0; |
| 248 | return pVm->nResColumn; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /* |
| 253 | ** Check to see if column iCol of the given statement is valid. If |
| 254 | ** it is, return a pointer to the Mem for the value of that column. |
| 255 | ** If iCol is not valid, return a pointer to a Mem which has a value |
| 256 | ** of NULL. |
| 257 | */ |
| 258 | static Mem *columnMem(sqlite3_stmt *pStmt, int i){ |
| 259 | Vdbe *pVm = (Vdbe *)pStmt; |
| 260 | int vals = sqlite3_data_count(pStmt); |
| 261 | if( i>=vals || i<0 ){ |
| 262 | static Mem nullMem; |
| 263 | if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; } |
| 264 | sqlite3Error(pVm->db, SQLITE_RANGE, 0); |
| 265 | return &nullMem; |
| 266 | } |
| 267 | return &pVm->pTos[(1-vals)+i]; |
| 268 | } |
| 269 | |
| 270 | /**************************** sqlite3_column_ ******************************* |
| 271 | ** The following routines are used to access elements of the current row |
| 272 | ** in the result set. |
| 273 | */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 274 | const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ |
| 275 | return sqlite3_value_blob( columnMem(pStmt,i) ); |
| 276 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 277 | int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ |
| 278 | return sqlite3_value_bytes( columnMem(pStmt,i) ); |
| 279 | } |
| 280 | int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ |
| 281 | return sqlite3_value_bytes16( columnMem(pStmt,i) ); |
| 282 | } |
| 283 | double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ |
| 284 | return sqlite3_value_double( columnMem(pStmt,i) ); |
| 285 | } |
| 286 | int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ |
| 287 | return sqlite3_value_int( columnMem(pStmt,i) ); |
| 288 | } |
| 289 | long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ |
| 290 | return sqlite3_value_int64( columnMem(pStmt,i) ); |
| 291 | } |
| 292 | const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ |
| 293 | return sqlite3_value_text( columnMem(pStmt,i) ); |
| 294 | } |
| 295 | const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ |
| 296 | return sqlite3_value_text16( columnMem(pStmt,i) ); |
| 297 | } |
| 298 | int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ |
| 299 | return sqlite3_value_type( columnMem(pStmt,i) ); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /* |
| 304 | ** Return the name of the Nth column of the result set returned by SQL |
| 305 | ** statement pStmt. |
| 306 | */ |
| 307 | const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
| 308 | Vdbe *p = (Vdbe *)pStmt; |
| 309 | Mem *pColName; |
| 310 | |
| 311 | if( N>=sqlite3_column_count(pStmt) || N<0 ){ |
| 312 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | pColName = &(p->aColName[N]); |
| 317 | return sqlite3_value_text(pColName); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | ** Return the name of the 'i'th column of the result set of SQL statement |
| 322 | ** pStmt, encoded as UTF-16. |
| 323 | */ |
| 324 | const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
| 325 | Vdbe *p = (Vdbe *)pStmt; |
| 326 | Mem *pColName; |
| 327 | |
| 328 | if( N>=sqlite3_column_count(pStmt) || N<0 ){ |
| 329 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | pColName = &(p->aColName[N]); |
| 334 | return sqlite3_value_text16(pColName); |
| 335 | } |
| 336 | |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 337 | /* |
| 338 | ** Return the column declaration type (if applicable) of the 'i'th column |
| 339 | ** of the result set of SQL statement pStmt, encoded as UTF-8. |
| 340 | */ |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 341 | const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 342 | Vdbe *p = (Vdbe *)pStmt; |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 343 | Mem *pColName; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 344 | |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 345 | if( N>=sqlite3_column_count(pStmt) || N<0 ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 346 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 347 | return 0; |
| 348 | } |
| 349 | |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 350 | pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]); |
| 351 | return sqlite3_value_text(pColName); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | ** Return the column declaration type (if applicable) of the 'i'th column |
| 356 | ** of the result set of SQL statement pStmt, encoded as UTF-16. |
| 357 | */ |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 358 | const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ |
| 359 | Vdbe *p = (Vdbe *)pStmt; |
| 360 | Mem *pColName; |
| 361 | |
| 362 | if( N>=sqlite3_column_count(pStmt) || N<0 ){ |
| 363 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]); |
| 368 | return sqlite3_value_text16(pColName); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /******************************* sqlite3_bind_ *************************** |
| 372 | ** |
| 373 | ** Routines used to attach values to wildcards in a compiled SQL statement. |
| 374 | */ |
| 375 | /* |
| 376 | ** Unbind the value bound to variable i in virtual machine p. This is the |
| 377 | ** the same as binding a NULL value to the column. If the "i" parameter is |
| 378 | ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
| 379 | ** |
| 380 | ** The error code stored in database p->db is overwritten with the return |
| 381 | ** value in any case. |
| 382 | */ |
| 383 | static int vdbeUnbind(Vdbe *p, int i){ |
| 384 | Mem *pVar; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame^] | 385 | if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 386 | sqlite3Error(p->db, SQLITE_MISUSE, 0); |
| 387 | return SQLITE_MISUSE; |
| 388 | } |
| 389 | if( i<1 || i>p->nVar ){ |
| 390 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
| 391 | return SQLITE_RANGE; |
| 392 | } |
| 393 | i--; |
| 394 | pVar = &p->apVar[i]; |
| 395 | if( pVar->flags&MEM_Dyn ){ |
| 396 | sqliteFree(pVar->z); |
| 397 | } |
| 398 | pVar->flags = MEM_Null; |
| 399 | sqlite3Error(p->db, SQLITE_OK, 0); |
| 400 | return SQLITE_OK; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | ** Bind a blob value to an SQL statement variable. |
| 405 | */ |
| 406 | int sqlite3_bind_blob( |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 407 | sqlite3_stmt *pStmt, |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 408 | int i, |
| 409 | const void *zData, |
| 410 | int nData, |
| 411 | int eCopy |
| 412 | ){ |
| 413 | Vdbe *p = (Vdbe *)pStmt; |
| 414 | Mem *pVar; |
| 415 | int rc; |
| 416 | |
| 417 | rc = vdbeUnbind(p, i); |
| 418 | if( rc ){ |
| 419 | return rc; |
| 420 | } |
| 421 | pVar = &p->apVar[i-1]; |
| 422 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy); |
| 423 | return rc; |
| 424 | } |
| 425 | int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
| 426 | int rc; |
| 427 | Vdbe *p = (Vdbe *)pStmt; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 428 | rc = vdbeUnbind(p, i); |
| 429 | if( rc==SQLITE_OK ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 430 | sqlite3VdbeMemSetDouble(&p->apVar[i-1], rValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 431 | } |
| 432 | return SQLITE_OK; |
| 433 | } |
| 434 | int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
| 435 | return sqlite3_bind_int64(p, i, (long long int)iValue); |
| 436 | } |
| 437 | int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){ |
| 438 | int rc; |
| 439 | Vdbe *p = (Vdbe *)pStmt; |
| 440 | rc = vdbeUnbind(p, i); |
| 441 | if( rc==SQLITE_OK ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 442 | sqlite3VdbeMemSetInt64(&p->apVar[i-1], iValue); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 443 | } |
| 444 | return rc; |
| 445 | } |
| 446 | int sqlite3_bind_null(sqlite3_stmt* p, int i){ |
| 447 | return vdbeUnbind((Vdbe *)p, i); |
| 448 | } |
| 449 | int sqlite3_bind_text( |
| 450 | sqlite3_stmt *pStmt, |
| 451 | int i, |
| 452 | const char *zData, |
| 453 | int nData, |
| 454 | int eCopy |
| 455 | ){ |
| 456 | Vdbe *p = (Vdbe *)pStmt; |
| 457 | Mem *pVar; |
| 458 | int rc; |
| 459 | |
| 460 | rc = vdbeUnbind(p, i); |
| 461 | if( rc ){ |
| 462 | return rc; |
| 463 | } |
| 464 | pVar = &p->apVar[i-1]; |
| 465 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, TEXT_Utf8, eCopy); |
| 466 | if( rc ){ |
| 467 | return rc; |
| 468 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 469 | rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 470 | return rc; |
| 471 | } |
| 472 | int sqlite3_bind_text16( |
| 473 | sqlite3_stmt *pStmt, |
| 474 | int i, |
| 475 | const void *zData, |
| 476 | int nData, |
| 477 | int eCopy |
| 478 | ){ |
| 479 | Vdbe *p = (Vdbe *)pStmt; |
| 480 | Mem *pVar; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 481 | int rc, txt_enc; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 482 | |
| 483 | rc = vdbeUnbind(p, i); |
| 484 | if( rc ){ |
| 485 | return rc; |
| 486 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 487 | pVar = &p->apVar[i-1]; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 488 | |
| 489 | /* There may or may not be a byte order mark at the start of the UTF-16. |
| 490 | ** Either way set 'txt_enc' to the TEXT_Utf16* value indicating the |
| 491 | ** actual byte order used by this string. If the string does happen |
| 492 | ** to contain a BOM, then move zData so that it points to the first |
| 493 | ** byte after the BOM. |
| 494 | */ |
| 495 | txt_enc = sqlite3UtfReadBom(zData, nData); |
| 496 | if( txt_enc ){ |
| 497 | zData = (void *)(((u8 *)zData) + 2); |
| 498 | nData -= 2; |
| 499 | }else{ |
| 500 | txt_enc = SQLITE3_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le; |
| 501 | } |
| 502 | rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy); |
| 503 | if( rc ){ |
| 504 | return rc; |
| 505 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 506 | rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 507 | return rc; |
| 508 | } |