diff --git a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java index 61936a3..e5ab1d3 100644 --- a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java @@ -80,12 +80,16 @@ public class SQLSikevaDB implements SikevaDB } /** - * This method opens a database session. - * - * @param host - * @param port + * Instantiates a new SQLSikevaDB. + * + * @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,42 +1359,49 @@ 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; - // - result = new StringList((int) countOfElements(key)); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (this.status == Status.CLOSED) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC"); - statement.setString(1, key); - resultSet = statement.executeQuery(); + throw new UnopenedDatabaseException(); + } + else + { + // + result = new StringList((int) countOfElements(key)); - while (resultSet.next()) + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { - result.add(resultSet.getString(1)); + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) + { + result.add(resultSet.getString(1)); + } + } + catch (SQLException exception) + { + logger.error("Error getting values.", exception); + throw new SikevaDBException("Error getting values", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); } - } - catch (SQLException exception) - { - logger.error("Error getting values.", exception); - throw new SikevaDBException("Error getting values", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); } // @@ -1390,7 +1409,9 @@ public class SQLSikevaDB implements SikevaDB } /** - * + * 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,51 +1488,61 @@ 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; - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (this.status == Status.CLOSED) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); - resultSet = statement.executeQuery(); + throw new UnopenedDatabaseException(); + } + else + { + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); + resultSet = statement.executeQuery(); - resultSet.next(); - result = resultSet.getLong(1); - } - catch (SQLException exception) - { - logger.error("Error computing memory size.", exception); - throw new SikevaDBException("Error computing memory size", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + resultSet.next(); + result = resultSet.getLong(1); + } + catch (SQLException exception) + { + logger.error("Error computing memory size.", exception); + throw new SikevaDBException("Error computing memory size", exception); + } + finally + { + 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,93 +1813,104 @@ 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 { - // - Element element = getElement(key); - - // - if (element == null) + if (this.status == Status.CLOSED) { - element = new Element(); - element.setKey(key); - element.update(value); - element.setArchiveDate(null); + throw new UnopenedDatabaseException(); } else - { - element.update(value); - } - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { // - connection = getConnection(); - connection.setAutoCommit(false); + Element element = getElement(key); - // Archive existing element. - if (element.getId() != Element.NO_ID) + // + if (element == null) { - statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); - - statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); - statement.setString(2, element.getKey()); - - statement.executeUpdate(); - statement.clearParameters(); - statement.close(); + element = new Element(); + element.setKey(key); + element.update(value); + element.setArchiveDate(null); + } + else + { + element.update(value); } - // Insert new version of the element. - statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); - - statement.setString(1, element.getKey()); - statement.setString(2, element.getSubkey()); - statement.setString(3, element.getValue()); - statement.setLong(4, element.getSize()); - statement.setString(5, element.getDigest()); - statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); - statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); - statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); - - statement.executeUpdate(); - - connection.commit(); - } - catch (SQLException exception) - { - logger.error("Error putting element.", exception); + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; try { - connection.rollback(); + // + connection = getConnection(); + connection.setAutoCommit(false); + + // Archive existing element. + if (element.getId() != Element.NO_ID) + { + statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); + + statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); + statement.setString(2, element.getKey()); + + statement.executeUpdate(); + statement.clearParameters(); + statement.close(); + } + + // Insert new version of the element. + statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); + + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + statement.setString(3, element.getValue()); + statement.setLong(4, element.getSize()); + statement.setString(5, element.getDigest()); + statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); + statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); + statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); + + statement.executeUpdate(); + + connection.commit(); } - catch (SQLException exception1) + catch (SQLException exception) { - logger.error("Rollback failed.", exception); + logger.error("Error putting element.", exception); + try + { + connection.rollback(); + } + catch (SQLException exception1) + { + logger.error("Rollback failed.", exception); + } + throw new SikevaDBException("Error putting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); } - throw new SikevaDBException("Error putting element", exception); - } - finally - { - 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,19 +2049,26 @@ 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 ((key == null) || (oldKey == null) || (newKey == null)) + if (this.status == Status.CLOSED) { - throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "]."); + throw new UnopenedDatabaseException(); } else { - // TODO Auto-generated method stub + if ((key == null) || (oldKey == null) || (newKey == null)) + { + throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "]."); + } + else + { + // TODO Auto-generated method stub + } } } @@ -2016,64 +2078,77 @@ public class SQLSikevaDB implements SikevaDB @Override public void replaceInValue(final String key, final String... tokens) throws SikevaDBException { - logger.info("replaceInValue starting... [{}]", key); - - // - String value = getValue(key); - - // - for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + if (this.status == Status.CLOSED) { - value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + throw new UnopenedDatabaseException(); } + else + { + logger.info("replaceInValue starting... [{}]", key); - // - put(key, value); + String value = getValue(key); - logger.info("replaceInValue done."); + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + { + value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(key, value); + + 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 { - logger.info("replaceInValues starting... [{}]", key); - - // - Elements elements = getElements(key); - - long count = 0; - for (Element element : elements) + if (this.status == Status.CLOSED) { - // - logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size()); - - if (element.getSubkey() != null) - { - // - count += 1; - - // - String value = element.getValue(); - - // - for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) - { - value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); - } - - // - put(element.getKey(), element.getSubkey(), value); - } + throw new UnopenedDatabaseException(); } + else + { + logger.info("replaceInValues starting... [{}]", key); - logger.info("replaceInValues done."); + Elements elements = getElements(key); + + long count = 0; + for (Element element : elements) + { + logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size()); + + if (element.getSubkey() != null) + { + // + count += 1; + + // + String value = element.getValue(); + + // + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + { + value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(element.getKey(), element.getSubkey(), value); + } + } + + 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; } - }