Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Created May 23, 2018 11:34
Show Gist options
  • Save bdarfler/aee1b11956babd8887c99360100ad73f to your computer and use it in GitHub Desktop.
Save bdarfler/aee1b11956babd8887c99360100ad73f to your computer and use it in GitHub Desktop.
final Collection<String> parts = buildQueryParts( dbObj );
if ( parts.isEmpty() ) { return ImmutableList.of(); }
final String query = "SELECT " + columnNames + " FROM " + dbObj.getTableName() + " WHERE " + join( parts, " AND " );
return result = simpleJdbcTemplate.query( query, new BeanPropertyRowMapper<DbObj>( dbObj.getClass() ), new BeanPropertySqlParameterSource( dbObj ) );
private static final Collection<String> buildQueryParts( final LocaDbObj dbObj ) {
 return transform( filter( dbObj.getFieldNames(), new HasValue( dbObj.toMap() ) ), new BuildPart( dbObj ) );
}
private static final class HasValue implements Predicate<String> {
 private final Map<String, Object> map;
 public HasValue( final Map<String, Object> map ) {
 this.map = map;
 }
 public boolean apply( final String propName ) {
 return map.get( propName ) != null;
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment