blob: a3fc2f03f8fbb2d2b41ca7255f00bb514172b8a0 [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){
danielk1977dc8453f2004-06-12 00:42:34 +000061 return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000062}
63const void *sqlite3_value_text16(sqlite3_value* pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000064 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000065}
66int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000067 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000068}
69
70/**************************** sqlite3_result_ *******************************
71** The following routines are used by user-defined functions to specify
72** the function result.
73*/
74void sqlite3_result_blob(
75 sqlite3_context *pCtx,
76 const void *z,
77 int n,
78 int eCopy
79){
80 assert( n>0 );
drhf4479502004-05-27 03:12:53 +000081 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, eCopy);
drh4f26d6c2004-05-26 23:25:30 +000082}
83void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
84 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
85}
86void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
87 pCtx->isError = 1;
danielk1977dc8453f2004-06-12 00:42:34 +000088 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, 1);
drh4f26d6c2004-05-26 23:25:30 +000089}
90void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
91 pCtx->isError = 1;
danielk1977dc8453f2004-06-12 00:42:34 +000092 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, 1);
drh4f26d6c2004-05-26 23:25:30 +000093}
drhf4479502004-05-27 03:12:53 +000094void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +000095 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
96}
97void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
98 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
99}
100void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +0000101 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000102}
103void sqlite3_result_text(
104 sqlite3_context *pCtx,
105 const char *z,
106 int n,
107 int eCopy
108){
danielk1977dc8453f2004-06-12 00:42:34 +0000109 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000110}
111void sqlite3_result_text16(
112 sqlite3_context *pCtx,
113 const void *z,
114 int n,
115 int eCopy
116){
danielk1977dc8453f2004-06-12 00:42:34 +0000117 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000118}
119void 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*/
128int 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 }
danielk19771d850a72004-05-31 08:26:49 +0000141 if( p->pc<0 ){
142 db->activeVdbeCnt++;
143 p->pc = 0;
144 }
drh4f26d6c2004-05-26 23:25:30 +0000145 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/*
drheb2e1762004-05-27 01:53:56 +0000160** Extract the user data from a sqlite3_context structure and return a
161** pointer to it.
162*/
163void *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*/
177void *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/*
danielk1977682f68b2004-06-05 10:22:17 +0000191** Return the auxilary data pointer, if any, for the iArg'th argument to
192** the user-function defined by pCtx.
193*/
194void *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*/
207void 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;
danielk1977d02eb1f2004-06-06 09:44:03 +0000221 pCtx->pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000222 }
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/*
drheb2e1762004-05-27 01:53:56 +0000233** 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*/
240int sqlite3_aggregate_count(sqlite3_context *p){
241 assert( p && p->pFunc && p->pFunc->xStep );
242 return p->cnt;
243}
244
245/*
drh4f26d6c2004-05-26 23:25:30 +0000246** Return the number of columns in the result set for the statement pStmt.
247*/
248int 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*/
257int 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*/
270static 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*/
danielk1977c572ef72004-05-27 09:28:41 +0000286const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
287 return sqlite3_value_blob( columnMem(pStmt,i) );
288}
drh4f26d6c2004-05-26 23:25:30 +0000289int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
290 return sqlite3_value_bytes( columnMem(pStmt,i) );
291}
292int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
293 return sqlite3_value_bytes16( columnMem(pStmt,i) );
294}
295double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
296 return sqlite3_value_double( columnMem(pStmt,i) );
297}
298int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
299 return sqlite3_value_int( columnMem(pStmt,i) );
300}
301long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
302 return sqlite3_value_int64( columnMem(pStmt,i) );
303}
304const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
305 return sqlite3_value_text( columnMem(pStmt,i) );
306}
307const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
308 return sqlite3_value_text16( columnMem(pStmt,i) );
309}
310int 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*/
319const 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*/
336const 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
drh4f26d6c2004-05-26 23:25:30 +0000349/*
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*/
danielk197776d505b2004-05-28 13:13:02 +0000353const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
drh4f26d6c2004-05-26 23:25:30 +0000354 Vdbe *p = (Vdbe *)pStmt;
danielk197776d505b2004-05-28 13:13:02 +0000355 Mem *pColName;
drh4f26d6c2004-05-26 23:25:30 +0000356
danielk197776d505b2004-05-28 13:13:02 +0000357 if( N>=sqlite3_column_count(pStmt) || N<0 ){
drh4f26d6c2004-05-26 23:25:30 +0000358 sqlite3Error(p->db, SQLITE_RANGE, 0);
359 return 0;
360 }
361
danielk197776d505b2004-05-28 13:13:02 +0000362 pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]);
363 return sqlite3_value_text(pColName);
drh4f26d6c2004-05-26 23:25:30 +0000364}
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*/
danielk197776d505b2004-05-28 13:13:02 +0000370const 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);
drh4f26d6c2004-05-26 23:25:30 +0000381}
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*/
395static int vdbeUnbind(Vdbe *p, int i){
396 Mem *pVar;
danielk19771d850a72004-05-31 08:26:49 +0000397 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000398 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*/
418int sqlite3_bind_blob(
drhf4479502004-05-27 03:12:53 +0000419 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000420 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}
437int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
438 int rc;
439 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000440 rc = vdbeUnbind(p, i);
441 if( rc==SQLITE_OK ){
drhf4479502004-05-27 03:12:53 +0000442 sqlite3VdbeMemSetDouble(&p->apVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000443 }
444 return SQLITE_OK;
445}
446int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
447 return sqlite3_bind_int64(p, i, (long long int)iValue);
448}
449int 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 ){
drhf4479502004-05-27 03:12:53 +0000454 sqlite3VdbeMemSetInt64(&p->apVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000455 }
456 return rc;
457}
458int sqlite3_bind_null(sqlite3_stmt* p, int i){
459 return vdbeUnbind((Vdbe *)p, i);
460}
461int 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];
danielk1977dc8453f2004-06-12 00:42:34 +0000477 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, SQLITE_UTF8, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000478 if( rc ){
479 return rc;
480 }
drhf4479502004-05-27 03:12:53 +0000481 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
drh4f26d6c2004-05-26 23:25:30 +0000482 return rc;
483}
484int 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;
drhf4479502004-05-27 03:12:53 +0000493 int rc, txt_enc;
drh4f26d6c2004-05-26 23:25:30 +0000494
495 rc = vdbeUnbind(p, i);
496 if( rc ){
497 return rc;
498 }
drhf4479502004-05-27 03:12:53 +0000499 pVar = &p->apVar[i-1];
drh4f26d6c2004-05-26 23:25:30 +0000500
501 /* There may or may not be a byte order mark at the start of the UTF-16.
danielk1977dc8453f2004-06-12 00:42:34 +0000502 ** Either way set 'txt_enc' to the SQLITE_UTF16* value indicating the
drh4f26d6c2004-05-26 23:25:30 +0000503 ** 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{
danielk1977dc8453f2004-06-12 00:42:34 +0000512 txt_enc = SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE;
drh4f26d6c2004-05-26 23:25:30 +0000513 }
514 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy);
515 if( rc ){
516 return rc;
517 }
drhf4479502004-05-27 03:12:53 +0000518 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
drh4f26d6c2004-05-26 23:25:30 +0000519 return rc;
520}