X-Git-Url: http://jaekl.net/gitweb/?p=squelch.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fjaekl%2Fsquelch%2Fstmt%2FSelect.java;h=c5f785e5a3d6acd651bc30b3822f362a255f5f30;hp=088181a50662c1015d0f1a936a29730e2bbfe531;hb=5c97ab69edb44c0f1a7dc416ffaf1b934f7a3d7d;hpb=63163502c2d86fad43c38ae4041c8bf4b2e4b387 diff --git a/src/main/java/net/jaekl/squelch/stmt/Select.java b/src/main/java/net/jaekl/squelch/stmt/Select.java index 088181a..c5f785e 100644 --- a/src/main/java/net/jaekl/squelch/stmt/Select.java +++ b/src/main/java/net/jaekl/squelch/stmt/Select.java @@ -5,11 +5,10 @@ import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Locale; -import net.jaekl.squelch.sql.Column; +import net.jaekl.squelch.db.DbDriver; public class Select extends Query { @@ -24,71 +23,33 @@ public class Select extends Query { } @Override - public int exec(Connection conn, PrintWriter pw, String line) throws IOException, SQLException + public int exec(DbDriver driver, Connection conn, PrintWriter pw, String line) throws IOException, SQLException { int rowCount = 0; - try (PreparedStatement ps = conn.prepareStatement(line)) + // If there's a ';' on the end of this line, remove it. + String trimmed = line.trim(); + if (trimmed.endsWith(";")) { + trimmed = trimmed.substring(0, trimmed.length() - 1); + } + + try (PreparedStatement ps = conn.prepareStatement(trimmed)) { try (ResultSet rs = ps.executeQuery()) { - rowCount = printFormatted(pw, rs); - + rowCount = printFormatted(pw, rs); } } return rowCount; } - - private Column[] getColumns(ResultSetMetaData metaData) - throws SQLException, ClassNotFoundException - { - int colCount = metaData.getColumnCount(); - - Column cols[] = new Column[colCount]; - for (int idx = 1; idx <= colCount; ++idx) { - String label = metaData.getColumnLabel(idx); - Class clazz = Class.forName(metaData.getColumnTypeName(idx)); - int width = metaData.getColumnDisplaySize(idx); - cols[idx] = new Column(label, clazz, width); - } - - return cols; - } - + private int printFormatted(PrintWriter pw, ResultSet rs) throws IOException, SQLException { - try { - ResultSetMetaData metaData = rs.getMetaData(); - int rowCount = 0; - Column[] cols = getColumns(metaData); - StringBuilder sb = new StringBuilder(); - sb.append("----------------------------------\n"); - for (int idx = 1; idx <= cols.length; ++idx) { - sb.append("|" + cols[idx - 1].getLabel()); - } - sb.append("|\n"); - String header = sb.toString(); - - while (rs.next()) { - if (null != header) { - pw.print(header); - header = null; - } - - for (int idx = 1; idx <= cols.length; ++idx) { - pw.print("|" + rs.getObject(idx)); - } - pw.println("|"); - } - if (rowCount > 0) { - pw.println("----------------------------------"); - } - pw.println("" + rowCount + " row(s) returned."); - - return rowCount; - } - catch (ClassNotFoundException exc) { - throw new SQLException(exc); - } + TabularResultSet trs = new TabularResultSet(rs); + // TODO: StringTable i18n + int rowCount = trs.printTable(pw, "No rows returned."); + pw.flush(); + + return rowCount; } }