Skip to content

Commit 6668426

Browse files
author
shulkaolka
authored
Merge pull request #6 from elasticio/PollingTriggerIssue
Polling trigger issue
2 parents cd0c8c3 + 4444712 commit 6668426

File tree

7 files changed

+103
-256
lines changed

7 files changed

+103
-256
lines changed

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

Lines changed: 3 additions & 43 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 {
@@ -28,36 +24,8 @@ public ArrayList executePolling(Connection connection) throws SQLException {
2824
" )" +
2925
" SELECT *" +
3026
" FROM Results_CTE" +
31-
" WHERE RowNum > ?" +
32-
" AND RowNum < ?";
33-
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
34-
stmt.setTimestamp(1, pollingValue);
35-
stmt.setInt(2, skipNumber);
36-
stmt.setInt(3, countNumber + skipNumber);
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-
}
27+
" WHERE RowNum <= ?";
28+
return getRowsExecutePolling(connection, sql);
6129
}
6230

6331
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
@@ -74,7 +42,7 @@ public JsonObject executeLookup(Connection connection, JsonObject body) throws S
7442
" FROM Results_CTE" +
7543
" WHERE RowNum > ?" +
7644
" AND RowNum < ?";
77-
return Utils.getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
45+
return getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
7846
}
7947

8048
public int executeDelete(Connection connection, JsonObject body) throws SQLException {
@@ -88,14 +56,6 @@ public int executeDelete(Connection connection, JsonObject body) throws SQLExcep
8856
}
8957
}
9058

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

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

Lines changed: 4 additions & 43 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 {
@@ -23,38 +19,11 @@ public ArrayList executePolling(Connection connection) throws SQLException {
2319
sql.append(pollingField);
2420
sql.append(" > ?");
2521
if (orderField != null) {
26-
sql.append(" ORDER BY ").append(orderField);
22+
sql.append(" ORDER BY ").append(orderField).append(" ASC");
2723
}
28-
sql.append(" ASC LIMIT ? OFFSET ?");
24+
sql.append(" LIMIT ?");
2925

30-
try (PreparedStatement stmt = connection.prepareStatement(sql.toString())) {
31-
stmt.setTimestamp(1, pollingValue);
32-
stmt.setInt(2, countNumber);
33-
stmt.setInt(3, skipNumber);
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+
return getRowsExecutePolling(connection, sql.toString());
5827
}
5928

6029
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
@@ -66,7 +35,7 @@ public JsonObject executeLookup(Connection connection, JsonObject body) throws S
6635
sql.append(" = ?");
6736
sql.append(" ORDER BY ").append(lookupField);
6837
sql.append(" ASC LIMIT ? OFFSET ?");
69-
return Utils.getLookupRow(connection, body, sql.toString(), countNumber, skipNumber);
38+
return getLookupRow(connection, body, sql.toString(), countNumber, skipNumber);
7039
}
7140

7241
public int executeDelete(Connection connection, JsonObject body) throws SQLException {
@@ -79,14 +48,6 @@ public int executeDelete(Connection connection, JsonObject body) throws SQLExcep
7948
}
8049
}
8150

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

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

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,24 @@
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 Oracle extends Query {
1713

1814
public ArrayList executePolling(Connection connection) throws SQLException {
1915
validateQuery();
20-
String sql = "SELECT * FROM " +
21-
" (SELECT b.*, rank() over (order by " + pollingField + ") as rnk FROM " +
22-
tableName + " b) WHERE " + pollingField + " > ?" +
23-
" AND rnk BETWEEN ? AND ?" +
24-
" ORDER BY " + pollingField;
25-
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
26-
/* data types mapping https://docs.oracle.com/cd/B19306_01/java.102/b14188/datamap.htm */
27-
stmt.setTimestamp(1, pollingValue);
28-
stmt.setInt(2, skipNumber);
29-
stmt.setInt(3, countNumber);
30-
try (ResultSet rs = stmt.executeQuery()) {
31-
ArrayList listResult = new ArrayList();
32-
JsonObjectBuilder row = Json.createObjectBuilder();
33-
ResultSetMetaData metaData = rs.getMetaData();
34-
while (rs.next()) {
35-
for (int i = 1; i <= metaData.getColumnCount(); i++) {
36-
row = Utils.getColumnDataByType(rs, metaData, i, row);
37-
if (metaData.getColumnName(i).toUpperCase().equals(pollingField.toUpperCase())) {
38-
if (maxPollingValue.before(rs.getTimestamp(i))) {
39-
if (rs.getString(metaData.getColumnName(i)).length() > 10) {
40-
maxPollingValue = java.sql.Timestamp
41-
.valueOf(rs.getString(metaData.getColumnName(i)));
42-
} else {
43-
maxPollingValue = java.sql.Timestamp
44-
.valueOf(rs.getString(metaData.getColumnName(i)) + " 00:00:00");
45-
}
46-
}
47-
}
48-
}
49-
listResult.add(row.build());
50-
}
51-
return listResult;
52-
}
53-
}
16+
String sql = String.format("SELECT * FROM ("
17+
+ "SELECT ROW_NUMBER() OVER( ORDER BY %s) as rn, o.* from %s o WHERE %s > ?) "
18+
+ "WHERE rn<=? ORDER BY %s",
19+
pollingField,
20+
tableName,
21+
pollingField,
22+
pollingField);
23+
return getRowsExecutePolling(connection, sql);
5424
}
5525

5626
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
@@ -60,7 +30,7 @@ public JsonObject executeLookup(Connection connection, JsonObject body) throws S
6030
tableName + " b) WHERE " + lookupField + " = ? " +
6131
"AND rnk BETWEEN ? AND ? " +
6232
"ORDER BY " + lookupField;
63-
return Utils.getLookupRow(connection, body, sql, skipNumber, countNumber);
33+
return getLookupRow(connection, body, sql, skipNumber, countNumber);
6434
}
6535

6636
public int executeDelete(Connection connection, JsonObject body) throws SQLException {
@@ -73,14 +43,6 @@ public int executeDelete(Connection connection, JsonObject body) throws SQLExcep
7343
}
7444
}
7545

76-
public boolean executeRecordExists(Connection connection, JsonObject body) throws SQLException {
77-
validateQuery();
78-
String sql = "SELECT COUNT(*)" +
79-
" FROM " + tableName +
80-
" WHERE " + lookupField + " = ?";
81-
return Utils.isRecordExists(connection, body, sql, lookupField);
82-
}
83-
8446
public void executeInsert(Connection connection, String tableName, JsonObject body)
8547
throws SQLException {
8648
validateQuery();

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

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

6230
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
@@ -73,7 +41,7 @@ public JsonObject executeLookup(Connection connection, JsonObject body) throws S
7341
" FROM results_cte" +
7442
" WHERE rownum > ?" +
7543
" AND rownum < ?";
76-
return Utils.getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
44+
return getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
7745
}
7846

7947
public int executeDelete(Connection connection, JsonObject body) throws SQLException {
@@ -89,14 +57,6 @@ public int executeDelete(Connection connection, JsonObject body) throws SQLExcep
8957
}
9058
}
9159

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

0 commit comments

Comments
 (0)