Skip to content

Commit 89dd2ee

Browse files
authored
Merge pull request #49 from qaspen-python/feature/support_all_floats
Added support for FLOAT4/FLOAT8 in PostgreSQL
2 parents 1f7d347 + 467f535 commit 89dd2ee

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

python/psqlpy/_internal/extra_types.pyi

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

35+
class Float32:
36+
"""Represents `FLOAT4` in `PostgreSQL` and `f32` in Rust."""
37+
38+
def __init__(self: Self, inner_value: float) -> None:
39+
"""Create new instance of a class.
40+
41+
### Parameters:
42+
- `inner_value`: float object.
43+
"""
44+
45+
class Float64:
46+
"""Represents `FLOAT8` in `PostgreSQL` and `f64` in Rust."""
47+
48+
def __init__(self: Self, inner_value: float) -> None:
49+
"""Create new instance of a class.
50+
51+
### Parameters:
52+
- `inner_value`: float object.
53+
"""
54+
3555
class PyVarChar:
3656
"""Represent VarChar in PostgreSQL and String in Rust."""
3757

python/psqlpy/extra_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from ._internal.extra_types import (
22
BigInt,
3+
Float32,
4+
Float64,
35
Integer,
46
PyCustomType,
57
PyJSON,
@@ -22,4 +24,6 @@
2224
"PyVarChar",
2325
"PyText",
2426
"PyCustomType",
27+
"Float32",
28+
"Float64",
2529
]

python/tests/test_value_converter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from psqlpy._internal.extra_types import PyCustomType
1212
from psqlpy.extra_types import (
1313
BigInt,
14+
Float32,
15+
Float64,
1416
Integer,
1517
PyJSON,
1618
PyJSONB,
@@ -74,6 +76,8 @@ async def test_as_class(
7476
("INT4", Integer(121231231), 121231231),
7577
("INT8", BigInt(99999999999999999), 99999999999999999),
7678
("FLOAT4", 32.12329864501953, 32.12329864501953),
79+
("FLOAT4", Float32(32.12329864501953), 32.12329864501953),
80+
("FLOAT8", Float64(32.12329864501953), 32.12329864501953),
7781
("DATE", now_datetime.date(), now_datetime.date()),
7882
("TIME", now_datetime.time(), now_datetime.time()),
7983
("TIMESTAMP", now_datetime, now_datetime),
@@ -288,6 +292,7 @@ async def test_deserialization_composite_into_python(
288292
int4_ INT4,
289293
int8_ INT8,
290294
flaot4_ FLOAT4,
295+
flaot8_ FLOAT8,
291296
date_ DATE,
292297
time_ TIME,
293298
timestamp_ TIMESTAMP,
@@ -325,7 +330,7 @@ async def test_deserialization_composite_into_python(
325330
querystring=create_table_query,
326331
)
327332
await psql_pool.execute(
328-
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
333+
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
329334
parameters=[
330335
b"Bytes",
331336
"Some String",
@@ -335,6 +340,7 @@ async def test_deserialization_composite_into_python(
335340
Integer(199),
336341
BigInt(10001),
337342
32.12329864501953,
343+
Float64(32.12329864501953),
338344
now_datetime.date(),
339345
now_datetime.time(),
340346
now_datetime,
@@ -400,6 +406,7 @@ class ValidateModelForCustomType(BaseModel):
400406
int4_: int
401407
int8_: int
402408
flaot4_: float
409+
flaot8_: float
403410
date_: datetime.date
404411
time_: datetime.time
405412
timestamp_: datetime.datetime

src/extra_types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ macro_rules! build_python_type {
4444
build_python_type!(SmallInt, i16);
4545
build_python_type!(Integer, i32);
4646
build_python_type!(BigInt, i64);
47+
build_python_type!(Float32, f32);
48+
build_python_type!(Float64, f64);
4749

4850
#[pyclass]
4951
#[derive(Clone)]
@@ -187,6 +189,8 @@ pub fn extra_types_module(_py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyRes
187189
pymod.add_class::<SmallInt>()?;
188190
pymod.add_class::<Integer>()?;
189191
pymod.add_class::<BigInt>()?;
192+
pymod.add_class::<Float32>()?;
193+
pymod.add_class::<Float64>()?;
190194
pymod.add_class::<PyText>()?;
191195
pymod.add_class::<PyVarChar>()?;
192196
pymod.add_class::<PyJSONB>()?;

src/value_converter.rs

Lines changed: 15 additions & 3 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, PyCustomType, PyJSON, PyJSONB, PyMacAddr6, PyMacAddr8, PyText, PyVarChar,
27-
SmallInt,
26+
BigInt, Float32, Float64, Integer, PyCustomType, PyJSON, PyJSONB, PyMacAddr6, PyMacAddr8,
27+
PyText, PyVarChar, SmallInt,
2828
},
2929
};
3030

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

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

325+
if parameter.is_instance_of::<Float32>() {
326+
return Ok(PythonDTO::PyFloat32(
327+
parameter.extract::<Float32>()?.retrieve_value(),
328+
));
329+
}
330+
331+
if parameter.is_instance_of::<Float64>() {
332+
return Ok(PythonDTO::PyFloat64(
333+
parameter.extract::<Float64>()?.retrieve_value(),
334+
));
335+
}
336+
325337
if parameter.is_instance_of::<SmallInt>() {
326338
return Ok(PythonDTO::PyIntI16(
327339
parameter.extract::<SmallInt>()?.retrieve_value(),

0 commit comments

Comments
 (0)