import ConnectionMutable from "./ConnectionMutable.ts"; import {MalformedSQLGrammarError} from "./Select.ts"; import {TableRefBuilder} from "./TableRefBuilder.ts"; import {applyMixins} from "../../../../lib/src/support/mixins.ts"; import {QuerySource} from "../types.ts"; export class Truncate extends ConnectionMutable { protected _source?: QuerySource protected _only: boolean = false protected _restart: boolean = false protected _continue: boolean = false protected _cascade: boolean = false protected _restrict: boolean = false constructor(table?: QuerySource, alias?: string) { super() if ( table ) this.table(table, alias) } sql(level: number = 0): string { const indent = Array(level).fill(' ').join('') if ( typeof this._source === 'undefined' ) throw new MalformedSQLGrammarError(`No table reference has been provided.`) const table_ref = this.source_alias_to_table_ref(this._source) let identity = '' if ( this._continue ) identity = 'CONTINUE IDENTITY ' else if ( this._restart ) identity = 'RESTART IDENTITY ' let cascade_redirect = '' if ( this._cascade ) cascade_redirect = 'CASCADE ' else if ( this._restrict ) cascade_redirect = 'RESTRICT' return [ `TRUNCATE TABLE ${this._only ? 'ONLY ' : ''}${this.serialize_table_ref(table_ref)}`, `${identity}${cascade_redirect}`, ].filter(x => String(x).trim()).join(`\n${indent}`) } table(source: QuerySource, alias?: string) { if ( !alias ) this._source = source else this._source = { ref: source, alias } return this } restart_identity() { this._continue = false this._restart = true return this } continue_identity() { this._continue = true this._restart = false return this } cascade() { this._cascade = true this._restrict = false return this } restrict() { this._cascade = false this._restrict = true return this } } export interface Truncate extends TableRefBuilder {} applyMixins(Truncate, [TableRefBuilder])