Skip to content

Commit

Permalink
libsql/core: Implement Rows::as_ref()
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Aug 14, 2023
1 parent 8f28d52 commit fa46bad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
33 changes: 18 additions & 15 deletions crates/core/src/rows.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use crate::{errors, Connection, Error, Params, Result, Value};
use crate::{errors, Connection, Statement, Error, Params, Result, Value};
use libsql_sys::ValueType;

use std::cell::RefCell;
use std::sync::Arc;

/// Query result rows.
#[derive(Debug)]
#[derive(Clone)]
pub struct Rows {
pub(crate) stmt: Arc<libsql_sys::Statement>,
pub(crate) stmt: Statement,
pub(crate) err: RefCell<Option<i32>>,
}

unsafe impl Send for Rows {} // TODO: is this safe?

impl Rows {
pub fn new(stmt: Arc<libsql_sys::Statement>) -> Rows {
pub fn new(stmt: Statement) -> Rows {
Rows {
stmt,
err: RefCell::new(None),
Expand All @@ -24,7 +23,7 @@ impl Rows {
pub fn next(&self) -> Result<Option<Row>> {
let err = match self.err.take() {
Some(err) => err,
None => self.stmt.step(),
None => self.stmt.inner.step(),
};
match err as u32 {
libsql_sys::ffi::SQLITE_OK => Ok(None),
Expand All @@ -37,15 +36,19 @@ impl Rows {
}

pub fn column_count(&self) -> i32 {
self.stmt.column_count()
self.stmt.inner.column_count()
}

pub fn column_name(&self, idx: i32) -> &str {
self.stmt.column_name(idx)
self.stmt.inner.column_name(idx)
}

pub fn column_type(&self, idx: i32) -> i32 {
self.stmt.column_type(idx)
self.stmt.inner.column_type(idx)
}

pub fn as_ref(&self) -> &Statement {
&self.stmt
}
}

Expand Down Expand Up @@ -75,25 +78,25 @@ impl futures::Future for RowsFuture {
}

pub struct Row {
pub(crate) stmt: Arc<libsql_sys::Statement>,
pub(crate) stmt: Statement,
}

impl Row {
pub fn get<T>(&self, idx: i32) -> Result<T>
where
T: FromValue,
{
let val = self.stmt.column_value(idx);
let val = self.stmt.inner.column_value(idx);
T::from_sql(val)
}

pub fn get_value(&self, idx: i32) -> Result<Value> {
let val = self.stmt.column_value(idx);
let val = self.stmt.inner.column_value(idx);
Ok(val.into())
}

pub fn column_type(&self, idx: i32) -> Result<ValueType> {
let val = self.stmt.column_type(idx);
let val = self.stmt.inner.column_type(idx);
match val as u32 {
libsql_sys::ffi::SQLITE_INTEGER => Ok(ValueType::Integer),
libsql_sys::ffi::SQLITE_FLOAT => Ok(ValueType::Real),
Expand All @@ -105,11 +108,11 @@ impl Row {
}

pub fn column_name(&self, idx: i32) -> &str {
self.stmt.column_name(idx)
self.stmt.inner.column_name(idx)
}

pub fn get_ref(&self, idx: i32) -> Result<crate::params::ValueRef<'_>> {
Ok(crate::Statement::value_ref(&self.stmt, idx as usize))
Ok(crate::Statement::value_ref(&self.stmt.inner, idx as usize))
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/core/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use std::ffi::c_int;
use std::sync::Arc;

/// A prepared statement.
#[derive(Clone)]
pub struct Statement {
conn: Connection,
inner: Arc<libsql_sys::Statement>,
pub(crate) inner: Arc<libsql_sys::Statement>,
}

impl Statement {
Expand Down Expand Up @@ -45,7 +46,7 @@ impl Statement {
self.bind(params);
let err = self.inner.step();
Ok(Rows {
stmt: self.inner.clone(),
stmt: self.clone(),
err: RefCell::new(Some(err)),
})
}
Expand Down

0 comments on commit fa46bad

Please sign in to comment.