Skip to content

Commit

Permalink
feat: Add line and line array support (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Soremwar committed Jan 31, 2021
1 parent 14fdcb4 commit 1140516
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
6 changes: 6 additions & 0 deletions decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
decodeIntArray,
decodeJson,
decodeJsonArray,
decodeLine,
decodeLineArray,
decodePoint,
decodePointArray,
decodeStringArray,
Expand Down Expand Up @@ -116,6 +118,10 @@ function decodeText(value: Uint8Array, typeOid: number): any {
case Oid.json_array:
case Oid.jsonb_array:
return decodeJsonArray(strValue);
case Oid.line:
return decodeLine(strValue);
case Oid.line_array:
return decodeLineArray(strValue);
case Oid.point:
return decodePoint(strValue);
case Oid.point_array:
Expand Down
5 changes: 2 additions & 3 deletions oid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ export const Oid = {
_box_0: 603,
// deno-lint-ignore camelcase
_polygon_0: 604,
line: 628,
// deno-lint-ignore camelcase
_line_0: 628,
// deno-lint-ignore camelcase
_line_1: 629,
line_array: 629,
cidr: 650,
// deno-lint-ignore camelcase
cidr_array: 651,
Expand Down
16 changes: 15 additions & 1 deletion query/decoders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseArray } from "./array_parser.ts";
import { Float8, Point, TID } from "./types.ts";
import { Float8, Line, Point, TID } from "./types.ts";

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

export function decodeLine(value: string): Line {
const [a, b, c] = value.slice(1).slice(0, -1).split(",");

return {
a: a as Float8,
b: b as Float8,
c: c as Float8,
};
}

export function decodeLineArray(value: string) {
return parseArray(value, decodeLine);
}

// Ported from https://github.com/brianc/node-pg-types
// Copyright (c) 2014 Brian M. Carlson. All rights reserved. MIT License.
export function decodePoint(value: string): Point {
Expand Down
9 changes: 9 additions & 0 deletions query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export type Float4 = "string";
* */
export type Float8 = "string";

/**
* https://www.postgresql.org/docs/13/datatype-geometric.html#DATATYPE-LINE
*/
export interface Line {
a: Float8;
b: Float8;
c: Float8;
}

/**
* https://www.postgresql.org/docs/13/datatype-geometric.html#id-1.5.7.16.5
*/
Expand Down
25 changes: 24 additions & 1 deletion tests/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Client } from "../mod.ts";
import TEST_CONNECTION_PARAMS from "./config.ts";
import { getTestClient } from "./helpers.ts";
import { Float4, Float8, Point, TID, Timestamp } from "../query/types.ts";
import { Float4, Float8, Line, Point, TID, Timestamp } from "../query/types.ts";

const SETUP = [
"DROP TABLE IF EXISTS data_types;",
Expand Down Expand Up @@ -705,3 +705,26 @@ testClient(async function dateArray() {
dates.map((date) => parseDate(date, "yyyy-MM-dd")),
);
});

testClient(async function line() {
const result = await CLIENT.queryArray<[Line]>(
"SELECT '[(1, 2), (3, 4)]'::LINE",
);

assertEquals(result.rows[0][0], { a: "1", b: "-1", c: "1" });
});

testClient(async function lineArray() {
const result = await CLIENT.queryArray<[[Line, Line]]>(
"SELECT ARRAY['[(1, 2), (3, 4)]'::LINE, '41, 1, -9, 25.5']",
);

assertEquals(result.rows[0][0], [
{ a: "1", b: "-1", c: "1" },
{
a: "-0.49",
b: "-1",
c: "21.09",
},
]);
});

0 comments on commit 1140516

Please sign in to comment.