Archived
1
0
Fork 0

chore: move db statements to another trait

This commit is contained in:
Dmitriy Pleshevskiy 2021-04-08 00:49:38 +03:00
parent a49b9b4ecb
commit e81298d1ba
5 changed files with 16 additions and 10 deletions

View file

@ -22,15 +22,16 @@ impl OpenDatabaseConnection for MySqlConnection {
}
}
impl DatabaseConnection for MySqlConnection {
fn migration_table_stmt(&self) -> String {
impl DatabaseStatements for MySqlConnection {
fn create_migration_table_stmt(&self) -> &'static str {
r#"CREATE TABLE IF NOT EXISTS migrations (
id int AUTO_INCREMENT PRIMARY KEY,
name varchar(256) NOT NULL UNIQUE
)"#
.to_string()
}
}
impl DatabaseConnection for MySqlConnection {
fn batch_execute(&mut self, query: &str) -> StdResult<()> {
self.client()?.query_drop(query)?;
Ok(())

View file

@ -14,15 +14,16 @@ impl OpenDatabaseConnection for PostgresConnection {
}
}
impl DatabaseConnection for PostgresConnection {
fn migration_table_stmt(&self) -> String {
impl DatabaseStatements for PostgresConnection {
fn create_migration_table_stmt(&self) -> &'static str {
r#"CREATE TABLE IF NOT EXISTS migrations (
id serial PRIMARY KEY,
name text NOT NULL UNIQUE
)"#
.to_string()
}
}
impl DatabaseConnection for PostgresConnection {
fn batch_execute(&mut self, query: &str) -> StdResult<()> {
self.client.batch_execute(query)?;
Ok(())

View file

@ -9,9 +9,11 @@ pub trait OpenDatabaseConnection: Sized {
fn open(connection_string: &str) -> StdResult<Self>;
}
pub trait DatabaseConnection {
fn migration_table_stmt(&self) -> String;
pub trait DatabaseStatements {
fn create_migration_table_stmt(&self) -> &'static str;
}
pub trait DatabaseConnection: DatabaseStatements {
fn batch_execute(&mut self, query: &str) -> StdResult<()>;
fn execute<'b>(&mut self, query: &str, params: ToSqlParams<'b>) -> StdResult<u64>;

View file

@ -104,7 +104,7 @@ impl ManageMigration for MigrationManager {
}
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)
}

View file

@ -7,7 +7,9 @@ pub(crate) mod transaction;
pub mod prelude {
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::transaction::ManageTransaction;
}