33 lines
949 B
Rust
33 lines
949 B
Rust
use std::collections::HashMap;
|
|
|
|
use diesel::{
|
|
pg::{Pg,
|
|
row::{NamedRow, Row},
|
|
QueryableByName,
|
|
};
|
|
|
|
/// Internally a HashMap mapping field names to a custom sum type capable of
|
|
/// deserializing common SQL types. This allows Diesel to load rows without a
|
|
/// hard-coded structure.
|
|
pub struct FlexiRow {
|
|
internal: HashMap<String, FlexiField>,
|
|
}
|
|
|
|
/// Sum type representing a range of SQL data types.
|
|
pub enum FlexiField {
|
|
Text(String),
|
|
Int(i32),
|
|
Unknown,
|
|
}
|
|
|
|
impl QueryableByName<Pg> for FlexiRow {
|
|
fn build<'a>(row: &impl NamedRow<'a, Pg>) -> diesel::deserialize::Result<Self> {
|
|
let mut hm: HashMap<String, FlexiField> = HashMap::new();
|
|
for i in 0..row.field_count() {
|
|
if let Some(field) = diesel::row::Row::<'a, Pg>::get(&row, i) {
|
|
let name = field.field_name().or("Unnamed");
|
|
}
|
|
}
|
|
diesel::deserialize::Result::Ok(FlexiRow { internal: hm })
|
|
}
|
|
}
|