GeSHi Test

Useful code snippets and help for problems with Velocity-/Java-Script Code

GeSHi Test

Postby tantalus » 18 Sep 2013, 16:56

Macro SQL:
Code: Select all

#if($RR.isNull($ActualExpectedResultTuple))
''##
#elseif($ActualExpectedResultTuple.size() == 0)
''##
#elseif($RR.mod($ActualExpectedResultTuple.size(), 2) != 0)
$RR.throwException("Incomplete list of actual/expected result tupples (Odd number)!")
#else
## for 0..count of tuples -1
#set($compareCaseWhens = [])
#foreach($idx in [0..$RR.sum($RR.div($ActualExpectedResultTuple.size(), 2), -1)])
#set($testResultTarget = $RR.ifNull($ActualExpectedResultTuple[$RR.prod(2, $idx).intValue()], "NULL"))
#set($testActualResult = $RR.ifNull($ActualExpectedResultTuple[$RR.sum($RR.prod(2, $idx), 1).intValue()], "NULL"))
#set($no = "#" + $RR.sum($idx, 1).intValue().toString().trim())
#set($failed   = "FAILED: " + $no + " $testResultTarget  $testActualResult; ")
#set($succeded =              $no + " $testResultTarget = $testActualResult; ")
#set($failed = "'" + $failed.replace("'" , '"') + "'")
#set($succeded = "'" + $succeded.replace("'" , '"') + "'")
#if($justCountFails)
$RR.call($compareCaseWhens.add("#testResultCompareCaseWhen($testResultTarget $testActualResult '0' '1' $typeSaveNull)"))
#else
$RR.call($compareCaseWhens.add("#testResultCompareCaseWhen($testResultTarget $testActualResult $succeded $failed $typeSaveNull)"))
#endif
#next
#endif
#if($justCountFails)
#foreach($compareCaseWhen in $compareCaseWhens)
$compareCaseWhen##
#if($velocityHasNext)
 + ##
#endif
#next
#else
#sqlConcatNYE($compareCaseWhens)
#endif
 


JavaScript:
Code: Select all

var sglQuote = String.fromCharCode(39);
var allSglQuoteRegExpr = new RegExp(sglQuote, "g");
var allLineBreaksRegExpr = new RegExp("[\n\r]", "g");
var allCharX00RegExpr = new RegExp("\x00", "g");
var charX00RegExpr = new RegExp("\x00", "");

function JavaListToJavaScriptArray(javaList) {
  var idx = 0;
  var javaScriptArray = [];

  while(idx < javaList.size()) {

    javaScriptArray.push(javaList.get(idx));

    idx++;

  }

  return javaScriptArray;
}

function quote(literal) {
  $CurrentReport.logger().debug(literal);
  return sglQuote + literal.toString().replace(allSglQuoteRegExpr, "~").replace(allLineBreaksRegExpr, " ") + sglQuote;
}
 
// aliases and literals are assumed being same sized string arraies
function getAliasedLiterals(aliases, literals) {
  var nullDelimitedLiterals = "";
  var idxAliases = 0;

  nullDelimitedLiterals = literals.join(String.fromCharCode(0)).replace(allLineBreaksRegExpr, " ");

  nullDelimitedLiterals = nullDelimitedLiterals + String.fromCharCode(0);

  while(idxAliases < aliases.length) {

    nullDelimitedLiterals = nullDelimitedLiterals.replace(charX00RegExpr, " AS " + aliases[idxAliases] + ", ");

    idxAliases++;

  }

  return nullDelimitedLiterals.substr(0, nullDelimitedLiterals.length - 2); // cut off trailing ", "

}

    $CurrentReport.logger().debug("in in in");

var rs;
var row = "";
var script;
var connectionName = "unknown";
var sql = "";
var columnList = JavaListToJavaScriptArray($columnList);
    $CurrentReport.logger().debug("convert");

try {

  try { // Check, if script is accessable

    $CurrentReport.logger().debug("script = $CurrentReport.getScriptByName($scriptName);");
    script = $CurrentReport.getScriptByName($scriptName);
  $CurrentReport.logger().debug("$scriptName=" + $scriptName);
   
  $CurrentReport.logger().debug("connectionName ='" + script.getLogicalConnectionName() + "'");
 
  } catch (error) {
 
    throw new UserException("Script " + $scriptName + " does not exist!");

  }

  $CurrentReport.logger().debug("sql = script.getEvaluatedBody();");
  $CurrentReport.logger().debug("SCRIPT&#058; " + script.getName());
  sql = script.getEvaluatedBody();
  $CurrentReport.logger().debug("SQL= " + sql);

  connectionName = script.getLogicalConnectionName();
  rs = $RR.getResultSet(sql, connectionName);


  if (rs.next()) {

    var column = "";
    var idxCol = 1;
    var literals = [];
 
    do {

        try {

          column = rs.getString(idxCol);
       
        } catch (error) {
          column = "ERROR: accessing column #" + idxCol + error.message;
       
        } finally {
   
          literals.push(quote(column));

        }

      idxCol++;
 
    } while (idxCol <= rs.getMetaData().getColumnCount())

    // place OK as very first column
    literals = [quote("OK")].concat(literals);

    row = getAliasedLiterals(columnList, literals);

  } else {
 
   row = getAliasedLiterals(columnList, [quote("ERROR: No result set row! "),
                                         quote($scriptName),
                                         quote(""),  
                                         quote(-1),
                                         quote(sql),
                                         quote(""),
                                         quote("")
                                         ]);

  }

} catch (error) {
   $CurrentReport.logger().debug("ERROR: Cannot access result row: " + error.message);
   $CurrentReport.logger().debug($scriptName);
   $CurrentReport.logger().debug(connectionName.toUpperCase());
   $CurrentReport.logger().debug(sql);
   row = getAliasedLiterals(columnList, [quote("ERROR: Cannot access result row: " + error.message),
                                         quote($scriptName),
                                         quote(connectionName.toUpperCase()),  
                                         quote(-1),
                                         quote(sql),
                                         quote(""),
                                         quote("")
                                         ]);
} finally {
  try {
    rs.close();
  } catch(error) {
    // so what?!
  }
}

  return row;
 
tantalus
Site Admin
 
Posts: 1
Joined: 10 Sep 2013, 10:15

Return to Developers Corner



Who is online

Users browsing this forum: No registered users and 3 guests

cron