Skip to content

Commit

Permalink
query api q(full-text) seach applied to db #14
Browse files Browse the repository at this point in the history
  • Loading branch information
serayuzgur committed May 5, 2017
1 parent 8116d86 commit 8c74361
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 10 deletions.
29 changes: 20 additions & 9 deletions src/database/query_api/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde_json::error::ErrorCode::Message;
use service::query_api::Queries;

/// filter array according to the query api
pub fn apply(obj: &mut Value, queries: &Queries) -> Value {
pub fn apply(obj: &mut Value, queries: &Queries) {
let ref filters = queries.filter;
if let &mut Value::Array(ref mut arr) = obj {
let mut size = arr.len();
Expand All @@ -33,7 +33,6 @@ pub fn apply(obj: &mut Value, queries: &Queries) -> Value {
}
}
} else {}
obj.clone()
}

fn convert_2_same_type(field_value: &Value, query_value: &str) -> Result<Value, Error> {
Expand Down Expand Up @@ -97,7 +96,7 @@ fn is_valid(op: &str, field_value: &Value, query_value: &str) -> bool {
valid = false;
if let Ok(qval) = convert_2_same_type(field_value, part) {
println!("checking {:?}|={:?}", field_value, &qval);
if field_value == &qval{
if field_value == &qval {
valid = true;
break;
}
Expand Down Expand Up @@ -158,7 +157,9 @@ mod tests {
"password":"123"
}]"#)
.unwrap();
assert_eq!(apply(&mut get_json(), &queries), expected);
let json = &mut get_json();
apply(json, &queries);
assert_eq!(json.clone(), expected);
}
#[test]
fn apply_ne_test() {
Expand All @@ -176,7 +177,9 @@ mod tests {
"password":"333"
}]"#)
.unwrap();
assert_eq!(apply(&mut get_json(), &queries), expected);
let json = &mut get_json();
apply(json, &queries);
assert_eq!(json.clone(), expected);
}

#[test]
Expand All @@ -195,7 +198,9 @@ mod tests {
"password":"123"
}]"#)
.unwrap();
assert_eq!(apply(&mut get_json(), &queries), expected);
let json = &mut get_json();
apply(json, &queries);
assert_eq!(json.clone(), expected);
}
#[test]
fn apply_gte_lte_test() {
Expand All @@ -213,7 +218,9 @@ mod tests {
"password":"123"
}]"#)
.unwrap();
assert_eq!(apply(&mut get_json(), &queries), expected);
let json = &mut get_json();
apply(json, &queries);
assert_eq!(json.clone(), expected);
}
#[test]
fn apply_like_test() {
Expand All @@ -237,7 +244,9 @@ mod tests {
}
]"#)
.unwrap();
assert_eq!(apply(&mut get_json(), &queries), expected);
let json = &mut get_json();
apply(json, &queries);
assert_eq!(json.clone(), expected);
}

#[test]
Expand All @@ -262,6 +271,8 @@ mod tests {
}
]"#)
.unwrap();
assert_eq!(apply(&mut get_json(), &queries), expected);
let json = &mut get_json();
apply(json, &queries);
assert_eq!(json.clone(), expected);
}
}
3 changes: 2 additions & 1 deletion src/database/query_api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pub mod filter;
pub mod q;
// TODO:
// Get the result
// If it is List than do the ops
// filter & operations & full text
// Sort
// Paginate
// Slice
// fields
104 changes: 104 additions & 0 deletions src/database/query_api/q.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//! # query_api
//! All necessery functions for appliying query api to json results.
use serde_json::Value;
use service::query_api::Queries;

/// full-text search on all String fields according to the query api
pub fn apply(obj: &mut Value, queries: &Queries) {
if let Some(ref q) = queries.q {
if let &mut Value::Array(ref mut arr) = obj {
let mut size = arr.len();
let mut i = 0;
while i < size {
let mut valid = false;
if let Some(item) = arr.get(i) {
/// get item field list
match item {
&Value::Object(ref map) => {
for key in map.keys() {
if let Some(field) = item.get(key) {
if let &Value::String(ref f_str) = field {
println!("Checking - {}.contains({})", f_str, q);
valid = f_str.contains(q);
if valid {
break;
}
}
}
}
}
&Value::String(ref f_val) => {
valid = f_val == q;
if !valid {
break;
}
}
_ => {}
}
} else {
break;
}
if !valid {
arr.remove(i);
size -= 1;
} else {
i += 1;
}
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json;
fn get_json() -> Value {
let json_string = r#"[
{
"name":"seray",
"age":31,
"active":true,
"password":"123"
},
{
"name":"kamil",
"age":900,
"active":false,
"password":"333"
},
{
"name":"hasan",
"age":25,
"active":true,
"password":"3212"
}
]"#;
serde_json::from_str(&json_string).unwrap()
}

#[test]
fn apply_q_test() {
let mut queries = Queries::new();
{
queries.q = Some("12".to_string());
}
let expected: Value = serde_json::from_str(&r#"[
{
"name":"seray",
"age":31,
"active":true,
"password":"123"
},
{
"name":"hasan",
"age":25,
"active":true,
"password":"3212"
}]"#)
.unwrap();
let json = &mut get_json();
apply(json, &queries);
assert_eq!(json.clone(), expected);
}
}
1 change: 1 addition & 0 deletions src/database/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Database {
println!("{:?}", queries);
if let Some(q) = queries {
query_api::filter::apply(obj, &q);
query_api::q::apply(obj, &q);
}
Ok(obj.clone())
}
Expand Down

0 comments on commit 8c74361

Please sign in to comment.