2014-09-01 17:18:45 +02:00
'use strict' ;
2015-05-09 13:58:18 -04:00
var _ = require ( 'lodash' ) ;
var inherits = require ( 'inherits' ) ;
var assign = require ( 'lodash/object/assign' ) ;
var utils = require ( '../utils' ) ;
var Raw = require ( '../../../raw' ) ;
var ColumnCompiler = require ( '../../../schema/columncompiler' ) ;
2014-08-11 12:25:39 +02:00
// Column Compiler
// -------
2015-04-22 10:34:14 -04:00
function ColumnCompiler _Oracle ( ) {
2014-08-11 12:25:39 +02:00
this . modifiers = [ 'defaultTo' , 'checkIn' , 'nullable' , 'comment' ] ;
2015-04-22 10:34:14 -04:00
ColumnCompiler . apply ( this , arguments ) ;
2014-08-11 12:25:39 +02:00
}
2015-04-19 16:31:52 -04:00
inherits ( ColumnCompiler _Oracle , ColumnCompiler ) ;
2014-08-11 12:25:39 +02:00
2015-04-22 10:34:14 -04:00
assign ( ColumnCompiler _Oracle . prototype , {
// helper function for pushAdditional in increments() and bigincrements()
2015-05-09 13:58:18 -04:00
_createAutoIncrementTriggerAndSequence : function _createAutoIncrementTriggerAndSequence ( ) {
2015-04-22 10:34:14 -04:00
// TODO Add warning that sequence etc is created
this . pushAdditional ( function ( ) {
var sequenceName = this . tableCompiler . _indexCommand ( 'seq' , this . tableCompiler . tableNameRaw ) ;
2015-05-09 13:58:18 -04:00
var triggerName = this . tableCompiler . _indexCommand ( 'trg' , this . tableCompiler . tableNameRaw , this . getColumnName ( ) ) ;
var tableName = this . tableCompiler . tableName ( ) ;
var columnName = this . formatter . wrap ( this . getColumnName ( ) ) ;
var createTriggerSQL = 'create or replace trigger ' + triggerName + ' before insert on ' + tableName + ' for each row' + ' when (new.' + columnName + ' is null) ' + ' begin' + ' select ' + sequenceName + '.nextval into :new.' + columnName + ' from dual;' + ' end;' ;
2015-04-22 10:34:14 -04:00
this . pushQuery ( utils . wrapSqlWithCatch ( 'create sequence ' + sequenceName , - 955 ) ) ;
this . pushQuery ( createTriggerSQL ) ;
} ) ;
} ,
2015-05-09 13:58:18 -04:00
increments : function increments ( ) {
2015-04-22 10:34:14 -04:00
this . _createAutoIncrementTriggerAndSequence ( ) ;
return 'integer not null primary key' ;
} ,
2015-05-09 13:58:18 -04:00
bigincrements : function bigincrements ( ) {
2015-04-22 10:34:14 -04:00
this . _createAutoIncrementTriggerAndSequence ( ) ;
return 'number(20, 0) not null primary key' ;
} ,
2015-05-09 13:58:18 -04:00
floating : function floating ( precision ) {
2015-04-22 10:34:14 -04:00
var parsedPrecision = this . _num ( precision , 0 ) ;
return 'float' + ( parsedPrecision ? '(' + parsedPrecision + ')' : '' ) ;
} ,
2015-05-09 13:58:18 -04:00
double : function double ( precision , scale ) {
2015-04-22 10:34:14 -04:00
// if (!precision) return 'number'; // TODO: Check If default is ok
return 'number(' + this . _num ( precision , 8 ) + ', ' + this . _num ( scale , 2 ) + ')' ;
} ,
2015-05-09 13:58:18 -04:00
integer : function integer ( length ) {
return length ? 'number(' + this . _num ( length , 11 ) + ')' : 'integer' ;
2015-04-22 10:34:14 -04:00
} ,
tinyint : 'smallint' ,
2015-05-09 13:58:18 -04:00
2015-04-22 10:34:14 -04:00
smallint : 'smallint' ,
2015-05-09 13:58:18 -04:00
2015-04-22 10:34:14 -04:00
mediumint : 'integer' ,
2015-05-09 13:58:18 -04:00
2015-04-22 10:34:14 -04:00
biginteger : 'number(20, 0)' ,
2015-05-09 13:58:18 -04:00
2015-04-22 10:34:14 -04:00
text : 'clob' ,
2015-05-09 13:58:18 -04:00
enu : function enu ( allowed ) {
2015-04-22 10:34:14 -04:00
allowed = _ . uniq ( allowed ) ;
var maxLength = ( allowed || [ ] ) . reduce ( function ( maxLength , name ) {
return Math . max ( maxLength , String ( name ) . length ) ;
} , 1 ) ;
// implicitly add the enum values as checked values
this . columnBuilder . _modifiers . checkIn = [ allowed ] ;
2015-05-09 13:58:18 -04:00
return 'varchar2(' + maxLength + ')' ;
2015-04-22 10:34:14 -04:00
} ,
2015-06-29 13:07:28 -04:00
time : 'timestamp with time zone' ,
2015-04-22 10:34:14 -04:00
2015-06-29 13:07:28 -04:00
datetime : function datetime ( without ) {
return without ? 'timestamp' : 'timestamp with time zone' ;
} ,
2015-04-22 10:34:14 -04:00
2015-06-29 13:07:28 -04:00
timestamp : function timestamp ( without ) {
return without ? 'timestamp' : 'timestamp with time zone' ;
} ,
2015-04-22 10:34:14 -04:00
bit : 'clob' ,
2015-05-09 13:58:18 -04:00
2015-04-22 10:34:14 -04:00
json : 'clob' ,
2015-05-09 13:58:18 -04:00
bool : function bool ( ) {
2015-04-22 10:34:14 -04:00
// implicitly add the check for 0 and 1
this . columnBuilder . _modifiers . checkIn = [ [ 0 , 1 ] ] ;
return 'number(1, 0)' ;
} ,
2015-05-09 13:58:18 -04:00
varchar : function varchar ( length ) {
2015-04-22 10:34:14 -04:00
return 'varchar2(' + this . _num ( length , 255 ) + ')' ;
} ,
// Modifiers
// ------
2015-05-09 13:58:18 -04:00
comment : function comment ( _comment ) {
this . pushAdditional ( function ( ) {
this . pushQuery ( 'comment on column ' + this . tableCompiler . tableName ( ) + '.' + this . formatter . wrap ( this . args [ 0 ] ) + ' is \'' + ( _comment || '' ) + '\'' ) ;
} , _comment ) ;
2015-04-22 10:34:14 -04:00
} ,
2015-05-09 13:58:18 -04:00
checkIn : function checkIn ( value ) {
2015-04-22 10:34:14 -04:00
// TODO: Maybe accept arguments also as array
// TODO: value(s) should be escaped properly
if ( value === undefined ) {
return '' ;
} else if ( value instanceof Raw ) {
value = value . toQuery ( ) ;
} else if ( Array . isArray ( value ) ) {
value = _ . map ( value , function ( v ) {
2015-05-09 13:58:18 -04:00
return '\'' + v + '\'' ;
2015-04-22 10:34:14 -04:00
} ) . join ( ', ' ) ;
} else {
2015-05-09 13:58:18 -04:00
value = '\'' + value + '\'' ;
2015-04-22 10:34:14 -04:00
}
return 'check (' + this . formatter . wrap ( this . args [ 0 ] ) + ' in (' + value + '))' ;
2014-08-11 12:25:39 +02:00
}
2015-04-22 10:34:14 -04:00
2015-05-09 13:58:18 -04:00
} ) ;
2014-08-11 12:25:39 +02:00
2015-05-09 13:58:18 -04:00
module . exports = ColumnCompiler _Oracle ;