welds 0.5.0

An async ORM for (postgres, mssql, mysql, sqlite)
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::Syntax;

#[derive(Debug, Clone)]
pub struct Pair {
    db_type: &'static str,
    rust_type: &'static str,
    // if true this pair shouldn't be used in a migration unless it is a PK
    id_only: bool,
    // If true a size is required to make this type in the DB
    sized: bool,
    // If true a size is required to make this type in the DB
    default_size: Option<&'static str>,
    // match on the array version of this pair not the two types
    array: bool,
}

impl Pair {
    const fn new(db_type: &'static str, rust_type: &'static str) -> Pair {
        Pair {
            db_type,
            rust_type,
            id_only: false,
            sized: false,
            default_size: None,
            array: false,
        }
    }

    /// Used to say the db type includes a size value.
    /// This is important to know when checking if the
    /// DB type matches the models fields
    const fn sized(
        db_type: &'static str,
        rust_type: &'static str,
        default_size: &'static str,
    ) -> Pair {
        Pair {
            db_type,
            rust_type,
            id_only: false,
            sized: true,
            default_size: Some(default_size),
            array: false,
        }
    }

    /// Used to to say this pair should only be used on ID fields
    /// This way the migrations will not pick this match unless
    /// it is looking for a primary key type
    const fn key_only(db_type: &'static str, rust_type: &'static str) -> Pair {
        Pair {
            db_type,
            rust_type,
            id_only: true,
            sized: false,
            default_size: None,
            array: false,
        }
    }

    /// returns true is this Pair should ONLY be used in a PK column type.
    pub fn id_only(&self) -> bool {
        self.id_only
    }

    // returns true if the DB type of this pair requires a size. Example: VARCHAR(255)
    pub fn db_sized(&self) -> bool {
        self.sized
    }

    // Returns a reasonable default value to use with a given DB type if it is size.
    // This is used when creating migrations and the use don't pick a size
    pub fn default_size(&self) -> Option<&'static str> {
        self.default_size
    }

    pub fn db_type(&self) -> String {
        if self.array {
            format!("{}[]", self.db_type)
        } else {
            self.db_type.to_owned()
        }
    }

    pub fn rust_type(&self) -> String {
        let base = match self.rust_type.rfind(':') {
            Some(index) => &self.rust_type[index + 1..],
            None => self.rust_type,
        };
        if self.array {
            format!("Vec<{}>", base)
        } else {
            base.to_owned()
        }
    }

    /// Returns a full rust type with a namespace if there is one
    pub fn full_rust_type(&self) -> String {
        if self.array {
            format!("Vec<{}>", self.rust_type)
        } else {
            self.rust_type.to_owned()
        }
    }

    /// returns true is this Pair matches a db_type and rust_type
    pub fn matches(&self, db: &str, rust: &str) -> bool {
        self.db_type() == db && self.is_rust_type(rust)
    }

    /// Returns true the Pair matches a given rust type
    pub fn is_rust_type(&self, rust: &str) -> bool {
        let self_rust_type = self.rust_type();
        if self_rust_type == rust {
            return true;
        }
        // ignoring the namespace
        let rust_typeonly = match rust.rfind(':') {
            Some(index) => &rust[index + 1..],
            None => rust,
        };
        if self_rust_type == rust_typeonly {
            return true;
        }
        // not a match
        false
    }
}

/// Returns a list of DB_TYPE and RUST_TYPE pairs.
/// A pair can be assumed to be usable together in welds
/// I.E.  INT <=> i32
/// a model with a type i32 will work with a db_type of INT
pub fn get_pairs(syntax: Syntax) -> Vec<Pair> {
    let base_pairs = get_basic_type_pairs(syntax);
    let arrays = base_pairs.iter().map(|p| {
        let mut a = p.clone();
        a.array = true;
        a
    });
    base_pairs.iter().cloned().chain(arrays).collect()
}

/// Same as get_pairs but doesn't include arrays types
/// INT[] <=> Vec<i32>
pub fn get_basic_type_pairs(syntax: Syntax) -> &'static [Pair] {
    match syntax {
        Syntax::Postgres => POSTGRES_PAIRS,
        Syntax::Sqlite => SQLITE_PAIRS,
        Syntax::Mysql => MYSQL_PAIRS,
        Syntax::Mssql => MSSQL_PAIRS,
    }
}

