-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathmod.rs
1371 lines (1232 loc) · 45.4 KB
/
mod.rs
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2016 Anatoly Ikorsky
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
pub use mysql_common::named_params;
use mysql_common::{
constants::DEFAULT_MAX_ALLOWED_PACKET,
crypto,
packets::{
parse_auth_switch_request, parse_handshake_packet, AuthPlugin, AuthSwitchRequest,
HandshakeResponse, SslRequest,
},
};
use std::{
fmt,
future::Future,
mem,
pin::Pin,
str::FromStr,
sync::Arc,
time::{Duration, Instant},
};
use crate::{
conn::{pool::Pool, stmt_cache::StmtCache},
connection_like::{streamless::Streamless, ConnectionLike, StmtCacheResult},
consts::{self, CapabilityFlags},
error::*,
io::Stream,
local_infile_handler::LocalInfileHandler,
opts::Opts,
queryable::{query_result, BinaryProtocol, Queryable, TextProtocol},
Column, OptsBuilder,
};
pub mod pool;
pub mod stmt_cache;
/// Helper that asynchronously disconnects connection on the default tokio executor.
fn disconnect(mut conn: Conn) {
let disconnected = conn.inner.disconnected;
// Mark conn as disconnected.
conn.inner.disconnected = true;
if !disconnected {
// We shouldn't call tokio::spawn if unwinding
if std::thread::panicking() {
return;
}
// Server will report broken connection if spawn fails.
// this might fail if, say, the runtime is shutting down, but we've done what we could
if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle.spawn(async move {
if let Ok(conn) = conn.cleanup().await {
let _ = conn.disconnect().await;
}
});
}
}
}
/// Mysql connection
struct ConnInner {
stream: Option<Stream>,
id: u32,
version: (u16, u16, u16),
max_allowed_packet: usize,
socket: Option<String>,
capabilities: consts::CapabilityFlags,
status: consts::StatusFlags,
last_insert_id: u64,
affected_rows: u64,
warnings: u16,
pool: Option<Pool>,
has_result: Option<(Arc<Vec<Column>>, Option<StmtCacheResult>)>,
in_transaction: bool,
opts: Opts,
last_io: Instant,
wait_timeout: Duration,
stmt_cache: StmtCache,
nonce: Vec<u8>,
auth_plugin: AuthPlugin<'static>,
auth_switched: bool,
/// Connection is already disconnected.
disconnected: bool,
}
impl fmt::Debug for ConnInner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Conn")
.field("connection id", &self.id)
.field("server version", &self.version)
.field("pool", &self.pool)
.field("has result", &self.has_result.is_some())
.field("in transaction", &self.in_transaction)
.field("stream", &self.stream)
.field("options", &self.opts)
.finish()
}
}
impl ConnInner {
/// Constructs an empty connection.
fn empty(opts: Opts) -> ConnInner {
ConnInner {
capabilities: opts.get_capabilities(),
status: consts::StatusFlags::empty(),
last_insert_id: 0,
affected_rows: 0,
stream: None,
max_allowed_packet: DEFAULT_MAX_ALLOWED_PACKET,
warnings: 0,
version: (0, 0, 0),
id: 0,
has_result: None,
pool: None,
in_transaction: false,
last_io: Instant::now(),
wait_timeout: Duration::from_secs(0),
stmt_cache: StmtCache::new(opts.get_stmt_cache_size()),
socket: opts.get_socket().map(Into::into),
opts,
nonce: Vec::default(),
auth_plugin: AuthPlugin::MysqlNativePassword,
auth_switched: false,
disconnected: false,
}
}
}
#[derive(Debug)]
pub struct Conn {
inner: Box<ConnInner>,
}
impl Conn {
/// Returns the ID generated by a query (usually `INSERT`) on a table with a column having the
/// `AUTO_INCREMENT` attribute. Returns `None` if there was no previous query on the connection
/// or if the query did not update an AUTO_INCREMENT value.
pub fn last_insert_id(&self) -> Option<u64> {
self.get_last_insert_id()
}
/// Returns the number of rows affected by the last `INSERT`, `UPDATE`, `REPLACE` or `DELETE`
/// query.
pub fn affected_rows(&self) -> u64 {
self.get_affected_rows()
}
async fn close(mut self) -> Result<()> {
self.inner.disconnected = true;
self.cleanup().await?.disconnect().await
}
fn is_secure(&self) -> bool {
if let Some(ref stream) = self.inner.stream {
stream.is_secure()
} else {
false
}
}
/// Hacky way to move connection through &mut. `self` becomes unusable.
fn take(&mut self) -> Conn {
let inner = mem::replace(&mut *self.inner, ConnInner::empty(Default::default()));
Conn {
inner: Box::new(inner),
}
}
fn empty(opts: Opts) -> Self {
Self {
inner: Box::new(ConnInner::empty(opts)),
}
}
fn setup_stream(mut self) -> Result<Conn> {
if let Some(stream) = self.inner.stream.take() {
stream.set_keepalive_ms(self.inner.opts.get_tcp_keepalive())?;
stream.set_tcp_nodelay(self.inner.opts.get_tcp_nodelay())?;
self.inner.stream = Some(stream);
Ok(self)
} else {
unreachable!();
}
}
async fn handle_handshake(self) -> Result<Conn> {
let (mut conn, packet) = self.read_packet().await?;
let handshake = parse_handshake_packet(&*packet)?;
conn.inner.nonce = {
let mut nonce = Vec::from(handshake.scramble_1_ref());
nonce.extend_from_slice(handshake.scramble_2_ref().unwrap_or(&[][..]));
nonce
};
conn.inner.capabilities = handshake.capabilities() & conn.inner.opts.get_capabilities();
conn.inner.version = handshake.server_version_parsed().unwrap_or((0, 0, 0));
conn.inner.id = handshake.connection_id();
conn.inner.status = handshake.status_flags();
conn.inner.auth_plugin = match handshake.auth_plugin() {
Some(AuthPlugin::MysqlNativePassword) => AuthPlugin::MysqlNativePassword,
Some(AuthPlugin::CachingSha2Password) => AuthPlugin::CachingSha2Password,
Some(AuthPlugin::Other(ref name)) => {
let name = String::from_utf8_lossy(name).into();
return Err(DriverError::UnknownAuthPlugin { name }.into());
}
None => AuthPlugin::MysqlNativePassword,
};
Ok(conn)
}
async fn switch_to_ssl_if_needed(self) -> Result<Conn> {
if self
.inner
.opts
.get_capabilities()
.contains(CapabilityFlags::CLIENT_SSL)
{
let ssl_request = SslRequest::new(self.inner.capabilities);
let conn = self.write_packet(ssl_request.as_ref()).await?;
let ssl_opts = conn
.get_opts()
.get_ssl_opts()
.cloned()
.expect("unreachable");
let domain = conn.get_opts().get_ip_or_hostname().into();
let (streamless, stream) = conn.take_stream();
let stream = stream.make_secure(domain, ssl_opts).await?;
Ok(streamless.return_stream(stream))
} else {
Ok(self)
}
}
async fn do_handshake_response(self) -> Result<Conn> {
let auth_data = self
.inner
.auth_plugin
.gen_data(self.inner.opts.get_pass(), &*self.inner.nonce);
let handshake_response = HandshakeResponse::new(
&auth_data,
self.inner.version,
self.inner.opts.get_user(),
self.inner.opts.get_db_name(),
&self.inner.auth_plugin,
self.get_capabilities(),
&Default::default(), // TODO: Add support
);
self.write_packet(handshake_response.as_ref()).await
}
async fn perform_auth_switch(
mut self,
auth_switch_request: AuthSwitchRequest<'_>,
) -> Result<Conn> {
if !self.inner.auth_switched {
self.inner.auth_switched = true;
self.inner.nonce = auth_switch_request.plugin_data().into();
self.inner.auth_plugin = auth_switch_request.auth_plugin().clone().into_owned();
let plugin_data = self
.inner
.auth_plugin
.gen_data(self.inner.opts.get_pass(), &*self.inner.nonce)
.unwrap_or_else(Vec::new);
self.write_packet(plugin_data).await?.continue_auth().await
} else {
unreachable!("auth_switched flag should be checked by caller")
}
}
fn continue_auth(self) -> Pin<Box<dyn Future<Output = Result<Conn>> + Send>> {
// NOTE: we need to box this since it may recurse
// see https://github.com/rust-lang/rust/issues/46415#issuecomment-528099782
Box::pin(async move {
match self.inner.auth_plugin {
AuthPlugin::MysqlNativePassword => self.continue_mysql_native_password_auth().await,
AuthPlugin::CachingSha2Password => self.continue_caching_sha2_password_auth().await,
AuthPlugin::Other(ref name) => Err(DriverError::UnknownAuthPlugin {
name: String::from_utf8_lossy(name.as_ref()).to_string(),
})?,
}
})
}
fn switch_to_compression(mut self) -> Result<Conn> {
if self
.get_capabilities()
.contains(CapabilityFlags::CLIENT_COMPRESS)
{
if let Some(compression) = self.inner.opts.get_compression() {
if let Some(stream) = self.inner.stream.as_mut() {
stream.compress(compression);
}
}
}
Ok(self)
}
async fn continue_caching_sha2_password_auth(self) -> Result<Conn> {
let (conn, packet) = self.read_packet().await?;
match packet.get(0) {
Some(0x00) => {
// ok packet for empty password
Ok(conn)
}
Some(0x01) => match packet.get(1) {
Some(0x03) => {
// auth ok
conn.drop_packet().await
}
Some(0x04) => {
let mut pass = conn
.inner
.opts
.get_pass()
.map(Vec::from)
.unwrap_or_default();
pass.push(0);
let conn = if conn.is_secure() {
conn.write_packet(&*pass).await?
} else {
let conn = conn.write_packet(&[0x02][..]).await?;
let (conn, packet) = conn.read_packet().await?;
let key = &packet[1..];
for (i, byte) in pass.iter_mut().enumerate() {
*byte ^= conn.inner.nonce[i % conn.inner.nonce.len()];
}
let encrypted_pass = crypto::encrypt(&*pass, key);
conn.write_packet(&*encrypted_pass).await?
};
conn.drop_packet().await
}
_ => Err(DriverError::UnexpectedPacket {
payload: packet.into(),
}
.into()),
},
Some(0xfe) if !conn.inner.auth_switched => {
let auth_switch_request = parse_auth_switch_request(&*packet)?.into_owned();
conn.perform_auth_switch(auth_switch_request).await
}
_ => Err(DriverError::UnexpectedPacket {
payload: packet.into(),
}
.into()),
}
}
async fn continue_mysql_native_password_auth(self) -> Result<Conn> {
let (this, packet) = self.read_packet().await?;
match packet.get(0) {
Some(0x00) => Ok(this),
Some(0xfe) if !this.inner.auth_switched => {
let auth_switch_request = parse_auth_switch_request(packet.as_ref())?.into_owned();
this.perform_auth_switch(auth_switch_request).await
}
_ => Err(DriverError::UnexpectedPacket { payload: packet }.into()),
}
}
async fn drop_packet(self) -> Result<Conn> {
Ok(self.read_packet().await?.0)
}
async fn run_init_commands(self) -> Result<Conn> {
let mut init: Vec<_> = self.inner.opts.get_init().iter().cloned().collect();
let mut conn = self;
while let Some(query) = init.pop() {
conn = conn.drop_query(query).await?;
}
Ok(conn)
}
/// Returns future that resolves to [`Conn`].
pub async fn new<T: Into<Opts>>(opts: T) -> Result<Conn> {
let opts = opts.into();
let mut conn = Conn::empty(opts.clone());
let stream = if let Some(path) = opts.get_socket() {
Stream::connect_socket(path.to_owned()).await?
} else {
Stream::connect_tcp((opts.get_ip_or_hostname(), opts.get_tcp_port())).await?
};
conn.inner.stream = Some(stream);
conn.setup_stream()?
.handle_handshake()
.await?
.switch_to_ssl_if_needed()
.await?
.do_handshake_response()
.await?
.continue_auth()
.await?
.switch_to_compression()?
.read_socket()
.await?
.reconnect_via_socket_if_needed()
.await?
.read_max_allowed_packet()
.await?
.read_wait_timeout()
.await?
.run_init_commands()
.await
}
/// Returns future that resolves to [`Conn`].
pub async fn from_url<T: AsRef<str>>(url: T) -> Result<Conn> {
Conn::new(Opts::from_str(url.as_ref())?).await
}
/// Will try to connect via socket using socket address in `self.inner.socket`.
///
/// Returns new connection on success or self on error.
///
/// Won't try to reconnect if socket connection is already enforced in [`Opts`].
fn reconnect_via_socket_if_needed(self) -> Pin<Box<dyn Future<Output = Result<Conn>> + Send>> {
// NOTE: we need to box this since it may recurse
// see https://github.com/rust-lang/rust/issues/46415#issuecomment-528099782
Box::pin(async move {
if let Some(socket) = self.inner.socket.as_ref() {
let opts = self.inner.opts.clone();
if opts.get_socket().is_none() {
let mut builder = OptsBuilder::from_opts(opts);
builder.socket(Some(&**socket));
match Conn::new(builder).await {
Ok(conn) => return Ok(conn),
Err(_) => return Ok(self),
}
}
}
Ok(self)
})
}
/// Returns future that resolves to [`Conn`] with socket address stored in it.
///
/// Do nothing if socket address is already in [`Opts`] or if `prefer_socket` is `false`.
async fn read_socket(self) -> Result<Self> {
if self.inner.opts.get_prefer_socket() && self.inner.socket.is_none() {
let (mut this, row_opt) = self.first("SELECT @@socket").await?;
this.inner.socket = row_opt.unwrap_or((None,)).0;
Ok(this)
} else {
Ok(self)
}
}
/// Returns future that resolves to [`Conn`] with `max_allowed_packet` stored in it.
async fn read_max_allowed_packet(self) -> Result<Self> {
let (mut this, row_opt): (Self, _) = self.first("SELECT @@max_allowed_packet").await?;
if let Some(stream) = this.inner.stream.as_mut() {
stream.set_max_allowed_packet(row_opt.unwrap_or((DEFAULT_MAX_ALLOWED_PACKET,)).0);
}
Ok(this)
}
/// Returns future that resolves to [`Conn`] with `wait_timeout` stored in it.
async fn read_wait_timeout(self) -> Result<Self> {
let (mut this, row_opt) = self.first("SELECT @@wait_timeout").await?;
let wait_timeout_secs = row_opt.unwrap_or((28800,)).0;
this.inner.wait_timeout = Duration::from_secs(wait_timeout_secs);
Ok(this)
}
/// Returns true if time since last io exceeds wait_timeout (or conn_ttl if specified in opts).
fn expired(&self) -> bool {
let ttl = self
.inner
.opts
.get_conn_ttl()
.unwrap_or(self.inner.wait_timeout);
self.idling() > ttl
}
/// Returns duration since last io.
fn idling(&self) -> Duration {
self.inner.last_io.elapsed()
}
/// Returns future that resolves to a [`Conn`] with `COM_RESET_CONNECTION` executed on it.
pub async fn reset(self) -> Result<Conn> {
let pool = self.inner.pool.clone();
let mut conn = if self.inner.version > (5, 7, 2) {
self.write_command_data(consts::Command::COM_RESET_CONNECTION, &[])
.await?
.read_packet()
.await?
.0
} else {
Conn::new(self.inner.opts.clone()).await?
};
conn.inner.stmt_cache.clear();
conn.inner.pool = pool;
Ok(conn)
}
async fn rollback_transaction(mut self) -> Result<Self> {
assert!(self.inner.in_transaction);
self.inner.in_transaction = false;
self.drop_query("ROLLBACK").await
}
async fn drop_result(mut self) -> Result<Conn> {
match self.inner.has_result.take() {
Some((columns, None)) => {
query_result::assemble::<_, TextProtocol>(self, Some(columns), None)
.drop_result()
.await
}
Some((columns, cached)) => {
query_result::assemble::<_, BinaryProtocol>(self, Some(columns), cached)
.drop_result()
.await
}
None => Ok(self),
}
}
fn cleanup(self) -> Pin<Box<dyn Future<Output = Result<Conn>> + Send>> {
// NOTE: we need to box this since it may recurse
// see https://github.com/rust-lang/rust/issues/46415#issuecomment-528099782
Box::pin(async move {
if self.inner.has_result.is_some() {
self.drop_result().await?.cleanup().await
} else if self.inner.in_transaction {
self.rollback_transaction().await?.cleanup().await
} else {
Ok(self)
}
})
}
}
impl ConnectionLike for Conn {
fn take_stream(mut self) -> (Streamless<Self>, Stream) {
let stream = self.inner.stream.take().expect("Logic error: stream taken");
(Streamless::new(self), stream)
}
fn return_stream(&mut self, stream: Stream) {
self.inner.stream = Some(stream);
}
fn stmt_cache_ref(&self) -> &StmtCache {
&self.inner.stmt_cache
}
fn stmt_cache_mut(&mut self) -> &mut StmtCache {
&mut self.inner.stmt_cache
}
fn get_affected_rows(&self) -> u64 {
self.inner.affected_rows
}
fn get_capabilities(&self) -> consts::CapabilityFlags {
self.inner.capabilities
}
fn get_in_transaction(&self) -> bool {
self.inner.in_transaction
}
fn get_last_insert_id(&self) -> Option<u64> {
match self.inner.last_insert_id {
0 => None,
x => Some(x),
}
}
fn get_local_infile_handler(&self) -> Option<Arc<dyn LocalInfileHandler>> {
self.inner.opts.get_local_infile_handler()
}
fn get_max_allowed_packet(&self) -> usize {
self.inner.max_allowed_packet
}
fn get_opts(&self) -> &Opts {
&self.inner.opts
}
fn get_pending_result(&self) -> Option<&(Arc<Vec<Column>>, Option<StmtCacheResult>)> {
self.inner.has_result.as_ref()
}
fn get_server_version(&self) -> (u16, u16, u16) {
self.inner.version
}
fn get_status(&self) -> consts::StatusFlags {
self.inner.status
}
fn set_affected_rows(&mut self, affected_rows: u64) {
self.inner.affected_rows = affected_rows;
}
fn set_in_transaction(&mut self, in_transaction: bool) {
self.inner.in_transaction = in_transaction;
}
fn set_last_insert_id(&mut self, last_insert_id: u64) {
self.inner.last_insert_id = last_insert_id;
}
fn set_pending_result(&mut self, meta: Option<(Arc<Vec<Column>>, Option<StmtCacheResult>)>) {
self.inner.has_result = meta;
}
fn set_status(&mut self, status: consts::StatusFlags) {
self.inner.status = status;
}
fn set_warnings(&mut self, warnings: u16) {
self.inner.warnings = warnings;
}
fn reset_seq_id(&mut self) {
if let Some(stream) = self.inner.stream.as_mut() {
stream.reset_seq_id();
}
}
fn sync_seq_id(&mut self) {
if let Some(stream) = self.inner.stream.as_mut() {
stream.sync_seq_id();
}
}
fn touch(&mut self) {
self.inner.last_io = Instant::now();
}
fn on_disconnect(&mut self) {
self.inner.disconnected = true;
}
}
#[cfg(test)]
mod test {
use crate::{
from_row, params, prelude::*, test_misc::get_opts, Conn, OptsBuilder, TransactionOptions,
WhiteListFsLocalInfileHandler,
};
#[test]
fn opts_should_satisfy_send_and_sync() {
struct A<T: Sync + Send>(T);
A(get_opts());
}
#[tokio::test]
async fn should_connect_without_database() -> super::Result<()> {
let mut opts = get_opts();
// no database name
opts.db_name(None::<String>);
let conn: Conn = Conn::new(opts.clone()).await?.ping().await?;
conn.disconnect().await?;
// empty database name
opts.db_name(Some(""));
let conn: Conn = Conn::new(opts).await?.ping().await?;
conn.disconnect().await?;
Ok(())
}
#[tokio::test]
async fn should_connect() -> super::Result<()> {
let conn: Conn = Conn::new(get_opts()).await?.ping().await?;
let (mut conn, plugins): (Conn, _) = conn
.query("SHOW PLUGINS")
.await?
.map_and_drop(|mut row| row.take::<String, _>("Name").unwrap())
.await?;
// Should connect with any combination of supported plugin and empty-nonempty password.
let variants = vec![
("caching_sha2_password", 2, "non-empty"),
("caching_sha2_password", 2, ""),
("mysql_native_password", 0, "non-empty"),
("mysql_native_password", 0, ""),
]
.into_iter()
.filter(|variant| plugins.iter().any(|p| p == variant.0));
for (plug, val, pass) in variants {
let query = format!("CREATE USER 'test_user'@'%' IDENTIFIED WITH {}", plug);
conn = conn.drop_query(query).await.unwrap();
conn = if (8, 0, 11) <= conn.inner.version && conn.inner.version <= (9, 0, 0) {
conn.drop_query(format!("SET PASSWORD FOR 'test_user'@'%' = '{}'", pass))
.await
.unwrap()
} else {
conn = conn
.drop_query(format!("SET old_passwords = {}", val))
.await
.unwrap();
conn.drop_query(format!(
"SET PASSWORD FOR 'test_user'@'%' = PASSWORD('{}')",
pass
))
.await
.unwrap()
};
let mut opts = get_opts();
opts.user(Some("test_user"))
.pass(Some(pass))
.db_name(None::<String>);
let result = Conn::new(opts).await;
conn = conn.drop_query("DROP USER 'test_user'@'%'").await.unwrap();
result?.disconnect().await?;
}
if crate::test_misc::test_compression() {
assert!(format!("{:?}", conn).contains("Compression"));
}
if crate::test_misc::test_ssl() {
assert!(format!("{:?}", conn).contains("Tls"));
}
conn.disconnect().await?;
Ok(())
}
#[test]
fn should_not_panic_if_dropped_without_tokio_runtime() {
let fut = Conn::new(get_opts());
let mut runtime = tokio::runtime::Runtime::new().unwrap();
runtime.block_on(async {
fut.await.unwrap();
});
// connection will drop here
}
#[tokio::test]
async fn should_execute_init_queries_on_new_connection() -> super::Result<()> {
let mut opts_builder = OptsBuilder::from_opts(get_opts());
opts_builder.init(vec!["SET @a = 42", "SET @b = 'foo'"]);
let (conn, result) = Conn::new(opts_builder)
.await?
.query("SELECT @a, @b")
.await?
.collect_and_drop::<(u8, String)>()
.await?;
conn.disconnect().await?;
assert_eq!(result, vec![(42, "foo".into())]);
Ok(())
}
#[tokio::test]
async fn should_reset_the_connection() -> super::Result<()> {
let conn = Conn::new(get_opts()).await?;
let conn = conn.drop_exec("SELECT ?", (1,)).await?;
let conn = conn.reset().await?;
let conn = conn.drop_exec("SELECT ?", (1,)).await?;
conn.disconnect().await?;
Ok(())
}
#[tokio::test]
async fn should_not_cache_statements_if_stmt_cache_size_is_zero() -> super::Result<()> {
let mut opts = OptsBuilder::from_opts(get_opts());
opts.stmt_cache_size(0);
let conn = Conn::new(opts).await?;
let conn = conn.drop_exec("DO ?", (1,)).await?;
let stmt = conn.prepare("DO 2").await?;
let (stmt, _) = stmt.first::<_, (crate::Value,)>(()).await?;
let (stmt, _) = stmt.first::<_, (crate::Value,)>(()).await?;
let conn = stmt.close().await?;
let conn = conn.prep_exec("DO 3", ()).await?.drop_result().await?;
let conn = conn.batch_exec("DO 4", vec![(), ()]).await?;
let (conn, _) = conn.first_exec::<_, _, (u8,)>("DO 5", ()).await?;
let (conn, row) = conn
.first("SHOW SESSION STATUS LIKE 'Com_stmt_close';")
.await?;
assert_eq!(from_row::<(String, usize)>(row.unwrap()).1, 5);
conn.disconnect().await?;
Ok(())
}
#[tokio::test]
async fn should_hold_stmt_cache_size_bound() -> super::Result<()> {
use crate::connection_like::ConnectionLike;
let mut opts = OptsBuilder::from_opts(get_opts());
opts.stmt_cache_size(3);
let conn = Conn::new(opts)
.await?
.drop_exec("DO 1", ())
.await?
.drop_exec("DO 2", ())
.await?
.drop_exec("DO 3", ())
.await?
.drop_exec("DO 1", ())
.await?
.drop_exec("DO 4", ())
.await?
.drop_exec("DO 3", ())
.await?
.drop_exec("DO 5", ())
.await?
.drop_exec("DO 6", ())
.await?;
let (conn, row_opt) = conn
.first("SHOW SESSION STATUS LIKE 'Com_stmt_close';")
.await?;
let (_, count): (String, usize) = row_opt.unwrap();
assert_eq!(count, 3);
let order = conn
.stmt_cache_ref()
.iter()
.map(Clone::clone)
.collect::<Vec<String>>();
assert_eq!(order, &["DO 3", "DO 5", "DO 6"]);
conn.disconnect().await?;
Ok(())
}
#[tokio::test]
async fn should_perform_queries() -> super::Result<()> {
let long_string = ::std::iter::repeat('A')
.take(18 * 1024 * 1024)
.collect::<String>();
let conn = Conn::new(get_opts()).await?;
let result = conn
.query(format!(r"SELECT '{}', 231", long_string))
.await?;
let (conn, result) = result
.reduce_and_drop(vec![], move |mut acc, row| {
acc.push(from_row(row));
acc
})
.await?;
conn.disconnect().await?;
assert_eq!((long_string, 231), result[0]);
Ok(())
}
#[tokio::test]
async fn should_drop_query() -> super::Result<()> {
let conn = Conn::new(get_opts()).await?;
let (conn, result) = conn
.drop_query("CREATE TEMPORARY TABLE tmp (id int DEFAULT 10, name text)")
.await?
.drop_query("INSERT INTO tmp VALUES (1, 'foo')")
.await?
.first::<_, (u8,)>("SELECT COUNT(*) FROM tmp")
.await?;
conn.disconnect().await?;
assert_eq!(result, Some((1,)));
Ok(())
}
#[tokio::test]
async fn should_try_collect() -> super::Result<()> {
let conn = Conn::new(get_opts()).await?;
let result = conn
.query(
r"SELECT 'hello', 123
UNION ALL
SELECT 'world', 'bar'
UNION ALL
SELECT 'hello', 123
",
)
.await?;
let (result, mut rows) = result.try_collect::<(String, u8)>().await?;
assert!(rows.pop().unwrap().is_ok());
assert!(rows.pop().unwrap().is_err());
assert!(rows.pop().unwrap().is_ok());
let conn = result.drop_result().await?;
conn.disconnect().await?;
Ok(())
}
#[tokio::test]
async fn should_try_collect_and_drop() -> super::Result<()> {
let conn = Conn::new(get_opts()).await?;
let (conn, mut rows) = conn
.query(
r"SELECT 'hello', 123
UNION ALL
SELECT 'world', 'bar'
UNION ALL
SELECT 'hello', 123;
SELECT 'foo', 255;
",
)
.await?
.try_collect_and_drop::<(String, u8)>()
.await?;
assert!(rows.pop().unwrap().is_ok());
assert!(rows.pop().unwrap().is_err());
assert!(rows.pop().unwrap().is_ok());
conn.disconnect().await?;
Ok(())
}
#[tokio::test]
async fn should_handle_mutliresult_set() -> super::Result<()> {
let conn = Conn::new(get_opts()).await?;
let result = conn
.query(
r"SELECT 'hello', 123
UNION ALL
SELECT 'world', 231;
SELECT 'foo', 255;
",
)
.await?;
let (result, rows_1) = result.collect::<(String, u8)>().await?;
let (conn, rows_2) = result.collect_and_drop().await?;
conn.disconnect().await?;
assert_eq!((String::from("hello"), 123), rows_1[0]);
assert_eq!((String::from("world"), 231), rows_1[1]);
assert_eq!((String::from("foo"), 255), rows_2[0]);
Ok(())
}
#[tokio::test]
async fn should_map_resultset() -> super::Result<()> {
let conn = Conn::new(get_opts()).await?;
let result = conn
.query(
r"
SELECT 'hello', 123
UNION ALL
SELECT 'world', 231;
SELECT 'foo', 255;
",
)
.await?;
let (result, rows_1) = result.map(|row| from_row::<(String, u8)>(row)).await?;
let (conn, rows_2) = result.map_and_drop(from_row).await?;
conn.disconnect().await?;
assert_eq!((String::from("hello"), 123), rows_1[0]);
assert_eq!((String::from("world"), 231), rows_1[1]);
assert_eq!((String::from("foo"), 255), rows_2[0]);
Ok(())
}
#[tokio::test]
async fn should_reduce_resultset() -> super::Result<()> {
let conn = Conn::new(get_opts()).await?;
let result = conn
.query(
r"SELECT 5
UNION ALL
SELECT 6;
SELECT 7;",
)
.await?;
let (result, reduced) = result
.reduce(0, |mut acc, row| {
acc += from_row::<i32>(row);
acc
})
.await?;
let (conn, rows_2) = result.collect_and_drop::<i32>().await?;
conn.disconnect().await?;
assert_eq!(11, reduced);
assert_eq!(7, rows_2[0]);
Ok(())
}
#[tokio::test]
async fn should_handle_multi_result_sets_where_some_results_have_no_output() -> super::Result<()>
{
const QUERY: &str = r"SELECT 1;
UPDATE time_zone SET Time_zone_id = 1 WHERE Time_zone_id = 1;
SELECT 2;