Skip to content

Commit

Permalink
Merge pull request #554 from pjhades/impl-debug-for-row
Browse files Browse the repository at this point in the history
libsql: implement Debug trait for Row
  • Loading branch information
LucioFranco committed Nov 3, 2023
2 parents 99ef281 + c536380 commit 0c7cfda
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions libsql/src/hrana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ impl RowsInner for Rows {
}
}

#[derive(Debug)]
pub struct Row {
cols: Arc<Vec<Col>>,
inner: Vec<proto::Value>,
Expand Down
7 changes: 7 additions & 0 deletions libsql/src/local/impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Arc;
use std::fmt;

use crate::{
connection::Conn,
Expand Down Expand Up @@ -162,3 +163,9 @@ impl RowInner for LibsqlRow {
self.0.column_type(idx).map(ValueType::from)
}
}

impl fmt::Debug for LibsqlRow {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
self.0.fmt(f)
}
}
27 changes: 27 additions & 0 deletions libsql/src/local/rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{errors, Error, Result};
use crate::{Value, ValueRef};
use libsql_sys::ValueType;

use std::fmt;
use std::cell::RefCell;
use std::ffi::c_char;
/// Query result rows.
Expand Down Expand Up @@ -151,6 +152,32 @@ impl Row {
}
}

impl fmt::Debug for Row {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
let mut dbg_map = f.debug_map();
for column in 0..self.stmt.column_count() {
dbg_map.key(&self.stmt.column_name(column));
let value = self.get_ref(column as i32);
match value {
Ok(value_ref) => {
let value_type = value_ref.data_type();
match value_ref {
ValueRef::Null => dbg_map.value(&(value_type, ())),
ValueRef::Integer(i) => dbg_map.value(&(value_type, i)),
ValueRef::Real(f) => dbg_map.value(&(value_type, f)),
ValueRef::Text(s) => dbg_map.value(&(value_type, String::from_utf8_lossy(s))),
ValueRef::Blob(b) => dbg_map.value(&(value_type, b.len())),
};
}
Err(_) => {
dbg_map.value(&value);
}
}
}
dbg_map.finish()
}
}

pub trait FromValue {
fn from_sql(val: libsql_sys::Value) -> Result<Self>
where
Expand Down
1 change: 1 addition & 0 deletions libsql/src/replication/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ impl RowsInner for RemoteRows {
}
}

#[derive(Debug)]
struct RemoteRow(Vec<Value>, Vec<libsql_replication::rpc::proxy::Column>);

impl RowInner for RemoteRow {
Expand Down
9 changes: 8 additions & 1 deletion libsql/src/rows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{Result, Value, ValueType};
use std::fmt;

// NOTICE: Column is blatantly copy-pasted from rusqlite
pub struct Column<'stmt> {
Expand Down Expand Up @@ -99,6 +100,12 @@ impl Row {
}
}

impl fmt::Debug for Row {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
self.inner.fmt(f)
}
}

pub trait FromValue {
fn from_sql(val: Value) -> Result<Self>
where
Expand Down Expand Up @@ -207,7 +214,7 @@ where
}
}

pub(crate) trait RowInner {
pub(crate) trait RowInner: fmt::Debug {
fn column_value(&self, idx: i32) -> Result<Value>;
fn column_str(&self, idx: i32) -> Result<&str>;
fn column_name(&self, idx: i32) -> Option<&str>;
Expand Down
1 change: 1 addition & 0 deletions libsql/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl From<libsql_sys::Value> for Value {
}

// Heavily inspired by rusqlite's ValueRef
#[derive(Debug)]
pub enum ValueRef<'a> {
Null,
Integer(i64),
Expand Down
22 changes: 22 additions & 0 deletions libsql/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,25 @@ async fn custom_params() {
.await
.unwrap();
}

#[tokio::test]
async fn debug_print_row() {
let db = Database::open(":memory:").unwrap();
let conn = db.connect().unwrap();
let _ = conn
.execute(
"CREATE TABLE users (id INTEGER, name TEXT, score REAL, data BLOB, age INTEGER)",
(),
)
.await;
conn.execute("INSERT INTO users (id, name, score, data, age) VALUES (123, \"potato\", 3.14, X'deadbeef', NULL)", ())
.await
.unwrap();

let mut stmt = conn.prepare("SELECT * FROM users").await.unwrap();
let mut rows = stmt.query(()).await.unwrap();
assert_eq!(
format!("{:?}", rows.next().unwrap().unwrap()),
"{Some(\"id\"): (Integer, 123), Some(\"name\"): (Text, \"potato\"), Some(\"score\"): (Real, 3.14), Some(\"data\"): (Blob, 4), Some(\"age\"): (Null, ())}"
);
}

0 comments on commit 0c7cfda

Please sign in to comment.