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

issues-383 Rewrite examples using sea-query-binder #387

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 2 additions & 4 deletions examples/sqlx_mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ serde_json = "^1"
rust_decimal = { version = "^1" }
bigdecimal = { version = "^0.3" }
async-std = { version = "1.8", features = [ "attributes" ] }
sea-query = { path = "../../", features = [
sea-query = { path = "../../" }
sea-query-binder = { path = "../../sea-query-binder", features = [
"sqlx-mysql",
"with-chrono",
"with-json",
Expand All @@ -31,9 +32,6 @@ sea-query = { path = "../../", features = [
# "with-time",
# ] }

# To fix sqlx on unstable Rust:
# lexical-core = { version = "0.7.5" }

[dependencies.sqlx]
version = "^0.6"
default-features = false
Expand Down
49 changes: 20 additions & 29 deletions examples/sqlx_mysql/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use bigdecimal::{BigDecimal, FromPrimitive};
use chrono::NaiveDate;
use rust_decimal::Decimal;
use sea_query::{ColumnDef, Expr, Func, Iden, MysqlQueryBuilder, OnConflict, Order, Query, Table};
use sea_query_binder::SqlxBinder;
use sqlx::{types::chrono::NaiveDateTime, MySqlPool, Row};
use time::{
macros::{date, time},
PrimitiveDateTime,
};

sea_query::sea_query_driver_mysql!();
use sea_query_driver_mysql::{bind_query, bind_query_as};
use serde_json::{json, Value as Json};
use uuid::Uuid;

Expand Down Expand Up @@ -87,11 +86,9 @@ async fn main() {
.into(),
date!(2020 - 8 - 20).with_time(time!(0:0:0)).into(),
])
.build(MysqlQueryBuilder);
.build_sqlx(MysqlQueryBuilder);

let result = bind_query(sqlx::query(&sql), &values)
.execute(&mut pool)
.await;
let result = sqlx::query_with(&sql, values).execute(&mut pool).await;
println!("Insert into character: {:?}\n", result);
let id = result.unwrap().last_insert_id();

Expand All @@ -111,9 +108,9 @@ async fn main() {
.from(Character::Table)
.order_by(Character::Id, Order::Desc)
.limit(1)
.build(MysqlQueryBuilder);
.build_sqlx(MysqlQueryBuilder);

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructChrono>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructChrono, _>(&sql, values.clone())
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -123,7 +120,7 @@ async fn main() {
}
println!();

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructTime>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values)
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -139,11 +136,9 @@ async fn main() {
.table(Character::Table)
.values(vec![(Character::FontSize, 24.into())])
.and_where(Expr::col(Character::Id).eq(id))
.build(MysqlQueryBuilder);
.build_sqlx(MysqlQueryBuilder);

let result = bind_query(sqlx::query(&sql), &values)
.execute(&mut pool)
.await;
let result = sqlx::query_with(&sql, values).execute(&mut pool).await;
println!("Update character: {:?}\n", result);

// Read
Expand All @@ -162,9 +157,9 @@ async fn main() {
.from(Character::Table)
.order_by(Character::Id, Order::Desc)
.limit(1)
.build(MysqlQueryBuilder);
.build_sqlx(MysqlQueryBuilder);

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructChrono>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructChrono, _>(&sql, values.clone())
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -174,7 +169,7 @@ async fn main() {
}
println!();

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructTime>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values)
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -196,11 +191,9 @@ async fn main() {
.update_columns([Character::FontSize, Character::Character])
.to_owned(),
)
.build(MysqlQueryBuilder);
.build_sqlx(MysqlQueryBuilder);

let result = bind_query(sqlx::query(&sql), &values)
.execute(&mut pool)
.await;
let result = sqlx::query_with(&sql, values).execute(&mut pool).await;
println!("Insert into character (with upsert): {:?}\n", result);
let id = result.unwrap().last_insert_id();

Expand All @@ -219,9 +212,9 @@ async fn main() {
])
.from(Character::Table)
.order_by(Character::Id, Order::Desc)
.build(MysqlQueryBuilder);
.build_sqlx(MysqlQueryBuilder);

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructChrono>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructChrono, _>(&sql, values.clone())
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -231,7 +224,7 @@ async fn main() {
}
println!();

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructTime>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values)
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -246,9 +239,9 @@ async fn main() {
let (sql, values) = Query::select()
.from(Character::Table)
.expr(Func::count(Expr::col(Character::Id)))
.build(MysqlQueryBuilder);
.build_sqlx(MysqlQueryBuilder);

let row = bind_query(sqlx::query(&sql), &values)
let row = sqlx::query_with(&sql, values)
.fetch_one(&mut pool)
.await
.unwrap();
Expand All @@ -262,11 +255,9 @@ async fn main() {
let (sql, values) = Query::delete()
.from_table(Character::Table)
.and_where(Expr::col(Character::Id).eq(id))
.build(MysqlQueryBuilder);
.build_sqlx(MysqlQueryBuilder);

let result = bind_query(sqlx::query(&sql), &values)
.execute(&mut pool)
.await;
let result = sqlx::query_with(&sql, values).execute(&mut pool).await;
println!("Delete character: {:?}", result);
}

Expand Down
3 changes: 2 additions & 1 deletion examples/sqlx_postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ bigdecimal = { version = "^0.3" }
ipnetwork = { version = "^0.19" }
mac_address = { version = "^1.1" }
async-std = { version = "1.8", features = [ "attributes" ] }
sea-query = { path = "../../", features = [
sea-query = { path = "../../" }
sea-query-binder = { path = "../../sea-query-binder", features = [
"sqlx-postgres",
"with-chrono",
"with-json",
Expand Down
47 changes: 20 additions & 27 deletions examples/sqlx_postgres/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ use rust_decimal::Decimal;
use sea_query::{
ColumnDef, Expr, Func, Iden, OnConflict, Order, PostgresQueryBuilder, Query, Table,
};
use sea_query_binder::SqlxBinder;
use sqlx::{PgPool, Row};
use std::net::{IpAddr, Ipv4Addr};
use time::{
macros::{date, time},
PrimitiveDateTime,
};

sea_query::sea_query_driver_postgres!();
use ipnetwork::IpNetwork;
use mac_address::{get_mac_address, MacAddress};
use sea_query_driver_postgres::{bind_query, bind_query_as};
use serde_json::{json, Value as Json};
use uuid::Uuid;

Expand Down Expand Up @@ -105,9 +104,9 @@ async fn main() {
get_mac_address().unwrap().unwrap().into(),
])
.returning_col(Character::Id)
.build(PostgresQueryBuilder);
.build_sqlx(PostgresQueryBuilder);

let row = bind_query(sqlx::query(&sql), &values)
let row = sqlx::query_with(&sql, values)
.fetch_one(&mut pool)
.await
.unwrap();
Expand All @@ -132,9 +131,9 @@ async fn main() {
.from(Character::Table)
.order_by(Character::Id, Order::Desc)
.limit(1)
.build(PostgresQueryBuilder);
.build_sqlx(PostgresQueryBuilder);

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructChrono>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructChrono, _>(&sql, values.clone())
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -144,7 +143,7 @@ async fn main() {
}
println!();

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructTime>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values)
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -160,11 +159,9 @@ async fn main() {
.table(Character::Table)
.values(vec![(Character::FontSize, 24.into())])
.and_where(Expr::col(Character::Id).eq(id))
.build(PostgresQueryBuilder);
.build_sqlx(PostgresQueryBuilder);

let result = bind_query(sqlx::query(&sql), &values)
.execute(&mut pool)
.await;
let result = sqlx::query_with(&sql, values).execute(&mut pool).await;
println!("Update character: {:?}\n", result);

// Read
Expand All @@ -185,9 +182,9 @@ async fn main() {
.from(Character::Table)
.order_by(Character::Id, Order::Desc)
.limit(1)
.build(PostgresQueryBuilder);
.build_sqlx(PostgresQueryBuilder);

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructChrono>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructChrono, _>(&sql, values.clone())
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -197,7 +194,7 @@ async fn main() {
}
println!();

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructTime>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values)
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -212,9 +209,9 @@ async fn main() {
let (sql, values) = Query::select()
.from(Character::Table)
.expr(Func::count(Expr::col(Character::Id)))
.build(PostgresQueryBuilder);
.build_sqlx(PostgresQueryBuilder);

let row = bind_query(sqlx::query(&sql), &values)
let row = sqlx::query_with(&sql, values)
.fetch_one(&mut pool)
.await
.unwrap();
Expand All @@ -235,11 +232,9 @@ async fn main() {
.update_columns([Character::FontSize, Character::Character])
.to_owned(),
)
.build(PostgresQueryBuilder);
.build_sqlx(PostgresQueryBuilder);

let result = bind_query(sqlx::query(&sql), &values)
.execute(&mut pool)
.await;
let result = sqlx::query_with(&sql, values).execute(&mut pool).await;
println!("Insert into character (with upsert): {:?}\n", result);

// Read
Expand All @@ -259,9 +254,9 @@ async fn main() {
])
.from(Character::Table)
.order_by(Character::Id, Order::Desc)
.build(PostgresQueryBuilder);
.build_sqlx(PostgresQueryBuilder);

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructChrono>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructChrono, _>(&sql, values.clone())
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -271,7 +266,7 @@ async fn main() {
}
println!();

let rows = bind_query_as(sqlx::query_as::<_, CharacterStructTime>(&sql), &values)
let rows = sqlx::query_as_with::<_, CharacterStructTime, _>(&sql, values)
.fetch_all(&mut pool)
.await
.unwrap();
Expand All @@ -286,11 +281,9 @@ async fn main() {
let (sql, values) = Query::delete()
.from_table(Character::Table)
.and_where(Expr::col(Character::Id).eq(id))
.build(PostgresQueryBuilder);
.build_sqlx(PostgresQueryBuilder);

let result = bind_query(sqlx::query(&sql), &values)
.execute(&mut pool)
.await;
let result = sqlx::query_with(&sql, values).execute(&mut pool).await;
println!("Delete character: {:?}", result);
}

Expand Down
3 changes: 2 additions & 1 deletion examples/sqlx_sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ time = { version = "^0.3", features = ["macros"] }
uuid = { version = "^1", features = ["serde", "v4"] }
serde_json = "^1"
async-std = { version = "1.8", features = [ "attributes" ] }
sea-query = { path = "../../", features = [
sea-query-binder = { path = "../../sea-query-binder", features = [
"sqlx-sqlite",
"with-chrono",
"with-json",
"with-uuid",
"with-time",
] }
sea-query = { path = "../../" }
# NOTE: if you are copying this example into your own project, use the following line instead:
# sea-query = { version = "^0", features = [
# "sqlx-sqlite",
Expand Down
Loading