Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Fetch SQL queries in Postgres backend #196

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions askar-storage/src/backend/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,58 @@ const COUNT_QUERY: &str = "SELECT COUNT(*) FROM items i
AND (expiry IS NULL OR expiry > CURRENT_TIMESTAMP)";
const DELETE_QUERY: &str = "DELETE FROM items
WHERE profile_id = $1 AND kind = $2 AND category = $3 AND name = $4";
const FETCH_QUERY: &str = "SELECT id, value,
(SELECT ARRAY_TO_STRING(ARRAY_AGG(it.plaintext || ':'
|| ENCODE(it.name, 'hex') || ':' || ENCODE(it.value, 'hex')), ',')
FROM items_tags it WHERE it.item_id = i.id) tags
FROM items i
WHERE profile_id = $1 AND kind = $2 AND category = $3 AND name = $4
AND (expiry IS NULL OR expiry > CURRENT_TIMESTAMP)";
const FETCH_QUERY_UPDATE: &str = "SELECT id, value,
(SELECT ARRAY_TO_STRING(ARRAY_AGG(it.plaintext || ':'
|| ENCODE(it.name, 'hex') || ':' || ENCODE(it.value, 'hex')), ',')
FROM items_tags it WHERE it.item_id = i.id) tags
FROM items i
WHERE profile_id = $1 AND kind = $2 AND category = $3 AND name = $4
AND (expiry IS NULL OR expiry > CURRENT_TIMESTAMP) FOR NO KEY UPDATE";
const FETCH_QUERY: &str = "
SELECT
i.id,
i.value,
ARRAY_TO_STRING(
ARRAY_AGG(
it.plaintext || ':' || ENCODE(it.name, 'hex') || ':' || ENCODE(it.value, 'hex')
),
','
) as tags
FROM
items i
LEFT JOIN
items_tags it
ON it.item_id = i.id
WHERE
i.profile_id = $1
AND i.kind = $2
AND i.category = $3
AND i.name = $4
AND (i.expiry IS NULL OR i.expiry > CURRENT_TIMESTAMP)
GROUP BY
i.id,
i.value";
const FETCH_QUERY_UPDATE: &str = "
WITH fetched_data AS (
SELECT
i.id,
i.value,
ARRAY_TO_STRING(
ARRAY_AGG(it.plaintext || ':' || ENCODE(it.name, 'hex') || ':' || ENCODE(it.value, 'hex')), ','
) as tags
FROM
items i
LEFT JOIN
items_tags it
ON it.item_id = i.id
WHERE
i.profile_id = $1
AND i.kind = $2
AND i.category = $3
AND i.name = $4
AND (i.expiry IS NULL OR i.expiry > CURRENT_TIMESTAMP)
GROUP BY
i.id,
i.value
)
SELECT
*
FROM
fetched_data
FOR NO KEY UPDATE";
const INSERT_QUERY: &str = "INSERT INTO items (profile_id, kind, category, name, value, expiry)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT DO NOTHING RETURNING id";
Expand Down Expand Up @@ -128,7 +166,7 @@ impl Backend for PostgresBackend {
.await?;
let mut conn = self.conn_pool.acquire().await?;
let res = sqlx::query_scalar(
"INSERT INTO profiles (name, profile_key) VALUES ($1, $2)
"INSERT INTO profiles (name, profile_key) VALUES ($1, $2)
ON CONFLICT DO NOTHING RETURNING id",
)
.bind(&name)
Expand Down