Skip to content

Commit

Permalink
#551: Implement database delegate abstraction for ScriptUtils
Browse files Browse the repository at this point in the history
Supports #525
  • Loading branch information
AnkBurov authored and rnorth committed Feb 3, 2018
1 parent 4edf9e2 commit 5db33e0
Show file tree
Hide file tree
Showing 12 changed files with 593 additions and 292 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log
All notable changes to this project will be documented in this file.

## UNRELEASED

### Changed
- Abstracted and changed database init script functionality to support use of SQL-like scripts with non-JDBC connections. ([\#551](https://github.com/testcontainers/testcontainers-java/pull/551))

## [1.6.0] - 2018-01-28

### Fixed
Expand Down
23 changes: 23 additions & 0 deletions modules/database-commons/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-parent</artifactId>
<version>0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>database-commons</artifactId>
<name>TestContainers :: Database-Commons</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>testcontainers</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.testcontainers.delegate;

import java.util.Collection;

/**
* @param <CONNECTION> connection to the database
* @author Eugeny Karpov
*/
public abstract class AbstractDatabaseDelegate<CONNECTION> implements DatabaseDelegate {

/**
* Database connection
*/
private CONNECTION connection;

private boolean isConnectionStarted = false;

/**
* Get or create new connection to the database
*/
protected CONNECTION getConnection() {
if (!isConnectionStarted) {
connection = createNewConnection();
isConnectionStarted = true;
}
return connection;
}

@Override
public void execute(Collection<String> statements, String scriptPath, boolean continueOnError, boolean ignoreFailedDrops) {
int lineNumber = 0;
for (String statement : statements) {
lineNumber++;
execute(statement, scriptPath, lineNumber, continueOnError, ignoreFailedDrops);
}
}

@Override
public void close() {
if (isConnectionStarted) {
closeConnectionQuietly(connection);
isConnectionStarted = false;
}
}

/**
* Quietly close the connection
*/
protected abstract void closeConnectionQuietly(CONNECTION connection);

/**
* Template method for creating new connections to the database
*/
protected abstract CONNECTION createNewConnection();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.testcontainers.delegate;

import java.util.Collection;

/**
* Database delegate
*
* Gives an abstraction from concrete database
*
* @author Eugeny Karpov
*/
public interface DatabaseDelegate extends AutoCloseable {

/**
* Execute statement by the implementation of the delegate
*/
void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops);

/**
* Execute collection of statements
*/
void execute(Collection<String> statements, String scriptPath, boolean continueOnError, boolean ignoreFailedDrops);

/**
* Close connection to the database
*
* Overridden to suppress throwing Exception
*/
@Override
void close();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.testcontainers.exception;

/**
* Inability to create connection to the database
*
* @author Eugeny Karpov
*/
public class ConnectionCreationException extends RuntimeException {

public ConnectionCreationException(String message, Throwable cause) {
super(message, cause);
}
}
Loading

0 comments on commit 5db33e0

Please sign in to comment.