Skip to content

Commit

Permalink
Merge pull request #167 from planetscale/binary-text
Browse files Browse the repository at this point in the history
Support binary text data
  • Loading branch information
ayrton authored Feb 9, 2024
2 parents 19a65a6 + 27fc879 commit 1a00a50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@ describe('cast', () => {
test('casts binary data to array of 8-bit unsigned integers', () => {
expect(cast({ name: 'test', type: 'BLOB' }, '')).toEqual(new Uint8Array([]))
expect(cast({ name: 'test', type: 'BLOB' }, 'Å')).toEqual(new Uint8Array([197]))
expect(cast({ name: 'test', type: 'VARBINARY' }, 'Å')).toEqual(new Uint8Array([197]))
})

test('casts binary text data to text', () => {
expect(cast({ name: 'test', type: 'VARBINARY', flags: 4225 }, 'table')).toEqual('table')
})

test('casts JSON string to JSON object', () => {
Expand Down
19 changes: 17 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,28 @@ export function cast(field: Field, value: string | null): any {
return value
case 'BLOB':
case 'BIT':
case 'VARBINARY':
case 'BINARY':
case 'GEOMETRY':
return uint8Array(value)
case 'BINARY':
case 'VARBINARY':
return isText(field) ? value : uint8Array(value)
case 'JSON':
return value ? JSON.parse(decode(value)) : value
default:
return decode(value)
}
}

enum Flags {
NONE = 0,
ISINTEGRAL = 256,
ISUNSIGNED = 512,
ISFLOAT = 1024,
ISQUOTED = 2048,
ISTEXT = 4096,
ISBINARY = 8192
}

function isText(field: Field): boolean {
return ((field.flags ?? 0) & Flags.ISTEXT) === Flags.ISTEXT
}

0 comments on commit 1a00a50

Please sign in to comment.