diff --git a/.classpath b/.classpath
index 4cc5a1c..aef547d 100644
--- a/.classpath
+++ b/.classpath
@@ -4,7 +4,7 @@
-
+
@@ -21,5 +21,6 @@
+
diff --git a/bin/fr/devinsy/sikevadb/AlphaTest.class b/bin/fr/devinsy/sikevadb/AlphaTest.class
deleted file mode 100644
index acba22f..0000000
Binary files a/bin/fr/devinsy/sikevadb/AlphaTest.class and /dev/null differ
diff --git a/bin/fr/devinsy/sikevadb/Element.class b/bin/fr/devinsy/sikevadb/Element.class
index eee2b6c..d1c81db 100644
Binary files a/bin/fr/devinsy/sikevadb/Element.class and b/bin/fr/devinsy/sikevadb/Element.class differ
diff --git a/bin/fr/devinsy/sikevadb/FileSikevaDB.class b/bin/fr/devinsy/sikevadb/FileSikevaDB.class
index a303586..1660bde 100644
Binary files a/bin/fr/devinsy/sikevadb/FileSikevaDB.class and b/bin/fr/devinsy/sikevadb/FileSikevaDB.class differ
diff --git a/bin/fr/devinsy/sikevadb/SQLSikevaDB.class b/bin/fr/devinsy/sikevadb/SQLSikevaDB.class
index 4fc777c..7b167eb 100644
Binary files a/bin/fr/devinsy/sikevadb/SQLSikevaDB.class and b/bin/fr/devinsy/sikevadb/SQLSikevaDB.class differ
diff --git a/bin/fr/devinsy/sikevadb/SikevaDB.class b/bin/fr/devinsy/sikevadb/SikevaDB.class
index 928f713..b83d6c4 100644
Binary files a/bin/fr/devinsy/sikevadb/SikevaDB.class and b/bin/fr/devinsy/sikevadb/SikevaDB.class differ
diff --git a/bin/fr/devinsy/sikevadb/SikevaDBFactory.class b/bin/fr/devinsy/sikevadb/SikevaDBFactory.class
index 8194aef..e59274a 100644
Binary files a/bin/fr/devinsy/sikevadb/SikevaDBFactory.class and b/bin/fr/devinsy/sikevadb/SikevaDBFactory.class differ
diff --git a/lib/mysql-jdbc-5.0.8.jar b/lib/mysql-jdbc-5.0.8.jar
new file mode 100644
index 0000000..0170c3e
Binary files /dev/null and b/lib/mysql-jdbc-5.0.8.jar differ
diff --git a/src/fr/devinsy/sikevadb/Element.java b/src/fr/devinsy/sikevadb/Element.java
index d750e00..9180f0e 100644
--- a/src/fr/devinsy/sikevadb/Element.java
+++ b/src/fr/devinsy/sikevadb/Element.java
@@ -49,8 +49,7 @@ public class Element {
* @param value
*/
public Element(final String key, final String subkey, final String value) {
- updateValue(value);
- this.creationDate = this.editionDate;
+ update(value);
this.archiveDate = null;
this.key = key;
this.subkey = subkey;
@@ -127,9 +126,14 @@ public class Element {
this.value = value;
}
- public void updateValue(final String value) {
+ public void update(final String value) {
+ //
setValue(value);
+
+ //
this.editionDate = new Date();
+
+ //
if (value == null) {
this.size = 0;
this.digest = null;
@@ -137,5 +141,10 @@ public class Element {
this.size = this.value.length();
this.digest = DigestUtils.md5Hex(this.value);
}
+
+ //
+ if (this.creationDate == null) {
+ this.creationDate = this.editionDate;
+ }
}
}
diff --git a/src/fr/devinsy/sikevadb/FileSikevaDB.java b/src/fr/devinsy/sikevadb/FileSikevaDB.java
index 401a3a2..7b9be1d 100644
--- a/src/fr/devinsy/sikevadb/FileSikevaDB.java
+++ b/src/fr/devinsy/sikevadb/FileSikevaDB.java
@@ -15,6 +15,8 @@
*/
package fr.devinsy.sikevadb;
+import java.util.Date;
+
import fr.devinsy.util.StringList;
/**
@@ -43,6 +45,30 @@ public class FileSikevaDB implements SikevaDB {
}
+ @Override
+ public void clearAllArchive() throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void clearArchive(final Date beforeDate) throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void clearArchive(final int maxDays) throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void clearDatabase() throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
@Override
public void close() {
// TODO Auto-generated method stub
@@ -115,6 +141,18 @@ public class FileSikevaDB implements SikevaDB {
return null;
}
+ @Override
+ public StringList getAllKeys() throws Exception {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public StringList getAllSubkeys(final String key) throws Exception {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
@Override
public StringList getAllValues(final String key) {
// TODO Auto-generated method stub
@@ -163,6 +201,18 @@ public class FileSikevaDB implements SikevaDB {
return null;
}
+ @Override
+ public StringList getKeys() throws Exception {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public StringList getSubkeys(final String key) throws Exception {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
@Override
public String getValue(final String key) throws Exception {
// TODO Auto-generated method stub
@@ -240,4 +290,10 @@ public class FileSikevaDB implements SikevaDB {
// TODO Auto-generated method stub
}
+
+ @Override
+ public void removeAll(final String key) throws Exception {
+ // TODO Auto-generated method stub
+
+ }
}
diff --git a/src/fr/devinsy/sikevadb/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/SQLSikevaDB.java
index b9a34f1..bfe90c2 100644
--- a/src/fr/devinsy/sikevadb/SQLSikevaDB.java
+++ b/src/fr/devinsy/sikevadb/SQLSikevaDB.java
@@ -86,20 +86,11 @@ public class SQLSikevaDB implements SikevaDB {
*/
@Override
public void archive(final String key) throws SQLException {
- archive(key, null);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void archive(final String key, final String subkey) throws SQLException {
-
//
- Element element = getElement(key, null);
+ Element element = getElement(key);
if (element == null) {
- throw new NullPointerException("Undefined element [key=" + key + "][subkey=" + subkey + "]");
+ throw new NullPointerException("Undefined element [key=" + key + "]");
} else {
//
element.archive();
@@ -111,13 +102,13 @@ public class SQLSikevaDB implements SikevaDB {
try {
//
connection = getConnection();
- statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=?");
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL");
statement.setDate(1, dateToDate(element.getArchiveDate()));
statement.setString(2, element.getKey());
- statement.setString(3, element.getSubkey());
- statement.executeQuery();
+ statement.executeUpdate();
} finally {
close(connection, statement, resultSet);
@@ -125,6 +116,132 @@ public class SQLSikevaDB implements SikevaDB {
}
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void archive(final String key, final String subkey) throws SQLException {
+ //
+ if (subkey == null) {
+ archive(key);
+ } else {
+ //
+ Element element = getElement(key, subkey);
+
+ if (element == null) {
+ throw new NullPointerException("Undefined element [key=" + key + "][subkey=" + subkey + "]");
+ } else {
+ //
+ element.archive();
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ //
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=?");
+
+ statement.setDate(1, dateToDate(element.getArchiveDate()));
+ statement.setString(2, element.getKey());
+ statement.setString(3, element.getSubkey());
+
+ statement.executeUpdate();
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void clearAllArchive() throws SQLException {
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ //
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("DELETE FROM elements WHERE ARCHIVE_DATE IS NOT NULL");
+
+ statement.executeUpdate();
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void clearArchive(final Date beforeDate) throws SQLException {
+ if (beforeDate == null) {
+ throw new NullPointerException("beforeDate is null.");
+ } else {
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ //
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("DELETE FROM elements WHERE ARCHIVE_DATE");
+
+ statement.setDate(1, dateToDate(beforeDate));
+
+ statement.executeUpdate();
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void clearArchive(final int maxDays) throws SQLException {
+ if (maxDays < 0) {
+ throw new IndexOutOfBoundsException("maxDays is negative.");
+ } else {
+ Date beforeDate = new Date(new Date().getTime() + maxDays * 24 * 60 * 60 * 1000);
+ clearArchive(beforeDate);
+ }
+ }
+
+ /**
+ *
+ */
+ @Override
+ public void clearDatabase() throws Exception {
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ //
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("DELETE FROM elements");
+
+ statement.executeUpdate();
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
+
/**
*
*/
@@ -217,6 +334,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE AND TOPKEY=?");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -239,21 +357,27 @@ public class SQLSikevaDB implements SikevaDB {
public long countOfAllElements(final String key, final String subkey) throws SQLException {
long result;
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try {
- connection = getConnection();
- statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
+ //
+ if (subkey == null) {
+ result = countOfAllElements(key);
+ } else {
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE TOPKEY=? AND SUBKEY=?");
+ statement.setString(1, key);
+ statement.setString(2, subkey);
+ resultSet = statement.executeQuery();
- resultSet.next();
- result = resultSet.getLong(1);
+ resultSet.next();
+ result = resultSet.getLong(1);
- } finally {
- close(connection, statement, resultSet);
+ } finally {
+ close(connection, statement, resultSet);
+ }
}
//
@@ -272,6 +396,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NOT NULL");
@@ -298,6 +423,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=?");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -320,21 +446,27 @@ public class SQLSikevaDB implements SikevaDB {
public long countOfArchivedElements(final String key, final String subkey) throws SQLException {
long result;
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try {
- connection = getConnection();
- statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
+ //
+ if (subkey == null) {
+ result = countOfArchivedElements(key);
+ } else {
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=?");
+ statement.setString(1, key);
+ statement.setString(2, subkey);
+ resultSet = statement.executeQuery();
- resultSet.next();
- result = resultSet.getLong(1);
+ resultSet.next();
+ result = resultSet.getLong(1);
- } finally {
- close(connection, statement, resultSet);
+ } finally {
+ close(connection, statement, resultSet);
+ }
}
//
@@ -355,6 +487,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NULL");
@@ -383,6 +516,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -405,21 +539,27 @@ public class SQLSikevaDB implements SikevaDB {
public long countOfElements(final String key, final String subkey) throws SQLException {
long result;
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try {
- connection = getConnection();
- statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
+ //
+ if (subkey == null) {
+ result = countOfElements(key);
+ } else {
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT count(*) FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?");
+ statement.setString(1, key);
+ statement.setString(2, subkey);
+ resultSet = statement.executeQuery();
- resultSet.next();
- result = resultSet.getLong(1);
+ resultSet.next();
+ result = resultSet.getLong(1);
- } finally {
- close(connection, statement, resultSet);
+ } finally {
+ close(connection, statement, resultSet);
+ }
}
//
@@ -442,6 +582,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection
.prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC");
statement.setString(1, key);
@@ -479,7 +620,59 @@ public class SQLSikevaDB implements SikevaDB {
Elements result;
//
- result = new Elements((int) countOfAllElements(key, subkey));
+ if (subkey == null) {
+ result = getAllElements(key);
+ } else {
+ //
+ result = new Elements((int) countOfAllElements(key, subkey));
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection
+ .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
+ statement.setString(1, key);
+ statement.setString(2, subkey);
+ resultSet = statement.executeQuery();
+
+ while (resultSet.next()) {
+ //
+ Element element = new Element();
+ element.setKey(resultSet.getString(1));
+ element.setSubkey(resultSet.getString(2));
+ element.setSize(resultSet.getLong(3));
+ element.setDigest(resultSet.getString(4));
+ element.setCreationDate(dateToDate(resultSet.getDate(5)));
+ element.setEditionDate(dateToDate(resultSet.getDate(6)));
+ element.setArchiveDate(dateToDate(resultSet.getDate(7)));
+ element.setValue(resultSet.getString(8));
+
+ //
+ result.add(element);
+ }
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
+
+ //
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public StringList getAllKeys() throws SQLException {
+ StringList result;
+
+ //
+ result = new StringList();
//
Connection connection = null;
@@ -487,26 +680,47 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
- statement = connection
- .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
- statement.setString(1, key);
- statement.setString(2, subkey);
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT DISTINCT TOPKEY FROM elements ORDER BY CREATION_DATE ASC");
resultSet = statement.executeQuery();
while (resultSet.next()) {
//
- Element element = new Element();
- element.setKey(resultSet.getString(1));
- element.setSubkey(resultSet.getString(2));
- element.setSize(resultSet.getLong(3));
- element.setDigest(resultSet.getString(4));
- element.setCreationDate(dateToDate(resultSet.getDate(5)));
- element.setEditionDate(dateToDate(resultSet.getDate(6)));
- element.setArchiveDate(dateToDate(resultSet.getDate(7)));
- element.setValue(resultSet.getString(8));
+ result.add(resultSet.getString(1));
+ }
+ } finally {
+ close(connection, statement, resultSet);
+ }
+
+ //
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public StringList getAllSubkeys(final String key) throws SQLException {
+ StringList result;
+
+ //
+ result = new StringList();
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT DISTINCT SUBKEY FROM elements WHERE KEY=? ORDER BY CREATION_DATE ASC");
+ statement.setString(1, key);
+ resultSet = statement.executeQuery();
+
+ while (resultSet.next()) {
//
- result.add(element);
+ result.add(resultSet.getString(1));
}
} finally {
@@ -533,6 +747,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -558,26 +773,32 @@ public class SQLSikevaDB implements SikevaDB {
StringList result;
//
- result = new StringList((int) countOfAllElements(key, subkey));
+ if (subkey == null) {
+ result = getAllValues(key);
+ } else {
+ //
+ result = new StringList((int) countOfAllElements(key, subkey));
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try {
- connection = getConnection();
- statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE 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));
+ while (resultSet.next()) {
+ //
+ result.add(resultSet.getString(1));
+ }
+
+ } finally {
+ close(connection, statement, resultSet);
}
-
- } finally {
- close(connection, statement, resultSet);
}
//
@@ -600,6 +821,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -625,27 +847,33 @@ public class SQLSikevaDB implements SikevaDB {
StringList result;
//
- result = new StringList((int) countOfArchivedElements(key, subkey));
+ if (subkey == null) {
+ result = getArchivedValues(key);
+ } else {
+ //
+ result = new StringList((int) countOfArchivedElements(key, subkey));
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try {
- connection = getConnection();
- statement = connection
- .prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection
+ .prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NOT 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));
+ while (resultSet.next()) {
+ //
+ result.add(resultSet.getString(1));
+ }
+
+ } finally {
+ close(connection, statement, resultSet);
}
-
- } finally {
- close(connection, statement, resultSet);
}
//
@@ -694,8 +922,9 @@ public class SQLSikevaDB implements SikevaDB {
try {
//
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection
- .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=?");
+ .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -717,7 +946,7 @@ public class SQLSikevaDB implements SikevaDB {
//
if (resultSet.next()) {
- throw new SQLException("More than only once result.");
+ throw new SQLException("More than only once result [key=" + key + "].");
}
} finally {
@@ -736,41 +965,47 @@ public class SQLSikevaDB implements SikevaDB {
Element result;
//
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try {
+ if (subkey == null) {
+ result = getElement(key);
+ } else {
//
- connection = getConnection();
- statement = connection
- .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
-
- //
- if (resultSet.next()) {
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
//
- result = new Element();
- result.setKey(resultSet.getString(1));
- result.setSubkey(resultSet.getString(2));
- result.setSize(resultSet.getLong(3));
- result.setDigest(resultSet.getString(4));
- result.setCreationDate(dateToDate(resultSet.getDate(5)));
- result.setEditionDate(dateToDate(resultSet.getDate(6)));
- result.setArchiveDate(dateToDate(resultSet.getDate(7)));
- result.setValue(resultSet.getString(8));
- } else {
- result = null;
- }
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection
+ .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?");
+ statement.setString(1, key);
+ statement.setString(2, subkey);
+ resultSet = statement.executeQuery();
- //
- if (resultSet.next()) {
- throw new SQLException("More than only once result.");
- }
+ //
+ if (resultSet.next()) {
+ //
+ result = new Element();
+ result.setKey(resultSet.getString(1));
+ result.setSubkey(resultSet.getString(2));
+ result.setSize(resultSet.getLong(3));
+ result.setDigest(resultSet.getString(4));
+ result.setCreationDate(dateToDate(resultSet.getDate(5)));
+ result.setEditionDate(dateToDate(resultSet.getDate(6)));
+ result.setArchiveDate(dateToDate(resultSet.getDate(7)));
+ result.setValue(resultSet.getString(8));
+ } else {
+ result = null;
+ }
- } finally {
- close(connection, statement, resultSet);
+ //
+ if (resultSet.next()) {
+ throw new SQLException("More than only once result.");
+ }
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
}
//
@@ -793,6 +1028,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection
.prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC");
statement.setString(1, key);
@@ -830,7 +1066,59 @@ public class SQLSikevaDB implements SikevaDB {
Elements result;
//
- result = new Elements((int) countOfElements(key, subkey));
+ if (subkey == null) {
+ result = getElements(key);
+ } else {
+ //
+ result = new Elements((int) countOfElements(key, subkey));
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection
+ .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM 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();
+
+ while (resultSet.next()) {
+ //
+ Element element = new Element();
+ element.setKey(resultSet.getString(1));
+ element.setSubkey(resultSet.getString(2));
+ element.setSize(resultSet.getLong(3));
+ element.setDigest(resultSet.getString(4));
+ element.setCreationDate(dateToDate(resultSet.getDate(5)));
+ element.setEditionDate(dateToDate(resultSet.getDate(6)));
+ element.setArchiveDate(dateToDate(resultSet.getDate(7)));
+ element.setValue(resultSet.getString(8));
+
+ //
+ result.add(element);
+ }
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
+
+ //
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public StringList getKeys() throws SQLException {
+ StringList result;
+
+ //
+ result = new StringList();
//
Connection connection = null;
@@ -838,26 +1126,13 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
- statement = connection
- .prepareStatement("SELECT TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY CREATION_DATE ASC");
- statement.setString(1, key);
- statement.setString(2, subkey);
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT DISTINCT TOPKEY FROM elements WHERE ARCHIVE_DATE IS NULL ORDER BY CREATION_DATE ASC");
resultSet = statement.executeQuery();
while (resultSet.next()) {
//
- Element element = new Element();
- element.setKey(resultSet.getString(1));
- element.setSubkey(resultSet.getString(2));
- element.setSize(resultSet.getLong(3));
- element.setDigest(resultSet.getString(4));
- element.setCreationDate(dateToDate(resultSet.getDate(5)));
- element.setEditionDate(dateToDate(resultSet.getDate(6)));
- element.setArchiveDate(dateToDate(resultSet.getDate(7)));
- element.setValue(resultSet.getString(8));
-
- //
- result.add(element);
+ result.add(resultSet.getString(1));
}
} finally {
@@ -876,6 +1151,40 @@ public class SQLSikevaDB implements SikevaDB {
return password;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public StringList getSubkeys(final String key) throws SQLException {
+ StringList result;
+
+ //
+ result = new StringList();
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT DISTINCT SUBKEY FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC");
+ statement.setString(1, key);
+ resultSet = statement.executeQuery();
+
+ while (resultSet.next()) {
+ //
+ result.add(resultSet.getString(1));
+ }
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+
+ //
+ return result;
+ }
+
public String getUrl() {
return url;
}
@@ -894,6 +1203,7 @@ public class SQLSikevaDB implements SikevaDB {
try {
//
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -926,33 +1236,38 @@ public class SQLSikevaDB implements SikevaDB {
public String getValue(final String key, final String subkey) throws SQLException {
String result;
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try {
+ if (subkey == null) {
+ result = getValue(key);
+ } else {
//
- connection = getConnection();
- statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
-
- //
- if (resultSet.next()) {
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
//
- result = resultSet.getString(1);
- } else {
- result = null;
- }
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?");
+ statement.setString(1, key);
+ statement.setString(2, subkey);
+ resultSet = statement.executeQuery();
- //
- if (resultSet.next()) {
- throw new SQLException("More than only once result.");
- }
+ //
+ if (resultSet.next()) {
+ //
+ result = resultSet.getString(1);
+ } else {
+ result = null;
+ }
- } finally {
- close(connection, statement, resultSet);
+ //
+ if (resultSet.next()) {
+ throw new SQLException("More than only once result.");
+ }
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
}
//
@@ -975,6 +1290,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT VALUE FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? ORDER BY ARCHIVE_DATE ASC");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -1000,27 +1316,33 @@ public class SQLSikevaDB implements SikevaDB {
StringList result;
//
- result = new StringList((int) countOfElements(key, subkey));
+ 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();
- statement = connection
- .prepareStatement("SELECT VALUE FROM 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();
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection
+ .prepareStatement("SELECT VALUE FROM 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));
+ while (resultSet.next()) {
+ //
+ result.add(resultSet.getString(1));
+ }
+
+ } finally {
+ close(connection, statement, resultSet);
}
-
- } finally {
- close(connection, statement, resultSet);
}
//
@@ -1040,6 +1362,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements");
resultSet = statement.executeQuery();
@@ -1067,6 +1390,7 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE TOPKEY=?");
statement.setString(1, key);
resultSet = statement.executeQuery();
@@ -1090,21 +1414,27 @@ public class SQLSikevaDB implements SikevaDB {
long result;
//
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try {
- connection = getConnection();
- statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
+ if (subkey == null) {
+ result = memorySize(key);
+ } else {
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE TOPKEY=? AND SUBKEY=?");
+ statement.setString(1, key);
+ statement.setString(2, subkey);
+ resultSet = statement.executeQuery();
- resultSet.next();
- result = resultSet.getLong(1);
+ resultSet.next();
+ result = resultSet.getLong(1);
- } finally {
- close(connection, statement, resultSet);
+ } finally {
+ close(connection, statement, resultSet);
+ }
}
//
@@ -1143,8 +1473,9 @@ public class SQLSikevaDB implements SikevaDB {
ResultSet resultSet = null;
try {
connection = getConnection();
+ connection.setAutoCommit(true);
statement = connection
- .prepareStatement("INSERT INTO elements (TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE) VALUES(1, 2, 3, 4, 5,6,7,8)");
+ .prepareStatement("INSERT INTO elements (TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
@@ -1155,7 +1486,7 @@ public class SQLSikevaDB implements SikevaDB {
statement.setDate(7, dateToDate(element.getArchiveDate()));
statement.setString(8, element.getValue());
- resultSet = statement.executeQuery();
+ statement.executeUpdate();
} finally {
close(connection, statement, resultSet);
@@ -1167,7 +1498,64 @@ public class SQLSikevaDB implements SikevaDB {
*/
@Override
public void put(final String key, final String value) throws SQLException {
- put(key, null, value);
+ Element element = getElement(key);
+
+ if (element == null) {
+ //
+ element = new Element();
+ element.setKey(key);
+ element.update(value);
+ element.setArchiveDate(null);
+
+ //
+ put(element);
+
+ } else {
+ //
+ element.update(value);
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ //
+ connection = getConnection();
+ connection.setAutoCommit(false);
+
+ // Archive existing element.
+ statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL");
+
+ statement.setDate(1, dateToDate(element.getEditionDate()));
+ statement.setString(2, element.getKey());
+
+ statement.executeUpdate();
+ statement.clearParameters();
+
+ // Insert new version of the element.
+ statement.clearParameters();
+ statement = connection
+ .prepareStatement("INSERT INTO elements (TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
+
+ statement.setString(1, element.getKey());
+ statement.setString(2, element.getSubkey());
+ statement.setLong(3, element.getSize());
+ statement.setString(4, element.getDigest());
+ statement.setDate(5, dateToDate(element.getCreationDate()));
+ statement.setDate(6, dateToDate(element.getEditionDate()));
+ statement.setDate(7, dateToDate(element.getArchiveDate()));
+ statement.setString(8, element.getValue());
+
+ statement.executeUpdate();
+
+ connection.commit();
+
+ } catch (Exception exception) {
+ connection.rollback();
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
}
/**
@@ -1175,13 +1563,25 @@ public class SQLSikevaDB implements SikevaDB {
*/
@Override
public void put(final String key, final String subkey, final String value) throws SQLException {
- Element element = getElement(key, null);
+ //
+ Element element = getElement(key, subkey);
+
+ //
if (element == null) {
+ //
+ element = new Element();
+ element.setKey(key);
+ element.setSubkey(subkey);
+ element.update(value);
+ element.setArchiveDate(null);
+
+ //
+ put(element);
} else {
//
- element.updateValue(value);
+ element.update(value);
//
Connection connection = null;
@@ -1199,13 +1599,13 @@ public class SQLSikevaDB implements SikevaDB {
statement.setString(2, element.getKey());
statement.setString(3, element.getSubkey());
- resultSet = statement.executeQuery();
+ statement.executeUpdate();
statement.clearParameters();
// Insert new version of the element.
statement.clearParameters();
statement = connection
- .prepareStatement("INSERT INTO elements (TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE) VALUES(1, 2, 3, 4, 5,6,7,8)");
+ .prepareStatement("INSERT INTO elements (TOPKEY,SUBKEY,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE,VALUE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
@@ -1216,12 +1616,13 @@ public class SQLSikevaDB implements SikevaDB {
statement.setDate(7, dateToDate(element.getArchiveDate()));
statement.setString(8, element.getValue());
- resultSet = statement.executeQuery();
+ statement.executeUpdate();
connection.commit();
- } finally {
+ } catch (Exception exception) {
connection.rollback();
+ } finally {
close(connection, statement, resultSet);
}
}
@@ -1231,8 +1632,34 @@ public class SQLSikevaDB implements SikevaDB {
* {@inheritDoc}
*/
@Override
- public void remove(final String key) throws Exception {
- remove(key, null);
+ public void remove(final String key) throws SQLException {
+ //
+ Element element = getElement(key);
+
+ if (element == null) {
+ throw new NullPointerException("Undefined element [key=" + key + "]");
+ } else {
+ //
+ element.archive();
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ //
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("DELETE FROM elements WHERE TOPKEY=? AND SUBKEY IS NULL");
+
+ statement.setString(1, element.getKey());
+
+ statement.executeUpdate();
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
}
/**
@@ -1240,9 +1667,8 @@ public class SQLSikevaDB implements SikevaDB {
*/
@Override
public void remove(final String key, final String subkey) throws SQLException {
-
//
- Element element = getElement(key, null);
+ Element element = getElement(key, subkey);
if (element == null) {
throw new NullPointerException("Undefined element [key=" + key + "][subkey=" + subkey + "]");
@@ -1257,12 +1683,46 @@ public class SQLSikevaDB implements SikevaDB {
try {
//
connection = getConnection();
- statement = connection.prepareStatement("DELETE FROM elements WHERE WHERE TOPKEY=? AND SUBKEY=?");
+ statement = connection.prepareStatement("DELETE FROM elements WHERE TOPKEY=? AND SUBKEY=?");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
- statement.executeQuery();
+ statement.executeUpdate();
+
+ } finally {
+ close(connection, statement, resultSet);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void removeAll(final String key) throws SQLException {
+ //
+ Element element = getElement(key);
+
+ if (element == null) {
+ throw new NullPointerException("Undefined element [key=" + key + "]");
+ } else {
+ //
+ element.archive();
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ //
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("DELETE FROM elements WHERE TOPKEY=? AND SUBKEY IS NOT NULL");
+
+ statement.setString(1, element.getKey());
+
+ statement.executeUpdate();
} finally {
close(connection, statement, resultSet);
diff --git a/src/fr/devinsy/sikevadb/SikevaDB.java b/src/fr/devinsy/sikevadb/SikevaDB.java
index 0905fc7..ce74464 100644
--- a/src/fr/devinsy/sikevadb/SikevaDB.java
+++ b/src/fr/devinsy/sikevadb/SikevaDB.java
@@ -15,6 +15,8 @@
*/
package fr.devinsy.sikevadb;
+import java.util.Date;
+
import fr.devinsy.util.StringList;
/**
@@ -30,6 +32,14 @@ public interface SikevaDB {
public void archive(final String key, final String subkey) throws Exception;
+ public void clearAllArchive() throws Exception;
+
+ public void clearArchive(final Date beforeDate) throws Exception;
+
+ public void clearArchive(final int maxDays) throws Exception;
+
+ public void clearDatabase() throws Exception;
+
public void close() throws Exception;
public long countOfAllElements() throws Exception;
@@ -54,6 +64,10 @@ public interface SikevaDB {
public Elements getAllElements(String key, String subkey) throws Exception;
+ public StringList getAllKeys() throws Exception;
+
+ public StringList getAllSubkeys(String key) throws Exception;
+
public StringList getAllValues(String key) throws Exception;
public StringList getAllValues(String key, String subkey) throws Exception;
@@ -70,6 +84,10 @@ public interface SikevaDB {
public Elements getElements(String key, String subkey) throws Exception;
+ public StringList getKeys() throws Exception;
+
+ public StringList getSubkeys(String key) throws Exception;
+
public String getValue(String key) throws Exception;
public String getValue(String key, String subkey) throws Exception;
@@ -95,4 +113,6 @@ public interface SikevaDB {
public void remove(final String key) throws Exception;
public void remove(final String key, final String subkey) throws Exception;
+
+ public void removeAll(final String key) throws Exception;
}
diff --git a/src/fr/devinsy/sikevadb/SikevaDBFactory.java b/src/fr/devinsy/sikevadb/SikevaDBFactory.java
index 49f5347..989b356 100644
--- a/src/fr/devinsy/sikevadb/SikevaDBFactory.java
+++ b/src/fr/devinsy/sikevadb/SikevaDBFactory.java
@@ -37,16 +37,16 @@ public class SikevaDBFactory {
/**
*
- * @param host
- * @param port
+ * @param driverClassName
+ * @param url
* @param login
* @param password
* @return
*/
- public static SikevaDB get(final String host, final String port, final String login, final String password) {
+ public static SikevaDB get(final String driverClassName, final String url, final String login, final String password) {
SikevaDB result;
- result = new SQLSikevaDB(host, port, login, password);
+ result = new SQLSikevaDB(driverClassName, url, login, password);
//
return result;
diff --git a/test/fr/devinsy/sikevadb/AlphaTest.java b/test/fr/devinsy/sikevadb/AlphaTest.java
deleted file mode 100644
index f53704e..0000000
--- a/test/fr/devinsy/sikevadb/AlphaTest.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package fr.devinsy.sikevadb;
-
-import org.apache.log4j.BasicConfigurator;
-import org.apache.log4j.Level;
-import org.apache.log4j.Logger;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- *
- * @author Christian P. Momon
- */
-public class AlphaTest {
- static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(AlphaTest.class);
-
- /**
- *
- */
- @Before
- public void before() {
- BasicConfigurator.configure();
- Logger.getRootLogger().setLevel(Level.ERROR);
- }
-
- /**
- *
- */
- @Test
- public void testFoo01() {
- //
- logger.debug("===== test starting...");
-
- //
- Assert.assertEquals('a', 'a');
-
- //
- logger.debug("===== test done.");
- }
-}
diff --git a/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java b/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java
new file mode 100644
index 0000000..cef357e
--- /dev/null
+++ b/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java
@@ -0,0 +1,358 @@
+package fr.devinsy.sikevadb;
+
+import java.sql.SQLException;
+import java.util.Date;
+
+import javax.naming.NamingException;
+
+import org.apache.log4j.BasicConfigurator;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import fr.devinsy.util.StringList;
+
+/**
+ * ?profileSQL=true
+ *
+ * @author Christian P. Momon
+ */
+public class SQLSikevaDBTest {
+ static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SQLSikevaDBTest.class);
+ private SQLSikevaDB database;
+
+ /**
+ *
+ */
+ @After
+ public void after() {
+ if (this.database != null) {
+ this.database.close();
+ }
+ }
+
+ /**
+ * @throws NamingException
+ * @throws SQLException
+ * @throws ClassNotFoundException
+ * @throws IllegalAccessException
+ * @throws InstantiationException
+ *
+ */
+ @Before
+ public void before() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, NamingException {
+ BasicConfigurator.configure();
+ Logger.getRootLogger().setLevel(Level.ERROR);
+
+ this.database = new SQLSikevaDB("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/sikevadb-test", "sikevadb-test", "12345678");
+ this.database.open();
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testClearArchive01() 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(new Date());
+ element.setEditionDate(new Date());
+ element.setArchiveDate(new Date(new Date().getTime() - 10 * 24 * 60 * 60 * 1000));
+ element.setValue("bonjour");
+
+ database.put(element);
+
+ database.put("alpha", "bravo", "toto");
+
+ database.clearArchive(5);
+
+ Assert.assertEquals(1, database.countOfElements());
+ Assert.assertEquals(0, database.countOfArchivedElements());
+ Assert.assertEquals(1, database.countOfAllElements());
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testClearArchive02() 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(new Date());
+ element.setEditionDate(new Date());
+ element.setArchiveDate(new Date(new Date().getTime() - 10 * 24 * 60 * 60 * 1000));
+ element.setValue("bonjour");
+
+ database.put(element);
+
+ database.put("alpha", "bravo", "toto");
+
+ database.clearArchive(new Date(new Date().getTime() - 5 * 24 * 60 * 60 * 1000));
+
+ Assert.assertEquals(1, database.countOfElements());
+ Assert.assertEquals(0, database.countOfArchivedElements());
+ Assert.assertEquals(1, database.countOfAllElements());
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testPut01() throws Exception {
+ //
+ logger.debug("===== test starting...");
+
+ database.clearDatabase();
+
+ String source = "bonjour";
+ database.put("alpha01", source);
+ String target = database.getValue("alpha01");
+ Assert.assertEquals(source, target);
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testPut02() throws Exception {
+ //
+ logger.debug("===== test starting...");
+
+ database.clearDatabase();
+
+ String source = "bonjour";
+ database.put("alpha01", source);
+
+ String target = database.getValue("alpha01");
+ Assert.assertEquals(source, target);
+
+ target = database.getValue("alpha01", null);
+ Assert.assertEquals(source, target);
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testPut03() throws Exception {
+ //
+ logger.debug("===== test starting...");
+
+ database.clearDatabase();
+
+ String source = "bonjour";
+ database.put("alpha01", null, source);
+
+ String target = database.getValue("alpha01");
+ Assert.assertEquals(source, target);
+
+ target = database.getValue("alpha01", null);
+ Assert.assertEquals(source, target);
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testPut04() throws Exception {
+ //
+ logger.debug("===== test starting...");
+
+ database.clearDatabase();
+
+ String source = "bonjour";
+ database.put("alpha02", "bravo", source);
+
+ String target = database.getValue("alpha02", "bravo");
+ Assert.assertEquals(source, target);
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testPut05() throws Exception {
+ //
+ logger.debug("===== test starting...");
+
+ database.clearDatabase();
+
+ String source = "bonjour";
+ database.put("alpha01", source);
+
+ String target = database.getValue("alpha01");
+ Assert.assertEquals(source, target);
+
+ String source2 = "au revoir";
+ database.put("alpha01", source2);
+
+ target = database.getValue("alpha01");
+ Assert.assertEquals(source2, target);
+
+ StringList targets = database.getArchivedValues("alpha01");
+ Assert.assertEquals(1, targets.size());
+ Assert.assertEquals(source, targets.get(0));
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testPut06() throws Exception {
+ //
+ logger.debug("===== test starting...");
+
+ database.clearDatabase();
+
+ String source = "bonjour";
+ database.put("alpha01", "bravo", source);
+
+ String target = database.getValue("alpha01", "bravo");
+ Assert.assertEquals(source, target);
+
+ String source2 = "au revoir";
+ database.put("alpha01", "bravo", source2);
+
+ target = database.getValue("alpha01", "bravo");
+ Assert.assertEquals(source2, target);
+
+ StringList targets = database.getArchivedValues("alpha01", "bravo");
+ Assert.assertEquals(1, targets.size());
+ Assert.assertEquals(source, targets.get(0));
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testRemove01() throws Exception {
+ //
+ logger.debug("===== test starting...");
+
+ database.clearDatabase();
+
+ String source = "bonjour";
+ database.put("alpha01", "bravo", source);
+
+ String target = database.getValue("alpha01", "bravo");
+ Assert.assertEquals(source, target);
+
+ String source2 = "au revoir";
+ database.put("alpha01", "bravo", source2);
+
+ target = database.getValue("alpha01", "bravo");
+ Assert.assertEquals(source2, target);
+
+ StringList targets = database.getArchivedValues("alpha01", "bravo");
+ Assert.assertEquals(1, targets.size());
+ Assert.assertEquals(source, targets.get(0));
+
+ //
+ logger.debug("===== test done.");
+ }
+
+ /**
+ * @throws Exception
+ *
+ */
+ @Test
+ public void testSize01() throws Exception {
+ //
+ logger.debug("===== test starting...");
+
+ database.clearDatabase();
+
+ Assert.assertEquals(0, database.countOfElements());
+ Assert.assertEquals(0, database.countOfArchivedElements());
+ Assert.assertEquals(0, database.countOfAllElements());
+
+ database.put("alpha01", "qlskjfmlqja");
+ database.put("alpha01", "qlskjfmlqjb");
+ database.put("alpha02", "qlskjfmlqj");
+ database.put("alpha03", "qlskjfmlqj");
+ database.put("alpha04", "qlskjfmlqj");
+ database.put("alpha05", "qlskjfmlqj");
+ database.archive("alpha02");
+ database.remove("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.archive("alpha01", "bravo2");
+ database.remove("alpha01", "bravo3");
+
+ // System.out.println(database.countOfElements() + " " +
+ // database.countOfArchivedElements() + " " +
+ // database.countOfAllElements());
+
+ Assert.assertEquals(6, database.countOfElements());
+ Assert.assertEquals(4, database.countOfArchivedElements());
+ Assert.assertEquals(10, database.countOfAllElements());
+
+ database.clearDatabase();
+
+ Assert.assertEquals(0, database.countOfElements());
+ Assert.assertEquals(0, database.countOfArchivedElements());
+ Assert.assertEquals(0, database.countOfAllElements());
+
+ //
+ logger.debug("===== test done.");
+ }
+
+}
diff --git a/test/fr/devinsy/sikevadb/data.xml b/test/fr/devinsy/sikevadb/data.xml
new file mode 100644
index 0000000..36e4b81
--- /dev/null
+++ b/test/fr/devinsy/sikevadb/data.xml
@@ -0,0 +1,12 @@
+
+
+
\ No newline at end of file