Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: ae8ad3115fe60dfdf2b812f45a87d4789e20d7d5 [file] [log] [blame]
drhc7bc4fd2009-11-25 18:03:421/*
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:5115**
drh678a9aa2011-12-10 15:55:0116** The Vdbe parse-tree explainer is also found here.
drhc7bc4fd2009-11-25 18:03:4217*/
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:5329static int findNextHostParameter(const char *zSql, int *pnToken){
drhc7bc4fd2009-11-25 18:03:4230 int tokenType;
31 int nTotal = 0;
32 int n;
33
drh5f18a222009-11-26 14:01:5334 *pnToken = 0;
drhc7bc4fd2009-11-25 18:03:4235 while( zSql[0] ){
36 n = sqlite3GetToken((u8*)zSql, &tokenType);
37 assert( n>0 && tokenType!=TK_ILLEGAL );
drh5f18a222009-11-26 14:01:5338 if( tokenType==TK_VARIABLE ){
39 *pnToken = n;
40 break;
41 }
drhc7bc4fd2009-11-25 18:03:4242 nTotal += n;
43 zSql += n;
44 }
45 return nTotal;
46}
47
48/*
dan27381bd2011-01-22 13:32:2949** This function returns a pointer to a nul-terminated string in memory
drh4f7d3a52013-06-27 23:54:0250** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
dan27381bd2011-01-22 13:32:2951** string contains a copy of zRawSql but with host parameters expanded to
drh4f7d3a52013-06-27 23:54:0252** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
dan27381bd2011-01-22 13:32:2953** then the returned string holds a copy of zRawSql with "-- " prepended
54** to each line of text.
drhc7bc4fd2009-11-25 18:03:4255**
drh936c6d72013-04-02 13:56:5356** 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:4261** 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:4667** the host parameter index is found by scanning the prepared
drhc7bc4fd2009-11-25 18:03:4268** 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:5177 int idx = 0; /* Index of a host parameter */
drhc7bc4fd2009-11-25 18:03:4278 int nextIndex = 1; /* Index of next ? host parameter */
79 int n; /* Length of a token prefix */
drh5f18a222009-11-26 14:01:5380 int nToken; /* Length of the parameter token */
drhc7bc4fd2009-11-25 18:03:4281 int i; /* Loop counter */
drhc7bc4fd2009-11-25 18:03:4282 Mem *pVar; /* Value of a host parameter */
drhc7bc4fd2009-11-25 18:03:4283 StrAccum out; /* Accumulate the output here */
drhcf1e3952016-07-23 00:43:1484#ifndef SQLITE_OMIT_UTF16
drhf49759b2017-08-25 19:51:5185 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
drhcf1e3952016-07-23 00:43:1486#endif
drhc7bc4fd2009-11-25 18:03:4287
88 db = p->db;
drh9673cd02021-10-02 17:34:2889 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
drh4f7d3a52013-06-27 23:54:0290 if( db->nVdbeExec>1 ){
dan27381bd2011-01-22 13:32:2991 while( *zRawSql ){
92 const char *zStart = zRawSql;
93 while( *(zRawSql++)!='\n' && *zRawSql );
drh0cdbe1a2018-05-09 13:46:2694 sqlite3_str_append(&out, "-- ", 3);
drh39325ba2013-12-11 12:02:5595 assert( (zRawSql - zStart) > 0 );
drh0cdbe1a2018-05-09 13:46:2696 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
drhc7bc4fd2009-11-25 18:03:4297 }
drh816070c2015-04-18 19:20:1498 }else if( p->nVar==0 ){
drh0cdbe1a2018-05-09 13:46:2699 sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
dan27381bd2011-01-22 13:32:29100 }else{
101 while( zRawSql[0] ){
102 n = findNextHostParameter(zRawSql, &nToken);
103 assert( n>0 );
drh0cdbe1a2018-05-09 13:46:26104 sqlite3_str_append(&out, zRawSql, n);
dan27381bd2011-01-22 13:32:29105 zRawSql += n;
106 assert( zRawSql[0] || nToken==0 );
107 if( nToken==0 ) break;
108 if( zRawSql[0]=='?' ){
109 if( nToken>1 ){
110 assert( sqlite3Isdigit(zRawSql[1]) );
111 sqlite3GetInt32(&zRawSql[1], &idx);
112 }else{
113 idx = nextIndex;
114 }
115 }else{
drhc9828442015-04-18 00:22:17116 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
117 zRawSql[0]=='@' || zRawSql[0]=='#' );
dan27381bd2011-01-22 13:32:29118 testcase( zRawSql[0]==':' );
119 testcase( zRawSql[0]=='$' );
120 testcase( zRawSql[0]=='@' );
drhc9828442015-04-18 00:22:17121 testcase( zRawSql[0]=='#' );
dan27381bd2011-01-22 13:32:29122 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
123 assert( idx>0 );
124 }
125 zRawSql += nToken;
dan13dd0222020-12-17 11:24:26126 nextIndex = MAX(idx + 1, nextIndex);
dan27381bd2011-01-22 13:32:29127 assert( idx>0 && idx<=p->nVar );
128 pVar = &p->aVar[idx-1];
129 if( pVar->flags & MEM_Null ){
drh0cdbe1a2018-05-09 13:46:26130 sqlite3_str_append(&out, "NULL", 4);
drh169f0772019-05-02 21:36:26131 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
drh0cdbe1a2018-05-09 13:46:26132 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
dan27381bd2011-01-22 13:32:29133 }else if( pVar->flags & MEM_Real ){
drh0cdbe1a2018-05-09 13:46:26134 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
dan27381bd2011-01-22 13:32:29135 }else if( pVar->flags & MEM_Str ){
drhda8caa02013-04-22 23:38:50136 int nOut; /* Number of bytes of the string text to include in output */
drhc1bd1b32009-11-25 19:35:23137#ifndef SQLITE_OMIT_UTF16
dan27381bd2011-01-22 13:32:29138 u8 enc = ENC(db);
dan27381bd2011-01-22 13:32:29139 if( enc!=SQLITE_UTF8 ){
dan27381bd2011-01-22 13:32:29140 memset(&utf8, 0, sizeof(utf8));
141 utf8.db = db;
drh8afffe72016-07-23 04:58:57142 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
143 if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
drh0cdbe1a2018-05-09 13:46:26144 out.accError = SQLITE_NOMEM;
drh8afffe72016-07-23 04:58:57145 out.nAlloc = 0;
drhcf1e3952016-07-23 00:43:14146 }
drh936c6d72013-04-02 13:56:53147 pVar = &utf8;
dan27381bd2011-01-22 13:32:29148 }
drh936c6d72013-04-02 13:56:53149#endif
drhda8caa02013-04-22 23:38:50150 nOut = pVar->n;
drh936c6d72013-04-02 13:56:53151#ifdef SQLITE_TRACE_SIZE_LIMIT
drhe8601c62013-05-17 17:15:34152 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
drhda8caa02013-04-22 23:38:50153 nOut = SQLITE_TRACE_SIZE_LIMIT;
drhe8601c62013-05-17 17:15:34154 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
drh936c6d72013-04-02 13:56:53155 }
156#endif
drh0cdbe1a2018-05-09 13:46:26157 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
drh936c6d72013-04-02 13:56:53158#ifdef SQLITE_TRACE_SIZE_LIMIT
drha5c14162013-12-17 15:03:06159 if( nOut<pVar->n ){
drh0cdbe1a2018-05-09 13:46:26160 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
drha5c14162013-12-17 15:03:06161 }
drh936c6d72013-04-02 13:56:53162#endif
163#ifndef SQLITE_OMIT_UTF16
164 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
165#endif
dan27381bd2011-01-22 13:32:29166 }else if( pVar->flags & MEM_Zero ){
drh0cdbe1a2018-05-09 13:46:26167 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
dan27381bd2011-01-22 13:32:29168 }else{
drhda8caa02013-04-22 23:38:50169 int nOut; /* Number of bytes of the blob to include in output */
dan27381bd2011-01-22 13:32:29170 assert( pVar->flags & MEM_Blob );
drh0cdbe1a2018-05-09 13:46:26171 sqlite3_str_append(&out, "x'", 2);
drhda8caa02013-04-22 23:38:50172 nOut = pVar->n;
drh936c6d72013-04-02 13:56:53173#ifdef SQLITE_TRACE_SIZE_LIMIT
drhda8caa02013-04-22 23:38:50174 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
drh936c6d72013-04-02 13:56:53175#endif
drhda8caa02013-04-22 23:38:50176 for(i=0; i<nOut; i++){
drh0cdbe1a2018-05-09 13:46:26177 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
dan27381bd2011-01-22 13:32:29178 }
drh0cdbe1a2018-05-09 13:46:26179 sqlite3_str_append(&out, "'", 1);
drh936c6d72013-04-02 13:56:53180#ifdef SQLITE_TRACE_SIZE_LIMIT
drha5c14162013-12-17 15:03:06181 if( nOut<pVar->n ){
drh0cdbe1a2018-05-09 13:46:26182 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
drha5c14162013-12-17 15:03:06183 }
drh936c6d72013-04-02 13:56:53184#endif
drhc1bd1b32009-11-25 19:35:23185 }
drhc7bc4fd2009-11-25 18:03:42186 }
187 }
drh0cdbe1a2018-05-09 13:46:26188 if( out.accError ) sqlite3_str_reset(&out);
drhc7bc4fd2009-11-25 18:03:42189 return sqlite3StrAccumFinish(&out);
190}
191
192#endif /* #ifndef SQLITE_OMIT_TRACE */