UNPKG

@sqb/builder

Version:

Extensible multi-dialect SQL query builder written with TypeScript

58 lines (57 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeleteQuery = void 0; const enums_js_1 = require("../enums.js"); const op_and_js_1 = require("../sql-objects/operators/op-and.js"); const table_name_js_1 = require("../sql-objects/table-name.js"); const typeguards_js_1 = require("../typeguards.js"); const query_js_1 = require("./query.js"); class DeleteQuery extends query_js_1.Query { _table; _where; constructor(tableName) { super(); if (!tableName || !(typeof tableName === 'string' || (0, typeguards_js_1.isRawStatement)(tableName))) { throw new TypeError('String or Raw instance required as first argument (tableName) for UpdateQuery'); } this._table = typeof tableName === 'string' ? new table_name_js_1.TableName(tableName) : tableName; } get _type() { return enums_js_1.SerializationType.DELETE_QUERY; } /** * Defines "where" part of query */ where(...operator) { this._where = this._where || new op_and_js_1.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(enums_js_1.SerializationType.CONDITIONS_BLOCK, s, () => /* istanbul ignore next */ s ? 'where ' + s : ''); } } exports.DeleteQuery = DeleteQuery;