Package ejava.util.jpa
Class DBUtil
- java.lang.Object
-
- ejava.util.jpa.DBUtil
-
public class DBUtil extends Object
This class will execute a set of drop and create DDL scripts against a supplied entity manager.
-
-
Field Summary
Fields Modifier and Type Field Description protected List<String>createPathsprotected List<String>dropPathsprotected EntityManagerem(package private) static org.slf4j.Loggerlogger
-
Constructor Summary
Constructors Constructor Description DBUtil()DBUtil(EntityManager em, String dropPath, String createPath)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCreatePath(String createPath)voidaddDropPath(String dropPath)intcreateAll(Map<String,Number> sequenceVals)Execute the create script against the DB.Map<String,Number>dropAll()Execute the drop script against the DB.intexecuteScript(String path)Execute the SQL statements contained within the resource that is located by the path supplied.protected InputStreamgetInputStream(String path)Locate the input stream either as a file path or a resource path.Map<String,Number>getSequenceNextVals()Return the current nextVal for each sequence used by application.protected List<String>getStatements(String contents)Get list of SQL statements from the supplied string.protected StringgetString(InputStream is)Turn the InputStream into a String for easier parsing for SQL statements.voidsetEntityManager(EntityManager em)voidsetSequenceNextVals(Map<String,Number> sequenceVals)Updates the state of the provided sequence(s) to values provided
-
-
-
Field Detail
-
logger
static org.slf4j.Logger logger
-
em
protected EntityManager em
-
-
Constructor Detail
-
DBUtil
public DBUtil()
-
DBUtil
public DBUtil(EntityManager em, String dropPath, String createPath)
-
-
Method Detail
-
setEntityManager
public void setEntityManager(EntityManager em)
-
addDropPath
public void addDropPath(String dropPath)
-
addCreatePath
public void addCreatePath(String createPath)
-
getInputStream
protected InputStream getInputStream(String path) throws IllegalArgumentException
Locate the input stream either as a file path or a resource path.- Parameters:
path-- Returns:
- input stream
- Throws:
IllegalArgumentException- if path not found
-
getString
protected String getString(InputStream is) throws IOException
Turn the InputStream into a String for easier parsing for SQL statements.- Parameters:
is-- Returns:
- string contents of SQL file
- Throws:
IOException
-
getStatements
protected List<String> getStatements(String contents)
Get list of SQL statements from the supplied string.- Parameters:
contents-- Returns:
- list of distinct statements
- Throws:
Exception
-
executeScript
public int executeScript(String path)
Execute the SQL statements contained within the resource that is located by the path supplied. This path can be either a file path or resource path.- Parameters:
path-- Returns:
- count of statements executed
- Throws:
Exception
-
getSequenceNextVals
public Map<String,Number> getSequenceNextVals()
Return the current nextVal for each sequence used by application.- Returns:
- map of sequence nextVals
-
setSequenceNextVals
public void setSequenceNextVals(Map<String,Number> sequenceVals)
Updates the state of the provided sequence(s) to values provided- Parameters:
sequenceVals-
-
dropAll
public Map<String,Number> dropAll() throws RuntimeException
Execute the drop script against the DB.- Returns:
- map of sequence previous nextVal state
- Throws:
RuntimeException- on errors like schema does not currently exist. In that case the caller must rollback the current transaction and begin a new one for the create();
-
createAll
public int createAll(Map<String,Number> sequenceVals) throws RuntimeException
Execute the create script against the DB. Follow-up by altering the provided sequences to be their previous nextVal state. This is necessary when resetting schema while JPA provider has cached a block of sequence values from a previous execution.- Returns:
- count of statements executed
- Throws:
RuntimeException
-
-