UNPKG

@sqb/builder

Version:

Extensible multi-dialect SQL query builder written with TypeScript

54 lines (53 loc) 1.66 kB
import { SerializationType } from '../enums.js'; import { OpAnd } from '../sql-objects/operators/op-and.js'; import { TableName } from '../sql-objects/table-name.js'; import { isRawStatement } from '../typeguards.js'; import { Query } from './query.js'; export class DeleteQuery extends Query { _table; _where; constructor(tableName) { super(); if (!tableName || !(typeof tableName === 'string' || isRawStatement(tableName))) { throw new TypeError('String or Raw instance required as first argument (tableName) for UpdateQuery'); } this._table = typeof tableName === 'string' ? new TableName(tableName) : tableName; } get _type() { return SerializationType.DELETE_QUERY; } /** * Defines "where" part of query */ where(...operator) { this._where = this._where || new OpAnd(); this._where.add(...operator); return this; } /** * Performs serialization */ _serialize(ctx) { const o = { table: this._table._serialize(ctx), where: this._serializeWhere(ctx), }; return ctx.serialize(this._type, o, () => this.__defaultSerialize(ctx, o)); } __defaultSerialize(ctx, o) { return 'delete from ' + o.table + (o.where ? '\n' + o.where : ''); } /** * */ _serializeWhere(ctx) { if (!this._where) return ''; const s = this._where._serialize(ctx); return ctx.serialize(SerializationType.CONDITIONS_BLOCK, s, () => /* istanbul ignore next */ s ? 'where ' + s : ''); } }