4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
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
17 ** The interfaces in this file is only available when compiling
20 #include "sqliteInt.h"
24 ** Add a new subitem to the tree. The moreToFollow flag indicates that this
25 ** is not the last item in the tree.
27 static TreeView
*sqlite3TreeViewPush(TreeView
*p
, u8 moreToFollow
){
29 p
= sqlite3_malloc64( sizeof(*p
) );
31 memset(p
, 0, sizeof(*p
));
35 assert( moreToFollow
==0 || moreToFollow
==1 );
36 if( p
->iLevel
<sizeof(p
->bLine
) ) p
->bLine
[p
->iLevel
] = moreToFollow
;
41 ** Finished with one layer of the tree
43 static void sqlite3TreeViewPop(TreeView
*p
){
46 if( p
->iLevel
<0 ) sqlite3_free(p
);
50 ** Generate a single line of output for the tree, with a prefix that contains
51 ** all the appropriate tree lines
53 static void sqlite3TreeViewLine(TreeView
*p
, const char *zFormat
, ...){
58 sqlite3StrAccumInit(&acc
, 0, zBuf
, sizeof(zBuf
), 0);
60 for(i
=0; i
<p
->iLevel
&& i
<sizeof(p
->bLine
)-1; i
++){
61 sqlite3StrAccumAppend(&acc
, p
->bLine
[i
] ? "| " : " ", 4);
63 sqlite3StrAccumAppend(&acc
, p
->bLine
[i
] ? "|-- " : "'-- ", 4);
65 va_start(ap
, zFormat
);
66 sqlite3VXPrintf(&acc
, zFormat
, ap
);
68 if( zBuf
[acc
.nChar
-1]!='\n' ) sqlite3StrAccumAppend(&acc
, "\n", 1);
69 sqlite3StrAccumFinish(&acc
);
70 fprintf(stdout
,"%s", zBuf
);
75 ** Shorthand for starting a new tree item that consists of a single label
77 static void sqlite3TreeViewItem(TreeView
*p
, const char *zLabel
,u8 moreFollows
){
78 p
= sqlite3TreeViewPush(p
, moreFollows
);
79 sqlite3TreeViewLine(p
, "%s", zLabel
);
83 ** Generate a human-readable description of a WITH clause.
85 void sqlite3TreeViewWith(TreeView
*pView
, const With
*pWith
, u8 moreToFollow
){
87 if( pWith
==0 ) return;
88 if( pWith
->nCte
==0 ) return;
90 sqlite3TreeViewLine(pView
, "WITH (0x%p, pOuter=0x%p)",pWith
,pWith
->pOuter
);
92 sqlite3TreeViewLine(pView
, "WITH (0x%p)", pWith
);
95 pView
= sqlite3TreeViewPush(pView
, 1);
96 for(i
=0; i
<pWith
->nCte
; i
++){
99 const struct Cte
*pCte
= &pWith
->a
[i
];
100 sqlite3StrAccumInit(&x
, 0, zLine
, sizeof(zLine
), 0);
101 sqlite3XPrintf(&x
, "%s", pCte
->zName
);
102 if( pCte
->pCols
&& pCte
->pCols
->nExpr
>0 ){
105 for(j
=0; j
<pCte
->pCols
->nExpr
; j
++){
106 sqlite3XPrintf(&x
, "%c%s", cSep
, pCte
->pCols
->a
[j
].zName
);
109 sqlite3XPrintf(&x
, ")");
111 sqlite3XPrintf(&x
, " AS");
112 sqlite3StrAccumFinish(&x
);
113 sqlite3TreeViewItem(pView
, zLine
, i
<pWith
->nCte
-1);
114 sqlite3TreeViewSelect(pView
, pCte
->pSelect
, 0);
115 sqlite3TreeViewPop(pView
);
117 sqlite3TreeViewPop(pView
);
123 ** Generate a human-readable description of a Select object.
125 void sqlite3TreeViewSelect(TreeView
*pView
, const Select
*p
, u8 moreToFollow
){
128 pView
= sqlite3TreeViewPush(pView
, moreToFollow
);
130 sqlite3TreeViewWith(pView
, p
->pWith
, 1);
132 sqlite3TreeViewPush(pView
, 1);
135 sqlite3TreeViewLine(pView
, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
136 ((p
->selFlags
& SF_Distinct
) ? " DISTINCT" : ""),
137 ((p
->selFlags
& SF_Aggregate
) ? " agg_flag" : ""), p
, p
->selFlags
,
140 if( cnt
++ ) sqlite3TreeViewPop(pView
);
145 if( p
->pSrc
&& p
->pSrc
->nSrc
) n
++;
147 if( p
->pGroupBy
) n
++;
148 if( p
->pHaving
) n
++;
149 if( p
->pOrderBy
) n
++;
151 if( p
->pOffset
) n
++;
153 sqlite3TreeViewExprList(pView
, p
->pEList
, (n
--)>0, "result-set");
154 if( p
->pSrc
&& p
->pSrc
->nSrc
){
156 pView
= sqlite3TreeViewPush(pView
, (n
--)>0);
157 sqlite3TreeViewLine(pView
, "FROM");
158 for(i
=0; i
<p
->pSrc
->nSrc
; i
++){
159 struct SrcList_item
*pItem
= &p
->pSrc
->a
[i
];
162 sqlite3StrAccumInit(&x
, 0, zLine
, sizeof(zLine
), 0);
163 sqlite3XPrintf(&x
, "{%d,*}", pItem
->iCursor
);
164 if( pItem
->zDatabase
){
165 sqlite3XPrintf(&x
, " %s.%s", pItem
->zDatabase
, pItem
->zName
);
166 }else if( pItem
->zName
){
167 sqlite3XPrintf(&x
, " %s", pItem
->zName
);
170 sqlite3XPrintf(&x
, " tabname=%Q", pItem
->pTab
->zName
);
173 sqlite3XPrintf(&x
, " (AS %s)", pItem
->zAlias
);
175 if( pItem
->fg
.jointype
& JT_LEFT
){
176 sqlite3XPrintf(&x
, " LEFT-JOIN");
178 sqlite3StrAccumFinish(&x
);
179 sqlite3TreeViewItem(pView
, zLine
, i
<p
->pSrc
->nSrc
-1);
180 if( pItem
->pSelect
){
181 sqlite3TreeViewSelect(pView
, pItem
->pSelect
, 0);
183 if( pItem
->fg
.isTabFunc
){
184 sqlite3TreeViewExprList(pView
, pItem
->u1
.pFuncArg
, 0, "func-args:");
186 sqlite3TreeViewPop(pView
);
188 sqlite3TreeViewPop(pView
);
191 sqlite3TreeViewItem(pView
, "WHERE", (n
--)>0);
192 sqlite3TreeViewExpr(pView
, p
->pWhere
, 0);
193 sqlite3TreeViewPop(pView
);
196 sqlite3TreeViewExprList(pView
, p
->pGroupBy
, (n
--)>0, "GROUPBY");
199 sqlite3TreeViewItem(pView
, "HAVING", (n
--)>0);
200 sqlite3TreeViewExpr(pView
, p
->pHaving
, 0);
201 sqlite3TreeViewPop(pView
);
204 sqlite3TreeViewExprList(pView
, p
->pOrderBy
, (n
--)>0, "ORDERBY");
207 sqlite3TreeViewItem(pView
, "LIMIT", (n
--)>0);
208 sqlite3TreeViewExpr(pView
, p
->pLimit
, 0);
209 sqlite3TreeViewPop(pView
);
212 sqlite3TreeViewItem(pView
, "OFFSET", (n
--)>0);
213 sqlite3TreeViewExpr(pView
, p
->pOffset
, 0);
214 sqlite3TreeViewPop(pView
);
217 const char *zOp
= "UNION";
219 case TK_ALL
: zOp
= "UNION ALL"; break;
220 case TK_INTERSECT
: zOp
= "INTERSECT"; break;
221 case TK_EXCEPT
: zOp
= "EXCEPT"; break;
223 sqlite3TreeViewItem(pView
, zOp
, 1);
227 sqlite3TreeViewPop(pView
);
231 ** Generate a human-readable explanation of an expression tree.
233 void sqlite3TreeViewExpr(TreeView
*pView
, const Expr
*pExpr
, u8 moreToFollow
){
234 const char *zBinOp
= 0; /* Binary operator */
235 const char *zUniOp
= 0; /* Unary operator */
237 pView
= sqlite3TreeViewPush(pView
, moreToFollow
);
239 sqlite3TreeViewLine(pView
, "nil");
240 sqlite3TreeViewPop(pView
);
244 sqlite3_snprintf(sizeof(zFlgs
),zFlgs
," flags=0x%x",pExpr
->flags
);
249 case TK_AGG_COLUMN
: {
250 sqlite3TreeViewLine(pView
, "AGG{%d:%d}%s",
251 pExpr
->iTable
, pExpr
->iColumn
, zFlgs
);
255 if( pExpr
->iTable
<0 ){
256 /* This only happens when coding check constraints */
257 sqlite3TreeViewLine(pView
, "COLUMN(%d)%s", pExpr
->iColumn
, zFlgs
);
259 sqlite3TreeViewLine(pView
, "{%d:%d}%s",
260 pExpr
->iTable
, pExpr
->iColumn
, zFlgs
);
265 if( pExpr
->flags
& EP_IntValue
){
266 sqlite3TreeViewLine(pView
, "%d", pExpr
->u
.iValue
);
268 sqlite3TreeViewLine(pView
, "%s", pExpr
->u
.zToken
);
272 #ifndef SQLITE_OMIT_FLOATING_POINT
274 sqlite3TreeViewLine(pView
,"%s", pExpr
->u
.zToken
);
279 sqlite3TreeViewLine(pView
,"%Q", pExpr
->u
.zToken
);
283 sqlite3TreeViewLine(pView
,"NULL");
286 #ifndef SQLITE_OMIT_BLOB_LITERAL
288 sqlite3TreeViewLine(pView
,"%s", pExpr
->u
.zToken
);
293 sqlite3TreeViewLine(pView
,"VARIABLE(%s,%d)",
294 pExpr
->u
.zToken
, pExpr
->iColumn
);
298 sqlite3TreeViewLine(pView
,"REGISTER(%d)", pExpr
->iTable
);
302 sqlite3TreeViewLine(pView
,"ID \"%w\"", pExpr
->u
.zToken
);
305 #ifndef SQLITE_OMIT_CAST
307 /* Expressions of the form: CAST(pLeft AS token) */
308 sqlite3TreeViewLine(pView
,"CAST %Q", pExpr
->u
.zToken
);
309 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
312 #endif /* SQLITE_OMIT_CAST */
313 case TK_LT
: zBinOp
= "LT"; break;
314 case TK_LE
: zBinOp
= "LE"; break;
315 case TK_GT
: zBinOp
= "GT"; break;
316 case TK_GE
: zBinOp
= "GE"; break;
317 case TK_NE
: zBinOp
= "NE"; break;
318 case TK_EQ
: zBinOp
= "EQ"; break;
319 case TK_IS
: zBinOp
= "IS"; break;
320 case TK_ISNOT
: zBinOp
= "ISNOT"; break;
321 case TK_AND
: zBinOp
= "AND"; break;
322 case TK_OR
: zBinOp
= "OR"; break;
323 case TK_PLUS
: zBinOp
= "ADD"; break;
324 case TK_STAR
: zBinOp
= "MUL"; break;
325 case TK_MINUS
: zBinOp
= "SUB"; break;
326 case TK_REM
: zBinOp
= "REM"; break;
327 case TK_BITAND
: zBinOp
= "BITAND"; break;
328 case TK_BITOR
: zBinOp
= "BITOR"; break;
329 case TK_SLASH
: zBinOp
= "DIV"; break;
330 case TK_LSHIFT
: zBinOp
= "LSHIFT"; break;
331 case TK_RSHIFT
: zBinOp
= "RSHIFT"; break;
332 case TK_CONCAT
: zBinOp
= "CONCAT"; break;
333 case TK_DOT
: zBinOp
= "DOT"; break;
335 case TK_UMINUS
: zUniOp
= "UMINUS"; break;
336 case TK_UPLUS
: zUniOp
= "UPLUS"; break;
337 case TK_BITNOT
: zUniOp
= "BITNOT"; break;
338 case TK_NOT
: zUniOp
= "NOT"; break;
339 case TK_ISNULL
: zUniOp
= "ISNULL"; break;
340 case TK_NOTNULL
: zUniOp
= "NOTNULL"; break;
343 sqlite3TreeViewLine(pView
, "SPAN %Q", pExpr
->u
.zToken
);
344 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
349 sqlite3TreeViewLine(pView
, "COLLATE %Q", pExpr
->u
.zToken
);
350 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
354 case TK_AGG_FUNCTION
:
356 ExprList
*pFarg
; /* List of function arguments */
357 if( ExprHasProperty(pExpr
, EP_TokenOnly
) ){
360 pFarg
= pExpr
->x
.pList
;
362 if( pExpr
->op
==TK_AGG_FUNCTION
){
363 sqlite3TreeViewLine(pView
, "AGG_FUNCTION%d %Q",
364 pExpr
->op2
, pExpr
->u
.zToken
);
366 sqlite3TreeViewLine(pView
, "FUNCTION %Q", pExpr
->u
.zToken
);
369 sqlite3TreeViewExprList(pView
, pFarg
, 0, 0);
373 #ifndef SQLITE_OMIT_SUBQUERY
375 sqlite3TreeViewLine(pView
, "EXISTS-expr");
376 sqlite3TreeViewSelect(pView
, pExpr
->x
.pSelect
, 0);
380 sqlite3TreeViewLine(pView
, "SELECT-expr");
381 sqlite3TreeViewSelect(pView
, pExpr
->x
.pSelect
, 0);
385 sqlite3TreeViewLine(pView
, "IN");
386 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 1);
387 if( ExprHasProperty(pExpr
, EP_xIsSelect
) ){
388 sqlite3TreeViewSelect(pView
, pExpr
->x
.pSelect
, 0);
390 sqlite3TreeViewExprList(pView
, pExpr
->x
.pList
, 0, 0);
394 #endif /* SQLITE_OMIT_SUBQUERY */
399 ** This is equivalent to
403 ** X is stored in pExpr->pLeft.
404 ** Y is stored in pExpr->pList->a[0].pExpr.
405 ** Z is stored in pExpr->pList->a[1].pExpr.
408 Expr
*pX
= pExpr
->pLeft
;
409 Expr
*pY
= pExpr
->x
.pList
->a
[0].pExpr
;
410 Expr
*pZ
= pExpr
->x
.pList
->a
[1].pExpr
;
411 sqlite3TreeViewLine(pView
, "BETWEEN");
412 sqlite3TreeViewExpr(pView
, pX
, 1);
413 sqlite3TreeViewExpr(pView
, pY
, 1);
414 sqlite3TreeViewExpr(pView
, pZ
, 0);
418 /* If the opcode is TK_TRIGGER, then the expression is a reference
419 ** to a column in the new.* or old.* pseudo-tables available to
420 ** trigger programs. In this case Expr.iTable is set to 1 for the
421 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
422 ** is set to the column of the pseudo-table to read, or to -1 to
423 ** read the rowid field.
425 sqlite3TreeViewLine(pView
, "%s(%d)",
426 pExpr
->iTable
? "NEW" : "OLD", pExpr
->iColumn
);
430 sqlite3TreeViewLine(pView
, "CASE");
431 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 1);
432 sqlite3TreeViewExprList(pView
, pExpr
->x
.pList
, 0, 0);
435 #ifndef SQLITE_OMIT_TRIGGER
437 const char *zType
= "unk";
438 switch( pExpr
->affinity
){
439 case OE_Rollback
: zType
= "rollback"; break;
440 case OE_Abort
: zType
= "abort"; break;
441 case OE_Fail
: zType
= "fail"; break;
442 case OE_Ignore
: zType
= "ignore"; break;
444 sqlite3TreeViewLine(pView
, "RAISE %s(%Q)", zType
, pExpr
->u
.zToken
);
449 sqlite3TreeViewLine(pView
, "MATCH {%d:%d}%s",
450 pExpr
->iTable
, pExpr
->iColumn
, zFlgs
);
451 sqlite3TreeViewExpr(pView
, pExpr
->pRight
, 0);
455 sqlite3TreeViewBareExprList(pView
, pExpr
->x
.pList
, "VECTOR");
458 case TK_SELECT_COLUMN
: {
459 sqlite3TreeViewLine(pView
, "SELECT-COLUMN %d", pExpr
->iColumn
);
460 sqlite3TreeViewSelect(pView
, pExpr
->pLeft
->x
.pSelect
, 0);
464 sqlite3TreeViewLine(pView
, "op=%d", pExpr
->op
);
469 sqlite3TreeViewLine(pView
, "%s%s", zBinOp
, zFlgs
);
470 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 1);
471 sqlite3TreeViewExpr(pView
, pExpr
->pRight
, 0);
473 sqlite3TreeViewLine(pView
, "%s%s", zUniOp
, zFlgs
);
474 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
476 sqlite3TreeViewPop(pView
);
481 ** Generate a human-readable explanation of an expression list.
483 void sqlite3TreeViewBareExprList(
485 const ExprList
*pList
,
488 if( zLabel
==0 || zLabel
[0]==0 ) zLabel
= "LIST";
490 sqlite3TreeViewLine(pView
, "%s (empty)", zLabel
);
493 sqlite3TreeViewLine(pView
, "%s", zLabel
);
494 for(i
=0; i
<pList
->nExpr
; i
++){
495 int j
= pList
->a
[i
].u
.x
.iOrderByCol
;
497 sqlite3TreeViewPush(pView
, 0);
498 sqlite3TreeViewLine(pView
, "iOrderByCol=%d", j
);
500 sqlite3TreeViewExpr(pView
, pList
->a
[i
].pExpr
, i
<pList
->nExpr
-1);
501 if( j
) sqlite3TreeViewPop(pView
);
505 void sqlite3TreeViewExprList(
507 const ExprList
*pList
,
511 pView
= sqlite3TreeViewPush(pView
, moreToFollow
);
512 sqlite3TreeViewBareExprList(pView
, pList
, zLabel
);
513 sqlite3TreeViewPop(pView
);
516 #endif /* SQLITE_DEBUG */