2014-09-01 17:18:45 +02:00
'use strict' ;
2015-04-22 10:34:14 -04:00
// Oracle Schema Compiler
2014-08-11 12:25:39 +02:00
// -------
2015-05-09 13:58:18 -04:00
var inherits = require ( 'inherits' ) ;
2015-04-19 16:31:52 -04:00
var SchemaCompiler = require ( '../../../schema/compiler' ) ;
2015-05-09 13:58:18 -04:00
var utils = require ( '../utils' ) ;
2014-08-11 12:25:39 +02:00
2015-04-22 10:34:14 -04:00
function SchemaCompiler _Oracle ( ) {
SchemaCompiler . apply ( this , arguments ) ;
2014-08-11 12:25:39 +02:00
}
2015-04-22 10:34:14 -04:00
inherits ( SchemaCompiler _Oracle , SchemaCompiler ) ;
2014-08-11 12:25:39 +02:00
// Rename a table on the schema.
2015-05-09 13:58:18 -04:00
SchemaCompiler _Oracle . prototype . renameTable = function ( tableName , to ) {
2014-08-11 12:25:39 +02:00
this . pushQuery ( 'rename ' + this . formatter . wrap ( tableName ) + ' to ' + this . formatter . wrap ( to ) ) ;
} ;
// Check whether a table exists on the query.
2015-05-09 13:58:18 -04:00
SchemaCompiler _Oracle . prototype . hasTable = function ( tableName ) {
2014-08-11 12:25:39 +02:00
this . pushQuery ( {
2015-05-09 13:58:18 -04:00
sql : 'select TABLE_NAME from USER_TABLES where TABLE_NAME = ' + this . formatter . parameter ( tableName ) ,
output : function output ( resp ) {
2014-08-11 12:25:39 +02:00
return resp . length > 0 ;
}
} ) ;
} ;
// Check whether a column exists on the schema.
2015-05-09 13:58:18 -04:00
SchemaCompiler _Oracle . prototype . hasColumn = function ( tableName , column ) {
2014-08-11 12:25:39 +02:00
this . pushQuery ( {
2015-05-09 13:58:18 -04:00
sql : 'select COLUMN_NAME from USER_TAB_COLUMNS where TABLE_NAME = ' + this . formatter . parameter ( tableName ) + ' and COLUMN_NAME = ' + this . formatter . parameter ( column ) ,
output : function output ( resp ) {
2014-08-11 12:25:39 +02:00
return resp . length > 0 ;
}
} ) ;
} ;
SchemaCompiler _Oracle . prototype . dropSequenceIfExists = function ( sequenceName ) {
2015-05-09 13:58:18 -04:00
this . pushQuery ( utils . wrapSqlWithCatch ( 'drop sequence ' + this . formatter . wrap ( sequenceName ) , - 2289 ) ) ;
2014-08-11 12:25:39 +02:00
} ;
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 ) ;
} ;
2015-05-09 13:58:18 -04:00
SchemaCompiler _Oracle . prototype . dropTableIfExists = function ( tableName ) {
this . pushQuery ( utils . wrapSqlWithCatch ( 'drop table ' + this . formatter . wrap ( tableName ) , - 942 ) ) ;
2014-08-11 12:25:39 +02:00
// removing the sequence that was possibly generated by increments() column
this . _dropRelatedSequenceIfExists ( tableName ) ;
} ;
2015-05-09 13:58:18 -04:00
module . exports = SchemaCompiler _Oracle ;