Skip to content

Commit 4444712

Browse files
committed
Refactor executePolling method
1 parent 0b8b838 commit 4444712

File tree

7 files changed

+83
-227
lines changed

7 files changed

+83
-227
lines changed

src/main/java/io/elastic/jdbc/QueryBuilders/MSSQL.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
import io.elastic.jdbc.Utils;
44
import java.sql.Connection;
55
import java.sql.PreparedStatement;
6-
import java.sql.ResultSet;
7-
import java.sql.ResultSetMetaData;
86
import java.sql.SQLException;
97
import java.util.ArrayList;
108
import java.util.Map;
11-
import javax.json.Json;
129
import javax.json.JsonObject;
13-
import javax.json.JsonObjectBuilder;
1410
import javax.json.JsonValue;
1511

1612
public class MSSQL extends Query {
@@ -29,33 +25,7 @@ public ArrayList executePolling(Connection connection) throws SQLException {
2925
" SELECT *" +
3026
" FROM Results_CTE" +
3127
" WHERE RowNum <= ?";
32-
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
33-
stmt.setTimestamp(1, pollingValue);
34-
stmt.setInt(2, countNumber);
35-
try (ResultSet rs = stmt.executeQuery()) {
36-
ArrayList listResult = new ArrayList();
37-
JsonObjectBuilder row = Json.createObjectBuilder();
38-
ResultSetMetaData metaData = rs.getMetaData();
39-
while (rs.next()) {
40-
for (int i = 1; i <= metaData.getColumnCount(); i++) {
41-
row = Utils.getColumnDataByType(rs, metaData, i, row);
42-
if (metaData.getColumnName(i).toUpperCase().equals(pollingField.toUpperCase())) {
43-
if (maxPollingValue.before(rs.getTimestamp(i))) {
44-
if (rs.getString(metaData.getColumnName(i)).length() > 10) {
45-
maxPollingValue = java.sql.Timestamp
46-
.valueOf(rs.getString(metaData.getColumnName(i)));
47-
} else {
48-
maxPollingValue = java.sql.Timestamp
49-
.valueOf(rs.getString(metaData.getColumnName(i)) + " 00:00:00");
50-
}
51-
}
52-
}
53-
}
54-
listResult.add(row.build());
55-
}
56-
return listResult;
57-
}
58-
}
28+
return getRowsExecutePolling(connection, sql);
5929
}
6030

6131
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
@@ -72,7 +42,7 @@ public JsonObject executeLookup(Connection connection, JsonObject body) throws S
7242
" FROM Results_CTE" +
7343
" WHERE RowNum > ?" +
7444
" AND RowNum < ?";
75-
return Utils.getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
45+
return getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
7646
}
7747

7848
public int executeDelete(Connection connection, JsonObject body) throws SQLException {
@@ -86,14 +56,6 @@ public int executeDelete(Connection connection, JsonObject body) throws SQLExcep
8656
}
8757
}
8858

89-
public boolean executeRecordExists(Connection connection, JsonObject body) throws SQLException {
90-
validateQuery();
91-
String sql = "SELECT COUNT(*)" +
92-
" FROM " + tableName +
93-
" WHERE " + lookupField + " = ?";
94-
return Utils.isRecordExists(connection, body, sql, lookupField);
95-
}
96-
9759
public void executeInsert(Connection connection, String tableName, JsonObject body)
9860
throws SQLException {
9961
validateQuery();

src/main/java/io/elastic/jdbc/QueryBuilders/MySQL.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
import io.elastic.jdbc.Utils;
44
import java.sql.Connection;
55
import java.sql.PreparedStatement;
6-
import java.sql.ResultSet;
7-
import java.sql.ResultSetMetaData;
86
import java.sql.SQLException;
97
import java.util.ArrayList;
108
import java.util.Map;
11-
import javax.json.Json;
129
import javax.json.JsonObject;
13-
import javax.json.JsonObjectBuilder;
1410
import javax.json.JsonValue;
1511

1612
public class MySQL extends Query {
@@ -27,33 +23,7 @@ public ArrayList executePolling(Connection connection) throws SQLException {
2723
}
2824
sql.append(" LIMIT ?");
2925

30-
try (PreparedStatement stmt = connection.prepareStatement(sql.toString())) {
31-
stmt.setTimestamp(1, pollingValue);
32-
stmt.setInt(2, countNumber);
33-
try (ResultSet rs = stmt.executeQuery()) {
34-
ArrayList listResult = new ArrayList();
35-
JsonObjectBuilder row = Json.createObjectBuilder();
36-
ResultSetMetaData metaData = rs.getMetaData();
37-
while (rs.next()) {
38-
for (int i = 1; i <= metaData.getColumnCount(); i++) {
39-
row = Utils.getColumnDataByType(rs, metaData, i, row);
40-
if (metaData.getColumnName(i).toUpperCase().equals(pollingField.toUpperCase())) {
41-
if (maxPollingValue.before(rs.getTimestamp(i))) {
42-
if (rs.getString(metaData.getColumnName(i)).length() > 10) {
43-
maxPollingValue = java.sql.Timestamp
44-
.valueOf(rs.getString(metaData.getColumnName(i)));
45-
} else {
46-
maxPollingValue = java.sql.Timestamp
47-
.valueOf(rs.getString(metaData.getColumnName(i)) + " 00:00:00");
48-
}
49-
}
50-
}
51-
}
52-
listResult.add(row.build());
53-
}
54-
return listResult;
55-
}
56-
}
26+
return getRowsExecutePolling(connection, sql.toString());
5727
}
5828

5929
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
@@ -65,7 +35,7 @@ public JsonObject executeLookup(Connection connection, JsonObject body) throws S
6535
sql.append(" = ?");
6636
sql.append(" ORDER BY ").append(lookupField);
6737
sql.append(" ASC LIMIT ? OFFSET ?");
68-
return Utils.getLookupRow(connection, body, sql.toString(), countNumber, skipNumber);
38+
return getLookupRow(connection, body, sql.toString(), countNumber, skipNumber);
6939
}
7040

