xref: /sqlite-3.40.0/src/treeview.c (revision 3f09beda)
1 /*
2 ** 2015-06-08
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 C code to implement the TreeView debugging routines.
14 ** These routines print a parse tree to standard output for debugging and
15 ** analysis.
16 **
17 ** The interfaces in this file is only available when compiling
18 ** with SQLITE_DEBUG.
19 */
20 #include "sqliteInt.h"
21 #ifdef SQLITE_DEBUG
22 
23 /*
24 ** Add a new subitem to the tree.  The moreToFollow flag indicates that this
25 ** is not the last item in the tree.
26 */
27 static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
28   if( p==0 ){
29     p = sqlite3_malloc64( sizeof(*p) );
30     if( p==0 ) return 0;
31     memset(p, 0, sizeof(*p));
32   }else{
33     p->iLevel++;
34   }
35   assert( moreToFollow==0 || moreToFollow==1 );
36   if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
37   return p;
38 }
39 
40 /*
41 ** Finished with one layer of the tree
42 */
43 static void sqlite3TreeViewPop(TreeView *p){
44   if( p==0 ) return;
45   p->iLevel--;
46   if( p->iLevel<0 ) sqlite3_free(p);
47 }
48 
49 /*
50 ** Generate a single line of output for the tree, with a prefix that contains
51 ** all the appropriate tree lines
52 */
53 static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
54   va_list ap;
55   int i;
56   StrAccum acc;
57   char zBuf[500];
58   sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
59   if( p ){
60     for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
61       sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
62     }
63     sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
64   }
65   va_start(ap, zFormat);
66   sqlite3VXPrintf(&acc, 0, zFormat, ap);
67   va_end(ap);
68   if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
69   sqlite3StrAccumFinish(&acc);
70   fprintf(stdout,"%s", zBuf);
71   fflush(stdout);
72 }
73 
74 /*
75 ** Shorthand for starting a new tree item that consists of a single label
76 */
77 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
78   p = sqlite3TreeViewPush(p, moreFollows);
79   sqlite3TreeViewLine(p, "%s", zLabel);
80 }
81 
82 
83 /*
84 ** Generate a human-readable description of a the Select object.
85 */
86 void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
87   int n = 0;
88   pView = sqlite3TreeViewPush(pView, moreToFollow);
89   sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
90     ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
91     ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
92   );
93   if( p->pSrc && p->pSrc->nSrc ) n++;
94   if( p->pWhere ) n++;
95   if( p->pGroupBy ) n++;
96   if( p->pHaving ) n++;
97   if( p->pOrderBy ) n++;
98   if( p->pLimit ) n++;
99   if( p->pOffset ) n++;
100   if( p->pPrior ) n++;
101   sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
102   if( p->pSrc && p->pSrc->nSrc ){
103     int i;
104     pView = sqlite3TreeViewPush(pView, (n--)>0);
105     sqlite3TreeViewLine(pView, "FROM");
106     for(i=0; i<p->pSrc->nSrc; i++){
107       struct SrcList_item *pItem = &p->pSrc->a[i];
108       StrAccum x;
109       char zLine[100];
110       sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
111       sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
112       if( pItem->zDatabase ){
113         sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
114       }else if( pItem->zName ){
115         sqlite3XPrintf(&x, 0, " %s", pItem->zName);
116       }
117       if( pItem->pTab ){
118         sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
119       }
120       if( pItem->zAlias ){
121         sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
122       }
123       if( pItem->jointype & JT_LEFT ){
124         sqlite3XPrintf(&x, 0, " LEFT-JOIN");
125       }
126       sqlite3StrAccumFinish(&x);
127       sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
128       if( pItem->pSelect ){
129         sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
130       }
131       sqlite3TreeViewPop(pView);
132     }
133     sqlite3TreeViewPop(pView);
134   }
135   if( p->pWhere ){
136     sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
137     sqlite3TreeViewExpr(pView, p->pWhere, 0);
138     sqlite3TreeViewPop(pView);
139   }
140   if( p->pGroupBy ){
141     sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
142   }
143   if( p->pHaving ){
144     sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
145     sqlite3TreeViewExpr(pView, p->pHaving, 0);
146     sqlite3TreeViewPop(pView);
147   }
148   if( p->pOrderBy ){
149     sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
150   }
151   if( p->pLimit ){
152     sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
153     sqlite3TreeViewExpr(pView, p->pLimit, 0);
154     sqlite3TreeViewPop(pView);
155   }
156   if( p->pOffset ){
157     sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
158     sqlite3TreeViewExpr(pView, p->pOffset, 0);
159     sqlite3TreeViewPop(pView);
160   }
161   if( p->pPrior ){
162     const char *zOp = "UNION";
163     switch( p->op ){
164       case TK_ALL:         zOp = "UNION ALL";  break;
165       case TK_INTERSECT:   zOp = "INTERSECT";  break;
166       case TK_EXCEPT:      zOp = "EXCEPT";     break;
167     }
168     sqlite3TreeViewItem(pView, zOp, (n--)>0);
169     sqlite3TreeViewSelect(pView, p->pPrior, 0);
170     sqlite3TreeViewPop(pView);
171   }
172   sqlite3TreeViewPop(pView);
173 }
174 
175 /*
176 ** Generate a human-readable explanation of an expression tree.
177 */
178 void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
179   const char *zBinOp = 0;   /* Binary operator */
180   const char *zUniOp = 0;   /* Unary operator */
181   char zFlgs[30];
182   pView = sqlite3TreeViewPush(pView, moreToFollow);
183   if( pExpr==0 ){
184     sqlite3TreeViewLine(pView, "nil");
185     sqlite3TreeViewPop(pView);
186     return;
187   }
188   if( pExpr->flags ){
189     sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x",pExpr->flags);
190   }else{
191     zFlgs[0] = 0;
192   }
193   switch( pExpr->op ){
194     case TK_AGG_COLUMN: {
195       sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
196             pExpr->iTable, pExpr->iColumn, zFlgs);
197       break;
198     }
199     case TK_COLUMN: {
200       if( pExpr->iTable<0 ){
201         /* This only happens when coding check constraints */
202         sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
203       }else{
204         sqlite3TreeViewLine(pView, "{%d:%d}%s",
205                              pExpr->iTable, pExpr->iColumn, zFlgs);
206       }
207       break;
208     }
209     case TK_INTEGER: {
210       if( pExpr->flags & EP_IntValue ){
211         sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
212       }else{
213         sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
214       }
215       break;
216     }
217 #ifndef SQLITE_OMIT_FLOATING_POINT
218     case TK_FLOAT: {
219       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
220       break;
221     }
222 #endif
223     case TK_STRING: {
224       sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
225       break;
226     }
227     case TK_NULL: {
228       sqlite3TreeViewLine(pView,"NULL");
229       break;
230     }
231 #ifndef SQLITE_OMIT_BLOB_LITERAL
232     case TK_BLOB: {
233       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
234       break;
235     }
236 #endif
237     case TK_VARIABLE: {
238       sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
239                           pExpr->u.zToken, pExpr->iColumn);
240       break;
241     }
242     case TK_REGISTER: {
243       sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
244       break;
245     }
246     case TK_AS: {
247       sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
248       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
249       break;
250     }
251     case TK_ID: {
252       sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
253       break;
254     }
255 #ifndef SQLITE_OMIT_CAST
256     case TK_CAST: {
257       /* Expressions of the form:   CAST(pLeft AS token) */
258       sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
259       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
260       break;
261     }
262 #endif /* SQLITE_OMIT_CAST */
263     case TK_LT:      zBinOp = "LT";     break;
264     case TK_LE:      zBinOp = "LE";     break;
265     case TK_GT:      zBinOp = "GT";     break;
266     case TK_GE:      zBinOp = "GE";     break;
267     case TK_NE:      zBinOp = "NE";     break;
268     case TK_EQ:      zBinOp = "EQ";     break;
269     case TK_IS:      zBinOp = "IS";     break;
270     case TK_ISNOT:   zBinOp = "ISNOT";  break;
271     case TK_AND:     zBinOp = "AND";    break;
272     case TK_OR:      zBinOp = "OR";     break;
273     case TK_PLUS:    zBinOp = "ADD";    break;
274     case TK_STAR:    zBinOp = "MUL";    break;
275     case TK_MINUS:   zBinOp = "SUB";    break;
276     case TK_REM:     zBinOp = "REM";    break;
277     case TK_BITAND:  zBinOp = "BITAND"; break;
278     case TK_BITOR:   zBinOp = "BITOR";  break;
279     case TK_SLASH:   zBinOp = "DIV";    break;
280     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
281     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
282     case TK_CONCAT:  zBinOp = "CONCAT"; break;
283     case TK_DOT:     zBinOp = "DOT";    break;
284 
285     case TK_UMINUS:  zUniOp = "UMINUS"; break;
286     case TK_UPLUS:   zUniOp = "UPLUS";  break;
287     case TK_BITNOT:  zUniOp = "BITNOT"; break;
288     case TK_NOT:     zUniOp = "NOT";    break;
289     case TK_ISNULL:  zUniOp = "ISNULL"; break;
290     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
291 
292     case TK_COLLATE: {
293       sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
294       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
295       break;
296     }
297 
298     case TK_AGG_FUNCTION:
299     case TK_FUNCTION: {
300       ExprList *pFarg;       /* List of function arguments */
301       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
302         pFarg = 0;
303       }else{
304         pFarg = pExpr->x.pList;
305       }
306       if( pExpr->op==TK_AGG_FUNCTION ){
307         sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
308                              pExpr->op2, pExpr->u.zToken);
309       }else{
310         sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
311       }
312       if( pFarg ){
313         sqlite3TreeViewExprList(pView, pFarg, 0, 0);
314       }
315       break;
316     }
317 #ifndef SQLITE_OMIT_SUBQUERY
318     case TK_EXISTS: {
319       sqlite3TreeViewLine(pView, "EXISTS-expr");
320       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
321       break;
322     }
323     case TK_SELECT: {
324       sqlite3TreeViewLine(pView, "SELECT-expr");
325       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
326       break;
327     }
328     case TK_IN: {
329       sqlite3TreeViewLine(pView, "IN");
330       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
331       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
332         sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
333       }else{
334         sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
335       }
336       break;
337     }
338 #endif /* SQLITE_OMIT_SUBQUERY */
339 
340     /*
341     **    x BETWEEN y AND z
342     **
343     ** This is equivalent to
344     **
345     **    x>=y AND x<=z
346     **
347     ** X is stored in pExpr->pLeft.
348     ** Y is stored in pExpr->pList->a[0].pExpr.
349     ** Z is stored in pExpr->pList->a[1].pExpr.
350     */
351     case TK_BETWEEN: {
352       Expr *pX = pExpr->pLeft;
353       Expr *pY = pExpr->x.pList->a[0].pExpr;
354       Expr *pZ = pExpr->x.pList->a[1].pExpr;
355       sqlite3TreeViewLine(pView, "BETWEEN");
356       sqlite3TreeViewExpr(pView, pX, 1);
357       sqlite3TreeViewExpr(pView, pY, 1);
358       sqlite3TreeViewExpr(pView, pZ, 0);
359       break;
360     }
361     case TK_TRIGGER: {
362       /* If the opcode is TK_TRIGGER, then the expression is a reference
363       ** to a column in the new.* or old.* pseudo-tables available to
364       ** trigger programs. In this case Expr.iTable is set to 1 for the
365       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
366       ** is set to the column of the pseudo-table to read, or to -1 to
367       ** read the rowid field.
368       */
369       sqlite3TreeViewLine(pView, "%s(%d)",
370           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
371       break;
372     }
373     case TK_CASE: {
374       sqlite3TreeViewLine(pView, "CASE");
375       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
376       sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
377       break;
378     }
379 #ifndef SQLITE_OMIT_TRIGGER
380     case TK_RAISE: {
381       const char *zType = "unk";
382       switch( pExpr->affinity ){
383         case OE_Rollback:   zType = "rollback";  break;
384         case OE_Abort:      zType = "abort";     break;
385         case OE_Fail:       zType = "fail";      break;
386         case OE_Ignore:     zType = "ignore";    break;
387       }
388       sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
389       break;
390     }
391 #endif
392     default: {
393       sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
394       break;
395     }
396   }
397   if( zBinOp ){
398     sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
399     sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
400     sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
401   }else if( zUniOp ){
402     sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
403     sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
404   }
405   sqlite3TreeViewPop(pView);
406 }
407 
408 /*
409 ** Generate a human-readable explanation of an expression list.
410 */
411 void sqlite3TreeViewExprList(
412   TreeView *pView,
413   const ExprList *pList,
414   u8 moreToFollow,
415   const char *zLabel
416 ){
417   int i;
418   pView = sqlite3TreeViewPush(pView, moreToFollow);
419   if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
420   if( pList==0 ){
421     sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
422   }else{
423     sqlite3TreeViewLine(pView, "%s", zLabel);
424     for(i=0; i<pList->nExpr; i++){
425       sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
426     }
427   }
428   sqlite3TreeViewPop(pView);
429 }
430 
431 #endif /* SQLITE_DEBUG */
432