Skip to content

Commit d2a63c5

Browse files
committed
First variant for custom encoder
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent 510702c commit d2a63c5

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

python/psqlpy/_internal/extra_types.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,6 @@ class PyMacAddr8:
123123
### Parameters:
124124
- `value`: value for MACADDR8 field.
125125
"""
126+
127+
class PyCustomType:
128+
def __init__(self, value: bytes) -> None: ...

python/psqlpy/extra_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ._internal.extra_types import (
22
BigInt,
33
Integer,
4+
PyCustomType,
45
PyJSON,
56
PyJSONB,
67
PyMacAddr6,
@@ -22,4 +23,5 @@
2223
"PyMacAddr8",
2324
"PyVarChar",
2425
"PyText",
26+
"PyCustomType",
2527
]

src/extra_types.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ macro_rules! build_macaddr_type {
190190
build_macaddr_type!(PyMacAddr6, MacAddr6);
191191
build_macaddr_type!(PyMacAddr8, MacAddr8);
192192

193+
#[pyclass]
194+
#[derive(Clone, Debug)]
195+
pub struct PyCustomType {
196+
inner: Vec<u8>,
197+
}
198+
199+
impl PyCustomType {
200+
#[must_use]
201+
pub fn inner(&self) -> Vec<u8> {
202+
self.inner.clone()
203+
}
204+
}
205+
206+
#[pymethods]
207+
impl PyCustomType {
208+
#[new]
209+
fn new_class(type_bytes: Vec<u8>) -> Self {
210+
PyCustomType { inner: type_bytes }
211+
}
212+
}
213+
193214
#[allow(clippy::module_name_repetitions)]
194215
#[allow(clippy::missing_errors_doc)]
195216
pub fn extra_types_module(_py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyResult<()> {
@@ -203,5 +224,6 @@ pub fn extra_types_module(_py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyRes
203224
pymod.add_class::<PyJSON>()?;
204225
pymod.add_class::<PyMacAddr6>()?;
205226
pymod.add_class::<PyMacAddr8>()?;
227+
pymod.add_class::<PyCustomType>()?;
206228
Ok(())
207229
}

src/value_converter.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::{
2323
additional_types::{RustMacAddr6, RustMacAddr8},
2424
exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult},
2525
extra_types::{
26-
BigInt, Integer, PyJSON, PyJSONB, PyMacAddr6, PyMacAddr8, PyText, PyUUID, PyVarChar,
27-
SmallInt,
26+
BigInt, Integer, PyCustomType, PyJSON, PyJSONB, PyMacAddr6, PyMacAddr8, PyText, PyUUID,
27+
PyVarChar, SmallInt,
2828
},
2929
};
3030

@@ -62,6 +62,7 @@ pub enum PythonDTO {
6262
PyJson(Value),
6363
PyMacAddr6(MacAddr6),
6464
PyMacAddr8(MacAddr8),
65+
PyCustomType(Vec<u8>),
6566
}
6667

6768
impl PythonDTO {
@@ -174,6 +175,9 @@ impl ToSql for PythonDTO {
174175

175176
match self {
176177
PythonDTO::PyNone => {}
178+
PythonDTO::PyCustomType(some_bytes) => {
179+
<&[u8] as ToSql>::to_sql(&some_bytes.as_slice(), ty, out)?;
180+
}
177181
PythonDTO::PyBytes(pybytes) => {
178182
<Vec<u8> as ToSql>::to_sql(pybytes, ty, out)?;
179183
}
@@ -284,6 +288,12 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
284288
return Ok(PythonDTO::PyNone);
285289
}
286290

291+
if parameter.is_instance_of::<PyCustomType>() {
292+
return Ok(PythonDTO::PyCustomType(
293+
parameter.extract::<PyCustomType>()?.inner(),
294+
));
295+
}
296+
287297
if parameter.is_instance_of::<PyBool>() {
288298
return Ok(PythonDTO::PyBool(parameter.extract::<bool>()?));
289299
}

0 commit comments

Comments
 (0)