<commit_before>/*
 * MySQLConnect.cpp
 *
 *  Copyright (c) 2019, hikyuu.org
 *
 *  Created on: 2019-8-17
 *      Author: fasiondog
 */

#include "MySQLConnect.h"

namespace hku {

MySQLConnect::MySQLConnect(const Parameter& param) noexcept
: DBConnectBase(param), m_mysql(nullptr) {
    m_mysql = new MYSQL;
    try {
        HKU_CHECK(m_mysql, "Failed new MYSQL instance!");
        HKU_CHECK(mysql_init(m_mysql) != NULL, "Initial MySQL handle error!");

        string host = getParamFromOther<string>(param, "host", "127.0.0.1");
        string usr = getParamFromOther<string>(param, "usr", "root");
        string pwd = getParamFromOther<string>(param, "pwd", "");
        string database = getParamFromOther<string>(param, "db", "");
        unsigned int port = getParamFromOther<int>(param, "port", 3306);
        HKU_TRACE("MYSQL host: {}", host);
        HKU_TRACE("MYSQL port: {}", port);
        HKU_TRACE("MYSQL database: {}", database);

        my_bool reconnect = 1;
        HKU_CHECK(mysql_options(m_mysql, MYSQL_OPT_RECONNECT, &reconnect) == 0,
                  "Failed set reconnect options");
        HKU_CHECK(mysql_real_connect(m_mysql, host.c_str(), usr.c_str(), pwd.c_str(),
                                     database.c_str(), port, NULL, CLIENT_MULTI_STATEMENTS) != NULL,
                  "Failed to connect to database! {}", mysql_error(m_mysql));
        HKU_CHECK(mysql_set_character_set(m_mysql, "utf8") == 0, "mysql_set_character_set error!");
    } catch (std::exception& e) {
        HKU_FATAL(e.what());
        close();
    } catch (...) {
        HKU_FATAL("Unknown error!");
        close();
    }
}

MySQLConnect::~MySQLConnect() {
    close();
}

void MySQLConnect::close() {
    if (m_mysql) {
        mysql_close(m_mysql);
        delete m_mysql;
        m_mysql = nullptr;
    }
}

bool MySQLConnect::ping() {
    if (!m_mysql) {
        return false;
    }

    auto ret = mysql_ping(m_mysql);
    if (ret) {
        HKU_ERROR("mysql_ping error code: {}, msg: {}", ret, mysql_error(m_mysql));
        return false;
    }
    return true;
}

void MySQLConnect::exec(const string& sql_string) {
    HKU_CHECK(m_mysql, "mysql connect is invalid!");
    int ret = mysql_query(m_mysql, sql_string.c_str());
    if (CR_SERVER_GONE_ERROR == ret) {
        // 尝试重新连接
        if (ping()) {
            ret = mysql_query(m_mysql, sql_string.c_str());
        } else {
            HKU_THROW("SQL error：{}! error code：{}, error msg: {}", sql_string, ret,
                      mysql_error(m_mysql));
        }
    }

    if (ret) {
        HKU_THROW("SQL error：{}! error code：{}, error msg: {}", sql_string, ret,
                  mysql_error(m_mysql));
    }

    do {
        MYSQL_RES* result = mysql_store_result(m_mysql);
        if (result) {
            auto num_fields = mysql_num_fields(result);
            HKU_TRACE("num_fields: {}", num_fields);
            mysql_free_result(result);
        } else {
            if (mysql_field_count(m_mysql) == 0) {
                auto num_rows = mysql_affected_rows(m_mysql);
                HKU_TRACE("num_rows: {}", num_rows);
            } else {
                HKU_THROW("mysql_field_count error：{}! error code：{}, error msg: {}", sql_string,
                          ret, mysql_error(m_mysql));
            }
        }
    } while (!mysql_next_result(m_mysql));
}<commit_msg>fixed compile error on linux<commit_after>/*
 * MySQLConnect.cpp
 *
 *  Copyright (c) 2019, <EMAIL>
 *
 *  Created on: 2019-8-17
 *      Author: fasiondog
 */

#include "MySQLConnect.h"

namespace hku {

MySQLConnect::MySQLConnect(const Parameter& param) noexcept
: DBConnectBase(param), m_mysql(nullptr) {
    m_mysql = new MYSQL;
    try {
        HKU_CHECK(m_mysql, "Failed new MYSQL instance!");
        HKU_CHECK(mysql_init(m_mysql) != NULL, "Initial MySQL handle error!");

        string host = getParamFromOther<string>(param, "host", "127.0.0.1");
        string usr = getParamFromOther<string>(param, "usr", "root");
        string pwd = getParamFromOther<string>(param, "pwd", "");
        string database = getParamFromOther<string>(param, "db", "");
        unsigned int port = getParamFromOther<int>(param, "port", 3306);
        HKU_TRACE("MYSQL host: {}", host);
        HKU_TRACE("MYSQL port: {}", port);
        HKU_TRACE("MYSQL database: {}", database);

        my_bool reconnect = 1;
        HKU_CHECK(mysql_options(m_mysql, MYSQL_OPT_RECONNECT, &reconnect) == 0,
                  "Failed set reconnect options");
        HKU_CHECK(mysql_real_connect(m_mysql, host.c_str(), usr.c_str(), pwd.c_str(),
                                     database.c_str(), port, NULL, CLIENT_MULTI_STATEMENTS) != NULL,
                  "Failed to connect to database! {}", mysql_error(m_mysql));
        HKU_CHECK(mysql_set_character_set(m_mysql, "utf8") == 0, "mysql_set_character_set error!");
    } catch (std::exception& e) {
        HKU_FATAL(e.what());
        close();
    } catch (...) {
        HKU_FATAL("Unknown error!");
        close();
    }
}

MySQLConnect::~MySQLConnect() {
    close();
}

void MySQLConnect::close() {
    if (m_mysql) {
        mysql_close(m_mysql);
        delete m_mysql;
        m_mysql = nullptr;
    }
}

bool MySQLConnect::ping() {
    if (!m_mysql) {
        return false;
    }

    auto ret = mysql_ping(m_mysql);
    if (ret) {
        HKU_ERROR("mysql_ping error code: {}, msg: {}", ret, mysql_error(m_mysql));
        return false;
    }
    return true;
}

void MySQLConnect::exec(const string& sql_string) {
    HKU_CHECK(m_mysql, "mysql connect is invalid!");
    int ret = mysql_query(m_mysql, sql_string.c_str());
    if (ret) {
        // 尝试重新连接
        if (ping()) {
            ret = mysql_query(m_mysql, sql_string.c_str());
        } else {
            HKU_THROW("SQL error：{}! error code：{}, error msg: {}", sql_string, ret,
                      mysql_error(m_mysql));
        }
    }

    if (ret) {
        HKU_THROW("SQL error：{}! error code：{}, error msg: {}", sql_string, ret,
                  mysql_error(m_mysql));
    }

    do {
        MYSQL_RES* result = mysql_store_result(m_mysql);
        if (result) {
            // auto num_fields = mysql_num_fields(result);
            // HKU_TRACE("num_fields: {}", num_fields);
            mysql_num_fields(result);
            mysql_free_result(result);
        } else {
            if (mysql_field_count(m_mysql) == 0) {
                auto num_rows = mysql_affected_rows(m_mysql);
                HKU_TRACE("num_rows: {}", num_rows);
            } else {
                HKU_THROW("mysql_field_count error：{}! error code：{}, error msg: {}", sql_string,
                          ret, mysql_error(m_mysql));
            }
        }
    } while (!mysql_next_result(m_mysql));
}
{'fraction_non_alphanumeric': 0.14311058545239502, 'fraction_numerical': 0.00768775872264932, 'mean_word_length': 2.40463009562154, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3520977', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2220, 'n_words': 438}