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 for MacAddr8 type #27

Merged
merged 1 commit into from
Mar 28, 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
10 changes: 10 additions & 0 deletions python/psqlpy/_internal/extra_types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ class PyMacAddr6:
### Parameters:
- `value`: value for MACADDR field.
"""

class PyMacAddr8:
"""Represents MACADDR8 in PostgreSQL."""

def __init__(self, value: str) -> None:
"""Construct new MacAddr8.

### Parameters:
- `value`: value for MACADDR8 field.
"""
11 changes: 10 additions & 1 deletion python/psqlpy/extra_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
from ._internal.extra_types import BigInt, Integer, PyJSON, PyMacAddr6, PyUUID, SmallInt
from ._internal.extra_types import (
BigInt,
Integer,
PyJSON,
PyMacAddr6,
PyMacAddr8,
PyUUID,
SmallInt,
)

__all__ = [
"SmallInt",
Expand All @@ -7,4 +15,5 @@
"PyUUID",
"PyJSON",
"PyMacAddr6",
"PyMacAddr8",
]
54 changes: 40 additions & 14 deletions src/additional_types.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
use macaddr::MacAddr6;
use macaddr::{MacAddr6, MacAddr8};
use tokio_postgres::types::{FromSql, Type};

#[derive(Debug)]
pub struct RustMacAddr6 {
inner: MacAddr6,
}
macro_rules! build_additional_rust_type {
($st_name:ident, $rust_type:ty) => {
#[derive(Debug)]
pub struct $st_name {
inner: $rust_type,
}

impl RustMacAddr6 {
#[must_use]
pub fn new(inner: MacAddr6) -> Self {
RustMacAddr6 { inner }
}
impl $st_name {
#[must_use]
pub fn new(inner: $rust_type) -> Self {
$st_name { inner }
}

#[must_use]
pub fn inner(&self) -> &MacAddr6 {
&self.inner
}
#[must_use]
pub fn inner(&self) -> &$rust_type {
&self.inner
}
}
};
}

build_additional_rust_type!(RustMacAddr6, MacAddr6);
build_additional_rust_type!(RustMacAddr8, MacAddr8);

impl<'a> FromSql<'a> for RustMacAddr6 {
fn from_sql(
_ty: &Type,
Expand All @@ -34,3 +41,22 @@ impl<'a> FromSql<'a> for RustMacAddr6 {
true
}
}

impl<'a> FromSql<'a> for RustMacAddr8 {
fn from_sql(
_ty: &Type,
raw: &'a [u8],
) -> Result<RustMacAddr8, Box<dyn std::error::Error + Sync + Send>> {
if raw.len() == 8 {
let new_mac_address = MacAddr8::new(
raw[0], raw[1], raw[2], raw[3], raw[4], raw[5], raw[6], raw[7],
);
return Ok(RustMacAddr8::new(new_mac_address));
}
Err("Cannot convert PostgreSQL MACADDR8 into rust MacAddr8".into())
}

fn accepts(_ty: &Type) -> bool {
true
}
}
94 changes: 73 additions & 21 deletions src/extra_types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use macaddr::MacAddr6;
use macaddr::{MacAddr6, MacAddr8};
use pyo3::{pyclass, pymethods, types::PyModule, PyAny, PyResult, Python};
use serde_json::Value;
use uuid::Uuid;
Expand Down Expand Up @@ -95,30 +95,81 @@ impl PyJSON {
}
}

#[pyclass]
#[derive(Clone)]
pub struct PyMacAddr6 {
inner: MacAddr6,
}
macro_rules! build_macaddr_type {
($st_name:ident, $rust_type:ty) => {
#[pyclass]
#[derive(Clone)]
pub struct $st_name {
inner: $rust_type,
}

impl PyMacAddr6 {
#[must_use]
pub fn inner(self) -> MacAddr6 {
self.inner
}
}
impl $st_name {
#[must_use]
pub fn inner(self) -> $rust_type {
self.inner
}
}

#[pymethods]
impl PyMacAddr6 {
#[new]
#[allow(clippy::missing_errors_doc)]
pub fn new_macaddr6(value: &str) -> RustPSQLDriverPyResult<Self> {
Ok(Self {
inner: MacAddr6::from_str(value)?,
})
}
#[pymethods]
impl $st_name {
#[new]
#[allow(clippy::missing_errors_doc)]
pub fn new_class(value: &str) -> RustPSQLDriverPyResult<Self> {
Ok(Self {
inner: <$rust_type>::from_str(value)?,
})
}
}
};
}

build_macaddr_type!(PyMacAddr6, MacAddr6);
build_macaddr_type!(PyMacAddr8, MacAddr8);

// #[pyclass]
// #[derive(Clone)]
// pub struct PyMacAddr6 {
// inner: MacAddr6,
// }

// impl PyMacAddr6 {
// #[must_use]
// pub fn inner(self) -> MacAddr6 {
// self.inner
// }
// }

// #[pymethods]
// impl PyMacAddr6 {
// #[new]
// #[allow(clippy::missing_errors_doc)]
// pub fn new_macaddr6(value: &str) -> RustPSQLDriverPyResult<Self> {
// Ok(Self {
// inner: MacAddr6::from_str(value)?,
// })
// }
// }

// #[pyclass]
// #[derive(Clone)]
// pub struct PyMacAddr8 {
// inner: MacAddr8,
// }

// impl PyMacAddr8 {
// #[must_use]
// pub fn inner(self) -> MacAddr8 {
// self.inner
// }
// }

// #[pymethods]
// impl PyMacAddr8 {
// #[new]
// #[allow(clippy::missing_errors_doc)]
// pub fn new_macaddr8(value: &str) -> RustPSQLDriverPyResult<Self> {}
// }

#[allow(clippy::module_name_repetitions)]
#[allow(clippy::missing_errors_doc)]
pub fn extra_types_module(_py: Python<'_>, pymod: &PyModule) -> PyResult<()> {
Expand All @@ -128,5 +179,6 @@ pub fn extra_types_module(_py: Python<'_>, pymod: &PyModule) -> PyResult<()> {
pymod.add_class::<PyUUID>()?;
pymod.add_class::<PyJSON>()?;
pymod.add_class::<PyMacAddr6>()?;
pymod.add_class::<PyMacAddr8>()?;
Ok(())
}
33 changes: 28 additions & 5 deletions src/value_converter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::{self, DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime};
use macaddr::MacAddr6;
use macaddr::{MacAddr6, MacAddr8};
use serde_json::{json, Map, Value};
use std::{fmt::Debug, net::IpAddr};
use uuid::Uuid;
Expand All @@ -19,9 +19,9 @@ use tokio_postgres::{
};

use crate::{
additional_types::RustMacAddr6,
additional_types::{RustMacAddr6, RustMacAddr8},
exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult},
extra_types::{BigInt, Integer, PyJSON, PyMacAddr6, PyUUID, SmallInt},
extra_types::{BigInt, Integer, PyJSON, PyMacAddr6, PyMacAddr8, PyUUID, SmallInt},
};

pub type QueryParameter = (dyn ToSql + Sync);
Expand Down Expand Up @@ -54,6 +54,7 @@ pub enum PythonDTO {
PyTuple(Vec<PythonDTO>),
PyJson(Value),
PyMacAddr6(MacAddr6),
PyMacAddr8(MacAddr8),
}

impl PythonDTO {
Expand Down Expand Up @@ -188,6 +189,9 @@ impl ToSql for PythonDTO {
PythonDTO::PyMacAddr6(pymacaddr) => {
<&[u8] as ToSql>::to_sql(&pymacaddr.as_bytes(), ty, out)?;
}
PythonDTO::PyMacAddr8(pymacaddr) => {
<&[u8] as ToSql>::to_sql(&pymacaddr.as_bytes(), ty, out)?;
}
PythonDTO::PyList(py_iterable) | PythonDTO::PyTuple(py_iterable) => {
let mut items = Vec::new();
for inner in py_iterable {
Expand Down Expand Up @@ -245,6 +249,7 @@ pub fn convert_parameters(parameters: &PyAny) -> RustPSQLDriverPyResult<Vec<Pyth
///
/// May return Err Result if python type doesn't have support yet
/// or value of the type is incorrect.
#[allow(clippy::too_many_lines)]
pub fn py_to_rust(parameter: &PyAny) -> RustPSQLDriverPyResult<PythonDTO> {
if parameter.is_none() {
return Ok(PythonDTO::PyNone);
Expand Down Expand Up @@ -362,6 +367,12 @@ pub fn py_to_rust(parameter: &PyAny) -> RustPSQLDriverPyResult<PythonDTO> {
));
}

if parameter.is_instance_of::<PyMacAddr8>() {
return Ok(PythonDTO::PyMacAddr8(
parameter.extract::<PyMacAddr8>()?.inner(),
));
}

if let Ok(id_address) = parameter.extract::<IpAddr>() {
return Ok(PythonDTO::PyIpAddress(id_address));
}
Expand Down Expand Up @@ -494,8 +505,20 @@ pub fn postgres_to_py(
}
// Convert MACADDR into inner type for macaddr6, then into str
Type::MACADDR => {
let macaddr_ = row.try_get::<_, RustMacAddr6>(column_i)?;
Ok(macaddr_.inner().to_string().to_object(py))
let macaddr_ = row.try_get::<_, Option<RustMacAddr6>>(column_i)?;
if let Some(macaddr_) = macaddr_ {
Ok(macaddr_.inner().to_string().to_object(py))
} else {
Ok(py.None().to_object(py))
}
}
Type::MACADDR8 => {
let macaddr_ = row.try_get::<_, Option<RustMacAddr8>>(column_i)?;
if let Some(macaddr_) = macaddr_ {
Ok(macaddr_.inner().to_string().to_object(py))
} else {
Ok(py.None().to_object(py))
}
}
_ => Err(RustPSQLDriverError::RustToPyValueConversionError(
column.type_().to_string(),
Expand Down
Loading