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
use std::ops::DerefMut;
use std::sync::atomic::AtomicI64;
use google_cloud_googleapis::Status;
use prost_types::Struct;
use google_cloud_gax::call_option::BackoffRetrySettings;
use google_cloud_googleapis::spanner::v1::request_options::Priority;
use google_cloud_googleapis::spanner::v1::{
execute_sql_request::QueryMode, execute_sql_request::QueryOptions as ExecuteQueryOptions,
ExecuteSqlRequest, ReadRequest, RequestOptions, TransactionSelector,
};
use crate::key::{Key, KeySet};
use crate::reader::{AsyncIterator, RowIterator, StatementReader, TableReader};
use crate::row::Row;
use crate::session::ManagedSession;
use crate::statement::Statement;
#[derive(Clone)]
pub struct CallOptions {
pub priority: Option<Priority>,
pub call_setting: Option<BackoffRetrySettings>,
}
impl Default for CallOptions {
fn default() -> Self {
CallOptions {
priority: None,
call_setting: None,
}
}
}
#[derive(Clone)]
pub struct ReadOptions {
pub index: String,
pub limit: i64,
pub call_options: CallOptions,
}
impl Default for ReadOptions {
fn default() -> Self {
ReadOptions {
index: "".to_string(),
limit: 0,
call_options: CallOptions::default(),
}
}
}
#[derive(Clone)]
pub struct QueryOptions {
pub mode: QueryMode,
pub optimizer_options: Option<ExecuteQueryOptions>,
pub call_options: CallOptions,
}
impl Default for QueryOptions {
fn default() -> Self {
QueryOptions {
mode: QueryMode::Normal,
optimizer_options: None,
call_options: CallOptions::default(),
}
}
}
pub struct Transaction {
pub(crate) session: Option<ManagedSession>,
pub(crate) sequence_number: AtomicI64,
pub(crate) transaction_selector: TransactionSelector,
}
impl Transaction {
pub(crate) fn create_request_options(priority: Option<Priority>) -> Option<RequestOptions> {
priority.map(|s| RequestOptions {
priority: s.into(),
request_tag: "".to_string(),
transaction_tag: "".to_string(),
})
}
pub async fn query(&mut self, statement: Statement) -> Result<RowIterator<'_>, Status> {
return self
.query_with_option(statement, QueryOptions::default())
.await;
}
pub async fn query_with_option(
&mut self,
statement: Statement,
options: QueryOptions,
) -> Result<RowIterator<'_>, Status> {
let request = ExecuteSqlRequest {
session: self.session.as_ref().unwrap().session.name.to_string(),
transaction: Some(self.transaction_selector.clone()),
sql: statement.sql,
params: Some(Struct {
fields: statement.params,
}),
param_types: statement.param_types,
resume_token: vec![],
query_mode: options.mode.into(),
partition_token: vec![],
seqno: 0,
query_options: options.optimizer_options,
request_options: Transaction::create_request_options(options.call_options.priority),
};
let session = self.session.as_mut().unwrap().deref_mut();
return RowIterator::new(
session,
Box::new(StatementReader {
request,
call_setting: options.call_options.call_setting,
}),
)
.await;
}
pub async fn read(
&mut self,
table: &str,
columns: &[&str],
key_set: impl Into<KeySet>,
) -> Result<RowIterator<'_>, Status> {
return self
.read_with_option(table, columns, key_set, ReadOptions::default())
.await;
}
pub async fn read_with_option(
&mut self,
table: &str,
columns: &[&str],
key_set: impl Into<KeySet>,
options: ReadOptions,
) -> Result<RowIterator<'_>, Status> {
let request = ReadRequest {
session: self.get_session_name(),
transaction: Some(self.transaction_selector.clone()),
table: table.to_string(),
index: options.index,
columns: columns.iter().map(|x| x.to_string()).collect(),
key_set: Some(key_set.into().inner),
limit: options.limit,
resume_token: vec![],
partition_token: vec![],
request_options: Transaction::create_request_options(options.call_options.priority),
};
let session = self.as_mut_session();
return RowIterator::new(
session,
Box::new(TableReader {
request,
call_setting: options.call_options.call_setting,
}),
)
.await;
}
pub async fn read_row(
&mut self,
table: &str,
columns: &[&str],
key: Key,
) -> Result<Option<Row>, Status> {
return self
.read_row_with_option(table, columns, key, ReadOptions::default())
.await;
}
pub async fn read_row_with_option(
&mut self,
table: &str,
columns: &[&str],
key: Key,
options: ReadOptions,
) -> Result<Option<Row>, Status> {
let mut reader = self
.read_with_option(table, columns, KeySet::from(key), options)
.await?;
return reader.next().await;
}
pub(crate) fn get_session_name(&self) -> String {
return self.session.as_ref().unwrap().session.name.to_string();
}
pub(crate) fn as_mut_session(&mut self) -> &mut ManagedSession {
return self.session.as_mut().unwrap();
}
pub(crate) fn take_session(&mut self) -> Option<ManagedSession> {
self.session.take()
}
}