#include "stdafx.h"

#include "sql_batch_exec.h"
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <memory>

namespace sql
{


void SqlBatchExec::operator()(sql::Statement *stmt, std::list<std::string> &statements)
{
  _batch_exec_success_count= 0;
  _batch_exec_err_count= 0;
  _batch_exec_progress_state= 0;
  _batch_exec_progress_inc= 1.f / statements.size();

  for (std::list<std::string>::const_iterator i= statements.begin(), i_end= statements.end(); i != i_end; ++i)
  {
    try
    {
      stmt->execute(*i);
      std::auto_ptr<sql::ResultSet> rs(stmt->getResultSet());
      ++_batch_exec_success_count;
    }
    catch (SQLException &e)
    {
      ++_batch_exec_err_count;
      _error_cb(e.getErrorCode(), e.what(), *i);
      if (_error_cb.empty())
        throw;
    }
    _batch_exec_progress_state+= _batch_exec_progress_inc;
    _batch_exec_progress_cb(_batch_exec_progress_state);
  }

  _batch_exec_stat_cb(_batch_exec_success_count, _batch_exec_err_count);
}


} // namespace sql
