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