001/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
002/*
003 *                    BioJava development code
004 *
005 * This code may be freely distributed and modified under the
006 * terms of the GNU Lesser General Public Licence.  This should
007 * be distributed with the code.  If you do not have a copy,
008 * see:
009 *
010 *      http://www.gnu.org/copyleft/lesser.html
011 *
012 * Copyright for this code is held jointly by the individual
013 * authors.  These should be listed in @author doc comments.
014 *
015 * For more information on the BioJava project and its aims,
016 * or to join the biojava-l mailing list, visit the home page
017 * at:
018 *
019 *      http://www.biojava.org/
020 *
021 */
022
023package org.biojava.bio.seq.db.biosql;
024
025import java.sql.Connection;
026import java.sql.PreparedStatement;
027import java.sql.ResultSet;
028import java.sql.SQLException;
029import java.sql.Statement;
030
031import javax.sql.DataSource;
032
033import org.biojava.bio.BioRuntimeException;
034
035/**
036 * This is a <code>DBHelper</code> that provides support for the
037 * Hypersonic RDBMS. See the <a href="http://hsqldb.sourceforge.net/">HSQLDB home page</a>
038 *
039 * @author Len Trigg
040 * @author Richard Holland
041 * @deprecated Use hibernate and org.biojavax.bio.db.*
042 */
043public class HypersonicDBHelper extends DBHelper {
044
045    // Inherit docs
046    public int getInsertID(Connection conn, String table, String columnName) throws SQLException {
047        Statement st = null;
048        ResultSet rs = null;
049        try {
050            st = conn.createStatement();
051            rs = st.executeQuery("call identity()");
052            int id = -1;
053            if (rs.next()) {
054                id = rs.getInt(1);
055            }
056            if (id < 0) {
057                throw new SQLException("Couldn't get last insert id");
058            }
059            return id;
060        } finally {
061            if (rs != null) try { rs.close(); } catch (SQLException se) { }
062            if (st != null) try { st.close(); } catch (SQLException se) { }
063        }
064    }
065
066
067    // Inherit docs
068    public boolean containsTable(DataSource ds, String tablename) {
069        if (ds == null) {
070            throw new NullPointerException("Require a datasource.");
071        }
072        if ((tablename == null) || (tablename.length() == 0)) {
073            throw new IllegalArgumentException("Invalid table name given");
074        } 
075        //System.err.println("Checking for table existence: " + tablename);
076        Connection conn = null;
077        try {
078            boolean present;
079            conn = ds.getConnection();
080            PreparedStatement ps = null;
081            try {
082                ps = conn.prepareStatement("select top 1 * from " + tablename);
083                ps.executeQuery();
084                present = true;
085            } catch (SQLException ex) {
086                //System.err.println("Table " + tablename + " does not exist.");
087                present = false;
088            } finally {
089                if (ps != null) {
090                    ps.close();
091                }
092                if (conn != null) {
093                    conn.close();
094                }
095            }
096            return present;
097        } catch (SQLException ex) {
098            if (conn!=null) try {conn.close();} catch (SQLException ex3) {}
099            throw new BioRuntimeException(ex);
100        }
101    }
102
103}