boxdb 0.7.0

An extended SQLite client with typed columns and readable storage
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
use crate::{Error, Result};
use bigdecimal::BigDecimal;
use num_traits::FromPrimitive;
use serde::{Deserialize, Serialize};
use sqlparser::{
    dialect::SQLiteDialect,
    tokenizer::{Token, Tokenizer},
};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Value {
    Null,
    Integer(i64),
    UnsignedInteger(u64),
    Real(f64),
    Decimal(String),
    Boolean(bool),
    Text(String),
    Bytea(Vec<u8>),
    Uuid(String),
    Json(serde_json::Value),
    /// An RFC 3339 instant, normalized to UTC with microsecond precision.
    Instant(String),
    Date(String),
    /// Time of day without a date or time zone, with microsecond precision.
    Time(String),
    /// Calendar date and time without a time zone, with microsecond precision.
    DateTime(String),
    /// Separate signed months, days and microseconds.
    Interval(String),
    /// A validated SQLite changeset or patchset.
    Changeset(Vec<u8>),
    Array(Vec<Value>),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Type {
    Int8,
    #[serde(alias = "SmallInt")]
    Int16,
    #[serde(alias = "Integer")]
    Int32,
    #[serde(alias = "BigInt")]
    Int64,
    Uint8,
    Uint16,
    Uint32,
    Uint64,
    Double,
    Decimal(Option<(u32, u32)>),
    Boolean,
    Text,
    Varchar(Option<usize>),
    Enum(Vec<String>),
    Bytea,
    Uuid,
    Json,
    Instant,
    Date,
    Time,
    DateTime,
    Interval,
    Changeset,
    Array(Box<Type>),
}

impl Type {
    pub(crate) fn native_kind(&self) -> Option<libsqlite3_sys::native::Kind> {
        use libsqlite3_sys::native::Kind;
        match self {
            Self::Decimal(_) => Some(Kind::Decimal),
            Self::Uuid => Some(Kind::Uuid),
            Self::Uint8 => Some(Kind::Uint8),
            Self::Uint16 => Some(Kind::Uint16),
            Self::Uint32 => Some(Kind::Uint32),
            Self::Uint64 => Some(Kind::Uint64),
            Self::Instant => Some(Kind::Instant),
            Self::Date => Some(Kind::Date),
            Self::Time => Some(Kind::Time),
            Self::DateTime => Some(Kind::DateTime),
            Self::Interval => Some(Kind::Interval),
            _ => None,
        }
    }

    pub fn parse(input: &str) -> Result<Self> {
        Self::parse_nested(input, 0)
    }

    fn parse_nested(input: &str, depth: usize) -> Result<Self> {
        if input.len() > 4096 || depth > 32 {
            return Err(Error::new("type nesting or length exceeds limit".into()));
        }
        let input = input.trim();
        let ty = input.to_ascii_uppercase();
        if let Some(inner) = input.strip_suffix("[]") {
            return Ok(Self::Array(Box::new(Self::parse_nested(inner, depth + 1)?)));
        }
        if ty.starts_with("ARRAY<") && input.ends_with('>') {
            return Ok(Self::Array(Box::new(Self::parse_nested(
                &input[6..input.len() - 1],
                depth + 1,
            )?)));
        }
        if ty.starts_with("ENUM") {
            let tokens = Tokenizer::new(&SQLiteDialect {}, input)
                .tokenize()
                .map_err(Error::validation)?
                .into_iter()
                .filter(|t| !matches!(t, Token::Whitespace(_)))
                .collect::<Vec<_>>();
            let invalid = || {
                Error::new("expected ENUM with a nonempty list of unique string literals".into())
            };
            let [Token::Word(name), Token::LParen, rest @ .., Token::RParen] = tokens.as_slice()
            else {
                return Err(invalid());
            };
            if name.quote_style.is_some()
                || !name.value.eq_ignore_ascii_case("ENUM")
                || rest.is_empty()
            {
                return Err(invalid());
            }
            let mut labels = vec![];
            for (i, token) in rest.iter().enumerate() {
                match (i % 2, token) {
                    (0, Token::SingleQuotedString(label)) => labels.push(label.clone()),
                    (1, Token::Comma) => (),
                    _ => return Err(invalid()),
                }
            }
            if rest.len() % 2 == 0 {
                return Err(invalid());
            }
            let result = Self::Enum(labels);
            result.validate(&Value::Null)?;
            return Ok(result);
        }
        let (base, args) = if let Some((base, tail)) = ty.split_once('(') {
            let tail = tail
                .strip_suffix(')')
                .ok_or_else(|| Error::new("invalid type parameters".into()))?;
            let args = tail
                .split(',')
                .map(|s| {
                    s.trim().parse::<u32>().map_err(|error| {
                        Error::new("invalid type parameter".into()).with_source(error)
                    })
                })
                .collect::<Result<Vec<_>>>()?;
            (base.trim(), args)
        } else {
            (ty.as_str(), vec![])
        };
        let result = match base {
            "INT8" => Self::Int8,
            "INT16" => Self::Int16,
            "INT32" => Self::Int32,
            "INT64" | "INTEGER" => Self::Int64,
            "UINT8" => Self::Uint8,
            "UINT16" => Self::Uint16,
            "UINT32" => Self::Uint32,
            "UINT64" => Self::Uint64,
            "REAL" | "DOUBLE" => Self::Double,
            "DECIMAL" => {
                if args.is_empty() {
                    Self::Decimal(None)
                } else {
                    let p = args[0];
                    let s = *args.get(1).unwrap_or(&0);
                    if args.len() > 2 || p == 0 || p > 1000 || s > p {
                        return Err(Error::new("invalid DECIMAL precision/scale".into()));
                    }
                    Self::Decimal(Some((p, s)))
                }
            }
            "BOOLEAN" | "BOOL" => Self::Boolean,
            "TEXT" => Self::Text,
            "VARCHAR" => {
                if args.len() > 1 || args.first() == Some(&0) {
                    return Err(Error::new("invalid VARCHAR length".into()));
                }
                Self::Varchar(args.first().map(|n| *n as usize))
            }
            "BYTEA" | "BLOB" => Self::Bytea,
            "UUID" => Self::Uuid,
            "JSON" => Self::Json,
            "INSTANT" => Self::Instant,
            "DATE" => Self::Date,
            "TIME" => Self::Time,
            "DATETIME" => Self::DateTime,
            "INTERVAL" => Self::Interval,
            "CHANGESET" => Self::Changeset,
            _ => return Err(Error::new(format!("unsupported or missing type: {input}"))),
        };
        if !args.is_empty() && !matches!(result, Self::Decimal(_) | Self::Varchar(_)) {
            return Err(Error::new("type does not accept parameters".into()));
        }
        Ok(result)
    }
    pub fn validate(&self, value: &Value) -> Result<Value> {
        if let Self::Enum(labels) = self
            && (labels.is_empty()
                || labels
                    .iter()
                    .collect::<std::collections::BTreeSet<_>>()
                    .len()
                    != labels.len())
        {
            return Err(Error::new(
                "ENUM requires a nonempty list of unique labels".into(),
            ));
        }
        if matches!(self,Self::Decimal(Some((p,s))) if *p==0 || *p>1000 || s>p)
            || matches!(self, Self::Varchar(Some(0)))
        {
            return Err(Error::new("invalid type parameters".into()));
        }
        if let Self::Array(inner) = self {
            inner.validate(&Value::Null)?;
        }
        if matches!(value, Value::Null) {
            return Ok(Value::Null);
        }
        let invalid = || Error::new(format!("type mismatch: expected {self:?}, got {value:?}"));
        match (self, value) {
            (Self::Int8, Value::Integer(n)) if i8::try_from(*n).is_ok() => Ok(value.clone()),
            (Self::Int16, Value::Integer(n)) if i16::try_from(*n).is_ok() => Ok(value.clone()),
            (Self::Int32, Value::Integer(n)) if i32::try_from(*n).is_ok() => Ok(value.clone()),
            (Self::Int64, Value::Integer(_)) => Ok(value.clone()),
            (Self::Uint8 | Self::Uint16 | Self::Uint32 | Self::Uint64, Value::Integer(n)) => {
                let n = u64::try_from(*n).map_err(|error| invalid().with_source(error))?;
                self.validate(&Value::UnsignedInteger(n))
            }
            (Self::Uint8, Value::UnsignedInteger(n)) if u8::try_from(*n).is_ok() => {
                Ok(value.clone())
            }
            (Self::Uint16, Value::UnsignedInteger(n)) if u16::try_from(*n).is_ok() => {
                Ok(value.clone())
            }
            (Self::Uint32, Value::UnsignedInteger(n)) if u32::try_from(*n).is_ok() => {
                Ok(value.clone())
            }
            (Self::Uint64, Value::UnsignedInteger(_)) => Ok(value.clone()),
            (Self::Double, Value::Integer(_) | Value::Decimal(_) | Value::Real(_)) => {
                let n: f64 = value
                    .to_string()
                    .parse()
                    .map_err(|error| invalid().with_source(error))?;
                if !n.is_finite() {
                    return Err(invalid());
                }
                Ok(Value::Real(n))
            }
            (
                Self::Decimal(limits),
                Value::Integer(_) | Value::UnsignedInteger(_) | Value::Decimal(_),
            ) => {
                let decimal = crate::decimal::parse(&value.to_string())?;
                let text = decimal.normalized().to_plain_string();
                let unsigned = text.trim_start_matches('-');
                let (whole, frac) = unsigned.split_once('.').unwrap_or((unsigned, ""));
                if let Some((precision, scale)) = limits {
                    if frac.len() > *scale as usize
                        || whole.trim_start_matches('0').len() > (precision - scale) as usize
                    {
                        return Err(Error::new("DECIMAL precision or scale overflow".into()));
                    }
                    let text = decimal.with_scale(*scale as i64).to_plain_string();
                    Ok(Value::Decimal(text))
                } else {
                    Ok(Value::Decimal(text))
                }
            }
            (Self::Boolean, Value::Boolean(_))
            | (Self::Text, Value::Text(_))
            | (Self::Bytea, Value::Bytea(_)) => Ok(value.clone()),
            (Self::Varchar(limit), Value::Text(s))
                if limit.is_none_or(|n| s.chars().count() <= n) =>
            {
                Ok(value.clone())
            }
            (Self::Enum(labels), Value::Text(s)) if labels.contains(s) => Ok(value.clone()),
            (Self::Json, Value::Json(_)) => Ok(value.clone()),
            (Self::Changeset, Value::Changeset(bytes) | Value::Bytea(bytes)) => {
                crate::changeset::validate(bytes)?;
                Ok(Value::Changeset(bytes.clone()))
            }
            (Self::Uuid, Value::Uuid(s))
            | (Self::Instant, Value::Instant(s))
            | (Self::Date, Value::Date(s))
            | (Self::Time, Value::Time(s))
            | (Self::DateTime, Value::DateTime(s))
            | (Self::Interval, Value::Interval(s)) => {
                let kind = self.native_kind().unwrap();
                let text =
                    libsqlite3_sys::native::canonical(kind, s).map_err(|e| Error::new(e.0))?;
                Ok(match self {
                    Self::Uuid => Value::Uuid(text),
                    Self::Instant => Value::Instant(text),
                    Self::Date => Value::Date(text),
                    Self::Time => Value::Time(text),
                    Self::DateTime => Value::DateTime(text),
                    Self::Interval => Value::Interval(text),
                    _ => unreachable!(),
                })
            }
            (Self::Array(ty), Value::Array(values)) => Ok(Value::Array(
                values
                    .iter()
                    .map(|v| ty.validate(v))
                    .collect::<Result<_>>()?,
            )),
            _ => Err(invalid()),
        }
    }
    pub fn cast(&self, value: Value) -> Result<Value> {
        if matches!(value, Value::Null) {
            return Ok(value);
        }
        let source = match &value {
            Value::Decimal(_) => Some(libsqlite3_sys::native::Kind::Decimal),
            Value::Uuid(_) => Some(libsqlite3_sys::native::Kind::Uuid),
            Value::Instant(_) => Some(libsqlite3_sys::native::Kind::Instant),
            Value::Date(_) => Some(libsqlite3_sys::native::Kind::Date),
            Value::Time(_) => Some(libsqlite3_sys::native::Kind::Time),
            Value::DateTime(_) => Some(libsqlite3_sys::native::Kind::DateTime),
            Value::Interval(_) => Some(libsqlite3_sys::native::Kind::Interval),
            _ => None,
        };
        if let (Some(target), Some(source)) = (self.native_kind(), source)
            && target != source
        {
            let text = libsqlite3_sys::native::cast(target, source, &value.to_string())
                .map_err(|e| Error::new(e.0))?;
            return self.cast(Value::Text(text));
        }
        let text = if let (
            Self::Decimal(_) | Self::Uint8 | Self::Uint16 | Self::Uint32 | Self::Uint64,
            Value::Real(number),
        ) = (self, &value)
        {
            BigDecimal::from_f64(*number)
                .ok_or_else(|| Error::new("non-finite REAL cannot be an exact number".into()))?
                .normalized()
                .to_plain_string()
        } else {
            value.to_string()
        };
        let parsed = match self {
            Self::Int8 | Self::Int16 | Self::Int32 | Self::Int64 => {
                Value::Integer(text.parse().map_err(|error| {
                    Error::new("invalid integer cast".into()).with_source(error)
                })?)
            }
            Self::Uint8 | Self::Uint16 | Self::Uint32 | Self::Uint64 => {
                Value::UnsignedInteger(text.parse().map_err(|error| {
                    Error::new("invalid unsigned integer cast".into()).with_source(error)
                })?)
            }
            Self::Double => Value::Real(text.parse().map_err(|error| {
                Error::new("invalid floating-point cast".into()).with_source(error)
            })?),
            Self::Decimal(_) => Value::Decimal(text),
            Self::Boolean => Value::Boolean(match text.to_lowercase().as_str() {
                "true" => true,
                "false" => false,
                _ => return Err(Error::new("invalid boolean cast".into())),
            }),
            Self::Text | Self::Varchar(_) | Self::Enum(_) => Value::Text(text),
            Self::Uuid => Value::Uuid(text),
            Self::Instant => Value::Instant(text),
            Self::Date => Value::Date(text),
            Self::Time => Value::Time(text),
            Self::DateTime => Value::DateTime(text),
            Self::Interval => Value::Interval(text),
            Self::Json => Value::Json(serde_json::from_str(&text).map_err(Error::validation)?),
            Self::Bytea | Self::Array(_) | Self::Changeset => value,
        };
        self.validate(&parsed)
    }
}
impl std::fmt::Display for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Null => write!(f, "NULL"),
            Self::Integer(n) => write!(f, "{n}"),
            Self::UnsignedInteger(n) => write!(f, "{n}"),
            Self::Real(n) => write!(f, "{n}"),
            Self::Boolean(b) => write!(f, "{b}"),
            Self::Bytea(bytes) | Self::Changeset(bytes) => write!(f, "{}", hex::encode(bytes)),
            Self::Decimal(s)
            | Self::Text(s)
            | Self::Uuid(s)
            | Self::Instant(s)
            | Self::Date(s)
            | Self::Time(s)
            | Self::DateTime(s)
            | Self::Interval(s) => {
                write!(f, "{s}")
            }
            Self::Json(j) => write!(f, "{j}"),
            Self::Array(_) => write!(
                f,
                "{}",
                serde_json::to_string(self).map_err(|_| std::fmt::Error)?
            ),
        }
    }
}