blob: d21eb17308ba3514a39b0244eff70ac6db86d031 [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;
47 Realify(pMem, flagsToEnc(pMem->flags));
48 return pMem->r;
49}
50int sqlite3_value_int(sqlite3_value *pVal){
51 Mem *pMem = (Mem *)pVal;
52 Integerify(pMem, flagsToEnc(pMem->flags));
53 return (int)pVal->i;
54}
55long long int sqlite3_value_int64(sqlite3_value *pVal){
56 Mem *pMem = (Mem *)pVal;
57 Integerify(pMem, flagsToEnc(pMem->flags));
58 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 */
70 SetEncoding(pVal, MEM_Utf8|MEM_Term);
71 }else if( !(pVal->flags&MEM_Blob) ){
72 /* Otherwise, unless this is a blob, convert it to a UTF-8 string */
73 Stringify(pVal, TEXT_Utf8);
74 }
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 */
88 SetEncoding(pVal, encToFlags(TEXT_Utf16)|MEM_Term);
89 }else if( !(pVal->flags&MEM_Blob) ){
90 /* Otherwise, unless this is a blob, convert it to a UTF-16 string */
91 Stringify(pVal, TEXT_Utf16);
92 }
93
94 return (const void *)(pVal->z);
95}
96int sqlite3_value_type(sqlite3_value* pVal){
97 int f = ((Mem *)pVal)->flags;
98 if( f&MEM_Null ){
99 return SQLITE3_NULL;
100 }
101 if( f&MEM_Int ){
102 return SQLITE3_INTEGER;
103 }
104 if( f&MEM_Real ){
105 return SQLITE3_FLOAT;
106 }
107 if( f&MEM_Str ){
108 return SQLITE3_TEXT;
109 }
110 if( f&MEM_Blob ){
111 return SQLITE3_BLOB;
112 }
113 assert(0);
114}
115
116/**************************** sqlite3_result_ *******************************
117** The following routines are used by user-defined functions to specify
118** the function result.
119*/
120void sqlite3_result_blob(
121 sqlite3_context *pCtx,
122 const void *z,
123 int n,
124 int eCopy
125){
126 assert( n>0 );
127 MemSetStr(&pCtx->s, z, n, 0, eCopy);
128}
129void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
130 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
131}
132void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
133 pCtx->isError = 1;
134 sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf8, 1);
135}
136void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
137 pCtx->isError = 1;
138 sqlite3VdbeMemSetStr(&pCtx->s, z, n, TEXT_Utf16, 1);
139}
140void sqlite3_result_int32(sqlite3_context *pCtx, int iVal){
141 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
142}
143void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
144 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
145}
146void sqlite3_result_null(sqlite3_context *pCtx){
147 sqilte3VdbeMemSetNull(&pCtx->s);
148}
149void sqlite3_result_text(
150 sqlite3_context *pCtx,
151 const char *z,
152 int n,
153 int eCopy
154){
155 MemSetStr(&pCtx->s, z, n, TEXT_Utf8, eCopy);
156}
157void sqlite3_result_text16(
158 sqlite3_context *pCtx,
159 const void *z,
160 int n,
161 int eCopy
162){
163 MemSetStr(&pCtx->s, z, n, TEXT_Utf16, eCopy);
164}
165void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
166 sqlite3VdbeMemCopy(&pCtx->s, pValue);
167}
168
169
170/*
171** Execute the statement pStmt, either until a row of data is ready, the
172** statement is completely executed or an error occurs.
173*/
174int sqlite3_step(sqlite3_stmt *pStmt){
175 Vdbe *p = (Vdbe*)pStmt;
176 sqlite *db;
177 int rc;
178
179 if( p->magic!=VDBE_MAGIC_RUN ){
180 return SQLITE_MISUSE;
181 }
182 db = p->db;
183 if( sqlite3SafetyOn(db) ){
184 p->rc = SQLITE_MISUSE;
185 return SQLITE_MISUSE;
186 }
187 if( p->explain ){
188 rc = sqlite3VdbeList(p);
189 }else{
190 rc = sqlite3VdbeExec(p);
191 }
192
193 if( sqlite3SafetyOff(db) ){
194 rc = SQLITE_MISUSE;
195 }
196
197 sqlite3Error(p->db, rc, p->zErrMsg);
198 return rc;
199}
200
201/*
drheb2e1762004-05-27 01:53:56 +0000202** Extract the user data from a sqlite3_context structure and return a
203** pointer to it.
204*/
205void *sqlite3_user_data(sqlite3_context *p){
206 assert( p && p->pFunc );
207 return p->pFunc->pUserData;
208}
209
210/*
211** Allocate or return the aggregate context for a user function. A new
212** context is allocated on the first call. Subsequent calls return the
213** same context that was returned on prior calls.
214**
215** This routine is defined here in vdbe.c because it depends on knowing
216** the internals of the sqlite3_context structure which is only defined in
217** this source file.
218*/
219void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
220 assert( p && p->pFunc && p->pFunc->xStep );
221 if( p->pAgg==0 ){
222 if( nByte<=NBFS ){
223 p->pAgg = (void*)p->s.z;
224 memset(p->pAgg, 0, nByte);
225 }else{
226 p->pAgg = sqliteMalloc( nByte );
227 }
228 }
229 return p->pAgg;
230}
231
232/*
233** 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*/
286int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
287 return sqlite3_value_bytes( columnMem(pStmt,i) );
288}
289int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
290 return sqlite3_value_bytes16( columnMem(pStmt,i) );
291}
292double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
293 return sqlite3_value_double( columnMem(pStmt,i) );
294}
295int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
296 return sqlite3_value_int( columnMem(pStmt,i) );
297}
298long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
299 return sqlite3_value_int64( columnMem(pStmt,i) );
300}
301const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
302 return sqlite3_value_text( columnMem(pStmt,i) );
303}
304const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
305 return sqlite3_value_text16( columnMem(pStmt,i) );
306}
307int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
308 return sqlite3_value_type( columnMem(pStmt,i) );
309}
310
311
312/*
313** Return the name of the Nth column of the result set returned by SQL
314** statement pStmt.
315*/
316const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
317 Vdbe *p = (Vdbe *)pStmt;
318 Mem *pColName;
319
320 if( N>=sqlite3_column_count(pStmt) || N<0 ){
321 sqlite3Error(p->db, SQLITE_RANGE, 0);
322 return 0;
323 }
324
325 pColName = &(p->aColName[N]);
326 return sqlite3_value_text(pColName);
327}
328
329/*
330** Return the name of the 'i'th column of the result set of SQL statement
331** pStmt, encoded as UTF-16.
332*/
333const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
334 Vdbe *p = (Vdbe *)pStmt;
335 Mem *pColName;
336
337 if( N>=sqlite3_column_count(pStmt) || N<0 ){
338 sqlite3Error(p->db, SQLITE_RANGE, 0);
339 return 0;
340 }
341
342 pColName = &(p->aColName[N]);
343 return sqlite3_value_text16(pColName);
344}
345
346
347/*
348** This routine returns either the column name, or declaration type (see
349** sqlite3_column_decltype16() ) of the 'i'th column of the result set of
350** SQL statement pStmt. The returned string is UTF-16 encoded.
351**
352** The declaration type is returned if 'decltype' is true, otherwise
353** the column name.
354*/
355static const void *columnName16(sqlite3_stmt *pStmt, int i, int decltype){
356 Vdbe *p = (Vdbe *)pStmt;
357
358 if( i>=sqlite3_column_count(pStmt) || i<0 ){
359 sqlite3Error(p->db, SQLITE_RANGE, 0);
360 return 0;
361 }
362
363 if( decltype ){
364 i += p->nResColumn;
365 }
366
367 if( !p->azColName16 ){
368 p->azColName16 = (void **)sqliteMalloc(sizeof(void *)*p->nResColumn*2);
369 if( !p->azColName16 ){
370 sqlite3Error(p->db, SQLITE_NOMEM, 0);
371 return 0;
372 }
373 }
374 if( !p->azColName16[i] ){
375 if( SQLITE3_BIGENDIAN ){
376 p->azColName16[i] = sqlite3utf8to16be(p->azColName[i], -1);
377 }
378 if( !p->azColName16[i] ){
379 sqlite3Error(p->db, SQLITE_NOMEM, 0);
380 return 0;
381 }
382 }
383 return p->azColName16[i];
384}
385
386/*
387** Return the column declaration type (if applicable) of the 'i'th column
388** of the result set of SQL statement pStmt, encoded as UTF-8.
389*/
390const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int i){
391 Vdbe *p = (Vdbe *)pStmt;
392
393 if( i>=sqlite3_column_count(pStmt) || i<0 ){
394 sqlite3Error(p->db, SQLITE_RANGE, 0);
395 return 0;
396 }
397
398 return p->azColName[i+p->nResColumn];
399}
400
401/*
402** Return the column declaration type (if applicable) of the 'i'th column
403** of the result set of SQL statement pStmt, encoded as UTF-16.
404*/
405const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int i){
406 return columnName16(pStmt, i, 1);
407}
408
409/******************************* sqlite3_bind_ ***************************
410**
411** Routines used to attach values to wildcards in a compiled SQL statement.
412*/
413/*
414** Unbind the value bound to variable i in virtual machine p. This is the
415** the same as binding a NULL value to the column. If the "i" parameter is
416** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
417**
418** The error code stored in database p->db is overwritten with the return
419** value in any case.
420*/
421static int vdbeUnbind(Vdbe *p, int i){
422 Mem *pVar;
423 if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){
424 sqlite3Error(p->db, SQLITE_MISUSE, 0);
425 return SQLITE_MISUSE;
426 }
427 if( i<1 || i>p->nVar ){
428 sqlite3Error(p->db, SQLITE_RANGE, 0);
429 return SQLITE_RANGE;
430 }
431 i--;
432 pVar = &p->apVar[i];
433 if( pVar->flags&MEM_Dyn ){
434 sqliteFree(pVar->z);
435 }
436 pVar->flags = MEM_Null;
437 sqlite3Error(p->db, SQLITE_OK, 0);
438 return SQLITE_OK;
439}
440
441/*
442** Bind a blob value to an SQL statement variable.
443*/
444int sqlite3_bind_blob(
445 sqlite3_stmt *p,
446 int i,
447 const void *zData,
448 int nData,
449 int eCopy
450){
451 Vdbe *p = (Vdbe *)pStmt;
452 Mem *pVar;
453 int rc;
454
455 rc = vdbeUnbind(p, i);
456 if( rc ){
457 return rc;
458 }
459 pVar = &p->apVar[i-1];
460 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy);
461 return rc;
462}
463int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
464 int rc;
465 Vdbe *p = (Vdbe *)pStmt;
466 Mem *pVar;
467 rc = vdbeUnbind(p, i);
468 if( rc==SQLITE_OK ){
469 sqlite3VdbeMemSetReal(&p->apVar[i-1], rValue);
470 }
471 return SQLITE_OK;
472}
473int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
474 return sqlite3_bind_int64(p, i, (long long int)iValue);
475}
476int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){
477 int rc;
478 Vdbe *p = (Vdbe *)pStmt;
479 rc = vdbeUnbind(p, i);
480 if( rc==SQLITE_OK ){
481 sqlite3VdbeMemSetInt(&p->apVar[i-1], iValue);
482 }
483 return rc;
484}
485int sqlite3_bind_null(sqlite3_stmt* p, int i){
486 return vdbeUnbind((Vdbe *)p, i);
487}
488int sqlite3_bind_text(
489 sqlite3_stmt *pStmt,
490 int i,
491 const char *zData,
492 int nData,
493 int eCopy
494){
495 Vdbe *p = (Vdbe *)pStmt;
496 Mem *pVar;
497 int rc;
498
499 rc = vdbeUnbind(p, i);
500 if( rc ){
501 return rc;
502 }
503 pVar = &p->apVar[i-1];
504 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, TEXT_Utf8, eCopy);
505 if( rc ){
506 return rc;
507 }
508 rc = sqlite3VdbeSetEncoding(pVar, p->db->enc);
509 return rc;
510}
511int sqlite3_bind_text16(
512 sqlite3_stmt *pStmt,
513 int i,
514 const void *zData,
515 int nData,
516 int eCopy
517){
518 Vdbe *p = (Vdbe *)pStmt;
519 Mem *pVar;
520 int rc;
521
522 rc = vdbeUnbind(p, i);
523 if( rc ){
524 return rc;
525 }
526 Mem *pVar = &p->apVar[i-1];
527
528 /* There may or may not be a byte order mark at the start of the UTF-16.
529 ** Either way set 'txt_enc' to the TEXT_Utf16* value indicating the
530 ** actual byte order used by this string. If the string does happen
531 ** to contain a BOM, then move zData so that it points to the first
532 ** byte after the BOM.
533 */
534 txt_enc = sqlite3UtfReadBom(zData, nData);
535 if( txt_enc ){
536 zData = (void *)(((u8 *)zData) + 2);
537 nData -= 2;
538 }else{
539 txt_enc = SQLITE3_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le;
540 }
541 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy);
542 if( rc ){
543 return rc;
544 }
545 rc = sqlite3VdbeSetEncoding(pVar, p->db->enc);
546 return rc;
547}