Skip to content

Commit 402d23a

Browse files
author
AGAEV Denis E
committed
Remove polygon usage and fix some errors
1 parent 3581dc9 commit 402d23a

File tree

7 files changed

+26
-195
lines changed

7 files changed

+26
-195
lines changed

python/psqlpy/_internal/extra_types.pyi

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,22 +230,6 @@ class PyLineSegment:
230230
- `value`: any valid structure with int/float numbers in every combination.
231231
"""
232232

233-
class PyPolygon:
234-
"""Represent polygon field in PostgreSQL and Polygon in Rust."""
235-
236-
def __init__(self: Self, value: PairsOfCoordinates) -> None:
237-
"""Create new instance of PyPolygon.
238-
239-
You need to pass any of this structures:
240-
- sequence(List, Tuple or Set) of sequences(List, Tuple or Set),
241-
each with pair of int/float numbers in every combination
242-
- sequence(List, Tuple or Set) with pairs
243-
of int/float numbers in every combination
244-
245-
### Parameters:
246-
- `value`: any valid structure with int/float numbers in every combination.
247-
"""
248-
249233
class PyCircle:
250234
"""Represent line field in PostgreSQL and Line in Rust."""
251235

python/psqlpy/extra_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
PyMacAddr8,
1616
PyPath,
1717
PyPoint,
18-
PyPolygon,
1918
PyText,
2019
PyVarChar,
2120
SmallInt,
@@ -40,6 +39,5 @@
4039
"PyPath",
4140
"PyLine",
4241
"PyLineSegment",
43-
"PyPolygon",
4442
"PyCircle",
4543
]

python/tests/test_value_converter.py

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from decimal import Decimal
44
from enum import Enum
55
from ipaddress import IPv4Address
6-
from typing import Annotated, Any, Dict, List, Union
6+
from typing import Annotated, Any, Dict, List, Tuple, Union
77

88
import pytest
99
from pydantic import BaseModel
@@ -27,7 +27,6 @@
2727
PyMacAddr8,
2828
PyPath,
2929
PyPoint,
30-
PyPolygon,
3130
PyText,
3231
SmallInt,
3332
)
@@ -164,16 +163,6 @@ async def test_as_class(
164163
("LINE", PyLine([1, -2, 3]), (1.0, -2.0, 3.0)),
165164
("LSEG", PyLineSegment({(1, 2), (9, 9)}), [(1.0, 2.0), (9.0, 9.0)]),
166165
("LSEG", PyLineSegment(((1.7, 2.8), (9, 9))), [(1.7, 2.8), (9.0, 9.0)]),
167-
(
168-
"POLYGON",
169-
PyPolygon((1.7, 2.8, 3.3, 2.5, 9, 9, 8, 8, 1.7, 2.8)),
170-
((1.7, 2.8), (3.3, 2.5), (9.0, 9.0), (8.0, 8.0), (1.7, 2.8)),
171-
),
172-
(
173-
"POLYGON",
174-
PyPolygon(((1.7, 2.8), (3.3, 2.5), (9, 9), (8, 8), (1.7, 2.8))),
175-
((1.7, 2.8), (3.3, 2.5), (9.0, 9.0), (8.0, 8.0), (1.7, 2.8)),
176-
),
177166
(
178167
"CIRCLE",
179168
PyCircle((1.7, 2.8, 3)),
@@ -370,17 +359,6 @@ async def test_as_class(
370359
[(5.6, 3.1), (4.0, 5.0)],
371360
],
372361
),
373-
(
374-
"POLYGON ARRAY",
375-
[
376-
PyPolygon(((1.7, 2.8), (3.3, 2.5), (9, 9), (1.7, 2.8))),
377-
PyPolygon([{-1.0, 0.5}, {-0.3, 0.3}, {0.3, 0.7}, {0.6, -0.6}]),
378-
],
379-
[
380-
((1.7, 2.8), (3.3, 2.5), (9.0, 9.0), (1.7, 2.8)),
381-
((-1.0, 0.5), (-0.3, 0.3), (0.3, 0.7), (0.6, -0.6)),
382-
],
383-
),
384362
(
385363
"CIRCLE ARRAY",
386364
[
@@ -456,7 +434,6 @@ async def test_deserialization_composite_into_python(
456434
path_ PATH,
457435
line_ LINE,
458436
lseg_ LSEG,
459-
polygon_ POLYGON,
460437
circle_ CIRCLE,
461438
462439
varchar_arr VARCHAR ARRAY,
@@ -481,7 +458,6 @@ async def test_deserialization_composite_into_python(
481458
path_arr PATH ARRAY,
482459
line_arr LINE ARRAY,
483460
lseg_arr LSEG ARRAY,
484-
polygon_arr POLYGON ARRAY,
485461
circle_arr CIRCLE ARRAY
486462
)
487463
"""
@@ -501,9 +477,9 @@ class TestEnum(Enum):
501477
SAD = "sad"
502478
HAPPY = "happy"
503479

