浏览代码

Fixed SQL bug

Cesar Rodas 3 月之前
父节点
当前提交
a7234c4585
共有 3 个文件被更改,包括 36 次插入42 次删除
  1. 27 34
      crates/cdk-postgres/src/db.rs
  2. 0 3
      crates/cdk-sql-common/src/mint/auth/mod.rs
  3. 9 5
      crates/cdk-sql-common/src/mint/mod.rs

+ 27 - 34
crates/cdk-postgres/src/db.rs

@@ -1,26 +1,32 @@
 use cdk_common::database::Error;
 use cdk_common::database::Error;
 use cdk_sql_common::stmt::{Column, Statement};
 use cdk_sql_common::stmt::{Column, Statement};
 use futures_util::{pin_mut, TryStreamExt};
 use futures_util::{pin_mut, TryStreamExt};
-use tokio_postgres::Client;
+use tokio_postgres::{error::SqlState, Client, Error as PgError};
 
 
 use crate::value::PgValue;
 use crate::value::PgValue;
 
 
 #[inline(always)]
 #[inline(always)]
+fn to_pgsql_error(err: PgError) -> Error {
+    if let Some(err) = err.as_db_error() {
+        if *err.code() == SqlState::INTEGRITY_CONSTRAINT_VIOLATION {
+            return Error::Duplicate;
+        }
+    }
+
+    Error::Database(Box::new(err))
+}
+
+#[inline(always)]
 pub async fn pg_batch(conn: &Client, statement: Statement) -> Result<(), Error> {
 pub async fn pg_batch(conn: &Client, statement: Statement) -> Result<(), Error> {
     let (sql, _placeholder_values) = statement.to_sql()?;
     let (sql, _placeholder_values) = statement.to_sql()?;
 
 
-    conn.batch_execute(&sql)
-        .await
-        .map_err(|e| Error::Database(Box::new(e)))
+    conn.batch_execute(&sql).await.map_err(to_pgsql_error)
 }
 }
 
 
 #[inline(always)]
 #[inline(always)]
 pub async fn pg_execute(conn: &Client, statement: Statement) -> Result<usize, Error> {
 pub async fn pg_execute(conn: &Client, statement: Statement) -> Result<usize, Error> {
     let (sql, placeholder_values) = statement.to_sql()?;
     let (sql, placeholder_values) = statement.to_sql()?;
-    let prepared_statement = conn
-        .prepare(&sql)
-        .await
-        .map_err(|e| Error::Database(Box::new(e)))?;
+    let prepared_statement = conn.prepare(&sql).await.map_err(to_pgsql_error)?;
 
 
     conn.execute_raw(
     conn.execute_raw(
         &prepared_statement,
         &prepared_statement,
@@ -30,7 +36,7 @@ pub async fn pg_execute(conn: &Client, statement: Statement) -> Result<usize, Er
             .collect::<Vec<PgValue>>(),
             .collect::<Vec<PgValue>>(),
     )
     )
     .await
     .await
-    .map_err(|e| Error::Database(Box::new(e)))
+    .map_err(to_pgsql_error)
     .map(|x| x as usize)
     .map(|x| x as usize)
 }
 }
 
 
