Finally fiddled around enough to make this work

This commit is contained in:
R Tyler Croy 2021-03-03 21:46:13 -08:00
parent 2ebf41bb31
commit 102ded9756
2 changed files with 16 additions and 19 deletions

View File

@ -5,7 +5,7 @@
CREATE TABLE IF NOT EXISTS projects
(
uuid TEXT PRIMARY KEY NOT NULL,
uuid BLOB PRIMARY KEY NOT NULL,
path TEXT UNIQUE NOT NULL,
title TEXT NOT NULL,
description TEXT,

View File

@ -97,27 +97,24 @@ impl Loader<Uuid> for ProjectLoader {
type Error = FieldError;
async fn load(&self, keys: &[Uuid]) -> Result<HashMap<Uuid, Self::Value>, Self::Error> {
use std::str::FromStr;
use sqlx::Row;
let query = format!(
"SELECT * FROM projects WHERE uuid IN ({})",
(0..keys.len())
.map(|_| "?")
.collect::<Vec<&str>>()
.join(",")
);
let uuids = keys.iter().map(|u| u.to_string()).collect::<Vec<String>>();
// Doing awful things to allow the bulk query with the Uuid since sqlx cannot map a
// hyphenated string back out to a normal Uuid
let mut records = sqlx::query("SELECT * FROM projects WHERE uuid IN ($1)")
.bind(uuids.join(","))
.fetch(&self.0);
let mut out = HashMap::new();
while let Some(row) = records.try_next().await? {
let project = Project {
uuid: Uuid::from_str(row.get("uuid"))?,
title: row.get("title"),
path: row.get("path"),
};
out.insert(project.uuid, project);
let mut q = sqlx::query_as::<sqlx::Sqlite, Project>(&query);
for x in (0..keys.len()) {
q = q.bind(keys[x]);
}
debug!("query: {}", query);
Ok(out)
Ok(q.fetch(&self.0)
.map_ok(|p: Project| (p.uuid, p))
.try_collect()
.await?)
}
}