// 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) { return { 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)); return { sql: 'alter table ' + table + ' ' + columns.join(', ') }; }; // 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.pushQuery('comment on table ' + this.tableName() + ' is ' + "'" + this.attributes.comment + "'"); } }; // Compile a foreign key command. TableCompiler_PG.prototype.foreignKey = function(foreignData) { var sql = ''; if (foreignData.inTable && foreignData.references) { var keyName = this._indexCommand('foreign', this.tableNameRaw, foreignData.column); var column = this.formatter.columnize(foreignData.column); var references = this.formatter.columnize(foreignData.references); var inTable = this.formatter.wrap(foreignData.inTable); sql = 'alter table ' + this.tableName + ' add constraint ' + keyName + ' '; sql += 'foreign key (' + column + ') references ' + inTable + ' (' + references + ')'; } return { sql: sql }; }; // 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; };