UNPKG

@sap/xsodata

Version:

Expose data from a HANA database as OData V2 service with help of .xsodata files.

194 lines (167 loc) 6.13 kB
'use strict'; //Include const sqlStatement = require('./sqlStatement'); const statementProcessor = require('./statementProcessor'); const utils = require('../utils/utils'); const SqlError = require('../utils/errors/sqlError'); exports.moveRecordNV_ToSelectStm = function (context, asyncDone) { context.logger.silly('dataCollectorPost', 'moveRecordNV_ToSelectStm'); //get dbSegment for inserting the payload const dbSegment = context.oData.dbSegmentLast; const statement = dbSegment.sql.stmContainer.insertTmp; if (!statement) { return asyncDone('Internal server error', context); } for (const recordNV of dbSegment._recordNV) { statement.setValue( recordNV.name, recordNV.value, dbSegment.entityType.propertiesMap[recordNV.name].DATA_TYPE_NAME ); // OK } return asyncDone(null, context); }; exports.createTmpTables = function (context, asyncDone) { context.logger.silly('dataCollectorPost', 'createTmpTables'); const stm = context.oData.dbSegmentLast.sql.stmContainer.createTmp; return statementProcessor.execStmsDirectlyNoResult(context, stm, asyncDone); }; exports.checkAutoValues = null; exports.checkForAutoKeyGenUsage = function (context, asyncDone) { context.logger.silly('dataCollectorPost', 'checkForAutoKeyGenUsage'); const dbSegment = context.oData.dbSegmentLast; if (!exports.checkAutoValues && !dbSegment.entityType.hasAdmindata()) { return asyncDone(null, context); } const statement = dbSegment.sql.stmContainer.insertTmp; const table = dbSegment._DB_TableName; const appData = {}; const nv = statement.nv; const createBy = dbSegment.entityType.getAddAdmindata('create', 'by'); const createAt = dbSegment.entityType.getAddAdmindata('create', 'at'); const modifyBy = dbSegment.entityType.getAddAdmindata('modify', 'by'); const modifyAt = dbSegment.entityType.getAddAdmindata('modify', 'at'); let name = ''; for (const elem of nv) { name = elem.name.toSqlHana(null, null, { withoutTable: true }); name = name.slice(1, -1); if (createBy && name === 'CREATED_BY') { elem.overwriteValue = new sqlStatement.Value(context.userName); } if (createAt && name === 'CREATED_AT') { elem.formula = 'NOW()'; } if (modifyBy && name === 'MODIFIED_BY') { elem.overwriteValue = new sqlStatement.Value(context.userName); } if (modifyAt && name === 'MODIFIED_AT') { elem.formula = 'NOW()'; } // build application api structure appData[name] = { value: elem.value, sequence: null, }; } if (!exports.checkAutoValues) { return asyncDone(null, context); } return exports.checkAutoValues(table, appData, function (err) { let name; for (const elem of nv) { name = elem.name.toSqlHana(null, null, { withoutTable: true }); name = name.substr(1, name.length - 2); if (appData[name].sequence) { elem.sequence = appData[name].sequence; } } return asyncDone(err, context); }); }; exports.insertPayloadIntoTempTable = function (context, asyncDone) { context.logger.silly('dataCollectorPost', 'insertPayloadIntoTempTable'); const dbSeg = context.oData.dbSegmentLast; statementProcessor.execStmsAsPreparedNoResult( context, dbSeg.sql.stmContainer.insertTmp, asyncDone ); }; exports.insertTmpTableToRealTable = function (context, asyncDone) { context.logger.silly('dataCollectorPost', 'insertTmpTableToRealTable'); const dbSeg = context.oData.dbSegmentLast; statementProcessor.execStmsAsPreparedNoResult( context, dbSeg.sql.stmContainer.insertReal, asyncDone ); }; exports.selectData = function (context, asyncDone) { context.logger.silly('dataCollectorPost', 'selectData'); const dbSeg = context.oData.dbSegmentLast; const stm = dbSeg.sql.stmContainer.select; return statementProcessor.execStmsAsPrepared( context, stm, (err, context, resultSet) => { const rows = resultSet[0]; if (err) { return asyncDone(new SqlError(context, err), context); } dbSeg.sql.rows = rows; if ( rows.length === 1 && utils.isETagHeaderRequired(context, dbSeg) ) { dbSeg.etagHeader = rows[0].__etag; } context.logger.silly( 'selectData', 'rows: ' + JSON.stringify(rows.length) ); return asyncDone(null, context); } ); }; exports.commit = function (context, asyncDone) { context.logger.debug('dataCollectorPost', 'commit'); const client = context.db.client; client.commit(function (err) { if (err) { context.logger.info( 'SQL Exec', 'Commit Error: \n' + JSON.stringify(err) ); return asyncDone(err, context); } return asyncDone(null, context); }); }; /** * Executes truncation of temporary created tables * * @param {Object} context The xsodata context * @param {Function} asyncDone async waterfall callback */ exports.truncateTempTables = function (context, asyncDone) { context.logger.silly('dataCollectorPost', 'truncateTempTables'); return statementProcessor.execTempTableStatements( context, context.sql.container.createTmpTruncate, asyncDone ); }; /** * Executes deletion of temporary created tables * * @param {Object} context The xsodata context * @param {Function} asyncDone async waterfall callback */ exports.dropTempTables = function (context, asyncDone) { context.logger.silly('dataCollectorPost', 'dropTempTables'); statementProcessor.execTempTableStatements( context, context.sql.container.createTmpDrop, asyncDone ); };