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