feat: show create database (#4642)

* feat: show create database

* feat: add sqlness test

* chore: reorder mod and use

* feat: show create schema

* Update src/frontend/src/instance.rs
This commit is contained in:
jeremyhi
2024-08-30 11:58:11 +08:00
committed by GitHub
parent 9286e963e7
commit f641c562c2
12 changed files with 191 additions and 27 deletions

View File

@@ -21,8 +21,9 @@ use crate::error::{
};
use crate::parser::ParserContext;
use crate::statements::show::{
ShowColumns, ShowCreateFlow, ShowCreateTable, ShowCreateView, ShowDatabases, ShowFlows,
ShowIndex, ShowKind, ShowStatus, ShowTableStatus, ShowTables, ShowVariables, ShowViews,
ShowColumns, ShowCreateDatabase, ShowCreateFlow, ShowCreateTable, ShowCreateView,
ShowDatabases, ShowFlows, ShowIndex, ShowKind, ShowStatus, ShowTableStatus, ShowTables,
ShowVariables, ShowViews,
};
use crate::statements::statement::Statement;
@@ -74,7 +75,9 @@ impl<'a> ParserContext<'a> {
// SHOW {INDEX | INDEXES | KEYS}
self.parse_show_index()
} else if self.consume_token("CREATE") {
if self.consume_token("TABLE") {
if self.consume_token("DATABASE") || self.consume_token("SCHEMA") {
self.parse_show_create_database()
} else if self.consume_token("TABLE") {
self.parse_show_create_table()
} else if self.consume_token("FLOW") {
self.parse_show_create_flow()
@@ -109,6 +112,25 @@ impl<'a> ParserContext<'a> {
}
}
fn parse_show_create_database(&mut self) -> Result<Statement> {
let raw_database_name =
self.parse_object_name()
.with_context(|_| error::UnexpectedSnafu {
expected: "a database name",
actual: self.peek_token_as_string(),
})?;
let database_name = Self::canonicalize_object_name(raw_database_name);
ensure!(
!database_name.0.is_empty(),
InvalidDatabaseNameSnafu {
name: database_name.to_string(),
}
);
Ok(Statement::ShowCreateDatabase(ShowCreateDatabase {
database_name,
}))
}
/// Parse SHOW CREATE TABLE statement
fn parse_show_create_table(&mut self) -> Result<Statement> {
let raw_table_name = self

View File

@@ -161,6 +161,19 @@ impl Display for ShowTableStatus {
}
}
/// SQL structure for `SHOW CREATE DATABASE`.
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
pub struct ShowCreateDatabase {
pub database_name: ObjectName,
}
impl Display for ShowCreateDatabase {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let database_name = &self.database_name;
write!(f, r#"SHOW CREATE DATABASE {database_name}"#)
}
}
/// SQL structure for `SHOW CREATE TABLE`.
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
pub struct ShowCreateTable {

View File

@@ -32,8 +32,9 @@ use crate::statements::insert::Insert;
use crate::statements::query::Query;
use crate::statements::set_variables::SetVariables;
use crate::statements::show::{
ShowColumns, ShowCreateFlow, ShowCreateTable, ShowCreateView, ShowDatabases, ShowFlows,
ShowIndex, ShowKind, ShowStatus, ShowTableStatus, ShowTables, ShowVariables, ShowViews,
ShowColumns, ShowCreateDatabase, ShowCreateFlow, ShowCreateTable, ShowCreateView,
ShowDatabases, ShowFlows, ShowIndex, ShowKind, ShowStatus, ShowTableStatus, ShowTables,
ShowVariables, ShowViews,
};
use crate::statements::tql::Tql;
use crate::statements::truncate::TruncateTable;
@@ -84,6 +85,8 @@ pub enum Statement {
ShowCollation(ShowKind),
// SHOW INDEX
ShowIndex(ShowIndex),
// SHOW CREATE DATABASE
ShowCreateDatabase(ShowCreateDatabase),
// SHOW CREATE TABLE
ShowCreateTable(ShowCreateTable),
// SHOW CREATE FLOW
@@ -139,6 +142,7 @@ impl Display for Statement {
Statement::ShowCreateTable(s) => s.fmt(f),
Statement::ShowCreateFlow(s) => s.fmt(f),
Statement::ShowFlows(s) => s.fmt(f),
Statement::ShowCreateDatabase(s) => s.fmt(f),
Statement::ShowCreateView(s) => s.fmt(f),
Statement::ShowViews(s) => s.fmt(f),
Statement::ShowStatus(s) => s.fmt(f),