Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/libsql-client-wasm/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,6 @@ function rowFromSql(
intMode: IntMode,
): Row {
const row = {};
// make sure that the "length" property is not enumerable
Object.defineProperty(row, "length", { value: sqlRow.length });
for (let i = 0; i < sqlRow.length; ++i) {
const value = valueFromSql(sqlRow[i], intMode);
Object.defineProperty(row, i, { value });
Expand All @@ -411,6 +409,11 @@ function rowFromSql(
});
}
}
// make sure that the "length" property is not enumerable, unless a column
// is named "length", in which case the column value takes precedence
if (!Object.hasOwn(row, "length")) {
Object.defineProperty(row, "length", { value: sqlRow.length });
}
return row as Row;
}

Expand Down
28 changes: 28 additions & 0 deletions packages/libsql-client/src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ describe("execute()", () => {
}),
);

test(
'column named "length"',
withClient(async (c) => {
const rs = await c.execute("SELECT 1 AS one, 'two' AS \"length\"");
expect(rs.columns).toStrictEqual(["one", "length"]);
expect(rs.rows.length).toStrictEqual(1);

const r = rs.rows[0];
expect(r[0]).toStrictEqual(1);
expect(r[1]).toStrictEqual("two");
expect(r["length"]).toStrictEqual("two");
expect(Object.entries(r)).toStrictEqual([
["one", 1],
["length", "two"],
]);
}),
);

test(
"statement that produces error",
withClient(async (c) => {
Expand Down Expand Up @@ -994,6 +1012,16 @@ describe("ResultSet.toJSON()", () => {
}),
);

test(
'column named "length"',
withClient(async (c) => {
const rs = await c.execute("SELECT 1 AS one, 'two' AS \"length\"");
const json = rs.toJSON();
expect(json["columns"]).toStrictEqual(["one", "length"]);
expect(json["rows"]).toStrictEqual([[1, "two"]]);
}),
);

(hasHrana2 ? test : test.skip)(
"row values",
withClient(async (c) => {
Expand Down
38 changes: 37 additions & 1 deletion packages/libsql-client/src/hrana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as hrana from "@libsql/hrana-client";
import type {
InStatement,
ResultSet,
Row,
Transaction,
TransactionMode,
InArgs,
Expand Down Expand Up @@ -384,7 +385,15 @@ export function stmtToHrana(stmt: InStatement | [string, InArgs?]): hrana.Stmt {
export function resultSetFromHrana(hranaRows: hrana.RowsResult): ResultSet {
const columns = hranaRows.columnNames.map((c) => c ?? "");
const columnTypes = hranaRows.columnDecltypes.map((c) => c ?? "");
const rows = hranaRows.rows;
let rows: Array<Row> = hranaRows.rows;
if (columns.includes("length")) {
// Rows produced by @libsql/hrana-client have a non-configurable
// "length" property which hides a column named "length", so the rows
// are rebuilt to make the column value take precedence.
rows = hranaRows.rows.map((hranaRow) =>
rowFromHrana(hranaRow, hranaRows.columnNames),
);
}
const rowsAffected = hranaRows.affectedRowCount;
const lastInsertRowid =
hranaRows.lastInsertRowid !== undefined
Expand All @@ -399,6 +408,33 @@ export function resultSetFromHrana(hranaRows: hrana.RowsResult): ResultSet {
);
}

function rowFromHrana(
hranaRow: hrana.Row,
colNames: Array<string | undefined>,
): Row {
const row = {};
for (let i = 0; i < colNames.length; ++i) {
const value = hranaRow[i];
Object.defineProperty(row, i, { value });

const colName = colNames[i];
if (colName !== undefined && !Object.hasOwn(row, colName)) {
Object.defineProperty(row, colName, {
value,
enumerable: true,
configurable: true,
writable: true,
});
}
}
// make sure that the "length" property is not enumerable, unless a column
// is named "length", in which case the column value takes precedence
if (!Object.hasOwn(row, "length")) {
Object.defineProperty(row, "length", { value: colNames.length });
}
return row as Row;
}

export function mapHranaError(e: unknown): unknown {
if (e instanceof hrana.ClientError) {
const code = mapHranaErrorCode(e);
Expand Down
7 changes: 5 additions & 2 deletions packages/libsql-client/src/sqlite3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,6 @@ function rowFromSql(
intMode: IntMode,
): Row {
const row = {};
// make sure that the "length" property is not enumerable
Object.defineProperty(row, "length", { value: sqlRow.length });
for (let i = 0; i < sqlRow.length; ++i) {
const value = valueFromSql(sqlRow[i], intMode);
Object.defineProperty(row, i, { value });
Expand All @@ -740,6 +738,11 @@ function rowFromSql(
});
}
}
// make sure that the "length" property is not enumerable, unless a column
// is named "length", in which case the column value takes precedence
if (!Object.hasOwn(row, "length")) {
Object.defineProperty(row, "length", { value: sqlRow.length });
}
return row as Row;
}

Expand Down
12 changes: 9 additions & 3 deletions packages/libsql-core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ResultSetImpl implements ResultSet {
return {
columns: this.columns,
columnTypes: this.columnTypes,
rows: this.rows.map(rowToJson),
rows: this.rows.map((row) => rowToJson(row, this.columns.length)),
rowsAffected: this.rowsAffected,
lastInsertRowid:
this.lastInsertRowid !== undefined
Expand All @@ -60,8 +60,14 @@ export class ResultSetImpl implements ResultSet {
}
}

function rowToJson(row: Row): unknown {
return Array.prototype.map.call(row, valueToJson);
function rowToJson(row: Row, columnCount: number): unknown {
// a column may be named "length", in which case `row.length` is the
// column value rather than the number of columns
const values: Array<unknown> = [];
for (let i = 0; i < columnCount; ++i) {
values.push(valueToJson(row[i]));
}
return values;
}

function valueToJson(value: Value): unknown {
Expand Down