Skip to content

Commit 1ba5db3

Browse files
authored
feat: Add support for circle and circle array (#237)
1 parent deaa7a7 commit 1ba5db3

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

decode.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
decodeBoxArray,
1010
decodeBytea,
1111
decodeByteaArray,
12+
decodeCircle,
13+
decodeCircleArray,
1214
decodeDate,
1315
decodeDateArray,
1416
decodeDatetime,
@@ -112,6 +114,10 @@ function decodeText(value: Uint8Array, typeOid: number): any {
112114
return decodeBox(strValue);
113115
case Oid.box_array:
114116
return decodeBoxArray(strValue);
117+
case Oid.circle:
118+
return decodeCircle(strValue);
119+
case Oid.circle_array:
120+
return decodeCircleArray(strValue);
115121
case Oid.bytea:
116122
return decodeBytea(strValue);
117123
case Oid.byte_array:

oid.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ export const Oid = {
6161
// deno-lint-ignore camelcase
6262
_tinterval_0: 704,
6363
_unknown: 705,
64+
circle: 718,
6465
// deno-lint-ignore camelcase
65-
_circle_0: 718,
66-
// deno-lint-ignore camelcase
67-
_circle_1: 719,
66+
circle_array: 719,
6867
// deno-lint-ignore camelcase
6968
_money_0: 790,
7069
// deno-lint-ignore camelcase

query/decoders.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parseArray } from "./array_parser.ts";
22
import {
33
Box,
4+
Circle,
45
Float8,
56
Line,
67
LineSegment,
@@ -102,6 +103,21 @@ function decodeByteaHex(byteaStr: string): Uint8Array {
102103
return bytes;
103104
}
104105

106+
export function decodeCircle(value: string): Circle {
107+
const [point, radius] = value.substring(1, value.length - 1).split(
108+
/,(?![^(]*\))/,
109+
);
110+
111+
return {
112+
point: decodePoint(point),
113+
radius: radius as Float8,
114+
};
115+
}
116+
117+
export function decodeCircleArray(value: string) {
118+
return parseArray(value, decodeCircle);
119+
}
120+
105121
export function decodeDate(dateStr: string): Date | number {
106122
// there are special `infinity` and `-infinity`
107123
// cases representing out-of-range dates

query/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export interface Box {
66
b: Point;
77
}
88

9+
/**
10+
* https://www.postgresql.org/docs/13/datatype-geometric.html#DATATYPE-CIRCLE
11+
*/
12+
export interface Circle {
13+
point: Point;
14+
radius: Float8;
15+
}
16+
917
/**
1018
* Decimal-like string. Uses dot to split the decimal
1119
*

tests/data_types.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import TEST_CONNECTION_PARAMS from "./config.ts";
1010
import { getTestClient } from "./helpers.ts";
1111
import {
1212
Box,
13+
Circle,
1314
Float4,
1415
Float8,
1516
Line,
@@ -867,3 +868,25 @@ testClient(async function polygonArray() {
867868

868869
assertEquals(selectRes.rows[0][0][0], points);
869870
});
871+
872+
testClient(async function circle() {
873+
const point = generateRandomPoint();
874+
const radius = String(generateRandomNumber(100));
875+
876+
const { rows } = await CLIENT.queryArray<[Circle]>(
877+
`SELECT '<(${point.x},${point.y}), ${radius}>'::CIRCLE`,
878+
);
879+
880+
assertEquals(rows[0][0], { point, radius });
881+
});
882+
883+
testClient(async function circleArray() {
884+
const point = generateRandomPoint();
885+
const radius = String(generateRandomNumber(100));
886+
887+
const { rows } = await CLIENT.queryArray<[[Circle]]>(
888+
`SELECT ARRAY['<(${point.x},${point.y}), ${radius}>'::CIRCLE]`,
889+
);
890+
891+
assertEquals(rows[0][0][0], { point, radius });
892+
});

0 commit comments

Comments
 (0)