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 used to insert the values of host parameters
14 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
16 ** The Vdbe parse-tree explainer is also found here.
18 #include "sqliteInt.h"
21 #ifndef SQLITE_OMIT_TRACE
24 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
25 ** bytes in this text up to but excluding the first character in
26 ** a host parameter. If the text contains no host parameters, return
27 ** the total number of bytes in the text.
29 static int findNextHostParameter(const char *zSql
, int *pnToken
){
36 n
= sqlite3GetToken((u8
*)zSql
, &tokenType
);
37 assert( n
>0 && tokenType
!=TK_ILLEGAL
);
38 if( tokenType
==TK_VARIABLE
){
49 ** This function returns a pointer to a nul-terminated string in memory
50 ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
51 ** string contains a copy of zRawSql but with host parameters expanded to
52 ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
53 ** then the returned string holds a copy of zRawSql with "-- " prepended
54 ** to each line of text.
56 ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
57 ** then long strings and blobs are truncated to that many bytes. This
58 ** can be used to prevent unreasonably large trace strings when dealing
59 ** with large (multi-megabyte) strings and blobs.
61 ** The calling function is responsible for making sure the memory returned
62 ** is eventually freed.
64 ** ALGORITHM: Scan the input string looking for host parameters in any of
65 ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
66 ** string literals, quoted identifier names, and comments. For text forms,
67 ** the host parameter index is found by scanning the prepared
68 ** statement for the corresponding OP_Variable opcode. Once the host
69 ** parameter index is known, locate the value in p->aVar[]. Then render
70 ** the value as a literal in place of the host parameter name.
72 char *sqlite3VdbeExpandSql(
73 Vdbe
*p
, /* The prepared statement being evaluated */
74 const char *zRawSql
/* Raw text of the SQL statement */
76 sqlite3
*db
; /* The database connection */
77 int idx
= 0; /* Index of a host parameter */
78 int nextIndex
= 1; /* Index of next ? host parameter */
79 int n
; /* Length of a token prefix */
80 int nToken
; /* Length of the parameter token */
81 int i
; /* Loop counter */
82 Mem
*pVar
; /* Value of a host parameter */
83 StrAccum out
; /* Accumulate the output here */
84 #ifndef SQLITE_OMIT_UTF16
85 Mem utf8
; /* Used to convert UTF16 into UTF8 for display */
89 sqlite3StrAccumInit(&out
, 0, 0, 0, db
->aLimit
[SQLITE_LIMIT_LENGTH
]);
90 if( db
->nVdbeExec
>1 ){
92 const char *zStart
= zRawSql
;
93 while( *(zRawSql
++)!='\n' && *zRawSql
);
94 sqlite3_str_append(&out
, "-- ", 3);
95 assert( (zRawSql
- zStart
) > 0 );
96 sqlite3_str_append(&out
, zStart
, (int)(zRawSql
-zStart
));
98 }else if( p
->nVar
==0 ){
99 sqlite3_str_append(&out
, zRawSql
, sqlite3Strlen30(zRawSql
));
102 n
= findNextHostParameter(zRawSql
, &nToken
);
104 sqlite3_str_append(&out
, zRawSql
, n
);
106 assert( zRawSql
[0] || nToken
==0 );
107 if( nToken
==0 ) break;
108 if( zRawSql
[0]=='?' ){
110 assert( sqlite3Isdigit(zRawSql
[1]) );
111 sqlite3GetInt32(&zRawSql
[1], &idx
);
116 assert( zRawSql
[0]==':' || zRawSql
[0]=='$' ||
117 zRawSql
[0]=='@' || zRawSql
[0]=='#' );
118 testcase( zRawSql
[0]==':' );
119 testcase( zRawSql
[0]=='$' );
120 testcase( zRawSql
[0]=='@' );
121 testcase( zRawSql
[0]=='#' );
122 idx
= sqlite3VdbeParameterIndex(p
, zRawSql
, nToken
);
126 nextIndex
= MAX(idx
+ 1, nextIndex
);
127 assert( idx
>0 && idx
<=p
->nVar
);
128 pVar
= &p
->aVar
[idx
-1];
129 if( pVar
->flags
& MEM_Null
){
130 sqlite3_str_append(&out
, "NULL", 4);
131 }else if( pVar
->flags
& (MEM_Int
|MEM_IntReal
) ){
132 sqlite3_str_appendf(&out
, "%lld", pVar
->u
.i
);
133 }else if( pVar
->flags
& MEM_Real
){
134 sqlite3_str_appendf(&out
, "%!.15g", pVar
->u
.r
);
135 }else if( pVar
->flags
& MEM_Str
){
136 int nOut
; /* Number of bytes of the string text to include in output */
137 #ifndef SQLITE_OMIT_UTF16
139 if( enc
!=SQLITE_UTF8
){
140 memset(&utf8
, 0, sizeof(utf8
));
142 sqlite3VdbeMemSetStr(&utf8
, pVar
->z
, pVar
->n
, enc
, SQLITE_STATIC
);
143 if( SQLITE_NOMEM
==sqlite3VdbeChangeEncoding(&utf8
, SQLITE_UTF8
) ){
144 out
.accError
= SQLITE_NOMEM
;
151 #ifdef SQLITE_TRACE_SIZE_LIMIT
152 if( nOut
>SQLITE_TRACE_SIZE_LIMIT
){
153 nOut
= SQLITE_TRACE_SIZE_LIMIT
;
154 while( nOut
<pVar
->n
&& (pVar
->z
[nOut
]&0xc0)==0x80 ){ nOut
++; }
157 sqlite3_str_appendf(&out
, "'%.*q'", nOut
, pVar
->z
);
158 #ifdef SQLITE_TRACE_SIZE_LIMIT
160 sqlite3_str_appendf(&out
, "/*+%d bytes*/", pVar
->n
-nOut
);
163 #ifndef SQLITE_OMIT_UTF16
164 if( enc
!=SQLITE_UTF8
) sqlite3VdbeMemRelease(&utf8
);
166 }else if( pVar
->flags
& MEM_Zero
){
167 sqlite3_str_appendf(&out
, "zeroblob(%d)", pVar
->u
.nZero
);
169 int nOut
; /* Number of bytes of the blob to include in output */
170 assert( pVar
->flags
& MEM_Blob
);
171 sqlite3_str_append(&out
, "x'", 2);
173 #ifdef SQLITE_TRACE_SIZE_LIMIT
174 if( nOut
>SQLITE_TRACE_SIZE_LIMIT
) nOut
= SQLITE_TRACE_SIZE_LIMIT
;
176 for(i
=0; i
<nOut
; i
++){
177 sqlite3_str_appendf(&out
, "%02x", pVar
->z
[i
]&0xff);
179 sqlite3_str_append(&out
, "'", 1);
180 #ifdef SQLITE_TRACE_SIZE_LIMIT
182 sqlite3_str_appendf(&out
, "/*+%d bytes*/", pVar
->n
-nOut
);
188 if( out
.accError
) sqlite3_str_reset(&out
);
189 return sqlite3StrAccumFinish(&out
);
192 #endif /* #ifndef SQLITE_OMIT_TRACE */