aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2022-03-16 00:43:39 +0100
committerTeddy Wing2022-03-16 00:43:39 +0100
commit99bcc93dc2b1245fb977cb4d04b479652c13f9ac (patch)
treed8c9b09844a222ea8552962ee5880db8e1a5c43f /src
parentdaaab78f45dee52e6950e21cfd4b653cff778a11 (diff)
downloadyaqlite-99bcc93dc2b1245fb977cb4d04b479652c13f9ac.tar.bz2
select(): Trying to convert a SQLite row to `yaml_rust::Yaml`
Still a work in progress. Trying to figure out what makes the most sense for converting between the different types in SQLite and YAML. This code still has some compilation errors.
Diffstat (limited to 'src')
-rw-r--r--src/select.rs23
-rw-r--r--src/yaml/sql.rs27
2 files changed, 49 insertions, 1 deletions
diff --git a/src/select.rs b/src/select.rs
index 1e4d81b..0c61c34 100644
--- a/src/select.rs
+++ b/src/select.rs
@@ -3,6 +3,8 @@ pub fn select(
table_name: &str,
record_id: &str,
) -> yaml_rust::Yaml {
+ use crate::yaml::Yaml;
+
let mut stmt = dbconn.prepare(
&format!(
r#"
@@ -15,13 +17,32 @@ pub fn select(
),
).unwrap();
+ let column_count = stmt.column_count();
+
let rows = stmt.query_map(
rusqlite::named_params! {
":pk_column": "id",
":pk": record_id,
},
|row| {
- Ok(())
+ // let data: [dyn rusqlite::types::FromSql; column_count] = [rusqlite::types::Null; column_count];
+ // let data: Vec<dyn rusqlite::types::FromSql>
+ // = Vec::with_capacity(column_count);
+ // let data = Vec::with_capacity(column_count);
+ let data: Vec<Yaml> = Vec::with_capacity(column_count);
+
+ // for i in 0..=column_count {
+ // data.push(row.get(i));
+ // }
+
+ // TODO: column values must be converted to yaml_rust::Yaml in this
+ // closure.
+
+ for i in 0..=column_count {
+ data.push(row.get(i)?);
+ }
+
+ Ok(data)
},
).unwrap();
diff --git a/src/yaml/sql.rs b/src/yaml/sql.rs
index 68488d2..d065718 100644
--- a/src/yaml/sql.rs
+++ b/src/yaml/sql.rs
@@ -34,3 +34,30 @@ impl<'a> rusqlite::ToSql for Yaml<'a> {
Ok(sql_output)
}
}
+
+impl<'a> rusqlite::types::FromSql for Yaml<'a> {
+ fn column_result(
+ value: rusqlite::types::ValueRef<'_>,
+ ) -> rusqlite::types::FromSqlResult<Self> {
+ use rusqlite::types::ValueRef;
+
+ match value {
+ ValueRef::Integer(i) => Ok(Yaml(&yaml_rust::Yaml::Integer(i))),
+ ValueRef::Real(f) =>
+ Ok(Yaml(&yaml_rust::Yaml::Real(f.to_string()))),
+ ValueRef::Text(_) => {
+ let s = value.as_str()?;
+
+ Ok(Yaml(&yaml_rust::Yaml::String(s.to_owned())))
+ }
+ ValueRef::Blob(_) => {
+ // TODO: How should we handle blobs? Parsing as string might not
+ // make the most sense.
+ let b = value.as_str()?;
+
+ Ok(Yaml(&yaml_rust::Yaml::String(b.to_owned())))
+ }
+ ValueRef::Null => Ok(Yaml(&yaml_rust::Yaml::Null)),
+ }
+ }
+}