feat: v0.1.13 — 事务回滚 + 子查询 + 外键级联 + 连接池
This commit is contained in:
+118
-1
@@ -16,6 +16,13 @@ export class MemoryEngine implements IStorageEngine {
|
||||
private indexes: Map<string, Map<string, Map<unknown, Set<string>>>> = new Map();
|
||||
private opened = false;
|
||||
|
||||
// ---- 事务快照 ----
|
||||
private snapshot: {
|
||||
tables: Map<string, Map<string, Record<string, unknown>>>;
|
||||
schemas: Map<string, TableSchema>;
|
||||
indexes: Map<string, Map<string, Map<unknown, Set<string>>>>;
|
||||
} | null = null;
|
||||
|
||||
// ---- 生命周期 ----
|
||||
async open(_dbName: string, _version: number): Promise<void> { this.opened = true; }
|
||||
async close(): Promise<void> {
|
||||
@@ -109,8 +116,14 @@ export class MemoryEngine implements IStorageEngine {
|
||||
toDelete.push(pk);
|
||||
}
|
||||
}
|
||||
// 级联删除:检查引用此表的其他表
|
||||
let cascadeCount = 0;
|
||||
for (const pk of toDelete) {
|
||||
const row = table.get(pk);
|
||||
if (row) cascadeCount += await this.cascadeDelete(tableName, pk, row);
|
||||
}
|
||||
for (const pk of toDelete) table.delete(pk);
|
||||
return toDelete.length;
|
||||
return toDelete.length + cascadeCount;
|
||||
}
|
||||
|
||||
async count(tableName: string, query?: QueryPlan): Promise<number> {
|
||||
@@ -129,6 +142,56 @@ export class MemoryEngine implements IStorageEngine {
|
||||
if (tableIndexes) for (const colIndex of tableIndexes.values()) colIndex.clear();
|
||||
}
|
||||
|
||||
// ---- 事务 ----
|
||||
|
||||
async beginTransaction(): Promise<void> {
|
||||
if (this.snapshot) throw new DatabaseError('Transaction already in progress', 'TX_ACTIVE');
|
||||
this.snapshot = {
|
||||
tables: this.deepCloneMapMap(this.tables),
|
||||
schemas: new Map(this.schemas),
|
||||
indexes: this.deepCloneIndexes(this.indexes),
|
||||
};
|
||||
}
|
||||
|
||||
async commitTransaction(): Promise<void> {
|
||||
if (!this.snapshot) throw new DatabaseError('No active transaction', 'TX_NONE');
|
||||
this.snapshot = null;
|
||||
}
|
||||
|
||||
async rollbackTransaction(): Promise<void> {
|
||||
if (!this.snapshot) throw new DatabaseError('No active transaction', 'TX_NONE');
|
||||
this.tables = this.snapshot.tables;
|
||||
this.schemas = this.snapshot.schemas;
|
||||
this.indexes = this.snapshot.indexes;
|
||||
this.snapshot = null;
|
||||
}
|
||||
|
||||
// ---- 事务快照辅助 ----
|
||||
|
||||
private deepCloneMapMap(source: Map<string, Map<string, Record<string, unknown>>>): Map<string, Map<string, Record<string, unknown>>> {
|
||||
const clone = new Map<string, Map<string, Record<string, unknown>>>();
|
||||
for (const [k, v] of source) {
|
||||
const innerClone = new Map<string, Record<string, unknown>>();
|
||||
for (const [ik, iv] of v) innerClone.set(ik, { ...iv });
|
||||
clone.set(k, innerClone);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
private deepCloneIndexes(source: Map<string, Map<string, Map<unknown, Set<string>>>>): Map<string, Map<string, Map<unknown, Set<string>>>> {
|
||||
const clone = new Map<string, Map<string, Map<unknown, Set<string>>>>();
|
||||
for (const [tableName, tableIndexes] of source) {
|
||||
const tableClone = new Map<string, Map<unknown, Set<string>>>();
|
||||
for (const [col, colIndex] of tableIndexes) {
|
||||
const colClone = new Map<unknown, Set<string>>();
|
||||
for (const [val, pkSet] of colIndex) colClone.set(val, new Set(pkSet));
|
||||
tableClone.set(col, colClone);
|
||||
}
|
||||
clone.set(tableName, tableClone);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
// ---- 内部辅助 ----
|
||||
private ensureTable(tableName: string): void {
|
||||
if (!this.tables.has(tableName)) throw new DatabaseError(`Table "${tableName}" does not exist`, 'TABLE_NOT_FOUND');
|
||||
@@ -212,4 +275,58 @@ export class MemoryEngine implements IStorageEngine {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 外键级联 ----
|
||||
|
||||
/**
|
||||
* 级联删除:查找引用 tableName.pkValue 的所有表的行并删除。
|
||||
* @returns 级联删除的行数
|
||||
*/
|
||||
private async cascadeDelete(tableName: string, pkValue: string, _row: Record<string, unknown>): Promise<number> {
|
||||
let totalCascade = 0;
|
||||
|
||||
for (const [refTableName, refSchema] of this.schemas) {
|
||||
if (refTableName === tableName) continue;
|
||||
|
||||
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||
if (!colDef.references || !colDef.onDelete || colDef.onDelete === 'RESTRICT') continue;
|
||||
|
||||
const [refTable, refCol] = colDef.references.split('.');
|
||||
if (refTable !== tableName) continue;
|
||||
|
||||
const refPkCol = refCol;
|
||||
const refTableData = this.tables.get(refTableName);
|
||||
if (!refTableData) continue;
|
||||
|
||||
// 查找所有引用此主键的行
|
||||
const toDelete: string[] = [];
|
||||
for (const [refPk, refRow] of refTableData) {
|
||||
if (String(refRow[colName]) === pkValue) {
|
||||
toDelete.push(refPk);
|
||||
}
|
||||
}
|
||||
|
||||
if (colDef.onDelete === 'CASCADE') {
|
||||
// 递归级联
|
||||
for (const refPk of toDelete) {
|
||||
const refRow = refTableData.get(refPk);
|
||||
if (refRow) {
|
||||
totalCascade += await this.cascadeDelete(refTableName, refPk, refRow);
|
||||
}
|
||||
refTableData.delete(refPk);
|
||||
totalCascade++;
|
||||
}
|
||||
} else if (colDef.onDelete === 'SET NULL') {
|
||||
for (const refPk of toDelete) {
|
||||
const refRow = refTableData.get(refPk);
|
||||
if (refRow) {
|
||||
refRow[colName] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return totalCascade;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user