Skip to content

Commit 9120ce6

Browse files
authored
Merge pull request #1623 from ignatz/align_row_and_rows
Breaking change: Make Rows and Row API more consistent.
2 parents b97d37d + f510359 commit 9120ce6

File tree

7 files changed

+72
-55
lines changed

7 files changed

+72
-55
lines changed

libsql/src/de.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'de> Deserializer<'de> for RowDeserializer<'de> {
6868

6969
visitor.visit_map(RowMapAccess {
7070
row: self.row,
71-
idx: 0..self.row.inner.column_count(),
71+
idx: 0..(self.row.inner.column_count() as usize),
7272
value: None,
7373
})
7474
}

libsql/src/hrana/mod.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::pin::Pin;
2424
use std::sync::Arc;
2525
use std::task::{Context, Poll};
2626

27-
use super::rows::{RowInner, RowsInner};
27+
use super::rows::{ColumnsInner, RowInner, RowsInner};
2828

2929
pub(crate) type Result<T> = std::result::Result<T, HranaError>;
3030

@@ -261,7 +261,12 @@ where
261261
async fn next(&mut self) -> crate::Result<Option<super::Row>> {
262262
self.next().await
263263
}
264+
}
264265

266+
impl<S> ColumnsInner for HranaRows<S>
267+
where
268+
S: Stream<Item = std::io::Result<Bytes>> + Send + Sync + Unpin,
269+
{
265270
fn column_count(&self) -> i32 {
266271
self.column_count()
267272
}
@@ -303,13 +308,6 @@ impl RowInner for Row {
303308
Ok(into_value2(v))
304309
}
305310

306-
fn column_name(&self, idx: i32) -> Option<&str> {
307-
self.cols
308-
.get(idx as usize)
309-
.and_then(|c| c.name.as_ref())
310-
.map(|s| s.as_str())
311-
}
312-
313311
fn column_str(&self, idx: i32) -> crate::Result<&str> {
314312
if let Some(value) = self.inner.get(idx as usize) {
315313
if let proto::Value::Text { value } = value {
@@ -321,6 +319,15 @@ impl RowInner for Row {
321319
Err(crate::Error::ColumnNotFound(idx))
322320
}
323321
}
322+
}
323+
324+
impl ColumnsInner for Row {
325+
fn column_name(&self, idx: i32) -> Option<&str> {
326+
self.cols
327+
.get(idx as usize)
328+
.and_then(|c| c.name.as_ref())
329+
.map(|s| s.as_str())
330+
}
324331

325332
fn column_type(&self, idx: i32) -> crate::Result<ValueType> {
326333
if let Some(value) = self.inner.get(idx as usize) {
@@ -337,8 +344,8 @@ impl RowInner for Row {
337344
}
338345
}
339346

340-
fn column_count(&self) -> usize {
341-
self.cols.len()
347+
fn column_count(&self) -> i32 {
348+
self.cols.len() as i32
342349
}
343350
}
344351

@@ -417,7 +424,9 @@ impl RowsInner for StmtResultRows {
417424
inner: Box::new(row),
418425
}))
419426
}
427+
}
420428

