All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.espertech.esperio.db.core.DMLStatement Maven / Gradle / Ivy

There is a newer version: 9.0.0
Show newest version
/*
 ***************************************************************************************
 *  Copyright (C) 2006 EsperTech, Inc. All rights reserved.                            *
 *  http://www.espertech.com/esper                                                     *
 *  http://www.espertech.com                                                           *
 *  ---------------------------------------------------------------------------------- *
 *  The software in this package is published under the terms of the GPL license       *
 *  a copy of which has been included with this distribution in the license.txt file.  *
 ***************************************************************************************
 */
package com.espertech.esperio.db.core;

import com.espertech.esper.common.client.EventBean;
import com.espertech.esper.common.internal.util.ExecutionPathDebugLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

public class DMLStatement {
    private final static Logger log = LoggerFactory.getLogger(DMLStatement.class);

    private final StoreExceptionHandler storeExceptionHandler;
    private final String dmlSQL;
    private final Map bindings;

    public DMLStatement(StoreExceptionHandler storeExceptionHandler, String dmlSQL, Map bindings) {
        this.storeExceptionHandler = storeExceptionHandler;
        this.dmlSQL = dmlSQL;
        this.bindings = bindings;
    }

    public void execute(Connection connection, EventBean eventBean) {
        PreparedStatement statement = null;
        try {
            if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) {
                log.debug("Executing '" + dmlSQL + ")");
            }
            statement = connection.prepareStatement(dmlSQL);
            for (Map.Entry entry : bindings.entrySet()) {
                Object value = entry.getValue().getGetter().get(eventBean);
                statement.setObject(entry.getKey(), value);
            }

            int rows = statement.executeUpdate();
            if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) {
                log.debug("Execution yielded " + rows + " rows");
            }
        } catch (SQLException ex) {
            String message = "Failed to invoke : " + dmlSQL + " :" + ex.getMessage();
            log.error(message, ex);
            storeExceptionHandler.handle(message, ex);
            throw new StoreExceptionDBRel(message, ex);
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (SQLException e) {
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy