Skip to content

fix: allow "length" as a column name in result rows - #358

Open
cpruijsen wants to merge 1 commit into
tursodatabase:mainfrom
cpruijsen:fix/issue-234
Open

cpruijsen wants to merge 1 commit into
tursodatabase:mainfrom
cpruijsen:fix/issue-234

Conversation

@cpruijsen

Copy link
Copy Markdown

A query aliasing a column as length returns rows where that column is missing. rowFromSql in
packages/libsql-client-wasm/src/wasm.ts defines a non-enumerable length on the row object first,
so a column genuinely named length cannot be defined over it and the value is lost.

The row's own length exists so a row behaves array-like, which is worth keeping, but a real column
has the better claim on the name: the caller asked for it and there is no other way to read it, while
the array-like length is a convenience the column count already provides.

This moves the length definition after the column loop and only defines it when no column claimed
the name. Rows without such a column are unchanged.

Fixes #234

Result rows reserve a non-enumerable "length" property so that a row can
be indexed like an array. When a result column is named "length" (e.g.
SELECT COUNT(*) AS "length"), the column value was swallowed by that
property and rows appeared empty.

Define the array-like "length" only when no column claims the name, so
the column value takes precedence. For the hrana-based clients, rows
built by @libsql/hrana-client have a non-configurable "length"
property, so rebuild them in resultSetFromHrana when a "length" column
is present. ResultSet.toJSON() also iterated rows via row.length, which
is no longer reliable; iterate the real column count instead.

Fixes tursodatabase#234

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'length' keyword is not supported as an alias name on the client

1 participant