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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::{
    fmt::{
        Debug,
        Display,
    },
    rc::Rc,
    ops::Deref,
};
use crate::{
    sqlite::{
        types::{
            SimpleSimpleType,
            SimpleType,
            Type,
        },
        query::{
            expr::{
                Expr,
            },
        },
    },
};
use super::table::{
    Table,
};

#[derive(Clone, Debug)]
pub struct FieldType {
    pub type_: Type,
    pub migration_default: Option<Expr>,
}

impl FieldType {
    /// Create a field type from the specified value type.
    pub fn with(t: &Type) -> Self {
        Self {
            type_: t.clone(),
            migration_default: None,
        }
    }

    /// Create a field type from the specified value type, and provide a migration fill
    /// value.
    pub fn with_migration(t: &Type, def: Option<Expr>) -> Self {
        if t.opt {
            panic!("Optional fields can't have defaults.");
        }
        Self {
            type_: t.clone(),
            migration_default: def,
        }
    }
}

pub struct FieldBuilder {
    t: SimpleSimpleType,
    default_: Option<Expr>,
    opt: bool,
    custom: Option<String>,
}

impl FieldBuilder {
    fn new(t: SimpleSimpleType) -> FieldBuilder {
        FieldBuilder {
            t: t,
            opt: false,
            default_: None,
            custom: None,
        }
    }

    /// Make the field optional.
    pub fn opt(mut self) -> FieldBuilder {
        if self.default_.is_some() {
            panic!("Optional fields can't have migration fill expressions.");
        }
        self.opt = true;
        self
    }

    /// Specify an expression to use to populate the new column in existing rows. This
    /// is must be specified (only) for non-opt fields in a new version of an existing
    /// table.
    pub fn migrate_fill(mut self, expr: Expr) -> FieldBuilder {
        if self.opt {
            panic!("Optional fields can't have migration fill expressions.");
        }
        self.default_ = Some(expr);
        self
    }

    /// Use a custom Rust type for this field. This must be the full path to the type,
    /// like `crate::abcdef::MyType`.
    pub fn custom(mut self, type_: impl ToString) -> FieldBuilder {
        self.custom = Some(type_.to_string());
        self
    }

    pub fn build(self) -> FieldType {
        FieldType {
            type_: Type {
                type_: SimpleType {
                    custom: self.custom,
                    type_: self.t,
                },
                opt: self.opt,
            },
            migration_default: self.default_,
        }
    }
}

pub fn field_bool() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::Bool)
}

pub fn field_i32() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::I32)
}

pub fn field_i64() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::I64)
}

pub fn field_u32() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::U32)
}

pub fn field_f32() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::F32)
}

pub fn field_f64() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::F64)
}

pub fn field_str() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::String)
}

pub fn field_bytes() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::Bytes)
}

#[cfg(feature = "chrono")]
pub fn field_utctime_s() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::UtcTimeS)
}

#[cfg(feature = "chrono")]
pub fn field_utctime_ms() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::UtcTimeMs)
}

#[cfg(feature = "chrono")]
pub fn field_fixed_offset_time_ms() -> FieldBuilder {
    FieldBuilder::new(SimpleSimpleType::FixedOffsetTimeMs)
}

#[derive(Clone, Eq, PartialEq, Hash, Debug, PartialOrd, Ord)]
pub struct SchemaFieldId(pub String);

impl Display for SchemaFieldId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.0, f)
    }
}

#[derive(Debug)]
pub struct Field_ {
    pub table: Table,
    pub schema_id: SchemaFieldId,
    pub id: String,
    pub type_: FieldType,
}

#[derive(Clone, Debug)]
pub struct Field(pub Rc<Field_>);

impl std::hash::Hash for Field {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.schema_id.hash(state)
    }
}

impl PartialEq for Field {
    fn eq(&self, other: &Self) -> bool {
        self.table == other.table && self.schema_id == other.schema_id
    }
}

impl Eq for Field { }

impl Deref for Field {
    type Target = Field_;

    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl Display for Field {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&format!("{}.{} ({}.{})", self.table.id, self.id, self.table.schema_id.0, self.schema_id.0), f)
    }
}