use crate::common::PostgresSchemaType;
use crate::values::PostgresValue;
use drizzle_core::{SQLTable, ToSQL};
use std::fmt::Debug;
use std::marker::PhantomData;
use super::ExecutableState;
#[derive(Debug, Clone, Copy, Default)]
pub struct UpdateInitial;
#[derive(Debug, Clone, Copy, Default)]
pub struct UpdateSetClauseSet;
#[derive(Debug, Clone, Copy, Default)]
pub struct UpdateFromSet;
#[derive(Debug, Clone, Copy, Default)]
pub struct UpdateWhereSet;
#[derive(Debug, Clone, Copy, Default)]
pub struct UpdateReturningSet;
impl ExecutableState for UpdateSetClauseSet {}
impl ExecutableState for UpdateFromSet {}
impl ExecutableState for UpdateWhereSet {}
impl ExecutableState for UpdateReturningSet {}
pub type UpdateBuilder<'a, Schema, State, Table> = super::QueryBuilder<'a, Schema, State, Table>;
impl<'a, Schema, Table> UpdateBuilder<'a, Schema, UpdateInitial, Table>
where
Table: SQLTable<'a, PostgresSchemaType, PostgresValue<'a>>,
{
#[inline]
pub fn set(
self,
values: Table::Update,
) -> UpdateBuilder<'a, Schema, UpdateSetClauseSet, Table> {
let sql = crate::helpers::set::<'a, Table, PostgresSchemaType, PostgresValue<'a>>(values);
UpdateBuilder {
sql: self.sql.append(sql),
schema: PhantomData,
state: PhantomData,
table: PhantomData,
}
}
}
impl<'a, S, T> UpdateBuilder<'a, S, UpdateSetClauseSet, T> {
#[inline]
pub fn from(
self,
source: impl ToSQL<'a, PostgresValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateFromSet, T> {
let from_sql = crate::helpers::from(source);
UpdateBuilder {
sql: self.sql.append(from_sql),
schema: PhantomData,
state: PhantomData,
table: PhantomData,
}
}
#[inline]
pub fn r#where(
self,
condition: impl ToSQL<'a, PostgresValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateWhereSet, T> {
let where_sql = crate::helpers::r#where(condition);
UpdateBuilder {
sql: self.sql.append(where_sql),
schema: PhantomData,
state: PhantomData,
table: PhantomData,
}
}
#[inline]
pub fn returning(
self,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateReturningSet, T> {
let returning_sql = crate::helpers::returning(columns);
UpdateBuilder {
sql: self.sql.append(returning_sql),
schema: PhantomData,
state: PhantomData,
table: PhantomData,
}
}
}
impl<'a, S, T> UpdateBuilder<'a, S, UpdateFromSet, T> {
#[inline]
pub fn r#where(
self,
condition: impl ToSQL<'a, PostgresValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateWhereSet, T> {
let where_sql = crate::helpers::r#where(condition);
UpdateBuilder {
sql: self.sql.append(where_sql),
schema: PhantomData,
state: PhantomData,
table: PhantomData,
}
}
#[inline]
pub fn returning(
self,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateReturningSet, T> {
let returning_sql = crate::helpers::returning(columns);
UpdateBuilder {
sql: self.sql.append(returning_sql),
schema: PhantomData,
state: PhantomData,
table: PhantomData,
}
}
}
impl<'a, S, T> UpdateBuilder<'a, S, UpdateWhereSet, T> {
#[inline]
pub fn returning(
self,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> UpdateBuilder<'a, S, UpdateReturningSet, T> {
let returning_sql = crate::helpers::returning(columns);
UpdateBuilder {
sql: self.sql.append(returning_sql),
schema: PhantomData,
state: PhantomData,
table: PhantomData,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use drizzle_core::{SQL, ToSQL};
#[test]
fn test_update_builder_creation() {
let builder = UpdateBuilder::<(), UpdateInitial, ()> {
sql: SQL::raw("UPDATE test"),
schema: PhantomData,
state: PhantomData,
table: PhantomData,
};
assert_eq!(builder.to_sql().sql(), "UPDATE test");
}
}