From 8ee4d0eef6b6cee0caf991b06c585100b6f743a2 Mon Sep 17 00:00:00 2001 From: uint0 Date: Wed, 24 Jun 2020 16:24:52 +1000 Subject: [PATCH 1/3] Added skeleton to begin supporting oids --- decode.ts | 13 +++++++++++-- oid.ts | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/decode.ts b/decode.ts index 5012a3b6..63c520fa 100644 --- a/decode.ts +++ b/decode.ts @@ -1,4 +1,4 @@ -import { Oid } from "./oid.ts"; +import { Oid, runtimeOids } from "./oid.ts"; import { Column, Format } from "./connection.ts"; // Datetime parsing based on: @@ -177,6 +177,15 @@ function decodeByteaEscape(byteaStr: string): Uint8Array { const decoder = new TextDecoder(); +function handleDynamicOid(oid: number, strValue: string): any { + // TODO: handle this properly + if(runtimeOids.has(oid)) { + return strValue; + } else { + throw new Error(`Don't know how to parse column type: ${oid}`); + } +} + // deno-lint-ignore no-explicit-any function decodeText(value: Uint8Array, typeOid: number): any { const strValue = decoder.decode(value); @@ -231,7 +240,7 @@ function decodeText(value: Uint8Array, typeOid: number): any { case Oid.bytea: return decodeBytea(strValue); default: - throw new Error(`Don't know how to parse column type: ${typeOid}`); + return handleDynamicOid(typeOid, strValue); } } diff --git a/oid.ts b/oid.ts index 5bdbb8cd..3c7a4a99 100644 --- a/oid.ts +++ b/oid.ts @@ -1,3 +1,5 @@ +import { Client } from "./client.ts" + export const Oid = { bool: 16, bytea: 17, @@ -167,3 +169,16 @@ export const Oid = { regrole: 4096, _regrole: 4097, }; + +export const runtimeOids = new Set(); + +/** + * loadRuntimeOids + * Loads user defined oids at runtime + * @param handle database client that can be used to fetch enum ids + */ +export async function loadRuntimeOids(handle: Client) { + const result = await handle.query("SELECT DISTINCT typenumid FROM pg_enum"); + // TODO: error check + result.rows.forEach(oid => runtimeOids.add(oid)); +} \ No newline at end of file From 003c80ed32ba7937d12d68d301bb66cd8393afba Mon Sep 17 00:00:00 2001 From: uint0 Date: Wed, 24 Jun 2020 16:37:44 +1000 Subject: [PATCH 2/3] Fixed oid to point to correct column --- oid.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/oid.ts b/oid.ts index 3c7a4a99..1b348b54 100644 --- a/oid.ts +++ b/oid.ts @@ -178,7 +178,7 @@ export const runtimeOids = new Set(); * @param handle database client that can be used to fetch enum ids */ export async function loadRuntimeOids(handle: Client) { - const result = await handle.query("SELECT DISTINCT typenumid FROM pg_enum"); + const result = await handle.query("SELECT DISTINCT enumtypid FROM pg_enum"); // TODO: error check - result.rows.forEach(oid => runtimeOids.add(oid)); + result.rows.forEach(oid => runtimeOids.add(+oid[0])); } \ No newline at end of file From b3ee62240685dcb76491fb9ecc8763c98993dca8 Mon Sep 17 00:00:00 2001 From: uint0 Date: Wed, 24 Jun 2020 16:44:30 +1000 Subject: [PATCH 3/3] Added pool support to oid --- oid.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/oid.ts b/oid.ts index 1b348b54..097c4adb 100644 --- a/oid.ts +++ b/oid.ts @@ -1,4 +1,4 @@ -import { Client } from "./client.ts" +import { Client, PoolClient } from "./client.ts"; export const Oid = { bool: 16, @@ -177,7 +177,7 @@ export const runtimeOids = new Set(); * Loads user defined oids at runtime * @param handle database client that can be used to fetch enum ids */ -export async function loadRuntimeOids(handle: Client) { +export async function loadRuntimeOids(handle: Client | PoolClient) { const result = await handle.query("SELECT DISTINCT enumtypid FROM pg_enum"); // TODO: error check result.rows.forEach(oid => runtimeOids.add(+oid[0]));