/projects/jboss-5.1.0/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/keygen/JDBCPkSqlCreateCommand.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 110 lines · 71 code · 10 blank · 29 comment · 5 complexity · 9a4049851ab0aca50d5622f7902ee6e4 MD5 · raw file

  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.ejb.plugins.cmp.jdbc.keygen;
  23. import java.sql.Connection;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.sql.Statement;
  27. import javax.ejb.CreateException;
  28. import javax.sql.DataSource;
  29. import org.jboss.deployment.DeploymentException;
  30. import org.jboss.ejb.EntityEnterpriseContext;
  31. import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
  32. import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
  33. import org.jboss.ejb.plugins.cmp.jdbc.JDBCInsertPKCreateCommand;
  34. import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
  35. import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
  36. /**
  37. * Create command that uses an SQL statement to generate the primary key.
  38. * Typically used with databases that support sequences.
  39. *
  40. * @author <a href="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
  41. *
  42. * @version $Revision: 81030 $
  43. */
  44. public class JDBCPkSqlCreateCommand extends JDBCInsertPKCreateCommand
  45. {
  46. protected String pkSQL;
  47. protected JDBCCMPFieldBridge pkField;
  48. public void init(JDBCStoreManager manager) throws DeploymentException
  49. {
  50. super.init(manager);
  51. pkField = getGeneratedPKField();
  52. }
  53. protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException
  54. {
  55. super.initEntityCommand(entityCommand);
  56. pkSQL = entityCommand.getAttribute("pk-sql");
  57. if(pkSQL == null)
  58. {
  59. throw new DeploymentException("pk-sql attribute must be set for entity " + entity.getEntityName());
  60. }
  61. if(debug)
  62. {
  63. log.debug("Generate PK sql is: " + pkSQL);
  64. }
  65. }
  66. protected void generateFields(EntityEnterpriseContext ctx) throws CreateException
  67. {
  68. super.generateFields(ctx);
  69. Connection con = null;
  70. Statement s = null;
  71. ResultSet rs = null;
  72. try
  73. {
  74. if(debug)
  75. {
  76. log.debug("Executing SQL: " + pkSQL);
  77. }
  78. DataSource dataSource = entity.getDataSource();
  79. con = dataSource.getConnection();
  80. s = con.createStatement();
  81. rs = s.executeQuery(pkSQL);
  82. if(!rs.next())
  83. {
  84. throw new CreateException("Error fetching next primary key value: result set contains no rows");
  85. }
  86. pkField.loadInstanceResults(rs, 1, ctx);
  87. }
  88. catch(SQLException e)
  89. {
  90. log.error("Error fetching the next primary key value", e);
  91. throw new CreateException("Error fetching the next primary key value:" + e);
  92. }
  93. finally
  94. {
  95. JDBCUtil.safeClose(rs);
  96. JDBCUtil.safeClose(s);
  97. JDBCUtil.safeClose(con);
  98. }
  99. }
  100. }