package com.prueba.core.context.integration.database;
+import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
import java.util.List;
import java.util.Map;
public interface DataBaseAccess {
public interface ExecuteStatement<TResult> {
- TResult executeStatement(final Statement statement) throws SQLException;
+ TResult executeStatement(final PreparedStatement statement) throws SQLException;
}
public interface ExecuteResultSet<TResult> {
package com.prueba.core.context.integration.database.impl;
import java.sql.Connection;
+import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
final Connection connection = dataSource.getConnection();
return this.doExecuteQuery(
+ query,
connection,
- statement -> statement.executeQuery(query),
+ preparedStatement -> preparedStatement.executeQuery(),
executeResultSet
);
} finally {
}
}
- protected List<Map<String, String>> doExecuteQuery (final Connection connection,
+ protected List<Map<String, String>> doExecuteQuery (
+ final String query,
+ final Connection connection,
final ExecuteStatement<ResultSet> executeStatement,
final ExecuteResultSet<ResultSet> executeResultSet) throws SQLException {
- try (final Statement statement = connection.createStatement();
- final ResultSet answer = executeStatement.executeStatement(statement)) {
+ try (final PreparedStatement preparedStatement = connection.prepareStatement(query);
+ final ResultSet answer = executeStatement.executeStatement(preparedStatement)) {
return executeResultSet.executeResultSet(answer);
}
--- /dev/null
+package com.prueba.core.web.application.context;
+
+import javax.sql.DataSource;
+
+import com.prueba.core.context.ApplicationContext;
+import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext;
+
+
+public class ApplicationWebContext implements ApplicationContext {
+ private final DataSource dataSource;
+
+ private ApplicationWebContext() {
+ this.dataSource = DoDataSourceContext.getInstance().getDataSource();
+ }
+
+ private static class ApplicationWebContextHolder {
+ private static final ApplicationContext INSTANCE = new ApplicationWebContext();
+ }
+
+ public static ApplicationContext getInstance() {
+ return ApplicationWebContextHolder.INSTANCE;
+ }
+
+ @Override
+ public DataSource getDataSource() {
+ return this.dataSource;
+ }
+
+}
+++ /dev/null
-package com.prueba.core.web.application.context;
-
-import javax.sql.DataSource;
-
-import com.prueba.core.context.ApplicationContext;
-import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext;
-
-
-public class ApplicationWebContext implements ApplicationContext {
- private final DataSource dataSource;
-
- private ApplicationWebContext() {
- this.dataSource = DoDataSourceContext.getInstance().getDataSource();
- }
-
- private static class ApplicationWebContextHolder {
- private static final ApplicationContext INSTANCE = new ApplicationWebContext();
- }
-
- public static ApplicationContext getInstance() {
- return ApplicationWebContextHolder.INSTANCE;
- }
-
- @Override
- public DataSource getDataSource() {
- return this.dataSource;
- }
-
-}
private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE";
@Test
- public void whenLoadLiquibaseContextThenReturnSuccessFulQuery() {
+ public void whenLoadLiquibaseContextThenReturnResultsFromDatabase() {
final DataSource dataSource = DoDataSourceContext.getInstance().getDataSource();
final LiquibaseContext liquibaseContext = new LiquibaseContext(dataSource);
liquibaseContext.init();