fix: v0.7.4 写语句子查询 / 约束硬化 / 真惰性流式 — UPDATE-DELETE WHERE 子查询静默 0 行(四引擎,关联引用显式 NOT_SUPPORTED + EXPLAIN 同步)/ 主键 NULL-undefined 强制拒绝 / DROP INDEX 保留建表 UNIQUE(仅索引来源可解除)/ GROUP BY-DISTINCT-UNION 键类型安全编码 / UPDATE 未知列报错 / queryStream 多语句拒绝 / KVStore 后台错误跨 reopen 清理 + Hybrid begin 补偿 / RB-Tree 删除双黑修复 + LSM 死代码清理 / findStream 迭代器化真惰性(limit 早停 O(1) 内存)/ REINDEX 单次扫描 + 48 回归
This commit is contained in:
+62
-2
@@ -6,7 +6,7 @@
|
||||
import type { IStorageEngine } from './interface';
|
||||
import type { QueryPlan, TableSchema, WhereCondition } from '../constants';
|
||||
import { DatabaseError } from '../constants';
|
||||
import { matchWhere, applyOrderBy, projectColumns } from '../query/where-matcher';
|
||||
import { matchWhere, applyOrderBy, projectColumns, containsUnresolvedSubqueries } from '../query/where-matcher';
|
||||
import { stripUndefinedUpdates } from '../table/schema';
|
||||
|
||||
export class MemoryEngine implements IStorageEngine {
|
||||
@@ -18,6 +18,12 @@ export class MemoryEngine implements IStorageEngine {
|
||||
private opened = false;
|
||||
/** v0.4.2-fix: 库内元数据(迁移版本持久化用) */
|
||||
private metaStore: Map<string, string> = new Map();
|
||||
/**
|
||||
* v0.7.4: 由 CREATE UNIQUE INDEX 添加的 unique 列(table:col)。
|
||||
* 与建表 UNIQUE 约束区分:DROP INDEX 只允许解除索引来源的 unique,
|
||||
* 建表约束需重建表(对齐 SQLite 语义,此前静默解除且不可恢复)。
|
||||
*/
|
||||
private uniqueIndexCols: Set<string> = new Set();
|
||||
|
||||
// ---- 事务快照 ----
|
||||
private snapshot: {
|
||||
@@ -82,6 +88,11 @@ export class MemoryEngine implements IStorageEngine {
|
||||
async dropTable(tableName: string): Promise<void> {
|
||||
this.ensureTable(tableName);
|
||||
this.schemas.delete(tableName); this.tables.delete(tableName); this.indexes.delete(tableName);
|
||||
// v0.7.4: 清理该表的 unique 索引来源标记(重建同名表不残留)
|
||||
const prefix = `${tableName}:`;
|
||||
for (const key of this.uniqueIndexCols) {
|
||||
if (key.startsWith(prefix)) this.uniqueIndexCols.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
async hasTable(tableName: string): Promise<boolean> { return this.schemas.has(tableName); }
|
||||
@@ -229,6 +240,23 @@ export class MemoryEngine implements IStorageEngine {
|
||||
// v0.7.2: undefined 值视为"不更新该列"(保留旧值),null 显式置空
|
||||
const cleanUpdates = stripUndefinedUpdates(updates);
|
||||
|
||||
// v0.7.4: 防御 —— QueryBuilder 直通引擎不经 Executor 子查询解析,
|
||||
// 未解析的 $subquery/$col/$exists 在 matchWhere 中恒 false → 静默 0 行
|
||||
if (containsUnresolvedSubqueries(query.where)) {
|
||||
throw new DatabaseError(
|
||||
'Unresolved subqueries/column references in UPDATE WHERE (use db.query() to execute subqueries)',
|
||||
'NOT_SUPPORTED',
|
||||
);
|
||||
}
|
||||
|
||||
// v0.7.4: 未知列显式报错 —— 此前 SET nonexistent = ... 被静默写入存储行
|
||||
// (validateRow 只遍历 schema 列,脏列残留在行内并随持久化落盘)
|
||||
for (const col of Object.keys(cleanUpdates)) {
|
||||
if (!schema.columns[col]) {
|
||||
throw new DatabaseError(`Column "${col}" does not exist in table "${tableName}"`, 'COLUMN_NOT_FOUND');
|
||||
}
|
||||
}
|
||||
|
||||
// v0.7.2: 语句级原子性 — 两阶段(先全量预检,后执行)。
|
||||
// 此前逐行"校验+写入":第 N 行唯一冲突/校验失败抛错时,前 N-1 行已写入
|
||||
// → 无事务下语句级部分提交(数据半更新且调用方已收到错误)。
|
||||
@@ -421,6 +449,14 @@ export class MemoryEngine implements IStorageEngine {
|
||||
|
||||
async delete(tableName: string, query: QueryPlan): Promise<number> {
|
||||
this.ensureTable(tableName);
|
||||
// v0.7.4: 防御 —— QueryBuilder 直通引擎不经 Executor 子查询解析,
|
||||
// 未解析的 $subquery/$col/$exists 在 matchWhere 中恒 false → 静默 0 行
|
||||
if (containsUnresolvedSubqueries(query.where)) {
|
||||
throw new DatabaseError(
|
||||
'Unresolved subqueries/column references in DELETE WHERE (use db.query() to execute subqueries)',
|
||||
'NOT_SUPPORTED',
|
||||
);
|
||||
}
|
||||
const table = this.tables.get(tableName)!;
|
||||
const toDelete: { pk: string; row: Record<string, unknown> }[] = [];
|
||||
for (const [pk, row] of table) {
|
||||
@@ -551,7 +587,11 @@ export class MemoryEngine implements IStorageEngine {
|
||||
throw error;
|
||||
}
|
||||
colDef.index = true;
|
||||
if (unique) colDef.unique = true;
|
||||
if (unique) {
|
||||
colDef.unique = true;
|
||||
// v0.7.4: 记录唯一约束来源(DROP INDEX 时可解除;建表约束不可)
|
||||
this.uniqueIndexCols.add(`${tableName}:${column}`);
|
||||
}
|
||||
}
|
||||
|
||||
async dropIndex(tableName: string, column: string, _indexName?: string): Promise<void> {
|
||||
@@ -570,8 +610,20 @@ export class MemoryEngine implements IStorageEngine {
|
||||
if (!colDef.index && !colDef.unique) {
|
||||
throw new DatabaseError(`Index on column "${column}" does not exist in table "${tableName}"`, 'INDEX_NOT_FOUND');
|
||||
}
|
||||
// v0.7.4: 建表 UNIQUE 约束不可通过 DROP INDEX 解除 —— 此前 colDef.unique = false
|
||||
// 静默解除约束(后续唯一性检查失效、重复数据入库)。对齐 SQLite 语义:
|
||||
// 约束随建表存在,解除需重建表;仅 CREATE UNIQUE INDEX 添加的约束可随索引删除。
|
||||
const uniqueKey = `${tableName}:${column}`;
|
||||
if (colDef.unique && !this.uniqueIndexCols.has(uniqueKey)) {
|
||||
throw new DatabaseError(
|
||||
`Cannot drop index on column "${column}" in table "${tableName}": ` +
|
||||
'UNIQUE constraint defined at table creation must be removed by recreating the table',
|
||||
'NOT_SUPPORTED',
|
||||
);
|
||||
}
|
||||
colDef.index = false;
|
||||
colDef.unique = false;
|
||||
this.uniqueIndexCols.delete(uniqueKey);
|
||||
const tableIndexes = this.indexes.get(tableName);
|
||||
if (tableIndexes) tableIndexes.delete(column);
|
||||
}
|
||||
@@ -644,6 +696,14 @@ export class MemoryEngine implements IStorageEngine {
|
||||
if (colDef.required && (value === undefined || value === null)) {
|
||||
throw new DatabaseError(`Column "${colName}" is required in table "${schema.name}"`, 'VALIDATION_ERROR');
|
||||
}
|
||||
// v0.7.4: 主键列强制非空(SQL 语义 PK 隐含 NOT NULL)——
|
||||
// 此前 null/undefined 主键被 String() 化为 "null"/"undefined" 静默入库
|
||||
if (colDef.primaryKey && (value === undefined || value === null)) {
|
||||
throw new DatabaseError(
|
||||
`Primary key column "${colName}" in table "${schema.name}" cannot be null or undefined`,
|
||||
'VALIDATION_ERROR',
|
||||
);
|
||||
}
|
||||
if (value !== undefined && value !== null) this.checkType(colName, colDef.type, value);
|
||||
if (value !== undefined) validated[colName] = value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user