001 /** 002 * Copyright 2010-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.common.jdbc; 017 018 import java.io.BufferedReader; 019 import java.io.IOException; 020 import java.sql.Connection; 021 import java.sql.SQLException; 022 import java.sql.Statement; 023 import java.util.List; 024 025 import javax.sql.DataSource; 026 027 import org.kuali.common.jdbc.supplier.SqlSupplier; 028 import org.slf4j.Logger; 029 import org.slf4j.LoggerFactory; 030 import org.springframework.jdbc.datasource.DataSourceUtils; 031 import org.springframework.util.Assert; 032 033 public class JdbcUtils { 034 035 private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class); 036 037 /** 038 * Return the total size of the SQL statements contained in <code>suppliers</code>. Assumes <code>suppliers</code> has had its <code>SqlMetaData</code> filled in. 039 */ 040 public static long getSqlSize(List<SqlSupplier> suppliers) { 041 long size = 0; 042 for (SqlSupplier supplier : suppliers) { 043 SqlMetaData smd = supplier.getMetaData(); 044 size += smd.getSize(); 045 } 046 return size; 047 } 048 049 /** 050 * Return a count of the total number of SQL statements contained in <code>suppliers</code>. Assumes <code>suppliers</code> has had its <code>SqlMetaData</code> filled in. 051 */ 052 public static long getSqlCount(List<SqlSupplier> suppliers) { 053 long count = 0; 054 for (SqlSupplier supplier : suppliers) { 055 SqlMetaData smd = supplier.getMetaData(); 056 count += smd.getCount(); 057 } 058 return count; 059 } 060 061 public static final void closeQuietly(DataSource dataSource, Connection conn, Statement statement) { 062 closeQuietly(statement); 063 closeQuietly(dataSource, conn); 064 } 065 066 public static final void closeQuietly(Statement statement) { 067 if (statement == null) { 068 return; 069 } 070 try { 071 statement.close(); 072 } catch (SQLException e) { 073 throw new IllegalStateException(e); 074 } 075 } 076 077 public static final void closeQuietly(DataSource dataSource, Connection conn) { 078 if (conn == null && dataSource == null) { 079 return; 080 } 081 Assert.notNull(dataSource, "dataSource is null but conn is not"); 082 try { 083 logger.trace("releasing connection"); 084 DataSourceUtils.doReleaseConnection(conn, dataSource); 085 } catch (SQLException e) { 086 throw new IllegalStateException(e); 087 } 088 } 089 090 public static SqlMetaData getSqlMetaData(BufferedReader in, SqlReader reader) throws IOException { 091 long count = 0; 092 long size = 0; 093 List<String> sql = reader.getSql(in); 094 while (sql != null) { 095 for (String s : sql) { 096 count++; 097 size += s.length(); 098 } 099 sql = reader.getSql(in); 100 } 101 return new SqlMetaData(count, size); 102 } 103 104 }