/***************************************************************************
 *
 * WARNING: ORDER MATTERS!!!
 * these pairs of type are used to pick the best type when making migrations
 * The high up in the list, it will get match first.
 *
 * **********************************************************************/

const MSSQL_PAIRS: &[Pair] = &[
    Pair::new("INT", "i32"),
    Pair::new("BIT", "bool"),
    Pair::new("SMALLINT", "i16"),
    Pair::new("BIGINT", "i64"),
    Pair::new("FLOAT(24)", "f32"),
    Pair::new("FLOAT(53)", "f64"),
    Pair::sized("NVARCHAR", "String", "MAX"),
    Pair::sized("VARCHAR", "String", "MAX"),
    Pair::new("TEXT", "String"),
    Pair::sized("VARBINARY", "Vec<u8>", "MAX"),
    Pair::new("UNIQUEIDENTIFIER", "uuid::Uuid"),
    Pair::sized("NVARCHAR", "serde_json::Value", "MAX"),
    Pair::new("DATETIMEOFFSET", "chrono::DateTime<chrono::Utc>"),
    Pair::new("DATETIME2", "chrono::NaiveDateTime"),
    Pair::new("DATE", "chrono::NaiveDate"),
    Pair::new("TIME", "chrono::NaiveTime"),
];

const MYSQL_PAIRS: &[Pair] = &[
    Pair::new("TINYINT(1)", "bool"),
    Pair::new("BOOLEAN", "bool"),
    Pair::new("TINYINT", "i8"),
    Pair::new("SMALLINT", "i16"),
    Pair::new("INT", "i32"),
    Pair::new("BIGINT", "i64"),
    Pair::new("TINYINT UNSIGNED", "u8"),
    Pair::new("SMALLINT UNSIGNED", "u16"),
    Pair::new("INT UNSIGNED", "u32"),
    Pair::new("BIGINT UNSIGNED", "u64"),
    Pair::new("FLOAT", "f32"),
    Pair::new("DOUBLE", "f64"),
    Pair::new("VARCHAR(255)", "String"),
    Pair::new("TEXT", "String"),
    Pair::sized("VARCHAR", "String", "255"),
    Pair::sized("CHAR ", "String", "255"),
    Pair::new("BLOB", "Vec<u8>"),
    Pair::new("TINYBLOB", "Vec<u8>"),
    Pair::new("MEDIUMBLOB", "Vec<u8>"),
    Pair::new("LONGBLOB", "Vec<u8>"),
    Pair::sized("BINARY", "Vec<u8>", "255"),
    Pair::sized("VARBINARY", "Vec<u8>", "255"),
    Pair::new("TIMESTAMP", "chrono::DateTime<chrono::Utc>"),
    Pair::new("DATETIME", "chrono::NaiveDateTime"),
    Pair::new("DATE", "chrono::NaiveDate"),
    Pair::new("TIME", "chrono::NaiveTime"),
    Pair::new("VARCHAR(36)", "uuid::Uuid"),
    Pair::new("JSON", "serde_json::Value"),
];

