import { Transaction, TransactionContext, TransactionSelection, TransactionUpdation, TransactionDeletion, Condition, BuildQueryResult, StatementQueryOptions, } from '../types.js'; import type { Database } from './database.js'; /** * 事务查询选择器实现 */ class TrxSelection, T extends keyof S> implements TransactionSelection { private conditions: Condition = {}; private orderings: { field: keyof S[T]; direction: 'ASC' | 'DESC' }[] = []; private limitCount?: number; private offsetCount?: number; constructor( private readonly db: Database, private readonly trx: Transaction, private readonly tableName: T, private readonly fields?: (keyof S[T])[] ) {} where(condition: Condition): this { this.conditions = { ...this.conditions, ...condition }; return this; } orderBy(field: keyof S[T], direction: 'ASC' | 'DESC' = 'ASC'): this { this.orderings.push({ field, direction }); return this; } limit(count: number): this { this.limitCount = count; return this; } offset(count: number): this { this.offsetCount = count; return this; } then(onfulfilled?: ((value: S[T][]) => R | PromiseLike) | null, onrejected?: ((reason: unknown) => E | PromiseLike) | null): Promise { const { query, params } = this.db.buildQuery({ type: 'select', tableName: this.tableName, fields: this.fields, conditions: this.conditions, orderings: this.orderings, limitCount: this.limitCount, offsetCount: this.offsetCount }); return this.trx.query(query, params).then(onfulfilled, onrejected); } } /** * 事务更新器实现 */ class TrxUpdation, T extends keyof S> implements TransactionUpdation { private conditions: Condition = {}; constructor( private readonly db: Database, private readonly trx: Transaction, private readonly tableName: T, private readonly data: Partial ) {} where(condition: Condition): this { this.conditions = { ...this.conditions, ...condition }; return this; } then(onfulfilled?: ((value: number) => R | PromiseLike) | null, onrejected?: ((reason: unknown) => E | PromiseLike) | null): Promise { const { query, params } = this.db.buildQuery({ type: 'update', tableName: this.tableName, update: this.data, conditions: this.conditions }); return this.trx.query( this.db.dialectName === 'pg' ? `${query} RETURNING 1` : query, params) .then(affectedRows).then(onfulfilled, onrejected); } } /** * 事务删除器实现 */ class TrxDeletion, T extends keyof S> implements TransactionDeletion { private conditions: Condition = {}; constructor( private readonly db: Database, private readonly trx: Transaction, private readonly tableName: T ) {} where(condition: Condition): this { this.conditions = { ...this.conditions, ...condition }; return this; } then(onfulfilled?: ((value: number) => R | PromiseLike) | null, onrejected?: ((reason: unknown) => E | PromiseLike) | null): Promise { const { query, params } = this.db.buildQuery({ type: 'delete', tableName: this.tableName, conditions: this.conditions }); return this.trx.query( this.db.dialectName === 'pg' ? `${query} RETURNING 1` : query, params) .then(affectedRows).then(onfulfilled, onrejected); } } function affectedRows(result: number | unknown[] | { affectedRows?: number; changes?: number }): number { if (Array.isArray(result)) return result.length; return typeof result === 'number' ? result : result.affectedRows ?? result.changes ?? 0; } /** * 增强的事务上下文实现 * 支持链式调用的事务操作 */ export class TransactionContextImpl> implements TransactionContext { constructor( private readonly db: Database, private readonly trx: Transaction ) {} /** * 提交事务 */ async commit(): Promise { return this.trx.commit(); } /** * 回滚事务 */ async rollback(): Promise { return this.trx.rollback(); } /** * 执行原生 SQL */ async query(sql: string, params?: any[]): Promise { return this.trx.query(sql, params); } /** One statement through the single-statement protocol of the dialect (see {@link Transaction.queryStatement}). */ async queryStatement(sql: string, options?: StatementQueryOptions): Promise { if (!this.trx.queryStatement) throw new Error('This database does not support single-statement queries'); return this.trx.queryStatement(sql, options); } /** * 插入单条数据 */ async insert(tableName: T, data: S[T]): Promise { const { query, params } = this.db.buildQuery({ type: 'insert', tableName, data }); await this.trx.query(query, params); return data; } /** * 批量插入数据 */ async insertMany(tableName: T, data: S[T][]): Promise<{ affectedRows: number }> { if (!data.length) { return { affectedRows: 0 }; } const { query, params } = this.db.buildQuery({ type: 'insert_many', tableName, data }); const result = await this.trx.query<{ affectedRows?: number; changes?: number }>(query, params); return { affectedRows: result.affectedRows ?? result.changes ?? data.length }; } /** * 查询数据 - 返回链式选择器 */ select(tableName: T, fields?: (keyof S[T])[]): TransactionSelection { return new TrxSelection(this.db, this.trx, tableName, fields); } /** * 更新数据 - 返回链式更新器 */ update(tableName: T, data: Partial): TransactionUpdation { return new TrxUpdation(this.db, this.trx, tableName, data); } /** * 删除数据 - 返回链式删除器 */ delete(tableName: T): TransactionDeletion { return new TrxDeletion(this.db, this.trx, tableName); } }