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
impl QueryBuilder
Sourcepub fn new() -> Self
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());Sourcepub fn select(self, fields: &[&str]) -> Self
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;"));Sourcepub fn add_field(self, field: &str) -> Self
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"));Sourcepub fn search(self, term: &str) -> Self
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\";"));Sourcepub fn where_clause(self, clause: &str) -> Self
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;"));Sourcepub fn and_where(self, clause: &str) -> Self
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;"));Sourcepub fn sort_by(self, field: &str, descending: bool) -> Self
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;"));Sourcepub fn limit(self, n: u32) -> Self
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;"));Sourcepub fn offset(self, n: u32) -> Self
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;"));Sourcepub fn expand(self, parent: &str, sub_fields: &[&str]) -> Self
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"));Sourcepub fn build(&self) -> String
pub fn build(&self) -> String
Builds the final Apicalypse query string.
§Field Resolution
The fields clause is built by combining:
- Explicitly selected fields (from
select()/add_field()) - 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;"));Sourcepub fn field_count(&self) -> usize
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);Sourcepub fn has_search(&self) -> bool
pub fn has_search(&self) -> bool
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());Sourcepub fn where_count(&self) -> usize
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);Sourcepub fn expansion_count(&self) -> usize
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
impl Clone for QueryBuilder
Source§fn clone(&self) -> QueryBuilder
fn clone(&self) -> QueryBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more