Skip to content

Commit ac7b8a9

Browse files
SindriFrSindri Frostason
authored andcommitted
feat: Add support for postgres 'point' type
1 parent 4c59138 commit ac7b8a9

File tree

12 files changed

+79
-0
lines changed

12 files changed

+79
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ ipnetwork = { version = "0.20", default-features = false, optional = true }
4444
mac_address = { version = "1.1", default-features = false, optional = true }
4545
ordered-float = { version = "4.6", default-features = false, optional = true }
4646
itoa = { version = "1.0.15", optional = true }
47+
geo-types = { version = "0.7", default-features = false, optional = true }
4748

4849
[dev-dependencies]
4950
sea-query = { path = ".", features = ["tests-cfg"] }
@@ -87,6 +88,7 @@ with-ipnetwork = ["ipnetwork"]
8788
with-mac_address = ["mac_address"]
8889
sqlx-utils = ["derive"]
8990
sea-orm = ["sea-query-derive"]
91+
with-postgres-point = ["dep:geo-types", "dep:postgres-types", "postgres-types/with-geo-types-0_7"]
9092
tests-cfg = []
9193
all-features = [
9294
"all-types",

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
@@ -136,6 +136,8 @@ impl ToSql for PostgresValue {
136136
use eui48::MacAddress;
137137
v.map(|v| MacAddress::new(v.bytes())).to_sql(ty, out)
138138
}
139+
#[cfg(feature = "with-postgres-point")]
140+
Value::Point(v) => v.as_deref().to_sql(ty, out),
139141
}
140142
}
141143

sea-query-sqlx/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-sqlx/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);
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 => {

src/backend/mysql/table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl TableBuilder for MysqlQueryBuilder {
132132
ColumnType::Inet => unimplemented!("Inet is not available in MySQL."),
133133
ColumnType::MacAddr => unimplemented!("MacAddr is not available in MySQL."),
134134
ColumnType::LTree => unimplemented!("LTree is not available in MySQL."),
135+
ColumnType::Point => unimplemented!("Point is not available in MySQL."),
135136
}
136137
.unwrap();
137138

src/backend/postgres/table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl TableBuilder for PostgresQueryBuilder {
113113
ColumnType::MacAddr => sql.write_str("macaddr"),
114114
ColumnType::Year => unimplemented!("Year is not available in Postgres."),
115115
ColumnType::LTree => sql.write_str("ltree"),
116+
ColumnType::Point => sql.write_str("point"),
116117
}
117118
.unwrap()
118119
}

src/backend/query_builder.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,8 @@ pub trait QueryBuilder:
11671167
Value::Array(_, None) => buf.write_str("NULL")?,
11681168
#[cfg(feature = "postgres-vector")]
11691169
Value::Vector(None) => buf.write_str("NULL")?,
1170+
#[cfg(feature = "with-postgres-point")]
1171+
Value::Point(None) => buf.write_str("NULL")?,
11701172
Value::Bool(Some(b)) => buf.write_str(if *b { "TRUE" } else { "FALSE" })?,
11711173
Value::TinyInt(Some(v)) => {
11721174
write_int(buf, *v);
@@ -1355,6 +1357,14 @@ pub trait QueryBuilder:
13551357
write!(buf, "{v}")?;
13561358
buf.write_str("'")?;
13571359
}
1360+
#[cfg(feature = "with-postgres-point")]
1361+
Value::Point(Some(v)) => {
1362+
buf.write_str("'(")?;
1363+
write!(buf, "{}", v.x())?;
1364+
buf.write_str(",")?;
1365+
write!(buf, "{}", v.y())?;
1366+
buf.write_str(")'")?;
1367+
}
13581368
};
13591369

13601370
Ok(())

src/backend/sqlite/table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ impl SqliteQueryBuilder {
195195
ColumnType::Bit(_) => unimplemented!("Bit is not available in Sqlite."),
196196
ColumnType::VarBit(_) => unimplemented!("VarBit is not available in Sqlite."),
197197
ColumnType::LTree => unimplemented!("LTree is not available in Sqlite."),
198+
ColumnType::Point => unimplemented!("Point is not available in Sqlite."),
198199
}
199200
.unwrap()
200201
}

src/table/column.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ impl From<&mut ColumnDef> for ColumnDef {
7272
/// | Inet | N/A | inet | N/A |
7373
/// | MacAddr | N/A | macaddr | N/A |
7474
/// | LTree | N/A | ltree | N/A |
75+
/// | Point | N/A | point | N/A |
7576
#[non_exhaustive]
7677
#[derive(Debug, Clone)]
7778
pub enum ColumnType {
@@ -117,6 +118,7 @@ pub enum ColumnType {
117118
Inet,
118119
MacAddr,
119120
LTree,
121+
Point,
120122
}
121123

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

0 commit comments

Comments
 (0)