Skip to content

feat: Add support for polygon and polygon array #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2021
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
6 changes: 6 additions & 0 deletions decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
decodePathArray,
decodePoint,
decodePointArray,
decodePolygon,
decodePolygonArray,
decodeStringArray,
decodeTid,
decodeTidArray,
Expand Down Expand Up @@ -144,6 +146,10 @@ function decodeText(value: Uint8Array, typeOid: number): any {
return decodePoint(strValue);
case Oid.point_array:
return decodePointArray(strValue);
case Oid.polygon:
return decodePolygon(strValue);
case Oid.polygon_array:
return decodePolygonArray(strValue);
case Oid.tid:
return decodeTid(strValue);
case Oid.tid_array:
Expand Down
5 changes: 2 additions & 3 deletions oid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export const Oid = {
lseg: 601,
path: 602,
box: 603,
// deno-lint-ignore camelcase
_polygon_0: 604,
polygon: 604,
line: 628,
// deno-lint-ignore camelcase
line_array: 629,
Expand Down Expand Up @@ -125,7 +124,7 @@ export const Oid = {
// deno-lint-ignore camelcase
_tinterval_1: 1025,
// deno-lint-ignore camelcase
_polygon_1: 1027,
polygon_array: 1027,
// deno-lint-ignore camelcase
oid_array: 1028,
// deno-lint-ignore camelcase
Expand Down
19 changes: 18 additions & 1 deletion query/decoders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { parseArray } from "./array_parser.ts";
import { Box, Float8, Line, LineSegment, Path, Point, TID } from "./types.ts";
import {
Box,
Float8,
Line,
LineSegment,
Path,
Point,
Polygon,
TID,
} from "./types.ts";

// Datetime parsing based on:
// https://github.com/bendrucker/postgres-date/blob/master/index.js
Expand Down Expand Up @@ -251,6 +260,14 @@ export function decodePointArray(value: string) {
return parseArray(value, decodePoint);
}

export function decodePolygon(value: string): Polygon {
return decodePath(value);
}

export function decodePolygonArray(value: string) {
return parseArray(value, decodePolygon);
}

export function decodeStringArray(value: string) {
if (!value) return null;
return parseArray(value);
Expand Down
5 changes: 5 additions & 0 deletions query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export interface Point {
y: Float8;
}

/**
* https://www.postgresql.org/docs/13/datatype-geometric.html#DATATYPE-POLYGON
*/
export type Polygon = Point[];

/**
* https://www.postgresql.org/docs/13/datatype-oid.html
*/
Expand Down
59 changes: 43 additions & 16 deletions tests/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
LineSegment,
Path,
Point,
Polygon,
TID,
Timestamp,
} from "../query/types.ts";
Expand All @@ -37,6 +38,14 @@ function generateRandomNumber(max_value: number) {
return Math.round((Math.random() * max_value + Number.EPSILON) * 100) / 100;
}

// deno-lint-ignore camelcase
function generateRandomPoint(max_value = 100): Point {
return {
x: String(generateRandomNumber(max_value)) as Float8,
y: String(generateRandomNumber(max_value)) as Float8,
};
}

const CLIENT = new Client(TEST_CONNECTION_PARAMS);

const testClient = getTestClient(CLIENT, SETUP);
Expand Down Expand Up @@ -806,37 +815,55 @@ testClient(async function boxArray() {
testClient(async function path() {
const points = Array.from(
{ length: Math.floor((Math.random() + 1) * 10) },
() => {
return [
String(generateRandomNumber(100)),
String(generateRandomNumber(100)),
];
},
generateRandomPoint,
);

const selectRes = await CLIENT.queryArray<[Path]>(
`SELECT '(${points.map(([x, y]) => `(${x},${y})`).join(",")})'::PATH`,
`SELECT '(${points.map(({ x, y }) => `(${x},${y})`).join(",")})'::PATH`,
);

assertEquals(selectRes.rows[0][0], points.map(([x, y]) => ({ x, y })));
assertEquals(selectRes.rows[0][0], points);
});

testClient(async function pathArray() {
const points = Array.from(
{ length: Math.floor((Math.random() + 1) * 10) },
() => {
return [
String(generateRandomNumber(100)),
String(generateRandomNumber(100)),
];
},
generateRandomPoint,
);

const selectRes = await CLIENT.queryArray<[[Path]]>(
`SELECT ARRAY['(${
points.map(([x, y]) => `(${x},${y})`).join(",")
points.map(({ x, y }) => `(${x},${y})`).join(",")
})'::PATH]`,
);

assertEquals(selectRes.rows[0][0][0], points.map(([x, y]) => ({ x, y })));
assertEquals(selectRes.rows[0][0][0], points);
});

testClient(async function polygon() {
const points = Array.from(
{ length: Math.floor((Math.random() + 1) * 10) },
generateRandomPoint,
);

const selectRes = await CLIENT.queryArray<[Polygon]>(
`SELECT '(${points.map(({ x, y }) => `(${x},${y})`).join(",")})'::POLYGON`,
);

assertEquals(selectRes.rows[0][0], points);
});

testClient(async function polygonArray() {
const points = Array.from(
{ length: Math.floor((Math.random() + 1) * 10) },
generateRandomPoint,
);

const selectRes = await CLIENT.queryArray<[[Polygon]]>(
`SELECT ARRAY['(${
points.map(({ x, y }) => `(${x},${y})`).join(",")
})'::POLYGON]`,
);

assertEquals(selectRes.rows[0][0][0], points);
});