diff --git a/src/sqlite/SqliteDatabaseInspection.ts b/src/sqlite/SqliteDatabaseInspection.ts index fcd0da32a9a..8e4e22634a9 100644 --- a/src/sqlite/SqliteDatabaseInspection.ts +++ b/src/sqlite/SqliteDatabaseInspection.ts @@ -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(); - 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(", ")}` }; };