Skip to content

Commit 0e998ce

Browse files
authored
Added support to IpAddr with MySQL/MariaDB. (#3011)
* Added support for IpAddr with MySQL/MariaDB * Added IpAddr to mysql/types documentation
1 parent af31d50 commit 0e998ce

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

sqlx-mysql/src/types/inet.rs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
2+
3+
use crate::decode::Decode;
4+
use crate::encode::{Encode, IsNull};
5+
use crate::error::BoxDynError;
6+
use crate::io::MySqlBufMutExt;
7+
use crate::types::Type;
8+
use crate::{MySql, MySqlTypeInfo, MySqlValueRef};
9+
10+
impl Type<MySql> for Ipv4Addr {
11+
fn type_info() -> MySqlTypeInfo {
12+
<&str as Type<MySql>>::type_info()
13+
}
14+
15+
fn compatible(ty: &MySqlTypeInfo) -> bool {
16+
<&str as Type<MySql>>::compatible(ty)
17+
}
18+
}
19+
20+
impl Encode<'_, MySql> for Ipv4Addr {
21+
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
22+
buf.put_str_lenenc(&self.to_string());
23+
24+
IsNull::No
25+
}
26+
}
27+
28+
impl Decode<'_, MySql> for Ipv4Addr {
29+
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
30+
// delegate to the &str type to decode from MySQL
31+
let text = <&str as Decode<MySql>>::decode(value)?;
32+
33+
// parse a Ipv4Addr from the text
34+
text.parse().map_err(Into::into)
35+
}
36+
}
37+
38+
impl Type<MySql> for Ipv6Addr {
39+
fn type_info() -> MySqlTypeInfo {
40+
<&str as Type<MySql>>::type_info()
41+
}
42+
43+
fn compatible(ty: &MySqlTypeInfo) -> bool {
44+
<&str as Type<MySql>>::compatible(ty)
45+
}
46+
}
47+
48+
impl Encode<'_, MySql> for Ipv6Addr {
49+
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
50+
buf.put_str_lenenc(&self.to_string());
51+
52+
IsNull::No
53+
}
54+
}
55+
56+
impl Decode<'_, MySql> for Ipv6Addr {
57+
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
58+
// delegate to the &str type to decode from MySQL
59+
let text = <&str as Decode<MySql>>::decode(value)?;
60+
61+
// parse a Ipv6Addr from the text
62+
text.parse().map_err(Into::into)
63+
}
64+
}
65+
66+
impl Type<MySql> for IpAddr {
67+
fn type_info() -> MySqlTypeInfo {
68+
<&str as Type<MySql>>::type_info()
69+
}
70+
71+
fn compatible(ty: &MySqlTypeInfo) -> bool {
72+
<&str as Type<MySql>>::compatible(ty)
73+
}
74+
}
75+
76+
impl Encode<'_, MySql> for IpAddr {
77+
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
78+
buf.put_str_lenenc(&self.to_string());
79+
80+
IsNull::No
81+
}
82+
}
83+
84+
impl Decode<'_, MySql> for IpAddr {
85+
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
86+
// delegate to the &str type to decode from MySQL
87+
let text = <&str as Decode<MySql>>::decode(value)?;
88+
89+
// parse a IpAddr from the text
90+
text.parse().map_err(Into::into)
91+
}
92+
}

sqlx-mysql/src/types/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//! | `f64` | DOUBLE |
1818
//! | `&str`, [`String`] | VARCHAR, CHAR, TEXT |
1919
//! | `&[u8]`, `Vec<u8>` | VARBINARY, BINARY, BLOB |
20+
//! | `IpAddr`, `Ipv4Addr`, `Ipv6Addr` | VARCHAR, TEXT |
2021
//!
2122
//! ##### Note: `BOOLEAN`/`BOOL` Type
2223
//! MySQL and MariaDB treat `BOOLEAN` as an alias of the `TINYINT` type:
@@ -102,6 +103,7 @@ pub(crate) use sqlx_core::types::*;
102103
mod bool;
103104
mod bytes;
104105
mod float;
106+
mod inet;
105107
mod int;
106108
mod str;
107109
mod text;

0 commit comments

Comments
 (0)