2014-04-15 11:43:47 -04:00
|
|
|
// PostgreSQL Table Builder & Compiler
|
|
|
|
// -------
|
2015-05-09 14:01:19 -04:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var _ = require('lodash');
|
|
|
|
var inherits = require('inherits');
|
2015-04-19 16:31:52 -04:00
|
|
|
var TableCompiler = require('../../../schema/tablecompiler');
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
function TableCompiler_PG() {
|
2015-04-19 16:31:52 -04:00
|
|
|
TableCompiler.apply(this, arguments);
|
2014-04-15 11:43:47 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
inherits(TableCompiler_PG, TableCompiler);
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
// Compile a rename column command.
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.renameColumn = function (from, to) {
|
2014-04-16 01:23:50 -04:00
|
|
|
return this.pushQuery({
|
2015-05-09 13:58:18 -04:00
|
|
|
sql: 'alter table ' + this.tableName() + ' rename ' + this.formatter.wrap(from) + ' to ' + this.formatter.wrap(to)
|
2014-04-16 01:23:50 -04:00
|
|
|
});
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.compileAdd = function (builder) {
|
2014-04-15 11:43:47 -04:00
|
|
|
var table = this.formatter.wrap(builder);
|
|
|
|
var columns = this.prefixArray('add column', this.getColumns(builder));
|
2014-04-16 01:23:50 -04:00
|
|
|
return this.pushQuery({
|
2014-04-15 11:43:47 -04:00
|
|
|
sql: 'alter table ' + table + ' ' + columns.join(', ')
|
2014-04-16 01:23:50 -04:00
|
|
|
});
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Adds the "create" query to the query sequence.
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.createQuery = function (columns, ifNot) {
|
2014-08-14 15:32:26 -04:00
|
|
|
var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
|
2014-04-15 11:43:47 -04:00
|
|
|
this.pushQuery({
|
2014-08-14 15:32:26 -04:00
|
|
|
sql: createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')',
|
2014-04-15 11:43:47 -04:00
|
|
|
bindings: columns.bindings
|
|
|
|
});
|
2014-05-29 17:31:09 -04:00
|
|
|
var hasComment = _.has(this.single, 'comment');
|
|
|
|
if (hasComment) this.comment(this.single.comment);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
|
|
|
|
2014-05-29 17:31:09 -04:00
|
|
|
// Compiles the comment on the table.
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.comment = function (comment) {
|
2014-09-02 22:56:51 +02:00
|
|
|
/*jshint unused: false*/
|
2015-08-25 10:23:34 +03:00
|
|
|
this.pushQuery('comment on table ' + this.tableName() + ' is ' + "'" + (this.single.comment || '') + "'");
|
2014-05-29 17:31:09 -04:00
|
|
|
};
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// Indexes:
|
|
|
|
// -------
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.primary = function (columns) {
|
2015-08-25 10:23:34 +03:00
|
|
|
this.pushQuery('alter table ' + this.tableName() + " add primary key (" + this.formatter.columnize(columns) + ")");
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.unique = function (columns, indexName) {
|
2014-04-15 11:43:47 -04:00
|
|
|
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
2015-05-09 13:58:18 -04:00
|
|
|
this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + indexName + ' unique (' + this.formatter.columnize(columns) + ')');
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.index = function (columns, indexName, indexType) {
|
2014-04-15 11:43:47 -04:00
|
|
|
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
2015-05-09 13:58:18 -04:00
|
|
|
this.pushQuery('create index ' + indexName + ' on ' + this.tableName() + (indexType && ' using ' + indexType || '') + ' (' + this.formatter.columnize(columns) + ')');
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.dropPrimary = function () {
|
2015-08-25 10:23:34 +03:00
|
|
|
this.pushQuery('alter table ' + this.tableName() + " drop constraint " + this.tableNameRaw + "_pkey");
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.dropIndex = function (columns, indexName) {
|
2014-06-04 11:29:21 -04:00
|
|
|
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery('drop index ' + indexName);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.dropUnique = function (columns, indexName) {
|
2014-06-04 11:29:21 -04:00
|
|
|
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + indexName);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
TableCompiler_PG.prototype.dropForeign = function (columns, indexName) {
|
2014-06-04 11:29:21 -04:00
|
|
|
indexName = indexName || this._indexCommand('foreign', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + indexName);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
module.exports = TableCompiler_PG;
|