1use crate::{Dialect, ToSql, Type};
2use crate::query::Expr;
3use crate::schema::constraint::Constraint;
4use crate::util::SqlExtension;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Column {
9 pub name: String,
10 pub typ: Type,
11 pub nullable: bool,
12 pub primary_key: bool,
13 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
14 pub default: Option<Expr>,
15 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
16 pub constraint: Option<Constraint>
17}
18
19
20impl ToSql for Column {
21 fn write_sql(&self, buf: &mut String, dialect: Dialect) {
22 buf.push_quoted(&self.name);
23 buf.push(' ');
24 buf.push_str(&self.typ.to_sql(dialect));
25 if !self.nullable {
26 buf.push_str(" NOT NULL");
27 }
28 if self.primary_key {
29 buf.push_str(" PRIMARY KEY");
30 }
31 if let Some(default) = &self.default {
32 buf.push_str(" DEFAULT ");
33 buf.push_sql(default, dialect);
34 }
35 if let Some(constraint) = &self.constraint {
36 buf.push(' ');
37 buf.push_sql(constraint, dialect);
38 }
39 }
40}