Skip to content

Commit

Permalink
Support SQLite SeaORM Codegen (#36)
Browse files Browse the repository at this point in the history
* Implement Error & Display for SQLite error

* MySQL & PostgreSQL write to `TableCreateStatement`

* Ignore "sqlite_sequence" table

* Discover timestamp column

* Map text column to string
  • Loading branch information
billy1624 authored Dec 18, 2021
1 parent b1dadb6 commit 10230e0
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 22 deletions.
10 changes: 3 additions & 7 deletions src/mysql/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ pub use table::*;
pub use types::*;

use super::def::Schema;
use sea_query::SchemaStatement;
use sea_query::TableCreateStatement;

impl Schema {
pub fn write(&self) -> Vec<SchemaStatement> {
let mut statements = Vec::new();
for table in self.tables.iter() {
statements.push(SchemaStatement::TableStatement(table.write()));
}
statements
pub fn write(&self) -> Vec<TableCreateStatement> {
self.tables.iter().map(|table| table.write()).collect()
}
}
6 changes: 3 additions & 3 deletions src/mysql/writer/table.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::mysql::def::TableDef;
use sea_query::{Alias, Iden, Table, TableStatement};
use sea_query::{Alias, Iden, Table, TableCreateStatement};

impl TableDef {
pub fn write(&self) -> TableStatement {
pub fn write(&self) -> TableCreateStatement {
let mut table = Table::create();
table.table(Alias::new(self.info.name.as_ref()));
for col in self.columns.iter() {
Expand All @@ -17,7 +17,7 @@ impl TableDef {
for key in self.foreign_keys.iter() {
table.foreign_key(&mut key.write());
}
TableStatement::Create(table)
table
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/postgres/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ pub use table::*;
pub use types::*;

use super::def::Schema;
use sea_query::SchemaStatement;
use sea_query::TableCreateStatement;

impl Schema {
pub fn write(&self) -> Vec<SchemaStatement> {
let mut statements = Vec::new();
for table in self.tables.iter() {
statements.push(SchemaStatement::TableStatement(table.write()));
}
statements
pub fn write(&self) -> Vec<TableCreateStatement> {
self.tables.iter().map(|table| table.write()).collect()
}
}
6 changes: 3 additions & 3 deletions src/postgres/writer/table.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::postgres::def::TableDef;
use sea_query::{Alias, Table, TableStatement};
use sea_query::{Alias, Table, TableCreateStatement};

impl TableDef {
pub fn write(&self) -> TableStatement {
pub fn write(&self) -> TableCreateStatement {
let mut table = Table::create();
table.table(Alias::new(self.info.name.as_ref()));
for col in self.columns.iter() {
Expand All @@ -17,6 +17,6 @@ impl TableDef {
for reference in self.reference_constraints.iter() {
table.foreign_key(&mut reference.write());
}
TableStatement::Create(table)
table
}
}
2 changes: 2 additions & 0 deletions src/sqlite/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl SchemaDiscovery {
.column(Alias::new("name"))
.from(Alias::new("sqlite_master"))
.and_where(Expr::col(Alias::new("type")).eq("table"))
.and_where(Expr::col(Alias::new("name")).ne("sqlite_sequence"))
.to_owned();

let mut tables = Vec::new();
Expand All @@ -50,6 +51,7 @@ impl SchemaDiscovery {
.column(Alias::new("name"))
.from(Alias::new("sqlite_master"))
.and_where(Expr::col(Alias::new("type")).eq("table"))
.and_where(Expr::col(Alias::new("name")).ne("sqlite_sequence"))
.to_owned();

let mut tables = Vec::new();
Expand Down
13 changes: 13 additions & 0 deletions src/sqlite/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,16 @@ impl From<sqlx::Error> for SqliteDiscoveryError {
SqliteDiscoveryError::SqlxError(error)
}
}

impl std::error::Error for SqliteDiscoveryError {}

impl std::fmt::Display for SqliteDiscoveryError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
SqliteDiscoveryError::ParseIntError => write!(f, "Parse Integer Error"),
SqliteDiscoveryError::ParseFloatError => write!(f, "Parse Float Error Error"),
SqliteDiscoveryError::SqlxError(e) => write!(f, "SQLx Error: {}", e),
SqliteDiscoveryError::NoIndexesFound => write!(f, "No Indexes Found Error"),
}
}
}
7 changes: 6 additions & 1 deletion src/sqlite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum Type {
Boolean,
Date,
DateTime,
Timestamp,
}

impl Type {
Expand Down Expand Up @@ -71,6 +72,7 @@ impl Type {
"BOOLEAN" => Type::Boolean,
"DATE" => Type::Date,
"DATETIME" => Type::DateTime,
"TIMESTAMP" => Type::Timestamp,
_ => Type::variable_types(&split_type)?,
};

Expand Down Expand Up @@ -100,7 +102,7 @@ impl Type {
| Self::NvarChar { .. }
| Self::Text
| Self::Clob => {
column_def.text();
column_def.string();
}
Self::Blob => {
column_def.custom(Alias::new("BLOB"));
Expand All @@ -123,6 +125,9 @@ impl Type {
Self::DateTime => {
column_def.date_time();
}
Self::Timestamp => {
column_def.timestamp();
}
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/live/sqlite/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ async fn test_001() -> DiscoveryResult<()> {
.collect::<Vec<_>>(),
vec![
create_table.to_string(SqliteQueryBuilder),
"CREATE TABLE `sqlite_sequence` ( `name` BLOB, `seq` BLOB )".to_owned(),
table_create_supplier_groups.to_string(SqliteQueryBuilder),
table_create_suppliers.to_string(SqliteQueryBuilder),
]
Expand Down

0 comments on commit 10230e0

Please sign in to comment.