1 16 package org.outerj.daisy.query.model.functions; 17 18 import org.outerj.daisy.query.model.*; 19 import org.outerj.daisy.query.QueryContext; 20 import org.outerj.daisy.repository.query.QueryException; 21 import org.outerj.daisy.repository.query.EvaluationContext; 22 import org.outerj.daisy.repository.Document; 23 import org.outerj.daisy.repository.Version; 24 25 import java.util.Locale ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 29 public class ContextDocFunction extends AbstractFunction { 30 public static final String NAME = "ContextDoc"; 31 32 public String getFunctionName() { 33 return NAME; 34 } 35 36 public void prepare(QueryContext context) throws QueryException { 37 super.prepare(context); 38 if (params.size() != 1) 39 throw new QueryException(NAME + " function expects exactly one parameter."); 40 } 41 42 public void generateSqlValueExpr(StringBuffer sql, SqlGenerationContext context) throws QueryException { 43 sql.append(" ? "); 44 } 45 46 public String getSqlPreConditions(SqlGenerationContext context) throws QueryException { 47 return null; 49 } 50 51 public int bindPreConditions(PreparedStatement stmt, int bindPos) throws SQLException , QueryException { 52 return bindPos; 54 } 55 56 public int bindValueExpr(PreparedStatement stmt, int bindPos, QValueType valueType, EvaluationContext evaluationContext) throws SQLException , QueryException { 57 if (evaluationContext.getContextDocument() == null) 58 throw new QueryException(NAME + " function is used but there is no context document available."); 59 60 valueType = getParam(0).getValueType() != null ? getParam(0).getValueType() : valueType; 61 Object value = evaluate(valueType, evaluationContext.getContextDocument(), evaluationContext.getContextVersion(), evaluationContext); 62 Literal.bindLiteral(stmt, bindPos, valueType, value); 63 return ++bindPos; 64 } 65 66 public boolean isMultiValue() { 67 return getParam(0).isMultiValue(); 68 } 69 70 public Object evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException { 71 return getOutputValue(null, null, evaluationContext); 72 } 73 74 public Object evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 75 return getOutputValue(document, version, evaluationContext); 76 } 77 78 public QValueType getValueType() { 79 return getParam(0).getOutputValueType(); 80 } 81 82 public String getTitle(Locale locale) { 83 return getExpression(); 84 } 85 86 public QValueType getOutputValueType() { 87 return getParam(0).getOutputValueType(); 88 } 89 90 public Object getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 91 if (evaluationContext.getContextDocument() == null) 92 throw new QueryException(NAME + " function is used but there is no context document available."); 93 return getParam(0).getOutputValue(evaluationContext.getContextDocument(), evaluationContext.getContextVersion(), evaluationContext); 94 } 95 } 96 | Popular Tags |