-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves #159 - Added mysql subquery support. Added alias to subquery
- Loading branch information
1 parent
7aab98c
commit d114a7a
Showing
11 changed files
with
359 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,56 @@ | ||
use crate::{sqlite::select::SelectQueryBuilder, table::Table}; | ||
use crate::query::QueryBuilder; | ||
|
||
/// Define the enum to represent a column as either a String or SelectQueryBuilder | ||
#[derive(Clone)] | ||
pub enum Column<'a, T: Table + Default> { | ||
pub enum Column<'a> { | ||
Text(String), | ||
SubQuery(SelectQueryBuilder<'a, T>), | ||
// Subquery with alias | ||
SubQuery(Box<dyn QueryBuilder<'a> + 'a>, String), | ||
} | ||
|
||
// Implement the build method to convert the enum to a string | ||
impl<'a, T: Table + Default> Column<'a, T> { | ||
impl<'a> Column<'a> { | ||
/// Helper function to convert the columns to a string | ||
pub fn build(&self) -> String { | ||
match self { | ||
Column::Text(text) => text.clone(), | ||
Column::SubQuery(sub_query) => "(".to_string() + &sub_query.build_query() + ")", | ||
Column::SubQuery(sub_query, alias) => { | ||
"(".to_string() + &sub_query.to_sql() + ") AS " + alias | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Implementation of fmt::Display for Column | ||
impl<'a, T: Table + Default> std::fmt::Display for Column<'a, T> { | ||
impl<'a> std::fmt::Display for Column<'a> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.build()) | ||
} | ||
} | ||
|
||
// Implementation of PartialEq for Column | ||
impl<'a, T: Table + Default> PartialEq for Column<'a, T> { | ||
impl<'a> PartialEq for Column<'a> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.build() == other.build() | ||
} | ||
} | ||
|
||
// Implementation of PartialEq<String> for Column | ||
impl<'a, T: Table + Default> PartialEq<String> for Column<'a, T> { | ||
impl<'a> PartialEq<String> for Column<'a> { | ||
fn eq(&self, other: &String) -> bool { | ||
match self { | ||
Column::Text(text) => text == other, | ||
Column::SubQuery(sub_query) => sub_query.build_query() == *other, | ||
Column::SubQuery(sub_query, _) => sub_query.to_sql() == *other, | ||
} | ||
} | ||
} | ||
|
||
// Implementation of PartialEq<&str> for Column | ||
impl<'a, T: Table + Default> PartialEq<&str> for Column<'a, T> { | ||
impl<'a> PartialEq<&str> for Column<'a> { | ||
fn eq(&self, other: &&str) -> bool { | ||
match self { | ||
Column::Text(text) => text == other, | ||
Column::SubQuery(sub_query) => sub_query.build_query() == *other, | ||
Column::SubQuery(sub_query, _) => sub_query.to_sql() == *other, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.