Skip to content

Commit 9a7fceb

Browse files
piodulwprzytula
andcommitted
treewide: get rid of (most) uses of QueryResult::rows
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]>
1 parent 7648b1b commit 9a7fceb

31 files changed

+357
-548
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ let uri = "127.0.0.1:9042";
1919

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

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

docs/source/data-types/blob.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ session
1717
.await?;
1818

1919
// Read blobs from the table
20-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
21-
for row in rows.into_typed::<(Vec<u8>,)>() {
22-
let (blob_value,): (Vec<u8>,) = row?;
23-
}
20+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
21+
let mut iter = result.rows_typed::<(Vec<u8>,)>()?;
22+
while let Some((blob_value,)) = iter.next().transpose()? {
23+
println!("{:?}", blob_value);
2424
}
2525
# Ok(())
2626
# }

docs/source/data-types/collections.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ session
1717
.await?;
1818

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

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

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

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

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

148148
// Read a map from the table
149-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
150-
for row in rows.into_typed::<(BTreeMap<String, i32>,)>() {
151-
let (map_value,): (BTreeMap<String, i32>,) = row?;
152-
}
149+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
150+
let mut iter = result.rows_typed::<(BTreeMap<String, i32>,)>()?;
151+
while let Some((map_value,)) = iter.next().transpose()? {
152+
println!("{:?}", map_value);
153153
}
154154
# Ok(())
155155
# }

docs/source/data-types/counter.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use scylla::IntoTypedRows;
1111
use scylla::frame::value::Counter;
1212

