|
67 | 67 |
|
68 | 68 | import java.math.BigDecimal; |
69 | 69 | import java.sql.Connection; |
| 70 | +import java.sql.PreparedStatement; |
70 | 71 | import java.sql.ResultSet; |
71 | | -import java.sql.Statement; |
| 72 | +import java.sql.SQLException; |
72 | 73 | import java.text.SimpleDateFormat; |
73 | 74 | import java.util.ArrayList; |
74 | 75 | import java.util.Date; |
@@ -239,9 +240,9 @@ public List<AuditVO> listByCondition(AuditRequest request) throws Exception { |
239 | 240 | } |
240 | 241 | } else if (AuditQuerySource.CLICKHOUSE == querySource) { |
241 | 242 | try (Connection connection = ClickHouseConfig.getCkConnection(); |
242 | | - Statement statement = connection.createStatement(); |
243 | | - ResultSet resultSet = statement.executeQuery( |
244 | | - toAuditCkSql(groupId, streamId, auditId, request.getDt()))) { |
| 243 | + PreparedStatement statement = |
| 244 | + getAuditCkStatement(connection, groupId, streamId, auditId, request.getDt()); |
| 245 | + ResultSet resultSet = statement.executeQuery()) { |
245 | 246 | List<AuditInfo> auditSet = new ArrayList<>(); |
246 | 247 | while (resultSet.next()) { |
247 | 248 | AuditInfo vo = new AuditInfo(); |
@@ -308,28 +309,41 @@ private SearchRequest toAuditSearchRequest(String index, String groupId, String |
308 | 309 | } |
309 | 310 |
|
310 | 311 | /** |
311 | | - * Convert to clickhouse search sql |
| 312 | + * Get clickhouse Statement |
312 | 313 | * |
| 314 | + * @param connection The ClickHouse connection |
313 | 315 | * @param groupId The groupId of inlong |
314 | 316 | * @param streamId The streamId of inlong |
315 | 317 | * @param auditId The auditId of request |
316 | 318 | * @param dt The datetime of request |
317 | | - * @return clickhouse sql |
| 319 | + * @return The clickhouse Statement |
318 | 320 | */ |
319 | | - private String toAuditCkSql(String groupId, String streamId, String auditId, String dt) { |
| 321 | + private PreparedStatement getAuditCkStatement(Connection connection, String groupId, String streamId, |
| 322 | + String auditId, String dt) throws SQLException { |
320 | 323 | DateTimeFormatter formatter = DateTimeFormat.forPattern(DAY_FORMAT); |
321 | 324 | DateTime date = formatter.parseDateTime(dt); |
322 | 325 | String startDate = date.toString(SECOND_FORMAT); |
323 | 326 | String endDate = date.plusDays(1).toString(SECOND_FORMAT); |
324 | | - return new SQL() |
| 327 | + |
| 328 | + String sql = new SQL() |
325 | 329 | .SELECT("log_ts", "sum(count) as total") |
326 | 330 | .FROM("audit_data") |
327 | | - .WHERE("inlong_group_id = '" + groupId + "'", "inlong_stream_id = '" + streamId + "'", |
328 | | - "audit_id = '" + auditId + "'") |
329 | | - .WHERE("log_ts >= '" + startDate + "'", "log_ts < '" + endDate + "'") |
| 331 | + .WHERE("inlong_group_id = ?") |
| 332 | + .WHERE("inlong_stream_id = ?") |
| 333 | + .WHERE("audit_id = ?") |
| 334 | + .WHERE("log_ts >= ?") |
| 335 | + .WHERE("log_ts < ?") |
330 | 336 | .GROUP_BY("log_ts") |
331 | 337 | .ORDER_BY("log_ts") |
332 | 338 | .toString(); |
| 339 | + |
| 340 | + PreparedStatement statement = connection.prepareStatement(sql); |
| 341 | + statement.setString(1, groupId); |
| 342 | + statement.setString(2, streamId); |
| 343 | + statement.setString(3, auditId); |
| 344 | + statement.setString(4, startDate); |
| 345 | + statement.setString(5, endDate); |
| 346 | + return statement; |
333 | 347 | } |
334 | 348 |
|
335 | 349 | /** |
|
0 commit comments