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.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029: import javax.ejb.EJBException;
030:
031: import org.jboss.deployment.DeploymentException;
032: import org.jboss.ejb.EntityEnterpriseContext;
033: import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand;
034: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
035: import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
036: import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil;
037: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
038:
039: /**
040: * Create command for PostgreSQL that fetches the currval of the sequence
041: * associated with a SERIAL column in this table.
042: *
043: * @author <a href="mailto:jeremy@boynes.com">Jeremy Boynes</a>
044: * @version $Revision: 57209 $
045: */
046: public class JDBCPostgreSQLCreateCommand extends
047: JDBCIdentityColumnCreateCommand {
048: private String sequence;
049: private String sequenceSQL;
050:
051: public void init(JDBCStoreManager manager)
052: throws DeploymentException {
053: super .init(manager);
054: }
055:
056: protected void initEntityCommand(
057: JDBCEntityCommandMetaData entityCommand)
058: throws DeploymentException {
059: super .initEntityCommand(entityCommand);
060: sequence = entityCommand.getAttribute("sequence");
061: if (sequence == null) {
062: sequence = entity.getQualifiedTableName()
063: + '_'
064: + SQLUtil.getColumnNamesClause(pkField,
065: new StringBuffer(20)) + "_seq";
066: }
067: sequenceSQL = "SELECT currval('" + sequence + "')";
068: if (debug) {
069: log.debug("SEQUENCE SQL is :" + sequenceSQL);
070: }
071: }
072:
073: protected int executeInsert(int index, PreparedStatement ps,
074: EntityEnterpriseContext ctx) throws SQLException {
075: int rows = ps.executeUpdate();
076:
077: Statement s = null;
078: ResultSet rs = null;
079: try {
080: if (trace) {
081: log.trace("Executing SQL :" + sequenceSQL);
082: }
083: Connection c = ps.getConnection();
084: s = c.createStatement();
085: rs = s.executeQuery(sequenceSQL);
086: if (!rs.next()) {
087: throw new EJBException(
088: "sequence sql returned an empty ResultSet");
089: }
090: pkField.loadInstanceResults(rs, 1, ctx);
091: } catch (RuntimeException e) {
092: throw e;
093: } catch (Exception e) {
094: // throw EJBException to force a rollback as the row has been inserted
095: throw new EJBException("Error extracting generated keys", e);
096: } finally {
097: JDBCUtil.safeClose(rs);
098: JDBCUtil.safeClose(s);
099: }
100:
101: return rows;
102: }
103: }
|