Skip to content

Commit

Permalink
use sqlx::FromRow
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoto7250 committed Sep 4, 2022
1 parent 99097fd commit 03939ac
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions examples/sqlx_sqlite/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use chrono::{NaiveDate, NaiveDateTime};
use sea_query::{
time_format, ColumnDef, Expr, Func, Iden, OnConflict, Order, Query, SqliteQueryBuilder, Table,
ColumnDef, Expr, Func, Iden, OnConflict, Order, Query, SqliteQueryBuilder, Table,
};
use sea_query_binder::SqlxBinder;
use serde_json::{json, Value as Json};
use sqlx::{sqlite::SqliteRow, Row, SqlitePool};
use sqlx::{Row, SqlitePool};
use time::{
macros::{date, time},
PrimitiveDateTime,
Expand Down Expand Up @@ -99,14 +99,13 @@ async fn main() {
}
println!();

let rows = sqlx::query_with(&sql, values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values)
.fetch_all(&pool)
.await
.unwrap();
println!("Select one from character:");
for row in rows.iter() {
let item = CharacterStructTime::try_from(row).unwrap();
println!("{:?}", item);
println!("{:?}", row);
}
println!();

Expand Down Expand Up @@ -143,14 +142,13 @@ async fn main() {
for row in rows.iter() {
println!("{:?}\n", row);
}
let rows = sqlx::query_with(&sql, values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values.clone())
.fetch_all(&pool)
.await
.unwrap();
println!("Select one from character:");
for row in rows.iter() {
let item = CharacterStructTime::try_from(row).unwrap();
println!("{:?}", item);
println!("{:?}", row);
}
println!();

Expand Down Expand Up @@ -207,14 +205,13 @@ async fn main() {
println!("{:?}\n", row);
}

let rows = sqlx::query_with(&sql, values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values)
.fetch_all(&pool)
.await
.unwrap();
println!("Select all characters:");
for row in rows.iter() {
let item = CharacterStructTime::try_from(row).unwrap();
println!("{:?}", item);
println!("{:?}", row);
}
println!();

Expand Down Expand Up @@ -251,7 +248,7 @@ struct CharacterStructChrono {
created: NaiveDateTime,
}

#[derive(Debug)]
#[derive(sqlx::FromRow, Debug)]
#[allow(dead_code)]
struct CharacterStructTime {
id: i32,
Expand All @@ -261,20 +258,3 @@ struct CharacterStructTime {
meta: Json,
created: PrimitiveDateTime,
}

impl TryFrom<&SqliteRow> for CharacterStructTime {
type Error = sqlx::Error;

fn try_from(row: &SqliteRow) -> Result<Self, Self::Error> {
let created: String = dbg!(row.try_get("created")?);
let created = PrimitiveDateTime::parse(&created, time_format::FORMAT_DATETIME).unwrap();
Ok(Self {
id: row.try_get("id")?,
uuid: row.try_get("uuid")?,
character: row.try_get("character")?,
font_size: row.try_get("font_size")?,
meta: row.try_get("meta")?,
created,
})
}
}

0 comments on commit 03939ac

Please sign in to comment.