vdbetrace.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. ** The Vdbe parse-tree explainer is also found here.
  17. */
  18. #include "sqliteInt.h"
  19. #include "vdbeInt.h"
  20. #ifndef SQLITE_OMIT_TRACE
  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. */
  27. static int findNextHostParameter(const char *zSql, int *pnToken){
  28. int tokenType;
  29. int nTotal = 0;
  30. int n;
  31. *pnToken = 0;
  32. while( zSql[0] ){
  33. n = sqlite3GetToken((u8*)zSql, &tokenType);
  34. assert( n>0 && tokenType!=TK_ILLEGAL );
  35. if( tokenType==TK_VARIABLE ){
  36. *pnToken = n;
  37. break;
  38. }
  39. nTotal += n;
  40. zSql += n;
  41. }
  42. return nTotal;
  43. }
  44. /*
  45. ** This function returns a pointer to a nul-terminated string in memory
  46. ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
  47. ** string contains a copy of zRawSql but with host parameters expanded to
  48. ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
  49. ** then the returned string holds a copy of zRawSql with "-- " prepended
  50. ** to each line of text.
  51. **
  52. ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
  53. ** then long strings and blobs are truncated to that many bytes. This
  54. ** can be used to prevent unreasonably large trace strings when dealing
  55. ** with large (multi-megabyte) strings and blobs.
  56. **
  57. ** The calling function is responsible for making sure the memory returned
  58. ** is eventually freed.
  59. **
  60. ** ALGORITHM: Scan the input string looking for host parameters in any of
  61. ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
  62. ** string literals, quoted identifier names, and comments. For text forms,
  63. ** the host parameter index is found by scanning the perpared
  64. ** statement for the corresponding OP_Variable opcode. Once the host
  65. ** parameter index is known, locate the value in p->aVar[]. Then render
  66. ** the value as a literal in place of the host parameter name.
  67. */
  68. char *sqlite3VdbeExpandSql(
  69. Vdbe *p, /* The prepared statement being evaluated */
  70. const char *zRawSql /* Raw text of the SQL statement */
  71. ){
  72. sqlite3 *db; /* The database connection */
  73. int idx = 0; /* Index of a host parameter */
  74. int nextIndex = 1; /* Index of next ? host parameter */
  75. int n; /* Length of a token prefix */
  76. int nToken; /* Length of the parameter token */
  77. int i; /* Loop counter */
  78. Mem *pVar; /* Value of a host parameter */
  79. StrAccum out; /* Accumulate the output here */
  80. char zBase[100]; /* Initial working space */
  81. db = p->db;
  82. sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
  83. db->aLimit[SQLITE_LIMIT_LENGTH]);
  84. out.db = db;
  85. if( db->nVdbeExec>1 ){
  86. while( *zRawSql ){
  87. const char *zStart = zRawSql;
  88. while( *(zRawSql++)!='\n' && *zRawSql );
  89. sqlite3StrAccumAppend(&out, "-- ", 3);
  90. sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
  91. }
  92. }else{
  93. while( zRawSql[0] ){
  94. n = findNextHostParameter(zRawSql, &nToken);
  95. assert( n>0 );
  96. sqlite3StrAccumAppend(&out, zRawSql, n);
  97. zRawSql += n;
  98. assert( zRawSql[0] || nToken==0 );
  99. if( nToken==0 ) break;
  100. if( zRawSql[0]=='?' ){
  101. if( nToken>1 ){
  102. assert( sqlite3Isdigit(zRawSql[1]) );
  103. sqlite3GetInt32(&zRawSql[1], &idx);
  104. }else{
  105. idx = nextIndex;
  106. }
  107. }else{
  108. assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
  109. testcase( zRawSql[0]==':' );
  110. testcase( zRawSql[0]=='$' );
  111. testcase( zRawSql[0]=='@' );
  112. idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
  113. assert( idx>0 );
  114. }
  115. zRawSql += nToken;
  116. nextIndex = idx + 1;
  117. assert( idx>0 && idx<=p->nVar );
  118. pVar = &p->aVar[idx-1];
  119. if( pVar->flags & MEM_Null ){
  120. sqlite3StrAccumAppend(&out, "NULL", 4);
  121. }else if( pVar->flags & MEM_Int ){
  122. sqlite3XPrintf(&out, "%lld", pVar->u.i);
  123. }else if( pVar->flags & MEM_Real ){
  124. sqlite3XPrintf(&out, "%!.15g", pVar->r);
  125. }else if( pVar->flags & MEM_Str ){
  126. int nOut; /* Number of bytes of the string text to include in output */
  127. #ifndef SQLITE_OMIT_UTF16
  128. u8 enc = ENC(db);
  129. Mem utf8;
  130. if( enc!=SQLITE_UTF8 ){
  131. memset(&utf8, 0, sizeof(utf8));
  132. utf8.db = db;
  133. sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
  134. sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
  135. pVar = &utf8;
  136. }
  137. #endif
  138. nOut = pVar->n;
  139. #ifdef SQLITE_TRACE_SIZE_LIMIT
  140. if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
  141. nOut = SQLITE_TRACE_SIZE_LIMIT;
  142. while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
  143. }
  144. #endif
  145. sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
  146. #ifdef SQLITE_TRACE_SIZE_LIMIT
  147. if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
  148. #endif
  149. #ifndef SQLITE_OMIT_UTF16
  150. if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
  151. #endif
  152. }else if( pVar->flags & MEM_Zero ){
  153. sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
  154. }else{
  155. int nOut; /* Number of bytes of the blob to include in output */
  156. assert( pVar->flags & MEM_Blob );
  157. sqlite3StrAccumAppend(&out, "x'", 2);
  158. nOut = pVar->n;
  159. #ifdef SQLITE_TRACE_SIZE_LIMIT
  160. if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
  161. #endif
  162. for(i=0; i<nOut; i++){
  163. sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
  164. }
  165. sqlite3StrAccumAppend(&out, "'", 1);
  166. #ifdef SQLITE_TRACE_SIZE_LIMIT
  167. if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
  168. #endif
  169. }
  170. }
  171. }
  172. return sqlite3StrAccumFinish(&out);
  173. }
  174. #endif /* #ifndef SQLITE_OMIT_TRACE */
  175. /*****************************************************************************
  176. ** The following code implements the data-structure explaining logic
  177. ** for the Vdbe.
  178. */
  179. #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  180. /*
  181. ** Allocate a new Explain object
  182. */
  183. void sqlite3ExplainBegin(Vdbe *pVdbe){
  184. if( pVdbe ){
  185. Explain *p;
  186. sqlite3BeginBenignMalloc();
  187. p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
  188. if( p ){
  189. p->pVdbe = pVdbe;
  190. sqlite3_free(pVdbe->pExplain);
  191. pVdbe->pExplain = p;
  192. sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
  193. SQLITE_MAX_LENGTH);
  194. p->str.useMalloc = 2;
  195. }else{
  196. sqlite3EndBenignMalloc();
  197. }
  198. }
  199. }
  200. /*
  201. ** Return true if the Explain ends with a new-line.
  202. */
  203. static int endsWithNL(Explain *p){
  204. return p && p->str.zText && p->str.nChar
  205. && p->str.zText[p->str.nChar-1]=='\n';
  206. }
  207. /*
  208. ** Append text to the indentation
  209. */
  210. void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
  211. Explain *p;
  212. if( pVdbe && (p = pVdbe->pExplain)!=0 ){
  213. va_list ap;
  214. if( p->nIndent && endsWithNL(p) ){
  215. int n = p->nIndent;
  216. if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
  217. sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
  218. }
  219. va_start(ap, zFormat);
  220. sqlite3VXPrintf(&p->str, 1, zFormat, ap);
  221. va_end(ap);
  222. }
  223. }
  224. /*
  225. ** Append a '\n' if there is not already one.
  226. */
  227. void sqlite3ExplainNL(Vdbe *pVdbe){
  228. Explain *p;
  229. if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
  230. sqlite3StrAccumAppend(&p->str, "\n", 1);
  231. }
  232. }
  233. /*
  234. ** Push a new indentation level. Subsequent lines will be indented
  235. ** so that they begin at the current cursor position.
  236. */
  237. void sqlite3ExplainPush(Vdbe *pVdbe){
  238. Explain *p;
  239. if( pVdbe && (p = pVdbe->pExplain)!=0 ){
  240. if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
  241. const char *z = p->str.zText;
  242. int i = p->str.nChar-1;
  243. int x;
  244. while( i>=0 && z[i]!='\n' ){ i--; }
  245. x = (p->str.nChar - 1) - i;
  246. if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
  247. x = p->aIndent[p->nIndent-1];
  248. }
  249. p->aIndent[p->nIndent] = x;
  250. }
  251. p->nIndent++;
  252. }
  253. }
  254. /*
  255. ** Pop the indentation stack by one level.
  256. */
  257. void sqlite3ExplainPop(Vdbe *p){
  258. if( p && p->pExplain ) p->pExplain->nIndent--;
  259. }
  260. /*
  261. ** Free the indentation structure
  262. */
  263. void sqlite3ExplainFinish(Vdbe *pVdbe){
  264. if( pVdbe && pVdbe->pExplain ){
  265. sqlite3_free(pVdbe->zExplain);
  266. sqlite3ExplainNL(pVdbe);
  267. pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
  268. sqlite3_free(pVdbe->pExplain);
  269. pVdbe->pExplain = 0;
  270. sqlite3EndBenignMalloc();
  271. }
  272. }
  273. /*
  274. ** Return the explanation of a virtual machine.
  275. */
  276. const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
  277. return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
  278. }
  279. #endif /* defined(SQLITE_DEBUG) */