88import java .io .InputStream ;
99import java .io .InputStreamReader ;
1010import java .io .PrintWriter ;
11+ import java .io .StringWriter ;
1112import java .sql .Connection ;
13+ import java .sql .Driver ;
14+ import java .sql .DriverManager ;
1215import java .sql .ResultSet ;
1316import java .sql .ResultSetMetaData ;
1417import java .sql .SQLException ;
@@ -30,24 +33,55 @@ public static void main (String[] args) throws Exception {
3033 }
3134 String sql = readAll (in );
3235 PrintWriter out = null ;
33- if (config .getOutputResult () == null )
34- out = new PrintWriter (System .out );
36+ StringWriter mailBody = null ;
37+ if (config .getMailSendTo () != null ) {
38+ mailBody = new StringWriter ();
39+ out = new PrintWriter (mailBody );
40+ }
41+ else if (config .getOutputResult () == null )
42+ out = new PrintWriter (System .out , true );
3543 else
3644 out = new PrintWriter (config .getOutputResult ());
3745
3846 Connection conn = null ;
3947 try {
40- conn = DynamicDriver .getConnection (config .getJdbcDriverPath (), config .getJdbcDriverClass (),
48+ if (config .getJdbcDriverPath () == null ) {
49+ // driver in classpath and registered
50+ conn = DriverManager .getConnection (config .getJdbcUrl (), config .getJdbcUser (), config .getJdbcPass ());
51+ } else
52+ conn = DynamicDriver .getConnection (config .getJdbcDriverPath (), config .getJdbcDriverClass (),
4153 config .getJdbcUrl (), config .getJdbcUser (), config .getJdbcPass ());
4254 Statement st = conn .createStatement ();
4355 boolean isResultSet = st .execute (sql );
4456 boolean hasMore = true ;
57+ int updateCount = st .getUpdateCount (); // just once per result
4558 while (hasMore ) {
46- if (isResultSet )
59+ if (isResultSet && updateCount < 0 )
4760 writeResultSet (st .getResultSet (), out , config );
4861 else
49- out .println ("UpdateCount = " + st .getUpdateCount ());
50- hasMore = st .getMoreResults ();
62+ writeUpdateCount (updateCount , out , config );
63+ isResultSet = st .getMoreResults ();
64+ updateCount = st .getUpdateCount ();
65+ hasMore = isResultSet || updateCount > -1 ;
66+ }
67+ // send result to mail?
68+ if (config .getMailSendTo () != null && mailBody != null ) {
69+ SendMail sendMail ;
70+ if (config .getMailAuth () != null ) {
71+ sendMail = new SendMail (config .getMailHost (),
72+ config .getMailPort (),
73+ config .getMailFrom (),
74+ Boolean .valueOf (config .getMailAuth ()),
75+ Boolean .valueOf (config .getMailTLS ()),
76+ config .getMailUser (),
77+ config .getMailPass ()
78+ );
79+ } else {
80+ sendMail = new SendMail (config .getMailHost (),
81+ config .getMailPort (),
82+ config .getMailFrom ());
83+ }
84+ sendMail .sendText (config .getMailSubject (), mailBody .toString (), config .getMailSendTo ());
5185 }
5286
5387 } finally {
@@ -56,16 +90,31 @@ public static void main (String[] args) throws Exception {
5690 }
5791 }
5892
93+ private static void writeUpdateCount (int updateCount , PrintWriter out , RunnerConfig config ) {
94+ String s ;
95+ if (config .getPrintHeader ())
96+ s = String .format ("==================\n UpdateCount = %d\n ==================" , updateCount );
97+ else
98+ s = "UpdateCount = " + updateCount ;
99+ out .println (s );
100+
101+ }
102+
59103 private static void writeResultSet (ResultSet resultSet , PrintWriter out , RunnerConfig conf ) throws SQLException {
60104 ResultSetMetaData rsmd = resultSet .getMetaData ();
61105 int colCount = rsmd .getColumnCount ();
62106 if (conf .getPrintHeader ()) {
107+ StringBuffer underline = new StringBuffer ();
63108 for (int i = 1 ; i <= colCount ; i ++) {
64109 out .print (rsmd .getColumnName (i ));
65- if (i < colCount )
110+ underline .append (repeatChar ('=' , rsmd .getColumnName (i ).length ()));
111+ if (i < colCount ) {
66112 out .print (conf .getPrintFieldSeparator ());
113+ underline .append (conf .getPrintFieldSeparator ());
114+ }
67115 }
68116 out .println ();
117+ out .println (underline .toString ());
69118 } // header
70119 while (resultSet .next ()) {
71120 for (int i = 1 ; i <= colCount ; i ++) {
@@ -78,6 +127,10 @@ private static void writeResultSet(ResultSet resultSet, PrintWriter out, RunnerC
78127 out .flush ();
79128 }
80129
130+ private static String repeatChar (char c , int length ) {
131+ return new String (new char [length ]).replace ('\0' , c );
132+ }
133+
81134 private static String readAll (InputStream in ) throws IOException {
82135 BufferedReader br = new BufferedReader (new InputStreamReader (in ));
83136 StringBuffer sb = new StringBuffer ();
0 commit comments