This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class DataExtractor implements RowMapper<DataEventMessage> { | |
protected ObjectMapper objectMapper; | |
private String sql; | |
private String schema; | |
private JdbcTemplate jdbcTemplate; | |
public DataEventMessage extract(final long uniqueId) { | |
return jdbcTemplate.query(sql, this, uniqueId); | |
} | |
public abstract String getPrimaryTable(); | |
public abstract String getSql(); | |
@PostConstruct | |
public void postConstruct() { | |
this.objectMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); | |
this.sql = getSql().replace("<schema>", schema); | |
} | |
@Autowired | |
public DataExtractor setJdbcTemplate(final JdbcTemplate jdbcTemplate) { | |
this.jdbcTemplate = jdbcTemplate; | |
return this; | |
} | |
@Autowired | |
public DataExtractor setSchema(@Value("${database.schema}") final String schema) { | |
this.schema = schema; | |
return this; | |
} | |
public boolean shouldExport() { | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FooExtractor extends DataExtractor { | |
private static final String SQL = "" | |
+ " SELECT bar.baz AS baz," | |
+ " foo.qux AS qux," | |
+ " foo.quux AS quux" | |
+ " FROM <schema>.foo foo" | |
+ " JOIN <schema>.bar bar ON foo.id = bar.foo_id" | |
+ " WHERE foo.id = ?"; | |
@Override | |
public String getPrimaryTable() { | |
return "foo"; | |
} | |
@Override | |
public String getSql() { | |
return SQL; | |
} | |
@Override | |
public DataEventMessage mapRow(final ResultSet rs, final int rowNum) throws SQLException { | |
var message = new DataEventMessage(); | |
message.setBaz(rs.getString("baz")) | |
.setQux(rs.getString("qux")) | |
.setQuux(rs.getString("quux")); | |
return message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment