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