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
20 changes: 20 additions & 0 deletions python/psqlpy/_internal/extra_types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ class BigInt:
- `inner_value`: int object.
"""

class Float32:
"""Represents `FLOAT4` in `PostgreSQL` and `f32` in Rust."""

def __init__(self: Self, inner_value: float) -> None:
"""Create new instance of a class.

### Parameters:
- `inner_value`: float object.
"""

class Float64:
"""Represents `FLOAT8` in `PostgreSQL` and `f64` in Rust."""

def __init__(self: Self, inner_value: float) -> None:
"""Create new instance of a class.

### Parameters:
- `inner_value`: float object.
"""

class PyVarChar:
"""Represent VarChar in PostgreSQL and String in Rust."""

Expand Down
4 changes: 4 additions & 0 deletions python/psqlpy/extra_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from ._internal.extra_types import (
BigInt,
Float32,
Float64,
Integer,
PyCustomType,
PyJSON,
Expand All @@ -22,4 +24,6 @@
"PyVarChar",
"PyText",
"PyCustomType",
"Float32",
"Float64",
]
9 changes: 8 additions & 1 deletion python/tests/test_value_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from psqlpy._internal.extra_types import PyCustomType
from psqlpy.extra_types import (
BigInt,
Float32,
Float64,
Integer,
PyJSON,
PyJSONB,
Expand Down Expand Up @@ -74,6 +76,8 @@ async def test_as_class(
("INT4", Integer(121231231), 121231231),
("INT8", BigInt(99999999999999999), 99999999999999999),
("FLOAT4", 32.12329864501953, 32.12329864501953),
("FLOAT4", Float32(32.12329864501953), 32.12329864501953),
("FLOAT8", Float64(32.12329864501953), 32.12329864501953),
("DATE", now_datetime.date(), now_datetime.date()),
("TIME", now_datetime.time(), now_datetime.time()),
("TIMESTAMP", now_datetime, now_datetime),
Expand Down Expand Up @@ -288,6 +292,7 @@ async def test_deserialization_composite_into_python(
int4_ INT4,
int8_ INT8,
flaot4_ FLOAT4,
flaot8_ FLOAT8,
date_ DATE,
time_ TIME,
timestamp_ TIMESTAMP,
Expand Down Expand Up @@ -325,7 +330,7 @@ async def test_deserialization_composite_into_python(
querystring=create_table_query,
)
await psql_pool.execute(
querystring="INSERT INTO for_test VALUES (ROW($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31))", # noqa: E501
querystring="INSERT INTO for_test VALUES (ROW($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32))", # noqa: E501
parameters=[
b"Bytes",
"Some String",
Expand All @@ -335,6 +340,7 @@ async def test_deserialization_composite_into_python(
Integer(199),
BigInt(10001),
32.12329864501953,
Float64(32.12329864501953),
now_datetime.date(),
now_datetime.time(),
now_datetime,
Expand Down Expand Up @@ -400,6 +406,7 @@ class ValidateModelForCustomType(BaseModel):
int4_: int
int8_: int
flaot4_: float
flaot8_: float
date_: datetime.date
time_: datetime.time
timestamp_: datetime.datetime
Expand Down
4 changes: 4 additions & 0 deletions src/extra_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ macro_rules! build_python_type {
build_python_type!(SmallInt, i16);
build_python_type!(Integer, i32);
build_python_type!(BigInt, i64);
build_python_type!(Float32, f32);
build_python_type!(Float64, f64);

#[pyclass]
#[derive(Clone)]
Expand Down Expand Up @@ -187,6 +189,8 @@ 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::<Float32>()?;
pymod.add_class::<Float64>()?;
pymod.add_class::<PyText>()?;
pymod.add_class::<PyVarChar>()?;
pymod.add_class::<PyJSONB>()?;
Expand Down
18 changes: 15 additions & 3 deletions src/value_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, PyVarChar,
SmallInt,
BigInt, Float32, Float64, Integer, PyCustomType, PyJSON, PyJSONB, PyMacAddr6, PyMacAddr8,
PyText, PyVarChar, SmallInt,
},
};

Expand Down Expand Up @@ -118,6 +118,7 @@ impl PythonDTO {
PythonDTO::PyIntI32(pyint) => Ok(json!(pyint)),
PythonDTO::PyIntI64(pyint) => Ok(json!(pyint)),
PythonDTO::PyIntU64(pyint) => Ok(json!(pyint)),
PythonDTO::PyFloat32(pyfloat) => Ok(json!(pyfloat)),
PythonDTO::PyFloat64(pyfloat) => Ok(json!(pyfloat)),
PythonDTO::PyList(pylist) => {
let mut vec_serde_values: Vec<Value> = vec![];
Expand Down Expand Up @@ -318,10 +319,21 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
}

if parameter.is_instance_of::<PyFloat>() {
// TODO: Add support for all types of float.
return Ok(PythonDTO::PyFloat32(parameter.extract::<f32>()?));
}

if parameter.is_instance_of::<Float32>() {
return Ok(PythonDTO::PyFloat32(
parameter.extract::<Float32>()?.retrieve_value(),
));
}

if parameter.is_instance_of::<Float64>() {
return Ok(PythonDTO::PyFloat64(
parameter.extract::<Float64>()?.retrieve_value(),
));
}

if parameter.is_instance_of::<SmallInt>() {
return Ok(PythonDTO::PyIntI16(
parameter.extract::<SmallInt>()?.retrieve_value(),
Expand Down