Skip to content

Commit b378131

Browse files
committed
Removed unnecessary extra types
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent fb54a1f commit b378131

File tree

5 files changed

+42
-84
lines changed

5 files changed

+42
-84
lines changed

python/psqlpy/_internal/extra_types.pyi

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ class BigInt:
3232
- `inner_value`: int object.
3333
"""
3434

35-
class PyUUID:
36-
"""Represent UUID in PostgreSQL and Uuid in Rust."""
37-
38-
def __init__(self: Self, inner_value: str) -> None:
39-
"""Create new instance of class.
40-
41-
You need to pass uuid as a str.
42-
43-
### Parameters:
44-
- `inner_value`: str object.
45-
"""
46-
4735
class PyVarChar:
4836
"""Represent VarChar in PostgreSQL and String in Rust."""
4937

python/psqlpy/extra_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
PyMacAddr6,
88
PyMacAddr8,
99
PyText,
10-
PyUUID,
1110
PyVarChar,
1211
SmallInt,
1312
)
@@ -16,7 +15,6 @@
1615
"SmallInt",
1716
"Integer",
1817
"BigInt",
19-
"PyUUID",
2018
"PyJSONB",
2119
"PyJSON",
2220
"PyMacAddr6",

python/tests/test_value_converter.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
Integer,
1515
PyJSON,
1616
PyJSONB,
17+
PyMacAddr6,
18+
PyMacAddr8,
1719
PyText,
18-
PyUUID,
1920
SmallInt,
2021
)
2122

@@ -77,7 +78,7 @@ async def test_as_class(
7778
("TIME", now_datetime.time(), now_datetime.time()),
7879
("TIMESTAMP", now_datetime, now_datetime),
7980
("TIMESTAMPTZ", now_datetime_with_tz, now_datetime_with_tz),
80-
("UUID", PyUUID(str(uuid_)), str(uuid_)),
81+
("UUID", uuid_, str(uuid_)),
8182
("INET", IPv4Address("192.0.0.1"), IPv4Address("192.0.0.1")),
8283
(
8384
"JSONB",
@@ -111,6 +112,16 @@ async def test_as_class(
111112
PyJSON([{"array": "json"}, {"one more": "test"}]),
112113
[{"array": "json"}, {"one more": "test"}],
113114
),
115+
(
116+
"MACADDR",
117+
PyMacAddr6("08:00:2b:01:02:03"),
118+
"08:00:2B:01:02:03",
119+
),
120+
(
121+
"MACADDR8",
122+
PyMacAddr8("08:00:2b:01:02:03:04:05"),
123+
"08:00:2B:01:02:03:04:05",
124+
),
114125
(
115126
"VARCHAR ARRAY",
116127
["Some String", "Some String"],
@@ -152,7 +163,7 @@ async def test_as_class(
152163
),
153164
(
154165
"UUID ARRAY",
155-
[PyUUID(str(uuid_)), PyUUID(str(uuid_))],
166+
[uuid_, uuid_],
156167
[str(uuid_), str(uuid_)],
157168
),
158169
(
@@ -328,7 +339,7 @@ async def test_deserialization_composite_into_python(
328339
now_datetime.time(),
329340
now_datetime,
330341
now_datetime_with_tz,
331-
PyUUID(str(uuid_)),
342+
uuid_,
332343
IPv4Address("192.0.0.1"),
333344
{
334345
"test": ["something", 123, "here"],
@@ -351,7 +362,7 @@ async def test_deserialization_composite_into_python(
351362
[now_datetime.time(), now_datetime.time()],
352363
[now_datetime, now_datetime],
353364
[now_datetime_with_tz, now_datetime_with_tz],
354-
[PyUUID(str(uuid_)), PyUUID(str(uuid_))],
365+
[uuid_, uuid_],
355366
[IPv4Address("192.0.0.1"), IPv4Address("192.0.0.1")],
356367
[
357368
{

src/extra_types.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use pyo3::{
77
Bound, Py, PyAny, PyResult, Python,
88
};
99
use serde_json::Value;
10-
use uuid::Uuid;
1110

1211
use crate::{exceptions::rust_errors::RustPSQLDriverPyResult, value_converter::build_serde_value};
1312

@@ -46,19 +45,6 @@ build_python_type!(SmallInt, i16);
4645
build_python_type!(Integer, i32);
4746
build_python_type!(BigInt, i64);
4847

49-
#[pyclass]
50-
#[derive(Clone)]
51-
pub struct PyUUID {
52-
inner: Uuid,
53-
}
54-
55-
impl PyUUID {
56-
#[must_use]
57-
pub fn inner(&self) -> Uuid {
58-
self.inner
59-
}
60-
}
61-
6248
#[pyclass]
6349
#[derive(Clone)]
6450
pub struct PyText {
@@ -107,22 +93,6 @@ impl PyVarChar {
10793
}
10894
}
10995

110-
#[pymethods]
111-
impl PyUUID {
112-
/// Create new uuid from Python str.
113-
///
114-
/// # Errors
115-
/// May return Err Result if cannot convert python string
116-
/// into rust Uuid.
117-
#[new]
118-
#[allow(clippy::missing_errors_doc)]
119-
pub fn new_uuid(uuid_value: &str) -> RustPSQLDriverPyResult<Self> {
120-
Ok(Self {
121-
inner: Uuid::from_str(uuid_value)?,
122-
})
123-
}
124-
}
125-
12696
macro_rules! build_json_py_type {
12797
($st_name:ident, $rust_type:ty) => {
12898
#[pyclass]
@@ -217,7 +187,6 @@ pub fn extra_types_module(_py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyRes
217187
pymod.add_class::<SmallInt>()?;
218188
pymod.add_class::<Integer>()?;
219189
pymod.add_class::<BigInt>()?;
220-
pymod.add_class::<PyUUID>()?;
221190
pymod.add_class::<PyText>()?;
222191
pymod.add_class::<PyVarChar>()?;
223192
pymod.add_class::<PyJSONB>()?;

src/value_converter.rs

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use bytes::{BufMut, BytesMut};
99
use postgres_protocol::types;
1010
use pyo3::{
1111
types::{
12-
PyAnyMethods, PyBool, PyBytes, PyDict, PyDictMethods, PyFloat, PyInt, PyList,
13-
PyListMethods, PyString, PyTuple, PyTypeMethods,
12+
PyAnyMethods, PyBool, PyBytes, PyDate, PyDateTime, PyDict, PyDictMethods, PyFloat, PyInt,
13+
PyList, PyListMethods, PyString, PyTime, PyTuple, PyTypeMethods,
1414
},
1515
Bound, Py, PyAny, Python, ToPyObject,
1616
};
@@ -303,28 +303,6 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
303303
return Ok(PythonDTO::PyBytes(parameter.extract::<Vec<u8>>()?));
304304
}
305305

306-
if parameter.get_type().name()? == "datetime" {
307-
let timestamp_tz = parameter.extract::<DateTime<FixedOffset>>();
308-
if let Ok(pydatetime_tz) = timestamp_tz {
309-
return Ok(PythonDTO::PyDateTimeTz(pydatetime_tz));
310-
}
311-
312-
let timestamp_no_tz = parameter.extract::<NaiveDateTime>();
313-
if let Ok(pydatetime_no_tz) = timestamp_no_tz {
314-
return Ok(PythonDTO::PyDateTime(pydatetime_no_tz));
315-
}
316-
317-
return Err(RustPSQLDriverError::PyToRustValueConversionError(
318-
"Can not convert you datetime to rust type".into(),
319-
));
320-
}
321-
322-
if parameter.get_type().name()? == "UUID" {
323-
return Ok(PythonDTO::PyUUID(Uuid::parse_str(
324-
parameter.str()?.extract::<&str>()?,
325-
)?));
326-
}
327-
328306
if parameter.is_instance_of::<PyText>() {
329307
return Ok(PythonDTO::PyText(parameter.extract::<PyText>()?.inner()));
330308
}
@@ -366,22 +344,30 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
366344
return Ok(PythonDTO::PyIntI32(parameter.extract::<i32>()?));
367345
}
368346

369-
if parameter.get_type().name()? == "date" {
370-
return Ok(PythonDTO::PyDate(parameter.extract::<NaiveDate>()?));
347+
if parameter.is_instance_of::<PyDateTime>() {
348+
let timestamp_tz = parameter.extract::<DateTime<FixedOffset>>();
349+
if let Ok(pydatetime_tz) = timestamp_tz {
350+
return Ok(PythonDTO::PyDateTimeTz(pydatetime_tz));
351+
}
352+
353+
let timestamp_no_tz = parameter.extract::<NaiveDateTime>();
354+
if let Ok(pydatetime_no_tz) = timestamp_no_tz {
355+
return Ok(PythonDTO::PyDateTime(pydatetime_no_tz));
356+
}
357+
358+
return Err(RustPSQLDriverError::PyToRustValueConversionError(
359+
"Can not convert you datetime to rust type".into(),
360+
));
371361
}
372362

373-
// if parameter.is_instance_of::<PyDate>() {
374-
// return Ok(PythonDTO::PyDate(parameter.extract::<NaiveDate>()?));
375-
// }
363+
if parameter.is_instance_of::<PyDate>() {
364+
return Ok(PythonDTO::PyDate(parameter.extract::<NaiveDate>()?));
365+
}
376366

377-
if parameter.get_type().name()? == "time" {
367+
if parameter.is_instance_of::<PyTime>() {
378368
return Ok(PythonDTO::PyTime(parameter.extract::<NaiveTime>()?));
379369
}
380370

381-
// if parameter.is_instance_of::<PyTime>() {
382-
// return Ok(PythonDTO::PyTime(parameter.extract::<NaiveTime>()?));
383-
// }
384-
385371
if parameter.is_instance_of::<PyList>() | parameter.is_instance_of::<PyTuple>() {
386372
let mut items = Vec::new();
387373
for inner in parameter.iter()? {
@@ -439,6 +425,12 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
439425
));
440426
}
441427

428+
if parameter.get_type().name()? == "UUID" {
429+
return Ok(PythonDTO::PyUUID(Uuid::parse_str(
430+
parameter.str()?.extract::<&str>()?,
431+
)?));
432+
}
433+
442434
if let Ok(id_address) = parameter.extract::<IpAddr>() {
443435
return Ok(PythonDTO::PyIpAddress(id_address));
444436
}

0 commit comments

Comments
 (0)