1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 25 import org.jboss.deployment.DeploymentException; 26 27 import org.jboss.ejb.plugins.cmp.ejbql.Catalog; 28 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge; 29 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge; 30 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData; 31 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDeclaredQueryMetaData; 32 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData; 33 34 47 public final class JDBCDeclaredSQLQuery extends JDBCAbstractQueryCommand 48 { 49 private final JDBCDeclaredQueryMetaData metadata; 50 51 55 public JDBCDeclaredSQLQuery(JDBCStoreManager manager, 56 JDBCQueryMetaData q) 57 throws DeploymentException 58 { 59 super(manager, q); 60 61 metadata = (JDBCDeclaredQueryMetaData)q; 62 63 initSelectObject(manager); 65 66 JDBCReadAheadMetaData readAhead = metadata.getReadAhead(); 68 JDBCEntityBridge selectEntity = getSelectEntity(); 69 if(selectEntity != null && readAhead.isOnFind()) 70 { 71 setEagerLoadGroup(readAhead.getEagerLoadGroup()); 72 } 73 74 String sql = buildSQL(); 76 setSQL(parseParameters(sql)); 77 } 78 79 84 private void initSelectObject(JDBCStoreManager manager) 85 throws DeploymentException 86 { 87 String entityName = metadata.getEJBName(); 88 89 if(entityName == null) 91 { 92 return; 93 } 94 95 Catalog catalog = manager.getCatalog(); 96 97 JDBCEntityBridge entity = (JDBCEntityBridge)catalog.getEntityByEJBName(entityName); 98 if(entity == null) 99 { 100 throw new DeploymentException("Unknown entity: " + entityName); 101 } 102 103 String fieldName = metadata.getFieldName(); 104 if(fieldName == null) 105 { 106 setSelectEntity(entity); 107 } 108 else 109 { 110 JDBCCMPFieldBridge field = entity.getCMPFieldByName(fieldName); 111 if(field == null) 112 { 113 throw new DeploymentException("Unknown cmp field: " + fieldName); 114 } 115 setSelectField(field); 116 } 117 } 118 119 123 private String buildSQL() 124 { 125 StringBuffer sql = new StringBuffer (300); 126 127 sql.append(SQLUtil.SELECT); 128 if(metadata.isSelectDistinct()) 129 { 130 sql.append(SQLUtil.DISTINCT); 131 } 132 133 String alias = metadata.getAlias(); 134 String from = metadata.getFrom(); 135 String table; 136 String selectList; 137 if(getSelectField() == null) 138 { 139 table = getSelectEntity().getQualifiedTableName(); 141 142 String tableAlias = getTableAlias(alias, from, getSelectEntity().getTableName()); 145 selectList = SQLUtil.getColumnNamesClause( 146 getSelectEntity().getPrimaryKeyFields(), 147 tableAlias, 148 new StringBuffer (35) 149 ).toString(); 150 151 if(getEagerLoadGroup() != null) 152 { 153 selectList += SQLUtil.appendColumnNamesClause( 154 getSelectEntity(), 155 getEagerLoadGroup(), 156 tableAlias, 157 new StringBuffer (35)); 158 } 159 } 160 else 161 { 162 JDBCCMPFieldBridge selectField = getSelectField(); 164 JDBCStoreManager manager = (JDBCStoreManager)getSelectField().getManager(); 165 table = manager.getEntityBridge().getQualifiedTableName(); 166 selectList = SQLUtil.getColumnNamesClause( 167 selectField, getTableAlias(alias, from, manager.getEntityBridge().getTableName()), new StringBuffer ()).toString(); 168 } 169 sql.append(selectList); 170 String additionalColumns = metadata.getAdditionalColumns(); 171 if(additionalColumns != null) 172 { 173 sql.append(additionalColumns); 174 } 175 sql.append(SQLUtil.FROM).append(table); 176 if(alias != null) 177 { 178 sql.append(' ').append(alias); 179 } 180 if(from != null) 181 { 182 sql.append(' ').append(from); 183 } 184 185 String where = metadata.getWhere(); 186 if(where != null && where.trim().length() > 0) 187 { 188 sql.append(SQLUtil.WHERE).append(where); 189 } 190 191 String order = metadata.getOrder(); 192 if(order != null && order.trim().length() > 0) 193 { 194 sql.append(SQLUtil.ORDERBY).append(order); 195 } 196 197 String other = metadata.getOther(); 198 if(other != null && other.trim().length() > 0) 199 { 200 sql.append(' ').append(other); 201 } 202 return sql.toString(); 203 } 204 205 private static String getTableAlias(String alias, String from, String table) 206 { 207 String tableAlias; 208 if(alias != null) 209 { 210 tableAlias = alias; 211 } 212 else if(from != null) 213 { 214 tableAlias = table; 215 } 216 else 217 { 218 tableAlias = SQLUtil.EMPTY_STRING; 219 } 220 return tableAlias; 221 } 222 } 223 | Popular Tags |