Skip to content
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
12 changes: 0 additions & 12 deletions python/psqlpy/_internal/extra_types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
2 changes: 0 additions & 2 deletions python/psqlpy/extra_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
PyMacAddr6,
PyMacAddr8,
PyText,
PyUUID,
PyVarChar,
SmallInt,
)
Expand All @@ -16,7 +15,6 @@
"SmallInt",
"Integer",
"BigInt",
"PyUUID",
"PyJSONB",
"PyJSON",
"PyMacAddr6",
Expand Down
21 changes: 16 additions & 5 deletions python/tests/test_value_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
Integer,
PyJSON,
PyJSONB,
PyMacAddr6,
PyMacAddr8,
PyText,
PyUUID,
SmallInt,
)

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -152,7 +163,7 @@ async def test_as_class(
),
(
"UUID ARRAY",
[PyUUID(str(uuid_)), PyUUID(str(uuid_))],
[uuid_, uuid_],
[str(uuid_), str(uuid_)],
),
(
Expand Down Expand Up @@ -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"],
Expand All @@ -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")],
[
{
Expand Down
31 changes: 0 additions & 31 deletions src/extra_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Self> {
Ok(Self {
inner: Uuid::from_str(uuid_value)?,
})
}
}

macro_rules! build_json_py_type {
($st_name:ident, $rust_type:ty) => {
#[pyclass]
Expand Down Expand Up @@ -217,7 +187,6 @@ pub fn extra_types_module(_py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyRes
pymod.add_class::<SmallInt>()?;
pymod.add_class::<Integer>()?;
pymod.add_class::<BigInt>()?;
pymod.add_class::<PyUUID>()?;
pymod.add_class::<PyText>()?;
pymod.add_class::<PyVarChar>()?;
pymod.add_class::<PyJSONB>()?;
Expand Down
48 changes: 25 additions & 23 deletions src/value_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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,
},
};

Expand Down Expand Up @@ -303,26 +303,6 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
return Ok(PythonDTO::PyBytes(parameter.extract::<Vec<u8>>()?));
}

if parameter.is_instance_of::<PyDateTime>() {
let timestamp_tz = parameter.extract::<DateTime<FixedOffset>>();
if let Ok(pydatetime_tz) = timestamp_tz {
return Ok(PythonDTO::PyDateTimeTz(pydatetime_tz));
}

let timestamp_no_tz = parameter.extract::<NaiveDateTime>();
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::<PyUUID>() {
return Ok(PythonDTO::PyUUID(parameter.extract::<PyUUID>()?.inner()));
}

if parameter.is_instance_of::<PyText>() {
return Ok(PythonDTO::PyText(parameter.extract::<PyText>()?.inner()));
}
Expand Down Expand Up @@ -364,6 +344,22 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
return Ok(PythonDTO::PyIntI32(parameter.extract::<i32>()?));
}

if parameter.is_instance_of::<PyDateTime>() {
let timestamp_tz = parameter.extract::<DateTime<FixedOffset>>();
if let Ok(pydatetime_tz) = timestamp_tz {
return Ok(PythonDTO::PyDateTimeTz(pydatetime_tz));
}

let timestamp_no_tz = parameter.extract::<NaiveDateTime>();
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::<PyDate>() {
return Ok(PythonDTO::PyDate(parameter.extract::<NaiveDate>()?));
}
Expand Down Expand Up @@ -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::<IpAddr>() {
return Ok(PythonDTO::PyIpAddress(id_address));
}
Expand Down