1 22 package org.jboss.ejb.plugins.cmp.jdbc2; 23 24 import java.util.Collection ; 25 import java.util.List ; 26 import java.util.Set ; 27 import javax.ejb.FinderException ; 28 import org.jboss.ejb.GenericEntityObjectFactory; 29 import org.jboss.ejb.plugins.cmp.jdbc.EJBQLToSQL92Compiler; 30 import org.jboss.ejb.plugins.cmp.jdbc.QLCompiler; 31 import org.jboss.ejb.plugins.cmp.jdbc.QueryParameter; 32 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDynamicQLQueryMetaData; 33 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMPFieldBridge2; 34 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2; 35 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema; 36 import org.jboss.logging.Logger; 37 38 42 public class DynamicQueryCommand 43 implements QueryCommand 44 { 45 private Logger log; 46 private JDBCEntityBridge2 entity; 47 private JDBCDynamicQLQueryMetaData metadata; 48 private AbstractQueryCommand.CollectionFactory collectionFactory; 49 50 public DynamicQueryCommand(JDBCEntityBridge2 entity, JDBCDynamicQLQueryMetaData metadata) 51 { 52 log = 53 Logger.getLogger(getClass().getName() + "." + entity.getEntityName() + "#" + metadata.getMethod().getName()); 54 this.entity = entity; 55 this.metadata = metadata; 56 57 Class returnType = metadata.getMethod().getReturnType(); 58 if(Collection .class.isAssignableFrom(returnType)) 59 { 60 if(Set .class.isAssignableFrom(returnType)) 61 { 62 collectionFactory = AbstractQueryCommand.SET_FACTORY; 63 } 64 else 65 { 66 collectionFactory = AbstractQueryCommand.COLLECTION_FACTORY; 67 } 68 } 69 } 70 71 public JDBCStoreManager2 getStoreManager() 72 { 73 return (JDBCStoreManager2)entity.getManager(); 74 } 75 76 public Collection fetchCollection(Schema schema, GenericEntityObjectFactory factory, Object [] args) 77 throws FinderException 78 { 79 if(log.isTraceEnabled()) 80 { 81 log.trace("executing dynamic-ql: " + args[0]); 82 } 83 84 JDBCStoreManager2 manager = (JDBCStoreManager2)entity.getManager(); 85 QLCompiler compiler = new EJBQLToSQL92Compiler(manager.getCatalog()); 86 try 87 { 88 compiler.compileJBossQL((String )args[0], 89 metadata.getMethod().getReturnType(), 90 getParamTypes(args), 91 metadata 92 ); 93 } 94 catch(Throwable t) 95 { 96 log.error("Error compiling JBossQL statement '" + args[0] + "'", t); 97 throw new FinderException ("Error compiling JBossQL statement '" + args[0] + "'"); 98 } 99 100 String sql = compiler.getSQL(); 101 102 AbstractQueryCommand.ResultReader resultReader; 103 if(!compiler.isSelectEntity()) 104 { 105 if(compiler.isSelectField()) 106 { 107 resultReader = new AbstractQueryCommand.FieldReader((JDBCCMPFieldBridge2)compiler.getSelectField()); 108 } 109 else 110 { 111 resultReader = new AbstractQueryCommand.FunctionReader(compiler.getSelectFunction()); 112 } 113 } 114 else 115 { 116 resultReader = new AbstractQueryCommand.EntityReader((JDBCEntityBridge2)compiler.getSelectEntity(), compiler.isSelectDistinct()); 117 } 118 119 return AbstractQueryCommand.fetchCollection( 120 entity, sql, toArray(compiler.getInputParameters()), 121 new AbstractQueryCommand.EagerCollectionStrategy(collectionFactory, resultReader, log), 122 schema, factory, (Object [])args[1], log); 123 } 124 125 public Object fetchOne(Schema schema, GenericEntityObjectFactory factory, Object [] args) throws FinderException 126 { 127 if(log.isTraceEnabled()) 128 { 129 log.trace("executing dynamic-ql: " + args[0]); 130 } 131 132 JDBCStoreManager2 manager = (JDBCStoreManager2)entity.getManager(); 133 QLCompiler compiler = new EJBQLToSQL92Compiler(manager.getCatalog()); 134 try 135 { 136 compiler.compileJBossQL((String )args[0], 137 metadata.getMethod().getReturnType(), 138 getParamTypes(args), 139 metadata 140 ); 141 } 142 catch(Throwable t) 143 { 144 log.error("Error compiling JBossQL statement '" + args[0] + "'", t); 145 throw new FinderException ("Error compiling JBossQL statement '" + args[0] + "'"); 146 } 147 148 String sql = compiler.getSQL(); 149 150 AbstractQueryCommand.ResultReader resultReader; 151 if(!compiler.isSelectEntity()) 152 { 153 if(compiler.isSelectField()) 154 { 155 resultReader = new AbstractQueryCommand.FieldReader((JDBCCMPFieldBridge2)compiler.getSelectField()); 156 } 157 else 158 { 159 resultReader = new AbstractQueryCommand.FunctionReader(compiler.getSelectFunction()); 160 } 161 } 162 else 163 { 164 resultReader = new AbstractQueryCommand.EntityReader((JDBCEntityBridge2)compiler.getSelectEntity(), compiler.isSelectDistinct()); 165 } 166 167 return AbstractQueryCommand.fetchOne(entity, sql, toArray(compiler.getInputParameters()), 168 resultReader, (Object [])args[1], factory, log 169 ); 170 } 171 172 private static Class [] getParamTypes(Object [] args) 173 throws FinderException 174 { 175 Class [] parameterTypes; 176 Object [] parameters = (Object [])args[1]; 178 if(parameters == null) 179 { 180 parameterTypes = new Class [0]; 181 } 182 else 183 { 184 parameterTypes = new Class [parameters.length]; 186 for(int i = 0; i < parameters.length; i++) 187 { 188 if(parameters[i] == null) 189 { 190 throw new FinderException ("Parameter[" + i + "] is null"); 191 } 192 parameterTypes[i] = parameters[i].getClass(); 193 } 194 } 195 return parameterTypes; 196 } 197 198 static QueryParameter[] toArray(List p) 199 { 200 QueryParameter[] params = null; 201 if(p.size() > 0) 202 { 203 params = (QueryParameter[])p.toArray(new QueryParameter[p.size()]); 204 } 205 return params; 206 } 207 } 208 | Popular Tags |