This commit is contained in:
2026-04-24 08:53:40 +02:00
parent a7030d7d0f
commit 54778373b8
31 changed files with 1706 additions and 164 deletions

View File

@@ -0,0 +1,67 @@
// file: kb_lib/src/db/queries/pool_token.rs
//! Queries for `kb_pool_tokens`.
/// Inserts or updates one normalized pool token composition row.
pub async fn upsert_pool_token(
database: &crate::KbDatabase,
dto: &crate::KbPoolTokenDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_pool_tokens (
pool_id,
token_id,
role,
vault_address,
token_order,
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(pool_id, token_id, role) DO UPDATE SET
vault_address = excluded.vault_address,
token_order = excluded.token_order,
updated_at = excluded.updated_at
"#,
)
.bind(dto.pool_id)
.bind(dto.token_id)
.bind(dto.role.to_i16())
.bind(dto.vault_address.clone())
.bind(dto.token_order)
.bind(dto.created_at.to_rfc3339())
.bind(dto.updated_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_pool_tokens on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_pool_tokens
WHERE pool_id = ? AND token_id = ? AND role = ?
LIMIT 1
"#,
)
.bind(dto.pool_id)
.bind(dto.token_id)
.bind(dto.role.to_i16())
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_pool_tokens id on sqlite: {}",
error
))),
}
},
}
}