From 324c98dfa423119c2a775b08680cbb781e1a17d6 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 10 Apr 2019 19:37:02 -0400 Subject: [PATCH] Rough implementation of LIKE methods --- src/query_builder.rs | 56 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/query_builder.rs b/src/query_builder.rs index d7e7e18..bd2a1a6 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -154,6 +154,12 @@ impl QueryState { self } + pub fn append_where_values(&mut self, val: Box) -> &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, 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, 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, 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, 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, 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>) -> HashMap> { + unimplemented!(); + } + + fn _where_in(&mut self, key: &str, values: Vec>) -> &mut Self { + unimplemented!(); + } + + fn _where_in_string(&mut self, key: &str, values: Vec>) -> &mut Self { + unimplemented!(); + } + + fn _where_string(&mut self, key: &str, value: Box) -> &mut Self { + unimplemented!(); + } + fn compile(&self, query_type: QueryType, table: &str) -> String { unimplemented!(); }