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); |
drh | 10c0e71 | 2019-04-25 18:15:38 +0000 | [diff] [blame] | 69 | assert( acc.nChar>0 || acc.accError ); |
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 | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 109 | sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName); |
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 | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 114 | if( pCte->pUse ){ |
| 115 | sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse, |
| 116 | pCte->pUse->nUse); |
| 117 | } |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 118 | sqlite3StrAccumFinish(&x); |
| 119 | sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); |
| 120 | sqlite3TreeViewSelect(pView, pCte->pSelect, 0); |
| 121 | sqlite3TreeViewPop(pView); |
| 122 | } |
| 123 | sqlite3TreeViewPop(pView); |
| 124 | } |
| 125 | } |
| 126 | |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 127 | /* |
| 128 | ** Generate a human-readable description of a SrcList object. |
| 129 | */ |
| 130 | void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){ |
| 131 | int i; |
| 132 | for(i=0; i<pSrc->nSrc; i++){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 133 | const SrcItem *pItem = &pSrc->a[i]; |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 134 | StrAccum x; |
| 135 | char zLine[100]; |
| 136 | sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
drh | 6610e6a | 2021-03-19 19:44:56 +0000 | [diff] [blame^] | 137 | sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem); |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 138 | if( pItem->pTab ){ |
drh | f7f6dbf | 2020-03-21 22:03:32 +0000 | [diff] [blame] | 139 | sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx", |
| 140 | pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed); |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 141 | } |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 142 | if( pItem->fg.jointype & JT_LEFT ){ |
| 143 | sqlite3_str_appendf(&x, " LEFT-JOIN"); |
| 144 | } |
drh | b7e5199 | 2020-01-08 14:39:57 +0000 | [diff] [blame] | 145 | if( pItem->fg.fromDDL ){ |
| 146 | sqlite3_str_appendf(&x, " DDL"); |
| 147 | } |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 148 | if( pItem->fg.isCte ){ |
| 149 | sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse); |
| 150 | } |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 151 | sqlite3StrAccumFinish(&x); |
| 152 | sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); |
| 153 | if( pItem->pSelect ){ |
| 154 | sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 155 | } |
| 156 | if( pItem->fg.isTabFunc ){ |
| 157 | sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); |
| 158 | } |
| 159 | sqlite3TreeViewPop(pView); |
| 160 | } |
| 161 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 162 | |
| 163 | /* |
drh | 2e5c505 | 2016-08-27 20:21:51 +0000 | [diff] [blame] | 164 | ** Generate a human-readable description of a Select object. |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 165 | */ |
| 166 | void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ |
| 167 | int n = 0; |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 168 | int cnt = 0; |
drh | 510b7ff | 2017-03-13 17:37:13 +0000 | [diff] [blame] | 169 | if( p==0 ){ |
| 170 | sqlite3TreeViewLine(pView, "nil-SELECT"); |
| 171 | return; |
| 172 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 173 | pView = sqlite3TreeViewPush(pView, moreToFollow); |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 174 | if( p->pWith ){ |
| 175 | sqlite3TreeViewWith(pView, p->pWith, 1); |
| 176 | cnt = 1; |
| 177 | sqlite3TreeViewPush(pView, 1); |
| 178 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 179 | do{ |
drh | 55b4c82 | 2019-08-03 16:17:46 +0000 | [diff] [blame] | 180 | if( p->selFlags & SF_WhereBegin ){ |
| 181 | sqlite3TreeViewLine(pView, "sqlite3WhereBegin()"); |
| 182 | }else{ |
| 183 | sqlite3TreeViewLine(pView, |
| 184 | "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d", |
| 185 | ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 186 | ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), |
| 187 | p->selId, p, p->selFlags, |
| 188 | (int)p->nSelectRow |
| 189 | ); |
| 190 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 191 | if( cnt++ ) sqlite3TreeViewPop(pView); |
| 192 | if( p->pPrior ){ |
| 193 | n = 1000; |
| 194 | }else{ |
| 195 | n = 0; |
| 196 | if( p->pSrc && p->pSrc->nSrc ) n++; |
| 197 | if( p->pWhere ) n++; |
| 198 | if( p->pGroupBy ) n++; |
| 199 | if( p->pHaving ) n++; |
| 200 | if( p->pOrderBy ) n++; |
| 201 | if( p->pLimit ) n++; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 202 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 203 | if( p->pWin ) n++; |
| 204 | if( p->pWinDefn ) n++; |
| 205 | #endif |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 206 | } |
drh | 55b4c82 | 2019-08-03 16:17:46 +0000 | [diff] [blame] | 207 | if( p->pEList ){ |
| 208 | sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set"); |
| 209 | } |
| 210 | n--; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 211 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 212 | if( p->pWin ){ |
| 213 | Window *pX; |
| 214 | pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 215 | sqlite3TreeViewLine(pView, "window-functions"); |
| 216 | for(pX=p->pWin; pX; pX=pX->pNextWin){ |
| 217 | sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); |
| 218 | } |
| 219 | sqlite3TreeViewPop(pView); |
| 220 | } |
| 221 | #endif |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 222 | if( p->pSrc && p->pSrc->nSrc ){ |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 223 | pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 224 | sqlite3TreeViewLine(pView, "FROM"); |
drh | 145d0a3 | 2018-11-08 22:53:06 +0000 | [diff] [blame] | 225 | sqlite3TreeViewSrcList(pView, p->pSrc); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 226 | sqlite3TreeViewPop(pView); |
| 227 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 228 | if( p->pWhere ){ |
| 229 | sqlite3TreeViewItem(pView, "WHERE", (n--)>0); |
| 230 | sqlite3TreeViewExpr(pView, p->pWhere, 0); |
| 231 | sqlite3TreeViewPop(pView); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 232 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 233 | if( p->pGroupBy ){ |
| 234 | sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); |
| 235 | } |
| 236 | if( p->pHaving ){ |
| 237 | sqlite3TreeViewItem(pView, "HAVING", (n--)>0); |
| 238 | sqlite3TreeViewExpr(pView, p->pHaving, 0); |
| 239 | sqlite3TreeViewPop(pView); |
| 240 | } |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 241 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 242 | if( p->pWinDefn ){ |
| 243 | Window *pX; |
| 244 | sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); |
| 245 | for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ |
| 246 | sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); |
| 247 | } |
| 248 | sqlite3TreeViewPop(pView); |
| 249 | } |
| 250 | #endif |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 251 | if( p->pOrderBy ){ |
| 252 | sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); |
| 253 | } |
| 254 | if( p->pLimit ){ |
| 255 | sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 256 | sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); |
| 257 | if( p->pLimit->pRight ){ |
| 258 | sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); |
| 259 | sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); |
| 260 | sqlite3TreeViewPop(pView); |
| 261 | } |
drh | 1c4505d | 2015-08-26 11:34:31 +0000 | [diff] [blame] | 262 | sqlite3TreeViewPop(pView); |
| 263 | } |
| 264 | if( p->pPrior ){ |
| 265 | const char *zOp = "UNION"; |
| 266 | switch( p->op ){ |
| 267 | case TK_ALL: zOp = "UNION ALL"; break; |
| 268 | case TK_INTERSECT: zOp = "INTERSECT"; break; |
| 269 | case TK_EXCEPT: zOp = "EXCEPT"; break; |
| 270 | } |
| 271 | sqlite3TreeViewItem(pView, zOp, 1); |
| 272 | } |
| 273 | p = p->pPrior; |
| 274 | }while( p!=0 ); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 275 | sqlite3TreeViewPop(pView); |
| 276 | } |
| 277 | |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 278 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 279 | /* |
| 280 | ** Generate a description of starting or stopping bounds |
| 281 | */ |
| 282 | void sqlite3TreeViewBound( |
| 283 | TreeView *pView, /* View context */ |
| 284 | u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */ |
| 285 | Expr *pExpr, /* Value for PRECEDING or FOLLOWING */ |
| 286 | u8 moreToFollow /* True if more to follow */ |
| 287 | ){ |
| 288 | switch( eBound ){ |
| 289 | case TK_UNBOUNDED: { |
| 290 | sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow); |
| 291 | sqlite3TreeViewPop(pView); |
| 292 | break; |
| 293 | } |
| 294 | case TK_CURRENT: { |
| 295 | sqlite3TreeViewItem(pView, "CURRENT", moreToFollow); |
| 296 | sqlite3TreeViewPop(pView); |
| 297 | break; |
| 298 | } |
| 299 | case TK_PRECEDING: { |
| 300 | sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow); |
| 301 | sqlite3TreeViewExpr(pView, pExpr, 0); |
| 302 | sqlite3TreeViewPop(pView); |
| 303 | break; |
| 304 | } |
| 305 | case TK_FOLLOWING: { |
| 306 | sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow); |
| 307 | sqlite3TreeViewExpr(pView, pExpr, 0); |
| 308 | sqlite3TreeViewPop(pView); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 314 | |
| 315 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 316 | /* |
| 317 | ** Generate a human-readable explanation for a Window object |
| 318 | */ |
| 319 | void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){ |
drh | fc15f4c | 2019-03-28 13:03:41 +0000 | [diff] [blame] | 320 | int nElement = 0; |
drh | 0dc0e9c | 2019-03-28 13:35:28 +0000 | [diff] [blame] | 321 | if( pWin->pFilter ){ |
| 322 | sqlite3TreeViewItem(pView, "FILTER", 1); |
| 323 | sqlite3TreeViewExpr(pView, pWin->pFilter, 0); |
| 324 | sqlite3TreeViewPop(pView); |
| 325 | } |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 326 | pView = sqlite3TreeViewPush(pView, more); |
| 327 | if( pWin->zName ){ |
drh | 6f1644c | 2019-03-28 13:53:12 +0000 | [diff] [blame] | 328 | sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin); |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 329 | }else{ |
drh | 6f1644c | 2019-03-28 13:53:12 +0000 | [diff] [blame] | 330 | sqlite3TreeViewLine(pView, "OVER (%p)", pWin); |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 331 | } |
drh | fc15f4c | 2019-03-28 13:03:41 +0000 | [diff] [blame] | 332 | if( pWin->zBase ) nElement++; |
| 333 | if( pWin->pOrderBy ) nElement++; |
| 334 | if( pWin->eFrmType ) nElement++; |
| 335 | if( pWin->eExclude ) nElement++; |
drh | fc15f4c | 2019-03-28 13:03:41 +0000 | [diff] [blame] | 336 | if( pWin->zBase ){ |
| 337 | sqlite3TreeViewPush(pView, (--nElement)>0); |
| 338 | sqlite3TreeViewLine(pView, "window: %s", pWin->zBase); |
| 339 | sqlite3TreeViewPop(pView); |
| 340 | } |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 341 | if( pWin->pPartition ){ |
drh | fc15f4c | 2019-03-28 13:03:41 +0000 | [diff] [blame] | 342 | sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY"); |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 343 | } |
| 344 | if( pWin->pOrderBy ){ |
drh | fc15f4c | 2019-03-28 13:03:41 +0000 | [diff] [blame] | 345 | sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY"); |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 346 | } |
drh | fc15f4c | 2019-03-28 13:03:41 +0000 | [diff] [blame] | 347 | if( pWin->eFrmType ){ |
drh | 0dc0e9c | 2019-03-28 13:35:28 +0000 | [diff] [blame] | 348 | char zBuf[30]; |
drh | fc15f4c | 2019-03-28 13:03:41 +0000 | [diff] [blame] | 349 | const char *zFrmType = "ROWS"; |
| 350 | if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE"; |
| 351 | if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS"; |
drh | 0dc0e9c | 2019-03-28 13:35:28 +0000 | [diff] [blame] | 352 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType, |
| 353 | pWin->bImplicitFrame ? " (implied)" : ""); |
| 354 | sqlite3TreeViewItem(pView, zBuf, (--nElement)>0); |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 355 | sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); |
| 356 | sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); |
| 357 | sqlite3TreeViewPop(pView); |
| 358 | } |
drh | fc15f4c | 2019-03-28 13:03:41 +0000 | [diff] [blame] | 359 | if( pWin->eExclude ){ |
| 360 | char zBuf[30]; |
| 361 | const char *zExclude; |
| 362 | switch( pWin->eExclude ){ |
| 363 | case TK_NO: zExclude = "NO OTHERS"; break; |
| 364 | case TK_CURRENT: zExclude = "CURRENT ROW"; break; |
| 365 | case TK_GROUP: zExclude = "GROUP"; break; |
| 366 | case TK_TIES: zExclude = "TIES"; break; |
| 367 | default: |
| 368 | sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude); |
| 369 | zExclude = zBuf; |
| 370 | break; |
| 371 | } |
| 372 | sqlite3TreeViewPush(pView, 0); |
| 373 | sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude); |
| 374 | sqlite3TreeViewPop(pView); |
| 375 | } |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 376 | sqlite3TreeViewPop(pView); |
| 377 | } |
| 378 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 379 | |
| 380 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 381 | /* |
| 382 | ** Generate a human-readable explanation for a Window Function object |
| 383 | */ |
| 384 | void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){ |
| 385 | pView = sqlite3TreeViewPush(pView, more); |
| 386 | sqlite3TreeViewLine(pView, "WINFUNC %s(%d)", |
| 387 | pWin->pFunc->zName, pWin->pFunc->nArg); |
| 388 | sqlite3TreeViewWindow(pView, pWin, 0); |
| 389 | sqlite3TreeViewPop(pView); |
| 390 | } |
| 391 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 392 | |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 393 | /* |
| 394 | ** Generate a human-readable explanation of an expression tree. |
| 395 | */ |
| 396 | void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ |
| 397 | const char *zBinOp = 0; /* Binary operator */ |
| 398 | const char *zUniOp = 0; /* Unary operator */ |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 399 | char zFlgs[200]; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 400 | pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 401 | if( pExpr==0 ){ |
| 402 | sqlite3TreeViewLine(pView, "nil"); |
| 403 | sqlite3TreeViewPop(pView); |
| 404 | return; |
| 405 | } |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 406 | if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){ |
drh | b7e5199 | 2020-01-08 14:39:57 +0000 | [diff] [blame] | 407 | StrAccum x; |
| 408 | sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0); |
| 409 | sqlite3_str_appendf(&x, " fg.af=%x.%c", |
| 410 | pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n'); |
drh | d97cda4 | 2017-04-14 14:02:14 +0000 | [diff] [blame] | 411 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | b7e5199 | 2020-01-08 14:39:57 +0000 | [diff] [blame] | 412 | sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable); |
drh | d97cda4 | 2017-04-14 14:02:14 +0000 | [diff] [blame] | 413 | } |
drh | b7e5199 | 2020-01-08 14:39:57 +0000 | [diff] [blame] | 414 | if( ExprHasProperty(pExpr, EP_FromDDL) ){ |
| 415 | sqlite3_str_appendf(&x, " DDL"); |
| 416 | } |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 417 | if( ExprHasVVAProperty(pExpr, EP_Immutable) ){ |
| 418 | sqlite3_str_appendf(&x, " IMMUTABLE"); |
| 419 | } |
drh | b7e5199 | 2020-01-08 14:39:57 +0000 | [diff] [blame] | 420 | sqlite3StrAccumFinish(&x); |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 421 | }else{ |
| 422 | zFlgs[0] = 0; |
| 423 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 424 | switch( pExpr->op ){ |
| 425 | case TK_AGG_COLUMN: { |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 426 | sqlite3TreeViewLine(pView, "AGG{%d:%d}%s", |
| 427 | pExpr->iTable, pExpr->iColumn, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | case TK_COLUMN: { |
| 431 | if( pExpr->iTable<0 ){ |
| 432 | /* This only happens when coding check constraints */ |
drh | d493353 | 2019-10-31 12:30:38 +0000 | [diff] [blame] | 433 | char zOp2[16]; |
| 434 | if( pExpr->op2 ){ |
| 435 | sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2); |
| 436 | }else{ |
| 437 | zOp2[0] = 0; |
| 438 | } |
| 439 | sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s", |
| 440 | pExpr->iColumn, zFlgs, zOp2); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 441 | }else{ |
drh | a513e59 | 2019-12-20 20:08:56 +0000 | [diff] [blame] | 442 | sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s", |
| 443 | pExpr->iTable, pExpr->iColumn, |
| 444 | pExpr->y.pTab, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 445 | } |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 446 | if( ExprHasProperty(pExpr, EP_FixedCol) ){ |
| 447 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 448 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 449 | break; |
| 450 | } |
| 451 | case TK_INTEGER: { |
| 452 | if( pExpr->flags & EP_IntValue ){ |
| 453 | sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); |
| 454 | }else{ |
| 455 | sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); |
| 456 | } |
| 457 | break; |
| 458 | } |
| 459 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 460 | case TK_FLOAT: { |
| 461 | sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 462 | break; |
| 463 | } |
| 464 | #endif |
| 465 | case TK_STRING: { |
| 466 | sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); |
| 467 | break; |
| 468 | } |
| 469 | case TK_NULL: { |
| 470 | sqlite3TreeViewLine(pView,"NULL"); |
| 471 | break; |
| 472 | } |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 473 | case TK_TRUEFALSE: { |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 474 | sqlite3TreeViewLine(pView, |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 475 | sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE"); |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 476 | break; |
| 477 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 478 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 479 | case TK_BLOB: { |
| 480 | sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 481 | break; |
| 482 | } |
| 483 | #endif |
| 484 | case TK_VARIABLE: { |
| 485 | sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", |
| 486 | pExpr->u.zToken, pExpr->iColumn); |
| 487 | break; |
| 488 | } |
| 489 | case TK_REGISTER: { |
| 490 | sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); |
| 491 | break; |
| 492 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 493 | case TK_ID: { |
| 494 | sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); |
| 495 | break; |
| 496 | } |
| 497 | #ifndef SQLITE_OMIT_CAST |
| 498 | case TK_CAST: { |
| 499 | /* Expressions of the form: CAST(pLeft AS token) */ |
| 500 | sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); |
| 501 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 502 | break; |
| 503 | } |
| 504 | #endif /* SQLITE_OMIT_CAST */ |
| 505 | case TK_LT: zBinOp = "LT"; break; |
| 506 | case TK_LE: zBinOp = "LE"; break; |
| 507 | case TK_GT: zBinOp = "GT"; break; |
| 508 | case TK_GE: zBinOp = "GE"; break; |
| 509 | case TK_NE: zBinOp = "NE"; break; |
| 510 | case TK_EQ: zBinOp = "EQ"; break; |
| 511 | case TK_IS: zBinOp = "IS"; break; |
| 512 | case TK_ISNOT: zBinOp = "ISNOT"; break; |
| 513 | case TK_AND: zBinOp = "AND"; break; |
| 514 | case TK_OR: zBinOp = "OR"; break; |
| 515 | case TK_PLUS: zBinOp = "ADD"; break; |
| 516 | case TK_STAR: zBinOp = "MUL"; break; |
| 517 | case TK_MINUS: zBinOp = "SUB"; break; |
| 518 | case TK_REM: zBinOp = "REM"; break; |
| 519 | case TK_BITAND: zBinOp = "BITAND"; break; |
| 520 | case TK_BITOR: zBinOp = "BITOR"; break; |
| 521 | case TK_SLASH: zBinOp = "DIV"; break; |
| 522 | case TK_LSHIFT: zBinOp = "LSHIFT"; break; |
| 523 | case TK_RSHIFT: zBinOp = "RSHIFT"; break; |
| 524 | case TK_CONCAT: zBinOp = "CONCAT"; break; |
| 525 | case TK_DOT: zBinOp = "DOT"; break; |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 526 | case TK_LIMIT: zBinOp = "LIMIT"; break; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 527 | |
| 528 | case TK_UMINUS: zUniOp = "UMINUS"; break; |
| 529 | case TK_UPLUS: zUniOp = "UPLUS"; break; |
| 530 | case TK_BITNOT: zUniOp = "BITNOT"; break; |
| 531 | case TK_NOT: zUniOp = "NOT"; break; |
| 532 | case TK_ISNULL: zUniOp = "ISNULL"; break; |
| 533 | case TK_NOTNULL: zUniOp = "NOTNULL"; break; |
| 534 | |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 535 | case TK_TRUTH: { |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 536 | int x; |
| 537 | const char *azOp[] = { |
| 538 | "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" |
| 539 | }; |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 540 | assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); |
| 541 | assert( pExpr->pRight ); |
dan | 6ece353 | 2019-06-12 13:49:32 +0000 | [diff] [blame] | 542 | assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 543 | x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 544 | zUniOp = azOp[x]; |
drh | 3432821 | 2018-02-26 19:03:25 +0000 | [diff] [blame] | 545 | break; |
| 546 | } |
| 547 | |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 548 | case TK_SPAN: { |
| 549 | sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); |
| 550 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 551 | break; |
| 552 | } |
| 553 | |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 554 | case TK_COLLATE: { |
drh | c204d81 | 2019-09-10 17:51:27 +0000 | [diff] [blame] | 555 | /* COLLATE operators without the EP_Collate flag are intended to |
drh | 018dbb1 | 2019-09-28 16:14:55 +0000 | [diff] [blame] | 556 | ** emulate collation associated with a table column. These show |
| 557 | ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE |
| 558 | ** operators that appear in the original SQL always have the |
| 559 | ** EP_Collate bit set and appear in treeview output as just "COLLATE" */ |
drh | c204d81 | 2019-09-10 17:51:27 +0000 | [diff] [blame] | 560 | sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s", |
| 561 | !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "", |
| 562 | pExpr->u.zToken, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 563 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | case TK_AGG_FUNCTION: |
| 568 | case TK_FUNCTION: { |
| 569 | ExprList *pFarg; /* List of function arguments */ |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 570 | Window *pWin; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 571 | if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 572 | pFarg = 0; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 573 | pWin = 0; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 574 | }else{ |
| 575 | pFarg = pExpr->x.pList; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 576 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | 014fff2 | 2020-01-08 22:22:36 +0000 | [diff] [blame] | 577 | pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0; |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 578 | #else |
| 579 | pWin = 0; |
| 580 | #endif |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 581 | } |
| 582 | if( pExpr->op==TK_AGG_FUNCTION ){ |
drh | e26d428 | 2020-06-09 11:59:15 +0000 | [diff] [blame] | 583 | sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p", |
drh | ca74fbf | 2020-05-24 02:05:04 +0000 | [diff] [blame] | 584 | pExpr->op2, pExpr->u.zToken, zFlgs, |
drh | e26d428 | 2020-06-09 11:59:15 +0000 | [diff] [blame] | 585 | pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0, |
drh | ca74fbf | 2020-05-24 02:05:04 +0000 | [diff] [blame] | 586 | pExpr->iAgg, pExpr->pAggInfo); |
drh | d493353 | 2019-10-31 12:30:38 +0000 | [diff] [blame] | 587 | }else if( pExpr->op2!=0 ){ |
| 588 | const char *zOp2; |
| 589 | char zBuf[8]; |
| 590 | sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); |
| 591 | zOp2 = zBuf; |
| 592 | if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck"; |
| 593 | if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr"; |
| 594 | if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx"; |
| 595 | if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol"; |
| 596 | sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s", |
| 597 | pExpr->u.zToken, zFlgs, zOp2); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 598 | }else{ |
drh | 42d2fce | 2019-08-15 20:04:09 +0000 | [diff] [blame] | 599 | sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 600 | } |
| 601 | if( pFarg ){ |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 602 | sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 603 | } |
mistachkin | 1489785 | 2018-07-23 18:53:49 +0000 | [diff] [blame] | 604 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | a1fd4b5 | 2018-07-10 06:32:53 +0000 | [diff] [blame] | 605 | if( pWin ){ |
| 606 | sqlite3TreeViewWindow(pView, pWin, 0); |
| 607 | } |
| 608 | #endif |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 609 | break; |
| 610 | } |
| 611 | #ifndef SQLITE_OMIT_SUBQUERY |
| 612 | case TK_EXISTS: { |
drh | 9f6e14c | 2017-07-10 13:24:58 +0000 | [diff] [blame] | 613 | sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 614 | sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 615 | break; |
| 616 | } |
| 617 | case TK_SELECT: { |
drh | a0365c4 | 2020-06-05 04:01:50 +0000 | [diff] [blame] | 618 | sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 619 | sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 620 | break; |
| 621 | } |
| 622 | case TK_IN: { |
drh | 9f6e14c | 2017-07-10 13:24:58 +0000 | [diff] [blame] | 623 | sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 624 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 625 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 626 | sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 627 | }else{ |
| 628 | sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 629 | } |
| 630 | break; |
| 631 | } |
| 632 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 633 | |
| 634 | /* |
| 635 | ** x BETWEEN y AND z |
| 636 | ** |
| 637 | ** This is equivalent to |
| 638 | ** |
| 639 | ** x>=y AND x<=z |
| 640 | ** |
| 641 | ** X is stored in pExpr->pLeft. |
| 642 | ** Y is stored in pExpr->pList->a[0].pExpr. |
| 643 | ** Z is stored in pExpr->pList->a[1].pExpr. |
| 644 | */ |
| 645 | case TK_BETWEEN: { |
| 646 | Expr *pX = pExpr->pLeft; |
| 647 | Expr *pY = pExpr->x.pList->a[0].pExpr; |
| 648 | Expr *pZ = pExpr->x.pList->a[1].pExpr; |
| 649 | sqlite3TreeViewLine(pView, "BETWEEN"); |
| 650 | sqlite3TreeViewExpr(pView, pX, 1); |
| 651 | sqlite3TreeViewExpr(pView, pY, 1); |
| 652 | sqlite3TreeViewExpr(pView, pZ, 0); |
| 653 | break; |
| 654 | } |
| 655 | case TK_TRIGGER: { |
| 656 | /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 657 | ** to a column in the new.* or old.* pseudo-tables available to |
| 658 | ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 659 | ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 660 | ** is set to the column of the pseudo-table to read, or to -1 to |
| 661 | ** read the rowid field. |
| 662 | */ |
| 663 | sqlite3TreeViewLine(pView, "%s(%d)", |
| 664 | pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); |
| 665 | break; |
| 666 | } |
| 667 | case TK_CASE: { |
| 668 | sqlite3TreeViewLine(pView, "CASE"); |
| 669 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 670 | sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 671 | break; |
| 672 | } |
| 673 | #ifndef SQLITE_OMIT_TRIGGER |
| 674 | case TK_RAISE: { |
| 675 | const char *zType = "unk"; |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 676 | switch( pExpr->affExpr ){ |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 677 | case OE_Rollback: zType = "rollback"; break; |
| 678 | case OE_Abort: zType = "abort"; break; |
| 679 | case OE_Fail: zType = "fail"; break; |
| 680 | case OE_Ignore: zType = "ignore"; break; |
| 681 | } |
| 682 | sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); |
| 683 | break; |
| 684 | } |
| 685 | #endif |
drh | c84a402 | 2016-05-27 12:30:20 +0000 | [diff] [blame] | 686 | case TK_MATCH: { |
| 687 | sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s", |
| 688 | pExpr->iTable, pExpr->iColumn, zFlgs); |
| 689 | sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 690 | break; |
| 691 | } |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 692 | case TK_VECTOR: { |
drh | 269d322 | 2019-10-23 18:09:39 +0000 | [diff] [blame] | 693 | char *z = sqlite3_mprintf("VECTOR%s",zFlgs); |
| 694 | sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z); |
| 695 | sqlite3_free(z); |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 696 | break; |
| 697 | } |
drh | 48cb3a7 | 2016-08-18 18:09:10 +0000 | [diff] [blame] | 698 | case TK_SELECT_COLUMN: { |
| 699 | sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn); |
| 700 | sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); |
| 701 | break; |
| 702 | } |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 703 | case TK_IF_NULL_ROW: { |
| 704 | sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); |
| 705 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 706 | break; |
| 707 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 708 | default: { |
| 709 | sqlite3TreeViewLine(pView, "op=%d", pExpr->op); |
| 710 | break; |
| 711 | } |
| 712 | } |
| 713 | if( zBinOp ){ |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 714 | sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 715 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 716 | sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 717 | }else if( zUniOp ){ |
drh | b3d903e | 2015-06-18 14:09:13 +0000 | [diff] [blame] | 718 | sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs); |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 719 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 720 | } |
| 721 | sqlite3TreeViewPop(pView); |
| 722 | } |
| 723 | |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 724 | |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 725 | /* |
| 726 | ** Generate a human-readable explanation of an expression list. |
| 727 | */ |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 728 | void sqlite3TreeViewBareExprList( |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 729 | TreeView *pView, |
| 730 | const ExprList *pList, |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 731 | const char *zLabel |
| 732 | ){ |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 733 | if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; |
| 734 | if( pList==0 ){ |
| 735 | sqlite3TreeViewLine(pView, "%s (empty)", zLabel); |
| 736 | }else{ |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 737 | int i; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 738 | sqlite3TreeViewLine(pView, "%s", zLabel); |
| 739 | for(i=0; i<pList->nExpr; i++){ |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 740 | int j = pList->a[i].u.x.iOrderByCol; |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 741 | char *zName = pList->a[i].zEName; |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 742 | int moreToFollow = i<pList->nExpr - 1; |
drh | e1f49b8 | 2020-01-03 00:28:14 +0000 | [diff] [blame] | 743 | if( pList->a[i].eEName!=ENAME_NAME ) zName = 0; |
drh | 5a699a0 | 2017-12-22 19:53:02 +0000 | [diff] [blame] | 744 | if( j || zName ){ |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 745 | sqlite3TreeViewPush(pView, moreToFollow); |
| 746 | moreToFollow = 0; |
| 747 | sqlite3TreeViewLine(pView, 0); |
| 748 | if( zName ){ |
| 749 | fprintf(stdout, "AS %s ", zName); |
| 750 | } |
| 751 | if( j ){ |
| 752 | fprintf(stdout, "iOrderByCol=%d", j); |
| 753 | } |
| 754 | fprintf(stdout, "\n"); |
| 755 | fflush(stdout); |
drh | 5a699a0 | 2017-12-22 19:53:02 +0000 | [diff] [blame] | 756 | } |
drh | fbe0753 | 2018-04-23 20:04:38 +0000 | [diff] [blame] | 757 | sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); |
drh | 5a699a0 | 2017-12-22 19:53:02 +0000 | [diff] [blame] | 758 | if( j || zName ){ |
| 759 | sqlite3TreeViewPop(pView); |
| 760 | } |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
drh | db97e56 | 2016-08-18 17:55:57 +0000 | [diff] [blame] | 763 | } |
| 764 | void sqlite3TreeViewExprList( |
| 765 | TreeView *pView, |
| 766 | const ExprList *pList, |
| 767 | u8 moreToFollow, |
| 768 | const char *zLabel |
| 769 | ){ |
| 770 | pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 771 | sqlite3TreeViewBareExprList(pView, pList, zLabel); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 772 | sqlite3TreeViewPop(pView); |
| 773 | } |
| 774 | |
| 775 | #endif /* SQLITE_DEBUG */ |