blob: 167178af34cb141bd7ed57d3c93215a69723a667 [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 }
171 if( p->explain ){
172 rc = sqlite3VdbeList(p);
173 }else{
174 rc = sqlite3VdbeExec(p);
175 }
176
177 if( sqlite3SafetyOff(db) ){
178 rc = SQLITE_MISUSE;
179 }
180
181 sqlite3Error(p->db, rc, p->zErrMsg);
182 return rc;
183}
184
185/*
drheb2e1762004-05-27 01:53:56 +0000186** Extract the user data from a sqlite3_context structure and return a
187** pointer to it.
188*/
189void *sqlite3_user_data(sqlite3_context *p){
190 assert( p && p->pFunc );
191 return p->pFunc->pUserData;
192}
193
194/*
195** Allocate or return the aggregate context for a user function. A new
196** context is allocated on the first call. Subsequent calls return the
197** same context that was returned on prior calls.
198**
199** This routine is defined here in vdbe.c because it depends on knowing
200** the internals of the sqlite3_context structure which is only defined in
201** this source file.
202*/
203void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
204 assert( p && p->pFunc && p->pFunc->xStep );
205 if( p->pAgg==0 ){
206 if( nByte<=NBFS ){
207 p->pAgg = (void*)p->s.z;
208 memset(p->pAgg, 0, nByte);
209 }else{
210 p->pAgg = sqliteMalloc( nByte );
211 }
212 }
213 return p->pAgg;
214}
215
216/*
217** Return the number of times the Step function of a aggregate has been
218** called.
219**
220** This routine is defined here in vdbe.c because it depends on knowing
221** the internals of the sqlite3_context structure which is only defined in
222** this source file.
223*/
224int sqlite3_aggregate_count(sqlite3_context *p){
225 assert( p && p->pFunc && p->pFunc->xStep );
226 return p->cnt;
227}
228
229/*
drh4f26d6c2004-05-26 23:25:30 +0000230** Return the number of columns in the result set for the statement pStmt.
231*/
232int sqlite3_column_count(sqlite3_stmt *pStmt){
233 Vdbe *pVm = (Vdbe *)pStmt;
234 return pVm->nResColumn;
235}
236
237/*
238** Return the number of values available from the current row of the
239** currently executing statement pStmt.
240*/
241int sqlite3_data_count(sqlite3_stmt *pStmt){
242 Vdbe *pVm = (Vdbe *)pStmt;
243 if( !pVm->resOnStack ) return 0;
244 return pVm->nResColumn;
245}
246
247
248/*
249** Check to see if column iCol of the given statement is valid. If
250** it is, return a pointer to the Mem for the value of that column.
251** If iCol is not valid, return a pointer to a Mem which has a value
252** of NULL.
253*/
254static Mem *columnMem(sqlite3_stmt *pStmt, int i){
255 Vdbe *pVm = (Vdbe *)pStmt;
256 int vals = sqlite3_data_count(pStmt);
257 if( i>=vals || i<0 ){
258 static Mem nullMem;
259 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
260 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
261 return &nullMem;
262 }
263 return &pVm->pTos[(1-vals)+i];
264}
265
266/**************************** sqlite3_column_ *******************************
267** The following routines are used to access elements of the current row
268** in the result set.
269*/
danielk1977c572ef72004-05-27 09:28:41 +0000270const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
271 return sqlite3_value_blob( columnMem(pStmt,i) );
272}
drh4f26d6c2004-05-26 23:25:30 +0000273int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
274 return sqlite3_value_bytes( columnMem(pStmt,i) );
275}
276int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
277 return sqlite3_value_bytes16( columnMem(pStmt,i) );
278}
279double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
280 return sqlite3_value_double( columnMem(pStmt,i) );
281}
282int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
283 return sqlite3_value_int( columnMem(pStmt,i) );
284}
285long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
286 return sqlite3_value_int64( columnMem(pStmt,i) );
287}
288const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
289 return sqlite3_value_text( columnMem(pStmt,i) );
290}
291const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
292 return sqlite3_value_text16( columnMem(pStmt,i) );
293}
294int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
295 return sqlite3_value_type( columnMem(pStmt,i) );
296}
297
298
299/*
300** Return the name of the Nth column of the result set returned by SQL
301** statement pStmt.
302*/
303const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
304 Vdbe *p = (Vdbe *)pStmt;
305 Mem *pColName;
306
307 if( N>=sqlite3_column_count(pStmt) || N<0 ){
308 sqlite3Error(p->db, SQLITE_RANGE, 0);
309 return 0;
310 }
311
312 pColName = &(p->aColName[N]);
313 return sqlite3_value_text(pColName);
314}
315
316/*
317** Return the name of the 'i'th column of the result set of SQL statement
318** pStmt, encoded as UTF-16.
319*/
320const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
321 Vdbe *p = (Vdbe *)pStmt;
322 Mem *pColName;
323
324 if( N>=sqlite3_column_count(pStmt) || N<0 ){
325 sqlite3Error(p->db, SQLITE_RANGE, 0);
326 return 0;
327 }
328
329 pColName = &(p->aColName[N]);
330 return sqlite3_value_text16(pColName);
331}
332
333
334/*
335** This routine returns either the column name, or declaration type (see
336** sqlite3_column_decltype16() ) of the 'i'th column of the result set of
337** SQL statement pStmt. The returned string is UTF-16 encoded.
338**
339** The declaration type is returned if 'decltype' is true, otherwise
340** the column name.
341*/
342static const void *columnName16(sqlite3_stmt *pStmt, int i, int decltype){
343 Vdbe *p = (Vdbe *)pStmt;
344
345 if( i>=sqlite3_column_count(pStmt) || i<0 ){
346 sqlite3Error(p->db, SQLITE_RANGE, 0);
347 return 0;
348 }
349
350 if( decltype ){
351 i += p->nResColumn;
352 }
353
354 if( !p->azColName16 ){
355 p->azColName16 = (void **)sqliteMalloc(sizeof(void *)*p->nResColumn*2);
356 if( !p->azColName16 ){
357 sqlite3Error(p->db, SQLITE_NOMEM, 0);
358 return 0;
359 }
360 }
361 if( !p->azColName16[i] ){
362 if( SQLITE3_BIGENDIAN ){
363 p->azColName16[i] = sqlite3utf8to16be(p->azColName[i], -1);
364 }
365 if( !p->azColName16[i] ){
366 sqlite3Error(p->db, SQLITE_NOMEM, 0);
367 return 0;
368 }
369 }
370 return p->azColName16[i];
371}
372
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*/
377const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int i){
378 Vdbe *p = (Vdbe *)pStmt;
379
380 if( i>=sqlite3_column_count(pStmt) || i<0 ){
381 sqlite3Error(p->db, SQLITE_RANGE, 0);
382 return 0;
383 }
384
385 return p->azColName[i+p->nResColumn];
386}
387
388/*
389** Return the column declaration type (if applicable) of the 'i'th column
390** of the result set of SQL statement pStmt, encoded as UTF-16.
391*/
392const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int i){
393 return columnName16(pStmt, i, 1);
394}
395
396/******************************* sqlite3_bind_ ***************************
397**
398** Routines used to attach values to wildcards in a compiled SQL statement.
399*/
400/*
401** Unbind the value bound to variable i in virtual machine p. This is the
402** the same as binding a NULL value to the column. If the "i" parameter is
403** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
404**
405** The error code stored in database p->db is overwritten with the return
406** value in any case.
407*/
408static int vdbeUnbind(Vdbe *p, int i){
409 Mem *pVar;
410 if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){
411 sqlite3Error(p->db, SQLITE_MISUSE, 0);
412 return SQLITE_MISUSE;
413 }
414 if( i<1 || i>p->nVar ){
415 sqlite3Error(p->db, SQLITE_RANGE, 0);
416 return SQLITE_RANGE;
417 }
418 i--;
419 pVar = &p->apVar[i];
420 if( pVar->flags&MEM_Dyn ){
421 sqliteFree(pVar->z);
422 }
423 pVar->flags = MEM_Null;
424 sqlite3Error(p->db, SQLITE_OK, 0);
425 return SQLITE_OK;
426}
427
428/*
429** Bind a blob value to an SQL statement variable.
430*/
431int sqlite3_bind_blob(
drhf4479502004-05-27 03:12:53 +0000432 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000433 int i,
434 const void *zData,
435 int nData,
436 int eCopy
437){
438 Vdbe *p = (Vdbe *)pStmt;
439 Mem *pVar;
440 int rc;
441
442 rc = vdbeUnbind(p, i);
443 if( rc ){
444 return rc;
445 }
446 pVar = &p->apVar[i-1];
447 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy);
448 return rc;
449}
450int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
451 int rc;
452 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000453 rc = vdbeUnbind(p, i);
454 if( rc==SQLITE_OK ){
drhf4479502004-05-27 03:12:53 +0000455 sqlite3VdbeMemSetDouble(&p->apVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000456 }
457 return SQLITE_OK;
458}
459int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
460 return sqlite3_bind_int64(p, i, (long long int)iValue);
461}
462int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){
463 int rc;
464 Vdbe *p = (Vdbe *)pStmt;
465 rc = vdbeUnbind(p, i);
466 if( rc==SQLITE_OK ){
drhf4479502004-05-27 03:12:53 +0000467 sqlite3VdbeMemSetInt64(&p->apVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000468 }
469 return rc;
470}
471int sqlite3_bind_null(sqlite3_stmt* p, int i){
472 return vdbeUnbind((Vdbe *)p, i);
473}
474int sqlite3_bind_text(
475 sqlite3_stmt *pStmt,
476 int i,
477 const char *zData,
478 int nData,
479 int eCopy
480){
481 Vdbe *p = (Vdbe *)pStmt;
482 Mem *pVar;
483 int rc;
484
485 rc = vdbeUnbind(p, i);
486 if( rc ){
487 return rc;
488 }
489 pVar = &p->apVar[i-1];
490 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, TEXT_Utf8, eCopy);
491 if( rc ){
492 return rc;
493 }
drhf4479502004-05-27 03:12:53 +0000494 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
drh4f26d6c2004-05-26 23:25:30 +0000495 return rc;
496}
497int sqlite3_bind_text16(
498 sqlite3_stmt *pStmt,
499 int i,
500 const void *zData,
501 int nData,
502 int eCopy
503){
504 Vdbe *p = (Vdbe *)pStmt;
505 Mem *pVar;
drhf4479502004-05-27 03:12:53 +0000506 int rc, txt_enc;
drh4f26d6c2004-05-26 23:25:30 +0000507
508 rc = vdbeUnbind(p, i);
509 if( rc ){
510 return rc;
511 }
drhf4479502004-05-27 03:12:53 +0000512 pVar = &p->apVar[i-1];
drh4f26d6c2004-05-26 23:25:30 +0000513
514 /* There may or may not be a byte order mark at the start of the UTF-16.
515 ** Either way set 'txt_enc' to the TEXT_Utf16* value indicating the
516 ** actual byte order used by this string. If the string does happen
517 ** to contain a BOM, then move zData so that it points to the first
518 ** byte after the BOM.
519 */
520 txt_enc = sqlite3UtfReadBom(zData, nData);
521 if( txt_enc ){
522 zData = (void *)(((u8 *)zData) + 2);
523 nData -= 2;
524 }else{
525 txt_enc = SQLITE3_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le;
526 }
527 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy);
528 if( rc ){
529 return rc;
530 }
drhf4479502004-05-27 03:12:53 +0000531 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
drh4f26d6c2004-05-26 23:25:30 +0000532 return rc;
533}