1 16 package org.outerj.daisy.query.model.functions; 17 18 import org.outerj.daisy.query.model.SqlGenerationContext; 19 import org.outerj.daisy.repository.query.QueryException; 20 21 import java.math.BigDecimal ; 22 23 public class RoundFunction extends AbstractBinaryMathFunction { 24 public static final String NAME = "Round"; 25 26 public String getFunctionName() { 27 return NAME; 28 } 29 30 protected Object performCalculation(BigDecimal value1, BigDecimal value2) { 31 int scale = value2.intValue(); 32 if (scale < 0) 33 scale = 0; 34 if (value1.scale() > scale) 35 return value1.setScale(scale, BigDecimal.ROUND_HALF_DOWN); 36 else 37 return value1; 38 } 39 40 public void generateSqlValueExpr(StringBuffer sql, SqlGenerationContext context) throws QueryException { 41 sql.append(" ROUND( "); 42 getParam(0).generateSqlValueExpr(sql, context); 43 sql.append(", "); 44 getParam(1).generateSqlValueExpr(sql, context); 45 sql.append(") "); 46 } 47 48 public String getExpression() { 49 return getFunctionName() + "(" + getParam(0).getExpression() + ", " + getParam(1).getExpression() + ")"; 50 } 51 52 protected String getMathSymbol() { 53 throw new RuntimeException ("This method should not be called."); 54 } 55 } 56 | Popular Tags |