2013-09-08 15:57:32 -04:00
|
|
|
// postgresql
|
|
|
|
// -------
|
2013-05-02 00:21:49 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// All of the "when.js" promise components needed in this module.
|
2013-09-11 23:36:55 -04:00
|
|
|
var when = require('when');
|
|
|
|
var nodefn = require('when/node/function');
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Other dependencies, including the `pg` library,
|
|
|
|
// which needs to be added as a dependency to the project
|
|
|
|
// using this database.
|
|
|
|
var _ = require('underscore');
|
|
|
|
var pg = require('pg');
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// All other local project modules needed in this scope.
|
|
|
|
var ServerBase = require('./base').ServerBase;
|
2013-09-11 23:36:55 -04:00
|
|
|
var baseGrammar = require('../base/grammar').BaseGrammar;
|
|
|
|
var baseSchemaGrammar = require('../base/schemagrammar').BaseSchemaGrammar;
|
2013-09-08 15:57:32 -04:00
|
|
|
var Helpers = require('../../lib/helpers').Helpers;
|
2013-04-28 20:58:38 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Constructor for the PostgreSQL Client
|
|
|
|
exports.Client = ServerBase.extend({
|
2013-07-17 06:16:41 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
dialect: 'postgresql',
|
2013-05-04 12:23:55 -04:00
|
|
|
|
2013-09-13 10:24:39 -04:00
|
|
|
initialize: function() {},
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-13 11:40:51 -04:00
|
|
|
runQuery: function(connection, sql, bindings, builder) {
|
2013-09-13 13:27:00 -04:00
|
|
|
if (!connection) throw new Error('No database connection exists for the query');
|
2013-09-13 12:59:12 -04:00
|
|
|
var questionCount = 0;
|
|
|
|
sql = sql.replace(/\?/g, function() {
|
|
|
|
questionCount++;
|
|
|
|
return '$' + questionCount;
|
|
|
|
});
|
2013-09-13 11:40:51 -04:00
|
|
|
if (builder && builder.flags.options) sql = _.extend({text: sql}, builder.flags.options);
|
2013-09-11 23:36:55 -04:00
|
|
|
return nodefn.call(connection.query.bind(connection), sql, bindings);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
2013-06-09 16:57:31 -04:00
|
|
|
getRawConnection: function(callback) {
|
2013-07-17 09:53:00 -04:00
|
|
|
var instance = this;
|
|
|
|
var connection = new pg.Client(this.connectionSettings);
|
2013-09-11 23:36:55 -04:00
|
|
|
return nodefn.call(connection.connect.bind(connection)).tap(function() {
|
|
|
|
if (!instance.version) return instance.checkVersion(connection);
|
|
|
|
}).yield(connection);
|
2013-07-17 09:53:00 -04:00
|
|
|
},
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
// In PostgreSQL, we need to do a version check to do some feature
|
|
|
|
// checking on the database.
|
|
|
|
checkVersion: function(connection) {
|
2013-07-17 09:53:00 -04:00
|
|
|
var instance = this;
|
2013-09-11 23:36:55 -04:00
|
|
|
this.runQuery(connection, 'select version();').then(function(resp) {
|
2013-07-17 09:53:00 -04:00
|
|
|
instance.version = /^PostgreSQL (.*?) /.exec(resp.rows[0].version)[1];
|
2013-09-08 15:57:32 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Extends the standard sql grammar.
|
2013-09-11 23:36:55 -04:00
|
|
|
var grammar = exports.grammar = _.defaults({
|
2013-09-08 15:57:32 -04:00
|
|
|
|
2013-03-12 12:58:33 -04:00
|
|
|
// The keyword identifier wrapper format.
|
2013-03-12 11:52:08 -04:00
|
|
|
wrapValue: function(value) {
|
2013-09-08 15:57:32 -04:00
|
|
|
return (value !== '*' ? Helpers.format('"%s"', value) : "*");
|
2013-03-12 11:52:08 -04:00
|
|
|
},
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Compiles a truncate query.
|
2013-05-03 00:20:51 -04:00
|
|
|
compileTruncate: function(qb) {
|
2013-05-04 20:06:39 -04:00
|
|
|
return 'truncate ' + this.wrapTable(qb.table) + ' restart identity';
|
2013-05-04 12:23:55 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Compiles an `insert` query, allowing for multiple
|
|
|
|
// inserts using a single query statement.
|
|
|
|
compileInsert: function(qb) {
|
2013-08-22 16:46:13 -04:00
|
|
|
var values = qb.values;
|
|
|
|
var columns = _.pluck(values[0], 0);
|
|
|
|
var paramBlocks = [];
|
|
|
|
|
|
|
|
// If there are any "where" clauses, we need to omit
|
|
|
|
// any bindings that may have been associated with them.
|
2013-09-11 23:36:55 -04:00
|
|
|
if (qb.wheres.length > 0) this.clearWhereBindings(qb);
|
2013-08-22 16:46:13 -04:00
|
|
|
|
|
|
|
var sql = 'insert into ' + this.wrapTable(qb.table) + ' ';
|
|
|
|
|
|
|
|
if (columns.length === 0) {
|
|
|
|
sql += 'default values';
|
|
|
|
} else {
|
|
|
|
for (var i = 0, l = values.length; i < l; ++i) {
|
|
|
|
paramBlocks.push("(" + this.parameterize(_.pluck(values[i], 1)) + ")");
|
|
|
|
}
|
|
|
|
sql += "(" + this.columnize(columns) + ") values " + paramBlocks.join(', ');
|
|
|
|
}
|
|
|
|
|
2013-09-13 10:51:12 -04:00
|
|
|
if (qb.flags.returning) {
|
|
|
|
sql += ' returning "' + qb.flags.returning + '"';
|
2013-05-04 12:23:55 -04:00
|
|
|
}
|
|
|
|
return sql;
|
2013-09-11 23:36:55 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Handles the response
|
|
|
|
handleResponse: function(builder, response) {
|
|
|
|
if (response.command === 'SELECT') return response.rows;
|
|
|
|
if (response.command === 'INSERT') {
|
|
|
|
return _.map(response.rows, function(row) {
|
2013-09-13 10:51:12 -04:00
|
|
|
return row[builder.flags.returning];
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (response.command === 'UPDATE' || response.command === 'DELETE') {
|
|
|
|
return response.rowCount;
|
|
|
|
}
|
|
|
|
return '';
|
2013-03-12 11:52:08 -04:00
|
|
|
}
|
2013-05-04 12:23:55 -04:00
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
}, baseGrammar);
|
2013-03-12 12:45:54 -04:00
|
|
|
|
|
|
|
// Grammar for the schema builder.
|
2013-09-11 23:36:55 -04:00
|
|
|
exports.schemaGrammar = _.defaults({
|
2013-03-12 12:45:54 -04:00
|
|
|
|
|
|
|
// The possible column modifiers.
|
|
|
|
modifiers: ['Increment', 'Nullable', 'Default'],
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
handleResponse: function(builder, resp) {
|
|
|
|
resp = resp[0];
|
2013-09-11 23:36:55 -04:00
|
|
|
if (builder.type === 'tableExists' || builder.type === 'columnExists') {
|
2013-09-12 13:30:47 -04:00
|
|
|
return resp.rows.length > 0;
|
2013-09-11 23:36:55 -04:00
|
|
|
}
|
2013-09-12 13:30:47 -04:00
|
|
|
return resp;
|
2013-09-11 23:36:55 -04:00
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Compile the query to determine if a table exists.
|
|
|
|
compileTableExists: function() {
|
|
|
|
return 'select * from information_schema.tables where table_name = ?';
|
|
|
|
},
|
|
|
|
|
2013-09-04 19:13:02 -04:00
|
|
|
// Compile the query to determine if a column exists in a table.
|
|
|
|
compileColumnExists: function() {
|
|
|
|
return 'select * from information_schema.columns where table_name = ? and column_name = ?';
|
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Compile a create table command.
|
2013-05-22 13:10:42 -04:00
|
|
|
compileAdd: function(blueprint) {
|
2013-03-12 12:45:54 -04:00
|
|
|
var table = this.wrapTable(blueprint);
|
|
|
|
var columns = this.prefixArray('add column', this.getColumns(blueprint));
|
|
|
|
return 'alter table ' + table + ' ' + columns.join(', ');
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a primary key command.
|
|
|
|
compilePrimary: function(blueprint, command) {
|
|
|
|
var columns = this.columnize(command.columns);
|
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + " add primary key (" + columns + ")";
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a unique key command.
|
|
|
|
compileUnique: function(blueprint, command) {
|
|
|
|
var table = this.wrapTable(blueprint);
|
|
|
|
var columns = this.columnize(command.columns);
|
2013-05-04 21:37:10 -04:00
|
|
|
return 'alter table ' + table + ' add constraint ' + command.index + ' unique (' + columns + ')';
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a plain index key command.
|
|
|
|
compileIndex: function(blueprint, command) {
|
|
|
|
var columns = this.columnize(command.columns);
|
|
|
|
return "create index " + command.index + " on " + this.wrapTable(blueprint) + ' (' + columns + ')';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop column command.
|
|
|
|
compileDropColumn: function(blueprint, command) {
|
|
|
|
var columns = this.prefixArray('drop column', this.wrapArray(command.columns));
|
2013-05-22 13:10:42 -04:00
|
|
|
var table = this.wrapTable(blueprint);
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'alter table ' + table + ' ' + columns.join(', ');
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop primary key command.
|
2013-05-22 13:10:42 -04:00
|
|
|
compileDropPrimary: function(blueprint) {
|
2013-03-12 12:45:54 -04:00
|
|
|
var table = blueprint.getTable();
|
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + " drop constraint " + table + "_pkey";
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop unique key command.
|
|
|
|
compileDropUnique: function(blueprint, command) {
|
|
|
|
var table = this.wrapTable(blueprint);
|
|
|
|
return "alter table " + table + " drop constraint " + command.index;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop foreign key command.
|
|
|
|
compileDropForeign: function(blueprint, command) {
|
|
|
|
var table = this.wrapTable(blueprint);
|
|
|
|
return "alter table " + table + " drop constraint " + command.index;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a rename table command.
|
2013-05-02 00:21:49 -04:00
|
|
|
compileRenameTable: function(blueprint, command) {
|
2013-04-21 18:26:22 -04:00
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + ' rename to ' + this.wrapTable(command.to);
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
2013-09-04 17:32:32 -04:00
|
|
|
// Compile a rename column command.
|
|
|
|
compileRenameColumn: function(blueprint, command) {
|
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + ' rename '+ this.wrapTable(command.from) + ' to ' + this.wrapTable(command.to);
|
|
|
|
},
|
|
|
|
|
2013-06-16 20:27:24 +10:00
|
|
|
// Compile a comment command.
|
|
|
|
compileComment: function(blueprint, command) {
|
2013-07-17 06:16:41 -04:00
|
|
|
var sql = '';
|
|
|
|
if (command.comment) {
|
|
|
|
sql += 'comment on table ' + this.wrapTable(blueprint) + ' is ' + "'" + command.comment + "'";
|
2013-06-16 20:27:24 +10:00
|
|
|
}
|
2013-07-17 06:16:41 -04:00
|
|
|
return sql;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile any additional postgres specific items.
|
|
|
|
compileAdditional: function(blueprint, command) {
|
|
|
|
return _.compact(_.map(blueprint.columns, function(column) {
|
|
|
|
if (column.isCommented && _.isString(column.isCommented)) {
|
|
|
|
return 'comment on column ' + this.wrapTable(blueprint) + '.' + this.wrap(column.name) + " is '" + column.isCommented + "'";
|
|
|
|
}
|
|
|
|
}, this));
|
2013-06-16 20:27:24 +10:00
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Create the column definition for a integer type.
|
|
|
|
typeInteger: function(column) {
|
|
|
|
return column.autoIncrement ? 'serial' : 'integer';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a tiny integer type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeTinyInteger: function() {
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'smallint';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a float type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeFloat: function() {
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'real';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a decimal type.
|
|
|
|
typeDecimal: function(column) {
|
|
|
|
return "decimal(" + column.total + ", " + column.places + ")";
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a boolean type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeBoolean: function() {
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'boolean';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for an enum type.
|
2013-07-17 08:38:33 -04:00
|
|
|
// Using method 2 here: http://stackoverflow.com/questions/10923213/postgres-enum-data-type-or-check-constraint
|
2013-03-12 12:45:54 -04:00
|
|
|
typeEnum: function(column) {
|
2013-07-17 08:38:33 -04:00
|
|
|
return 'text check(' + this.wrap(column.name) + " in('" + column.allowed.join("', '") + "'))";
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a date-time type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeDateTime: function() {
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'timestamp';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a timestamp type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeTimestamp: function() {
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'timestamp';
|
|
|
|
},
|
|
|
|
|
2013-04-21 18:26:22 -04:00
|
|
|
// Create the column definition for a bit type.
|
|
|
|
typeBit: function(column) {
|
|
|
|
return column.length !== false ? 'bit(' + column.length + ')' : 'bit';
|
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Create the column definition for a binary type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeBinary: function() {
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'bytea';
|
|
|
|
},
|
|
|
|
|
2013-07-17 10:26:24 -04:00
|
|
|
// Create the column definition for a uuid type.
|
|
|
|
typeUuid: function() {
|
|
|
|
return 'uuid';
|
|
|
|
},
|
|
|
|
|
2013-07-17 09:53:00 -04:00
|
|
|
// Create the column definition for a json type,
|
|
|
|
// checking whether the json type is supported - falling
|
|
|
|
// back to "text" if it's not.
|
|
|
|
typeJson: function(column, blueprint) {
|
2013-09-08 15:57:32 -04:00
|
|
|
if (parseFloat(blueprint.client.version) >= 9.2) return 'json';
|
2013-07-17 09:53:00 -04:00
|
|
|
return 'text';
|
2013-06-16 22:31:29 +10:00
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Get the SQL for an auto-increment column modifier.
|
|
|
|
modifyIncrement: function(blueprint, column) {
|
2013-09-11 23:36:55 -04:00
|
|
|
if (column.autoIncrement && (column.type == 'integer' || column.type == 'bigInteger')) {
|
2013-09-04 18:34:59 -04:00
|
|
|
return ' primary key not null';
|
2013-03-12 12:45:54 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-17 06:16:41 -04:00
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
}, baseSchemaGrammar, grammar);
|