Skip to content

Commit 1140516

Browse files
committed
feat: Add line and line array support (#232)
1 parent 14fdcb4 commit 1140516

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

decode.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
decodeIntArray,
1616
decodeJson,
1717
decodeJsonArray,
18+
decodeLine,
19+
decodeLineArray,
1820
decodePoint,
1921
decodePointArray,
2022
decodeStringArray,
@@ -116,6 +118,10 @@ function decodeText(value: Uint8Array, typeOid: number): any {
116118
case Oid.json_array:
117119
case Oid.jsonb_array:
118120
return decodeJsonArray(strValue);
121+
case Oid.line:
122+
return decodeLine(strValue);
123+
case Oid.line_array:
124+
return decodeLineArray(strValue);
119125
case Oid.point:
120126
return decodePoint(strValue);
121127
case Oid.point_array:

oid.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ export const Oid = {
5050
_box_0: 603,
5151
// deno-lint-ignore camelcase
5252
_polygon_0: 604,
53+
line: 628,
5354
// deno-lint-ignore camelcase
54-
_line_0: 628,
55-
// deno-lint-ignore camelcase
56-
_line_1: 629,
55+
line_array: 629,
5756
cidr: 650,
5857
// deno-lint-ignore camelcase
5958
cidr_array: 651,

query/decoders.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parseArray } from "./array_parser.ts";
2-
import { Float8, Point, TID } from "./types.ts";
2+
import { Float8, Line, Point, TID } from "./types.ts";
33

44
// Datetime parsing based on:
55
// https://github.com/bendrucker/postgres-date/blob/master/index.js
@@ -178,6 +178,20 @@ export function decodeJsonArray(value: string): unknown[] {
178178
return parseArray(value, JSON.parse);
179179
}
180180

181+
export function decodeLine(value: string): Line {
182+
const [a, b, c] = value.slice(1).slice(0, -1).split(",");
183+
184+
return {
185+
a: a as Float8,
186+
b: b as Float8,
187+
c: c as Float8,
188+
};
189+
}
190+
191+
export function decodeLineArray(value: string) {
192+
return parseArray(value, decodeLine);
193+
}
194+
181195
// Ported from https://github.com/brianc/node-pg-types
182196
// Copyright (c) 2014 Brian M. Carlson. All rights reserved. MIT License.
183197
export function decodePoint(value: string): Point {

query/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export type Float4 = "string";
1616
* */
1717
export type Float8 = "string";
1818

19+
/**
20+
* https://www.postgresql.org/docs/13/datatype-geometric.html#DATATYPE-LINE
21+
*/
22+
export interface Line {
23+
a: Float8;
24+
b: Float8;
25+
c: Float8;
26+
}
27+
1928
/**
2029
* https://www.postgresql.org/docs/13/datatype-geometric.html#id-1.5.7.16.5
2130
*/

tests/data_types.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { Client } from "../mod.ts";
99
import TEST_CONNECTION_PARAMS from "./config.ts";
1010
import { getTestClient } from "./helpers.ts";
11-
import { Float4, Float8, Point, TID, Timestamp } from "../query/types.ts";
11+
import { Float4, Float8, Line, Point, TID, Timestamp } from "../query/types.ts";
1212

1313
const SETUP = [
1414
"DROP TABLE IF EXISTS data_types;",
@@ -705,3 +705,26 @@ testClient(async function dateArray() {
705705
dates.map((date) => parseDate(date, "yyyy-MM-dd")),
706706
);
707707
});
708+
709+
testClient(async function line() {
710+
const result = await CLIENT.queryArray<[Line]>(
711+
"SELECT '[(1, 2), (3, 4)]'::LINE",
712+
);
713+
714+
assertEquals(result.rows[0][0], { a: "1", b: "-1", c: "1" });
715+
});
716+
717+
testClient(async function lineArray() {
718+
const result = await CLIENT.queryArray<[[Line, Line]]>(
719+
"SELECT ARRAY['[(1, 2), (3, 4)]'::LINE, '41, 1, -9, 25.5']",
720+
);
721+
722+
assertEquals(result.rows[0][0], [
723+
{ a: "1", b: "-1", c: "1" },
724+
{
725+
a: "-0.49",
726+
b: "-1",
727+
c: "21.09",
728+
},
729+
]);
730+
});

0 commit comments

Comments
 (0)