This is just awful

Hopefully finding better ways in https://github.com/launchbadge/sqlx/issues/1083
This commit is contained in:
R Tyler Croy 2021-03-03 16:12:17 -08:00
parent 14d78e009f
commit 2ebf41bb31
2 changed files with 22 additions and 10 deletions

4
Cargo.lock generated
View File

@ -292,9 +292,9 @@ dependencies = [
[[package]]
name = "async-h1"
version = "2.3.1"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e9e2a9745d9cd0d92ed7641ce4d07568985762f92633260f0afe8ac7917d9d7"
checksum = "cc5142de15b549749cce62923a50714b0d7b77f5090ced141599e78899865451"
dependencies = [
"async-channel",
"async-dup",

View File

@ -97,15 +97,27 @@ 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 uuids = keys.iter().map(|u| u.to_string()).collect::<Vec<String>>();
Ok(
sqlx::query_as::<sqlx::Sqlite, Self::Value>("SELECT * FROM projects WHERE uuid IN (?)")
.bind(&uuids.join(","))
.fetch(&self.0)
.map_ok(|p: Project| (p.uuid, p))
.try_collect()
.await?,
)
// 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);
}
Ok(out)
}
}