|
| 1 | +use super::{ |
| 2 | + BinaryAssociationRecord, BinaryRecord, CombiningRule, IdentifierOption, Parameters, PureRecord, |
| 3 | +}; |
| 4 | +use crate::FeosResult; |
| 5 | +use rusqlite::{Connection, ToSql, params_from_iter}; |
| 6 | +use serde::de::DeserializeOwned; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +impl<P: Clone, B: Clone, A: CombiningRule<P> + Clone> Parameters<P, B, A> { |
| 10 | + pub fn from_database<F, S>( |
| 11 | + substances: &[S], |
| 12 | + file: F, |
| 13 | + identifier_option: IdentifierOption, |
| 14 | + ) -> FeosResult<Self> |
| 15 | + where |
| 16 | + F: AsRef<Path>, |
| 17 | + S: ToSql, |
| 18 | + P: DeserializeOwned + Clone, |
| 19 | + B: DeserializeOwned + Clone, |
| 20 | + A: DeserializeOwned + Clone, |
| 21 | + { |
| 22 | + let conn = Connection::open(file)?; |
| 23 | + let pure_records = PureRecord::from_database(substances, &conn, identifier_option)?; |
| 24 | + let binary_records = BinaryRecord::from_database(substances, &conn, identifier_option)?; |
| 25 | + Self::new(pure_records, binary_records) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl<M, A> PureRecord<M, A> { |
| 30 | + pub fn from_database<S>( |
| 31 | + substances: &[S], |
| 32 | + connection: &Connection, |
| 33 | + identifier_option: IdentifierOption, |
| 34 | + ) -> FeosResult<Vec<Self>> |
| 35 | + where |
| 36 | + S: ToSql, |
| 37 | + M: DeserializeOwned, |
| 38 | + A: DeserializeOwned, |
| 39 | + { |
| 40 | + let values = (0..substances.len()) |
| 41 | + .map(|i| format!("({i},?)")) |
| 42 | + .collect::<Vec<_>>() |
| 43 | + .join(","); |
| 44 | + |
| 45 | + let query = format!( |
| 46 | + " |
| 47 | + WITH input(idx, ident) AS ( |
| 48 | + VALUES {values} |
| 49 | + ) |
| 50 | + SELECT pr.pure_record |
| 51 | + FROM input |
| 52 | + JOIN pure_records pr |
| 53 | + ON pr.{identifier_option} = input.ident |
| 54 | + " |
| 55 | + ); |
| 56 | + let mut stmt = connection.prepare(&query)?; |
| 57 | + stmt.query_and_then(params_from_iter(substances), |r| { |
| 58 | + Ok(serde_json::from_str(&r.get::<_, String>("pure_record")?)?) |
| 59 | + })? |
| 60 | + .collect() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<B, A> BinaryRecord<usize, B, A> { |
| 65 | + pub fn from_database<S>( |
| 66 | + substances: &[S], |
| 67 | + connection: &Connection, |
| 68 | + identifier_option: IdentifierOption, |
| 69 | + ) -> FeosResult<Vec<Self>> |
| 70 | + where |
| 71 | + S: ToSql, |
| 72 | + B: DeserializeOwned, |
| 73 | + A: DeserializeOwned, |
| 74 | + { |
| 75 | + let values = (0..substances.len()) |
| 76 | + .map(|i| format!("({i},?)")) |
| 77 | + .collect::<Vec<_>>() |
| 78 | + .join(","); |
| 79 | + |
| 80 | + let query = format!( |
| 81 | + " |
| 82 | + WITH input(idx, ident) AS ( |
| 83 | + VALUES {values} |
| 84 | + ) |
| 85 | + SELECT i1.idx AS comp1, i2.idx AS comp2, br.model_record, br.association_sites |
| 86 | + FROM binary_records br |
| 87 | + JOIN pure_records p1 ON br.id1 = p1.id |
| 88 | + JOIN pure_records p2 ON br.id2 = p2.id |
| 89 | + JOIN input i1 ON p1.{identifier_option} = i1.ident |
| 90 | + JOIN input i2 ON p2.{identifier_option} = i2.ident |
| 91 | + " |
| 92 | + ); |
| 93 | + let mut stmt = connection.prepare(&query)?; |
| 94 | + stmt.query_and_then(params_from_iter(substances), |r| { |
| 95 | + let mut id1: i32 = r.get("comp1")?; |
| 96 | + let mut id2: i32 = r.get("comp2")?; |
| 97 | + let model_record = serde_json::from_str(&r.get::<_, String>("model_record")?)?; |
| 98 | + let mut association_sites: Vec<BinaryAssociationRecord<_>> = |
| 99 | + serde_json::from_str(&r.get::<_, String>("association_sites")?)?; |
| 100 | + if id1 > id2 { |
| 101 | + association_sites |
| 102 | + .iter_mut() |
| 103 | + .for_each(|a| std::mem::swap(&mut a.id1, &mut a.id2)); |
| 104 | + std::mem::swap(&mut id1, &mut id2); |
| 105 | + }; |
| 106 | + |
| 107 | + Ok(BinaryRecord::with_association( |
| 108 | + id1 as usize, |
| 109 | + id2 as usize, |
| 110 | + model_record, |
| 111 | + association_sites, |
| 112 | + )) |
| 113 | + })? |
| 114 | + .collect() |
| 115 | + } |
| 116 | +} |
0 commit comments