Implement db output reading
This commit is contained in:
		| @@ -1,6 +1,8 @@ | |||||||
| package at.compax.tools.sql; | package at.compax.tools.sql; | ||||||
|  |  | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.sql.CallableStatement; | ||||||
|  | import java.sql.Clob; | ||||||
| import java.sql.Connection; | import java.sql.Connection; | ||||||
| import java.sql.PreparedStatement; | import java.sql.PreparedStatement; | ||||||
| import java.sql.ResultSet; | import java.sql.ResultSet; | ||||||
| @@ -62,6 +64,8 @@ public class SqlExecutor { | |||||||
|       resultBuilder.userErrors(e.getUserErrors()); |       resultBuilder.userErrors(e.getUserErrors()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     resultBuilder.dbOutput(parseDbOutput(conn)); | ||||||
|  |  | ||||||
|     return resultBuilder.build(); |     return resultBuilder.build(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -162,7 +166,12 @@ public class SqlExecutor { | |||||||
|         } else if (columnType == Types.TIMESTAMP) { |         } else if (columnType == Types.TIMESTAMP) { | ||||||
|           columnBuilder.value(rs.getTimestamp(columnIndex)); |           columnBuilder.value(rs.getTimestamp(columnIndex)); | ||||||
|         } else if (columnType == Types.CLOB) { |         } else if (columnType == Types.CLOB) { | ||||||
|           columnBuilder.value(rs.getClob(columnIndex).getSubString(1, 100)); |           Clob clob = rs.getClob(columnIndex); | ||||||
|  |           if (clob != null) { | ||||||
|  |             columnBuilder.value(clob.getSubString(1, 100)); | ||||||
|  |           } else { | ||||||
|  |             columnBuilder.value(null); | ||||||
|  |           } | ||||||
|         } else if (columnType == Types.BLOB) { |         } else if (columnType == Types.BLOB) { | ||||||
|           columnBuilder.value("[BLOB]"); |           columnBuilder.value("[BLOB]"); | ||||||
|         } else { |         } else { | ||||||
| @@ -179,6 +188,7 @@ public class SqlExecutor { | |||||||
|  |  | ||||||
|     return queryResultBuilder.rows(rows).build(); |     return queryResultBuilder.rows(rows).build(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static QueryResult executeUpdate(Connection conn, String sql, Object... parameters) throws SQLException { |   public static QueryResult executeUpdate(Connection conn, String sql, Object... parameters) throws SQLException { | ||||||
|     log.trace("SQL: " + sql.trim()); |     log.trace("SQL: " + sql.trim()); | ||||||
|     String updateQuery = sql.replaceAll(";$", "").trim(); |     String updateQuery = sql.replaceAll(";$", "").trim(); | ||||||
| @@ -301,4 +311,37 @@ public class SqlExecutor { | |||||||
|  |  | ||||||
|     return userErrors; |     return userErrors; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   private static List<String> parseDbOutput(Connection conn) throws SQLException { | ||||||
|  |     enableDbOutput(conn); | ||||||
|  |  | ||||||
|  |     List<String> result = new Vector<>(); | ||||||
|  |  | ||||||
|  |     @Cleanup | ||||||
|  |     CallableStatement call = conn.prepareCall("{call DBMS_OUTPUT.GET_LINE(?, ?)}"); | ||||||
|  |     call.registerOutParameter(1, Types.VARCHAR); | ||||||
|  |     call.registerOutParameter(2, Types.NUMERIC); | ||||||
|  |  | ||||||
|  |     long status = 0L; | ||||||
|  |     while (status == 0L) { | ||||||
|  |       call.execute(); | ||||||
|  |       status = call.getLong(2); | ||||||
|  |  | ||||||
|  |       String line = call.getString(1); | ||||||
|  |       log.trace("DBMS_OUTPUT: " + line); | ||||||
|  |       log.trace("DBMS_OUTPUT Status: " + status); | ||||||
|  |       if (line != null && status == 0L) { | ||||||
|  |         result.add(line); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     ; | ||||||
|  |  | ||||||
|  |     return result; | ||||||
|  |  | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   private static void enableDbOutput(Connection conn) throws SQLException { | ||||||
|  |     conn.createStatement() // | ||||||
|  |         .execute("BEGIN DBMS_OUTPUT.ENABLE; END;"); | ||||||
|  |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -118,6 +118,15 @@ public class Main { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   public static String formatJsonObject(Object object) throws JsonProcessingException { | ||||||
|  |     ObjectMapper mapper = new ObjectMapper(); | ||||||
|  |     if (executionMode == ExecutionMode.TERMINAL) { | ||||||
|  |       return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object); | ||||||
|  |     } else { | ||||||
|  |       return mapper.writeValueAsString(object); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|   public static void main(String[] args) throws Exception { |   public static void main(String[] args) throws Exception { | ||||||
|     BasicConfigurator.configure(); |     BasicConfigurator.configure(); | ||||||
|     LogManager.getRootLogger().setLevel(Level.OFF); |     LogManager.getRootLogger().setLevel(Level.OFF); | ||||||
| @@ -163,13 +172,4 @@ public class Main { | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static String formatJsonObject(Object object) throws JsonProcessingException { |  | ||||||
|     ObjectMapper mapper = new ObjectMapper(); |  | ||||||
|     if (executionMode == ExecutionMode.TERMINAL) { |  | ||||||
|       return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object); |  | ||||||
|     } else { |  | ||||||
|       return mapper.writeValueAsString(object); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -16,4 +16,5 @@ public class Result { | |||||||
|   private List<UserError> userErrors; |   private List<UserError> userErrors; | ||||||
|   private String exception; |   private String exception; | ||||||
|   private String exceptionMessage; |   private String exceptionMessage; | ||||||
|  |   private List<String> dbOutput; | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user