import ConnectionMutable from './ConnectionMutable.ts' import {MalformedSQLGrammarError} from './Select.ts' import {escape, EscapedValue} from "../types.ts"; /** * Base query builder class for ALTER DATABASE queries. * @extends ConnectionMutable */ export class AlterDatabase extends ConnectionMutable { protected _name?: string protected _rename?: string protected _owner?: string protected _tablespace?: string protected _conn_limit?: number protected _is_template?: boolean protected _allow_connections?: boolean protected _reset_all: boolean = false protected _config_resets: string[] = [] protected _config_from_current: string[] = [] protected _config_sets: (string|EscapedValue)[][] = [] constructor(name?: string) { super() if ( name ) this._name = name } sql(level: number = 0): string { if ( !this._name ) { throw new MalformedSQLGrammarError(`Database name to alter is undefined.`) } const queries = [] const option_pairs: (string|boolean|number)[][] = [] if ( typeof this._allow_connections !== 'undefined' ) { option_pairs.push(['ALLOW_CONNECTIONS', this._allow_connections]) } if ( typeof this._conn_limit !== 'undefined' ) { option_pairs.push(['CONNECTION LIMIT', this._conn_limit]) } if ( typeof this._is_template !== 'undefined' ) { option_pairs.push(['IS_TEMPLATE', this._is_template]) } if ( option_pairs.length > 0 ) { option_pairs.some(pair => { queries.push(`ALTER DATABASE ${this._name} WITH ${pair[0]} ${pair[1]}`) }) } if ( this._owner ) { queries.push(`ALTER DATABASE ${this._name} OWNER TO ${this._owner}`) } if ( this._tablespace ) { queries.push(`ALTER DATABASE ${this._name} SET TABLESPACE ${this._tablespace}`) } if ( this._reset_all ) { queries.push(`ALTER DATABASE ${this._name} RESET ALL`) } if ( this._config_resets.length > 0 ) { this._config_resets.some(param => { queries.push(`ALTER DATABASE ${this._name} RESET ${param}`) }) } if ( this._config_from_current.length > 0 ) { this._config_from_current.some(param => { queries.push(`ALTER DATABASE ${this._name} SET ${param} FROM CURRENT`) }) } if ( this._config_sets.length > 0 ) { this._config_sets.some(set => { queries.push(`ALTER DATABASE ${this._name} SET ${set[0]} TO ${escape(set[1])}`) }) } // This needs to happen last if ( this._rename ) { queries.push(`ALTER DATABASE ${this._name} RENAME TO ${this._rename}`) } return queries.join(';\n') } name(name: string): this { this._name = name return this } rename_to(name: string): this { this._rename = name return this } owner(user: string): this { this._owner = user return this } tablespace(name: string): this { this._tablespace = name return this } connection_limit(max: number): this { this._conn_limit = max return this } as_template(): this { this._is_template = true return this } not_as_template(): this { this._is_template = false return this } disallow_connections(): this { this._allow_connections = false return this } allow_connections(): this { this._allow_connections = true return this } reset_all(): this { this._reset_all = true return this } reset(parameter: string): this { this._config_resets.push(parameter) return this } set_from_current(parameter: string): this { this._config_from_current.push(parameter) return this } set(parameter: string, value: EscapedValue): this { this._config_sets.push([parameter, value]) return this } }