Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement database delegate abstraction for ScriptUtils #551

Closed
wants to merge 3 commits into from
Closed
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
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