1 19 package org.apache.cayenne.query; 20 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.ejbql.EJBQLCompiledExpression; 27 import org.apache.cayenne.ejbql.EJBQLException; 28 import org.apache.cayenne.ejbql.EJBQLParserFactory; 29 import org.apache.cayenne.map.DataMap; 30 import org.apache.cayenne.map.EntityResolver; 31 32 38 public class EJBQLQuery implements Query { 39 40 protected String name; 41 protected String ejbqlStatement; 42 protected Map parameters; 43 44 protected transient EJBQLCompiledExpression expression; 45 EJBQLQueryMetadata metadata = new EJBQLQueryMetadata(); 46 47 public EJBQLQuery(String ejbqlStatement) { 48 this.ejbqlStatement = ejbqlStatement; 49 } 50 51 public QueryMetadata getMetaData(EntityResolver resolver) { 52 metadata.resolve(resolver, this); 53 return metadata; 54 } 55 56 public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) { 57 DataMap map = getMetaData(resolver).getDataMap(); 58 59 if (map == null) { 60 throw new CayenneRuntimeException("No DataMap found, can't route query " 61 + this); 62 } 63 64 router.route(router.engineForDataMap(map), this, substitutedQuery); 65 } 66 67 public SQLAction createSQLAction(SQLActionVisitor visitor) { 68 return visitor.ejbqlAction(this); 69 } 70 71 74 public String getEjbqlStatement() { 75 return ejbqlStatement; 76 } 77 78 81 public EJBQLCompiledExpression getExpression(EntityResolver resolver) 82 throws EJBQLException { 83 if (expression == null) { 84 this.expression = EJBQLParserFactory.getParser().compile( 85 ejbqlStatement, 86 resolver); 87 } 88 89 return expression; 90 } 91 92 public String getName() { 93 return name; 94 } 95 96 public void setName(String name) { 97 this.name = name; 98 } 99 100 104 public Map getParameters() { 105 return parameters != null 106 ? Collections.unmodifiableMap(parameters) 107 : Collections.EMPTY_MAP; 108 } 109 110 113 public void setParameter(String name, Object object) { 114 115 if (name == null || name.length() < 1) { 117 throw new IllegalArgumentException ("Null or empty parameter name"); 118 } 119 120 123 if (parameters == null) { 124 parameters = new HashMap (); 125 } 126 127 parameters.put(name, object); 128 } 129 130 134 public void setParameter(int position, Object object) { 135 136 if (position < 1) { 137 throw new IllegalArgumentException ("Parameter position must be >= 1: " 138 + position); 139 } 140 141 144 if (parameters == null) { 145 parameters = new HashMap (); 146 } 147 148 parameters.put(new Integer (position), object); 149 } 150 } 151 | Popular Tags |