blob: 1e40fad7517d84a73f4cb98d3c4e15399599d7e4 [file] [log] [blame]
drh4f26d6c2004-05-26 23:25:30 +00001/*
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*/
23const 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}
31int 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}
38int 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}
45double sqlite3_value_double(sqlite3_value *pVal){
46 Mem *pMem = (Mem *)pVal;
drhf4479502004-05-27 03:12:53 +000047 sqlite3VdbeMemRealify(pMem);
drh4f26d6c2004-05-26 23:25:30 +000048 return pMem->r;
49}
50int sqlite3_value_int(sqlite3_value *pVal){
51 Mem *pMem = (Mem *)pVal;
drhf4479502004-05-27 03:12:53 +000052 sqlite3VdbeMemIntegerify(pMem);
drh4f26d6c2004-05-26 23:25:30 +000053 return (int)pVal->i;
54}
55long long int sqlite3_value_int64(sqlite3_value *pVal){
56 Mem *pMem = (Mem *)pVal;
drhf4479502004-05-27 03:12:53 +000057 sqlite3VdbeMemIntegerify(pMem);
drh4f26d6c2004-05-26 23:25:30 +000058 return pVal->i;
59}
60const 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 */
drhf4479502004-05-27 03:12:53 +000070 sqlite3VdbeChangeEncoding(pVal, TEXT_Utf8);
drh4f26d6c2004-05-26 23:25:30 +000071 }else if( !(pVal->flags&MEM_Blob) ){
72 /* Otherwise, unless this is a blob, convert it to a UTF-8 string */
drhf4479502004-05-27 03:12:53 +000073 sqlite3VdbeMemStringify(pVal, TEXT_Utf8);
drh4f26d6c2004-05-26 23:25:30 +000074 }
75
76 return pVal->z;
77}
78const 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 */
drhf4479502004-05-27 03:12:53 +000088 sqlite3VdbeChangeEncoding(pVal, TEXT_Utf16);
drh4f26d6c2004-05-26 23:25:30 +000089 }else if( !(pVal->flags&MEM_Blob) ){
90 /* Otherwise, unless this is a blob, convert it to a UTF-16 string */
drhf4479502004-05-27 03:12:53 +000091 sqlite3VdbeMemStringify(pVal, TEXT_Utf16);
drh4f26d6c2004-05-26 23:25:30 +000092 }
93
94 return (const void *)(pVal->z);
95}
96int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000097 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000098}
99
100/**************************** sqlite3_result_ *******************************
101** The following routines are used by user-defined functions to specify
102** the function result.
103*/
104void sqlite3_result_blob(
105 sqlite3_context *pCtx,
106 const void *z,
107 int n,
108 int eCopy
109){
110 assert( n>0 );
drhf4479502004-05-27 03:12:53 +0000111 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000112}
113void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
114 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
115}
116void 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}
120void 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}
drhf4479502004-05-27 03:12:53 +0000124void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +0000125 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
126}
127void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
128 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
129}
130void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +0000131 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000132}
133void sqlite3_result_text(
134 sqlite3_context *pCtx,
135 const char *z,
136 int n,
137 int eCopy
138){
drhf4479502004-05-27 03:12:53 +0000139 sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf8, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000140}
141void sqlite3_result_text16(
142 sqlite3_context *pCtx,
143 const void *z,
144 int n,
145 int eCopy
146){
drhf4479502004-05-27 03:12:53 +0000147 sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf16, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000148}
149void 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*/
158int 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 }
danielk19771d850a72004-05-31 08:26:49 +0000171 if( p->pc<0 ){
172 db->activeVdbeCnt++;
173 p->pc = 0;
174 }
drh4f26d6c2004-05-26 23:25:30 +0000175 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/*
drheb2e1762004-05-27 01:53:56 +0000190** Extract the user data from a sqlite3_context structure and return a
191** pointer to it.
192*/
193void *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*/
207void *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/*
danielk1977682f68b2004-06-05 10:22:17 +0000221** Return the auxilary data pointer, if any, for the iArg'th argument to
222** the user-function defined by pCtx.
223*/
224void *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*/
237void 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/*
drheb2e1762004-05-27 01:53:56 +0000262** 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*/
269int sqlite3_aggregate_count(sqlite3_context *p){
270 assert( p && p->pFunc && p->pFunc->xStep );
271 return p->cnt;
272}
273
274/*
drh4f26d6c2004-05-26 23:25:30 +0000275** Return the number of columns in the result set for the statement pStmt.
276*/
277int 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*/
286int 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*/
299static 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*/
danielk1977c572ef72004-05-27 09:28:41 +0000315const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
316 return sqlite3_value_blob( columnMem(pStmt,i) );
317}
drh4f26d6c2004-05-26 23:25:30 +0000318int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
319 return sqlite3_value_bytes( columnMem(pStmt,i) );
320}
321int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
322 return sqlite3_value_bytes16( columnMem(pStmt,i) );
323}
324double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
325 return sqlite3_value_double( columnMem(pStmt,i) );
326}
327int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
328 return sqlite3_value_int( columnMem(pStmt,i) );
329}
330long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
331 return sqlite3_value_int64( columnMem(pStmt,i) );
332}
333const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
334 return sqlite3_value_text( columnMem(pStmt,i) );
335}
336const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
337 return sqlite3_value_text16( columnMem(pStmt,i) );
338}
339int 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*/
348const 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*/
365const 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
drh4f26d6c2004-05-26 23:25:30 +0000378/*
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*/
danielk197776d505b2004-05-28 13:13:02 +0000382const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
drh4f26d6c2004-05-26 23:25:30 +0000383 Vdbe *p = (Vdbe *)pStmt;
danielk197776d505b2004-05-28 13:13:02 +0000384 Mem *pColName;
drh4f26d6c2004-05-26 23:25:30 +0000385
danielk197776d505b2004-05-28 13:13:02 +0000386 if( N>=sqlite3_column_count(pStmt) || N<0 ){
drh4f26d6c2004-05-26 23:25:30 +0000387 sqlite3Error(p->db, SQLITE_RANGE, 0);
388 return 0;
389 }
390
danielk197776d505b2004-05-28 13:13:02 +0000391 pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]);
392 return sqlite3_value_text(pColName);
drh4f26d6c2004-05-26 23:25:30 +0000393}
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*/
danielk197776d505b2004-05-28 13:13:02 +0000399const 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);
drh4f26d6c2004-05-26 23:25:30 +0000410}
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*/
424static int vdbeUnbind(Vdbe *p, int i){
425 Mem *pVar;
danielk19771d850a72004-05-31 08:26:49 +0000426 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000427 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*/
447int sqlite3_bind_blob(
drhf4479502004-05-27 03:12:53 +0000448 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000449 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}
466int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
467 int rc;
468 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000469 rc = vdbeUnbind(p, i);
470 if( rc==SQLITE_OK ){
drhf4479502004-05-27 03:12:53 +0000471 sqlite3VdbeMemSetDouble(&p->apVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000472 }
473 return SQLITE_OK;
474}
475int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
476 return sqlite3_bind_int64(p, i, (long long int)iValue);
477}
478int 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 ){
drhf4479502004-05-27 03:12:53 +0000483 sqlite3VdbeMemSetInt64(&p->apVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000484 }
485 return rc;
486}
487int sqlite3_bind_null(sqlite3_stmt* p, int i){
488 return vdbeUnbind((Vdbe *)p, i);
489}
490int 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 }
drhf4479502004-05-27 03:12:53 +0000510 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
drh4f26d6c2004-05-26 23:25:30 +0000511 return rc;
512}
513int 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;
drhf4479502004-05-27 03:12:53 +0000522 int rc, txt_enc;
drh4f26d6c2004-05-26 23:25:30 +0000523
524 rc = vdbeUnbind(p, i);
525 if( rc ){
526 return rc;
527 }
drhf4479502004-05-27 03:12:53 +0000528 pVar = &p->apVar[i-1];
drh4f26d6c2004-05-26 23:25:30 +0000529
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{
drh9c054832004-05-31 18:51:57 +0000541 txt_enc = SQLITE_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le;
drh4f26d6c2004-05-26 23:25:30 +0000542 }
543 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy);
544 if( rc ){
545 return rc;
546 }
drhf4479502004-05-27 03:12:53 +0000547 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
drh4f26d6c2004-05-26 23:25:30 +0000548 return rc;
549}