Skip to content

Commit

Permalink
Slight refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Sep 6, 2023
1 parent c2a3e38 commit 7c8ea13
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
20 changes: 7 additions & 13 deletions src/mysql/probe.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use sea_query::{Condition, Expr, Iden, Query, SelectStatement, SimpleExpr};
use sea_query::{Condition, Expr, Query, SelectStatement, SimpleExpr};

use super::query::{InformationSchema as Schema, TablesFields};
use super::query::{InformationSchema as Schema, StatisticsFields, TablesFields};
use super::MySql;
use crate::mysql::query::InformationSchema;
use crate::probe::{DatabaseSchema, Has, SchemaProbe};
use crate::probe::{Has, SchemaProbe};

impl SchemaProbe for MySql {
fn get_current_schema() -> SimpleExpr {
Expand All @@ -30,18 +29,13 @@ impl SchemaProbe for MySql {
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
.from((DatabaseSchema::Info, InformationSchema::Statistics))
.from((Schema::Schema, Schema::Statistics))
.cond_where(
Condition::all()
.add(Expr::col(DatabaseSchema::TableSchema).eq(Self::get_current_schema()))
.add(Expr::col(DatabaseSchema::TableName).eq(table.as_ref()))
.add(Expr::col(InternalDatabaseSchema::IndexName).eq(index.as_ref())),
.add(Expr::col(StatisticsFields::TableSchema).eq(Self::get_current_schema()))
.add(Expr::col(StatisticsFields::TableName).eq(table.as_ref()))
.add(Expr::col(StatisticsFields::IndexName).eq(index.as_ref())),
)
.take()
}
}

#[derive(Debug, Iden)]
pub enum InternalDatabaseSchema {
IndexName,
}
22 changes: 6 additions & 16 deletions src/postgres/probe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sea_query::{Alias, Condition, Expr, Iden, Query, SelectStatement, SimpleExpr};
use sea_query::{Condition, Expr, Query, SelectStatement, SimpleExpr};

use super::query::{InformationSchema as Schema, TablesFields};
use super::query::{InformationSchema as Schema, PgIndexes, TablesFields};
use super::Postgres;
use crate::probe::{Has, SchemaProbe};

Expand Down Expand Up @@ -31,23 +31,13 @@ impl SchemaProbe for Postgres {
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
.from(Alias::new("pg_indexes"))
.from(PgIndexes::Table)
.cond_where(
Condition::all()
.add(Expr::col(DatabaseSchema::Schema).eq(Self::get_current_schema()))
.add(Expr::col(DatabaseSchema::Table).eq(table.as_ref()))
.add(Expr::col(DatabaseSchema::Index).eq(index.as_ref())),
.add(Expr::col(PgIndexes::SchemaName).eq(Self::get_current_schema()))
.add(Expr::col(PgIndexes::TableName).eq(table.as_ref()))
.add(Expr::col(PgIndexes::IndexName).eq(index.as_ref())),
)
.take()
}
}

#[derive(Debug, Iden)]
enum DatabaseSchema {
#[iden = "tablename"]
Table,
#[iden = "schemaname"]
Schema,
#[iden = "indexname"]
Index,
}
2 changes: 2 additions & 0 deletions src/postgres/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ pub mod char_set;
pub mod column;
pub mod constraints;
pub mod enumeration;
pub mod pg_indexes;
pub mod schema;
pub mod table;

pub use char_set::*;
pub use column::*;
pub use constraints::*;
pub use enumeration::*;
pub use pg_indexes::*;
pub use schema::*;
pub use table::*;
12 changes: 12 additions & 0 deletions src/postgres/query/pg_indexes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use sea_query::Iden;

