KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCDeclaredSQLQuery


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

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 /**
35  * This class generates a query based on the delcared-sql xml specification.
36  *
37  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
38  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
39  * @author <a HREF="mailto:marc.fleury@telkel.com">Marc Fleury</a>
40  * @author <a HREF="mailto:shevlandj@kpi.com.au">Joe Shevland</a>
41  * @author <a HREF="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
42  * @author <a HREF="mailto:michel.anke@wolmail.nl">Michel de Groot</a>
43  * @author <a HREF="danch@nvisia.com">danch (Dan Christopherson</a>
44  * @author <a HREF="alex@jboss.org">Alex Loubyansky</a>
45  * @version $Revision: 37459 $
46  */

47 public final class JDBCDeclaredSQLQuery extends JDBCAbstractQueryCommand
48 {
49    private final JDBCDeclaredQueryMetaData metadata;
50
51    /**
52     * Creted a defined finder command based on the information
53     * in a declared-sql declaration.
54     */

55    public JDBCDeclaredSQLQuery(JDBCStoreManager manager,
56                                JDBCQueryMetaData q)
57       throws DeploymentException
58    {
59       super(manager, q);
60
61       metadata = (JDBCDeclaredQueryMetaData)q;
62
63       // set the select object (either selectEntity or selectField)
64
initSelectObject(manager);
65
66       // set the preload fields
67
JDBCReadAheadMetaData readAhead = metadata.getReadAhead();
68       JDBCEntityBridge selectEntity = getSelectEntity();
69       if(selectEntity != null && readAhead.isOnFind())
70       {
71          setEagerLoadGroup(readAhead.getEagerLoadGroup());
72       }
73
74       // set the sql and parameters
75
String JavaDoc sql = buildSQL();
76       setSQL(parseParameters(sql));
77    }
78
79    /**
80     * Initializes the entity or field to be selected.
81     * @throws DeploymentException if the specified object is invalid or
82     * non-existant
83     */

84    private void initSelectObject(JDBCStoreManager manager)
85       throws DeploymentException
86    {
87       String JavaDoc entityName = metadata.getEJBName();
88
89       // if no name is specified we are done
90
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 JavaDoc 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    /**
120     * Builds the sql statement based on the delcared-sql metadata specification.
121     * @return the sql statement for this query
122     */

123    private String JavaDoc buildSQL()
124    {
125       StringBuffer JavaDoc sql = new StringBuffer JavaDoc(300);
126
127       sql.append(SQLUtil.SELECT);
128       if(metadata.isSelectDistinct())
129       {
130          sql.append(SQLUtil.DISTINCT);
131       }
132
133       String JavaDoc alias = metadata.getAlias();
134       String JavaDoc from = metadata.getFrom();
135       String JavaDoc table;
136       String JavaDoc selectList;
137       if(getSelectField() == null)
138       {
139          // we are selecting a full entity
140
table = getSelectEntity().getQualifiedTableName();
141
142          // get a list of all fields to be loaded
143
// put pk fields in front
144
String JavaDoc tableAlias = getTableAlias(alias, from, getSelectEntity().getTableName());
145          selectList = SQLUtil.getColumnNamesClause(
146             getSelectEntity().getPrimaryKeyFields(),
147             tableAlias,
148             new StringBuffer JavaDoc(35)
149          ).toString();
150
151          if(getEagerLoadGroup() != null)
152          {
153             selectList += SQLUtil.appendColumnNamesClause(
154             getSelectEntity(),
155             getEagerLoadGroup(),
156             tableAlias,
157             new StringBuffer JavaDoc(35));
158          }
159       }
160       else
161       {
162          // we are just selecting one field
163
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 JavaDoc()).toString();
168       }
169       sql.append(selectList);
170       String JavaDoc 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 JavaDoc where = metadata.getWhere();
186       if(where != null && where.trim().length() > 0)
187       {
188          sql.append(SQLUtil.WHERE).append(where);
189       }
190
191       String JavaDoc order = metadata.getOrder();
192       if(order != null && order.trim().length() > 0)
193       {
194          sql.append(SQLUtil.ORDERBY).append(order);
195       }
196
197       String JavaDoc 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 JavaDoc getTableAlias(String JavaDoc alias, String JavaDoc from, String JavaDoc table)
206    {
207       String JavaDoc 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