1313
// Read counter from the table
14-
if let Some(rows) = session.query("SELECT c FROM keyspace.table", &[]).await?.rows {
15-
for row in rows.into_typed::<(Counter,)>() {
16-
let (counter_value,): (Counter,) = row?;
17-
let counter_int_value: i64 = counter_value.0;
18-
}
14+
let result = session.query("SELECT c FROM keyspace.table", &[]).await?;
15+
let mut iter = result.rows_typed::<(Counter,)>()?;
16+
while let Some((counter_value,)) = iter.next().transpose()? {
17+
let counter_int_value: i64 = counter_value.0;
18+
println!("{}", counter_int_value);
1919
}
2020
# Ok(())
2121
# }

docs/source/data-types/date.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,10 @@ session
6767
.await?;
6868

6969
// Read NaiveDate from the table
70-
if let Some(rows) = session
71-
.query("SELECT a FROM keyspace.table", &[])
72-
.await?
73-
.rows
74-
{
75-
for row in rows.into_typed::<(NaiveDate,)>() {
76-
let (date_value,): (NaiveDate,) = row?;
77-
}
70+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
71+
let mut iter = result.rows_typed::<(NaiveDate,)>()?;
72+
while let Some((date_value,)) = iter.next().transpose()? {
73+
println!("{:?}", date_value);
7874
}
7975
# Ok(())
8076
# }
@@ -105,14 +101,10 @@ session
105101
.await?;
106102

107103
// Read Date from the table
108-
if let Some(rows) = session
109-
.query("SELECT a FROM keyspace.table", &[])
110-
.await?
111-
.rows
112-
{
113-
for row in rows.into_typed::<(Date,)>() {
114-
let (date_value,): (Date,) = row?;
115-
}
104+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
105+
let mut iter = result.rows_typed::<(Date,)>()?;
106+
while let Some((date_value,)) = iter.next().transpose()? {
107+
println!("{:?}", date_value);
116108
}
117109
# Ok(())
118110
# }

docs/source/data-types/decimal.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ session
5252
.await?;
5353

5454
// Read a decimal from the table
55-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
56-
for row in rows.into_typed::<(BigDecimal,)>() {
57-
let (decimal_value,): (BigDecimal,) = row?;
58-
}
55+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
56+
let mut iter = result.rows_typed::<(BigDecimal,)>()?;
57+
while let Some((decimal_value,)) = iter.next().transpose()? {
58+
println!("{:?}", decimal_value);
5959
}
6060
# Ok(())
6161
# }

docs/source/data-types/duration.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
use scylla::IntoTypedRows;
1010
use scylla::frame::value::CqlDuration;
1111

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

18-
// Read inet from the table
19-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
20-
for row in rows.into_typed::<(CqlDuration,)>() {
21-
let (cql_duration,): (CqlDuration,) = row?;
22-
}
18+
// Read duration from the table
19+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
20+
let mut iter = result.rows_typed::<(CqlDuration,)>()?;
21+
while let Some((duration_value,)) = iter.next().transpose()? {
22+
println!("{:?}", duration_value);
2323
}
2424
# Ok(())
2525
# }

docs/source/data-types/inet.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ session
1616
.await?;
1717

1818
// Read inet from the table
19-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
20-
for row in rows.into_typed::<(IpAddr,)>() {
21-
let (inet_value,): (IpAddr,) = row?;
22-
}
19+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
20+
let mut iter = result.rows_typed::<(IpAddr,)>()?;
21+
while let Some((inet_value,)) = iter.next().transpose()? {
22+
println!("{:?}", inet_value);
2323
}
2424
# Ok(())
2525
# }

docs/source/data-types/primitive.md

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Bool, Tinyint, Smallint, Int, Bigint, Float, Double
22

33
### Bool
4+
45
`Bool` is represented as rust `bool`
56

67
```rust
@@ -17,16 +18,17 @@ session
1718
.await?;
1819

1920
// Read a bool from the table
20-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
21-
for row in rows.into_typed::<(bool,)>() {
22-
let (bool_value,): (bool,) = row?;
23-
}
21+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
22+
let mut iter = result.rows_typed::<(bool,)>()?;
23+
while let Some((bool_value,)) = iter.next().transpose()? {
24+
println!("{}", bool_value);
2425
}
2526
# Ok(())
2627
# }
2728
```
2829

2930
### Tinyint
31+
3032
`Tinyint` is represented as rust `i8`
3133

3234
```rust
@@ -43,16 +45,17 @@ session
4345
.await?;
4446

4547
// Read a tinyint from the table
46-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
47-
for row in rows.into_typed::<(i8,)>() {
48-
let (tinyint_value,): (i8,) = row?;
49-
}
48+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
49+
let mut iter = result.rows_typed::<(i8,)>()?;
50+
while let Some((tinyint_value,)) = iter.next().transpose()? {
51+
println!("{:?}", tinyint_value);
5052
}
5153
# Ok(())
5254
# }
5355
```
5456

5557
### Smallint
58+
5659
`Smallint` is represented as rust `i16`
5760

5861
```rust
@@ -69,16 +72,17 @@ session
6972
.await?;
7073

7174
// Read a smallint from the table
72-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
73-
for row in rows.into_typed::<(i16,)>() {
74-
let (smallint_value,): (i16,) = row?;
75-
}
75+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
76+
let mut iter = result.rows_typed::<(i16,)>()?;
77+
while let Some((smallint_value,)) = iter.next().transpose()? {
78+
println!("{}", smallint_value);
7679
}
7780
# Ok(())
7881
# }
7982
```
8083

8184
### Int
85+
8286
`Int` is represented as rust `i32`
8387

8488
```rust
@@ -95,16 +99,17 @@ session
9599
.await?;
96100

97101
// Read an int from the table
98-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
99-
for row in rows.into_typed::<(i32,)>() {
100-
let (int_value,): (i32,) = row?;
101-
}
102+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
103+
let mut iter = result.rows_typed::<(i32,)>()?;
104+
while let Some((int_value,)) = iter.next().transpose()? {
105+
println!("{}", int_value);
102106
}
103107
# Ok(())
104108
# }
105109
```
106110

107111
### Bigint
112+
108113
`Bigint` is represented as rust `i64`
109114

110115
```rust
@@ -121,16 +126,17 @@ session
121126
.await?;
122127

123128
// Read a bigint from the table
124-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
125-
for row in rows.into_typed::<(i64,)>() {
126-
let (bigint_value,): (i64,) = row?;
127-
}
129+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
130+
let mut iter = result.rows_typed::<(i64,)>()?;
131+
while let Some((bigint_value,)) = iter.next().transpose()? {
132+
println!("{:?}", bigint_value);
128133
}
129134
# Ok(())
130135
# }
131136
```
132137

133-
### Float
138+
### Float
139+
134140
`Float` is represented as rust `f32`
135141

136142
```rust
@@ -147,16 +153,17 @@ session
147153
.await?;
148154

149155
// Read a float from the table
150-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
151-
for row in rows.into_typed::<(f32,)>() {
152-
let (float_value,): (f32,) = row?;
153-
}
156+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
157+
let mut iter = result.rows_typed::<(f32,)>()?;
158+
while let Some((float_value,)) = iter.next().transpose()? {
159+
println!("{:?}", float_value);
154160
}
155161
# Ok(())
156162
# }
157163
```
158164

159165
### Double
166+
160167
`Double` is represented as rust `f64`
161168

162169
```rust
@@ -173,11 +180,11 @@ session
173180
.await?;
174181

175182
// Read a double from the table
176-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
177-
for row in rows.into_typed::<(f64,)>() {
178-
let (double_value,): (f64,) = row?;
179-
}
183+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
184+
let mut iter = result.rows_typed::<(f64,)>()?;
185+
while let Some((double_value,)) = iter.next().transpose()? {
186+
println!("{:?}", double_value);
180187
}
181188
# Ok(())
182189
# }
183-
```
190+
```

0 commit comments

Comments
 (0)