Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 66 additions & 90 deletions src/main/java/io/elastic/jdbc/QueryBuilders/MSSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,19 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;

public class MSSQL extends Query {

public ResultSet executeSelectQuery(Connection connection, String sqlQuery, JsonObject body)
throws SQLException {
PreparedStatement stmt = connection.prepareStatement(sqlQuery);
int i = 1;
for (Entry<String, JsonValue> entry : body.entrySet()) {
Utils.setStatementParam(stmt, i, entry.getKey(), body);
i++;
}
return stmt.executeQuery();
}

public ResultSet executeSelectTrigger(Connection connection, String sqlQuery)
throws SQLException {
PreparedStatement stmt = connection.prepareStatement(sqlQuery);
if (pollingValue != null) {
stmt.setTimestamp(1, pollingValue);
}
return stmt.executeQuery();
}

public ResultSet executePolling(Connection connection) throws SQLException {
public ArrayList executePolling(Connection connection) throws SQLException {
validateQuery();
String sql = "WITH Results_CTE AS" +
"(" +
Expand All @@ -46,14 +30,37 @@ public ResultSet executePolling(Connection connection) throws SQLException {
" FROM Results_CTE" +
" WHERE RowNum > ?" +
" AND RowNum < ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setTimestamp(1, pollingValue);
stmt.setInt(2, skipNumber);
stmt.setInt(3, countNumber + skipNumber);
return stmt.executeQuery();
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setTimestamp(1, pollingValue);
stmt.setInt(2, skipNumber);
stmt.setInt(3, countNumber + skipNumber);
try (ResultSet rs = stmt.executeQuery()) {
ArrayList listResult = new ArrayList();
JsonObjectBuilder row = Json.createObjectBuilder();
ResultSetMetaData metaData = rs.getMetaData();
while (rs.next()) {
for (int i = 1; i <= metaData.getColumnCount(); i++) {
row = Utils.getColumnDataByType(rs, metaData, i, row);
if (metaData.getColumnName(i).toUpperCase().equals(pollingField.toUpperCase())) {
if (maxPollingValue.before(rs.getTimestamp(i))) {
if (rs.getString(metaData.getColumnName(i)).length() > 10) {
maxPollingValue = java.sql.Timestamp
.valueOf(rs.getString(metaData.getColumnName(i)));
} else {
maxPollingValue = java.sql.Timestamp
.valueOf(rs.getString(metaData.getColumnName(i)) + " 00:00:00");
}
}
}
}
listResult.add(row.build());
}
return listResult;
}
}
}

public ResultSet executeLookup(Connection connection, JsonObject body) throws SQLException {
public JsonObject executeLookup(Connection connection, JsonObject body) throws SQLException {
validateQuery();
String sql = "WITH Results_CTE AS" +
"(" +
Expand All @@ -67,41 +74,26 @@ public ResultSet executeLookup(Connection connection, JsonObject body) throws SQ
" FROM Results_CTE" +
" WHERE RowNum > ?" +
" AND RowNum < ?";
PreparedStatement stmt = connection.prepareStatement(sql);
for (Map.Entry<String, JsonValue> entry : body.entrySet()) {
Utils.setStatementParam(stmt, 1, entry.getKey(), body);
}
stmt.setInt(2, skipNumber);
stmt.setInt(3, countNumber + skipNumber);
return stmt.executeQuery();
return Utils.getLookupRow(connection, body, sql, skipNumber, countNumber + skipNumber);
}

public int executeDelete(Connection connection, JsonObject body) throws SQLException {
validateQuery();
String sql = "DELETE" +
" FROM " + tableName +
" WHERE " + lookupField + " = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, lookupValue);
return stmt.executeUpdate();
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, lookupValue);
return stmt.executeUpdate();
}
}

public boolean executeRecordExists(Connection connection, JsonObject body) throws SQLException {
validateQuery();
String sql = "SELECT COUNT(*)" +
" FROM " + tableName +
" WHERE " + lookupField + " = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
try {
Utils.setStatementParam(stmt, 1, lookupField, body);
ResultSet rs = stmt.executeQuery();
rs.next();
return rs.getInt(1) > 0;
} finally {
if (stmt != null) {
stmt.close();
}
}
return Utils.isRecordExists(connection, body, sql, lookupField);
}

public void executeInsert(Connection connection, String tableName, JsonObject body)
Expand All @@ -122,18 +114,37 @@ public void executeInsert(Connection connection, String tableName, JsonObject bo
String sql = "INSERT INTO " + tableName +
" (" + keys.toString() + ")" +
" VALUES (" + values.toString() + ")";
PreparedStatement stmt = connection.prepareStatement(sql);
try {
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
int i = 1;
for (Map.Entry<String, JsonValue> entry : body.entrySet()) {
Utils.setStatementParam(stmt, i, entry.getKey(), body);
i++;
}
stmt.execute();
} finally {
if (stmt != null) {
stmt.close();
}
}

public void executeUpdate(Connection connection, String tableName, String idColumn,
String idValue, JsonObject body) throws SQLException {
validateQuery();
StringBuilder setString = new StringBuilder();
for (Map.Entry<String, JsonValue> entry : body.entrySet()) {
if (setString.length() > 0) {
setString.append(",");
}
setString.append(entry.getKey()).append(" = ?");
}
String sql = "UPDATE " + tableName +
" SET " + setString.toString() +
" WHERE " + idColumn + " = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
int i = 1;
for (Map.Entry<String, JsonValue> entry : body.entrySet()) {
Utils.setStatementParam(stmt, i, entry.getKey(), body);
i++;
}
Utils.setStatementParam(stmt, i, idColumn, body);
stmt.execute();
}
}

Expand Down Expand Up @@ -168,55 +179,20 @@ public void executeUpsert(Connection connection, String idColumn, JsonObject bod
" (" + keys.toString() + ")" +
" VALUES (" + values.toString() + ")" +
" COMMIT;";
PreparedStatement stmt = null;
try {
stmt = connection.prepareStatement(sql);
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
Utils.setStatementParam(stmt, 1, idColumn, body);
int i = 2;
for (Map.Entry<String, JsonValue> entry : body.entrySet()) {
Utils.setStatementParam(stmt, i, entry.getKey(), body);
i++;
}
Utils.setStatementParam(stmt, i, idColumn, body);
i++;
Utils.setStatementParam(stmt, i++, idColumn, body);
for (Map.Entry<String, JsonValue> entry : body.entrySet()) {
Utils.setStatementParam(stmt, i, entry.getKey(), body);
i++;
}
stmt.execute();
} finally {
if (stmt != null) {
stmt.close();
}
}
}

public void executeUpdate(Connection connection, String tableName, String idColumn,
String idValue, JsonObject body) throws SQLException {
validateQuery();
StringBuilder setString = new StringBuilder();
for (Map.Entry<String, JsonValue> entry : body.entrySet()) {
if (setString.length() > 0) {
setString.append(",");
}
setString.append(entry.getKey()).append(" = ?");
}
String sql = "UPDATE " + tableName +
" SET " + setString.toString() +
" WHERE " + idColumn + " = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
try {
int i = 1;
for (Map.Entry<String, JsonValue> entry : body.entrySet()) {
Utils.setStatementParam(stmt, i, entry.getKey(), body);
i++;
}
Utils.setStatementParam(stmt, i, idColumn, body);
stmt.execute();
} finally {
if (stmt != null) {
stmt.close();
}
}
}
}
Loading