chuchi_postgres/table/column/
mod.rs

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