Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<'a, T> {
    pub has_soft_deletes: bool,
    /* private fields */
}
Expand description

QueryBuilder — Fluent API untuk membangun SQL query secara dinamis.

Fields§

§has_soft_deletes: bool

Implementations§

Source§

impl<'a, T> QueryBuilder<'a, T>
where T: for<'r> FromRow<'r, AnyRow> + Send + Unpin + DeserializeOwned,

Source

pub fn new(pool: &'a DatabasePool, table: &'static str) -> QueryBuilder<'a, T>

Source

pub fn with(self, relation: &str) -> QueryBuilder<'a, T>

Eager load relasi. Contoh: .with("posts")

Source

pub fn join(self, join_clause: &str) -> QueryBuilder<'a, T>

Tambahkan JOIN. Contoh: .join("JOIN posts p ON p.user_id = users.id")

Source

pub fn select(self, columns: &str) -> QueryBuilder<'a, T>

Tentukan kolom yang akan diambil.

Source

pub fn filter<V>( self, column: &str, operator: &str, value: V, ) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Tambahkan klausa WHERE. Contoh: .filter("email", "=", "user@example.com")

Source

pub fn where_eq<V>(self, column: &str, value: V) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Shortcut WHERE column = value

Source

pub fn where_eq_str(self, column: &str, value: &str) -> QueryBuilder<'a, T>

Dynamically add a equality condition with owned String or &str column and value

Source

pub fn where_ne<V>(self, column: &str, value: V) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Shortcut WHERE column != value

Source

pub fn where_like<V>(self, column: &str, value: V) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Shortcut WHERE column LIKE value

Source

pub fn where_null(self, column: &str) -> QueryBuilder<'a, T>

Shortcut WHERE column IS NULL

Source

pub fn where_not_null(self, column: &str) -> QueryBuilder<'a, T>

Shortcut WHERE column IS NOT NULL

Source

pub fn or_filter<V>( self, column: &str, operator: &str, value: V, ) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Tambahkan klausa OR WHERE.

Source

pub fn where_in<V>(self, column: &str, values: Vec<V>) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Tambahkan klausa WHERE IN.

Source

pub fn latest(self) -> QueryBuilder<'a, T>

Shortcut untuk ORDER BY created_at DESC.

Source

pub fn oldest(self) -> QueryBuilder<'a, T>

Shortcut untuk ORDER BY created_at ASC.

Source

pub fn order_by(self, column: &str, direction: &str) -> QueryBuilder<'a, T>

Tambahkan ORDER BY.

Source

pub fn limit(self, limit: usize) -> QueryBuilder<'a, T>

Batasi jumlah record.

Source

pub fn offset(self, offset: usize) -> QueryBuilder<'a, T>

Tentukan offset query.

Source

pub fn where_between<V1, V2>( self, column: &str, min: V1, max: V2, ) -> QueryBuilder<'a, T>
where V1: 'a + Send + Encode<'a, Any> + Type<Any>, V2: 'a + Send + Encode<'a, Any> + Type<Any>,

Tambahkan klausa WHERE BETWEEN min AND max.

Source

pub fn where_not_in<V>( self, column: &str, values: Vec<V>, ) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Tambahkan klausa WHERE NOT IN.

Source

pub fn or_where<V>( self, column: &str, operator: &str, value: V, ) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Shortcut OR WHERE.

Source

pub fn group_by(self, column: &str) -> QueryBuilder<'a, T>

Tambahkan GROUP BY.

Source

pub fn having<V>( self, column: &str, operator: &str, value: V, ) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Tambahkan HAVING.

Source

pub fn where_has( self, relation_table: &str, foreign_key: &str, local_key: &str, ) -> QueryBuilder<'a, T>

Tambahkan klausa WHERE EXISTS untuk relasi (Laravel: whereHas). Contoh: query.where_has("posts", "user_id", "id")

Source

pub fn where_doesnt_have( self, relation_table: &str, foreign_key: &str, local_key: &str, ) -> QueryBuilder<'a, T>

Tambahkan klausa WHERE NOT EXISTS untuk relasi (Laravel: whereDoesntHave). Contoh: query.where_doesnt_have("posts", "user_id", "id")

Source

pub fn soft_deletes(self) -> QueryBuilder<'a, T>

Sertakan baris yang telah di-soft-delete dalam query (Laravel: withTrashed) Aktifkan mode soft deletes pada query ini

Source

pub fn with_trashed(self) -> QueryBuilder<'a, T>

Sertakan baris yang telah di-soft-delete dalam query (Laravel: withTrashed)

Source

pub fn only_trashed(self) -> QueryBuilder<'a, T>

Ambil HANYA baris yang telah di-soft-delete (Laravel: onlyTrashed)

Source

