160 lines
5.3 KiB
Java
160 lines
5.3 KiB
Java
|
|
package com.stephenschafer.budget;
|
||
|
|
|
||
|
|
import java.math.BigDecimal;
|
||
|
|
import java.sql.Date;
|
||
|
|
import java.sql.SQLException;
|
||
|
|
import java.util.Optional;
|
||
|
|
import java.util.function.Consumer;
|
||
|
|
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||
|
|
import org.springframework.jdbc.core.PreparedStatementCreator;
|
||
|
|
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
|
||
|
|
@Slf4j
|
||
|
|
public class TransactionDaoImpl implements TransactionDao {
|
||
|
|
@Autowired
|
||
|
|
private JdbcTemplate jdbcTemplate;
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Optional<Transaction> getById(final String year, final int id) {
|
||
|
|
final String sql = ("select"
|
||
|
|
+ " source, unique_identifier, type, description, extra_description, date, amount, optional, regex_id"
|
||
|
|
+ " from budget_${year}.transaction where id = ?").replace("${year}", year);
|
||
|
|
return jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
|
||
|
|
int i = 0;
|
||
|
|
final String source = rs.getString(++i);
|
||
|
|
final String uniqueIdentifier = rs.getString(++i);
|
||
|
|
final String type = rs.getString(++i);
|
||
|
|
final String description = rs.getString(++i);
|
||
|
|
final String extraDescription = rs.getString(++i);
|
||
|
|
final Date date = rs.getDate(++i);
|
||
|
|
final BigDecimal amount = rs.getBigDecimal(++i);
|
||
|
|
final int optional = rs.getInt(++i);
|
||
|
|
final int regexId = rs.getInt(++i);
|
||
|
|
return Optional.of(new Transaction(id, source, uniqueIdentifier, type, description,
|
||
|
|
extraDescription, date, amount, optional, regexId));
|
||
|
|
}, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void update(final String year, final Transaction transaction) {
|
||
|
|
// TODO Auto-generated method stub
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void getAll(final String year, final Consumer<Transaction> consumer) {
|
||
|
|
final PreparedStatementHolder holder = new PreparedStatementHolder();
|
||
|
|
final String sql = ("select"
|
||
|
|
+ " id, source, unique_identifier, type, description, extra_description, date, amount, optional, regex_id"
|
||
|
|
+ " from budget_${year}.transaction" + " order by id").replace("${year}", year);
|
||
|
|
final PreparedStatementCreator creator = connection -> {
|
||
|
|
holder.statement = connection.prepareStatement(sql);
|
||
|
|
return holder.statement;
|
||
|
|
};
|
||
|
|
try {
|
||
|
|
jdbcTemplate.query(creator, rs -> {
|
||
|
|
int i = 0;
|
||
|
|
final int id = rs.getInt(++i);
|
||
|
|
final String source = rs.getString(++i);
|
||
|
|
final String uniqueIdentifier = rs.getString(++i);
|
||
|
|
final String type = rs.getString(++i);
|
||
|
|
final String description = rs.getString(++i);
|
||
|
|
final String extraDescription = rs.getString(++i);
|
||
|
|
final Date date = rs.getDate(++i);
|
||
|
|
final BigDecimal amount = rs.getBigDecimal(++i);
|
||
|
|
final int optional = rs.getInt(++i);
|
||
|
|
final int regexId = rs.getInt(++i);
|
||
|
|
consumer.accept(new Transaction(id, source, uniqueIdentifier, type, description,
|
||
|
|
extraDescription, date, amount, optional, regexId));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
catch (final StopException e) {
|
||
|
|
try {
|
||
|
|
holder.statement.cancel();
|
||
|
|
}
|
||
|
|
catch (final SQLException e1) {
|
||
|
|
log.error("getByCategory failed", e1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void getByCategory(final String year, final int categoryId,
|
||
|
|
final Consumer<Transaction> consumer) {
|
||
|
|
// TODO Auto-generated method stub
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void getBySource(final String year, final String source,
|
||
|
|
final Consumer<Transaction> consumer) {
|
||
|
|
// TODO Auto-generated method stub
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void getByRegexId(final String year, final Integer regexId,
|
||
|
|
final Consumer<Transaction> consumer) {
|
||
|
|
final PreparedStatementHolder holder = new PreparedStatementHolder();
|
||
|
|
final String sql = ("select"
|
||
|
|
+ " id, source, unique_identifier, type, description, extra_description, date, amount, optional"
|
||
|
|
+ " from budget_${year}.transaction where regex_id = ?" + " order by id").replace(
|
||
|
|
"${year}", year);
|
||
|
|
final PreparedStatementCreator creator = connection -> {
|
||
|
|
holder.statement = connection.prepareStatement(sql);
|
||
|
|
holder.statement.setInt(1, regexId.intValue());
|
||
|
|
return holder.statement;
|
||
|
|
};
|
||
|
|
try {
|
||
|
|
jdbcTemplate.query(creator, rs -> {
|
||
|
|
int i = 0;
|
||
|
|
final int id = rs.getInt(++i);
|
||
|
|
final String source = rs.getString(++i);
|
||
|
|
final String uniqueIdentifier = rs.getString(++i);
|
||
|
|
final String type = rs.getString(++i);
|
||
|
|
final String description = rs.getString(++i);
|
||
|
|
final String extraDescription = rs.getString(++i);
|
||
|
|
final Date date = rs.getDate(++i);
|
||
|
|
final BigDecimal amount = rs.getBigDecimal(++i);
|
||
|
|
final int optional = rs.getInt(++i);
|
||
|
|
consumer.accept(new Transaction(id, source, uniqueIdentifier, type, description,
|
||
|
|
extraDescription, date, amount, optional, regexId));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
catch (final StopException e) {
|
||
|
|
try {
|
||
|
|
holder.statement.cancel();
|
||
|
|
}
|
||
|
|
catch (final SQLException e1) {
|
||
|
|
log.error("getByCategory failed", e1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void getSources(final String year, final Consumer<String> consumer) {
|
||
|
|
final PreparedStatementHolder holder = new PreparedStatementHolder();
|
||
|
|
final String sql = "select source from budget_${year}.transaction group by source order by source".replace(
|
||
|
|
"${year}", year);
|
||
|
|
final PreparedStatementCreator creator = connection -> {
|
||
|
|
holder.statement = connection.prepareStatement(sql);
|
||
|
|
return holder.statement;
|
||
|
|
};
|
||
|
|
try {
|
||
|
|
jdbcTemplate.query(creator, rs -> {
|
||
|
|
int i = 0;
|
||
|
|
final String source = rs.getString(++i);
|
||
|
|
consumer.accept(source);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
catch (final StopException e) {
|
||
|
|
try {
|
||
|
|
holder.statement.cancel();
|
||
|
|
}
|
||
|
|
catch (final SQLException e1) {
|
||
|
|
log.error("getSources failed", e1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|