Arefe
Arefe

Reputation: 12417

Remove the redundancy for the database insertation

I have almost same 2 methods that are used to insert in a MySQL table where the later have one parameter more namely the PaymentRequest. Now, the first method is used in quite a few instances in the codebase that doesn't have the PaymentRequest field and this bugs me as rest of the code is just the same.

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()));
    }

This is the method that I newly wrote with the PaymentRequest field.

    /**
     * @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()));
    }

The SqlQuery.INSERT_ORDER_WITH_IOC and SqlQuery.INSERT_ORDER has the almost same sql queries written in the sql.yml file. Any suggestion how to get rid of the redundency of the code?

Upvotes: 0

Views: 62

Answers (1)

Cowboy Farnz
Cowboy Farnz

Reputation: 329

Please see : How to Create a Stored Procedure

Create two stored procedures, one for SqlQuery.INSERT_ORDER_WITH_IOC and one for SqlQuery.INSERT_ORDER.

Use the above link to find out how to create the stored procedure in mySql.

Then use this link How to use JDBC Callable Statements to Exceute Stored Procedures. This will give you information on how to interact with them.

Good luck!

The Cowboy.

Upvotes: 1

Related Questions