Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions bindings/rust/src/rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ unsafe impl Send for Row {}
unsafe impl Sync for Row {}

impl Row {
pub fn get_value(&self, index: usize) -> Result<Value> {
let value = &self.values[index];
match value {
pub fn get_value(&self, idx: usize) -> Result<Value> {
let val = &self
.values
.get(idx)
.ok_or(Error::SqlExecutionFailure("invalid row index".to_string()))?;
match val {
turso_core::Value::Integer(i) => Ok(Value::Integer(*i)),
turso_core::Value::Null => Ok(Value::Null),
turso_core::Value::Float(f) => Ok(Value::Real(*f)),
Expand All @@ -102,7 +105,10 @@ impl Row {
where
T: FromValue,
{
let val = &self.values[idx];
let val = self
.values
.get(idx)
.ok_or(Error::SqlExecutionFailure("invalid row index".to_string()))?;
T::from_sql(val.clone()).map_err(|err| Error::ConversionFailure(err.to_string()))
}

Expand Down
25 changes: 25 additions & 0 deletions bindings/rust/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,31 @@ async fn test_transaction_prepared_statement() {
assert_eq!(id, 1);
}

#[tokio::test]
async fn test_row_get_value_out_of_bounds() {
let db = Builder::new_local(":memory:").build().await.unwrap();
let conn = db.connect().unwrap();

conn.execute("CREATE TABLE t (x INTEGER)", ())
.await
.unwrap();
conn.execute("INSERT INTO t VALUES (1)", ()).await.unwrap();

let mut rows = conn.query("SELECT x FROM t", ()).await.unwrap();
let row = rows.next().await.unwrap().unwrap();

// Valid index works
assert!(row.get_value(0).is_ok());

// Out of bounds returns error instead of panicking
let result = row.get_value(999);
assert!(matches!(result, Err(Error::SqlExecutionFailure(_))));

// Also test get<T>() for OOB
let result: Result<i64, _> = row.get(999);
assert!(matches!(result, Err(Error::SqlExecutionFailure(_))));
}

// Test Connection clone
#[tokio::test]
async fn test_connection_clone() {
Expand Down
Loading