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