Skip to content

Align SQL formatting and add all missing table options #1746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 182 additions & 1 deletion src/ast/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "visitor")]
use sqlparser_derive::{Visit, VisitMut};

use crate::parser::{
Compression, DelayKeyWrite, DirectoryOption, Encryption, InsertMethod, OptionState, RowFormat,
StorageType, TablespaceOption,
};

pub use super::ddl::{ColumnDef, TableConstraint};

use super::{
Expand Down Expand Up @@ -138,6 +143,30 @@ pub struct CreateTable {
pub engine: Option<TableEngine>,
pub comment: Option<CommentDef>,
pub auto_increment_offset: Option<u32>,
pub key_block_size: Option<u32>,
pub max_rows: Option<u32>,
pub min_rows: Option<u32>,
pub autoextend_size: Option<u32>,
pub avg_row_length: Option<u32>,
pub checksum: Option<bool>,
pub connection: Option<String>,
pub engine_attribute: Option<String>,
pub password: Option<String>,
pub secondary_engine_attribute: Option<String>,
pub tablespace_option: Option<TablespaceOption>,
pub row_format: Option<RowFormat>,
pub insert_method: Option<InsertMethod>,
pub compression: Option<Compression>,
pub delay_key_write: Option<DelayKeyWrite>,
pub encryption: Option<Encryption>,
pub pack_keys: Option<OptionState>,
pub stats_auto_recalc: Option<OptionState>,
pub stats_persistent: Option<OptionState>,
pub stats_sample_pages: Option<u32>,
pub start_transaction: Option<bool>,
pub union_tables: Option<Vec<String>>,
pub data_directory: Option<DirectoryOption>,
pub index_directory: Option<DirectoryOption>,
pub default_charset: Option<String>,
pub collation: Option<String>,
pub on_commit: Option<OnCommit>,
Expand Down Expand Up @@ -378,7 +407,7 @@ impl Display for CreateTable {
}

if let Some(auto_increment_offset) = self.auto_increment_offset {
write!(f, " AUTO_INCREMENT {auto_increment_offset}")?;
write!(f, " AUTO_INCREMENT={auto_increment_offset}")?;
}
if let Some(primary_key) = &self.primary_key {
write!(f, " PRIMARY KEY {}", primary_key)?;
Expand Down Expand Up @@ -483,6 +512,158 @@ impl Display for CreateTable {
write!(f, " COLLATE={collation}")?;
}

if let Some(insert_method) = &self.insert_method {
match insert_method {
InsertMethod::No => write!(f, " INSERT_METHOD=NO")?,
InsertMethod::First => write!(f, " INSERT_METHOD=FIRST")?,
InsertMethod::Last => write!(f, " INSERT_METHOD=LAST")?,
}
}

if let Some(key_block_size) = self.key_block_size {
write!(f, " KEY_BLOCK_SIZE={key_block_size}")?;
}

if let Some(row_format) = &self.row_format {
match row_format {
RowFormat::Default => write!(f, " ROW_FORMAT=DEFAULT")?,
RowFormat::Dynamic => write!(f, " ROW_FORMAT=DYNAMIC")?,
RowFormat::Fixed => write!(f, " ROW_FORMAT=FIXED")?,
RowFormat::Compressed => write!(f, " ROW_FORMAT=COMPRESSED")?,
RowFormat::Redundant => write!(f, " ROW_FORMAT=REDUNDANT")?,
RowFormat::Compact => write!(f, " ROW_FORMAT=COMPACT")?,
}
}

if let Some(data_dir) = &self.data_directory {
write!(f, " DATA DIRECTORY='{}'", data_dir.path)?;
}

if let Some(index_dir) = &self.index_directory {
write!(f, " INDEX DIRECTORY='{}'", index_dir.path)?;
}

if let Some(pack_keys) = &self.pack_keys {
match pack_keys {
OptionState::Default => write!(f, " PACK_KEYS=DEFAULT")?,
OptionState::One => write!(f, " PACK_KEYS=1")?,
OptionState::Zero => write!(f, " PACK_KEYS=0")?,
}
}

if let Some(stats_auto_recalc) = &self.stats_auto_recalc {
match stats_auto_recalc {
OptionState::Default => write!(f, " STATS_AUTO_RECALC=DEFAULT")?,
OptionState::One => write!(f, " STATS_AUTO_RECALC=1")?,
OptionState::Zero => write!(f, " STATS_AUTO_RECALC=0")?,
}
}

if let Some(stats_persistent) = &self.stats_persistent {
match stats_persistent {
OptionState::Default => write!(f, " STATS_PERSISTENT=DEFAULT")?,
OptionState::One => write!(f, " STATS_PERSISTENT=1")?,
OptionState::Zero => write!(f, " STATS_PERSISTENT=0")?,
}
}

if let Some(stats_sample_pages) = self.stats_sample_pages {
write!(f, " STATS_SAMPLE_PAGES={stats_sample_pages}")?;
}

if let Some(delay_key_write) = &self.delay_key_write {
match delay_key_write {
DelayKeyWrite::Enabled => write!(f, " DELAY_KEY_WRITE=1")?,
DelayKeyWrite::Disabled => write!(f, " DELAY_KEY_WRITE=0")?,
}
}

if let Some(compression) = &self.compression {
match compression {
Compression::Lz4 => write!(f, " COMPRESSION=LZ4")?,
Compression::Zlib => write!(f, " COMPRESSION=ZLIB")?,
Compression::None => write!(f, " COMPRESSION=NONE")?,
}
}

if let Some(encryption) = &self.encryption {
match encryption {
Encryption::Yes => write!(f, " ENCRYPTION='Y'")?,
Encryption::No => write!(f, " ENCRYPTION='N'")?,
}
}

if let Some(max_rows) = &self.max_rows {
write!(f, " MAX_ROWS={}", max_rows)?;
}

if let Some(min_rows) = &self.min_rows {
write!(f, " MIN_ROWS={}", min_rows)?;
}

if let Some(autoextend_size) = &self.autoextend_size {
write!(f, " AUTOEXTEND_SIZE={}", autoextend_size)?;
}

if let Some(avg_row_length) = &self.avg_row_length {
write!(f, " AVG_ROW_LENGTH={}", avg_row_length)?;
}

if let Some(checksum) = &self.checksum {
match checksum {
true => write!(f, " CHECKSUM=1")?,
false => write!(f, " CHECKSUM=0")?,
}
}

if let Some(connection) = &self.connection {
write!(f, " CONNECTION='{}'", connection)?;
}

if let Some(engine_attribute) = &self.engine_attribute {
write!(f, " ENGINE_ATTRIBUTE='{}'", engine_attribute)?;
}

if let Some(password) = &self.password {
write!(f, " PASSWORD='{}'", password)?;
}

if let Some(secondary_engine_attribute) = &self.secondary_engine_attribute {
write!(
f,
" SECONDARY_ENGINE_ATTRIBUTE='{}'",
secondary_engine_attribute
)?;
}

if self.start_transaction.unwrap_or(false) {
write!(f, " START TRANSACTION")?;
}

if let Some(tablespace_option) = &self.tablespace_option {
write!(f, " TABLESPACE {}", tablespace_option.name)?;
if let Some(storage) = &tablespace_option.storage {
match storage {
StorageType::Disk => write!(f, " STORAGE DISK")?,
StorageType::Memory => write!(f, " STORAGE MEMORY")?,
}
}
}

if let Some(union_tables) = &self.union_tables {
if !union_tables.is_empty() {
write!(
f,
" UNION=({})",
union_tables
.iter()
.map(|table| table.to_string())
.collect::<Vec<String>>()
.join(", ")
)?;
}
}

if self.on_commit.is_some() {
let on_commit = match self.on_commit {
Some(OnCommit::DeleteRows) => "ON COMMIT DELETE ROWS",
Expand Down
Loading