const SQLITE_PAIRS: &[Pair] = &[
    Pair::new("BOOLEAN", "bool"),
    Pair::new("BOOL", "bool"),
    Pair::new("INTEGER", "i32"),
    Pair::new("INTEGER", "i64"),
    Pair::new("BIGINT", "i64"),
    Pair::new("INT8", "i64"),
    Pair::new("INT2", "i64"),
    Pair::new("INT", "i64"),
    Pair::new("TINYINT", "i64"),
    Pair::new("SMALLINT", "i64"),
    Pair::new("MEDIUMINT", "i64"),
    Pair::new("UNSIGNED BIG INT", "i64"),
    Pair::new("REAL", "f64"),
    Pair::new("DOUBLE", "f64"),
    Pair::new("DOUBLE PRECISION", "f64"),
    Pair::new("FLOAT", "f64"),
    Pair::new("TEXT", "String"),
    Pair::new("CHAR", "String"),
    Pair::new("VARCHAR", "String"),
    Pair::new("VARYING CHARACTER", "String"),
    Pair::new("NCHAR", "String"),
    Pair::new("NATIVE CHARACTER", "String"),
    Pair::new("NVARCHAR", "String"),
    Pair::new("CLOB", "String"),
    Pair::new("BLOB", "Vec<u8>"),
    Pair::new("NUMERIC", "rust_decimal::Decimal"),
    Pair::new("DECIMAL", "rust_decimal::Decimal"),
    Pair::new("DATETIME", "chrono::NaiveDateTime"),
    Pair::new("DATETIME", "chrono::DateTime<chrono::Utc>"),
    Pair::new("DATETIME", "chrono::DateTime<Utc>"),
    Pair::new("DATE", "chrono::NaiveDate"),
    Pair::new("TIME", "chrono::NaiveTime"),
    // These Pairs are here so all rust int types have a match
    Pair::new("INTEGER", "i16"),
    Pair::new("INTEGER", "i32"),
    Pair::new("REAL", "f32"),
    Pair::new("TEXT", "uuid::Uuid"),
    Pair::new("TEXT", "serde_json::Value"),
];

const POSTGRES_PAIRS: &[Pair] = &[
    Pair::key_only("SERIAL", "i32"),
    Pair::key_only("BIGSERIAL", "i64"),
    Pair::key_only("SMALLSERIAL", "i16"),
    Pair::new("UUID", "uuid::Uuid"),
    Pair::new("BOOL", "bool"),
    Pair::new("CHAR", "i8"), // not sized, single char
    Pair::new("INT2", "i16"),
    Pair::new("SMALLINT", "i16"),
    Pair::new("INT", "i32"),
    Pair::new("INT4", "i32"),
    Pair::new("BIGINT", "i64"),
    Pair::new("INT8", "i64"),
    Pair::new("REAL", "f32"),
    Pair::new("FLOAT4", "f32"),
    Pair::new("DOUBLE PRECISION", "f64"),
    Pair::new("FLOAT8", "f64"),
    Pair::new("TEXT", "String"),
    Pair::sized("VARCHAR", "String", "255"),
    Pair::sized("CHAR", "String", "255"),
    Pair::new("NAME", "String"),
    Pair::new("BYTEA", "Vec<u8>"),
    Pair::new("BLOB", "Vec<u8>"),
    Pair::new("INTERVAL", "sqlx::postgres::types::PgInterval"),
    Pair::new("MONEY", "sqlx::postgres::types::PgMoney"),
    Pair::new("INT4RANGE", "sqlx::postgres::types::PgRange<i32>"),
    Pair::new("INT8RANGE", "sqlx::postgres::types::PgRange<i64>"),
    Pair::new("TIMESTAMPTZ", "chrono::DateTime<chrono::Utc>"),
    Pair::new("TIMESTAMPTZ", "chrono::DateTime<Utc>"),
    Pair::new("TIMESTAMP", "chrono::NaiveDateTime"),
    Pair::new("DATE", "chrono::NaiveDate"),
    Pair::new("TIME", "chrono::NaiveTime"),
    Pair::new("TIMETZ", "sqlx::postgres::types::PgTimeTz"),
    Pair::new("JSONB", "serde_json::Value"),
];

/// Returns true if two types are a match
#[allow(dead_code)]
pub(crate) fn are_equivalent_types(pairs: &[Pair], db: &str, rust: &str) -> bool {
    let db = db.trim().to_uppercase();
    for pair in pairs {
        if pair.matches(&db, rust) {
            return true;
        }
    }
    false
}

