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
022package org.biojava.bio.seq.db.biosql;
023
024import java.sql.Connection;
025import java.sql.DatabaseMetaData;
026import java.sql.ResultSet;
027import java.sql.SQLException;
028import java.sql.Statement;
029
030/**
031 * This is a <code>DBHelper</code> that provides support for MySQL
032 * databases.
033 *
034 * @author Thomas Down
035 * @author Matthew Pocock
036 * @author Len Trigg
037 * @deprecated Use hibernate and org.biojavax.bio.db.*
038 */
039public class MySQLDBHelper extends DBHelper {
040
041    private final DeleteStyle mDeleteStyle;
042
043    public MySQLDBHelper(Connection connection) {
044        DeleteStyle deleteStyle = DELETE_GENERIC;
045        try {
046            DatabaseMetaData metadata = connection.getMetaData();
047            int major = metadata.getDatabaseMajorVersion();
048                                                // Minor version irrelevant as returns 0 for 4.0.*
049                                                // Actually need subminor version which not in metadata
050                                                // Could parse from getDatabaseProductVersion() string if really needed
051            int minor = metadata.getDatabaseMinorVersion();
052            if ((major > 4) || ((major == 4) && (minor >= 0))) {
053                deleteStyle = DELETE_MYSQL4;
054            }
055        } catch (SQLException e) {
056            System.err.println("Exception getting DatabaseMetaData:" +  e.getMessage());
057            // Stick with generic style
058        }
059        mDeleteStyle = deleteStyle;
060    }
061
062    // Inherit docs
063    public int getInsertID(Connection conn,
064                           String table,
065                           String columnName)
066        throws SQLException
067    {
068        Statement st = conn.createStatement();
069        ResultSet rs = st.executeQuery("select last_insert_id()");
070        int id = -1;
071        if (rs.next()) {
072            id = rs.getInt(1);
073        }
074        rs.close();
075        st.close();
076        
077        if (id < 1) {
078            throw new SQLException("Couldn't get last_insert_id()");
079        }
080        return id;
081    }
082
083    // Inherit docs
084    public DeleteStyle getDeleteStyle() {
085        return mDeleteStyle;
086    }
087}