sea_schema/postgres/def/column.rs
1#[cfg(feature = "with-serde")]
2use serde::{Deserialize, Serialize};
3
4use super::{NotNull, Type};
5
6#[derive(Debug, Clone, PartialEq)]
7#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
8pub struct ColumnInfo {
9 /// The name of the column
10 pub name: String,
11 /// The type of the column with any additional definitions such as the precision or the character
12 /// set
13 pub col_type: ColumnType,
14 /// The default value for this column, if any
15 pub default: Option<ColumnDefault>,
16 /// The generation expression for this column, if it is a generated column
17 pub generated: Option<ColumnExpression>,
18 pub not_null: Option<NotNull>,
19 pub is_identity: bool,
20 // TODO:
21 // /// A constraint that ensures the value of a column is unique among all other rows in the table
22 // pub unique: Option<Vec<constraints::Unique>>,
23 // /// A constraint that states that the column is the unique identifier or part of the unique
24 // /// identifier of each row for this table
25 // pub primary_key: Option<constraints::PrimaryKey>,
26 // /// A constraint that ensures that the value of this column must refer to a unique key in another
27 // /// table
28 // pub references: Option<constraints::References>,
29
30 // FIXME: Include if there's a convenient way to look for this
31 // /// Comments on the column made by the user
32 // pub comment: String,
33}
34
35pub type ColumnType = Type;
36
37#[derive(Debug, Clone, PartialEq)]
38#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
39pub enum ColumnDefault {
40 Int(i64),
41 Real(f64),
42 String(String),
43 Bool(bool),
44 CurrentTimestamp,
45 /// A sequence default, e.g. `nextval('table_id_seq'::regclass)`
46 AutoIncrement(String),
47 /// Any other expression not covered by the above variants
48 Expression(String),
49}
50
51#[derive(Debug, Clone, PartialEq)]
52#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
53pub struct ColumnExpression(pub String);
54
55impl ColumnExpression {
56 pub fn from_option_string(maybe_string: Option<String>) -> Option<ColumnExpression> {
57 maybe_string.map(ColumnExpression)
58 }
59}