xref: /sqlite-3.40.0/src/treeview.c (revision 181d75ef)
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_OuterON) ){
493       sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin);
494     }
495     if( ExprHasProperty(pExpr, EP_InnerON) ){
496       sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin);
497     }
498     if( ExprHasProperty(pExpr, EP_FromDDL) ){
499       sqlite3_str_appendf(&x, " DDL");
500     }
501     if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
502       sqlite3_str_appendf(&x, " IMMUTABLE");
503     }
504     sqlite3StrAccumFinish(&x);
505   }else{
506     zFlgs[0] = 0;
507   }
508   switch( pExpr->op ){
509     case TK_AGG_COLUMN: {
510       sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
511             pExpr->iTable, pExpr->iColumn, zFlgs);
512       break;
513     }
514     case TK_COLUMN: {
515       if( pExpr->iTable<0 ){
516         /* This only happens when coding check constraints */
517         char zOp2[16];
518         if( pExpr->op2 ){
519           sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
520         }else{
521           zOp2[0] = 0;
522         }
523         sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
524                                     pExpr->iColumn, zFlgs, zOp2);
525       }else{
526         assert( ExprUseYTab(pExpr) );
527         sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
528                         pExpr->iTable, pExpr->iColumn,
529                         pExpr->y.pTab, zFlgs);
530       }
531       if( ExprHasProperty(pExpr, EP_FixedCol) ){
532         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
533       }
534       break;
535     }
536     case TK_INTEGER: {
537       if( pExpr->flags & EP_IntValue ){
538         sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
539       }else{
540         sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
541       }
542       break;
543     }
544 #ifndef SQLITE_OMIT_FLOATING_POINT
545     case TK_FLOAT: {
546       assert( !ExprHasProperty(pExpr, EP_IntValue) );
547       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
548       break;
549     }
550 #endif
551     case TK_STRING: {
552       assert( !ExprHasProperty(pExpr, EP_IntValue) );
553       sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
554       break;
555     }
556     case TK_NULL: {
557       sqlite3TreeViewLine(pView,"NULL");
558       break;
559     }
560     case TK_TRUEFALSE: {
561       sqlite3TreeViewLine(pView,"%s%s",
562          sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs);
563       break;
564     }
565 #ifndef SQLITE_OMIT_BLOB_LITERAL
566     case TK_BLOB: {
567       assert( !ExprHasProperty(pExpr, EP_IntValue) );
568       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
569       break;
570     }
571 #endif
572     case TK_VARIABLE: {
573       assert( !ExprHasProperty(pExpr, EP_IntValue) );
574       sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
575                           pExpr->u.zToken, pExpr->iColumn);
576       break;
577     }
578     case TK_REGISTER: {
579       sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
580       break;
581     }
582     case TK_ID: {
583       assert( !ExprHasProperty(pExpr, EP_IntValue) );
584       sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
585       break;
586     }
587 #ifndef SQLITE_OMIT_CAST
588     case TK_CAST: {
589       /* Expressions of the form:   CAST(pLeft AS token) */
590       assert( !ExprHasProperty(pExpr, EP_IntValue) );
591       sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
592       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
593       break;
594     }
595 #endif /* SQLITE_OMIT_CAST */
596     case TK_LT:      zBinOp = "LT";     break;
597     case TK_LE:      zBinOp = "LE";     break;
598     case TK_GT:      zBinOp = "GT";     break;
599     case TK_GE:      zBinOp = "GE";     break;
600     case TK_NE:      zBinOp = "NE";     break;
601     case TK_EQ:      zBinOp = "EQ";     break;
602     case TK_IS:      zBinOp = "IS";     break;
603     case TK_ISNOT:   zBinOp = "ISNOT";  break;
604     case TK_AND:     zBinOp = "AND";    break;
605     case TK_OR:      zBinOp = "OR";     break;
606     case TK_PLUS:    zBinOp = "ADD";    break;
607     case TK_STAR:    zBinOp = "MUL";    break;
608     case TK_MINUS:   zBinOp = "SUB";    break;
609     case TK_REM:     zBinOp = "REM";    break;
610     case TK_BITAND:  zBinOp = "BITAND"; break;
611     case TK_BITOR:   zBinOp = "BITOR";  break;
612     case TK_SLASH:   zBinOp = "DIV";    break;
613     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
614     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
615     case TK_CONCAT:  zBinOp = "CONCAT"; break;
616     case TK_DOT:     zBinOp = "DOT";    break;
617     case TK_LIMIT:   zBinOp = "LIMIT";  break;
618 
619     case TK_UMINUS:  zUniOp = "UMINUS"; break;
620     case TK_UPLUS:   zUniOp = "UPLUS";  break;
621     case TK_BITNOT:  zUniOp = "BITNOT"; break;
622     case TK_NOT:     zUniOp = "NOT";    break;
623     case TK_ISNULL:  zUniOp = "ISNULL"; break;
624     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
625 
626     case TK_TRUTH: {
627       int x;
628       const char *azOp[] = {
629          "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
630       };
631       assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
632       assert( pExpr->pRight );
633       assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
634       x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
635       zUniOp = azOp[x];
636       break;
637     }
638 
639     case TK_SPAN: {
640       assert( !ExprHasProperty(pExpr, EP_IntValue) );
641       sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
642       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
643       break;
644     }
645 
646     case TK_COLLATE: {
647       /* COLLATE operators without the EP_Collate flag are intended to
648       ** emulate collation associated with a table column.  These show
649       ** up in the treeview output as "SOFT-COLLATE".  Explicit COLLATE
650       ** operators that appear in the original SQL always have the
651       ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
652       assert( !ExprHasProperty(pExpr, EP_IntValue) );
653       sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
654         !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
655         pExpr->u.zToken, zFlgs);
656       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
657       break;
658     }
659 
660     case TK_AGG_FUNCTION:
661     case TK_FUNCTION: {
662       ExprList *pFarg;       /* List of function arguments */
663       Window *pWin;
664       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
665         pFarg = 0;
666         pWin = 0;
667       }else{
668         assert( ExprUseXList(pExpr) );
669         pFarg = pExpr->x.pList;
670 #ifndef SQLITE_OMIT_WINDOWFUNC
671         pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
672 #else
673         pWin = 0;
674 #endif
675       }
676       assert( !ExprHasProperty(pExpr, EP_IntValue) );
677       if( pExpr->op==TK_AGG_FUNCTION ){
678         sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
679                              pExpr->op2, pExpr->u.zToken, zFlgs,
680                              pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
681                              pExpr->iAgg, pExpr->pAggInfo);
682       }else if( pExpr->op2!=0 ){
683         const char *zOp2;
684         char zBuf[8];
685         sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
686         zOp2 = zBuf;
687         if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
688         if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
689         if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
690         if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
691         sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
692                             pExpr->u.zToken, zFlgs, zOp2);
693       }else{
694         sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
695       }
696       if( pFarg ){
697         sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
698       }
699 #ifndef SQLITE_OMIT_WINDOWFUNC
700       if( pWin ){
701         sqlite3TreeViewWindow(pView, pWin, 0);
702       }
703 #endif
704       break;
705     }
706 #ifndef SQLITE_OMIT_SUBQUERY
707     case TK_EXISTS: {
708       assert( ExprUseXSelect(pExpr) );
709       sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
710       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
711       break;
712     }
713     case TK_SELECT: {
714       assert( ExprUseXSelect(pExpr) );
715       sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
716       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
717       break;
718     }
719     case TK_IN: {
720       sqlite3_str *pStr = sqlite3_str_new(0);
721       char *z;
722       sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
723       if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
724       if( ExprHasProperty(pExpr, EP_Subrtn) ){
725         sqlite3_str_appendf(pStr, " subrtn(%d,%d)",
726             pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
727       }
728       z = sqlite3_str_finish(pStr);
729       sqlite3TreeViewLine(pView, z);
730       sqlite3_free(z);
731       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
732       if( ExprUseXSelect(pExpr) ){
733         sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
734       }else{
735         sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
736       }
737       break;
738     }
739 #endif /* SQLITE_OMIT_SUBQUERY */
740 
741     /*
742     **    x BETWEEN y AND z
743     **
744     ** This is equivalent to
745     **
746     **    x>=y AND x<=z
747     **
748     ** X is stored in pExpr->pLeft.
749     ** Y is stored in pExpr->pList->a[0].pExpr.
750     ** Z is stored in pExpr->pList->a[1].pExpr.
751     */
752     case TK_BETWEEN: {
753       const Expr *pX, *pY, *pZ;
754       pX = pExpr->pLeft;
755       assert( ExprUseXList(pExpr) );
756       assert( pExpr->x.pList->nExpr==2 );
757       pY = pExpr->x.pList->a[0].pExpr;
758       pZ = pExpr->x.pList->a[1].pExpr;
759       sqlite3TreeViewLine(pView, "BETWEEN");
760       sqlite3TreeViewExpr(pView, pX, 1);
761       sqlite3TreeViewExpr(pView, pY, 1);
762       sqlite3TreeViewExpr(pView, pZ, 0);
763       break;
764     }
765     case TK_TRIGGER: {
766       /* If the opcode is TK_TRIGGER, then the expression is a reference
767       ** to a column in the new.* or old.* pseudo-tables available to
768       ** trigger programs. In this case Expr.iTable is set to 1 for the
769       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
770       ** is set to the column of the pseudo-table to read, or to -1 to
771       ** read the rowid field.
772       */
773       sqlite3TreeViewLine(pView, "%s(%d)",
774           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
775       break;
776     }
777     case TK_CASE: {
778       sqlite3TreeViewLine(pView, "CASE");
779       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
780       assert( ExprUseXList(pExpr) );
781       sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
782       break;
783     }
784 #ifndef SQLITE_OMIT_TRIGGER
785     case TK_RAISE: {
786       const char *zType = "unk";
787       switch( pExpr->affExpr ){
788         case OE_Rollback:   zType = "rollback";  break;
789         case OE_Abort:      zType = "abort";     break;
790         case OE_Fail:       zType = "fail";      break;
791         case OE_Ignore:     zType = "ignore";    break;
792       }
793       assert( !ExprHasProperty(pExpr, EP_IntValue) );
794       sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
795       break;
796     }
797 #endif
798     case TK_MATCH: {
799       sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
800                           pExpr->iTable, pExpr->iColumn, zFlgs);
801       sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
802       break;
803     }
804     case TK_VECTOR: {
805       char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
806       assert( ExprUseXList(pExpr) );
807       sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
808       sqlite3_free(z);
809       break;
810     }
811     case TK_SELECT_COLUMN: {
812       sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
813               pExpr->iColumn, pExpr->iTable-1,
814               pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
815       assert( ExprUseXSelect(pExpr->pLeft) );
816       sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
817       break;
818     }
819     case TK_IF_NULL_ROW: {
820       sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
821       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
822       break;
823     }
824     case TK_ERROR: {
825       Expr tmp;
826       sqlite3TreeViewLine(pView, "ERROR");
827       tmp = *pExpr;
828       tmp.op = pExpr->op2;
829       sqlite3TreeViewExpr(pView, &tmp, 0);
830       break;
831     }
832     case TK_ROW: {
833       if( pExpr->iColumn<=0 ){
834         sqlite3TreeViewLine(pView, "First FROM table rowid");
835       }else{
836         sqlite3TreeViewLine(pView, "First FROM table column %d",
837             pExpr->iColumn-1);
838       }
839       break;
840     }
841     default: {
842       sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
843       break;
844     }
845   }
846   if( zBinOp ){
847     sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
848     sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
849     sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
850   }else if( zUniOp ){
851     sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
852    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
853   }
854   sqlite3TreeViewPop(&pView);
855 }
856 
857 
858 /*
859 ** Generate a human-readable explanation of an expression list.
860 */
861 void sqlite3TreeViewBareExprList(
862   TreeView *pView,
863   const ExprList *pList,
864   const char *zLabel
865 ){
866   if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
867   if( pList==0 ){
868     sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
869   }else{
870     int i;
871     sqlite3TreeViewLine(pView, "%s", zLabel);
872     for(i=0; i<pList->nExpr; i++){
873       int j = pList->a[i].u.x.iOrderByCol;
874       char *zName = pList->a[i].zEName;
875       int moreToFollow = i<pList->nExpr - 1;
876       if( j || zName ){
877         sqlite3TreeViewPush(&pView, moreToFollow);
878         moreToFollow = 0;
879         sqlite3TreeViewLine(pView, 0);
880         if( zName ){
881           switch( pList->a[i].fg.eEName ){
882             default:
883               fprintf(stdout, "AS %s ", zName);
884               break;
885             case ENAME_TAB:
886               fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
887               if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) ");
888               if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) ");
889               if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) ");
890               break;
891             case ENAME_SPAN:
892               fprintf(stdout, "SPAN(\"%s\") ", zName);
893               break;
894           }
895         }
896         if( j ){
897           fprintf(stdout, "iOrderByCol=%d", j);
898         }
899         fprintf(stdout, "\n");
900         fflush(stdout);
901       }
902       sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
903       if( j || zName ){
904         sqlite3TreeViewPop(&pView);
905       }
906     }
907   }
908 }
909 void sqlite3TreeViewExprList(
910   TreeView *pView,
911   const ExprList *pList,
912   u8 moreToFollow,
913   const char *zLabel
914 ){
915   sqlite3TreeViewPush(&pView, moreToFollow);
916   sqlite3TreeViewBareExprList(pView, pList, zLabel);
917   sqlite3TreeViewPop(&pView);
918 }
919 
920 /*
921 ** Generate a human-readable explanation of an id-list.
922 */
923 void sqlite3TreeViewBareIdList(
924   TreeView *pView,
925   const IdList *pList,
926   const char *zLabel
927 ){
928   if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
929   if( pList==0 ){
930     sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
931   }else{
932     int i;
933     sqlite3TreeViewLine(pView, "%s", zLabel);
934     for(i=0; i<pList->nId; i++){
935       char *zName = pList->a[i].zName;
936       int moreToFollow = i<pList->nId - 1;
937       if( zName==0 ) zName = "(null)";
938       sqlite3TreeViewPush(&pView, moreToFollow);
939       sqlite3TreeViewLine(pView, 0);
940       if( pList->eU4==EU4_NONE ){
941         fprintf(stdout, "%s\n", zName);
942       }else if( pList->eU4==EU4_IDX ){
943         fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx);
944       }else{
945         assert( pList->eU4==EU4_EXPR );
946         if( pList->a[i].u4.pExpr==0 ){
947           fprintf(stdout, "%s (pExpr=NULL)\n", zName);
948         }else{
949           fprintf(stdout, "%s\n", zName);
950           sqlite3TreeViewPush(&pView, i<pList->nId-1);
951           sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0);
952           sqlite3TreeViewPop(&pView);
953         }
954       }
955       sqlite3TreeViewPop(&pView);
956     }
957   }
958 }
959 void sqlite3TreeViewIdList(
960   TreeView *pView,
961   const IdList *pList,
962   u8 moreToFollow,
963   const char *zLabel
964 ){
965   sqlite3TreeViewPush(&pView, moreToFollow);
966   sqlite3TreeViewBareIdList(pView, pList, zLabel);
967   sqlite3TreeViewPop(&pView);
968 }
969 
970 /*
971 ** Generate a human-readable explanation of a list of Upsert objects
972 */
973 void sqlite3TreeViewUpsert(
974   TreeView *pView,
975   const Upsert *pUpsert,
976   u8 moreToFollow
977 ){
978   if( pUpsert==0 ) return;
979   sqlite3TreeViewPush(&pView, moreToFollow);
980   while( pUpsert ){
981     int n;
982     sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
983     sqlite3TreeViewLine(pView, "ON CONFLICT DO %s",
984          pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
985     n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
986     sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
987     sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
988     if( pUpsert->pUpsertWhere ){
989       sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
990       sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
991       sqlite3TreeViewPop(&pView);
992     }
993     sqlite3TreeViewPop(&pView);
994     pUpsert = pUpsert->pNextUpsert;
995   }
996   sqlite3TreeViewPop(&pView);
997 }
998 
999 /*
1000 ** Generate a human-readable diagram of the data structure that go
1001 ** into generating an DELETE statement.
1002 */
1003 void sqlite3TreeViewDelete(
1004   const With *pWith,
1005   const SrcList *pTabList,
1006   const Expr *pWhere,
1007   const ExprList *pOrderBy,
1008   const Expr *pLimit,
1009   const Trigger *pTrigger
1010 ){
1011   int n = 0;
1012   TreeView *pView = 0;
1013   sqlite3TreeViewPush(&pView, 0);
1014   sqlite3TreeViewLine(pView, "DELETE");
1015   if( pWith ) n++;
1016   if( pTabList ) n++;
1017   if( pWhere ) n++;
1018   if( pOrderBy ) n++;
1019   if( pLimit ) n++;
1020   if( pTrigger ) n++;
1021   if( pWith ){
1022     sqlite3TreeViewPush(&pView, (--n)>0);
1023     sqlite3TreeViewWith(pView, pWith, 0);
1024     sqlite3TreeViewPop(&pView);
1025   }
1026   if( pTabList ){
1027     sqlite3TreeViewPush(&pView, (--n)>0);
1028     sqlite3TreeViewLine(pView, "FROM");
1029     sqlite3TreeViewSrcList(pView, pTabList);
1030     sqlite3TreeViewPop(&pView);
1031   }
1032   if( pWhere ){
1033     sqlite3TreeViewPush(&pView, (--n)>0);
1034     sqlite3TreeViewLine(pView, "WHERE");
1035     sqlite3TreeViewExpr(pView, pWhere, 0);
1036     sqlite3TreeViewPop(&pView);
1037   }
1038   if( pOrderBy ){
1039     sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
1040   }
1041   if( pLimit ){
1042     sqlite3TreeViewPush(&pView, (--n)>0);
1043     sqlite3TreeViewLine(pView, "LIMIT");
1044     sqlite3TreeViewExpr(pView, pLimit, 0);
1045     sqlite3TreeViewPop(&pView);
1046   }
1047   if( pTrigger ){
1048     sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
1049   }
1050   sqlite3TreeViewPop(&pView);
1051 }
1052 
1053 /*
1054 ** Generate a human-readable diagram of the data structure that go
1055 ** into generating an INSERT statement.
1056 */
1057 void sqlite3TreeViewInsert(
1058   const With *pWith,
1059   const SrcList *pTabList,
1060   const IdList *pColumnList,
1061   const Select *pSelect,
1062   const ExprList *pExprList,
1063   int onError,
1064   const Upsert *pUpsert,
1065   const Trigger *pTrigger
1066 ){
1067   TreeView *pView = 0;
1068   int n = 0;
1069   const char *zLabel = "INSERT";
1070   switch( onError ){
1071     case OE_Replace:  zLabel = "REPLACE";             break;
1072     case OE_Ignore:   zLabel = "INSERT OR IGNORE";    break;
1073     case OE_Rollback: zLabel = "INSERT OR ROLLBACK";  break;
1074     case OE_Abort:    zLabel = "INSERT OR ABORT";     break;
1075     case OE_Fail:     zLabel = "INSERT OR FAIL";      break;
1076   }
1077   sqlite3TreeViewPush(&pView, 0);
1078   sqlite3TreeViewLine(pView, zLabel);
1079   if( pWith ) n++;
1080   if( pTabList ) n++;
1081   if( pColumnList ) n++;
1082   if( pSelect ) n++;
1083   if( pExprList ) n++;
1084   if( pUpsert ) n++;
1085   if( pTrigger ) n++;
1086   if( pWith ){
1087     sqlite3TreeViewPush(&pView, (--n)>0);
1088     sqlite3TreeViewWith(pView, pWith, 0);
1089     sqlite3TreeViewPop(&pView);
1090   }
1091   if( pTabList ){
1092     sqlite3TreeViewPush(&pView, (--n)>0);
1093     sqlite3TreeViewLine(pView, "INTO");
1094     sqlite3TreeViewSrcList(pView, pTabList);
1095     sqlite3TreeViewPop(&pView);
1096   }
1097   if( pColumnList ){
1098     sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
1099   }
1100   if( pSelect ){
1101     sqlite3TreeViewPush(&pView, (--n)>0);
1102     sqlite3TreeViewLine(pView, "DATA-SOURCE");
1103     sqlite3TreeViewSelect(pView, pSelect, 0);
1104     sqlite3TreeViewPop(&pView);
1105   }
1106   if( pExprList ){
1107     sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
1108   }
1109   if( pUpsert ){
1110     sqlite3TreeViewPush(&pView, (--n)>0);
1111     sqlite3TreeViewLine(pView, "UPSERT");
1112     sqlite3TreeViewUpsert(pView, pUpsert, 0);
1113     sqlite3TreeViewPop(&pView);
1114   }
1115   if( pTrigger ){
1116     sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
1117   }
1118   sqlite3TreeViewPop(&pView);
1119 }
1120 
1121 /*
1122 ** Generate a human-readable diagram of the data structure that go
1123 ** into generating an UPDATE statement.
1124 */
1125 void sqlite3TreeViewUpdate(
1126   const With *pWith,
1127   const SrcList *pTabList,
1128   const ExprList *pChanges,
1129   const Expr *pWhere,
1130   int onError,
1131   const ExprList *pOrderBy,
1132   const Expr *pLimit,
1133   const Upsert *pUpsert,
1134   const Trigger *pTrigger
1135 ){
1136   int n = 0;
1137   TreeView *pView = 0;
1138   const char *zLabel = "UPDATE";
1139   switch( onError ){
1140     case OE_Replace:  zLabel = "UPDATE OR REPLACE";   break;
1141     case OE_Ignore:   zLabel = "UPDATE OR IGNORE";    break;
1142     case OE_Rollback: zLabel = "UPDATE OR ROLLBACK";  break;
1143     case OE_Abort:    zLabel = "UPDATE OR ABORT";     break;
1144     case OE_Fail:     zLabel = "UPDATE OR FAIL";      break;
1145   }
1146   sqlite3TreeViewPush(&pView, 0);
1147   sqlite3TreeViewLine(pView, zLabel);
1148   if( pWith ) n++;
1149   if( pTabList ) n++;
1150   if( pChanges ) n++;
1151   if( pWhere ) n++;
1152   if( pOrderBy ) n++;
1153   if( pLimit ) n++;
1154   if( pUpsert ) n++;
1155   if( pTrigger ) n++;
1156   if( pWith ){
1157     sqlite3TreeViewPush(&pView, (--n)>0);
1158     sqlite3TreeViewWith(pView, pWith, 0);
1159     sqlite3TreeViewPop(&pView);
1160   }
1161   if( pTabList ){
1162     sqlite3TreeViewPush(&pView, (--n)>0);
1163     sqlite3TreeViewLine(pView, "FROM");
1164     sqlite3TreeViewSrcList(pView, pTabList);
1165     sqlite3TreeViewPop(&pView);
1166   }
1167   if( pChanges ){
1168     sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
1169   }
1170   if( pWhere ){
1171     sqlite3TreeViewPush(&pView, (--n)>0);
1172     sqlite3TreeViewLine(pView, "WHERE");
1173     sqlite3TreeViewExpr(pView, pWhere, 0);
1174     sqlite3TreeViewPop(&pView);
1175   }
1176   if( pOrderBy ){
1177     sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
1178   }
1179   if( pLimit ){
1180     sqlite3TreeViewPush(&pView, (--n)>0);
1181     sqlite3TreeViewLine(pView, "LIMIT");
1182     sqlite3TreeViewExpr(pView, pLimit, 0);
1183     sqlite3TreeViewPop(&pView);
1184   }
1185   if( pUpsert ){
1186     sqlite3TreeViewPush(&pView, (--n)>0);
1187     sqlite3TreeViewLine(pView, "UPSERT");
1188     sqlite3TreeViewUpsert(pView, pUpsert, 0);
1189     sqlite3TreeViewPop(&pView);
1190   }
1191   if( pTrigger ){
1192     sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
1193   }
1194   sqlite3TreeViewPop(&pView);
1195 }
1196 
1197 #ifndef SQLITE_OMIT_TRIGGER
1198 /*
1199 ** Show a human-readable graph of a TriggerStep
1200 */
1201 void sqlite3TreeViewTriggerStep(
1202   TreeView *pView,
1203   const TriggerStep *pStep,
1204   u8 moreToFollow,
1205   u8 showFullList
1206 ){
1207   int cnt = 0;
1208   if( pStep==0 ) return;
1209   sqlite3TreeViewPush(&pView,
1210       moreToFollow || (showFullList && pStep->pNext!=0));
1211   do{
1212     if( cnt++ && pStep->pNext==0 ){
1213       sqlite3TreeViewPop(&pView);
1214       sqlite3TreeViewPush(&pView, 0);
1215     }
1216     sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
1217   }while( showFullList && (pStep = pStep->pNext)!=0 );
1218   sqlite3TreeViewPop(&pView);
1219 }
1220 
1221 /*
1222 ** Show a human-readable graph of a Trigger
1223 */
1224 void sqlite3TreeViewTrigger(
1225   TreeView *pView,
1226   const Trigger *pTrigger,
1227   u8 moreToFollow,
1228   u8 showFullList
1229 ){
1230   int cnt = 0;
1231   if( pTrigger==0 ) return;
1232   sqlite3TreeViewPush(&pView,
1233      moreToFollow || (showFullList && pTrigger->pNext!=0));
1234   do{
1235     if( cnt++ && pTrigger->pNext==0 ){
1236       sqlite3TreeViewPop(&pView);
1237       sqlite3TreeViewPush(&pView, 0);
1238     }
1239     sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
1240     sqlite3TreeViewPush(&pView, 0);
1241     sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
1242     sqlite3TreeViewPop(&pView);
1243   }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
1244   sqlite3TreeViewPop(&pView);
1245 }
1246 #endif /* SQLITE_OMIT_TRIGGER */
1247 
1248 
1249 /*
1250 ** These simplified versions of the tree-view routines omit unnecessary
1251 ** parameters.  These variants are intended to be used from a symbolic
1252 ** debugger, such as "gdb", during interactive debugging sessions.
1253 **
1254 ** This routines are given external linkage so that they will always be
1255 ** accessible to the debugging, and to avoid warnings about unused
1256 ** functions.  But these routines only exist in debugging builds, so they
1257 ** do not contaminate the interface.
1258 */
1259 void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); }
1260 void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);}
1261 void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); }
1262 void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); }
1263 void sqlite3ShowSelect(const Select *p){ sqlite3TreeViewSelect(0,p,0); }
1264 void sqlite3ShowWith(const With *p){ sqlite3TreeViewWith(0,p,0); }
1265 void sqlite3ShowUpsert(const Upsert *p){ sqlite3TreeViewUpsert(0,p,0); }
1266 #ifndef SQLITE_OMIT_TRIGGER
1267 void sqlite3ShowTriggerStep(const TriggerStep *p){
1268   sqlite3TreeViewTriggerStep(0,p,0,0);
1269 }
1270 void sqlite3ShowTriggerStepList(const TriggerStep *p){
1271   sqlite3TreeViewTriggerStep(0,p,0,1);
1272 }
1273 void sqlite3ShowTrigger(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,0); }
1274 void sqlite3ShowTriggerList(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,1);}
1275 #endif
1276 #ifndef SQLITE_OMIT_WINDOWFUNC
1277 void sqlite3ShowWindow(const Window *p){ sqlite3TreeViewWindow(0,p,0); }
1278 void sqlite3ShowWinFunc(const Window *p){ sqlite3TreeViewWinFunc(0,p,0); }
1279 #endif
1280 
1281 #endif /* SQLITE_DEBUG */
1282