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 TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ 28 if( p==0 ){ 29 p = sqlite3_malloc64( sizeof(*p) ); 30 if( p==0 ) return 0; 31 memset(p, 0, sizeof(*p)); 32 }else{ 33 p->iLevel++; 34 } 35 assert( moreToFollow==0 || moreToFollow==1 ); 36 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; 37 return p; 38 } 39 40 /* 41 ** Finished with one layer of the tree 42 */ 43 static void sqlite3TreeViewPop(TreeView *p){ 44 if( p==0 ) return; 45 p->iLevel--; 46 if( p->iLevel<0 ) sqlite3_free(p); 47 } 48 49 /* 50 ** Generate a single line of output for the tree, with a prefix that contains 51 ** all the appropriate tree lines 52 */ 53 static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ 54 va_list ap; 55 int i; 56 StrAccum acc; 57 char zBuf[500]; 58 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 59 if( p ){ 60 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){ 61 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); 62 } 63 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); 64 } 65 va_start(ap, zFormat); 66 sqlite3VXPrintf(&acc, zFormat, ap); 67 va_end(ap); 68 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); 69 sqlite3StrAccumFinish(&acc); 70 fprintf(stdout,"%s", zBuf); 71 fflush(stdout); 72 } 73 74 /* 75 ** Shorthand for starting a new tree item that consists of a single label 76 */ 77 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){ 78 p = sqlite3TreeViewPush(p, moreFollows); 79 sqlite3TreeViewLine(p, "%s", zLabel); 80 } 81 82 /* 83 ** Generate a human-readable description of a WITH clause. 84 */ 85 void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){ 86 int i; 87 if( pWith==0 ) return; 88 if( pWith->nCte==0 ) return; 89 if( pWith->pOuter ){ 90 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); 91 }else{ 92 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith); 93 } 94 if( pWith->nCte>0 ){ 95 pView = sqlite3TreeViewPush(pView, 1); 96 for(i=0; i<pWith->nCte; i++){ 97 StrAccum x; 98 char zLine[1000]; 99 const struct Cte *pCte = &pWith->a[i]; 100 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); 101 sqlite3XPrintf(&x, "%s", pCte->zName); 102 if( pCte->pCols && pCte->pCols->nExpr>0 ){ 103 char cSep = '('; 104 int j; 105 for(j=0; j<pCte->pCols->nExpr; j++){ 106 sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName); 107 cSep = ','; 108 } 109 sqlite3XPrintf(&x, ")"); 110 } 111 sqlite3XPrintf(&x, " AS"); 112 sqlite3StrAccumFinish(&x); 113 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); 114 sqlite3TreeViewSelect(pView, pCte->pSelect, 0); 115 sqlite3TreeViewPop(pView); 116 } 117 sqlite3TreeViewPop(pView); 118 } 119 } 120 121 122 /* 123 ** Generate a human-readable description of a the Select object. 124 */ 125 void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ 126 int n = 0; 127 int cnt = 0; 128 pView = sqlite3TreeViewPush(pView, moreToFollow); 129 if( p->pWith ){ 130 sqlite3TreeViewWith(pView, p->pWith, 1); 131 cnt = 1; 132 sqlite3TreeViewPush(pView, 1); 133 } 134 do{ 135 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x", 136 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), 137 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags 138 ); 139 if( cnt++ ) sqlite3TreeViewPop(pView); 140 if( p->pPrior ){ 141 n = 1000; 142 }else{ 143 n = 0; 144 if( p->pSrc && p->pSrc->nSrc ) n++; 145 if( p->pWhere ) n++; 146 if( p->pGroupBy ) n++; 147 if( p->pHaving ) n++; 148 if( p->pOrderBy ) n++; 149 if( p->pLimit ) n++; 150 if( p->pOffset ) n++; 151 } 152 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); 153 if( p->pSrc && p->pSrc->nSrc ){ 154 int i; 155 pView = sqlite3TreeViewPush(pView, (n--)>0); 156 sqlite3TreeViewLine(pView, "FROM"); 157 for(i=0; i<p->pSrc->nSrc; i++){ 158 struct SrcList_item *pItem = &p->pSrc->a[i]; 159 StrAccum x; 160 char zLine[100]; 161 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); 162 sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor); 163 if( pItem->zDatabase ){ 164 sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName); 165 }else if( pItem->zName ){ 166 sqlite3XPrintf(&x, " %s", pItem->zName); 167 } 168 if( pItem->pTab ){ 169 sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName); 170 } 171 if( pItem->zAlias ){ 172 sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias); 173 } 174 if( pItem->fg.jointype & JT_LEFT ){ 175 sqlite3XPrintf(&x, " LEFT-JOIN"); 176 } 177 sqlite3StrAccumFinish(&x); 178 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 179 if( pItem->pSelect ){ 180 sqlite3TreeViewSelect(pView, pItem->pSelect, 0); 181 } 182 if( pItem->fg.isTabFunc ){ 183 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); 184 } 185 sqlite3TreeViewPop(pView); 186 } 187 sqlite3TreeViewPop(pView); 188 } 189 if( p->pWhere ){ 190 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); 191 sqlite3TreeViewExpr(pView, p->pWhere, 0); 192 sqlite3TreeViewPop(pView); 193 } 194 if( p->pGroupBy ){ 195 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); 196 } 197 if( p->pHaving ){ 198 sqlite3TreeViewItem(pView, "HAVING", (n--)>0); 199 sqlite3TreeViewExpr(pView, p->pHaving, 0); 200 sqlite3TreeViewPop(pView); 201 } 202 if( p->pOrderBy ){ 203 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); 204 } 205 if( p->pLimit ){ 206 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); 207 sqlite3TreeViewExpr(pView, p->pLimit, 0); 208 sqlite3TreeViewPop(pView); 209 } 210 if( p->pOffset ){ 211 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); 212 sqlite3TreeViewExpr(pView, p->pOffset, 0); 213 sqlite3TreeViewPop(pView); 214 } 215 if( p->pPrior ){ 216 const char *zOp = "UNION"; 217 switch( p->op ){ 218 case TK_ALL: zOp = "UNION ALL"; break; 219 case TK_INTERSECT: zOp = "INTERSECT"; break; 220 case TK_EXCEPT: zOp = "EXCEPT"; break; 221 } 222 sqlite3TreeViewItem(pView, zOp, 1); 223 } 224 p = p->pPrior; 225 }while( p!=0 ); 226 sqlite3TreeViewPop(pView); 227 } 228 229 /* 230 ** Generate a human-readable explanation of an expression tree. 231 */ 232 void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ 233 const char *zBinOp = 0; /* Binary operator */ 234 const char *zUniOp = 0; /* Unary operator */ 235 char zFlgs[30]; 236 pView = sqlite3TreeViewPush(pView, moreToFollow); 237 if( pExpr==0 ){ 238 sqlite3TreeViewLine(pView, "nil"); 239 sqlite3TreeViewPop(pView); 240 return; 241 } 242 if( pExpr->flags ){ 243 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags); 244 }else{ 245 zFlgs[0] = 0; 246 } 247 switch( pExpr->op ){ 248 case TK_AGG_COLUMN: { 249 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s", 250 pExpr->iTable, pExpr->iColumn, zFlgs); 251 break; 252 } 253 case TK_COLUMN: { 254 if( pExpr->iTable<0 ){ 255 /* This only happens when coding check constraints */ 256 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs); 257 }else{ 258 sqlite3TreeViewLine(pView, "{%d:%d}%s", 259 pExpr->iTable, pExpr->iColumn, zFlgs); 260 } 261 break; 262 } 263 case TK_INTEGER: { 264 if( pExpr->flags & EP_IntValue ){ 265 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); 266 }else{ 267 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); 268 } 269 break; 270 } 271 #ifndef SQLITE_OMIT_FLOATING_POINT 272 case TK_FLOAT: { 273 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); 274 break; 275 } 276 #endif 277 case TK_STRING: { 278 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); 279 break; 280 } 281 case TK_NULL: { 282 sqlite3TreeViewLine(pView,"NULL"); 283 break; 284 } 285 #ifndef SQLITE_OMIT_BLOB_LITERAL 286 case TK_BLOB: { 287 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); 288 break; 289 } 290 #endif 291 case TK_VARIABLE: { 292 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", 293 pExpr->u.zToken, pExpr->iColumn); 294 break; 295 } 296 case TK_REGISTER: { 297 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); 298 break; 299 } 300 case TK_ID: { 301 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); 302 break; 303 } 304 #ifndef SQLITE_OMIT_CAST 305 case TK_CAST: { 306 /* Expressions of the form: CAST(pLeft AS token) */ 307 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); 308 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); 309 break; 310 } 311 #endif /* SQLITE_OMIT_CAST */ 312 case TK_LT: zBinOp = "LT"; break; 313 case TK_LE: zBinOp = "LE"; break; 314 case TK_GT: zBinOp = "GT"; break; 315 case TK_GE: zBinOp = "GE"; break; 316 case TK_NE: zBinOp = "NE"; break; 317 case TK_EQ: zBinOp = "EQ"; break; 318 case TK_IS: zBinOp = "IS"; break; 319 case TK_ISNOT: zBinOp = "ISNOT"; break; 320 case TK_AND: zBinOp = "AND"; break; 321 case TK_OR: zBinOp = "OR"; break; 322 case TK_PLUS: zBinOp = "ADD"; break; 323 case TK_STAR: zBinOp = "MUL"; break; 324 case TK_MINUS: zBinOp = "SUB"; break; 325 case TK_REM: zBinOp = "REM"; break; 326 case TK_BITAND: zBinOp = "BITAND"; break; 327 case TK_BITOR: zBinOp = "BITOR"; break; 328 case TK_SLASH: zBinOp = "DIV"; break; 329 case TK_LSHIFT: zBinOp = "LSHIFT"; break; 330 case TK_RSHIFT: zBinOp = "RSHIFT"; break; 331 case TK_CONCAT: zBinOp = "CONCAT"; break; 332 case TK_DOT: zBinOp = "DOT"; break; 333 334 case TK_UMINUS: zUniOp = "UMINUS"; break; 335 case TK_UPLUS: zUniOp = "UPLUS"; break; 336 case TK_BITNOT: zUniOp = "BITNOT"; break; 337 case TK_NOT: zUniOp = "NOT"; break; 338 case TK_ISNULL: zUniOp = "ISNULL"; break; 339 case TK_NOTNULL: zUniOp = "NOTNULL"; break; 340 341 case TK_COLLATE: { 342 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); 343 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); 344 break; 345 } 346 347 case TK_AGG_FUNCTION: 348 case TK_FUNCTION: { 349 ExprList *pFarg; /* List of function arguments */ 350 if( ExprHasProperty(pExpr, EP_TokenOnly) ){ 351 pFarg = 0; 352 }else{ 353 pFarg = pExpr->x.pList; 354 } 355 if( pExpr->op==TK_AGG_FUNCTION ){ 356 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", 357 pExpr->op2, pExpr->u.zToken); 358 }else{ 359 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); 360 } 361 if( pFarg ){ 362 sqlite3TreeViewExprList(pView, pFarg, 0, 0); 363 } 364 break; 365 } 366 #ifndef SQLITE_OMIT_SUBQUERY 367 case TK_EXISTS: { 368 sqlite3TreeViewLine(pView, "EXISTS-expr"); 369 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); 370 break; 371 } 372 case TK_SELECT: { 373 sqlite3TreeViewLine(pView, "SELECT-expr"); 374 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); 375 break; 376 } 377 case TK_IN: { 378 sqlite3TreeViewLine(pView, "IN"); 379 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); 380 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 381 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); 382 }else{ 383 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); 384 } 385 break; 386 } 387 #endif /* SQLITE_OMIT_SUBQUERY */ 388 389 /* 390 ** x BETWEEN y AND z 391 ** 392 ** This is equivalent to 393 ** 394 ** x>=y AND x<=z 395 ** 396 ** X is stored in pExpr->pLeft. 397 ** Y is stored in pExpr->pList->a[0].pExpr. 398 ** Z is stored in pExpr->pList->a[1].pExpr. 399 */ 400 case TK_BETWEEN: { 401 Expr *pX = pExpr->pLeft; 402 Expr *pY = pExpr->x.pList->a[0].pExpr; 403 Expr *pZ = pExpr->x.pList->a[1].pExpr; 404 sqlite3TreeViewLine(pView, "BETWEEN"); 405 sqlite3TreeViewExpr(pView, pX, 1); 406 sqlite3TreeViewExpr(pView, pY, 1); 407 sqlite3TreeViewExpr(pView, pZ, 0); 408 break; 409 } 410 case TK_TRIGGER: { 411 /* If the opcode is TK_TRIGGER, then the expression is a reference 412 ** to a column in the new.* or old.* pseudo-tables available to 413 ** trigger programs. In this case Expr.iTable is set to 1 for the 414 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 415 ** is set to the column of the pseudo-table to read, or to -1 to 416 ** read the rowid field. 417 */ 418 sqlite3TreeViewLine(pView, "%s(%d)", 419 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); 420 break; 421 } 422 case TK_CASE: { 423 sqlite3TreeViewLine(pView, "CASE"); 424 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); 425 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); 426 break; 427 } 428 #ifndef SQLITE_OMIT_TRIGGER 429 case TK_RAISE: { 430 const char *zType = "unk"; 431 switch( pExpr->affinity ){ 432 case OE_Rollback: zType = "rollback"; break; 433 case OE_Abort: zType = "abort"; break; 434 case OE_Fail: zType = "fail"; break; 435 case OE_Ignore: zType = "ignore"; break; 436 } 437 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); 438 break; 439 } 440 #endif 441 default: { 442 sqlite3TreeViewLine(pView, "op=%d", pExpr->op); 443 break; 444 } 445 } 446 if( zBinOp ){ 447 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs); 448 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); 449 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); 450 }else if( zUniOp ){ 451 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs); 452 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); 453 } 454 sqlite3TreeViewPop(pView); 455 } 456 457 /* 458 ** Generate a human-readable explanation of an expression list. 459 */ 460 void sqlite3TreeViewExprList( 461 TreeView *pView, 462 const ExprList *pList, 463 u8 moreToFollow, 464 const char *zLabel 465 ){ 466 int i; 467 pView = sqlite3TreeViewPush(pView, moreToFollow); 468 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; 469 if( pList==0 ){ 470 sqlite3TreeViewLine(pView, "%s (empty)", zLabel); 471 }else{ 472 sqlite3TreeViewLine(pView, "%s", zLabel); 473 for(i=0; i<pList->nExpr; i++){ 474 int j = pList->a[i].u.x.iOrderByCol; 475 if( j ){ 476 sqlite3TreeViewPush(pView, 0); 477 sqlite3TreeViewLine(pView, "iOrderByCol=%d", j); 478 } 479 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); 480 if( j ) sqlite3TreeViewPop(pView); 481 } 482 } 483 sqlite3TreeViewPop(pView); 484 } 485 486 #endif /* SQLITE_DEBUG */ 487