1010import java .io .PrintWriter ;
1111import java .io .StringWriter ;
1212import java .sql .Connection ;
13- import java .sql .Driver ;
1413import java .sql .DriverManager ;
1514import java .sql .ResultSet ;
1615import java .sql .ResultSetMetaData ;
@@ -81,7 +80,11 @@ else if (config.getOutputResult() == null)
8180 config .getMailPort (),
8281 config .getMailFrom ());
8382 }
84- sendMail .sendText (config .getMailSubject (), mailBody .toString (), config .getMailSendTo ());
83+ if (ResultFormatter .HTML .equalsIgnoreCase (config .getFormatterName ()))
84+ sendMail .sendMime (config .buildFormatter ().getMime (),
85+ config .getMailSubject (), mailBody .toString (), config .getMailSendTo ());
86+ else // careful 'no object DCH for MIME type csv or others needs activation
87+ sendMail .sendText (config .getMailSubject (), mailBody .toString (), config .getMailSendTo ());
8588 }
8689
8790 } finally {
@@ -91,44 +94,21 @@ else if (config.getOutputResult() == null)
9194 }
9295
9396 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-
97+ ResultFormatter fmt = config .buildFormatter ();
98+ fmt .writeUpdate (out , updateCount );
10199 }
102100
103101 private static void writeResultSet (ResultSet resultSet , PrintWriter out , RunnerConfig conf ) throws SQLException {
104102 ResultSetMetaData rsmd = resultSet .getMetaData ();
105103 int colCount = rsmd .getColumnCount ();
106- if (conf .getPrintHeader ()) {
107- StringBuffer underline = new StringBuffer ();
108- for (int i = 1 ; i <= colCount ; i ++) {
109- out .print (rsmd .getColumnName (i ));
110- underline .append (repeatChar ('=' , rsmd .getColumnName (i ).length ()));
111- if (i < colCount ) {
112- out .print (conf .getPrintFieldSeparator ());
113- underline .append (conf .getPrintFieldSeparator ());
114- }
115- }
116- out .println ();
117- out .println (underline .toString ());
118- } // header
104+ ResultFormatter fmt = conf .buildFormatter ();
105+ fmt .writeBeginResultSet (out );
106+ fmt .writeHeader (out , rsmd );
119107 while (resultSet .next ()) {
120- for (int i = 1 ; i <= colCount ; i ++) {
121- out .print (resultSet .getString (i ));
122- if (i < colCount )
123- out .print (conf .getPrintFieldSeparator ());
124- } // for col
125- out .println ();
108+ fmt .writeRow (out , resultSet , colCount );
126109 } // while row
127110 out .flush ();
128- }
129-
130- private static String repeatChar (char c , int length ) {
131- return new String (new char [length ]).replace ('\0' , c );
111+ fmt .writeEndResultSet (out );
132112 }
133113
134114 private static String readAll (InputStream in ) throws IOException {
@@ -169,6 +149,8 @@ private static RunnerConfig readArgs(String[] args) throws ConfigException {
169149 conf .setPrintFieldSeparator ("\t " );
170150 if (conf .getPrintHeader () == null )
171151 conf .setPrintHeader (true );
152+ if (conf .getFormatterName () == null )
153+ conf .setFormatterName ("text" );
172154 return conf ;
173155 }
174156}
0 commit comments