2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Oracle Schema Compiler
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import SchemaCompiler from '../../../schema/compiler';
|
|
|
|
import * as utils from '../utils';
|
2016-10-14 17:00:39 +02:00
|
|
|
import Trigger from './trigger';
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
function SchemaCompiler_Oracle() {
|
|
|
|
SchemaCompiler.apply(this, arguments);
|
|
|
|
}
|
|
|
|
inherits(SchemaCompiler_Oracle, SchemaCompiler);
|
|
|
|
|
|
|
|
// Rename a table on the schema.
|
|
|
|
SchemaCompiler_Oracle.prototype.renameTable = function(tableName, to) {
|
2018-05-29 17:42:03 +02:00
|
|
|
const renameTable = Trigger.renameTableAndAutoIncrementTrigger(
|
|
|
|
this.client.logger,
|
|
|
|
tableName,
|
|
|
|
to
|
|
|
|
);
|
2016-10-14 17:00:39 +02:00
|
|
|
this.pushQuery(renameTable);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check whether a table exists on the query.
|
|
|
|
SchemaCompiler_Oracle.prototype.hasTable = function(tableName) {
|
|
|
|
this.pushQuery({
|
|
|
|
sql: 'select TABLE_NAME from USER_TABLES where TABLE_NAME = ' +
|
|
|
|
this.formatter.parameter(tableName),
|
2016-05-17 01:01:34 +10:00
|
|
|
output(resp) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return resp.length > 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check whether a column exists on the schema.
|
|
|
|
SchemaCompiler_Oracle.prototype.hasColumn = function(tableName, column) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const sql =
|
|
|
|
`select COLUMN_NAME from USER_TAB_COLUMNS ` +
|
|
|
|
`where TABLE_NAME = ${this.formatter.parameter(tableName)} ` +
|
|
|
|
`and COLUMN_NAME = ${this.formatter.parameter(column)}`;
|
|
|
|
this.pushQuery({ sql, output: resp => resp.length > 0 });
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_Oracle.prototype.dropSequenceIfExists = function (sequenceName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(
|
|
|
|
utils.wrapSqlWithCatch(`drop sequence ${this.formatter.wrap(sequenceName)}`, -2289)
|
|
|
|
);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_Oracle.prototype._dropRelatedSequenceIfExists = function (tableName) {
|
|
|
|
// removing the sequence that was possibly generated by increments() column
|
2018-05-29 17:42:03 +02:00
|
|
|
const sequenceName = utils.generateCombinedName(this.client.logger, 'seq', tableName);
|
2015-05-09 13:58:18 -04:00
|
|
|
this.dropSequenceIfExists(sequenceName);
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_Oracle.prototype.dropTable = function (tableName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`drop table ${this.formatter.wrap(tableName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// removing the sequence that was possibly generated by increments() column
|
|
|
|
this._dropRelatedSequenceIfExists(tableName);
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_Oracle.prototype.dropTableIfExists = function(tableName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(utils.wrapSqlWithCatch(`drop table ${this.formatter.wrap(tableName)}`, -942));
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// removing the sequence that was possibly generated by increments() column
|
|
|
|
this._dropRelatedSequenceIfExists(tableName);
|
|
|
|
};
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default SchemaCompiler_Oracle;
|