feophantlib/engine/objects/
attribute.rs

1//!Postgres Doc: https://www.postgresql.org/docs/current/catalog-pg-attribute.html
2
3use super::types::BaseSqlTypesMapper;
4use crate::constants::Nullable;
5use std::fmt;
6
7#[derive(Clone, Debug, PartialEq)]
8pub struct Attribute {
9    pub name: String,                 //Column Name
10    pub sql_type: BaseSqlTypesMapper, //Underlying type
11    pub nullable: Nullable,           //Null constraint
12    pub length: Option<usize>,        //Length of variable length columns - constraint
13}
14
15impl Attribute {
16    pub fn new(
17        name: String,
18        sql_type: BaseSqlTypesMapper,
19        nullable: Nullable,
20        length: Option<usize>, //TODO make use of the length for certain types
21    ) -> Attribute {
22        Attribute {
23            name,
24            sql_type,
25            nullable,
26            length,
27        }
28    }
29}
30
31impl fmt::Display for Attribute {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        write!(
34            f,
35            "Attribute name: {0}, sql_type: {1}, nullable: {2}, length: {3:?}",
36            self.name, self.sql_type, self.nullable, self.length
37        )
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_attribute_newfmt() -> Result<(), Box<dyn std::error::Error>> {
47        let test = Attribute::new(
48            "test".to_string(),
49            BaseSqlTypesMapper::Text,
50            Nullable::NotNull,
51            None,
52        );
53        assert_eq!(
54            test.to_string(),
55            "Attribute name: test, sql_type: Text, nullable: NotNull, length: None".to_string()
56        );
57
58        Ok(())
59    }
60}