From 03e8764a866b0184bae88891f20800e46b6a8ceb Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Wed, 11 Dec 2013 15:57:46 +0100 Subject: [PATCH] Fix archive bug. Add patch methods. Include ID. --- src/fr/devinsy/sikevadb/Element.java | 28 +- src/fr/devinsy/sikevadb/FileSikevaDB.java | 30 +- src/fr/devinsy/sikevadb/SQLSikevaDB.java | 469 +++++++++++++----- src/fr/devinsy/sikevadb/SikevaDB.java | 10 +- src/fr/devinsy/sikevadb/XMLSikevaDB.java | 18 +- test/fr/devinsy/sikevadb/SQLSikevaDBTest.java | 150 ++++++ 6 files changed, 568 insertions(+), 137 deletions(-) diff --git a/src/fr/devinsy/sikevadb/Element.java b/src/fr/devinsy/sikevadb/Element.java index 2d6ad4c..b50f709 100644 --- a/src/fr/devinsy/sikevadb/Element.java +++ b/src/fr/devinsy/sikevadb/Element.java @@ -25,6 +25,9 @@ import org.joda.time.DateTime; */ public class Element { + public static final long NO_ID = -1; + + private long id; private DateTime creationDate; private DateTime editionDate; private DateTime archiveDate; @@ -40,6 +43,7 @@ public class Element { * @param value */ public Element() { + this.id = NO_ID; } /** @@ -62,35 +66,39 @@ public class Element { } public DateTime getArchiveDate() { - return archiveDate; + return this.archiveDate; } public DateTime getCreationDate() { - return creationDate; + return this.creationDate; } public String getDigest() { - return digest; + return this.digest; } public DateTime getEditionDate() { - return editionDate; + return this.editionDate; + } + + public long getId() { + return this.id; } public String getKey() { - return key; + return this.key; } public long getSize() { - return size; + return this.size; } public String getSubkey() { - return subkey; + return this.subkey; } public String getValue() { - return value; + return this.value; } public void setArchiveDate(final DateTime date) { @@ -109,6 +117,10 @@ public class Element { this.editionDate = date; } + public void setId(final long id) { + this.id = id; + } + public void setKey(final String key) { this.key = key; } diff --git a/src/fr/devinsy/sikevadb/FileSikevaDB.java b/src/fr/devinsy/sikevadb/FileSikevaDB.java index b946eb2..6da6ff1 100644 --- a/src/fr/devinsy/sikevadb/FileSikevaDB.java +++ b/src/fr/devinsy/sikevadb/FileSikevaDB.java @@ -139,6 +139,12 @@ public class FileSikevaDB implements SikevaDB { return 0; } + @Override + public Elements getAllElements() throws Exception { + // TODO Auto-generated method stub + return null; + } + @Override public Elements getAllElements(final String key) throws Exception { // TODO Auto-generated method stub @@ -241,12 +247,6 @@ public class FileSikevaDB implements SikevaDB { return null; } - @Override - public StringList getValues(final String key, final String subkey) { - // TODO Auto-generated method stub - return null; - } - @Override public long memorySize() { // TODO Auto-generated method stub @@ -336,4 +336,22 @@ public class FileSikevaDB implements SikevaDB { // TODO Auto-generated method stub } + + @Override + public void renameKey(final String oldKey, final String newKey) throws Exception { + // TODO Auto-generated method stub + + } + + @Override + public void replaceInValue(final String key, final String... tokens) throws Exception { + // TODO Auto-generated method stub + + } + + @Override + public void replaceInValues(final String key, final String... tokens) throws Exception { + // TODO Auto-generated method stub + + } } diff --git a/src/fr/devinsy/sikevadb/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/SQLSikevaDB.java index 9fd39f0..aab155c 100644 --- a/src/fr/devinsy/sikevadb/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/SQLSikevaDB.java @@ -626,6 +626,74 @@ public class SQLSikevaDB implements SikevaDB { } } + /** + * + * @param id + * @return + * @throws SQLException + */ + public boolean exists(final Element element) throws SQLException { + boolean result; + + if (element == null) { + result = false; + } else if (getElement(element.getId()) == null) { + result = false; + } else { + result = true; + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public Elements getAllElements() throws SQLException { + Elements result; + + // + result = new Elements((int) countOfAllElements()); + + // + 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 ORDER BY ARCHIVE_DATE ASC"); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + // + Element element = new 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(toDateTime(resultSet.getTimestamp(7))); + element.setEditionDate(toDateTime(resultSet.getTimestamp(8))); + element.setArchiveDate(toDateTime(resultSet.getTimestamp(9))); + + // + result.add(element); + } + + } finally { + close(connection, statement, resultSet); + } + + // + return result; + } + /** * {@inheritDoc} */ @@ -644,21 +712,23 @@ public class SQLSikevaDB implements SikevaDB { connection = getConnection(); connection.setAutoCommit(true); statement = connection - .prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC"); + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC"); statement.setString(1, key); resultSet = statement.executeQuery(); while (resultSet.next()) { // Element element = new Element(); - element.setKey(resultSet.getString(1)); - element.setSubkey(resultSet.getString(2)); - element.setValue(resultSet.getString(3)); - element.setSize(resultSet.getLong(4)); - element.setDigest(resultSet.getString(5)); - element.setCreationDate(toDateTime(resultSet.getTimestamp(6))); - element.setEditionDate(toDateTime(resultSet.getTimestamp(7))); - element.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); + + 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(toDateTime(resultSet.getTimestamp(7))); + element.setEditionDate(toDateTime(resultSet.getTimestamp(8))); + element.setArchiveDate(toDateTime(resultSet.getTimestamp(9))); // result.add(element); @@ -694,7 +764,7 @@ public class SQLSikevaDB implements SikevaDB { connection = getConnection(); connection.setAutoCommit(true); statement = connection - .prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC"); + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC"); statement.setString(1, key); statement.setString(2, subkey); resultSet = statement.executeQuery(); @@ -702,14 +772,16 @@ public class SQLSikevaDB implements SikevaDB { while (resultSet.next()) { // Element element = new Element(); - element.setKey(resultSet.getString(1)); - element.setSubkey(resultSet.getString(2)); - element.setValue(resultSet.getString(3)); - element.setSize(resultSet.getLong(4)); - element.setDigest(resultSet.getString(5)); - element.setCreationDate(toDateTime(resultSet.getTimestamp(6))); - element.setEditionDate(toDateTime(resultSet.getTimestamp(7))); - element.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); + + 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(toDateTime(resultSet.getTimestamp(7))); + element.setEditionDate(toDateTime(resultSet.getTimestamp(8))); + element.setArchiveDate(toDateTime(resultSet.getTimestamp(9))); // result.add(element); @@ -972,6 +1044,61 @@ public class SQLSikevaDB implements SikevaDB { return this.driverClassname; } + /** + * + */ + public Element getElement(final long id) throws SQLException { + Element result; + + 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(toDateTime(resultSet.getTimestamp(7))); + result.setEditionDate(toDateTime(resultSet.getTimestamp(8))); + result.setArchiveDate(toDateTime(resultSet.getTimestamp(9))); + + } else { + result = null; + } + + // + if (resultSet.next()) { + throw new SQLException("More than only once result [id=" + id + "]."); + } + + } finally { + close(connection, statement, resultSet); + } + } + + // + return result; + } + /** * {@inheritDoc} */ @@ -988,7 +1115,7 @@ public class SQLSikevaDB implements SikevaDB { connection = getConnection(); connection.setAutoCommit(true); statement = connection - .prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); statement.setString(1, key); resultSet = statement.executeQuery(); @@ -996,14 +1123,16 @@ public class SQLSikevaDB implements SikevaDB { if (resultSet.next()) { // result = new Element(); - result.setKey(resultSet.getString(1)); - result.setSubkey(resultSet.getString(2)); - result.setValue(resultSet.getString(3)); - result.setSize(resultSet.getLong(4)); - result.setDigest(resultSet.getString(5)); - result.setCreationDate(toDateTime(resultSet.getTimestamp(6))); - result.setEditionDate(toDateTime(resultSet.getTimestamp(7))); - result.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); + + 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(toDateTime(resultSet.getTimestamp(7))); + result.setEditionDate(toDateTime(resultSet.getTimestamp(8))); + result.setArchiveDate(toDateTime(resultSet.getTimestamp(9))); } else { result = null; @@ -1042,7 +1171,7 @@ public class SQLSikevaDB implements SikevaDB { connection = getConnection(); connection.setAutoCommit(true); statement = connection - .prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); statement.setString(1, key); statement.setString(2, subkey); resultSet = statement.executeQuery(); @@ -1051,14 +1180,16 @@ public class SQLSikevaDB implements SikevaDB { if (resultSet.next()) { // result = new Element(); - result.setKey(resultSet.getString(1)); - result.setSubkey(resultSet.getString(2)); - result.setValue(resultSet.getString(3)); - result.setSize(resultSet.getLong(4)); - result.setDigest(resultSet.getString(5)); - result.setCreationDate(toDateTime(resultSet.getTimestamp(6))); - result.setEditionDate(toDateTime(resultSet.getTimestamp(7))); - result.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); + + 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(toDateTime(resultSet.getTimestamp(7))); + result.setEditionDate(toDateTime(resultSet.getTimestamp(8))); + result.setArchiveDate(toDateTime(resultSet.getTimestamp(9))); } else { result = null; } @@ -1095,21 +1226,23 @@ public class SQLSikevaDB implements SikevaDB { connection = getConnection(); connection.setAutoCommit(true); statement = connection - .prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC"); + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC"); statement.setString(1, key); resultSet = statement.executeQuery(); while (resultSet.next()) { // Element element = new Element(); - element.setKey(resultSet.getString(1)); - element.setSubkey(resultSet.getString(2)); - element.setValue(resultSet.getString(3)); - element.setSize(resultSet.getLong(4)); - element.setDigest(resultSet.getString(5)); - element.setCreationDate(toDateTime(resultSet.getTimestamp(6))); - element.setEditionDate(toDateTime(resultSet.getTimestamp(7))); - element.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); + + 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(toDateTime(resultSet.getTimestamp(7))); + element.setEditionDate(toDateTime(resultSet.getTimestamp(8))); + element.setArchiveDate(toDateTime(resultSet.getTimestamp(9))); // result.add(element); @@ -1145,7 +1278,7 @@ public class SQLSikevaDB implements SikevaDB { connection = getConnection(); connection.setAutoCommit(true); statement = connection - .prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY CREATION_DATE ASC"); + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY CREATION_DATE ASC"); statement.setString(1, key); statement.setString(2, subkey); resultSet = statement.executeQuery(); @@ -1153,14 +1286,16 @@ public class SQLSikevaDB implements SikevaDB { while (resultSet.next()) { // Element element = new Element(); - element.setKey(resultSet.getString(1)); - element.setSubkey(resultSet.getString(2)); - element.setValue(resultSet.getString(3)); - element.setSize(resultSet.getLong(4)); - element.setDigest(resultSet.getString(5)); - element.setCreationDate(toDateTime(resultSet.getTimestamp(6))); - element.setEditionDate(toDateTime(resultSet.getTimestamp(7))); - element.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); + + 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(toDateTime(resultSet.getTimestamp(7))); + element.setEditionDate(toDateTime(resultSet.getTimestamp(8))); + element.setArchiveDate(toDateTime(resultSet.getTimestamp(9))); // result.add(element); @@ -1358,7 +1493,7 @@ public class SQLSikevaDB implements SikevaDB { connection = getConnection(); connection.setAutoCommit(true); statement = connection - .prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC"); + .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(); @@ -1375,47 +1510,6 @@ public class SQLSikevaDB implements SikevaDB { return result; } - /** - * {@inheritDoc} - */ - @Override - public StringList getValues(final String key, final String subkey) throws SQLException { - StringList result; - - // - if (subkey == null) { - result = getValues(key); - } else { - // - result = new StringList((int) countOfElements(key, subkey)); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection - .prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC"); - statement.setString(1, key); - statement.setString(2, subkey); - resultSet = statement.executeQuery(); - - while (resultSet.next()) { - // - result.add(resultSet.getString(1)); - } - - } finally { - close(connection, statement, resultSet); - } - } - - // - return result; - } - /** * {@inheritDoc} */ @@ -1659,29 +1753,94 @@ public class SQLSikevaDB implements SikevaDB { */ @Override public void put(final Element element) throws SQLException { + // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection - .prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); + if (element == null) { + // + throw new IllegalArgumentException("element is null."); + } else { + // + if (exists(element)) { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + connection.setAutoCommit(true); - 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, toTimestamp(element.getCreationDate())); - statement.setTimestamp(7, toTimestamp(element.getEditionDate())); - statement.setTimestamp(8, toTimestamp(element.getArchiveDate())); + // Archive existing element. + statement = connection + .prepareStatement("UPDATE sikevadb_elements SET TOPKEY=?,SUBKEY=?,VALUE=?,SIZE=?,DIGEST=?,CREATION_DATE=?,EDITION_DATE=?,ARCHIVE_DATE=? WHERE ID=?"); - statement.executeUpdate(); + 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, toTimestamp(element.getCreationDate())); + statement.setTimestamp(7, toTimestamp(element.getEditionDate())); + statement.setTimestamp(8, toTimestamp(element.getArchiveDate())); + statement.setLong(9, element.getId()); - } finally { - close(connection, statement, resultSet); + statement.executeUpdate(); + + } finally { + close(connection, statement, resultSet); + } + } else if (element.getId() == Element.NO_ID) { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + connection.setAutoCommit(true); + 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, toTimestamp(element.getCreationDate())); + statement.setTimestamp(7, toTimestamp(element.getEditionDate())); + statement.setTimestamp(8, toTimestamp(element.getArchiveDate())); + + statement.executeUpdate(); + + } finally { + close(connection, statement, resultSet); + } + } else { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection + .prepareStatement("INSERT INTO sikevadb_elements (ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?,?, ?, ?, ?, ?, ?, ?, ?)"); + + statement.setLong(1, element.getId()); + statement.setString(2, element.getKey()); + statement.setString(3, element.getSubkey()); + statement.setString(4, element.getValue()); + statement.setLong(5, element.getSize()); + statement.setString(6, element.getDigest()); + statement.setTimestamp(7, toTimestamp(element.getCreationDate())); + statement.setTimestamp(8, toTimestamp(element.getEditionDate())); + statement.setTimestamp(9, toTimestamp(element.getArchiveDate())); + + statement.executeUpdate(); + + } finally { + close(connection, statement, resultSet); + } + } } } @@ -1717,7 +1876,7 @@ public class SQLSikevaDB implements SikevaDB { connection.setAutoCommit(false); // Archive existing element. - if (element.getCreationDate() != null) { + 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"); @@ -1786,7 +1945,7 @@ public class SQLSikevaDB implements SikevaDB { connection.setAutoCommit(false); // Archive existing element. - if (element.getCreationDate() != null) { + if (element.getId() != Element.NO_ID) { statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); statement.setTimestamp(1, toTimestamp(element.getEditionDate())); @@ -1924,6 +2083,82 @@ public class SQLSikevaDB implements SikevaDB { } } + /** + * {@inheritDoc} + */ + @Override + public void renameKey(final String oldKey, final String newKey) throws SQLException { + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { + // + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("UPDATE sikevadb_elements SET TOPKEY=? WHERE TOPKEY=?"); + + statement.setString(1, newKey); + statement.setString(2, oldKey); + + statement.executeUpdate(); + + } finally { + close(connection, statement, resultSet); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void replaceInValue(final String key, final String... tokens) throws SQLException { + + // + String value = getValue(key); + + // + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) { + value = value.replace(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(key, value); + } + + /** + * {@inheritDoc} + */ + @Override + public void replaceInValues(final String key, final String... tokens) throws SQLException { + + // + Elements elements = getElements(key); + + long count = 0; + for (Element element : elements) { + // + System.out.println(element.getKey() + count + " " + element.getSubkey() + "/" + elements.size()); + + if (element.getSubkey() != null) { + // + count++; + + // + String value = element.getValue(); + + // + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) { + value = value.replace(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(element.getKey(), element.getSubkey(), value); + } + } + } + public void setDriverClassname(final String driverClassname) { this.driverClassname = driverClassname; } diff --git a/src/fr/devinsy/sikevadb/SikevaDB.java b/src/fr/devinsy/sikevadb/SikevaDB.java index 37348e3..c99a971 100644 --- a/src/fr/devinsy/sikevadb/SikevaDB.java +++ b/src/fr/devinsy/sikevadb/SikevaDB.java @@ -60,6 +60,8 @@ public interface SikevaDB { public long countOfElements(String key, String subkey) throws Exception; + public Elements getAllElements() throws Exception; + public Elements getAllElements(String key) throws Exception; public Elements getAllElements(String key, String subkey) throws Exception; @@ -94,8 +96,6 @@ public interface SikevaDB { public StringList getValues(String key) throws Exception; - public StringList getValues(String key, String subkey) throws Exception; - public long memorySize() throws Exception; public long memorySize(String key) throws Exception; @@ -125,4 +125,10 @@ public interface SikevaDB { public void remove(final String key, final String subkey) throws Exception; public void removeAll(final String key) throws Exception; + + public void renameKey(final String oldKey, final String newKey) throws Exception; + + public void replaceInValue(final String key, final String... tokens) throws Exception; + + public void replaceInValues(final String key, final String... tokens) throws Exception; } diff --git a/src/fr/devinsy/sikevadb/XMLSikevaDB.java b/src/fr/devinsy/sikevadb/XMLSikevaDB.java index 609a0d0..7dda06b 100644 --- a/src/fr/devinsy/sikevadb/XMLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/XMLSikevaDB.java @@ -115,6 +115,7 @@ public class XMLSikevaDB { // { // + long id = Long.parseLong(tag.attributes().getByLabel("id").getValue()); String key = StringEscapeUtils.unescapeXml(in.readContentTag("key").getContent()); String subkey = StringEscapeUtils.unescapeXml(in.readNullableContentTag("subkey").getContent()); String value = StringEscapeUtils.unescapeXml(in.readContentTag("value").getContent()); @@ -133,6 +134,8 @@ public class XMLSikevaDB { // result = new Element(); + + result.setId(id); result.setKey(key); result.setSubkey(subkey); result.setValue(value); @@ -196,6 +199,7 @@ public class XMLSikevaDB { // { + out.writeTag("id", String.valueOf(source.getId())); out.writeTag("key", StringEscapeUtils.escapeXml(source.getKey())); if (source.getSubkey() == null) { out.writeTag("subkey", null); @@ -228,24 +232,30 @@ public class XMLSikevaDB { public static void write(final XMLWriter out, final SikevaDB source) throws Exception { if (out == null) { + // throw new NullPointerException("out is null."); + } else if (source == null) { + // out.writeEmptyTag("elements"); + } else { // out.writeStartTag("elements"); for (String key : source.getAllKeys()) { // - Element element = source.getElement(key); - if (element != null) { + Elements elements = source.getAllElements(key); + for (Element element : elements) { write(out, element); } // for (String subkey : source.getSubkeys(key)) { - Element subElement = source.getElement(key, subkey); - write(out, subElement); + Elements subElements = source.getAllElements(key, subkey); + for (Element subElement : subElements) { + write(out, subElement); + } } } diff --git a/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java b/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java index 445be49..3e89ada 100644 --- a/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java @@ -353,6 +353,126 @@ public class SQLSikevaDBTest { logger.debug("===== test done."); } + /** + * @throws Exception + * + */ + @Test + public void testPutElement01() throws Exception { + // + logger.debug("===== test starting..."); + + database.clearDatabase(); + + Element element = new Element(); + + element.setKey("alpha"); + element.setSubkey(null); + element.setSize(10); + element.setDigest("qsdkfqskjf"); + element.setCreationDate(DateTime.now().minusDays(11)); + element.setEditionDate(DateTime.now().minusDays(11)); + element.setArchiveDate(DateTime.now().minusDays(10)); + element.setValue("bonjour"); + + database.put(element); + + Assert.assertEquals(0, database.countOfElements()); + Assert.assertEquals(1, database.countOfArchivedElements()); + Assert.assertEquals(1, database.countOfAllElements()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + * + */ + @Test + public void testPutElement02() throws Exception { + // + logger.debug("===== test starting..."); + + database.clearDatabase(); + + Element element = new Element(); + + element.setId(123); + element.setKey("alpha"); + element.setSubkey(null); + element.setSize(10); + element.setDigest("qsdkfqskjf"); + element.setCreationDate(DateTime.now().minusDays(11)); + element.setEditionDate(DateTime.now().minusDays(11)); + element.setArchiveDate(DateTime.now().minusDays(10)); + element.setValue("bonjour"); + + database.put(element); + + Assert.assertEquals(0, database.countOfElements()); + Assert.assertEquals(1, database.countOfArchivedElements()); + Assert.assertEquals(1, database.countOfAllElements()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + * + */ + @Test + public void testPutElement03() throws Exception { + // + logger.debug("===== test starting..."); + + database.clearDatabase(); + + { + Element element = new Element(); + + element.setId(123); + element.setKey("alpha"); + element.setSubkey(null); + element.setSize(10); + element.setDigest("qsdkfqskjf"); + element.setCreationDate(DateTime.now().minusDays(11)); + element.setEditionDate(DateTime.now().minusDays(11)); + element.setArchiveDate(DateTime.now().minusDays(10)); + element.setValue("bonjour"); + + database.put(element); + } + + { + Element element = new Element(); + + element.setKey("bravo"); + element.setSubkey(null); + element.setSize(10); + element.setDigest("qsdkfqskjf"); + element.setCreationDate(DateTime.now().minusDays(11)); + element.setEditionDate(DateTime.now().minusDays(11)); + element.setArchiveDate(DateTime.now().minusDays(10)); + element.setValue("bonjour"); + + database.put(element); + } + + Elements elements = database.getAllElements(); + + Assert.assertEquals(0, database.countOfElements()); + Assert.assertEquals(2, database.countOfArchivedElements()); + Assert.assertEquals(2, database.countOfAllElements()); + + Assert.assertEquals(123, elements.get(0).getId()); + Assert.assertEquals(124, elements.get(1).getId()); + + // + logger.debug("===== test done."); + } + /** * @throws Exception * @@ -409,6 +529,36 @@ public class SQLSikevaDBTest { logger.debug("===== test done."); } + /** + * @throws Exception + * + */ + @Test + public void testRenameKey01() throws Exception { + // + logger.debug("===== test starting..."); + + database.clearDatabase(); + + database.put("alpha01", "val-alpha01"); + database.put("alpha01", "sub-alpha01", "val-alpha01"); + database.put("alpha01", "sub-alpha02", "val-alpha02"); + database.put("alpha01", "sub-alpha03", "val-alpha03"); + database.put("alpha02", "sub-alpha01", "val-alpha01"); + database.put("alpha03", "sub-alpha02", "val-alpha02"); + database.put("alpha04", "sub-alpha03", "val-alpha03"); + + database.renameKey("alpha01", "november"); + + Elements targets = database.getAllElements(); + + Assert.assertEquals(7, targets.size()); + Assert.assertEquals(3, database.getValues("november").size()); + + // + logger.debug("===== test done."); + } + /** * @throws Exception *