Added opened database check. Reviewed code.

This commit is contained in:
Christian P. MOMON 2018-02-28 11:13:38 +01:00
parent b17515229a
commit a17ca91389

View file

@ -80,12 +80,16 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* This method opens a database session. * Instantiates a new SQLSikevaDB.
* *
* @param host * @param driverClassName
* @param port * the driver class name
* @param url
* the url
* @param login * @param login
* the login
* @param password * @param password
* the password
*/ */
public SQLSikevaDB(final String driverClassName, final String url, final String login, final String password) public SQLSikevaDB(final String driverClassName, final String url, final String login, final String password)
{ {
@ -150,14 +154,12 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* This method closes the current database session. * @see fr.devinsy.sikevadb.core.SikevaDB#close()
*
*/ */
@Override @Override
public void close() public void close()
{ {
//
if (this.singleConnection != null) if (this.singleConnection != null)
{ {
try try
@ -174,7 +176,6 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
//
this.dataSource = null; this.dataSource = null;
this.status = Status.CLOSED; this.status = Status.CLOSED;
@ -376,7 +377,6 @@ public class SQLSikevaDB implements SikevaDB
/** /**
* This method creates the schema (table) used by SQLSikevaDB. * This method creates the schema (table) used by SQLSikevaDB.
* *
* @throws IOException
* @throws SikevaDBException * @throws SikevaDBException
*/ */
@Override @Override
@ -448,13 +448,17 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#delete(java.lang.String)
*/ */
@Override @Override
public void delete(final String key) throws SikevaDBException public void delete(final String key) throws SikevaDBException
{ {
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -500,13 +504,17 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#delete(java.lang.String, java.lang.String)
*/ */
@Override @Override
public void delete(final String key, final String subkey) throws SikevaDBException public void delete(final String key, final String subkey) throws SikevaDBException
{ {
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -548,19 +556,22 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#deleteMany(java.lang.String, java.lang.String[])
*/ */
@Override @Override
public void deleteMany(final String key, final String... subkeys) throws SikevaDBException public void deleteMany(final String key, final String... subkeys) throws SikevaDBException
{ {
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
else if ((subkeys != null) && (subkeys.length > 0)) else if ((subkeys != null) && (subkeys.length > 0))
{ {
//
Connection connection = null; Connection connection = null;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -816,7 +827,6 @@ public class SQLSikevaDB implements SikevaDB
ResultSet resultSet = null; ResultSet resultSet = null;
try try
{ {
//
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
@ -824,7 +834,6 @@ public class SQLSikevaDB implements SikevaDB
statement.setString(1, key); statement.setString(1, key);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
//
if (resultSet.next()) if (resultSet.next())
{ {
// //
@ -1280,20 +1289,24 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet); closeQuietly(connection, statement, resultSet);
} }
} }
getValue("", "");
// //
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#getValue(java.lang.String, java.lang.String)
*/ */
@Override @Override
public String getValue(final String key, final String subkey) throws SikevaDBException public String getValue(final String key, final String subkey) throws SikevaDBException
{ {
String result; String result;
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -1303,7 +1316,6 @@ public class SQLSikevaDB implements SikevaDB
} }
else else
{ {
//
Connection connection = null; Connection connection = null;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -1347,14 +1359,20 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#getValues(java.lang.String)
*/ */
@Override @Override
public StringList getValues(final String key) throws SikevaDBException public StringList getValues(final String key) throws SikevaDBException
{ {
StringList result; StringList result;
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
// //
result = new StringList((int) countOfElements(key)); result = new StringList((int) countOfElements(key));
@ -1384,13 +1402,16 @@ public class SQLSikevaDB implements SikevaDB
{ {
closeQuietly(connection, statement, resultSet); closeQuietly(connection, statement, resultSet);
} }
}
// //
return result; return result;
} }
/** /**
* Checks if is archive off.
* *
* @return true, if is archive off
*/ */
public boolean isArchiveOff() public boolean isArchiveOff()
{ {
@ -1403,7 +1424,9 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Checks if is archive on.
* *
* @return true, if is archive on
*/ */
public boolean isArchiveOn() public boolean isArchiveOn()
{ {
@ -1415,8 +1438,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* * @see fr.devinsy.sikevadb.core.SikevaDB#isClosed()
*/ */
@Override @Override
public boolean isClosed() public boolean isClosed()
@ -1436,8 +1459,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#isCreated()
*/ */
@Override @Override
public boolean isCreated() throws SikevaDBException public boolean isCreated() throws SikevaDBException
@ -1451,8 +1474,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* * @see fr.devinsy.sikevadb.core.SikevaDB#isOpened()
*/ */
@Override @Override
public boolean isOpened() public boolean isOpened()
@ -1465,15 +1488,20 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#memorySize()
*/ */
@Override @Override
public long memorySize() throws SikevaDBException public long memorySize() throws SikevaDBException
{ {
long result; long result;
// if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
Connection connection = null; Connection connection = null;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -1496,20 +1524,25 @@ public class SQLSikevaDB implements SikevaDB
{ {
closeQuietly(connection, statement, resultSet); closeQuietly(connection, statement, resultSet);
} }
}
// //
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#memorySize(java.lang.String)
*/ */
@Override @Override
public long memorySize(final String key) throws SikevaDBException public long memorySize(final String key) throws SikevaDBException
{ {
long result; long result;
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -1544,8 +1577,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#memorySize(java.lang.String, java.lang.String)
*/ */
@Override @Override
public long memorySize(final String key, final String subkey) throws SikevaDBException public long memorySize(final String key, final String subkey) throws SikevaDBException
@ -1553,7 +1586,11 @@ public class SQLSikevaDB implements SikevaDB
long result; long result;
// //
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -1563,7 +1600,6 @@ public class SQLSikevaDB implements SikevaDB
} }
else else
{ {
//
Connection connection = null; Connection connection = null;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -1594,8 +1630,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#open()
*/ */
@Override @Override
public void open() throws SikevaDBException public void open() throws SikevaDBException
@ -1653,13 +1689,17 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#put(fr.devinsy.sikevadb.core.Element)
*/ */
@Override @Override
public void put(final Element element) throws SikevaDBException public void put(final Element element) throws SikevaDBException
{ {
if (element == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (element == null)
{ {
throw new IllegalArgumentException("element is null."); throw new IllegalArgumentException("element is null.");
} }
@ -1773,11 +1813,17 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String)
*/ */
@Override @Override
public void put(final String key, final String value) throws SikevaDBException public void put(final String key, final String value) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{ {
// //
Element element = getElement(key); Element element = getElement(key);
@ -1852,14 +1898,19 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet); closeQuietly(connection, statement, resultSet);
} }
} }
}
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String, java.lang.String)
*/ */
@Override @Override
public void put(final String key, final String subkey, final String value) throws SikevaDBException public void put(final String key, final String subkey, final String value) throws SikevaDBException
{ {
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -1946,15 +1997,19 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#renameKey(java.lang.String, java.lang.String)
*/ */
@Override @Override
public void renameKey(final String oldKey, final String newKey) throws SikevaDBException public void renameKey(final String oldKey, final String newKey) throws SikevaDBException
{ {
logger.info("renameKey starting... [{}][{}]", oldKey, newKey); logger.info("renameKey starting... [{}][{}]", oldKey, newKey);
if (oldKey == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (oldKey == null)
{ {
throw new IllegalArgumentException("OldKey is null."); throw new IllegalArgumentException("OldKey is null.");
} }
@ -1994,11 +2049,17 @@ public class SQLSikevaDB implements SikevaDB
logger.info("renameKey done."); logger.info("renameKey done.");
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#renameSubKey(java.lang.String, java.lang.String, java.lang.String)
*/ */
@Override @Override
public void renameSubKey(final String key, final String oldKey, final String newKey) throws SikevaDBException public void renameSubKey(final String key, final String oldKey, final String newKey) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{ {
if ((key == null) || (oldKey == null) || (newKey == null)) if ((key == null) || (oldKey == null) || (newKey == null))
{ {
@ -2009,19 +2070,24 @@ public class SQLSikevaDB implements SikevaDB
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
} }
}
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#replaceInValue(java.lang.String, java.lang.String[]) * @see fr.devinsy.sikevadb.core.SikevaDB#replaceInValue(java.lang.String, java.lang.String[])
*/ */
@Override @Override
public void replaceInValue(final String key, final String... tokens) throws SikevaDBException public void replaceInValue(final String key, final String... tokens) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{ {
logger.info("replaceInValue starting... [{}]", key); logger.info("replaceInValue starting... [{}]", key);
//
String value = getValue(key); String value = getValue(key);
//
for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2)
{ {
value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]);
@ -2032,22 +2098,27 @@ public class SQLSikevaDB implements SikevaDB
logger.info("replaceInValue done."); logger.info("replaceInValue done.");
} }
}
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#replaceInValues(java.lang.String, java.lang.String[])
*/ */
@Override @Override
public void replaceInValues(final String key, final String... tokens) throws SikevaDBException public void replaceInValues(final String key, final String... tokens) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{ {
logger.info("replaceInValues starting... [{}]", key); logger.info("replaceInValues starting... [{}]", key);
//
Elements elements = getElements(key); Elements elements = getElements(key);
long count = 0; long count = 0;
for (Element element : elements) for (Element element : elements)
{ {
//
logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size()); logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size());
if (element.getSubkey() != null) if (element.getSubkey() != null)
@ -2071,9 +2142,13 @@ public class SQLSikevaDB implements SikevaDB
logger.info("replaceInValues done."); logger.info("replaceInValues done.");
} }
}
/** /**
* Sets the archive flag.
* *
* @param value
* the new archive flag
*/ */
public void setArchiveFlag(final boolean value) public void setArchiveFlag(final boolean value)
{ {
@ -2081,8 +2156,10 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Sets the driver classname.
* *
* @param driverClassname * @param driverClassname
* the new driver classname
*/ */
public void setDriverClassname(final String driverClassname) public void setDriverClassname(final String driverClassname)
{ {
@ -2090,8 +2167,10 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Sets the login.
* *
* @param login * @param login
* the new login
*/ */
public void setLogin(final String login) public void setLogin(final String login)
{ {
@ -2099,8 +2178,10 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Sets the password.
* *
* @param password * @param password
* the new password
*/ */
public void setPassword(final String password) public void setPassword(final String password)
{ {
@ -2108,12 +2189,13 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Sets the url.
* *
* @param url * @param url
* the new url
*/ */
public void setUrl(final String url) public void setUrl(final String url)
{ {
this.url = url; this.url = url;
} }
} }