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