#[derive(Debug, Iden)]
pub enum PgIndexes {
Table,
#[iden = "tablename"]
TableName,
#[iden = "schemaname"]
SchemaName,
#[iden = "indexname"]
IndexName,
}
12 changes: 6 additions & 6 deletions src/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait SchemaProbe {
T: AsRef<str>,
{
let mut subquery = Self::query_tables();
subquery.cond_where(Expr::col(DatabaseSchema::TableName).eq(table.as_ref()));
subquery.cond_where(Expr::col(Schema::TableName).eq(table.as_ref()));
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Table)
.from_subquery(subquery, Subquery)
Expand All @@ -24,15 +24,15 @@ pub trait SchemaProbe {
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Column)
.from((DatabaseSchema::Info, DatabaseSchema::Columns))
.from((Schema::Info, Schema::Columns))
.cond_where(
Condition::all()
.add(
Expr::expr(Self::get_current_schema())
.equals((DatabaseSchema::Columns, DatabaseSchema::TableSchema)),
.equals((Schema::Columns, Schema::TableSchema)),
)
.add(Expr::col(DatabaseSchema::TableName).eq(table.as_ref()))
.add(Expr::col(DatabaseSchema::ColumnName).eq(column.as_ref())),
.add(Expr::col(Schema::TableName).eq(table.as_ref()))
.add(Expr::col(Schema::ColumnName).eq(column.as_ref())),
)
.take()
}
Expand All @@ -54,7 +54,7 @@ pub enum Has {
}

#[derive(Debug, Iden)]
pub enum DatabaseSchema {
pub(crate) enum Schema {
#[iden = "information_schema"]
Info,
Columns,
Expand Down
32 changes: 12 additions & 20 deletions src/sqlite/probe.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use sea_query::{Condition, Expr, Iden, IntoTableRef, Query, SelectStatement, SimpleExpr};
use sea_query::{Condition, Expr, Query, SelectStatement, SimpleExpr};

use super::query::SqliteMaster;
use super::query::{SqliteMaster, SqliteSchema};
use super::Sqlite;
use crate::probe::{Has, SchemaProbe};
use crate::probe::{Has, Schema, SchemaProbe};

impl SchemaProbe for Sqlite {
fn get_current_schema() -> SimpleExpr {
Expand All @@ -11,12 +11,12 @@ impl SchemaProbe for Sqlite {

fn query_tables() -> SelectStatement {
Query::select()
.expr_as(Expr::col(Schema::Name), Schema::TableName)
.from(SqliteMaster.into_table_ref())
.expr_as(Expr::col(SqliteSchema::Name), Schema::TableName)
.from(SqliteMaster)
.cond_where(
Condition::all()
.add(Expr::col(Schema::Type).eq("table"))
.add(Expr::col(Schema::Name).ne("sqlite_sequence")),
.add(Expr::col(SqliteSchema::Type).eq("table"))
.add(Expr::col(SqliteSchema::Name).ne("sqlite_sequence")),
)
.take()
}
Expand All @@ -31,7 +31,7 @@ impl SchemaProbe for Sqlite {
"COUNT(*) > 0 AS \"has_column\" FROM pragma_table_info(?)",
[table.as_ref()],
))
.and_where(Expr::col(Schema::Name).eq(column.as_ref()))
.and_where(Expr::col(SqliteSchema::Name).eq(column.as_ref()))
.take()
}

Expand All @@ -42,21 +42,13 @@ impl SchemaProbe for Sqlite {
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
.from(SqliteMaster.into_table_ref())
.from(SqliteMaster)
.cond_where(
Condition::all()
.add(Expr::col(Schema::Type).eq("index"))
.add(Expr::col(Schema::TblName).eq(table.as_ref()))
.add(Expr::col(Schema::Name).eq(index.as_ref())),
.add(Expr::col(SqliteSchema::Type).eq("index"))
.add(Expr::col(SqliteSchema::TblName).eq(table.as_ref()))
.add(Expr::col(SqliteSchema::Name).eq(index.as_ref())),
)
.take()
}
}

#[derive(Debug, Iden)]
enum Schema {
Name,
Type,
TableName,
TblName,
}
10 changes: 10 additions & 0 deletions src/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@ use sea_query::Iden;

#[derive(Debug, Iden)]
pub struct SqliteMaster;

#[derive(Debug, Iden)]
pub enum SqliteSchema {
Type,
Name,
TblName,
#[iden = "rootpage"]
RootPage,
Sql,
}

0 comments on commit 7c8ea13

Please sign in to comment.