7141
public int executeDelete(Connection connection, JsonObject body) throws SQLException {
@@ -78,14 +48,6 @@ public int executeDelete(Connection connection, JsonObject body) throws SQLExcep
7848
}
7949
}
8050

81-
public boolean executeRecordExists(Connection connection, JsonObject body) throws SQLException {
82-
validateQuery();
83-
String sql = "SELECT COUNT(*)" +
84-
" FROM " + tableName +
85-
" WHERE " + lookupField + " = ?";
86-
return Utils.isRecordExists(connection, body, sql, lookupField);
87-
}
88-
8951
public void executeInsert(Connection connection, String tableName, JsonObject body)
9052
throws SQLException {
9153
validateQuery();

src/main/java/io/elastic/jdbc/QueryBuilders/Oracle.java

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,14 @@
33
import io.elastic.jdbc.Utils;
44
import java.sql.Connection;
55
import java.sql.PreparedStatement;
6-
import java.sql.ResultSet;
7-
import java.sql.ResultSetMetaData;
86
import java.sql.SQLException;
97
import java.util.ArrayList;
108
import java.util.Map;
11-
import javax.json.Json;
129
import javax.json.JsonObject;
13-
import javax.json.JsonObjectBuilder;
1410
import javax.json.JsonValue;
15-
import org.slf4j.Logger;
16-
import org.slf4j.LoggerFactory;
1711

1812
public class Oracle extends Query {
1913

20-
private static final Logger LOGGER = LoggerFactory.getLogger(Oracle.class);
21-
22-
2314
public ArrayList executePolling(Connection connection) throws SQLException {
2415
validateQuery();
2516
String sql = String.format("SELECT * FROM ("
@@ -29,35 +20,7 @@ public ArrayList executePolling(Connection connection) throws SQLException {
2920
tableName,
3021
pollingField,
3122
pollingField);
32-
LOGGER.info("SQL Query:, {} with params: {}, {}", sql, pollingValue, countNumber);
33-
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
34-
/* data types mapping https://docs.oracle.com/cd/B19306_01/java.102/b14188/datamap.htm */
35-
stmt.setTimestamp(1, pollingValue);
36-
stmt.setInt(2, countNumber);
37-
try (ResultSet rs = stmt.executeQuery()) {
38-
ArrayList listResult = new ArrayList();
39-
JsonObjectBuilder row = Json.createObjectBuilder();
40-
ResultSetMetaData metaData = rs.getMetaData();
41-
while (rs.next()) {
42-
for (int i = 1; i <= metaData.getColumnCount(); i++) {
43-
row = Utils.getColumnDataByType(rs, metaData, i, row);
44-
if (metaData.getColumnName(i).toUpperCase().equals(pollingField.toUpperCase())) {
45-
if (maxPollingValue.before(rs.getTimestamp(i))) {
46-
if (rs.getString(metaData.getColumnName(i)).length() > 10) {
47-
maxPollingValue = java.sql.Timestamp
48-
.valueOf(rs.getString(metaData.getColumnName(i)));
49-
} else {
50-
maxPollingValue = java.sql.Timestamp
51-
.valueOf(rs.getString(metaData.getColumnName(i)) + " 00:00:00");
52-
}
53-
}
54-
}
55-
}
56-
listResult.add(row.build());
57-
}
58-
return listResult;
59-
}
60-
}
23+
return getRowsExecutePolling(connection, sql);
6124
}
6225

6326
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
@@ -67,7 +30,7 @@ public JsonObject executeLookup(Connection connection, JsonObject body) throws S
6730
tableName + " b) WHERE " + lookupField + " = ? " +
6831
"AND rnk BETWEEN ? AND ? " +
6932
"ORDER BY " + lookupField;
70-
return Utils.getLookupRow(connection, body, sql, skipNumber, countNumber);
33+
return getLookupRow(connection, body, sql, skipNumber, countNumber);
7134
}
7235

7336
public int executeDelete(Connection connection, JsonObject body) throws SQLException {
@@ -80,14 +43,6 @@ public int executeDelete(Connection connection, JsonObject body) throws SQLExcep
8043
}
8144
}
8245

