1 /*
2 ** 2009 November 25
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 used to insert the values of host parameters
14 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
15 */
16 #include "sqliteInt.h"
17 #include "vdbeInt.h"
18
19 #ifndef SQLITE_OMIT_TRACE
20
21 /*
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.
26 */
findNextHostParameter(const char * zSql,int * pnToken)27 static int findNextHostParameter(const char *zSql, int *pnToken){
28 int tokenType;
29 int nTotal = 0;
30 int n;
31
32 *pnToken = 0;
33 while( zSql[0] ){
34 n = sqlite3GetToken((u8*)zSql, &tokenType);
35 assert( n>0 && tokenType!=TK_ILLEGAL );
36 if( tokenType==TK_VARIABLE ){
37 *pnToken = n;
38 break;
39 }
40 nTotal += n;
41 zSql += n;
42 }
43 return nTotal;
44 }
45
46 /*
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.
53 **
54 ** The calling function is responsible for making sure the memory returned
55 ** is eventually freed.
56 **
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.
64 */
sqlite3VdbeExpandSql(Vdbe * p,const char * zRawSql)65 char *sqlite3VdbeExpandSql(
66 Vdbe *p, /* The prepared statement being evaluated */
67 const char *zRawSql /* Raw text of the SQL statement */
68 ){
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 */
78
79 db = p->db;
80 sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
81 db->aLimit[SQLITE_LIMIT_LENGTH]);
82 out.db = db;
83 if( db->vdbeExecCnt>1 ){
84 while( *zRawSql ){
85 const char *zStart = zRawSql;
86 while( *(zRawSql++)!='\n' && *zRawSql );
87 sqlite3StrAccumAppend(&out, "-- ", 3);
88 sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
89 }
90 }else{
91 while( zRawSql[0] ){
92 n = findNextHostParameter(zRawSql, &nToken);
93 assert( n>0 );
94 sqlite3StrAccumAppend(&out, zRawSql, n);
95 zRawSql += n;
96 assert( zRawSql[0] || nToken==0 );
97 if( nToken==0 ) break;
98 if( zRawSql[0]=='?' ){
99 if( nToken>1 ){
100 assert( sqlite3Isdigit(zRawSql[1]) );
101 sqlite3GetInt32(&zRawSql[1], &idx);
102 }else{
103 idx = nextIndex;
104 }
105 }else{
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);
111 assert( idx>0 );
112 }
113 zRawSql += nToken;
114 nextIndex = idx + 1;
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
125 u8 enc = ENC(db);
126 if( enc!=SQLITE_UTF8 ){
127 Mem utf8;
128 memset(&utf8, 0, sizeof(utf8));
129 utf8.db = db;
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);
134 }else
135 #endif
136 {
137 sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
138 }
139 }else if( pVar->flags & MEM_Zero ){
140 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
141 }else{
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);
146 }
147 sqlite3StrAccumAppend(&out, "'", 1);
148 }
149 }
150 }
151 return sqlite3StrAccumFinish(&out);
152 }
153
154 #endif /* #ifndef SQLITE_OMIT_TRACE */
155