PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/cmp/src/main/java/org/jboss/as/cmp/jdbc2/DeclaredSQLQueryCommand.java

#
Java | 203 lines | 134 code | 25 blank | 44 comment | 43 complexity | a382c54799f8b5614c817ed2c165553c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a 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.as.cmp.jdbc2;
  23. import static org.jboss.as.cmp.CmpMessages.MESSAGES;
  24. import org.jboss.as.cmp.jdbc2.bridge.JDBCEntityBridge2;
  25. import org.jboss.as.cmp.jdbc2.bridge.JDBCCMPFieldBridge2;
  26. import org.jboss.as.cmp.jdbc.metadata.JDBCDeclaredQueryMetaData;
  27. import org.jboss.as.cmp.jdbc.SQLUtil;
  28. import org.jboss.as.cmp.jdbc.QueryParameter;
  29. import org.jboss.as.cmp.ejbql.Catalog;
  30. import org.jboss.logging.Logger;
  31. import java.util.ArrayList;
  32. import java.util.StringTokenizer;
  33. /**
  34. * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
  35. * @version <tt>$Revision: 81030 $</tt>
  36. */
  37. public class DeclaredSQLQueryCommand extends AbstractQueryCommand {
  38. private JDBCCMPFieldBridge2 selectedField;
  39. public DeclaredSQLQueryCommand(JDBCEntityBridge2 entity, JDBCDeclaredQueryMetaData metadata) {
  40. initResultReader(entity, metadata);
  41. this.sql = buildSQL(metadata);
  42. this.sql = parseParameters(this.sql, metadata);
  43. setResultType(metadata.getMethod().getReturnType());
  44. log =
  45. Logger.getLogger(getClass().getName() + "." + entity.getEntityName() + "#" + metadata.getMethod().getName());
  46. log.debug("sql: " + sql);
  47. }
  48. private void initResultReader(JDBCEntityBridge2 entity, JDBCDeclaredQueryMetaData metadata) {
  49. String entityName = metadata.getEJBName();
  50. if (entityName != null) {
  51. Catalog catalog = entity.getManager().getCatalog();
  52. JDBCEntityBridge2 otherEntity = (JDBCEntityBridge2) catalog.getEntityByEJBName(entityName);
  53. if (otherEntity == null) {
  54. throw MESSAGES.unknownEntity(entityName);
  55. }
  56. this.entity = otherEntity;
  57. } else {
  58. this.entity = entity;
  59. }
  60. String fieldName = metadata.getFieldName();
  61. if (fieldName == null) {
  62. setEntityReader(this.entity, metadata.isSelectDistinct());
  63. } else {
  64. selectedField = (JDBCCMPFieldBridge2) entity.getFieldByName(fieldName);
  65. if (selectedField == null) {
  66. throw MESSAGES.unknownCmpField(fieldName);
  67. }
  68. setFieldReader(selectedField);
  69. }
  70. }
  71. private String buildSQL(JDBCDeclaredQueryMetaData metadata) {
  72. StringBuffer sql = new StringBuffer(300);
  73. sql.append(SQLUtil.SELECT);
  74. if (metadata.isSelectDistinct()) {
  75. sql.append(SQLUtil.DISTINCT);
  76. }
  77. String alias = metadata.getAlias();
  78. String from = metadata.getFrom();
  79. String table;
  80. String selectList;
  81. if (metadata.getFieldName() == null) {
  82. // we are selecting a full entity
  83. table = this.entity.getQualifiedTableName();
  84. // get a list of all fields to be loaded
  85. // put pk fields in front
  86. String tableAlias = getTableAlias(alias, from, this.entity.getTableName());
  87. selectList = SQLUtil.getColumnNamesClause(this.entity.getPrimaryKeyFields(),
  88. tableAlias,
  89. new StringBuffer(35)).toString();
  90. } else {
  91. // we are just selecting one field
  92. JDBCStoreManager2 manager = (JDBCStoreManager2) selectedField.getManager();
  93. table = manager.getEntityBridge().getQualifiedTableName();
  94. selectList = SQLUtil.getColumnNamesClause(selectedField,
  95. getTableAlias(alias, from, manager.getEntityBridge().getTableName()),
  96. new StringBuffer()).toString();
  97. }
  98. sql.append(selectList);
  99. String additionalColumns = metadata.getAdditionalColumns();
  100. if (additionalColumns != null) {
  101. sql.append(additionalColumns);
  102. }
  103. sql.append(SQLUtil.FROM).append(table);
  104. if (alias != null) {
  105. sql.append(' ').append(alias);
  106. }
  107. if (from != null) {
  108. sql.append(' ').append(from);
  109. }
  110. String where = metadata.getWhere();
  111. if (where != null && where.trim().length() > 0) {
  112. sql.append(SQLUtil.WHERE).append(where);
  113. }
  114. String order = metadata.getOrder();
  115. if (order != null && order.trim().length() > 0) {
  116. sql.append(SQLUtil.ORDERBY).append(order);
  117. }
  118. String other = metadata.getOther();
  119. if (other != null && other.trim().length() > 0) {
  120. sql.append(' ').append(other);
  121. }
  122. return sql.toString();
  123. }
  124. private static String getTableAlias(String alias, String from, String table) {
  125. String tableAlias;
  126. if (alias != null) {
  127. tableAlias = alias;
  128. } else if (from != null) {
  129. tableAlias = table;
  130. } else {
  131. tableAlias = SQLUtil.EMPTY_STRING;
  132. }
  133. return tableAlias;
  134. }
  135. /**
  136. * Replaces the parameters in the specific sql with question marks, and
  137. * initializes the parameter setting code. Parameters are encoded in curly
  138. * brackets use a zero based index.
  139. *
  140. * @param sql the sql statement that is parsed for parameters
  141. * @return the original sql statement with the parameters replaced with a
  142. * question mark
  143. */
  144. protected String parseParameters(String sql, JDBCDeclaredQueryMetaData metadata) {
  145. StringBuffer sqlBuf = new StringBuffer();
  146. ArrayList params = new ArrayList();
  147. // Replace placeholders {0} with ?
  148. if (sql != null) {
  149. sql = sql.trim();
  150. StringTokenizer tokens = new StringTokenizer(sql, "{}", true);
  151. while (tokens.hasMoreTokens()) {
  152. String token = tokens.nextToken();
  153. if (token.equals("{")) {
  154. token = tokens.nextToken();
  155. if (Character.isDigit(token.charAt(0))) {
  156. QueryParameter parameter = new QueryParameter(entity.getManager(), metadata.getMethod(), token);
  157. // of if we are here we can assume that we have
  158. // a parameter and not a function
  159. sqlBuf.append("?");
  160. params.add(parameter);
  161. if (!tokens.nextToken().equals("}")) {
  162. throw MESSAGES.missingClosingCurlyBrace(sql);
  163. }
  164. } else {
  165. // ok we don't have a parameter, we have a function
  166. // push the tokens on the buffer and continue
  167. sqlBuf.append("{").append(token);
  168. }
  169. } else {
  170. // not parameter... just append it
  171. sqlBuf.append(token);
  172. }
  173. }
  174. }
  175. setParameters(params);
  176. return sqlBuf.toString();
  177. }
  178. }