blob: c230b505547b1f9a86a260cf31c349a1e60c1a42 [file] [log] [blame]
drhc7bc4fd2009-11-25 18:03:42 +00001/*
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().
drh7e02e5e2011-12-06 19:44:51 +000015**
drh678a9aa2011-12-10 15:55:01 +000016** The Vdbe parse-tree explainer is also found here.
drhc7bc4fd2009-11-25 18:03:42 +000017*/
18#include "sqliteInt.h"
19#include "vdbeInt.h"
20
21#ifndef SQLITE_OMIT_TRACE
22
23/*
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.
28*/
drh5f18a222009-11-26 14:01:53 +000029static int findNextHostParameter(const char *zSql, int *pnToken){
drhc7bc4fd2009-11-25 18:03:42 +000030 int tokenType;
31 int nTotal = 0;
32 int n;
33
drh5f18a222009-11-26 14:01:53 +000034 *pnToken = 0;
drhc7bc4fd2009-11-25 18:03:42 +000035 while( zSql[0] ){
36 n = sqlite3GetToken((u8*)zSql, &tokenType);
37 assert( n>0 && tokenType!=TK_ILLEGAL );
drh5f18a222009-11-26 14:01:53 +000038 if( tokenType==TK_VARIABLE ){
39 *pnToken = n;
40 break;
41 }
drhc7bc4fd2009-11-25 18:03:42 +000042 nTotal += n;
43 zSql += n;
44 }
45 return nTotal;
46}
47
48/*
dan27381bd2011-01-22 13:32:29 +000049** This function returns a pointer to a nul-terminated string in memory
drh4f7d3a52013-06-27 23:54:02 +000050** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
dan27381bd2011-01-22 13:32:29 +000051** string contains a copy of zRawSql but with host parameters expanded to
drh4f7d3a52013-06-27 23:54:02 +000052** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
dan27381bd2011-01-22 13:32:29 +000053** then the returned string holds a copy of zRawSql with "-- " prepended
54** to each line of text.
drhc7bc4fd2009-11-25 18:03:42 +000055**
drh936c6d72013-04-02 13:56:53 +000056** 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.
60**
drhc7bc4fd2009-11-25 18:03:42 +000061** The calling function is responsible for making sure the memory returned
62** is eventually freed.
63**
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,
peter.d.reid60ec9142014-09-06 16:39:46 +000067** the host parameter index is found by scanning the prepared
drhc7bc4fd2009-11-25 18:03:42 +000068** 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.
71*/
72char *sqlite3VdbeExpandSql(
73 Vdbe *p, /* The prepared statement being evaluated */
74 const char *zRawSql /* Raw text of the SQL statement */
75){
76 sqlite3 *db; /* The database connection */
drh1e634992009-11-28 13:46:51 +000077 int idx = 0; /* Index of a host parameter */
drhc7bc4fd2009-11-25 18:03:42 +000078 int nextIndex = 1; /* Index of next ? host parameter */
79 int n; /* Length of a token prefix */
drh5f18a222009-11-26 14:01:53 +000080 int nToken; /* Length of the parameter token */
drhc7bc4fd2009-11-25 18:03:42 +000081 int i; /* Loop counter */
drhc7bc4fd2009-11-25 18:03:42 +000082 Mem *pVar; /* Value of a host parameter */
drhc7bc4fd2009-11-25 18:03:42 +000083 StrAccum out; /* Accumulate the output here */
84 char zBase[100]; /* Initial working space */
85
86 db = p->db;
drhc0490572015-05-02 11:45:53 +000087 sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase),
drhc7bc4fd2009-11-25 18:03:42 +000088 db->aLimit[SQLITE_LIMIT_LENGTH]);
drh4f7d3a52013-06-27 23:54:02 +000089 if( db->nVdbeExec>1 ){
dan27381bd2011-01-22 13:32:29 +000090 while( *zRawSql ){
91 const char *zStart = zRawSql;
92 while( *(zRawSql++)!='\n' && *zRawSql );
93 sqlite3StrAccumAppend(&out, "-- ", 3);
drh39325ba2013-12-11 12:02:55 +000094 assert( (zRawSql - zStart) > 0 );
95 sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
drhc7bc4fd2009-11-25 18:03:42 +000096 }
drh816070c2015-04-18 19:20:14 +000097 }else if( p->nVar==0 ){
98 sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql));
dan27381bd2011-01-22 13:32:29 +000099 }else{
100 while( zRawSql[0] ){
101 n = findNextHostParameter(zRawSql, &nToken);
102 assert( n>0 );
103 sqlite3StrAccumAppend(&out, zRawSql, n);
104 zRawSql += n;
105 assert( zRawSql[0] || nToken==0 );
106 if( nToken==0 ) break;
107 if( zRawSql[0]=='?' ){
108 if( nToken>1 ){
109 assert( sqlite3Isdigit(zRawSql[1]) );
110 sqlite3GetInt32(&zRawSql[1], &idx);
111 }else{
112 idx = nextIndex;
113 }
114 }else{
drhc9828442015-04-18 00:22:17 +0000115 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
116 zRawSql[0]=='@' || zRawSql[0]=='#' );
dan27381bd2011-01-22 13:32:29 +0000117 testcase( zRawSql[0]==':' );
118 testcase( zRawSql[0]=='$' );
119 testcase( zRawSql[0]=='@' );
drhc9828442015-04-18 00:22:17 +0000120 testcase( zRawSql[0]=='#' );
dan27381bd2011-01-22 13:32:29 +0000121 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
122 assert( idx>0 );
123 }
124 zRawSql += nToken;
125 nextIndex = idx + 1;
126 assert( idx>0 && idx<=p->nVar );
127 pVar = &p->aVar[idx-1];
128 if( pVar->flags & MEM_Null ){
129 sqlite3StrAccumAppend(&out, "NULL", 4);
130 }else if( pVar->flags & MEM_Int ){
drha5c14162013-12-17 15:03:06 +0000131 sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
dan27381bd2011-01-22 13:32:29 +0000132 }else if( pVar->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000133 sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r);
dan27381bd2011-01-22 13:32:29 +0000134 }else if( pVar->flags & MEM_Str ){
drhda8caa02013-04-22 23:38:50 +0000135 int nOut; /* Number of bytes of the string text to include in output */
drhc1bd1b32009-11-25 19:35:23 +0000136#ifndef SQLITE_OMIT_UTF16
dan27381bd2011-01-22 13:32:29 +0000137 u8 enc = ENC(db);
drh936c6d72013-04-02 13:56:53 +0000138 Mem utf8;
dan27381bd2011-01-22 13:32:29 +0000139 if( enc!=SQLITE_UTF8 ){
dan27381bd2011-01-22 13:32:29 +0000140 memset(&utf8, 0, sizeof(utf8));
141 utf8.db = db;
142 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
143 sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
drh936c6d72013-04-02 13:56:53 +0000144 pVar = &utf8;
dan27381bd2011-01-22 13:32:29 +0000145 }
drh936c6d72013-04-02 13:56:53 +0000146#endif
drhda8caa02013-04-22 23:38:50 +0000147 nOut = pVar->n;
drh936c6d72013-04-02 13:56:53 +0000148#ifdef SQLITE_TRACE_SIZE_LIMIT
drhe8601c62013-05-17 17:15:34 +0000149 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
drhda8caa02013-04-22 23:38:50 +0000150 nOut = SQLITE_TRACE_SIZE_LIMIT;
drhe8601c62013-05-17 17:15:34 +0000151 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
drh936c6d72013-04-02 13:56:53 +0000152 }
153#endif
drha5c14162013-12-17 15:03:06 +0000154 sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
drh936c6d72013-04-02 13:56:53 +0000155#ifdef SQLITE_TRACE_SIZE_LIMIT
drha5c14162013-12-17 15:03:06 +0000156 if( nOut<pVar->n ){
157 sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
158 }
drh936c6d72013-04-02 13:56:53 +0000159#endif
160#ifndef SQLITE_OMIT_UTF16
161 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
162#endif
dan27381bd2011-01-22 13:32:29 +0000163 }else if( pVar->flags & MEM_Zero ){
drha5c14162013-12-17 15:03:06 +0000164 sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
dan27381bd2011-01-22 13:32:29 +0000165 }else{
drhda8caa02013-04-22 23:38:50 +0000166 int nOut; /* Number of bytes of the blob to include in output */
dan27381bd2011-01-22 13:32:29 +0000167 assert( pVar->flags & MEM_Blob );
168 sqlite3StrAccumAppend(&out, "x'", 2);
drhda8caa02013-04-22 23:38:50 +0000169 nOut = pVar->n;
drh936c6d72013-04-02 13:56:53 +0000170#ifdef SQLITE_TRACE_SIZE_LIMIT
drhda8caa02013-04-22 23:38:50 +0000171 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
drh936c6d72013-04-02 13:56:53 +0000172#endif
drhda8caa02013-04-22 23:38:50 +0000173 for(i=0; i<nOut; i++){
drha5c14162013-12-17 15:03:06 +0000174 sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
dan27381bd2011-01-22 13:32:29 +0000175 }
176 sqlite3StrAccumAppend(&out, "'", 1);
drh936c6d72013-04-02 13:56:53 +0000177#ifdef SQLITE_TRACE_SIZE_LIMIT
drha5c14162013-12-17 15:03:06 +0000178 if( nOut<pVar->n ){
179 sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
180 }
drh936c6d72013-04-02 13:56:53 +0000181#endif
drhc1bd1b32009-11-25 19:35:23 +0000182 }
drhc7bc4fd2009-11-25 18:03:42 +0000183 }
184 }
185 return sqlite3StrAccumFinish(&out);
186}
187
188#endif /* #ifndef SQLITE_OMIT_TRACE */