2014-08-11 12:25:39 +02:00
'use strict' ;
2015-04-22 10:34:14 -04:00
var helpers = require ( '../../helpers' ) ;
2014-08-11 12:25:39 +02:00
function generateCombinedName ( postfix , name , subNames ) {
2015-04-22 14:53:31 -04:00
var crypto = require ( 'crypto' ) ;
2015-05-09 13:58:18 -04:00
var limit = 30 ;
2015-04-22 10:34:14 -04:00
if ( ! Array . isArray ( subNames ) ) subNames = subNames ? [ subNames ] : [ ] ;
2014-08-11 12:25:39 +02:00
var table = name . replace ( /\.|-/g , '_' ) ;
var subNamesPart = subNames . join ( '_' ) ;
2015-05-09 13:58:18 -04:00
var result = ( table + '_' + ( subNamesPart . length ? subNamesPart + '_' : '' ) + postfix ) . toLowerCase ( ) ;
2014-08-11 12:25:39 +02:00
if ( result . length > limit ) {
helpers . warn ( 'Automatically generated name "' + result + '" exceeds ' + limit + ' character limit for Oracle. Using base64 encoded sha1 of that name instead.' ) ;
// generates the sha1 of the name and encode it with base64
2015-05-09 13:58:18 -04:00
result = crypto . createHash ( 'sha1' ) . update ( result ) . digest ( 'base64' ) . replace ( '=' , '' ) ;
2014-08-11 12:25:39 +02:00
}
return result ;
}
function wrapSqlWithCatch ( sql , errorNumberToCatch ) {
2015-05-09 13:58:18 -04:00
return 'begin execute immediate \'' + sql . replace ( /'/g , '\'\'' ) + '\'; exception when others then if sqlcode != ' + errorNumberToCatch + ' then raise; end if; end;' ;
2014-08-11 12:25:39 +02:00
}
function ReturningHelper ( columnName ) {
this . columnName = columnName ;
}
ReturningHelper . prototype . toString = function ( ) {
return '[object ReturningHelper:' + this . columnName + ']' ;
2015-05-09 13:58:18 -04:00
} ;
2014-08-11 12:25:39 +02:00
module . exports = {
generateCombinedName : generateCombinedName ,
wrapSqlWithCatch : wrapSqlWithCatch ,
ReturningHelper : ReturningHelper
2015-05-09 13:58:18 -04:00
} ;