knex/clients/postgres.js

258 lines
8.0 KiB
JavaScript
Raw Normal View History

2013-05-08 20:16:39 -04:00
var When = require('when');
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
};
_.extend(PostgresClient.prototype, base.protoProps, {
2013-04-28 20:58:38 -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;
var debug = this.debug || builder._debug;
var instance = this;
2013-05-08 20:16:39 -04:00
return When((builder._connection || this.getConnection()))
.then(function(conn) {
2013-05-08 20:16:39 -04:00
var dfd = When.defer();
// 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
// If we have a debug flag set, console.log the query.
if (debug) base.debug(builder, conn);
// 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-08 12:27:33 -04:00
if (builder._source === 'Raw') return dfd.resolve(resp);
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);
}
}
if (resp.command === 'SELECT') {
resp = resp.rows;
2013-05-04 20:06:39 -04:00
} else if (resp.command === 'INSERT') {
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') {
resp = resp.rowCount;
2013-05-04 20:06:39 -04:00
} else {
resp = '';
}
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() {
if (emptyConnection) instance.pool.release(conn);
});
});
},
2013-05-03 00:20:51 -04:00
// Returns a connection from the `pg` lib.
getRawConnection: function(callback) {
2013-05-03 00:20:51 -04:00
var conn = new pg.Client(this.connectionSettings);
conn.connect(function(err) {
callback(err, conn);
});
}
});
2013-03-12 11:52:08 -04:00
// Extends the standard sql grammar.
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';
},
// 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);
if (qb._idAttribute) {
sql += ' returning "' + qb._idAttribute + '"';
}
return sql;
2013-03-12 11:52:08 -04:00
}
};
// Grammar for the schema builder.
2013-05-03 10:03:44 -04:00
PostgresClient.schemaGrammar = _.extend({}, base.schemaGrammar, PostgresClient.grammar, {
// 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) {
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));
2013-05-22 13:10:42 -04:00
var table = this.wrapTable(blueprint);
return 'alter table ' + table + ' ' + columns.join(', ');
},
// Compile a drop primary key command.
2013-05-22 13:10:42 -04:00
compileDropPrimary: function(blueprint) {
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.
compileRenameTable: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + ' rename to ' + this.wrapTable(command.to);
},
// 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';
} 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;
},
// 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() {
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() {
return 'smallint';
},
// Create the column definition for a float type.
2013-05-22 13:10:42 -04:00
typeFloat: function() {
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() {
return 'boolean';
},
// Create the column definition for an enum type.
typeEnum: function(column) {
return "enum('" + column.allowed.join("', '") + "')";
},
// Create the column definition for a date-time type.
2013-05-22 13:10:42 -04:00
typeDateTime: function() {
return 'timestamp';
},
// Create the column definition for a timestamp type.
2013-05-22 13:10:42 -04:00
typeTimestamp: function() {
return 'timestamp';
},
// Create the column definition for a bit type.
typeBit: function(column) {
return column.length !== false ? 'bit(' + column.length + ')' : 'bit';
},
// Create the column definition for a binary type.
2013-05-22 13:10:42 -04:00
typeBinary: function() {
return 'bytea';
},
// Get the SQL for an auto-increment column modifier.
modifyIncrement: function(blueprint, column) {
if (column.type == 'integer' && column.autoIncrement) {
return ' primary key';
}
}
});