forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to IpAddr with MySQL/MariaDB. (launchbadge#3011)
* Added support for IpAddr with MySQL/MariaDB * Added IpAddr to mysql/types documentation
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters