93 lines
3.2 KiB
JavaScript
Raw Normal View History

// PostgreSQL Table Builder & Compiler
// -------
module.exports = function(client) {
var _ = require('lodash');
var inherits = require('inherits');
var Schema = require('../../../schema');
// Table
// ------
function TableBuilder_PG() {
this.client = client;
Schema.TableBuilder.apply(this, arguments);
}
inherits(TableBuilder_PG, Schema.TableBuilder);
function TableCompiler_PG() {
this.client = client;
this.Formatter = client.Formatter;
Schema.TableCompiler.apply(this, arguments);
}
inherits(TableCompiler_PG, Schema.TableCompiler);
// Compile a rename column command.
TableCompiler_PG.prototype.renameColumn = function(from, to) {
2014-04-16 01:23:50 -04:00
return this.pushQuery({
sql: 'alter table ' + this.tableName() + ' rename '+ this.formatter.wrap(from) + ' to ' + this.formatter.wrap(to)
});
};
TableCompiler_PG.prototype.compileAdd = function(builder) {
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({
sql: 'alter table ' + table + ' ' + columns.join(', ')
2014-04-16 01:23:50 -04:00
});
};
// Adds the "create" query to the query sequence.
TableCompiler_PG.prototype.createQuery = function(columns) {
this.pushQuery({
sql: 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')',
bindings: columns.bindings
});
var hasComment = _.has(this.single, 'comment');
if (hasComment) this.comment(this.single.comment);
};
// Compile a foreign key command.
2014-04-16 01:23:50 -04:00
TableCompiler_PG.prototype.foreign = function(foreignData) {
var sql = Schema.TableCompiler.prototype.foreign.apply(this, arguments);
if (sql) this.pushQuery(sql);
};
// Compiles the comment on the table.
TableCompiler_PG.prototype.comment = function(comment) {
this.pushQuery('comment on table ' + this.tableName() + ' is ' + "'" + (this.single.comment || '') + "'");
};
// Indexes:
// -------
TableCompiler_PG.prototype.primary = function(columns) {
this.pushQuery('alter table ' + this.tableName() + " add primary key (" + this.formatter.columnize(columns) + ")");
};
TableCompiler_PG.prototype.unique = function(columns, indexName) {
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + indexName +
' unique (' + this.formatter.columnize(columns) + ')');
};
TableCompiler_PG.prototype.index = function(columns, indexName) {
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery('create index ' + indexName + ' on ' + this.tableName() +
' (' + this.formatter.columnize(columns) + ')');
};
TableCompiler_PG.prototype.dropPrimary = function() {
this.pushQuery('alter table ' + this.tableName() + " drop constraint " + this.tableNameRaw + "_pkey");
};
TableCompiler_PG.prototype.dropIndex = function(index) {
this.pushQuery('drop index ' + index);
};
TableCompiler_PG.prototype.dropUnique = function(index) {
this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + index);
};
TableCompiler_PG.prototype.dropForeign = function(index) {
this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + index);
};
client.TableBuilder = TableBuilder_PG;
client.TableCompiler = TableCompiler_PG;
};