83-
public boolean executeRecordExists(Connection connection, JsonObject body) throws SQLException {
84-
validateQuery();
85-
String sql = "SELECT COUNT(*)" +
86-
" FROM " + tableName +
87-
" WHERE " + lookupField + " = ?";
88-
return Utils.isRecordExists(connection, body, sql, lookupField);
89-
}
90-
9146
public void executeInsert(Connection connection, String tableName, JsonObject body)
9247
throws SQLException {
9348
validateQuery();

src/main/java/io/elastic/jdbc/QueryBuilders/PostgreSQL.java

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
import io.elastic.jdbc.Utils;
44
import java.sql.Connection;
55
import java.sql.PreparedStatement;
6-
import java.sql.ResultSet;
7-
import java.sql.ResultSetMetaData;
86
import java.sql.SQLException;
97
import java.util.ArrayList;
108
import java.util.Map;
11-
import javax.json.Json;
129
import javax.json.JsonObject;
13-
import javax.json.JsonObjectBuilder;
1410
import javax.json.JsonValue;
1511

1612
public class PostgreSQL extends Query {
@@ -27,34 +23,8 @@ public ArrayList executePolling(Connection connection) throws SQLException {
2723
" )" +
2824
" SELECT *" +
2925
" FROM results_cte" +
30-
" WHERE rownum =< ?";
31-
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
32-
stmt.setTimestamp(1, pollingValue);
33-
stmt.setInt(2, countNumber);
34-
try (ResultSet rs = stmt.executeQuery()) {
35-
ArrayList listResult = new ArrayList();
36-
JsonObjectBuilder row = Json.createObjectBuilder();
37-
ResultSetMetaData metaData = rs.getMetaData();
38-
while (rs.next()) {
39-
for (int i = 1; i <= metaData.getColumnCount(); i++) {
40-
row = Utils.getColumnDataByType(rs, metaData, i, row);
41-
if (metaData.getColumnName(i).toUpperCase().equals(pollingField.toUpperCase())) {
42-
if (maxPollingValue.before(rs.getTimestamp(i))) {
43-
if (rs.getString(metaData.getColumnName(i)).length() > 10) {
44-
maxPollingValue = java.sql.Timestamp
45-
.valueOf(rs.getString(metaData.getColumnName(i)));
46-
} else {
47-
maxPollingValue = java.sql.Timestamp
48-
.valueOf(rs.getString(metaData.getColumnName(i)) + " 00:00:00");
49-
}
50-
}
51-
}
52-
}
53-
listResult.add(row.build());
54-
}
55-
return listResult;
56-
}
57-
}
26+
" WHERE rownum <= ?";
27+
return getRowsExecutePolling(connection, sql);
5828
}
5929

6030
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
@@ -71,7 +41,7 @@ public JsonObject executeLookup(Connection connection, JsonObject body) throws S
7141
" FROM results_cte" +
7242
" WHERE rownum > ?" +
7343
" AND rownum < ?";
74-
return Utils.getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
44+
return getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
7545
}
7646

7747
public int executeDelete(Connection connection, JsonObject body) throws SQLException {
@@ -87,14 +57,6 @@ public int executeDelete(Connection connection, JsonObject body) throws SQLExcep
8757
}
8858
}
8959

90-
public boolean executeRecordExists(Connection connection, JsonObject body) throws SQLException {
91-
validateQuery();
92-
String sql = "SELECT COUNT(*)" +
93-
" FROM " + tableName +
94-
" WHERE " + lookupField + " = ?";
95-
return Utils.isRecordExists(connection, body, sql, lookupField);
96-
}
97-
9860
public void executeInsert(Connection connection, String tableName, JsonObject body)
9961
throws SQLException {
10062
validateQuery();

0 commit comments

Comments
 (0)