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.service;
017    
018    import java.io.BufferedReader;
019    import java.io.IOException;
020    import java.util.List;
021    
022    import org.apache.commons.io.IOUtils;
023    import org.kuali.common.jdbc.reader.SqlReader;
024    import org.kuali.common.jdbc.sql.model.SqlMetaData;
025    import org.kuali.common.jdbc.suppliers.ComplexStringSupplier;
026    import org.kuali.common.jdbc.suppliers.SimpleStringSupplier;
027    import org.kuali.common.jdbc.suppliers.SqlLocationContext;
028    import org.kuali.common.jdbc.suppliers.SqlSupplier;
029    import org.kuali.common.util.LocationUtils;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    public class MetaDataUtils {
034    
035            private static final Logger logger = LoggerFactory.getLogger(MetaDataUtils.class);
036    
037            public static SqlMetaData getSqlMetaData(String location, SqlLocationContext context) {
038                    BufferedReader in = null;
039                    try {
040                            logger.debug("Getting metadata for [{}] - encoding {}", location, context.getEncoding());
041                            in = LocationUtils.getBufferedReader(location, context.getEncoding());
042                            return getSqlMetaData(in, context.getReader());
043                    } catch (IOException e) {
044                            throw new IllegalStateException(e);
045                    } finally {
046                            IOUtils.closeQuietly(in);
047                    }
048            }
049    
050            public static SqlMetaData getSqlMetaData(ComplexStringSupplier supplier) {
051                    long count = 0;
052                    long size = 0;
053                    for (String string : supplier.getStrings()) {
054                            SqlMetaData smd = getSqlMetaData(string, supplier.getReader());
055                            count += smd.getCount();
056                            size += smd.getSize();
057                    }
058                    return new SqlMetaData(count, size);
059            }
060    
061            public static SqlMetaData getSqlMetaData(String sql, SqlReader reader) {
062                    BufferedReader in = null;
063                    try {
064                            in = LocationUtils.getBufferedReaderFromString(sql);
065                            return MetaDataUtils.getSqlMetaData(in, reader);
066                    } catch (IOException e) {
067                            throw new IllegalStateException(e);
068                    } finally {
069                            IOUtils.closeQuietly(in);
070                    }
071            }
072    
073            public static SqlMetaData getSqlMetaData(BufferedReader in, SqlReader reader) throws IOException {
074                    long count = 0;
075                    long size = 0;
076                    String sql = reader.getSql(in);
077                    while (sql != null) {
078                            count++;
079                            size += sql.length();
080                            sql = reader.getSql(in);
081                    }
082                    return new SqlMetaData(count, size);
083            }
084    
085            public static SqlMetaData getSqlMetaData(SimpleStringSupplier supplier) {
086                    List<String> strings = supplier.getStrings();
087                    int count = strings.size();
088                    long size = 0;
089                    for (String string : strings) {
090                            size += string.length();
091                    }
092                    return new SqlMetaData(count, size);
093            }
094    
095            /**
096             * Return a count of the total number of SQL statements contained in <code>suppliers</code>.
097             */
098            public static long getSqlCount(List<SqlSupplier> suppliers) {
099                    long count = 0;
100                    for (SqlSupplier supplier : suppliers) {
101                            SqlMetaData smd = supplier.getMetaData();
102                            count += smd.getCount();
103                    }
104                    return count;
105            }
106    
107    }