use std::marker::PhantomData;
use crate::interfaces::Record;
use super::executables::{
All, BatchInsert, BatchUpsert, Count, Delete, Exists, First, Insert, One, ReturningBatchInsert,
ReturningBatchUpsert, ReturningDelete, ReturningInsert, ReturningUpdate, Scalar, Slice, Update,
};
use super::expr::IntoExpr;
use super::scope::{QueryScope, ReturningScope};
use super::traits::IntoColumnRef;
use super::values::{NoRecord, WriteInput, WriteUsing, WriteValues};
impl QueryScope {
pub fn all<T>(self) -> All<T>
where
T: Record,
{
All {
scope: self,
_marker: PhantomData,
}
}
pub fn one<T>(self) -> One<T>
where
T: Record,
{
One {
scope: self,
_marker: PhantomData,
}
}
pub fn first<T>(self) -> First<T>
where
T: Record,
{
First {
scope: self,
_marker: PhantomData,
}
}
pub fn slice<T>(self, offset: usize, count: usize) -> Slice<T>
where
T: Record,
{
Slice {
scope: self,
offset,
count,
_marker: PhantomData,
}
}
pub fn insert<W>(self, row: W) -> Insert<W>
where
W: WriteInput,
{
Insert { scope: self, row }
}
#[doc(hidden)]
pub fn insert_using<F>(self, f: F) -> Insert<WriteValues<'static, NoRecord>>
where
F: FnOnce(WriteUsing) -> WriteUsing,
{
Insert {
scope: self,
row: f(WriteUsing::new()).into_values(),
}
}
pub fn update<W>(self, row: W) -> Update<W>
where
W: WriteInput,
{
Update { scope: self, row }
}
#[doc(hidden)]
pub fn update_using<F>(self, f: F) -> Update<WriteValues<'static, NoRecord>>
where
F: FnOnce(WriteUsing) -> WriteUsing,
{
Update {
scope: self,
row: f(WriteUsing::new()).into_values(),
}
}
pub fn delete(self) -> Delete {
Delete { scope: self }
}
pub fn count(self) -> Count {
Count { scope: self }
}
pub fn exists(self) -> Exists {
Exists { scope: self }
}
pub fn scalar<V>(self, expr: impl IntoExpr<V>) -> Scalar<V> {
Scalar {
scope: self,
expr: expr.into_expr().node,
_marker: PhantomData,
}
}
pub fn batch_insert<T>(self, rows: &[T]) -> BatchInsert<'_, T>
where
T: Record,
{
BatchInsert { scope: self, rows }
}
pub fn batch_upsert<T, I, C>(self, rows: &[T], conflict: I) -> BatchUpsert<'_, T>
where
T: Record,
I: IntoIterator<Item = C>,
C: IntoColumnRef,
{
let conflict = conflict
.into_iter()
.map(IntoColumnRef::into_column_ref)
.collect();
BatchUpsert {
scope: self,
rows,
conflict,
}
}
}
impl<R> ReturningScope<R>
where
R: Record,
{
pub fn insert<W>(self, row: W) -> ReturningInsert<R, W>
where
W: WriteInput,
{
ReturningInsert {
returning: self,
row,
}
}
#[doc(hidden)]
pub fn insert_using<F>(self, f: F) -> ReturningInsert<R, WriteValues<'static, NoRecord>>
where
F: FnOnce(WriteUsing) -> WriteUsing,
{
ReturningInsert {
returning: self,
row: f(WriteUsing::new()).into_values(),
}
}
pub fn update<W>(self, row: W) -> ReturningUpdate<R, W>
where
W: WriteInput,
{
ReturningUpdate {
returning: self,
row,
}
}
#[doc(hidden)]
pub fn update_using<F>(self, f: F) -> ReturningUpdate<R, WriteValues<'static, NoRecord>>
where
F: FnOnce(WriteUsing) -> WriteUsing,
{
ReturningUpdate {
returning: self,
row: f(WriteUsing::new()).into_values(),
}
}
pub fn delete(self) -> ReturningDelete<R> {
ReturningDelete { returning: self }
}
pub fn batch_insert<T>(self, rows: &[T]) -> ReturningBatchInsert<'_, R, T>
where
T: Record,
{
ReturningBatchInsert {
returning: self,
rows,
}
}
pub fn batch_upsert<T, I, C>(self, rows: &[T], conflict: I) -> ReturningBatchUpsert<'_, R, T>
where
T: Record,
I: IntoIterator<Item = C>,
C: IntoColumnRef,
{
let conflict = conflict
.into_iter()
.map(IntoColumnRef::into_column_ref)
.collect();
ReturningBatchUpsert {
returning: self,
rows,
conflict,
}
}
}