Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Often when I need to create a demo or PoC, I don't have a well defined design to start with, and the demo evolves. This makes it a pain to add or change a field if you have multiple layers of views or procedures, so I often want to just call some arbitary SQL from my HTML5 (or SAPUI5) front end.

oData doesn't really give me the full flexibility to do this, so I've written a simple xsjs routine to accept an SQL string, execute it and return the results as JSON. Strictly I do this as JSONP to allow me to develop the HTML locally, and call Hana without cross-domain issues. It also allows me to add optimiser hints.

The xsjs is below. It's certainly not the kind of approach you'd use in production, but it speeds up demos no end.You call it (typically from a jQuery ajax call) with a URL like http://server:port/generalSQL.xsjs?SQL=SELECT 23 from DUMMY  , and unwrap the results in javascript.

(Note - don't worry about spaces, and don;t put a ; on the end.

GeneralSQL.xsjs:

var rs;

var resultMetaData;

function columnValue2(columnNum){

var colValue = "";

switch(resultMetaData.getColumnType(columnNum)){

case xsruntime.db.types.BIGINT:          colValue = rs.getBigInt(columnNum); break;

case xsruntime.db.types.CHAR:

case xsruntime.db.types.VARCHAR:     colValue = rs.getString(columnNum); break;

case xsruntime.db.types.DATE:colValue = rs.getDate(columnNum); break;

case xsruntime.db.types.SMALLDECIMAL:

case xsruntime.db.types.DECIMAL:     colValue = rs.getDecimal(columnNum); break;

case xsruntime.db.types.DOUBLE:          colValue = rs.getDouble(columnNum); break;

case xsruntime.db.types.INT:

case xsruntime.db.types.INTEGER:

case xsruntime.db.types.SMALLINT:

case xsruntime.db.types.TINYINT:     colValue = rs.getInteger(columnNum); break;

case xsruntime.db.types.NCHAR:

case xsruntime.db.types.SHORTTEXT:

case xsruntime.db.types.NVARCHAR:     colValue = rs.getNString(columnNum); break;

case xsruntime.db.types.TEXT:

case xsruntime.db.types.NCLOB:          colValue = rs.getNClob(columnNum); break;

case xsruntime.db.types.REAL:          colValue = rs.getReal(columnNum); break;

case xsruntime.db.types.SECONDDATE:     colValue = rs.getSeconddate(columnNum); break;

case xsruntime.db.types.BLOB:          colValue = rs.getBlob(columnNum); break;

case xsruntime.db.types.CLOB:          colValue = rs.getClob(columnNum); break;

case xsruntime.db.types.ALPHANUM:     colValue = rs.getString(columnNum); break;

case xsruntime.db.types.TIME:          colValue = rs.getTime(columnNum); break;

case xsruntime.db.types.TIMESTAMP:     colValue = rs.getTimestamp(columnNum); break;

case xsruntime.db.types.BINARY:

case xsruntime.db.types.VARBINARY:     colValue = rs.getBString(columnNum); break;
}

return(colValue);

}

$.response.contentType = "text/html";

var thisColumn;

try {

var callbackFunctionName = $.request.parameters.get('callback');

var output = callbackFunctionName + "('";

var conn = $.db.getConnection();

var SQL = $.request.parameters.get('SQL');

var numColumns = 0;

var currentRecSep = "";

var resultTemplate = "";

if (SQL === null){

output = "No SQL specified";

$.response.setBody(output);

}

else {

var pstmt = conn.prepareStatement(SQL);

var rs = pstmt.executeQuery();

if (!rs.next()) {

$.response.setBody( "Failed to retrieve data" );

$.response.status = $.net.http.INTERNAL_SERVER_ERROR;

} else {

resultMetaData = rs.getMetaData();

numColumns = resultMetaData.getColumnCount();

output +='{"ALLRECORDS":[';

do {

output += currentRecSep + "{";

for (thisColumn = 1; thisColumn <= numColumns; thisColumn++){

if (thisColumn > 1) {output += ',';}

output+= '"' + resultMetaData.getColumnLabel(thisColumn) + '":"' + rs.getString(thisColumn) + '"';

}

output += "}";

currentRecSep = ",";

}

while (rs.next());

}

output += "]}";

output += "');"; // Close JSONP

rs.close();

pstmt.close();

conn.close();

$.response.setBody(output);

}

}

catch(e) {

$.response.setBody(

"Exception: " + e.toString());

}


1 Comment
Labels in this area