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..097c4adb 100644 --- a/oid.ts +++ b/oid.ts @@ -1,3 +1,5 @@ +import { Client, PoolClient } 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 | PoolClient) { + const result = await handle.query("SELECT DISTINCT enumtypid FROM pg_enum"); + // TODO: error check + result.rows.forEach(oid => runtimeOids.add(+oid[0])); +} \ No newline at end of file