Skip to content

Commit

Permalink
qr: flatten return type of QueryResult::into_rows_result to Result<...>
Browse files Browse the repository at this point in the history
Instead of returning Result<Option<_>, _>, we will simply return a Result.
IntoRowsResultError is introduced specifically for this method.

Adjusted all of usages of this API. Most of the changes were simply
to replace `unwrap().unwrap()` to single `unwrap()`.

There are 4 places that require more focus during review:
- print_result() method in `cql-sh.rs` example.
- changes to SchemaVersionFetchError (and corresponding code in connection.rs)
- changes to TracingProtocolError (and corresponding code in session.rs)
- adjustment to `scylla_supports_tablets` in test_utils.rs
  • Loading branch information
muzarski committed Nov 12, 2024
1 parent b493c7b commit 6eaabf2
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 209 deletions.
1 change: 0 additions & 1 deletion examples/compare-tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ async fn main() -> Result<()> {
)
.await?
.into_rows_result()?
.expect("Got not Rows result")
.single_row()?;
assert_eq!(t, qt);
println!("token for {}: {}", pk, t);
Expand Down
43 changes: 22 additions & 21 deletions examples/cqlsh-rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use rustyline::error::ReadlineError;
use rustyline::{CompletionType, Config, Context, Editor};
use rustyline_derive::{Helper, Highlighter, Hinter, Validator};
use scylla::frame::response::result::Row;
use scylla::transport::query_result::IntoRowsResultError;
use scylla::transport::session::Session;
use scylla::transport::Compression;
use scylla::QueryRowsResult;
use scylla::QueryResult;
use scylla::SessionBuilder;
use std::env;

Expand Down Expand Up @@ -176,24 +177,27 @@ impl Completer for CqlHelper {
}
}

