Skip to content

Commit 375c74e

Browse files
committed
fix: Check for empty row in mysql2 driver
This is a port of the fix in sqlc-dev#18 but for the mysql2 driver.
1 parent 395a0ba commit 375c74e

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

examples/bun-mysql2/src/db/query_sql.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export async function getAuthor(client: Client, args: GetAuthorArgs): Promise<Ge
2828
return null;
2929
}
3030
const row = rows[0];
31+
if (!row) {
32+
return null;
33+
}
3134
return {
3235
id: row[0],
3336
name: row[1],
@@ -168,6 +171,9 @@ export async function test(client: Client): Promise<TestRow | null> {
168171
return null;
169172
}
170173
const row = rows[0];
174+
if (!row) {
175+
return null;
176+
}
171177
return {
172178
cBit: row[0],
173179
cTinyint: row[1],

examples/node-mysql2/src/db/query_sql.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export async function getAuthor(client: Client, args: GetAuthorArgs): Promise<Ge
2828
return null;
2929
}
3030
const row = rows[0];
31+
if (!row) {
32+
return null;
33+
}
3134
return {
3235
id: row[0],
3336
name: row[1],
@@ -168,6 +171,9 @@ export async function test(client: Client): Promise<TestRow | null> {
168171
return null;
169172
}
170173
const row = rows[0];
174+
if (!row) {
175+
return null;
176+
}
171177
return {
172178
cBit: row[0],
173179
cTinyint: row[1],

src/drivers/mysql2.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,17 @@ export class Driver {
627627
NodeFlags.TypeExcludesFlags
628628
)
629629
),
630+
factory.createIfStatement(
631+
factory.createPrefixUnaryExpression(
632+
SyntaxKind.ExclamationToken,
633+
factory.createIdentifier("row")
634+
),
635+
factory.createBlock(
636+
[factory.createReturnStatement(factory.createNull())],
637+
true
638+
),
639+
undefined
640+
),
630641
factory.createReturnStatement(
631642
factory.createObjectLiteralExpression(
632643
columns.map((col, i) =>

0 commit comments

Comments
 (0)