pub struct QueryBuilder<'a> { /* private fields */ }
Implementations
sourceimpl<'a> QueryBuilder<'a>
impl<'a> QueryBuilder<'a>
pub fn new() -> Self
sourcepub fn create<T: IntoQueryBuilderSegment + 'a>(self, node: T) -> Self
pub fn create<T: IntoQueryBuilderSegment + 'a>(self, node: T) -> Self
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new().create("Person:ee").build();
assert_eq!(query, "CREATE Person:ee")
sourcepub fn update<T: IntoQueryBuilderSegment + 'a>(self, node: T) -> Self
pub fn update<T: IntoQueryBuilderSegment + 'a>(self, node: T) -> Self
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new().update("Person:ee").build();
assert_eq!(query, "UPDATE Person:ee")
sourcepub fn select<T: IntoQueryBuilderSegment + 'a>(self, node: T) -> Self
pub fn select<T: IntoQueryBuilderSegment + 'a>(self, node: T) -> Self
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new().select("ee:Person").build();
assert_eq!(query, "SELECT ee:Person")
sourcepub fn from<T>(self, node: T) -> Selfwhere
T: IntoQueryBuilderSegment + 'a,
pub fn from<T>(self, node: T) -> Selfwhere
T: IntoQueryBuilderSegment + 'a,
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new().from("Person").build();
assert_eq!(query, "FROM Person")
sourcepub fn select_many<T: IntoQueryBuilderSegment + 'a>(self, nodes: &[T]) -> Selfwhere
T: Copy,
pub fn select_many<T: IntoQueryBuilderSegment + 'a>(self, nodes: &[T]) -> Selfwhere
T: Copy,
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new().select_many(&["ee:Person", "o:Order"]).build();
assert_eq!(query, "SELECT ee:Person , o:Order")
sourcepub fn also<T: IntoQueryBuilderSegment + 'a>(self, query: T) -> Self
pub fn also<T: IntoQueryBuilderSegment + 'a>(self, query: T) -> Self
Adds the supplied query with a comma in front of it
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new().also(&"ee").build();
assert_eq!(query, ", ee")
sourcepub fn filter<T: IntoQueryBuilderSegment + 'a>(self, condition: T) -> Self
pub fn filter<T: IntoQueryBuilderSegment + 'a>(self, condition: T) -> Self
Starts a WHERE clause.
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.filter("handle = ?1")
.build();
assert_eq!(query, "WHERE handle = ?1");
sourcepub fn and_where<T: IntoQueryBuilderSegment + 'a>(self, condition: T) -> Self
pub fn and_where<T: IntoQueryBuilderSegment + 'a>(self, condition: T) -> Self
An alias for QueryBuilder::filter
sourcepub fn and<T: IntoQueryBuilderSegment + 'a>(self, condition: T) -> Self
pub fn and<T: IntoQueryBuilderSegment + 'a>(self, condition: T) -> Self
Starts a WHERE clause.
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.and("handle = ?1")
.build();
assert_eq!(query, "AND handle = ?1");
sourcepub fn set<T: IntoQueryBuilderSegment + 'a>(self, update: T) -> Self
pub fn set<T: IntoQueryBuilderSegment + 'a>(self, update: T) -> Self
Starts a SET clause.
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.set("handle = ?1")
.build();
assert_eq!(query, "SET handle = ?1");
sourcepub fn set_many<T: IntoQueryBuilderSegment + 'a>(self, updates: &[T]) -> Selfwhere
T: Copy,
pub fn set_many<T: IntoQueryBuilderSegment + 'a>(self, updates: &[T]) -> Selfwhere
T: Copy,
Starts a SET clause with many fields.
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.set_many(&["handle = $1", "password = $2"])
.build();
assert_eq!(query, "SET handle = $1 , password = $2");
sourcepub fn fetch<T: IntoQueryBuilderSegment + 'a>(self, field: T) -> Self
pub fn fetch<T: IntoQueryBuilderSegment + 'a>(self, field: T) -> Self
Starts a FETCH clause,
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.fetch("author")
.build();
assert_eq!(query, "FETCH author");
sourcepub fn fetch_many<T: IntoQueryBuilderSegment + 'a>(self, fields: &[T]) -> Selfwhere
T: Copy,
pub fn fetch_many<T: IntoQueryBuilderSegment + 'a>(self, fields: &[T]) -> Selfwhere
T: Copy,
Starts a FETCH clause with zero or more fields,
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.fetch_many(&["author", "projects"])
.build();
assert_eq!(query, "FETCH author , projects");
sourcepub fn if_then(self, condition: bool, action: fn(_: Self) -> Self) -> Self
pub fn if_then(self, condition: bool, action: fn(_: Self) -> Self) -> Self
Queues a condition which allows the next statement to be ignored if
condition
is false
.
Conditions can be nested, the queue works as a LIFO queue.
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.select_many(&["1", "2"])
.if_then(false, |query| query
.select_many(&["3", "4"])
// will not run:
.if_then(true, |query| query
.select_many(&["5", "6"])
)
)
.if_then(true, |query| query
.select_many(&["7", "8"])
)
.build();
assert_eq!(query, "SELECT 1 , 2 SELECT 7 , 8");
sourcepub fn raw(self, text: &'a str) -> Self
pub fn raw(self, text: &'a str) -> Self
Pushes raw text to the buffer
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.raw("foo bar")
.build();
assert_eq!(query, "foo bar");
sourcepub fn commas(self, action: fn(_: Self) -> Self) -> Self
pub fn commas(self, action: fn(_: Self) -> Self) -> Self
Start a queue where all of the new pushed actions are separated by commas.
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.commas(|query| query
.raw("foo")
.raw("bar")
).build();
assert_eq!(query, "foo , bar");
sourcepub fn limit<T: IntoQueryBuilderSegment + 'a>(self, limit: T) -> Self
pub fn limit<T: IntoQueryBuilderSegment + 'a>(self, limit: T) -> Self
Start a LIMIT clause.
Example
use surreal_simple_querybuilder::prelude::*;
let page_size = 10.to_string();
let query = QueryBuilder::new()
.limit(&page_size)
.build();
assert_eq!(query, "LIMIT 10")
sourcepub fn start_at<T: IntoQueryBuilderSegment + 'a>(self, offset: T) -> Self
pub fn start_at<T: IntoQueryBuilderSegment + 'a>(self, offset: T) -> Self
Start a START AT clause.
Example
use surreal_simple_querybuilder::prelude::*;
let page_size = 10.to_string();
let query = QueryBuilder::new()
.start_at(&page_size)
.build();
assert_eq!(query, "START AT 10")
sourcepub fn add_segment<T: IntoQueryBuilderSegment + 'a>(
&mut self,
segment: T
) -> &mut Self
pub fn add_segment<T: IntoQueryBuilderSegment + 'a>(
&mut self,
segment: T
) -> &mut Self
Add the given segment to the internal buffer. This is a rather internal
method that is set public for special cases, you should prefer using the raw
method instead.
sourcepub fn param(self, key: &'a str, value: &'a str) -> Self
pub fn param(self, key: &'a str, value: &'a str) -> Self
Add a parameter and its value to the query that will be used to replace all
occurences of key
into value
when the build
method is called.
IMPORTANT Do not use this for user provided data, the input is not sanitized
Example
use surreal_simple_querybuilder::prelude::*;
let query = QueryBuilder::new()
.select("{{field}}")
.from("Account")
.param("{{field}}", "id")
.build();
assert_eq!("SELECT id FROM Account", query);
pub fn build(self) -> String
sourcepub fn set_object<T: QueryBuilderSetObject>(self) -> Selfwhere
T: 'a,
pub fn set_object<T: QueryBuilderSetObject>(self) -> Selfwhere
T: 'a,
Tell the current query builder to execute the QueryBuilderSetObject trait
for the given T
generic type.
sourcepub fn hold(&mut self, string: String) -> QueryBuilderSegment<'a>
pub fn hold(&mut self, string: String) -> QueryBuilderSegment<'a>
Tell the current querybuilder to hold the given string into its internal
buffer and to return a reference to the newly held value a QueryBuilder
can use.
This function is particularely useful if some of your code is inside a
short-lived scope such as a closure but you still need to add segments to
the querybuilder. However the fact the querybuilder initially holds series
of &'a str
makes it impossible, this is where you can tell the builder
to hold the given string for you.