Skip to content

Commit

Permalink
MySQL: rename constraint to foreign_key
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Sep 23, 2021
1 parent e0c1a8b commit 8f9a91c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/mysql/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use crate::debug_print;
use crate::mysql::def::*;
use crate::mysql::parser::{parse_constraint_query_results, parse_index_query_results};
use crate::mysql::parser::{parse_foreign_key_query_results, parse_index_query_results};
use crate::mysql::query::{
ColumnQueryResult, ConstraintQueryResult, IndexQueryResult, SchemaQueryBuilder,
ColumnQueryResult, ForeignKeyQueryResult, IndexQueryResult, SchemaQueryBuilder,
TableQueryResult, VersionQueryResult,
};
use futures::future;
Expand Down Expand Up @@ -168,10 +168,10 @@ impl SchemaDiscovery {
) -> Vec<ForeignKeyInfo> {
let rows = self
.executor
.fetch_all(self.query.query_constraints(schema.clone(), table.clone()))
.fetch_all(self.query.query_foreign_key(schema.clone(), table.clone()))
.await;

let results: Vec<ConstraintQueryResult> = rows
let results: Vec<ForeignKeyQueryResult> = rows
.iter()
.map(|row| {
let result = row.into();
Expand All @@ -180,7 +180,7 @@ impl SchemaDiscovery {
})
.collect();

let foreign_keys = parse_constraint_query_results(Box::new(results.into_iter()))
let foreign_keys = parse_foreign_key_query_results(Box::new(results.into_iter()))
.map(|index| {
debug_print!("{:?}", index);
index
Expand Down
38 changes: 19 additions & 19 deletions src/mysql/parser/constraint.rs → src/mysql/parser/foreign_key.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
use crate::mysql::def::*;
use crate::mysql::query::ConstraintQueryResult;
use crate::mysql::query::ForeignKeyQueryResult;
use crate::Name;

pub struct ConstraintQueryResultParser {
pub struct ForeignKeyQueryResultParser {
curr: Option<ForeignKeyInfo>,
results: Box<dyn Iterator<Item = ConstraintQueryResult>>,
results: Box<dyn Iterator<Item = ForeignKeyQueryResult>>,
}

/// ConstraintQueryResult must be sorted by (TableName, ConstraintName, OrdinalPosition)
pub fn parse_constraint_query_results(
results: Box<dyn Iterator<Item = ConstraintQueryResult>>,
/// ForeignKeyQueryResult must be sorted by (TableName, ConstraintName, OrdinalPosition)
pub fn parse_foreign_key_query_results(
results: Box<dyn Iterator<Item = ForeignKeyQueryResult>>,
) -> impl Iterator<Item = ForeignKeyInfo> {
ConstraintQueryResultParser {
ForeignKeyQueryResultParser {
curr: None,
results,
}
}

impl Iterator for ConstraintQueryResultParser {
impl Iterator for ForeignKeyQueryResultParser {
type Item = ForeignKeyInfo;

fn next(&mut self) -> Option<Self::Item> {
while let Some(result) = self.results.next() {
let mut constraint = parse_constraint_query_result(result);
let mut foreign_key = parse_foreign_key_query_result(result);
if let Some(curr) = &mut self.curr {
// group by `constraint.name`
if curr.name == constraint.name {
curr.columns.push(constraint.columns.pop().unwrap());
// group by `foreign_key.name`
if curr.name == foreign_key.name {
curr.columns.push(foreign_key.columns.pop().unwrap());
curr.referenced_columns
.push(constraint.referenced_columns.pop().unwrap());
.push(foreign_key.referenced_columns.pop().unwrap());
} else {
let prev = self.curr.take();
self.curr = Some(constraint);
self.curr = Some(foreign_key);
return prev;
}
} else {
self.curr = Some(constraint);
self.curr = Some(foreign_key);
}
}
self.curr.take()
}
}

pub fn parse_constraint_query_result(result: ConstraintQueryResult) -> ForeignKeyInfo {
pub fn parse_foreign_key_query_result(result: ForeignKeyQueryResult) -> ForeignKeyInfo {
ForeignKeyInfo {
name: result.constraint_name,
columns: vec![result.column_name],
Expand All @@ -64,17 +64,17 @@ mod tests {
#[test]
fn test_1() {
assert_eq!(
parse_constraint_query_results(Box::new(
parse_foreign_key_query_results(Box::new(
vec![
ConstraintQueryResult {
ForeignKeyQueryResult {
constraint_name: "fk-cat-dog".to_owned(),
column_name: "d1".to_owned(),
referenced_table_name: "cat".to_owned(),
referenced_column_name: "c1".to_owned(),
update_rule: "CASCADE".to_owned(),
delete_rule: "NO ACTION".to_owned(),
},
ConstraintQueryResult {
ForeignKeyQueryResult {
constraint_name: "fk-cat-dog".to_owned(),
column_name: "d2".to_owned(),
referenced_table_name: "cat".to_owned(),
Expand Down
4 changes: 2 additions & 2 deletions src/mysql/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! To parse MySQL's INFORMATION_SCHEMA
mod column;
mod constraint;
mod foreign_key;
mod index;
mod system;
mod table;

pub use column::*;
pub use constraint::*;
pub use foreign_key::*;
pub use index::*;
pub use system::*;
pub use table::*;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum ReferentialConstraintsFields {
}

#[derive(Debug, Default)]
pub struct ConstraintQueryResult {
pub struct ForeignKeyQueryResult {
pub constraint_name: String,
pub column_name: String,
pub referenced_table_name: String,
Expand All @@ -41,7 +41,7 @@ pub struct ConstraintQueryResult {
}

impl SchemaQueryBuilder {
pub fn query_constraints(
pub fn query_foreign_key(
&self,
schema: SeaRc<dyn Iden>,
table: SeaRc<dyn Iden>,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl SchemaQueryBuilder {
}

#[cfg(feature = "sqlx-mysql")]
impl From<&MySqlRow> for ConstraintQueryResult {
impl From<&MySqlRow> for ForeignKeyQueryResult {
fn from(row: &MySqlRow) -> Self {
Self {
constraint_name: row.get(0),
Expand All @@ -95,7 +95,7 @@ impl From<&MySqlRow> for ConstraintQueryResult {
}

#[cfg(not(feature = "sqlx-mysql"))]
impl From<&MySqlRow> for ConstraintQueryResult {
impl From<&MySqlRow> for ForeignKeyQueryResult {
fn from(row: &MySqlRow) -> Self {
Self::default()
}
Expand Down
4 changes: 2 additions & 2 deletions src/mysql/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
mod char_set;
mod column;
mod constraint;
mod foreign_key;
mod index;
mod schema;
mod table;
mod version;

pub use char_set::*;
pub use column::*;
pub use constraint::*;
pub use foreign_key::*;
pub use index::*;
pub use schema::*;
pub use table::*;
Expand Down

0 comments on commit 8f9a91c

Please sign in to comment.