Add Repo.exists()
This commit is contained in:
@@ -98,6 +98,13 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
|
||||
|
||||
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<String, Object> 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<TDTO extends IBaseDTO>
|
||||
// helper methods //
|
||||
//================//
|
||||
|
||||
/** Example: <code> Id = '0' </code> */
|
||||
public String createWherePrimaryKeyStatement(TDTO dto) { return this.createWherePrimaryKeyStatement(dto.getPrimaryKeyString()); }
|
||||
public String createWherePrimaryKeyStatement(String primaryKeyValue) { return "WHERE "+this.getPrimaryKeyName()+" = '"+primaryKeyValue+"'"; }
|
||||
/** Example: <code> Id = '0' </code> */
|
||||
public String createWherePrimaryKeyStatement(String primaryKeyValue) { return this.getPrimaryKeyName()+" = '"+primaryKeyValue+"'"; }
|
||||
|
||||
public static List<Map<String, Object>> convertResultSetToDictionaryList(ResultSet resultSet) throws SQLException
|
||||
{
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user