Add rocket-mysql driver

This commit is contained in:
Billy Chan 2021-08-25 23:05:30 +08:00
parent f6994477eb
commit 87d4f09931
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
6 changed files with 75 additions and 41 deletions

View File

@ -40,6 +40,8 @@ sqlx-core = { version = "^0.5", optional = true }
sqlx-macros = { version = "^0.5", optional = true } sqlx-macros = { version = "^0.5", optional = true }
serde_json = { version = "^1", optional = true } serde_json = { version = "^1", optional = true }
uuid = { version = "0.8", features = ["serde", "v4"], optional = true } uuid = { version = "0.8", features = ["serde", "v4"], optional = true }
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", features = ["json"], optional = true }
rocket_db_pools = { git = "https://github.com/SergioBenitez/Rocket.git", features = ["sqlx_mysql"], optional = true }
[dev-dependencies] [dev-dependencies]
smol = { version = "^1.2" } smol = { version = "^1.2" }
@ -91,3 +93,4 @@ runtime-actix-rustls = ["sqlx/runtime-actix-rustls", "runtime-actix"]
runtime-tokio = [] runtime-tokio = []
runtime-tokio-native-tls = ["sqlx/runtime-tokio-native-tls", "runtime-tokio"] runtime-tokio-native-tls = ["sqlx/runtime-tokio-native-tls", "runtime-tokio"]
runtime-tokio-rustls = ["sqlx/runtime-tokio-rustls", "runtime-tokio"] runtime-tokio-rustls = ["sqlx/runtime-tokio-rustls", "runtime-tokio"]
rocket-mysql = ["rocket", "rocket_db_pools"]

View File

@ -5,12 +5,9 @@ edition = "2018"
publish = false publish = false
[workspace] [workspace]
[dependencies] [dependencies]
rocket = { path = "../../../Rocket/core/lib", features = ["json"] } rocket = { git = "https://github.com/SergioBenitez/Rocket.git", features = ["json"] }
# rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "master", features = [ rocket_db_pools = { git = "https://github.com/SergioBenitez/Rocket.git", features = ["sqlx_mysql"] }
# "json", sea-orm = { path = "../../", features = ["sqlx-all", "rocket-mysql"] }
# ] }
# async-std = { version = "^1.9", features = ["attributes"] }
sea-orm = { path = "../../", features = ["sqlx-all"] }
sea-query = { version = "^0.12.8" } sea-query = { version = "^0.12.8" }
serde_json = { version = "^1" } serde_json = { version = "^1" }
@ -22,13 +19,3 @@ futures-util = { version = "^0.3" }
version = "0.5.1" version = "0.5.1"
default-features = false default-features = false
features = ["macros", "offline", "migrate"] features = ["macros", "offline", "migrate"]
# [dependencies.rocket_db_pools]
# git = "https://github.com/SergioBenitez/Rocket"
# branch = "master"
# features = ["sea_orm"]
[dependencies.rocket_db_pools]
path = "../../../Rocket/contrib/db_pools/lib"
# git = "https://github.com/samsamai/Rocket.git"
# branch = "ss/seaorm-contrib"
features = ["seaorm_sqlx_sqlite"]

View File

@ -9,8 +9,13 @@ mod sqlx_postgres;
#[cfg(feature = "sqlx-sqlite")] #[cfg(feature = "sqlx-sqlite")]
mod sqlx_sqlite; mod sqlx_sqlite;
#[cfg(feature = "rocket-mysql")]
mod rocket_mysql;
#[cfg(feature = "mock")] #[cfg(feature = "mock")]
pub use mock::*; pub use mock::*;
#[cfg(feature = "rocket-mysql")]
pub use rocket_mysql::*;
#[cfg(feature = "sqlx-dep")] #[cfg(feature = "sqlx-dep")]
pub use sqlx_common::*; pub use sqlx_common::*;
#[cfg(feature = "sqlx-mysql")] #[cfg(feature = "sqlx-mysql")]

View File

@ -0,0 +1,39 @@
use rocket::figment::Figment;
use rocket_db_pools::{Config, Error};
#[rocket::async_trait]
impl rocket_db_pools::Pool for crate::Database {
type Error = crate::DbErr;
type Connection = crate::DatabaseConnection;
async fn init(figment: &Figment) -> Result<Self, Self::Error> {
// let config = figment.extract::<Config>()?;
// let mut opts = config.url.parse::<Options<D>>().map_err(Error::Init)?;
// opts.disable_statement_logging();
// specialize(&mut opts, &config);
// sqlx::pool::PoolOptions::new()
// .max_connections(config.max_connections as u32)
// .connect_timeout(Duration::from_secs(config.connect_timeout))
// .idle_timeout(config.idle_timeout.map(Duration::from_secs))
// .min_connections(config.min_connections.unwrap_or_default())
// .connect_with(opts)
// .await
// .map_err(Error::Init)
Ok(crate::Database {})
}
async fn get(&self) -> Result<Self::Connection, Self::Error> {
// self.acquire().await.map_err(Error::Get)
// let con = crate::Database::connect("sqlite::memory:").await;
// Ok(crate::Database::connect("sqlite::memory:").await.unwrap())
// "mysql://root:@localhost"
Ok(
crate::Database::connect("mysql://root:@localhost/rocket_example")
.await
.unwrap(),
)
}
}

View File

@ -1,6 +1,6 @@
use std::str::FromStr;
use crate::{EntityName, IdenStatic, Iterable}; use crate::{EntityName, IdenStatic, Iterable};
use sea_query::{DynIden, Expr, SeaRc, SelectStatement, SimpleExpr, Value}; use sea_query::{DynIden, Expr, SeaRc, SelectStatement, SimpleExpr, Value};
use std::str::FromStr;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ColumnDef { pub struct ColumnDef {