001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2005, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.util;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import java.sql.Connection;
028: import java.sql.DatabaseMetaData;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.sql.Statement;
032:
033: /**
034: * Helper for SQL-related stuff.
035: * <br>
036: * TODO: testcases
037: * @author Felipe Leme <dbunit@felipeal.net>
038: * @version $Revision: 554 $
039: * @since Nov 5, 2005
040: *
041: */
042:
043: public class SQLHelper {
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger logger = LoggerFactory
049: .getLogger(SQLHelper.class);
050:
051: // class is "static"
052: private SQLHelper() {
053: }
054:
055: /**
056: * Gets the primary column for a table.
057: * @param conn connection with the database
058: * @param table table name
059: * @return name of primary column for a table (assuming it's just 1 column).
060: * @throws SQLException raised while getting the meta data
061: */
062: public static String getPrimaryKeyColumn(Connection conn,
063: String table) throws SQLException {
064: logger.debug("getPrimaryKeyColumn(conn=" + conn + ", table="
065: + table + ") - start");
066:
067: DatabaseMetaData metadata = conn.getMetaData();
068: ResultSet rs = metadata.getPrimaryKeys(null, null, table);
069: rs.next();
070: String pkColumn = rs.getString(4);
071: return pkColumn;
072: }
073:
074: /**
075: * Close a result set and a prepared statement, checking for null references.
076: * @param rs result set to be closed
077: * @param stmt prepared statement to be closed
078: * @throws SQLException exception raised in either close() method
079: */
080: public static void close(ResultSet rs, Statement stmt)
081: throws SQLException {
082: logger.debug("close(rs=" + rs + ", stmt=" + stmt + ") - start");
083:
084: try {
085: if (rs != null) {
086: rs.close();
087: }
088: } finally {
089: close(stmt);
090: }
091: }
092:
093: /**
094: * Close a preparement statement, checking for null references.
095: * @param rs result set to be closed
096: * @param stmt statement to be closed
097: * @throws SQLException exception raised while closing the statement
098: */
099: public static void close(Statement stmt) throws SQLException {
100: logger.debug("close(stmt=" + stmt + ") - start");
101:
102: if (stmt != null) {
103: stmt.close();
104: }
105: }
106:
107: }
|