Esempio n. 1
0
    void Connection::open(const char* app, const char* host, const char* user,
      const char* passwd, const char* db, unsigned int port,
      const char* unix_socket, unsigned long client_flag)
    {
      log_debug("mysql_real_connect(MYSQL, "
        << str(app) << ", "
        << str(host) << ", "
        << str(user) << ", "
        << str(passwd) << ", "
        << str(db) << ", "
        << port << ", "
        << str(unix_socket) << ", "
        << client_flag << ')');

      if (::mysql_init(&mysql) == 0)
        throw std::runtime_error("cannot initalize mysql");
      initialized = true;

      if (::mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, app && app[0] ? app : "tntdb") != 0)
        throw MysqlError("mysql_options", &mysql);

      if (!::mysql_real_connect(&mysql, zstr(host), zstr(user), zstr(passwd),
                                zstr(db), port, zstr(unix_socket), client_flag))
        throw MysqlError("mysql_real_connect", &mysql);
    }
Esempio n. 2
0
    Connection::size_type Connection::execute(const std::string& query)
    {
      log_debug("mysql_query(\"" << query << "\")");
      if (::mysql_query(&mysql, query.c_str()) != 0)
        throw MysqlError("mysql_query", &mysql);

      log_debug("mysql_affected_rows(" << &mysql << ')');
      return ::mysql_affected_rows(&mysql);
    }
Esempio n. 3
0
    tntdb::Result Connection::select(const std::string& query)
    {
      execute(query);

      log_debug("mysql_store_result(" << &mysql << ')');
      MYSQL_RES* res = ::mysql_store_result(&mysql);
      if (res == 0)
        throw MysqlError("mysql_store_result", &mysql);

      return tntdb::Result(new Result(tntdb::Connection(this), &mysql, res));
    }
Esempio n. 4
0
    void Connection::beginTransaction()
    {
      if (transactionActive == 0)
      {
        log_debug("mysql_autocomit(" << &mysql << ", " << 0 << ')');
        if (::mysql_autocommit(&mysql, 0) != 0)
          throw MysqlError("mysql_autocommit", &mysql);
      }

      ++transactionActive;
    }
Esempio n. 5
0
    void Connection::rollbackTransaction()
    {
      if (transactionActive == 0 || --transactionActive == 0)
      {
        log_debug("mysql_rollback(" << &mysql << ')');
        if (::mysql_rollback(&mysql) != 0)
          throw MysqlError("mysql_rollback", &mysql);

        if (!lockTablesQuery.empty())
        {
          log_debug("mysql_query(\"UNLOCK TABLES\")");
          if (::mysql_query(&mysql, "UNLOCK TABLES") != 0)
            throw MysqlError("mysql_query", &mysql);
          lockTablesQuery.clear();
        }

        log_debug("mysql_autocommit(" << &mysql << ", " << 1 << ')');
        if (::mysql_autocommit(&mysql, 1) != 0)
          throw MysqlError("mysql_autocommit", &mysql);

      }
    }
Esempio n. 6
0
    Row Result::getRow(size_type tup_num) const
    {
      log_debug("mysql_data_seek(" << tup_num << ')');
      ::mysql_data_seek(result, tup_num);

      log_debug("mysql_fetch_row");
      MYSQL_ROW row = ::mysql_fetch_row(result);
      if (row == 0)
        throw MysqlError("mysql_fetch_row", mysql);

      const IResult* resc = this;
      IResult* res = const_cast<IResult*>(resc);
      return Row(new ResultRow(tntdb::Result(res), result, row));
    }
Esempio n. 7
0
    void Connection::lockTable(const std::string& tablename, bool exclusive)
    {
      if (lockTablesQuery.empty())
        lockTablesQuery = "LOCK TABLES ";
      else
        lockTablesQuery += ", ";

      lockTablesQuery += tablename;
      lockTablesQuery += exclusive ? " WRITE" : " READ";

      log_debug("mysql_query(\"" << lockTablesQuery << "\")");
      if (::mysql_query(&mysql, lockTablesQuery.c_str()) != 0)
        throw MysqlError("mysql_query", &mysql);
    }
Esempio n. 8
0
    Connection::~Connection()
    {
      if (initialized)
      {
        clearStatementCache();

        if (!lockTablesQuery.empty())
        {
          log_debug("mysql_query(\"UNLOCK TABLES\")");
          if (::mysql_query(&mysql, "UNLOCK TABLES") != 0)
            log_warn(MysqlError("mysql_query", &mysql).what());
        }

        log_debug("mysql_close(" << &mysql << ')');
        ::mysql_close(&mysql);
      }
    }