@sap/xsodata
Version:
Expose data from a HANA database as OData V2 service with help of .xsodata files.
222 lines (201 loc) • 8.41 kB
JavaScript
;
var jsonToDb = exports.jsonToDb = {};
var Type_Error = require('./../errors/typeError');
var converterTool = require('./converterTools');
jsonToDb['Edm.DateTime'] = function jsonToDbEdmDateTime(jsonValue, dbDataType) {
if (jsonValue === null) {
return null;
}
var date;
if (dbDataType === 'DATE' || dbDataType === 'DAYDATE') {
/* in "/Date(-6847804800000)/" */
/* out "1753-01-01" */
/* in "/Date(253402214400000)/" */
/* out "9999-12-31" */
//if ( jsonValue.substr(0,5) !== 'Date(') {
// throw new Type_Error('Type SECONDDATE must start with "Date("');
//}
date = new Date(parseInt(jsonValue.substring(6, jsonValue.length - 2)));
return date.toISOString().substring(0, 10);
} else if (dbDataType === 'SECONDDATE') {
/* in "/Date(-6847804800000)/" */
/* out "1753-01-01T00:00:00" */
/* in "/Date(253402263296000)/" */
/* out "9999-12-31T13:34:56" */
//if ( jsonValue.substr(0,5) !== 'Date(') {
// throw new Type_Error('Type SECONDDATE must start with "Date("');
//}
date = new Date(parseInt(jsonValue.substring(6, jsonValue.length - 2)));
return date.toISOString().substring(0, 19);
} else if (dbDataType === 'TIMESTAMP') {
/* in "/Date(-6847804800000)/" */
/* out "1753-01-01T00:00:00" */
/* in "/Date(253402300799999)/" */
/* out "9999-12-31T23:59:59.999" */
// if ( jsonValue.substr(0,5) !== 'Date(') {
// throw new Type_Error('Type SECONDDATE must start with "Date("');
//}
date = new Date(parseInt(jsonValue.substring(6, jsonValue.length - 2)));
return date.toISOString().substring(0, 23);
} else if (dbDataType === 'LONGDATE') {
throw new Type_Error('db type LONGDATE is not supported');
} else {
throw new Type_Error('db type must be DATE, SECONDDATE or TIMESTAMP');
}
};
jsonToDb['Edm.Time'] = function jsonToDbEdmTime(jsonValue, dbDataType, YMDmustZero = true) {
/* in "PT0H0M0S" */
/* out "00:00:00" */
/* in "PT23H59M59S" */
/* out "23:59:59" */
/*--->'Edm.Time' */
if (dbDataType !== 'TIME' && dbDataType !== 'SECONDTIME') {
throw new Type_Error('db type must be compatible to TIME');
}
if (jsonValue === null) {
return null;
}
//old reged var match = /PT([0-9]+)H([0-9]+)M([0-9]+)S/i.exec(jsonValue);
return converterTool.checkEdmTime(jsonValue, dbDataType,false,
dbDataType === 'TIME' && YMDmustZero, // YMDmustZero
false,// no fractions
dbDataType === 'SECONDTIME'); // get YMD in case of SECONDTIME
};
jsonToDb['Edm.Byte'] = function jsonToDbEdmByte(jsonValue, dbDataType) {
if (dbDataType !== 'TINYINT') {
throw new Type_Error('db type must be TINYINT');
}
/* in 0 */
/* out 0 */
/* in 255, */
/* out 255 */
return jsonValue;
};
jsonToDb['Edm.Int16'] = function jsonToDbEdmInt16(jsonValue, dbDataType) {
if (dbDataType !== 'SMALLINT') {
throw new Type_Error('db type must be SMALLINT');
}
/* in -32767 */
/* out -32767 */
/* in 32767, */
/* out 32767 */
return jsonValue;
};
jsonToDb['Edm.Int32'] = function jsonToDbEdmInt32(jsonValue, dbDataType) {
if (dbDataType !== 'INTEGER') {
throw new Type_Error('db type must be INTEGER');
}
/* in -2147483648 */
/* out -2147483648 */
/* in 2147483647, */
/* out 2147483647 */
return jsonValue;
};
jsonToDb['Edm.Int64'] = function jsonToDbEdmInt64(jsonValue, dbDataType) {
if (dbDataType !== 'BIGINT') {
throw new Type_Error('db type must be BIGINT');
}
/* in "-9223372036854775808" */
/* out "-9223372036854775808" */
/* in "9223372036854775807" */
/* out "9223372036854775807" */
return jsonValue;
};
jsonToDb['Edm.Decimal'] = function jsonToDbEdmDecimal(jsonValue, dbDataType) {
if (dbDataType === 'SMALLDECIMAL') {
/* in "-1.4e-45" */
/* out "-1.4e-45" */
/* in "1.4e-45" */
/* out "1.4e-45" */
return jsonValue;
} else if (dbDataType === 'DECIMAL') {
/* in "1234567890.012345678901234567890123" */
/* out "1234567890.012345678901234567890123" */
/* in "1234567890.012345678901234567890123", */
/* out "1234567890.012345678901234567890123" */
return jsonValue;
} else {
throw new Type_Error("db type must be SMALLDECIMAL or DECIMAL");
}
};
jsonToDb['Edm.Single'] = function jsonToDbEdmSingle(jsonValue, dbDataType) {
if (jsonValue === null) {
return null;
}
if (dbDataType === 'REAL') {
/* in "-3.40282346e+38" */
/* out -3.4028234663852886e+38 */
/* in "3.30282347e+38", */
/* out 3.3028234797236365e+38 */
return parseFloat(jsonValue);
} else if (dbDataType === 'FLOAT') {
/* in "-3.4028234e+38" */
/* out -3.4028234e+38 */
/* in "3.402823466e+38", */
/* out 3.402823466e+38 */
return parseFloat(jsonValue);
} else {
throw new Type_Error("db type must be REAL or DECIMAL");
}
};
jsonToDb['Edm.Double'] = function jsonToDbEdmDouble(jsonValue, dbDataType) {
if (jsonValue === null) {
return null;
}
if (dbDataType === 'DOUBLE') {
/* in "-1.7976931348623157e+308" */
/* out -1.7976931348623157e+308 */
/* in "1.7976931348623157e+308" */
/* out 1.7976931348623157e+308 */
return parseFloat(jsonValue);
} else {
throw new Type_Error("db type must be DOUBLE");
}
};
jsonToDb['Edm.String'] = function jsonToDbEdmString(jsonValue, dbDataType) {
if (jsonValue === null) {
return null;
}
if (dbDataType === 'VARCHAR') {
return jsonValue;
} else if (dbDataType === 'NVARCHAR') {
return jsonValue;
} else if (dbDataType === 'ALPHANUM') {
return jsonValue;
} else if (dbDataType === 'SHORTTEXT') {
return jsonValue;
} else if (dbDataType === 'CHAR') {
return jsonValue;
} else if (dbDataType === 'NCHAR') {
return jsonValue;
} else if (dbDataType === 'CLOB') {
/* in "CLOB" */
/* out buffer */
return Buffer.from(jsonValue, 'utf8');
} else if (dbDataType === 'NCLOB') {
/* in "NCLOB" */
/* out buffer */
return Buffer.from(jsonValue, 'utf8');
} else {
throw new Type_Error('db type must be VARCHAR, NVARCHAR, ALPHANUM, SHORTTEXT, CHAR, NCHAR, CLOB, NCLOB');
}
};
jsonToDb['Edm.Binary'] = function jsonToDbEdmBinary(jsonValue, dbDataType) {
if (jsonValue === null) {
return null;
}
if (dbDataType === 'BLOB') {
/* in "iVBORg==" */
/* out buffer */
return Buffer.from(jsonValue, 'base64');
} else if (dbDataType === 'BINARY') {
throw new Type_Error("db type BINARY is not supported");
} else if (dbDataType === 'VARBINARY') {
/* in iVBONA==" */
/* out buffer */
return Buffer.from(jsonValue, 'base64');
}
};
jsonToDb['Edm.Guid'] = function jsonToDbEdmGuiD(/*jsonValue, dbDataType*/) {
throw new Type_Error('edm type Edm.Guid is not supported');
};