|
| 1 | +package es.jdlopez.sqlcmd; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.io.PrintWriter; |
| 11 | +import java.sql.Connection; |
| 12 | +import java.sql.ResultSet; |
| 13 | +import java.sql.ResultSetMetaData; |
| 14 | +import java.sql.SQLException; |
| 15 | +import java.sql.Statement; |
| 16 | +import java.util.Properties; |
| 17 | + |
| 18 | +public class MainRunner { |
| 19 | + |
| 20 | + public static void main (String[] args) throws Exception { |
| 21 | + RunnerConfig config = readArgs(args); |
| 22 | + InputStream in; |
| 23 | + if (config.getInputSQL() == null) { |
| 24 | + in = System.in; |
| 25 | + } else { |
| 26 | + File fIn = new File(config.getInputSQL()); |
| 27 | + if (!fIn.exists()) |
| 28 | + throw new ConfigException("InputSQL", config.getInputSQL() + " does not exists"); |
| 29 | + in = new FileInputStream(fIn); |
| 30 | + } |
| 31 | + String sql = readAll(in); |
| 32 | + PrintWriter out = null; |
| 33 | + if (config.getOutputResult() == null) |
| 34 | + out = new PrintWriter(System.out); |
| 35 | + else |
| 36 | + out = new PrintWriter(config.getOutputResult()); |
| 37 | + |
| 38 | + Connection conn = null; |
| 39 | + try { |
| 40 | + conn = DynamicDriver.getConnection(config.getJdbcDriverPath(), config.getJdbcDriverClass(), |
| 41 | + config.getJdbcUrl(), config.getJdbcUser(), config.getJdbcPass()); |
| 42 | + Statement st = conn.createStatement(); |
| 43 | + boolean isResultSet = st.execute(sql); |
| 44 | + boolean hasMore = true; |
| 45 | + while (hasMore) { |
| 46 | + if (isResultSet) |
| 47 | + writeResultSet(st.getResultSet(), out, config); |
| 48 | + else |
| 49 | + out.println("UpdateCount = " + st.getUpdateCount()); |
| 50 | + hasMore = st.getMoreResults(); |
| 51 | + } |
| 52 | + |
| 53 | + } finally { |
| 54 | + if (conn != null) |
| 55 | + conn.close(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static void writeResultSet(ResultSet resultSet, PrintWriter out, RunnerConfig conf) throws SQLException { |
| 60 | + ResultSetMetaData rsmd = resultSet.getMetaData(); |
| 61 | + int colCount = rsmd.getColumnCount(); |
| 62 | + if (conf.getPrintHeader()) { |
| 63 | + for (int i = 1; i <= colCount; i++) { |
| 64 | + out.print(rsmd.getColumnName(i)); |
| 65 | + if (i <= colCount) |
| 66 | + out.print(conf.getPrintFieldSeparator()); |
| 67 | + } |
| 68 | + out.println(); |
| 69 | + } // header |
| 70 | + while (resultSet.next()) { |
| 71 | + for (int i = 1; i <= colCount; i++) { |
| 72 | + out.print(resultSet.getString(i)); |
| 73 | + if (i <= colCount) |
| 74 | + out.print(conf.getPrintFieldSeparator()); |
| 75 | + } // for col |
| 76 | + out.println(); |
| 77 | + } // while row |
| 78 | + } |
| 79 | + |
| 80 | + private static String readAll(InputStream in) throws IOException { |
| 81 | + BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
| 82 | + StringBuffer sb = new StringBuffer(); |
| 83 | + String line; |
| 84 | + while ((line = br.readLine()) != null) |
| 85 | + sb.append(line); |
| 86 | + br.close(); |
| 87 | + return sb.toString(); |
| 88 | + } |
| 89 | + |
| 90 | + private static RunnerConfig readArgs(String[] args) throws ConfigException { |
| 91 | + RunnerConfig conf = null; |
| 92 | + if (args != null) |
| 93 | + for (String a: args) { |
| 94 | + if (a.startsWith("-c:")) { // config |
| 95 | + Properties p = new Properties(); |
| 96 | + try { |
| 97 | + p.load(new FileReader(a.substring("-c:".length()))); |
| 98 | + } catch (IOException e) { |
| 99 | + throw new ConfigException("configFile", "Reading config file", e); |
| 100 | + } |
| 101 | + conf = BeanReader.fromProperties(p, RunnerConfig.class); |
| 102 | + } // else if ... for any other args TODO |
| 103 | + } |
| 104 | + else |
| 105 | + throw new ConfigException(null, "No arguments"); |
| 106 | + return conf; |
| 107 | + } |
| 108 | +} |
0 commit comments