<commit_before>/*
    This file is part of RIBS2.0 (Robust Infrastructure for Backend Systems).
    RIBS is an infrastructure for building great SaaS applications (but not
    limited to).

    Copyright (C) 2012 Adap.tv, Inc.

    RIBS is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, version 2.1 of the License.

    RIBS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with RIBS.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "mysql_dumper.h"
#include "logger.h"
#include "vmbuf.h"
#include "vmfile.h"
#include "file_writer.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ds.h"
#include "hashtable.h"
#include "file_utils.h"
#include "mysql_common.h"

#define IS_UNSIGNED(f)  ((f) & UNSIGNED_FLAG)

static int report_error(MYSQL *mysql) {
    LOGGER_ERROR("(%d) %s", mysql_errno(mysql), mysql_error(mysql));
    mysql_close(mysql);
    return -1;
}

static int report_stmt_error(MYSQL *mysql, MYSQL_STMT *stmt) {
    LOGGER_ERROR("%s, %s", mysql_error(mysql), mysql_stmt_error(stmt));
    mysql_stmt_close(stmt);
    mysql_close(mysql);
    return -1;
}

static int is_var_length_field(int type) {
    switch(type) {
    case MYSQL_TYPE_VARCHAR:
    case MYSQL_TYPE_VAR_STRING:
    case MYSQL_TYPE_STRING:
    case MYSQL_TYPE_BLOB:
    case MYSQL_TYPE_TINY_BLOB:
    case MYSQL_TYPE_MEDIUM_BLOB:
    case MYSQL_TYPE_LONG_BLOB:
    case MYSQL_TYPE_DECIMAL:
    case MYSQL_TYPE_NEWDECIMAL:
    case MYSQL_TYPE_ENUM:
    case MYSQL_TYPE_SET:
    case MYSQL_TYPE_BIT:
        return 1;
    default:
        return 0;
    }
}

#define MYSQL_TYPE_TO_DS(m, is_un, ts, tus)         \
    case m:                                         \
    return is_un ? ds_type_##tus : ds_type_##ts;

static int get_ds_type(int type, int is_unsigned) {
    switch(type) {
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_TINY, is_unsigned, int8_t, uint8_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_SHORT, is_unsigned, int16_t, uint16_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_LONG, is_unsigned, int32_t, uint32_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_LONGLONG, is_unsigned, int64_t, uint64_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_FLOAT, is_unsigned, float, float);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_DOUBLE, is_unsigned, double, double);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_INT24, is_unsigned, int32_t, uint32_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_YEAR, is_unsigned, uint16_t, uint16_t);
    }
    return -1;
}

#define DS_TYPE_MAPPER_ENUM_TO_STR(t) case ds_type_##t: return (#t);

static const char *get_ds_type_str(int e) {
    switch(e) {
        DS_TYPE_MAPPER_ENUM_TO_STR(int8_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(uint8_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(int16_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(uint16_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(int32_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(uint32_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(int64_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(uint64_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(float);
        DS_TYPE_MAPPER_ENUM_TO_STR(double);
    }
    return "N/A";
}


int mysql_dumper_dump(struct mysql_login_info *mysql_login_info, const char *outputdir, const char *dbname, const char *tablename, const char *query, size_t query_len, struct mysql_dumper_type *types) {
    MYSQL mysql;
    MYSQL_STMT *stmt = NULL;
    mysql_init(&mysql);
    if (NULL == mysql_real_connect(&mysql, mysql_login_info->host, mysql_login_info->user, mysql_login_info->pass, mysql_login_info->db, mysql_login_info->port, NULL, CLIENT_COMPRESS))
        return report_error(&mysql);

    my_bool b_flag = 0;<commit_msg>enable reconnect by default with mysql<commit_after>/*
    This file is part of RIBS2.0 (Robust Infrastructure for Backend Systems).
    RIBS is an infrastructure for building great SaaS applications (but not
    limited to).

    Copyright (C) 2012 Adap.tv, Inc.

    RIBS is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, version 2.1 of the License.

    RIBS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with RIBS.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "mysql_dumper.h"
#include "logger.h"
#include "vmbuf.h"
#include "vmfile.h"
#include "file_writer.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ds.h"
#include "hashtable.h"
#include "file_utils.h"
#include "mysql_common.h"

#define IS_UNSIGNED(f)  ((f) & UNSIGNED_FLAG)

static int report_error(MYSQL *mysql) {
    LOGGER_ERROR("(%d) %s", mysql_errno(mysql), mysql_error(mysql));
    mysql_close(mysql);
    return -1;
}

static int report_stmt_error(MYSQL *mysql, MYSQL_STMT *stmt) {
    LOGGER_ERROR("%s, %s", mysql_error(mysql), mysql_stmt_error(stmt));
    mysql_stmt_close(stmt);
    mysql_close(mysql);
    return -1;
}

static int is_var_length_field(int type) {
    switch(type) {
    case MYSQL_TYPE_VARCHAR:
    case MYSQL_TYPE_VAR_STRING:
    case MYSQL_TYPE_STRING:
    case MYSQL_TYPE_BLOB:
    case MYSQL_TYPE_TINY_BLOB:
    case MYSQL_TYPE_MEDIUM_BLOB:
    case MYSQL_TYPE_LONG_BLOB:
    case MYSQL_TYPE_DECIMAL:
    case MYSQL_TYPE_NEWDECIMAL:
    case MYSQL_TYPE_ENUM:
    case MYSQL_TYPE_SET:
    case MYSQL_TYPE_BIT:
        return 1;
    default:
        return 0;
    }
}

#define MYSQL_TYPE_TO_DS(m, is_un, ts, tus)         \
    case m:                                         \
    return is_un ? ds_type_##tus : ds_type_##ts;

static int get_ds_type(int type, int is_unsigned) {
    switch(type) {
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_TINY, is_unsigned, int8_t, uint8_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_SHORT, is_unsigned, int16_t, uint16_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_LONG, is_unsigned, int32_t, uint32_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_LONGLONG, is_unsigned, int64_t, uint64_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_FLOAT, is_unsigned, float, float);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_DOUBLE, is_unsigned, double, double);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_INT24, is_unsigned, int32_t, uint32_t);
        MYSQL_TYPE_TO_DS(MYSQL_TYPE_YEAR, is_unsigned, uint16_t, uint16_t);
    }
    return -1;
}

#define DS_TYPE_MAPPER_ENUM_TO_STR(t) case ds_type_##t: return (#t);

static const char *get_ds_type_str(int e) {
    switch(e) {
        DS_TYPE_MAPPER_ENUM_TO_STR(int8_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(uint8_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(int16_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(uint16_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(int32_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(uint32_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(int64_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(uint64_t);
        DS_TYPE_MAPPER_ENUM_TO_STR(float);
        DS_TYPE_MAPPER_ENUM_TO_STR(double);
    }
    return "N/A";
}


int mysql_dumper_dump(struct mysql_login_info *mysql_login_info, const char *outputdir, const char *dbname, const char *tablename, const char *query, size_t query_len, struct mysql_dumper_type *types) {
    MYSQL mysql;
    MYSQL_STMT *stmt = NULL;
    mysql_init(&mysql);
    my_bool b_flag = 1;
    if (0 != mysql_options(&mysql, MYSQL_OPT_RECONNECT, (const char *)&b_flag))
        return report_error(&mysql);

    if (NULL == mysql_real_connect(&mysql, mysql_login_info->host, mysql_login_info->user, mysql_login_info->pass, mysql_login_info->db, mysql_login_info->port, NULL, CLIENT_COMPRESS))
        return report_error(&mysql);

    b_flag = 0;
{'fraction_non_alphanumeric': 0.09108837667940342, 'fraction_numerical': 0.013065450511524714, 'mean_word_length': 3.812574139976275, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '722807', 'n_tokens_mistral': 3026, 'n_tokens_neox': 3043, 'n_words': 671}