elefant_tools/models/
column.rs1use crate::quoting::{AttemptedKeywordUsage, IdentifierQuoter, Quotable};
2use crate::{PostgresSchema, PostgresTable};
3use serde::{Deserialize, Serialize};
4use AttemptedKeywordUsage::Other;
5
6#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
7pub struct PostgresColumn {
8 pub name: String,
9 pub ordinal_position: i32,
10 pub is_nullable: bool,
11 pub data_type: String,
12 pub default_value: Option<String>,
13 pub generated: Option<String>,
14 pub comment: Option<String>,
15 pub array_dimensions: i32,
16 pub data_type_length: Option<i32>,
17}
18
19impl PostgresColumn {
20 pub fn get_alter_table_set_default_statement(
21 &self,
22 table: &PostgresTable,
23 schema: &PostgresSchema,
24 identifier_quoter: &IdentifierQuoter,
25 ) -> Option<String> {
26 self.default_value.as_ref().map(|default_value| {
27 format!(
28 "alter table {}.{} alter column {} set default {};",
29 schema.name.quote(identifier_quoter, Other),
30 table.name.quote(identifier_quoter, Other),
31 self.name.quote(identifier_quoter, Other),
32 default_value
33 )
34 })
35 }
36}
37
38impl PostgresColumn {
39 pub fn get_simplified_data_type(&self) -> SimplifiedDataType {
40 if self.array_dimensions > 0 {
41 return SimplifiedDataType::Text;
42 }
43 match self.data_type.as_str() {
44 "int2" | "int4" | "int8" | "float4" | "float8" => SimplifiedDataType::Number,
45 "boolean" => SimplifiedDataType::Bool,
46 _ => SimplifiedDataType::Text,
47 }
48 }
49}
50
51impl Default for PostgresColumn {
52 fn default() -> Self {
53 Self {
54 name: "".to_string(),
55 ordinal_position: 0,
56 is_nullable: true,
57 data_type: "".to_string(),
58 default_value: None,
59 generated: None,
60 comment: None,
61 array_dimensions: 0,
62 data_type_length: None,
63 }
64 }
65}
66
67#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
68pub enum SimplifiedDataType {
69 Number,
70 Text,
71 Bool,
72}