blob: 56388fd7153d052c8132cc482d7a002aaffe58e8 [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/*
221** Return the number of times the Step function of a aggregate has been
222** called.
223**
224** This routine is defined here in vdbe.c because it depends on knowing
225** the internals of the sqlite3_context structure which is only defined in
226** this source file.
227*/
228int sqlite3_aggregate_count(sqlite3_context *p){
229 assert( p && p->pFunc && p->pFunc->xStep );
230 return p->cnt;
231}
232
233/*
drh4f26d6c2004-05-26 23:25:30 +0000234** Return the number of columns in the result set for the statement pStmt.
235*/
236int sqlite3_column_count(sqlite3_stmt *pStmt){
237 Vdbe *pVm = (Vdbe *)pStmt;
238 return pVm->nResColumn;
239}
240
241/*
242** Return the number of values available from the current row of the
243** currently executing statement pStmt.
244*/
245int sqlite3_data_count(sqlite3_stmt *pStmt){
246 Vdbe *pVm = (Vdbe *)pStmt;
247 if( !pVm->resOnStack ) return 0;
248 return pVm->nResColumn;
249}
250
251
252/*
253** Check to see if column iCol of the given statement is valid. If
254** it is, return a pointer to the Mem for the value of that column.
255** If iCol is not valid, return a pointer to a Mem which has a value
256** of NULL.
257*/
258static Mem *columnMem(sqlite3_stmt *pStmt, int i){
259 Vdbe *pVm = (Vdbe *)pStmt;
260 int vals = sqlite3_data_count(pStmt);
261 if( i>=vals || i<0 ){
262 static Mem nullMem;
263 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
264 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
265 return &nullMem;
266 }
267 return &pVm->pTos[(1-vals)+i];
268}
269
270/**************************** sqlite3_column_ *******************************
271** The following routines are used to access elements of the current row
272** in the result set.
273*/
danielk1977c572ef72004-05-27 09:28:41 +0000274const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
275 return sqlite3_value_blob( columnMem(pStmt,i) );
276}
drh4f26d6c2004-05-26 23:25:30 +0000277int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
278 return sqlite3_value_bytes( columnMem(pStmt,i) );
279}
280int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
281 return sqlite3_value_bytes16( columnMem(pStmt,i) );
282}
283double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
284 return sqlite3_value_double( columnMem(pStmt,i) );
285}
286int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
287 return sqlite3_value_int( columnMem(pStmt,i) );
288}
289long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
290 return sqlite3_value_int64( columnMem(pStmt,i) );
291}
292const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
293 return sqlite3_value_text( columnMem(pStmt,i) );
294}
295const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
296 return sqlite3_value_text16( columnMem(pStmt,i) );
297}
298int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
299 return sqlite3_value_type( columnMem(pStmt,i) );
300}
301
302
303/*
304** Return the name of the Nth column of the result set returned by SQL
305** statement pStmt.
306*/
307const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
308 Vdbe *p = (Vdbe *)pStmt;
309 Mem *pColName;
310
311 if( N>=sqlite3_column_count(pStmt) || N<0 ){
312 sqlite3Error(p->db, SQLITE_RANGE, 0);
313 return 0;
314 }
315
316 pColName = &(p->aColName[N]);
317 return sqlite3_value_text(pColName);
318}
319
320/*
321** Return the name of the 'i'th column of the result set of SQL statement
322** pStmt, encoded as UTF-16.
323*/
324const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
325 Vdbe *p = (Vdbe *)pStmt;
326 Mem *pColName;
327
328 if( N>=sqlite3_column_count(pStmt) || N<0 ){
329 sqlite3Error(p->db, SQLITE_RANGE, 0);
330 return 0;
331 }
332
333 pColName = &(p->aColName[N]);
334 return sqlite3_value_text16(pColName);
335}
336
drh4f26d6c2004-05-26 23:25:30 +0000337/*
338** Return the column declaration type (if applicable) of the 'i'th column
339** of the result set of SQL statement pStmt, encoded as UTF-8.
340*/
danielk197776d505b2004-05-28 13:13:02 +0000341const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
drh4f26d6c2004-05-26 23:25:30 +0000342 Vdbe *p = (Vdbe *)pStmt;
danielk197776d505b2004-05-28 13:13:02 +0000343 Mem *pColName;
drh4f26d6c2004-05-26 23:25:30 +0000344
danielk197776d505b2004-05-28 13:13:02 +0000345 if( N>=sqlite3_column_count(pStmt) || N<0 ){
drh4f26d6c2004-05-26 23:25:30 +0000346 sqlite3Error(p->db, SQLITE_RANGE, 0);
347 return 0;
348 }
349
danielk197776d505b2004-05-28 13:13:02 +0000350 pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]);
351 return sqlite3_value_text(pColName);
drh4f26d6c2004-05-26 23:25:30 +0000352}
353
354/*
355** Return the column declaration type (if applicable) of the 'i'th column
356** of the result set of SQL statement pStmt, encoded as UTF-16.
357*/
danielk197776d505b2004-05-28 13:13:02 +0000358const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
359 Vdbe *p = (Vdbe *)pStmt;
360 Mem *pColName;
361
362 if( N>=sqlite3_column_count(pStmt) || N<0 ){
363 sqlite3Error(p->db, SQLITE_RANGE, 0);
364 return 0;
365 }
366
367 pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]);
368 return sqlite3_value_text16(pColName);
drh4f26d6c2004-05-26 23:25:30 +0000369}
370
371/******************************* sqlite3_bind_ ***************************
372**
373** Routines used to attach values to wildcards in a compiled SQL statement.
374*/
375/*
376** Unbind the value bound to variable i in virtual machine p. This is the
377** the same as binding a NULL value to the column. If the "i" parameter is
378** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
379**
380** The error code stored in database p->db is overwritten with the return
381** value in any case.
382*/
383static int vdbeUnbind(Vdbe *p, int i){
384 Mem *pVar;
danielk19771d850a72004-05-31 08:26:49 +0000385 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000386 sqlite3Error(p->db, SQLITE_MISUSE, 0);
387 return SQLITE_MISUSE;
388 }
389 if( i<1 || i>p->nVar ){
390 sqlite3Error(p->db, SQLITE_RANGE, 0);
391 return SQLITE_RANGE;
392 }
393 i--;
394 pVar = &p->apVar[i];
395 if( pVar->flags&MEM_Dyn ){
396 sqliteFree(pVar->z);
397 }
398 pVar->flags = MEM_Null;
399 sqlite3Error(p->db, SQLITE_OK, 0);
400 return SQLITE_OK;
401}
402
403/*
404** Bind a blob value to an SQL statement variable.
405*/
406int sqlite3_bind_blob(
drhf4479502004-05-27 03:12:53 +0000407 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000408 int i,
409 const void *zData,
410 int nData,
411 int eCopy
412){
413 Vdbe *p = (Vdbe *)pStmt;
414 Mem *pVar;
415 int rc;
416
417 rc = vdbeUnbind(p, i);
418 if( rc ){
419 return rc;
420 }
421 pVar = &p->apVar[i-1];
422 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy);
423 return rc;
424}
425int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
426 int rc;
427 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000428 rc = vdbeUnbind(p, i);
429 if( rc==SQLITE_OK ){
drhf4479502004-05-27 03:12:53 +0000430 sqlite3VdbeMemSetDouble(&p->apVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000431 }
432 return SQLITE_OK;
433}
434int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
435 return sqlite3_bind_int64(p, i, (long long int)iValue);
436}
437int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){
438 int rc;
439 Vdbe *p = (Vdbe *)pStmt;
440 rc = vdbeUnbind(p, i);
441 if( rc==SQLITE_OK ){
drhf4479502004-05-27 03:12:53 +0000442 sqlite3VdbeMemSetInt64(&p->apVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000443 }
444 return rc;
445}
446int sqlite3_bind_null(sqlite3_stmt* p, int i){
447 return vdbeUnbind((Vdbe *)p, i);
448}
449int sqlite3_bind_text(
450 sqlite3_stmt *pStmt,
451 int i,
452 const char *zData,
453 int nData,
454 int eCopy
455){
456 Vdbe *p = (Vdbe *)pStmt;
457 Mem *pVar;
458 int rc;
459
460 rc = vdbeUnbind(p, i);
461 if( rc ){
462 return rc;
463 }
464 pVar = &p->apVar[i-1];
465 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, TEXT_Utf8, eCopy);
466 if( rc ){
467 return rc;
468 }
drhf4479502004-05-27 03:12:53 +0000469 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
drh4f26d6c2004-05-26 23:25:30 +0000470 return rc;
471}
472int sqlite3_bind_text16(
473 sqlite3_stmt *pStmt,
474 int i,
475 const void *zData,
476 int nData,
477 int eCopy
478){
479 Vdbe *p = (Vdbe *)pStmt;
480 Mem *pVar;
drhf4479502004-05-27 03:12:53 +0000481 int rc, txt_enc;
drh4f26d6c2004-05-26 23:25:30 +0000482
483 rc = vdbeUnbind(p, i);
484 if( rc ){
485 return rc;
486 }
drhf4479502004-05-27 03:12:53 +0000487 pVar = &p->apVar[i-1];
drh4f26d6c2004-05-26 23:25:30 +0000488
489 /* There may or may not be a byte order mark at the start of the UTF-16.
490 ** Either way set 'txt_enc' to the TEXT_Utf16* value indicating the
491 ** actual byte order used by this string. If the string does happen
492 ** to contain a BOM, then move zData so that it points to the first
493 ** byte after the BOM.
494 */
495 txt_enc = sqlite3UtfReadBom(zData, nData);
496 if( txt_enc ){
497 zData = (void *)(((u8 *)zData) + 2);
498 nData -= 2;
499 }else{
500 txt_enc = SQLITE3_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le;
501 }
502 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy);
503 if( rc ){
504 return rc;
505 }
drhf4479502004-05-27 03:12:53 +0000506 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
drh4f26d6c2004-05-26 23:25:30 +0000507 return rc;
508}