pub fn when<F>(self, condition: bool, callback: F) -> QueryBuilder<'a, T>
where F: FnOnce(QueryBuilder<'a, T>) -> QueryBuilder<'a, T>,

Terapkan filter query secara kondisional jika kondisi bernilai true (Laravel: when)

Source

pub fn unless<F>(self, condition: bool, callback: F) -> QueryBuilder<'a, T>
where F: FnOnce(QueryBuilder<'a, T>) -> QueryBuilder<'a, T>,

Terapkan filter query secara kondisional jika kondisi bernilai false (Laravel: unless)

Source

pub fn select_raw(self, raw_sql: &str) -> QueryBuilder<'a, T>

Tentukan kolom SELECT kustom atau ekspresi SQL mentah (Laravel: selectRaw)

Source

pub fn where_raw(self, raw_sql: &str) -> QueryBuilder<'a, T>

Tambahkan klausa WHERE mentah (Laravel: whereRaw)

Source

pub fn or_where_eq<V>(self, column: &str, value: V) -> QueryBuilder<'a, T>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Shortcut OR WHERE column = value

Source

pub fn where_not_between<V1, V2>( self, column: &str, min: V1, max: V2, ) -> QueryBuilder<'a, T>
where V1: 'a + Send + Encode<'a, Any> + Type<Any>, V2: 'a + Send + Encode<'a, Any> + Type<Any>,

Tambahkan klausa WHERE column NOT BETWEEN min AND max.

Source

pub fn where_date(self, column: &str, date: &str) -> QueryBuilder<'a, T>

Tambahkan klausa WHERE DATE(column) = date

Source

pub async fn increment(self, column: &str, amount: i64) -> Result<u64, Error>

Tambahkan nilai suatu kolom secara atomik di database (Laravel: increment)

Source

pub async fn decrement(self, column: &str, amount: i64) -> Result<u64, Error>

Kurangkan nilai suatu kolom secara atomik di database (Laravel: decrement)

Source

pub async fn restore(self) -> Result<u64, Error>

Pulihkan record yang terhapus (Soft Delete Restore ala Laravel)

Source

pub async fn pluck_strings(self, column: &str) -> Result<Vec<String>, Error>

Ambil satu kolom tertentu sebagai Vec (Laravel: pluck)

Source

pub fn to_sql(&self) -> String

Debugging: Mengambil string SQL yang dihasilkan.

Source

pub async fn get(self) -> Result<Vec<T>, Error>
where T: Model,

Ambil semua record yang cocok.

Source

pub async fn first(self) -> Result<Option<T>, Error>

Ambil record pertama yang cocok.

Source

pub async fn first_or_create<K, V>( self, data: impl IntoIterator<Item = (K, V)>, ) -> Result<T, Error>
where K: AsRef<str>, V: ToString, T: Model,

Ambil record pertama yang cocok, atau buat baru dengan data yang diberikan jika tidak ditemukan.

Source

pub async fn update_or_create<K, V>( self, values: impl IntoIterator<Item = (K, V)>, ) -> Result<T, Error>
where K: AsRef<str>, V: ToString, T: Model,

Cari record berdasarkan kriteria query. Jika ditemukan, perbarui dengan data yang diberikan. Jika tidak ditemukan, buat record baru dengan data tersebut (Laravel: updateOrCreate).

Source

pub async fn chunk<F, Fut>( self, chunk_size: usize, callback: F, ) -> Result<(), Error>
where T: Model, F: FnMut(Vec<T>) -> Fut, Fut: Future<Output = Result<(), Error>>,

Memproses data dalam potongan batch secara bertahap (Laravel: chunk). Sangat ramah memori (RAM-efficient) saat memproses data berjumlah besar.

Source

pub async fn update<K, V>( self, data: impl IntoIterator<Item = (K, V)>, ) -> Result<u64, Error>
where K: AsRef<str>, V: ToString,

Lakukan mass update pada record yang memenuhi kriteria query.

Source

pub async fn first_or_fail(self) -> Result<T, Error>

Ambil record pertama yang cocok, atau return Error jika tidak ditemukan.

Source

pub async fn find_or_fail(pool: &'a DatabasePool, id: i64) -> Result<T, Error>
where T: Model,

Ambil record berdasarkan ID atau return Error.

Source

pub async fn count(self) -> Result<i64, Error>

Count total rows.

Source

pub async fn paginate( self, page: usize, per_page: usize, ) -> Result<LengthAwarePaginator<T>, Error>
where T: Model,

Melakukan paginasi query dan mengembalikan LengthAwarePaginator<T>.

Source

pub async fn delete(self) -> Result<u64, Error>

Soft delete semua record yang cocok dengan kriteria query.

Source

pub async fn force_delete(self) -> Result<u64, Error>

Hard delete (permanen) semua record yang cocok dengan kriteria query.

Source

pub async fn soft_delete(self) -> Result<u64, Error>

Alias eksplisit untuk delete() (Soft Delete semua record yang cocok).

Source

pub async fn exists(self) -> Result<bool, Error>

Check if any matching rows exist.

Source

pub async fn doesnt_exist(self) -> Result<bool, Error>

Check if no matching rows exist.

Source

pub async fn sum(self, column: &str) -> Result<f64, Error>

Hitung SUM dari sebuah kolom.

Source

pub async fn avg(self, column: &str) -> Result<f64, Error>

Hitung AVG dari sebuah kolom.

Source

pub async fn min(self, column: &str) -> Result<Option<f64>, Error>

Cari MIN dari sebuah kolom.

Source

pub async fn max(self, column: &str) -> Result<Option<f64>, Error>

Cari MAX dari sebuah kolom.

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for QueryBuilder<'a, T>

§

impl<'a, T> !UnwindSafe for QueryBuilder<'a, T>

§

impl<'a, T> Forget for QueryBuilder<'a, T>

§

impl<'a, T> Freeze for QueryBuilder<'a, T>
where PhantomData<T>: Freeze,

§

impl<'a, T> Move for QueryBuilder<'a, T>

§

impl<'a, T> Send for QueryBuilder<'a, T>
where PhantomData<T>: Send,

§

impl<'a, T> Sync for QueryBuilder<'a, T>
where PhantomData<T>: Sync,

§

impl<'a, T> Unpin for QueryBuilder<'a, T>
where PhantomData<T>: Unpin,

§

impl<'a, T> UnsafeUnpin for QueryBuilder<'a, 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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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