[New] Implemented StrolchDao.hasElement()

This commit is contained in:
Robert von Burg 2014-08-04 11:47:51 +02:00
parent 7b6ad39060
commit 5623f27f2d
1 changed files with 21 additions and 1 deletions

View File

@ -51,6 +51,27 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
protected abstract T parseFromXml(String id, String type, SQLXML xml);
@Override
public boolean hasElement(String type, String id) {
String sql = "select count(*) from " + getTableName() + " where type = ?";
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
statement.setString(1, type);
try (ResultSet result = statement.executeQuery()) {
result.next();
long numberOfElements = result.getLong(1);
if (numberOfElements == 0)
return false;
if (numberOfElements == 1)
return true;
String msg = MessageFormat.format("Non unique number of elements with type {0} and id {1}", type, id);
throw new StrolchPersistenceException(msg);
}
} catch (SQLException e) {
throw new StrolchPersistenceException("Failed to query size due to: " + e.getMessage(), e);
}
}
@Override
public long querySize() {
String sql = "select count(*) from " + getTableName();
@ -66,7 +87,6 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
@Override
public long querySize(String type) {
String sql = "select count(*) from " + getTableName() + " where type = ?";
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
statement.setString(1, type);