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 #423

Merged
Merged
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
12 changes: 6 additions & 6 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
profile: minimal
toolchain: stable
override: true

- uses: actions-rs/cargo@v1
with:
command: build
Expand Down Expand Up @@ -178,7 +178,7 @@ jobs:

binder-build:
name: Build `sea-query-binder`
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -339,7 +339,7 @@ jobs:
sqlite:
name: SQLite
runs-on: ubuntu-latest
needs: ["test", "derive-test"]
needs: ["test", "derive-test", "binder-build"]
strategy:
matrix:
example: [rusqlite, sqlx_sqlite]
Expand All @@ -365,7 +365,7 @@ jobs:
mysql:
name: MySQL
runs-on: ubuntu-latest
needs: ["test", "derive-test"]
needs: ["test", "derive-test", "binder-build"]
strategy:
matrix:
version: [8.0, 5.7]
Expand Down Expand Up @@ -408,7 +408,7 @@ jobs:
mariadb:
name: MariaDB
runs-on: ubuntu-latest
needs: ["test", "derive-test"]
needs: ["test", "derive-test", "binder-build"]
strategy:
matrix:
version: [10.6]
Expand Down Expand Up @@ -451,7 +451,7 @@ jobs:
postgres:
name: PostgreSQL
runs-on: ubuntu-latest
needs: ["test", "derive-test"]
needs: ["test", "derive-test", "binder-build"]
strategy:
matrix:
version: [14.4, 13.7, 12.11]
Expand Down
8 changes: 3 additions & 5 deletions examples/sqlx_mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ 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",
"with-rust_decimal",
"with-bigdecimal",
"with-uuid",
"with-time",
"runtime-async-std-native-tls",
] }
# NOTE: if you are copying this example into your own project, use the following line instead:
# sea-query = { version = "^0", features = [
Expand All @@ -34,14 +36,10 @@ 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
features = [
"runtime-async-std-native-tls",
"macros",
"mysql",
"tls",
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
5 changes: 3 additions & 2 deletions examples/sqlx_postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 All @@ -26,6 +27,7 @@ sea-query = { path = "../../", features = [
"with-time",
"with-ipnetwork",
"with-mac_address",
"runtime-async-std-native-tls",
] }
# NOTE: if you are copying this example into your own project, use the following line instead:
# sea-query = { version = "^0", features = [...] }
Expand All @@ -34,7 +36,6 @@ sea-query = { path = "../../", features = [
version = "^0.6"
default-features = false
features = [
"runtime-async-std-native-tls",
"macros",
"postgres",
"tls",
Expand Down
Loading