429+
impl ColumnsInner for StmtResultRows {
421430
fn column_count(&self) -> i32 {
422431
self.cols.len() as i32
423432
}

libsql/src/local/impls.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::connection::BatchRows;
55
use crate::{
66
connection::Conn,
77
params::Params,
8-
rows::{RowInner, RowsInner},
8+
rows::{ColumnsInner, RowInner, RowsInner},
99
statement::Stmt,
1010
transaction::Tx,
1111
Column, Connection, Result, Row, Rows, Statement, Transaction, TransactionBehavior, Value,
@@ -159,7 +159,9 @@ impl RowsInner for LibsqlRows {
159159

160160
Ok(row)
161161
}
162+
}
162163

164+
impl ColumnsInner for LibsqlRows {
163165
fn column_count(&self) -> i32 {
164166
self.0.column_count()
165167
}
@@ -180,20 +182,22 @@ impl RowInner for LibsqlRow {
180182
self.0.get_value(idx)
181183
}
182184

183-
fn column_name(&self, idx: i32) -> Option<&str> {
184-
self.0.column_name(idx)
185-
}
186-
187185
fn column_str(&self, idx: i32) -> Result<&str> {
188186
self.0.get::<&str>(idx)
189187
}
188+
}
189+
190+
impl ColumnsInner for LibsqlRow {
191+
fn column_name(&self, idx: i32) -> Option<&str> {
192+
self.0.column_name(idx)
193+
}
190194

191195
fn column_type(&self, idx: i32) -> Result<ValueType> {
192196
self.0.column_type(idx).map(ValueType::from)
193197
}
194198

195-
fn column_count(&self) -> usize {
196-
self.0.stmt.column_count()
199+
fn column_count(&self) -> i32 {
200+
self.0.stmt.column_count() as i32
197201
}
198202
}
199203

libsql/src/local/rows.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::local::{Connection, Statement};
22
use crate::params::Params;
3-
use crate::rows::{RowInner, RowsInner};
3+
use crate::rows::{ColumnsInner, RowInner, RowsInner};
44
use crate::{errors, Error, Result};
55
use crate::{Value, ValueRef};
66
use libsql_sys::ValueType;
@@ -213,7 +213,9 @@ impl RowsInner for BatchedRows {
213213
Ok(None)
214214
}
215215
}
216+
}
216217

