refactor(sqlite): use primary-key names for membership checks (#16122)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-02 22:25:19 +00:00
1 parent 83f9567739
commit ba601b28bc
1 file changed
+11 -24
+11 -24
View File
@@ -343,10 +343,7 @@ const readColumns = (
* projection. Only a selected key needs to be decoded.
*/
type RowKeyFacts = {
readonly primaryKey: readonly {
readonly name: string;
readonly position: number;
}[];
readonly primaryKeyColumns: readonly string[];
readonly rowidAlias: string | undefined;
};
@@ -446,7 +443,7 @@ const readRowKeyFacts = (
);
const withoutRowid = row !== undefined && row.wr !== 0 && row.wr !== 0n;
const shadowed = new Set<string>();
const primaryKey: { name: string; position: number }[] = [];
const primaryKeyColumns: string[] = [];
let declaredKey: Uint8Array | undefined;
for (const raw of db
.prepare(
@@ -472,10 +469,9 @@ const readRowKeyFacts = (
const pk = Number(value.pk);
if (pk <= 0) continue;
if (withoutRowid) {
primaryKey.push({
name: decodeSchemaText(value.name, encoding, "primary key column"),
position: pk,
});
primaryKeyColumns.push(
decodeSchemaText(value.name, encoding, "primary key column"),
);
} else if (pk === 1) {
declaredKey = value.name;
}
@@ -484,12 +480,7 @@ const readRowKeyFacts = (
? undefined
: (ROWID_ALIASES.find((spelling) => !shadowed.has(spelling)) ??
readDeclaredRowidAlias(db, name, declaredKey, encoding));
return {
primaryKey: primaryKey.sort(
(left, right) => left.position - right.position,
),
rowidAlias,
};
return { primaryKeyColumns, rowidAlias };
};
const readSchema = (
@@ -971,8 +962,8 @@ const readRowOrder = (
? { order: "unspecified", clause: "" }
: { order: "rowid", clause: ` ORDER BY ${quoteIdentifier(alias)}` };
}
const key = facts.primaryKey;
if (key.length === 0) return { order: "unspecified", clause: "" };
if (facts.primaryKeyColumns.length === 0)
return { order: "unspecified", clause: "" };
const primaryKey = readPrimaryKeyIndexName(db, table.name);
if (primaryKey === undefined) return { order: "unspecified", clause: "" };
// A WITHOUT ROWID key may repeat a column at different collations or
@@ -980,10 +971,7 @@ const readRowOrder = (
// valid — so the terms must follow the index's own sequence. Keying these
// by column name would collapse the repetition and advertise an order the
// actual key does not establish.
const ordered = new Map<
string,
{ readonly desc: boolean; readonly coll: string }
>();
const missingColumns = new Set(facts.primaryKeyColumns);
const terms: string[] = [];
for (const raw of db
.prepare(
@@ -996,7 +984,7 @@ const readRowOrder = (
desc: desc === 1n || desc === 1,
coll: decodeSchemaText(coll, encoding, "collation"),
};
ordered.set(column, detail);
missingColumns.delete(column);
// The declared collation keeps NOCASE/RTRIM keys in index order; spelling
// it explicitly also prevents a caller column's own collation from
// re-sorting.
@@ -1004,8 +992,7 @@ const readRowOrder = (
`${quoteIdentifier(column)} COLLATE ${quoteIdentifier(detail.coll)}${detail.desc ? " DESC" : ""}`,
);
}
for (const column of key)
if (!ordered.has(column.name)) return { order: "unspecified", clause: "" };
if (missingColumns.size > 0) return { order: "unspecified", clause: "" };
return { order: "primary_key", clause: ` ORDER BY ${terms.join(", ")}` };
};