1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use {
	crate::ValueType,
	serde::{Deserialize, Serialize},
	sqlparser::{
		ast::{ColumnDef, ColumnOption, ColumnOptionDef, Expr, Ident},
		dialect::keywords::Keyword,
		tokenizer::{Token, Word},
	},
	std::fmt::Debug,
};

#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct Column {
	pub name: String,
	pub data_type: ValueType,
	pub default: Option<ValueDefault>,

	pub is_nullable: bool,
	pub is_unique: bool,
}

impl From<&ColumnDef> for Column {
	fn from(column_def: &ColumnDef) -> Self {
		column_def.clone().into()
	}
}
impl From<ColumnDef> for Column {
	fn from(column_def: ColumnDef) -> Self {
		let ColumnDef {
			name: Ident { value: name, .. },
			data_type,
			options,
			..
		} = column_def;

		let is_nullable = options
			.iter()
			.any(|ColumnOptionDef { option, .. }| matches!(option, ColumnOption::Null));

		let is_unique = options
			.iter()
			.any(|ColumnOptionDef { option, .. }| matches!(option, ColumnOption::Unique { .. }));

		let default = options
			.iter()
			.find_map(|ColumnOptionDef { option, .. }| match option {
				ColumnOption::Default(expr) => Some(ValueDefault::Recipe(expr.clone())),
				ColumnOption::DialectSpecific(tokens)
					if matches!(
						tokens[..],
						[
							Token::Word(Word {
								keyword: Keyword::AUTO_INCREMENT,
								..
							}),
							..
						]
					) =>
				{
					Some(ValueDefault::AutoIncrement(1))
				}
				_ => None,
			});

		Self {
			name,
			data_type: data_type.into(),
			default,
			is_nullable,
			is_unique,
		}
	}
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum ValueDefault {
	Recipe(Expr), // TODO: Recipe serialisation
	AutoIncrement(u64),
}