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 #include "sqliteInt.h"
19 #ifndef SQLITE_OMIT_TRACE
22 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
23 ** bytes in this text up to but excluding the first character in
24 ** a host parameter. If the text contains no host parameters, return
25 ** the total number of bytes in the text.
27 static int findNextHostParameter(const char *zSql
, int *pnToken
){
34 n
= sqlite3GetToken((u8
*)zSql
, &tokenType
);
35 assert( n
>0 && tokenType
!=TK_ILLEGAL
);
36 if( tokenType
==TK_VARIABLE
){
47 ** This function returns a pointer to a nul-terminated string in memory
48 ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
49 ** string contains a copy of zRawSql but with host parameters expanded to
50 ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
51 ** then the returned string holds a copy of zRawSql with "-- " prepended
52 ** to each line of text.
54 ** The calling function is responsible for making sure the memory returned
55 ** is eventually freed.
57 ** ALGORITHM: Scan the input string looking for host parameters in any of
58 ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
59 ** string literals, quoted identifier names, and comments. For text forms,
60 ** the host parameter index is found by scanning the perpared
61 ** statement for the corresponding OP_Variable opcode. Once the host
62 ** parameter index is known, locate the value in p->aVar[]. Then render
63 ** the value as a literal in place of the host parameter name.
65 char *sqlite3VdbeExpandSql(
66 Vdbe
*p
, /* The prepared statement being evaluated */
67 const char *zRawSql
/* Raw text of the SQL statement */
69 sqlite3
*db
; /* The database connection */
70 int idx
= 0; /* Index of a host parameter */
71 int nextIndex
= 1; /* Index of next ? host parameter */
72 int n
; /* Length of a token prefix */
73 int nToken
; /* Length of the parameter token */
74 int i
; /* Loop counter */
75 Mem
*pVar
; /* Value of a host parameter */
76 StrAccum out
; /* Accumulate the output here */
77 char zBase
[100]; /* Initial working space */
80 sqlite3StrAccumInit(&out
, zBase
, sizeof(zBase
),
81 db
->aLimit
[SQLITE_LIMIT_LENGTH
]);
83 if( db
->vdbeExecCnt
>1 ){
85 const char *zStart
= zRawSql
;
86 while( *(zRawSql
++)!='\n' && *zRawSql
);
87 sqlite3StrAccumAppend(&out
, "-- ", 3);
88 sqlite3StrAccumAppend(&out
, zStart
, (int)(zRawSql
-zStart
));
92 n
= findNextHostParameter(zRawSql
, &nToken
);
94 sqlite3StrAccumAppend(&out
, zRawSql
, n
);
96 assert( zRawSql
[0] || nToken
==0 );
97 if( nToken
==0 ) break;
98 if( zRawSql
[0]=='?' ){
100 assert( sqlite3Isdigit(zRawSql
[1]) );
101 sqlite3GetInt32(&zRawSql
[1], &idx
);
106 assert( zRawSql
[0]==':' || zRawSql
[0]=='$' || zRawSql
[0]=='@' );
107 testcase( zRawSql
[0]==':' );
108 testcase( zRawSql
[0]=='$' );
109 testcase( zRawSql
[0]=='@' );
110 idx
= sqlite3VdbeParameterIndex(p
, zRawSql
, nToken
);
115 assert( idx
>0 && idx
<=p
->nVar
);
116 pVar
= &p
->aVar
[idx
-1];
117 if( pVar
->flags
& MEM_Null
){
118 sqlite3StrAccumAppend(&out
, "NULL", 4);
119 }else if( pVar
->flags
& MEM_Int
){
120 sqlite3XPrintf(&out
, "%lld", pVar
->u
.i
);
121 }else if( pVar
->flags
& MEM_Real
){
122 sqlite3XPrintf(&out
, "%!.15g", pVar
->r
);
123 }else if( pVar
->flags
& MEM_Str
){
124 #ifndef SQLITE_OMIT_UTF16
126 if( enc
!=SQLITE_UTF8
){
128 memset(&utf8
, 0, sizeof(utf8
));
130 sqlite3VdbeMemSetStr(&utf8
, pVar
->z
, pVar
->n
, enc
, SQLITE_STATIC
);
131 sqlite3VdbeChangeEncoding(&utf8
, SQLITE_UTF8
);
132 sqlite3XPrintf(&out
, "'%.*q'", utf8
.n
, utf8
.z
);
133 sqlite3VdbeMemRelease(&utf8
);
137 sqlite3XPrintf(&out
, "'%.*q'", pVar
->n
, pVar
->z
);
139 }else if( pVar
->flags
& MEM_Zero
){
140 sqlite3XPrintf(&out
, "zeroblob(%d)", pVar
->u
.nZero
);
142 assert( pVar
->flags
& MEM_Blob
);
143 sqlite3StrAccumAppend(&out
, "x'", 2);
144 for(i
=0; i
<pVar
->n
; i
++){
145 sqlite3XPrintf(&out
, "%02x", pVar
->z
[i
]&0xff);
147 sqlite3StrAccumAppend(&out
, "'", 1);
151 return sqlite3StrAccumFinish(&out
);
154 #endif /* #ifndef SQLITE_OMIT_TRACE */