blob: f07bc889924be2fc43caffd72f95206f903db3d8 [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.
drhd89bd002005-01-22 03:03:54 +000026*/
27int sqlite3_expired(sqlite3_stmt *pStmt){
28 Vdbe *p = (Vdbe*)pStmt;
29 return p==0 || p->expired;
30}
31
drh4f26d6c2004-05-26 23:25:30 +000032/**************************** sqlite3_value_ *******************************
33** The following routines extract information from a Mem or sqlite3_value
34** structure.
35*/
36const void *sqlite3_value_blob(sqlite3_value *pVal){
37 Mem *p = (Mem*)pVal;
38 if( p->flags & (MEM_Blob|MEM_Str) ){
39 return p->z;
40 }else{
41 return sqlite3_value_text(pVal);
42 }
43}
44int sqlite3_value_bytes(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000045 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000046}
47int sqlite3_value_bytes16(sqlite3_value *pVal){
danielk197713073932004-06-30 11:54:06 +000048 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000049}
50double sqlite3_value_double(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000051 return sqlite3VdbeRealValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000052}
53int sqlite3_value_int(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000054 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000055}
drhefad9992004-06-22 12:13:55 +000056sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
drh6a6124e2004-06-27 01:56:33 +000057 return sqlite3VdbeIntValue((Mem*)pVal);
drh4f26d6c2004-05-26 23:25:30 +000058}
59const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000060 return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +000061}
drh6c626082004-11-14 21:56:29 +000062#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +000063const void *sqlite3_value_text16(sqlite3_value* pVal){
danielk1977dc8453f2004-06-12 00:42:34 +000064 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +000065}
danielk1977d8123362004-06-12 09:25:12 +000066const void *sqlite3_value_text16be(sqlite3_value *pVal){
67 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
68}
69const void *sqlite3_value_text16le(sqlite3_value *pVal){
70 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
71}
drh6c626082004-11-14 21:56:29 +000072#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +000073int sqlite3_value_type(sqlite3_value* pVal){
drhf4479502004-05-27 03:12:53 +000074 return pVal->type;
drh4f26d6c2004-05-26 23:25:30 +000075}
76
77/**************************** sqlite3_result_ *******************************
78** The following routines are used by user-defined functions to specify
79** the function result.
80*/
81void sqlite3_result_blob(
82 sqlite3_context *pCtx,
83 const void *z,
84 int n,
danielk1977d8123362004-06-12 09:25:12 +000085 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +000086){
87 assert( n>0 );
danielk1977d8123362004-06-12 09:25:12 +000088 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
drh4f26d6c2004-05-26 23:25:30 +000089}
90void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
91 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
92}
93void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
94 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +000095 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +000096}
97void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
98 pCtx->isError = 1;
danielk1977d8123362004-06-12 09:25:12 +000099 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
drh4f26d6c2004-05-26 23:25:30 +0000100}
drhf4479502004-05-27 03:12:53 +0000101void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
drh4f26d6c2004-05-26 23:25:30 +0000102 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
103}
104void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
105 sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
106}
107void sqlite3_result_null(sqlite3_context *pCtx){
drhf4479502004-05-27 03:12:53 +0000108 sqlite3VdbeMemSetNull(&pCtx->s);
drh4f26d6c2004-05-26 23:25:30 +0000109}
110void sqlite3_result_text(
111 sqlite3_context *pCtx,
112 const char *z,
113 int n,
danielk1977d8123362004-06-12 09:25:12 +0000114 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000115){
danielk1977d8123362004-06-12 09:25:12 +0000116 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000117}
drh6c626082004-11-14 21:56:29 +0000118#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000119void sqlite3_result_text16(
120 sqlite3_context *pCtx,
121 const void *z,
122 int n,
danielk1977d8123362004-06-12 09:25:12 +0000123 void (*xDel)(void *)
drh4f26d6c2004-05-26 23:25:30 +0000124){
danielk1977d8123362004-06-12 09:25:12 +0000125 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
126}
127void sqlite3_result_text16be(
128 sqlite3_context *pCtx,
129 const void *z,
130 int n,
131 void (*xDel)(void *)
132){
133 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
134}
135void sqlite3_result_text16le(
136 sqlite3_context *pCtx,
137 const void *z,
138 int n,
139 void (*xDel)(void *)
140){
141 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
drh4f26d6c2004-05-26 23:25:30 +0000142}
drh6c626082004-11-14 21:56:29 +0000143#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000144void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
145 sqlite3VdbeMemCopy(&pCtx->s, pValue);
146}
147
148
149/*
150** Execute the statement pStmt, either until a row of data is ready, the
151** statement is completely executed or an error occurs.
152*/
153int sqlite3_step(sqlite3_stmt *pStmt){
154 Vdbe *p = (Vdbe*)pStmt;
drh9bb575f2004-09-06 17:24:11 +0000155 sqlite3 *db;
drh4f26d6c2004-05-26 23:25:30 +0000156 int rc;
157
drh1bcdb0c2004-08-28 14:49:46 +0000158 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
drh4f26d6c2004-05-26 23:25:30 +0000159 return SQLITE_MISUSE;
160 }
drh91b48aa2004-06-30 11:14:18 +0000161 if( p->aborted ){
162 return SQLITE_ABORT;
163 }
danielk1977a21c6b62005-01-24 10:25:59 +0000164 if( p->pc<=0 && p->expired ){
165 if( p->rc==SQLITE_OK ){
166 p->rc = SQLITE_SCHEMA;
167 }
168 return SQLITE_ERROR;
169 }
drh4f26d6c2004-05-26 23:25:30 +0000170 db = p->db;
171 if( sqlite3SafetyOn(db) ){
172 p->rc = SQLITE_MISUSE;
173 return SQLITE_MISUSE;
174 }
danielk19771d850a72004-05-31 08:26:49 +0000175 if( p->pc<0 ){
drhc16a03b2004-09-15 13:38:10 +0000176 /* Invoke the trace callback if there is one
177 */
178 if( (db = p->db)->xTrace && !db->init.busy ){
179 assert( p->nOp>0 );
180 assert( p->aOp[p->nOp-1].opcode==OP_Noop );
181 assert( p->aOp[p->nOp-1].p3!=0 );
182 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC );
183 sqlite3SafetyOff(db);
184 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3);
185 if( sqlite3SafetyOn(db) ){
186 p->rc = SQLITE_MISUSE;
187 return SQLITE_MISUSE;
188 }
189 }
190
191 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
192 ** on in debugging mode.
193 */
194#ifdef SQLITE_DEBUG
195 if( (db->flags & SQLITE_SqlTrace)!=0 ){
196 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3);
197 }
198#endif /* SQLITE_DEBUG */
199
danielk19771d850a72004-05-31 08:26:49 +0000200 db->activeVdbeCnt++;
201 p->pc = 0;
202 }
drhb7f91642004-10-31 02:22:47 +0000203#ifndef SQLITE_OMIT_EXPLAIN
drh4f26d6c2004-05-26 23:25:30 +0000204 if( p->explain ){
205 rc = sqlite3VdbeList(p);
drhb7f91642004-10-31 02:22:47 +0000206 }else
207#endif /* SQLITE_OMIT_EXPLAIN */
208 {
drh4f26d6c2004-05-26 23:25:30 +0000209 rc = sqlite3VdbeExec(p);
210 }
211
212 if( sqlite3SafetyOff(db) ){
213 rc = SQLITE_MISUSE;
214 }
215
216 sqlite3Error(p->db, rc, p->zErrMsg);
217 return rc;
218}
219
220/*
drheb2e1762004-05-27 01:53:56 +0000221** Extract the user data from a sqlite3_context structure and return a
222** pointer to it.
223*/
224void *sqlite3_user_data(sqlite3_context *p){
225 assert( p && p->pFunc );
226 return p->pFunc->pUserData;
227}
228
229/*
230** Allocate or return the aggregate context for a user function. A new
231** context is allocated on the first call. Subsequent calls return the
232** same context that was returned on prior calls.
233**
234** This routine is defined here in vdbe.c because it depends on knowing
235** the internals of the sqlite3_context structure which is only defined in
236** this source file.
237*/
238void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
239 assert( p && p->pFunc && p->pFunc->xStep );
240 if( p->pAgg==0 ){
241 if( nByte<=NBFS ){
242 p->pAgg = (void*)p->s.z;
243 memset(p->pAgg, 0, nByte);
244 }else{
245 p->pAgg = sqliteMalloc( nByte );
246 }
247 }
248 return p->pAgg;
249}
250
251/*
danielk1977682f68b2004-06-05 10:22:17 +0000252** Return the auxilary data pointer, if any, for the iArg'th argument to
253** the user-function defined by pCtx.
254*/
255void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
256 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
257 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
258 return 0;
259 }
drhf92c7ff2004-06-19 15:40:23 +0000260 return pVdbeFunc->apAux[iArg].pAux;
danielk1977682f68b2004-06-05 10:22:17 +0000261}
262
263/*
264** Set the auxilary data pointer and delete function, for the iArg'th
265** argument to the user-function defined by pCtx. Any previous value is
266** deleted by calling the delete function specified when it was set.
267*/
268void sqlite3_set_auxdata(
269 sqlite3_context *pCtx,
270 int iArg,
271 void *pAux,
272 void (*xDelete)(void*)
273){
274 struct AuxData *pAuxData;
drhf92c7ff2004-06-19 15:40:23 +0000275 VdbeFunc *pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000276 if( iArg<0 ) return;
277
drhf92c7ff2004-06-19 15:40:23 +0000278 pVdbeFunc = pCtx->pVdbeFunc;
279 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
280 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
281 pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
drh998da3a2004-06-19 15:22:56 +0000282 if( !pVdbeFunc ) return;
drh0e3d7472004-06-19 17:33:07 +0000283 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
284 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
drh998da3a2004-06-19 15:22:56 +0000285 pVdbeFunc->nAux = iArg+1;
286 pVdbeFunc->pFunc = pCtx->pFunc;
danielk1977682f68b2004-06-05 10:22:17 +0000287 }
288
drhf92c7ff2004-06-19 15:40:23 +0000289 pAuxData = &pVdbeFunc->apAux[iArg];
danielk1977682f68b2004-06-05 10:22:17 +0000290 if( pAuxData->pAux && pAuxData->xDelete ){
291 pAuxData->xDelete(pAuxData->pAux);
292 }
293 pAuxData->pAux = pAux;
294 pAuxData->xDelete = xDelete;
295}
296
297/*
drheb2e1762004-05-27 01:53:56 +0000298** Return the number of times the Step function of a aggregate has been
299** called.
300**
301** This routine is defined here in vdbe.c because it depends on knowing
302** the internals of the sqlite3_context structure which is only defined in
303** this source file.
304*/
305int sqlite3_aggregate_count(sqlite3_context *p){
306 assert( p && p->pFunc && p->pFunc->xStep );
307 return p->cnt;
308}
309
310/*
drh4f26d6c2004-05-26 23:25:30 +0000311** Return the number of columns in the result set for the statement pStmt.
312*/
313int sqlite3_column_count(sqlite3_stmt *pStmt){
314 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000315 return pVm ? pVm->nResColumn : 0;
drh4f26d6c2004-05-26 23:25:30 +0000316}
317
318/*
319** Return the number of values available from the current row of the
320** currently executing statement pStmt.
321*/
322int sqlite3_data_count(sqlite3_stmt *pStmt){
323 Vdbe *pVm = (Vdbe *)pStmt;
drh1bcdb0c2004-08-28 14:49:46 +0000324 if( pVm==0 || !pVm->resOnStack ) return 0;
drh4f26d6c2004-05-26 23:25:30 +0000325 return pVm->nResColumn;
326}
327
328
329/*
330** Check to see if column iCol of the given statement is valid. If
331** it is, return a pointer to the Mem for the value of that column.
332** If iCol is not valid, return a pointer to a Mem which has a value
333** of NULL.
334*/
335static Mem *columnMem(sqlite3_stmt *pStmt, int i){
336 Vdbe *pVm = (Vdbe *)pStmt;
337 int vals = sqlite3_data_count(pStmt);
338 if( i>=vals || i<0 ){
339 static Mem nullMem;
340 if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; }
341 sqlite3Error(pVm->db, SQLITE_RANGE, 0);
342 return &nullMem;
343 }
344 return &pVm->pTos[(1-vals)+i];
345}
346
347/**************************** sqlite3_column_ *******************************
348** The following routines are used to access elements of the current row
349** in the result set.
350*/
danielk1977c572ef72004-05-27 09:28:41 +0000351const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
352 return sqlite3_value_blob( columnMem(pStmt,i) );
353}
drh4f26d6c2004-05-26 23:25:30 +0000354int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
355 return sqlite3_value_bytes( columnMem(pStmt,i) );
356}
357int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
358 return sqlite3_value_bytes16( columnMem(pStmt,i) );
359}
360double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
361 return sqlite3_value_double( columnMem(pStmt,i) );
362}
363int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
364 return sqlite3_value_int( columnMem(pStmt,i) );
365}
drhefad9992004-06-22 12:13:55 +0000366sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
drh4f26d6c2004-05-26 23:25:30 +0000367 return sqlite3_value_int64( columnMem(pStmt,i) );
368}
369const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
370 return sqlite3_value_text( columnMem(pStmt,i) );
371}
drh6c626082004-11-14 21:56:29 +0000372#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000373const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
374 return sqlite3_value_text16( columnMem(pStmt,i) );
375}
drh6c626082004-11-14 21:56:29 +0000376#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000377int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
378 return sqlite3_value_type( columnMem(pStmt,i) );
379}
380
drh5e2517e2004-09-24 23:59:12 +0000381/*
382** Convert the N-th element of pStmt->pColName[] into a string using
383** xFunc() then return that string. If N is out of range, return 0.
drhe590fbd2005-05-20 19:36:01 +0000384**
385** There are up to 5 names for each column. useType determines which
386** name is returned. Here are the names:
387**
388** 0 The column name as it should be displayed for output
389** 1 The datatype name for the column
390** 2 The name of the database that the column derives from
391** 3 The name of the table that the column derives from
392** 4 The name of the table column that the result column derives from
393**
394** If the result is not a simple column reference (if it is an expression
395** or a constant) then useTypes 2, 3, and 4 return NULL.
drh5e2517e2004-09-24 23:59:12 +0000396*/
397static const void *columnName(
398 sqlite3_stmt *pStmt,
399 int N,
400 const void *(*xFunc)(Mem*),
401 int useType
402){
403 Vdbe *p = (Vdbe *)pStmt;
404 int n = sqlite3_column_count(pStmt);
405
406 if( p==0 || N>=n || N<0 ){
407 return 0;
408 }
drhe590fbd2005-05-20 19:36:01 +0000409 N += useType*n;
drh5e2517e2004-09-24 23:59:12 +0000410 return xFunc(&p->aColName[N]);
411}
412
drh4f26d6c2004-05-26 23:25:30 +0000413
414/*
415** Return the name of the Nth column of the result set returned by SQL
416** statement pStmt.
417*/
418const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000419 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0);
drh4f26d6c2004-05-26 23:25:30 +0000420}
drhe590fbd2005-05-20 19:36:01 +0000421#ifndef SQLITE_OMIT_UTF16
422const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
423 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0);
424}
425#endif
drh4f26d6c2004-05-26 23:25:30 +0000426
427/*
drh6c626082004-11-14 21:56:29 +0000428** Return the column declaration type (if applicable) of the 'i'th column
drhe590fbd2005-05-20 19:36:01 +0000429** of the result set of SQL statement pStmt.
drh6c626082004-11-14 21:56:29 +0000430*/
431const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
432 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1);
433}
drh6c626082004-11-14 21:56:29 +0000434#ifndef SQLITE_OMIT_UTF16
danielk197776d505b2004-05-28 13:13:02 +0000435const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
drh5e2517e2004-09-24 23:59:12 +0000436 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1);
drh4f26d6c2004-05-26 23:25:30 +0000437}
drh6c626082004-11-14 21:56:29 +0000438#endif /* SQLITE_OMIT_UTF16 */
drh4f26d6c2004-05-26 23:25:30 +0000439
drhe590fbd2005-05-20 19:36:01 +0000440#if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0
441/*
442** Return the name of the database from which a result column derives.
443** NULL is returned if the result column is an expression or constant or
444** anything else which is not an unabiguous reference to a database column.
445*/
446const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
447 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2);
448}
449#ifndef SQLITE_OMIT_UTF16
450const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
451 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2);
452}
453#endif /* SQLITE_OMIT_UTF16 */
454
455/*
456** Return the name of the table from which a result column derives.
457** NULL is returned if the result column is an expression or constant or
458** anything else which is not an unabiguous reference to a database column.
459*/
460const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
461 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3);
462}
463#ifndef SQLITE_OMIT_UTF16
464const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
465 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3);
466}
467#endif /* SQLITE_OMIT_UTF16 */
468
469/*
470** Return the name of the table column from which a result column derives.
471** NULL is returned if the result column is an expression or constant or
472** anything else which is not an unabiguous reference to a database column.
473*/
474const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
475 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4);
476}
477#ifndef SQLITE_OMIT_UTF16
478const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
479 return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4);
480}
481#endif /* SQLITE_OMIT_UTF16 */
482#endif /* SQLITE_OMIT_ORIGIN_NAMES */
483
484
485
486
drh4f26d6c2004-05-26 23:25:30 +0000487/******************************* sqlite3_bind_ ***************************
488**
489** Routines used to attach values to wildcards in a compiled SQL statement.
490*/
491/*
492** Unbind the value bound to variable i in virtual machine p. This is the
493** the same as binding a NULL value to the column. If the "i" parameter is
494** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
495**
496** The error code stored in database p->db is overwritten with the return
497** value in any case.
498*/
499static int vdbeUnbind(Vdbe *p, int i){
500 Mem *pVar;
drh1bcdb0c2004-08-28 14:49:46 +0000501 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
drh473d1792005-06-06 17:54:55 +0000502 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0);
drh4f26d6c2004-05-26 23:25:30 +0000503 return SQLITE_MISUSE;
504 }
505 if( i<1 || i>p->nVar ){
506 sqlite3Error(p->db, SQLITE_RANGE, 0);
507 return SQLITE_RANGE;
508 }
509 i--;
drh290c1942004-08-21 17:54:45 +0000510 pVar = &p->aVar[i];
danielk1977d8123362004-06-12 09:25:12 +0000511 sqlite3VdbeMemRelease(pVar);
drh4f26d6c2004-05-26 23:25:30 +0000512 pVar->flags = MEM_Null;
513 sqlite3Error(p->db, SQLITE_OK, 0);
514 return SQLITE_OK;
515}
516
517/*
drh5e2517e2004-09-24 23:59:12 +0000518** Bind a text or BLOB value.
drh4f26d6c2004-05-26 23:25:30 +0000519*/
drh5e2517e2004-09-24 23:59:12 +0000520static int bindText(
drhf4479502004-05-27 03:12:53 +0000521 sqlite3_stmt *pStmt,
drh4f26d6c2004-05-26 23:25:30 +0000522 int i,
523 const void *zData,
524 int nData,
drh5e2517e2004-09-24 23:59:12 +0000525 void (*xDel)(void*),
526 int encoding
drh4f26d6c2004-05-26 23:25:30 +0000527){
528 Vdbe *p = (Vdbe *)pStmt;
529 Mem *pVar;
530 int rc;
531
532 rc = vdbeUnbind(p, i);
drhfeac5f82004-08-01 00:10:45 +0000533 if( rc || zData==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000534 return rc;
535 }
drh290c1942004-08-21 17:54:45 +0000536 pVar = &p->aVar[i-1];
drh5e2517e2004-09-24 23:59:12 +0000537 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
538 if( rc ){
539 return rc;
540 }
541 if( rc==SQLITE_OK && encoding!=0 ){
542 rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc);
543 }
drh4f26d6c2004-05-26 23:25:30 +0000544 return rc;
545}
drh5e2517e2004-09-24 23:59:12 +0000546
547
548/*
549** Bind a blob value to an SQL statement variable.
550*/
551int sqlite3_bind_blob(
552 sqlite3_stmt *pStmt,
553 int i,
554 const void *zData,
555 int nData,
556 void (*xDel)(void*)
557){
558 return bindText(pStmt, i, zData, nData, xDel, 0);
559}
drh4f26d6c2004-05-26 23:25:30 +0000560int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
561 int rc;
562 Vdbe *p = (Vdbe *)pStmt;
drh4f26d6c2004-05-26 23:25:30 +0000563 rc = vdbeUnbind(p, i);
564 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000565 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
drh4f26d6c2004-05-26 23:25:30 +0000566 }
danielk1977f4618892004-06-28 13:09:11 +0000567 return rc;
drh4f26d6c2004-05-26 23:25:30 +0000568}
569int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
drhefad9992004-06-22 12:13:55 +0000570 return sqlite3_bind_int64(p, i, (i64)iValue);
drh4f26d6c2004-05-26 23:25:30 +0000571}
drhefad9992004-06-22 12:13:55 +0000572int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
drh4f26d6c2004-05-26 23:25:30 +0000573 int rc;
574 Vdbe *p = (Vdbe *)pStmt;
575 rc = vdbeUnbind(p, i);
576 if( rc==SQLITE_OK ){
drh290c1942004-08-21 17:54:45 +0000577 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
drh4f26d6c2004-05-26 23:25:30 +0000578 }
579 return rc;
580}
581int sqlite3_bind_null(sqlite3_stmt* p, int i){
582 return vdbeUnbind((Vdbe *)p, i);
583}
584int sqlite3_bind_text(
585 sqlite3_stmt *pStmt,
586 int i,
587 const char *zData,
588 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000589 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000590){
drh5e2517e2004-09-24 23:59:12 +0000591 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
drh4f26d6c2004-05-26 23:25:30 +0000592}
drh6c626082004-11-14 21:56:29 +0000593#ifndef SQLITE_OMIT_UTF16
drh4f26d6c2004-05-26 23:25:30 +0000594int sqlite3_bind_text16(
595 sqlite3_stmt *pStmt,
596 int i,
597 const void *zData,
598 int nData,
danielk1977d8123362004-06-12 09:25:12 +0000599 void (*xDel)(void*)
drh4f26d6c2004-05-26 23:25:30 +0000600){
drh5e2517e2004-09-24 23:59:12 +0000601 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
drh4f26d6c2004-05-26 23:25:30 +0000602}
drh6c626082004-11-14 21:56:29 +0000603#endif /* SQLITE_OMIT_UTF16 */
drh75f6a032004-07-15 14:15:00 +0000604
605/*
606** Return the number of wildcards that can be potentially bound to.
607** This routine is added to support DBD::SQLite.
drh75f6a032004-07-15 14:15:00 +0000608*/
609int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
drh92febd92004-08-20 18:34:20 +0000610 Vdbe *p = (Vdbe*)pStmt;
611 return p ? p->nVar : 0;
drh75f6a032004-07-15 14:15:00 +0000612}
drh895d7472004-08-20 16:02:39 +0000613
614/*
drhfa6bc002004-09-07 16:19:52 +0000615** Create a mapping from variable numbers to variable names
616** in the Vdbe.azVar[] array, if such a mapping does not already
617** exist.
drh895d7472004-08-20 16:02:39 +0000618*/
drhfa6bc002004-09-07 16:19:52 +0000619static void createVarMap(Vdbe *p){
drh895d7472004-08-20 16:02:39 +0000620 if( !p->okVar ){
621 int j;
622 Op *pOp;
623 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
624 if( pOp->opcode==OP_Variable ){
625 assert( pOp->p1>0 && pOp->p1<=p->nVar );
626 p->azVar[pOp->p1-1] = pOp->p3;
627 }
628 }
629 p->okVar = 1;
630 }
drhfa6bc002004-09-07 16:19:52 +0000631}
632
633/*
634** Return the name of a wildcard parameter. Return NULL if the index
635** is out of range or if the wildcard is unnamed.
636**
637** The result is always UTF-8.
638*/
639const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
640 Vdbe *p = (Vdbe*)pStmt;
641 if( p==0 || i<1 || i>p->nVar ){
642 return 0;
643 }
644 createVarMap(p);
drh895d7472004-08-20 16:02:39 +0000645 return p->azVar[i-1];
646}
drhfa6bc002004-09-07 16:19:52 +0000647
648/*
649** Given a wildcard parameter name, return the index of the variable
650** with that name. If there is no variable with the given name,
651** return 0.
652*/
653int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
654 Vdbe *p = (Vdbe*)pStmt;
655 int i;
656 if( p==0 ){
657 return 0;
658 }
659 createVarMap(p);
drh6a8903c2004-12-10 18:00:04 +0000660 if( zName ){
661 for(i=0; i<p->nVar; i++){
662 const char *z = p->azVar[i];
663 if( z && strcmp(z,zName)==0 ){
664 return i+1;
665 }
drhfa6bc002004-09-07 16:19:52 +0000666 }
667 }
668 return 0;
669}
drhf8db1bc2005-04-22 02:38:37 +0000670
drh51942bc2005-06-12 22:01:42 +0000671/*
drhf8db1bc2005-04-22 02:38:37 +0000672** Transfer all bindings from the first statement over to the second.
673** If the two statements contain a different number of bindings, then
674** an SQLITE_ERROR is returned.
675*/
676int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
677 Vdbe *pFrom = (Vdbe*)pFromStmt;
678 Vdbe *pTo = (Vdbe*)pToStmt;
679 int i, rc = SQLITE_OK;
680 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
681 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
682 return SQLITE_MISUSE;
683 }
684 if( pFrom->nVar!=pTo->nVar ){
685 return SQLITE_ERROR;
686 }
687 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
688 rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
689 }
690 return rc;
691}
drh51942bc2005-06-12 22:01:42 +0000692
693/*
694** Return the sqlite3* database handle to which the prepared statement given
695** in the argument belongs. This is the same database handle that was
696** the first argument to the sqlite3_prepare() that was used to create
697** the statement in the first place.
698*/
699sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
700 return pStmt ? ((Vdbe*)pStmt)->db : 0;
701}