Skip to content

Commit 1f86f8a

Browse files
SindriFrSindri Frostason
authored andcommitted
feat: Add support for postgres 'point' type
1 parent d328d0d commit 1f86f8a

File tree

12 files changed

+80
-0
lines changed

12 files changed

+80
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ time = { version = "0.3.36", default-features = false, optional = true, features
4141
ipnetwork = { version = "0.20", default-features = false, optional = true }
4242
mac_address = { version = "1.1", default-features = false, optional = true }
4343
ordered-float = { version = "4.6", default-features = false, optional = true }
44+
geo-types = { version = "0.7", default-features = false, optional = true }
4445

4546
[dev-dependencies]
4647
sea-query = { path = ".", features = ["tests-cfg"] }
@@ -68,6 +69,7 @@ with-uuid = ["uuid"]
6869
with-time = ["time"]
6970
with-ipnetwork = ["ipnetwork"]
7071
with-mac_address = ["mac_address"]
72+
with-postgres-point = ["dep:geo-types", "dep:postgres-types", "postgres-types/with-geo-types-0_7"]
7173
tests-cfg = []
7274
all-features = [
7375
"backend-mysql",
@@ -91,6 +93,7 @@ all-types = [
9193
"with-time",
9294
"with-ipnetwork",
9395
"with-mac_address",
96+
"with-postgres-point",
9497
]
9598
option-more-parentheses = []
9699
option-sqlite-exact-column-type = []

sea-query-binder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ with-uuid = ["sqlx?/uuid", "sea-query/with-uuid", "uuid"]
4242
with-time = ["sqlx?/time", "sea-query/with-time", "time"]
4343
with-ipnetwork = ["sqlx?/ipnetwork", "sea-query/with-ipnetwork", "ipnetwork"]
4444
with-mac_address = ["sqlx?/mac_address", "sea-query/with-mac_address", "mac_address"]
45+
with-postgres-point = ["sea-query/with-postgres-point"]
4546
postgres-array = ["sea-query/postgres-array"]
4647
postgres-vector = ["sea-query/postgres-vector", "pgvector/sqlx"]
4748
runtime-async-std = ["sqlx?/runtime-async-std"]

sea-query-binder/src/sqlx_postgres.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ impl sqlx::IntoArguments<'_, sqlx::postgres::Postgres> for SqlxValues {
129129
Value::MacAddress(mac) => {
130130
let _ = args.add(mac.as_deref());
131131
}
132+
#[cfg(feature = "with-postgres-point")]
133+
Value::Point(point) => {
134+
let _ = args.add(point.as_deref());
135+
}
132136
#[cfg(feature = "postgres-array")]
133137
Value::Array(ty, v) => match ty {
134138
ArrayType::Bool => {

sea-query-postgres/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ postgres-array = ["postgres-types/array-impls", "sea-query/postgres-array"]
3939
postgres-vector = ["sea-query/postgres-vector", "pgvector/postgres"]
4040
with-ipnetwork = ["postgres-types/with-cidr-0_2", "sea-query/with-ipnetwork", "ipnetwork", "cidr"]
4141
with-mac_address = ["postgres-types/with-eui48-1", "sea-query/with-mac_address", "mac_address", "eui48"]
42+
with-postgres-point = ["sea-query/with-postgres-point"]

sea-query-postgres/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ impl ToSql for PostgresValue {
137137
.map(|v| MacAddress::new(v.bytes()))
138138
.to_sql(ty, out)
139139
}
140+
#[cfg(feature = "with-postgres-point")]
141+
Value::Point(v) => v.as_deref().to_sql(ty, out),
140142
}
141143
}
142144

src/backend/mysql/table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl TableBuilder for MysqlQueryBuilder {
9595
ColumnType::Inet => unimplemented!("Inet is not available in MySQL."),
9696
ColumnType::MacAddr => unimplemented!("MacAddr is not available in MySQL."),
9797
ColumnType::LTree => unimplemented!("LTree is not available in MySQL."),
98+
ColumnType::Point => unimplemented!("Point is not available in MySQL."),
9899
}
99100
)
100101
.unwrap();

src/backend/postgres/table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ impl TableBuilder for PostgresQueryBuilder {
8282
ColumnType::MacAddr => "macaddr".into(),
8383
ColumnType::Year => unimplemented!("Year is not available in Postgres."),
8484
ColumnType::LTree => "ltree".into(),
85+
ColumnType::Point => "point".into(),
8586
}
8687
)
8788
.unwrap()

src/backend/query_builder.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,8 @@ pub trait QueryBuilder:
11101110
Value::Array(_, None) => write!(s, "NULL").unwrap(),
11111111
#[cfg(feature = "postgres-vector")]
11121112
Value::Vector(None) => write!(s, "NULL").unwrap(),
1113+
#[cfg(feature = "with-postgres-point")]
1114+
Value::Point(None) => s.write_str("NULL").unwrap(),
11131115
Value::Bool(Some(b)) => write!(s, "{}", if *b { "TRUE" } else { "FALSE" }).unwrap(),
11141116
Value::TinyInt(Some(v)) => write!(s, "{v}").unwrap(),
11151117
Value::SmallInt(Some(v)) => write!(s, "{v}").unwrap(),
@@ -1204,6 +1206,14 @@ pub trait QueryBuilder:
12041206
Value::IpNetwork(Some(v)) => write!(s, "'{v}'").unwrap(),
12051207
#[cfg(feature = "with-mac_address")]
12061208
Value::MacAddress(Some(v)) => write!(s, "'{v}'").unwrap(),
1209+
#[cfg(feature = "with-postgres-point")]
1210+
Value::Point(Some(v)) => {
1211+
s.write_str("'(").unwrap();
1212+
write!(s, "{}", v.x()).unwrap();
1213+
s.write_str(",").unwrap();
1214+
write!(s, "{}", v.y()).unwrap();
1215+
s.write_str(")'").unwrap();
1216+
}
12071217
};
12081218
s
12091219
}

src/backend/sqlite/table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl SqliteQueryBuilder {
192192
ColumnType::Bit(_) => unimplemented!("Bit is not available in Sqlite."),
193193
ColumnType::VarBit(_) => unimplemented!("VarBit is not available in Sqlite."),
194194
ColumnType::LTree => unimplemented!("LTree is not available in Sqlite."),
195+
ColumnType::Point => unimplemented!("Point is not available in Sqlite."),
195196
}
196197
)
197198
.unwrap()

src/table/column.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub trait IntoColumnDef {
5757
/// | Inet | N/A | inet | N/A |
5858
/// | MacAddr | N/A | macaddr | N/A |
5959
/// | LTree | N/A | ltree | N/A |
60+
/// | Point | N/A | point | N/A |
6061
#[non_exhaustive]
6162
#[derive(Debug, Clone)]
6263
pub enum ColumnType {
@@ -102,6 +103,7 @@ pub enum ColumnType {
102103
Inet,
103104
MacAddr,
104105
LTree,
106+
Point,
105107
}
106108

107109
/// Length for var-char/binary; default to 255

0 commit comments

Comments
 (0)