From d2b59e0c2925a1789f67fd3ca3c062f423528ddc Mon Sep 17 00:00:00 2001 From: Andrew Tierney Date: Tue, 17 Nov 2026 09:13:49 +1100 Subject: [PATCH] fix: composite keys, computed and atomic UPDATE, batch UPDATE-as-DELETE, CHECK and JOIN/subquery ORDER BY - Composite PRIMARY KEY and multi-column UNIQUE (directory storage): table-level PRIMARY KEY (a, b) was read as a phantom column PRIMARY/CONSTRAINT that became an always-NULL key; PRIMARY KEY on several columns (EF Core EnsureCreated) kept only the last one; UNIQUE (a, b) was never checked. A composite key is now a UNIQUE constraint over its columns with the row id as storage key, enforced on INSERT, multi-row INSERT and every UPDATE path. - ExecuteBatchSQL ran an UPDATE its fast path declined (e.g. a quoted SET column) as a DELETE of the same WHERE. - UPDATE SET values that read a column or call a function are computed per row from the pre-statement row (directory, single-file, batch); every matched row is validated before the first write (statement atomicity). - UPDATE without WHERE is accepted; a SET literal containing "where" is no longer cut. - CHECK: table-level constraints no longer reject every row; NULL passes; <> and functions work. - ORDER BY on the JOIN and AST/subquery paths reads every key and expression keys; JOIN honours LIMIT/OFFSET. --- docs/CHANGELOG.md | 8 + docs/sharpcoredb-known-issues.md | 24 +- src/SharpCoreDB/DataStructures/Table.CRUD.cs | 67 ++++- .../DataStructures/Table.ComputedUpdate.cs | 91 ++++++ .../DataStructures/Table.UniqueKeys.cs | 283 +++++++++++++++++- .../Database/Execution/Database.Batch.cs | 20 +- src/SharpCoreDB/Execution/SubqueryExecutor.cs | 16 +- src/SharpCoreDB/Interfaces/ITable.cs | 18 ++ .../Services/EnhancedSqlParser.Select.cs | 107 +++++++ src/SharpCoreDB/Services/SqlAst.Nodes.cs | 6 + src/SharpCoreDB/Services/SqlOrderBy.cs | 30 +- src/SharpCoreDB/Services/SqlParser.DDL.cs | 81 ++++- src/SharpCoreDB/Services/SqlParser.DML.cs | 117 +++++++- .../Services/SqlParser.SetExpressions.cs | 178 +++++++++++ .../Services/SqlScalarExpression.cs | 31 ++ .../Services/SqlToStringVisitor.Query.cs | 2 +- src/SharpCoreDB/Services/TypeConverter.cs | 29 +- src/SharpCoreDB/SingleFileTable.cs | 83 +++++ .../BatchUpdateClassificationTests.cs | 94 ++++++ tests/SharpCoreDB.Tests/CompositeKeyTests.cs | 189 ++++++++++++ .../JoinAndSubqueryOrderByTests.cs | 129 ++++++++ .../TableCheckConstraintTests.cs | 116 +++++++ .../UpdateSetExpressionTests.cs | 202 +++++++++++++ 23 files changed, 1861 insertions(+), 60 deletions(-) create mode 100644 src/SharpCoreDB/DataStructures/Table.ComputedUpdate.cs create mode 100644 src/SharpCoreDB/Services/SqlParser.SetExpressions.cs create mode 100644 tests/SharpCoreDB.Tests/BatchUpdateClassificationTests.cs create mode 100644 tests/SharpCoreDB.Tests/CompositeKeyTests.cs create mode 100644 tests/SharpCoreDB.Tests/JoinAndSubqueryOrderByTests.cs create mode 100644 tests/SharpCoreDB.Tests/TableCheckConstraintTests.cs create mode 100644 tests/SharpCoreDB.Tests/UpdateSetExpressionTests.cs 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 + { + public static readonly KeyComparer Instance = new(); + + public int Compare(object? x, object? y) + { + bool xNull = x is null or DBNull; + bool yNull = y is null or DBNull; + if (xNull || yNull) + { + return xNull == yNull ? 0 : xNull ? -1 : 1; + } + + return x is string xs && y is string ys + ? string.CompareOrdinal(xs, ys) + : SqlScalarExpression.TryCompare(x, y, out var c) ? c : 0; + } + } + /// One ORDER BY key. internal sealed class Term(SqlScalarExpression.Node key, bool descending, bool nullsFirst, string? collation, string? columnName) { @@ -96,11 +118,11 @@ internal sealed class Term(SqlScalarExpression.Node key, bool descending, bool n /// Sorts rows by the terms (stable), before any OFFSET/LIMIT is applied. /// The rows to sort. /// The ORDER BY terms. - /// The table, for declared column collations. + /// The table, for declared column collations; null for a JOIN result (BINARY unless COLLATE). internal static List> Sort( List> rows, IReadOnlyList terms, - ITable table) + ITable? table) { if (rows.Count < 2) { @@ -168,7 +190,7 @@ private static int CompareKey(object? x, object? y, Term term, IComparer } /// The text comparer for a term: its COLLATE, else the column's declared collation, else BINARY (null). - private static IComparer? TextComparerFor(Term term, ITable table) + private static IComparer? TextComparerFor(Term term, ITable? table) { switch (term.Collation) { @@ -180,7 +202,7 @@ private static int CompareKey(object? x, object? y, Term term, IComparer return null; } - if (term.ColumnName is null) + if (term.ColumnName is null || table is null) { return null; } diff --git a/src/SharpCoreDB/Services/SqlParser.DDL.cs b/src/SharpCoreDB/Services/SqlParser.DDL.cs index 65572531..0a38eb97 100644 --- a/src/SharpCoreDB/Services/SqlParser.DDL.cs +++ b/src/SharpCoreDB/Services/SqlParser.DDL.cs @@ -8,6 +8,7 @@ namespace SharpCoreDB.Services; using SharpCoreDB.DataStructures; using SharpCoreDB.Interfaces; using SharpCoreDB.Storage.Hybrid; +using System.Text.RegularExpressions; /// /// SqlParser partial class containing DDL (Data Definition Language) operations: @@ -77,8 +78,12 @@ private void ExecuteCreateTable(string sql, string[] parts, IWAL? wal) // ✅ Phase 9: Per-column locale names (null for non-Locale collations) var columnLocaleNames = new List(); - - // ✅ FIX: Determine default storage mode from DatabaseConfig if present + + // Every column the PRIMARY KEY names — inline (`id INTEGER PRIMARY KEY`) or as a table constraint + // (`PRIMARY KEY (a, b)`). Resolved after the loop: one column is the table's key; several are a composite key. + var primaryKeyColumns = new List(); + + // ✅ FIX: Determine default storage modefrom DatabaseConfig if present // This ensures encrypted PageBased databases create tables with PageBased mode var storageMode = this.config?.StorageEngineType switch { @@ -114,9 +119,20 @@ private void ExecuteCreateTable(string sql, string[] parts, IWAL? wal) for (int i = 0; i < colDefs.Count; i++) { - var def = colDefs[i]; + // A table constraint may carry a name (`CONSTRAINT pk_t PRIMARY KEY (a, b)`, as EF Core migrations write + // it). SQLite uses the name only in messages; it is dropped here. Up to 2.1.4 such a definition was read + // as a column called CONSTRAINT. + var def = StripConstraintName(colDefs[i]); var defUpper = def.ToUpperInvariant(); + // A table-level PRIMARY KEY (a, b). Up to 2.1.4 it was read as a column called PRIMARY that became the + // primary key — always NULL, so a second row was a key violation and the table held one row. + if (TableLevelPrimaryKeyRegex.IsMatch(def)) + { + primaryKeyColumns.AddRange(ParseUniqueConstraintColumns(def)); + continue; + } + if (defUpper.StartsWith("UNIQUE", StringComparison.OrdinalIgnoreCase)) { var uniqueColumns = ParseUniqueConstraintColumns(def); @@ -209,15 +225,17 @@ private void ExecuteCreateTable(string sql, string[] parts, IWAL? wal) if (isPrimary) { - primaryKeyIndex = i; + primaryKeyColumns.Add(colName); } - + if (isUniqueCol) { uniqueConstraints.Add([colName]); } } + primaryKeyIndex = ResolvePrimaryKey(primaryKeyColumns, columns, uniqueConstraints); + // ✅ AUTO-ROWID: When no explicit PRIMARY KEY is defined, inject a hidden // _rowid column using ULID type. This ensures every table has a primary key // for efficient DELETE/UPDATE operations on the columnar engine, and follows @@ -243,6 +261,7 @@ private void ExecuteCreateTable(string sql, string[] parts, IWAL? wal) // ✅ NEW: Parse FOREIGN KEY constraints (Phase 1.2) // Look for FOREIGN KEY definitions in the column definitions foreignKeys.AddRange(colDefs + .Select(StripConstraintName) .Where(def => def.Trim().ToUpper().StartsWith("FOREIGN KEY")) .Select(def => ParseForeignKeyFromString(def.Trim())) .Where(fk => fk is not null)); @@ -481,6 +500,58 @@ private static string ReadCreateTableName(string token) /// /// A column definition as split out of the statement. /// The column name the catalog is keyed by. + private static readonly Regex TableLevelPrimaryKeyRegex = new( + @"^\s*PRIMARY\s+KEY\s*\(", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled, + TimeSpan.FromSeconds(1)); + + private static readonly Regex ConstraintNameRegex = new( + @"^\s*CONSTRAINT\s+(?:""[^""]*""|\[[^\]]*\]|`[^`]*`|[A-Za-z_][\w$]*)\s+", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled, + TimeSpan.FromSeconds(1)); + + /// Drops a leading CONSTRAINT name from a table-constraint definition. + private static string StripConstraintName(string definition) + { + var match = ConstraintNameRegex.Match(definition); + return match.Success ? definition[match.Length..] : definition; + } + + /// + /// Resolves the declared PRIMARY KEY columns to the table's key column. One column is the key. Several — a + /// table-level PRIMARY KEY (a, b), or PRIMARY KEY written on more than one column, which is how EF + /// Core's EnsureCreated declares a composite key — are a composite key: as in a SQLite rowid table, the + /// table keeps its row id as the storage key and the composite key is enforced as a UNIQUE constraint over all + /// its columns. Up to 2.1.4 the last of several inline PRIMARY KEY columns silently became the whole key, so rows + /// that differed only in the other key columns were refused as duplicates. + /// + /// The index of the single key column, or -1 when there is none (or the key is composite). + private static int ResolvePrimaryKey(List primaryKeyColumns, List columns, List> uniqueConstraints) + { + var resolved = new List(primaryKeyColumns.Count); + foreach (var name in primaryKeyColumns) + { + var column = columns.Find(c => c.Equals(name, StringComparison.OrdinalIgnoreCase)) + ?? throw new InvalidOperationException($"PRIMARY KEY names column '{name}', which the table does not have"); + if (!resolved.Contains(column, StringComparer.OrdinalIgnoreCase)) + { + resolved.Add(column); + } + } + + if (resolved.Count == 1) + { + return columns.IndexOf(resolved[0]); + } + + if (resolved.Count > 1) + { + uniqueConstraints.Add(resolved); + } + + return -1; + } + private static string ReadColumnName(string definition) { if (SqlIdentifierScanner.TryReadIdentifier(definition, 0, out int start, out int end)) diff --git a/src/SharpCoreDB/Services/SqlParser.DML.cs b/src/SharpCoreDB/Services/SqlParser.DML.cs index 868dd7ee..b2852fe1 100644 --- a/src/SharpCoreDB/Services/SqlParser.DML.cs +++ b/src/SharpCoreDB/Services/SqlParser.DML.cs @@ -20,7 +20,11 @@ namespace SharpCoreDB.Services; /// public partial class SqlParser { - private static readonly Regex UpdateRegex = new(@"UPDATE\s+[""'`\[]?(\w+)[""'`\]]?\s+SET\s+(.*?)\s+WHERE\s+(.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled, TimeSpan.FromSeconds(1)); + // The WHERE clause is OPTIONAL, as for DELETE: SQLite's `UPDATE t SET n = 0` changes every row, and this regex + // required a WHERE, so the statement threw "Invalid UPDATE syntax". The SET list and the WHERE clause are split by + // SplitUpdateSetAndWhere, at the first WHERE outside a quoted literal or parentheses — the regex split at the first + // WHERE anywhere, so `SET note = 'see where' WHERE id = 1` lost the rest of the literal. + private static readonly Regex UpdateRegex = new(@"UPDATE\s+[""'`\[]?(\w+)[""'`\]]?\s+SET\s+(.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled, TimeSpan.FromSeconds(1)); // The WHERE clause is OPTIONAL (2026-09-27, session 73): SQLite accepts `DELETE FROM t` and removes every // row, and the table layer already reads a null/empty WHERE that way (Table.CRUD.cs — the full-scan filter @@ -1940,23 +1944,35 @@ var s when s.StartsWith("FULL") => JoinExecutor.ExecuteFullJoin(currentRows, rig currentRows = [.. currentRows.Where(r => EvaluateJoinRowWhere(r, whereClause))]; } - // 4. Apply ORDER BY + // 4. Apply ORDER BY — the whole list as the single-table path reads it (SqlOrderBy): every key with its own + // direction, expressions (LENGTH(p.name), c.score * -1), aliases and ordinals. Up to 2.1.4 only the first token of + // the first key was read, so a second key, an expression or a DESC after an expression was ignored. var orderPos2 = FindKeywordPosition(sqlUpper, "ORDER BY"); if (orderPos2 >= 0) { var limitPos = FindKeywordPosition(sqlUpper, SqlConstants.LIMIT, orderPos2 + 8); int end = limitPos >= 0 ? limitPos : sql.Length; var orderClause = sql.Substring(orderPos2 + 8, end - orderPos2 - 8).Trim(); - currentRows = ApplyJoinOrderBy(currentRows, orderClause); + var joinSelect = System.Text.RegularExpressions.Regex.Match( + sql, @"SELECT\s+(.*?)\s+FROM\b", + System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline, TimeSpan.FromSeconds(1)); + var orderTerms = SqlOrderBy.TryParse(orderClause, joinSelect.Success ? joinSelect.Groups[1].Value : "*"); + currentRows = orderTerms is null + ? ApplyJoinOrderBy(currentRows, orderClause) + : SqlOrderBy.Sort(currentRows, orderTerms, table: null); } - // 5. Apply LIMIT + // 5. Apply LIMIT / OFFSET, in SQLite's three forms (LIMIT n, LIMIT n OFFSET m, LIMIT m, n). Up to 2.1.4 the OFFSET + // was ignored and LIMIT 0 returned every row. var limitPos2 = FindKeywordPosition(sqlUpper, SqlConstants.LIMIT); if (limitPos2 >= 0) { - var limitStr = sql[(limitPos2 + 5)..].Trim().Split(' ', ',')[0]; - if (int.TryParse(limitStr, out var lim) && lim > 0) - currentRows = [.. currentRows.Take(lim)]; + string[] limitParts = ["SELECT", .. sql[limitPos2..].Split([' ', '\t', '\r', '\n'], StringSplitOptions.RemoveEmptyEntries)]; + var (joinLimit, joinOffset) = ParseLimitClause(limitParts, 1); + if (joinOffset is > 0) + currentRows = [.. currentRows.Skip(joinOffset.Value)]; + if (joinLimit.HasValue) + currentRows = [.. currentRows.Take(joinLimit.Value)]; } // 6. Apply SELECT column projection and aliases @@ -2428,12 +2444,14 @@ private void ExecuteUpdate(string sql, IWAL? wal) if (!tables.TryGetValue(tableName, out var table)) throw new InvalidOperationException($"Table {tableName} does not exist"); + var (setList, whereClause) = SplitUpdateSetAndWhere(updateMatch.Groups[2].Value); + // Bracket-aware split: `SET embedding = [0.5, 0.25]` is one assignment, not two // (SqlInPredicate.SplitTopLevelComma honours quotes, parentheses and bracketed literals). - var setClauses = SqlInPredicate.SplitTopLevelComma(updateMatch.Groups[2].Value.Trim()); - var whereClause = updateMatch.Groups[3].Value.Trim(); + var setClauses = SqlInPredicate.SplitTopLevelComma(setList); var updates = new Dictionary(); + List, object?>>>? computed = null; foreach (var setClause in setClauses) { // Split at the FIRST '=' only. A bound text value may contain '=' itself (a URL query string, a @@ -2454,8 +2472,15 @@ private void ExecuteUpdate(string sql, IWAL? wal) if (colIndex >= 0) { var colType = table.ColumnTypes[colIndex]; - var value = SqlParser.ParseValue(valueStr, colType); - updates[colName] = value; + + // `SET n = n + 1`, `SET name = upper(name)`: computed per row (see SqlParser.SetExpressions). + if (IsComputedSetValue(valueStr, table.Columns, out var expression)) + { + (computed ??= []).Add(new(colName, CompileSetExpression(expression, colType))); + continue; + } + + updates[colName] = ParseSetLiteral(valueStr, colType); } else { @@ -2470,7 +2495,9 @@ private void ExecuteUpdate(string sql, IWAL? wal) // Issue #8: single-pass — UpdateAffectedCount applies the update AND returns the affected // count, so change-tracking no longer needs a separate full Select pass (the old code // materialized every matching row just to count them). - var affectedCount = table.UpdateAffectedCount(whereClause, updates); + var affectedCount = computed is null + ? table.UpdateAffectedCount(whereClause, updates) + : table.UpdateAffectedCount(whereClause, updates!, computed); _lastChanges = affectedCount; _totalChanges += affectedCount; @@ -2484,6 +2511,57 @@ private void ExecuteUpdate(string sql, IWAL? wal) wal?.Log(sqlWithoutReturning); } + /// + /// Splits the text after SET into the SET list and the WHERE clause (empty when there is none), at the first + /// WHERE keyword outside a quoted literal, a quoted identifier or parentheses. + /// + private static (string SetList, string Where) SplitUpdateSetAndWhere(string text) + { + int depth = 0; + for (int i = 0; i < text.Length; i++) + { + char c = text[i]; + if (c is '\'' or '"' or '`' or '[') + { + char close = c == '[' ? ']' : c; + for (i++; i < text.Length; i++) + { + if (text[i] != close) + { + continue; + } + + if (close != ']' && i + 1 < text.Length && text[i + 1] == close) + { + i++; // a doubled quote is one quote of data + continue; + } + + break; + } + + continue; + } + + if (c == '(') + { + depth++; + } + else if (c == ')') + { + depth--; + } + else if (depth == 0 && (c is 'W' or 'w') && i > 0 && char.IsWhiteSpace(text[i - 1]) && + string.Compare(text, i, "WHERE", 0, 5, StringComparison.OrdinalIgnoreCase) == 0 && + (i + 5 == text.Length || char.IsWhiteSpace(text[i + 5]) || text[i + 5] == '(')) + { + return (text[..i].Trim(), text[(i + 5)..].Trim()); + } + } + + return (text.Trim(), string.Empty); + } + /// /// Executes DELETE statement with RETURNING clause and change tracking support. /// @@ -3286,23 +3364,30 @@ private static List> ApplyOrderBy(List GetValue(row, item.Column), comparer) - : rows.OrderByDescending(row => GetValue(row, item.Column), comparer); + ? rows.OrderBy(key, comparer) + : rows.OrderByDescending(key, comparer); } else { ordered = item.IsAscending - ? ordered.ThenBy(row => GetValue(row, item.Column), comparer) - : ordered.ThenByDescending(row => GetValue(row, item.Column), comparer); + ? ordered.ThenBy(key, comparer) + : ordered.ThenByDescending(key, comparer); } } return [.. ordered]; } + /// The sort key of an ORDER BY item: its expression's value (LENGTH(name)), else the column's. + internal static Func, object?> OrderKeySelector(OrderByItem item) => + item.Expression is { } expression && SqlScalarExpression.TryParse(expression, out var node) + ? row => SqlScalarExpression.Evaluate(node, row) + : row => GetValue(row, item.Column); + private static List> ApplyWindowing(List> rows, int? offset, int? limit) { var result = rows.AsEnumerable(); diff --git a/src/SharpCoreDB/Services/SqlParser.SetExpressions.cs b/src/SharpCoreDB/Services/SqlParser.SetExpressions.cs new file mode 100644 index 00000000..6098e3a4 --- /dev/null +++ b/src/SharpCoreDB/Services/SqlParser.SetExpressions.cs @@ -0,0 +1,178 @@ +// +// 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.Services; + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +/// +/// SqlParser partial — reading an UPDATE's SET values: a literal, or an expression computed per row. +/// +/// Up to 2.1.4 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 is now an +/// expression when it reads a column of the table or calls a function — as in SQLite. Text that is only constant +/// arithmetic keeps the literal reading it always had (an unquoted 2024-01-15 stays a date for a DATETIME +/// column), and is evaluated only when the literal reading fails (SET n = 2 * 3). +/// +/// +public partial class SqlParser +{ + /// + /// Returns true when is an expression that reads a column of the table or calls + /// a function, with its parsed form. A plain literal (a quoted string, a number, NULL, a parameter, a vector or blob + /// literal) is recognised without parsing and allocates nothing. + /// + internal static bool IsComputedSetValue(string valueText, IReadOnlyList columns, out SqlScalarExpression.Node node) + { + node = null!; + if (IsPlainSetLiteral(valueText) || !SqlScalarExpression.TryParse(valueText.Trim(), out var parsed)) + { + return false; + } + + if (!SqlScalarExpression.ReferencesColumnOrFunction(parsed, name => ContainsColumn(columns, name))) + { + return false; + } + + node = parsed; + return true; + } + + /// + /// Parses a SET value that is not a per-row expression: the literal parse, or — when that fails and the text is a + /// constant expression such as 2 * 3 — its value. + /// + internal static object? ParseSetLiteral(string valueText, DataType type) + { + try + { + return ParseValue(valueText, type); + } + catch (InvalidOperationException) when (!IsPlainSetLiteral(valueText) && + SqlScalarExpression.TryParse(valueText.Trim(), out var constant)) + { + return ConvertComputedSetValue(SqlScalarExpression.Evaluate(constant, null), type); + } + } + + /// Builds the per-row function for a computed SET value of a column of type . + internal static Func, object?> CompileSetExpression(SqlScalarExpression.Node node, DataType type) => + row => ConvertComputedSetValue(SqlScalarExpression.Evaluate(node, row), type); + + /// Converts a computed value to the column's storage type (SQL NULL stays null). + internal static object? ConvertComputedSetValue(object? value, DataType type) + { + if (value is null or DBNull) + { + return null; + } + + try + { + return type switch + { + DataType.String => value as string ?? SqlScalarExpression.ToText(value), + DataType.Integer => value switch + { + int i => i, + long l => checked((int)l), + double d when double.IsInteger(d) => checked((int)d), + bool b => b ? 1 : 0, + _ => ParseValue(SqlScalarExpression.ToText(value), type), + }, + DataType.Long => value switch + { + int i => (long)i, + long l => l, + double d when double.IsInteger(d) => checked((long)d), + bool b => b ? 1L : 0L, + _ => ParseValue(SqlScalarExpression.ToText(value), type), + }, + DataType.Real => value is string + ? ParseValue(SqlScalarExpression.ToText(value), type) + : Convert.ToDouble(value, CultureInfo.InvariantCulture), + DataType.Decimal => value is string + ? ParseValue(SqlScalarExpression.ToText(value), type) + : Convert.ToDecimal(value, CultureInfo.InvariantCulture), + DataType.Boolean => value switch + { + bool b => b, + string => ParseValue(SqlScalarExpression.ToText(value), type), + _ => Convert.ToDouble(value, CultureInfo.InvariantCulture) != 0, + }, + DataType.Blob => value as byte[] ?? Encoding.UTF8.GetBytes(SqlScalarExpression.ToText(value)), + DataType.DateTime when value is DateTime => value, + _ => ParseValue(SqlScalarExpression.ToText(value), type), + }; + } + catch (OverflowException) + { + throw new InvalidOperationException($"The computed value {SqlScalarExpression.ToText(value)} does not fit a {type} column"); + } + } + + /// + /// Returns true for a SET value that is certainly a literal: one quoted string, a number, NULL/TRUE/FALSE, a + /// parameter, a vector ([…]) or blob (X'…') literal. Allocates nothing. + /// + private static bool IsPlainSetLiteral(string valueText) + { + var span = valueText.AsSpan().Trim(); + if (span.IsEmpty) + { + return true; + } + + char first = span[0]; + if (first == '\'') + { + for (int i = 1; i < span.Length; i++) + { + if (span[i] != '\'') + { + continue; + } + + if (i + 1 < span.Length && span[i + 1] == '\'') + { + i++; // a doubled quote is one quote of data + continue; + } + + return i == span.Length - 1; + } + + return true; // unterminated: the literal parser reports it + } + + if (first is '[' or '?' or '@' or ':' or '$' || + ((first is 'X' or 'x') && span.Length > 1 && span[1] == '\'')) + { + return true; + } + + return span.Equals("NULL", StringComparison.OrdinalIgnoreCase) || + span.Equals("TRUE", StringComparison.OrdinalIgnoreCase) || + span.Equals("FALSE", StringComparison.OrdinalIgnoreCase) || + double.TryParse(span, NumberStyles.Float, CultureInfo.InvariantCulture, out _); + } + + private static bool ContainsColumn(IReadOnlyList columns, string name) + { + for (int i = 0; i < columns.Count; i++) + { + if (columns[i].Equals(name, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } +} diff --git a/src/SharpCoreDB/Services/SqlScalarExpression.cs b/src/SharpCoreDB/Services/SqlScalarExpression.cs index fa474d78..4492d10f 100644 --- a/src/SharpCoreDB/Services/SqlScalarExpression.cs +++ b/src/SharpCoreDB/Services/SqlScalarExpression.cs @@ -32,6 +32,7 @@ internal static class SqlScalarExpression private static readonly ConcurrentDictionary ExpressionCache = new(StringComparer.Ordinal); private static readonly ConcurrentDictionary ConditionCache = new(StringComparer.Ordinal); + private static readonly ConcurrentDictionary CheckCache = new(StringComparer.Ordinal); private static readonly Dictionary EmptyRow = new(StringComparer.OrdinalIgnoreCase); private static readonly Regex NumericPrefix = new( @@ -96,6 +97,33 @@ internal static bool TryEvaluateCondition(Dictionary row, string return true; } + /// + /// Evaluates a CHECK constraint as SQLite does: the row passes unless the expression is false — a NULL + /// (unknown) result passes, so CHECK (price > 0) accepts a row without a price. Returns false — + /// "not mine" — when the expression uses syntax this evaluator does not cover (IN, BETWEEN, a subquery), so the + /// caller's own evaluator stays in charge of it. + /// + internal static bool TryEvaluateCheck(string expression, Dictionary row, out bool passes) + { + var compiled = GetOrAdd(CheckCache, expression, static c => ConditionCompiler.CompileAny(c)); + passes = compiled is null || compiled.Evaluate(row) != false; + return compiled is not null; + } + + /// + /// Returns true when the expression reads a column for which holds, or calls a + /// function or CAST — that is, when its value can differ from row to row. Constant arithmetic such as + /// 2024-01-15 returns false. + /// + internal static bool ReferencesColumnOrFunction(Node node, Func isColumn) => node switch + { + Column column => isColumn(column.Name), + Call or Cast => true, + Binary binary => ReferencesColumnOrFunction(binary.Left, isColumn) || ReferencesColumnOrFunction(binary.Right, isColumn), + Negate negate => ReferencesColumnOrFunction(negate.Operand, isColumn), + _ => false, + }; + /// SQLite's text form of a value (what upper, length, || and CAST AS TEXT see). internal static string ToText(object? value) => value switch { @@ -1151,6 +1179,9 @@ private sealed class ConditionCompiler return compiled is not null && compiler._hasComputedOperand ? compiled : null; } + /// Compiles any condition this evaluator covers, computed operand or not (CHECK constraints). + public static Condition? CompileAny(string condition) => new ConditionCompiler().CompileCondition(condition); + private Condition? CompileCondition(string text) { text = SqlInPredicate.StripOuterParentheses(text.Trim()); diff --git a/src/SharpCoreDB/Services/SqlToStringVisitor.Query.cs b/src/SharpCoreDB/Services/SqlToStringVisitor.Query.cs index d893d2a4..ce7e7297 100644 --- a/src/SharpCoreDB/Services/SqlToStringVisitor.Query.cs +++ b/src/SharpCoreDB/Services/SqlToStringVisitor.Query.cs @@ -153,7 +153,7 @@ protected override string VisitOrderByCore(OrderByNode node) { var items = node.Items.Select(i => { - var col = i.Column.Accept(this); + var col = i.Expression ?? i.Column.Accept(this); return i.IsAscending ? col : $"{col} DESC"; }); return $"ORDER BY {string.Join(", ", items)}"; diff --git a/src/SharpCoreDB/Services/TypeConverter.cs b/src/SharpCoreDB/Services/TypeConverter.cs index f38712ea..c34de833 100644 --- a/src/SharpCoreDB/Services/TypeConverter.cs +++ b/src/SharpCoreDB/Services/TypeConverter.cs @@ -136,10 +136,21 @@ public static bool EvaluateCheckConstraint(string? expression, Dictionary id)` — and up to 2.1.4 the keyword reached the + // evaluator, failed to parse and so rejected every row: a table with a table-level CHECK accepted no INSERT. + expression = StripCheckKeyword(expression.Trim()); + + // SQLite semantics first: three-valued logic (a NULL operand passes), `<>`, functions and expressions. Syntax + // that evaluator does not cover keeps the original evaluator below. + if (SqlScalarExpression.TryEvaluateCheck(expression, row, out var passes)) + { + return passes; + } + try { // Parse and evaluate the full expression - return EvaluateExpression(expression.Trim(), row, columnTypes); + return EvaluateExpression(expression, row, columnTypes); } catch { @@ -148,6 +159,22 @@ public static bool EvaluateCheckConstraint(string? expression, Dictionary 5 && + expression.StartsWith("CHECK", StringComparison.OrdinalIgnoreCase) && + (char.IsWhiteSpace(expression[5]) || expression[5] == '(')) + { + var rest = expression[5..].Trim(); + if (rest.Length > 1 && rest[0] == '(') + { + return SqlInPredicate.StripOuterParentheses(rest); + } + } + + return expression; + } + /// /// Evaluates a full CHECK constraint expression with support for: /// - Column references diff --git a/src/SharpCoreDB/SingleFileTable.cs b/src/SharpCoreDB/SingleFileTable.cs index 5c0870c4..38204ea1 100644 --- a/src/SharpCoreDB/SingleFileTable.cs +++ b/src/SharpCoreDB/SingleFileTable.cs @@ -487,6 +487,89 @@ private static IEnumerable> ApplyOrderBy( return affected; } + /// + public int UpdateAffectedCount( + string? where, + Dictionary updates, + IReadOnlyList, object?>>> computed) + { + ArgumentNullException.ThrowIfNull(updates); + ArgumentNullException.ThrowIfNull(computed); + if (computed.Count == 0) + { + return UpdateAffectedCount(where, updates); + } + + EnsureCacheLoaded(); + + var condition = where?.Trim(); + if (condition is not null && condition.StartsWith(WhereKeywordPrefix, StringComparison.OrdinalIgnoreCase)) + { + condition = condition[6..].Trim(); + } + + bool updatesTouchPk = PrimaryKeyIndex >= 0 && + updates.Keys.Concat(computed.Select(c => c.Key)) + .Any(k => string.Equals(k, Columns[PrimaryKeyIndex], StringComparison.OrdinalIgnoreCase)); + + int affected = 0; + lock (_tableLock) + { + // Every value is computed from the row as it was before the statement, and checked, before any row changes. + var matched = new List<(Dictionary Row, Dictionary Values)>(); + foreach (var row in _rowCache) + { + if (!string.IsNullOrWhiteSpace(condition) && !EvaluateCondition(row, condition)) + { + continue; + } + + var values = new Dictionary(updates); + foreach (var (column, compute) in computed) + { + values[column] = compute(row)!; + } + + if (FindNotNullViolation(values) is { } notNullColumn) + { + throw new InvalidOperationException($"Column '{notNullColumn}' cannot be NULL"); + } + + matched.Add((row, values)); + } + + foreach (var (row, values) in matched) + { + string? oldPkKey = updatesTouchPk ? GetPkKey(row) : null; + + foreach (var update in values) + { + row[update.Key] = update.Value; + } + + if (oldPkKey is not null) + { + var newPkKey = GetPkKey(row); + if (!string.Equals(oldPkKey, newPkKey, StringComparison.Ordinal)) + { + UnindexRow(row, oldPkKey); + IndexRow(row); + } + } + + _isDirty = true; + affected++; + } + } + + if (AutoFlush && _isDirty && !_isInTransaction) + { + FlushCache(); + } + + return affected; + } + /// /// Executes batch updates keyed by primary key value. /// diff --git a/tests/SharpCoreDB.Tests/BatchUpdateClassificationTests.cs b/tests/SharpCoreDB.Tests/BatchUpdateClassificationTests.cs new file mode 100644 index 00000000..5e7722b7 --- /dev/null +++ b/tests/SharpCoreDB.Tests/BatchUpdateClassificationTests.cs @@ -0,0 +1,94 @@ +// +// 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.Tests; + +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using SharpCoreDB.Interfaces; +using Xunit; + +/// +/// An UPDATE that ExecuteBatchSQL's UPDATE fast path declines must run as that UPDATE. +/// +/// Up to 2.1.4 the DELETE classifier, tried next, accepted the canonical UPDATE shape too: an UPDATE with a quoted SET +/// column (SET "n" = 5, as linq2db and EF Core write it) was executed as DELETE … WHERE id = 1, and the +/// batch deleted the rows it was asked to update. +/// +/// +public sealed class BatchUpdateClassificationTests : IDisposable +{ + private readonly DatabaseFactory _factory; + private readonly string _root = Path.Combine(Path.GetTempPath(), $"SCDB_BatchUpdate_{Guid.NewGuid():N}"); + + public BatchUpdateClassificationTests() + { + var services = new ServiceCollection(); + services.AddSharpCoreDB(); + _factory = services.BuildServiceProvider().GetRequiredService(); + Directory.CreateDirectory(_root); + } + + public static TheoryData Modes => new() { "columnar", "page-based" }; + + public void Dispose() + { + try + { + if (Directory.Exists(_root)) + { + Directory.Delete(_root, true); + } + } + catch (IOException) + { + // Best-effort cleanup of a temp directory. + } + } + + private (IDatabase Db, string Path) Open(string mode, string ddl) + { + var path = Path.Combine(_root, mode == "single-file" ? $"db_{Guid.NewGuid():N}.scdb" : $"db_{Guid.NewGuid():N}"); + var db = Reopen(mode, path); + db.ExecuteSQL(mode == "page-based" ? ddl + " STORAGE = PAGE_BASED" : ddl); + return (db, path); + } + + private IDatabase Reopen(string mode, string path) => mode == "single-file" + ? _factory.CreateWithOptions(path, "pw", DatabaseOptions.CreateSingleFileDefault()) + : _factory.Create(path, "pw"); + + private static string[] Rows(IDatabase db, string sql) => + [.. db.ExecuteQuery(sql).Select(r => string.Join(",", r.Values.Select(v => v is null or DBNull ? "NULL" : Convert.ToString(v, CultureInfo.InvariantCulture))))]; + + [Theory] + [MemberData(nameof(Modes))] + public void BatchUpdate_QuotedSetColumn_UpdatesTheRowsAndDeletesNone(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER, name TEXT)"); + db.ExecuteSQL("INSERT INTO t (id, n, name) VALUES (1, 1, 'ann'), (2, 2, 'bob')"); + + db.ExecuteBatchSQL(["UPDATE t SET \"n\" = 5 WHERE id = 1", "UPDATE t SET \"name\" = 'z' WHERE id = 2"]); + + Assert.Equal(["1,5,ann", "2,2,z"], Rows(db, "SELECT id, n, name FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void BatchUpdate_QuotedSetColumn_IsKeptAfterReopen(string mode) + { + var (db, path) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER)"); + db.ExecuteSQL("INSERT INTO t (id, n) VALUES (1, 1), (2, 2)"); + db.ExecuteBatchSQL(["UPDATE t SET \"n\" = 5 WHERE id = 1", "UPDATE t SET \"n\" = 6 WHERE id = 2"]); + (db as IDisposable)?.Dispose(); + + var reopened = Reopen(mode, path); + Assert.Equal(["1,5", "2,6"], Rows(reopened, "SELECT id, n FROM t").Order()); + (reopened as IDisposable)?.Dispose(); + } +} diff --git a/tests/SharpCoreDB.Tests/CompositeKeyTests.cs b/tests/SharpCoreDB.Tests/CompositeKeyTests.cs new file mode 100644 index 00000000..2f4d99dd --- /dev/null +++ b/tests/SharpCoreDB.Tests/CompositeKeyTests.cs @@ -0,0 +1,189 @@ +// +// 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.Tests; + +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using SharpCoreDB.Interfaces; +using Xunit; + +/// +/// A composite PRIMARY KEY and a multi-column UNIQUE constraint on directory storage (columnar and page-based). +/// +/// Up to 2.1.4 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 key — always NULL, so the table +/// held one row. PRIMARY KEY written on two columns, as EF Core's EnsureCreated declares a composite key, +/// made the last column the whole key. 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 staying the +/// storage key. +/// +/// +public sealed class CompositeKeyTests : IDisposable +{ + private readonly DatabaseFactory _factory; + private readonly string _root = Path.Combine(Path.GetTempPath(), $"SCDB_CompositeKey_{Guid.NewGuid():N}"); + + public CompositeKeyTests() + { + var services = new ServiceCollection(); + services.AddSharpCoreDB(); + _factory = services.BuildServiceProvider().GetRequiredService(); + Directory.CreateDirectory(_root); + } + + public static TheoryData Modes => new() { "columnar", "page-based" }; + + public void Dispose() + { + try + { + if (Directory.Exists(_root)) + { + Directory.Delete(_root, true); + } + } + catch (IOException) + { + // Best-effort cleanup of a temp directory. + } + } + + private (IDatabase Db, string Path) Open(string mode, string ddl) + { + var path = Path.Combine(_root, mode == "single-file" ? $"db_{Guid.NewGuid():N}.scdb" : $"db_{Guid.NewGuid():N}"); + var db = Reopen(mode, path); + db.ExecuteSQL(mode == "page-based" ? ddl + " STORAGE = PAGE_BASED" : ddl); + return (db, path); + } + + private IDatabase Reopen(string mode, string path) => mode == "single-file" + ? _factory.CreateWithOptions(path, "pw", DatabaseOptions.CreateSingleFileDefault()) + : _factory.Create(path, "pw"); + + private static string[] Rows(IDatabase db, string sql) => + [.. db.ExecuteQuery(sql).Select(r => string.Join(",", r.Values.Select(v => v is null or DBNull ? "NULL" : Convert.ToString(v, CultureInfo.InvariantCulture))))]; + + [Theory] + [MemberData(nameof(Modes))] + public void CreateTable_TableLevelCompositePrimaryKey_AcceptsDistinctPairsAndRejectsADuplicate(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (a INTEGER NOT NULL, b INTEGER NOT NULL, v TEXT, PRIMARY KEY (a, b))"); + db.ExecuteSQL("INSERT INTO t (a, b, v) VALUES (1, 1, 'x')"); + db.ExecuteSQL("INSERT INTO t (a, b, v) VALUES (2, 1, 'y')"); + db.ExecuteSQL("INSERT INTO t (a, b, v) VALUES (1, 2, 'z')"); + + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (a, b, v) VALUES (1, 1, 'dup')")); + Assert.Equal(["1,1,x", "1,2,z", "2,1,y"], Rows(db, "SELECT a, b, v FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void CreateTable_NamedCompositePrimaryKeyConstraint_DeclaresNoPhantomColumn(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (a INTEGER NOT NULL, b INTEGER NOT NULL, CONSTRAINT pk_t PRIMARY KEY (a, b))"); + db.TryGetTable("t", out var table); + + Assert.DoesNotContain(table.Columns, c => c.Equals("CONSTRAINT", StringComparison.OrdinalIgnoreCase) || + c.Equals("PRIMARY", StringComparison.OrdinalIgnoreCase)); + db.ExecuteSQL("INSERT INTO t (a, b) VALUES (1, 1)"); + db.ExecuteSQL("INSERT INTO t (a, b) VALUES (2, 1)"); + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (a, b) VALUES (1, 1)")); + Assert.Equal(2, db.ExecuteQuery("SELECT a FROM t").Count); + } + + [Theory] + [MemberData(nameof(Modes))] + public void CreateTable_PrimaryKeyWrittenOnTwoColumns_IsOneCompositeKey(string mode) + { + // The shape EF Core's EnsureCreated writes for HasKey(x => new { x.A, x.B }). + var (db, _) = Open(mode, "CREATE TABLE t (a INTEGER NOT NULL PRIMARY KEY, b INTEGER NOT NULL PRIMARY KEY, v TEXT)"); + db.ExecuteSQL("INSERT INTO t (a, b, v) VALUES (1, 1, 'x')"); + db.ExecuteSQL("INSERT INTO t (a, b, v) VALUES (2, 1, 'y')"); + + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (a, b, v) VALUES (1, 1, 'dup')")); + Assert.Equal(2, db.ExecuteQuery("SELECT a FROM t").Count); + } + + [Theory] + [MemberData(nameof(Modes))] + public void CreateTable_TableLevelSingleColumnPrimaryKey_MakesThatColumnTheKey(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER NOT NULL, v TEXT, PRIMARY KEY (id))"); + db.TryGetTable("t", out var table); + + Assert.Equal("id", table.Columns[table.PrimaryKeyIndex]); + db.ExecuteSQL("INSERT INTO t (id, v) VALUES (1, 'x')"); + db.ExecuteSQL("INSERT INTO t (id, v) VALUES (2, 'y')"); + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (id, v) VALUES (1, 'dup')")); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Insert_DuplicateOfCompositeUnique_IsRejected_AndNullsNeverConflict(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, UNIQUE (a, b))"); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (1, 1, 1)"); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (2, 1, 2)"); + + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (3, 1, 1)")); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (4, 1, NULL)"); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (5, 1, NULL)"); + Assert.Equal(["1", "2", "4", "5"], Rows(db, "SELECT id FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void MultiRowInsert_DuplicateOfCompositeUniqueWithinTheStatement_KeepsNoRow(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, UNIQUE (a, b))"); + + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (1, 1, 1), (2, 2, 2), (3, 1, 1)")); + Assert.Empty(db.ExecuteQuery("SELECT id FROM t")); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_OntoAnotherRowsCompositeKey_IsRejectedAndChangesNothing(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, UNIQUE (a, b))"); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (1, 1, 1)"); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (2, 1, 2)"); + + Assert.ThrowsAny(() => db.ExecuteSQL("UPDATE t SET b = 1 WHERE id = 2")); + Assert.Equal(["1,1,1", "2,1,2"], Rows(db, "SELECT id, a, b FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_MovingARowOffItsCompositeKey_ReleasesTheKey(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, UNIQUE (a, b))"); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (1, 1, 1)"); + + db.ExecuteSQL("UPDATE t SET b = 9 WHERE id = 1"); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (2, 1, 1)"); + + Assert.Equal(["1,1,9", "2,1,1"], Rows(db, "SELECT id, a, b FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Insert_DuplicateOfCompositeKey_AfterReopen_IsRejected(string mode) + { + var (db, path) = Open(mode, "CREATE TABLE t (a INTEGER NOT NULL, b INTEGER NOT NULL, PRIMARY KEY (a, b))"); + db.ExecuteSQL("INSERT INTO t (a, b) VALUES (1, 1)"); + (db as IDisposable)?.Dispose(); + + var reopened = Reopen(mode, path); + reopened.ExecuteSQL("INSERT INTO t (a, b) VALUES (1, 2)"); + Assert.ThrowsAny(() => reopened.ExecuteSQL("INSERT INTO t (a, b) VALUES (1, 1)")); + Assert.Equal(2, reopened.ExecuteQuery("SELECT a FROM t").Count); + (reopened as IDisposable)?.Dispose(); + } +} diff --git a/tests/SharpCoreDB.Tests/JoinAndSubqueryOrderByTests.cs b/tests/SharpCoreDB.Tests/JoinAndSubqueryOrderByTests.cs new file mode 100644 index 00000000..cf98cd9f --- /dev/null +++ b/tests/SharpCoreDB.Tests/JoinAndSubqueryOrderByTests.cs @@ -0,0 +1,129 @@ +// +// 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.Tests; + +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using SharpCoreDB.Interfaces; +using Xunit; + +/// +/// ORDER BY on the JOIN path and on the AST path (derived tables, IN-subqueries): every key, each with its own direction, +/// and expression keys. +/// +/// Up to 2.1.4 the JOIN path read only the first token of the first key — ORDER BY LENGTH(p.name) DESC sorted by +/// nothing and ORDER BY c.score * -1 sorted ascending by score — and ignored OFFSET. The AST path read each key as a +/// bare name, so ORDER BY UPPER(name) left "(name)" as unparsed trailing content and sorted nothing; its subquery +/// executor read the first key only. +/// +/// +public sealed class JoinAndSubqueryOrderByTests : IDisposable +{ + private readonly DatabaseFactory _factory; + private readonly string _root = Path.Combine(Path.GetTempPath(), $"SCDB_JoinOrder_{Guid.NewGuid():N}"); + + public JoinAndSubqueryOrderByTests() + { + var services = new ServiceCollection(); + services.AddSharpCoreDB(); + _factory = services.BuildServiceProvider().GetRequiredService(); + Directory.CreateDirectory(_root); + } + + public static TheoryData Modes => new() { "columnar", "page-based" }; + + public void Dispose() + { + try + { + if (Directory.Exists(_root)) + { + Directory.Delete(_root, true); + } + } + catch (IOException) + { + // Best-effort cleanup of a temp directory. + } + } + + private (IDatabase Db, string Path) Open(string mode, string ddl) + { + var path = Path.Combine(_root, mode == "single-file" ? $"db_{Guid.NewGuid():N}.scdb" : $"db_{Guid.NewGuid():N}"); + var db = Reopen(mode, path); + db.ExecuteSQL(mode == "page-based" ? ddl + " STORAGE = PAGE_BASED" : ddl); + return (db, path); + } + + private IDatabase Reopen(string mode, string path) => mode == "single-file" + ? _factory.CreateWithOptions(path, "pw", DatabaseOptions.CreateSingleFileDefault()) + : _factory.Create(path, "pw"); + + private static string[] Rows(IDatabase db, string sql) => + [.. db.ExecuteQuery(sql).Select(r => string.Join(",", r.Values.Select(v => v is null or DBNull ? "NULL" : Convert.ToString(v, CultureInfo.InvariantCulture))))]; + + private IDatabase Seed() + { + var (db, _) = Open("columnar", "CREATE TABLE p (id INTEGER PRIMARY KEY, name TEXT)"); + db.ExecuteSQL("CREATE TABLE c (id INTEGER PRIMARY KEY, pid INTEGER, score INTEGER)"); + db.ExecuteSQL("INSERT INTO p (id, name) VALUES (1, 'bob'), (2, 'Al'), (3, 'carl'), (4, 'dee')"); + db.ExecuteSQL("INSERT INTO c (id, pid, score) VALUES (1, 1, 5), (2, 2, 9), (3, 3, 1), (4, 4, 5)"); + return db; + } + + private static string[] Names(IDatabase db, string sql) => + [.. db.ExecuteQuery(sql).Select(r => Convert.ToString(r["name"], CultureInfo.InvariantCulture)!)]; + + [Fact] + public void Join_OrderByFunctionDescending_SortsByTheFunction() + { + var db = Seed(); + Assert.Equal(["carl", "bob", "dee", "Al"], + Names(db, "SELECT p.name, c.score FROM p JOIN c ON c.pid = p.id ORDER BY LENGTH(p.name) DESC, p.name")); + } + + [Fact] + public void Join_OrderByArithmetic_SortsByTheComputedValue() + { + var db = Seed(); + Assert.Equal(["Al", "bob", "dee", "carl"], + Names(db, "SELECT p.name, c.score FROM p JOIN c ON c.pid = p.id ORDER BY c.score * -1, p.name")); + } + + [Fact] + public void Join_OrderByTwoKeys_UsesTheSecondKeyForTies() + { + var db = Seed(); + Assert.Equal(["carl", "dee", "bob", "Al"], + Names(db, "SELECT p.name, c.score FROM p JOIN c ON c.pid = p.id ORDER BY c.score, p.name DESC")); + } + + [Fact] + public void Join_LimitWithOffset_SkipsTheOffsetRows() + { + var db = Seed(); + Assert.Equal(["bob", "carl"], + Names(db, "SELECT p.name, c.score FROM p JOIN c ON c.pid = p.id ORDER BY p.name LIMIT 2 OFFSET 1")); + } + + [Fact] + public void DerivedTable_OrderByFunction_SortsByTheFunction() + { + var db = Seed(); + Assert.Equal(["Al", "bob", "dee", "carl"], Names(db, "SELECT name FROM (SELECT name FROM p) ORDER BY LENGTH(name), name")); + } + + [Fact] + public void InSubquery_OrderByFunctionDescending_SortsByTheFunction() + { + var db = Seed(); + Assert.Equal(["dee", "carl", "bob", "Al"], + Names(db, "SELECT name FROM p WHERE id IN (SELECT pid FROM c) ORDER BY UPPER(name) DESC")); + } +} diff --git a/tests/SharpCoreDB.Tests/TableCheckConstraintTests.cs b/tests/SharpCoreDB.Tests/TableCheckConstraintTests.cs new file mode 100644 index 00000000..a28099f1 --- /dev/null +++ b/tests/SharpCoreDB.Tests/TableCheckConstraintTests.cs @@ -0,0 +1,116 @@ +// +// 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.Tests; + +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using SharpCoreDB.Interfaces; +using Xunit; + +/// +/// CHECK constraints evaluated as SQLite does. +/// +/// Up to 2.1.4 a table-level CHECK was stored with its keyword (CHECK (n <> id)), failed to evaluate and +/// so rejected every row — a table with a table-level CHECK accepted no INSERT. <> was not an operator, and +/// a NULL operand failed the check, where SQLite passes a CHECK whose result is NULL. +/// +/// +public sealed class TableCheckConstraintTests : IDisposable +{ + private readonly DatabaseFactory _factory; + private readonly string _root = Path.Combine(Path.GetTempPath(), $"SCDB_Check_{Guid.NewGuid():N}"); + + public TableCheckConstraintTests() + { + var services = new ServiceCollection(); + services.AddSharpCoreDB(); + _factory = services.BuildServiceProvider().GetRequiredService(); + Directory.CreateDirectory(_root); + } + + public static TheoryData Modes => new() { "columnar", "page-based" }; + + public void Dispose() + { + try + { + if (Directory.Exists(_root)) + { + Directory.Delete(_root, true); + } + } + catch (IOException) + { + // Best-effort cleanup of a temp directory. + } + } + + private (IDatabase Db, string Path) Open(string mode, string ddl) + { + var path = Path.Combine(_root, mode == "single-file" ? $"db_{Guid.NewGuid():N}.scdb" : $"db_{Guid.NewGuid():N}"); + var db = Reopen(mode, path); + db.ExecuteSQL(mode == "page-based" ? ddl + " STORAGE = PAGE_BASED" : ddl); + return (db, path); + } + + private IDatabase Reopen(string mode, string path) => mode == "single-file" + ? _factory.CreateWithOptions(path, "pw", DatabaseOptions.CreateSingleFileDefault()) + : _factory.Create(path, "pw"); + + private static string[] Rows(IDatabase db, string sql) => + [.. db.ExecuteQuery(sql).Select(r => string.Join(",", r.Values.Select(v => v is null or DBNull ? "NULL" : Convert.ToString(v, CultureInfo.InvariantCulture))))]; + + [Theory] + [MemberData(nameof(Modes))] + public void Insert_SatisfyingATableLevelCheck_IsAccepted_AndViolatingItIsRejected(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, lo INTEGER, hi INTEGER, CHECK (lo <= hi))"); + + db.ExecuteSQL("INSERT INTO t (id, lo, hi) VALUES (1, 1, 2)"); + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (id, lo, hi) VALUES (2, 3, 2)")); + + Assert.Equal(["1,1,2"], Rows(db, "SELECT id, lo, hi FROM t")); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Insert_NamedTableCheckWithNotEqualOperator_IsEnforced(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER, CONSTRAINT ck_t CHECK (n <> id))"); + + db.ExecuteSQL("INSERT INTO t (id, n) VALUES (1, 0)"); + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (id, n) VALUES (2, 2)")); + + Assert.Equal(["1,0"], Rows(db, "SELECT id, n FROM t")); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Insert_NullIntoACheckedColumn_PassesTheCheck(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, price REAL CHECK (price > 0))"); + + db.ExecuteSQL("INSERT INTO t (id, price) VALUES (1, NULL)"); + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (id, price) VALUES (2, -1)")); + + Assert.Equal(["1,NULL"], Rows(db, "SELECT id, price FROM t")); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Insert_CheckCallingAFunction_IsEnforced(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, code TEXT CHECK (length(code) = 3))"); + + db.ExecuteSQL("INSERT INTO t (id, code) VALUES (1, 'abc')"); + Assert.ThrowsAny(() => db.ExecuteSQL("INSERT INTO t (id, code) VALUES (2, 'abcd')")); + + Assert.Equal(["1,abc"], Rows(db, "SELECT id, code FROM t")); + } +} diff --git a/tests/SharpCoreDB.Tests/UpdateSetExpressionTests.cs b/tests/SharpCoreDB.Tests/UpdateSetExpressionTests.cs new file mode 100644 index 00000000..b19d74ad --- /dev/null +++ b/tests/SharpCoreDB.Tests/UpdateSetExpressionTests.cs @@ -0,0 +1,202 @@ +// +// 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.Tests; + +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using SharpCoreDB.Interfaces; +using Xunit; + +/// +/// An UPDATE whose SET value is an expression (SET n = n + 1), statement atomicity of a multi-row UPDATE, and an +/// UPDATE without WHERE. +/// +/// Up to 2.1.4 every SET value was parsed as a literal of the column's type: SET n = n + 5 failed ("Invalid value +/// 'n + 5'") and SET label = name on a TEXT column stored the four letters name. NOT NULL and CHECK were +/// checked row by row as rows were written, so a statement that failed on its second row kept its first. And an UPDATE +/// without WHERE threw "Invalid UPDATE syntax", although SQLite changes every row. +/// +/// +public sealed class UpdateSetExpressionTests : IDisposable +{ + private readonly DatabaseFactory _factory; + private readonly string _root = Path.Combine(Path.GetTempPath(), $"SCDB_UpdateSet_{Guid.NewGuid():N}"); + + public UpdateSetExpressionTests() + { + var services = new ServiceCollection(); + services.AddSharpCoreDB(); + _factory = services.BuildServiceProvider().GetRequiredService(); + Directory.CreateDirectory(_root); + } + + public static TheoryData Modes => new() { "columnar", "page-based" }; + + public void Dispose() + { + try + { + if (Directory.Exists(_root)) + { + Directory.Delete(_root, true); + } + } + catch (IOException) + { + // Best-effort cleanup of a temp directory. + } + } + + private (IDatabase Db, string Path) Open(string mode, string ddl) + { + var path = Path.Combine(_root, mode == "single-file" ? $"db_{Guid.NewGuid():N}.scdb" : $"db_{Guid.NewGuid():N}"); + var db = Reopen(mode, path); + db.ExecuteSQL(mode == "page-based" ? ddl + " STORAGE = PAGE_BASED" : ddl); + return (db, path); + } + + private IDatabase Reopen(string mode, string path) => mode == "single-file" + ? _factory.CreateWithOptions(path, "pw", DatabaseOptions.CreateSingleFileDefault()) + : _factory.Create(path, "pw"); + + private static string[] Rows(IDatabase db, string sql) => + [.. db.ExecuteQuery(sql).Select(r => string.Join(",", r.Values.Select(v => v is null or DBNull ? "NULL" : Convert.ToString(v, CultureInfo.InvariantCulture))))]; + + [Theory] + [MemberData(nameof(Modes))] + public void Update_SetValueReadsTheRow_IsComputedPerRow(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER, name TEXT, label TEXT)"); + db.ExecuteSQL("INSERT INTO t (id, n, name, label) VALUES (1, 1, 'ann', 'x'), (2, 2, 'bob', 'y')"); + + db.ExecuteSQL("UPDATE t SET n = n * 10 + id, label = upper(name) || '-' || n WHERE id > 0"); + + Assert.Equal(["1,11,ann,ANN-1", "2,22,bob,BOB-2"], Rows(db, "SELECT id, n, name, label FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_SwappingTwoColumns_ReadsTheValuesFromBeforeTheStatement(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, a TEXT, b TEXT)"); + db.ExecuteSQL("INSERT INTO t (id, a, b) VALUES (1, 'left', 'right')"); + + db.ExecuteSQL("UPDATE t SET a = b, b = a WHERE id = 1"); + + Assert.Equal(["1,right,left"], Rows(db, "SELECT id, a, b FROM t")); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_ConstantExpression_IsEvaluatedForANumericColumn(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER)"); + db.ExecuteSQL("INSERT INTO t (id, n) VALUES (1, 0)"); + + db.ExecuteSQL("UPDATE t SET n = 2 * 3 WHERE id = 1"); + + Assert.Equal(["1,6"], Rows(db, "SELECT id, n FROM t")); + } + + [Theory] + [MemberData(nameof(Modes))] + public void BatchUpdate_SetValueReadsTheRow_SeesTheEarlierStatement(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER)"); + db.ExecuteSQL("INSERT INTO t (id, n) VALUES (1, 1), (2, 2)"); + + db.ExecuteBatchSQL(["UPDATE t SET n = n + 1 WHERE id = 1", "UPDATE t SET n = n + 1 WHERE id = 1"]); + + Assert.Equal(["1,3", "2,2"], Rows(db, "SELECT id, n FROM t").Order()); + } + + [Fact] + public void Update_SetValueReadsTheRow_IsComputedPerRowOnSingleFile() + { + var (db, _) = Open("single-file", "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER, name TEXT)"); + db.ExecuteSQL("INSERT INTO t (id, n, name) VALUES (1, 1, 'ann'), (2, 2, 'bob')"); + + db.ExecuteSQL("UPDATE t SET n = n + 5, name = upper(name) WHERE id > 0"); + + Assert.Equal(["1,6,ANN", "2,7,BOB"], Rows(db, "SELECT id, n, name FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_CheckFailsOnALaterRow_LeavesEveryRowUnchanged(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER CHECK (n < 10))"); + db.ExecuteSQL("INSERT INTO t (id, n) VALUES (1, 1), (2, 8), (3, 2)"); + + Assert.ThrowsAny(() => db.ExecuteSQL("UPDATE t SET n = n + 5 WHERE id > 0")); + + Assert.Equal(["1,1", "2,8", "3,2"], Rows(db, "SELECT id, n FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_LiteralFailingAColumnCheckOnALaterRow_LeavesEveryRowUnchanged(string mode) + { + // Row 1 passes (2 > 1), row 2 fails (2 > 2): up to 2.1.4 row 1 was already written when row 2 failed. + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER CHECK (n > id))"); + db.ExecuteSQL("INSERT INTO t (id, n) VALUES (1, 5), (2, 5), (3, 5)"); + + Assert.ThrowsAny(() => db.ExecuteSQL("UPDATE t SET n = 2 WHERE id > 0")); + + Assert.Equal(["1,5", "2,5", "3,5"], Rows(db, "SELECT id, n FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_LiteralFailingATableCheckOnALaterRow_LeavesEveryRowUnchanged(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER, CHECK (n <> id))"); + db.ExecuteSQL("INSERT INTO t (id, n) VALUES (1, 0), (2, 0), (3, 0)"); + + Assert.ThrowsAny(() => db.ExecuteSQL("UPDATE t SET n = 2 WHERE id > 0")); + + Assert.Equal(["1,0", "2,0", "3,0"], Rows(db, "SELECT id, n FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_NotNullFailsOnALaterRow_LeavesEveryRowUnchanged(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER NOT NULL, o INTEGER)"); + db.ExecuteSQL("INSERT INTO t (id, n, o) VALUES (1, 1, 1), (2, 2, NULL), (3, 3, 3)"); + + Assert.ThrowsAny(() => db.ExecuteSQL("UPDATE t SET n = o WHERE id > 0")); + + Assert.Equal(["1,1,1", "2,2,NULL", "3,3,3"], Rows(db, "SELECT id, n, o FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_WithoutWhere_ChangesEveryRow(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, label TEXT)"); + db.ExecuteSQL("INSERT INTO t (id, label) VALUES (1, 'a'), (2, 'b')"); + + db.ExecuteSQL("UPDATE t SET label = 'z'"); + + Assert.Equal(["1,z", "2,z"], Rows(db, "SELECT id, label FROM t").Order()); + } + + [Theory] + [MemberData(nameof(Modes))] + public void Update_LiteralContainingTheWordWhere_KeepsTheWholeLiteral(string mode) + { + var (db, _) = Open(mode, "CREATE TABLE t (id INTEGER PRIMARY KEY, label TEXT)"); + db.ExecuteSQL("INSERT INTO t (id, label) VALUES (1, 'a'), (2, 'b')"); + + db.ExecuteSQL("UPDATE t SET label = 'see where it goes' WHERE id = 2"); + + Assert.Equal(["1,a", "2,see where it goes"], Rows(db, "SELECT id, label FROM t").Order()); + } +}