504-
row_values = ", ".join([f"${index}" for index in range(1, 41)])
505-
row_values += ", ROW($41, $42), "
506-
row_values += ", ".join([f"${index}" for index in range(43, 51)])
480+
row_values = ", ".join([f"${index}" for index in range(1, 40)])
481+
row_values += ", ROW($40, $41), "
482+
row_values += ", ".join([f"${index}" for index in range(42, 49)])
507483

508484
await psql_pool.execute(
509485
querystring=f"INSERT INTO for_test VALUES (ROW({row_values}))",
@@ -539,7 +515,6 @@ class TestEnum(Enum):
539515
PyPath(((1.7, 2.8), (3.3, 2.5), (9, 9), (1.7, 2.8))),
540516
PyLine({-2, 1, 2}),
541517
PyLineSegment(((1.7, 2.8), (9, 9))),
542-
PyPolygon(((1.7, 2.8), (3.3, 2.5), (9, 9), (1.7, 2.8))),
543518
PyCircle([1.7, 2.8, 3]),
544519
["Some String", "Some String"],
545520
[PyText("Some String"), PyText("Some String")],
@@ -601,10 +576,6 @@ class TestEnum(Enum):
601576
PyLineSegment({(1, 2), (9, 9)}),
602577
PyLineSegment([(5.6, 3.1), (4, 5)]),
603578
],
604-
[
605-
PyPolygon(((1.7, 2.8), (3.3, 2.5), (9, 9), (1.7, 2.8))),
606-
PyPolygon([{-1.0, 0.5}, {-0.3, 0.3}, {0.3, 0.7}, {0.6, -0.6}]),
607-
],
608579
[
609580
PyCircle([1.7, 2.8, 3]),
610581
PyCircle([5, 1.8, 10]),
@@ -635,13 +606,12 @@ class ValidateModelForCustomType(BaseModel):
635606
inet_: IPv4Address
636607
jsonb_: Dict[str, List[Union[str, int, List[str]]]]
637608
json_: Dict[str, List[Union[str, int, List[str]]]]
638-
point_: tuple[float, float]
639-
box_: tuple[tuple[float, float], tuple[float, float]]
640-
path_: list[tuple[float, float]]
609+
point_: Tuple[float, float]
610+
box_: Tuple[Tuple[float, float], Tuple[float, float]]
611+
path_: List[Tuple[float, float]]
641612
line_: Annotated[list[float], 3]
642-
lseg_: Annotated[list[tuple[float, float]], 2]
643-
polygon_: tuple[tuple[float, float], ...]
644-
circle_: tuple[float, float, float]
613+
lseg_: Annotated[list[Tuple[float, float]], 2]
614+
circle_: Tuple[Tuple[float, float], float]
645615

646616
varchar_arr: List[str]
647617
text_arr: List[str]
@@ -658,13 +628,12 @@ class ValidateModelForCustomType(BaseModel):
658628
inet_arr: List[IPv4Address]
659629
jsonb_arr: List[Dict[str, List[Union[str, int, List[str]]]]]
660630
json_arr: List[Dict[str, List[Union[str, int, List[str]]]]]
661-
point_arr: list[tuple[float, float]]
662-
box_arr: list[tuple[tuple[float, float], tuple[float, float]]]
663-
path_arr: list[list[tuple[float, float]]]
664-
line_arr: list[Annotated[list[float], 3]]
665-
lseg_arr: list[Annotated[list[tuple[float, float]], 2]]
666-
polygon_arr: list[tuple[tuple[float, float], ...]]
667-
circle_arr: list[tuple[float, float, float]]
631+
point_arr: List[Tuple[float, float]]
632+
box_arr: List[Tuple[Tuple[float, float], Tuple[float, float]]]
633+
path_arr: List[list[Tuple[float, float]]]
634+
line_arr: List[Annotated[List[float], 3]]
635+
lseg_arr: List[Annotated[List[Tuple[float, float]], 2]]
636+
circle_arr: List[Tuple[Tuple[float, float], float]]
668637

669638
test_inner_value: ValidateModelForInnerValueType
670639
test_enum_type: TestEnum

src/additional_types.rs

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
22

33
use byteorder::{BigEndian, ReadBytesExt};
44
use bytes::{BufMut, BytesMut};
5-
use geo_types::{
6-
coord, Coord, CoordFloat, CoordNum, Line as LineSegment, LineString, Point, Polygon, Rect,
7-
};
8-
use itertools::Itertools;
5+
use geo_types::{coord, Coord, CoordFloat, CoordNum, Line as LineSegment, LineString, Point, Rect};
96
use macaddr::{MacAddr6, MacAddr8};
107
use postgres_protocol::types;
118
use postgres_types::{to_sql_checked, IsNull, ToSql};
@@ -79,7 +76,6 @@ build_additional_rust_type!(RustPoint, Point);
7976
build_additional_rust_type!(RustRect, Rect);
8077
build_additional_rust_type!(RustLineString, LineString);
8178
build_additional_rust_type!(RustLineSegment, LineSegment);
82-
build_additional_rust_type!(RustPolygon, Polygon);
8379

8480
impl<'a> IntoPy<PyObject> for &'a RustPoint {
8581
#[inline]
@@ -296,76 +292,6 @@ impl<'a> FromSql<'a> for RustLineSegment {
296292
}
297293
}
298294

299-
impl<'a> IntoPy<PyObject> for &'a RustPolygon {
300-
#[inline]
301-
fn into_py(self, py: Python<'_>) -> PyObject {
302-
let inner_value = self.inner();
303-
304-
let mut result_vec: Vec<Py<PyAny>> = vec![];
305-
for coordinate in inner_value.exterior() {
306-
result_vec.push(
307-
PyTuple::new_bound(py, vec![coordinate.x.into_py(py), coordinate.y.into_py(py)])
308-
.into(),
309-
);
310-
}
311-
312-
return PyTuple::new_bound(py, result_vec).into();
313-
}
314-
}
315-
316-
impl ToSql for RustPolygon {
317-
fn to_sql(
318-
&self,
319-
_: &Type,
320-
out: &mut BytesMut,
321-
) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> {
322-
for coord in &self.inner().exterior().0 {
323-
let coordinates_pair = format!("{} {}", coord.x, coord.y);
324-
let coordinates_bytes_repr = coordinates_pair.as_bytes();
325-
out.extend_from_slice(coordinates_bytes_repr);
326-
}
327-
328-
Ok(IsNull::No)
329-
}
330-
331-
to_sql_checked!();
332-
333-
fn accepts(_ty: &Type) -> bool {
334-
true
335-
}
336-
}
337-
338-
impl<'a> FromSql<'a> for RustPolygon {
339-
fn from_sql(
340-
_ty: &Type,
341-
raw: &'a [u8],
342-
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
343-
let mut vec_raw = vec![];
344-
vec_raw.extend_from_slice(raw);
345-
let mut buf = vec_raw.as_slice();
346-
347-
let mut vec_raw_coord = vec![];
348-
buf.read_f64_into::<BigEndian>(&mut vec_raw_coord)?;
349-
350-
let mut vec_coord = vec![];
351-
for (x1, y1) in vec_raw_coord.into_iter().tuples() {
352-
vec_coord.push(coord!(x: x1, y: y1));
353-
}
354-
355-
if !buf.is_empty() {
356-
return Err("Cannot convert PostgreSQL POLYGON into rust Polygon".into());
357-
}
358-
359-
let polygon_exterior = LineString::from(vec_coord);
360-
let new_polygon = Polygon::new(polygon_exterior, vec![]);
361-
Ok(RustPolygon::new(new_polygon))
362-
}
363-
364-
fn accepts(_ty: &Type) -> bool {
365-
true
366-
}
367-
}
368-
369295
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
370296
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
371297
pub struct Line<T: CoordNum = f64> {

src/driver/connection_pool.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ impl ConnectionPool {
304304
.collect::<Vec<&QueryParameter>>(),
305305
)
306306
.await
307+
.map_err(|err| {
308+
RustPSQLDriverError::ConnectionExecuteError(format!(
309+
"Cannot execute statement from ConnectionPool, error - {err}"
310+
))
311+
})
307312
})
308313
.await??
309314
} else {

src/extra_types.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::str::FromStr;
22

3-
use geo_types::{Line as LineSegment, LineString, Point, Polygon, Rect};
3+
use geo_types::{Line as LineSegment, LineString, Point, Rect};
44
use macaddr::{MacAddr6, MacAddr8};
55
use pyo3::{
66
pyclass, pymethods,
@@ -211,7 +211,6 @@ build_geo_type!(PyBox, Rect);
211211
build_geo_type!(PyPath, LineString);
212212
build_geo_type!(PyLine, Line);
213213
build_geo_type!(PyLineSegment, LineSegment);
214-
build_geo_type!(PyPolygon, Polygon);
215214
build_geo_type!(PyCircle, Circle);
216215

217216
#[pymethods]
@@ -279,19 +278,6 @@ impl PyLineSegment {
279278
}
280279
}
281280

282-
#[pymethods]
283-
impl PyPolygon {
284-
#[new]
285-
#[allow(clippy::missing_errors_doc)]
286-
pub fn new_polygon(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
287-
let polygon_coords = build_geo_coords(value, None)?;
288-
289-
Ok(Self {
290-
inner: Polygon::new(LineString::from(polygon_coords), vec![]),
291-
})
292-
}
293-
}
294-
295281
#[pymethods]
296282
impl PyCircle {
297283
#[new]
@@ -325,7 +311,6 @@ pub fn extra_types_module(_py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyRes
325311
pymod.add_class::<PyPath>()?;
326312
pymod.add_class::<PyLine>()?;
327313
pymod.add_class::<PyLineSegment>()?;
328-
pymod.add_class::<PyPolygon>()?;
329314
pymod.add_class::<PyCircle>()?;
330315
Ok(())
331316
}

0 commit comments

Comments
 (0)