Archived
1
0
Fork 0

Merge pull request #4 from pleshevskiy/stmt

chore: move db statements to another trait
This commit is contained in:
Dmitriy Pleshevskiy 2021-04-08 18:04:55 +02:00 committed by GitHub
commit cb1ca43dcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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;
}