Skip to main content

uqa_sql/ast/
identity_sequence.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! The sequence options of an identity column declaration.
8
9use super::{RelationPersistence, SequenceDeclaration};
10use crate::SQLError;
11use serde::{Deserialize, Serialize};
12
13/// The sequence options an identity column declaration writes, `GENERATED ... AS IDENTITY (options)`. They create the column's sequence, which keeps them; the column does not.
14#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
15pub struct IdentitySequenceDeclaration {
16    /// `SEQUENCE NAME`.
17    pub name: Option<IdentitySequenceName>,
18    /// `LOGGED` or `UNLOGGED`, which the sequence takes instead of its table's persistence.
19    pub persistence: Option<RelationPersistence>,
20    /// The options `CREATE SEQUENCE` interprets.
21    pub sequence: SequenceDeclaration,
22    /// The first error interpreting those options raised. `PostgreSQL` interprets them when it creates the sequence, after it has analyzed the whole statement, so the declaration raises it there.
23    pub error: Option<DeferredSQLError>,
24}
25
26/// The name `SEQUENCE NAME` gives an identity sequence: its schema, when written, and its name.
27#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28pub struct IdentitySequenceName {
29    pub schema: Option<String>,
30    pub name: String,
31}
32
33/// An error a statement found while compiling, which it raises later, where `PostgreSQL` raises it.
34#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
35pub struct DeferredSQLError {
36    pub sqlstate: String,
37    pub message: String,
38}
39
40impl From<&SQLError> for DeferredSQLError {
41    fn from(error: &SQLError) -> Self {
42        Self {
43            sqlstate: error.sqlstate().unwrap_or("XX000").to_string(),
44            message: error.to_string(),
45        }
46    }
47}
48
49impl From<DeferredSQLError> for SQLError {
50    fn from(error: DeferredSQLError) -> Self {
51        Self::Routine {
52            sqlstate: error.sqlstate,
53            message: error.message,
54        }
55    }
56}