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.ResultSet;
022 import java.sql.SQLException;
023 import java.sql.Statement;
024 import java.util.List;
025
026 import javax.sql.DataSource;
027
028 import org.kuali.common.jdbc.supplier.SqlSupplier;
029 import org.slf4j.Logger;
030 import org.slf4j.LoggerFactory;
031 import org.springframework.jdbc.datasource.DataSourceUtils;
032 import org.springframework.util.Assert;
033
034 /**
035 * @deprecated
036 */
037 @Deprecated
038 public class JdbcUtils {
039
040 private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class);
041
042 /**
043 * 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.
044 */
045 public static long getSqlSize(List<SqlSupplier> suppliers) {
046 long size = 0;
047 for (SqlSupplier supplier : suppliers) {
048 SqlMetaData smd = supplier.getMetaData();
049 size += smd.getSize();
050 }
051 return size;
052 }
053
054 /**
055 * 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.
056 */
057 public static long getSqlCount(List<SqlSupplier> suppliers) {
058 long count = 0;
059 for (SqlSupplier supplier : suppliers) {
060 SqlMetaData smd = supplier.getMetaData();
061 count += smd.getCount();
062 }
063 return count;
064 }
065
066 public static final void closeQuietly(DataSource dataSource, Connection conn, Statement statement) {
067 closeQuietly(statement);
068 closeQuietly(dataSource, conn);
069 }
070
071 public static final void closeQuietly(Statement statement) {
072 if (statement == null) {
073 return;
074 }
075 try {
076 statement.close();
077 } catch (SQLException e) {
078 throw new IllegalStateException(e);
079 }
080 }
081
082 public static final void closeQuietly(ResultSet rs) {
083 if (rs == null) {
084 return;
085 }
086 try {
087 rs.close();
088 } catch (SQLException e) {
089 throw new IllegalStateException(e);
090 }
091 }
092
093 public static final void closeQuietly(DataSource dataSource, Connection conn) {
094 if (conn == null && dataSource == null) {
095 return;
096 }
097 Assert.notNull(dataSource, "dataSource is null but conn is not");
098 try {
099 logger.trace("releasing connection");
100 DataSourceUtils.doReleaseConnection(conn, dataSource);
101 } catch (SQLException e) {
102 throw new IllegalStateException(e);
103 }
104 }
105
106 public static SqlMetaData getSqlMetaData(BufferedReader in, org.kuali.common.jdbc.SqlReader reader) throws IOException {
107 long count = 0;
108 long size = 0;
109 List<String> sql = reader.getSql(in);
110 while (sql != null) {
111 for (String s : sql) {
112 count++;
113 size += s.length();
114 }
115 sql = reader.getSql(in);
116 }
117 return new SqlMetaData(count, size);
118 }
119
120 }