Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder { /* private fields */ }
Expand description

Builder for constructing Apicalypse query strings.

§Examples

use igdb_atlas::QueryBuilder;

let query = QueryBuilder::new()
    .select(&["name", "rating"])
    .search("Dark Souls")
    .limit(5)
    .build();

assert!(query.contains("fields name,rating;"));
assert!(query.contains("search \"Dark Souls\";"));
assert!(query.contains("limit 5;"));

Implementations§

Source§

impl QueryBuilder

Source

pub fn new() -> Self

Creates a new empty query builder.

§Examples
use igdb_atlas::QueryBuilder;

let builder = QueryBuilder::new();
assert_eq!(builder.field_count(), 0);
assert!(!builder.has_search());
Source

pub fn select(self, fields: &[&str]) -> Self

Sets the fields to return.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new().select(&["name", "rating"]).build();
assert!(q.contains("fields name,rating;"));
Source

pub fn add_field(self, field: &str) -> Self

Appends a single field to the selection.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new()
    .select(&["name"])
    .add_field("rating")
    .build();
assert!(q.contains("name"));
assert!(q.contains("rating"));
Source

pub fn search(self, term: &str) -> Self

Sets the search term.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new().search("Zelda").build();
assert!(q.contains("search \"Zelda\";"));
Source

pub fn where_clause(self, clause: &str) -> Self

Sets or replaces the WHERE clause.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new().where_clause("rating > 80").build();
assert!(q.contains("where rating > 80;"));
Source

pub fn and_where(self, clause: &str) -> Self

Appends an AND condition to the WHERE clause.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new()
    .where_clause("rating > 80")
    .and_where("platforms = 48")
    .build();
assert!(q.contains("where rating > 80 & platforms = 48;"));
Source

pub fn sort_by(self, field: &str, descending: bool) -> Self

Sets the sort field and direction.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new().sort_by("rating", true).build();
assert!(q.contains("sort rating desc;"));
Source

pub fn limit(self, n: u32) -> Self

Sets the result limit.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new().limit(25).build();
assert!(q.contains("limit 25;"));
Source

pub fn offset(self, n: u32) -> Self

Sets the pagination offset.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new().offset(50).build();
assert!(q.contains("offset 50;"));
Source

pub fn expand(self, parent: &str, sub_fields: &[&str]) -> Self

Adds a field expansion using Apicalypse dot notation.

When sub_fields is empty, expands all sub-fields (parent.*). When sub_fields has entries, expands each one individually (parent.field1, parent.field2).

The expanded fields are automatically added to the fields clause in the final query. You do not need to include the parent field in your select() call - it will be added.

§Examples
use igdb_atlas::QueryBuilder;

// Expand all platform sub-fields
let q = QueryBuilder::new()
    .select(&["name"])
    .expand("platforms", &[])
    .build();
assert!(q.contains("platforms.*"));

// Expand specific sub-fields
let q = QueryBuilder::new()
    .select(&["name"])
    .expand("platforms", &["name", "abbreviation"])
    .build();
assert!(q.contains("platforms.name"));
assert!(q.contains("platforms.abbreviation"));
Source

pub fn build(&self) -> String

Builds the final Apicalypse query string.

§Field Resolution

The fields clause is built by combining:

  1. Explicitly selected fields (from select() / add_field())
  2. Expanded field references (from expand())

If no fields are selected and no expansions exist, defaults to fields *;.

§Examples
use igdb_atlas::QueryBuilder;

let q = QueryBuilder::new()
    .select(&["name", "rating"])
    .search("Zelda")
    .where_clause("rating > 80")
    .sort_by("rating", true)
    .limit(10)
    .expand("platforms", &["name", "abbreviation"])
    .build();

// All parts present
assert!(q.contains("fields name,rating,platforms.name,platforms.abbreviation;"));
assert!(q.contains("search \"Zelda\";"));
assert!(q.contains("where rating > 80;"));
assert!(q.contains("sort rating desc;"));
assert!(q.contains("limit 10;"));
Source

pub fn field_count(&self) -> usize

Returns the number of explicitly selected fields.

§Examples
use igdb_atlas::QueryBuilder;

let b = QueryBuilder::new().select(&["name", "rating"]);
assert_eq!(b.field_count(), 2);

Returns true if a search term has been set.

§Examples
use igdb_atlas::QueryBuilder;

assert!(!QueryBuilder::new().has_search());
assert!(QueryBuilder::new().search("test").has_search());
Source

pub fn where_count(&self) -> usize

Returns the number of WHERE clauses.

§Examples
use igdb_atlas::QueryBuilder;

let b = QueryBuilder::new()
    .where_clause("a = 1")
    .and_where("b = 2");
assert_eq!(b.where_count(), 2);
Source

pub fn expansion_count(&self) -> usize

Returns the number of field expansions.

§Examples
use igdb_atlas::QueryBuilder;

let b = QueryBuilder::new()
    .expand("platforms", &["name"])
    .expand("genres", &["name"]);
assert_eq!(b.expansion_count(), 2);

Trait Implementations§

Source§

impl Clone for QueryBuilder

Source§

fn clone(&self) -> QueryBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QueryBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for QueryBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more