chore: move db statements to another trait
This commit is contained in:
parent
a49b9b4ecb
commit
e81298d1ba
5 changed files with 16 additions and 10 deletions
|
@ -22,15 +22,16 @@ impl OpenDatabaseConnection for MySqlConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DatabaseConnection for MySqlConnection {
|
impl DatabaseStatements for MySqlConnection {
|
||||||
fn migration_table_stmt(&self) -> String {
|
fn create_migration_table_stmt(&self) -> &'static str {
|
||||||
r#"CREATE TABLE IF NOT EXISTS migrations (
|
r#"CREATE TABLE IF NOT EXISTS migrations (
|
||||||
id int AUTO_INCREMENT PRIMARY KEY,
|
id int AUTO_INCREMENT PRIMARY KEY,
|
||||||
name varchar(256) NOT NULL UNIQUE
|
name varchar(256) NOT NULL UNIQUE
|
||||||
)"#
|
)"#
|
||||||
.to_string()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DatabaseConnection for MySqlConnection {
|
||||||
fn batch_execute(&mut self, query: &str) -> StdResult<()> {
|
fn batch_execute(&mut self, query: &str) -> StdResult<()> {
|
||||||
self.client()?.query_drop(query)?;
|
self.client()?.query_drop(query)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -14,15 +14,16 @@ impl OpenDatabaseConnection for PostgresConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DatabaseConnection for PostgresConnection {
|
impl DatabaseStatements for PostgresConnection {
|
||||||
fn migration_table_stmt(&self) -> String {
|
fn create_migration_table_stmt(&self) -> &'static str {
|
||||||
r#"CREATE TABLE IF NOT EXISTS migrations (
|
r#"CREATE TABLE IF NOT EXISTS migrations (
|
||||||
id serial PRIMARY KEY,
|
id serial PRIMARY KEY,
|
||||||
name text NOT NULL UNIQUE
|
name text NOT NULL UNIQUE
|
||||||
)"#
|
)"#
|
||||||
.to_string()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DatabaseConnection for PostgresConnection {
|
||||||
fn batch_execute(&mut self, query: &str) -> StdResult<()> {
|
fn batch_execute(&mut self, query: &str) -> StdResult<()> {
|
||||||
self.client.batch_execute(query)?;
|
self.client.batch_execute(query)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -9,9 +9,11 @@ pub trait OpenDatabaseConnection: Sized {
|
||||||
fn open(connection_string: &str) -> StdResult<Self>;
|
fn open(connection_string: &str) -> StdResult<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait DatabaseConnection {
|
pub trait DatabaseStatements {
|
||||||
fn migration_table_stmt(&self) -> String;
|
fn create_migration_table_stmt(&self) -> &'static str;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait DatabaseConnection: DatabaseStatements {
|
||||||
fn batch_execute(&mut self, query: &str) -> StdResult<()>;
|
fn batch_execute(&mut self, query: &str) -> StdResult<()>;
|
||||||
|
|
||||||
fn execute<'b>(&mut self, query: &str, params: ToSqlParams<'b>) -> StdResult<u64>;
|
fn execute<'b>(&mut self, query: &str, params: ToSqlParams<'b>) -> StdResult<u64>;
|
||||||
|
|
|
@ -104,7 +104,7 @@ impl ManageMigration for MigrationManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()> {
|
fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()> {
|
||||||
let stmt = conn.migration_table_stmt();
|
let stmt = conn.create_migration_table_stmt();
|
||||||
conn.batch_execute(&stmt)
|
conn.batch_execute(&stmt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,9 @@ pub(crate) mod transaction;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use super::adapter::{ToSql, ToSqlParams, TryFromSql};
|
pub use super::adapter::{ToSql, ToSqlParams, TryFromSql};
|
||||||
pub use super::connection::{AnyConnection, DatabaseConnection, OpenDatabaseConnection};
|
pub use super::connection::{
|
||||||
|
AnyConnection, DatabaseConnection, DatabaseStatements, OpenDatabaseConnection,
|
||||||
|
};
|
||||||
pub use super::migration::ManageMigration;
|
pub use super::migration::ManageMigration;
|
||||||
pub use super::transaction::ManageTransaction;
|
pub use super::transaction::ManageTransaction;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue