diff --git a/src/fr/devinsy/sikevadb/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/SQLSikevaDB.java index 8180b64..9fd39f0 100644 --- a/src/fr/devinsy/sikevadb/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/SQLSikevaDB.java @@ -1690,8 +1690,11 @@ public class SQLSikevaDB implements SikevaDB { */ @Override public void put(final String key, final String value) throws SQLException { + + // Element element = getElement(key); + // if (element == null) { // element = new Element(); @@ -1699,54 +1702,54 @@ public class SQLSikevaDB implements SikevaDB { element.update(value); element.setArchiveDate(null); - // - put(element); - } else { // element.update(value); + } + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - // - connection = getConnection(); - connection.setAutoCommit(false); + connection = getConnection(); + connection.setAutoCommit(false); - // Archive existing element. - statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); + // Archive existing element. + if (element.getCreationDate() != null) { + statement = connection + .prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); statement.setTimestamp(1, toTimestamp(element.getEditionDate())); statement.setString(2, element.getKey()); statement.executeUpdate(); statement.clearParameters(); - - // Insert new version of the element. - statement.clearParameters(); - 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(); - - connection.commit(); - - } catch (Exception exception) { - connection.rollback(); - } finally { - close(connection, statement, resultSet); + statement.close(); } + + // Insert new version of the element. + statement = connection + .prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); + + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + statement.setString(3, element.getValue()); + statement.setLong(4, element.getSize()); + statement.setString(5, element.getDigest()); + statement.setTimestamp(6, toTimestamp(element.getCreationDate())); + statement.setTimestamp(7, toTimestamp(element.getEditionDate())); + statement.setTimestamp(8, toTimestamp(element.getArchiveDate())); + + statement.executeUpdate(); + + connection.commit(); + + } catch (Exception exception) { + connection.rollback(); + } finally { + close(connection, statement, resultSet); } } @@ -1768,23 +1771,22 @@ public class SQLSikevaDB implements SikevaDB { element.update(value); element.setArchiveDate(null); - // - put(element); - } else { // element.update(value); + } + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - // - connection = getConnection(); - connection.setAutoCommit(false); + connection = getConnection(); + connection.setAutoCommit(false); - // Archive existing element. + // Archive existing element. + if (element.getCreationDate() != null) { statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); statement.setTimestamp(1, toTimestamp(element.getEditionDate())); @@ -1793,30 +1795,30 @@ public class SQLSikevaDB implements SikevaDB { statement.executeUpdate(); statement.clearParameters(); - - // Insert new version of the element. - statement.clearParameters(); - 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(); - - connection.commit(); - - } catch (Exception exception) { - connection.rollback(); - } finally { - close(connection, statement, resultSet); + statement.close(); } + + // Insert new version of the element. + statement = connection + .prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); + + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + statement.setString(3, element.getValue()); + statement.setLong(4, element.getSize()); + statement.setString(5, element.getDigest()); + statement.setTimestamp(6, toTimestamp(element.getCreationDate())); + statement.setTimestamp(7, toTimestamp(element.getEditionDate())); + statement.setTimestamp(8, toTimestamp(element.getArchiveDate())); + + statement.executeUpdate(); + + connection.commit(); + + } catch (Exception exception) { + connection.rollback(); + } finally { + close(connection, statement, resultSet); } }