fn print_result(result: Option<&QueryRowsResult>) {
if let Some(rows_result) = result {
for row in rows_result.rows::<Row>().unwrap() {
let row = row.unwrap();
for column in &row.columns {
print!("|");
print!(
" {:16}",
match column {
None => "null".to_owned(),
Some(value) => format!("{:?}", value),
}
);
fn print_result(result: QueryResult) -> Result<(), IntoRowsResultError> {
match result.into_rows_result() {
Ok(rows_result) => {
for row in rows_result.rows::<Row>().unwrap() {
let row = row.unwrap();
for column in &row.columns {
print!("|");
print!(
" {:16}",
match column {
None => "null".to_owned(),
Some(value) => format!("{:?}", value),
}
);
}
println!("|");
}
println!("|")
Ok(())
}
} else {
println!("OK");
Err(IntoRowsResultError::ResultNotRows) => Ok(println!("OK")),
Err(e) => Err(e),
}
}

Expand Down Expand Up @@ -226,10 +230,7 @@ async fn main() -> Result<()> {
let maybe_res = session.query_unpaged(line, &[]).await;
match maybe_res {
Err(err) => println!("Error: {}", err),
Ok(res) => {
let rows_res = res.into_rows_result()?;
print_result(rows_res.as_ref())
}
Ok(res) => print_result(res)?,
}
}
Err(ReadlineError::Interrupted) => continue,
Expand Down
5 changes: 2 additions & 3 deletions examples/custom_deserialization.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use anyhow::Result;
use scylla::deserialize::DeserializeValue;
use scylla::frame::response::result::ColumnType;
use scylla::transport::session::Session;
Expand Down Expand Up @@ -55,8 +55,7 @@ async fn main() -> Result<()> {
(),
)
.await?
.into_rows_result()?
.context("Expected Result:Rows response, got a different Result response.")?;
.into_rows_result()?;

let (v,) = rows_result.single_row::<(MyType,)>()?;
assert_eq!(v, MyType("asdf"));
Expand Down
5 changes: 2 additions & 3 deletions examples/get_by_name.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Context as _, Result};
use anyhow::{anyhow, Result};
use scylla::frame::response::result::Row;
use scylla::transport::session::Session;
use scylla::SessionBuilder;
Expand Down Expand Up @@ -39,8 +39,7 @@ async fn main() -> Result<()> {
let rows_result = session
.query_unpaged("SELECT pk, ck, value FROM examples_ks.get_by_name", &[])
.await?
.into_rows_result()?
.context("Response is not of Rows type")?;
.into_rows_result()?;
let col_specs = rows_result.column_specs();
let (ck_idx, _) = col_specs
.get_by_name("ck")
Expand Down
8 changes: 2 additions & 6 deletions examples/select-paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ async fn main() -> Result<()> {
.query_single_page(paged_query.clone(), &[], paging_state)
.await?;

let res = res
.into_rows_result()?
.expect("Got result different than Rows");
let res = res.into_rows_result()?;

println!(
"Paging state: {:#?} ({} rows)",
Expand Down Expand Up @@ -85,9 +83,7 @@ async fn main() -> Result<()> {
.execute_single_page(&paged_prepared, &[], paging_state)
.await?;

let res = res
.into_rows_result()?
.expect("Got result different than Rows");
let res = res.into_rows_result()?;

println!(
"Paging state from the prepared statement execution: {:#?} ({} rows)",
Expand Down
3 changes: 1 addition & 2 deletions examples/tower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ async fn main() -> anyhow::Result<()> {
let rows_result = session
.call("SELECT keyspace_name, table_name FROM system_schema.tables;".into())
.await?
.into_rows_result()?
.expect("Got result different than Rows");
.into_rows_result()?;

let print_text = |t: &Option<scylla::frame::response::result::CqlValue>| {
t.as_ref()
Expand Down
9 changes: 3 additions & 6 deletions scylla/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,9 @@
//! .await?
//! .into_rows_result()?;
//!
//!
//! if let Some(rows) = query_rows {
//! for row in rows.rows()? {
//! // Parse row as int and text \
//! let (int_val, text_val): (i32, &str) = row?;
//! }
//! for row in query_rows.rows()? {
//! // Parse row as int and text \
//! let (int_val, text_val): (i32, &str) = row?;
//! }
//! # Ok(())
//! # }
Expand Down
8 changes: 3 additions & 5 deletions scylla/src/transport/caching_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ mod tests {
.execute_unpaged("select * from test_table", &[])
.await
.unwrap();
let result_rows = result.into_rows_result().unwrap().unwrap();
let result_rows = result.into_rows_result().unwrap();

assert_eq!(1, session.cache.len());
assert_eq!(1, result_rows.rows_num());
Expand All @@ -438,7 +438,7 @@ mod tests {
.await
.unwrap();

let result_rows = result.into_rows_result().unwrap().unwrap();
let result_rows = result.into_rows_result().unwrap();

assert_eq!(1, session.cache.len());
assert_eq!(1, result_rows.rows_num());
Expand Down Expand Up @@ -485,7 +485,7 @@ mod tests {
.unwrap();

assert_eq!(1, session.cache.len());
assert_eq!(1, result.into_rows_result().unwrap().unwrap().rows_num());
assert_eq!(1, result.into_rows_result().unwrap().rows_num());
}

async fn assert_test_batch_table_rows_contain(
Expand All @@ -498,7 +498,6 @@ mod tests {
.unwrap()
.into_rows_result()
.unwrap()
.unwrap()
.rows::<(i32, i32)>()
.unwrap()
.map(|r| r.unwrap())
Expand Down Expand Up @@ -710,7 +709,6 @@ mod tests {
.unwrap()
.into_rows_result()
.unwrap()
.unwrap()
.rows::<(i32, i64)>()
.unwrap()
.collect::<Result<Vec<_>, _>>()
Expand Down
6 changes: 1 addition & 5 deletions scylla/src/transport/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,12 +1437,9 @@ impl Connection {
.into_rows_result()
.map_err(|err| {
QueryError::ProtocolError(ProtocolError::SchemaVersionFetch(
SchemaVersionFetchError::ResultMetadataDeserializationError(err),
SchemaVersionFetchError::TracesEventsIntoRowsResultError(err),
))
})?
.ok_or(QueryError::ProtocolError(
ProtocolError::SchemaVersionFetch(SchemaVersionFetchError::ResultNotRows),
))?
.single_row::<(Uuid,)>()
.map_err(|err| {
ProtocolError::SchemaVersionFetch(SchemaVersionFetchError::SingleRowError(err))
Expand Down Expand Up @@ -2625,7 +2622,6 @@ mod tests {
.unwrap()
.into_rows_result()
.unwrap()
.unwrap()
.rows::<(i32, Vec<u8>)>()
.unwrap()
.collect::<Result<Vec<_>, _>>()
Expand Down
1 change: 0 additions & 1 deletion scylla/src/transport/cql_collections_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ async fn insert_and_select<InsertT, SelectT>(
.unwrap()
.into_rows_result()
.unwrap()
.unwrap()
.single_row::<(SelectT,)>()
.unwrap()
.0;
Expand Down
Loading

0 comments on commit 6eaabf2

Please sign in to comment.