blob: 574738eae8dcd1a9c1d892b04227cf851ef565c6 [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
drhd89bd002005-01-22 03:03:54 +000019/*
20** Return TRUE (non-zero) of the statement supplied as an argument needs
21** to be recompiled. A statement needs to be recompiled whenever the
22** execution environment changes in a way that would alter the program
23** that sqlite3_prepare() generates. For example, if new functions or
24** collating sequences are registered or if an authorizer function is
25** added or changed.
26**
27***** EXPERIMENTAL ******
28*/
29int sqlite3_expired(sqlite3_stmt *pStmt){
30 Vdbe *p = (Vdbe*)pStmt;
31 return p==0 || p->expired;
32}
33
drh4f26d6c2004-05-26 23:25:30 +000034/**************************** sqlite3_value_ *******************************
35** The following routines extract information from a Mem or sqlite3_value
36** structure.
37*/
38const void *sqlite3_value_blob(sqlite3_value *pVal){
39 Mem *p = (Mem*)pVal;
40 if( p->flags & (MEM_Blob|MEM_Str) ){
41 return p->z;
42 }else{
43 return sqlite3_value_text(pVal);
44 }
45}
46int sqlite3_value_bytes(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000047 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000048}
49int sqlite3_value_bytes16(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000050 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000051}
52double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000053 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000054}
55int sqlite3_value_int(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000056 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000057}
drhefad9992004-06-22 12:13:55 +000058sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000059 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000060}
61const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000062 return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000063}
drh6c626082004-11-14 21:56:29 +000064#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +000065const void *sqlite3_value_text16(sqlite3_value* pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000066 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000067}
danielk1977d8123362004-06-12 09:25:12 +000068const void *sqlite3_value_text16be(sqlite3_value *pVal){
69 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
70}
71const void *sqlite3_value_text16le(sqlite3_value *pVal){
72 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
73}
drh6c626082004-11-14 21:56:29 +000074#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +000075int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000076 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000077}
78
79/**************************** sqlite3_result_ *******************************
80** The following routines are used by user-defined functions to specify
81** the function result.
82*/
83void sqlite3_result_blob(
84 sqlite3_context *pCtx,
85 const void *z,
86 int n,
danielk1977d8123362004-06-12 09:25:12 +000087 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +000088){
89 assert( n>0 );
danielk1977d8123362004-06-12 09:25:12 +000090 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +000091}
92void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
93 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
94}
95void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
96 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +000097 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +000098}
99void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
100 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +0000101 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000102}
drhf4479502004-05-27 03:12:53 +0000103void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +0000104 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
105}
106void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
107 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
108}
109void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +0000110 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000111}
112void sqlite3_result_text(
113 sqlite3_context *pCtx,
114 const char *z,
115 int n,
danielk1977d8123362004-06-12 09:25:12 +0000116 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000117){
danielk1977d8123362004-06-12 09:25:12 +0000118 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000119}
drh6c626082004-11-14 21:56:29 +0000120#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000121void sqlite3_result_text16(
122 sqlite3_context *pCtx,
123 const void *z,
124 int n,
danielk1977d8123362004-06-12 09:25:12 +0000125 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000126){
danielk1977d8123362004-06-12 09:25:12 +0000127 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
128}
129void sqlite3_result_text16be(
130 sqlite3_context *pCtx,
131 const void *z,
132 int n,
133 void (*xDel)(void *)
134){
135 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
136}
137void sqlite3_result_text16le(
138 sqlite3_context *pCtx,
139 const void *z,
140 int n,
141 void (*xDel)(void *)
142){
143 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000144}
drh6c626082004-11-14 21:56:29 +0000145#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000146void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
147 sqlite3VdbeMemCopy(&pCtx->s, pValue);
148}
149
150
151/*
152** Execute the statement pStmt, either until a row of data is ready, the
153** statement is completely executed or an error occurs.
154*/
155int sqlite3_step(sqlite3_stmt *pStmt){
156 Vdbe *p = (Vdbe*)pStmt;
drh9bb575f2004-09-06 17:24:11 +0000157 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000158 int rc;
159
drh1bcdb0c2004-08-28 14:49:46 +0000160 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
drh4f26d6c2004-05-26 23:25:30 +0000161 return SQLITE_MISUSE;
162 }
drh91b48aa2004-06-30 11:14:18 +0000163 if( p->aborted ){
164 return SQLITE_ABORT;
165 }
danielk1977a21c6b62005-01-24 10:25:59 +0000166 if( p->pc<=0 && p->expired ){
167 if( p->rc==SQLITE_OK ){
168 p->rc = SQLITE_SCHEMA;
169 }
170 return SQLITE_ERROR;
171 }
drh4f26d6c2004-05-26 23:25:30 +0000172 db = p->db;
173 if( sqlite3SafetyOn(db) ){
174 p->rc = SQLITE_MISUSE;
175 return SQLITE_MISUSE;
176 }
danielk19771d850a72004-05-31 08:26:49 +0000177 if( p->pc<0 ){
drhc16a03b2004-09-15 13:38:10 +0000178 /* Invoke the trace callback if there is one
179 */
180 if( (db = p->db)->xTrace && !db->init.busy ){
181 assert( p->nOp>0 );
182 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
183 assert( p->aOp[p->nOp-1].p3!=0 );
184 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
185 sqlite3SafetyOff(db);
186 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
187 if( sqlite3SafetyOn(db) ){
188 p->rc = SQLITE_MISUSE;
189 return SQLITE_MISUSE;
190 }
191 }
192
193 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
194 ** on in debugging mode.
195 */
196#ifdef SQLITE_DEBUG
197 if( (db->flags & SQLITE_SqlTrace)!=0 ){
198 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
199 }
200#endif /* SQLITE_DEBUG */
201
danielk19771d850a72004-05-31 08:26:49 +0000202 db->activeVdbeCnt++;
203 p->pc = 0;
204 }
drhb7f91642004-10-31 02:22:47 +0000205#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000206 if( p->explain ){
207 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000208 }else
209#endif /* SQLITE_OMIT_EXPLAIN */
210 {
drh4f26d6c2004-05-26 23:25:30 +0000211 rc = sqlite3VdbeExec(p);
212 }
213
214 if( sqlite3SafetyOff(db) ){
215 rc = SQLITE_MISUSE;
216 }
217
218 sqlite3Error(p->db, rc, p->zErrMsg);
219 return rc;
220}
221
222/*
drheb2e1762004-05-27 01:53:56 +0000223** Extract the user data from a sqlite3_context structure and return a
224** pointer to it.
225*/
226void *sqlite3_user_data(sqlite3_context *p){
227 assert( p && p->pFunc );
228 return p->pFunc->pUserData;
229}
230
231/*
232** Allocate or return the aggregate context for a user function. A new
233** context is allocated on the first call. Subsequent calls return the
234** same context that was returned on prior calls.
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*/
240void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
241 assert( p && p->pFunc && p->pFunc->xStep );
242 if( p->pAgg==0 ){
243 if( nByte<=NBFS ){
244 p->pAgg = (void*)p->s.z;
245 memset(p->pAgg, 0, nByte);
246 }else{
247 p->pAgg = sqliteMalloc( nByte );
248 }
249 }
250 return p->pAgg;
251}
252
253/*
danielk1977682f68b2004-06-05 10:22:17 +0000254** Return the auxilary data pointer, if any, for the iArg'th argument to
255** the user-function defined by pCtx.
256*/
257void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
258 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
259 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
260 return 0;
261 }
drhf92c7ff2004-06-19 15:40:23 +0000262 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000263}
264
265/*
266** Set the auxilary data pointer and delete function, for the iArg'th
267** argument to the user-function defined by pCtx. Any previous value is
268** deleted by calling the delete function specified when it was set.
269*/
270void sqlite3_set_auxdata(
271 sqlite3_context *pCtx,
272 int iArg,
273 void *pAux,
274 void (*xDelete)(void*)
275){
276 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000277 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000278 if( iArg<0 ) return;
279
drhf92c7ff2004-06-19 15:40:23 +0000280 pVdbeFunc = pCtx->pVdbeFunc;
281 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
282 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
283 pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000284 if( !pVdbeFunc ) return;
drh0e3d7472004-06-19 17:33:07 +0000285 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
286 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000287 pVdbeFunc->nAux = iArg+1;
288 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000289 }
290
drhf92c7ff2004-06-19 15:40:23 +0000291 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000292 if( pAuxData->pAux && pAuxData->xDelete ){
293 pAuxData->xDelete(pAuxData->pAux);
294 }
295 pAuxData->pAux = pAux;
296 pAuxData->xDelete = xDelete;
297}
298
299/*
drheb2e1762004-05-27 01:53:56 +0000300** Return the number of times the Step function of a aggregate has been
301** called.
302**
303** This routine is defined here in vdbe.c because it depends on knowing
304** the internals of the sqlite3_context structure which is only defined in
305** this source file.
306*/
307int sqlite3_aggregate_count(sqlite3_context *p){
308 assert( p && p->pFunc && p->pFunc->xStep );
309 return p->cnt;
310}
311
312/*
drh4f26d6c2004-05-26 23:25:30 +0000313** Return the number of columns in the result set for the statement pStmt.
314*/
315int sqlite3_column_count(sqlite3_stmt *pStmt){
316 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000317 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000318}
319
320/*
321** Return the number of values available from the current row of the
322** currently executing statement pStmt.
323*/
324int sqlite3_data_count(sqlite3_stmt *pStmt){
325 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000326 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000327 return pVm->nResColumn;
328}
329
330
331/*
332** Check to see if column iCol of the given statement is valid. If
333** it is, return a pointer to the Mem for the value of that column.
334** If iCol is not valid, return a pointer to a Mem which has a value
335** of NULL.
336*/
337static Mem *columnMem(sqlite3_stmt *pStmt, int i){
338 Vdbe *pVm = (Vdbe *)pStmt;
339 int vals = sqlite3_data_count(pStmt);
340 if( i>=vals || i<0 ){
341 static Mem nullMem;
342 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
343 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
344 return &nullMem;
345 }
346 return &pVm->pTos[(1-vals)+i];
347}
348
349/**************************** sqlite3_column_ *******************************
350** The following routines are used to access elements of the current row
351** in the result set.
352*/
danielk1977c572ef72004-05-27 09:28:41 +0000353const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
354 return sqlite3_value_blob( columnMem(pStmt,i) );
355}
drh4f26d6c2004-05-26 23:25:30 +0000356int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
357 return sqlite3_value_bytes( columnMem(pStmt,i) );
358}
359int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
360 return sqlite3_value_bytes16( columnMem(pStmt,i) );
361}
362double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
363 return sqlite3_value_double( columnMem(pStmt,i) );
364}
365int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
366 return sqlite3_value_int( columnMem(pStmt,i) );
367}
drhefad9992004-06-22 12:13:55 +0000368sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
drh4f26d6c2004-05-26 23:25:30 +0000369 return sqlite3_value_int64( columnMem(pStmt,i) );
370}
371const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
372 return sqlite3_value_text( columnMem(pStmt,i) );
373}
drh6c626082004-11-14 21:56:29 +0000374#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000375const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
376 return sqlite3_value_text16( columnMem(pStmt,i) );
377}
drh6c626082004-11-14 21:56:29 +0000378#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000379int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
380 return sqlite3_value_type( columnMem(pStmt,i) );
381}
382
drh5e2517e2004-09-24 23:59:12 +0000383/*
384** Convert the N-th element of pStmt->pColName[] into a string using
385** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000386**
387** There are up to 5 names for each column. useType determines which
388** name is returned. Here are the names:
389**
390** 0 The column name as it should be displayed for output
391** 1 The datatype name for the column
392** 2 The name of the database that the column derives from
393** 3 The name of the table that the column derives from
394** 4 The name of the table column that the result column derives from
395**
396** If the result is not a simple column reference (if it is an expression
397** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000398*/
399static const void *columnName(
400 sqlite3_stmt *pStmt,
401 int N,
402 const void *(*xFunc)(Mem*),
403 int useType
404){
405 Vdbe *p = (Vdbe *)pStmt;
406 int n = sqlite3_column_count(pStmt);
407
408 if( p==0 || N>=n || N<0 ){
409 return 0;
410 }
drhe590fbd2005-05-20 19:36:01 +0000411 N += useType*n;
drh5e2517e2004-09-24 23:59:12 +0000412 return xFunc(&p->aColName[N]);
413}
414
drh4f26d6c2004-05-26 23:25:30 +0000415
416/*
417** Return the name of the Nth column of the result set returned by SQL
418** statement pStmt.
419*/
420const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000421 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
drh4f26d6c2004-05-26 23:25:30 +0000422}
drhe590fbd2005-05-20 19:36:01 +0000423#ifndef SQLITE_OMIT_UTF16
424const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
425 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
426}
427#endif
drh4f26d6c2004-05-26 23:25:30 +0000428
429/*
drh6c626082004-11-14 21:56:29 +0000430** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000431** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000432*/
433const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
434 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
435}
drh6c626082004-11-14 21:56:29 +0000436#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000437const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000438 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
drh4f26d6c2004-05-26 23:25:30 +0000439}
drh6c626082004-11-14 21:56:29 +0000440#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000441
drhe590fbd2005-05-20 19:36:01 +0000442#if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0
443/*
444** Return the name of the database from which a result column derives.
445** NULL is returned if the result column is an expression or constant or
446** anything else which is not an unabiguous reference to a database column.
447*/
448const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
449 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2);
450}
451#ifndef SQLITE_OMIT_UTF16
452const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
453 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2);
454}
455#endif /* SQLITE_OMIT_UTF16 */
456
457/*
458** Return the name of the table from which a result column derives.
459** NULL is returned if the result column is an expression or constant or
460** anything else which is not an unabiguous reference to a database column.
461*/
462const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
463 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3);
464}
465#ifndef SQLITE_OMIT_UTF16
466const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
467 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3);
468}
469#endif /* SQLITE_OMIT_UTF16 */
470
471/*
472** Return the name of the table column from which a result column derives.
473** NULL is returned if the result column is an expression or constant or
474** anything else which is not an unabiguous reference to a database column.
475*/
476const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
477 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4);
478}
479#ifndef SQLITE_OMIT_UTF16
480const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
481 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4);
482}
483#endif /* SQLITE_OMIT_UTF16 */
484#endif /* SQLITE_OMIT_ORIGIN_NAMES */
485
486
487
488
drh4f26d6c2004-05-26 23:25:30 +0000489/******************************* sqlite3_bind_ ***************************
490**
491** Routines used to attach values to wildcards in a compiled SQL statement.
492*/
493/*
494** Unbind the value bound to variable i in virtual machine p. This is the
495** the same as binding a NULL value to the column. If the "i" parameter is
496** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
497**
498** The error code stored in database p->db is overwritten with the return
499** value in any case.
500*/
501static int vdbeUnbind(Vdbe *p, int i){
502 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000503 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh4f26d6c2004-05-26 23:25:30 +0000504 sqlite3Error(p->db, SQLITE_MISUSE, 0);
505 return SQLITE_MISUSE;
506 }
507 if( i<1 || i>p->nVar ){
508 sqlite3Error(p->db, SQLITE_RANGE, 0);
509 return SQLITE_RANGE;
510 }
511 i--;
drh290c1942004-08-21 17:54:45 +0000512 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000513 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000514 pVar->flags = MEM_Null;
515 sqlite3Error(p->db, SQLITE_OK, 0);
516 return SQLITE_OK;
517}
518
519/*
drh5e2517e2004-09-24 23:59:12 +0000520** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000521*/
drh5e2517e2004-09-24 23:59:12 +0000522static int bindText(
drhf4479502004-05-27 03:12:53 +0000523 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000524 int i,
525 const void *zData,
526 int nData,
drh5e2517e2004-09-24 23:59:12 +0000527 void (*xDel)(void*),
528 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000529){
530 Vdbe *p = (Vdbe *)pStmt;
531 Mem *pVar;
532 int rc;
533
534 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000535 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000536 return rc;
537 }
drh290c1942004-08-21 17:54:45 +0000538 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000539 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
540 if( rc ){
541 return rc;
542 }
543 if( rc==SQLITE_OK && encoding!=0 ){
544 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
545 }
drh4f26d6c2004-05-26 23:25:30 +0000546 return rc;
547}
drh5e2517e2004-09-24 23:59:12 +0000548
549
550/*
551** Bind a blob value to an SQL statement variable.
552*/
553int sqlite3_bind_blob(
554 sqlite3_stmt *pStmt,
555 int i,
556 const void *zData,
557 int nData,
558 void (*xDel)(void*)
559){
560 return bindText(pStmt, i, zData, nData, xDel, 0);
561}
drh4f26d6c2004-05-26 23:25:30 +0000562int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
563 int rc;
564 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000565 rc = vdbeUnbind(p, i);
566 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000567 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000568 }
danielk1977f4618892004-06-28 13:09:11 +0000569 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000570}
571int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000572 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000573}
drhefad9992004-06-22 12:13:55 +0000574int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000575 int rc;
576 Vdbe *p = (Vdbe *)pStmt;
577 rc = vdbeUnbind(p, i);
578 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000579 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000580 }
581 return rc;
582}
583int sqlite3_bind_null(sqlite3_stmt* p, int i){
584 return vdbeUnbind((Vdbe *)p, i);
585}
586int sqlite3_bind_text(
587 sqlite3_stmt *pStmt,
588 int i,
589 const char *zData,
590 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000591 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000592){
drh5e2517e2004-09-24 23:59:12 +0000593 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000594}
drh6c626082004-11-14 21:56:29 +0000595#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000596int sqlite3_bind_text16(
597 sqlite3_stmt *pStmt,
598 int i,
599 const void *zData,
600 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000601 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000602){
drh5e2517e2004-09-24 23:59:12 +0000603 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000604}
drh6c626082004-11-14 21:56:29 +0000605#endif /* SQLITE_OMIT_UTF16 */
drh75f6a032004-07-15 14:15:00 +0000606
607/*
608** Return the number of wildcards that can be potentially bound to.
609** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000610*/
611int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000612 Vdbe *p = (Vdbe*)pStmt;
613 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000614}
drh895d7472004-08-20 16:02:39 +0000615
616/*
drhfa6bc002004-09-07 16:19:52 +0000617** Create a mapping from variable numbers to variable names
618** in the Vdbe.azVar[] array, if such a mapping does not already
619** exist.
drh895d7472004-08-20 16:02:39 +0000620*/
drhfa6bc002004-09-07 16:19:52 +0000621static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000622 if( !p->okVar ){
623 int j;
624 Op *pOp;
625 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
626 if( pOp->opcode==OP_Variable ){
627 assert( pOp->p1>0 && pOp->p1<=p->nVar );
628 p->azVar[pOp->p1-1] = pOp->p3;
629 }
630 }
631 p->okVar = 1;
632 }
drhfa6bc002004-09-07 16:19:52 +0000633}
634
635/*
636** Return the name of a wildcard parameter. Return NULL if the index
637** is out of range or if the wildcard is unnamed.
638**
639** The result is always UTF-8.
640*/
641const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
642 Vdbe *p = (Vdbe*)pStmt;
643 if( p==0 || i<1 || i>p->nVar ){
644 return 0;
645 }
646 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000647 return p->azVar[i-1];
648}
drhfa6bc002004-09-07 16:19:52 +0000649
650/*
651** Given a wildcard parameter name, return the index of the variable
652** with that name. If there is no variable with the given name,
653** return 0.
654*/
655int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
656 Vdbe *p = (Vdbe*)pStmt;
657 int i;
658 if( p==0 ){
659 return 0;
660 }
661 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +0000662 if( zName ){
663 for(i=0; i<p->nVar; i++){
664 const char *z = p->azVar[i];
665 if( z && strcmp(z,zName)==0 ){
666 return i+1;
667 }
drhfa6bc002004-09-07 16:19:52 +0000668 }
669 }
670 return 0;
671}
drhf8db1bc2005-04-22 02:38:37 +0000672
673/* EXPERIMENTAL
674**
675** Transfer all bindings from the first statement over to the second.
676** If the two statements contain a different number of bindings, then
677** an SQLITE_ERROR is returned.
678*/
679int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
680 Vdbe *pFrom = (Vdbe*)pFromStmt;
681 Vdbe *pTo = (Vdbe*)pToStmt;
682 int i, rc = SQLITE_OK;
683 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
684 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
685 return SQLITE_MISUSE;
686 }
687 if( pFrom->nVar!=pTo->nVar ){
688 return SQLITE_ERROR;
689 }
690 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
691 rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
692 }
693 return rc;
694}