-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added formatters to output: text, html and csv
- Loading branch information
Showing
10 changed files
with
272 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package es.jdlopez.sqlcmd; | ||
|
||
import java.io.PrintWriter; | ||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
|
||
public class CsvFormatter extends ResultFormatter { | ||
private RunnerConfig conf; | ||
|
||
public CsvFormatter(RunnerConfig c) { | ||
this.conf = c; | ||
} | ||
|
||
@Override | ||
public void writeHeader(PrintWriter out, ResultSetMetaData rsmd) throws SQLException { | ||
int colCount = rsmd.getColumnCount(); | ||
if (conf.getPrintHeader()) { | ||
for (int i = 1; i <= colCount; i++) { | ||
out.print(rsmd.getColumnName(i)); | ||
if (i < colCount) { | ||
out.print(conf.getPrintFieldSeparator()); | ||
} | ||
} | ||
out.println(); | ||
} // header | ||
|
||
} | ||
|
||
@Override | ||
public void writeRow(PrintWriter out, ResultSet resultSet, int colCount) throws SQLException { | ||
for (int i = 1; i <= colCount; i++) { | ||
out.print(resultSet.getString(i)); | ||
if (i < colCount) | ||
out.print(conf.getPrintFieldSeparator()); | ||
} // for col | ||
out.println(); | ||
} | ||
|
||
@Override | ||
public void writeEndResultSet(PrintWriter out) { | ||
// nothing needed | ||
} | ||
|
||
@Override | ||
public void writeBeginResultSet(PrintWriter out) { | ||
// nothing needed | ||
} | ||
|
||
@Override | ||
public void writeUpdate(PrintWriter out, int updateCount) { | ||
out.println(updateCount); | ||
} | ||
|
||
@Override | ||
public String getMime() { | ||
return "text/csv"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package es.jdlopez.sqlcmd; | ||
|
||
import java.io.PrintWriter; | ||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
|
||
// TODO: put all html code in configurable templates | ||
public class HtmlFormatter extends ResultFormatter { | ||
private RunnerConfig conf; | ||
|
||
public HtmlFormatter(RunnerConfig c) { | ||
this.conf = c; | ||
} | ||
|
||
@Override | ||
public void writeHeader(PrintWriter out, ResultSetMetaData rsmd) throws SQLException { | ||
if (conf.getPrintHeader()) { // this check must be on MainRunner :-( | ||
int colCount = rsmd.getColumnCount(); | ||
// add thead tag??? | ||
out.println("<tr>"); | ||
for (int i = 1; i <= colCount; i++) { | ||
out.print(String.format("<th>%s</th>", rsmd.getColumnName(i))); | ||
} | ||
out.println("</tr>"); | ||
} // header | ||
} | ||
|
||
@Override | ||
public void writeRow(PrintWriter out, ResultSet resultSet, int colCount) throws SQLException { | ||
out.println("<tr>"); | ||
for (int i = 1; i <= colCount; i++) { | ||
out.print(String.format("<td>%s</td>", resultSet.getString(i))); | ||
} // for col | ||
out.println("</tr>"); | ||
} | ||
|
||
@Override | ||
public void writeBeginResultSet(PrintWriter out) { | ||
out.println("<table>"); | ||
} | ||
|
||
@Override | ||
public void writeEndResultSet(PrintWriter out) { | ||
out.println("</table>"); | ||
} | ||
|
||
@Override | ||
public void writeUpdate(PrintWriter out, int updateCount) { | ||
out.println(String.format("<hr/>UpdateCount: %d<hr/>", updateCount)); | ||
} | ||
|
||
@Override | ||
public String getMime() { | ||
return "text/html"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package es.jdlopez.sqlcmd; | ||
|
||
import java.io.PrintWriter; | ||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
|
||
public abstract class ResultFormatter { | ||
|
||
public static final String TEXT = "text"; | ||
public static final String HTML = "html"; | ||
public static final String CSV = "csv"; | ||
|
||
public abstract void writeHeader(PrintWriter out, ResultSetMetaData rsmd) throws SQLException; | ||
|
||
public abstract void writeRow(PrintWriter out, ResultSet resultSet, int colCount) throws SQLException; | ||
|
||
public abstract void writeEndResultSet(PrintWriter out); | ||
|
||
public abstract void writeUpdate(PrintWriter out, int updateCount); | ||
|
||
public abstract void writeBeginResultSet(PrintWriter out); | ||
|
||
public abstract String getMime(); | ||
|
||
public static String repeatChar(char c, int length) { | ||
return new String(new char[length]).replace('\0', c); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package es.jdlopez.sqlcmd; | ||
|
||
import java.io.PrintWriter; | ||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
|
||
public class TextFormatter extends ResultFormatter { | ||
private RunnerConfig conf; | ||
|
||
public TextFormatter(RunnerConfig c) { | ||
this.conf = c; | ||
} | ||
|
||
@Override | ||
public void writeHeader(PrintWriter out, ResultSetMetaData rsmd) throws SQLException { | ||
int colCount = rsmd.getColumnCount(); | ||
if (conf.getPrintHeader()) { | ||
StringBuffer underline = new StringBuffer(); | ||
for (int i = 1; i <= colCount; i++) { | ||
out.print(rsmd.getColumnName(i)); | ||
underline.append(repeatChar('=', rsmd.getColumnName(i).length())); | ||
if (i < colCount) { | ||
out.print(conf.getPrintFieldSeparator()); | ||
underline.append(conf.getPrintFieldSeparator()); | ||
} | ||
} | ||
out.println(); | ||
out.println(underline.toString()); | ||
} // header | ||
|
||
} | ||
|
||
@Override | ||
public void writeRow(PrintWriter out, ResultSet resultSet, int colCount) throws SQLException { | ||
for (int i = 1; i <= colCount; i++) { | ||
out.print(resultSet.getString(i)); | ||
if (i < colCount) | ||
out.print(conf.getPrintFieldSeparator()); | ||
} // for col | ||
out.println(); | ||
} | ||
|
||
@Override | ||
public void writeEndResultSet(PrintWriter out) { | ||
// nothing needed | ||
} | ||
|
||
@Override | ||
public void writeBeginResultSet(PrintWriter out) { | ||
// nothing needed | ||
} | ||
|
||
@Override | ||
public void writeUpdate(PrintWriter out, int updateCount) { | ||
String s; | ||
if (conf.getPrintHeader()) | ||
s = String.format("==================\nUpdateCount = %d\n==================", updateCount); | ||
else | ||
s = "UpdateCount = " + updateCount; | ||
out.println(s); | ||
} | ||
|
||
@Override | ||
public String getMime() { | ||
return "text/plain"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,11 @@ jdbcUser=SA | |
jdbcPass= | ||
|
||
# mail config | ||
mailFrom=[email protected] | ||
mailSubject=SqlCmd subject sent | ||
mailSendTo=[email protected] | ||
mailSubject=SqlCmd subject sent | ||
# mail server | ||
mailFrom=[email protected] | ||
mailHost=localhost | ||
mailPort=25 | ||
|
||
formatterName=html |