2013-05-02 00:21:49 -04:00
|
|
|
var Q = require('q');
|
|
|
|
var _ = require('underscore');
|
|
|
|
var util = require('util');
|
|
|
|
var base = require('./base');
|
|
|
|
var pg = require('pg');
|
|
|
|
|
|
|
|
// Constructor for the PostgresClient
|
|
|
|
var PostgresClient = module.exports = function(name, options) {
|
|
|
|
base.setup.call(this, PostgresClient, name, options);
|
2013-03-12 11:52:08 -04:00
|
|
|
};
|
|
|
|
|
2013-05-02 00:21:49 -04:00
|
|
|
_.extend(PostgresClient.prototype, base.protoProps, {
|
2013-04-28 20:58:38 -04:00
|
|
|
|
2013-05-03 00:20:51 -04:00
|
|
|
// Returns a connection from the `pg` lib.
|
|
|
|
getRawConnection: function() {
|
|
|
|
var conn = new pg.Client(this.connectionSettings);
|
|
|
|
conn.connect();
|
|
|
|
return conn;
|
2013-05-02 00:21:49 -04:00
|
|
|
},
|
2013-04-28 20:58:38 -04:00
|
|
|
|
2013-05-03 00:20:51 -04:00
|
|
|
prepData: function(data) {
|
2013-05-02 00:21:49 -04:00
|
|
|
// Bind all of the ? to numbered vars.
|
|
|
|
var questionCount = 0;
|
2013-05-03 00:20:51 -04:00
|
|
|
data.sql = data.sql.replace(/\?/g, function() {
|
2013-05-02 00:21:49 -04:00
|
|
|
questionCount++;
|
|
|
|
return '$' + questionCount;
|
2013-03-12 11:52:08 -04:00
|
|
|
});
|
2013-05-03 00:20:51 -04:00
|
|
|
return data;
|
|
|
|
},
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-05-03 00:20:51 -04:00
|
|
|
prepResp: function(resp) {
|
|
|
|
if (resp.command === 'INSERT' || resp.command === 'UPDATE') {
|
|
|
|
return _.extend(resp, {insertId: resp.oid});
|
2013-05-02 00:21:49 -04:00
|
|
|
}
|
2013-05-03 00:20:51 -04:00
|
|
|
return resp.rows;
|
2013-05-02 00:21:49 -04:00
|
|
|
}
|
|
|
|
});
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Extends the standard sql grammar.
|
2013-05-02 00:21:49 -04:00
|
|
|
PostgresClient.grammar = {
|
2013-03-12 11:52:08 -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) {
|
|
|
|
return (value !== '*' ? util.format('"%s"', value) : "*");
|
|
|
|
},
|
|
|
|
|
2013-05-03 00:20:51 -04:00
|
|
|
compileTruncate: function(qb) {
|
2013-03-12 11:52:08 -04:00
|
|
|
var query = {};
|
|
|
|
query['truncate ' + this.wrapTable(qb.from) + ' restart identity'] = [];
|
|
|
|
return query;
|
|
|
|
}
|
2013-03-12 12:45:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Grammar for the schema builder.
|
2013-05-03 10:03:44 -04:00
|
|
|
PostgresClient.schemaGrammar = _.extend({}, base.schemaGrammar, PostgresClient.grammar, {
|
2013-03-12 12:45:54 -04:00
|
|
|
|
|
|
|
// The possible column modifiers.
|
|
|
|
modifiers: ['Increment', 'Nullable', 'Default'],
|
|
|
|
|
|
|
|
// Compile the query to determine if a table exists.
|
|
|
|
compileTableExists: function() {
|
|
|
|
return 'select * from information_schema.tables where table_name = ?';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a create table command.
|
2013-04-01 23:04:02 -04:00
|
|
|
compileCreateTable: function(blueprint, command) {
|
2013-03-12 12:45:54 -04:00
|
|
|
var columns = this.getColumns(blueprint).join(', ');
|
|
|
|
return 'create table ' + this.wrapTable(blueprint) + " (" + columns + ")";
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a create table command.
|
|
|
|
compileAdd: function(blueprint, command) {
|
|
|
|
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);
|
|
|
|
return 'alter table table add constraint ' + command.index + ' unique (' + columns + ')';
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
table = this.wrapTable(blueprint);
|
|
|
|
return 'alter table ' + table + ' ' + columns.join(', ');
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop primary key command.
|
|
|
|
compileDropPrimary: function(blueprint, command) {
|
|
|
|
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 index command.
|
|
|
|
compileDropIndex: function(blueprint, command) {
|
|
|
|
return "drop index " + 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
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a string type.
|
|
|
|
typeString: function(column) {
|
|
|
|
return "varchar(" + column.length + ")";
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a text type.
|
|
|
|
typeText: function(column) {
|
|
|
|
return 'text';
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
typeTinyInteger: function(column) {
|
|
|
|
return 'smallint';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a float type.
|
|
|
|
typeFloat: function(column) {
|
|
|
|
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.
|
|
|
|
typeBoolean: function(column) {
|
|
|
|
return 'boolean';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for an enum type.
|
|
|
|
typeEnum: function(column) {
|
2013-05-03 12:51:54 -04:00
|
|
|
return "enum('" + column.allowed.join("', '") + "')";
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a date type.
|
|
|
|
typeDate: function(column) {
|
|
|
|
return 'date';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a date-time type.
|
|
|
|
typeDateTime: function(column) {
|
|
|
|
return 'timestamp';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a time type.
|
|
|
|
typeTime: function(column) {
|
|
|
|
return 'time';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a timestamp type.
|
|
|
|
typeTimestamp: function(column) {
|
|
|
|
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.
|
|
|
|
typeBinary: function(column) {
|
|
|
|
return 'bytea';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get the SQL for a nullable column modifier.
|
|
|
|
modifyNullable: function(blueprint, column) {
|
2013-04-01 23:35:01 -04:00
|
|
|
return column.isNullable ? ' null' : ' not null';
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get the SQL for a default column modifier.
|
|
|
|
modifyDefault: function(blueprint, column) {
|
|
|
|
if (column.defaultValue) {
|
2013-04-01 23:35:01 -04:00
|
|
|
return " default '" + this.getDefaultValue(column.defaultValue) + "'";
|
2013-03-12 12:45:54 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get the SQL for an auto-increment column modifier.
|
|
|
|
modifyIncrement: function(blueprint, column) {
|
|
|
|
if (column.type == 'integer' && column.autoIncrement) {
|
|
|
|
return ' primary key';
|
|
|
|
}
|
|
|
|
}
|
2013-03-12 12:58:33 -04:00
|
|
|
});
|