001/* 
002 * JKNIV, whinstone one contract to access your database.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.whinstone.jdbc.commands;
021
022import java.sql.Connection;
023import java.sql.PreparedStatement;
024import java.sql.ResultSet;
025import java.sql.SQLException;
026
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import net.sf.jkniv.exception.HandleableException;
031import net.sf.jkniv.whinstone.Queryable;
032import net.sf.jkniv.whinstone.commands.Command;
033import net.sf.jkniv.whinstone.commands.CommandHandler;
034import net.sf.jkniv.whinstone.statement.StatementAdapter;
035
036public abstract class AbstractJdbcCommand implements Command
037{
038    private final static Logger                    LOG = LoggerFactory.getLogger(AbstractJdbcCommand.class);
039    protected HandleableException                  handlerException;
040    protected CommandHandler                       commandHandler;
041    protected StatementAdapter<?, ResultSet> stmt;
042    protected final Connection                     conn;
043    protected final Queryable                      queryable;
044    
045    @SuppressWarnings({ "unchecked", "rawtypes" })
046    protected AbstractJdbcCommand(Queryable queryable, Connection conn)
047    {
048        this.queryable = queryable;
049        this.conn = conn;
050    }
051    
052    @Override
053    public <T> Command with(T stmt)
054    {
055        this.stmt = (StatementAdapter)stmt;
056        return this;
057    }
058    
059    @Override
060    public Command with(HandleableException handlerException)
061    {
062        this.handlerException = handlerException;
063        return this;
064    }
065    
066    @Override
067    public Command with(CommandHandler commandHandler)
068    {
069        this.commandHandler = commandHandler;
070        return this;
071    }
072    
073    protected int executeBulk()
074    {
075        return queryable.bind(stmt).onBulk();
076    }
077    
078    protected int simpleExecute()
079    {
080        queryable.bind(stmt).on();
081        return stmt.execute();
082    }
083    
084    protected void close(PreparedStatement stmt)
085    {
086        if (stmt != null)// TODO test me (create test with FOR EACH to insert many rows, Cannot insert record. ORA-01000: maximo de cursores abertos excedido
087        {
088            try
089            {
090                stmt.close();
091            }
092            catch (SQLException ignore)
093            {
094                LOG.warn("Cannot close PreparedStament state: {}, errorCode: {}, message: {}", ignore.getSQLState(),
095                        ignore.getErrorCode(), ignore.getMessage());
096            }
097        }
098    }
099}