Skip to content

Commit

Permalink
需要增加一个是否存在的查询API:exists (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Dec 22, 2022
1 parent fed8812 commit 9a7ceb2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,6 @@ public interface BaseRepository<T> extends Typed<T> {
*/
<T> void findToHandle(Criteria criteria, RowHandler<T> handler);


boolean exists(Criteria criteria);

}
2 changes: 2 additions & 0 deletions sqli-core/src/main/java/io/xream/sqli/core/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ public interface Repository extends QueryForCache {
void findToHandle(Criteria.ResultMapCriteria resultMapCriteria, RowHandler<Map<String, Object>> handler);

<T> List<T> listByClzz(Class<T> clzz);

boolean exists(Criteria criteria);
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,8 @@ public void findToHandle(Criteria.ResultMapCriteria criteria, RowHandler<Map<Str
this.dao.findToHandle(criteria,handler);
}

@Override
public boolean exists(Criteria criteria) {
return this.dao.exists(criteria);
}
}
2 changes: 2 additions & 0 deletions sqli-repo/src/main/java/io/xream/sqli/repository/dao/Dao.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ List<Map<String,Object>> list(String sql,

<T> void findToHandle(Criteria criteria, RowHandler<T> handler);
void findToHandle(Criteria.ResultMapCriteria resultMapCriteria, RowHandler<Map<String, Object>> handler);

boolean exists(Criteria criteria);
}
15 changes: 15 additions & 0 deletions sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,21 @@ public <T> void findToHandle(Criteria criteria, RowHandler<T> handler) {
this.jdbcHelper.queryForMapToHandle(sql, valueList, dialect,null, Parser.get(clz), handler);
}

@Override
public boolean exists(Criteria criteria) {

Class clz = criteria.getClzz();
List<Object> valueList = new ArrayList<>();
SqlBuilt sqlBuilt = sqlBuilder.buildQueryByCriteria(valueList,criteria, criteriaToSql, dialect);
String sql = sqlBuilt.getSql().toString();
sql = sql.replace("*","1");
sql += " LIMIT 1";
SqliLoggerProxy.debug(clz, sql);

List<Long> list = this.jdbcHelper.queryForPlainValueList(Long.class, sql, valueList,this.dialect);

return ! list.isEmpty();
}

private boolean update(String sql, Collection<Object> list, Dialect dialect, JdbcHelper jdbcHelper) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ public <T> void findToHandle(Criteria criteria, RowHandler<T> handler) {
this.repository.findToHandle(criteria,handler);
}

@Override
public boolean exists(Criteria criteria) {
assertCriteriaClzz(criteria);
this.setDefaultClzz(criteria);
return this.repository.exists(criteria);
}

@Override
public Page<Map<String, Object>> find(Criteria.ResultMapCriteria resultMapCriteria) {
Expand Down

0 comments on commit 9a7ceb2

Please sign in to comment.