1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::result_set_metadata::ResultSetMetadata;
use crate::value::Value;
/// A row in a query result.
#[derive(Clone, Debug, PartialEq)]
pub struct Row {
pub(crate) values: Vec<Value>,
pub(crate) metadata: ResultSetMetadata,
}
pub(crate) mod private {
/// A sealed trait to prevent external implementation of `ColumnIndex`.
pub trait Sealed {}
impl Sealed for usize {}
impl Sealed for &str {}
impl Sealed for String {}
}
/// A trait for types that can be used to index into a [`Row`].
///
/// This trait is sealed and cannot be implemented for types outside of this crate.
pub trait ColumnIndex: private::Sealed + std::fmt::Debug {
/// Returns the index of the column in the given row, if it exists.
fn index(&self, row: &Row) -> Option<usize>;
}
impl ColumnIndex for usize {
fn index(&self, _row: &Row) -> Option<usize> {
Some(*self)
}
}
impl ColumnIndex for &str {
fn index(&self, row: &Row) -> Option<usize> {
row.metadata
.column_names
.iter()
.position(|name| name == *self)
}
}
impl ColumnIndex for String {
fn index(&self, row: &Row) -> Option<usize> {
self.as_str().index(row)
}
}
/// Errors that can occur when getting a value from a [`Row`].
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum RowError {
/// The requested column name or index was not found in the row.
#[error("Could not find column with index: {0}")]
ColumnNotFound(String),
/// The requested column index was out of range.
#[error("Column index out of range: {index} (expected < {len})")]
IndexOutOfRange { index: usize, len: usize },
}
impl Row {
/// Returns the raw values of the row.
pub fn raw_values(&self) -> &[Value] {
&self.values
}
/// Returns true if the value at the specified column name or index is null.
///
/// # Example
/// ```
/// # use google_cloud_spanner::client::Spanner;
/// # use google_cloud_spanner::statement::Statement;
/// # async fn test_doc() -> anyhow::Result<()> {
/// let client = Spanner::builder().build().await?;
/// let db_client = client.database_client("projects/p/instances/i/databases/d").build().await?;
/// let transaction = db_client.single_use().build();
/// let mut result_set = transaction.execute_query(Statement::builder("SELECT NULL AS Age").build()).await?;
///
/// if let Some(row) = result_set.next().await {
/// let is_null = row?.try_is_null("Age")?;
/// println!("Is null: {}", is_null);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Arguments
///
/// * `index` - The column name (string) or index (zero-based integer).
///
/// # Returns
///
/// * `Ok(bool)` if the value is null or not.
/// * `Err(Error)` if the column name or index is invalid.
pub fn try_is_null<I: ColumnIndex>(&self, index: I) -> crate::Result<bool> {
let (_, value) = self.get_value(index)?;
Ok(value.kind() == crate::value::Kind::Null)
}
/// Returns true if the value at the specified column name or index is null, panicking on error.
///
/// # Example
/// ```
/// # use google_cloud_spanner::client::Spanner;
/// # use google_cloud_spanner::statement::Statement;
/// # async fn test_doc() -> anyhow::Result<()> {
/// let client = Spanner::builder().build().await?;
/// let db_client = client.database_client("projects/p/instances/i/databases/d").build().await?;
/// let transaction = db_client.single_use().build();
/// let mut result_set = transaction.execute_query(Statement::builder("SELECT NULL AS Age").build()).await?;
///
/// if let Some(row) = result_set.next().await {
/// let is_null = row?.is_null("Age");
/// println!("Is null: {}", is_null);
/// }
/// # Ok(())
/// # }
/// ```
///
/// This is a convenience wrapper around [`try_is_null`](Row::try_is_null).
///
/// # Panics
///
/// Panics if the column name or index is invalid.
pub fn is_null<I: ColumnIndex>(&self, index: I) -> bool {
self.try_is_null(index).unwrap()
}
/// Retrieves a value from the row by column name or zero-based index.
///
/// # Example
/// ```
/// # use google_cloud_spanner::client::Spanner;
/// # use google_cloud_spanner::statement::Statement;
/// # async fn test_doc() -> anyhow::Result<()> {
/// let client = Spanner::builder().build().await?;
/// let db_client = client.database_client("projects/p/instances/i/databases/d").build().await?;
/// let transaction = db_client.single_use().build();
/// let mut result_set = transaction.execute_query(Statement::builder("SELECT 42 AS Age").build()).await?;
///
/// if let Some(row) = result_set.next().await {
/// let age: i64 = row?.try_get("Age")?;
/// println!("Age: {}", age);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Arguments
///
/// * `index` - The column name (string) or index (zero-based integer).
///
/// # Returns
///
/// * `Ok(T)` if the value was successfully retrieved and converted to type `T`.
/// * `Err(Error)` if:
/// * The column name or index is invalid.
/// * The column value is incompatible with type `T`.
pub fn try_get<T: crate::from_value::FromValue, I: ColumnIndex>(
&self,
index: I,
) -> crate::Result<T> {
let (idx, value) = self.get_value(index)?;
let r#type = self.metadata.column_types.get(idx).ok_or_else(|| {
crate::Error::deser(RowError::IndexOutOfRange {
index: idx,
len: self.metadata.column_types.len(),
})
})?;
T::from_value(value, r#type).map_err(crate::Error::deser)
}
/// Retrieves a value from the row by column name or zero-based index, panicking on error.
///
/// # Example
/// ```
/// # use google_cloud_spanner::client::Spanner;
/// # use google_cloud_spanner::statement::Statement;
/// # async fn test_doc() -> anyhow::Result<()> {
/// let client = Spanner::builder().build().await?;
/// let db_client = client.database_client("projects/p/instances/i/databases/d").build().await?;
/// let transaction = db_client.single_use().build();
/// let mut result_set = transaction.execute_query(Statement::builder("SELECT 42 AS Age").build()).await?;
///
/// if let Some(row) = result_set.next().await {
/// let age: i64 = row?.get("Age");
/// println!("Age: {}", age);
/// }
/// # Ok(())
/// # }
/// ```
///
/// This is a convenience wrapper around [`try_get`](Row::try_get).
///
/// # Panics
///
/// Panics if:
/// * The column name or index is invalid.
/// * The column value is incompatible with type `T`.
pub fn get<T: crate::from_value::FromValue, I: ColumnIndex>(&self, index: I) -> T {
self.try_get(index).unwrap()
}
fn get_value<I: ColumnIndex>(&self, index: I) -> crate::Result<(usize, &Value)> {
let idx = index
.index(self)
.ok_or_else(|| crate::Error::deser(RowError::ColumnNotFound(format!("{:?}", index))))?;
let value = self.values.get(idx).ok_or_else(|| {
crate::Error::deser(RowError::IndexOutOfRange {
index: idx,
len: self.values.len(),
})
})?;
Ok((idx, value))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::to_value::ToValue;
use crate::types;
use rust_decimal::Decimal;
use std::sync::Arc;
use time::{Date, Month, OffsetDateTime};
#[test]
fn auto_traits() {
static_assertions::assert_impl_all!(Row: Clone, std::fmt::Debug, PartialEq, Send, Sync);
}
#[test]
fn row_get() {
let names = vec![
"col_string".to_string(),
"col_int64".to_string(),
"col_float64".to_string(),
"col_bool".to_string(),
"col_bytes".to_string(),
"col_numeric".to_string(),
"col_date".to_string(),
"col_timestamp".to_string(),
"col_float32".to_string(),
"col_json".to_string(),
"col_uuid".to_string(),
"col_interval".to_string(),
];
let types = vec![
types::string(),
types::int64(),
types::float64(),
types::bool(),
types::bytes(),
types::numeric(),
types::date(),
types::timestamp(),
types::float32(),
types::json(),
types::uuid(),
types::interval(),
];
let d = Decimal::from_str_exact("123.456").unwrap();
let dt = Date::from_calendar_date(2023, Month::October, 27).unwrap();
let ts = OffsetDateTime::parse(
"2023-10-27T10:00:00Z",
&time::format_description::well_known::Rfc3339,
)
.unwrap();
let values = vec![
"hello".to_string().to_value(),
42_i64.to_value(),
42.5_f64.to_value(),
true.to_value(),
vec![1_u8, 2, 3].to_value(),
d.to_value(),
dt.to_value(),
ts.to_value(),
1.23_f32.to_value(),
"{\"key\":\"value\"}".to_string().to_value(),
"123e4567-e89b-12d3-a456-426614174000"
.to_string()
.to_value(),
"P1Y2M3D".to_string().to_value(),
];
let row = Row {
values,
metadata: ResultSetMetadata {
column_names: Arc::new(names),
column_types: Arc::new(types),
undeclared_parameters: Arc::new(std::collections::BTreeMap::new()),
},
};
// Test getting by valid index
assert_eq!(row.get::<String, _>(0), "hello");
assert_eq!(row.get::<i64, _>(1), 42);
assert_eq!(row.get::<f64, _>(2), 42.5);
assert!(row.get::<bool, _>(3));
assert_eq!(row.get::<Vec<u8>, _>(4), vec![1_u8, 2, 3]);
assert_eq!(row.get::<Decimal, _>(5), d);
assert_eq!(row.get::<Date, _>(6), dt);
assert_eq!(row.get::<OffsetDateTime, _>(7), ts);
assert_eq!(row.get::<f32, _>(8), 1.23_f32);
assert_eq!(row.get::<String, _>(9), "{\"key\":\"value\"}");
assert_eq!(
row.get::<String, _>(10),
"123e4567-e89b-12d3-a456-426614174000"
);
assert_eq!(row.get::<String, _>(11), "P1Y2M3D");
// Test getting by valid name
assert_eq!(row.get::<String, _>("col_string"), "hello");
assert_eq!(row.get::<i64, _>("col_int64"), 42);
assert_eq!(row.get::<f64, _>("col_float64"), 42.5);
assert!(row.get::<bool, _>("col_bool"));
assert_eq!(row.get::<Vec<u8>, _>("col_bytes"), vec![1_u8, 2, 3]);
assert_eq!(row.get::<Decimal, _>("col_numeric"), d);
assert_eq!(row.get::<Date, _>("col_date"), dt);
assert_eq!(row.get::<OffsetDateTime, _>("col_timestamp"), ts);
assert_eq!(row.get::<f32, _>("col_float32"), 1.23_f32);
assert_eq!(row.get::<String, _>("col_json"), "{\"key\":\"value\"}");
assert_eq!(
row.get::<String, _>("col_uuid"),
"123e4567-e89b-12d3-a456-426614174000"
);
assert_eq!(row.get::<String, _>("col_interval"), "P1Y2M3D");
// Test getting by invalid index
assert!(row.try_get::<String, _>(12).is_err());
// Test getting by invalid name
assert!(row.try_get::<String, _>("col_invalid").is_err());
// Test getting mismatched type
assert!(row.try_get::<i64, _>(0).is_err());
assert!(row.try_get::<bool, _>(1).is_err());
// int64 is encoded as a string, so getting it as a string is also possible.
assert_eq!(row.get::<String, _>(1), "42");
}
}