218+
impl ColumnsInner for BatchedRows {
217219
fn column_count(&self) -> i32 {
218220
self.cols.len() as i32
219221
}
@@ -244,10 +246,6 @@ impl RowInner for BatchedRow {
244246
.ok_or(Error::InvalidColumnIndex)
245247
}
246248

247-
fn column_name(&self, idx: i32) -> Option<&str> {
248-
self.cols.get(idx as usize).map(|c| c.0.as_str())
249-
}
250-
251249
fn column_str(&self, idx: i32) -> Result<&str> {
252250
self.row
253251
.get(idx as usize)
@@ -258,9 +256,15 @@ impl RowInner for BatchedRow {
258256
.ok_or(Error::InvalidColumnType)
259257
})
260258
}
259+
}
260+
261+
impl ColumnsInner for BatchedRow {
262+
fn column_name(&self, idx: i32) -> Option<&str> {
263+
self.cols.get(idx as usize).map(|c| c.0.as_str())
264+
}
261265

262-
fn column_count(&self) -> usize {
263-
self.cols.len()
266+
fn column_count(&self) -> i32 {
267+
self.cols.len() as i32
264268
}
265269

266270
fn column_type(&self, idx: i32) -> Result<crate::value::ValueType> {

libsql/src/local/statement.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ impl Statement {
250250
/// sure that current statement has already been stepped once before
251251
/// calling this method.
252252
pub fn column_names(&self) -> Vec<&str> {
253-
let n = self.column_count();
254-
let mut cols = Vec::with_capacity(n);
255-
for i in 0..n {
256-
let s = self.column_name(i);
257-
if let Some(s) = s {
258-
cols.push(s);
259-
}
260-
}
261-
cols
253+
let n = self.column_count();
254+
let mut cols = Vec::with_capacity(n);
255+
for i in 0..n {
256+
let s = self.column_name(i);
257+
if let Some(s) = s {
258+
cols.push(s);
259+
}
260+
}
261+
cols
262262
}
263263

264264
/// Return the number of columns in the result set returned by the prepared
@@ -314,12 +314,11 @@ impl Statement {
314314
/// the specified `name`.
315315
pub fn column_index(&self, name: &str) -> Result<usize> {
316316
let bytes = name.as_bytes();
317-
let n = self.column_count() as i32;
317+
let n = self.column_count();
318318
for i in 0..n {
319319
// Note: `column_name` is only fallible if `i` is out of bounds,
320320
// which we've already checked.
321321
let col_name = self
322-
.inner
323322
.column_name(i)
324323
.ok_or_else(|| Error::InvalidColumnName(name.to_string()))?;
325324
if bytes.eq_ignore_ascii_case(col_name.as_bytes()) {

libsql/src/replication/connection.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use parking_lot::Mutex;
1111

1212
use crate::parser;
1313
use crate::parser::StmtKind;
14-
use crate::rows::{RowInner, RowsInner};
14+
use crate::rows::{ColumnsInner, RowInner, RowsInner};
1515
use crate::statement::Stmt;
1616
use crate::transaction::Tx;
1717
use crate::{
@@ -780,7 +780,9 @@ impl RowsInner for RemoteRows {
780780
let row = RemoteRow(values, self.0.column_descriptions.clone());
781781
Ok(Some(row).map(Box::new).map(|inner| Row { inner }))
782782
}
783+
}
783784

785+
impl ColumnsInner for RemoteRows {
784786
fn column_count(&self) -> i32 {
785787
self.0.column_descriptions.len() as i32
786788
}
@@ -813,10 +815,6 @@ impl RowInner for RemoteRow {
813815
.ok_or(Error::InvalidColumnIndex)
814816
}
815817

816-
fn column_name(&self, idx: i32) -> Option<&str> {
817-
self.1.get(idx as usize).map(|s| s.name.as_str())
818-
}
819-
820818
fn column_str(&self, idx: i32) -> Result<&str> {
821819
let value = self.0.get(idx as usize).ok_or(Error::InvalidColumnIndex)?;
822820

@@ -825,6 +823,12 @@ impl RowInner for RemoteRow {
825823
_ => Err(Error::InvalidColumnType),
826824
}
827825
}
826+
}
827+
828+
impl ColumnsInner for RemoteRow {
829+
fn column_name(&self, idx: i32) -> Option<&str> {
830+
self.1.get(idx as usize).map(|s| s.name.as_str())
831+
}
828832

829833
fn column_type(&self, idx: i32) -> Result<ValueType> {
830834
let col = self.1.get(idx as usize).unwrap();
@@ -835,8 +839,8 @@ impl RowInner for RemoteRow {
835839
.ok_or(Error::InvalidColumnType)
836840
}
837841

838-
fn column_count(&self) -> usize {
839-
self.1.len()
842+
fn column_count(&self) -> i32 {
843+
self.1.len() as i32
840844
}
841845
}
842846

libsql/src/rows.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,8 @@ impl Column<'_> {
3838
}
3939

4040
#[async_trait::async_trait]
41-
pub(crate) trait RowsInner {
41+
pub(crate) trait RowsInner: ColumnsInner {
4242
async fn next(&mut self) -> Result<Option<Row>>;
43-
44-
fn column_count(&self) -> i32;
45-
46-
fn column_name(&self, idx: i32) -> Option<&str>;
47-
48-
fn column_type(&self, idx: i32) -> Result<ValueType>;
4943
}
5044

5145
/// A set of rows returned from a connection.
@@ -131,7 +125,7 @@ impl Row {
131125
}
132126

133127
/// Get the count of columns in this set of rows.
134-
pub fn column_count(&self) -> usize {
128+
pub fn column_count(&self) -> i32 {
135129
self.inner.column_count()
136130
}
137131

@@ -284,12 +278,15 @@ where
284278
}
285279
impl<T> Sealed for Option<T> {}
286280

287-
pub(crate) trait RowInner: fmt::Debug {
288-
fn column_value(&self, idx: i32) -> Result<Value>;
289-
fn column_str(&self, idx: i32) -> Result<&str>;
281+
pub(crate) trait ColumnsInner {
290282
fn column_name(&self, idx: i32) -> Option<&str>;
291283
fn column_type(&self, idx: i32) -> Result<ValueType>;
292-
fn column_count(&self) -> usize;
284+
fn column_count(&self) -> i32;
285+
}
286+
287+
pub(crate) trait RowInner: ColumnsInner + fmt::Debug {
288+
fn column_value(&self, idx: i32) -> Result<Value>;
289+
fn column_str(&self, idx: i32) -> Result<&str>;
293290
}
294291

295292
mod sealed {

0 commit comments

Comments
 (0)