diff --git a/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java b/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java index 89b12c846..c54cc384e 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java @@ -98,6 +98,13 @@ public abstract class AbstractDhRepo public void delete(TDTO dto) { this.queryDictionaryFirst(this.createDeleteSql(dto)); } + public boolean exists(TDTO dto) { return this.existsWithPrimaryKey(dto.getPrimaryKeyString()); } + public boolean existsWithPrimaryKey(String primaryKey) + { + String whereEqualStatement = this.createWherePrimaryKeyStatement(primaryKey); + Map result = this.queryDictionaryFirst("SELECT EXISTS(SELECT 1 FROM "+this.getTableName()+" WHERE "+whereEqualStatement+") as 'existingCount';"); + return result != null && (int)result.get("existingCount") != 0; + } //==============// @@ -186,8 +193,10 @@ public abstract class AbstractDhRepo // helper methods // //================// + /** Example: Id = '0' */ public String createWherePrimaryKeyStatement(TDTO dto) { return this.createWherePrimaryKeyStatement(dto.getPrimaryKeyString()); } - public String createWherePrimaryKeyStatement(String primaryKeyValue) { return "WHERE "+this.getPrimaryKeyName()+" = '"+primaryKeyValue+"'"; } + /** Example: Id = '0' */ + public String createWherePrimaryKeyStatement(String primaryKeyValue) { return this.getPrimaryKeyName()+" = '"+primaryKeyValue+"'"; } public static List> convertResultSetToDictionaryList(ResultSet resultSet) throws SQLException { diff --git a/core/src/test/java/tests/DhRepoSqliteTest.java b/core/src/test/java/tests/DhRepoSqliteTest.java index dbcf508f3..2b4d0529b 100644 --- a/core/src/test/java/tests/DhRepoSqliteTest.java +++ b/core/src/test/java/tests/DhRepoSqliteTest.java @@ -91,6 +91,10 @@ public class DhRepoSqliteTest Assert.assertEquals("get/insert failed, not equal", insertDto.id, getDto.id); Assert.assertEquals("get/insert failed, not equal", insertDto.value, getDto.value); + // exists - DTO present + Assert.assertTrue("DTO exists failed", testDataRepo.exists(insertDto)); + Assert.assertTrue("DTO exists failed", testDataRepo.existsWithPrimaryKey(insertDto.getPrimaryKeyString())); + // update TestDto updateMetaFile = new TestDto(0, "b"); @@ -110,6 +114,10 @@ public class DhRepoSqliteTest getDto = testDataRepo.getByPrimaryKey("0"); Assert.assertNull("delete failed, not null returned", getDto); + // exists - DTO absent + Assert.assertFalse("DTO exists failed", testDataRepo.exists(insertDto)); + Assert.assertFalse("DTO exists failed", testDataRepo.existsWithPrimaryKey(insertDto.getPrimaryKeyString())); + } catch (SQLException e) {