From 734f588dca9cf12c8d01e69fb0e383ddb9b11ca9 Mon Sep 17 00:00:00 2001 From: Nicolas Guerrero Date: Mon, 5 Oct 2020 13:50:32 -0500 Subject: [PATCH 1/3] Add json array type --- decode.ts | 7 +++++++ oid.ts | 2 +- tests/data_types.ts | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/decode.ts b/decode.ts index 192495ca..b9b9e81d 100644 --- a/decode.ts +++ b/decode.ts @@ -195,6 +195,11 @@ function decodeIntArray(value: string): any { return parseArray(value, decodeBaseTenInt); } +// deno-lint-ignore no-explicit-any +function decodeJsonArray(value: any): unknown[] { + return parseArray(value, JSON.parse); +} + // deno-lint-ignore no-explicit-any function decodeText(value: Uint8Array, typeOid: number): any { const strValue = decoder.decode(value); @@ -253,6 +258,8 @@ function decodeText(value: Uint8Array, typeOid: number): any { case Oid.json: case Oid.jsonb: return JSON.parse(strValue); + case Oid.json_array: + return decodeJsonArray(strValue); case Oid.bytea: return decodeBytea(strValue); default: diff --git a/oid.ts b/oid.ts index 5bdbb8cd..12b244ad 100644 --- a/oid.ts +++ b/oid.ts @@ -23,7 +23,7 @@ export const Oid = { xml: 142, _xml: 143, pg_node_tree: 194, - _json: 199, + json_array: 199, smgr: 210, index_am_handler: 325, point: 600, diff --git a/tests/data_types.ts b/tests/data_types.ts index 46feb2a1..e4a22008 100644 --- a/tests/data_types.ts +++ b/tests/data_types.ts @@ -267,3 +267,37 @@ testClient(async function bpcharNestedArray() { ); assertEquals(result.rows[0], [[["AB1234"], ["4321BA"]]]); }); + +testClient(async function jsonArray() { + const json_array = await CLIENT.query( + `SELECT ARRAY_AGG(A) FROM ( + SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A + UNION ALL + SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A + ) A` + ); + + assertEquals(json_array.rows[0][0], [{X: '1'}, {Y: '2'}]); + + const json_array_nested = await CLIENT.query( + `SELECT ARRAY[ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)], ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)]] FROM ( + SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A + UNION ALL + SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A + ) A` + ); + + assertEquals( + json_array_nested.rows[0][0], + [ + [ + [{X: '1'}, {Y: '2'}], + [{X: '1'}, {Y: '2'}], + ], + [ + [{X: '1'}, {Y: '2'}], + [{X: '1'}, {Y: '2'}], + ], + ], + ); +}); \ No newline at end of file From 769808f089c575345743d22d980e7c1d122acb79 Mon Sep 17 00:00:00 2001 From: Nicolas Guerrero Date: Mon, 5 Oct 2020 14:47:54 -0500 Subject: [PATCH 2/3] Add jsonb equivalence --- decode.ts | 1 + oid.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/decode.ts b/decode.ts index b9b9e81d..44b1a6cc 100644 --- a/decode.ts +++ b/decode.ts @@ -259,6 +259,7 @@ function decodeText(value: Uint8Array, typeOid: number): any { case Oid.jsonb: return JSON.parse(strValue); case Oid.json_array: + case Oid.jsonb_array: return decodeJsonArray(strValue); case Oid.bytea: return decodeBytea(strValue); diff --git a/oid.ts b/oid.ts index 12b244ad..300407d8 100644 --- a/oid.ts +++ b/oid.ts @@ -146,7 +146,7 @@ export const Oid = { regdictionary: 3769, _regdictionary: 3770, jsonb: 3802, - _jsonb: 3807, + jsonb_array: 3807, anyrange: 3831, event_trigger: 3838, int4range: 3904, From 2e419905aacc36d6b62ccdb4decfc03ddbff088e Mon Sep 17 00:00:00 2001 From: Nicolas Guerrero Date: Mon, 5 Oct 2020 14:51:38 -0500 Subject: [PATCH 3/3] Cleanup and fmt --- connection.ts | 2 +- decode.ts | 3 +-- deps.ts | 5 +---- tests/data_types.ts | 16 ++++++++-------- tests/pool.ts | 7 ++----- tests/utils.ts | 2 +- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/connection.ts b/connection.ts index 52c6d492..c571aad2 100644 --- a/connection.ts +++ b/connection.ts @@ -30,7 +30,7 @@ import { BufReader, BufWriter } from "./deps.ts"; import { PacketWriter } from "./packet_writer.ts"; import { hashMd5Password, readUInt32BE } from "./utils.ts"; import { PacketReader } from "./packet_reader.ts"; -import { QueryConfig, QueryResult, Query } from "./query.ts"; +import { Query, QueryConfig, QueryResult } from "./query.ts"; import { parseError } from "./error.ts"; import type { ConnectionParams } from "./connection_params.ts"; import { DeferredStack } from "./deferred.ts"; diff --git a/decode.ts b/decode.ts index 44b1a6cc..3e32098f 100644 --- a/decode.ts +++ b/decode.ts @@ -195,8 +195,7 @@ function decodeIntArray(value: string): any { return parseArray(value, decodeBaseTenInt); } -// deno-lint-ignore no-explicit-any -function decodeJsonArray(value: any): unknown[] { +function decodeJsonArray(value: string): unknown[] { return parseArray(value, JSON.parse); } diff --git a/deps.ts b/deps.ts index fbd99e49..fdb5cc5b 100644 --- a/deps.ts +++ b/deps.ts @@ -1,7 +1,4 @@ -export { - BufReader, - BufWriter, -} from "https://deno.land/std@0.69.0/io/bufio.ts"; +export { BufReader, BufWriter } from "https://deno.land/std@0.69.0/io/bufio.ts"; export { copyBytes } from "https://deno.land/std@0.67.0/bytes/mod.ts"; export { deferred } from "https://deno.land/std@0.69.0/async/deferred.ts"; export type { Deferred } from "https://deno.land/std@0.69.0/async/deferred.ts"; diff --git a/tests/data_types.ts b/tests/data_types.ts index e4a22008..e30a185b 100644 --- a/tests/data_types.ts +++ b/tests/data_types.ts @@ -274,30 +274,30 @@ testClient(async function jsonArray() { SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A UNION ALL SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A - ) A` + ) A`, ); - assertEquals(json_array.rows[0][0], [{X: '1'}, {Y: '2'}]); + assertEquals(json_array.rows[0][0], [{ X: "1" }, { Y: "2" }]); const json_array_nested = await CLIENT.query( `SELECT ARRAY[ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)], ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)]] FROM ( SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A UNION ALL SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A - ) A` + ) A`, ); assertEquals( json_array_nested.rows[0][0], [ [ - [{X: '1'}, {Y: '2'}], - [{X: '1'}, {Y: '2'}], + [{ X: "1" }, { Y: "2" }], + [{ X: "1" }, { Y: "2" }], ], [ - [{X: '1'}, {Y: '2'}], - [{X: '1'}, {Y: '2'}], + [{ X: "1" }, { Y: "2" }], + [{ X: "1" }, { Y: "2" }], ], ], ); -}); \ No newline at end of file +}); diff --git a/tests/pool.ts b/tests/pool.ts index 09df7959..dd941b76 100644 --- a/tests/pool.ts +++ b/tests/pool.ts @@ -1,10 +1,7 @@ -import { - assertEquals, - assertThrowsAsync, -} from "../test_deps.ts"; +import { assertEquals, assertThrowsAsync } from "../test_deps.ts"; import { Pool } from "../pool.ts"; import { delay } from "../utils.ts"; -import { TEST_CONNECTION_PARAMS, DEFAULT_SETUP } from "./constants.ts"; +import { DEFAULT_SETUP, TEST_CONNECTION_PARAMS } from "./constants.ts"; async function testPool( t: (pool: Pool) => void | Promise, diff --git a/tests/utils.ts b/tests/utils.ts index 2b022f4b..d0fd81a1 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -1,6 +1,6 @@ const { test } = Deno; import { assertEquals } from "../test_deps.ts"; -import { parseDsn, DsnResult } from "../utils.ts"; +import { DsnResult, parseDsn } from "../utils.ts"; test("testParseDsn", function () { let c: DsnResult;