1 56 package org.objectstyle.cayenne.query; 57 58 import java.util.Collections ; 59 import java.util.Map ; 60 61 import org.apache.commons.lang.builder.ToStringBuilder; 62 import org.objectstyle.cayenne.CayenneRuntimeException; 63 import org.objectstyle.cayenne.map.EntityResolver; 64 import org.objectstyle.cayenne.util.Util; 65 66 73 public class NamedQuery implements QueryExecutionPlan { 74 75 protected String name; 76 protected Map parameters; 77 78 public NamedQuery(String name) { 79 this(name, null); 80 } 81 82 public NamedQuery(String name, Map parameters) { 83 this.name = name; 84 this.parameters = parameters; 85 } 86 87 90 public NamedQuery(String name, String [] keys, Object [] values) { 91 this.name = name; 92 this.parameters = Util.toMap(keys, values); 93 } 94 95 public String getName() { 96 return name; 97 } 98 99 public void setName(String name) { 100 this.name = name; 101 } 102 103 109 public Query resolve(EntityResolver resolver) { 110 Query substituteQuery = substituteQuery(resolver); 111 112 if (substituteQuery == null) { 113 throw new CayenneRuntimeException("Can't find named query for name '" 114 + getName() 115 + "'"); 116 } 117 118 return substituteQuery; 119 } 120 121 124 public void route(QueryRouter router, EntityResolver resolver) { 125 throw new CayenneRuntimeException(this 126 + " doesn't support its own routing. " 127 + "It should've been delegated to another " 128 + "query during resolution phase."); 129 } 130 131 135 public SQLAction createSQLAction(SQLActionVisitor visitor) { 136 throw new CayenneRuntimeException(this 137 + " doesn't support its own sql actions. " 138 + "It should've been delegated to another " 139 + "query during resolution phase."); 140 } 141 142 145 protected Query substituteQuery(EntityResolver resolver) { 146 Query query = resolver.lookupQuery(getName()); 147 148 if(query instanceof ParameterizedQuery) { 149 Map parameters = (this.parameters != null) ? this.parameters : Collections.EMPTY_MAP; 151 query = ((ParameterizedQuery) query).createQuery(parameters); 152 } 153 154 return query; 155 } 156 157 public String toString() { 158 return new ToStringBuilder(this).append("name", name).toString(); 159 } 160 } 161 | Popular Tags |