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 2ac12fe
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 229 deletions.
2 changes: 1 addition & 1 deletion docs/source/queries/paged.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ loop {
.execute_single_page(&paged_prepared, &[], paging_state)
.await?;

let rows_res = res.into_rows_result()?.unwrap();
let rows_res = res.into_rows_result()?;

println!(
"Paging state response from the prepared statement execution: {:#?} ({} rows)",
Expand Down
32 changes: 15 additions & 17 deletions docs/source/queries/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,18 @@ Additionally, [`QueryResult`](https://docs.rs/scylla/latest/scylla/transport/que
let result = session
.query_unpaged("SELECT a from ks.tab", &[])
.await?
.into_rows_result()?
.unwrap();
.into_rows_result()?;

for row in result.rows::<(i32,)>()? {
let (int_value,): (i32,) = row?;
}

// first_row gets the first row and parses it as the given type
let first_int_val: Option<(i32,)> = session
let first_int_val: (i32,) = session
.query_unpaged("SELECT a from ks.tab", &[])
.await?
.into_rows_result()?
.map(|res| res.first_row::<(i32,)>())
.transpose()?;
.first_row::<(i32,)>()?;

// result_not_rows fails when the response is rows
session.query_unpaged("INSERT INTO ks.tab (a) VALUES (0)", &[]).await?.result_not_rows()?;
Expand All @@ -75,13 +73,13 @@ To properly handle `NULL` values parse column as an `Option<>`:
use scylla::IntoTypedRows;

// Parse row as two columns containing an int and text which might be null
if let Some(rows_result) = session.query_unpaged("SELECT a, b from ks.tab", &[])
let rows_result = session
.query_unpaged("SELECT a, b from ks.tab", &[])
.await?
.into_rows_result()?
{
for row in rows_result.rows::<(i32, Option<&str>)>()? {
let (int_value, str_or_null): (i32, Option<&str>) = row?;
}
.into_rows_result()?;

for row in rows_result.rows::<(i32, Option<&str>)>()? {
let (int_value, str_or_null): (i32, Option<&str>) = row?;
}
# Ok(())
# }
Expand Down Expand Up @@ -111,13 +109,13 @@ struct MyRow {
}

// Parse row as two columns containing an int and text which might be null
if let Some(result_rows) = session.query_unpaged("SELECT a, b from ks.tab", &[])
let result_rows = session
.query_unpaged("SELECT a, b from ks.tab", &[])
.await?
.into_rows_result()?
{
for row in result_rows.rows::<MyRow>()? {
let my_row: MyRow = row?;
}
.into_rows_result()?;

for row in result_rows.rows::<MyRow>()? {
let my_row: MyRow = row?;
}
# Ok(())
# }
Expand Down
4 changes: 2 additions & 2 deletions docs/source/queries/simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ use scylla::IntoTypedRows;
// Query rows from the table and print them
let result = session.query_unpaged("SELECT a FROM ks.tab", &[])
.await?
.into_rows_result()?
.unwrap();
.into_rows_result()?;

let mut iter = result.rows::<(i32,)>()?;
while let Some(read_row) = iter.next().transpose()? {
println!("Read a value from row: {}", read_row.0);
Expand Down
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 2ac12fe

Please sign in to comment.