Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to IpAddr with MySQL/MariaDB. #3011

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions sqlx-mysql/src/types/inet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::io::MySqlBufMutExt;
use crate::types::Type;
use crate::{MySql, MySqlTypeInfo, MySqlValueRef};

impl Type<MySql> for Ipv4Addr {
fn type_info() -> MySqlTypeInfo {
<&str as Type<MySql>>::type_info()
}

fn compatible(ty: &MySqlTypeInfo) -> bool {
<&str as Type<MySql>>::compatible(ty)
}
}

impl Encode<'_, MySql> for Ipv4Addr {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
buf.put_str_lenenc(&self.to_string());

IsNull::No
}
}

impl Decode<'_, MySql> for Ipv4Addr {
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
// delegate to the &str type to decode from MySQL
let text = <&str as Decode<MySql>>::decode(value)?;

// parse a Ipv4Addr from the text
text.parse().map_err(Into::into)
}
}

impl Type<MySql> for Ipv6Addr {
fn type_info() -> MySqlTypeInfo {
<&str as Type<MySql>>::type_info()
}

fn compatible(ty: &MySqlTypeInfo) -> bool {
<&str as Type<MySql>>::compatible(ty)
}
}

impl Encode<'_, MySql> for Ipv6Addr {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
buf.put_str_lenenc(&self.to_string());

IsNull::No
}
}

impl Decode<'_, MySql> for Ipv6Addr {
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
// delegate to the &str type to decode from MySQL
let text = <&str as Decode<MySql>>::decode(value)?;

// parse a Ipv6Addr from the text
text.parse().map_err(Into::into)
}
}

impl Type<MySql> for IpAddr {
fn type_info() -> MySqlTypeInfo {
<&str as Type<MySql>>::type_info()
}

fn compatible(ty: &MySqlTypeInfo) -> bool {
<&str as Type<MySql>>::compatible(ty)
}
}

impl Encode<'_, MySql> for IpAddr {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
buf.put_str_lenenc(&self.to_string());

IsNull::No
}
}

impl Decode<'_, MySql> for IpAddr {
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
// delegate to the &str type to decode from MySQL
let text = <&str as Decode<MySql>>::decode(value)?;

// parse a IpAddr from the text
text.parse().map_err(Into::into)
}
}
2 changes: 2 additions & 0 deletions sqlx-mysql/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! | `f64` | DOUBLE |
//! | `&str`, [`String`] | VARCHAR, CHAR, TEXT |
//! | `&[u8]`, `Vec<u8>` | VARBINARY, BINARY, BLOB |
//! | `IpAddr`, `Ipv4Addr`, `Ipv6Addr` | VARCHAR, TEXT |
//!
//! ##### Note: `BOOLEAN`/`BOOL` Type
//! MySQL and MariaDB treat `BOOLEAN` as an alias of the `TINYINT` type:
Expand Down Expand Up @@ -102,6 +103,7 @@ pub(crate) use sqlx_core::types::*;
mod bool;
mod bytes;
mod float;
mod inet;
mod int;
mod str;
mod text;
Expand Down
Loading