Skip to main content

datasources_sqlite/
sql_generate.rs

1use datasources::{
2    DefaultValueConverter, DefaultValueOptions, DeterministicTypeConverter, SpecFieldRules,
3};
4use regex::Regex;
5use std::collections::HashMap;
6use std::sync::LazyLock;
7
8pub const QUOTE_LEFT: &str = "\"";
9pub const QUOTE_RIGHT: &str = "\"";
10
11fn string_sized(n: i32) -> String {
12    format!("VARCHAR({n})")
13}
14
15pub static TYPES: LazyLock<DeterministicTypeConverter> = LazyLock::new(|| {
16    DeterministicTypeConverter::new(
17        HashMap::from([
18            ("string".into(), "TEXT".into()),
19            ("character".into(), "TEXT".into()),
20            ("number".into(), "INTEGER".into()),
21            ("integer".into(), "INTEGER".into()),
22            ("unsignedinteger".into(), "INTEGER".into()),
23            ("smallinteger".into(), "INTEGER".into()),
24            ("unsignedsmallinteger".into(), "INTEGER".into()),
25            ("biginteger".into(), "INTEGER".into()),
26            ("unsignedbiginteger".into(), "INTEGER".into()),
27            ("float".into(), "REAL".into()),
28            ("decimal".into(), "NUMERIC".into()),
29            ("boolean".into(), "BOOLEAN".into()),
30            ("datetime".into(), "TEXT".into()),
31            ("binary".into(), "BLOB".into()),
32            ("uuid".into(), "TEXT".into()),
33            ("reference".into(), "INTEGER".into()),
34        ]),
35        SpecFieldRules {
36            dialect_name: Some("sqlite".into()),
37            string_unlimited: "TEXT".into(),
38            string_sized,
39            character: None,
40            decimal: None,
41            binary_unlimited: None,
42            binary_sized: None,
43        },
44    )
45});
46
47pub static DEFAULTS: LazyLock<DefaultValueConverter> = LazyLock::new(|| {
48    DefaultValueConverter::new(DefaultValueOptions {
49        new_id: || String::new(),
50        now: || "(strftime('%Y-%m-%dT%H:%M:%f', 'now', 'localtime'))".into(),
51        utc_now: || "(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))".into(),
52        boolean_on: "1".into(),
53        boolean_off: "0".into(),
54        hex: |arg| format!("X'{arg}'"),
55        hex_reverse: Regex::new(r"^X'(.*)'$").expect("hex"),
56    })
57});