Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit cee791d

Browse files
authored
replaces returnsData with a natively supported function (#533)
1 parent 3dfd60b commit cee791d

File tree

1 file changed

+2
-16
lines changed

1 file changed

+2
-16
lines changed

packages/d1/src/api.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,8 @@ function normaliseResults(rows: any[]): any[] {
9292
);
9393
}
9494

95-
const DOESNT_RETURN_DATA_MESSAGE =
96-
"The columns() method is only for statements that return data";
9795
const EXECUTE_RETURNS_DATA_MESSAGE =
9896
"SQL execute error: Execute returned results - did you mean to call query?";
99-
function returnsData(stmt: SqliteStatement): boolean {
100-
try {
101-
stmt.columns();
102-
return true;
103-
} catch (e) {
104-
// `columns()` fails on statements that don't return data
105-
if (e instanceof TypeError && e.message === DOESNT_RETURN_DATA_MESSAGE) {
106-
return false;
107-
}
108-
throw e;
109-
}
110-
}
11197

11298
export class D1DatabaseAPI {
11399
constructor(private readonly db: SqliteDB) {}
@@ -119,7 +105,7 @@ export class D1DatabaseAPI {
119105
const stmt = this.db.prepare(sql);
120106
const params = normaliseParams(query.params);
121107
let results: any[];
122-
if (returnsData(stmt)) {
108+
if (stmt.reader) {
123109
results = stmt.all(params);
124110
} else {
125111
// `/query` does support queries that don't return data,
@@ -138,7 +124,7 @@ export class D1DatabaseAPI {
138124
const sql = splitSqlQuery(query.sql)[0];
139125
const stmt = this.db.prepare(sql);
140126
// `/execute` only supports queries that don't return data
141-
if (returnsData(stmt)) throw new Error(EXECUTE_RETURNS_DATA_MESSAGE);
127+
if (stmt.reader) throw new Error(EXECUTE_RETURNS_DATA_MESSAGE);
142128
const params = normaliseParams(query.params);
143129
const result = stmt.run(params);
144130
meta.last_row_id = Number(result.lastInsertRowid);

0 commit comments

Comments
 (0)