示例#1
0
文件: command.hpp 项目: respu/brig
inline void command::exec(const std::string& sql, const std::vector<column_def>& params)
{
  close_stmt();
  m_stmt = lib::singleton().p_mysql_stmt_init(m_con);
  if (!m_stmt) throw std::runtime_error("MySQL error");
  check(lib::singleton().p_mysql_stmt_prepare(m_stmt, sql.c_str(), (unsigned long)sql.size()) == 0);

  std::vector<MYSQL_BIND> binds(params.size());
  if (!binds.empty())
  {
    memset(binds.data(), 0, binds.size() * sizeof(MYSQL_BIND));
    for (size_t i(0); i < params.size(); ++i)
      bind_param(params[i].query_value, binds[i]);
    check(lib::singleton().p_mysql_stmt_bind_param(m_stmt, binds.data()) == 0);
  }

  check(lib::singleton().p_mysql_stmt_execute(m_stmt) == 0);
}
示例#2
0
//=========================================================================
bool SQLiteQuery::exec(const scx::ScriptRef* args)
{
  SQLiteQuery_DEBUG_LOG("{" << m_profile.object()->m_name <<
                        "} exec: " << m_query);
  
  const scx::ScriptList* argsl = 
    (args ? dynamic_cast<const scx::ScriptList*>(args->object()) : 0);

  if (m_invalid) {
    // If the query is invalid, don't go any further
    return false;
  }

  ::sqlite3_reset(m_stmt);
  
  // If the statement has parameters, check and bind
  int params = ::sqlite3_bind_parameter_count(m_stmt);
  if (params > 0) {
    ::sqlite3_clear_bindings(m_stmt);
    if (!argsl || argsl->size() != params) {
      log_error("Incorrect number of parameters", false);
      return false;
    }
    for (int i=0; i<params; ++i) {
      bind_param(1+i, argsl->get(i));
    }
  }

  // Execute the statement
  m_step_err = ::sqlite3_step(m_stmt);
  if (m_step_err != SQLITE_DONE &&
      m_step_err != SQLITE_ROW) {
    log_error("sqlite3_step (init)", false);
  }
  m_first = true;
  
  return true;
}