From 0998b64ed479a14c6ea98183488fff1355afc5e4 Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Sun, 22 Aug 2021 14:18:27 +1000 Subject: [PATCH] WIP --- examples/rocket_example/Cargo.toml | 7 +-- examples/rocket_example/src/sqlx/mod.rs | 83 ++++++++++++++++++------- 2 files changed, 61 insertions(+), 29 deletions(-) diff --git a/examples/rocket_example/Cargo.toml b/examples/rocket_example/Cargo.toml index b53c613b..d6293676 100644 --- a/examples/rocket_example/Cargo.toml +++ b/examples/rocket_example/Cargo.toml @@ -10,10 +10,7 @@ rocket = { path = "../../../Rocket/core/lib", features = ["json"] } # "json", # ] } # async-std = { version = "^1.9", features = ["attributes"] } -sea-orm = { path = "../../", features = [ - "sqlx-all", - # "runtime-async-std-native-tls", -] } +sea-orm = { path = "../../", features = ["sqlx-all"] } sea-query = { version = "^0.12.8" } serde_json = { version = "^1" } @@ -32,4 +29,4 @@ features = ["macros", "offline", "migrate"] # features = ["sea_orm"] [dependencies.rocket_db_pools] path = "../../../Rocket/contrib/db_pools/lib" -features = ["sqlx_sqlite"] +features = ["seaorm_sqlx_sqlite"] diff --git a/examples/rocket_example/src/sqlx/mod.rs b/examples/rocket_example/src/sqlx/mod.rs index aec71324..e39b6165 100644 --- a/examples/rocket_example/src/sqlx/mod.rs +++ b/examples/rocket_example/src/sqlx/mod.rs @@ -7,13 +7,15 @@ use rocket_db_pools::{sqlx, Connection, Database}; use futures::{future::TryFutureExt, stream::TryStreamExt}; use sea_orm::entity::*; -use sea_orm::QueryFilter; +use sea_orm::{ + DatabaseBackend, QueryFilter, SqlxSqliteConnector, SqlxSqlitePoolConnection, Statement, +}; mod setup; #[derive(Database, Debug)] #[database("blog")] -struct Db(rocket_db_pools::sqlx::SqlitePool); +struct Db(sea_orm::DatabaseConnection); type Result> = std::result::Result; @@ -21,6 +23,7 @@ type Result> = std::result::Result) -> Result>> { // let ids: Vec = recs.into(); // println!("recs: {:#?}", ids); + println!("db: {:#?}", &*db); + let res = db + .execute(Statement::from_string( + DatabaseBackend::Sqlite, + "SELECT * from posts".to_owned(), + )) + .await; + println!("res: {:#?}", res); - let posts = Post::find().all(&mut *db).await.unwrap(); - assert_eq!(posts.len(), 0); + // let con = SqlxSqliteConnector::from_sqlx_sqlite_pool(db); + // let posts = Post::find().all(&db).await.unwrap(); + // assert_eq!(posts.len(), 0); let ids: Vec = vec![]; Ok(Json(ids)) @@ -172,27 +184,50 @@ pub fn stage() -> AdHoc { .attach(Db::init()) .attach(AdHoc::try_on_ignite("Create init post", |rocket| async { let db = Db::fetch(&rocket).expect("database mounted"); - let res = sqlx::query( - r#" - CREATE TABLE posts ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - title VARCHAR NOT NULL, - text VARCHAR NOT NULL, - published BOOLEAN NOT NULL DEFAULT 0 - )"#, - ) - .execute(&**db) - .await; - println!("res: {:#?}", res); + // let res = sqlx::query( + // r#" + // CREATE TABLE posts ( + // id INTEGER PRIMARY KEY AUTOINCREMENT, + // title VARCHAR NOT NULL, + // text VARCHAR NOT NULL, + // published BOOLEAN NOT NULL DEFAULT 0 + // )"#, + // ) + // .execute(&**db) + // .await; - let res2 = sqlx::query( - r#" - INSERT INTO posts (title, text) VALUES ('a post', 'content of a post') - "#, - ) - .execute(&**db) - .await; - println!("res2: {:#?}", res2); + let create_post_table = db + .execute(Statement::from_string( + DatabaseBackend::Sqlite, + r#" + CREATE TABLE posts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR NOT NULL, + text VARCHAR NOT NULL, + published BOOLEAN NOT NULL DEFAULT 0 + )"# + .to_owned(), + )) + .await; + println!("create_post_table: {:#?}", create_post_table); + + let create_post = db + .execute(Statement::from_string( + DatabaseBackend::Sqlite, + "INSERT INTO posts (title, text) VALUES ('a post', 'content of a post')" + .to_owned(), + )) + .await; + println!("create_post: {:#?}", create_post); + + // let res2 = sqlx::query( + // r#" + // INSERT INTO posts (title, text) VALUES ('a post', 'content of a post') + // "#, + // ) + // .execute(&**db) + // .await; + // println!("res2: {:#?}", res2); // Db::fetch(&rocket) // .run(|db| {