Skip to content

Commit 8bd06b9

Browse files
Detect arrays and extend metadata
1 parent 326c694 commit 8bd06b9

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

src/gateway/gateway.ts

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -258,22 +258,7 @@ async function queryAsync(connection: SQLiteCloudBunConnection, apiRequest: SqlA
258258
const data = apiRequest.row === 'dictionary' ? rowset : rowset.map(rowsetRow => rowsetRow.getData()) // rows as arrays by default
259259
return { data, metadata: rowset.metadata }
260260
} else {
261-
// detect that this array was sent in response to an insert, update or delete statement and will add special
262-
// metadata so it's easier for clients to extract useful information like the number of rows
263-
// affected by the statement. in the future, the server may produce a typed rowset instead
264-
if (Array.isArray(result) && result.length === 6) {
265-
const lowerSql = apiRequest.sql.toLocaleLowerCase()
266-
if (lowerSql.includes('insert ') || lowerSql.includes('update ') || lowerSql.includes('delete ')) {
267-
const metadata = {
268-
version: 1,
269-
numberOfRows: 1,
270-
numberOfColumns: 6,
271-
// https://github.com/sqlitecloud/sdk/blob/master/PROTOCOL.md#sqlite-statements
272-
columns: [{ name: 'TYPE' }, { name: 'INDEX' }, { name: 'ROWID' }, { name: 'CHANGES' }, { name: 'TOTAL_CHANGES' }, { name: 'FINALIZED' }]
273-
}
274-
return { data: [result], metadata }
275-
}
276-
}
261+
return generateMetadata(apiRequest.sql, result)
277262
}
278263
}
279264
} catch (error) {
@@ -295,6 +280,49 @@ async function queryAsync(connection: SQLiteCloudBunConnection, apiRequest: SqlA
295280
return { data: result }
296281
}
297282

283+
function generateMetadata(sql: string, result: any): ApiResponse {
284+
// detect that this array was sent in response to an insert, update or delete statement and will add special
285+
// metadata so it's easier for clients to extract useful information like the number of rows
286+
// affected by the statement. in the future, the server may produce a typed rowset instead
287+
if (Array.isArray(result) && result.length === 6) {
288+
const lowerSql = sql.toLocaleLowerCase()
289+
if (lowerSql.includes('insert ') || lowerSql.includes('update ') || lowerSql.includes('delete ')) {
290+
return {
291+
data: [result],
292+
metadata: {
293+
version: 1,
294+
numberOfRows: 1,
295+
numberOfColumns: 6,
296+
// https://github.com/sqlitecloud/sdk/blob/master/PROTOCOL.md#sqlite-statements
297+
columns: [
298+
{ name: 'TYPE', type: 'INTEGER' },
299+
{ name: 'INDEX', type: 'INTEGER' },
300+
{ name: 'ROWID', type: 'INTEGER' },
301+
{ name: 'CHANGES', type: 'INTEGER' },
302+
{ name: 'TOTAL_CHANGES', type: 'INTEGER' },
303+
{ name: 'FINALIZED', type: 'INTEGER' }
304+
]
305+
}
306+
}
307+
}
308+
}
309+
310+
// response is an array value but a special array like above
311+
if (Array.isArray(result)) {
312+
return {
313+
data: result.map(v => [v]),
314+
metadata: {
315+
version: 1,
316+
numberOfRows: result.length,
317+
numberOfColumns: 1,
318+
columns: [{ name: 'Result' }]
319+
}
320+
}
321+
}
322+
323+
return { data: result }
324+
}
325+
298326
/** Log only in verbose mode */
299327
function log(...args: unknown[]) {
300328
if (VERBOSE) {

0 commit comments

Comments
 (0)