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

View File

@ -97,57 +97,44 @@ public class MySQLProvider extends SQLProvider {
@Override
public boolean runQuery(String query, QueryPS queryPS) {
boolean success = false;
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection();
if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null");
try (Connection connection = getConnection()) {
if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null");
}
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement);
preparedStatement.execute();
return true;
}
}
preparedStatement = connection.prepareStatement(query);
queryPS.onRun(preparedStatement);
preparedStatement.execute();
success = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(preparedStatement);
close(connection);
}
return success;
return false;
}
@Override
public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) {
boolean success = false;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null");
try (Connection connection = getConnection()) {
if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null");
}
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
return queryRS.onResult(resultSet);
}
}
}
preparedStatement = connection.prepareStatement(query);
queryPS.onRun(preparedStatement);
resultSet = preparedStatement.executeQuery();
success = queryRS.onResult(resultSet);
} catch (SQLException e) {
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[] addressSplit = address.split(":");
address = addressSplit[0];
String port = addressSplit.length > 1 ? addressSplit[1] : "3306";
String port = addressSplit.length > 1 ? addressSplit[1] : "5432";
String database = configuration.getDatabase();
String username = configuration.getUsername();
@ -88,57 +88,40 @@ public class PostgreSQLProvider extends SQLProvider {
@Override
public boolean runQuery(String query, QueryPS queryPS) {
boolean success = false;
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection();
try (Connection connection = getConnection()) {
if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null");
}
preparedStatement = connection.prepareStatement(query);
queryPS.onRun(preparedStatement);
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement);
preparedStatement.execute();
success = true;
preparedStatement.execute();
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(preparedStatement);
close(connection);
}
return success;
return false;
}
@Override
public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) {
boolean success = false;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
try (Connection connection = getConnection()){
if (connection == null || connection.isClosed()) {
throw new IllegalStateException("SQL connection is null");
}
preparedStatement = connection.prepareStatement(query);
queryPS.onRun(preparedStatement);
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
queryPS.onRun(preparedStatement);
resultSet = preparedStatement.executeQuery();
success = queryRS.onResult(resultSet);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
return queryRS.onResult(resultSet);
}
}
} catch (SQLException e) {
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 {
private static final QueryPS EMPTY_PS = preparedStatement -> {};
static void close(AutoCloseable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception ignored) {
}
}
}
@Getter
private final String name;