@sap/xsodata
Version:
Expose data from a HANA database as OData V2 service with help of .xsodata files.
520 lines (483 loc) • 11.9 kB
JavaScript
'use strict';
var Big = require("big.js");
var timeRegexObj = /^P((\d+)Y)?((\d+)M)?((\d+)D)?T((\d+)H)?((\d+)M)?((\d+)(?:\.(\d*))?S)?$/i;
var timeRegexObjTime = /^time'P((\d+)Y)?((\d+)M)?((\d+)D)?T((\d+)H)?((\d+)M)?((\d+)(?:\.(\d*))?S)?'$/i;
var Type_Error = require('./../errors/typeError');
function padZero(string, len) {
if (string.length < len) {
return new Array(len - string.length + 1).join('0') + string;
}
return string;
}
exports.checkEdmTime = function (value, dbDataType, withTimePrefix, YMDmustBeZero, allowFractions, getYMD) {
let matches = null;
if (withTimePrefix === true) {
matches = timeRegexObjTime.exec(value);
} else {
matches = timeRegexObj.exec(value);
}
if (!allowFractions) {
if (matches[13] && Number.parseInt(matches[13]) !== 0) {
throw new Type_Error(`Fractional part for OData type Edm.Time not supported`);
}
}
if (YMDmustBeZero) {
if (matches[1] && Number.parseInt(matches[1]) !== 0) {
throw new Type_Error(`value must be compatible to hana type ${dbDataType} (check year)`);
}
if (matches[3] && Number.parseInt(matches[3]) !== 0) {
throw new Type_Error(`value must be compatible to hana type ${dbDataType} (check month)`);
}
if (matches[5] && Number.parseInt(matches[5]) !== 0) {
throw new Type_Error(`value must be compatible to hana type ${dbDataType} (check day)`);
}
}
if (getYMD === false) {
let timeParts = [];
timeParts[0] = (matches[7 + 1]) ? padZero(matches[7 + 1], 2) : '00';
timeParts[1] = (matches[7 + 3]) ? padZero(matches[7 + 3], 2) : '00';
timeParts[2] = (matches[7 + 5]) ? padZero(matches[7 + 5], 2) : '00';
return timeParts.join(':');
} else {
// SECONDTIME
// for range see https://help.sap.com/viewer/4fe29514fd584807ac9f2a04f6754767/2.0.03/en-US/3f81ccc7e35d44cbbc595c7d552c202a.html
// minimal value is 0001-01-01 00:00:01
if (!matches[2] || Number.parseInt(matches[2]) <= 0) {
throw new Type_Error('value must be compatible to hana type SECONDTIME (check year)');
}
if (!matches[4] || Number.parseInt(matches[4]) <= 0) {
throw new Type_Error('value must be compatible to hana type SECONDTIME (check month)');
}
if (!matches[6] || Number.parseInt(matches[6]) <= 0) {
throw new Type_Error('value must be compatible to hana type SECONDTIME (check day)');
}
let dateParts = [];
dateParts[0] = (matches[2]) ? padZero(matches[2], 4) : '00';
dateParts[1] = (matches[4]) ? padZero(matches[4], 2) : '00';
dateParts[2] = (matches[6]) ? padZero(matches[6], 2) : '00';
let timeParts = [];
timeParts[0] = (matches[7 + 1]) ? padZero(matches[7 + 1], 2) : '00';
timeParts[1] = (matches[7 + 3]) ? padZero(matches[7 + 3], 2) : '00';
timeParts[2] = (matches[7 + 5]) ? padZero(matches[7 + 5], 2) : '00';
return dateParts.join('-') + ' ' + timeParts.join(':');
}
};
exports.datetime7 = function (dbValue, addLetterTbehindDate) {
let parts = dbValue.split('.');
let dateAndTime = parts[0];
let frac = parts[1];
if (addLetterTbehindDate && dateAndTime.indexOf('T') === -1) {
const dtPart = dateAndTime.split(' ');
dateAndTime = dtPart.join('T');
}
if (parts.length === 1) {
return '' + dateAndTime + '.0000000';
} else {
while (frac.length < 7) {
frac += '0';
}
/*
hdb db driver returned only 3 digits
if (frac.length > 7 && frac.substring(7) !== '00') {
throw new Error('Fractional value can not be converted');
}
*/
frac = frac.substr(0, 7);
return '' + dateAndTime + '.' + frac;
}
};
/**
* Converts the input <code>decimalString</code> into the decimal notation, if it is specified in the exponential
* notation.
* @param {string} decimalString - string representing a decimal value.
* @returns {string} decimal value in the decimal notation.
*/
exports.formatDecimal = function formatDecimal(decimalString) {
var decimalValue;
if (!decimalString) {
return decimalString;
}
if (decimalString.indexOf("e") !== -1 || decimalString.indexOf("E") !== -1) {
decimalValue = new Big(decimalString);
return decimalValue.toFixed();
}
return decimalString;
};
exports.decimal_db_to_uri = function (decimalAsString) {
var arr = decimalAsString.split(/[eE]/);
if (arr.length === 1) {
// input was "3"
return decimalAsString;
}
if (arr[1] === '') {
// input was"3e"
return decimalAsString.substr(0, decimalAsString.length - 1);
}
var man = arr[0];
var exp = arr[1];
var sign = '';
if (man[0] === '+' || man[0] === '-') {
sign = man[0];
man = man.substr(1);
}
var point = man.indexOf('.');
if (point === -1) {
point = man.length;
}
var expInt = parseInt(exp);
var out = sign;
//move comma left
var pre = 0;
while (pre < point && man[pre] !== '0') {
pre++;
}
var post = (man.length === point) ? 0 : man.length - point - 1;
var spre = man.substr(point - pre, pre);
var spost = man.substr(point + 1, post);
//return spost;
// out = '(' + pre + ' ' + post + ')';
if (expInt === 0) {
out += (pre === 0) ? '0' : spre;
if (post !== 0) {
out += '.';
out += spost;
}
return out;
}
//var pn = point + expInt;
//return pn;
if (expInt > 0) {
out += spre;
if (post) {
if (expInt >= post) {
out += spost;
expInt -= post;
} else {
out += spost.substr(0, expInt);
out += '.';
out += spost.substr(expInt);
return out;
}
}
while (expInt > 0) {
out += '0';
expInt--;
}
return out;
} else {
expInt *= -1;
if (pre - expInt <= 0) {
out += '0.';
while (expInt > pre) {
out += '0';
expInt--;
}
out += spre;
out += spost;
} else {
out += spre.substr(0, pre - expInt);
out += '.';
out += spre.substr(pre - expInt);
}
/*
if (pre) {
if (expInt <= pre) {
out += spre;
expInt -= pre;
} else {
out +=
}
}*/
return out;
}
};
/*TODO future: Add automatic tests for this */
/*
print('1');
print('1.5');
print('123.5');
print('123.56');
print('123.567');
print('123.567e');
print('123.567e-5');
print('123.567e-4');
print('123.567e-3');
print('123.567e-2');
print('123.567e-1');
print('123.567e-0');
print('123.567e0');
print('123.567e1');
print('123.567e2');
print('123.567e3');
print('123.567e4');
print('123.567e5');
print('123.567e+0');
print('123.567e+1');
print('123.567e+2');
print('123.567e+3');
print('123.567e+4');
print('123.567e+5');
print('123e-5');
print('123e-4');
print('123e-3');
print('123e-2');
print('123e-1');
print('123e-0');
print('123e0');
print('123e1');
print('123e2');
print('123e3');
print('123e4');
print('123e5');
print('123e+0');
print('123e+1');
print('123e+2');
print('123e+3');
print('123e+4');
print('123e+5');
print('0.1e-5');
print('0.1e-4');
print('0.1e-3');
print('0.1e-2');
print('0.1e-1');
print('0.1e-0');
print('0.1e0');
print('0.1e1');
print('0.1e2');
print('0.1e3');
print('0.1e4');
print('0.1e5');
print('0.1e+0');
print('0.1e+1');
print('0.1e+2');
print('0.1e+3');
print('0.1e+4');
print('0.1e+5');
print('.1e-5');
print('.1e-4');
print('.1e-3');
print('.1e-2');
print('.1e-1');
print('.1e-0');
print('.1e0');
print('.1e1');
print('.1e2');
print('.1e3');
print('.1e4');
print('.1e5');
print('.1e+0');
print('.1e+1');
print('.1e+2');
print('.1e+3');
print('.1e+4');
print('.1e+5');
print('1.e-5');
print('1.e-4');
print('1.e-3');
print('1.e-2');
print('1.e-1');
print('1.e-0');
print('1.e0');
print('1.e1');
print('1.e2');
print('1.e3');
print('1.e4');
print('1.e5');
print('1.e-0');
print('1.e-1');
print('1.e-2');
print('1.e-3');
print('1.e-4');
print('1.e-5');
print('-1');
print('-1.5');
print('-123.5');
print('-123.56');
print('-123.567');
print('-123.567e');
print('-123.567e-5');
print('-123.567e-4');
print('-123.567e-3');
print('-123.567e-2');
print('-123.567e-1');
print('-123.567e-0');
print('-123.567e0');
print('-123.567e1');
print('-123.567e2');
print('-123.567e3');
print('-123.567e4');
print('-123.567e5');
print('-123.567e+0');
print('-123.567e+1');
print('-123.567e+2');
print('-123.567e+3');
print('-123.567e+4');
print('-123.567e+5');
print('-123e-5');
print('-123e-4');
print('-123e-3');
print('-123e-2');
print('-123e-1');
print('-123e-0');
print('-123e0');
print('-123e1');
print('-123e2');
print('-123e3');
print('-123e4');
print('-123e5');
print('-123e+0');
print('-123e+1');
print('-123e+2');
print('-123e+3');
print('-123e+4');
print('-123e+5');
print('-0.1e-5');
print('-0.1e-4');
print('-0.1e-3');
print('-0.1e-2');
print('-0.1e-1');
print('-0.1e-0');
print('-0.1e0');
print('-0.1e1');
print('-0.1e2');
print('-0.1e3');
print('-0.1e4');
print('-0.1e5');
print('-0.1e+0');
print('-0.1e+1');
print('-0.1e+2');
print('-0.1e+3');
print('-0.1e+4');
print('-0.1e+5');
print('-.1e-5');
print('-.1e-4');
print('-.1e-3');
print('-.1e-2');
print('-.1e-1');
print('-.1e-0');
print('-.1e0');
print('-.1e1');
print('-.1e2');
print('-.1e3');
print('-.1e4');
print('-.1e5');
print('-.1e+0');
print('-.1e+1');
print('-.1e+2');
print('-.1e+3');
print('-.1e+4');
print('-.1e+5');
print('-1.e-5');
print('-1.e-4');
print('-1.e-3');
print('-1.e-2');
print('-1.e-1');
print('-1.e-0');
print('-1.e0');
print('-1.e1');
print('-1.e2');
print('-1.e3');
print('-1.e4');
print('-1.e5');
print('-1.e+0');
print('-1.e+1');
print('-1.e+2');
print('-1.e+3');
print('-1.e+4');
print('-1.e+5');
print('+1');
print('+1.5');
print('+123.5');
print('+123.56');
print('+123.567');
print('+123.567e');
print('+123.567e-5');
print('+123.567e-4');
print('+123.567e-3');
print('+123.567e-2');
print('+123.567e-1');
print('+123.567e-0');
print('+123.567e0');
print('+123.567e1');
print('+123.567e2');
print('+123.567e3');
print('+123.567e4');
print('+123.567e5');
print('+123.567e+0');
print('+123.567e+1');
print('+123.567e+2');
print('+123.567e+3');
print('+123.567e+4');
print('+123.567e+5');
print('+123e-5');
print('+123e-4');
print('+123e-3');
print('+123e-2');
print('+123e-1');
print('+123e-0');
print('+123e0');
print('+123e1');
print('+123e2');
print('+123e3');
print('+123e4');
print('+123e5');
print('+123e+0');
print('+123e+1');
print('+123e+2');
print('+123e+3');
print('+123e+4');
print('+123e+5');
print('+0.1e-5');
print('+0.1e-4');
print('+0.1e-3');
print('+0.1e-2');
print('+0.1e-1');
print('+0.1e-0');
print('+0.1e0');
print('+0.1e1');
print('+0.1e2');
print('+0.1e3');
print('+0.1e4');
print('+0.1e5');
print('+0.1e+0');
print('+0.1e+1');
print('+0.1e+2');
print('+0.1e+3');
print('+0.1e+4');
print('+0.1e+5');
print('+.1e-5');
print('+.1e-4');
print('+.1e-3');
print('+.1e-2');
print('+.1e-1');
print('+.1e-0');
print('+.1e0');
print('+.1e1');
print('+.1e2');
print('+.1e3');
print('+.1e4');
print('+.1e5');
print('+.1e+0');
print('+.1e+1');
print('+.1e+2');
print('+.1e+3');
print('+.1e+4');
print('+.1e+5');
print('+1.e-5');
print('+1.e-4');
print('+1.e-3');
print('+1.e-2');
print('+1.e-1');
print('+1.e-0');
print('+1.e0');
print('+1.e1');
print('+1.e2');
print('+1.e3');
print('+1.e4');
print('+1.e5');
print('+1.e+0');
print('+1.e+1');
print('+1.e+2');
print('+1.e+3');
print('+1.e+4');
print('+1.e+5');*/