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