pub enum SqliteValue {
Null,
Integer(i64),
Float(f64),
Text(Arc<str>),
Blob(Arc<[u8]>),
}Expand description
A dynamically-typed SQLite value.
Corresponds to C SQLite’s sqlite3_value / Mem type. SQLite has five
fundamental storage classes: NULL, INTEGER, REAL, TEXT, and BLOB.
Variants§
Null
A NULL value.
Integer(i64)
A signed 64-bit integer.
Float(f64)
A 64-bit IEEE floating point number.
Text(Arc<str>)
A UTF-8 text string.
Uses Arc<str> so that register copies (SCopy, Copy, ResultRow) are
O(1) atomic refcount increments instead of O(n) heap copies.
Blob(Arc<[u8]>)
A binary large object.
Uses Arc<[u8]> for the same O(1)-clone benefit as Text.
Implementations§
Source§impl SqliteValue
impl SqliteValue
Sourcepub const fn affinity(&self) -> TypeAffinity
pub const fn affinity(&self) -> TypeAffinity
Returns the type affinity that best describes this value.
Sourcepub const fn storage_class(&self) -> StorageClass
pub const fn storage_class(&self) -> StorageClass
Returns the storage class of this value.
Sourcepub fn apply_affinity(self, affinity: TypeAffinity) -> Self
pub fn apply_affinity(self, affinity: TypeAffinity) -> Self
Apply column type affinity coercion (advisory mode).
In non-STRICT tables, affinity is advisory: values are coerced when possible but never rejected. Follows SQLite §3.4 rules from https://www.sqlite.org/datatype3.html#type_affinity_of_a_column.
- TEXT affinity: numeric values converted to text before storing.
- NUMERIC affinity: text parsed as integer/real if well-formed.
- INTEGER affinity: like NUMERIC, plus exact-integer reals become integer.
- REAL affinity: like NUMERIC, plus integers forced to float.
- BLOB affinity: no conversion.
Sourcepub fn validate_strict(
self,
col_type: StrictColumnType,
) -> Result<Self, StrictTypeError>
pub fn validate_strict( self, col_type: StrictColumnType, ) -> Result<Self, StrictTypeError>
Validate a value against a STRICT table column type.
NULL is always accepted (nullability is enforced separately via NOT NULL).
Returns Ok(value) with possible implicit coercion (REAL columns accept
integers, converting them to float), or Err if the storage class is
incompatible.
Sourcepub const fn as_integer(&self) -> Option<i64>
pub const fn as_integer(&self) -> Option<i64>
Try to extract an integer value.
Sourcepub fn to_integer(&self) -> i64
pub fn to_integer(&self) -> i64
Convert to an integer following SQLite’s type coercion rules.
- NULL -> 0
- Integer -> itself
- Float -> truncated to i64
- Text -> attempt to parse, 0 on failure
- Blob -> 0
Sourcepub fn to_float(&self) -> f64
pub fn to_float(&self) -> f64
Convert to a float following SQLite’s type coercion rules.
- NULL -> 0.0
- Integer -> as f64
- Float -> itself
- Text -> attempt to parse, 0.0 on failure
- Blob -> 0.0
Sourcepub fn as_text_str(&self) -> Option<&str>
pub fn as_text_str(&self) -> Option<&str>
Borrow the inner text string without allocating.
Returns Some(&str) for Text values, None otherwise.
Use this in comparisons, LIKE patterns, and WHERE clause
evaluation to avoid the clone that to_text() incurs.
Sourcepub fn as_blob_bytes(&self) -> Option<&[u8]>
pub fn as_blob_bytes(&self) -> Option<&[u8]>
Borrow the inner blob bytes without allocating.
Sourcepub fn to_text(&self) -> String
pub fn to_text(&self) -> String
Convert to text following SQLite’s CAST(x AS TEXT) coercion rules.
For blobs, this interprets the raw bytes as UTF-8 (with lossy
replacement for invalid sequences), matching C SQLite behavior.
For the SQL-literal hex format (X'...'), use the Display impl.
Sourcepub fn cast_to_numeric(&self) -> Self
pub fn cast_to_numeric(&self) -> Self
Convert to NUMERIC using SQLite CAST semantics rather than affinity.
Unlike NUMERIC affinity, CAST always produces a numeric storage class for
text/blob input, using the longest leading numeric prefix or 0 when no
numeric prefix exists.
Sourcepub const fn typeof_str(&self) -> &'static str
pub const fn typeof_str(&self) -> &'static str
Returns the SQLite typeof() string for this value.
Matches C sqlite3: “null”, “integer”, “real”, “text”, or “blob”.
Sourcepub fn sql_length(&self) -> Option<i64>
pub fn sql_length(&self) -> Option<i64>
Returns the SQLite length() result for this value.
- NULL → NULL (represented as None)
- TEXT → character count
- BLOB → byte count
- INTEGER/REAL → character count of text representation
Sourcepub fn unique_eq(&self, other: &Self) -> bool
pub fn unique_eq(&self, other: &Self) -> bool
Check equality for UNIQUE constraint purposes.
In SQLite, NULL != NULL for uniqueness: if either value is NULL, the
result is false (they are never considered duplicates). Non-NULL values
compare by storage class ordering (same as PartialEq).
Sourcepub fn is_integer_numeric_type(&self) -> bool
pub fn is_integer_numeric_type(&self) -> bool
Mirrors C SQLite’s numericType() (SQLite VDBE:496): returns true if this
value should be treated as an integer for arithmetic purposes.
Integer values are obviously integer-typed. Text/Blob values that parse as i64 are also integer-typed. Float and Null are not.
Sourcepub fn sql_add(&self, other: &Self) -> Self
pub fn sql_add(&self, other: &Self) -> Self
Add two values following SQLite’s overflow semantics.
- Integer + Integer: checked add; overflows promote to REAL.
- Any REAL operand: float addition.
- NULL propagates (NULL + x = NULL).
- Text/Blob coerced via
numericType(): if both parse as integer, integer math is used (SQLite VDBE:1932-1934).
Trait Implementations§
Source§impl Clone for SqliteValue
impl Clone for SqliteValue
Source§fn clone(&self) -> SqliteValue
fn clone(&self) -> SqliteValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more