|
1 /* |
|
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 #include "os.h" |
|
19 |
|
20 /* |
|
21 ** Return TRUE (non-zero) of the statement supplied as an argument needs |
|
22 ** to be recompiled. A statement needs to be recompiled whenever the |
|
23 ** execution environment changes in a way that would alter the program |
|
24 ** that sqlite3_prepare() generates. For example, if new functions or |
|
25 ** collating sequences are registered or if an authorizer function is |
|
26 ** added or changed. |
|
27 */ |
|
28 EXPORT_C int sqlite3_expired(sqlite3_stmt *pStmt){ |
|
29 Vdbe *p = (Vdbe*)pStmt; |
|
30 return p==0 || p->expired; |
|
31 } |
|
32 |
|
33 /**************************** sqlite3_value_ ******************************* |
|
34 ** The following routines extract information from a Mem or sqlite3_value |
|
35 ** structure. |
|
36 */ |
|
37 const void *sqlite3_value_blob(sqlite3_value *pVal){ |
|
38 Mem *p = (Mem*)pVal; |
|
39 if( p->flags & (MEM_Blob|MEM_Str) ){ |
|
40 return p->z; |
|
41 }else{ |
|
42 return sqlite3_value_text(pVal); |
|
43 } |
|
44 } |
|
45 int sqlite3_value_bytes(sqlite3_value *pVal){ |
|
46 return sqlite3ValueBytes(pVal, SQLITE_UTF8); |
|
47 } |
|
48 int sqlite3_value_bytes16(sqlite3_value *pVal){ |
|
49 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); |
|
50 } |
|
51 double sqlite3_value_double(sqlite3_value *pVal){ |
|
52 return sqlite3VdbeRealValue((Mem*)pVal); |
|
53 } |
|
54 int sqlite3_value_int(sqlite3_value *pVal){ |
|
55 return sqlite3VdbeIntValue((Mem*)pVal); |
|
56 } |
|
57 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ |
|
58 return sqlite3VdbeIntValue((Mem*)pVal); |
|
59 } |
|
60 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ |
|
61 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); |
|
62 } |
|
63 #ifndef SQLITE_OMIT_UTF16 |
|
64 const void *sqlite3_value_text16(sqlite3_value* pVal){ |
|
65 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
|
66 } |
|
67 const void *sqlite3_value_text16be(sqlite3_value *pVal){ |
|
68 return sqlite3ValueText(pVal, SQLITE_UTF16BE); |
|
69 } |
|
70 const void *sqlite3_value_text16le(sqlite3_value *pVal){ |
|
71 return sqlite3ValueText(pVal, SQLITE_UTF16LE); |
|
72 } |
|
73 #endif /* SQLITE_OMIT_UTF16 */ |
|
74 int sqlite3_value_type(sqlite3_value* pVal){ |
|
75 return pVal->type; |
|
76 } |
|
77 /* sqlite3_value_numeric_type() defined in vdbe.c */ |
|
78 |
|
79 /**************************** sqlite3_result_ ******************************* |
|
80 ** The following routines are used by user-defined functions to specify |
|
81 ** the function result. |
|
82 */ |
|
83 void sqlite3_result_blob( |
|
84 sqlite3_context *pCtx, |
|
85 const void *z, |
|
86 int n, |
|
87 void (*xDel)(void *) |
|
88 ){ |
|
89 assert( n>=0 ); |
|
90 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel); |
|
91 } |
|
92 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ |
|
93 sqlite3VdbeMemSetDouble(&pCtx->s, rVal); |
|
94 } |
|
95 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ |
|
96 pCtx->isError = 1; |
|
97 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); |
|
98 } |
|
99 #ifndef SQLITE_OMIT_UTF16 |
|
100 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ |
|
101 pCtx->isError = 1; |
|
102 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); |
|
103 } |
|
104 #endif |
|
105 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ |
|
106 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); |
|
107 } |
|
108 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ |
|
109 sqlite3VdbeMemSetInt64(&pCtx->s, iVal); |
|
110 } |
|
111 void sqlite3_result_null(sqlite3_context *pCtx){ |
|
112 sqlite3VdbeMemSetNull(&pCtx->s); |
|
113 } |
|
114 void sqlite3_result_text( |
|
115 sqlite3_context *pCtx, |
|
116 const char *z, |
|
117 int n, |
|
118 void (*xDel)(void *) |
|
119 ){ |
|
120 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); |
|
121 } |
|
122 #ifndef SQLITE_OMIT_UTF16 |
|
123 void sqlite3_result_text16( |
|
124 sqlite3_context *pCtx, |
|
125 const void *z, |
|
126 int n, |
|
127 void (*xDel)(void *) |
|
128 ){ |
|
129 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); |
|
130 } |
|
131 void sqlite3_result_text16be( |
|
132 sqlite3_context *pCtx, |
|
133 const void *z, |
|
134 int n, |
|
135 void (*xDel)(void *) |
|
136 ){ |
|
137 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); |
|
138 } |
|
139 void sqlite3_result_text16le( |
|
140 sqlite3_context *pCtx, |
|
141 const void *z, |
|
142 int n, |
|
143 void (*xDel)(void *) |
|
144 ){ |
|
145 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); |
|
146 } |
|
147 #endif /* SQLITE_OMIT_UTF16 */ |
|
148 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ |
|
149 sqlite3VdbeMemCopy(&pCtx->s, pValue); |
|
150 } |
|
151 |
|
152 |
|
153 /* |
|
154 ** Execute the statement pStmt, either until a row of data is ready, the |
|
155 ** statement is completely executed or an error occurs. |
|
156 */ |
|
157 EXPORT_C int sqlite3_step(sqlite3_stmt *pStmt){ |
|
158 Vdbe *p = (Vdbe*)pStmt; |
|
159 sqlite3 *db; |
|
160 int rc; |
|
161 |
|
162 /* Assert that malloc() has not failed */ |
|
163 assert( !sqlite3MallocFailed() ); |
|
164 |
|
165 if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ |
|
166 return SQLITE_MISUSE; |
|
167 } |
|
168 if( p->aborted ){ |
|
169 return SQLITE_ABORT; |
|
170 } |
|
171 if( p->pc<=0 && p->expired ){ |
|
172 if( p->rc==SQLITE_OK ){ |
|
173 p->rc = SQLITE_SCHEMA; |
|
174 } |
|
175 return SQLITE_ERROR; |
|
176 } |
|
177 db = p->db; |
|
178 if( sqlite3SafetyOn(db) ){ |
|
179 p->rc = SQLITE_MISUSE; |
|
180 return SQLITE_MISUSE; |
|
181 } |
|
182 if( p->pc<0 ){ |
|
183 /* If there are no other statements currently running, then |
|
184 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt |
|
185 ** from interrupting a statement that has not yet started. |
|
186 */ |
|
187 if( db->activeVdbeCnt==0 ){ |
|
188 db->u1.isInterrupted = 0; |
|
189 } |
|
190 |
|
191 #ifndef SQLITE_OMIT_TRACE |
|
192 /* Invoke the trace callback if there is one |
|
193 */ |
|
194 if( db->xTrace && !db->init.busy ){ |
|
195 assert( p->nOp>0 ); |
|
196 assert( p->aOp[p->nOp-1].opcode==OP_Noop ); |
|
197 assert( p->aOp[p->nOp-1].p3!=0 ); |
|
198 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); |
|
199 sqlite3SafetyOff(db); |
|
200 db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); |
|
201 if( sqlite3SafetyOn(db) ){ |
|
202 p->rc = SQLITE_MISUSE; |
|
203 return SQLITE_MISUSE; |
|
204 } |
|
205 } |
|
206 if( db->xProfile && !db->init.busy ){ |
|
207 double rNow; |
|
208 sqlite3OsCurrentTime(&rNow); |
|
209 p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0; |
|
210 } |
|
211 #endif |
|
212 |
|
213 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned |
|
214 ** on in debugging mode. |
|
215 */ |
|
216 #ifdef SQLITE_DEBUG |
|
217 if( (db->flags & SQLITE_SqlTrace)!=0 ){ |
|
218 sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); |
|
219 } |
|
220 #endif /* SQLITE_DEBUG */ |
|
221 |
|
222 db->activeVdbeCnt++; |
|
223 p->pc = 0; |
|
224 } |
|
225 #ifndef SQLITE_OMIT_EXPLAIN |
|
226 if( p->explain ){ |
|
227 rc = sqlite3VdbeList(p); |
|
228 }else |
|
229 #endif /* SQLITE_OMIT_EXPLAIN */ |
|
230 { |
|
231 rc = sqlite3VdbeExec(p); |
|
232 } |
|
233 |
|
234 if( sqlite3SafetyOff(db) ){ |
|
235 rc = SQLITE_MISUSE; |
|
236 } |
|
237 |
|
238 #ifndef SQLITE_OMIT_TRACE |
|
239 /* Invoke the profile callback if there is one |
|
240 */ |
|
241 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){ |
|
242 double rNow; |
|
243 u64 elapseTime; |
|
244 |
|
245 sqlite3OsCurrentTime(&rNow); |
|
246 elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; |
|
247 assert( p->nOp>0 ); |
|
248 assert( p->aOp[p->nOp-1].opcode==OP_Noop ); |
|
249 assert( p->aOp[p->nOp-1].p3!=0 ); |
|
250 assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); |
|
251 db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime); |
|
252 } |
|
253 #endif |
|
254 |
|
255 sqlite3Error(p->db, rc, 0); |
|
256 p->rc = sqlite3ApiExit(p->db, p->rc); |
|
257 return rc; |
|
258 } |
|
259 |
|
260 /* |
|
261 ** Extract the user data from a sqlite3_context structure and return a |
|
262 ** pointer to it. |
|
263 */ |
|
264 void *sqlite3_user_data(sqlite3_context *p){ |
|
265 assert( p && p->pFunc ); |
|
266 return p->pFunc->pUserData; |
|
267 } |
|
268 |
|
269 /* |
|
270 ** Allocate or return the aggregate context for a user function. A new |
|
271 ** context is allocated on the first call. Subsequent calls return the |
|
272 ** same context that was returned on prior calls. |
|
273 */ |
|
274 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ |
|
275 Mem *pMem = p->pMem; |
|
276 assert( p && p->pFunc && p->pFunc->xStep ); |
|
277 if( (pMem->flags & MEM_Agg)==0 ){ |
|
278 if( nByte==0 ){ |
|
279 assert( pMem->flags==MEM_Null ); |
|
280 pMem->z = 0; |
|
281 }else{ |
|
282 pMem->flags = MEM_Agg; |
|
283 pMem->xDel = sqlite3FreeX; |
|
284 *(FuncDef**)&pMem->i = p->pFunc; |
|
285 if( nByte<=NBFS ){ |
|
286 pMem->z = pMem->zShort; |
|
287 memset(pMem->z, 0, nByte); |
|
288 }else{ |
|
289 pMem->z = sqliteMalloc( nByte ); |
|
290 } |
|
291 } |
|
292 } |
|
293 return (void*)pMem->z; |
|
294 } |
|
295 |
|
296 /* |
|
297 ** Return the auxilary data pointer, if any, for the iArg'th argument to |
|
298 ** the user-function defined by pCtx. |
|
299 */ |
|
300 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ |
|
301 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc; |
|
302 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ |
|
303 return 0; |
|
304 } |
|
305 return pVdbeFunc->apAux[iArg].pAux; |
|
306 } |
|
307 |
|
308 /* |
|
309 ** Set the auxilary data pointer and delete function, for the iArg'th |
|
310 ** argument to the user-function defined by pCtx. Any previous value is |
|
311 ** deleted by calling the delete function specified when it was set. |
|
312 */ |
|
313 void sqlite3_set_auxdata( |
|
314 sqlite3_context *pCtx, |
|
315 int iArg, |
|
316 void *pAux, |
|
317 void (*xDelete)(void*) |
|
318 ){ |
|
319 struct AuxData *pAuxData; |
|
320 VdbeFunc *pVdbeFunc; |
|
321 if( iArg<0 ) return; |
|
322 |
|
323 pVdbeFunc = pCtx->pVdbeFunc; |
|
324 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ |
|
325 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; |
|
326 pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc); |
|
327 if( !pVdbeFunc ) return; |
|
328 pCtx->pVdbeFunc = pVdbeFunc; |
|
329 memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, |
|
330 sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux)); |
|
331 pVdbeFunc->nAux = iArg+1; |
|
332 pVdbeFunc->pFunc = pCtx->pFunc; |
|
333 } |
|
334 |
|
335 pAuxData = &pVdbeFunc->apAux[iArg]; |
|
336 if( pAuxData->pAux && pAuxData->xDelete ){ |
|
337 pAuxData->xDelete(pAuxData->pAux); |
|
338 } |
|
339 pAuxData->pAux = pAux; |
|
340 pAuxData->xDelete = xDelete; |
|
341 } |
|
342 |
|
343 /* |
|
344 ** Return the number of times the Step function of a aggregate has been |
|
345 ** called. |
|
346 ** |
|
347 ** This function is deprecated. Do not use it for new code. It is |
|
348 ** provide only to avoid breaking legacy code. New aggregate function |
|
349 ** implementations should keep their own counts within their aggregate |
|
350 ** context. |
|
351 */ |
|
352 int sqlite3_aggregate_count(sqlite3_context *p){ |
|
353 assert( p && p->pFunc && p->pFunc->xStep ); |
|
354 return p->pMem->n; |
|
355 } |
|
356 |
|
357 /* |
|
358 ** Return the number of columns in the result set for the statement pStmt. |
|
359 */ |
|
360 int sqlite3_column_count(sqlite3_stmt *pStmt){ |
|
361 Vdbe *pVm = (Vdbe *)pStmt; |
|
362 return pVm ? pVm->nResColumn : 0; |
|
363 } |
|
364 |
|
365 /* |
|
366 ** Return the number of values available from the current row of the |
|
367 ** currently executing statement pStmt. |
|
368 */ |
|
369 EXPORT_C int sqlite3_data_count(sqlite3_stmt *pStmt){ |
|
370 Vdbe *pVm = (Vdbe *)pStmt; |
|
371 if( pVm==0 || !pVm->resOnStack ) return 0; |
|
372 return pVm->nResColumn; |
|
373 } |
|
374 |
|
375 |
|
376 /* |
|
377 ** Check to see if column iCol of the given statement is valid. If |
|
378 ** it is, return a pointer to the Mem for the value of that column. |
|
379 ** If iCol is not valid, return a pointer to a Mem which has a value |
|
380 ** of NULL. |
|
381 */ |
|
382 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ |
|
383 Vdbe *pVm = (Vdbe *)pStmt; |
|
384 int vals = sqlite3_data_count(pStmt); |
|
385 if( i>=vals || i<0 ){ |
|
386 static const Mem nullMem = {0, 0.0, "", 0, MEM_Null, MEM_Null }; |
|
387 sqlite3Error(pVm->db, SQLITE_RANGE, 0); |
|
388 return (Mem*)&nullMem; |
|
389 } |
|
390 return &pVm->pTos[(1-vals)+i]; |
|
391 } |
|
392 |
|
393 /* |
|
394 ** This function is called after invoking an sqlite3_value_XXX function on a |
|
395 ** column value (i.e. a value returned by evaluating an SQL expression in the |
|
396 ** select list of a SELECT statement) that may cause a malloc() failure. If |
|
397 ** malloc() has failed, the threads mallocFailed flag is cleared and the result |
|
398 ** code of statement pStmt set to SQLITE_NOMEM. |
|
399 ** |
|
400 ** Specificly, this is called from within: |
|
401 ** |
|
402 ** sqlite3_column_int() |
|
403 ** sqlite3_column_int64() |
|
404 ** sqlite3_column_text() |
|
405 ** sqlite3_column_text16() |
|
406 ** sqlite3_column_real() |
|
407 ** sqlite3_column_bytes() |
|
408 ** sqlite3_column_bytes16() |
|
409 ** |
|
410 ** But not for sqlite3_column_blob(), which never calls malloc(). |
|
411 */ |
|
412 static void columnMallocFailure(sqlite3_stmt *pStmt) |
|
413 { |
|
414 /* If malloc() failed during an encoding conversion within an |
|
415 ** sqlite3_column_XXX API, then set the return code of the statement to |
|
416 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR |
|
417 ** and _finalize() will return NOMEM. |
|
418 */ |
|
419 Vdbe *p = (Vdbe *)pStmt; |
|
420 p->rc = sqlite3ApiExit(0, p->rc); |
|
421 } |
|
422 |
|
423 /**************************** sqlite3_column_ ******************************* |
|
424 ** The following routines are used to access elements of the current row |
|
425 ** in the result set. |
|
426 */ |
|
427 EXPORT_C const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ |
|
428 const void *val; |
|
429 sqlite3MallocDisallow(); |
|
430 val = sqlite3_value_blob( columnMem(pStmt,i) ); |
|
431 sqlite3MallocAllow(); |
|
432 return val; |
|
433 } |
|
434 EXPORT_C int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ |
|
435 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); |
|
436 columnMallocFailure(pStmt); |
|
437 return val; |
|
438 } |
|
439 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ |
|
440 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); |
|
441 columnMallocFailure(pStmt); |
|
442 return val; |
|
443 } |
|
444 EXPORT_C double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ |
|
445 double val = sqlite3_value_double( columnMem(pStmt,i) ); |
|
446 columnMallocFailure(pStmt); |
|
447 return val; |
|
448 } |
|
449 EXPORT_C int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ |
|
450 int val = sqlite3_value_int( columnMem(pStmt,i) ); |
|
451 columnMallocFailure(pStmt); |
|
452 return val; |
|
453 } |
|
454 EXPORT_C sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ |
|
455 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); |
|
456 columnMallocFailure(pStmt); |
|
457 return val; |
|
458 } |
|
459 EXPORT_C const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ |
|
460 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); |
|
461 columnMallocFailure(pStmt); |
|
462 return val; |
|
463 } |
|
464 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ |
|
465 return columnMem(pStmt, i); |
|
466 } |
|
467 #ifndef SQLITE_OMIT_UTF16 |
|
468 EXPORT_C const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ |
|
469 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); |
|
470 columnMallocFailure(pStmt); |
|
471 return val; |
|
472 } |
|
473 #endif /* SQLITE_OMIT_UTF16 */ |
|
474 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ |
|
475 return sqlite3_value_type( columnMem(pStmt,i) ); |
|
476 } |
|
477 |
|
478 /* The following function is experimental and subject to change or |
|
479 ** removal */ |
|
480 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ |
|
481 ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); |
|
482 **} |
|
483 */ |
|
484 |
|
485 /* |
|
486 ** Convert the N-th element of pStmt->pColName[] into a string using |
|
487 ** xFunc() then return that string. If N is out of range, return 0. |
|
488 ** |
|
489 ** There are up to 5 names for each column. useType determines which |
|
490 ** name is returned. Here are the names: |
|
491 ** |
|
492 ** 0 The column name as it should be displayed for output |
|
493 ** 1 The datatype name for the column |
|
494 ** 2 The name of the database that the column derives from |
|
495 ** 3 The name of the table that the column derives from |
|
496 ** 4 The name of the table column that the result column derives from |
|
497 ** |
|
498 ** If the result is not a simple column reference (if it is an expression |
|
499 ** or a constant) then useTypes 2, 3, and 4 return NULL. |
|
500 */ |
|
501 static const void *columnName( |
|
502 sqlite3_stmt *pStmt, |
|
503 int N, |
|
504 const void *(*xFunc)(Mem*), |
|
505 int useType |
|
506 ){ |
|
507 const void *ret; |
|
508 Vdbe *p = (Vdbe *)pStmt; |
|
509 int n = sqlite3_column_count(pStmt); |
|
510 |
|
511 if( p==0 || N>=n || N<0 ){ |
|
512 return 0; |
|
513 } |
|
514 N += useType*n; |
|
515 ret = xFunc(&p->aColName[N]); |
|
516 |
|
517 /* A malloc may have failed inside of the xFunc() call. If this is the case, |
|
518 ** clear the mallocFailed flag and return NULL. |
|
519 */ |
|
520 sqlite3ApiExit(0, 0); |
|
521 return ret; |
|
522 } |
|
523 |
|
524 /* |
|
525 ** Return the name of the Nth column of the result set returned by SQL |
|
526 ** statement pStmt. |
|
527 */ |
|
528 EXPORT_C const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ |
|
529 return columnName( |
|
530 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); |
|
531 } |
|
532 #ifndef SQLITE_OMIT_UTF16 |
|
533 EXPORT_C const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ |
|
534 return columnName( |
|
535 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); |
|
536 } |
|
537 #endif |
|
538 |
|
539 /* |
|
540 ** Return the column declaration type (if applicable) of the 'i'th column |
|
541 ** of the result set of SQL statement pStmt. |
|
542 */ |
|
543 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ |
|
544 return columnName( |
|
545 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); |
|
546 } |
|
547 #ifndef SQLITE_OMIT_UTF16 |
|
548 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ |
|
549 return columnName( |
|
550 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); |
|
551 } |
|
552 #endif /* SQLITE_OMIT_UTF16 */ |
|
553 |
|
554 #ifdef SQLITE_ENABLE_COLUMN_METADATA |
|
555 /* |
|
556 ** Return the name of the database from which a result column derives. |
|
557 ** NULL is returned if the result column is an expression or constant or |
|
558 ** anything else which is not an unabiguous reference to a database column. |
|
559 */ |
|
560 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ |
|
561 return columnName( |
|
562 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); |
|
563 } |
|
564 #ifndef SQLITE_OMIT_UTF16 |
|
565 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ |
|
566 return columnName( |
|
567 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); |
|
568 } |
|
569 #endif /* SQLITE_OMIT_UTF16 */ |
|
570 |
|
571 /* |
|
572 ** Return the name of the table from which a result column derives. |
|
573 ** NULL is returned if the result column is an expression or constant or |
|
574 ** anything else which is not an unabiguous reference to a database column. |
|
575 */ |
|
576 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ |
|
577 return columnName( |
|
578 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); |
|
579 } |
|
580 #ifndef SQLITE_OMIT_UTF16 |
|
581 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ |
|
582 return columnName( |
|
583 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); |
|
584 } |
|
585 #endif /* SQLITE_OMIT_UTF16 */ |
|
586 |
|
587 /* |
|
588 ** Return the name of the table column from which a result column derives. |
|
589 ** NULL is returned if the result column is an expression or constant or |
|
590 ** anything else which is not an unabiguous reference to a database column. |
|
591 */ |
|
592 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ |
|
593 return columnName( |
|
594 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); |
|
595 } |
|
596 #ifndef SQLITE_OMIT_UTF16 |
|
597 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ |
|
598 return columnName( |
|
599 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); |
|
600 } |
|
601 #endif /* SQLITE_OMIT_UTF16 */ |
|
602 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ |
|
603 |
|
604 |
|
605 /******************************* sqlite3_bind_ *************************** |
|
606 ** |
|
607 ** Routines used to attach values to wildcards in a compiled SQL statement. |
|
608 */ |
|
609 /* |
|
610 ** Unbind the value bound to variable i in virtual machine p. This is the |
|
611 ** the same as binding a NULL value to the column. If the "i" parameter is |
|
612 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
|
613 ** |
|
614 ** The error code stored in database p->db is overwritten with the return |
|
615 ** value in any case. |
|
616 */ |
|
617 static int vdbeUnbind(Vdbe *p, int i){ |
|
618 Mem *pVar; |
|
619 if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ |
|
620 if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0); |
|
621 return SQLITE_MISUSE; |
|
622 } |
|
623 if( i<1 || i>p->nVar ){ |
|
624 sqlite3Error(p->db, SQLITE_RANGE, 0); |
|
625 return SQLITE_RANGE; |
|
626 } |
|
627 i--; |
|
628 pVar = &p->aVar[i]; |
|
629 sqlite3VdbeMemRelease(pVar); |
|
630 pVar->flags = MEM_Null; |
|
631 sqlite3Error(p->db, SQLITE_OK, 0); |
|
632 return SQLITE_OK; |
|
633 } |
|
634 |
|
635 /* |
|
636 ** Bind a text or BLOB value. |
|
637 */ |
|
638 static int bindText( |
|
639 sqlite3_stmt *pStmt, |
|
640 int i, |
|
641 const void *zData, |
|
642 int nData, |
|
643 void (*xDel)(void*), |
|
644 int encoding |
|
645 ){ |
|
646 Vdbe *p = (Vdbe *)pStmt; |
|
647 Mem *pVar; |
|
648 int rc; |
|
649 |
|
650 rc = vdbeUnbind(p, i); |
|
651 if( rc || zData==0 ){ |
|
652 return rc; |
|
653 } |
|
654 pVar = &p->aVar[i-1]; |
|
655 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); |
|
656 if( rc==SQLITE_OK && encoding!=0 ){ |
|
657 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); |
|
658 } |
|
659 |
|
660 sqlite3Error(((Vdbe *)pStmt)->db, rc, 0); |
|
661 return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc); |
|
662 } |
|
663 |
|
664 |
|
665 /* |
|
666 ** Bind a blob value to an SQL statement variable. |
|
667 */ |
|
668 EXPORT_C int sqlite3_bind_blob( |
|
669 sqlite3_stmt *pStmt, |
|
670 int i, |
|
671 const void *zData, |
|
672 int nData, |
|
673 void (*xDel)(void*) |
|
674 ){ |
|
675 return bindText(pStmt, i, zData, nData, xDel, 0); |
|
676 } |
|
677 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ |
|
678 int rc; |
|
679 Vdbe *p = (Vdbe *)pStmt; |
|
680 rc = vdbeUnbind(p, i); |
|
681 if( rc==SQLITE_OK ){ |
|
682 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); |
|
683 } |
|
684 return rc; |
|
685 } |
|
686 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
|
687 return sqlite3_bind_int64(p, i, (i64)iValue); |
|
688 } |
|
689 EXPORT_C int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
|
690 int rc; |
|
691 Vdbe *p = (Vdbe *)pStmt; |
|
692 rc = vdbeUnbind(p, i); |
|
693 if( rc==SQLITE_OK ){ |
|
694 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
|
695 } |
|
696 return rc; |
|
697 } |
|
698 EXPORT_C int sqlite3_bind_null(sqlite3_stmt* p, int i){ |
|
699 return vdbeUnbind((Vdbe *)p, i); |
|
700 } |
|
701 EXPORT_C int sqlite3_bind_text( |
|
702 sqlite3_stmt *pStmt, |
|
703 int i, |
|
704 const char *zData, |
|
705 int nData, |
|
706 void (*xDel)(void*) |
|
707 ){ |
|
708 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); |
|
709 } |
|
710 #ifndef SQLITE_OMIT_UTF16 |
|
711 EXPORT_C int sqlite3_bind_text16( |
|
712 sqlite3_stmt *pStmt, |
|
713 int i, |
|
714 const void *zData, |
|
715 int nData, |
|
716 void (*xDel)(void*) |
|
717 ){ |
|
718 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); |
|
719 } |
|
720 #endif /* SQLITE_OMIT_UTF16 */ |
|
721 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ |
|
722 int rc; |
|
723 Vdbe *p = (Vdbe *)pStmt; |
|
724 rc = vdbeUnbind(p, i); |
|
725 if( rc==SQLITE_OK ){ |
|
726 sqlite3VdbeMemCopy(&p->aVar[i-1], pValue); |
|
727 } |
|
728 return rc; |
|
729 } |
|
730 |
|
731 /* |
|
732 ** Return the number of wildcards that can be potentially bound to. |
|
733 ** This routine is added to support DBD::SQLite. |
|
734 */ |
|
735 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ |
|
736 Vdbe *p = (Vdbe*)pStmt; |
|
737 return p ? p->nVar : 0; |
|
738 } |
|
739 |
|
740 /* |
|
741 ** Create a mapping from variable numbers to variable names |
|
742 ** in the Vdbe.azVar[] array, if such a mapping does not already |
|
743 ** exist. |
|
744 */ |
|
745 static void createVarMap(Vdbe *p){ |
|
746 if( !p->okVar ){ |
|
747 int j; |
|
748 Op *pOp; |
|
749 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ |
|
750 if( pOp->opcode==OP_Variable ){ |
|
751 assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
|
752 p->azVar[pOp->p1-1] = pOp->p3; |
|
753 } |
|
754 } |
|
755 p->okVar = 1; |
|
756 } |
|
757 } |
|
758 |
|
759 /* |
|
760 ** Return the name of a wildcard parameter. Return NULL if the index |
|
761 ** is out of range or if the wildcard is unnamed. |
|
762 ** |
|
763 ** The result is always UTF-8. |
|
764 */ |
|
765 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ |
|
766 Vdbe *p = (Vdbe*)pStmt; |
|
767 if( p==0 || i<1 || i>p->nVar ){ |
|
768 return 0; |
|
769 } |
|
770 createVarMap(p); |
|
771 return p->azVar[i-1]; |
|
772 } |
|
773 |
|
774 /* |
|
775 ** Given a wildcard parameter name, return the index of the variable |
|
776 ** with that name. If there is no variable with the given name, |
|
777 ** return 0. |
|
778 */ |
|
779 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ |
|
780 Vdbe *p = (Vdbe*)pStmt; |
|
781 int i; |
|
782 if( p==0 ){ |
|
783 return 0; |
|
784 } |
|
785 createVarMap(p); |
|
786 if( zName ){ |
|
787 for(i=0; i<p->nVar; i++){ |
|
788 const char *z = p->azVar[i]; |
|
789 if( z && strcmp(z,zName)==0 ){ |
|
790 return i+1; |
|
791 } |
|
792 } |
|
793 } |
|
794 return 0; |
|
795 } |
|
796 |
|
797 /* |
|
798 ** Transfer all bindings from the first statement over to the second. |
|
799 ** If the two statements contain a different number of bindings, then |
|
800 ** an SQLITE_ERROR is returned. |
|
801 */ |
|
802 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ |
|
803 Vdbe *pFrom = (Vdbe*)pFromStmt; |
|
804 Vdbe *pTo = (Vdbe*)pToStmt; |
|
805 int i, rc = SQLITE_OK; |
|
806 if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) |
|
807 || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){ |
|
808 return SQLITE_MISUSE; |
|
809 } |
|
810 if( pFrom->nVar!=pTo->nVar ){ |
|
811 return SQLITE_ERROR; |
|
812 } |
|
813 for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ |
|
814 sqlite3MallocDisallow(); |
|
815 rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); |
|
816 sqlite3MallocAllow(); |
|
817 } |
|
818 return rc; |
|
819 } |
|
820 |
|
821 /* |
|
822 ** Return the sqlite3* database handle to which the prepared statement given |
|
823 ** in the argument belongs. This is the same database handle that was |
|
824 ** the first argument to the sqlite3_prepare() that was used to create |
|
825 ** the statement in the first place. |
|
826 */ |
|
827 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ |
|
828 return pStmt ? ((Vdbe*)pStmt)->db : 0; |
|
829 } |