use crate::dialect::DialectTypes;
use crate::sql::SQL;
use crate::traits::SQLParam;
use crate::types::Textual;
use super::{AggOr, AggregateKind, Expr, NonNull, Nullability, SQLExpr, Scalar};
use crate::PostgresDialect;
#[diagnostic::on_unimplemented(
message = "sequence functions are not available for this dialect",
label = "sequence functions require PostgreSQL"
)]
pub trait SequenceSupport {}
impl SequenceSupport for PostgresDialect {}
pub fn nextval<'a, V, E>(
sequence: E,
) -> SQLExpr<'a, V, <V::DialectMarker as DialectTypes>::BigInt, NonNull, Scalar>
where
V: SQLParam + 'a,
V::DialectMarker: SequenceSupport,
E: Expr<'a, V>,
E::SQLType: Textual,
{
SQLExpr::new(SQL::func("NEXTVAL", sequence.into_sql()))
}
pub fn currval<'a, V, E>(
sequence: E,
) -> SQLExpr<'a, V, <V::DialectMarker as DialectTypes>::BigInt, NonNull, Scalar>
where
V: SQLParam + 'a,
V::DialectMarker: SequenceSupport,
E: Expr<'a, V>,
E::SQLType: Textual,
{
SQLExpr::new(SQL::func("CURRVAL", sequence.into_sql()))
}
#[allow(clippy::type_complexity)]
pub fn setval<'a, V, E, N>(
sequence: E,
value: N,
) -> SQLExpr<
'a,
V,
<V::DialectMarker as DialectTypes>::BigInt,
<E::Nullable as super::NullOr<N::Nullable>>::Output,
<E::Aggregate as AggOr<N::Aggregate>>::Output,
>
where
V: SQLParam + 'a,
V::DialectMarker: SequenceSupport,
E: Expr<'a, V>,
E::SQLType: Textual,
N: Expr<'a, V>,
N::SQLType: crate::types::Integral,
E::Nullable: super::NullOr<N::Nullable>,
N::Nullable: Nullability,
E::Aggregate: AggOr<N::Aggregate>,
N::Aggregate: AggregateKind,
{
SQLExpr::new(SQL::func(
"SETVAL",
sequence
.into_sql()
.push(crate::Token::COMMA)
.append(value.into_sql()),
))
}