/// Override the type of a column to the type that should be used
/// to crate its PK value. e.g.
#[allow(dead_code)]
pub(crate) fn pk_override(syntax: Syntax, db_type: &str) -> Option<&'static str> {
    // Use the Serial type to create the PKs, the type will be reported back as int..
    if let Syntax::Postgres = syntax {
        match db_type {
            "INT2" => return Some("SMALLSERIAL"),
            "SMALLINT" => return Some("SMALLSERIAL"),
            "INT" => return Some("SERIAL"),
            "INT4" => return Some("SERIAL"),
            "BIGINT" => return Some("BIGSERIAL"),
            "INT8" => return Some("BIGSERIAL"),
            _ => {}
        }
    }
    // Sqlite uses dynamic sizes for int, should always be INTEGER in pk create
    if let Syntax::Sqlite = syntax {
        match db_type {
            "INT4" => return Some("INTEGER"),
            "INT8" => return Some("INTEGER"),
            "INT" => return Some("INTEGER"),
            "BIGINT" => return Some("INTEGER"),
            "INTSMALL" => return Some("INTEGER"),
            _ => {}
        }
    }
    None
}

/// Returns the recommenced rust type to use for a given Database type.
pub fn recommended_rust_type(syntax: Syntax, db_type: &str) -> Option<Pair> {
    // find the root of the type VARCHAR from VARCHAR(MAX)
    let base = match db_type.find('(') {
        Some(index) => &db_type[..index],
        None => db_type,
    };

    let pairs = get_pairs(syntax);
    let db_type = db_type.trim().to_uppercase();
    for pair in pairs {
        let pair_type = pair.db_type();
        if base == pair_type || db_type == pair_type {
            return Some(pair);
        }
    }
    None
}

/// Returns the recommended rust type to use for a given Database type.
pub fn recommended_db_type(syntax: Syntax, rust_type: &str) -> Option<Pair> {
    let pairs = get_pairs(syntax);
    let pairs = pairs.iter().filter(|&x| !x.id_only());
    let ty = rust_type.trim();
    for pair in pairs {
        if pair.is_rust_type(ty) {
            return Some(pair.clone());
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_equivalent_types() {
        let pairs = get_pairs(Syntax::Postgres);
        assert!(!are_equivalent_types(&pairs, "INT4", "i64"));
        assert!(are_equivalent_types(&pairs, "INT4", "i32"));
        assert!(!are_equivalent_types(&pairs, "SERIAL", "String"));
        assert!(are_equivalent_types(&pairs, "BIGSERIAL", "i64"));
        assert!(are_equivalent_types(&pairs, "BIGINT[]", "Vec<i64>"));
        assert!(!are_equivalent_types(&pairs, "BIGINT", "Vec<i64>"));
        assert!(are_equivalent_types(&pairs, "VARCHAR", "String"));
        assert!(!are_equivalent_types(&pairs, "FLOAT", "String"));
        assert!(are_equivalent_types(&pairs, "MONEY", "PgMoney"));
    }

    #[test]
    fn should_recommend_good_rust_types() {
        let s = Syntax::Postgres;
        assert_eq!(
            recommended_rust_type(s, "Int").unwrap().full_rust_type(),
            "i32"
        );
        assert_eq!(
            recommended_rust_type(s, "text").unwrap().full_rust_type(),
            "String"
        );
        assert_eq!(
            recommended_rust_type(s, "bool").unwrap().full_rust_type(),
            "bool"
        );
        let s = Syntax::Sqlite;
        assert_eq!(
            recommended_rust_type(s, "SMALLINT")
                .unwrap()
                .full_rust_type(),
            "i64"
        );
        assert_eq!(
            recommended_rust_type(s, "BIGINT").unwrap().full_rust_type(),
            "i64"
        );
        let s = Syntax::Mssql;
        assert_eq!(
            recommended_rust_type(s, "VARCHAR(MAX)")
                .unwrap()
                .full_rust_type(),
            "String"
        );
        assert_eq!(
            recommended_rust_type(s, "TEXT").unwrap().full_rust_type(),
            "String"
        );
    }

    #[test]
    fn should_recommend_good_db_types() {
        let s = Syntax::Postgres;
        assert_eq!(recommended_db_type(s, "i32").unwrap().db_type(), "INT");
        assert_eq!(recommended_db_type(s, "String").unwrap().db_type(), "TEXT");
        assert_eq!(recommended_db_type(s, "bool").unwrap().db_type(), "BOOL");
        let s = Syntax::Sqlite;
        assert_eq!(recommended_db_type(s, "i64").unwrap().db_type(), "INTEGER");
        assert_eq!(recommended_db_type(s, "i16").unwrap().db_type(), "INTEGER");
    }
}