001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022/**
023 * @author Primary aauthor unknown
024 * @author Greg Cox
025 * @deprecated Use hibernate and org.biojavax.bio.db.*
026 */
027
028package org.biojava.bio.seq.db.biosql;
029
030import java.sql.Connection;
031import java.sql.ResultSet;
032import java.sql.SQLException;
033import java.sql.Statement;
034
035public class PostgreSQLDBHelper extends DBHelper {
036    public int getInsertID(Connection conn,
037                           String table,
038                           String columnName)
039        throws SQLException
040    {
041        StringBuffer sequenceName = new StringBuffer();
042
043        /* Old style
044
045        int totalLength = table.length() + columnName.length();
046
047        if (totalLength > 26 && table.length() > 13) {
048            sequenceName.append(table.substring(0, 13));
049        } else {
050            sequenceName.append(table);
051        }
052        sequenceName.append('_');
053        if (totalLength > 26 && columnName.length() > 13) {
054            sequenceName.append(columnName.substring(0, 13));
055        } else {
056            sequenceName.append(columnName);
057        }
058        sequenceName.append("_seq");
059
060        */
061
062        sequenceName.append(table);
063        sequenceName.append("_pk_seq");
064
065        Statement st = conn.createStatement();
066        ResultSet rs = st.executeQuery("select currval('" + sequenceName.substring(0) + "')");
067        int id = -1;
068        if (rs.next()) {
069            id = rs.getInt(1);
070        }
071        st.close();
072
073        if (id < 1) {
074            throw new SQLException("Couldn't get last_insert_id()");
075        }
076        return id;
077    }
078
079    
080    public DeleteStyle getDeleteStyle() {
081        return DELETE_POSTGRESQL;
082    }
083}