diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 86ba22aa..ecd292c2 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -55,6 +55,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **An UPDATE that moved a PRIMARY KEY onto an existing key lost a row (directory storage).** `UPDATE t SET id = 1 WHERE id = 3` with a row 1 present was accepted: the primary-key index was re-pointed at the moved row and the original row 1 became unreachable. Every UPDATE path — `Table.UpdateAffectedCount` (SQL UPDATE), `UpdateMultipleCore` (`ExecuteBatchSQL`, the structured batch API) and `UpdateByPrimaryKey` — now validates new key values before any row is written, counting what earlier operations of a batch claim and release; a SET of the key matching more than one row is rejected too. The check starts with a test that allocates nothing and returns at once for an UPDATE that does not touch a key. Pinned by `PrimaryKeyUpdateTests` (columnar and page-based, in the session and after a reopen). Known issue §8; its single-file half stays with §11.
- **A UNIQUE constraint was never enforced; a UNIQUE index only partly.** A column declared `UNIQUE` (or `UNIQUE (col)`) accepted duplicates on every storage mode: the INSERT-time check was a stub. A UNIQUE *index* was checked only by a single-row INSERT into a columnar table — not on page-based tables, not by a multi-row INSERT, not by an UPDATE — and rejected a second NULL as a duplicate. A single-column UNIQUE now gets an automatic unique hash index on directory storage, as SQLite's `sqlite_autoindex_…` (registered at CREATE TABLE and on every open, so existing tables are covered; rows stored earlier never block an open). One validator covers INSERT, both batch critical sections and every UPDATE path; NULLs never conflict; and a table with a unique index keeps the batched — atomic — multi-row INSERT, which it used to give up for a per-row loop that left earlier rows behind. Pinned by `UniqueConstraintTests` (columnar, page-based) and `MultiRowInsertBatchingTests.CanUseBatchedInsert_IsTrue_WithUniqueSecondaryIndex_BecauseTheBatchCoreEnforcesIt`, which replaces the test that pinned the old per-row fallback. Known issue §9. Still open: single-file storage, whose persisted `ColumnFlags.Unique` is also written for every inline PRIMARY KEY column, so a reopened file cannot tell a declared UNIQUE from an EF Core composite key's column (the format decision of §10/§11; enforcing it was tried and backed out when `CompleteExampleIntegrationTests.CompositeKey_*` failed); and a composite `UNIQUE (a, b)` on INSERT. Suites: core **2392 / 0 failed (6 skipped: documented single-file cases)**, VectorSearch 255 / 0, EF Core 116 / 0, linq2DB 96 / 0, regression canaries 51 / 0; write-path gate: GATE PASSED on a NOISY box (WSearch and Defender need elevation), every metric ok, worst best-of-reps ratio 1.22x on raw UPDATE against a 1.50x limit while its median ratio was 0.60x (faster).
+- **Composite keys and multi-column UNIQUE constraints (directory storage).** A table-level `PRIMARY KEY (a, b)` was read as a column called `PRIMARY` — with `CONSTRAINT pk PRIMARY KEY (a, b)`, a column called `CONSTRAINT` — that became the primary key: always NULL, so the table held one row and every further INSERT was a key violation. `PRIMARY KEY` written on several columns, as EF Core's `EnsureCreated` declares a composite key, made the last column the whole key, so rows sharing it were refused. And a multi-column `UNIQUE (a, b)` was never checked. A composite key is now enforced as SQLite enforces it in a rowid table: a UNIQUE constraint over all its columns (the row id stays the storage key), checked on INSERT, multi-row INSERT and every UPDATE path, with a hash index on the constraint's first column narrowing the check. NULLs never conflict. `PRIMARY KEY (id)` on one column makes that column the key. A directory table created before keeps the key it was created with; re-create it to drop the false violations. Pinned by `CompositeKeyTests`. Known issues §9 and §11 (single-file storage is unchanged: owner decision).
+- **`ExecuteBatchSQL` executed some UPDATEs as DELETEs.** When the batch UPDATE fast path declined a statement — a quoted SET column such as `SET "n" = 5`, which linq2db and EF Core write — the DELETE classifier tried next accepted the canonical UPDATE shape and ran it as `DELETE … WHERE
= `: the batch deleted the rows it was asked to update (columnar and page-based, durable after reopen). A statement the scanner reads as an UPDATE is never a DELETE now, and the reverse. Pinned by `BatchUpdateClassificationTests`.
+- **An UPDATE's SET value can be an expression.** Every SET value was parsed as a literal of the column's type: `SET n = n + 5` failed with "Invalid value 'n + 5'", and `SET label = name` on a TEXT column stored the four letters `name`. A value that reads a column of the table or calls a function is computed per row from the row as it was before the statement (so `SET a = b, b = a` swaps), on directory and single-file storage and through `ExecuteBatchSQL`; constant arithmetic keeps its literal reading and is evaluated only when that fails (`SET n = 2 * 3`). Pinned by `UpdateSetExpressionTests`.
+- **A multi-row UPDATE that failed part-way kept the rows before the failure.** NOT NULL and CHECK were checked row by row as rows were written, so `UPDATE t SET n = 2 WHERE id > 0` failing a CHECK on its second row kept the first row changed. Every matched row is validated before the first is written now (SQLite's statement atomicity); the NOT NULL check reads through the SET list without allocating, and only a table with CHECK constraints builds the updated row. Pinned by `UpdateSetExpressionTests.Update_LiteralFailingAColumnCheckOnALaterRow_LeavesEveryRowUnchanged`.
+- **An UPDATE without WHERE threw "Invalid UPDATE syntax"**, and a SET literal containing the word `where` was cut at it. SQLite's `UPDATE t SET n = 0` changes every row; the SET list and WHERE clause are now split at the first WHERE outside quotes and parentheses.
+- **Table-level CHECK constraints rejected every row.** A table-level `CHECK (lo <= hi)` was stored with its keyword, failed to evaluate and so failed every INSERT. CHECK constraints are now evaluated with SQLite's semantics: a NULL result passes (`CHECK (price > 0)` accepts a row without a price — it failed before), `<>` is an operator, and functions and expressions work; syntax that evaluator does not cover keeps the previous evaluator. Pinned by `TableCheckConstraintTests`.
+- **ORDER BY on JOIN and subquery queries.** The JOIN path read only the first token of the first ORDER BY key — `ORDER BY LENGTH(p.name) DESC` sorted by nothing, `ORDER BY c.score * -1` sorted ascending by score — and ignored OFFSET; LIMIT 0 returned every row. The AST path (derived tables, IN-subqueries) read each key as a bare name, so `ORDER BY UPPER(name)` left "(name)" unparsed and sorted nothing, and its subquery executor read the first key only. Both now read the whole list — every key with its direction, expression keys — the JOIN path through the single-table path's `SqlOrderBy`, and the JOIN path honours `LIMIT n OFFSET m` and `LIMIT m, n`. Pinned by `JoinAndSubqueryOrderByTests`. Suites: core **2449/0 failed (6 skipped)**, VectorSearch 255/0, EF Core 116/0, linq2DB 96/0, regression canaries 51/0; write-path gate: GATE PASSED (worst best-of-reps ratio 1.24× on raw UPDATE, limit 1.50×).
+
## [2.1.4] - 2026-11-13
> The issue #497 follow-up: a quoted identifier in `CREATE TABLE` is catalogued without its quoting, so a schema
diff --git a/docs/sharpcoredb-known-issues.md b/docs/sharpcoredb-known-issues.md
index 09abf5ba..eb48754e 100644
--- a/docs/sharpcoredb-known-issues.md
+++ b/docs/sharpcoredb-known-issues.md
@@ -455,10 +455,11 @@ SELECT id FROM t;
> registered at CREATE TABLE and on every open, so existing tables are covered. INSERT, multi-row INSERT (now atomic on
> tables with a unique index) and every UPDATE path are checked; NULLs never conflict. On the way: a UNIQUE *index* was
> enforced only by a single-row INSERT into a columnar table, and a second NULL was rejected as a duplicate. Pinned by
-> `UniqueConstraintTests` (columnar and page-based). **Still open:** single-file storage (its persisted
-> `ColumnFlags.Unique` is also written for every inline PRIMARY KEY column, so a reopened file cannot tell a declared
-> UNIQUE from an EF Core composite key's column — the format decision of §10/§11; tried and backed out when
-> `CompleteExampleIntegrationTests.CompositeKey_*` failed) and a composite `UNIQUE (a, b)` on INSERT.
+> `UniqueConstraintTests` (columnar and page-based). A multi-column `UNIQUE (a, b)` is enforced too since
+> `fix/composite-keys-update-atomicity-orderby` (INSERT, multi-row INSERT and UPDATE; `CompositeKeyTests`). **Still
+> open:** single-file storage (its persisted `ColumnFlags.Unique` is also written for every inline PRIMARY KEY column, so
+> a reopened file cannot tell a declared UNIQUE from an EF Core composite key's column — the format decision of
+> §10/§11; tried and backed out when `CompleteExampleIntegrationTests.CompositeKey_*` failed).
Found 2026-11-17. A column declared `UNIQUE` accepts duplicates on both storage modes:
@@ -494,8 +495,15 @@ on-disk format extension. A `Locale(…)` collation would still need a name fiel
## 11. 🔴 OPEN (owner decision) — PRIMARY KEY uniqueness and composite keys
-> ⚠️ **STILL TRUE (verified 2026-11-17), pending an owner decision** on modelling composite keys. Single-file
-> enforcement was implemented and backed out in the same change because of the compatibility hazard below.
+> ⚠️ **STILL TRUE for single-file storage (verified 2026-11-17), pending an owner decision** on modelling composite
+> keys there. Single-file enforcement was implemented and backed out because of the compatibility hazard below.
+> **Directory storage is fixed for tables created from `fix/composite-keys-update-atomicity-orderby` on:** a composite
+> key — table-level `PRIMARY KEY (a, b)`, `CONSTRAINT name PRIMARY KEY (a, b)`, or `PRIMARY KEY` written on several
+> columns as EF Core's `EnsureCreated` writes it — is enforced as SQLite enforces it in a rowid table: a UNIQUE
+> constraint over all its columns, the row id staying the storage key (`CompositeKeyTests`). A directory table created
+> earlier keeps the key it was created with (its last key column; the other columns were never recorded) and needs to
+> be re-created to drop the false violations. The table-level form was worse than described below: it was read as a
+> column called `PRIMARY` (or `CONSTRAINT`) that became the key, always NULL, so such a table held one row.
Found 2026-11-17 while fixing single-file constraint enforcement.
@@ -532,6 +540,6 @@ single-file cases in `PrimaryKeyUniquenessTests`.
| 6 | INTEGER → Int32 | 🟡 Low | `BIGINT` columns | **Still the default** — opt in with `UseSqliteIntegerAffinity` |
| 7 | Bound text re-parsed as SQL | 🔴 High | Write through `ITable.Insert` instead of SQL text | **Fixed** — one `SqlLiteralScanner` (SQL/SQLite: a doubled quote is one quote, a backslash is data) behind every scanner; `ParseValue` strips one pair and un-collapses doubling; a tuple with too many values is rejected. `ParameterLiteralRoundTripTests` 74/74 |
| 8 | PK-changing UPDATE loses/duplicates a row | 🔴 High | — | **Fixed on directory storage** — every UPDATE path validates the new key first; single-file part tracked in §11 |
-| 9 | Column `UNIQUE` not enforced | 🟠 Medium | — | **Fixed on directory storage** — automatic unique index; single-file (format decision, §10/§11) and composite `UNIQUE (a, b)` still open |
+| 9 | Column `UNIQUE` not enforced | 🟠 Medium | — | **Fixed on directory storage** — automatic unique index, composite `UNIQUE (a, b)` included; single-file still open (format decision, §10/§11) |
| 10 | Single-file ignores `COLLATE` | 🟡 Low | — | **Open, owner decision** — needs a column-entry format extension (§10) |
-| 11 | PK uniqueness / composite keys | 🔴 High | — | **Open, owner decision** — single-file enforces no PK uniqueness; composite keys are modelled as their last column (§11) |
\ No newline at end of file
+| 11 | PK uniqueness / composite keys | 🔴 High | — | **Fixed on directory storage for new tables** (composite key = UNIQUE over its columns); **open, owner decision** for single-file storage, which enforces no PK uniqueness (§11) |
\ No newline at end of file
diff --git a/src/SharpCoreDB/DataStructures/Table.CRUD.cs b/src/SharpCoreDB/DataStructures/Table.CRUD.cs
index 4f49ba31..c4e3889f 100644
--- a/src/SharpCoreDB/DataStructures/Table.CRUD.cs
+++ b/src/SharpCoreDB/DataStructures/Table.CRUD.cs
@@ -1772,6 +1772,15 @@ public int UpdateAffectedCount(string? where, Dictionary updates
ValidateKeyChanges([(rows.ConvertAll(r => r.Row), updates)]);
}
+ // NOT NULL and CHECK are checked for every matched row before the first one is written, so a statement that
+ // fails on its third row leaves the first two unchanged — SQLite's statement atomicity. Up to 2.1.4 each row
+ // was validated as it was written and a failure kept the rows before it.
+ bool hasCheckConstraints = HasCheckConstraints();
+ foreach (var (_, row) in rows)
+ {
+ ValidateRowAfterUpdate(row, updates, hasCheckConstraints);
+ }
+
foreach (var (rowPos, row) in rows)
{
affected++;
@@ -1814,14 +1823,12 @@ private void UpdateSingleRow(Dictionary row, IStorageEngine engi
.Where(row.ContainsKey)
.ToDictionary(key => key, key => row[key]);
- // Apply updates to the row
+ // Apply updates to the row (validated by the caller, for every matched row, before any was written)
foreach (var update in updates)
{
row[update.Key] = update.Value;
}
- ValidateUpdatedRow(row);
-
if (StorageMode == StorageMode.Columnar)
{
UpdateColumnarRow(row, engine, updates, oldPkValue, oldHashKeys, rowPos, touchesIndex);
@@ -1876,6 +1883,60 @@ private bool UpdateTouchesAnyLoadedIndex(ICollection updatedColumns)
return false;
}
+ /// Returns true when the table has a column-level or table-level CHECK constraint. Allocates nothing.
+ private bool HasCheckConstraints()
+ {
+ if (this.TableCheckConstraints.Count > 0)
+ {
+ return true;
+ }
+
+ foreach (var expression in this.ColumnCheckExpressions) // NOSONAR:S3267 - write hot path: LINQ would allocate
+ {
+ if (expression is not null)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ /// Validates as would leave it, without changing it: NOT NULL is
+ /// read through the SET list and allocates nothing; only a table with CHECK constraints builds the updated row.
+ ///
+ private void ValidateRowAfterUpdate(Dictionary row, Dictionary updates, bool hasCheckConstraints)
+ {
+ for (int i = 0; i < this.Columns.Count && i < this.IsNotNull.Count; i++)
+ {
+ if (!this.IsNotNull[i])
+ {
+ continue;
+ }
+
+ var column = this.Columns[i];
+ var value = TryGetUpdateValue(updates, column, out var updated)
+ ? updated
+ : row.TryGetValue(column, out var current) ? current : null;
+ if (value is null or DBNull)
+ {
+ throw new InvalidOperationException($"Column '{column}' cannot be NULL");
+ }
+ }
+
+ if (hasCheckConstraints)
+ {
+ var updatedRow = new Dictionary(row, row.Comparer);
+ foreach (var (column, value) in updates)
+ {
+ updatedRow[column] = value;
+ }
+
+ ValidateUpdatedRow(updatedRow);
+ }
+ }
+
private void ValidateUpdatedRow(Dictionary row)
{
// ✅ NOT NULL validation for UPDATE
diff --git a/src/SharpCoreDB/DataStructures/Table.ComputedUpdate.cs b/src/SharpCoreDB/DataStructures/Table.ComputedUpdate.cs
new file mode 100644
index 00000000..6bd1ecc0
--- /dev/null
+++ b/src/SharpCoreDB/DataStructures/Table.ComputedUpdate.cs
@@ -0,0 +1,91 @@
+//
+// Copyright (c) 2026 MPCoreDeveloper. All rights reserved.
+// Licensed under the MIT License. See LICENSE file in the project root for full license information.
+//
+
+namespace SharpCoreDB.DataStructures;
+
+using System;
+using System.Collections.Generic;
+using SharpCoreDB.Storage.Hybrid;
+
+///
+/// Table partial — an UPDATE whose SET values are computed per row (SET n = n + 1, SET name = upper(name)).
+///
+/// Up to 2.1.4 a SET value was always parsed as a literal of the column's type, so SET n = n + 5 failed ("Invalid
+/// value 'n + 5'") and SET label = name on a TEXT column stored the text name. As in SQLite, every
+/// expression reads the row as it was before the statement, and every value is computed and every row validated before
+/// the first row is written, so an expression or constraint that fails leaves the table unchanged.
+///
+///
+public partial class Table
+{
+ ///
+ public int UpdateAffectedCount(
+ string? where,
+ Dictionary updates,
+ IReadOnlyList, object?>>> computed)
+ {
+ ArgumentNullException.ThrowIfNull(updates);
+ ArgumentNullException.ThrowIfNull(computed);
+ if (computed.Count == 0)
+ {
+ return UpdateAffectedCount(where, updates);
+ }
+
+ if (this.isReadOnly) throw new InvalidOperationException("Cannot update in readonly mode");
+
+ this.rwLock.EnterWriteLock();
+ try
+ {
+ var engine = GetOrCreateStorageEngine();
+ EnsureAllRegisteredIndexesLoaded();
+
+ var rows = ResolveUpdateRows(where);
+ var rowUpdates = new List>(rows.Count);
+ foreach (var (_, row) in rows)
+ {
+ var values = new Dictionary(updates);
+ foreach (var (column, compute) in computed)
+ {
+ values[column] = compute(row)!;
+ }
+
+ rowUpdates.Add(values);
+ }
+
+ if (rows.Count > 0 && UpdateTouchesKeyColumns(rowUpdates[0]))
+ {
+ var changes = new List<(IReadOnlyList> Rows, Dictionary Updates)>(rows.Count);
+ for (int i = 0; i < rows.Count; i++)
+ {
+ changes.Add(([rows[i].Row], rowUpdates[i]));
+ }
+
+ ValidateKeyChanges(changes);
+ }
+
+ bool hasCheckConstraints = HasCheckConstraints();
+ for (int i = 0; i < rows.Count; i++)
+ {
+ ValidateRowAfterUpdate(rows[i].Row, rowUpdates[i], hasCheckConstraints);
+ }
+
+ for (int i = 0; i < rows.Count; i++)
+ {
+ UpdateSingleRow(rows[i].Row, engine, rowUpdates[i], rows[i].Position);
+ }
+
+ if (StorageMode == StorageMode.Columnar)
+ {
+ TryAutoCompact();
+ }
+
+ return rows.Count;
+ }
+ finally
+ {
+ this.rwLock.ExitWriteLock();
+ }
+ }
+}
diff --git a/src/SharpCoreDB/DataStructures/Table.UniqueKeys.cs b/src/SharpCoreDB/DataStructures/Table.UniqueKeys.cs
index a72f9cc5..01eaee86 100644
--- a/src/SharpCoreDB/DataStructures/Table.UniqueKeys.cs
+++ b/src/SharpCoreDB/DataStructures/Table.UniqueKeys.cs
@@ -10,7 +10,7 @@ namespace SharpCoreDB.DataStructures;
using System.Globalization;
///
-/// Table partial — uniqueness of the PRIMARY KEY and of single-column UNIQUE constraints on every write path.
+/// Table partial — uniqueness of the PRIMARY KEY and of UNIQUE constraints (single- and multi-column) on every write path.
///
/// Up to 2.1.4 two guarantees were missing (known issues §8 and §9). An UPDATE that changed the primary key never
/// checked the new key: UPDATE t SET id = 1 WHERE id = 3 with a row 1 present was accepted, the primary-key
@@ -20,8 +20,10 @@ namespace SharpCoreDB.DataStructures;
///
///
/// A UNIQUE column now gets an automatic unique hash index, as SQLite gives it a sqlite_autoindex_…, so the
-/// check is a hash lookup. Every check starts with a test that allocates nothing and returns at once for a table or a
-/// statement without keys to protect, which keeps the measured write paths unchanged.
+/// check is a hash lookup. A multi-column UNIQUE (a, b) — a composite PRIMARY KEY is one — gets a hash index on its
+/// first column, which narrows the check to the rows sharing that value; up to 2.1.4 it was never checked at all. Every
+/// check starts with a test that allocates nothing and returns at once for a table or a statement without keys to
+/// protect, which keeps the measured write paths unchanged.
///
///
public partial class Table
@@ -37,19 +39,26 @@ internal void EnsureUniqueConstraintIndexes()
for (int i = 0; i < this.UniqueConstraints.Count; i++)
{
var constraint = this.UniqueConstraints[i];
- if (constraint.Count != 1)
+ var indexName = $"sqlite_autoindex_{this.Name}_{i + 1}";
+ if (constraint.Count == 0 || this.indexNameToColumn.ContainsKey(indexName))
{
continue;
}
- var column = this.Columns.Find(c => c.Equals(constraint[0], StringComparison.OrdinalIgnoreCase));
- if (column is null || (this.PrimaryKeyIndex >= 0 && column == this.Columns[this.PrimaryKeyIndex]))
+ if (constraint.Count > 1)
{
+ // A multi-column constraint: a non-unique index on its first column finds the candidate rows.
+ var columns = ResolveCompositeConstraint(constraint);
+ if (columns is not null)
+ {
+ CreateHashIndex(indexName, columns[0], isUnique: false);
+ }
+
continue;
}
- var indexName = $"sqlite_autoindex_{this.Name}_{i + 1}";
- if (this.indexNameToColumn.ContainsKey(indexName))
+ var column = this.Columns.Find(c => c.Equals(constraint[0], StringComparison.OrdinalIgnoreCase));
+ if (column is null || (this.PrimaryKeyIndex >= 0 && column == this.Columns[this.PrimaryKeyIndex]))
{
continue;
}
@@ -93,6 +102,36 @@ private bool UpdateTouchesKeyColumns(Dictionary updates)
}
}
+ foreach (var constraint in this.UniqueConstraints) // NOSONAR:S3267 - write hot path: LINQ would allocate
+ {
+ if (constraint.Count < 2)
+ {
+ continue;
+ }
+
+ foreach (var column in constraint) // NOSONAR:S3267 - write hot path: LINQ would allocate
+ {
+ if (TryGetUpdateValue(updates, column, out _))
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /// Returns true when the table has a multi-column UNIQUE constraint. Allocates nothing.
+ private bool HasCompositeUniqueConstraint()
+ {
+ foreach (var constraint in this.UniqueConstraints) // NOSONAR:S3267 - write hot path: LINQ would allocate
+ {
+ if (constraint.Count > 1)
+ {
+ return true;
+ }
+ }
+
return false;
}
@@ -103,7 +142,7 @@ private bool UpdateTouchesKeyColumns(Dictionary updates)
/// Validates one inserted row against the unique indexes. Returns at once without any.
private void ValidateUniqueValues(Dictionary row)
{
- if (HasUniqueIndex())
+ if (HasUniqueIndex() || HasCompositeUniqueConstraint())
{
ValidateUniqueValuesCore(row);
}
@@ -112,7 +151,7 @@ private void ValidateUniqueValues(Dictionary row)
/// Validates inserted rows (one statement) against the unique indexes and against each other.
private void ValidateUniqueValues(IReadOnlyList> rows)
{
- if (HasUniqueIndex())
+ if (HasUniqueIndex() || HasCompositeUniqueConstraint())
{
ValidateUniqueValuesCore(rows);
}
@@ -121,7 +160,7 @@ private void ValidateUniqueValues(IReadOnlyList> rows
/// Validates inserted rows in table column order (the batch parser's shape).
private void ValidateUniqueValues(object[][] rows)
{
- if (HasUniqueIndex())
+ if (HasUniqueIndex() || HasCompositeUniqueConstraint())
{
ValidateUniqueValuesCore(rows);
}
@@ -161,6 +200,31 @@ private void ValidateUniqueValues(int rowCount, Func value
}
}
}
+
+ foreach (var constraint in CompositeUniqueConstraints())
+ {
+ var seen = new HashSet(StringComparer.Ordinal);
+ var values = new Dictionary(StringComparer.OrdinalIgnoreCase);
+ for (int r = 0; r < rowCount; r++)
+ {
+ values.Clear();
+ foreach (var column in constraint)
+ {
+ values[column] = valueAt(r, column)!;
+ }
+
+ var key = CompositeKey(constraint, values);
+ if (key is null)
+ {
+ continue; // a NULL in any column never conflicts, as in SQLite
+ }
+
+ if (!seen.Add(key) || FindCompositeKeyHolders(constraint, values).Count > 0)
+ {
+ throw DuplicateCompositeValue(constraint, values);
+ }
+ }
+ }
}
///
@@ -190,6 +254,86 @@ private void ValidateKeyChanges(
(_, value) => hashIndex is not null && hashIndex.ContainsKey(value),
isPrimaryKey: false);
}
+
+ foreach (var constraint in CompositeUniqueConstraints())
+ {
+ ValidateCompositeKeyChanges(changes, constraint);
+ }
+ }
+
+ ///
+ /// Validates the changes to one multi-column UNIQUE constraint. The statement is judged by its end state: every
+ /// touched row's key after all its SET lists (a later change sees an earlier one) must be unique among the touched
+ /// rows, and a changed key may be held by no untouched row — a row the statement moves away from a key releases it.
+ ///
+ private void ValidateCompositeKeyChanges(
+ IReadOnlyList<(IReadOnlyList> Rows, Dictionary Updates)> changes,
+ List constraint)
+ {
+ var pkColumn = this.PrimaryKeyIndex >= 0 ? this.Columns[this.PrimaryKeyIndex] : null;
+ var finalValues = new Dictionary>(StringComparer.Ordinal);
+ var originalKeys = new Dictionary(StringComparer.Ordinal);
+ int anonymous = 0;
+ foreach (var (rows, updates) in changes)
+ {
+ if (!TouchesAnyColumn(updates, constraint))
+ {
+ continue;
+ }
+
+ foreach (var row in rows)
+ {
+ var identity = RowIdentity(row, pkColumn) ?? $"#{anonymous++}";
+ if (!finalValues.TryGetValue(identity, out var values))
+ {
+ values = new Dictionary(StringComparer.OrdinalIgnoreCase);
+ foreach (var column in constraint)
+ {
+ values[column] = row.TryGetValue(column, out var current) ? current : null!;
+ }
+
+ finalValues[identity] = values;
+ originalKeys[identity] = CompositeKey(constraint, values);
+ }
+
+ foreach (var column in constraint)
+ {
+ if (TryGetUpdateValue(updates, column, out var newValue))
+ {
+ values[column] = newValue!;
+ }
+ }
+ }
+ }
+
+ var claimed = new HashSet(StringComparer.Ordinal);
+ foreach (var (identity, values) in finalValues)
+ {
+ var key = CompositeKey(constraint, values);
+ if (key is null)
+ {
+ continue;
+ }
+
+ if (!claimed.Add(key))
+ {
+ throw DuplicateCompositeValue(constraint, values);
+ }
+
+ if (string.Equals(key, originalKeys[identity], StringComparison.Ordinal))
+ {
+ continue; // the row keeps its own key
+ }
+
+ foreach (var holder in FindCompositeKeyHolders(constraint, values))
+ {
+ var holderIdentity = RowIdentity(holder, pkColumn);
+ if (holderIdentity is null || !finalValues.ContainsKey(holderIdentity))
+ {
+ throw DuplicateCompositeValue(constraint, values);
+ }
+ }
+ }
}
private void ValidateKeyColumnChanges(
@@ -297,6 +441,123 @@ private List UniqueIndexedColumns()
return columns;
}
+ ///
+ /// The multi-column UNIQUE constraints, with their columns resolved to the table's own names. A constraint that
+ /// names a missing column, or includes the primary key (and so can never be violated), is left out.
+ ///
+ private List> CompositeUniqueConstraints()
+ {
+ var result = new List>();
+ foreach (var constraint in this.UniqueConstraints)
+ {
+ if (constraint.Count > 1 && ResolveCompositeConstraint(constraint) is { } columns)
+ {
+ result.Add(columns);
+ }
+ }
+
+ return result;
+ }
+
+ private List? ResolveCompositeConstraint(List constraint)
+ {
+ var columns = new List(constraint.Count);
+ foreach (var name in constraint)
+ {
+ var column = this.Columns.Find(c => c.Equals(name, StringComparison.OrdinalIgnoreCase));
+ if (column is null || IsPrimaryKeyColumn(column))
+ {
+ return null;
+ }
+
+ columns.Add(column);
+ }
+
+ return columns;
+ }
+
+ ///
+ /// The stored rows whose values in every column of equal ,
+ /// found through the hash index on the constraint's first column.
+ ///
+ private List> FindCompositeKeyHolders(List constraint, Dictionary values)
+ {
+ var holders = new List>();
+ var seed = constraint[0];
+ foreach (var candidate in this.registeredIndexes.ContainsKey(seed)
+ ? FindByIndex(seed, values[seed])
+ : SelectInternal(null, orderBy: null, asc: true, noEncrypt: false))
+ {
+ bool match = true;
+ foreach (var column in constraint)
+ {
+ candidate.TryGetValue(column, out var stored);
+ if (!CompositeComponentEquals(column, stored, values[column]))
+ {
+ match = false;
+ break;
+ }
+ }
+
+ if (match)
+ {
+ holders.Add(candidate);
+ }
+ }
+
+ return holders;
+ }
+
+ private bool CompositeComponentEquals(string column, object? stored, object? value) =>
+ stored is not null and not DBNull && value is not null and not DBNull &&
+ UniqueKeyComparer(column).Equals(UniqueKey(stored), UniqueKey(value));
+
+ /// The composite key of , or null when any of its columns is NULL.
+ private string? CompositeKey(List constraint, Dictionary values)
+ {
+ var key = new System.Text.StringBuilder();
+ foreach (var column in constraint)
+ {
+ if (!values.TryGetValue(column, out var value) || value is null or DBNull)
+ {
+ return null;
+ }
+
+ var text = UniqueKey(value);
+ if (ReferenceEquals(UniqueKeyComparer(column), StringComparer.OrdinalIgnoreCase))
+ {
+ text = text.ToUpperInvariant();
+ }
+
+ // Length-prefixed, so ("ab", "c") and ("a", "bc") stay distinct.
+ key.Append(text.Length).Append(':').Append(text);
+ }
+
+ return key.ToString();
+ }
+
+ private static bool TouchesAnyColumn(Dictionary updates, List columns)
+ {
+ foreach (var column in columns)
+ {
+ if (TryGetUpdateValue(updates, column, out _))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private static string? RowIdentity(Dictionary row, string? pkColumn) =>
+ pkColumn is not null && row.TryGetValue(pkColumn, out var pk) && pk is not null and not DBNull
+ ? UniqueKey(pk)
+ : null;
+
+ private static InvalidOperationException DuplicateCompositeValue(List constraint, Dictionary values) =>
+ new($"Duplicate key value ({string.Join(", ", constraint.ConvertAll(c => Convert.ToString(values[c], CultureInfo.InvariantCulture)))}) " +
+ $"violates unique constraint on columns ({string.Join(", ", constraint)})");
+
private bool IsPrimaryKeyColumn(string column) =>
this.PrimaryKeyIndex >= 0 &&
column.Equals(this.Columns[this.PrimaryKeyIndex], StringComparison.OrdinalIgnoreCase);
diff --git a/src/SharpCoreDB/Database/Execution/Database.Batch.cs b/src/SharpCoreDB/Database/Execution/Database.Batch.cs
index 332ec730..1dae2a2c 100644
--- a/src/SharpCoreDB/Database/Execution/Database.Batch.cs
+++ b/src/SharpCoreDB/Database/Execution/Database.Batch.cs
@@ -1099,8 +1099,10 @@ private bool TryParseUpdateForBatch(
operation = (string.Empty, null, null, null, null!);
// Phase-2 fast path: canonical single-column shape
- // `UPDATE
SET
= WHERE
= ` — regex-free.
- if (TryScanCanonicalDml(sql, out var fastTable, out var setCol, out var setValRaw, out var whereCol, out var whereValRaw))
+ // `UPDATE
SET
= WHERE
= ` — regex-free. A canonical DELETE has no SET
+ // column and is never an UPDATE.
+ if (TryScanCanonicalDml(sql, out var fastTable, out var setCol, out var setValRaw, out var whereCol, out var whereValRaw) &&
+ setCol.Length > 0)
{
if (!tables.TryGetValue(fastTable, out var fastTableMeta))
{
@@ -1108,8 +1110,9 @@ private bool TryParseUpdateForBatch(
}
int colIdx = fastTableMeta.Columns.IndexOf(setCol);
- if (colIdx < 0)
+ if (colIdx < 0 || SqlParser.IsComputedSetValue(setValRaw, fastTableMeta.Columns, out _))
{
+ // A computed SET value (`SET n = n + 1`) is evaluated per row by the shared parser's UPDATE path.
return false;
}
@@ -1178,7 +1181,7 @@ private bool TryParseUpdateForBatch(
var valStr = setPart[(eqIdx + 1)..].Trim();
var fallbackColIdx = table.Columns.IndexOf(colName);
- if (fallbackColIdx < 0) return false;
+ if (fallbackColIdx < 0 || SqlParser.IsComputedSetValue(valStr, table.Columns, out _)) return false;
var fallbackParsed = SqlParser.ParseValue(valStr, table.ColumnTypes[fallbackColIdx]);
fallbackUpdates[colName] = fallbackParsed ?? DBNull.Value;
@@ -1218,8 +1221,13 @@ private bool TryParseDeleteForBatch(string sql, out string tableName, out string
whereLiteral = null;
// Phase-2 fast path: canonical single-row shape
- // `DELETE FROM
WHERE
= ` — regex-free.
- if (TryScanCanonicalDml(sql, out var fastTable, out _, out _, out var whereCol, out var whereValRaw))
+ // `DELETE FROM
WHERE
= ` — regex-free. The scanner accepts the canonical UPDATE shape
+ // too, so a statement it read as an UPDATE (a SET column was found) is never a DELETE. Up to 2.1.4 this did not
+ // check, and an UPDATE the UPDATE classifier declined — a quoted SET column such as `SET "n" = 5`, which linq2db
+ // and EF Core write — was executed here as `DELETE ... WHERE
= `: the batch deleted the rows it
+ // was asked to update.
+ if (TryScanCanonicalDml(sql, out var fastTable, out var scannedSetColumn, out _, out var whereCol, out var whereValRaw) &&
+ scannedSetColumn.Length == 0)
{
if (!tables.ContainsKey(fastTable))
{
diff --git a/src/SharpCoreDB/Execution/SubqueryExecutor.cs b/src/SharpCoreDB/Execution/SubqueryExecutor.cs
index 06b9932a..8aceacf9 100644
--- a/src/SharpCoreDB/Execution/SubqueryExecutor.cs
+++ b/src/SharpCoreDB/Execution/SubqueryExecutor.cs
@@ -453,12 +453,18 @@ private List> ApplyOrderBy(
{
if (orderBy.Items.Count == 0) return rows;
- var firstItem = orderBy.Items[0];
- var columnName = firstItem.Column.ColumnName;
+ // Every key, each with its own direction, and an expression key evaluated per row. Up to 2.1.4 only the first key
+ // was read, as a column name, so `ORDER BY grp, name` and `ORDER BY LENGTH(name)` came back unsorted.
+ IOrderedEnumerable>? ordered = null;
+ foreach (var item in orderBy.Items)
+ {
+ var key = AstExecutor.OrderKeySelector(item);
+ ordered = ordered is null
+ ? item.IsAscending ? rows.OrderBy(key, SqlOrderBy.KeyComparer.Instance) : rows.OrderByDescending(key, SqlOrderBy.KeyComparer.Instance)
+ : item.IsAscending ? ordered.ThenBy(key, SqlOrderBy.KeyComparer.Instance) : ordered.ThenByDescending(key, SqlOrderBy.KeyComparer.Instance);
+ }
- return firstItem.IsAscending
- ? rows.OrderBy(r => r.TryGetValue(columnName, out var v) ? v : null).ToList()
- : rows.OrderByDescending(r => r.TryGetValue(columnName, out var v) ? v : null).ToList();
+ return [.. ordered!];
}
///
diff --git a/src/SharpCoreDB/Interfaces/ITable.cs b/src/SharpCoreDB/Interfaces/ITable.cs
index 3bacd592..00c0cc29 100644
--- a/src/SharpCoreDB/Interfaces/ITable.cs
+++ b/src/SharpCoreDB/Interfaces/ITable.cs
@@ -232,6 +232,24 @@ int UpdateAffectedCount(string? where, Dictionary updates)
return count;
}
+ ///
+ /// Updates rows matching with SET values that are computed per row
+ /// (SET n = n + 1) and returns the number of affected rows. Every function reads the row as it was before
+ /// the statement, and every value is computed before the first row is written. The default implementation throws
+ /// ; and implement it.
+ ///
+ /// The where clause string.
+ /// The SET values that are the same for every row.
+ /// The SET columns whose value is computed from the row, with the function computing it.
+ /// The number of affected rows.
+ int UpdateAffectedCount(
+ string? where,
+ Dictionary updates,
+ IReadOnlyList, object?>>> computed) =>
+ computed.Count == 0
+ ? UpdateAffectedCount(where, updates)
+ : throw new NotSupportedException($"{GetType().Name} does not support an UPDATE whose SET value is an expression.");
+
///
/// Deletes rows from the table.
///
diff --git a/src/SharpCoreDB/Services/EnhancedSqlParser.Select.cs b/src/SharpCoreDB/Services/EnhancedSqlParser.Select.cs
index 9f33beab..8dabb03e 100644
--- a/src/SharpCoreDB/Services/EnhancedSqlParser.Select.cs
+++ b/src/SharpCoreDB/Services/EnhancedSqlParser.Select.cs
@@ -981,6 +981,95 @@ private HavingNode ParseHaving()
return node;
}
+ /// Returns true when the text after the current position continues an expression (a call, an operator).
+ private bool ContinuesAsExpression()
+ {
+ int i = _position;
+ while (i < _sql.Length && char.IsWhiteSpace(_sql[i]))
+ {
+ i++;
+ }
+
+ return i < _sql.Length && _sql[i] is '(' or '+' or '-' or '*' or '/' or '%' or '|';
+ }
+
+ ///
+ /// Reads one ORDER BY key from as an expression, up to the next top-level comma or the end
+ /// of the ORDER BY list, with its ASC/DESC.
+ ///
+ private bool TryParseExpressionOrderItem(int itemStart, out OrderByItem item)
+ {
+ item = null!;
+ int end = FindOrderItemEnd(itemStart);
+ var text = _sql[itemStart..end].Trim();
+ bool ascending = true;
+ var direction = Regex.Match(text, @"\s+(ASC|DESC)$", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
+ if (direction.Success)
+ {
+ ascending = direction.Groups[1].Value.Equals("ASC", StringComparison.OrdinalIgnoreCase);
+ text = text[..direction.Index].TrimEnd();
+ }
+
+ if (text.Length == 0 || !SqlScalarExpression.TryParse(text, out _))
+ {
+ return false;
+ }
+
+ item = new OrderByItem
+ {
+ Column = new ColumnReferenceNode { ColumnName = text },
+ Expression = text,
+ IsAscending = ascending,
+ };
+ _position = end;
+ return true;
+ }
+
+ /// The end of the ORDER BY key starting at : a top-level comma, a closing parenthesis
+ /// that ends a subquery, LIMIT/OFFSET/a set operator, a semicolon or the end of the statement.
+ private int FindOrderItemEnd(int start)
+ {
+ int depth = 0;
+ for (int i = start; i < _sql.Length; i++)
+ {
+ char c = _sql[i];
+ if (c is '\'' or '"' or '`' or '[')
+ {
+ char close = c == '[' ? ']' : c;
+ for (i++; i < _sql.Length && _sql[i] != close; i++)
+ {
+ }
+
+ continue;
+ }
+
+ if (c == '(')
+ {
+ depth++;
+ }
+ else if (c == ')')
+ {
+ if (depth == 0)
+ {
+ return i;
+ }
+
+ depth--;
+ }
+ else if (depth == 0 && (c is ',' or ';'))
+ {
+ return i;
+ }
+ else if (depth == 0 && char.IsWhiteSpace(c) &&
+ Regex.IsMatch(_sql[(i + 1)..], @"^(LIMIT|OFFSET|UNION|INTERSECT|EXCEPT)\b", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1)))
+ {
+ return i;
+ }
+ }
+
+ return _sql.Length;
+ }
+
private OrderByNode ParseOrderBy()
{
var node = new OrderByNode { Position = _position };
@@ -1005,9 +1094,18 @@ private OrderByNode ParseOrderBy()
}
else
{
+ int itemStart = _position;
var tableAlias = ConsumeIdentifier();
if (tableAlias is null)
+ {
+ if (TryParseExpressionOrderItem(itemStart, out var startsWithExpression))
+ {
+ node.Items.Add(startsWithExpression);
+ continue;
+ }
+
break;
+ }
string columnName;
if (MatchToken("."))
@@ -1020,6 +1118,15 @@ private OrderByNode ParseOrderBy()
tableAlias = null;
}
+ // A key that continues past the column name — LENGTH(name), price * qty — is an expression. Up to
+ // 2.1.4 only the name was read: `ORDER BY LENGTH(name)` left "(name)" as unparsed trailing content and
+ // the rows came back unsorted.
+ if (ContinuesAsExpression() && TryParseExpressionOrderItem(itemStart, out var expressionItem))
+ {
+ node.Items.Add(expressionItem);
+ continue;
+ }
+
var item = new OrderByItem
{
Column = new ColumnReferenceNode { TableAlias = tableAlias, ColumnName = columnName },
diff --git a/src/SharpCoreDB/Services/SqlAst.Nodes.cs b/src/SharpCoreDB/Services/SqlAst.Nodes.cs
index b8966153..6326333f 100644
--- a/src/SharpCoreDB/Services/SqlAst.Nodes.cs
+++ b/src/SharpCoreDB/Services/SqlAst.Nodes.cs
@@ -388,6 +388,12 @@ public class OrderByItem
/// Null when ordering by column name.
///
public int? OrdinalPosition { get; set; }
+
+ ///
+ /// Gets or sets the key's expression text when the key is an expression rather than a column
+ /// (ORDER BY LENGTH(name), ORDER BY price * qty); null for a column or an ordinal.
+ ///
+ public string? Expression { get; set; }
}
///
diff --git a/src/SharpCoreDB/Services/SqlOrderBy.cs b/src/SharpCoreDB/Services/SqlOrderBy.cs
index b2473b05..988157f9 100644
--- a/src/SharpCoreDB/Services/SqlOrderBy.cs
+++ b/src/SharpCoreDB/Services/SqlOrderBy.cs
@@ -24,6 +24,28 @@ namespace SharpCoreDB.Services;
///
internal static class SqlOrderBy
{
+ ///
+ /// Compares sort keys as SQLite does: NULL first, numbers and text by .
+ ///
+ internal sealed class KeyComparer : IComparer