drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 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++){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 61 | sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 62 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 63 | sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 64 | } |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 65 | if( zFormat!=0 ){ |
| 66 | va_start(ap, zFormat); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 67 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 68 | va_end(ap); |
| 69 | assert( acc.nChar>0 ); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 70 | sqlite3_str_append(&acc, "\n", 1); |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 71 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 72 | sqlite3StrAccumFinish(&acc); |
| 73 | fprintf(stdout,"%s", zBuf); |
| 74 | fflush(stdout); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | ** Shorthand for starting a new tree item that consists of a single label |
| 79 | */ |
| 80 | static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){ |
| 81 | p = sqlite3TreeViewPush(p, moreFollows); |
| 82 | sqlite3TreeViewLine(p, "%s", zLabel); |
| 83 | } |
| 84 | |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 85 | /* |
| 86 | ** Generate a human-readable description of a WITH clause. |
| 87 | */ |
| 88 | void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){ |
| 89 | int i; |
| 90 | if( pWith==0 ) return; |
| 91 | if( pWith->nCte==0 ) return; |
| 92 | if( pWith->pOuter ){ |
| 93 | sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); |
| 94 | }else{ |
| 95 | sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith); |
| 96 | } |
| 97 | if( pWith->nCte>0 ){ |
| 98 | pView = sqlite3TreeViewPush(pView, 1); |
| 99 | for(i=0; i<pWith->nCte; i++){ |
| 100 | StrAccum x; |
| 101 | char zLine[1000]; |
| 102 | const struct Cte *pCte = &pWith->a[i]; |
| 103 | sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 104 | sqlite3_str_appendf(&x, "%s", pCte->zName); |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 105 | if( pCte->pCols && pCte->pCols->nExpr>0 ){ |
| 106 | char cSep = '('; |
| 107 | int j; |
| 108 | for(j=0; j<pCte->pCols->nExpr; j++){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 109 | sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zName); |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 110 | cSep = ','; |
| 111 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 112 | sqlite3_str_appendf(&x, ")"); |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 113 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 114 | sqlite3_str_appendf(&x, " AS"); |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 115 | sqlite3StrAccumFinish(&x); |
| 116 | sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); |
| 117 | sqlite3TreeViewSelect(pView, pCte->pSelect, 0); |
| 118 | sqlite3TreeViewPop(pView); |
| 119 | } |
| 120 | sqlite3TreeViewPop(pView); |
| 121 | } |
| 122 | } |
| 123 | |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 124 | /* |
| 125 | ** Generate a human-readable description of a SrcList object. |
| 126 | */ |
| 127 | void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){ |
| 128 | int i; |
| 129 | for(i=0; i<pSrc->nSrc; i++){ |
| 130 | const struct SrcList_item *pItem = &pSrc->a[i]; |
| 131 | StrAccum x; |
| 132 | char zLine[100]; |
| 133 | sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
| 134 | sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor); |
| 135 | if( pItem->zDatabase ){ |
| 136 | sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName); |
| 137 | }else if( pItem->zName ){ |
| 138 | sqlite3_str_appendf(&x, " %s", pItem->zName); |
| 139 | } |
| 140 | if( pItem->pTab ){ |
drh | 7eb2c91 | 2018-12-27 00:30:42 +0000 | [diff] [blame] | 141 | sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p", |
| 142 | pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab); |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 143 | } |
| 144 | if( pItem->zAlias ){ |
| 145 | sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias); |
| 146 | } |
| 147 | if( pItem->fg.jointype & JT_LEFT ){ |
| 148 | sqlite3_str_appendf(&x, " LEFT-JOIN"); |
| 149 | } |
| 150 | sqlite3StrAccumFinish(&x); |
| 151 | sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); |
| 152 | if( pItem->pSelect ){ |
| 153 | sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 154 | } |
| 155 | if( pItem->fg.isTabFunc ){ |
| 156 | sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); |
| 157 | } |
| 158 | sqlite3TreeViewPop(pView); |
| 159 | } |
| 160 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
drh | 2e5c505 | 2016-08-27 20:21:51 +0000 | [diff] [blame] | 163 | ** Generate a human-readable description of a Select object. |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 164 | */ |
| 165 | void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ |
| 166 | int n = 0; |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 167 | int cnt = 0; |
drh | 510b7ff | 2017-03-13 17:37:13 +0000 | [diff] [blame] | 168 | if( p==0 ){ |
| 169 | sqlite3TreeViewLine(pView, "nil-SELECT"); |
| 170 | return; |
| 171 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 172 | pView = sqlite3TreeViewPush(pView, moreToFollow); |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 173 | if( p->pWith ){ |
| 174 | sqlite3TreeViewWith(pView, p->pWith, 1); |
| 175 | cnt = 1; |
| 176 | sqlite3TreeViewPush(pView, 1); |
| 177 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 178 | do{ |
drh | 926961d | 2018-03-19 16:06:11 +0000 | [diff] [blame] | 179 | sqlite3TreeViewLine(pView, |
drh | fef3776 | 2018-07-10 19:48:35 +0000 | [diff] [blame] | 180 | "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d", |
drh | 926961d | 2018-03-19 16:06:11 +0000 | [diff] [blame] | 181 | ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 182 | ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), |
drh | fef3776 | 2018-07-10 19:48:35 +0000 | [diff] [blame] | 183 | p->selId, p, p->selFlags, |
drh | 926961d | 2018-03-19 16:06:11 +0000 | [diff] [blame] | 184 | (int)p->nSelectRow |
| 185 | ); |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 186 | if( cnt++ ) sqlite3TreeViewPop(pView); |
| 187 | if( p->pPrior ){ |
| 188 | n = 1000; |
| 189 | }else{ |
| 190 | n = 0; |
| 191 | if( p->pSrc && p->pSrc->nSrc ) n++; |
| 192 | if( p->pWhere ) n++; |
| 193 | if( p->pGroupBy ) n++; |
| 194 | if( p->pHaving ) n++; |
| 195 | if( p->pOrderBy ) n++; |
| 196 | if( p->pLimit ) n++; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 197 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 198 | if( p->pWin ) n++; |
| 199 | if( p->pWinDefn ) n++; |
| 200 | #endif |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 201 | } |
| 202 | sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 203 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 204 | if( p->pWin ){ |
| 205 | Window *pX; |
| 206 | pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 207 | sqlite3TreeViewLine(pView, "window-functions"); |
| 208 | for(pX=p->pWin; pX; pX=pX->pNextWin){ |
| 209 | sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); |
| 210 | } |
| 211 | sqlite3TreeViewPop(pView); |
| 212 | } |
| 213 | #endif |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 214 | if( p->pSrc && p->pSrc->nSrc ){ |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 215 | pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 216 | sqlite3TreeViewLine(pView, "FROM"); |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 217 | sqlite3TreeViewSrcList(pView, p->pSrc); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 218 | sqlite3TreeViewPop(pView); |
| 219 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 220 | if( p->pWhere ){ |
| 221 | sqlite3TreeViewItem(pView, "WHERE", (n--)>0); |
| 222 | sqlite3TreeViewExpr(pView, p->pWhere, 0); |
| 223 | sqlite3TreeViewPop(pView); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 224 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 225 | if( p->pGroupBy ){ |
| 226 | sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); |
| 227 | } |
| 228 | if( p->pHaving ){ |
| 229 | sqlite3TreeViewItem(pView, "HAVING", (n--)>0); |
| 230 | sqlite3TreeViewExpr(pView, p->pHaving, 0); |
| 231 | sqlite3TreeViewPop(pView); |
| 232 | } |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 233 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 234 | if( p->pWinDefn ){ |
| 235 | Window *pX; |
| 236 | sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); |
| 237 | for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ |
| 238 | sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); |
| 239 | } |
| 240 | sqlite3TreeViewPop(pView); |
| 241 | } |
| 242 | #endif |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 243 | if( p->pOrderBy ){ |
| 244 | sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); |
| 245 | } |
| 246 | if( p->pLimit ){ |
| 247 | sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 248 | sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); |
| 249 | if( p->pLimit->pRight ){ |
| 250 | sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); |
| 251 | sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); |
| 252 | sqlite3TreeViewPop(pView); |
| 253 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 254 | sqlite3TreeViewPop(pView); |
| 255 | } |
| 256 | if( p->pPrior ){ |
| 257 | const char *zOp = "UNION"; |
| 258 | switch( p->op ){ |
| 259 | case TK_ALL: zOp = "UNION ALL"; break; |
| 260 | case TK_INTERSECT: zOp = "INTERSECT"; break; |
| 261 | case TK_EXCEPT: zOp = "EXCEPT"; break; |
| 262 | } |
| 263 | sqlite3TreeViewItem(pView, zOp, 1); |
| 264 | } |
| 265 | p = p->pPrior; |
| 266 | }while( p!=0 ); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 267 | sqlite3TreeViewPop(pView); |
| 268 | } |
| 269 | |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 270 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 271 | /* |
| 272 | ** Generate a description of starting or stopping bounds |
| 273 | */ |
| 274 | void sqlite3TreeViewBound( |
| 275 | TreeView *pView, /* View context */ |
| 276 | u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */ |
| 277 | Expr *pExpr, /* Value for PRECEDING or FOLLOWING */ |
| 278 | u8 moreToFollow /* True if more to follow */ |
| 279 | ){ |
| 280 | switch( eBound ){ |
| 281 | case TK_UNBOUNDED: { |
| 282 | sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow); |
| 283 | sqlite3TreeViewPop(pView); |
| 284 | break; |
| 285 | } |
| 286 | case TK_CURRENT: { |
| 287 | sqlite3TreeViewItem(pView, "CURRENT", moreToFollow); |
| 288 | sqlite3TreeViewPop(pView); |
| 289 | break; |
| 290 | } |
| 291 | case TK_PRECEDING: { |
| 292 | sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow); |
| 293 | sqlite3TreeViewExpr(pView, pExpr, 0); |
| 294 | sqlite3TreeViewPop(pView); |
| 295 | break; |
| 296 | } |
| 297 | case TK_FOLLOWING: { |
| 298 | sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow); |
| 299 | sqlite3TreeViewExpr(pView, pExpr, 0); |
| 300 | sqlite3TreeViewPop(pView); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 306 | |
| 307 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 308 | /* |
| 309 | ** Generate a human-readable explanation for a Window object |
| 310 | */ |
| 311 | void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){ |
| 312 | pView = sqlite3TreeViewPush(pView, more); |
| 313 | if( pWin->zName ){ |
| 314 | sqlite3TreeViewLine(pView, "OVER %s", pWin->zName); |
| 315 | }else{ |
| 316 | sqlite3TreeViewLine(pView, "OVER"); |
| 317 | } |
| 318 | if( pWin->pPartition ){ |
| 319 | sqlite3TreeViewExprList(pView, pWin->pPartition, 1, "PARTITION-BY"); |
| 320 | } |
| 321 | if( pWin->pOrderBy ){ |
| 322 | sqlite3TreeViewExprList(pView, pWin->pOrderBy, 1, "ORDER-BY"); |
| 323 | } |
| 324 | if( pWin->eType ){ |
| 325 | sqlite3TreeViewItem(pView, pWin->eType==TK_RANGE ? "RANGE" : "ROWS", 0); |
| 326 | sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); |
| 327 | sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); |
| 328 | sqlite3TreeViewPop(pView); |
| 329 | } |
| 330 | sqlite3TreeViewPop(pView); |
| 331 | } |
| 332 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 333 | |
| 334 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 335 | /* |
| 336 | ** Generate a human-readable explanation for a Window Function object |
| 337 | */ |
| 338 | void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){ |
| 339 | pView = sqlite3TreeViewPush(pView, more); |
| 340 | sqlite3TreeViewLine(pView, "WINFUNC %s(%d)", |
| 341 | pWin->pFunc->zName, pWin->pFunc->nArg); |
| 342 | sqlite3TreeViewWindow(pView, pWin, 0); |
| 343 | sqlite3TreeViewPop(pView); |
| 344 | } |
| 345 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 346 | |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 347 | /* |
| 348 | ** Generate a human-readable explanation of an expression tree. |
| 349 | */ |
| 350 | void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ |
| 351 | const char *zBinOp = 0; /* Binary operator */ |
| 352 | const char *zUniOp = 0; /* Unary operator */ |
drh | d97cda4 | 2017-04-14 14:02:14 +0000 | [diff] [blame] | 353 | char zFlgs[60]; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 354 | pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 355 | if( pExpr==0 ){ |
| 356 | sqlite3TreeViewLine(pView, "nil"); |
| 357 | sqlite3TreeViewPop(pView); |
| 358 | return; |
| 359 | } |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 360 | if( pExpr->flags ){ |
drh | d97cda4 | 2017-04-14 14:02:14 +0000 | [diff] [blame] | 361 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 362 | sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x iRJT=%d", |
| 363 | pExpr->flags, pExpr->iRightJoinTable); |
| 364 | }else{ |
| 365 | sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags); |
| 366 | } |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 367 | }else{ |
| 368 | zFlgs[0] = 0; |
| 369 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 370 | switch( pExpr->op ){ |
| 371 | case TK_AGG_COLUMN: { |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 372 | sqlite3TreeViewLine(pView, "AGG{%d:%d}%s", |
| 373 | pExpr->iTable, pExpr->iColumn, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | case TK_COLUMN: { |
| 377 | if( pExpr->iTable<0 ){ |
| 378 | /* This only happens when coding check constraints */ |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 379 | sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 380 | }else{ |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 381 | sqlite3TreeViewLine(pView, "{%d:%d}%s", |
| 382 | pExpr->iTable, pExpr->iColumn, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 383 | } |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 384 | if( ExprHasProperty(pExpr, EP_FixedCol) ){ |
| 385 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 386 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 387 | break; |
| 388 | } |
| 389 | case TK_INTEGER: { |
| 390 | if( pExpr->flags & EP_IntValue ){ |
| 391 | sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); |
| 392 | }else{ |
| 393 | sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); |
| 394 | } |
| 395 | break; |
| 396 | } |
| 397 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 398 | case TK_FLOAT: { |
| 399 | sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 400 | break; |
| 401 | } |
| 402 | #endif |
| 403 | case TK_STRING: { |
| 404 | sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); |
| 405 | break; |
| 406 | } |
| 407 | case TK_NULL: { |
| 408 | sqlite3TreeViewLine(pView,"NULL"); |
| 409 | break; |
| 410 | } |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 411 | case TK_TRUEFALSE: { |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 412 | sqlite3TreeViewLine(pView, |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 413 | sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE"); |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 414 | break; |
| 415 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 416 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 417 | case TK_BLOB: { |
| 418 | sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 419 | break; |
| 420 | } |
| 421 | #endif |
| 422 | case TK_VARIABLE: { |
| 423 | sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", |
| 424 | pExpr->u.zToken, pExpr->iColumn); |
| 425 | break; |
| 426 | } |
| 427 | case TK_REGISTER: { |
| 428 | sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); |
| 429 | break; |
| 430 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 431 | case TK_ID: { |
| 432 | sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); |
| 433 | break; |
| 434 | } |
| 435 | #ifndef SQLITE_OMIT_CAST |
| 436 | case TK_CAST: { |
| 437 | /* Expressions of the form: CAST(pLeft AS token) */ |
| 438 | sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); |
| 439 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 440 | break; |
| 441 | } |
| 442 | #endif /* SQLITE_OMIT_CAST */ |
| 443 | case TK_LT: zBinOp = "LT"; break; |
| 444 | case TK_LE: zBinOp = "LE"; break; |
| 445 | case TK_GT: zBinOp = "GT"; break; |
| 446 | case TK_GE: zBinOp = "GE"; break; |
| 447 | case TK_NE: zBinOp = "NE"; break; |
| 448 | case TK_EQ: zBinOp = "EQ"; break; |
| 449 | case TK_IS: zBinOp = "IS"; break; |
| 450 | case TK_ISNOT: zBinOp = "ISNOT"; break; |
| 451 | case TK_AND: zBinOp = "AND"; break; |
| 452 | case TK_OR: zBinOp = "OR"; break; |
| 453 | case TK_PLUS: zBinOp = "ADD"; break; |
| 454 | case TK_STAR: zBinOp = "MUL"; break; |
| 455 | case TK_MINUS: zBinOp = "SUB"; break; |
| 456 | case TK_REM: zBinOp = "REM"; break; |
| 457 | case TK_BITAND: zBinOp = "BITAND"; break; |
| 458 | case TK_BITOR: zBinOp = "BITOR"; break; |
| 459 | case TK_SLASH: zBinOp = "DIV"; break; |
| 460 | case TK_LSHIFT: zBinOp = "LSHIFT"; break; |
| 461 | case TK_RSHIFT: zBinOp = "RSHIFT"; break; |
| 462 | case TK_CONCAT: zBinOp = "CONCAT"; break; |
| 463 | case TK_DOT: zBinOp = "DOT"; break; |
| 464 | |
| 465 | case TK_UMINUS: zUniOp = "UMINUS"; break; |
| 466 | case TK_UPLUS: zUniOp = "UPLUS"; break; |
| 467 | case TK_BITNOT: zUniOp = "BITNOT"; break; |
| 468 | case TK_NOT: zUniOp = "NOT"; break; |
| 469 | case TK_ISNULL: zUniOp = "ISNULL"; break; |
| 470 | case TK_NOTNULL: zUniOp = "NOTNULL"; break; |
| 471 | |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 472 | case TK_TRUTH: { |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 473 | int x; |
| 474 | const char *azOp[] = { |
| 475 | "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" |
| 476 | }; |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 477 | assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); |
| 478 | assert( pExpr->pRight ); |
| 479 | assert( pExpr->pRight->op==TK_TRUEFALSE ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 480 | x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 481 | zUniOp = azOp[x]; |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 485 | case TK_SPAN: { |
| 486 | sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); |
| 487 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 488 | break; |
| 489 | } |
| 490 | |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 491 | case TK_COLLATE: { |
| 492 | sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); |
| 493 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | case TK_AGG_FUNCTION: |
| 498 | case TK_FUNCTION: { |
| 499 | ExprList *pFarg; /* List of function arguments */ |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 500 | Window *pWin; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 501 | if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 502 | pFarg = 0; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 503 | pWin = 0; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 504 | }else{ |
| 505 | pFarg = pExpr->x.pList; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 506 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 507 | pWin = pExpr->y.pWin; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 508 | #else |
| 509 | pWin = 0; |
| 510 | #endif |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 511 | } |
| 512 | if( pExpr->op==TK_AGG_FUNCTION ){ |
| 513 | sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", |
| 514 | pExpr->op2, pExpr->u.zToken); |
| 515 | }else{ |
| 516 | sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); |
| 517 | } |
| 518 | if( pFarg ){ |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 519 | sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 520 | } |
mistachkin | 1489785 | 2018-07-23 18:53:49 +0000 | [diff] [blame] | 521 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 522 | if( pWin ){ |
| 523 | sqlite3TreeViewWindow(pView, pWin, 0); |
| 524 | } |
| 525 | #endif |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 526 | break; |
| 527 | } |
| 528 | #ifndef SQLITE_OMIT_SUBQUERY |
| 529 | case TK_EXISTS: { |
drh | 9f6e14c | 2017-07-10 13:24:58 +0000 | [diff] [blame] | 530 | sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 531 | sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 532 | break; |
| 533 | } |
| 534 | case TK_SELECT: { |
drh | 9f6e14c | 2017-07-10 13:24:58 +0000 | [diff] [blame] | 535 | sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 536 | sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 537 | break; |
| 538 | } |
| 539 | case TK_IN: { |
drh | 9f6e14c | 2017-07-10 13:24:58 +0000 | [diff] [blame] | 540 | sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 541 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 542 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 543 | sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 544 | }else{ |
| 545 | sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 546 | } |
| 547 | break; |
| 548 | } |
| 549 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 550 | |
| 551 | /* |
| 552 | ** x BETWEEN y AND z |
| 553 | ** |
| 554 | ** This is equivalent to |
| 555 | ** |
| 556 | ** x>=y AND x<=z |
| 557 | ** |
| 558 | ** X is stored in pExpr->pLeft. |
| 559 | ** Y is stored in pExpr->pList->a[0].pExpr. |
| 560 | ** Z is stored in pExpr->pList->a[1].pExpr. |
| 561 | */ |
| 562 | case TK_BETWEEN: { |
| 563 | Expr *pX = pExpr->pLeft; |
| 564 | Expr *pY = pExpr->x.pList->a[0].pExpr; |
| 565 | Expr *pZ = pExpr->x.pList->a[1].pExpr; |
| 566 | sqlite3TreeViewLine(pView, "BETWEEN"); |
| 567 | sqlite3TreeViewExpr(pView, pX, 1); |
| 568 | sqlite3TreeViewExpr(pView, pY, 1); |
| 569 | sqlite3TreeViewExpr(pView, pZ, 0); |
| 570 | break; |
| 571 | } |
| 572 | case TK_TRIGGER: { |
| 573 | /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 574 | ** to a column in the new.* or old.* pseudo-tables available to |
| 575 | ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 576 | ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 577 | ** is set to the column of the pseudo-table to read, or to -1 to |
| 578 | ** read the rowid field. |
| 579 | */ |
| 580 | sqlite3TreeViewLine(pView, "%s(%d)", |
| 581 | pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); |
| 582 | break; |
| 583 | } |
| 584 | case TK_CASE: { |
| 585 | sqlite3TreeViewLine(pView, "CASE"); |
| 586 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 587 | sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 588 | break; |
| 589 | } |
| 590 | #ifndef SQLITE_OMIT_TRIGGER |
| 591 | case TK_RAISE: { |
| 592 | const char *zType = "unk"; |
| 593 | switch( pExpr->affinity ){ |
| 594 | case OE_Rollback: zType = "rollback"; break; |
| 595 | case OE_Abort: zType = "abort"; break; |
| 596 | case OE_Fail: zType = "fail"; break; |
| 597 | case OE_Ignore: zType = "ignore"; break; |
| 598 | } |
| 599 | sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); |
| 600 | break; |
| 601 | } |
| 602 | #endif |
drh | c84a402 | 2016-05-27 12:30:20 +0000 | [diff] [blame] | 603 | case TK_MATCH: { |
| 604 | sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s", |
| 605 | pExpr->iTable, pExpr->iColumn, zFlgs); |
| 606 | sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 607 | break; |
| 608 | } |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 609 | case TK_VECTOR: { |
| 610 | sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR"); |
| 611 | break; |
| 612 | } |
drh | 48cb3a7 | 2016-08-18 18:09:10 +0000 | [diff] [blame] | 613 | case TK_SELECT_COLUMN: { |
| 614 | sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn); |
| 615 | sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); |
| 616 | break; |
| 617 | } |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 618 | case TK_IF_NULL_ROW: { |
| 619 | sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); |
| 620 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 621 | break; |
| 622 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 623 | default: { |
| 624 | sqlite3TreeViewLine(pView, "op=%d", pExpr->op); |
| 625 | break; |
| 626 | } |
| 627 | } |
| 628 | if( zBinOp ){ |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 629 | sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 630 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 631 | sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 632 | }else if( zUniOp ){ |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 633 | sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 634 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 635 | } |
| 636 | sqlite3TreeViewPop(pView); |
| 637 | } |
| 638 | |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 639 | |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 640 | /* |
| 641 | ** Generate a human-readable explanation of an expression list. |
| 642 | */ |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 643 | void sqlite3TreeViewBareExprList( |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 644 | TreeView *pView, |
| 645 | const ExprList *pList, |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 646 | const char *zLabel |
| 647 | ){ |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 648 | if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; |
| 649 | if( pList==0 ){ |
| 650 | sqlite3TreeViewLine(pView, "%s (empty)", zLabel); |
| 651 | }else{ |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 652 | int i; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 653 | sqlite3TreeViewLine(pView, "%s", zLabel); |
| 654 | for(i=0; i<pList->nExpr; i++){ |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 655 | int j = pList->a[i].u.x.iOrderByCol; |
drh | 5a699a0 | 2017-12-22 19:53:02 +0000 | [diff] [blame] | 656 | char *zName = pList->a[i].zName; |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 657 | int moreToFollow = i<pList->nExpr - 1; |
drh | 5a699a0 | 2017-12-22 19:53:02 +0000 | [diff] [blame] | 658 | if( j || zName ){ |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 659 | sqlite3TreeViewPush(pView, moreToFollow); |
| 660 | moreToFollow = 0; |
| 661 | sqlite3TreeViewLine(pView, 0); |
| 662 | if( zName ){ |
| 663 | fprintf(stdout, "AS %s ", zName); |
| 664 | } |
| 665 | if( j ){ |
| 666 | fprintf(stdout, "iOrderByCol=%d", j); |
| 667 | } |
| 668 | fprintf(stdout, "\n"); |
| 669 | fflush(stdout); |
drh | 5a699a0 | 2017-12-22 19:53:02 +0000 | [diff] [blame] | 670 | } |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 671 | sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); |
drh | 5a699a0 | 2017-12-22 19:53:02 +0000 | [diff] [blame] | 672 | if( j || zName ){ |
| 673 | sqlite3TreeViewPop(pView); |
| 674 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 677 | } |
| 678 | void sqlite3TreeViewExprList( |
| 679 | TreeView *pView, |
| 680 | const ExprList *pList, |
| 681 | u8 moreToFollow, |
| 682 | const char *zLabel |
| 683 | ){ |
| 684 | pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 685 | sqlite3TreeViewBareExprList(pView, pList, zLabel); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 686 | sqlite3TreeViewPop(pView); |
| 687 | } |
| 688 | |
| 689 | #endif /* SQLITE_DEBUG */ |