Skip to content
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
21 changes: 20 additions & 1 deletion src/backend/table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ pub trait TableBuilder: IndexBuilder + ForeignKeyBuilder + QuotedBuilder + Table
create: &TableCreateStatement,
sql: &mut dyn SqlWriter,
) {
write!(sql, "CREATE TABLE ").unwrap();
if let Some(modifier) = create.modifier.as_ref() {
write!(sql, "CREATE ").unwrap();

if modifier.virtual_table.is_some() {
write!(sql, "VIRTUAL ").unwrap();
}

write!(sql, "TABLE ").unwrap();
} else {
write!(sql, "CREATE TABLE ").unwrap();
}

if create.if_not_exists {
write!(sql, "IF NOT EXISTS ").unwrap();
Expand All @@ -17,6 +27,15 @@ pub trait TableBuilder: IndexBuilder + ForeignKeyBuilder + QuotedBuilder + Table
self.prepare_table_ref_table_stmt(table_ref, sql);
}

if let Some(virtual_table_extension) = create
.modifier
.as_ref()
.map(|modifier| modifier.virtual_table.as_ref())
.flatten()
{
write!(sql, " USING {}", virtual_table_extension).unwrap();
}

write!(sql, " ( ").unwrap();
let mut count = 0;

Expand Down
30 changes: 30 additions & 0 deletions src/table/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ use crate::{
#[derive(Debug, Clone)]
pub struct TableCreateStatement {
pub(crate) table: Option<TableRef>,
pub(crate) modifier: Option<TableModifier>,
pub(crate) columns: Vec<ColumnDef>,
pub(crate) options: Vec<TableOpt>,
pub(crate) partitions: Vec<TablePartition>,
Expand All @@ -86,6 +87,28 @@ pub struct TableCreateStatement {
pub(crate) if_not_exists: bool,
}

#[derive(Debug, Default, Clone)]
pub struct TableModifier {
pub(crate) virtual_table: Option<String>,
}

impl TableModifier {
pub fn new() -> Self {
Self::default()
}

pub fn virtual_table(&mut self, extension: impl Into<String>) -> &mut Self {
self.virtual_table = Some(extension.into());
self
}

pub fn take(&mut self) -> Self {
Self {
virtual_table: self.virtual_table.take(),
}
}
}

/// All available table options
#[derive(Debug, Clone)]
pub enum TableOpt {
Expand All @@ -109,6 +132,7 @@ impl TableCreateStatement {
pub fn new() -> Self {
Self {
table: None,
modifier: None,
columns: Vec::new(),
options: Vec::new(),
partitions: Vec::new(),
Expand All @@ -124,6 +148,11 @@ impl TableCreateStatement {
self
}

pub fn modifier(&mut self, modifier: TableModifier) -> &mut Self {
self.modifier = Some(modifier);
self
}

/// Set table name
pub fn table<T>(&mut self, table: T) -> &mut Self
where
Expand Down Expand Up @@ -276,6 +305,7 @@ impl TableCreateStatement {
pub fn take(&mut self) -> Self {
Self {
table: self.table.take(),
modifier: self.modifier.take(),
columns: std::mem::take(&mut self.columns),
options: std::mem::take(&mut self.options),
partitions: std::mem::take(&mut self.partitions),
Expand Down
17 changes: 17 additions & 0 deletions tests/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,23 @@ fn create_with_unique_index_constraint() {
);
}

#[test]
fn create_virtual_table() {
assert_eq!(
Table::create()
.table(Font::Table)
.if_not_exists()
.modifier(TableModifier::new().virtual_table("fts5").take())
.col(ColumnDef::new(Font::Name).text().not_null())
.to_string(SqliteQueryBuilder),
[
r#"CREATE VIRTUAL TABLE IF NOT EXISTS "font" USING fts5 ("#,
r#""name" text NOT NULL"#,
r#")"#,
].join(" ")
);
}

#[test]
fn drop_1() {
assert_eq!(
Expand Down