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