You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.1 KiB
125 lines
3.1 KiB
import ConnectionMutable from './ConnectionMutable.ts'
|
|
import {MalformedSQLGrammarError} from './Select.ts'
|
|
|
|
/**
|
|
* Base query builder class for CREATE DATABASE queries.
|
|
* @extends ConnectionMutable
|
|
*/
|
|
export class CreateDatabase<T> extends ConnectionMutable<T> {
|
|
protected _name?: string
|
|
protected _owner?: string
|
|
protected _template?: string
|
|
protected _encoding?: string
|
|
protected _lc_collate?: string
|
|
protected _lc_ctype?: string
|
|
protected _tablespace?: string
|
|
protected _conn_limit?: number
|
|
protected _is_template?: boolean
|
|
protected _allow_connections?: boolean
|
|
|
|
constructor(name?: string) {
|
|
super()
|
|
if ( name ) this._name = name
|
|
}
|
|
|
|
sql(level: number = 0): string {
|
|
const indent = Array(level).fill(' ').join('')
|
|
|
|
if ( !this._name ) {
|
|
throw new MalformedSQLGrammarError(`Database name to create is undefined.`)
|
|
}
|
|
|
|
const key_pairs: (string|number)[][] = []
|
|
if ( this._owner ) {
|
|
key_pairs.push(['OWNER', this._owner])
|
|
}
|
|
|
|
if ( this._template ) {
|
|
key_pairs.push(['TEMPLATE', this._template])
|
|
}
|
|
|
|
if ( this._encoding ) {
|
|
key_pairs.push(['ENCODING', this._encoding])
|
|
}
|
|
|
|
if ( this._lc_collate ) {
|
|
key_pairs.push(['LC_COLLATE', this._lc_collate])
|
|
}
|
|
|
|
if ( this._lc_ctype ) {
|
|
key_pairs.push(['LC_CTYPE', this._lc_ctype])
|
|
}
|
|
|
|
if ( this._tablespace ) {
|
|
key_pairs.push(['TABLESPACE', this._tablespace])
|
|
}
|
|
|
|
if ( this._conn_limit ) {
|
|
key_pairs.push(['CONNECTION LIMIT', this._conn_limit])
|
|
}
|
|
|
|
if ( typeof this._allow_connections !== 'undefined' ) {
|
|
key_pairs.push(['ALLOW_CONNECTIONS', this._allow_connections ? 'TRUE' : 'FALSE'])
|
|
}
|
|
|
|
if ( typeof this._is_template !== 'undefined' ) {
|
|
key_pairs.push(['IS_TEMPLATE', this._is_template ? 'TRUE' : 'FALSE'])
|
|
}
|
|
|
|
return [
|
|
`CREATE DATABASE ${this._name}`,
|
|
...(key_pairs.length < 1 ? [] : [`${indent}WITH`, ...key_pairs.map(x => `${indent}${indent}${x[0]} ${x[1]}`)]),
|
|
].filter(x => String(x).trim()).join(`\n${indent}`)
|
|
}
|
|
|
|
name(name: string): this {
|
|
this._name = name
|
|
return this
|
|
}
|
|
|
|
owner(user: string): this {
|
|
this._owner = user
|
|
return this
|
|
}
|
|
|
|
template(name: string): this {
|
|
this._template = name
|
|
return this
|
|
}
|
|
|
|
encoding(name: string): this {
|
|
this._encoding = name
|
|
return this
|
|
}
|
|
|
|
lc_collate(type: string): this {
|
|
this._lc_collate = type
|
|
return this
|
|
}
|
|
|
|
lc_ctype(type: string): this {
|
|
this._lc_ctype = type
|
|
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
|
|
}
|
|
|
|
disallow_connections(): this {
|
|
this._allow_connections = false
|
|
return this
|
|
}
|
|
}
|