Skip to content

Commit 3ba9af9

Browse files
committed
Add custom sqlite type names
1 parent 19d620d commit 3ba9af9

1 file changed

Lines changed: 58 additions & 9 deletions

File tree

modules/fdb/src/sqlite.rs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,63 @@ impl<'a> ToSql for Field<'a> {
2626
}
2727
}
2828

29+
impl ValueType {
30+
/// Get the canonical SQLite name of this data type
31+
pub fn to_sqlite_type(self) -> &'static str {
32+
match self {
33+
ValueType::Nothing => "BLOB_NONE",
34+
ValueType::Integer => "INT32",
35+
ValueType::Float => "REAL",
36+
ValueType::Text => "TEXT4",
37+
ValueType::Boolean => "INT_BOOL",
38+
ValueType::BigInt => "INT64",
39+
ValueType::VarChar => "TEXT_XML",
40+
}
41+
}
42+
43+
/// Take an SQLite column declaration type and guess the ValueType
44+
///
45+
/// This function does a proper round-trip with `to_sqlite_type`
46+
///
47+
/// ```
48+
/// use assembly_fdb::common::ValueType;
49+
///
50+
/// fn round_trip(v: ValueType) -> Option<ValueType> {
51+
/// ValueType::from_sqlite_type(v.to_sqlite_type())
52+
/// }
53+
///
54+
/// // Check whether the round-trip works
55+
/// assert_eq!(round_trip(ValueType::Nothing), Some(ValueType::Nothing));
56+
/// assert_eq!(round_trip(ValueType::Integer), Some(ValueType::Integer));
57+
/// assert_eq!(round_trip(ValueType::Float), Some(ValueType::Float));
58+
/// assert_eq!(round_trip(ValueType::Text), Some(ValueType::Text));
59+
/// assert_eq!(round_trip(ValueType::Boolean), Some(ValueType::Boolean));
60+
/// assert_eq!(round_trip(ValueType::BigInt), Some(ValueType::BigInt));
61+
/// assert_eq!(round_trip(ValueType::VarChar), Some(ValueType::VarChar));
62+
///
63+
/// // Check whether lcdr's names work
64+
/// assert_eq!(ValueType::from_sqlite_type("none"), Some(ValueType::Nothing));
65+
/// assert_eq!(ValueType::from_sqlite_type("int32"), Some(ValueType::Integer));
66+
/// assert_eq!(ValueType::from_sqlite_type("real"), Some(ValueType::Float));
67+
/// assert_eq!(ValueType::from_sqlite_type("text_4"), Some(ValueType::Text));
68+
/// assert_eq!(ValueType::from_sqlite_type("int_bool"), Some(ValueType::Boolean));
69+
/// assert_eq!(ValueType::from_sqlite_type("int64"), Some(ValueType::BigInt));
70+
/// assert_eq!(ValueType::from_sqlite_type("text_8"), Some(ValueType::VarChar));
71+
/// ```
72+
pub fn from_sqlite_type(decl_type: &str) -> Option<Self> {
73+
match decl_type {
74+
"BLOB_NONE" | "blob_none" | "none" | "NULL" => Some(ValueType::Nothing),
75+
"INT32" | "int32" | "TINYINT" | "SMALLINT" => Some(ValueType::Integer),
76+
"real" | "REAL" | "FLOAT" => Some(ValueType::Float),
77+
"TEXT4" | "text_4" | "TEXT" => Some(ValueType::Text),
78+
"BIT" | "INT_BOOL" | "int_bool" => Some(ValueType::Boolean),
79+
"INT64" | "int64" | "BIGINT" | "INTEGER" => Some(ValueType::BigInt),
80+
"XML" | "TEXT_XML" | "xml" | "text_8" | "text_xml" | "VARCHAR" => Some(ValueType::VarChar),
81+
_ => None,
82+
}
83+
}
84+
}
85+
2986
/// Try to export a database to a SQL connection
3087
///
3188
/// This function does the following:
@@ -52,15 +109,7 @@ pub fn try_export_db(conn: &mut Connection, db: Database) -> rusqlite::Result<()
52109
writeln!(create_query, ",").unwrap();
53110
write!(insert_query, ", ").unwrap();
54111
}
55-
let typ = match col.value_type() {
56-
ValueType::Nothing => "NULL",
57-
ValueType::Integer => "INTEGER",
58-
ValueType::Float => "REAL",
59-
ValueType::Text => "TEXT",
60-
ValueType::Boolean => "INTEGER",
61-
ValueType::BigInt => "INTEGER",
62-
ValueType::VarChar => "BLOB",
63-
};
112+
let typ = col.value_type().to_sqlite_type();
64113
write!(create_query, " [{}] {}", col.name(), typ).unwrap();
65114
write!(insert_query, "[{}]", col.name()).unwrap();
66115
}

0 commit comments

Comments
 (0)