drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 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(). |
drh | 7e02e5e | 2011-12-06 19:44:51 +0000 | [diff] [blame] | 15 | ** |
drh | 678a9aa | 2011-12-10 15:55:01 +0000 | [diff] [blame] | 16 | ** The Vdbe parse-tree explainer is also found here. |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 17 | */ |
| 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 | */ |
drh | 5f18a22 | 2009-11-26 14:01:53 +0000 | [diff] [blame] | 29 | static int findNextHostParameter(const char *zSql, int *pnToken){ |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 30 | int tokenType; |
| 31 | int nTotal = 0; |
| 32 | int n; |
| 33 | |
drh | 5f18a22 | 2009-11-26 14:01:53 +0000 | [diff] [blame] | 34 | *pnToken = 0; |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 35 | while( zSql[0] ){ |
| 36 | n = sqlite3GetToken((u8*)zSql, &tokenType); |
| 37 | assert( n>0 && tokenType!=TK_ILLEGAL ); |
drh | 5f18a22 | 2009-11-26 14:01:53 +0000 | [diff] [blame] | 38 | if( tokenType==TK_VARIABLE ){ |
| 39 | *pnToken = n; |
| 40 | break; |
| 41 | } |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 42 | nTotal += n; |
| 43 | zSql += n; |
| 44 | } |
| 45 | return nTotal; |
| 46 | } |
| 47 | |
| 48 | /* |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 49 | ** This function returns a pointer to a nul-terminated string in memory |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 50 | ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 51 | ** string contains a copy of zRawSql but with host parameters expanded to |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 52 | ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1, |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 53 | ** then the returned string holds a copy of zRawSql with "-- " prepended |
| 54 | ** to each line of text. |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 55 | ** |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 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 | ** |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 61 | ** 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.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 67 | ** the host parameter index is found by scanning the prepared |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 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. |
| 71 | */ |
| 72 | char *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 */ |
drh | 1e63499 | 2009-11-28 13:46:51 +0000 | [diff] [blame] | 77 | int idx = 0; /* Index of a host parameter */ |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 78 | int nextIndex = 1; /* Index of next ? host parameter */ |
| 79 | int n; /* Length of a token prefix */ |
drh | 5f18a22 | 2009-11-26 14:01:53 +0000 | [diff] [blame] | 80 | int nToken; /* Length of the parameter token */ |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 81 | int i; /* Loop counter */ |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 82 | Mem *pVar; /* Value of a host parameter */ |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 83 | StrAccum out; /* Accumulate the output here */ |
drh | cf1e395 | 2016-07-23 00:43:14 +0000 | [diff] [blame] | 84 | #ifndef SQLITE_OMIT_UTF16 |
drh | f49759b | 2017-08-25 19:51:51 +0000 | [diff] [blame] | 85 | Mem utf8; /* Used to convert UTF16 into UTF8 for display */ |
drh | cf1e395 | 2016-07-23 00:43:14 +0000 | [diff] [blame] | 86 | #endif |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 87 | char zBase[100]; /* Initial working space */ |
| 88 | |
| 89 | db = p->db; |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 90 | sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase), |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 91 | db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 92 | if( db->nVdbeExec>1 ){ |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 93 | while( *zRawSql ){ |
| 94 | const char *zStart = zRawSql; |
| 95 | while( *(zRawSql++)!='\n' && *zRawSql ); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 96 | sqlite3_str_append(&out, "-- ", 3); |
drh | 39325ba | 2013-12-11 12:02:55 +0000 | [diff] [blame] | 97 | assert( (zRawSql - zStart) > 0 ); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 98 | sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart)); |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 99 | } |
drh | 816070c | 2015-04-18 19:20:14 +0000 | [diff] [blame] | 100 | }else if( p->nVar==0 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 101 | sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql)); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 102 | }else{ |
| 103 | while( zRawSql[0] ){ |
| 104 | n = findNextHostParameter(zRawSql, &nToken); |
| 105 | assert( n>0 ); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 106 | sqlite3_str_append(&out, zRawSql, n); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 107 | zRawSql += n; |
| 108 | assert( zRawSql[0] || nToken==0 ); |
| 109 | if( nToken==0 ) break; |
| 110 | if( zRawSql[0]=='?' ){ |
| 111 | if( nToken>1 ){ |
| 112 | assert( sqlite3Isdigit(zRawSql[1]) ); |
| 113 | sqlite3GetInt32(&zRawSql[1], &idx); |
| 114 | }else{ |
| 115 | idx = nextIndex; |
| 116 | } |
| 117 | }else{ |
drh | c982844 | 2015-04-18 00:22:17 +0000 | [diff] [blame] | 118 | assert( zRawSql[0]==':' || zRawSql[0]=='$' || |
| 119 | zRawSql[0]=='@' || zRawSql[0]=='#' ); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 120 | testcase( zRawSql[0]==':' ); |
| 121 | testcase( zRawSql[0]=='$' ); |
| 122 | testcase( zRawSql[0]=='@' ); |
drh | c982844 | 2015-04-18 00:22:17 +0000 | [diff] [blame] | 123 | testcase( zRawSql[0]=='#' ); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 124 | idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken); |
| 125 | assert( idx>0 ); |
| 126 | } |
| 127 | zRawSql += nToken; |
| 128 | nextIndex = idx + 1; |
| 129 | assert( idx>0 && idx<=p->nVar ); |
| 130 | pVar = &p->aVar[idx-1]; |
| 131 | if( pVar->flags & MEM_Null ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 132 | sqlite3_str_append(&out, "NULL", 4); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 133 | }else if( pVar->flags & MEM_Int ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 134 | sqlite3_str_appendf(&out, "%lld", pVar->u.i); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 135 | }else if( pVar->flags & MEM_Real ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 136 | sqlite3_str_appendf(&out, "%!.15g", pVar->u.r); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 137 | }else if( pVar->flags & MEM_Str ){ |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 138 | int nOut; /* Number of bytes of the string text to include in output */ |
drh | c1bd1b3 | 2009-11-25 19:35:23 +0000 | [diff] [blame] | 139 | #ifndef SQLITE_OMIT_UTF16 |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 140 | u8 enc = ENC(db); |
| 141 | if( enc!=SQLITE_UTF8 ){ |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 142 | memset(&utf8, 0, sizeof(utf8)); |
| 143 | utf8.db = db; |
drh | 8afffe7 | 2016-07-23 04:58:57 +0000 | [diff] [blame] | 144 | sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC); |
| 145 | if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 146 | out.accError = SQLITE_NOMEM; |
drh | 8afffe7 | 2016-07-23 04:58:57 +0000 | [diff] [blame] | 147 | out.nAlloc = 0; |
drh | cf1e395 | 2016-07-23 00:43:14 +0000 | [diff] [blame] | 148 | } |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 149 | pVar = &utf8; |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 150 | } |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 151 | #endif |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 152 | nOut = pVar->n; |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 153 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
drh | e8601c6 | 2013-05-17 17:15:34 +0000 | [diff] [blame] | 154 | if( nOut>SQLITE_TRACE_SIZE_LIMIT ){ |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 155 | nOut = SQLITE_TRACE_SIZE_LIMIT; |
drh | e8601c6 | 2013-05-17 17:15:34 +0000 | [diff] [blame] | 156 | while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; } |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 157 | } |
| 158 | #endif |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 159 | sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z); |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 160 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 161 | if( nOut<pVar->n ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 162 | sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut); |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 163 | } |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 164 | #endif |
| 165 | #ifndef SQLITE_OMIT_UTF16 |
| 166 | if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8); |
| 167 | #endif |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 168 | }else if( pVar->flags & MEM_Zero ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 169 | sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 170 | }else{ |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 171 | int nOut; /* Number of bytes of the blob to include in output */ |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 172 | assert( pVar->flags & MEM_Blob ); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 173 | sqlite3_str_append(&out, "x'", 2); |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 174 | nOut = pVar->n; |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 175 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 176 | if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT; |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 177 | #endif |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 178 | for(i=0; i<nOut; i++){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 179 | sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff); |
dan | 27381bd | 2011-01-22 13:32:29 +0000 | [diff] [blame] | 180 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 181 | sqlite3_str_append(&out, "'", 1); |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 182 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 183 | if( nOut<pVar->n ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 184 | sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut); |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 185 | } |
drh | 936c6d7 | 2013-04-02 13:56:53 +0000 | [diff] [blame] | 186 | #endif |
drh | c1bd1b3 | 2009-11-25 19:35:23 +0000 | [diff] [blame] | 187 | } |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 190 | if( out.accError ) sqlite3_str_reset(&out); |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 191 | return sqlite3StrAccumFinish(&out); |
| 192 | } |
| 193 | |
| 194 | #endif /* #ifndef SQLITE_OMIT_TRACE */ |