Coverage Report - net.mtu.eggplant.util.sql.SQLFunctions
 
Classes in this File Line Coverage Branch Coverage Complexity
SQLFunctions
0%
0/60
0%
0/33
8.143
 
 1  
 /*
 2  
  * Copyright (c) 2000
 3  
  *      Jon Schewe.  All rights reserved
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  * 1. Redistributions of source code must retain the above copyright
 9  
  *    notice, this list of conditions and the following disclaimer.
 10  
  * 2. Redistributions in binary form must reproduce the above copyright
 11  
  *    notice, this list of conditions and the following disclaimer in the
 12  
  *    documentation and/or other materials provided with the distribution.
 13  
  *
 14  
  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 15  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17  
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 18  
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19  
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20  
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22  
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23  
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24  
  * SUCH DAMAGE.
 25  
  *
 26  
  * I'd appreciate comments/suggestions on the code jpschewe@mtu.net
 27  
  */
 28  
 package net.mtu.eggplant.util.sql;
 29  
 
 30  
 import java.sql.Connection;
 31  
 import java.sql.DatabaseMetaData;
 32  
 import java.sql.PreparedStatement;
 33  
 import java.sql.ResultSet;
 34  
 import java.sql.SQLException;
 35  
 import java.sql.Statement;
 36  
 import java.sql.Types;
 37  
 import java.util.Collection;
 38  
 import java.util.LinkedList;
 39  
 
 40  
 import org.slf4j.Logger;
 41  
 import org.slf4j.LoggerFactory;
 42  
 
 43  
 /**
 44  
  * Handy functions for SQL
 45  
  * 
 46  
  * @version $Revision$
 47  
  */
 48  
 public final class SQLFunctions {
 49  
 
 50  0
   private static final Logger LOGGER = LoggerFactory.getLogger(SQLFunctions.class);
 51  
 
 52  0
   private SQLFunctions() {
 53  0
   }
 54  
 
 55  
   /**
 56  
    * Do simple mapping from an SQL type to a Java Class.
 57  
    */
 58  
   public static Class<?> getClassForType(final int type) {
 59  
     try {
 60  0
       switch (type) {
 61  
       case Types.CHAR:
 62  
       case Types.LONGVARCHAR:
 63  
       case Types.VARCHAR:
 64  0
         return Class.forName("java.lang.String");
 65  
       case Types.DECIMAL:
 66  
       case Types.NUMERIC:
 67  0
         return Class.forName("java.math.BigDecimal");
 68  
       case Types.BIT:
 69  0
         return Class.forName("java.lang.Boolean");
 70  
       case Types.SMALLINT:
 71  
       case Types.TINYINT:
 72  
       case Types.INTEGER:
 73  0
         return Class.forName("java.lang.Integer");
 74  
       case Types.BIGINT:
 75  0
         return Class.forName("java.lang.Long");
 76  
       case Types.REAL:
 77  0
         return Class.forName("java.lang.Float");
 78  
       case Types.DOUBLE:
 79  
       case Types.FLOAT:
 80  0
         return Class.forName("java.lang.Double");
 81  
       case Types.BINARY:
 82  
       case Types.LONGVARBINARY:
 83  
       case Types.VARBINARY:
 84  0
         return (new byte[0]).getClass();
 85  
       case Types.DATE:
 86  0
         return Class.forName("java.sql.Date");
 87  
       case Types.TIME:
 88  0
         return Class.forName("java.sql.Time");
 89  
       case Types.TIMESTAMP:
 90  0
         return Class.forName("java.sql.Timestamp");
 91  
       case Types.NULL:
 92  0
         return Class.forName("java.lang.Void");
 93  
       case Types.OTHER:
 94  
       default:
 95  0
         return Class.forName("java.lang.Object");
 96  
       }
 97  0
     } catch (final ClassNotFoundException cnfe) {
 98  0
       throw new RuntimeException("Can't find standard class type "
 99  
           + cnfe);
 100  
     }
 101  
   }
 102  
 
 103  
   /**
 104  
    * Close stmt and ignore SQLExceptions. This is useful in a finally so that
 105  
    * all of the finally block gets executed. Handles null.
 106  
    */
 107  
   public static void close(final Statement stmt) {
 108  
     try {
 109  0
       if (null != stmt) {
 110  0
         stmt.close();
 111  
       }
 112  0
     } catch (final SQLException sqle) {
 113  
       // ignore
 114  0
       if (LOGGER.isDebugEnabled()) {
 115  0
         LOGGER.debug("exception closing statement", sqle);
 116  
       }
 117  0
     }
 118  0
   }
 119  
 
 120  
   /**
 121  
    * Close prep and ignore SQLExceptions. This is useful in a finally so that
 122  
    * all of the finally block gets executed. Handles null.
 123  
    */
 124  
   public static void close(final PreparedStatement prep) {
 125  
     try {
 126  0
       if (null != prep) {
 127  0
         prep.close();
 128  
       }
 129  0
     } catch (final SQLException sqle) {
 130  
       // ignore
 131  0
       if (LOGGER.isDebugEnabled()) {
 132  0
         LOGGER.debug("exception closing prepared statement", sqle);
 133  
       }
 134  0
     }
 135  0
   }
 136  
 
 137  
   /**
 138  
    * Close rs and ignore SQLExceptions. This is useful in a finally so that all
 139  
    * of the finally block gets executed. Handles null.
 140  
    */
 141  
   public static void close(final ResultSet rs) {
 142  
     try {
 143  0
       if (null != rs) {
 144  0
         rs.close();
 145  
       }
 146  0
     } catch (final SQLException sqle) {
 147  
       // ignore
 148  0
       if (LOGGER.isDebugEnabled()) {
 149  0
         LOGGER.debug("exception closing result set", sqle);
 150  
       }
 151  0
     }
 152  0
   }
 153  
 
 154  
   /**
 155  
    * Close connection and ignore SQLExceptions. This is useful in a finally so
 156  
    * that all of the finally block gets executed. Handles null.
 157  
    */
 158  
   public static void close(final Connection connection) {
 159  
     try {
 160  0
       if (null != connection) {
 161  0
         connection.close();
 162  
       }
 163  0
     } catch (final SQLException sqle) {
 164  
       // ignore
 165  0
       if (LOGGER.isDebugEnabled()) {
 166  0
         LOGGER.debug("exception closing connection", sqle);
 167  
       }
 168  0
     }
 169  0
   }
 170  
 
 171  
   /**
 172  
    * Get the tables in the database.
 173  
    * 
 174  
    * @param connection the connection to the database
 175  
    * @return the names of the tables that are of type "TABLE", the names will be
 176  
    *         all lowercase
 177  
    * @see DatabaseMetaData#getTables(String, String, String, String[])
 178  
    */
 179  
   public static Collection<String> getTablesInDB(final Connection connection) throws SQLException {
 180  0
     final Collection<String> tables = new LinkedList<String>();
 181  0
     ResultSet rs = null;
 182  
     try {
 183  
       // get list of tables that already exist
 184  0
       final DatabaseMetaData metadata = connection.getMetaData();
 185  0
       rs = metadata.getTables(null, null, "%", new String[] { "TABLE" });
 186  0
       while (rs.next()) {
 187  0
         final String tableName = rs.getString("TABLE_NAME").toLowerCase();
 188  0
         tables.add(tableName);
 189  0
       }
 190  0
       if (LOGGER.isDebugEnabled()) {
 191  0
         LOGGER.debug("Tables:"
 192  
             + tables);
 193  
       }
 194  
     } finally {
 195  0
       SQLFunctions.close(rs);
 196  0
     }
 197  0
     return tables;
 198  
   }
 199  
 
 200  
 }