Move row limiting into SqlExecutor
This commit is contained in:
		| @@ -35,6 +35,8 @@ import lombok.extern.log4j.Log4j; | ||||
|  | ||||
| @Log4j | ||||
| public class SqlExecutor { | ||||
|   private static final long MAX_ROWS = 10L; | ||||
|  | ||||
|   public static Result execute(Connection conn, String sql) throws SQLException, JsonProcessingException { | ||||
|     log.trace("SQL: " + sql.trim()); | ||||
|  | ||||
| @@ -63,29 +65,33 @@ public class SqlExecutor { | ||||
|     return resultBuilder.build(); | ||||
|   } | ||||
|  | ||||
|   public static QueryResult executeJsonQuery(Connection conn, String json) throws SQLException, JsonParseException, JsonMappingException, IOException { | ||||
|   public static QueryResult executeJsonQuery(Connection conn, Long maxRows, String json) throws SQLException, JsonParseException, JsonMappingException, IOException { | ||||
|     log.debug("Parsing JSON: " + json); | ||||
|  | ||||
|     ObjectMapper mapper = new ObjectMapper(); | ||||
|     QueryParameter queryParameter = mapper.readValue(json, QueryParameter.class); | ||||
|     log.debug("Parsed QueryParamter: " + queryParameter.toString()); | ||||
|  | ||||
|     return executeQuery(conn, queryParameter); | ||||
|     return executeQuery(conn, maxRows, queryParameter); | ||||
|   } | ||||
|  | ||||
|   public static QueryResult executeQuery(Connection conn, QueryParameter parameters) throws SQLException { | ||||
|   public static QueryResult executeQuery(Connection conn, Long maxRows, QueryParameter parameters) throws SQLException { | ||||
|     if (parameters == null || parameters.getParameters() == null) { | ||||
|       return executeQuery(conn, parameters.getSql()); | ||||
|       return executeQuery(conn, parameters.getSql(), maxRows); | ||||
|     } else { | ||||
|       return executeQuery(conn, parameters.getSql(), parameters.getParameters().toArray()); | ||||
|       return executeQuery(conn, parameters.getSql(), maxRows, parameters.getParameters().toArray()); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   public static QueryResult executeQuery(Connection conn, String sql, Object... parameters) throws SQLException { | ||||
|   public static QueryResult executeQuery(Connection conn, String sql, Long maxRows, Object... parameters) throws SQLException { | ||||
|     log.trace("SQL: " + sql.trim()); | ||||
|     if (maxRows == null) { | ||||
|       maxRows = MAX_ROWS; | ||||
|     } | ||||
|     String selectQuery = String.format("SELECT * FROM (%s) WHERE rownum <= %d", sql.replaceAll(";$", "").trim(), maxRows); | ||||
|  | ||||
|     @Cleanup | ||||
|     PreparedStatement stmt = conn.prepareStatement(sql); | ||||
|     PreparedStatement stmt = conn.prepareStatement(selectQuery); | ||||
|  | ||||
|     if (parameters != null) { | ||||
|       int parameterIndex = 1; | ||||
|   | ||||
| @@ -27,8 +27,6 @@ import lombok.extern.log4j.Log4j; | ||||
|  | ||||
| @Log4j | ||||
| public class Main { | ||||
|   private static final long MAX_ROWNUMS = 10L; | ||||
|  | ||||
|   @Getter | ||||
|   @RequiredArgsConstructor | ||||
|   public static enum ExecutionMode { | ||||
| @@ -59,12 +57,12 @@ public class Main { | ||||
|  | ||||
|   private static QueryResult executeExampleQueryWithException(Connection conn) throws SQLException, JsonProcessingException { | ||||
|     String sql = "SELECT id, version, workspac FROM s_settings"; | ||||
|     return SqlExecutor.executeQuery(conn, sql); | ||||
|     return SqlExecutor.executeQuery(conn, sql, 10L); | ||||
|   } | ||||
|  | ||||
|   private static QueryResult executeExampleQueryWithoutParameter(Connection conn) throws SQLException, JsonProcessingException { | ||||
|     String sql = "SELECT id, version, workspace FROM s_settings"; | ||||
|     return SqlExecutor.executeQuery(conn, sql); | ||||
|     return SqlExecutor.executeQuery(conn, sql, 10L); | ||||
|   } | ||||
|  | ||||
|   private static QueryResult executeExampleQueryWithParameter(Connection conn) throws SQLException, JsonProcessingException { | ||||
| @@ -109,7 +107,7 @@ public class Main { | ||||
|       @Cleanup | ||||
|       Connection conn = getConnection("172.19.13.66", 1521L, "aax2qc", "aax2qc", "aax2qc"); | ||||
|  | ||||
|       System.out.println(formatJsonObject(SqlExecutor.executeJsonQuery(conn, "{ \"sql\": \"select customer_id from d_customers\"}"))); | ||||
|       System.out.println(formatJsonObject(SqlExecutor.executeJsonQuery(conn, 10L, "{ \"sql\": \"select customer_id from d_customers\"}"))); | ||||
|       System.out.println(formatJsonObject(compileExampleSpec(conn))); | ||||
|       System.out.println(formatJsonObject(compileExampleSpecWithErrors(conn))); | ||||
|       System.out.println(formatJsonObject(executeExampleQueryWithException(conn))); | ||||
| @@ -137,7 +135,6 @@ public class Main { | ||||
|       String sid = args[2]; | ||||
|       String username = args[3]; | ||||
|       String password = args[4]; | ||||
|       long maxRownums = (args.length >= 6) ? Long.parseLong(args[5]) : MAX_ROWNUMS; | ||||
|  | ||||
|       @Cleanup | ||||
|       Connection conn = getConnection(host, port, sid, username, password); | ||||
| @@ -154,9 +151,7 @@ public class Main { | ||||
|           String query = commandBuffer.toString().trim(); | ||||
|           String result; | ||||
|           if (query.toLowerCase().startsWith("select")) { | ||||
|             String selectQuery = String.format("SELECT * FROM (%s) WHERE rownum <= %d", query.replaceAll(";$", "").trim(), maxRownums); | ||||
|  | ||||
|             result = formatJsonObject(SqlExecutor.executeQuery(conn, selectQuery)); | ||||
|             result = formatJsonObject(SqlExecutor.executeQuery(conn, query, null)); | ||||
|           } else { | ||||
|             result = formatJsonObject(SqlExecutor.execute(conn, query)); | ||||
|           } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user