Change providers syntax; fix port for PostgreSQL provider

This commit is contained in:
bakatrouble 2016-12-25 20:14:17 +03:00 committed by Luck
parent 4c93bf7cf0
commit 9dbef6acc2
4 changed files with 57 additions and 108 deletions

View File

@ -30,13 +30,13 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
public abstract class FlatfileProvider extends SQLProvider { abstract class FlatfileProvider extends SQLProvider {
private final File file; private final File file;
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
private Connection connection; private Connection connection;
public FlatfileProvider(String name, File file) { FlatfileProvider(String name, File file) {
super(name); super(name);
this.file = file; this.file = file;
} }
@ -77,53 +77,41 @@ public abstract class FlatfileProvider extends SQLProvider {
@Override @Override
public boolean runQuery(String query, QueryPS queryPS) { public boolean runQuery(String query, QueryPS queryPS) {
boolean success = false; try (Connection connection = getConnection()) {
PreparedStatement preparedStatement = null;
try {
Connection connection = getConnection();
if (connection == null || connection.isClosed()) { if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null"); throw new IllegalStateException("SQL connection is null");
} }
preparedStatement = connection.prepareStatement(query); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement); queryPS.onRun(preparedStatement);
preparedStatement.execute(); preparedStatement.execute();
success = true; return true;
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(preparedStatement);
} }
return success; return false;
} }
@Override @Override
public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) { public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) {
boolean success = false;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try { try {
Connection connection = getConnection(); Connection connection = getConnection();
if (connection == null || connection.isClosed()) { if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null"); throw new IllegalStateException("SQL connection is null");
} }
preparedStatement = connection.prepareStatement(query); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement); queryPS.onRun(preparedStatement);
resultSet = preparedStatement.executeQuery(); try (ResultSet resultSet = preparedStatement.executeQuery()) {
success = queryRS.onResult(resultSet); return queryRS.onResult(resultSet);
}
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(resultSet);
close(preparedStatement);
} }
return success; return false;
} }
} }

View File

@ -97,57 +97,44 @@ public class MySQLProvider extends SQLProvider {
@Override @Override
public boolean runQuery(String query, QueryPS queryPS) { public boolean runQuery(String query, QueryPS queryPS) {
boolean success = false;
Connection connection = null;
PreparedStatement preparedStatement = null;
try { try {
connection = getConnection(); try (Connection connection = getConnection()) {
if (connection == null || connection.isClosed()) { if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null"); throw new IllegalStateException("SQL connection is null");
} }
preparedStatement = connection.prepareStatement(query); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement); queryPS.onRun(preparedStatement);
preparedStatement.execute(); preparedStatement.execute();
success = true; return true;
}
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(preparedStatement);
close(connection);
} }
return success; return false;
} }
@Override @Override
public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) { public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) {
boolean success = false;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try { try {
connection = getConnection(); try (Connection connection = getConnection()) {
if (connection == null || connection.isClosed()) { if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null"); throw new IllegalStateException("SQL connection is null");
} }
preparedStatement = connection.prepareStatement(query); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement); queryPS.onRun(preparedStatement);
resultSet = preparedStatement.executeQuery(); try (ResultSet resultSet = preparedStatement.executeQuery()) {
success = queryRS.onResult(resultSet); return queryRS.onResult(resultSet);
}
}
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(resultSet);
close(preparedStatement);
close(connection);
} }
return success; return false;
} }
} }

View File

@ -50,7 +50,7 @@ public class PostgreSQLProvider extends SQLProvider {
String address = configuration.getAddress(); String address = configuration.getAddress();
String[] addressSplit = address.split(":"); String[] addressSplit = address.split(":");
address = addressSplit[0]; address = addressSplit[0];
String port = addressSplit.length > 1 ? addressSplit[1] : "3306"; String port = addressSplit.length > 1 ? addressSplit[1] : "5432";
String database = configuration.getDatabase(); String database = configuration.getDatabase();
String username = configuration.getUsername(); String username = configuration.getUsername();
@ -88,57 +88,40 @@ public class PostgreSQLProvider extends SQLProvider {
@Override @Override
public boolean runQuery(String query, QueryPS queryPS) { public boolean runQuery(String query, QueryPS queryPS) {
boolean success = false; try (Connection connection = getConnection()) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection();
if (connection == null || connection.isClosed()) { if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null"); throw new IllegalStateException("SQL connection is null");
} }
preparedStatement = connection.prepareStatement(query); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement); queryPS.onRun(preparedStatement);
preparedStatement.execute(); preparedStatement.execute();
success = true; return true;
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(preparedStatement);
close(connection);
} }
return success; return false;
} }
@Override @Override
public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) { public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) {
boolean success = false; try (Connection connection = getConnection()){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
if (connection == null || connection.isClosed()) { if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null"); throw new IllegalStateException("SQL connection is null");
} }
preparedStatement = connection.prepareStatement(query); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement); queryPS.onRun(preparedStatement);
resultSet = preparedStatement.executeQuery(); try (ResultSet resultSet = preparedStatement.executeQuery()) {
success = queryRS.onResult(resultSet); return queryRS.onResult(resultSet);
}
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(resultSet);
close(preparedStatement);
close(connection);
} }
return success; return false;
} }
} }

View File

@ -34,15 +34,6 @@ import java.sql.SQLException;
public abstract class SQLProvider { public abstract class SQLProvider {
private static final QueryPS EMPTY_PS = preparedStatement -> {}; private static final QueryPS EMPTY_PS = preparedStatement -> {};
static void close(AutoCloseable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception ignored) {
}
}
}
@Getter @Getter
private final String name; private final String name;