4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
13 ** This file contains code use to implement APIs that are part of the
16 #include "sqliteInt.h"
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
27 int sqlite3_expired(sqlite3_stmt
*pStmt
){
28 Vdbe
*p
= (Vdbe
*)pStmt
;
29 return p
==0 || p
->expired
;
32 /**************************** sqlite3_value_ *******************************
33 ** The following routines extract information from a Mem or sqlite3_value
36 const void *sqlite3_value_blob(sqlite3_value
*pVal
){
38 if( p
->flags
& (MEM_Blob
|MEM_Str
) ){
41 return sqlite3_value_text(pVal
);
44 int sqlite3_value_bytes(sqlite3_value
*pVal
){
45 return sqlite3ValueBytes(pVal
, SQLITE_UTF8
);
47 int sqlite3_value_bytes16(sqlite3_value
*pVal
){
48 return sqlite3ValueBytes(pVal
, SQLITE_UTF16NATIVE
);
50 double sqlite3_value_double(sqlite3_value
*pVal
){
51 return sqlite3VdbeRealValue((Mem
*)pVal
);
53 int sqlite3_value_int(sqlite3_value
*pVal
){
54 return sqlite3VdbeIntValue((Mem
*)pVal
);
56 sqlite_int64
sqlite3_value_int64(sqlite3_value
*pVal
){
57 return sqlite3VdbeIntValue((Mem
*)pVal
);
59 const unsigned char *sqlite3_value_text(sqlite3_value
*pVal
){
60 return (const char *)sqlite3ValueText(pVal
, SQLITE_UTF8
);
62 #ifndef SQLITE_OMIT_UTF16
63 const void *sqlite3_value_text16(sqlite3_value
* pVal
){
64 return sqlite3ValueText(pVal
, SQLITE_UTF16NATIVE
);
66 const void *sqlite3_value_text16be(sqlite3_value
*pVal
){
67 return sqlite3ValueText(pVal
, SQLITE_UTF16BE
);
69 const void *sqlite3_value_text16le(sqlite3_value
*pVal
){
70 return sqlite3ValueText(pVal
, SQLITE_UTF16LE
);
72 #endif /* SQLITE_OMIT_UTF16 */
73 int sqlite3_value_type(sqlite3_value
* pVal
){
77 /**************************** sqlite3_result_ *******************************
78 ** The following routines are used by user-defined functions to specify
79 ** the function result.
81 void sqlite3_result_blob(
82 sqlite3_context
*pCtx
,
88 sqlite3VdbeMemSetStr(&pCtx
->s
, z
, n
, 0, xDel
);
90 void sqlite3_result_double(sqlite3_context
*pCtx
, double rVal
){
91 sqlite3VdbeMemSetDouble(&pCtx
->s
, rVal
);
93 void sqlite3_result_error(sqlite3_context
*pCtx
, const char *z
, int n
){
95 sqlite3VdbeMemSetStr(&pCtx
->s
, z
, n
, SQLITE_UTF8
, SQLITE_TRANSIENT
);
97 void sqlite3_result_error16(sqlite3_context
*pCtx
, const void *z
, int n
){
99 sqlite3VdbeMemSetStr(&pCtx
->s
, z
, n
, SQLITE_UTF16NATIVE
, SQLITE_TRANSIENT
);
101 void sqlite3_result_int(sqlite3_context
*pCtx
, int iVal
){
102 sqlite3VdbeMemSetInt64(&pCtx
->s
, (i64
)iVal
);
104 void sqlite3_result_int64(sqlite3_context
*pCtx
, i64 iVal
){
105 sqlite3VdbeMemSetInt64(&pCtx
->s
, iVal
);
107 void sqlite3_result_null(sqlite3_context
*pCtx
){
108 sqlite3VdbeMemSetNull(&pCtx
->s
);
110 void sqlite3_result_text(
111 sqlite3_context
*pCtx
,
116 sqlite3VdbeMemSetStr(&pCtx
->s
, z
, n
, SQLITE_UTF8
, xDel
);
118 #ifndef SQLITE_OMIT_UTF16
119 void sqlite3_result_text16(
120 sqlite3_context
*pCtx
,
125 sqlite3VdbeMemSetStr(&pCtx
->s
, z
, n
, SQLITE_UTF16NATIVE
, xDel
);
127 void sqlite3_result_text16be(
128 sqlite3_context
*pCtx
,
133 sqlite3VdbeMemSetStr(&pCtx
->s
, z
, n
, SQLITE_UTF16BE
, xDel
);
135 void sqlite3_result_text16le(
136 sqlite3_context
*pCtx
,
141 sqlite3VdbeMemSetStr(&pCtx
->s
, z
, n
, SQLITE_UTF16LE
, xDel
);
143 #endif /* SQLITE_OMIT_UTF16 */
144 void sqlite3_result_value(sqlite3_context
*pCtx
, sqlite3_value
*pValue
){
145 sqlite3VdbeMemCopy(&pCtx
->s
, pValue
);
150 ** Execute the statement pStmt, either until a row of data is ready, the
151 ** statement is completely executed or an error occurs.
153 int sqlite3_step(sqlite3_stmt
*pStmt
){
154 Vdbe
*p
= (Vdbe
*)pStmt
;
158 if( p
==0 || p
->magic
!=VDBE_MAGIC_RUN
){
159 return SQLITE_MISUSE
;
164 if( p
->pc
<=0 && p
->expired
){
165 if( p
->rc
==SQLITE_OK
){
166 p
->rc
= SQLITE_SCHEMA
;
171 if( sqlite3SafetyOn(db
) ){
172 p
->rc
= SQLITE_MISUSE
;
173 return SQLITE_MISUSE
;
176 /* Invoke the trace callback if there is one
178 if( (db
= p
->db
)->xTrace
&& !db
->init
.busy
){
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
;
191 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
192 ** on in debugging mode.
195 if( (db
->flags
& SQLITE_SqlTrace
)!=0 ){
196 sqlite3DebugPrintf("SQL-trace: %s\n", p
->aOp
[p
->nOp
-1].p3
);
198 #endif /* SQLITE_DEBUG */
203 #ifndef SQLITE_OMIT_EXPLAIN
205 rc
= sqlite3VdbeList(p
);
207 #endif /* SQLITE_OMIT_EXPLAIN */
209 rc
= sqlite3VdbeExec(p
);
212 if( sqlite3SafetyOff(db
) ){
216 sqlite3Error(p
->db
, rc
, p
->zErrMsg
);
221 ** Extract the user data from a sqlite3_context structure and return a
224 void *sqlite3_user_data(sqlite3_context
*p
){
225 assert( p
&& p
->pFunc
);
226 return p
->pFunc
->pUserData
;
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.
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
238 void *sqlite3_aggregate_context(sqlite3_context
*p
, int nByte
){
239 assert( p
&& p
->pFunc
&& p
->pFunc
->xStep
);
242 p
->pAgg
= (void*)p
->s
.z
;
243 memset(p
->pAgg
, 0, nByte
);
245 p
->pAgg
= sqliteMalloc( nByte
);
252 ** Return the auxilary data pointer, if any, for the iArg'th argument to
253 ** the user-function defined by pCtx.
255 void *sqlite3_get_auxdata(sqlite3_context
*pCtx
, int iArg
){
256 VdbeFunc
*pVdbeFunc
= pCtx
->pVdbeFunc
;
257 if( !pVdbeFunc
|| iArg
>=pVdbeFunc
->nAux
|| iArg
<0 ){
260 return pVdbeFunc
->apAux
[iArg
].pAux
;
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.
268 void sqlite3_set_auxdata(
269 sqlite3_context
*pCtx
,
272 void (*xDelete
)(void*)
274 struct AuxData
*pAuxData
;
278 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
);
282 if( !pVdbeFunc
) return;
283 memset(&pVdbeFunc
->apAux
[pVdbeFunc
->nAux
], 0,
284 sizeof(struct AuxData
)*(iArg
+1-pVdbeFunc
->nAux
));
285 pVdbeFunc
->nAux
= iArg
+1;
286 pVdbeFunc
->pFunc
= pCtx
->pFunc
;
289 pAuxData
= &pVdbeFunc
->apAux
[iArg
];
290 if( pAuxData
->pAux
&& pAuxData
->xDelete
){
291 pAuxData
->xDelete(pAuxData
->pAux
);
293 pAuxData
->pAux
= pAux
;
294 pAuxData
->xDelete
= xDelete
;
298 ** Return the number of times the Step function of a aggregate has been
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
305 int sqlite3_aggregate_count(sqlite3_context
*p
){
306 assert( p
&& p
->pFunc
&& p
->pFunc
->xStep
);
311 ** Return the number of columns in the result set for the statement pStmt.
313 int sqlite3_column_count(sqlite3_stmt
*pStmt
){
314 Vdbe
*pVm
= (Vdbe
*)pStmt
;
315 return pVm
? pVm
->nResColumn
: 0;
319 ** Return the number of values available from the current row of the
320 ** currently executing statement pStmt.
322 int sqlite3_data_count(sqlite3_stmt
*pStmt
){
323 Vdbe
*pVm
= (Vdbe
*)pStmt
;
324 if( pVm
==0 || !pVm
->resOnStack
) return 0;
325 return pVm
->nResColumn
;
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
335 static 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 ){
340 if( nullMem
.flags
==0 ){ nullMem
.flags
= MEM_Null
; }
341 sqlite3Error(pVm
->db
, SQLITE_RANGE
, 0);
344 return &pVm
->pTos
[(1-vals
)+i
];
347 /**************************** sqlite3_column_ *******************************
348 ** The following routines are used to access elements of the current row
349 ** in the result set.
351 const void *sqlite3_column_blob(sqlite3_stmt
*pStmt
, int i
){
352 return sqlite3_value_blob( columnMem(pStmt
,i
) );
354 int sqlite3_column_bytes(sqlite3_stmt
*pStmt
, int i
){
355 return sqlite3_value_bytes( columnMem(pStmt
,i
) );
357 int sqlite3_column_bytes16(sqlite3_stmt
*pStmt
, int i
){
358 return sqlite3_value_bytes16( columnMem(pStmt
,i
) );
360 double sqlite3_column_double(sqlite3_stmt
*pStmt
, int i
){
361 return sqlite3_value_double( columnMem(pStmt
,i
) );
363 int sqlite3_column_int(sqlite3_stmt
*pStmt
, int i
){
364 return sqlite3_value_int( columnMem(pStmt
,i
) );
366 sqlite_int64
sqlite3_column_int64(sqlite3_stmt
*pStmt
, int i
){
367 return sqlite3_value_int64( columnMem(pStmt
,i
) );
369 const unsigned char *sqlite3_column_text(sqlite3_stmt
*pStmt
, int i
){
370 return sqlite3_value_text( columnMem(pStmt
,i
) );
372 #ifndef SQLITE_OMIT_UTF16
373 const void *sqlite3_column_text16(sqlite3_stmt
*pStmt
, int i
){
374 return sqlite3_value_text16( columnMem(pStmt
,i
) );
376 #endif /* SQLITE_OMIT_UTF16 */
377 int sqlite3_column_type(sqlite3_stmt
*pStmt
, int i
){
378 return sqlite3_value_type( columnMem(pStmt
,i
) );
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.
385 ** There are up to 5 names for each column. useType determines which
386 ** name is returned. Here are the names:
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
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.
397 static const void *columnName(
400 const void *(*xFunc
)(Mem
*),
403 Vdbe
*p
= (Vdbe
*)pStmt
;
404 int n
= sqlite3_column_count(pStmt
);
406 if( p
==0 || N
>=n
|| N
<0 ){
410 return xFunc(&p
->aColName
[N
]);
415 ** Return the name of the Nth column of the result set returned by SQL
418 const char *sqlite3_column_name(sqlite3_stmt
*pStmt
, int N
){
419 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, 0);
421 #ifndef SQLITE_OMIT_UTF16
422 const void *sqlite3_column_name16(sqlite3_stmt
*pStmt
, int N
){
423 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, 0);
428 ** Return the column declaration type (if applicable) of the 'i'th column
429 ** of the result set of SQL statement pStmt.
431 const char *sqlite3_column_decltype(sqlite3_stmt
*pStmt
, int N
){
432 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, 1);
434 #ifndef SQLITE_OMIT_UTF16
435 const void *sqlite3_column_decltype16(sqlite3_stmt
*pStmt
, int N
){
436 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, 1);
438 #endif /* SQLITE_OMIT_UTF16 */
440 #if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0
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.
446 const char *sqlite3_column_database_name(sqlite3_stmt
*pStmt
, int N
){
447 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, 2);
449 #ifndef SQLITE_OMIT_UTF16
450 const void *sqlite3_column_database_name16(sqlite3_stmt
*pStmt
, int N
){
451 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, 2);
453 #endif /* SQLITE_OMIT_UTF16 */
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.
460 const char *sqlite3_column_table_name(sqlite3_stmt
*pStmt
, int N
){
461 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, 3);
463 #ifndef SQLITE_OMIT_UTF16
464 const void *sqlite3_column_table_name16(sqlite3_stmt
*pStmt
, int N
){
465 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, 3);
467 #endif /* SQLITE_OMIT_UTF16 */
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.
474 const char *sqlite3_column_origin_name(sqlite3_stmt
*pStmt
, int N
){
475 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text
, 4);
477 #ifndef SQLITE_OMIT_UTF16
478 const void *sqlite3_column_origin_name16(sqlite3_stmt
*pStmt
, int N
){
479 return columnName(pStmt
, N
, (const void*(*)(Mem
*))sqlite3_value_text16
, 4);
481 #endif /* SQLITE_OMIT_UTF16 */
482 #endif /* SQLITE_OMIT_ORIGIN_NAMES */
487 /******************************* sqlite3_bind_ ***************************
489 ** Routines used to attach values to wildcards in a compiled SQL statement.
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.
496 ** The error code stored in database p->db is overwritten with the return
497 ** value in any case.
499 static int vdbeUnbind(Vdbe
*p
, int i
){
501 if( p
==0 || p
->magic
!=VDBE_MAGIC_RUN
|| p
->pc
>=0 ){
502 if( p
) sqlite3Error(p
->db
, SQLITE_MISUSE
, 0);
503 return SQLITE_MISUSE
;
505 if( i
<1 || i
>p
->nVar
){
506 sqlite3Error(p
->db
, SQLITE_RANGE
, 0);
511 sqlite3VdbeMemRelease(pVar
);
512 pVar
->flags
= MEM_Null
;
513 sqlite3Error(p
->db
, SQLITE_OK
, 0);
518 ** Bind a text or BLOB value.
528 Vdbe
*p
= (Vdbe
*)pStmt
;
532 rc
= vdbeUnbind(p
, i
);
533 if( rc
|| zData
==0 ){
536 pVar
= &p
->aVar
[i
-1];
537 rc
= sqlite3VdbeMemSetStr(pVar
, zData
, nData
, encoding
, xDel
);
541 if( rc
==SQLITE_OK
&& encoding
!=0 ){
542 rc
= sqlite3VdbeChangeEncoding(pVar
, p
->db
->enc
);
549 ** Bind a blob value to an SQL statement variable.
551 int sqlite3_bind_blob(
558 return bindText(pStmt
, i
, zData
, nData
, xDel
, 0);
560 int sqlite3_bind_double(sqlite3_stmt
*pStmt
, int i
, double rValue
){
562 Vdbe
*p
= (Vdbe
*)pStmt
;
563 rc
= vdbeUnbind(p
, i
);
565 sqlite3VdbeMemSetDouble(&p
->aVar
[i
-1], rValue
);
569 int sqlite3_bind_int(sqlite3_stmt
*p
, int i
, int iValue
){
570 return sqlite3_bind_int64(p
, i
, (i64
)iValue
);
572 int sqlite3_bind_int64(sqlite3_stmt
*pStmt
, int i
, sqlite_int64 iValue
){
574 Vdbe
*p
= (Vdbe
*)pStmt
;
575 rc
= vdbeUnbind(p
, i
);
577 sqlite3VdbeMemSetInt64(&p
->aVar
[i
-1], iValue
);
581 int sqlite3_bind_null(sqlite3_stmt
* p
, int i
){
582 return vdbeUnbind((Vdbe
*)p
, i
);
584 int sqlite3_bind_text(
591 return bindText(pStmt
, i
, zData
, nData
, xDel
, SQLITE_UTF8
);
593 #ifndef SQLITE_OMIT_UTF16
594 int sqlite3_bind_text16(
601 return bindText(pStmt
, i
, zData
, nData
, xDel
, SQLITE_UTF16NATIVE
);
603 #endif /* SQLITE_OMIT_UTF16 */
606 ** Return the number of wildcards that can be potentially bound to.
607 ** This routine is added to support DBD::SQLite.
609 int sqlite3_bind_parameter_count(sqlite3_stmt
*pStmt
){
610 Vdbe
*p
= (Vdbe
*)pStmt
;
611 return p
? p
->nVar
: 0;
615 ** Create a mapping from variable numbers to variable names
616 ** in the Vdbe.azVar[] array, if such a mapping does not already
619 static void createVarMap(Vdbe
*p
){
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
;
634 ** Return the name of a wildcard parameter. Return NULL if the index
635 ** is out of range or if the wildcard is unnamed.
637 ** The result is always UTF-8.
639 const char *sqlite3_bind_parameter_name(sqlite3_stmt
*pStmt
, int i
){
640 Vdbe
*p
= (Vdbe
*)pStmt
;
641 if( p
==0 || i
<1 || i
>p
->nVar
){
645 return p
->azVar
[i
-1];
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,
653 int sqlite3_bind_parameter_index(sqlite3_stmt
*pStmt
, const char *zName
){
654 Vdbe
*p
= (Vdbe
*)pStmt
;
661 for(i
=0; i
<p
->nVar
; i
++){
662 const char *z
= p
->azVar
[i
];
663 if( z
&& strcmp(z
,zName
)==0 ){
672 ** 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.
676 int 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
;
684 if( pFrom
->nVar
!=pTo
->nVar
){
687 for(i
=0; rc
==SQLITE_OK
&& i
<pFrom
->nVar
; i
++){
688 rc
= sqlite3VdbeMemMove(&pTo
->aVar
[i
], &pFrom
->aVar
[i
]);
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.
699 sqlite3
*sqlite3_db_handle(sqlite3_stmt
*pStmt
){
700 return pStmt
? ((Vdbe
*)pStmt
)->db
: 0;