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
use crate::describe::Describe;
use crate::error::Error;
use crate::executor::{Execute, Executor};
use crate::logger::QueryLogger;
use crate::mssql::connection::prepare::prepare;
use crate::mssql::protocol::col_meta_data::Flags;
use crate::mssql::protocol::done::Status;
use crate::mssql::protocol::message::Message;
use crate::mssql::protocol::packet::PacketType;
use crate::mssql::protocol::rpc::{OptionFlags, Procedure, RpcRequest};
use crate::mssql::protocol::sql_batch::SqlBatch;
use crate::mssql::{
Mssql, MssqlArguments, MssqlConnection, MssqlQueryResult, MssqlRow, MssqlStatement,
MssqlTypeInfo,
};
use either::Either;
use futures_core::future::BoxFuture;
use futures_core::stream::BoxStream;
use futures_util::TryStreamExt;
use std::borrow::Cow;
use std::sync::Arc;
impl MssqlConnection {
async fn run(&mut self, query: &str, arguments: Option<MssqlArguments>) -> Result<(), Error> {
self.stream.wait_until_ready().await?;
self.stream.pending_done_count += 1;
if let Some(mut arguments) = arguments {
let proc = Either::Right(Procedure::ExecuteSql);
let mut proc_args = MssqlArguments::default();
proc_args.add_unnamed(query);
if !arguments.data.is_empty() {
proc_args.add_unnamed(&*arguments.declarations);
proc_args.append(&mut arguments);
}
self.stream.write_packet(
PacketType::Rpc,
RpcRequest {
transaction_descriptor: self.stream.transaction_descriptor,
arguments: &proc_args,
procedure: proc,
options: OptionFlags::empty(),
},
);
} else {
self.stream.write_packet(
PacketType::SqlBatch,
SqlBatch {
transaction_descriptor: self.stream.transaction_descriptor,
sql: query,
},
);
}
self.stream.flush().await?;
Ok(())
}
}
impl<'c> Executor<'c> for &'c mut MssqlConnection {
type Database = Mssql;
fn fetch_many<'e, 'q: 'e, E: 'q>(
self,
mut query: E,
) -> BoxStream<'e, Result<Either<MssqlQueryResult, MssqlRow>, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
{
let sql = query.sql();
let arguments = query.take_arguments();
let mut logger = QueryLogger::new(sql, self.log_settings.clone());
Box::pin(try_stream! {
self.run(sql, arguments).await?;
loop {
let message = self.stream.recv_message().await?;
match message {
Message::Row(row) => {
let columns = Arc::clone(&self.stream.columns);
let column_names = Arc::clone(&self.stream.column_names);
logger.increment_rows_returned();
r#yield!(Either::Right(MssqlRow { row, column_names, columns }));
}
Message::Done(done) | Message::DoneProc(done) => {
if !done.status.contains(Status::DONE_MORE) {
self.stream.handle_done(&done);
}
if done.status.contains(Status::DONE_COUNT) {
let rows_affected = done.affected_rows;
logger.increase_rows_affected(rows_affected);
r#yield!(Either::Left(MssqlQueryResult {
rows_affected,
}));
}
if !done.status.contains(Status::DONE_MORE) {
break;
}
}
Message::DoneInProc(done) => {
if done.status.contains(Status::DONE_COUNT) {
let rows_affected = done.affected_rows;
logger.increase_rows_affected(rows_affected);
r#yield!(Either::Left(MssqlQueryResult {
rows_affected,
}));
}
}
_ => {}
}
}
Ok(())
})
}
fn fetch_optional<'e, 'q: 'e, E: 'q>(
self,
query: E,
) -> BoxFuture<'e, Result<Option<MssqlRow>, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
{
let mut s = self.fetch_many(query);
Box::pin(async move {
while let Some(v) = s.try_next().await? {
if let Either::Right(r) = v {
return Ok(Some(r));
}
}
Ok(None)
})
}
fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
_parameters: &[MssqlTypeInfo],
) -> BoxFuture<'e, Result<MssqlStatement<'q>, Error>>
where
'c: 'e,
{
Box::pin(async move {
let metadata = prepare(self, sql).await?;
Ok(MssqlStatement {
sql: Cow::Borrowed(sql),
metadata,
})
})
}
fn describe<'e, 'q: 'e>(
self,
sql: &'q str,
) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>>
where
'c: 'e,
{
Box::pin(async move {
let metadata = prepare(self, sql).await?;
let mut nullable = Vec::with_capacity(metadata.columns.len());
for col in metadata.columns.iter() {
nullable.push(Some(col.flags.contains(Flags::NULLABLE)));
}
Ok(Describe {
nullable,
columns: (metadata.columns).clone(),
parameters: None,
})
})
}
}