Skip to content

Commit 88b0cdd

Browse files
benrsatoritomershaniii
authored andcommitted
Align SQL formatting and add all missing table options to be handled in any order
1 parent 6ec5223 commit 88b0cdd

9 files changed

+1464
-60
lines changed

src/ast/dml.rs

+182-1
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::{
@@ -157,6 +162,30 @@ pub struct CreateTable {
157162
pub engine: Option<TableEngine>,
158163
pub comment: Option<CommentDef>,
159164
pub auto_increment_offset: Option<u32>,
165+
pub key_block_size: Option<u32>,
166+
pub max_rows: Option<u32>,
167+
pub min_rows: Option<u32>,
168+
pub autoextend_size: Option<u32>,
169+
pub avg_row_length: Option<u32>,
170+
pub checksum: Option<bool>,
171+
pub connection: Option<String>,
172+
pub engine_attribute: Option<String>,
173+
pub password: Option<String>,
174+
pub secondary_engine_attribute: Option<String>,
175+
pub tablespace_option: Option<TablespaceOption>,
176+
pub row_format: Option<RowFormat>,
177+
pub insert_method: Option<InsertMethod>,
178+
pub compression: Option<Compression>,
179+
pub delay_key_write: Option<DelayKeyWrite>,
180+
pub encryption: Option<Encryption>,
181+
pub pack_keys: Option<OptionState>,
182+
pub stats_auto_recalc: Option<OptionState>,
183+
pub stats_persistent: Option<OptionState>,
184+
pub stats_sample_pages: Option<u32>,
185+
pub start_transaction: Option<bool>,
186+
pub union_tables: Option<Vec<String>>,
187+
pub data_directory: Option<DirectoryOption>,
188+
pub index_directory: Option<DirectoryOption>,
160189
pub default_charset: Option<String>,
161190
pub collation: Option<String>,
162191
pub on_commit: Option<OnCommit>,
@@ -397,7 +426,7 @@ impl Display for CreateTable {
397426
}
398427

399428
if let Some(auto_increment_offset) = self.auto_increment_offset {
400-
write!(f, " AUTO_INCREMENT {auto_increment_offset}")?;
429+
write!(f, " AUTO_INCREMENT={auto_increment_offset}")?;
401430
}
402431
if let Some(primary_key) = &self.primary_key {
403432
write!(f, " PRIMARY KEY {}", primary_key)?;
@@ -502,6 +531,158 @@ impl Display for CreateTable {
502531
write!(f, " COLLATE={collation}")?;
503532
}
504533

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

0 commit comments

Comments
 (0)