Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

Commit

Permalink
DATAJDBC-6 added count, countDistinct, exists, notExists methods to Q…
Browse files Browse the repository at this point in the history
…ueryDslJdbcOperations
  • Loading branch information
Thomas Risberg committed Apr 21, 2011
1 parent 3310124 commit b786038
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
Expand Up @@ -19,6 +19,14 @@ public interface QueryDslJdbcOperations {
JdbcOperations getJdbcOperations();

SQLQuery newSqlQuery();

long count(final SQLQuery sqlQuery);

long countDistinct(final SQLQuery sqlQuery);

boolean exists(final SQLQuery sqlQuery);

boolean notExists(final SQLQuery sqlQuery);

<T> T queryForObject(final SQLQuery sqlQuery, final RowMapper<T> rowMapper,
final Expression<?>... cols);
Expand Down
Expand Up @@ -89,6 +89,46 @@ public SQLQuery newSqlQuery() {
return new SQLQueryImpl(this.dialect);
}

public long count(final SQLQuery sqlQuery) {
long count = jdbcTemplate.execute(new ConnectionCallback<Long>() {
public Long doInConnection(Connection con) throws SQLException,
DataAccessException {
SQLQuery liveQuery = sqlQuery.clone(con);
return liveQuery.count();
}});
return count;
}

public long countDistinct(final SQLQuery sqlQuery) {
long count = jdbcTemplate.execute(new ConnectionCallback<Long>() {
public Long doInConnection(Connection con) throws SQLException,
DataAccessException {
SQLQuery liveQuery = sqlQuery.clone(con);
return liveQuery.countDistinct();
}});
return count;
}

public boolean exists(final SQLQuery sqlQuery) {
boolean exists = jdbcTemplate.execute(new ConnectionCallback<Boolean>() {
public Boolean doInConnection(Connection con) throws SQLException,
DataAccessException {
SQLQuery liveQuery = sqlQuery.clone(con);
return liveQuery.exists();
}});
return exists;
}

public boolean notExists(final SQLQuery sqlQuery) {
boolean notExists = jdbcTemplate.execute(new ConnectionCallback<Boolean>() {
public Boolean doInConnection(Connection con) throws SQLException,
DataAccessException {
SQLQuery liveQuery = sqlQuery.clone(con);
return liveQuery.notExists();
}});
return notExists;
}

public <T> T queryForObject(final SQLQuery sqlQuery, final RowMapper<T> rowMapper, final Expression<?>... projection) {
List<T> results = query(sqlQuery, rowMapper, projection);
if (results.size() == 0) {
Expand Down
Expand Up @@ -15,5 +15,13 @@ public interface CustomerDao {
Customer findById(Long id);

List<Customer> findAll();

long countCustomers();

long countDistinctForLastName(String name);

boolean customerExists(Long id);

boolean customerExists(Customer c);

}
Expand Up @@ -75,4 +75,36 @@ public void testDelete() {
Assert.assertEquals(1, customers.size());
}

@Test
public void testCountCustomer() {
Customer c = new Customer();
c.setFirstName("Oliver");
c.setLastName("Gierke");
customerDao.add(c);
long count = customerDao.countCustomers();
Assert.assertEquals(3, count);
}

@Test
public void testCountDistinctCustomer() {
Customer c = new Customer();
c.setFirstName("Mark2");
c.setLastName("Pollack");
customerDao.add(c);
long count = customerDao.countDistinctForLastName(c.getLastName());
Assert.assertEquals(2, count);
}

@Test
public void testCustomerExists() {
Customer c = customerDao.findById(1L);
boolean exists = customerDao.customerExists(c);
Assert.assertTrue(exists);
}

@Test
public void testCustomerNotExists() {
boolean exists = customerDao.customerExists(99L);
Assert.assertFalse(exists);
}
}
Expand Up @@ -80,5 +80,36 @@ public long doInSqlDeleteClause(SQLDeleteClause sqlDeleteClause) {
});
}

@Override
public long countCustomers() {
SQLQuery sqlQuery = template.newSqlQuery()
.from(qCustomer);
return template.count(sqlQuery);
}

@Override
public long countDistinctForLastName(String name) {
SQLQuery sqlQuery = template.newSqlQuery()
.from(qCustomer)
.where(qCustomer.lastName.eq(name));
return template.countDistinct(sqlQuery);
}

@Override
public boolean customerExists(Long id) {
SQLQuery sqlQuery = template.newSqlQuery()
.from(qCustomer)
.where(qCustomer.id.eq(id));
return !template.notExists(sqlQuery);
}

@Override
public boolean customerExists(Customer c) {
SQLQuery sqlQuery = template.newSqlQuery()
.from(qCustomer)
.where(qCustomer.id.eq(c.getId()));
return template.exists(sqlQuery);
}


}

0 comments on commit b786038

Please sign in to comment.