Skip to content

Commit

Permalink
treewide: get rid of (most) uses of QueryResult::rows
Browse files Browse the repository at this point in the history
This commit serves two purposes:

- Modernizes the existing examples and internal uses of QueryResult in
  the driver. We have had helpers such as rows_typed etc. for a long
  time and they are supposed to offer superior experience, although many
  of our tests still use QueryResult::rows directly.
- Prepares for the iterator-based deserialization refactor. The
  representation of QueryResult will change and the rows field will not
  be available anymore - instead, the helper methods very similar to the
  current ones will be mandatory.

Co-authored-by: Wojciech Przytuła <[email protected]>
  • Loading branch information
piodul and wprzytula committed Mar 14, 2024
1 parent 7648b1b commit 9316106
Show file tree
Hide file tree
Showing 31 changed files with 346 additions and 552 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ let uri = "127.0.0.1:9042";

let session: Session = SessionBuilder::new().known_node(uri).build().await?;

if let Some(rows) = session.query("SELECT a, b, c FROM ks.t", &[]).await?.rows {
for row in rows.into_typed::<(i32, i32, String)>() {
let (a, b, c) = row?;
println!("a, b, c: {}, {}, {}", a, b, c);
}
let result = session.query("SELECT a, b, c FROM ks.t", &[]).await?;
let mut iter = result.rows_typed::<(i32, i32, String)>()?;
while let Some((a, b, c)) = iter.next().transpose()? {
println!("a, b, c: {}, {}, {}", a, b, c);
}
```

Expand Down
8 changes: 4 additions & 4 deletions docs/source/data-types/blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ session
.await?;

// Read blobs from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(Vec<u8>,)>() {
let (blob_value,): (Vec<u8>,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(Vec<u8>,)>()?;
while let Some((blob_value,)) = iter.next().transpose()? {
println!("{:?}", blob_value);
}
# Ok(())
# }
Expand Down
48 changes: 24 additions & 24 deletions docs/source/data-types/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ session
.await?;

// Read a list of ints from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(Vec<i32>,)>() {
let (list_value,): (Vec<i32>,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(Vec<i32>,)>()?;
while let Some((list_value,)) = iter.next().transpose()? {
println!("{:?}", list_value);
}
# Ok(())
# }
Expand All @@ -43,10 +43,10 @@ session
.await?;

// Read a set of ints from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(Vec<i32>,)>() {
let (set_value,): (Vec<i32>,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(Vec<i32>,)>()?;
while let Some((list_value,)) = iter.next().transpose()? {
println!("{:?}", list_value);
}
# Ok(())
# }
Expand All @@ -67,10 +67,10 @@ session
.await?;

// Read a set of ints from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(HashSet<i32>,)>() {
let (set_value,): (HashSet<i32>,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(HashSet<i32>,)>()?;
while let Some((list_value,)) = iter.next().transpose()? {
println!("{:?}", list_value);
}
# Ok(())
# }
Expand All @@ -91,10 +91,10 @@ session
.await?;

// Read a set of ints from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(BTreeSet<i32>,)>() {
let (set_value,): (BTreeSet<i32>,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(BTreeSet<i32>,)>()?;
while let Some((list_value,)) = iter.next().transpose()? {
println!("{:?}", list_value);
}
# Ok(())
# }
Expand All @@ -120,10 +120,10 @@ session
.await?;

// Read a map from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(HashMap<String, i32>,)>() {
let (map_value,): (HashMap<String, i32>,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(HashMap<String, i32>,)>()?;
while let Some((map_value,)) = iter.next().transpose()? {
println!("{:?}", map_value);
}
# Ok(())
# }
Expand All @@ -146,10 +146,10 @@ session
.await?;

// Read a map from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(BTreeMap<String, i32>,)>() {
let (map_value,): (BTreeMap<String, i32>,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(BTreeMap<String, i32>,)>()?;
while let Some((map_value,)) = iter.next().transpose()? {
println!("{:?}", map_value);
}
# Ok(())
# }
Expand Down
10 changes: 5 additions & 5 deletions docs/source/data-types/counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use scylla::IntoTypedRows;
use scylla::frame::value::Counter;

// Read counter from the table
if let Some(rows) = session.query("SELECT c FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(Counter,)>() {
let (counter_value,): (Counter,) = row?;
let counter_int_value: i64 = counter_value.0;
}
let result = session.query("SELECT c FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(Counter,)>()?;
while let Some((counter_value,)) = iter.next().transpose()? {
let counter_int_value: i64 = counter_value.0;
println!("{}", counter_int_value);
}
# Ok(())
# }
Expand Down
24 changes: 8 additions & 16 deletions docs/source/data-types/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,10 @@ session
.await?;

// Read NaiveDate from the table
if let Some(rows) = session
.query("SELECT a FROM keyspace.table", &[])
.await?
.rows
{
for row in rows.into_typed::<(NaiveDate,)>() {
let (date_value,): (NaiveDate,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(NaiveDate,)>()?;
while let Some((date_value,)) = iter.next().transpose()? {
println!("{:?}", date_value);
}
# Ok(())
# }
Expand Down Expand Up @@ -105,14 +101,10 @@ session
.await?;

// Read Date from the table
if let Some(rows) = session
.query("SELECT a FROM keyspace.table", &[])
.await?
.rows
{
for row in rows.into_typed::<(Date,)>() {
let (date_value,): (Date,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(Date,)>()?;
while let Some((date_value,)) = iter.next().transpose()? {
println!("{:?}", date_value);
}
# Ok(())
# }
Expand Down
8 changes: 4 additions & 4 deletions docs/source/data-types/decimal.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ session
.await?;

// Read a decimal from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(BigDecimal,)>() {
let (decimal_value,): (BigDecimal,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(BigDecimal,)>()?;
while let Some((decimal_value,)) = iter.next().transpose()? {
println!("{:?}", decimal_value);
}
# Ok(())
# }
Expand Down
12 changes: 6 additions & 6 deletions docs/source/data-types/duration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
use scylla::IntoTypedRows;
use scylla::frame::value::CqlDuration;

// Insert some ip address into the table
// Insert some duration into the table
let to_insert: CqlDuration = CqlDuration { months: 1, days: 2, nanoseconds: 3 };
session
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;

// Read inet from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(CqlDuration,)>() {
let (cql_duration,): (CqlDuration,) = row?;
}
// Read duration from the table
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(CqlDuration,)>()?;
while let Some((duration_value,)) = iter.next().transpose()? {
println!("{:?}", duration_value);
}
# Ok(())
# }
Expand Down
8 changes: 4 additions & 4 deletions docs/source/data-types/inet.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ session
.await?;

// Read inet from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(IpAddr,)>() {
let (inet_value,): (IpAddr,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(IpAddr,)>()?;
while let Some((inet_value,)) = iter.next().transpose()? {
println!("{:?}", inet_value);
}
# Ok(())
# }
Expand Down
67 changes: 37 additions & 30 deletions docs/source/data-types/primitive.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Bool, Tinyint, Smallint, Int, Bigint, Float, Double

### Bool

`Bool` is represented as rust `bool`

```rust
Expand All @@ -17,16 +18,17 @@ session
.await?;

// Read a bool from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(bool,)>() {
let (bool_value,): (bool,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(bool,)>()?;
while let Some((bool_value,)) = iter.next().transpose()? {
println!("{}", bool_value);
}
# Ok(())
# }
```

### Tinyint

`Tinyint` is represented as rust `i8`

```rust
Expand All @@ -43,16 +45,17 @@ session
.await?;

// Read a tinyint from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(i8,)>() {
let (tinyint_value,): (i8,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(i8,)>()?;
while let Some((tinyint_value,)) = iter.next().transpose()? {
println!("{:?}", tinyint_value);
}
# Ok(())
# }
```

### Smallint

`Smallint` is represented as rust `i16`

```rust
Expand All @@ -69,16 +72,17 @@ session
.await?;

// Read a smallint from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(i16,)>() {
let (smallint_value,): (i16,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(i16,)>()?;
while let Some((smallint_value,)) = iter.next().transpose()? {
println!("{}", smallint_value);
}
# Ok(())
# }
```

### Int

`Int` is represented as rust `i32`

```rust
Expand All @@ -95,16 +99,17 @@ session
.await?;

// Read an int from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(i32,)>() {
let (int_value,): (i32,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(i32,)>()?;
while let Some((int_value,)) = iter.next().transpose()? {
println!("{}", int_value);
}
# Ok(())
# }
```

### Bigint

`Bigint` is represented as rust `i64`

```rust
Expand All @@ -121,16 +126,17 @@ session
.await?;

// Read a bigint from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(i64,)>() {
let (bigint_value,): (i64,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(i64,)>()?;
while let Some((bigint_value,)) = iter.next().transpose()? {
println!("{:?}", bigint_value);
}
# Ok(())
# }
```

### Float
### Float

`Float` is represented as rust `f32`

```rust
Expand All @@ -147,16 +153,17 @@ session
.await?;

// Read a float from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(f32,)>() {
let (float_value,): (f32,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(f32,)>()?;
while let Some((float_value,)) = iter.next().transpose()? {
println!("{:?}", float_value);
}
# Ok(())
# }
```

### Double

`Double` is represented as rust `f64`

```rust
Expand All @@ -173,11 +180,11 @@ session
.await?;

// Read a double from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(f64,)>() {
let (double_value,): (f64,) = row?;
}
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
let mut iter = result.rows_typed::<(f64,)>()?;
while let Some((double_value,)) = iter.next().transpose()? {
println!("{:?}", double_value);
}
# Ok(())
# }
```
```
Loading

0 comments on commit 9316106

Please sign in to comment.