Skip to content

Commit

Permalink
More probes
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed May 9, 2022
1 parent b3e59e9 commit 51042aa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/mysql/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ impl SchemaProbe for MySql {
}

fn query_tables() -> SelectStatement {
let mut stmt = Query::select();
let (expr, tbl_ref, condition) = (
Expr::col(Alias::new("table_name")),
(Alias::new("information_schema"), Alias::new("tables")).into_table_ref(),
Expand All @@ -18,9 +17,10 @@ impl SchemaProbe for MySql {
.equals(Alias::new("tables"), Alias::new("table_schema")),
),
);
stmt.expr_as(expr, Alias::new("table_name"))
Query::select()
.expr_as(expr, Alias::new("table_name"))
.from(tbl_ref)
.cond_where(condition);
stmt
.cond_where(condition)
.take()
}
}
8 changes: 4 additions & 4 deletions src/postgres/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ impl SchemaProbe for Postgres {
}

fn query_tables() -> SelectStatement {
let mut stmt = Query::select();
let (expr, tbl_ref, condition) = (
Expr::col(Alias::new("table_name")),
(Alias::new("information_schema"), Alias::new("tables")).into_table_ref(),
Expand All @@ -20,9 +19,10 @@ impl SchemaProbe for Postgres {
)
.add(Expr::col(Alias::new("table_type")).eq("BASE TABLE")),
);
stmt.expr_as(expr, Alias::new("table_name"))
Query::select()
.expr_as(expr, Alias::new("table_name"))
.from(tbl_ref)
.cond_where(condition);
stmt
.cond_where(condition)
.take()
}
}
34 changes: 33 additions & 1 deletion src/probe.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
use sea_query::{SelectStatement, SimpleExpr};
use sea_query::{Alias, Condition, Expr, Query, SelectStatement, SimpleExpr};

pub trait SchemaProbe {
fn get_current_schema() -> SimpleExpr;

fn query_tables() -> SelectStatement;

fn has_table<T>(&self, table: T) -> SelectStatement
where
T: AsRef<str>,
{
let mut subquery = Self::query_tables();
subquery.cond_where(Expr::col(Alias::new("table_name")).eq(table.as_ref()));
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Alias::new("has_table"))
.from_subquery(subquery, Alias::new("subquery"))
.take()
}

fn has_column<T, C>(table: T, column: C) -> SelectStatement
where
T: AsRef<str>,
C: AsRef<str>,
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Alias::new("has_column"))
.from((Alias::new("information_schema"), Alias::new("columns")))
.cond_where(
Condition::all()
.add(
Expr::expr(Self::get_current_schema())
.equals(Alias::new("columns"), Alias::new("table_schema")),
)
.add(Expr::col(Alias::new("table_name")).eq(table.as_ref()))
.add(Expr::col(Alias::new("column_name")).eq(column.as_ref())),
)
.take()
}
}
7 changes: 4 additions & 3 deletions src/sqlite/def/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl TableDef {

Ok(self)
}

/// Get a list of all the indexes in the table.
/// Note that this does not get the column name mapped by the index.
/// To get the column name mapped by the index, the `self.get_single_indexinfo` method is invoked
Expand All @@ -70,7 +71,7 @@ impl TableDef {
indexes: &mut Vec<IndexInfo>,
) -> DiscoveryResult<()> {
let mut index_query = String::default();
index_query.push_str("pragma index_list('");
index_query.push_str("PRAGMA index_list('");
index_query.push_str(&self.name);
index_query.push_str("')");

Expand Down Expand Up @@ -107,7 +108,7 @@ impl TableDef {
/// Get a list of all the foreign keys in the table
pub async fn get_foreign_keys(&mut self, executor: &Executor) -> DiscoveryResult<&mut Self> {
let mut index_query = String::default();
index_query.push_str("pragma foreign_key_list('");
index_query.push_str("PRAGMA foreign_key_list('");
index_query.push_str(&self.name);
index_query.push_str("')");

Expand All @@ -125,7 +126,7 @@ impl TableDef {
/// Get a list of all the columns in the table mapped as [ColumnInfo]
pub async fn get_column_info(&mut self, executor: &Executor) -> DiscoveryResult<&TableDef> {
let mut index_query = String::default();
index_query.push_str("pragma table_info('");
index_query.push_str("PRAGMA table_info('");
index_query.push_str(&self.name);
index_query.push_str("')");

Expand Down
22 changes: 18 additions & 4 deletions src/sqlite/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,31 @@ impl SchemaProbe for Sqlite {
}

fn query_tables() -> SelectStatement {
let mut stmt = Query::select();
let (expr, tbl_ref, condition) = (
Expr::col(Alias::new("name")),
Alias::new("sqlite_master").into_table_ref(),
Condition::all()
.add(Expr::col(Alias::new("type")).eq("table"))
.add(Expr::col(Alias::new("name")).ne("sqlite_sequence")),
);
stmt.expr_as(expr, Alias::new("table_name"))
Query::select()
.expr_as(expr, Alias::new("table_name"))
.from(tbl_ref)
.cond_where(condition);
stmt
.cond_where(condition)
.take()
}

fn has_column<T, C>(table: T, column: C) -> SelectStatement
where
T: AsRef<str>,
C: AsRef<str>,
{
Query::select()
.expr(Expr::cust_with_values(
"COUNT(*) > 0 AS \"has_column\" FROM pragma_table_info(?)",
[table.as_ref()],
))
.and_where(Expr::col(Alias::new("name")).eq(column.as_ref()))
.take()
}
}

0 comments on commit 51042aa

Please sign in to comment.