| 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 ModFunction extends AbstractBinaryMathFunction { 24 public static final String NAME = "Mod"; 25 26 public String getFunctionName() { 27 return NAME; 28 } 29 30 protected Object performCalculation(BigDecimal value1, BigDecimal value2) { 31 value1 = value1.abs(); 34 value2 = value2.abs(); 35 BigDecimal quotient = value1.divide(value2, BigDecimal.ROUND_HALF_DOWN); 36 quotient = quotient.setScale(0, BigDecimal.ROUND_DOWN); 37 return value1.subtract(quotient.multiply(value2)); 38 } 39 40 public void generateSqlValueExpr(StringBuffer sql, SqlGenerationContext context) throws QueryException { 41 sql.append(" MOD( "); 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 57 | Popular Tags |