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