2015-04-22 10:34:14 -04:00
|
|
|
// PostgreSQL Schema Compiler
|
2014-04-15 11:43:47 -04:00
|
|
|
// -------
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
'use strict';
|
2014-04-15 11:43:47 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var inherits = require('inherits');
|
2015-04-22 10:34:14 -04:00
|
|
|
var SchemaCompiler = require('../../../schema/compiler');
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
function SchemaCompiler_PG() {
|
2015-04-22 10:34:14 -04:00
|
|
|
SchemaCompiler.apply(this, arguments);
|
2014-04-15 11:43:47 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
inherits(SchemaCompiler_PG, SchemaCompiler);
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
// Check whether the current table
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.hasTable = function (tableName) {
|
2015-08-09 22:24:55 -03:00
|
|
|
var sql = 'select * from information_schema.tables where table_name = ?';
|
|
|
|
var bindings = [tableName];
|
|
|
|
|
|
|
|
if (this.schema) {
|
|
|
|
sql += ' and table_schema = ?';
|
|
|
|
bindings.push(this.schema);
|
|
|
|
} else {
|
|
|
|
sql += ' and table_schema = current_schema';
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
this.pushQuery({
|
2015-08-09 22:24:55 -03:00
|
|
|
sql: sql,
|
|
|
|
bindings: bindings,
|
2015-05-09 13:58:18 -04:00
|
|
|
output: function output(resp) {
|
2014-04-15 11:43:47 -04:00
|
|
|
return resp.rows.length > 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compile the query to determine if a column exists in a table.
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.hasColumn = function (tableName, columnName) {
|
2015-08-09 22:24:55 -03:00
|
|
|
var sql = 'select * from information_schema.columns where table_name = ? and column_name = ?';
|
|
|
|
var bindings = [tableName, columnName];
|
|
|
|
|
|
|
|
if (this.schema) {
|
|
|
|
sql += ' and table_schema = ?';
|
|
|
|
bindings.push(this.schema);
|
|
|
|
} else {
|
|
|
|
sql += ' and table_schema = current_schema';
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
this.pushQuery({
|
2015-08-09 22:24:55 -03:00
|
|
|
sql: sql,
|
|
|
|
bindings: bindings,
|
2015-05-09 13:58:18 -04:00
|
|
|
output: function output(resp) {
|
2014-04-15 11:43:47 -04:00
|
|
|
return resp.rows.length > 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-09 22:24:55 -03:00
|
|
|
SchemaCompiler_PG.prototype.qualifiedTableName = function (tableName) {
|
|
|
|
var name = this.schema ? this.schema + '.' + tableName : tableName;
|
|
|
|
return this.formatter.wrap(name);
|
|
|
|
};
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// Compile a rename table command.
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.renameTable = function (from, to) {
|
2015-08-09 22:24:55 -03:00
|
|
|
this.pushQuery('alter table ' + this.qualifiedTableName(from) + ' rename to ' + this.qualifiedTableName(to));
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.createSchema = function (schemaName) {
|
2014-09-30 12:52:41 -04:00
|
|
|
this.pushQuery('create schema ' + this.formatter.wrap(schemaName));
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.createSchemaIfNotExists = function (schemaName) {
|
2014-09-30 12:52:41 -04:00
|
|
|
this.pushQuery('create schema if not exists ' + this.formatter.wrap(schemaName));
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.dropSchema = function (schemaName) {
|
2014-09-30 12:52:41 -04:00
|
|
|
this.pushQuery('drop schema ' + this.formatter.wrap(schemaName));
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.dropSchemaIfExists = function (schemaName) {
|
2014-09-30 12:52:41 -04:00
|
|
|
this.pushQuery('drop schema if exists ' + this.formatter.wrap(schemaName));
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.dropExtension = function (extensionName) {
|
2014-10-24 13:28:59 -04:00
|
|
|
this.pushQuery('drop extension ' + this.formatter.wrap(extensionName));
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.dropExtensionIfExists = function (extensionName) {
|
2014-10-24 13:28:59 -04:00
|
|
|
this.pushQuery('drop extension if exists ' + this.formatter.wrap(extensionName));
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.createExtension = function (extensionName) {
|
2014-10-24 13:28:59 -04:00
|
|
|
this.pushQuery('create extension ' + this.formatter.wrap(extensionName));
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
SchemaCompiler_PG.prototype.createExtensionIfNotExists = function (extensionName) {
|
2014-10-24 13:28:59 -04:00
|
|
|
this.pushQuery('create extension if not exists ' + this.formatter.wrap(extensionName));
|
|
|
|
};
|
2014-09-30 12:52:41 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
module.exports = SchemaCompiler_PG;
|