Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bool and bytea array types #189

Merged
merged 2 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ function decodeByteaEscape(byteaStr: string): Uint8Array {
return new Uint8Array(bytes);
}

function decodeByteaArray(value: string): unknown[] {
return parseArray(value, decodeBytea);
}

const decoder = new TextDecoder();

// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -240,6 +244,8 @@ function decodeText(value: Uint8Array, typeOid: number): any {
return decodeStringArray(strValue);
case Oid.bool:
return strValue[0] === "t";
case Oid._bool:
return parseArray(strValue, (x) => x[0] === "t");
case Oid.int2:
case Oid.int4:
return decodeBaseTenInt(strValue);
Expand All @@ -262,6 +268,8 @@ function decodeText(value: Uint8Array, typeOid: number): any {
return decodeJsonArray(strValue);
case Oid.bytea:
return decodeBytea(strValue);
case Oid._bytea:
return decodeByteaArray(strValue);
default:
throw new Error(`Don't know how to parse column type: ${typeOid}`);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,37 @@ testClient(async function jsonArray() {
],
);
});

testClient(async function bool() {
const result = await CLIENT.query(
`SELECT bool('y')`,
);
assertEquals(result.rows[0][0], true);
});

testClient(async function _bool() {
const result = await CLIENT.query(
`SELECT array[bool('y'), bool('n'), bool('1'), bool('0')]`,
);
assertEquals(result.rows[0][0], [true, false, true, false]);
});

testClient(async function bytea() {
const result = await CLIENT.query(
`SELECT decode('MTIzAAE=','base64')`,
);
assertEquals(result.rows[0][0], new Uint8Array([49, 50, 51, 0, 1]));
});

testClient(async function _bytea() {
const result = await CLIENT.query(
`SELECT array[ decode('MTIzAAE=','base64'), decode('MAZzBtf=', 'base64') ]`,
);
assertEquals(
result.rows[0][0],
[
new Uint8Array([49, 50, 51, 0, 1]),
new Uint8Array([48, 6, 115, 6, 215]),
],
);
});