xref: /sqlite-3.40.0/src/treeview.c (revision d88fd539)
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 void sqlite3TreeViewPush(TreeView **pp, u8 moreToFollow){
28   TreeView *p = *pp;
29   if( p==0 ){
30     *pp = p = sqlite3_malloc64( sizeof(*p) );
31     if( p==0 ) return;
32     memset(p, 0, sizeof(*p));
33   }else{
34     p->iLevel++;
35   }
36   assert( moreToFollow==0 || moreToFollow==1 );
37   if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
38 }
39 
40 /*
41 ** Finished with one layer of the tree
42 */
43 static void sqlite3TreeViewPop(TreeView **pp){
44   TreeView *p = *pp;
45   if( p==0 ) return;
46   p->iLevel--;
47   if( p->iLevel<0 ){
48     sqlite3_free(p);
49     *pp = 0;
50   }
51 }
52 
53 /*
54 ** Generate a single line of output for the tree, with a prefix that contains
55 ** all the appropriate tree lines
56 */
57 void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
58   va_list ap;
59   int i;
60   StrAccum acc;
61   char zBuf[500];
62   sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
63   if( p ){
64     for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){
65       sqlite3_str_append(&acc, p->bLine[i] ? "|   " : "    ", 4);
66     }
67     sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
68   }
69   if( zFormat!=0 ){
70     va_start(ap, zFormat);
71     sqlite3_str_vappendf(&acc, zFormat, ap);
72     va_end(ap);
73     assert( acc.nChar>0 || acc.accError );
74     sqlite3_str_append(&acc, "\n", 1);
75   }
76   sqlite3StrAccumFinish(&acc);
77   fprintf(stdout,"%s", zBuf);
78   fflush(stdout);
79 }
80 
81 /*
82 ** Shorthand for starting a new tree item that consists of a single label
83 */
84 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
85   sqlite3TreeViewPush(&p, moreFollows);
86   sqlite3TreeViewLine(p, "%s", zLabel);
87 }
88 
89 /*
90 ** Show a list of Column objects in tree format.
91 */
92 void sqlite3TreeViewColumnList(
93   TreeView *pView,
94   const Column *aCol,
95   int nCol,
96   u8 moreToFollow
97 ){
98   int i;
99   sqlite3TreeViewPush(&pView, moreToFollow);
100   sqlite3TreeViewLine(pView, "COLUMNS");
101   for(i=0; i<nCol; i++){
102     u16 flg = aCol[i].colFlags;
103     int moreToFollow = i<(nCol - 1);
104     sqlite3TreeViewPush(&pView, moreToFollow);
105     sqlite3TreeViewLine(pView, 0);
106     printf(" %s", aCol[i].zCnName);
107     switch( aCol[i].eCType ){
108       case COLTYPE_ANY:      printf(" ANY");        break;
109       case COLTYPE_BLOB:     printf(" BLOB");       break;
110       case COLTYPE_INT:      printf(" INT");        break;
111       case COLTYPE_INTEGER:  printf(" INTEGER");    break;
112       case COLTYPE_REAL:     printf(" REAL");       break;
113       case COLTYPE_TEXT:     printf(" TEXT");       break;
114       case COLTYPE_CUSTOM: {
115         if( flg & COLFLAG_HASTYPE ){
116           const char *z = aCol[i].zCnName;
117           z += strlen(z)+1;
118           printf(" X-%s", z);
119           break;
120         }
121       }
122     }
123     if( flg & COLFLAG_PRIMKEY ) printf(" PRIMARY KEY");
124     if( flg & COLFLAG_HIDDEN ) printf(" HIDDEN");
125 #ifdef COLFLAG_NOEXPAND
126     if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND");
127 #endif
128     if( flg ) printf(" flags=%04x", flg);
129     printf("\n");
130     fflush(stdout);
131     sqlite3TreeViewPop(&pView);
132   }
133   sqlite3TreeViewPop(&pView);
134 }
135 
136 /*
137 ** Generate a human-readable description of a WITH clause.
138 */
139 void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
140   int i;
141   if( pWith==0 ) return;
142   if( pWith->nCte==0 ) return;
143   if( pWith->pOuter ){
144     sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
145   }else{
146     sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
147   }
148   if( pWith->nCte>0 ){
149     sqlite3TreeViewPush(&pView, moreToFollow);
150     for(i=0; i<pWith->nCte; i++){
151       StrAccum x;
152       char zLine[1000];
153       const struct Cte *pCte = &pWith->a[i];
154       sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
155       sqlite3_str_appendf(&x, "%s", pCte->zName);
156       if( pCte->pCols && pCte->pCols->nExpr>0 ){
157         char cSep = '(';
158         int j;
159         for(j=0; j<pCte->pCols->nExpr; j++){
160           sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
161           cSep = ',';
162         }
163         sqlite3_str_appendf(&x, ")");
164       }
165       if( pCte->eM10d!=M10d_Any ){
166         sqlite3_str_appendf(&x, " %sMATERIALIZED",
167            pCte->eM10d==M10d_No ? "NOT " : "");
168       }
169       if( pCte->pUse ){
170         sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
171                  pCte->pUse->nUse);
172       }
173       sqlite3StrAccumFinish(&x);
174       sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
175       sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
176       sqlite3TreeViewPop(&pView);
177     }
178     sqlite3TreeViewPop(&pView);
179   }
180 }
181 
182 /*
183 ** Generate a human-readable description of a SrcList object.
184 */
185 void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
186   int i;
187   if( pSrc==0 ) return;
188   for(i=0; i<pSrc->nSrc; i++){
189     const SrcItem *pItem = &pSrc->a[i];
190     StrAccum x;
191     int n = 0;
192     char zLine[100];
193     sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
194     x.printfFlags |= SQLITE_PRINTF_INTERNAL;
195     sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
196     if( pItem->pTab ){
197       sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
198            pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
199     }
200     if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){
201       sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
202     }else if( pItem->fg.jointype & JT_LEFT ){
203       sqlite3_str_appendf(&x, " LEFT-JOIN");
204     }else if( pItem->fg.jointype & JT_RIGHT ){
205       sqlite3_str_appendf(&x, " RIGHT-JOIN");
206     }else if( pItem->fg.jointype & JT_CROSS ){
207       sqlite3_str_appendf(&x, " CROSS-JOIN");
208     }
209     if( pItem->fg.jointype & JT_LTORJ ){
210       sqlite3_str_appendf(&x, " LTORJ");
211     }
212     if( pItem->fg.fromDDL ){
213       sqlite3_str_appendf(&x, " DDL");
214     }
215     if( pItem->fg.isCte ){
216       sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
217     }
218     sqlite3StrAccumFinish(&x);
219     sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
220     n = 0;
221     if( pItem->pSelect ) n++;
222     if( pItem->fg.isTabFunc ) n++;
223     if( pItem->fg.isUsing ) n++;
224     if( pItem->fg.isUsing ){
225       sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
226     }
227     if( pItem->pSelect ){
228       if( pItem->pTab ){
229         Table *pTab = pItem->pTab;
230         sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
231       }
232       assert( pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
233       sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
234     }
235     if( pItem->fg.isTabFunc ){
236       sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
237     }
238     sqlite3TreeViewPop(&pView);
239   }
240 }
241 
242 /*
243 ** Generate a human-readable description of a Select object.
244 */
245 void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
246   int n = 0;
247   int cnt = 0;
248   if( p==0 ){
249     sqlite3TreeViewLine(pView, "nil-SELECT");
250     return;
251   }
252   sqlite3TreeViewPush(&pView, moreToFollow);
253   if( p->pWith ){
254     sqlite3TreeViewWith(pView, p->pWith, 1);
255     cnt = 1;
256     sqlite3TreeViewPush(&pView, 1);
257   }
258   do{
259     if( p->selFlags & SF_WhereBegin ){
260       sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
261     }else{
262       sqlite3TreeViewLine(pView,
263         "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
264         ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
265         ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
266         p->selId, p, p->selFlags,
267         (int)p->nSelectRow
268       );
269     }
270     if( cnt++ ) sqlite3TreeViewPop(&pView);
271     if( p->pPrior ){
272       n = 1000;
273     }else{
274       n = 0;
275       if( p->pSrc && p->pSrc->nSrc ) n++;
276       if( p->pWhere ) n++;
277       if( p->pGroupBy ) n++;
278       if( p->pHaving ) n++;
279       if( p->pOrderBy ) n++;
280       if( p->pLimit ) n++;
281 #ifndef SQLITE_OMIT_WINDOWFUNC
282       if( p->pWin ) n++;
283       if( p->pWinDefn ) n++;
284 #endif
285     }
286     if( p->pEList ){
287       sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
288     }
289     n--;
290 #ifndef SQLITE_OMIT_WINDOWFUNC
291     if( p->pWin ){
292       Window *pX;
293       sqlite3TreeViewPush(&pView, (n--)>0);
294       sqlite3TreeViewLine(pView, "window-functions");
295       for(pX=p->pWin; pX; pX=pX->pNextWin){
296         sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
297       }
298       sqlite3TreeViewPop(&pView);
299     }
300 #endif
301     if( p->pSrc && p->pSrc->nSrc ){
302       sqlite3TreeViewPush(&pView, (n--)>0);
303       sqlite3TreeViewLine(pView, "FROM");
304       sqlite3TreeViewSrcList(pView, p->pSrc);
305       sqlite3TreeViewPop(&pView);
306     }
307     if( p->pWhere ){
308       sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
309       sqlite3TreeViewExpr(pView, p->pWhere, 0);
310       sqlite3TreeViewPop(&pView);
311     }
312     if( p->pGroupBy ){
313       sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
314     }
315     if( p->pHaving ){
316       sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
317       sqlite3TreeViewExpr(pView, p->pHaving, 0);
318       sqlite3TreeViewPop(&pView);
319     }
320 #ifndef SQLITE_OMIT_WINDOWFUNC
321     if( p->pWinDefn ){
322       Window *pX;
323       sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
324       for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
325         sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
326       }
327       sqlite3TreeViewPop(&pView);
328     }
329 #endif
330     if( p->pOrderBy ){
331       sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
332     }
333     if( p->pLimit ){
334       sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
335       sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
336       if( p->pLimit->pRight ){
337         sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
338         sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
339         sqlite3TreeViewPop(&pView);
340       }
341       sqlite3TreeViewPop(&pView);
342     }
343     if( p->pPrior ){
344       const char *zOp = "UNION";
345       switch( p->op ){
346         case TK_ALL:         zOp = "UNION ALL";  break;
347         case TK_INTERSECT:   zOp = "INTERSECT";  break;
348         case TK_EXCEPT:      zOp = "EXCEPT";     break;
349       }
350       sqlite3TreeViewItem(pView, zOp, 1);
351     }
352     p = p->pPrior;
353   }while( p!=0 );
354   sqlite3TreeViewPop(&pView);
355 }
356 
357 #ifndef SQLITE_OMIT_WINDOWFUNC
358 /*
359 ** Generate a description of starting or stopping bounds
360 */
361 void sqlite3TreeViewBound(
362   TreeView *pView,        /* View context */
363   u8 eBound,              /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
364   Expr *pExpr,            /* Value for PRECEDING or FOLLOWING */
365   u8 moreToFollow         /* True if more to follow */
366 ){
367   switch( eBound ){
368     case TK_UNBOUNDED: {
369       sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
370       sqlite3TreeViewPop(&pView);
371       break;
372     }
373     case TK_CURRENT: {
374       sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
375       sqlite3TreeViewPop(&pView);
376       break;
377     }
378     case TK_PRECEDING: {
379       sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
380       sqlite3TreeViewExpr(pView, pExpr, 0);
381       sqlite3TreeViewPop(&pView);
382       break;
383     }
384     case TK_FOLLOWING: {
385       sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
386       sqlite3TreeViewExpr(pView, pExpr, 0);
387       sqlite3TreeViewPop(&pView);
388       break;
389     }
390   }
391 }
392 #endif /* SQLITE_OMIT_WINDOWFUNC */
393 
394 #ifndef SQLITE_OMIT_WINDOWFUNC
395 /*
396 ** Generate a human-readable explanation for a Window object
397 */
398 void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
399   int nElement = 0;
400   if( pWin==0 ) return;
401   if( pWin->pFilter ){
402     sqlite3TreeViewItem(pView, "FILTER", 1);
403     sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
404     sqlite3TreeViewPop(&pView);
405   }
406   sqlite3TreeViewPush(&pView, more);
407   if( pWin->zName ){
408     sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
409   }else{
410     sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
411   }
412   if( pWin->zBase )    nElement++;
413   if( pWin->pOrderBy ) nElement++;
414   if( pWin->eFrmType ) nElement++;
415   if( pWin->eExclude ) nElement++;
416   if( pWin->zBase ){
417     sqlite3TreeViewPush(&pView, (--nElement)>0);
418     sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
419     sqlite3TreeViewPop(&pView);
420   }
421   if( pWin->pPartition ){
422     sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
423   }
424   if( pWin->pOrderBy ){
425     sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
426   }
427   if( pWin->eFrmType ){
428     char zBuf[30];
429     const char *zFrmType = "ROWS";
430     if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
431     if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
432     sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
433         pWin->bImplicitFrame ? " (implied)" : "");
434     sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
435     sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
436     sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
437     sqlite3TreeViewPop(&pView);
438   }
439   if( pWin->eExclude ){
440     char zBuf[30];
441     const char *zExclude;
442     switch( pWin->eExclude ){
443       case TK_NO:      zExclude = "NO OTHERS";   break;
444       case TK_CURRENT: zExclude = "CURRENT ROW"; break;
445       case TK_GROUP:   zExclude = "GROUP";       break;
446       case TK_TIES:    zExclude = "TIES";        break;
447       default:
448         sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
449         zExclude = zBuf;
450         break;
451     }
452     sqlite3TreeViewPush(&pView, 0);
453     sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
454     sqlite3TreeViewPop(&pView);
455   }
456   sqlite3TreeViewPop(&pView);
457 }
458 #endif /* SQLITE_OMIT_WINDOWFUNC */
459 
460 #ifndef SQLITE_OMIT_WINDOWFUNC
461 /*
462 ** Generate a human-readable explanation for a Window Function object
463 */
464 void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
465   if( pWin==0 ) return;
466   sqlite3TreeViewPush(&pView, more);
467   sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
468                        pWin->pWFunc->zName, pWin->pWFunc->nArg);
469   sqlite3TreeViewWindow(pView, pWin, 0);
470   sqlite3TreeViewPop(&pView);
471 }
472 #endif /* SQLITE_OMIT_WINDOWFUNC */
473 
474 /*
475 ** Generate a human-readable explanation of an expression tree.
476 */
477 void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
478   const char *zBinOp = 0;   /* Binary operator */
479   const char *zUniOp = 0;   /* Unary operator */
480   char zFlgs[200];
481   sqlite3TreeViewPush(&pView, moreToFollow);
482   if( pExpr==0 ){
483     sqlite3TreeViewLine(pView, "nil");
484     sqlite3TreeViewPop(&pView);
485     return;
486   }
487   if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
488     StrAccum x;
489     sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
490     sqlite3_str_appendf(&x, " fg.af=%x.%c",
491       pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
492     if( ExprHasProperty(pExpr, EP_FromJoin) ){
493       sqlite3_str_appendf(&x, " iJoin=%d", pExpr->w.iJoin);
494     }
495     if( ExprHasProperty(pExpr, EP_FromDDL) ){
496       sqlite3_str_appendf(&x, " DDL");
497     }
498     if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
499       sqlite3_str_appendf(&x, " IMMUTABLE");
500     }
501     sqlite3StrAccumFinish(&x);
502   }else{
503     zFlgs[0] = 0;
504   }
505   switch( pExpr->op ){
506     case TK_AGG_COLUMN: {
507       sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
508             pExpr->iTable, pExpr->iColumn, zFlgs);
509       break;
510     }
511     case TK_COLUMN: {
512       if( pExpr->iTable<0 ){
513         /* This only happens when coding check constraints */
514         char zOp2[16];
515         if( pExpr->op2 ){
516           sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
517         }else{
518           zOp2[0] = 0;
519         }
520         sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
521                                     pExpr->iColumn, zFlgs, zOp2);
522       }else{
523         assert( ExprUseYTab(pExpr) );
524         sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
525                         pExpr->iTable, pExpr->iColumn,
526                         pExpr->y.pTab, zFlgs);
527       }
528       if( ExprHasProperty(pExpr, EP_FixedCol) ){
529         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
530       }
531       break;
532     }
533     case TK_INTEGER: {
534       if( pExpr->flags & EP_IntValue ){
535         sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
536       }else{
537         sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
538       }
539       break;
540     }
541 #ifndef SQLITE_OMIT_FLOATING_POINT
542     case TK_FLOAT: {
543       assert( !ExprHasProperty(pExpr, EP_IntValue) );
544       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
545       break;
546     }
547 #endif
548     case TK_STRING: {
549       assert( !ExprHasProperty(pExpr, EP_IntValue) );
550       sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
551       break;
552     }
553     case TK_NULL: {
554       sqlite3TreeViewLine(pView,"NULL");
555       break;
556     }
557     case TK_TRUEFALSE: {
558       sqlite3TreeViewLine(pView,"%s%s",
559          sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs);
560       break;
561     }
562 #ifndef SQLITE_OMIT_BLOB_LITERAL
563     case TK_BLOB: {
564       assert( !ExprHasProperty(pExpr, EP_IntValue) );
565       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
566       break;
567     }
568 #endif
569     case TK_VARIABLE: {
570       assert( !ExprHasProperty(pExpr, EP_IntValue) );
571       sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
572                           pExpr->u.zToken, pExpr->iColumn);
573       break;
574     }
575     case TK_REGISTER: {
576       sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
577       break;
578     }
579     case TK_ID: {
580       assert( !ExprHasProperty(pExpr, EP_IntValue) );
581       sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
582       break;
583     }
584 #ifndef SQLITE_OMIT_CAST
585     case TK_CAST: {
586       /* Expressions of the form:   CAST(pLeft AS token) */
587       assert( !ExprHasProperty(pExpr, EP_IntValue) );
588       sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
589       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
590       break;
591     }
592 #endif /* SQLITE_OMIT_CAST */
593     case TK_LT:      zBinOp = "LT";     break;
594     case TK_LE:      zBinOp = "LE";     break;
595     case TK_GT:      zBinOp = "GT";     break;
596     case TK_GE:      zBinOp = "GE";     break;
597     case TK_NE:      zBinOp = "NE";     break;
598     case TK_EQ:      zBinOp = "EQ";     break;
599     case TK_IS:      zBinOp = "IS";     break;
600     case TK_ISNOT:   zBinOp = "ISNOT";  break;
601     case TK_AND:     zBinOp = "AND";    break;
602     case TK_OR:      zBinOp = "OR";     break;
603     case TK_PLUS:    zBinOp = "ADD";    break;
604     case TK_STAR:    zBinOp = "MUL";    break;
605     case TK_MINUS:   zBinOp = "SUB";    break;
606     case TK_REM:     zBinOp = "REM";    break;
607     case TK_BITAND:  zBinOp = "BITAND"; break;
608     case TK_BITOR:   zBinOp = "BITOR";  break;
609     case TK_SLASH:   zBinOp = "DIV";    break;
610     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
611     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
612     case TK_CONCAT:  zBinOp = "CONCAT"; break;
613     case TK_DOT:     zBinOp = "DOT";    break;
614     case TK_LIMIT:   zBinOp = "LIMIT";  break;
615 
616     case TK_UMINUS:  zUniOp = "UMINUS"; break;
617     case TK_UPLUS:   zUniOp = "UPLUS";  break;
618     case TK_BITNOT:  zUniOp = "BITNOT"; break;
619     case TK_NOT:     zUniOp = "NOT";    break;
620     case TK_ISNULL:  zUniOp = "ISNULL"; break;
621     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
622 
623     case TK_TRUTH: {
624       int x;
625       const char *azOp[] = {
626          "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
627       };
628       assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
629       assert( pExpr->pRight );
630       assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
631       x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
632       zUniOp = azOp[x];
633       break;
634     }
635 
636     case TK_SPAN: {
637       assert( !ExprHasProperty(pExpr, EP_IntValue) );
638       sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
639       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
640       break;
641     }
642 
643     case TK_COLLATE: {
644       /* COLLATE operators without the EP_Collate flag are intended to
645       ** emulate collation associated with a table column.  These show
646       ** up in the treeview output as "SOFT-COLLATE".  Explicit COLLATE
647       ** operators that appear in the original SQL always have the
648       ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
649       assert( !ExprHasProperty(pExpr, EP_IntValue) );
650       sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
651         !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
652         pExpr->u.zToken, zFlgs);
653       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
654       break;
655     }
656 
657     case TK_AGG_FUNCTION:
658     case TK_FUNCTION: {
659       ExprList *pFarg;       /* List of function arguments */
660       Window *pWin;
661       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
662         pFarg = 0;
663         pWin = 0;
664       }else{
665         assert( ExprUseXList(pExpr) );
666         pFarg = pExpr->x.pList;
667 #ifndef SQLITE_OMIT_WINDOWFUNC
668         pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
669 #else
670         pWin = 0;
671 #endif
672       }
673       assert( !ExprHasProperty(pExpr, EP_IntValue) );
674       if( pExpr->op==TK_AGG_FUNCTION ){
675         sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
676                              pExpr->op2, pExpr->u.zToken, zFlgs,
677                              pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
678                              pExpr->iAgg, pExpr->pAggInfo);
679       }else if( pExpr->op2!=0 ){
680         const char *zOp2;
681         char zBuf[8];
682         sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
683         zOp2 = zBuf;
684         if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
685         if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
686         if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
687         if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
688         sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
689                             pExpr->u.zToken, zFlgs, zOp2);
690       }else{
691         sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
692       }
693       if( pFarg ){
694         sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
695       }
696 #ifndef SQLITE_OMIT_WINDOWFUNC
697       if( pWin ){
698         sqlite3TreeViewWindow(pView, pWin, 0);
699       }
700 #endif
701       break;
702     }
703 #ifndef SQLITE_OMIT_SUBQUERY
704     case TK_EXISTS: {
705       assert( ExprUseXSelect(pExpr) );
706       sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
707       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
708       break;
709     }
710     case TK_SELECT: {
711       assert( ExprUseXSelect(pExpr) );
712       sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
713       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
714       break;
715     }
716     case TK_IN: {
717       sqlite3_str *pStr = sqlite3_str_new(0);
718       char *z;
719       sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
720       if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
721       if( ExprHasProperty(pExpr, EP_Subrtn) ){
722         sqlite3_str_appendf(pStr, " subrtn(%d,%d)",
723             pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
724       }
725       z = sqlite3_str_finish(pStr);
726       sqlite3TreeViewLine(pView, z);
727       sqlite3_free(z);
728       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
729       if( ExprUseXSelect(pExpr) ){
730         sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
731       }else{
732         sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
733       }
734       break;
735     }
736 #endif /* SQLITE_OMIT_SUBQUERY */
737 
738     /*
739     **    x BETWEEN y AND z
740     **
741     ** This is equivalent to
742     **
743     **    x>=y AND x<=z
744     **
745     ** X is stored in pExpr->pLeft.
746     ** Y is stored in pExpr->pList->a[0].pExpr.
747     ** Z is stored in pExpr->pList->a[1].pExpr.
748     */
749     case TK_BETWEEN: {
750       const Expr *pX, *pY, *pZ;
751       pX = pExpr->pLeft;
752       assert( ExprUseXList(pExpr) );
753       assert( pExpr->x.pList->nExpr==2 );
754       pY = pExpr->x.pList->a[0].pExpr;
755       pZ = pExpr->x.pList->a[1].pExpr;
756       sqlite3TreeViewLine(pView, "BETWEEN");
757       sqlite3TreeViewExpr(pView, pX, 1);
758       sqlite3TreeViewExpr(pView, pY, 1);
759       sqlite3TreeViewExpr(pView, pZ, 0);
760       break;
761     }
762     case TK_TRIGGER: {
763       /* If the opcode is TK_TRIGGER, then the expression is a reference
764       ** to a column in the new.* or old.* pseudo-tables available to
765       ** trigger programs. In this case Expr.iTable is set to 1 for the
766       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
767       ** is set to the column of the pseudo-table to read, or to -1 to
768       ** read the rowid field.
769       */
770       sqlite3TreeViewLine(pView, "%s(%d)",
771           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
772       break;
773     }
774     case TK_CASE: {
775       sqlite3TreeViewLine(pView, "CASE");
776       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
777       assert( ExprUseXList(pExpr) );
778       sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
779       break;
780     }
781 #ifndef SQLITE_OMIT_TRIGGER
782     case TK_RAISE: {
783       const char *zType = "unk";
784       switch( pExpr->affExpr ){
785         case OE_Rollback:   zType = "rollback";  break;
786         case OE_Abort:      zType = "abort";     break;
787         case OE_Fail:       zType = "fail";      break;
788         case OE_Ignore:     zType = "ignore";    break;
789       }
790       assert( !ExprHasProperty(pExpr, EP_IntValue) );
791       sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
792       break;
793     }
794 #endif
795     case TK_MATCH: {
796       sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
797                           pExpr->iTable, pExpr->iColumn, zFlgs);
798       sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
799       break;
800     }
801     case TK_VECTOR: {
802       char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
803       assert( ExprUseXList(pExpr) );
804       sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
805       sqlite3_free(z);
806       break;
807     }
808     case TK_SELECT_COLUMN: {
809       sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
810               pExpr->iColumn, pExpr->iTable-1,
811               pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
812       assert( ExprUseXSelect(pExpr->pLeft) );
813       sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
814       break;
815     }
816     case TK_IF_NULL_ROW: {
817       sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
818       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
819       break;
820     }
821     case TK_ERROR: {
822       Expr tmp;
823       sqlite3TreeViewLine(pView, "ERROR");
824       tmp = *pExpr;
825       tmp.op = pExpr->op2;
826       sqlite3TreeViewExpr(pView, &tmp, 0);
827       break;
828     }
829     case TK_ROW: {
830       if( pExpr->iColumn<=0 ){
831         sqlite3TreeViewLine(pView, "First FROM table rowid");
832       }else{
833         sqlite3TreeViewLine(pView, "First FROM table column %d",
834             pExpr->iColumn-1);
835       }
836       break;
837     }
838     default: {
839       sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
840       break;
841     }
842   }
843   if( zBinOp ){
844     sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
845     sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
846     sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
847   }else if( zUniOp ){
848     sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
849    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
850   }
851   sqlite3TreeViewPop(&pView);
852 }
853 
854 
855 /*
856 ** Generate a human-readable explanation of an expression list.
857 */
858 void sqlite3TreeViewBareExprList(
859   TreeView *pView,
860   const ExprList *pList,
861   const char *zLabel
862 ){
863   if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
864   if( pList==0 ){
865     sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
866   }else{
867     int i;
868     sqlite3TreeViewLine(pView, "%s", zLabel);
869     for(i=0; i<pList->nExpr; i++){
870       int j = pList->a[i].u.x.iOrderByCol;
871       char *zName = pList->a[i].zEName;
872       int moreToFollow = i<pList->nExpr - 1;
873       if( j || zName ){
874         sqlite3TreeViewPush(&pView, moreToFollow);
875         moreToFollow = 0;
876         sqlite3TreeViewLine(pView, 0);
877         if( zName ){
878           switch( pList->a[i].fg.eEName ){
879             default:
880               fprintf(stdout, "AS %s ", zName);
881               break;
882             case ENAME_TAB:
883               fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
884               if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) ");
885               if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) ");
886               if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) ");
887               break;
888             case ENAME_SPAN:
889               fprintf(stdout, "SPAN(\"%s\") ", zName);
890               break;
891           }
892         }
893         if( j ){
894           fprintf(stdout, "iOrderByCol=%d", j);
895         }
896         fprintf(stdout, "\n");
897         fflush(stdout);
898       }
899       sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
900       if( j || zName ){
901         sqlite3TreeViewPop(&pView);
902       }
903     }
904   }
905 }
906 void sqlite3TreeViewExprList(
907   TreeView *pView,
908   const ExprList *pList,
909   u8 moreToFollow,
910   const char *zLabel
911 ){
912   sqlite3TreeViewPush(&pView, moreToFollow);
913   sqlite3TreeViewBareExprList(pView, pList, zLabel);
914   sqlite3TreeViewPop(&pView);
915 }
916 
917 /*
918 ** Generate a human-readable explanation of an id-list.
919 */
920 void sqlite3TreeViewBareIdList(
921   TreeView *pView,
922   const IdList *pList,
923   const char *zLabel
924 ){
925   if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
926   if( pList==0 ){
927     sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
928   }else{
929     int i;
930     sqlite3TreeViewLine(pView, "%s", zLabel);
931     for(i=0; i<pList->nId; i++){
932       char *zName = pList->a[i].zName;
933       int moreToFollow = i<pList->nId - 1;
934       if( zName==0 ) zName = "(null)";
935       sqlite3TreeViewPush(&pView, moreToFollow);
936       sqlite3TreeViewLine(pView, 0);
937       if( pList->eU4==EU4_NONE ){
938         fprintf(stdout, "%s\n", zName);
939       }else if( pList->eU4==EU4_IDX ){
940         fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx);
941       }else{
942         assert( pList->eU4==EU4_EXPR );
943         if( pList->a[i].u4.pExpr==0 ){
944           fprintf(stdout, "%s (pExpr=NULL)\n", zName);
945         }else{
946           fprintf(stdout, "%s\n", zName);
947           sqlite3TreeViewPush(&pView, i<pList->nId-1);
948           sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0);
949           sqlite3TreeViewPop(&pView);
950         }
951       }
952       sqlite3TreeViewPop(&pView);
953     }
954   }
955 }
956 void sqlite3TreeViewIdList(
957   TreeView *pView,
958   const IdList *pList,
959   u8 moreToFollow,
960   const char *zLabel
961 ){
962   sqlite3TreeViewPush(&pView, moreToFollow);
963   sqlite3TreeViewBareIdList(pView, pList, zLabel);
964   sqlite3TreeViewPop(&pView);
965 }
966 
967 /*
968 ** Generate a human-readable explanation of a list of Upsert objects
969 */
970 void sqlite3TreeViewUpsert(
971   TreeView *pView,
972   const Upsert *pUpsert,
973   u8 moreToFollow
974 ){
975   if( pUpsert==0 ) return;
976   sqlite3TreeViewPush(&pView, moreToFollow);
977   while( pUpsert ){
978     int n;
979     sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
980     sqlite3TreeViewLine(pView, "ON CONFLICT DO %s",
981          pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
982     n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
983     sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
984     sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
985     if( pUpsert->pUpsertWhere ){
986       sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
987       sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
988       sqlite3TreeViewPop(&pView);
989     }
990     sqlite3TreeViewPop(&pView);
991     pUpsert = pUpsert->pNextUpsert;
992   }
993   sqlite3TreeViewPop(&pView);
994 }
995 
996 /*
997 ** Generate a human-readable diagram of the data structure that go
998 ** into generating an DELETE statement.
999 */
1000 void sqlite3TreeViewDelete(
1001   const With *pWith,
1002   const SrcList *pTabList,
1003   const Expr *pWhere,
1004   const ExprList *pOrderBy,
1005   const Expr *pLimit,
1006   const Trigger *pTrigger
1007 ){
1008   int n = 0;
1009   TreeView *pView = 0;
1010   sqlite3TreeViewPush(&pView, 0);
1011   sqlite3TreeViewLine(pView, "DELETE");
1012   if( pWith ) n++;
1013   if( pTabList ) n++;
1014   if( pWhere ) n++;
1015   if( pOrderBy ) n++;
1016   if( pLimit ) n++;
1017   if( pTrigger ) n++;
1018   if( pWith ){
1019     sqlite3TreeViewPush(&pView, (--n)>0);
1020     sqlite3TreeViewWith(pView, pWith, 0);
1021     sqlite3TreeViewPop(&pView);
1022   }
1023   if( pTabList ){
1024     sqlite3TreeViewPush(&pView, (--n)>0);
1025     sqlite3TreeViewLine(pView, "FROM");
1026     sqlite3TreeViewSrcList(pView, pTabList);
1027     sqlite3TreeViewPop(&pView);
1028   }
1029   if( pWhere ){
1030     sqlite3TreeViewPush(&pView, (--n)>0);
1031     sqlite3TreeViewLine(pView, "WHERE");
1032     sqlite3TreeViewExpr(pView, pWhere, 0);
1033     sqlite3TreeViewPop(&pView);
1034   }
1035   if( pOrderBy ){
1036     sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
1037   }
1038   if( pLimit ){
1039     sqlite3TreeViewPush(&pView, (--n)>0);
1040     sqlite3TreeViewLine(pView, "LIMIT");
1041     sqlite3TreeViewExpr(pView, pLimit, 0);
1042     sqlite3TreeViewPop(&pView);
1043   }
1044   if( pTrigger ){
1045     sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
1046   }
1047   sqlite3TreeViewPop(&pView);
1048 }
1049 
1050 /*
1051 ** Generate a human-readable diagram of the data structure that go
1052 ** into generating an INSERT statement.
1053 */
1054 void sqlite3TreeViewInsert(
1055   const With *pWith,
1056   const SrcList *pTabList,
1057   const IdList *pColumnList,
1058   const Select *pSelect,
1059   const ExprList *pExprList,
1060   int onError,
1061   const Upsert *pUpsert,
1062   const Trigger *pTrigger
1063 ){
1064   TreeView *pView = 0;
1065   int n = 0;
1066   const char *zLabel = "INSERT";
1067   switch( onError ){
1068     case OE_Replace:  zLabel = "REPLACE";             break;
1069     case OE_Ignore:   zLabel = "INSERT OR IGNORE";    break;
1070     case OE_Rollback: zLabel = "INSERT OR ROLLBACK";  break;
1071     case OE_Abort:    zLabel = "INSERT OR ABORT";     break;
1072     case OE_Fail:     zLabel = "INSERT OR FAIL";      break;
1073   }
1074   sqlite3TreeViewPush(&pView, 0);
1075   sqlite3TreeViewLine(pView, zLabel);
1076   if( pWith ) n++;
1077   if( pTabList ) n++;
1078   if( pColumnList ) n++;
1079   if( pSelect ) n++;
1080   if( pExprList ) n++;
1081   if( pUpsert ) n++;
1082   if( pTrigger ) n++;
1083   if( pWith ){
1084     sqlite3TreeViewPush(&pView, (--n)>0);
1085     sqlite3TreeViewWith(pView, pWith, 0);
1086     sqlite3TreeViewPop(&pView);
1087   }
1088   if( pTabList ){
1089     sqlite3TreeViewPush(&pView, (--n)>0);
1090     sqlite3TreeViewLine(pView, "INTO");
1091     sqlite3TreeViewSrcList(pView, pTabList);
1092     sqlite3TreeViewPop(&pView);
1093   }
1094   if( pColumnList ){
1095     sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
1096   }
1097   if( pSelect ){
1098     sqlite3TreeViewPush(&pView, (--n)>0);
1099     sqlite3TreeViewLine(pView, "DATA-SOURCE");
1100     sqlite3TreeViewSelect(pView, pSelect, 0);
1101     sqlite3TreeViewPop(&pView);
1102   }
1103   if( pExprList ){
1104     sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
1105   }
1106   if( pUpsert ){
1107     sqlite3TreeViewPush(&pView, (--n)>0);
1108     sqlite3TreeViewLine(pView, "UPSERT");
1109     sqlite3TreeViewUpsert(pView, pUpsert, 0);
1110     sqlite3TreeViewPop(&pView);
1111   }
1112   if( pTrigger ){
1113     sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
1114   }
1115   sqlite3TreeViewPop(&pView);
1116 }
1117 
1118 /*
1119 ** Generate a human-readable diagram of the data structure that go
1120 ** into generating an UPDATE statement.
1121 */
1122 void sqlite3TreeViewUpdate(
1123   const With *pWith,
1124   const SrcList *pTabList,
1125   const ExprList *pChanges,
1126   const Expr *pWhere,
1127   int onError,
1128   const ExprList *pOrderBy,
1129   const Expr *pLimit,
1130   const Upsert *pUpsert,
1131   const Trigger *pTrigger
1132 ){
1133   int n = 0;
1134   TreeView *pView = 0;
1135   const char *zLabel = "UPDATE";
1136   switch( onError ){
1137     case OE_Replace:  zLabel = "UPDATE OR REPLACE";   break;
1138     case OE_Ignore:   zLabel = "UPDATE OR IGNORE";    break;
1139     case OE_Rollback: zLabel = "UPDATE OR ROLLBACK";  break;
1140     case OE_Abort:    zLabel = "UPDATE OR ABORT";     break;
1141     case OE_Fail:     zLabel = "UPDATE OR FAIL";      break;
1142   }
1143   sqlite3TreeViewPush(&pView, 0);
1144   sqlite3TreeViewLine(pView, zLabel);
1145   if( pWith ) n++;
1146   if( pTabList ) n++;
1147   if( pChanges ) n++;
1148   if( pWhere ) n++;
1149   if( pOrderBy ) n++;
1150   if( pLimit ) n++;
1151   if( pUpsert ) n++;
1152   if( pTrigger ) n++;
1153   if( pWith ){
1154     sqlite3TreeViewPush(&pView, (--n)>0);
1155     sqlite3TreeViewWith(pView, pWith, 0);
1156     sqlite3TreeViewPop(&pView);
1157   }
1158   if( pTabList ){
1159     sqlite3TreeViewPush(&pView, (--n)>0);
1160     sqlite3TreeViewLine(pView, "FROM");
1161     sqlite3TreeViewSrcList(pView, pTabList);
1162     sqlite3TreeViewPop(&pView);
1163   }
1164   if( pChanges ){
1165     sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
1166   }
1167   if( pWhere ){
1168     sqlite3TreeViewPush(&pView, (--n)>0);
1169     sqlite3TreeViewLine(pView, "WHERE");
1170     sqlite3TreeViewExpr(pView, pWhere, 0);
1171     sqlite3TreeViewPop(&pView);
1172   }
1173   if( pOrderBy ){
1174     sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
1175   }
1176   if( pLimit ){
1177     sqlite3TreeViewPush(&pView, (--n)>0);
1178     sqlite3TreeViewLine(pView, "LIMIT");
1179     sqlite3TreeViewExpr(pView, pLimit, 0);
1180     sqlite3TreeViewPop(&pView);
1181   }
1182   if( pUpsert ){
1183     sqlite3TreeViewPush(&pView, (--n)>0);
1184     sqlite3TreeViewLine(pView, "UPSERT");
1185     sqlite3TreeViewUpsert(pView, pUpsert, 0);
1186     sqlite3TreeViewPop(&pView);
1187   }
1188   if( pTrigger ){
1189     sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
1190   }
1191   sqlite3TreeViewPop(&pView);
1192 }
1193 
1194 #ifndef SQLITE_OMIT_TRIGGER
1195 /*
1196 ** Show a human-readable graph of a TriggerStep
1197 */
1198 void sqlite3TreeViewTriggerStep(
1199   TreeView *pView,
1200   const TriggerStep *pStep,
1201   u8 moreToFollow,
1202   u8 showFullList
1203 ){
1204   int cnt = 0;
1205   if( pStep==0 ) return;
1206   sqlite3TreeViewPush(&pView,
1207       moreToFollow || (showFullList && pStep->pNext!=0));
1208   do{
1209     if( cnt++ && pStep->pNext==0 ){
1210       sqlite3TreeViewPop(&pView);
1211       sqlite3TreeViewPush(&pView, 0);
1212     }
1213     sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
1214   }while( showFullList && (pStep = pStep->pNext)!=0 );
1215   sqlite3TreeViewPop(&pView);
1216 }
1217 
1218 /*
1219 ** Show a human-readable graph of a Trigger
1220 */
1221 void sqlite3TreeViewTrigger(
1222   TreeView *pView,
1223   const Trigger *pTrigger,
1224   u8 moreToFollow,
1225   u8 showFullList
1226 ){
1227   int cnt = 0;
1228   if( pTrigger==0 ) return;
1229   sqlite3TreeViewPush(&pView,
1230      moreToFollow || (showFullList && pTrigger->pNext!=0));
1231   do{
1232     if( cnt++ && pTrigger->pNext==0 ){
1233       sqlite3TreeViewPop(&pView);
1234       sqlite3TreeViewPush(&pView, 0);
1235     }
1236     sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
1237     sqlite3TreeViewPush(&pView, 0);
1238     sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
1239     sqlite3TreeViewPop(&pView);
1240   }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
1241   sqlite3TreeViewPop(&pView);
1242 }
1243 #endif /* SQLITE_OMIT_TRIGGER */
1244 
1245 
1246 /*
1247 ** These simplified versions of the tree-view routines omit unnecessary
1248 ** parameters.  These variants are intended to be used from a symbolic
1249 ** debugger, such as "gdb", during interactive debugging sessions.
1250 **
1251 ** This routines are given external linkage so that they will always be
1252 ** accessible to the debugging, and to avoid warnings about unused
1253 ** functions.  But these routines only exist in debugging builds, so they
1254 ** do not contaminate the interface.
1255 */
1256 void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); }
1257 void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);}
1258 void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); }
1259 void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); }
1260 void sqlite3ShowSelect(const Select *p){ sqlite3TreeViewSelect(0,p,0); }
1261 void sqlite3ShowWith(const With *p){ sqlite3TreeViewWith(0,p,0); }
1262 void sqlite3ShowUpsert(const Upsert *p){ sqlite3TreeViewUpsert(0,p,0); }
1263 #ifndef SQLITE_OMIT_TRIGGER
1264 void sqlite3ShowTriggerStep(const TriggerStep *p){
1265   sqlite3TreeViewTriggerStep(0,p,0,0);
1266 }
1267 void sqlite3ShowTriggerStepList(const TriggerStep *p){
1268   sqlite3TreeViewTriggerStep(0,p,0,1);
1269 }
1270 void sqlite3ShowTrigger(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,0); }
1271 void sqlite3ShowTriggerList(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,1);}
1272 #endif
1273 #ifndef SQLITE_OMIT_WINDOWFUNC
1274 void sqlite3ShowWindow(const Window *p){ sqlite3TreeViewWindow(0,p,0); }
1275 void sqlite3ShowWinFunc(const Window *p){ sqlite3TreeViewWinFunc(0,p,0); }
1276 #endif
1277 
1278 #endif /* SQLITE_DEBUG */
1279