Struct QueryBuilder

Source
pub struct QueryBuilder<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> QueryBuilder<'a>

Source

pub fn new() -> Self

Source

pub fn create<T: Into<CowSegment<'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")
Source

pub fn update<T: Into<CowSegment<'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")
Source

pub fn select<T: Into<CowSegment<'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")
Source

pub fn delete<T: Into<CowSegment<'a>>>(self, node: T) -> Self

Start a DELETE statement:

DELETE user:John
§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new().delete("ee:Person").build();

assert_eq!(query, "DELETE ee:Person");
Source

pub fn relate<T: Into<CowSegment<'a>>>(self, node: T) -> Self

Start a RELATE statement:

RELATE user:tobie->write->article:surreal SET time.written = time::now();

Note: the SET or anything after it in the example above should be added manually using the QueryBuilder::set() method.

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new().relate("user:John->likes->user:Mark").build();

assert_eq!(query, "RELATE user:John->likes->user:Mark");
Source

pub fn content<T: Into<CowSegment<'a>>>(self, json_content: T) -> Self

Start a CONTENT statement. Content statements often follow RELATE statements:

RELATE user:tobie->write->article:surreal CONTENT {
  source: 'Apple notes',
  tags: ['notes', 'markdown'],
  time: {
    written: time::now(),
  },
};

Note: Anything before the CONTENT in the example above should be added manually using the QueryBuilder::relate() method.

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new().content("{ creation_time: time::now() }").build();

assert_eq!(query, "CONTENT { creation_time: time::now() }");
Source

pub fn from<T: Into<CowSegment<'a>>>(self, node: T) -> Self

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new().from("Person").build();

assert_eq!(query, "FROM Person")
Source

pub fn select_many<T>(self, nodes: &[T]) -> Self
where T: Copy + Into<CowSegment<'a>>,

§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")
Source

pub fn also<T: Into<CowSegment<'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")
Source

pub fn filter<T: Into<CowSegment<'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");
Source

pub fn and_where<T: Into<CowSegment<'a>>>(self, condition: T) -> Self

An alias for QueryBuilder::filter

Source

pub fn or<T: Into<CowSegment<'a>>>(self, condition: T) -> Self

Writes a OR followed by the supplied condition

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .or("handle = ?1")
  .build();

assert_eq!(query, "OR handle = ?1");
Source

pub fn and<T: Into<CowSegment<'a>>>(self, condition: T) -> Self

Starts an AND followed by the supplied condition.

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .and("handle = ?1")
  .build();

assert_eq!(query, "AND handle = ?1");
Source

pub fn set<T: Into<CowSegment<'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");
Source

pub fn set_many<T>(self, updates: &[T]) -> Self
where T: Copy + Into<CowSegment<'a>>,

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");
Source

pub fn fetch<T: Into<CowSegment<'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");
Source

pub fn fetch_many<T>(self, fields: &[T]) -> Self
where T: Copy + Into<CowSegment<'a>>,

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");
Source

pub fn group_by<T: Into<CowSegment<'a>>>(self, field: T) -> Self

Starts a GROUP BY clause,

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .group_by("author")
  .build();

assert_eq!(query, "GROUP BY author");
Source

pub fn group_by_many<T>(self, fields: &[T]) -> Self
where T: Copy + Into<CowSegment<'a>>,

Starts a GROUP BY clause with zero or more fields,

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .group_by_many(&["author", "projects"])
  .build();

assert_eq!(query, "GROUP BY author , projects");
Source

pub fn order_by_asc<T: Into<CowSegment<'a>>>(self, field: T) -> Self

Starts a ORDER BY ASC clause,

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .order_by_asc("author")
  .build();

assert_eq!(query, "ORDER BY author ASC");
Source

pub fn order_by_asc_many<T>(self, fields: &[T]) -> Self
where T: Copy + Into<CowSegment<'a>>,

Starts a ORDER BY ASC clause with zero or more fields,

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .order_by_asc_many(&["author", "projects"])
  .build();

assert_eq!(query, "ORDER BY author ASC , projects ASC");
Source

pub fn order_by_desc<T: Into<CowSegment<'a>>>(self, field: T) -> Self

Starts a ORDER BY DESC clause,

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .order_by_desc("author")
  .build();

assert_eq!(query, "ORDER BY author DESC");
Source

pub fn order_by_desc_many<T>(self, fields: &[T]) -> Self
where T: Copy + Into<CowSegment<'a>>,

Starts a ORDER BY DESC clause with zero or more fields,

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .order_by_desc_many(&["author", "projects"])
  .build();

assert_eq!(query, "ORDER BY author DESC , projects DESC");
Source

pub fn if_then<F>(self, condition: bool, action: F) -> Self
where F: Fn(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");
Source

pub fn and_group<F, T: Into<CowSegment<'a>>>( self, first_condition: T, action: F, ) -> Self
where F: Fn(Self) -> Self,

Writes an AND followed by the supplied first_condition and any other statement added to the querybuilder in the action closure surrounded by parenthesis.

Can be used to compose boolean expressions with grouped OR statements like so:

WHERE name contains 'John' AND (name contains 'Doe' OR name contains 'Eod')
§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .select("*")
  .from("user")
  .filter("name contains 'John'")
  .and_group("name contains 'Doe'", |q| {
    q.or("name contains 'Eod'")
  })
  .build();

assert_eq!(query, "SELECT * FROM user WHERE name contains 'John' AND ( name contains 'Doe' OR name contains 'Eod' )");
Source

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");
Source

pub fn commas<F>(self, action: F) -> Self
where F: Fn(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");
Source

pub fn ands<F>(self, action: F) -> Self
where F: Fn(Self) -> Self,

Start a queue where all of the new pushed actions are separated by ANDs.

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .ands(|query| query
    .raw("foo")
    .raw("bar")
  ).build();

assert_eq!(query, "foo AND bar");
Source

pub fn ors<F>(self, action: F) -> Self
where F: Fn(Self) -> Self,

Start a queue where all of the new pushed actions are separated by ORs.

§Example
use surreal_simple_querybuilder::prelude::*;

let query = QueryBuilder::new()
  .ors(|query| query
    .raw("foo")
    .raw("bar")
  ).build();

assert_eq!(query, "foo OR bar");
Source

pub fn limit<T: Into<CowSegment<'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")
Source

pub fn start_at<T: Into<CowSegment<'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")
Source

pub fn add_segment<T: Into<CowSegment<'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.

Source

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);
Source

pub fn build(self) -> String

Auto Trait Implementations§

§

impl<'a> Freeze for QueryBuilder<'a>

§

impl<'a> RefUnwindSafe for QueryBuilder<'a>

§

impl<'a> Send for QueryBuilder<'a>

§

impl<'a> Sync for QueryBuilder<'a>

§

impl<'a> Unpin for QueryBuilder<'a>

§

impl<'a> UnwindSafe for QueryBuilder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.