Skip to content

Commit

Permalink
Get rarray for queries working
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhofer committed Oct 30, 2024
1 parent 2762921 commit fd7afb4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/models/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::{HashMap, HashSet};
use intervaltree::IntervalTree;
use itertools::Itertools;
use rusqlite::types::Value;
use rusqlite::Params;
use rusqlite::{params_from_iter, Connection};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -146,6 +147,25 @@ impl Path {
rows.next().unwrap().unwrap()
}

pub fn full_query(conn: &Connection, query: &str, params: impl Params) -> Vec<Path> {
let mut stmt = conn.prepare(query).unwrap();
let rows = stmt
.query_map(params, |row| {
let path_id = row.get(0).unwrap();
Ok(Path {
id: path_id,
block_group_id: row.get(1)?,
name: row.get(2)?,
})
})
.unwrap();
let mut paths = vec![];
for row in rows {
paths.push(row.unwrap());
}
paths
}

pub fn get_paths(conn: &Connection, query: &str, placeholders: Vec<Value>) -> Vec<Path> {
let mut stmt = conn.prepare(query).unwrap();
let rows = stmt
Expand Down
17 changes: 8 additions & 9 deletions src/updates/fasta.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use noodles::fasta;
use rusqlite;
use rusqlite::{types::Value as SQLValue, Connection};
use std::{io, str};
use std::{io, rc::Rc, str};

use crate::models::{
block_group::{BlockGroup, PathChange},
Expand Down Expand Up @@ -46,17 +47,15 @@ pub fn update_with_fasta(

let path_ids = operation_paths
.iter()
.map(|operation_path| operation_path.path_id.to_string())
.collect::<Vec<String>>();
let query = format!(
"select * from path where block_group_id = {block_group_id} and id in ({path_ids});",
path_ids = path_ids.join(", ")
);
let paths = Path::query(conn, &query, vec![]);
.map(|operation_path| SQLValue::from(operation_path.path_id))
.collect::<Vec<SQLValue>>();
let query = "select * from path where block_group_id = ?1 and id in rarray(?2);";
let params = rusqlite::params!(SQLValue::from(block_group_id), Rc::new(path_ids));
let paths = Path::full_query(conn, query, params);

if paths.len() != 1 {
panic!(
"Expected one path for block group {} (contig name: {}",
"Expected one path for block group {} (contig name: {})",
block_group_id, contig_name
);
}
Expand Down

0 comments on commit fd7afb4

Please sign in to comment.