-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#551: Implement database delegate abstraction for ScriptUtils
Supports #525
- Loading branch information
Showing
12 changed files
with
593 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
55 changes: 55 additions & 0 deletions
55
.../database-commons/src/main/java/org/testcontainers/delegate/AbstractDatabaseDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
31 changes: 31 additions & 0 deletions
31
modules/database-commons/src/main/java/org/testcontainers/delegate/DatabaseDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
13 changes: 13 additions & 0 deletions
13
...abase-commons/src/main/java/org/testcontainers/exception/ConnectionCreationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.