我有几乎相同的2个方法用于在MySQL表中插入,后者有一个参数更多,即PaymentRequest
。现在,第一种方法在代码库中的很多实例中使用,但没有
PaymentRequest
字段,这会让我感到不安,因为其余的代码也是一样的。
public final void insertOrder(final LocalOrderImpl o, final boolean retryCommit, final boolean withExecutableNominal,
final boolean withIocOrderId) throws SQLException {
Long orderId = null;
if (retryCommit) {
Object[] params =
new Object[]{o.getUserId(), o.getLimit(), o.getNominal(), o.getState(), o.getPause_no_credit(), o.getMarketId(),
o.getType(), o.getExchangeId(), o.getOriginalRate(), o.getShadowOrderId(),
withExecutableNominal ? o.getNominal() : null, o.getIocOrderNumber()};
orderId = insertOrderWithRetry(new InsertOrderWithRetryParams(params, withIocOrderId));
} else if (withIocOrderId && Config.IOC_ORDER_ID_COL) {
sqlProcessor.execute(SqlQuery.INSERT_ORDER_WITH_IOC, o.getUserId(), o.getLimit(), o.getNominal(), o.getState(),
o.getPause_no_credit(), o.getMarketId(), o.getType(), o.getExchangeId(), o.getOriginalRate(), o.getShadowOrderId(),
withExecutableNominal ? o.getNominal() : null, o.getIocOrderNumber());
orderId = readLastOrderId();
} else {
sqlProcessor.execute(SqlQuery.INSERT_ORDER, o.getUserId(), o.getLimit(), o.getNominal(), o.getState(), o.getPause_no_credit(),
o.getMarketId(), o.getType(), o.getExchangeId(), o.getOriginalRate(), o.getShadowOrderId(),
withExecutableNominal ? o.getNominal() : null);
orderId = readLastOrderId();
}
o.setOrderNumber(orderId);
log.debug("Inserted {} order id db: {}, nominal {}, limit {}", o.isBuyOrder() ? "buy" : "sell", o.getOrderId(),
NumberUtils.toExternal(o.getNominal()), NumberUtils.toExternal(o.getLimit()));
}
这是我用PaymentRequest
字段新编写的方法。
/**
* @param o
* @param retryCommit
* @param withExecutableNominal
* @throws SQLException
* @implNote stores the payment request Id in the orderbook table
*/
public final void insertOrder(final LocalOrderImpl o, final PaymentRequest paymentRequest, final boolean retryCommit, final boolean withExecutableNominal,
final boolean withIocOrderId) throws SQLException {
Long orderId = null;
if (retryCommit) {
Object[] params =
new Object[]{paymentRequest.getId(), o.getUserId(), o.getLimit(), o.getNominal(), o.getState(), o.getPause_no_credit(), o.getMarketId(),
o.getType(), o.getExchangeId(), o.getOriginalRate(), o.getShadowOrderId(),
withExecutableNominal ? o.getNominal() : null, o.getIocOrderNumber()};
orderId = insertOrderWithRetry(new InsertOrderWithRetryParams(params, withIocOrderId));
} else if (withIocOrderId && Config.IOC_ORDER_ID_COL) {
sqlProcessor.execute(SqlQuery.INSERT_ORDER_WITH_IOC, paymentRequest.getId(), o.getUserId(), o.getLimit(), o.getNominal(), o.getState(),
o.getPause_no_credit(), o.getMarketId(), o.getType(), o.getExchangeId(), o.getOriginalRate(), o.getShadowOrderId(),
withExecutableNominal ? o.getNominal() : null, o.getIocOrderNumber());
orderId = readLastOrderId();
} else {
sqlProcessor.execute(SqlQuery.INSERT_ORDER, paymentRequest.getId(), o.getUserId(), o.getLimit(), o.getNominal(), o.getState(), o.getPause_no_credit(),
o.getMarketId(), o.getType(), o.getExchangeId(), o.getOriginalRate(), o.getShadowOrderId(),
withExecutableNominal ? o.getNominal() : null);
orderId = readLastOrderId();
}
o.setOrderNumber(orderId);
log.debug("Inserted {} order id db: {}, nominal {}, limit {}", o.isBuyOrder() ? "buy" : "sell", o.getOrderId(),
NumberUtils.toExternal(o.getNominal()), NumberUtils.toExternal(o.getLimit()));
}
SqlQuery.INSERT_ORDER_WITH_IOC
和SqlQuery.INSERT_ORDER
具有与sql.yml
文件中写入的几乎相同的sql查询。有任何建议如何摆脱代码的冗余?
答案 0 :(得分:1)
请参阅:How to Create a Stored Procedure
创建两个存储过程,一个用于
SqlQuery.INSERT_ORDER_WITH_IOC
和SqlQuery.INSERT_ORDER
一个。
使用上面的链接了解如何在mySql中创建存储过程。
然后使用此链接How to use JDBC Callable Statements to Exceute Stored Procedures。这将为您提供有关如何与它们进行交互的信息。
祝你好运!牛仔。