@@ -40,10 +46,7 @@ pub async fn pg_fetch_one(
     statement: Statement,
     statement: Statement,
 ) -> Result<Option<Vec<Column>>, Error> {
 ) -> Result<Option<Vec<Column>>, Error> {
     let (sql, placeholder_values) = statement.to_sql()?;
     let (sql, placeholder_values) = statement.to_sql()?;
-    let prepared_statement = conn
-        .prepare(&sql)
-        .await
-        .map_err(|e| Error::Database(Box::new(e)))?;
+    let prepared_statement = conn.prepare(&sql).await.map_err(to_pgsql_error)?;
 
 
     let stream = conn
     let stream = conn
         .query_raw(
         .query_raw(
@@ -54,30 +57,27 @@ pub async fn pg_fetch_one(
                 .collect::<Vec<PgValue>>(),
                 .collect::<Vec<PgValue>>(),
         )
         )
         .await
         .await
-        .map_err(|e| Error::Database(Box::new(e)))?;
+        .map_err(to_pgsql_error)?;
 
 
     pin_mut!(stream);
     pin_mut!(stream);
 
 
     stream
     stream
         .try_next()
         .try_next()
         .await
         .await
-        .map_err(|e| Error::Database(Box::new(e)))?
+        .map_err(to_pgsql_error)?
         .map(|row| {
         .map(|row| {
             (0..row.len())
             (0..row.len())
                 .map(|i| row.try_get::<_, PgValue>(i).map(|value| value.into()))
                 .map(|i| row.try_get::<_, PgValue>(i).map(|value| value.into()))
                 .collect::<Result<Vec<_>, _>>()
                 .collect::<Result<Vec<_>, _>>()
         })
         })
         .transpose()
         .transpose()
-        .map_err(|e| Error::Database(Box::new(e)))
+        .map_err(to_pgsql_error)
 }
 }
 
 
 #[inline(always)]
 #[inline(always)]
 pub async fn pg_fetch_all(conn: &Client, statement: Statement) -> Result<Vec<Vec<Column>>, Error> {
 pub async fn pg_fetch_all(conn: &Client, statement: Statement) -> Result<Vec<Vec<Column>>, Error> {
     let (sql, placeholder_values) = statement.to_sql()?;
     let (sql, placeholder_values) = statement.to_sql()?;
-    let prepared_statement = conn
-        .prepare(&sql)
-        .await
-        .map_err(|e| Error::Database(Box::new(e)))?;
+    let prepared_statement = conn.prepare(&sql).await.map_err(to_pgsql_error)?;
 
 
     let stream = conn
     let stream = conn
         .query_raw(
         .query_raw(
@@ -88,21 +88,17 @@ pub async fn pg_fetch_all(conn: &Client, statement: Statement) -> Result<Vec<Vec
                 .collect::<Vec<PgValue>>(),
                 .collect::<Vec<PgValue>>(),
         )
         )
         .await
         .await
-        .map_err(|e| Error::Database(Box::new(e)))?;
+        .map_err(to_pgsql_error)?;
 
 
     pin_mut!(stream);
     pin_mut!(stream);
 
 
     let mut rows = vec![];
     let mut rows = vec![];
-    while let Some(row) = stream
-        .try_next()
-        .await
-        .map_err(|e| Error::Database(Box::new(e)))?
-    {
+    while let Some(row) = stream.try_next().await.map_err(to_pgsql_error)? {
         rows.push(
         rows.push(
             (0..row.len())
             (0..row.len())
                 .map(|i| row.try_get::<_, PgValue>(i).map(|value| value.into()))
                 .map(|i| row.try_get::<_, PgValue>(i).map(|value| value.into()))
                 .collect::<Result<Vec<_>, _>>()
                 .collect::<Result<Vec<_>, _>>()
-                .map_err(|e| Error::Database(Box::new(e)))?,
+                .map_err(to_pgsql_error)?,
         );
         );
     }
     }
 
 
@@ -112,10 +108,7 @@ pub async fn pg_fetch_all(conn: &Client, statement: Statement) -> Result<Vec<Vec
 #[inline(always)]
 #[inline(always)]
 pub async fn gn_pluck(conn: &Client, statement: Statement) -> Result<Option<Column>, Error> {
 pub async fn gn_pluck(conn: &Client, statement: Statement) -> Result<Option<Column>, Error> {
     let (sql, placeholder_values) = statement.to_sql()?;
     let (sql, placeholder_values) = statement.to_sql()?;
-    let prepared_statement = conn
-        .prepare(&sql)
-        .await
-        .map_err(|e| Error::Database(Box::new(e)))?;
+    let prepared_statement = conn.prepare(&sql).await.map_err(to_pgsql_error)?;
 
 
     let stream = conn
     let stream = conn
         .query_raw(
         .query_raw(
@@ -126,15 +119,15 @@ pub async fn gn_pluck(conn: &Client, statement: Statement) -> Result<Option<Colu
                 .collect::<Vec<PgValue>>(),
                 .collect::<Vec<PgValue>>(),
         )
         )
         .await
         .await
-        .map_err(|e| Error::Database(Box::new(e)))?;
+        .map_err(to_pgsql_error)?;
 
 
     pin_mut!(stream);
     pin_mut!(stream);
 
 
     stream
     stream
         .try_next()
         .try_next()
         .await
         .await
-        .map_err(|e| Error::Database(Box::new(e)))?
+        .map_err(to_pgsql_error)?
         .map(|row| row.try_get::<_, PgValue>(0).map(|value| value.into()))
         .map(|row| row.try_get::<_, PgValue>(0).map(|value| value.into()))
         .transpose()
         .transpose()
-        .map_err(|e| Error::Database(Box::new(e)))
+        .map_err(to_pgsql_error)
 }
 }

+ 0 - 3
crates/cdk-sql-common/src/mint/auth/mod.rs

@@ -325,9 +325,6 @@ where
             return Ok(vec![]);
             return Ok(vec![]);
         }
         }
         let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
         let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
-        if !ys.is_empty() {
-            return Ok(vec![]);
-        }
         let mut current_states = query(r#"SELECT y, state FROM proof WHERE y IN (:ys)"#)?
         let mut current_states = query(r#"SELECT y, state FROM proof WHERE y IN (:ys)"#)?
             .bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
             .bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
             .fetch_all(&*conn)
             .fetch_all(&*conn)

+ 9 - 5
crates/cdk-sql-common/src/mint/mod.rs

@@ -314,10 +314,15 @@ where
     // Get payment IDs and timestamps from the mint_quote_payments table
     // Get payment IDs and timestamps from the mint_quote_payments table
     query(
     query(
         r#"
         r#"
-SELECT payment_id, timestamp, amount
-FROM mint_quote_payments
-WHERE quote_id=:quote_id;
-            "#,
+        SELECT
+            payment_id,
+            timestamp,
+            amount
+        FROM
+            mint_quote_payments
+        WHERE
+            quote_id=:quote_id
+        "#,
     )?
     )?
     .bind("quote_id", quote_id.as_hyphenated().to_string())
     .bind("quote_id", quote_id.as_hyphenated().to_string())
     .fetch_all(conn)
     .fetch_all(conn)
@@ -662,7 +667,6 @@ where
             UPDATE mint_quote
             UPDATE mint_quote
             SET amount_issued = :amount_issued
             SET amount_issued = :amount_issued
             WHERE id = :quote_id
             WHERE id = :quote_id
-            FOR UPDATE
             "#,
             "#,
         )?
         )?
         .bind("amount_issued", new_amount_issued.to_i64())
         .bind("amount_issued", new_amount_issued.to_i64())