aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2022-03-10 00:02:40 +0100
committerTeddy Wing2022-03-10 00:03:08 +0100
commitf8fec23264acd576860509d6cf68d49b23330fc3 (patch)
treebc6da56469464eb89baf0be1b8ebf4401db5aaaa
parent06d872db01752763daf4a423806bb5e0cd4bd3f7 (diff)
downloadyaqlite-f8fec23264acd576860509d6cf68d49b23330fc3.tar.bz2
Start encoding SQLite affinity rules
Need to figure out what to do for NUMERIC affinity since Rusqlite doesn't expose a variant for that pseudo-type.
-rw-r--r--src/lib.rs2
-rw-r--r--src/main.rs11
-rw-r--r--src/sqlite.rs25
3 files changed, 33 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 8e0367d..6b1c108 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1 +1 @@
-mod sqlite;
+pub mod sqlite;
diff --git a/src/main.rs b/src/main.rs
index d289b01..e064baa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -31,7 +31,7 @@ fn main() {
fn yaml_extract(
doc: &yaml::Yaml,
tx: &rusqlite::Transaction,
- table_columns: &HashMap<String, String>,
+ table_columns: &HashMap<String, rusqlite::types::Type>,
) {
match doc {
yaml::Yaml::Array(ref array) => {
@@ -71,7 +71,7 @@ struct Zero {}
use std::collections::HashMap;
-fn get_column_names(dbconn: &rusqlite::Connection) -> HashMap<String, String> {
+fn get_column_names(dbconn: &rusqlite::Connection) -> HashMap<String, rusqlite::types::Type> {
let mut column_names = HashMap::new();
let mut stmt = dbconn.prepare(r#"
@@ -103,8 +103,11 @@ fn get_column_names(dbconn: &rusqlite::Connection) -> HashMap<String, String> {
let row = row_result.unwrap();
- // TODO: Translate to rusqlite::types::Type.
- column_names.insert(row.0, row.1);
+ let type_name: String = row.1;
+
+ let type_affinity = yaqlite::sqlite::affinity(&type_name);
+
+ column_names.insert(row.0, type_affinity);
}
column_names
diff --git a/src/sqlite.rs b/src/sqlite.rs
index 10dbb61..8094398 100644
--- a/src/sqlite.rs
+++ b/src/sqlite.rs
@@ -6,4 +6,29 @@ use rusqlite;
/// Use the SQLite rules for type affinity described in:
/// https://sqlite.org/datatype3.html#determination_of_column_affinity
pub fn affinity(type_name: &str) -> rusqlite::types::Type {
+ use rusqlite::types::Type;
+
+ let type_name = type_name.to_uppercase();
+
+ if type_name.contains("INT") {
+ return Type::Integer;
+ } else if type_name.contains("CHAR")
+ || type_name.contains("CLOB")
+ || type_name.contains("TEXT")
+ {
+ return Type::Text;
+ } else if type_name.contains("BLOB")
+ || type_name.is_empty()
+ {
+ return Type::Blob;
+ } else if type_name.contains("REAL")
+ || type_name.contains("FLOA")
+ || type_name.contains("DOUB")
+ {
+ return Type::Real;
+ }
+
+ // TODO: Numeric affinity
+
+ Type::Text
}