Skip to main content

sqlite_provider/
row.rs

1use crate::provider::{RawBytes, Sqlite3Api, ValueType};
2use crate::statement::Statement;
3use crate::value::{Value, ValueRef};
4
5/// Row view for the current step.
6pub struct Row<'s, 'c, 'p, P: Sqlite3Api> {
7    stmt: &'s Statement<'c, 'p, P>,
8}
9
10impl<'s, 'c, 'p, P: Sqlite3Api> Row<'s, 'c, 'p, P> {
11    pub(crate) fn new(stmt: &'s Statement<'c, 'p, P>) -> Self {
12        Self { stmt }
13    }
14
15    /// Number of columns.
16    pub fn column_count(&self) -> i32 {
17        unsafe { self.stmt.conn.api.column_count(self.stmt.stmt) }
18    }
19
20    /// Column type for the current row.
21    pub fn column_type(&self, col: i32) -> ValueType {
22        unsafe { self.stmt.conn.api.column_type(self.stmt.stmt, col) }
23    }
24
25    /// Column integer value.
26    pub fn column_int64(&self, col: i32) -> i64 {
27        unsafe { self.stmt.conn.api.column_int64(self.stmt.stmt, col) }
28    }
29
30    /// Column floating value.
31    pub fn column_double(&self, col: i32) -> f64 {
32        unsafe { self.stmt.conn.api.column_double(self.stmt.stmt, col) }
33    }
34
35    /// Raw column text bytes (SQLite-owned for the current row snapshot).
36    pub fn column_text_raw(&self, col: i32) -> RawBytes {
37        unsafe { self.stmt.conn.api.column_text(self.stmt.stmt, col) }
38    }
39
40    /// Raw column blob bytes (SQLite-owned for the current row snapshot).
41    pub fn column_blob_raw(&self, col: i32) -> RawBytes {
42        unsafe { self.stmt.conn.api.column_blob(self.stmt.stmt, col) }
43    }
44
45    /// Column text as UTF-8 if valid for the current row snapshot.
46    pub fn column_text(&self, col: i32) -> Option<&str> {
47        unsafe { self.column_text_raw(col).as_str() }
48    }
49
50    /// Column blob bytes for the current row snapshot.
51    pub fn column_blob(&self, col: i32) -> &[u8] {
52        unsafe { self.column_blob_raw(col).as_slice() }
53    }
54
55    /// Column value as a borrowed view for the current row snapshot.
56    pub fn column_value_ref(&self, col: i32) -> ValueRef<'_> {
57        match self.column_type(col) {
58            ValueType::Null => ValueRef::Null,
59            ValueType::Integer => ValueRef::Integer(self.column_int64(col)),
60            ValueType::Float => ValueRef::Float(self.column_double(col)),
61            ValueType::Text => unsafe { ValueRef::from_raw_text(self.column_text_raw(col)) },
62            ValueType::Blob => unsafe { ValueRef::from_raw_blob(self.column_blob_raw(col)) },
63        }
64    }
65
66    /// Column value as an owned `Value`.
67    pub fn column_value(&self, col: i32) -> Value {
68        self.column_value_ref(col).to_owned()
69    }
70}