Skip to content

Commit 0d40d0d

Browse files
authored
fix: mariadb 11.x version detection (#4)
1 parent d8ffc29 commit 0d40d0d

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ tokio-util = { version = "0.7.2", features = ["codec", "io"] }
3737
tracing = { version = "0.1.37", default-features = false, features = ["attributes"], optional = true }
3838
twox-hash = "1"
3939
url = "2.1"
40+
regex = "1.10.3"
41+
lexical = "6.1.0"
4042

4143
[dependencies.tokio-rustls]
4244
version = "0.23.4"

src/conn/mod.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ use crate::{
5050

5151
use self::routines::Routine;
5252

53+
use regex::bytes::Regex;
54+
5355
pub mod binlog_stream;
5456
pub mod pool;
5557
pub mod routines;
5658
pub mod stmt_cache;
5759

60+
lazy_static::lazy_static! {
61+
static ref FIXED_MARIADB_VERSION_RE: Regex =
62+
Regex::new(r"^(?:5.5.5-)?(\d{1,2})\.(\d{1,2})\.(\d{1,3})-MariaDB").unwrap();
63+
}
64+
5865
/// Helper that asynchronously disconnects the givent connection on the default tokio executor.
5966
fn disconnect(mut conn: Conn) {
6067
let disconnected = conn.inner.disconnected;
@@ -469,14 +476,14 @@ impl Conn {
469476
};
470477

471478
self.inner.capabilities = handshake.capabilities() & self.inner.opts.get_capabilities();
472-
self.inner.version = handshake
473-
.maria_db_server_version_parsed()
474-
.map(|version| {
475-
self.inner.is_mariadb = true;
476-
version
477-
})
478-
.or_else(|| handshake.server_version_parsed())
479-
.unwrap_or((0, 0, 0));
479+
self.inner.version =
480+
Self::fixed_maria_db_server_version_parsed(handshake.server_version_ref())
481+
.map(|version| {
482+
self.inner.is_mariadb = true;
483+
version
484+
})
485+
.or_else(|| handshake.server_version_parsed())
486+
.unwrap_or((0, 0, 0));
480487
self.inner.id = handshake.connection_id();
481488
self.inner.status = handshake.status_flags();
482489
self.inner.auth_plugin = match handshake.auth_plugin() {
@@ -493,6 +500,20 @@ impl Conn {
493500
Ok(())
494501
}
495502

503+
/// Parsed mariadb server version.
504+
/// Fixed version of `Handshake::maria_db_server_version_parsed` (only present on Prisma's fork).
505+
/// See https://github.com/blackbeam/rust_mysql_common/issues/124 for more info.
506+
pub fn fixed_maria_db_server_version_parsed(version: &[u8]) -> Option<(u16, u16, u16)> {
507+
FIXED_MARIADB_VERSION_RE.captures(version).map(|captures| {
508+
// Should not panic because validated with regex
509+
(
510+
lexical::parse::<u16, _>(captures.get(1).unwrap().as_bytes()).unwrap(),
511+
lexical::parse::<u16, _>(captures.get(2).unwrap().as_bytes()).unwrap(),
512+
lexical::parse::<u16, _>(captures.get(3).unwrap().as_bytes()).unwrap(),
513+
)
514+
})
515+
}
516+
496517
async fn switch_to_ssl_if_needed(&mut self) -> Result<()> {
497518
if self
498519
.inner

0 commit comments

Comments
 (0)