Skip to content

Commit

Permalink
refactoring query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
deeper-x committed Sep 6, 2024
1 parent dfc6dd1 commit d008f57
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/db/dml.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use deadpool_postgres::Client;
use tokio_pg_mapper::FromTokioPostgresRow;
use tokio_postgres::{row, Row};
use tokio_postgres::{Row, Statement};

use crate::{db::models::Ping, settings::errors::MyError};

// retrieve ping records list
pub async fn get_ping_records(client: &Client) -> Result<Vec<Ping>, MyError> {
let _stmt = include_str!("./sql/ping/get_records.sql");
let _stmt = _stmt.replace("$table_fields", &Ping::sql_table_fields());
let stmt = client.prepare(&_stmt).await.unwrap();
let _stmt: &str = include_str!("./sql/ping/get_records.sql");
let stmt: Statement = client.prepare(&_stmt).await.unwrap();

let results = client
let results: Vec<Ping> = client
.query(&stmt, &[])
.await?
.iter()
Expand All @@ -22,14 +21,12 @@ pub async fn get_ping_records(client: &Client) -> Result<Vec<Ping>, MyError> {

// add ping record
pub async fn add_ping_record(client: &Client, ping_info: Ping) -> Result<i64, MyError> {
let _stmt = include_str!("./sql/ping/add_record.sql");
let stmt = client.prepare(&_stmt).await.unwrap();

println!("statement: {:?}", stmt);
let _stmt: &str = include_str!("./sql/ping/add_record.sql");
let stmt: Statement = client.prepare(&_stmt).await.unwrap();

let rows: Vec<Row> = client.query(&stmt, &[&ping_info.value]).await.unwrap();

let idx = rows[0].get(0);
let idx: i64 = rows[0].get(0);

Ok(idx)
}
2 changes: 1 addition & 1 deletion src/db/sql/ping/get_records.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SELECT $table_fields FROM ping;
SELECT id, value, ts_created FROM ping;

0 comments on commit d008f57

Please sign in to comment.