'use strict'; // Oracle Schema Builder & Compiler // ------- module.exports = function(client) { var inherits = require('inherits'); var Schema = require('../../../schema'); var utils = require('../utils'); // Schema Builder // ------- function SchemaBuilder_Oracle() { this.client = client; Schema.Builder.apply(this, arguments); } inherits(SchemaBuilder_Oracle, Schema.Builder); // Schema Compiler // ------- function SchemaCompiler_Oracle() { this.client = client; this.Formatter = client.Formatter; Schema.Compiler.apply(this, arguments); } inherits(SchemaCompiler_Oracle, Schema.Compiler); // Rename a table on the schema. SchemaCompiler_Oracle.prototype.renameTable = function(tableName, to) { this.pushQuery('rename ' + this.formatter.wrap(tableName) + ' to ' + this.formatter.wrap(to)); }; // 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), output: function(resp) { return resp.length > 0; } }); }; // Check whether a column exists on the schema. SchemaCompiler_Oracle.prototype.hasColumn = function(tableName, column) { this.pushQuery({ sql: 'select COLUMN_NAME from USER_TAB_COLUMNS where TABLE_NAME = ' + this.formatter.parameter(tableName) + ' and COLUMN_NAME = ' + this.formatter.parameter(column), output: function(resp) { return resp.length > 0; } }); }; SchemaCompiler_Oracle.prototype.dropSequenceIfExists = function (sequenceName) { this.pushQuery(utils.wrapSqlWithCatch("drop sequence " + this.formatter.wrap(sequenceName), -2289)); }; SchemaCompiler_Oracle.prototype._dropRelatedSequenceIfExists = function (tableName) { // removing the sequence that was possibly generated by increments() column var sequenceName = utils.generateCombinedName('seq', tableName); this.dropSequenceIfExists(sequenceName); }; SchemaCompiler_Oracle.prototype.dropTable = function (tableName) { this.pushQuery('drop table ' + this.formatter.wrap(tableName)); // removing the sequence that was possibly generated by increments() column this._dropRelatedSequenceIfExists(tableName); }; SchemaCompiler_Oracle.prototype.dropTableIfExists = function(tableName) { this.pushQuery(utils.wrapSqlWithCatch("drop table " + this.formatter.wrap(tableName), -942)); // removing the sequence that was possibly generated by increments() column this._dropRelatedSequenceIfExists(tableName); }; client.SchemaBuilder = SchemaBuilder_Oracle; client.SchemaCompiler = SchemaCompiler_Oracle; };