View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.jdbc;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.sql.Connection;
21  import java.sql.SQLException;
22  import java.sql.Statement;
23  import java.util.List;
24  
25  import javax.sql.DataSource;
26  
27  import org.kuali.common.jdbc.supplier.SqlSupplier;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.jdbc.datasource.DataSourceUtils;
31  import org.springframework.util.Assert;
32  
33  public class JdbcUtils {
34  
35  	private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class);
36  
37  	/**
38  	 * 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.
39  	 */
40  	public static long getSqlSize(List<SqlSupplier> suppliers) {
41  		long size = 0;
42  		for (SqlSupplier supplier : suppliers) {
43  			SqlMetaData smd = supplier.getMetaData();
44  			size += smd.getSize();
45  		}
46  		return size;
47  	}
48  
49  	/**
50  	 * 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.
51  	 */
52  	public static long getSqlCount(List<SqlSupplier> suppliers) {
53  		long count = 0;
54  		for (SqlSupplier supplier : suppliers) {
55  			SqlMetaData smd = supplier.getMetaData();
56  			count += smd.getCount();
57  		}
58  		return count;
59  	}
60  
61  	public static final void closeQuietly(DataSource dataSource, Connection conn, Statement statement) {
62  		closeQuietly(statement);
63  		closeQuietly(dataSource, conn);
64  	}
65  
66  	public static final void closeQuietly(Statement statement) {
67  		if (statement == null) {
68  			return;
69  		}
70  		try {
71  			statement.close();
72  		} catch (SQLException e) {
73  			throw new IllegalStateException(e);
74  		}
75  	}
76  
77  	public static final void closeQuietly(DataSource dataSource, Connection conn) {
78  		if (conn == null && dataSource == null) {
79  			return;
80  		}
81  		Assert.notNull(dataSource, "dataSource is null but conn is not");
82  		try {
83  			logger.trace("releasing connection");
84  			DataSourceUtils.doReleaseConnection(conn, dataSource);
85  		} catch (SQLException e) {
86  			throw new IllegalStateException(e);
87  		}
88  	}
89  
90  	public static SqlMetaData getSqlMetaData(BufferedReader in, SqlReader reader) throws IOException {
91  		long count = 0;
92  		long size = 0;
93  		List<String> sql = reader.getSql(in);
94  		while (sql != null) {
95  			for (String s : sql) {
96  				count++;
97  				size += s.length();
98  			}
99  			sql = reader.getSql(in);
100 		}
101 		return new SqlMetaData(count, size);
102 	}
103 
104 }