Rough implementation of LIKE methods

This commit is contained in:
Timothy Warren 2019-04-10 19:37:02 -04:00
parent 1776233a93
commit 324c98dfa4

View File

@ -154,6 +154,12 @@ impl QueryState {
self
}
pub fn append_where_values(&mut self, val: Box<dyn Any>) -> &mut Self {
self.where_values.push(val);
self
}
pub fn append_query_map(
&mut self,
clause_type: QueryClauseType,
@ -243,9 +249,11 @@ impl QueryBuilder {
/// Creates a `like` clause in the sql statement
pub fn like(&mut self, field: &str, value: Box<dyn Any>, position: LikeWildcard) -> &mut Self {
unimplemented!();
self._like(field, value, position, "LIKE", "AND")
}
/// Generates an OR Like clause
pub fn or_like(
&mut self,
@ -253,7 +261,7 @@ impl QueryBuilder {
value: Box<dyn Any>,
position: LikeWildcard,
) -> &mut Self {
unimplemented!();
self._like(field, value, position, "LIKE", "OR")
}
/// Generates a NOI Like clause
@ -263,7 +271,7 @@ impl QueryBuilder {
value: Box<dyn Any>,
position: LikeWildcard,
) -> &mut Self {
unimplemented!();
self._like(field, value, position, "NOT LIKE", "AND")
}
/// Generates an OR NOT Like clause
@ -273,7 +281,7 @@ impl QueryBuilder {
value: Box<dyn Any>,
position: LikeWildcard,
) -> &mut Self {
unimplemented!();
self._like(field, value, position, "NOT LIKE", "OR")
}
// --------------------------------------------------------------------------
@ -575,6 +583,46 @@ impl QueryBuilder {
// ! Implementation Details
// --------------------------------------------------------------------------
fn _like(&mut self, field: &str, value: Box<dyn Any>, position: LikeWildcard, like: &str, conj: &str) -> &mut Self {
let field = self.driver.quote_identifier(field);
let like = format!("{} {} ?", field, like);
// @TODO Properly parse types of `value` for string formatting
let value = match position {
LikeWildcard::Before => format!("%{:?}", value),
LikeWildcard::After => format!("{:?}%s", value),
LikeWildcard::Both => format!("%{:?}%", value),
};
let conj = if self.state.query_map.len() == 0 {
" WHERE "
} else {
conj
};
self.state.append_query_map(QueryClauseType::Like, conj, &like);
self.state.append_where_values(Box::new(value));
self
}
fn _where(key: &str, values: Vec<Box<dyn Any>>) -> HashMap<String, Box<dyn Any>> {
unimplemented!();
}
fn _where_in(&mut self, key: &str, values: Vec<Box<dyn Any>>) -> &mut Self {
unimplemented!();
}
fn _where_in_string(&mut self, key: &str, values: Vec<Box<dyn Any>>) -> &mut Self {
unimplemented!();
}
fn _where_string(&mut self, key: &str, value: Box<dyn Any>) -> &mut Self {
unimplemented!();
}
fn compile(&self, query_type: QueryType, table: &str) -> String {
unimplemented!();
}