2013-05-08 20:16:39 -04:00
|
|
|
var When = require('when');
|
2013-05-02 00:21:49 -04:00
|
|
|
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-04 12:23:55 -04:00
|
|
|
// Execute a query on the specified Builder or QueryBuilder
|
|
|
|
// interface. If a `connection` is specified, use it, otherwise
|
|
|
|
// acquire a connection, and then dispose of it when we're done.
|
|
|
|
query: function(builder) {
|
|
|
|
var emptyConnection = !builder._connection;
|
2013-05-04 14:36:41 -04:00
|
|
|
var debug = this.debug || builder._debug;
|
2013-05-04 12:23:55 -04:00
|
|
|
var instance = this;
|
2013-05-08 20:16:39 -04:00
|
|
|
return When((builder._connection || this.getConnection()))
|
2013-05-04 12:23:55 -04:00
|
|
|
.then(function(conn) {
|
2013-05-08 20:16:39 -04:00
|
|
|
var dfd = When.defer();
|
2013-05-04 12:23:55 -04:00
|
|
|
|
|
|
|
// Bind all of the ? to numbered vars.
|
|
|
|
var questionCount = 0;
|
|
|
|
builder.sql = builder.sql.replace(/\?/g, function() {
|
|
|
|
questionCount++;
|
|
|
|
return '$' + questionCount;
|
|
|
|
});
|
2013-05-22 13:10:42 -04:00
|
|
|
|
2013-05-04 12:23:55 -04:00
|
|
|
// If we have a debug flag set, console.log the query.
|
2013-05-04 14:36:41 -04:00
|
|
|
if (debug) base.debug(builder, conn);
|
2013-05-04 12:23:55 -04:00
|
|
|
|
|
|
|
// Call the querystring and then release the client
|
|
|
|
conn.query(builder.sql, builder.bindings, function (err, resp) {
|
|
|
|
if (err) return dfd.reject(err);
|
|
|
|
resp || (resp = {});
|
2013-05-04 14:36:41 -04:00
|
|
|
|
2013-05-08 12:27:33 -04:00
|
|
|
if (builder._source === 'Raw') return dfd.resolve(resp);
|
|
|
|
|
2013-05-04 14:36:41 -04:00
|
|
|
if (builder._source === 'SchemaBuilder') {
|
|
|
|
if (builder.type === 'tableExists') {
|
|
|
|
if (resp.rows.length > 0) return dfd.resolve(resp.rows[0]);
|
|
|
|
return dfd.reject(new Error('Table does not exist:' + builder.sql));
|
|
|
|
} else {
|
|
|
|
return dfd.resolve(null);
|
|
|
|
}
|
2013-05-04 12:23:55 -04:00
|
|
|
}
|
|
|
|
|
2013-05-04 14:36:41 -04:00
|
|
|
if (resp.command === 'SELECT') {
|
|
|
|
resp = resp.rows;
|
2013-05-04 20:06:39 -04:00
|
|
|
} else if (resp.command === 'INSERT') {
|
2013-05-05 13:54:43 -04:00
|
|
|
resp = _.map(resp.rows, function(row) { return row[builder._idAttribute]; });
|
2013-05-04 20:06:39 -04:00
|
|
|
} else if (resp.command === 'UPDATE' || resp.command === 'DELETE') {
|
2013-05-04 14:36:41 -04:00
|
|
|
resp = resp.rowCount;
|
2013-05-04 20:06:39 -04:00
|
|
|
} else {
|
|
|
|
resp = '';
|
2013-05-04 14:36:41 -04:00
|
|
|
}
|
2013-05-04 12:23:55 -04:00
|
|
|
dfd.resolve(resp);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Empty the connection after we run the query, unless one was specifically
|
|
|
|
// set (in the case of transactions, etc).
|
2013-05-08 20:16:39 -04:00
|
|
|
return dfd.promise.ensure(function() {
|
2013-05-04 12:23:55 -04:00
|
|
|
if (emptyConnection) instance.pool.release(conn);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-05-03 00:20:51 -04:00
|
|
|
// Returns a connection from the `pg` lib.
|
2013-06-09 16:57:31 -04:00
|
|
|
getRawConnection: function(callback) {
|
2013-05-03 00:20:51 -04:00
|
|
|
var conn = new pg.Client(this.connectionSettings);
|
2013-06-09 16:57:31 -04:00
|
|
|
conn.connect(function(err) {
|
|
|
|
callback(err, conn);
|
|
|
|
});
|
2013-05-02 00:21:49 -04:00
|
|
|
}
|
2013-05-04 12:23:55 -04:00
|
|
|
|
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-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) {
|
|
|
|
var sql = require('../knex').Grammar.compileInsert.call(this, qb);
|
2013-05-05 13:54:43 -04:00
|
|
|
if (qb._idAttribute) {
|
|
|
|
sql += ' returning "' + qb._idAttribute + '"';
|
2013-05-04 12:23:55 -04:00
|
|
|
}
|
|
|
|
return sql;
|
2013-03-12 11:52:08 -04:00
|
|
|
}
|
2013-05-04 12:23:55 -04:00
|
|
|
|
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-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-06-16 20:27:24 +10:00
|
|
|
// Compile a comment command.
|
|
|
|
compileComment: function(blueprint, command) {
|
|
|
|
var table = this.wrapTable(blueprint);
|
|
|
|
var comment;
|
|
|
|
if (command.comment == void 0) {
|
2013-07-12 14:20:05 -04:00
|
|
|
comment = 'NULL';
|
2013-06-16 20:27:24 +10:00
|
|
|
} else {
|
|
|
|
comment = "'" + command.comment + "'";
|
|
|
|
}
|
|
|
|
var identifier;
|
|
|
|
if (command.isTable) {
|
|
|
|
identifier = 'table ' + table;
|
|
|
|
} else {
|
|
|
|
var column = this.wrap(command.columnName);
|
|
|
|
identifier = 'column ' + table + '.' + column;
|
|
|
|
}
|
|
|
|
return 'comment on ' + identifier + ' is ' + comment;
|
|
|
|
},
|
|
|
|
|
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.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeText: function() {
|
2013-03-12 12:45:54 -04:00
|
|
|
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.
|
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.
|
|
|
|
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-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';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get the SQL for an auto-increment column modifier.
|
|
|
|
modifyIncrement: function(blueprint, column) {
|
|
|
|
if (column.type == 'integer' && column.autoIncrement) {
|
|
|
|
return ' primary key';
|
|
|
|
}
|
|
|
|
}
|
2013-06-16 20:27:24 +10:00
|
|
|
});
|