diff --git a/python/psqlpy/_internal/extra_types.pyi b/python/psqlpy/_internal/extra_types.pyi index 525b314d..7b9c78a5 100644 --- a/python/psqlpy/_internal/extra_types.pyi +++ b/python/psqlpy/_internal/extra_types.pyi @@ -32,18 +32,6 @@ class BigInt: - `inner_value`: int object. """ -class PyUUID: - """Represent UUID in PostgreSQL and Uuid in Rust.""" - - def __init__(self: Self, inner_value: str) -> None: - """Create new instance of class. - - You need to pass uuid as a str. - - ### Parameters: - - `inner_value`: str object. - """ - class PyVarChar: """Represent VarChar in PostgreSQL and String in Rust.""" diff --git a/python/psqlpy/extra_types.py b/python/psqlpy/extra_types.py index 3fc13125..4a60a6d9 100644 --- a/python/psqlpy/extra_types.py +++ b/python/psqlpy/extra_types.py @@ -7,7 +7,6 @@ PyMacAddr6, PyMacAddr8, PyText, - PyUUID, PyVarChar, SmallInt, ) @@ -16,7 +15,6 @@ "SmallInt", "Integer", "BigInt", - "PyUUID", "PyJSONB", "PyJSON", "PyMacAddr6", diff --git a/python/tests/test_value_converter.py b/python/tests/test_value_converter.py index c11070a9..d10ec842 100644 --- a/python/tests/test_value_converter.py +++ b/python/tests/test_value_converter.py @@ -14,8 +14,9 @@ Integer, PyJSON, PyJSONB, + PyMacAddr6, + PyMacAddr8, PyText, - PyUUID, SmallInt, ) @@ -77,7 +78,7 @@ async def test_as_class( ("TIME", now_datetime.time(), now_datetime.time()), ("TIMESTAMP", now_datetime, now_datetime), ("TIMESTAMPTZ", now_datetime_with_tz, now_datetime_with_tz), - ("UUID", PyUUID(str(uuid_)), str(uuid_)), + ("UUID", uuid_, str(uuid_)), ("INET", IPv4Address("192.0.0.1"), IPv4Address("192.0.0.1")), ( "JSONB", @@ -111,6 +112,16 @@ async def test_as_class( PyJSON([{"array": "json"}, {"one more": "test"}]), [{"array": "json"}, {"one more": "test"}], ), + ( + "MACADDR", + PyMacAddr6("08:00:2b:01:02:03"), + "08:00:2B:01:02:03", + ), + ( + "MACADDR8", + PyMacAddr8("08:00:2b:01:02:03:04:05"), + "08:00:2B:01:02:03:04:05", + ), ( "VARCHAR ARRAY", ["Some String", "Some String"], @@ -152,7 +163,7 @@ async def test_as_class( ), ( "UUID ARRAY", - [PyUUID(str(uuid_)), PyUUID(str(uuid_))], + [uuid_, uuid_], [str(uuid_), str(uuid_)], ), ( @@ -328,7 +339,7 @@ async def test_deserialization_composite_into_python( now_datetime.time(), now_datetime, now_datetime_with_tz, - PyUUID(str(uuid_)), + uuid_, IPv4Address("192.0.0.1"), { "test": ["something", 123, "here"], @@ -351,7 +362,7 @@ async def test_deserialization_composite_into_python( [now_datetime.time(), now_datetime.time()], [now_datetime, now_datetime], [now_datetime_with_tz, now_datetime_with_tz], - [PyUUID(str(uuid_)), PyUUID(str(uuid_))], + [uuid_, uuid_], [IPv4Address("192.0.0.1"), IPv4Address("192.0.0.1")], [ { diff --git a/src/extra_types.rs b/src/extra_types.rs index d86b9a62..9df47714 100644 --- a/src/extra_types.rs +++ b/src/extra_types.rs @@ -7,7 +7,6 @@ use pyo3::{ Bound, Py, PyAny, PyResult, Python, }; use serde_json::Value; -use uuid::Uuid; use crate::{exceptions::rust_errors::RustPSQLDriverPyResult, value_converter::build_serde_value}; @@ -46,19 +45,6 @@ build_python_type!(SmallInt, i16); build_python_type!(Integer, i32); build_python_type!(BigInt, i64); -#[pyclass] -#[derive(Clone)] -pub struct PyUUID { - inner: Uuid, -} - -impl PyUUID { - #[must_use] - pub fn inner(&self) -> Uuid { - self.inner - } -} - #[pyclass] #[derive(Clone)] pub struct PyText { @@ -107,22 +93,6 @@ impl PyVarChar { } } -#[pymethods] -impl PyUUID { - /// Create new uuid from Python str. - /// - /// # Errors - /// May return Err Result if cannot convert python string - /// into rust Uuid. - #[new] - #[allow(clippy::missing_errors_doc)] - pub fn new_uuid(uuid_value: &str) -> RustPSQLDriverPyResult { - Ok(Self { - inner: Uuid::from_str(uuid_value)?, - }) - } -} - macro_rules! build_json_py_type { ($st_name:ident, $rust_type:ty) => { #[pyclass] @@ -217,7 +187,6 @@ pub fn extra_types_module(_py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyRes pymod.add_class::()?; pymod.add_class::()?; pymod.add_class::()?; - pymod.add_class::()?; pymod.add_class::()?; pymod.add_class::()?; pymod.add_class::()?; diff --git a/src/value_converter.rs b/src/value_converter.rs index fa7f6ff2..09937296 100644 --- a/src/value_converter.rs +++ b/src/value_converter.rs @@ -10,7 +10,7 @@ use postgres_protocol::types; use pyo3::{ types::{ PyAnyMethods, PyBool, PyBytes, PyDate, PyDateTime, PyDict, PyDictMethods, PyFloat, PyInt, - PyList, PyListMethods, PyString, PyTime, PyTuple, + PyList, PyListMethods, PyString, PyTime, PyTuple, PyTypeMethods, }, Bound, Py, PyAny, Python, ToPyObject, }; @@ -23,8 +23,8 @@ use crate::{ additional_types::{RustMacAddr6, RustMacAddr8}, exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult}, extra_types::{ - BigInt, Integer, PyCustomType, PyJSON, PyJSONB, PyMacAddr6, PyMacAddr8, PyText, PyUUID, - PyVarChar, SmallInt, + BigInt, Integer, PyCustomType, PyJSON, PyJSONB, PyMacAddr6, PyMacAddr8, PyText, PyVarChar, + SmallInt, }, }; @@ -303,26 +303,6 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult< return Ok(PythonDTO::PyBytes(parameter.extract::>()?)); } - if parameter.is_instance_of::() { - let timestamp_tz = parameter.extract::>(); - if let Ok(pydatetime_tz) = timestamp_tz { - return Ok(PythonDTO::PyDateTimeTz(pydatetime_tz)); - } - - let timestamp_no_tz = parameter.extract::(); - if let Ok(pydatetime_no_tz) = timestamp_no_tz { - return Ok(PythonDTO::PyDateTime(pydatetime_no_tz)); - } - - return Err(RustPSQLDriverError::PyToRustValueConversionError( - "Can not convert you datetime to rust type".into(), - )); - } - - if parameter.is_instance_of::() { - return Ok(PythonDTO::PyUUID(parameter.extract::()?.inner())); - } - if parameter.is_instance_of::() { return Ok(PythonDTO::PyText(parameter.extract::()?.inner())); } @@ -364,6 +344,22 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult< return Ok(PythonDTO::PyIntI32(parameter.extract::()?)); } + if parameter.is_instance_of::() { + let timestamp_tz = parameter.extract::>(); + if let Ok(pydatetime_tz) = timestamp_tz { + return Ok(PythonDTO::PyDateTimeTz(pydatetime_tz)); + } + + let timestamp_no_tz = parameter.extract::(); + if let Ok(pydatetime_no_tz) = timestamp_no_tz { + return Ok(PythonDTO::PyDateTime(pydatetime_no_tz)); + } + + return Err(RustPSQLDriverError::PyToRustValueConversionError( + "Can not convert you datetime to rust type".into(), + )); + } + if parameter.is_instance_of::() { return Ok(PythonDTO::PyDate(parameter.extract::()?)); } @@ -429,6 +425,12 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult< )); } + if parameter.get_type().name()? == "UUID" { + return Ok(PythonDTO::PyUUID(Uuid::parse_str( + parameter.str()?.extract::<&str>()?, + )?)); + } + if let Ok(id_address) = parameter.extract::() { return Ok(PythonDTO::PyIpAddress(id_address)); }