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