1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.jdbc.service;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.util.List;
21
22 import org.apache.commons.io.IOUtils;
23 import org.kuali.common.jdbc.reader.SqlReader;
24 import org.kuali.common.jdbc.sql.model.SqlMetaData;
25 import org.kuali.common.jdbc.suppliers.ComplexStringSupplier;
26 import org.kuali.common.jdbc.suppliers.SimpleStringSupplier;
27 import org.kuali.common.jdbc.suppliers.SqlLocationContext;
28 import org.kuali.common.jdbc.suppliers.SqlSupplier;
29 import org.kuali.common.util.LocationUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class MetaDataUtils {
34
35 private static final Logger logger = LoggerFactory.getLogger(MetaDataUtils.class);
36
37 public static SqlMetaData getSqlMetaData(String location, SqlLocationContext context) {
38 BufferedReader in = null;
39 try {
40 logger.debug("Getting metadata for [{}] - encoding {}", location, context.getEncoding());
41 in = LocationUtils.getBufferedReader(location, context.getEncoding());
42 return getSqlMetaData(in, context.getReader());
43 } catch (IOException e) {
44 throw new IllegalStateException(e);
45 } finally {
46 IOUtils.closeQuietly(in);
47 }
48 }
49
50 public static SqlMetaData getSqlMetaData(ComplexStringSupplier supplier) {
51 long count = 0;
52 long size = 0;
53 for (String string : supplier.getStrings()) {
54 SqlMetaData smd = getSqlMetaData(string, supplier.getReader());
55 count += smd.getCount();
56 size += smd.getSize();
57 }
58 return new SqlMetaData(count, size);
59 }
60
61 public static SqlMetaData getSqlMetaData(String sql, SqlReader reader) {
62 BufferedReader in = null;
63 try {
64 in = LocationUtils.getBufferedReaderFromString(sql);
65 return MetaDataUtils.getSqlMetaData(in, reader);
66 } catch (IOException e) {
67 throw new IllegalStateException(e);
68 } finally {
69 IOUtils.closeQuietly(in);
70 }
71 }
72
73 public static SqlMetaData getSqlMetaData(BufferedReader in, SqlReader reader) throws IOException {
74 long count = 0;
75 long size = 0;
76 String sql = reader.getSql(in);
77 while (sql != null) {
78 count++;
79 size += sql.length();
80 sql = reader.getSql(in);
81 }
82 return new SqlMetaData(count, size);
83 }
84
85 public static SqlMetaData getSqlMetaData(SimpleStringSupplier supplier) {
86 List<String> strings = supplier.getStrings();
87 int count = strings.size();
88 long size = 0;
89 for (String string : strings) {
90 size += string.length();
91 }
92 return new SqlMetaData(count, size);
93 }
94
95
96
97
98 public static long getSqlCount(List<SqlSupplier> suppliers) {
99 long count = 0;
100 for (SqlSupplier supplier : suppliers) {
101 SqlMetaData smd = supplier.getMetaData();
102 count += smd.getCount();
103 }
104 return count;
105 }
106
107 }