Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Werner Braun committed Jun 3, 2024
1 parent 70a3e72 commit 91618b3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com),
and this project adheres to [Semantic Versioning](https://semver.org).

## Unreleased

## 0.1.1 - 2024-06-03

- small description change

## 0.1.0 - 2024-06-03

- first public version
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.1"
authors = ["Werner Braun <[email protected]>"]
edition = "2021"
#rust-version = "1.78"
description = "Derive macros map struct/field to SQLite table/column. Generate const/fn for create-table,insert,select,update."
description = "Derive map of struct/field to SQLite table/column. Generate const/fn for create-table, insert, select, update."
repository = "https://github.com/wbcat/wb_sqlite"
license = "MIT OR Apache-2.0"
keywords = ["derive", "sqlite", "database", "orm"]
Expand Down
2 changes: 1 addition & 1 deletion src/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn gen_struct(
col_defs.push_str(&ident.to_string());
col_defs.push(' ');
if col_attr.typ.is_empty() {
col_defs.push_str(crate::col_typ(&uf.type_string()));
col_defs.push_str(crate::util::col_typ(&uf.type_string()));
} else {
col_defs.push_str(&col_attr.typ);
}
Expand Down
2 changes: 1 addition & 1 deletion src/create_table_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn gen_struct(
log_values.push_str(&col_name);
col_defs.push(' ');
if col_attr.typ.is_empty() {
col_defs.push_str(crate::col_typ(&uf.type_string()));
col_defs.push_str(crate::util::col_typ(&uf.type_string()));
} else {
col_defs.push_str(&col_attr.typ);
}
Expand Down
16 changes: 0 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,6 @@ pub fn create_table(input: TokenStream) -> TokenStream {
create_table::inner(input).unwrap_or_else(|e| e.into_token_stream())
}

// Get SQLite column datatype from given rust type-string.
fn col_typ(rust_typ: &str) -> &'static str {
match rust_typ {
"bool" | "u8" | "u16" | "u32" | "i8" | "i16" | "i32" | "i64" => "INTEGER NOT NULL",
"f32" | "f64" => "REAL NOT NULL",
"&str" | "String" => "TEXT NOT NULL",
"&[u8]" | "Vec<u8>" => "BLOB NOT NULL",
"Option<bool>" | "Option<u8>" | "Option<u16>" | "Option<u32>" | "Option<i8>"
| "Option<i16>" | "Option<i32>" | "Option<i64>" => "INTEGER",
"Option<f32>" | "Option<f64>" => "REAL",
"Option<String>" => "TEXT",
"Option<Vec<u8>>" => "BLOB",
_ => "ANY",
}
}

/// const CREATE_TABLE_LOG_SQL: &'static str = "CREATE ..."
///
/// Create logging-table + trigger to log all table row modifications to the logging-table.
Expand Down
16 changes: 16 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ pub fn tab_name(ident: &str) -> String {
.to_case(Case::Snake)
}

/// Get SQLite column datatype from given rust type-string.
pub fn col_typ(rust_typ: &str) -> &'static str {
match rust_typ {
"bool" | "u8" | "u16" | "u32" | "i8" | "i16" | "i32" | "i64" => "INTEGER NOT NULL",
"f32" | "f64" => "REAL NOT NULL",
"&str" | "String" => "TEXT NOT NULL",
"&[u8]" | "Vec<u8>" => "BLOB NOT NULL",
"Option<bool>" | "Option<u8>" | "Option<u16>" | "Option<u32>" | "Option<i8>"
| "Option<i16>" | "Option<i32>" | "Option<i64>" => "INTEGER",
"Option<f32>" | "Option<f64>" => "REAL",
"Option<String>" => "TEXT",
"Option<Vec<u8>>" => "BLOB",
_ => "ANY",
}
}

#[derive(Debug, Default)]
pub struct TabAttr {
pub constraint: String, // table-constraint(s)
Expand Down

0 comments on commit 91618b3

Please sign in to comment.