From babfe085be1b6aebe2c5ccc48e03012fff11e478 Mon Sep 17 00:00:00 2001 From: David Graham Date: Fri, 29 Jul 2022 15:51:07 -0600 Subject: [PATCH 1/3] Fix null comparison --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 9744006..977160b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -180,7 +180,7 @@ function decodeRow(row: QueryResultRow) { function parseColumn(type: string, value: string) { // For empty strings, just return back a blank string. - if (value === '' || null) { + if (value === '' || value == null) { return '' } From 849bf27dc3aacca6713cbb4c02db691b5a4ba7e4 Mon Sep 17 00:00:00 2001 From: David Graham Date: Fri, 29 Jul 2022 16:10:44 -0600 Subject: [PATCH 2/3] Emit null values for SQL NULL results Don't convert null into the empty string so the caller receives the actual value from the database. --- src/index.ts | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index 977160b..2833f94 100644 --- a/src/index.ts +++ b/src/index.ts @@ -159,29 +159,22 @@ function parse(result: QueryResult): Row[] { return rows.map((row) => parseRow(fields, row)) } -function decodeRow(row: QueryResultRow) { +function decodeRow(row: QueryResultRow): Array { const values = atob(row.values) - const rv = [] let offset = 0 - for (let i = 0; i < row.lengths.length; i++) { - const ll = parseInt(row.lengths[i], 10) - // If the length is less than zero, it indicates a null value, so we should - // just call it an empty string and move on. - if (ll < 0) { - rv.push('') - continue - } - - rv.push(values.substring(offset, offset + ll)) - offset += ll - } - return rv + return row.lengths.map((size) => { + const width = parseInt(size, 10) + // Negative length indicates a null value. + if (width < 0) return null + const splice = values.substring(offset, offset + width) + offset += width + return splice + }) } -function parseColumn(type: string, value: string) { - // For empty strings, just return back a blank string. +function parseColumn(type: string, value: string | null): number | string { if (value === '' || value == null) { - return '' + return value } switch (type) { From be9adbc33cea889be6092eff2e28f2a4cd5beae0 Mon Sep 17 00:00:00 2001 From: David Graham Date: Fri, 29 Jul 2022 16:14:02 -0600 Subject: [PATCH 3/3] Add null to return type --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 2833f94..554e501 100644 --- a/src/index.ts +++ b/src/index.ts @@ -172,7 +172,7 @@ function decodeRow(row: QueryResultRow): Array { }) } -function parseColumn(type: string, value: string | null): number | string { +function parseColumn(type: string, value: string | null): number | string | null { if (value === '' || value == null) { return value }