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 assert( acc
.nChar
>0 );
69 if( zBuf
[acc
.nChar
-1]!='\n' ) sqlite3StrAccumAppend(&acc
, "\n", 1);
70 sqlite3StrAccumFinish(&acc
);
71 fprintf(stdout
,"%s", zBuf
);
76 ** Shorthand for starting a new tree item that consists of a single label
78 static void sqlite3TreeViewItem(TreeView
*p
, const char *zLabel
,u8 moreFollows
){
79 p
= sqlite3TreeViewPush(p
, moreFollows
);
80 sqlite3TreeViewLine(p
, "%s", zLabel
);
84 ** Generate a human-readable description of a WITH clause.
86 void sqlite3TreeViewWith(TreeView
*pView
, const With
*pWith
, u8 moreToFollow
){
88 if( pWith
==0 ) return;
89 if( pWith
->nCte
==0 ) return;
91 sqlite3TreeViewLine(pView
, "WITH (0x%p, pOuter=0x%p)",pWith
,pWith
->pOuter
);
93 sqlite3TreeViewLine(pView
, "WITH (0x%p)", pWith
);
96 pView
= sqlite3TreeViewPush(pView
, 1);
97 for(i
=0; i
<pWith
->nCte
; i
++){
100 const struct Cte
*pCte
= &pWith
->a
[i
];
101 sqlite3StrAccumInit(&x
, 0, zLine
, sizeof(zLine
), 0);
102 sqlite3XPrintf(&x
, "%s", pCte
->zName
);
103 if( pCte
->pCols
&& pCte
->pCols
->nExpr
>0 ){
106 for(j
=0; j
<pCte
->pCols
->nExpr
; j
++){
107 sqlite3XPrintf(&x
, "%c%s", cSep
, pCte
->pCols
->a
[j
].zName
);
110 sqlite3XPrintf(&x
, ")");
112 sqlite3XPrintf(&x
, " AS");
113 sqlite3StrAccumFinish(&x
);
114 sqlite3TreeViewItem(pView
, zLine
, i
<pWith
->nCte
-1);
115 sqlite3TreeViewSelect(pView
, pCte
->pSelect
, 0);
116 sqlite3TreeViewPop(pView
);
118 sqlite3TreeViewPop(pView
);
124 ** Generate a human-readable description of a Select object.
126 void sqlite3TreeViewSelect(TreeView
*pView
, const Select
*p
, u8 moreToFollow
){
130 sqlite3TreeViewLine(pView
, "nil-SELECT");
133 pView
= sqlite3TreeViewPush(pView
, moreToFollow
);
135 sqlite3TreeViewWith(pView
, p
->pWith
, 1);
137 sqlite3TreeViewPush(pView
, 1);
140 sqlite3TreeViewLine(pView
, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
141 ((p
->selFlags
& SF_Distinct
) ? " DISTINCT" : ""),
142 ((p
->selFlags
& SF_Aggregate
) ? " agg_flag" : ""), p
, p
->selFlags
,
145 if( cnt
++ ) sqlite3TreeViewPop(pView
);
150 if( p
->pSrc
&& p
->pSrc
->nSrc
) n
++;
152 if( p
->pGroupBy
) n
++;
153 if( p
->pHaving
) n
++;
154 if( p
->pOrderBy
) n
++;
157 sqlite3TreeViewExprList(pView
, p
->pEList
, (n
--)>0, "result-set");
158 if( p
->pSrc
&& p
->pSrc
->nSrc
){
160 pView
= sqlite3TreeViewPush(pView
, (n
--)>0);
161 sqlite3TreeViewLine(pView
, "FROM");
162 for(i
=0; i
<p
->pSrc
->nSrc
; i
++){
163 struct SrcList_item
*pItem
= &p
->pSrc
->a
[i
];
166 sqlite3StrAccumInit(&x
, 0, zLine
, sizeof(zLine
), 0);
167 sqlite3XPrintf(&x
, "{%d,*}", pItem
->iCursor
);
168 if( pItem
->zDatabase
){
169 sqlite3XPrintf(&x
, " %s.%s", pItem
->zDatabase
, pItem
->zName
);
170 }else if( pItem
->zName
){
171 sqlite3XPrintf(&x
, " %s", pItem
->zName
);
174 sqlite3XPrintf(&x
, " tabname=%Q", pItem
->pTab
->zName
);
177 sqlite3XPrintf(&x
, " (AS %s)", pItem
->zAlias
);
179 if( pItem
->fg
.jointype
& JT_LEFT
){
180 sqlite3XPrintf(&x
, " LEFT-JOIN");
182 sqlite3StrAccumFinish(&x
);
183 sqlite3TreeViewItem(pView
, zLine
, i
<p
->pSrc
->nSrc
-1);
184 if( pItem
->pSelect
){
185 sqlite3TreeViewSelect(pView
, pItem
->pSelect
, 0);
187 if( pItem
->fg
.isTabFunc
){
188 sqlite3TreeViewExprList(pView
, pItem
->u1
.pFuncArg
, 0, "func-args:");
190 sqlite3TreeViewPop(pView
);
192 sqlite3TreeViewPop(pView
);
195 sqlite3TreeViewItem(pView
, "WHERE", (n
--)>0);
196 sqlite3TreeViewExpr(pView
, p
->pWhere
, 0);
197 sqlite3TreeViewPop(pView
);
200 sqlite3TreeViewExprList(pView
, p
->pGroupBy
, (n
--)>0, "GROUPBY");
203 sqlite3TreeViewItem(pView
, "HAVING", (n
--)>0);
204 sqlite3TreeViewExpr(pView
, p
->pHaving
, 0);
205 sqlite3TreeViewPop(pView
);
208 sqlite3TreeViewExprList(pView
, p
->pOrderBy
, (n
--)>0, "ORDERBY");
211 sqlite3TreeViewItem(pView
, "LIMIT", (n
--)>0);
212 sqlite3TreeViewExpr(pView
, p
->pLimit
->pLeft
, p
->pLimit
->pRight
!=0);
213 if( p
->pLimit
->pRight
){
214 sqlite3TreeViewItem(pView
, "OFFSET", (n
--)>0);
215 sqlite3TreeViewExpr(pView
, p
->pLimit
->pRight
, 0);
216 sqlite3TreeViewPop(pView
);
218 sqlite3TreeViewPop(pView
);
221 const char *zOp
= "UNION";
223 case TK_ALL
: zOp
= "UNION ALL"; break;
224 case TK_INTERSECT
: zOp
= "INTERSECT"; break;
225 case TK_EXCEPT
: zOp
= "EXCEPT"; break;
227 sqlite3TreeViewItem(pView
, zOp
, 1);
231 sqlite3TreeViewPop(pView
);
235 ** Generate a human-readable explanation of an expression tree.
237 void sqlite3TreeViewExpr(TreeView
*pView
, const Expr
*pExpr
, u8 moreToFollow
){
238 const char *zBinOp
= 0; /* Binary operator */
239 const char *zUniOp
= 0; /* Unary operator */
241 pView
= sqlite3TreeViewPush(pView
, moreToFollow
);
243 sqlite3TreeViewLine(pView
, "nil");
244 sqlite3TreeViewPop(pView
);
248 if( ExprHasProperty(pExpr
, EP_FromJoin
) ){
249 sqlite3_snprintf(sizeof(zFlgs
),zFlgs
," flags=0x%x iRJT=%d",
250 pExpr
->flags
, pExpr
->iRightJoinTable
);
252 sqlite3_snprintf(sizeof(zFlgs
),zFlgs
," flags=0x%x",pExpr
->flags
);
258 case TK_AGG_COLUMN
: {
259 sqlite3TreeViewLine(pView
, "AGG{%d:%d}%s",
260 pExpr
->iTable
, pExpr
->iColumn
, zFlgs
);
264 if( pExpr
->iTable
<0 ){
265 /* This only happens when coding check constraints */
266 sqlite3TreeViewLine(pView
, "COLUMN(%d)%s", pExpr
->iColumn
, zFlgs
);
268 sqlite3TreeViewLine(pView
, "{%d:%d}%s",
269 pExpr
->iTable
, pExpr
->iColumn
, zFlgs
);
274 if( pExpr
->flags
& EP_IntValue
){
275 sqlite3TreeViewLine(pView
, "%d", pExpr
->u
.iValue
);
277 sqlite3TreeViewLine(pView
, "%s", pExpr
->u
.zToken
);
281 #ifndef SQLITE_OMIT_FLOATING_POINT
283 sqlite3TreeViewLine(pView
,"%s", pExpr
->u
.zToken
);
288 sqlite3TreeViewLine(pView
,"%Q", pExpr
->u
.zToken
);
292 sqlite3TreeViewLine(pView
,"NULL");
295 #ifndef SQLITE_OMIT_BLOB_LITERAL
297 sqlite3TreeViewLine(pView
,"%s", pExpr
->u
.zToken
);
302 sqlite3TreeViewLine(pView
,"VARIABLE(%s,%d)",
303 pExpr
->u
.zToken
, pExpr
->iColumn
);
307 sqlite3TreeViewLine(pView
,"REGISTER(%d)", pExpr
->iTable
);
311 sqlite3TreeViewLine(pView
,"ID \"%w\"", pExpr
->u
.zToken
);
314 #ifndef SQLITE_OMIT_CAST
316 /* Expressions of the form: CAST(pLeft AS token) */
317 sqlite3TreeViewLine(pView
,"CAST %Q", pExpr
->u
.zToken
);
318 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
321 #endif /* SQLITE_OMIT_CAST */
322 case TK_LT
: zBinOp
= "LT"; break;
323 case TK_LE
: zBinOp
= "LE"; break;
324 case TK_GT
: zBinOp
= "GT"; break;
325 case TK_GE
: zBinOp
= "GE"; break;
326 case TK_NE
: zBinOp
= "NE"; break;
327 case TK_EQ
: zBinOp
= "EQ"; break;
328 case TK_IS
: zBinOp
= "IS"; break;
329 case TK_ISNOT
: zBinOp
= "ISNOT"; break;
330 case TK_AND
: zBinOp
= "AND"; break;
331 case TK_OR
: zBinOp
= "OR"; break;
332 case TK_PLUS
: zBinOp
= "ADD"; break;
333 case TK_STAR
: zBinOp
= "MUL"; break;
334 case TK_MINUS
: zBinOp
= "SUB"; break;
335 case TK_REM
: zBinOp
= "REM"; break;
336 case TK_BITAND
: zBinOp
= "BITAND"; break;
337 case TK_BITOR
: zBinOp
= "BITOR"; break;
338 case TK_SLASH
: zBinOp
= "DIV"; break;
339 case TK_LSHIFT
: zBinOp
= "LSHIFT"; break;
340 case TK_RSHIFT
: zBinOp
= "RSHIFT"; break;
341 case TK_CONCAT
: zBinOp
= "CONCAT"; break;
342 case TK_DOT
: zBinOp
= "DOT"; break;
344 case TK_UMINUS
: zUniOp
= "UMINUS"; break;
345 case TK_UPLUS
: zUniOp
= "UPLUS"; break;
346 case TK_BITNOT
: zUniOp
= "BITNOT"; break;
347 case TK_NOT
: zUniOp
= "NOT"; break;
348 case TK_ISNULL
: zUniOp
= "ISNULL"; break;
349 case TK_NOTNULL
: zUniOp
= "NOTNULL"; break;
352 sqlite3TreeViewLine(pView
, "SPAN %Q", pExpr
->u
.zToken
);
353 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
358 sqlite3TreeViewLine(pView
, "COLLATE %Q", pExpr
->u
.zToken
);
359 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
363 case TK_AGG_FUNCTION
:
365 ExprList
*pFarg
; /* List of function arguments */
366 if( ExprHasProperty(pExpr
, EP_TokenOnly
) ){
369 pFarg
= pExpr
->x
.pList
;
371 if( pExpr
->op
==TK_AGG_FUNCTION
){
372 sqlite3TreeViewLine(pView
, "AGG_FUNCTION%d %Q",
373 pExpr
->op2
, pExpr
->u
.zToken
);
375 sqlite3TreeViewLine(pView
, "FUNCTION %Q", pExpr
->u
.zToken
);
378 sqlite3TreeViewExprList(pView
, pFarg
, 0, 0);
382 #ifndef SQLITE_OMIT_SUBQUERY
384 sqlite3TreeViewLine(pView
, "EXISTS-expr flags=0x%x", pExpr
->flags
);
385 sqlite3TreeViewSelect(pView
, pExpr
->x
.pSelect
, 0);
389 sqlite3TreeViewLine(pView
, "SELECT-expr flags=0x%x", pExpr
->flags
);
390 sqlite3TreeViewSelect(pView
, pExpr
->x
.pSelect
, 0);
394 sqlite3TreeViewLine(pView
, "IN flags=0x%x", pExpr
->flags
);
395 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 1);
396 if( ExprHasProperty(pExpr
, EP_xIsSelect
) ){
397 sqlite3TreeViewSelect(pView
, pExpr
->x
.pSelect
, 0);
399 sqlite3TreeViewExprList(pView
, pExpr
->x
.pList
, 0, 0);
403 #endif /* SQLITE_OMIT_SUBQUERY */
408 ** This is equivalent to
412 ** X is stored in pExpr->pLeft.
413 ** Y is stored in pExpr->pList->a[0].pExpr.
414 ** Z is stored in pExpr->pList->a[1].pExpr.
417 Expr
*pX
= pExpr
->pLeft
;
418 Expr
*pY
= pExpr
->x
.pList
->a
[0].pExpr
;
419 Expr
*pZ
= pExpr
->x
.pList
->a
[1].pExpr
;
420 sqlite3TreeViewLine(pView
, "BETWEEN");
421 sqlite3TreeViewExpr(pView
, pX
, 1);
422 sqlite3TreeViewExpr(pView
, pY
, 1);
423 sqlite3TreeViewExpr(pView
, pZ
, 0);
427 /* If the opcode is TK_TRIGGER, then the expression is a reference
428 ** to a column in the new.* or old.* pseudo-tables available to
429 ** trigger programs. In this case Expr.iTable is set to 1 for the
430 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
431 ** is set to the column of the pseudo-table to read, or to -1 to
432 ** read the rowid field.
434 sqlite3TreeViewLine(pView
, "%s(%d)",
435 pExpr
->iTable
? "NEW" : "OLD", pExpr
->iColumn
);
439 sqlite3TreeViewLine(pView
, "CASE");
440 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 1);
441 sqlite3TreeViewExprList(pView
, pExpr
->x
.pList
, 0, 0);
444 #ifndef SQLITE_OMIT_TRIGGER
446 const char *zType
= "unk";
447 switch( pExpr
->affinity
){
448 case OE_Rollback
: zType
= "rollback"; break;
449 case OE_Abort
: zType
= "abort"; break;
450 case OE_Fail
: zType
= "fail"; break;
451 case OE_Ignore
: zType
= "ignore"; break;
453 sqlite3TreeViewLine(pView
, "RAISE %s(%Q)", zType
, pExpr
->u
.zToken
);
458 sqlite3TreeViewLine(pView
, "MATCH {%d:%d}%s",
459 pExpr
->iTable
, pExpr
->iColumn
, zFlgs
);
460 sqlite3TreeViewExpr(pView
, pExpr
->pRight
, 0);
464 sqlite3TreeViewBareExprList(pView
, pExpr
->x
.pList
, "VECTOR");
467 case TK_SELECT_COLUMN
: {
468 sqlite3TreeViewLine(pView
, "SELECT-COLUMN %d", pExpr
->iColumn
);
469 sqlite3TreeViewSelect(pView
, pExpr
->pLeft
->x
.pSelect
, 0);
472 case TK_IF_NULL_ROW
: {
473 sqlite3TreeViewLine(pView
, "IF-NULL-ROW %d", pExpr
->iTable
);
474 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
478 sqlite3TreeViewLine(pView
, "op=%d", pExpr
->op
);
483 sqlite3TreeViewLine(pView
, "%s%s", zBinOp
, zFlgs
);
484 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 1);
485 sqlite3TreeViewExpr(pView
, pExpr
->pRight
, 0);
487 sqlite3TreeViewLine(pView
, "%s%s", zUniOp
, zFlgs
);
488 sqlite3TreeViewExpr(pView
, pExpr
->pLeft
, 0);
490 sqlite3TreeViewPop(pView
);
495 ** Generate a human-readable explanation of an expression list.
497 void sqlite3TreeViewBareExprList(
499 const ExprList
*pList
,
502 if( zLabel
==0 || zLabel
[0]==0 ) zLabel
= "LIST";
504 sqlite3TreeViewLine(pView
, "%s (empty)", zLabel
);
507 sqlite3TreeViewLine(pView
, "%s", zLabel
);
508 for(i
=0; i
<pList
->nExpr
; i
++){
509 int j
= pList
->a
[i
].u
.x
.iOrderByCol
;
510 char *zName
= pList
->a
[i
].zName
;
512 sqlite3TreeViewPush(pView
, 0);
515 sqlite3TreeViewLine(pView
, "AS %s", zName
);
518 sqlite3TreeViewLine(pView
, "iOrderByCol=%d", j
);
520 sqlite3TreeViewExpr(pView
, pList
->a
[i
].pExpr
, i
<pList
->nExpr
-1);
522 sqlite3TreeViewPop(pView
);
527 void sqlite3TreeViewExprList(
529 const ExprList
*pList
,
533 pView
= sqlite3TreeViewPush(pView
, moreToFollow
);
534 sqlite3TreeViewBareExprList(pView
, pList
, zLabel
);
535 sqlite3TreeViewPop(pView
);
538 #endif /* SQLITE_DEBUG */