OLD | NEW |
1 /* | 1 /* |
2 ** 2009 November 25 | 2 ** 2009 November 25 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** | 12 ** |
13 ** This file contains code used to insert the values of host parameters | 13 ** This file contains code used to insert the values of host parameters |
14 ** (aka "wildcards") into the SQL text output by sqlite3_trace(). | 14 ** (aka "wildcards") into the SQL text output by sqlite3_trace(). |
| 15 ** |
| 16 ** The Vdbe parse-tree explainer is also found here. |
15 */ | 17 */ |
16 #include "sqliteInt.h" | 18 #include "sqliteInt.h" |
17 #include "vdbeInt.h" | 19 #include "vdbeInt.h" |
18 | 20 |
19 #ifndef SQLITE_OMIT_TRACE | 21 #ifndef SQLITE_OMIT_TRACE |
20 | 22 |
21 /* | 23 /* |
22 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of | 24 ** 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 | 25 ** bytes in this text up to but excluding the first character in |
24 ** a host parameter. If the text contains no host parameters, return | 26 ** a host parameter. If the text contains no host parameters, return |
(...skipping 13 matching lines...) Expand all Loading... |
38 break; | 40 break; |
39 } | 41 } |
40 nTotal += n; | 42 nTotal += n; |
41 zSql += n; | 43 zSql += n; |
42 } | 44 } |
43 return nTotal; | 45 return nTotal; |
44 } | 46 } |
45 | 47 |
46 /* | 48 /* |
47 ** This function returns a pointer to a nul-terminated string in memory | 49 ** This function returns a pointer to a nul-terminated string in memory |
48 ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the | 50 ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the |
49 ** string contains a copy of zRawSql but with host parameters expanded to | 51 ** string contains a copy of zRawSql but with host parameters expanded to |
50 ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1, | 52 ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1, |
51 ** then the returned string holds a copy of zRawSql with "-- " prepended | 53 ** then the returned string holds a copy of zRawSql with "-- " prepended |
52 ** to each line of text. | 54 ** to each line of text. |
53 ** | 55 ** |
| 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. |
| 60 ** |
54 ** The calling function is responsible for making sure the memory returned | 61 ** The calling function is responsible for making sure the memory returned |
55 ** is eventually freed. | 62 ** is eventually freed. |
56 ** | 63 ** |
57 ** ALGORITHM: Scan the input string looking for host parameters in any of | 64 ** 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 | 65 ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within |
59 ** string literals, quoted identifier names, and comments. For text forms, | 66 ** string literals, quoted identifier names, and comments. For text forms, |
60 ** the host parameter index is found by scanning the perpared | 67 ** the host parameter index is found by scanning the prepared |
61 ** statement for the corresponding OP_Variable opcode. Once the host | 68 ** statement for the corresponding OP_Variable opcode. Once the host |
62 ** parameter index is known, locate the value in p->aVar[]. Then render | 69 ** 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. | 70 ** the value as a literal in place of the host parameter name. |
64 */ | 71 */ |
65 char *sqlite3VdbeExpandSql( | 72 char *sqlite3VdbeExpandSql( |
66 Vdbe *p, /* The prepared statement being evaluated */ | 73 Vdbe *p, /* The prepared statement being evaluated */ |
67 const char *zRawSql /* Raw text of the SQL statement */ | 74 const char *zRawSql /* Raw text of the SQL statement */ |
68 ){ | 75 ){ |
69 sqlite3 *db; /* The database connection */ | 76 sqlite3 *db; /* The database connection */ |
70 int idx = 0; /* Index of a host parameter */ | 77 int idx = 0; /* Index of a host parameter */ |
71 int nextIndex = 1; /* Index of next ? host parameter */ | 78 int nextIndex = 1; /* Index of next ? host parameter */ |
72 int n; /* Length of a token prefix */ | 79 int n; /* Length of a token prefix */ |
73 int nToken; /* Length of the parameter token */ | 80 int nToken; /* Length of the parameter token */ |
74 int i; /* Loop counter */ | 81 int i; /* Loop counter */ |
75 Mem *pVar; /* Value of a host parameter */ | 82 Mem *pVar; /* Value of a host parameter */ |
76 StrAccum out; /* Accumulate the output here */ | 83 StrAccum out; /* Accumulate the output here */ |
77 char zBase[100]; /* Initial working space */ | 84 char zBase[100]; /* Initial working space */ |
78 | 85 |
79 db = p->db; | 86 db = p->db; |
80 sqlite3StrAccumInit(&out, zBase, sizeof(zBase), | 87 sqlite3StrAccumInit(&out, zBase, sizeof(zBase), |
81 db->aLimit[SQLITE_LIMIT_LENGTH]); | 88 db->aLimit[SQLITE_LIMIT_LENGTH]); |
82 out.db = db; | 89 out.db = db; |
83 if( db->vdbeExecCnt>1 ){ | 90 if( db->nVdbeExec>1 ){ |
84 while( *zRawSql ){ | 91 while( *zRawSql ){ |
85 const char *zStart = zRawSql; | 92 const char *zStart = zRawSql; |
86 while( *(zRawSql++)!='\n' && *zRawSql ); | 93 while( *(zRawSql++)!='\n' && *zRawSql ); |
87 sqlite3StrAccumAppend(&out, "-- ", 3); | 94 sqlite3StrAccumAppend(&out, "-- ", 3); |
| 95 assert( (zRawSql - zStart) > 0 ); |
88 sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart)); | 96 sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart)); |
89 } | 97 } |
90 }else{ | 98 }else{ |
91 while( zRawSql[0] ){ | 99 while( zRawSql[0] ){ |
92 n = findNextHostParameter(zRawSql, &nToken); | 100 n = findNextHostParameter(zRawSql, &nToken); |
93 assert( n>0 ); | 101 assert( n>0 ); |
94 sqlite3StrAccumAppend(&out, zRawSql, n); | 102 sqlite3StrAccumAppend(&out, zRawSql, n); |
95 zRawSql += n; | 103 zRawSql += n; |
96 assert( zRawSql[0] || nToken==0 ); | 104 assert( zRawSql[0] || nToken==0 ); |
97 if( nToken==0 ) break; | 105 if( nToken==0 ) break; |
(...skipping 12 matching lines...) Expand all Loading... |
110 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken); | 118 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken); |
111 assert( idx>0 ); | 119 assert( idx>0 ); |
112 } | 120 } |
113 zRawSql += nToken; | 121 zRawSql += nToken; |
114 nextIndex = idx + 1; | 122 nextIndex = idx + 1; |
115 assert( idx>0 && idx<=p->nVar ); | 123 assert( idx>0 && idx<=p->nVar ); |
116 pVar = &p->aVar[idx-1]; | 124 pVar = &p->aVar[idx-1]; |
117 if( pVar->flags & MEM_Null ){ | 125 if( pVar->flags & MEM_Null ){ |
118 sqlite3StrAccumAppend(&out, "NULL", 4); | 126 sqlite3StrAccumAppend(&out, "NULL", 4); |
119 }else if( pVar->flags & MEM_Int ){ | 127 }else if( pVar->flags & MEM_Int ){ |
120 sqlite3XPrintf(&out, "%lld", pVar->u.i); | 128 sqlite3XPrintf(&out, 0, "%lld", pVar->u.i); |
121 }else if( pVar->flags & MEM_Real ){ | 129 }else if( pVar->flags & MEM_Real ){ |
122 sqlite3XPrintf(&out, "%!.15g", pVar->r); | 130 sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r); |
123 }else if( pVar->flags & MEM_Str ){ | 131 }else if( pVar->flags & MEM_Str ){ |
| 132 int nOut; /* Number of bytes of the string text to include in output */ |
124 #ifndef SQLITE_OMIT_UTF16 | 133 #ifndef SQLITE_OMIT_UTF16 |
125 u8 enc = ENC(db); | 134 u8 enc = ENC(db); |
| 135 Mem utf8; |
126 if( enc!=SQLITE_UTF8 ){ | 136 if( enc!=SQLITE_UTF8 ){ |
127 Mem utf8; | |
128 memset(&utf8, 0, sizeof(utf8)); | 137 memset(&utf8, 0, sizeof(utf8)); |
129 utf8.db = db; | 138 utf8.db = db; |
130 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC); | 139 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC); |
131 sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8); | 140 sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8); |
132 sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z); | 141 pVar = &utf8; |
133 sqlite3VdbeMemRelease(&utf8); | 142 } |
134 }else | |
135 #endif | 143 #endif |
136 { | 144 nOut = pVar->n; |
137 sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z); | 145 #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 146 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){ |
| 147 nOut = SQLITE_TRACE_SIZE_LIMIT; |
| 148 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; } |
138 } | 149 } |
| 150 #endif |
| 151 sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z); |
| 152 #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 153 if( nOut<pVar->n ){ |
| 154 sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut); |
| 155 } |
| 156 #endif |
| 157 #ifndef SQLITE_OMIT_UTF16 |
| 158 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8); |
| 159 #endif |
139 }else if( pVar->flags & MEM_Zero ){ | 160 }else if( pVar->flags & MEM_Zero ){ |
140 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero); | 161 sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero); |
141 }else{ | 162 }else{ |
| 163 int nOut; /* Number of bytes of the blob to include in output */ |
142 assert( pVar->flags & MEM_Blob ); | 164 assert( pVar->flags & MEM_Blob ); |
143 sqlite3StrAccumAppend(&out, "x'", 2); | 165 sqlite3StrAccumAppend(&out, "x'", 2); |
144 for(i=0; i<pVar->n; i++){ | 166 nOut = pVar->n; |
145 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff); | 167 #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 168 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT; |
| 169 #endif |
| 170 for(i=0; i<nOut; i++){ |
| 171 sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff); |
146 } | 172 } |
147 sqlite3StrAccumAppend(&out, "'", 1); | 173 sqlite3StrAccumAppend(&out, "'", 1); |
| 174 #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 175 if( nOut<pVar->n ){ |
| 176 sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut); |
| 177 } |
| 178 #endif |
148 } | 179 } |
149 } | 180 } |
150 } | 181 } |
151 return sqlite3StrAccumFinish(&out); | 182 return sqlite3StrAccumFinish(&out); |
152 } | 183 } |
153 | 184 |
154 #endif /* #ifndef SQLITE_OMIT_TRACE */ | 185 #endif /* #ifndef SQLITE_OMIT_TRACE */ |
OLD | NEW |