1 16 package org.outerj.daisy.query.model.functions; 17 18 import org.outerj.daisy.query.model.AbstractFunction; 19 import org.outerj.daisy.query.model.QValueType; 20 import org.outerj.daisy.query.model.SqlGenerationContext; 21 import org.outerj.daisy.query.QueryContext; 22 import org.outerj.daisy.repository.query.QueryException; 23 import org.outerj.daisy.repository.query.EvaluationContext; 24 import org.outerj.daisy.repository.Document; 25 import org.outerj.daisy.repository.Version; 26 27 import java.util.Locale ; 28 import java.sql.PreparedStatement ; 29 import java.sql.SQLException ; 30 31 public class RandomFunction extends AbstractFunction { 32 public static final String NAME = "Random"; 33 34 public String getFunctionName() { 35 return NAME; 36 } 37 38 public void prepare(QueryContext context) throws QueryException { 39 super.prepare(context); 40 if (params.size() != 0) 41 throw new QueryException(NAME + " function takes no arguments."); 42 } 43 44 public void generateSqlValueExpr(StringBuffer sql, SqlGenerationContext context) throws QueryException { 45 sql.append(" ? "); 46 } 47 48 public int bindValueExpr(PreparedStatement stmt, int bindPos, QValueType valueType, EvaluationContext evaluationContext) throws SQLException , QueryException { 49 stmt.setDouble(bindPos, ((Double )evaluate(null, evaluationContext)).doubleValue()); 50 return ++bindPos; 51 } 52 53 public Object evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException { 54 return new Double (Math.random()); 55 } 56 57 public Object evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 58 return evaluate(null, null); 59 } 60 61 public QValueType getValueType() { 62 return QValueType.DOUBLE; 63 } 64 65 public String getTitle(Locale locale) { 66 return getExpression(); 67 } 68 69 public QValueType getOutputValueType() { 70 return QValueType.DOUBLE; 71 } 72 73 public Object getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 74 return evaluate(null, null); 75 } 76 } 77 | Popular Tags |