001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc.keygen;
023:
024: import java.sql.Connection;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.sql.Statement;
028: import javax.ejb.CreateException;
029: import javax.sql.DataSource;
030:
031: import org.jboss.deployment.DeploymentException;
032: import org.jboss.ejb.EntityEnterpriseContext;
033: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
034: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
035: import org.jboss.ejb.plugins.cmp.jdbc.JDBCInsertPKCreateCommand;
036: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
037: import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
038:
039: /**
040: * Create command that uses an SQL statement to generate the primary key.
041: * Typically used with databases that support sequences.
042: *
043: * @author <a href="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
044: *
045: * @version $Revision: 57209 $
046: */
047: public class JDBCPkSqlCreateCommand extends JDBCInsertPKCreateCommand {
048: protected String pkSQL;
049: protected JDBCCMPFieldBridge pkField;
050:
051: public void init(JDBCStoreManager manager)
052: throws DeploymentException {
053: super .init(manager);
054: pkField = getGeneratedPKField();
055: }
056:
057: protected void initEntityCommand(
058: JDBCEntityCommandMetaData entityCommand)
059: throws DeploymentException {
060: super .initEntityCommand(entityCommand);
061:
062: pkSQL = entityCommand.getAttribute("pk-sql");
063: if (pkSQL == null) {
064: throw new DeploymentException(
065: "pk-sql attribute must be set for entity "
066: + entity.getEntityName());
067: }
068: if (debug) {
069: log.debug("Generate PK sql is: " + pkSQL);
070: }
071: }
072:
073: protected void generateFields(EntityEnterpriseContext ctx)
074: throws CreateException {
075: super .generateFields(ctx);
076:
077: Connection con = null;
078: Statement s = null;
079: ResultSet rs = null;
080: try {
081: if (debug) {
082: log.debug("Executing SQL: " + pkSQL);
083: }
084:
085: DataSource dataSource = entity.getDataSource();
086: con = dataSource.getConnection();
087: s = con.createStatement();
088:
089: rs = s.executeQuery(pkSQL);
090: if (!rs.next()) {
091: throw new CreateException(
092: "Error fetching next primary key value: result set contains no rows");
093: }
094: pkField.loadInstanceResults(rs, 1, ctx);
095: } catch (SQLException e) {
096: log.error("Error fetching the next primary key value", e);
097: throw new CreateException(
098: "Error fetching the next primary key value:" + e);
099: } finally {
100: JDBCUtil.safeClose(rs);
101: JDBCUtil.safeClose(s);
102: JDBCUtil.safeClose(con);
103: }
104: }
105: }
|