Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<T: IEntityType> { /* private fields */ }
Expand description

A chainable query builder for entity type T.

Corresponds to EFCore’s IQueryable<T>.

Clone is derived so that builders can be forked for compositional reuse (e.g. applying additional filters on a base query without losing the original). Note that single/single_or_default still use the take(2) approach rather than clone().count() to avoid a double round-trip.

Implementations§

Source§

impl<T: IEntityType> QueryBuilder<T>

Source

pub fn new(table_name: impl Into<String>) -> Self

Creates a new QueryBuilder for a given table (without provider — SQL-only).

Source

pub fn with_provider( table_name: impl Into<String>, provider: Arc<dyn IDatabaseProvider>, ) -> Self

Creates a new QueryBuilder for a given table with a provider for execution.

Source

pub fn state(&self) -> &QueryState

Returns a reference to the accumulated query state.

Source

pub fn filter(self, f: impl FnOnce(Self) -> Self) -> Self

Applies a compile-time LINQ expression tree from linq!(?).

Source§

impl<T: IEntityType> QueryBuilder<T>

Source

pub fn distinct(self) -> Self

Marks this query as SELECT DISTINCT.

Source

pub fn where_exists_internal( self, nav_field: &'static str, related_type: &'static str, predicate: Option<BoolExpr>, negated: bool, ) -> Self

G5: Adds an EXISTS (or NOT EXISTS) correlated subquery condition.

#[doc(hidden)] — called by linq! expansion of b.posts.any(|p| p.published) / b.posts.none(...) / b.posts.all(...).

The nav_field and related_type arguments are the &'static str constants emitted by #[derive(EntityType)] (FIELD_<NAME> and NAV_RELATED_<NAME>). The table/column fields of the SubquerySpec are resolved later at SQL generation time via resolve_subqueries.

Source

pub fn where_in_subquery_internal( self, outer_column: &'static str, source_table: &'static str, projection_column: &'static str, predicate: Option<BoolExpr>, negated: bool, ) -> Self

v1.1: Adds an IN (SELECT ...) (or NOT IN (SELECT ...)) subquery condition.

#[doc(hidden)] — called by linq! expansion of b.field.in_subquery(|p: Post| p.blog_id).

Unlike where_exists_internal, the InSubquerySpec is fully specified at construction time (no navigation resolution needed). The source_table and projection_column are &'static str constants emitted by #[derive(EntityType)] (TABLE and COLUMN_<NAME>).

Source

pub fn skip(self, count: usize) -> Self

Skips the specified number of rows.

Source

pub fn take(self, count: usize) -> Self

Takes the specified number of rows.

Source§

impl<T: IEntityType> QueryBuilder<T>

Source

pub async fn find(self, id: impl Into<DbValue>) -> EFResult<Option<T>>

Finds an entity by its single primary key. Uses the entity’s PK metadata — no longer hardcodes "id".

Source

pub async fn find_by_key(self, keys: &[(&str, DbValue)]) -> EFResult<Option<T>>

Finds an entity by composite primary key. Keys are column-name constants paired with values, e.g. &[(BlogTag::COLUMN_BLOG_ID, DbValue::I32(1))].

Source

pub async fn exists_by_id(self, id: impl Into<DbValue>) -> EFResult<bool>

Checks if an entity with the given single primary key exists.

Uses SELECT 1 ... LIMIT 1 — cheaper than find(id).await?.is_some() which materializes the full row. Reads the PK column from entity metadata, mirroring find.

Source

pub async fn exists_by_key(self, keys: &[(&str, DbValue)]) -> EFResult<bool>

Checks if an entity with the given composite key exists.

Uses SELECT 1 ... LIMIT 1 — cheaper than find_by_key(keys).is_some().

Source

pub fn to_sql(&self) -> String

Builds the SQL string for this query.

Source

pub fn compile_sql(&self) -> (String, Vec<DbValue>)

Source

pub async fn to_list(self) -> EFResult<Vec<T>>

Executes the query and returns all matching entities.

Source

pub async fn to_list_with_includes(self) -> EFResult<Vec<T>>

Executes the query and eagerly loads included navigations.

Source

pub async fn first(self) -> EFResult<T>

Executes the query and returns the first matching entity.

Source

pub async fn first_or_default(self) -> EFResult<Option<T>>

Executes the query and returns the first matching entity or None.

Source

pub async fn count(self) -> EFResult<i64>

Executes a COUNT query.

Source

pub async fn any(self) -> EFResult<bool>

Checks if any entities match the query.

Source

pub async fn last(self) -> EFResult<T>

Executes the query and returns the last matching entity (reverses ordering, then takes 1). Errors if no rows match.

Source

pub async fn last_or_default(self) -> EFResult<Option<T>>

Executes the query and returns the last matching entity or None.

When the caller has set explicit order_by clauses, their directions are reversed and take(1) returns the last row under that ordering. When no ordering is set, a default ORDER BY <pk> DESC is injected so that “last” has deterministic semantics (matches the original design in the v0.4 plan §4 阶段 4). Errors if the entity has no primary key and no explicit ordering was provided.

Source

pub async fn single(self) -> EFResult<T>

Executes the query and returns the only matching entity. Errors if there are 0 or 2+ results.

Source

pub async fn single_or_default(self) -> EFResult<Option<T>>

Executes the query and returns the only matching entity, or None if empty. Errors if there are 2+ results.

Source

pub async fn long_count(self) -> EFResult<i64>

Executes a COUNT query and returns the result as i64. Alias for count() — in .NET LINQ, LongCount returns long while Count returns int; in Rust both are i64.

Source

pub async fn all<F>(self, predicate: F) -> EFResult<bool>

Determines whether all elements in the sequence satisfy a predicate. The predicate is applied in Rust after loading the entities.

Source

pub async fn contains(self, id: impl Into<DbValue>) -> EFResult<bool>

Determines whether the sequence contains an entity with the given primary key value.

Source

pub async fn to_dictionary<K, F>( self, key_selector: F, ) -> EFResult<HashMap<K, T>>

Projects each entity into a key-value pair and collects into a HashMap<K, T>. The key selector closure extracts the key from each entity.

Source

pub fn execute_update(self) -> ExecuteUpdateBuilder<T>

Prepares a bulk update operation.

Source

pub async fn execute_delete(self) -> EFResult<u64>

Executes a bulk delete operation.

Trait Implementations§

Source§

impl<T: Clone + IEntityType> Clone for QueryBuilder<T>

Source§

fn clone(&self) -> QueryBuilder<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: IEntityType> LinqSource for QueryBuilder<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for QueryBuilder<T>

§

impl<T> !UnwindSafe for QueryBuilder<T>

§

impl<T> Freeze for QueryBuilder<T>

§

impl<T> Send for QueryBuilder<T>

§

impl<T> Sync for QueryBuilder<T>

§

impl<T> Unpin for QueryBuilder<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for QueryBuilder<T>

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, 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.