Skip to content

Add Circle type #237

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 3 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 @@ -9,6 +9,8 @@ import {
decodeBoxArray,
decodeBytea,
decodeByteaArray,
decodeCircle,
decodeCircleArray,
decodeDate,
decodeDateArray,
decodeDatetime,
Expand Down Expand Up @@ -112,6 +114,10 @@ function decodeText(value: Uint8Array, typeOid: number): any {
return decodeBox(strValue);
case Oid.box_array:
return decodeBoxArray(strValue);
case Oid.circle:
return decodeCircle(strValue);
case Oid.circle_array:
return decodeCircleArray(strValue);
case Oid.bytea:
return decodeBytea(strValue);
case Oid.byte_array:
Expand Down
5 changes: 2 additions & 3 deletions oid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ export const Oid = {
// deno-lint-ignore camelcase
_tinterval_0: 704,
_unknown: 705,
circle: 718,
// deno-lint-ignore camelcase
_circle_0: 718,
// deno-lint-ignore camelcase
_circle_1: 719,
circle_array: 719,
// deno-lint-ignore camelcase
_money_0: 790,
// deno-lint-ignore camelcase
Expand Down
16 changes: 16 additions & 0 deletions query/decoders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parseArray } from "./array_parser.ts";
import {
Box,
Circle,
Float8,
Line,
LineSegment,
Expand Down Expand Up @@ -102,6 +103,21 @@ function decodeByteaHex(byteaStr: string): Uint8Array {
return bytes;
}

export function decodeCircle(value: string): Circle {
const [point, radius] = value.substring(1, value.length - 1).split(
/,(?![^(]*\))/,
);

return {
point: decodePoint(point),
radius: radius as Float8,
};
}

export function decodeCircleArray(value: string) {
return parseArray(value, decodeCircle);
}

export function decodeDate(dateStr: string): Date | number {
// there are special `infinity` and `-infinity`
// cases representing out-of-range dates
Expand Down
8 changes: 8 additions & 0 deletions query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ export interface Box {
b: Point;
}

/**
* https://www.postgresql.org/docs/13/datatype-geometric.html#DATATYPE-CIRCLE
*/
export interface Circle {
point: Point;
radius: Float8;
}

/**
* Decimal-like string. Uses dot to split the decimal
*
Expand Down
23 changes: 23 additions & 0 deletions tests/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import TEST_CONNECTION_PARAMS from "./config.ts";
import { getTestClient } from "./helpers.ts";
import {
Box,
Circle,
Float4,
Float8,
Line,
Expand Down Expand Up @@ -867,3 +868,25 @@ testClient(async function polygonArray() {

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

testClient(async function circle() {
const point = generateRandomPoint();
const radius = String(generateRandomNumber(100));

const { rows } = await CLIENT.queryArray<[Circle]>(
`SELECT '<(${point.x},${point.y}), ${radius}>'::CIRCLE`,
);

assertEquals(rows[0][0], { point, radius });
});

testClient(async function circleArray() {
const point = generateRandomPoint();
const radius = String(generateRandomNumber(100));

const { rows } = await CLIENT.queryArray<[[Circle]]>(
`SELECT ARRAY['<(${point.x},${point.y}), ${radius}>'::CIRCLE]`,
);

assertEquals(rows[0][0][0], { point, radius });
});