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
// Copyright 2018 Mozilla // // Licensed under the Apache License, Version 2.0 (the "License"); you may not use // this file except in compliance with the License. You may obtain a copy of the // License at http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software distributed // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. use std::collections::{ BTreeSet, }; use types::{ ValueType, ValueTypeTag, }; use value_type_set::{ ValueTypeSet, }; /// Type safe representation of the possible return values from SQLite's `typeof` #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] pub enum SQLTypeAffinity { Null, // "null" Integer, // "integer" Real, // "real" Text, // "text" Blob, // "blob" } // Put this here rather than in `db` simply because it's widely needed. pub trait SQLValueType { fn value_type_tag(&self) -> ValueTypeTag; fn accommodates_integer(&self, int: i64) -> bool; /// Return a pair of the ValueTypeTag for this value type, and the SQLTypeAffinity required /// to distinguish it from any other types that share the same tag. /// /// Background: The tag alone is not enough to determine the type of a value, since multiple /// ValueTypes may share the same tag (for example, ValueType::Long and ValueType::Double). /// However, each ValueType can be determined by checking both the tag and the type's affinity. fn sql_representation(&self) -> (ValueTypeTag, Option<SQLTypeAffinity>); } impl SQLValueType for ValueType { fn sql_representation(&self) -> (ValueTypeTag, Option<SQLTypeAffinity>) { match *self { ValueType::Ref => (0, None), ValueType::Boolean => (1, None), ValueType::Instant => (4, None), // SQLite distinguishes integral from decimal types, allowing long and double to share a tag. ValueType::Long => (5, Some(SQLTypeAffinity::Integer)), ValueType::Double => (5, Some(SQLTypeAffinity::Real)), ValueType::String => (10, None), ValueType::Uuid => (11, None), ValueType::Keyword => (13, None), } } #[inline] fn value_type_tag(&self) -> ValueTypeTag { self.sql_representation().0 } /// Returns true if the provided integer is in the SQLite value space of this type. For /// example, `1` is how we encode `true`. /// /// ``` /// use mentat_core::{ValueType, SQLValueType}; /// assert!(!ValueType::Instant.accommodates_integer(1493399581314)); /// assert!(!ValueType::Instant.accommodates_integer(1493399581314000)); /// assert!(ValueType::Boolean.accommodates_integer(1)); /// assert!(!ValueType::Boolean.accommodates_integer(-1)); /// assert!(!ValueType::Boolean.accommodates_integer(10)); /// assert!(!ValueType::String.accommodates_integer(10)); /// ``` fn accommodates_integer(&self, int: i64) -> bool { use ValueType::*; match *self { Instant => false, // Always use #inst. Long | Double => true, Ref => int >= 0, Boolean => (int == 0) || (int == 1), ValueType::String => false, Keyword => false, Uuid => false, } } } /// We have an enum of types, `ValueType`. It can be collected into a set, `ValueTypeSet`. Each type /// is associated with a type tag, which is how a type is represented in, e.g., SQL storage. Types /// can share type tags, because backing SQL storage is able to differentiate between some types /// (e.g., longs and doubles), and so distinct tags aren't necessary. That association is defined by /// `SQLValueType`. That trait similarly extends to `ValueTypeSet`, which maps a collection of types /// into a collection of tags. pub trait SQLValueTypeSet { fn value_type_tags(&self) -> BTreeSet<ValueTypeTag>; fn has_unique_type_tag(&self) -> bool; fn unique_type_tag(&self) -> Option<ValueTypeTag>; } impl SQLValueTypeSet for ValueTypeSet { // This is inefficient, but it'll do for now. fn value_type_tags(&self) -> BTreeSet<ValueTypeTag> { let mut out = BTreeSet::new(); for t in self.0.iter() { out.insert(t.value_type_tag()); } out } fn unique_type_tag(&self) -> Option<ValueTypeTag> { if self.is_unit() || self.has_unique_type_tag() { self.exemplar().map(|t| t.value_type_tag()) } else { None } } fn has_unique_type_tag(&self) -> bool { if self.is_unit() { return true; } let mut acc = BTreeSet::new(); for t in self.0.iter() { if acc.insert(t.value_type_tag()) && acc.len() > 1 { // We inserted a second or subsequent value. return false; } } !acc.is_empty() } }