Skip to content

Commit c4815d5

Browse files
authored
Merge pull request #10 from qaspen-python/feature/add_ipaddr_type
Added support for INET type in PostgreSQL, IPv4Address/IPv6Address in Python
2 parents 38f6444 + 5c6f839 commit c4815d5

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/value_converter.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use chrono::{self, DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime};
22
use serde_json::{json, Map, Value};
3-
use std::fmt::Debug;
3+
use std::{fmt::Debug, net::IpAddr};
44
use uuid::Uuid;
55

66
use bytes::{BufMut, BytesMut};
@@ -45,6 +45,7 @@ pub enum PythonDTO {
4545
PyTime(NaiveTime),
4646
PyDateTime(NaiveDateTime),
4747
PyDateTimeTz(DateTime<FixedOffset>),
48+
PyIpAddress(IpAddr),
4849
PyList(Vec<PythonDTO>),
4950
PyTuple(Vec<PythonDTO>),
5051
PyJson(Value),
@@ -70,6 +71,7 @@ impl PythonDTO {
7071
PythonDTO::PyIntI64(_) => Ok(tokio_postgres::types::Type::INT8_ARRAY),
7172
PythonDTO::PyFloat32(_) => Ok(tokio_postgres::types::Type::FLOAT4_ARRAY),
7273
PythonDTO::PyFloat64(_) => Ok(tokio_postgres::types::Type::FLOAT8_ARRAY),
74+
PythonDTO::PyIpAddress(_) => Ok(tokio_postgres::types::Type::INET_ARRAY),
7375
PythonDTO::PyJson(_) => Ok(tokio_postgres::types::Type::JSONB_ARRAY),
7476
_ => Err(RustPSQLDriverError::PyToRustValueConversionError(
7577
"Can't process array type, your type doesn't have support yet".into(),
@@ -175,6 +177,9 @@ impl ToSql for PythonDTO {
175177
PythonDTO::PyDateTimeTz(pydatetime_tz) => {
176178
<&DateTime<FixedOffset> as ToSql>::to_sql(&pydatetime_tz, ty, out)?;
177179
}
180+
PythonDTO::PyIpAddress(pyidaddress) => {
181+
<&IpAddr as ToSql>::to_sql(&pyidaddress, ty, out)?;
182+
}
178183
PythonDTO::PyList(py_iterable) | PythonDTO::PyTuple(py_iterable) => {
179184
let mut items = Vec::new();
180185
for inner in py_iterable {
@@ -343,6 +348,10 @@ pub fn py_to_rust(parameter: &PyAny) -> RustPSQLDriverPyResult<PythonDTO> {
343348
));
344349
}
345350

351+
if let Ok(id_address) = parameter.extract::<IpAddr>() {
352+
return Ok(PythonDTO::PyIpAddress(id_address));
353+
}
354+
346355
Err(RustPSQLDriverError::PyToRustValueConversionError(format!(
347356
"Can not covert you type {parameter} into inner one",
348357
)))
@@ -408,6 +417,8 @@ pub fn postgres_to_py(
408417
None => Ok(py.None()),
409418
}
410419
}
420+
// ---------- IpAddress Types ----------
421+
Type::INET => Ok(row.try_get::<_, Option<IpAddr>>(column_i)?.to_object(py)),
411422
// ---------- Array Text Types ----------
412423
// Convert ARRAY of TEXT or VARCHAR into Vec<String>, then into list[str]
413424
Type::TEXT_ARRAY | Type::VARCHAR_ARRAY => Ok(row
@@ -454,6 +465,10 @@ pub fn postgres_to_py(
454465
}
455466
None => Ok(py.None().to_object(py)),
456467
},
468+
// Convert ARRAY of INET into Vec<INET>, then into list[IPv4Address | IPv6Address]
469+
Type::INET_ARRAY => Ok(row
470+
.try_get::<_, Option<Vec<IpAddr>>>(column_i)?
471+
.to_object(py)),
457472
Type::JSONB | Type::JSON => {
458473
let db_json = row.try_get::<_, Option<Value>>(column_i)?;
459474

0 commit comments

Comments
 (0)