1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.lang.reflect.Method ; 25 import java.util.Collection ; 26 import java.util.List ; 27 import java.util.Collections ; 28 import javax.ejb.FinderException ; 29 30 import org.jboss.deployment.DeploymentException; 31 import org.jboss.ejb.EntityEnterpriseContext; 32 import org.jboss.ejb.GenericEntityObjectFactory; 33 import org.jboss.ejb.EntityContainer; 34 import org.jboss.ejb.plugins.cmp.ejbql.Catalog; 35 import org.jboss.ejb.plugins.cmp.ejbql.SelectFunction; 36 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge; 37 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge; 38 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge; 39 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData; 40 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDynamicQLQueryMetaData; 41 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData; 42 43 50 public final class JDBCDynamicQLQuery extends JDBCAbstractQueryCommand 51 { 52 private final Catalog catalog; 53 private final JDBCDynamicQLQueryMetaData metadata; 54 55 public JDBCDynamicQLQuery(JDBCStoreManager manager, JDBCQueryMetaData q) 56 throws DeploymentException 57 { 58 super(manager, q); 59 catalog = manager.getCatalog(); 60 metadata = (JDBCDynamicQLQueryMetaData)q; 61 } 62 63 public Collection execute(Method finderMethod, Object [] args, EntityEnterpriseContext ctx, GenericEntityObjectFactory factory) 64 throws FinderException 65 { 66 String dynamicQL = (String )args[0]; 67 if(getLog().isDebugEnabled()) 68 { 69 getLog().debug("DYNAMIC-QL: " + dynamicQL); 70 } 71 72 QLCompiler compiler = null; 73 try 74 { 75 compiler = JDBCQueryManager.getInstance(metadata.getQLCompilerClass(), catalog); 76 } 77 catch(DeploymentException e) 78 { 79 throw new FinderException (e.getMessage()); 80 } 81 82 Object [] parameters = (Object [])args[1]; 84 Class [] parameterTypes; 86 if(parameters == null) 87 { 88 parameterTypes = new Class [0]; 89 } 90 else 91 { 92 parameterTypes = new Class [parameters.length]; 94 for(int i = 0; i < parameters.length; i++) 95 { 96 if(parameters[i] == null) 97 { 98 throw new FinderException ("Parameter[" + i + "] is null"); 99 } 100 parameterTypes[i] = parameters[i].getClass(); 101 } 102 } 103 104 try 106 { 107 compiler.compileJBossQL( 108 dynamicQL, 109 finderMethod.getReturnType(), 110 parameterTypes, 111 metadata); 112 } 113 catch(Throwable t) 114 { 115 t.printStackTrace(); 116 throw new FinderException ("Error compiling ejbql: " + t); 117 } 118 119 int offset = toInt(parameters, compiler.getOffsetParam(), compiler.getOffsetValue()); 120 int limit = toInt(parameters, compiler.getLimitParam(), compiler.getLimitValue()); 121 122 JDBCEntityBridge selectEntity = null; 123 JDBCCMPFieldBridge selectField = null; 124 SelectFunction selectFunction = null; 125 if(compiler.isSelectEntity()) 126 { 127 selectEntity = (JDBCEntityBridge) compiler.getSelectEntity(); 128 } 129 else if(compiler.isSelectField()) 130 { 131 selectField = (JDBCCMPFieldBridge) compiler.getSelectField(); 132 } 133 else 134 { 135 selectFunction = compiler.getSelectFunction(); 136 } 137 138 boolean[] mask; 139 List leftJoinCMRList; 140 JDBCReadAheadMetaData readahead = metadata.getReadAhead(); 141 if(selectEntity != null && readahead.isOnFind()) 142 { 143 mask = selectEntity.getLoadGroupMask(readahead.getEagerLoadGroup()); 144 leftJoinCMRList = compiler.getLeftJoinCMRList(); 145 146 if(compiler.isSelectDistinct()) 148 { 149 JDBCFieldBridge[] tableFields = selectEntity.getTableFields(); 150 for(int i = 0; i < tableFields.length; ++i) 151 { 152 if(mask[i] && !tableFields[i].getJDBCType().isSearchable()) 153 { 154 mask[i] = false; 155 } 156 } 157 } 158 } 159 else 160 { 161 mask = null; 162 leftJoinCMRList = Collections.EMPTY_LIST; 163 } 164 165 setParameterList(compiler.getInputParameters()); 167 168 EntityContainer con = ((JDBCStoreManager)compiler.getStoreManager()).getContainer(); 169 factory = metadata.isResultTypeMappingLocal() ? (GenericEntityObjectFactory)con.getLocalProxyFactory() : con.getProxyFactory(); 170 171 return execute( 172 compiler.getSQL(), 173 parameters, 174 offset, 175 limit, 176 selectEntity, 177 selectField, 178 selectFunction, 179 (JDBCStoreManager) compiler.getStoreManager(), 180 mask, 181 compiler.getInputParameters(), 182 leftJoinCMRList, 183 metadata, 184 factory, 185 log 186 ); 187 } 188 } 189 | Popular Tags |