Add rocket-mysql driver
This commit is contained in:
parent
f6994477eb
commit
87d4f09931
@ -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"]
|
||||||
|
@ -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"]
|
|
||||||
|
@ -6,32 +6,32 @@ use sea_orm::sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Index, TableCr
|
|||||||
pub use super::post::*;
|
pub use super::post::*;
|
||||||
|
|
||||||
async fn create_table(db: &DbConn, stmt: &TableCreateStatement) -> Result<ExecResult, DbErr> {
|
async fn create_table(db: &DbConn, stmt: &TableCreateStatement) -> Result<ExecResult, DbErr> {
|
||||||
let builder = db.get_database_backend();
|
let builder = db.get_database_backend();
|
||||||
db.execute(builder.build(stmt)).await
|
db.execute(builder.build(stmt)).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_post_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
pub async fn create_post_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
let stmt = sea_query::Table::create()
|
let stmt = sea_query::Table::create()
|
||||||
.table(super::post::Entity)
|
.table(super::post::Entity)
|
||||||
.if_not_exists()
|
.if_not_exists()
|
||||||
.col(
|
.col(
|
||||||
ColumnDef::new(super::post::Column::Id)
|
ColumnDef::new(super::post::Column::Id)
|
||||||
.integer()
|
.integer()
|
||||||
.not_null()
|
.not_null()
|
||||||
.auto_increment()
|
.auto_increment()
|
||||||
.primary_key(),
|
.primary_key(),
|
||||||
)
|
)
|
||||||
.col(
|
.col(
|
||||||
ColumnDef::new(super::post::Column::Title)
|
ColumnDef::new(super::post::Column::Title)
|
||||||
.string()
|
.string()
|
||||||
.not_null(),
|
.not_null(),
|
||||||
)
|
)
|
||||||
.col(
|
.col(
|
||||||
ColumnDef::new(super::post::Column::Text)
|
ColumnDef::new(super::post::Column::Text)
|
||||||
.string()
|
.string()
|
||||||
.not_null(),
|
.not_null(),
|
||||||
)
|
)
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
create_table(db, &stmt).await
|
create_table(db, &stmt).await
|
||||||
}
|
}
|
||||||
|
@ -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")]
|
||||||
|
39
src/driver/rocket_mysql.rs
Normal file
39
src/driver/rocket_mysql.rs
Normal 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(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user