Skip to content

Commit 59833f3

Browse files
committed
Align SQL formatting and add all missing table options to be handled in any order
1 parent 3adc746 commit 59833f3

File tree

9 files changed

+1464
-60
lines changed

9 files changed

+1464
-60
lines changed

src/ast/dml.rs

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ use serde::{Deserialize, Serialize};
2929
#[cfg(feature = "visitor")]
3030
use sqlparser_derive::{Visit, VisitMut};
3131

32+
use crate::parser::{
33+
Compression, DelayKeyWrite, DirectoryOption, Encryption, InsertMethod, OptionState, RowFormat,
34+
StorageType, TablespaceOption,
35+
};
36+
3237
pub use super::ddl::{ColumnDef, TableConstraint};
3338

3439
use super::{
@@ -138,6 +143,30 @@ pub struct CreateTable {
138143
pub engine: Option<TableEngine>,
139144
pub comment: Option<CommentDef>,
140145
pub auto_increment_offset: Option<u32>,
146+
pub key_block_size: Option<u32>,
147+
pub max_rows: Option<u32>,
148+
pub min_rows: Option<u32>,
149+
pub autoextend_size: Option<u32>,
150+
pub avg_row_length: Option<u32>,
151+
pub checksum: Option<bool>,
152+
pub connection: Option<String>,
153+
pub engine_attribute: Option<String>,
154+
pub password: Option<String>,
155+
pub secondary_engine_attribute: Option<String>,
156+
pub tablespace_option: Option<TablespaceOption>,
157+
pub row_format: Option<RowFormat>,
158+
pub insert_method: Option<InsertMethod>,
159+
pub compression: Option<Compression>,
160+
pub delay_key_write: Option<DelayKeyWrite>,
161+
pub encryption: Option<Encryption>,
162+
pub pack_keys: Option<OptionState>,
163+
pub stats_auto_recalc: Option<OptionState>,
164+
pub stats_persistent: Option<OptionState>,
165+
pub stats_sample_pages: Option<u32>,
166+
pub start_transaction: Option<bool>,
167+
pub union_tables: Option<Vec<String>>,
168+
pub data_directory: Option<DirectoryOption>,
169+
pub index_directory: Option<DirectoryOption>,
141170
pub default_charset: Option<String>,
142171
pub collation: Option<String>,
143172
pub on_commit: Option<OnCommit>,
@@ -378,7 +407,7 @@ impl Display for CreateTable {
378407
}
379408

380409
if let Some(auto_increment_offset) = self.auto_increment_offset {
381-
write!(f, " AUTO_INCREMENT {auto_increment_offset}")?;
410+
write!(f, " AUTO_INCREMENT={auto_increment_offset}")?;
382411
}
383412
if let Some(primary_key) = &self.primary_key {
384413
write!(f, " PRIMARY KEY {}", primary_key)?;
@@ -483,6 +512,158 @@ impl Display for CreateTable {
483512
write!(f, " COLLATE={collation}")?;
484513
}
485514

515+
if let Some(insert_method) = &self.insert_method {
516+
match insert_method {
517+
InsertMethod::No => write!(f, " INSERT_METHOD=NO")?,
518+
InsertMethod::First => write!(f, " INSERT_METHOD=FIRST")?,
519+
InsertMethod::Last => write!(f, " INSERT_METHOD=LAST")?,
520+
}
521+
}
522+
523+
if let Some(key_block_size) = self.key_block_size {
524+
write!(f, " KEY_BLOCK_SIZE={key_block_size}")?;
525+
}
526+
527+
if let Some(row_format) = &self.row_format {
528+
match row_format {
529+
RowFormat::Default => write!(f, " ROW_FORMAT=DEFAULT")?,
530+
RowFormat::Dynamic => write!(f, " ROW_FORMAT=DYNAMIC")?,
531+
RowFormat::Fixed => write!(f, " ROW_FORMAT=FIXED")?,
532+
RowFormat::Compressed => write!(f, " ROW_FORMAT=COMPRESSED")?,
533+
RowFormat::Redundant => write!(f, " ROW_FORMAT=REDUNDANT")?,
534+
RowFormat::Compact => write!(f, " ROW_FORMAT=COMPACT")?,
535+
}
536+
}
537+
538+
if let Some(data_dir) = &self.data_directory {
539+
write!(f, " DATA DIRECTORY='{}'", data_dir.path)?;
540+
}
541+
542+
if let Some(index_dir) = &self.index_directory {
543+
write!(f, " INDEX DIRECTORY='{}'", index_dir.path)?;
544+
}
545+
546+
if let Some(pack_keys) = &self.pack_keys {
547+
match pack_keys {
548+
OptionState::Default => write!(f, " PACK_KEYS=DEFAULT")?,
549+
OptionState::One => write!(f, " PACK_KEYS=1")?,
550+
OptionState::Zero => write!(f, " PACK_KEYS=0")?,
551+
}
552+
}
553+
554+
if let Some(stats_auto_recalc) = &self.stats_auto_recalc {
555+
match stats_auto_recalc {
556+
OptionState::Default => write!(f, " STATS_AUTO_RECALC=DEFAULT")?,
557+
OptionState::One => write!(f, " STATS_AUTO_RECALC=1")?,
558+
OptionState::Zero => write!(f, " STATS_AUTO_RECALC=0")?,
559+
}
560+
}
561+
562+
if let Some(stats_persistent) = &self.stats_persistent {
563+
match stats_persistent {
564+
OptionState::Default => write!(f, " STATS_PERSISTENT=DEFAULT")?,
565+
OptionState::One => write!(f, " STATS_PERSISTENT=1")?,
566+
OptionState::Zero => write!(f, " STATS_PERSISTENT=0")?,
567+
}
568+
}
569+
570+
if let Some(stats_sample_pages) = self.stats_sample_pages {
571+
write!(f, " STATS_SAMPLE_PAGES={stats_sample_pages}")?;
572+
}
573+
574+
if let Some(delay_key_write) = &self.delay_key_write {
575+
match delay_key_write {
576+
DelayKeyWrite::Enabled => write!(f, " DELAY_KEY_WRITE=1")?,
577+
DelayKeyWrite::Disabled => write!(f, " DELAY_KEY_WRITE=0")?,
578+
}
579+
}
580+
581+
if let Some(compression) = &self.compression {
582+
match compression {
583+
Compression::Lz4 => write!(f, " COMPRESSION=LZ4")?,
584+
Compression::Zlib => write!(f, " COMPRESSION=ZLIB")?,
585+
Compression::None => write!(f, " COMPRESSION=NONE")?,
586+
}
587+
}
588+
589+
if let Some(encryption) = &self.encryption {
590+
match encryption {
591+
Encryption::Yes => write!(f, " ENCRYPTION='Y'")?,
592+
Encryption::No => write!(f, " ENCRYPTION='N'")?,
593+
}
594+
}
595+
596+
if let Some(max_rows) = &self.max_rows {
597+
write!(f, " MAX_ROWS={}", max_rows)?;
598+
}
599+
600+
if let Some(min_rows) = &self.min_rows {
601+
write!(f, " MIN_ROWS={}", min_rows)?;
602+
}
603+
604+
if let Some(autoextend_size) = &self.autoextend_size {
605+
write!(f, " AUTOEXTEND_SIZE={}", autoextend_size)?;
606+
}
607+
608+
if let Some(avg_row_length) = &self.avg_row_length {
609+
write!(f, " AVG_ROW_LENGTH={}", avg_row_length)?;
610+
}
611+
612+
if let Some(checksum) = &self.checksum {
613+
match checksum {
614+
true => write!(f, " CHECKSUM=1")?,
615+
false => write!(f, " CHECKSUM=0")?,
616+
}
617+
}
618+
619+
if let Some(connection) = &self.connection {
620+
write!(f, " CONNECTION='{}'", connection)?;
621+
}
622+
623+
if let Some(engine_attribute) = &self.engine_attribute {
624+
write!(f, " ENGINE_ATTRIBUTE='{}'", engine_attribute)?;
625+
}
626+
627+
if let Some(password) = &self.password {
628+
write!(f, " PASSWORD='{}'", password)?;
629+
}
630+
631+
if let Some(secondary_engine_attribute) = &self.secondary_engine_attribute {
632+
write!(
633+
f,
634+
" SECONDARY_ENGINE_ATTRIBUTE='{}'",
635+
secondary_engine_attribute
636+
)?;
637+
}
638+
639+
if self.start_transaction.unwrap_or(false) {
640+
write!(f, " START TRANSACTION")?;
641+
}
642+
643+
if let Some(tablespace_option) = &self.tablespace_option {
644+
write!(f, " TABLESPACE {}", tablespace_option.name)?;
645+
if let Some(storage) = &tablespace_option.storage {
646+
match storage {
647+
StorageType::Disk => write!(f, " STORAGE DISK")?,
648+
StorageType::Memory => write!(f, " STORAGE MEMORY")?,
649+
}
650+
}
651+
}
652+
653+
if let Some(union_tables) = &self.union_tables {
654+
if !union_tables.is_empty() {
655+
write!(
656+
f,
657+
" UNION=({})",
658+
union_tables
659+
.iter()
660+
.map(|table| table.to_string())
661+
.collect::<Vec<String>>()
662+
.join(", ")
663+
)?;
664+
}
665+
}
666+
486667
if self.on_commit.is_some() {
487668
let on_commit = match self.on_commit {
488669
Some(OnCommit::DeleteRows) => "ON COMMIT DELETE ROWS",

0 commit comments

Comments
 (0)