blob: bd34f942d3b4043c269e7da76cd71b9be0b3e574 [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;
98#if 0
drh4f26d6c2004-05-26 23:25:30 +000099 int f = ((Mem *)pVal)->flags;
100 if( f&MEM_Null ){
101 return SQLITE3_NULL;
102 }
103 if( f&MEM_Int ){
104 return SQLITE3_INTEGER;
105 }
106 if( f&MEM_Real ){
107 return SQLITE3_FLOAT;
108 }
109 if( f&MEM_Str ){
110 return SQLITE3_TEXT;
111 }
112 if( f&MEM_Blob ){
113 return SQLITE3_BLOB;
114 }
115 assert(0);
drhf4479502004-05-27 03:12:53 +0000116#endif
drh4f26d6c2004-05-26 23:25:30 +0000117}
118
119/**************************** sqlite3_result_ *******************************
120** The following routines are used by user-defined functions to specify
121** the function result.
122*/
123void sqlite3_result_blob(
124 sqlite3_context *pCtx,
125 const void *z,
126 int n,
127 int eCopy
128){
129 assert( n>0 );
drhf4479502004-05-27 03:12:53 +0000130 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000131}
132void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
133 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
134}
135void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
136 pCtx->isError = 1;
137 sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf8, 1);
138}
139void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
140 pCtx->isError = 1;
141 sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf16, 1);
142}
drhf4479502004-05-27 03:12:53 +0000143void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +0000144 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
145}
146void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
147 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
148}
149void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +0000150 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000151}
152void sqlite3_result_text(
153 sqlite3_context *pCtx,
154 const char *z,
155 int n,
156 int eCopy
157){
drhf4479502004-05-27 03:12:53 +0000158 sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf8, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000159}
160void sqlite3_result_text16(
161 sqlite3_context *pCtx,
162 const void *z,
163 int n,
164 int eCopy
165){
drhf4479502004-05-27 03:12:53 +0000166 sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf16, eCopy);
drh4f26d6c2004-05-26 23:25:30 +0000167}
168void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
169 sqlite3VdbeMemCopy(&pCtx->s, pValue);
170}
171
172
173/*
174** Execute the statement pStmt, either until a row of data is ready, the
175** statement is completely executed or an error occurs.
176*/
177int sqlite3_step(sqlite3_stmt *pStmt){
178 Vdbe *p = (Vdbe*)pStmt;
179 sqlite *db;
180 int rc;
181
182 if( p->magic!=VDBE_MAGIC_RUN ){
183 return SQLITE_MISUSE;
184 }
185 db = p->db;
186 if( sqlite3SafetyOn(db) ){
187 p->rc = SQLITE_MISUSE;
188 return SQLITE_MISUSE;
189 }
190 if( p->explain ){
191 rc = sqlite3VdbeList(p);
192 }else{
193 rc = sqlite3VdbeExec(p);
194 }
195
196 if( sqlite3SafetyOff(db) ){
197 rc = SQLITE_MISUSE;
198 }
199
200 sqlite3Error(p->db, rc, p->zErrMsg);
201 return rc;
202}
203
204/*
drheb2e1762004-05-27 01:53:56 +0000205** Extract the user data from a sqlite3_context structure and return a
206** pointer to it.
207*/
208void *sqlite3_user_data(sqlite3_context *p){
209 assert( p && p->pFunc );
210 return p->pFunc->pUserData;
211}
212
213/*
214** Allocate or return the aggregate context for a user function. A new
215** context is allocated on the first call. Subsequent calls return the
216** same context that was returned on prior calls.
217**
218** This routine is defined here in vdbe.c because it depends on knowing
219** the internals of the sqlite3_context structure which is only defined in
220** this source file.
221*/
222void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
223 assert( p && p->pFunc && p->pFunc->xStep );
224 if( p->pAgg==0 ){
225 if( nByte<=NBFS ){
226 p->pAgg = (void*)p->s.z;
227 memset(p->pAgg, 0, nByte);
228 }else{
229 p->pAgg = sqliteMalloc( nByte );
230 }
231 }
232 return p->pAgg;
233}
234
235/*
236** Return the number of times the Step function of a aggregate has been
237** called.
238**
239** This routine is defined here in vdbe.c because it depends on knowing
240** the internals of the sqlite3_context structure which is only defined in
241** this source file.
242*/
243int sqlite3_aggregate_count(sqlite3_context *p){
244 assert( p && p->pFunc && p->pFunc->xStep );
245 return p->cnt;
246}
247
248/*
drh4f26d6c2004-05-26 23:25:30 +0000249** Return the number of columns in the result set for the statement pStmt.
250*/
251int sqlite3_column_count(sqlite3_stmt *pStmt){
252 Vdbe *pVm = (Vdbe *)pStmt;
253 return pVm->nResColumn;
254}
255
256/*
257** Return the number of values available from the current row of the
258** currently executing statement pStmt.
259*/
260int sqlite3_data_count(sqlite3_stmt *pStmt){
261 Vdbe *pVm = (Vdbe *)pStmt;
262 if( !pVm->resOnStack ) return 0;
263 return pVm->nResColumn;
264}
265
266
267/*
268** Check to see if column iCol of the given statement is valid. If
269** it is, return a pointer to the Mem for the value of that column.
270** If iCol is not valid, return a pointer to a Mem which has a value
271** of NULL.
272*/
273static Mem *columnMem(sqlite3_stmt *pStmt, int i){
274 Vdbe *pVm = (Vdbe *)pStmt;
275 int vals = sqlite3_data_count(pStmt);
276 if( i>=vals || i<0 ){
277 static Mem nullMem;
278 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
279 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
280 return &nullMem;
281 }
282 return &pVm->pTos[(1-vals)+i];
283}
284
285/**************************** sqlite3_column_ *******************************
286** The following routines are used to access elements of the current row
287** in the result set.
288*/
289int 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
349
350/*
351** This routine returns either the column name, or declaration type (see
352** sqlite3_column_decltype16() ) of the 'i'th column of the result set of
353** SQL statement pStmt. The returned string is UTF-16 encoded.
354**
355** The declaration type is returned if 'decltype' is true, otherwise
356** the column name.
357*/
358static const void *columnName16(sqlite3_stmt *pStmt, int i, int decltype){
359 Vdbe *p = (Vdbe *)pStmt;
360
361 if( i>=sqlite3_column_count(pStmt) || i<0 ){
362 sqlite3Error(p->db, SQLITE_RANGE, 0);
363 return 0;
364 }
365
366 if( decltype ){
367 i += p->nResColumn;
368 }
369
370 if( !p->azColName16 ){
371 p->azColName16 = (void **)sqliteMalloc(sizeof(void *)*p->nResColumn*2);
372 if( !p->azColName16 ){
373 sqlite3Error(p->db, SQLITE_NOMEM, 0);
374 return 0;
375 }
376 }
377 if( !p->azColName16[i] ){
378 if( SQLITE3_BIGENDIAN ){
379 p->azColName16[i] = sqlite3utf8to16be(p->azColName[i], -1);
380 }
381 if( !p->azColName16[i] ){
382 sqlite3Error(p->db, SQLITE_NOMEM, 0);
383 return 0;
384 }
385 }
386 return p->azColName16[i];
387}
388
389/*
390** Return the column declaration type (if applicable) of the 'i'th column
391** of the result set of SQL statement pStmt, encoded as UTF-8.
392*/
393const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int i){
394 Vdbe *p = (Vdbe *)pStmt;
395
396 if( i>=sqlite3_column_count(pStmt) || i<0 ){
397 sqlite3Error(p->db, SQLITE_RANGE, 0);
398 return 0;
399 }
400
401 return p->azColName[i+p->nResColumn];
402}
403
404/*
405** Return the column declaration type (if applicable) of the 'i'th column
406** of the result set of SQL statement pStmt, encoded as UTF-16.
407*/
408const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int i){
409 return columnName16(pStmt, i, 1);
410}
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;
426 if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){
427 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{
541 txt_enc = SQLITE3_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le;
542 }
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}