fire_postgres/table/column/mod.rs
1mod column_type;
2pub use column_type::{ColumnType, FromDataError};
3
4mod data;
5pub use data::ColumnData;
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct Column {
9 pub name: &'static str,
10 pub kind: ColumnKind,
11 pub index: IndexKind,
12}
13
14impl Column {
15 pub fn new<T>(
16 name: &'static str,
17 len: Option<usize>,
18 index: IndexKind,
19 ) -> Self
20 where
21 T: ColumnType,
22 {
23 let mut kind = T::column_kind();
24 if let Some(len) = len {
25 kind = match kind {
26 ColumnKind::Text => ColumnKind::Varchar(len),
27 _ => panic!(
28 "column kind {:?} doenst support len attribute",
29 kind
30 ),
31 };
32 }
33
34 Self { name, kind, index }
35 }
36
37 /*pub fn name(&self) -> &'static str {
38 self.name
39 }
40
41 pub fn kind_string(&self) -> String {
42 self.kind.to_string(self.name)
43 }
44
45 pub fn index_str(&self) -> &'static str {
46 self.index.to_str()
47 }
48
49 pub fn not_null_str(&self) -> &'static str {
50 match self.index {
51 IndexKind::Primary => "",
52 _ => self.kind.not_null_str()
53 }
54 }
55
56 pub fn to_sql(&self) -> String {
57 let index_str = if self.index.is_constraint() {
58 self.index_str()
59 } else {""};
60 format!("{} {} {} {}", self.name(), self.kind_string(), index_str, self.not_null_str())
61 }*/
62}
63
64/*
65ToColumnType
66*/
67
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub enum ColumnKind {
70 Boolean,
71 // Char(usize),
72 Varchar(usize),
73 FixedText(usize),
74 Text,
75 Date,
76 Timestamp,
77 F64,
78 F32,
79 I64,
80 I32,
81 I16,
82 Option(Box<ColumnKind>),
83 TextArray,
84 Bytea,
85 Json,
86}
87
88impl ColumnKind {
89 pub fn short(&self) -> &'static str {
90 match self {
91 Self::Boolean => "boolean",
92 // Self::Char(_) => "char",
93 Self::Varchar(_) => "varchar",
94 Self::FixedText(_) => "text",
95 Self::Text => "text",
96 Self::Date => "date",
97 Self::Timestamp => "timestamp",
98 Self::F64 => "float8",
99 Self::F32 => "float4",
100 Self::I64 => "int8",
101 Self::I32 => "int4",
102 Self::I16 => "int2",
103 Self::Option(t) => t.short(),
104 Self::TextArray => "text []",
105 Self::Bytea => "bytea",
106 Self::Json => "json",
107 }
108 }
109
110 pub fn value(&self, name: &str) -> String {
111 match self {
112 // Self::Char(v) => Some(v.to_string()),
113 Self::Varchar(v) => format!("({})", v),
114 Self::FixedText(v) => format!(" CHECK (length({})={})", name, v),
115 Self::Option(t) => t.value(name),
116 _ => String::new(),
117 }
118 }
119
120 pub fn to_string(&self, name: &str) -> String {
121 format!("{}{}", self.short(), self.value(name))
122 }
123
124 pub fn not_null_str(&self) -> &'static str {
125 match self {
126 Self::Option(_) => "null",
127 _ => "not null",
128 }
129 }
130}
131
132#[derive(Debug, Clone, PartialEq)]
133pub enum IndexKind {
134 Primary,
135 Unique,
136 NamedUnique(&'static str),
137 Index,
138 None,
139}
140
141impl IndexKind {
142 /*pub fn parse(index: Option<&'static str>) -> Self {
143 if index.is_none() {return Self::None}
144 match index.unwrap() {
145 "primary" => Self::Primary,
146 "unique" => Self::Unique,
147 "index" => Self::Index,
148 "none" => Self::None,
149 "" => Self::None,
150 i => panic!("unknown index {}", i)
151 }
152 }*/
153
154 /*pub fn to_str(&self) -> &'static str {
155 match self {
156 Self::Primary => "primary key",
157 Self::Unique => "unique",
158 Self::Index => "index",
159 Self::None => ""
160 }
161 }*/
162
163 /*pub fn is_constraint(&self) -> bool {
164 match self {
165 Self::Primary |
166 Self::Unique => true,
167 _ => false
168 }
169 }*/
170
171 pub fn is_none(&self) -> bool {
172 matches!(self, Self::None)
173 }
174}
175
176/*
177CREATE TABLE account(
178 user_id serial PRIMARY KEY,
179 username VARCHAR (50) UNIQUE NOT NULL,
180 password VARCHAR (50) NOT NULL,
181 email VARCHAR (355) UNIQUE NOT NULL,
182 created_on TIMESTAMP NOT NULL,
183 last_login TIMESTAMP
184);
185
186// UNIQUE
187
188// INDEX
189
190CREATE TABLE account_role
191(
192 user_id integer NOT NULL,
193 role_id integer NOT NULL,
194 grant_date timestamp without time zone,
195 PRIMARY KEY (user_id, role_id),
196 CONSTRAINT account_role_role_id_fkey FOREIGN KEY (role_id)
197 REFERENCES role (role_id) MATCH SIMPLE
198 ON UPDATE NO ACTION ON DELETE NO ACTION,
199 CONSTRAINT account_role_user_id_fkey FOREIGN KEY (user_id)
200 REFERENCES account (user_id) MATCH SIMPLE
201 ON UPDATE NO ACTION ON DELETE NO ACTION
202);
203*/
204
205/*
206all types
207
208bigint int8 signed eight-byte integer
209bigserial serial8 autoincrementing eight-byte integer
210bit [ (n) ] fixed-length bit string
211varbit [ (n) ] variable-length bit string
212boolean bool logical Boolean (true/false)
213box rectangular box on a plane
214bytea binary data ("byte array")
215char [ (n) ] fixed-length character string
216varchar [ (n) ] variable-length character string
217cidr IPv4 or IPv6 network address
218circle circle on a plane
219date calendar date (year, month, day)
220float8 double precision floating-point number (8 bytes)
221inet IPv4 or IPv6 host address
222integer int, int4 signed four-byte integer
223interval [ fields ] [ (p) ] time span
224json textual JSON data
225jsonb binary JSON data, decomposed
226line infinite line on a plane
227lseg line segment on a plane
228macaddr MAC (Media Access Control) address
229money currency amount
230numeric [ (p, s) ] decimal [ (p, s) ] exact numeric of selectable precision
231path geometric path on a plane
232pg_lsn PostgreSQL Log Sequence Number
233point geometric point on a plane
234polygon closed geometric path on a plane
235real float4 single precision floating-point number (4 bytes)
236smallint int2 signed two-byte integer
237smallserial serial2 autoincrementing two-byte integer
238serial serial4 autoincrementing four-byte integer
239text variable-length character string
240time [ (p) ] [ without time zone ] time of day (no time zone)
241time [ (p) ] with time zone timetz time of day, including time zone
242timestamp [ (p) ] [ without time zone ] date and time (no time zone)
243timestamp [ (p) ] with time zone timestamptz date and time, including time zone
244tsquery text search query
245tsvector text search document
246txid_snapshot user-level transaction ID snapshot
247uuid universally unique identifier
248xml XML data
249*/