github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/show.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tree 16 17 type Show interface { 18 Explain 19 } 20 21 type showImpl struct { 22 Show 23 } 24 25 // SHOW CREATE TABLE statement 26 type ShowCreateTable struct { 27 showImpl 28 Name *UnresolvedObjectName 29 } 30 31 func (node *ShowCreateTable) Format(ctx *FmtCtx) { 32 ctx.WriteString("show create table ") 33 node.Name.ToTableName().Format(ctx) 34 } 35 36 func (node *ShowCreateTable) GetStatementType() string { return "Show Create Table" } 37 func (node *ShowCreateTable) GetQueryType() string { return QueryTypeOth } 38 39 func NewShowCreate(n *UnresolvedObjectName) *ShowCreateTable { 40 return &ShowCreateTable{Name: n} 41 } 42 43 // SHOW CREATE VIEW statement 44 type ShowCreateView struct { 45 showImpl 46 Name *UnresolvedObjectName 47 } 48 49 func (node *ShowCreateView) Format(ctx *FmtCtx) { 50 ctx.WriteString("show create view ") 51 node.Name.ToTableName().Format(ctx) 52 } 53 func (node *ShowCreateView) GetStatementType() string { return "Show Create View" } 54 func (node *ShowCreateView) GetQueryType() string { return QueryTypeOth } 55 56 func NewShowCreateView(n *UnresolvedObjectName) *ShowCreateView { 57 return &ShowCreateView{Name: n} 58 } 59 60 // SHOW CREATE DATABASE statement 61 type ShowCreateDatabase struct { 62 showImpl 63 IfNotExists bool 64 Name string 65 } 66 67 func (node *ShowCreateDatabase) Format(ctx *FmtCtx) { 68 ctx.WriteString("show create database") 69 if node.IfNotExists { 70 ctx.WriteString(" if not exists") 71 } 72 ctx.WriteByte(' ') 73 ctx.WriteString(string(node.Name)) 74 } 75 func (node *ShowCreateDatabase) GetStatementType() string { return "Show Create View" } 76 func (node *ShowCreateDatabase) GetQueryType() string { return QueryTypeOth } 77 78 func NewShowCreateDatabase(i bool, n string) *ShowCreateDatabase { 79 return &ShowCreateDatabase{IfNotExists: i, Name: n} 80 } 81 82 // SHOW COLUMNS statement. 83 type ShowColumns struct { 84 showImpl 85 Ext bool 86 Full bool 87 Table *UnresolvedObjectName 88 ColName *UnresolvedName 89 DBName string 90 Like *ComparisonExpr 91 Where *Where 92 } 93 94 func (node *ShowColumns) Format(ctx *FmtCtx) { 95 ctx.WriteString("show") 96 if node.Ext { 97 ctx.WriteString(" extended") 98 } 99 if node.Full { 100 ctx.WriteString(" full") 101 } 102 ctx.WriteString(" columns") 103 if node.Table != nil { 104 ctx.WriteString(" from ") 105 node.Table.Format(ctx) 106 } 107 if node.DBName != "" { 108 ctx.WriteString(" from ") 109 ctx.WriteString(node.DBName) 110 } 111 if node.Like != nil { 112 ctx.WriteByte(' ') 113 node.Like.Format(ctx) 114 } 115 if node.Where != nil { 116 ctx.WriteByte(' ') 117 node.Where.Format(ctx) 118 } 119 } 120 func (node *ShowColumns) GetStatementType() string { return "Show Columns" } 121 func (node *ShowColumns) GetQueryType() string { return QueryTypeOth } 122 123 func NewShowColumns(e bool, f bool, t *UnresolvedObjectName, d string, l *ComparisonExpr, w *Where, cn *UnresolvedName) *ShowColumns { 124 return &ShowColumns{ 125 Ext: e, 126 Full: f, 127 Table: t, 128 ColName: cn, 129 DBName: d, 130 Like: l, 131 Where: w, 132 } 133 } 134 135 // the SHOW DATABASES statement. 136 type ShowDatabases struct { 137 showImpl 138 Like *ComparisonExpr 139 Where *Where 140 } 141 142 func (node *ShowDatabases) Format(ctx *FmtCtx) { 143 ctx.WriteString("show databases") 144 if node.Like != nil { 145 ctx.WriteByte(' ') 146 node.Like.Format(ctx) 147 } 148 if node.Where != nil { 149 ctx.WriteByte(' ') 150 node.Where.Format(ctx) 151 } 152 } 153 func (node *ShowDatabases) GetStatementType() string { return "Show Databases" } 154 func (node *ShowDatabases) GetQueryType() string { return QueryTypeOth } 155 156 func NewShowDatabases(l *ComparisonExpr, w *Where) *ShowDatabases { 157 return &ShowDatabases{ 158 Like: l, 159 Where: w, 160 } 161 } 162 163 type ShowType int 164 165 const ( 166 ShowEngines = iota 167 ShowCharset 168 ShowCreateUser 169 ShowTriggers 170 ShowProcedureStatus 171 ShowConfig 172 ShowEvents 173 ShowPlugins 174 ShowProfile 175 ShowProfiles 176 ShowPrivileges 177 ) 178 179 func (s ShowType) String() string { 180 switch s { 181 case ShowEngines: 182 return "engines" 183 case ShowCharset: 184 return "charset" 185 case ShowCreateUser: 186 return "create user" 187 case ShowTriggers: 188 return "triggers" 189 case ShowProcedureStatus: 190 return "procedure status" 191 case ShowConfig: 192 return "config" 193 case ShowEvents: 194 return "events" 195 case ShowPlugins: 196 return "plugins" 197 case ShowProfile: 198 return "profile" 199 case ShowProfiles: 200 return "profiles" 201 case ShowPrivileges: 202 return "privileges" 203 default: 204 return "not implemented" 205 } 206 } 207 208 type ShowTarget struct { 209 showImpl 210 Global bool 211 Type ShowType 212 DbName string 213 Like *ComparisonExpr 214 Where *Where 215 } 216 217 func (node *ShowTarget) Format(ctx *FmtCtx) { 218 ctx.WriteString("show ") 219 if node.Global { 220 ctx.WriteString("global ") 221 } 222 ctx.WriteString(node.Type.String()) 223 if node.DbName != "" { 224 ctx.WriteString(" from ") 225 ctx.WriteString(node.DbName) 226 } 227 if node.Like != nil { 228 ctx.WriteByte(' ') 229 node.Like.Format(ctx) 230 } 231 if node.Where != nil { 232 ctx.WriteByte(' ') 233 node.Where.Format(ctx) 234 } 235 } 236 func (node *ShowTarget) GetStatementType() string { return "Show Target" } 237 func (node *ShowTarget) GetQueryType() string { return QueryTypeOth } 238 239 type ShowTableStatus struct { 240 showImpl 241 DbName string 242 Like *ComparisonExpr 243 Where *Where 244 } 245 246 func (node *ShowTableStatus) Format(ctx *FmtCtx) { 247 ctx.WriteString("show table status") 248 if node.DbName != "" { 249 ctx.WriteString(" from ") 250 ctx.WriteString(node.DbName) 251 } 252 if node.Like != nil { 253 ctx.WriteByte(' ') 254 node.Like.Format(ctx) 255 } 256 if node.Where != nil { 257 ctx.WriteByte(' ') 258 node.Where.Format(ctx) 259 } 260 } 261 func (node *ShowTableStatus) GetStatementType() string { return "Show Table Status" } 262 func (node *ShowTableStatus) GetQueryType() string { return QueryTypeOth } 263 264 type ShowGrants struct { 265 showImpl 266 Username string 267 Hostname string 268 Roles []*Role 269 ShowGrantType ShowGrantType 270 } 271 272 type ShowGrantType int 273 274 const ( 275 GrantForUser = iota 276 GrantForRole 277 ) 278 279 func (node *ShowGrants) Format(ctx *FmtCtx) { 280 if node.ShowGrantType == GrantForRole { 281 ctx.WriteString("show grants") 282 if node.Roles != nil { 283 ctx.WriteString("for") 284 ctx.WriteString(" ") 285 ctx.WriteString(node.Roles[0].UserName) 286 } 287 } else { 288 ctx.WriteString("show grants") 289 if node.Username != "" { 290 ctx.WriteString(" for ") 291 ctx.WriteString(node.Username) 292 if node.Hostname != "" { 293 ctx.WriteString("@") 294 ctx.WriteString(node.Hostname) 295 } 296 } 297 if node.Roles != nil { 298 prefix := "" 299 for _, r := range node.Roles { 300 ctx.WriteString(prefix) 301 r.Format(ctx) 302 prefix = ", " 303 } 304 } 305 } 306 } 307 func (node *ShowGrants) GetStatementType() string { return "Show Grants" } 308 func (node *ShowGrants) GetQueryType() string { return QueryTypeOth } 309 310 // SHOW TABLES statement. 311 type ShowTables struct { 312 showImpl 313 Ext bool 314 Open bool 315 Full bool 316 DBName string 317 Like *ComparisonExpr 318 Where *Where 319 } 320 321 func (node *ShowTables) Format(ctx *FmtCtx) { 322 ctx.WriteString("show") 323 if node.Open { 324 ctx.WriteString(" open") 325 } 326 if node.Full { 327 ctx.WriteString(" full") 328 } 329 ctx.WriteString(" tables") 330 if node.DBName != "" { 331 ctx.WriteString(" from ") 332 ctx.WriteString(node.DBName) 333 } 334 if node.Like != nil { 335 ctx.WriteByte(' ') 336 node.Like.Format(ctx) 337 } 338 if node.Where != nil { 339 ctx.WriteByte(' ') 340 node.Where.Format(ctx) 341 } 342 } 343 func (node *ShowTables) GetStatementType() string { return "Show Tables" } 344 func (node *ShowTables) GetQueryType() string { return QueryTypeOth } 345 346 func NewShowTables(e bool, f bool, n string, l *ComparisonExpr, w *Where) *ShowTables { 347 return &ShowTables{ 348 Ext: e, 349 Full: f, 350 DBName: n, 351 Like: l, 352 Where: w, 353 } 354 } 355 356 // SHOW PROCESSLIST 357 type ShowProcessList struct { 358 showImpl 359 Full bool 360 } 361 362 func (node *ShowProcessList) Format(ctx *FmtCtx) { 363 ctx.WriteString("show") 364 if node.Full { 365 ctx.WriteString(" full") 366 } 367 ctx.WriteString(" processlist") 368 } 369 func (node *ShowProcessList) GetStatementType() string { return "Show Processlist" } 370 func (node *ShowProcessList) GetQueryType() string { return QueryTypeOth } 371 372 func NewShowProcessList(f bool) *ShowProcessList { 373 return &ShowProcessList{Full: f} 374 } 375 376 type ShowErrors struct { 377 showImpl 378 } 379 380 func (node *ShowErrors) Format(ctx *FmtCtx) { 381 ctx.WriteString("show errors") 382 } 383 func (node *ShowErrors) GetStatementType() string { return "Show Errors" } 384 func (node *ShowErrors) GetQueryType() string { return QueryTypeOth } 385 386 func NewShowErrors() *ShowErrors { 387 return &ShowErrors{} 388 } 389 390 type ShowWarnings struct { 391 showImpl 392 } 393 394 func (node *ShowWarnings) Format(ctx *FmtCtx) { 395 ctx.WriteString("show warnings") 396 } 397 func (node *ShowWarnings) GetStatementType() string { return "Show Warnings" } 398 func (node *ShowWarnings) GetQueryType() string { return QueryTypeOth } 399 400 func NewShowWarnings() *ShowWarnings { 401 return &ShowWarnings{} 402 } 403 404 // SHOW collation statement 405 type ShowCollation struct { 406 showImpl 407 Like *ComparisonExpr 408 Where *Where 409 } 410 411 func (node *ShowCollation) Format(ctx *FmtCtx) { 412 ctx.WriteString("show collation") 413 if node.Like != nil { 414 ctx.WriteString(" like ") 415 node.Like.Format(ctx) 416 } 417 if node.Where != nil { 418 ctx.WriteByte(' ') 419 node.Where.Format(ctx) 420 } 421 } 422 func (node *ShowCollation) GetStatementType() string { return "Show Collation" } 423 func (node *ShowCollation) GetQueryType() string { return QueryTypeOth } 424 425 // SHOW VARIABLES statement 426 // System Variables 427 type ShowVariables struct { 428 showImpl 429 Global bool 430 Like *ComparisonExpr 431 Where *Where 432 } 433 434 func (node *ShowVariables) Format(ctx *FmtCtx) { 435 ctx.WriteString("show") 436 if node.Global { 437 ctx.WriteString(" global") 438 } 439 ctx.WriteString(" variables") 440 if node.Like != nil { 441 ctx.WriteByte(' ') 442 node.Like.Format(ctx) 443 } 444 if node.Where != nil { 445 ctx.WriteByte(' ') 446 node.Where.Format(ctx) 447 } 448 } 449 func (node *ShowVariables) GetStatementType() string { return "Show Variables" } 450 func (node *ShowVariables) GetQueryType() string { return QueryTypeOth } 451 452 func NewShowVariables(g bool, l *ComparisonExpr, w *Where) *ShowVariables { 453 return &ShowVariables{ 454 Global: g, 455 Like: l, 456 Where: w, 457 } 458 } 459 460 // SHOW STATUS statement 461 type ShowStatus struct { 462 showImpl 463 Global bool 464 Like *ComparisonExpr 465 Where *Where 466 } 467 468 func (node *ShowStatus) Format(ctx *FmtCtx) { 469 ctx.WriteString("show") 470 if node.Global { 471 ctx.WriteString(" global") 472 } 473 ctx.WriteString(" status") 474 if node.Like != nil { 475 ctx.WriteString(" like ") 476 node.Like.Format(ctx) 477 } 478 if node.Where != nil { 479 ctx.WriteByte(' ') 480 node.Where.Format(ctx) 481 } 482 } 483 func (node *ShowStatus) GetStatementType() string { return "Show Status" } 484 func (node *ShowStatus) GetQueryType() string { return QueryTypeOth } 485 486 func NewShowStatus(g bool, l *ComparisonExpr, w *Where) *ShowStatus { 487 return &ShowStatus{ 488 Global: g, 489 Like: l, 490 Where: w, 491 } 492 } 493 494 // show index statement 495 type ShowIndex struct { 496 showImpl 497 TableName TableName 498 Where *Where 499 } 500 501 func (node *ShowIndex) Format(ctx *FmtCtx) { 502 ctx.WriteString("show index from ") 503 node.TableName.Format(ctx) 504 if node.Where != nil { 505 ctx.WriteByte(' ') 506 node.Where.Format(ctx) 507 } 508 } 509 func (node *ShowIndex) GetStatementType() string { return "Show Index" } 510 func (node *ShowIndex) GetQueryType() string { return QueryTypeOth } 511 512 func NewShowIndex(t TableName, w *Where) *ShowIndex { 513 return &ShowIndex{ 514 TableName: t, 515 Where: w, 516 } 517 } 518 519 // show Function statement 520 521 type ShowFunctionStatus struct { 522 showImpl 523 Like *ComparisonExpr 524 Where *Where 525 } 526 527 func (node *ShowFunctionStatus) Format(ctx *FmtCtx) { 528 ctx.WriteString("show function status") 529 if node.Like != nil { 530 ctx.WriteString(" like ") 531 node.Like.Format(ctx) 532 } 533 if node.Where != nil { 534 ctx.WriteByte(' ') 535 node.Where.Format(ctx) 536 } 537 } 538 539 func (node *ShowFunctionStatus) GetStatementType() string { return "Show Function Status" } 540 func (node *ShowFunctionStatus) GetQueryType() string { return QueryTypeOth } 541 542 func NewShowFunctionStatus(l *ComparisonExpr, w *Where) *ShowFunctionStatus { 543 return &ShowFunctionStatus{ 544 Like: l, 545 Where: w, 546 } 547 } 548 549 // show node list 550 type ShowNodeList struct { 551 showImpl 552 } 553 554 func (node *ShowNodeList) Format(ctx *FmtCtx) { 555 ctx.WriteString("show node list") 556 } 557 558 func (node *ShowNodeList) GetStatementType() string { return "Show Node List" } 559 func (node *ShowNodeList) GetQueryType() string { return QueryTypeOth } 560 561 func NewShowNodeList() *ShowNodeList { 562 return &ShowNodeList{} 563 } 564 565 // show locks 566 type ShowLocks struct { 567 showImpl 568 } 569 570 func (node *ShowLocks) Format(ctx *FmtCtx) { 571 ctx.WriteString("show locks") 572 } 573 574 func (node *ShowLocks) GetStatementType() string { return "Show Locks" } 575 func (node *ShowLocks) GetQueryType() string { return QueryTypeOth } 576 577 func NewShowLocks() *ShowLocks { 578 return &ShowLocks{} 579 } 580 581 // show table number 582 type ShowTableNumber struct { 583 showImpl 584 DbName string 585 } 586 587 func (node *ShowTableNumber) Format(ctx *FmtCtx) { 588 ctx.WriteString("show table number") 589 if node.DbName != "" { 590 ctx.WriteString(" from ") 591 ctx.WriteString(node.DbName) 592 } 593 } 594 func (node *ShowTableNumber) GetStatementType() string { return "Show Table Number" } 595 func (node *ShowTableNumber) GetQueryType() string { return QueryTypeOth } 596 597 func NewShowTableNumber(dbname string) *ShowTableNumber { 598 return &ShowTableNumber{ 599 DbName: dbname, 600 } 601 } 602 603 // show column number 604 type ShowColumnNumber struct { 605 showImpl 606 Table *UnresolvedObjectName 607 DbName string 608 } 609 610 func (node *ShowColumnNumber) Format(ctx *FmtCtx) { 611 ctx.WriteString("show column number") 612 if node.Table != nil { 613 ctx.WriteString(" from ") 614 node.Table.Format(ctx) 615 } 616 if node.DbName != "" { 617 ctx.WriteString(" from ") 618 ctx.WriteString(node.DbName) 619 } 620 } 621 func (node *ShowColumnNumber) GetStatementType() string { return "Show Column Number" } 622 func (node *ShowColumnNumber) GetQueryType() string { return QueryTypeOth } 623 624 func NewShowColumnNumber(table *UnresolvedObjectName, dbname string) *ShowColumnNumber { 625 return &ShowColumnNumber{ 626 Table: table, 627 DbName: dbname, 628 } 629 } 630 631 // show table values 632 type ShowTableValues struct { 633 showImpl 634 Table *UnresolvedObjectName 635 DbName string 636 } 637 638 func (node *ShowTableValues) Format(ctx *FmtCtx) { 639 ctx.WriteString("show table values") 640 if node.Table != nil { 641 ctx.WriteString(" from ") 642 node.Table.Format(ctx) 643 } 644 if node.DbName != "" { 645 ctx.WriteString(" from ") 646 ctx.WriteString(node.DbName) 647 } 648 } 649 func (node *ShowTableValues) GetStatementType() string { return "Show Table Values" } 650 func (node *ShowTableValues) GetQueryType() string { return QueryTypeOth } 651 652 func NewShowTableValues(table *UnresolvedObjectName, dbname string) *ShowTableValues { 653 return &ShowTableValues{ 654 Table: table, 655 DbName: dbname, 656 } 657 } 658 659 type ShowAccounts struct { 660 showImpl 661 Like *ComparisonExpr 662 } 663 664 func (node *ShowAccounts) Format(ctx *FmtCtx) { 665 ctx.WriteString("show accounts") 666 if node.Like != nil { 667 ctx.WriteByte(' ') 668 node.Like.Format(ctx) 669 } 670 } 671 672 func (node *ShowAccounts) GetStatementType() string { return "Show Accounts" } 673 func (node *ShowAccounts) GetQueryType() string { return QueryTypeOth }