knex/src/dialects/postgres/schema/tablecompiler.js

83 lines
3.4 KiB
JavaScript
Raw Normal View History

/* eslint max-len: 0 */
2015-05-09 13:58:18 -04:00
// PostgreSQL Table Builder & Compiler
// -------
import inherits from 'inherits';
import TableCompiler from '../../../schema/tablecompiler';
2015-05-09 13:58:18 -04:00
2016-03-02 16:52:32 +01:00
import {has} from 'lodash'
2015-05-09 13:58:18 -04:00
function TableCompiler_PG() {
TableCompiler.apply(this, arguments);
}
inherits(TableCompiler_PG, TableCompiler);
// Compile a rename column command.
TableCompiler_PG.prototype.renameColumn = function(from, to) {
return this.pushQuery({
sql: `alter table ${this.tableName()} rename ${this.formatter.wrap(from)} to ${this.formatter.wrap(to)}`
2015-05-09 13:58:18 -04:00
});
};
TableCompiler_PG.prototype.compileAdd = function(builder) {
const table = this.formatter.wrap(builder);
const columns = this.prefixArray('add column', this.getColumns(builder));
2015-05-09 13:58:18 -04:00
return this.pushQuery({
sql: `alter table ${table} ${columns.join(', ')}`
2015-05-09 13:58:18 -04:00
});
};
// Adds the "create" query to the query sequence.
TableCompiler_PG.prototype.createQuery = function(columns, ifNot) {
const createStatement = ifNot ? 'create table if not exists ' : 'create table ';
let sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
if (this.single.inherits) sql += ` inherits (${this.formatter.wrap(this.single.inherits)})`;
2015-05-09 13:58:18 -04:00
this.pushQuery({
sql,
2015-05-09 13:58:18 -04:00
bindings: columns.bindings
});
const hasComment = has(this.single, 'comment');
2015-05-09 13:58:18 -04:00
if (hasComment) this.comment(this.single.comment);
};
// Compiles the comment on the table.
TableCompiler_PG.prototype.comment = function(comment) {
this.pushQuery(`comment on table ${this.tableName()} is '${this.single.comment || ''}'`);
2015-05-09 13:58:18 -04:00
};
// Indexes:
// -------
TableCompiler_PG.prototype.primary = function(columns) {
this.pushQuery(`alter table ${this.tableName()} add primary key (${this.formatter.columnize(columns)})`);
2015-05-09 13:58:18 -04:00
};
TableCompiler_PG.prototype.unique = function(columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
this.pushQuery(`alter table ${this.tableName()} add constraint ${indexName}` +
2015-05-09 13:58:18 -04:00
' unique (' + this.formatter.columnize(columns) + ')');
};
TableCompiler_PG.prototype.index = function(columns, indexName, indexType) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(`create index ${indexName} on ${this.tableName()}${indexType && (` using ${indexType}`) || ''}` +
2015-05-09 13:58:18 -04:00
' (' + this.formatter.columnize(columns) + ')');
};
TableCompiler_PG.prototype.dropPrimary = function() {
const constraintName = this.formatter.wrap(this.tableNameRaw + '_pkey');
this.pushQuery(`alter table ${this.tableName()} drop constraint ${constraintName}`);
2015-05-09 13:58:18 -04:00
};
TableCompiler_PG.prototype.dropIndex = function(columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(`drop index ${indexName}`);
2015-05-09 13:58:18 -04:00
};
TableCompiler_PG.prototype.dropUnique = function(columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
2015-05-09 13:58:18 -04:00
};
TableCompiler_PG.prototype.dropForeign = function(columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
2015-05-09 13:58:18 -04:00
};
export default TableCompiler_PG;