From b17515229a5d7e28a87dd769336aec3cb8aaa3a4 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Wed, 28 Feb 2018 11:01:06 +0100 Subject: [PATCH] Added opened database check. Renamed remove method with delete. --- .../core/UnopenedDatabaseException.java | 64 ++ .../sikevadb/filetree/FileTreeSikevaDB.java | 8 +- src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java | 931 ++++++++++-------- .../filetree/TreeFileSikevaDBTest.java | 6 +- .../devinsy/sikevadb/sql/SQLSikevaDBTest.java | 6 +- 5 files changed, 599 insertions(+), 416 deletions(-) create mode 100644 src/fr/devinsy/sikevadb/core/UnopenedDatabaseException.java diff --git a/src/fr/devinsy/sikevadb/core/UnopenedDatabaseException.java b/src/fr/devinsy/sikevadb/core/UnopenedDatabaseException.java new file mode 100644 index 0000000..937c420 --- /dev/null +++ b/src/fr/devinsy/sikevadb/core/UnopenedDatabaseException.java @@ -0,0 +1,64 @@ +/** + * Copyright (C) 2018 Christian Pierre MOMON + * + * This file is part of SikevaDB, simple key value database. + * + * SikevaDB is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * SikevaDB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with SikevaDB. If not, see . + */ +package fr.devinsy.sikevadb.core; + +/** + * + * @author Christian Pierre MOMON (christian.momon@devinsy.fr) + */ +public class UnopenedDatabaseException extends SikevaDBException +{ + private static final long serialVersionUID = 8364599416669077052L; + + /** + * + */ + public UnopenedDatabaseException() + { + super(); + } + + /** + * + * @param message + */ + public UnopenedDatabaseException(final String message) + { + super(message); + } + + /** + * + * @param message + * @param cause + */ + public UnopenedDatabaseException(final String message, final Throwable cause) + { + super(message, cause); + } + + /** + * + * @param cause + */ + public UnopenedDatabaseException(final Throwable cause) + { + super(cause); + } +} diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java index ddd8d13..9a00e99 100644 --- a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java @@ -1005,7 +1005,7 @@ public class FileTreeSikevaDB implements SikevaDB * @see fr.devinsy.sikevadb.core.SikevaDB#remove(java.lang.String) */ @Override - public void remove(final String key) throws SikevaDBException + public void delete(final String key) throws SikevaDBException { if (this.status == Status.CLOSED) { @@ -1038,7 +1038,7 @@ public class FileTreeSikevaDB implements SikevaDB * @see fr.devinsy.sikevadb.core.SikevaDB#remove(java.lang.String, java.lang.String) */ @Override - public void remove(final String key, final String subkey) throws SikevaDBException + public void delete(final String key, final String subkey) throws SikevaDBException { if (this.status == Status.CLOSED) { @@ -1072,7 +1072,7 @@ public class FileTreeSikevaDB implements SikevaDB * @see fr.devinsy.sikevadb.core.SikevaDB#removeMany(java.lang.String, java.lang.String[]) */ @Override - public void removeMany(final String key, final String... subkeys) throws SikevaDBException + public void deleteMany(final String key, final String... subkeys) throws SikevaDBException { if (this.status == Status.CLOSED) { @@ -1088,7 +1088,7 @@ public class FileTreeSikevaDB implements SikevaDB { for (String subkey : subkeys) { - remove(key, subkey); + delete(key, subkey); } } } diff --git a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java index 151dae2..61936a3 100644 --- a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java @@ -40,6 +40,7 @@ import fr.devinsy.sikevadb.core.Element; import fr.devinsy.sikevadb.core.Elements; import fr.devinsy.sikevadb.core.SikevaDB; import fr.devinsy.sikevadb.core.SikevaDBException; +import fr.devinsy.sikevadb.core.UnopenedDatabaseException; import fr.devinsy.util.strings.StringList; /** @@ -115,31 +116,37 @@ public class SQLSikevaDB implements SikevaDB } } - /** - * This methods clear all data in current opened database. - * + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#clear() */ @Override public void clear() throws SikevaDBException { - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (this.status == Status.CLOSED) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements"); + throw new UnopenedDatabaseException(); + } + else + { + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements"); - statement.executeUpdate(); - } - catch (SQLException exception) - { - logger.error("SQL Error getting connection.", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + statement.executeUpdate(); + } + catch (SQLException exception) + { + logger.error("SQL Error getting connection.", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } } @@ -174,10 +181,14 @@ public class SQLSikevaDB implements SikevaDB } /** - * + * Close quietly. + * * @param connection + * the connection * @param statement + * the statement * @param resultSet + * the result set */ private void closeQuietly(final Connection connection, final Statement statement, final ResultSet resultSet) { @@ -221,50 +232,61 @@ public class SQLSikevaDB implements SikevaDB } } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#countOfElements() */ @Override public long countOfElements() throws SikevaDBException { long result; - Connection connection = null; - Statement statement = null; - ResultSet resultSet = null; - try + if (this.status == Status.CLOSED) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.createStatement(); - resultSet = statement.executeQuery("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); + throw new UnopenedDatabaseException(); + } + else + { + Connection connection = null; + Statement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.createStatement(); + resultSet = statement.executeQuery("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); - resultSet.next(); - result = resultSet.getLong(1); - } - catch (SQLException exception) - { - logger.error("Error counting elements.", exception); - throw new SikevaDBException("Error counting elements", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + resultSet.next(); + result = resultSet.getLong(1); + } + catch (SQLException exception) + { + logger.error("Error counting elements.", exception); + throw new SikevaDBException("Error counting elements", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } // return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#countOfElements(java.lang.String) */ @Override public long countOfElements(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."); } @@ -299,15 +321,19 @@ public class SQLSikevaDB implements SikevaDB return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#countOfElements(java.lang.String, java.lang.String) */ @Override public long countOfElements(final String key, final String subkey) 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."); } @@ -423,94 +449,14 @@ public class SQLSikevaDB implements SikevaDB } /** - * - * @param id - * @return - * @throws SikevaDBException + * {@inheritDoc} */ - public boolean exists(final Element element) throws SikevaDBException + @Override + public void delete(final String key) throws SikevaDBException { - boolean result; - - if (element == null) + if (key == null) { - result = false; - } - else if (getElement(element.getId()) == null) - { - result = false; - } - else - { - result = true; - } - - // - return result; - } - - /** - * - * @return - * @throws SikevaDBException - */ - public Connection getConnection() throws SikevaDBException - { - Connection result; - - if (this.singleConnection != null) - { - result = this.singleConnection; - } - else if (this.dataSource != null) - { - try - { - result = this.dataSource.getConnection(); - } - catch (SQLException exception) - { - logger.error("Error counting elements.", exception); - throw new SikevaDBException("Error counting elements", exception); - } - } - else - { - throw new IllegalArgumentException("Connection is not initialized."); - } - - // - return result; - } - - /** - * - * @return - */ - public String getContextName() - { - return this.contextName; - } - - /** - * - * @return - */ - public String getDriverClassname() - { - return this.driverClassname; - } - - /** - * - */ - public Element getElement(final long id) throws SikevaDBException - { - Element result; - - if (id == Element.NO_ID) - { - result = null; + throw new IllegalArgumentException("Key is null."); } else { @@ -523,62 +469,342 @@ public class SQLSikevaDB implements SikevaDB // connection = getConnection(); connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ID=?"); - statement.setLong(1, id); - resultSet = statement.executeQuery(); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); - // - if (resultSet.next()) + statement.setString(1, key); + + int rowCount = statement.executeUpdate(); + + if (rowCount == 0) { - // - result = new Element(); - - result.setId(resultSet.getLong(1)); - result.setKey(resultSet.getString(2)); - result.setSubkey(resultSet.getString(3)); - result.setValue(resultSet.getString(4)); - result.setSize(resultSet.getLong(5)); - result.setDigest(resultSet.getString(6)); - result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); - result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); - result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); - - // - if (resultSet.next()) - { - throw new SikevaDBException("More than only once result [id=" + id + "]."); - } - } - else - { - result = null; + logger.warn("Remove action without existing target [key=" + key + "]"); } } catch (SQLException exception) { - logger.error("Error getting element.", exception); - throw new SikevaDBException("Error getting element", exception); + logger.error("Error removing element.", exception); + try + { + connection.rollback(); + } + catch (SQLException exception1) + { + logger.error("Rollback failed.", exception); + } + throw new SikevaDBException("Error removing element", exception); } finally { closeQuietly(connection, statement, resultSet); } } + } + + /** + * {@inheritDoc} + */ + @Override + public void delete(final String key, final String subkey) throws SikevaDBException + { + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); + + statement.setString(1, key); + statement.setString(2, subkey); + + int rowCount = statement.executeUpdate(); + + if (rowCount == 0) + { + logger.warn("Remove action without existing target [key=" + key + "][subkey=" + subkey + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error removing subkey.", exception); + throw new SikevaDBException("Error removing subkey", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void deleteMany(final String key, final String... subkeys) throws SikevaDBException + { + 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; + try + { + // + connection = getConnection(); + + // + String questionMarks = new StringList(subkeys.length).append('?').repeatLast(subkeys.length - 1).toStringWithCommas(); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IN (" + questionMarks + ") AND ARCHIVE_DATE IS NULL"); + // + statement.setString(1, key); + + // + for (int index = 0; index < subkeys.length; index++) + { + statement.setString(2 + index, subkeys[index]); + } + + // + int rowCount = statement.executeUpdate(); + if (rowCount == 0) + { + logger.warn("Remove action without existing target [key=" + key + "][subkeys=" + new StringList(subkeys).toStringWithCommas() + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error removing subkeys.", exception); + throw new SikevaDBException("Error removing subkeys", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } + } + + /** + * Exists. + * + * @param element + * the element + * @return true, if successful + * @throws SikevaDBException + * the sikeva DB exception + */ + public boolean exists(final Element element) throws SikevaDBException + { + boolean result; + + if (this.status == Status.CLOSED) + { + throw new UnopenedDatabaseException(); + } + else + { + + if (element == null) + { + result = false; + } + else if (getElement(element.getId()) == null) + { + result = false; + } + else + { + result = true; + } + } // return result; } /** - * {@inheritDoc} + * Gets the connection. + * + * @return the connection + * @throws SikevaDBException + * the sikeva DB exception + */ + public Connection getConnection() throws SikevaDBException + { + Connection result; + + if (this.status == Status.CLOSED) + { + throw new UnopenedDatabaseException(); + } + else + { + if (this.singleConnection != null) + { + result = this.singleConnection; + } + else if (this.dataSource != null) + { + try + { + result = this.dataSource.getConnection(); + } + catch (SQLException exception) + { + logger.error("Error counting elements.", exception); + throw new SikevaDBException("Error counting elements", exception); + } + } + else + { + throw new IllegalArgumentException("Connection is not initialized."); + } + } + + // + return result; + } + + /** + * Gets the context name. + * + * @return the context name + */ + public String getContextName() + { + String result; + + result = this.contextName; + + // + return result; + } + + /** + * Gets the driver classname. + * + * @return the driver classname + */ + public String getDriverClassname() + { + String result; + + result = this.driverClassname; + + // + return result; + } + + /** + * Gets the element. + * + * @param id + * the id + * @return the element + * @throws SikevaDBException + * the sikeva DB exception + */ + public Element getElement(final long id) throws SikevaDBException + { + Element result; + + if (this.status == Status.CLOSED) + { + throw new UnopenedDatabaseException(); + } + else + { + if (id == Element.NO_ID) + { + result = null; + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ID=?"); + statement.setLong(1, id); + resultSet = statement.executeQuery(); + + // + if (resultSet.next()) + { + // + result = new Element(); + + result.setId(resultSet.getLong(1)); + result.setKey(resultSet.getString(2)); + result.setSubkey(resultSet.getString(3)); + result.setValue(resultSet.getString(4)); + result.setSize(resultSet.getLong(5)); + result.setDigest(resultSet.getString(6)); + result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); + result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); + result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + + // + if (resultSet.next()) + { + throw new SikevaDBException("More than only once result [id=" + id + "]."); + } + } + else + { + result = null; + } + } + catch (SQLException exception) + { + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } + } + + // + return result; + } + + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#getElement(java.lang.String) */ @Override public Element getElement(final String key) throws SikevaDBException { Element result; - // - if (key == null) + if (this.status == Status.CLOSED) + { + throw new UnopenedDatabaseException(); + } + else if (key == null) { throw new IllegalArgumentException("Key is null."); } @@ -640,16 +866,19 @@ public class SQLSikevaDB implements SikevaDB return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#getElement(java.lang.String, java.lang.String) */ @Override public Element getElement(final String key, final String subkey) throws SikevaDBException { Element result; - // - if (key == null) + if (this.status == Status.CLOSED) + { + throw new UnopenedDatabaseException(); + } + else if (key == null) { throw new IllegalArgumentException("Key is null."); } @@ -717,72 +946,82 @@ public class SQLSikevaDB implements SikevaDB return result; } - /** - * + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#getElements() */ @Override public Elements getElements() throws SikevaDBException { Elements result; - // - result = new Elements((int) countOfElements()); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (this.status == Status.CLOSED) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection - .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL ORDER BY CREATION_DATE ASC"); - resultSet = statement.executeQuery(); + throw new UnopenedDatabaseException(); + } + else + { + // + result = new Elements((int) countOfElements()); - while (resultSet.next()) + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { - // - Element element = new Element(); + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL ORDER BY CREATION_DATE ASC"); + resultSet = statement.executeQuery(); - element.setId(resultSet.getLong(1)); - element.setKey(resultSet.getString(2)); - element.setSubkey(resultSet.getString(3)); - element.setValue(resultSet.getString(4)); - element.setSize(resultSet.getLong(5)); - element.setDigest(resultSet.getString(6)); - element.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); - element.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); - element.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + while (resultSet.next()) + { + // + Element element = new Element(); - // - result.add(element); + element.setId(resultSet.getLong(1)); + element.setKey(resultSet.getString(2)); + element.setSubkey(resultSet.getString(3)); + element.setValue(resultSet.getString(4)); + element.setSize(resultSet.getLong(5)); + element.setDigest(resultSet.getString(6)); + element.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); + element.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); + element.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + + // + result.add(element); + } + } + catch (SQLException exception) + { + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); } - } - catch (SQLException exception) - { - logger.error("Error getting element.", exception); - throw new SikevaDBException("Error getting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); } // return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#getElements(java.lang.String) */ @Override public Elements getElements(final String key) throws SikevaDBException { Elements result; - // - if (key == null) + if (this.status == Status.CLOSED) + { + throw new UnopenedDatabaseException(); + } + else if (key == null) { throw new IllegalArgumentException("Key is null."); } @@ -838,41 +1077,48 @@ public class SQLSikevaDB implements SikevaDB return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#getKeys() */ @Override public StringList getKeys() throws SikevaDBException { StringList result; - // - result = new StringList(); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (this.status == Status.CLOSED) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT DISTINCT TOPKEY FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); - resultSet = statement.executeQuery(); + throw new UnopenedDatabaseException(); + } + else + { + // + result = new StringList(); - 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 DISTINCT TOPKEY FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); + resultSet = statement.executeQuery(); + + while (resultSet.next()) + { + result.add(resultSet.getString(1)); + } + } + catch (SQLException exception) + { + logger.error("Error getting keys.", exception); + throw new SikevaDBException("Error getting keys", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); } - } - catch (SQLException exception) - { - logger.error("Error getting keys.", exception); - throw new SikevaDBException("Error getting keys", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); } // @@ -880,33 +1126,48 @@ public class SQLSikevaDB implements SikevaDB } /** - * - * @return + * Gets the login. + * + * @return the login */ public String getLogin() { - return this.login; + String result; + + result = this.login; + + // + return result; } /** - * - * @return + * Gets the password. + * + * @return the password */ public String getPassword() { - return this.password; + String result; + + result = this.password; + + // + return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#getSubkeys(java.lang.String) */ @Override public StringList getSubkeys(final String key) throws SikevaDBException { StringList result; - // - if (key == null) + if (this.status == Status.CLOSED) + { + throw new UnopenedDatabaseException(); + } + else if (key == null) { throw new IllegalArgumentException("Key is null."); } @@ -949,23 +1210,33 @@ public class SQLSikevaDB implements SikevaDB } /** - * - * @return + * Gets the url. + * + * @return the url */ public String getUrl() { - return this.url; + String result; + + result = this.url; + + // + return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#getValue(java.lang.String) */ @Override public String getValue(final String key) 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."); } @@ -1009,7 +1280,7 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } - + getValue("", ""); // return result; } @@ -1675,158 +1946,6 @@ public class SQLSikevaDB implements SikevaDB } } - /** - * {@inheritDoc} - */ - @Override - public void remove(final String key) throws SikevaDBException - { - if (key == null) - { - throw new IllegalArgumentException("Key is null."); - } - else - { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); - - statement.setString(1, key); - - int rowCount = statement.executeUpdate(); - - if (rowCount == 0) - { - logger.warn("Remove action without existing target [key=" + key + "]"); - } - } - catch (SQLException exception) - { - logger.error("Error removing element.", exception); - try - { - connection.rollback(); - } - catch (SQLException exception1) - { - logger.error("Rollback failed.", exception); - } - throw new SikevaDBException("Error removing element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } - } - } - - /** - * {@inheritDoc} - */ - @Override - public void remove(final String key, final String subkey) throws SikevaDBException - { - if (key == null) - { - throw new IllegalArgumentException("Key is null."); - } - else if (subkey == null) - { - throw new IllegalArgumentException("Subkey is null."); - } - else - { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); - - statement.setString(1, key); - statement.setString(2, subkey); - - int rowCount = statement.executeUpdate(); - - if (rowCount == 0) - { - logger.warn("Remove action without existing target [key=" + key + "][subkey=" + subkey + "]"); - } - } - catch (SQLException exception) - { - logger.error("Error removing subkey.", exception); - throw new SikevaDBException("Error removing subkey", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } - } - } - - /** - * {@inheritDoc} - */ - @Override - public void removeMany(final String key, final String... subkeys) throws SikevaDBException - { - 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; - try - { - // - connection = getConnection(); - - // - String questionMarks = new StringList(subkeys.length).append('?').repeatLast(subkeys.length - 1).toStringWithCommas(); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IN (" + questionMarks + ") AND ARCHIVE_DATE IS NULL"); - // - statement.setString(1, key); - - // - for (int index = 0; index < subkeys.length; index++) - { - statement.setString(2 + index, subkeys[index]); - } - - // - int rowCount = statement.executeUpdate(); - if (rowCount == 0) - { - logger.warn("Remove action without existing target [key=" + key + "][subkeys=" + new StringList(subkeys).toStringWithCommas() + "]"); - } - } - catch (SQLException exception) - { - logger.error("Error removing subkeys.", exception); - throw new SikevaDBException("Error removing subkeys", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } - } - } - /** * {@inheritDoc} */ @@ -1891,8 +2010,8 @@ public class SQLSikevaDB implements SikevaDB } } - /** - * {@inheritDoc} + /* (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 diff --git a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java index c4aedf2..bb880a8 100644 --- a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java @@ -501,7 +501,7 @@ public class TreeFileSikevaDBTest Assert.assertEquals(12, database.countOfElements()); - database.removeMany("alpha01", "bravo", "delta", "fox"); + database.deleteMany("alpha01", "bravo", "delta", "fox"); Assert.assertEquals(9, database.countOfElements()); @@ -560,14 +560,14 @@ public class TreeFileSikevaDBTest database.put("alpha03", "qlskjfmlqj"); database.put("alpha04", "qlskjfmlqj"); database.put("alpha05", "qlskjfmlqj"); - database.remove("alpha03"); + database.delete("alpha03"); database.put("alpha01s", "bravo1", "qlskjfmlqja"); database.put("alpha01s", "bravo1", "qlskjfmlqjb"); database.put("alpha01s", "bravo2", "qlskjfmlqj"); database.put("alpha01s", "bravo3", "qlskjfmlqj"); database.put("alpha01s", "bravo4", "qlskjfmlqj"); database.put("alpha01s", "bravo5", "qlskjfmlqj"); - database.remove("alpha01s", "bravo3"); + database.delete("alpha01s", "bravo3"); // System.out.println(database.countOfElements() + " " + // database.countOfArchivedElements() + " " + diff --git a/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java b/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java index bd4132e..800ee21 100644 --- a/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java @@ -566,7 +566,7 @@ public class SQLSikevaDBTest Assert.assertEquals(12, database.countOfElements()); - database.removeMany("alpha01", "bravo", "delta", "fox"); + database.deleteMany("alpha01", "bravo", "delta", "fox"); Assert.assertEquals(9, database.countOfElements()); @@ -625,14 +625,14 @@ public class SQLSikevaDBTest database.put("alpha03", "qlskjfmlqj"); database.put("alpha04", "qlskjfmlqj"); database.put("alpha05", "qlskjfmlqj"); - database.remove("alpha03"); + database.delete("alpha03"); database.put("alpha01", "bravo1", "qlskjfmlqja"); database.put("alpha01", "bravo1", "qlskjfmlqjb"); database.put("alpha01", "bravo2", "qlskjfmlqj"); database.put("alpha01", "bravo3", "qlskjfmlqj"); database.put("alpha01", "bravo4", "qlskjfmlqj"); database.put("alpha01", "bravo5", "qlskjfmlqj"); - database.remove("alpha01", "bravo3"); + database.delete("alpha01", "bravo3"); // System.out.println(database.countOfElements() + " " + // database.countOfArchivedElements() + " " +