Skip to content

Commit

Permalink
Merge pull request #370 from maxbube/config_file
Browse files Browse the repository at this point in the history
Adding config file functionality
  • Loading branch information
davidducos committed Aug 3, 2021
2 parents 7b392b1 + cb7a07e commit bc7ab57
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
55 changes: 55 additions & 0 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <mysql.h>
#include <glib.h>


char * checksum_table(MYSQL *conn, char *database, char *table, int *errn){
MYSQL_RES *result = NULL;
MYSQL_ROW row;
Expand All @@ -22,3 +23,57 @@ char * checksum_table(MYSQL *conn, char *database, char *table, int *errn){
mysql_free_result(result);
return r;
}

void load_config_file(gchar * cf, GOptionContext *context, const gchar * group, GString *ss){
GError *error = NULL;
GKeyFile *kf = g_key_file_new ();
if (!g_key_file_load_from_file (kf, cf,
G_KEY_FILE_KEEP_COMMENTS, &error)) {
g_warning ("Failed to load config file: %s", error->message);
return;
}
gsize len=0;
gchar ** keys=g_key_file_get_keys(kf,group, &len, &error);
gsize i=0;
GSList *list = NULL;
for (i=0; i < len; i++){
list = g_slist_append(list, g_strdup_printf("--%s",keys[i]));
gchar *value=g_key_file_get_value(kf,group,keys[i],&error);
if ( value != NULL ) list=g_slist_append(list, value);
}
gint slen = g_slist_length(list) + 1;
gchar ** gclist = g_new0(gchar *, slen);
GSList *ilist=list;
gint j=0;
for (j=1; j < slen ; j++){
gclist[j]=ilist->data;
ilist=ilist->next;
}
g_slist_free(list);
if (!g_option_context_parse(context, &slen, &gclist, &error)) {
g_print("option parsing failed: %s, try --help\n", error->message);
exit(EXIT_FAILURE);
}else{
g_message("Config file loaded");
}
gchar * group_variables=g_strdup_printf("%s_variables",group);
keys=g_key_file_get_keys(kf,group_variables, &len, &error);
for (i=0; i < len; i++){
gchar *value=g_key_file_get_value(kf,group_variables,keys[i],&error);
g_string_append_printf(ss,"SET SESSION %s = %s ;\n",keys[i],value);
}
}

void execute_gstring(MYSQL *conn, GString *ss)
{
if (ss != NULL ){
gchar** line=g_strsplit(ss->str, ";\n", -1);
int i=0;
for (i=0; i < (int)g_strv_length(line);i++){
if (strlen(line[i]) > 3 && mysql_query(conn, line[i])){
g_warning("Set session failed: %s | ",line[i]);
}
}
}
}

6 changes: 6 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ char *cipher = NULL;
char *tls_version = NULL;
gchar *ssl_mode = NULL;
#endif
gchar *config_file;
GString *set_session=NULL;

FILE * (*m_open)(const char *filename, const char *);
int (*m_close)(void *file) = NULL;
void load_config_file(gchar * cf, GOptionContext *context, const gchar * group, GString *ss);
void execute_gstring(MYSQL *conn, GString *ss);

gboolean askPassword = FALSE;
guint port = 0;
Expand Down Expand Up @@ -87,6 +91,8 @@ GOptionEntry common_entries[] = {
{"tls-version", 0, 0, G_OPTION_ARG_STRING, &tls_version,
"Which protocols the server permits for encrypted connections", NULL},
#endif
{ "config", 0, 0, G_OPTION_ARG_STRING, &config_file,
"Configuration file", NULL },
{NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};

#endif
Expand Down
14 changes: 11 additions & 3 deletions mydumper.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ gboolean have_snapshot_cloning = FALSE;

gchar *ignore_engines = NULL;
char **ignore = NULL;

gchar *tables_list = NULL;
gchar *tidb_snapshot = NULL;
GSequence *tables_skiplist = NULL;
Expand Down Expand Up @@ -566,6 +565,7 @@ void *process_queue(struct thread_data *td) {
g_message("Thread %d connected using MySQL connection ID %lu",
td->thread_id, mysql_thread_id(thrconn));
}
execute_gstring(thrconn, set_session);

if (use_savepoints && mysql_query(thrconn, "SET SQL_LOG_BIN = 0")) {
g_critical("Failed to disable binlog for the thread: %s",
Expand Down Expand Up @@ -629,7 +629,7 @@ void *process_queue(struct thread_data *td) {
if (res)
mysql_free_result(res);
}
mysql_query(thrconn, set_names_str);
mysql_query(thrconn, set_names_str);

g_async_queue_push(conf->ready, GINT_TO_POINTER(1));

Expand Down Expand Up @@ -1137,6 +1137,12 @@ int main(int argc, char *argv[]) {
g_print("option parsing failed: %s, try --help\n", error->message);
exit(EXIT_FAILURE);
}
set_session = g_string_new(NULL);

if (config_file != NULL){
load_config_file(config_file,context, "mydumper", set_session);
}

g_option_context_free(context);

if (!compress_output) {
Expand All @@ -1156,7 +1162,7 @@ int main(int argc, char *argv[]) {
}
}
}

// prompt for password if it's NULL
if (sizeof(password) == 0 || (password == NULL && askPassword)) {
password = passwordPrompt();
Expand Down Expand Up @@ -1340,6 +1346,8 @@ MYSQL *create_main_connection() {
exit(EXIT_FAILURE);
}

execute_gstring(conn, set_session);

detected_server = detect_server(conn);

if ((detected_server == SERVER_TYPE_MYSQL) &&
Expand Down
5 changes: 5 additions & 0 deletions myloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ enum purge_mode purge_mode;
static GMutex *init_mutex = NULL;
static GMutex *progress_mutex = NULL;
guint errors = 0;

unsigned long long int total_data_sql_files = 0;
unsigned long long int progress = 0;
gboolean read_data(FILE *file, gboolean is_compressed, GString *data,
Expand Down Expand Up @@ -133,6 +134,10 @@ int main(int argc, char *argv[]) {
g_print("option parsing failed: %s, try --help\n", error->message);
exit(EXIT_FAILURE);
}
if (config_file != NULL){
set_session = g_string_new(NULL);
load_config_file(config_file, context, "myloader", set_session);
}
g_option_context_free(context);

if (password != NULL){
Expand Down

0 comments on commit bc7ab57

Please sign in to comment.