Merge branch 'prerelease' into codecerror
[sqlcipher.git] / src / treeview.c
blob187f1a07d1c38cfa4a8b017c8fced961f0d271b1
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.
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
15 ** analysis.
17 ** The interfaces in this file is only available when compiling
18 ** with SQLITE_DEBUG.
20 #include "sqliteInt.h"
21 #ifdef SQLITE_DEBUG
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){
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++;
35 assert( moreToFollow==0 || moreToFollow==1 );
36 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
37 return p;
41 ** Finished with one layer of the tree
43 static void sqlite3TreeViewPop(TreeView *p){
44 if( p==0 ) return;
45 p->iLevel--;
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, ...){
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 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
63 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
65 if( zFormat!=0 ){
66 va_start(ap, zFormat);
67 sqlite3_str_vappendf(&acc, zFormat, ap);
68 va_end(ap);
69 assert( acc.nChar>0 || acc.accError );
70 sqlite3_str_append(&acc, "\n", 1);
72 sqlite3StrAccumFinish(&acc);
73 fprintf(stdout,"%s", zBuf);
74 fflush(stdout);
78 ** Shorthand for starting a new tree item that consists of a single label
80 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
81 p = sqlite3TreeViewPush(p, moreFollows);
82 sqlite3TreeViewLine(p, "%s", zLabel);
86 ** Generate a human-readable description of a WITH clause.
88 void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
89 int i;
90 if( pWith==0 ) return;
91 if( pWith->nCte==0 ) return;
92 if( pWith->pOuter ){
93 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
94 }else{
95 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
97 if( pWith->nCte>0 ){
98 pView = sqlite3TreeViewPush(pView, 1);
99 for(i=0; i<pWith->nCte; i++){
100 StrAccum x;
101 char zLine[1000];
102 const struct Cte *pCte = &pWith->a[i];
103 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
104 sqlite3_str_appendf(&x, "%s", pCte->zName);
105 if( pCte->pCols && pCte->pCols->nExpr>0 ){
106 char cSep = '(';
107 int j;
108 for(j=0; j<pCte->pCols->nExpr; j++){
109 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
110 cSep = ',';
112 sqlite3_str_appendf(&x, ")");
114 sqlite3_str_appendf(&x, " AS");
115 sqlite3StrAccumFinish(&x);
116 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
117 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
118 sqlite3TreeViewPop(pView);
120 sqlite3TreeViewPop(pView);
125 ** Generate a human-readable description of a SrcList object.
127 void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
128 int i;
129 for(i=0; i<pSrc->nSrc; i++){
130 const struct SrcList_item *pItem = &pSrc->a[i];
131 StrAccum x;
132 char zLine[100];
133 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
134 sqlite3_str_appendf(&x, "{%d:*}", pItem->iCursor);
135 if( pItem->zDatabase ){
136 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
137 }else if( pItem->zName ){
138 sqlite3_str_appendf(&x, " %s", pItem->zName);
140 if( pItem->pTab ){
141 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
142 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
144 if( pItem->zAlias ){
145 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
147 if( pItem->fg.jointype & JT_LEFT ){
148 sqlite3_str_appendf(&x, " LEFT-JOIN");
150 if( pItem->fg.fromDDL ){
151 sqlite3_str_appendf(&x, " DDL");
153 sqlite3StrAccumFinish(&x);
154 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
155 if( pItem->pSelect ){
156 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
158 if( pItem->fg.isTabFunc ){
159 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
161 sqlite3TreeViewPop(pView);
166 ** Generate a human-readable description of a Select object.
168 void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
169 int n = 0;
170 int cnt = 0;
171 if( p==0 ){
172 sqlite3TreeViewLine(pView, "nil-SELECT");
173 return;
175 pView = sqlite3TreeViewPush(pView, moreToFollow);
176 if( p->pWith ){
177 sqlite3TreeViewWith(pView, p->pWith, 1);
178 cnt = 1;
179 sqlite3TreeViewPush(pView, 1);
182 if( p->selFlags & SF_WhereBegin ){
183 sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
184 }else{
185 sqlite3TreeViewLine(pView,
186 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
187 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
188 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
189 p->selId, p, p->selFlags,
190 (int)p->nSelectRow
193 if( cnt++ ) sqlite3TreeViewPop(pView);
194 if( p->pPrior ){
195 n = 1000;
196 }else{
197 n = 0;
198 if( p->pSrc && p->pSrc->nSrc ) n++;
199 if( p->pWhere ) n++;
200 if( p->pGroupBy ) n++;
201 if( p->pHaving ) n++;
202 if( p->pOrderBy ) n++;
203 if( p->pLimit ) n++;
204 #ifndef SQLITE_OMIT_WINDOWFUNC
205 if( p->pWin ) n++;
206 if( p->pWinDefn ) n++;
207 #endif
209 if( p->pEList ){
210 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
212 n--;
213 #ifndef SQLITE_OMIT_WINDOWFUNC
214 if( p->pWin ){
215 Window *pX;
216 pView = sqlite3TreeViewPush(pView, (n--)>0);
217 sqlite3TreeViewLine(pView, "window-functions");
218 for(pX=p->pWin; pX; pX=pX->pNextWin){
219 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
221 sqlite3TreeViewPop(pView);
223 #endif
224 if( p->pSrc && p->pSrc->nSrc ){
225 pView = sqlite3TreeViewPush(pView, (n--)>0);
226 sqlite3TreeViewLine(pView, "FROM");
227 sqlite3TreeViewSrcList(pView, p->pSrc);
228 sqlite3TreeViewPop(pView);
230 if( p->pWhere ){
231 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
232 sqlite3TreeViewExpr(pView, p->pWhere, 0);
233 sqlite3TreeViewPop(pView);
235 if( p->pGroupBy ){
236 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
238 if( p->pHaving ){
239 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
240 sqlite3TreeViewExpr(pView, p->pHaving, 0);
241 sqlite3TreeViewPop(pView);
243 #ifndef SQLITE_OMIT_WINDOWFUNC
244 if( p->pWinDefn ){
245 Window *pX;
246 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
247 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
248 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
250 sqlite3TreeViewPop(pView);
252 #endif
253 if( p->pOrderBy ){
254 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
256 if( p->pLimit ){
257 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
258 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
259 if( p->pLimit->pRight ){
260 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
261 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
262 sqlite3TreeViewPop(pView);
264 sqlite3TreeViewPop(pView);
266 if( p->pPrior ){
267 const char *zOp = "UNION";
268 switch( p->op ){
269 case TK_ALL: zOp = "UNION ALL"; break;
270 case TK_INTERSECT: zOp = "INTERSECT"; break;
271 case TK_EXCEPT: zOp = "EXCEPT"; break;
273 sqlite3TreeViewItem(pView, zOp, 1);
275 p = p->pPrior;
276 }while( p!=0 );
277 sqlite3TreeViewPop(pView);
280 #ifndef SQLITE_OMIT_WINDOWFUNC
282 ** Generate a description of starting or stopping bounds
284 void sqlite3TreeViewBound(
285 TreeView *pView, /* View context */
286 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
287 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
288 u8 moreToFollow /* True if more to follow */
290 switch( eBound ){
291 case TK_UNBOUNDED: {
292 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
293 sqlite3TreeViewPop(pView);
294 break;
296 case TK_CURRENT: {
297 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
298 sqlite3TreeViewPop(pView);
299 break;
301 case TK_PRECEDING: {
302 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
303 sqlite3TreeViewExpr(pView, pExpr, 0);
304 sqlite3TreeViewPop(pView);
305 break;
307 case TK_FOLLOWING: {
308 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
309 sqlite3TreeViewExpr(pView, pExpr, 0);
310 sqlite3TreeViewPop(pView);
311 break;
315 #endif /* SQLITE_OMIT_WINDOWFUNC */
317 #ifndef SQLITE_OMIT_WINDOWFUNC
319 ** Generate a human-readable explanation for a Window object
321 void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
322 int nElement = 0;
323 if( pWin->pFilter ){
324 sqlite3TreeViewItem(pView, "FILTER", 1);
325 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
326 sqlite3TreeViewPop(pView);
328 pView = sqlite3TreeViewPush(pView, more);
329 if( pWin->zName ){
330 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
331 }else{
332 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
334 if( pWin->zBase ) nElement++;
335 if( pWin->pOrderBy ) nElement++;
336 if( pWin->eFrmType ) nElement++;
337 if( pWin->eExclude ) nElement++;
338 if( pWin->zBase ){
339 sqlite3TreeViewPush(pView, (--nElement)>0);
340 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
341 sqlite3TreeViewPop(pView);
343 if( pWin->pPartition ){
344 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
346 if( pWin->pOrderBy ){
347 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
349 if( pWin->eFrmType ){
350 char zBuf[30];
351 const char *zFrmType = "ROWS";
352 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
353 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
354 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
355 pWin->bImplicitFrame ? " (implied)" : "");
356 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
357 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
358 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
359 sqlite3TreeViewPop(pView);
361 if( pWin->eExclude ){
362 char zBuf[30];
363 const char *zExclude;
364 switch( pWin->eExclude ){
365 case TK_NO: zExclude = "NO OTHERS"; break;
366 case TK_CURRENT: zExclude = "CURRENT ROW"; break;
367 case TK_GROUP: zExclude = "GROUP"; break;
368 case TK_TIES: zExclude = "TIES"; break;
369 default:
370 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
371 zExclude = zBuf;
372 break;
374 sqlite3TreeViewPush(pView, 0);
375 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
376 sqlite3TreeViewPop(pView);
378 sqlite3TreeViewPop(pView);
380 #endif /* SQLITE_OMIT_WINDOWFUNC */
382 #ifndef SQLITE_OMIT_WINDOWFUNC
384 ** Generate a human-readable explanation for a Window Function object
386 void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
387 pView = sqlite3TreeViewPush(pView, more);
388 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
389 pWin->pFunc->zName, pWin->pFunc->nArg);
390 sqlite3TreeViewWindow(pView, pWin, 0);
391 sqlite3TreeViewPop(pView);
393 #endif /* SQLITE_OMIT_WINDOWFUNC */
396 ** Generate a human-readable explanation of an expression tree.
398 void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
399 const char *zBinOp = 0; /* Binary operator */
400 const char *zUniOp = 0; /* Unary operator */
401 char zFlgs[200];
402 pView = sqlite3TreeViewPush(pView, moreToFollow);
403 if( pExpr==0 ){
404 sqlite3TreeViewLine(pView, "nil");
405 sqlite3TreeViewPop(pView);
406 return;
408 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
409 StrAccum x;
410 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
411 sqlite3_str_appendf(&x, " fg.af=%x.%c",
412 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
413 if( ExprHasProperty(pExpr, EP_FromJoin) ){
414 sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
416 if( ExprHasProperty(pExpr, EP_FromDDL) ){
417 sqlite3_str_appendf(&x, " DDL");
419 if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
420 sqlite3_str_appendf(&x, " IMMUTABLE");
422 sqlite3StrAccumFinish(&x);
423 }else{
424 zFlgs[0] = 0;
426 switch( pExpr->op ){
427 case TK_AGG_COLUMN: {
428 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
429 pExpr->iTable, pExpr->iColumn, zFlgs);
430 break;
432 case TK_COLUMN: {
433 if( pExpr->iTable<0 ){
434 /* This only happens when coding check constraints */
435 char zOp2[16];
436 if( pExpr->op2 ){
437 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
438 }else{
439 zOp2[0] = 0;
441 sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
442 pExpr->iColumn, zFlgs, zOp2);
443 }else{
444 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
445 pExpr->iTable, pExpr->iColumn,
446 pExpr->y.pTab, zFlgs);
448 if( ExprHasProperty(pExpr, EP_FixedCol) ){
449 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
451 break;
453 case TK_INTEGER: {
454 if( pExpr->flags & EP_IntValue ){
455 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
456 }else{
457 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
459 break;
461 #ifndef SQLITE_OMIT_FLOATING_POINT
462 case TK_FLOAT: {
463 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
464 break;
466 #endif
467 case TK_STRING: {
468 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
469 break;
471 case TK_NULL: {
472 sqlite3TreeViewLine(pView,"NULL");
473 break;
475 case TK_TRUEFALSE: {
476 sqlite3TreeViewLine(pView,
477 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
478 break;
480 #ifndef SQLITE_OMIT_BLOB_LITERAL
481 case TK_BLOB: {
482 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
483 break;
485 #endif
486 case TK_VARIABLE: {
487 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
488 pExpr->u.zToken, pExpr->iColumn);
489 break;
491 case TK_REGISTER: {
492 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
493 break;
495 case TK_ID: {
496 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
497 break;
499 #ifndef SQLITE_OMIT_CAST
500 case TK_CAST: {
501 /* Expressions of the form: CAST(pLeft AS token) */
502 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
503 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
504 break;
506 #endif /* SQLITE_OMIT_CAST */
507 case TK_LT: zBinOp = "LT"; break;
508 case TK_LE: zBinOp = "LE"; break;
509 case TK_GT: zBinOp = "GT"; break;
510 case TK_GE: zBinOp = "GE"; break;
511 case TK_NE: zBinOp = "NE"; break;
512 case TK_EQ: zBinOp = "EQ"; break;
513 case TK_IS: zBinOp = "IS"; break;
514 case TK_ISNOT: zBinOp = "ISNOT"; break;
515 case TK_AND: zBinOp = "AND"; break;
516 case TK_OR: zBinOp = "OR"; break;
517 case TK_PLUS: zBinOp = "ADD"; break;
518 case TK_STAR: zBinOp = "MUL"; break;
519 case TK_MINUS: zBinOp = "SUB"; break;
520 case TK_REM: zBinOp = "REM"; break;
521 case TK_BITAND: zBinOp = "BITAND"; break;
522 case TK_BITOR: zBinOp = "BITOR"; break;
523 case TK_SLASH: zBinOp = "DIV"; break;
524 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
525 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
526 case TK_CONCAT: zBinOp = "CONCAT"; break;
527 case TK_DOT: zBinOp = "DOT"; break;
528 case TK_LIMIT: zBinOp = "LIMIT"; break;
530 case TK_UMINUS: zUniOp = "UMINUS"; break;
531 case TK_UPLUS: zUniOp = "UPLUS"; break;
532 case TK_BITNOT: zUniOp = "BITNOT"; break;
533 case TK_NOT: zUniOp = "NOT"; break;
534 case TK_ISNULL: zUniOp = "ISNULL"; break;
535 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
537 case TK_TRUTH: {
538 int x;
539 const char *azOp[] = {
540 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
542 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
543 assert( pExpr->pRight );
544 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
545 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
546 zUniOp = azOp[x];
547 break;
550 case TK_SPAN: {
551 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
552 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
553 break;
556 case TK_COLLATE: {
557 /* COLLATE operators without the EP_Collate flag are intended to
558 ** emulate collation associated with a table column. These show
559 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
560 ** operators that appear in the original SQL always have the
561 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
562 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
563 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
564 pExpr->u.zToken, zFlgs);
565 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
566 break;
569 case TK_AGG_FUNCTION:
570 case TK_FUNCTION: {
571 ExprList *pFarg; /* List of function arguments */
572 Window *pWin;
573 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
574 pFarg = 0;
575 pWin = 0;
576 }else{
577 pFarg = pExpr->x.pList;
578 #ifndef SQLITE_OMIT_WINDOWFUNC
579 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
580 #else
581 pWin = 0;
582 #endif
584 if( pExpr->op==TK_AGG_FUNCTION ){
585 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
586 pExpr->op2, pExpr->u.zToken, zFlgs,
587 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
588 pExpr->iAgg, pExpr->pAggInfo);
589 }else if( pExpr->op2!=0 ){
590 const char *zOp2;
591 char zBuf[8];
592 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
593 zOp2 = zBuf;
594 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
595 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
596 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
597 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
598 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
599 pExpr->u.zToken, zFlgs, zOp2);
600 }else{
601 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
603 if( pFarg ){
604 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
606 #ifndef SQLITE_OMIT_WINDOWFUNC
607 if( pWin ){
608 sqlite3TreeViewWindow(pView, pWin, 0);
610 #endif
611 break;
613 #ifndef SQLITE_OMIT_SUBQUERY
614 case TK_EXISTS: {
615 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
616 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
617 break;
619 case TK_SELECT: {
620 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
621 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
622 break;
624 case TK_IN: {
625 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
626 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
627 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
628 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
629 }else{
630 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
632 break;
634 #endif /* SQLITE_OMIT_SUBQUERY */
637 ** x BETWEEN y AND z
639 ** This is equivalent to
641 ** x>=y AND x<=z
643 ** X is stored in pExpr->pLeft.
644 ** Y is stored in pExpr->pList->a[0].pExpr.
645 ** Z is stored in pExpr->pList->a[1].pExpr.
647 case TK_BETWEEN: {
648 Expr *pX = pExpr->pLeft;
649 Expr *pY = pExpr->x.pList->a[0].pExpr;
650 Expr *pZ = pExpr->x.pList->a[1].pExpr;
651 sqlite3TreeViewLine(pView, "BETWEEN");
652 sqlite3TreeViewExpr(pView, pX, 1);
653 sqlite3TreeViewExpr(pView, pY, 1);
654 sqlite3TreeViewExpr(pView, pZ, 0);
655 break;
657 case TK_TRIGGER: {
658 /* If the opcode is TK_TRIGGER, then the expression is a reference
659 ** to a column in the new.* or old.* pseudo-tables available to
660 ** trigger programs. In this case Expr.iTable is set to 1 for the
661 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
662 ** is set to the column of the pseudo-table to read, or to -1 to
663 ** read the rowid field.
665 sqlite3TreeViewLine(pView, "%s(%d)",
666 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
667 break;
669 case TK_CASE: {
670 sqlite3TreeViewLine(pView, "CASE");
671 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
672 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
673 break;
675 #ifndef SQLITE_OMIT_TRIGGER
676 case TK_RAISE: {
677 const char *zType = "unk";
678 switch( pExpr->affExpr ){
679 case OE_Rollback: zType = "rollback"; break;
680 case OE_Abort: zType = "abort"; break;
681 case OE_Fail: zType = "fail"; break;
682 case OE_Ignore: zType = "ignore"; break;
684 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
685 break;
687 #endif
688 case TK_MATCH: {
689 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
690 pExpr->iTable, pExpr->iColumn, zFlgs);
691 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
692 break;
694 case TK_VECTOR: {
695 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
696 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
697 sqlite3_free(z);
698 break;
700 case TK_SELECT_COLUMN: {
701 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
702 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
703 break;
705 case TK_IF_NULL_ROW: {
706 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
707 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
708 break;
710 default: {
711 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
712 break;
715 if( zBinOp ){
716 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
717 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
718 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
719 }else if( zUniOp ){
720 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
721 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
723 sqlite3TreeViewPop(pView);
728 ** Generate a human-readable explanation of an expression list.
730 void sqlite3TreeViewBareExprList(
731 TreeView *pView,
732 const ExprList *pList,
733 const char *zLabel
735 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
736 if( pList==0 ){
737 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
738 }else{
739 int i;
740 sqlite3TreeViewLine(pView, "%s", zLabel);
741 for(i=0; i<pList->nExpr; i++){
742 int j = pList->a[i].u.x.iOrderByCol;
743 char *zName = pList->a[i].zEName;
744 int moreToFollow = i<pList->nExpr - 1;
745 if( pList->a[i].eEName!=ENAME_NAME ) zName = 0;
746 if( j || zName ){
747 sqlite3TreeViewPush(pView, moreToFollow);
748 moreToFollow = 0;
749 sqlite3TreeViewLine(pView, 0);
750 if( zName ){
751 fprintf(stdout, "AS %s ", zName);
753 if( j ){
754 fprintf(stdout, "iOrderByCol=%d", j);
756 fprintf(stdout, "\n");
757 fflush(stdout);
759 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
760 if( j || zName ){
761 sqlite3TreeViewPop(pView);
766 void sqlite3TreeViewExprList(
767 TreeView *pView,
768 const ExprList *pList,
769 u8 moreToFollow,
770 const char *zLabel
772 pView = sqlite3TreeViewPush(pView, moreToFollow);
773 sqlite3TreeViewBareExprList(pView, pList, zLabel);
774 sqlite3TreeViewPop(pView);
777 #endif /* SQLITE_DEBUG */