1use crate::dialect::DialectTypes;
6use crate::sql::SQL;
7use crate::traits::SQLParam;
8use crate::types::Textual;
9
10use super::{AggOr, AggregateKind, Expr, NonNull, Nullability, SQLExpr, Scalar};
11
12use crate::PostgresDialect;
13
14#[diagnostic::on_unimplemented(
15 message = "sequence functions are not available for this dialect",
16 label = "sequence functions require PostgreSQL"
17)]
18pub trait SequenceSupport {}
19
20impl SequenceSupport for PostgresDialect {}
21
22pub fn nextval<'a, V, E>(
37 sequence: E,
38) -> SQLExpr<'a, V, <V::DialectMarker as DialectTypes>::BigInt, NonNull, Scalar>
39where
40 V: SQLParam + 'a,
41 V::DialectMarker: SequenceSupport,
42 E: Expr<'a, V>,
43 E::SQLType: Textual,
44{
45 SQLExpr::new(SQL::func("NEXTVAL", sequence.into_sql()))
46}
47
48pub fn currval<'a, V, E>(
63 sequence: E,
64) -> SQLExpr<'a, V, <V::DialectMarker as DialectTypes>::BigInt, NonNull, Scalar>
65where
66 V: SQLParam + 'a,
67 V::DialectMarker: SequenceSupport,
68 E: Expr<'a, V>,
69 E::SQLType: Textual,
70{
71 SQLExpr::new(SQL::func("CURRVAL", sequence.into_sql()))
72}
73
74#[allow(clippy::type_complexity)]
87pub fn setval<'a, V, E, N>(
88 sequence: E,
89 value: N,
90) -> SQLExpr<
91 'a,
92 V,
93 <V::DialectMarker as DialectTypes>::BigInt,
94 <E::Nullable as super::NullOr<N::Nullable>>::Output,
95 <E::Aggregate as AggOr<N::Aggregate>>::Output,
96>
97where
98 V: SQLParam + 'a,
99 V::DialectMarker: SequenceSupport,
100 E: Expr<'a, V>,
101 E::SQLType: Textual,
102 N: Expr<'a, V>,
103 N::SQLType: crate::types::Integral,
104 E::Nullable: super::NullOr<N::Nullable>,
105 N::Nullable: Nullability,
106 E::Aggregate: AggOr<N::Aggregate>,
107 N::Aggregate: AggregateKind,
108{
109 SQLExpr::new(SQL::func(
110 "SETVAL",
111 sequence
112 .into_sql()
113 .push(crate::Token::COMMA)
114 .append(value.into_sql()),
115 ))
116}