adds support for null suppression
[squelch.git] / src / main / java / net / jaekl / squelch / stmt / Select.java
index f34b8c9392014b4e210972f3f1dae4b5be10befe..173138d661c44e52dd106f9887094db7c05c1c7b 100644 (file)
@@ -8,6 +8,8 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Locale;
 
+import net.jaekl.squelch.db.DbDriver;
+
 public class Select extends Query {
 
        @Override
@@ -21,24 +23,31 @@ 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(driver, pw, rs);              
                        }
                }
                return rowCount;
        }
        
-       private int printFormatted(PrintWriter pw, ResultSet rs) throws IOException, SQLException
+       private int printFormatted(DbDriver driver, PrintWriter pw, ResultSet rs) throws IOException, SQLException
        {
                TabularResultSet trs = new TabularResultSet(rs);
-               int rowCount = trs.printTable(pw);
+               // TODO:  StringTable i18n
+               int rowCount = trs.printTable(driver, pw, "No rows returned.");
                pw.flush();
                
                return rowCount;