All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.sync.Main Maven / Gradle / Ivy

Go to download

Simple tool for copying mysql data updates from one small mysql database to its mirror.

The newest version!
/**
 This is free and unencumbered software released into the public domain.

 Anyone is free to copy, modify, publish, use, compile, sell, or
 distribute this software, either in source code form or as a compiled
 binary, for any purpose, commercial or non-commercial, and by any
 means.

 In jurisdictions that recognize copyright laws, the author or authors
 of this software dedicate any and all copyright interest in the
 software to the public domain. We make this dedication for the benefit
 of the public at large and to the detriment of our heirs and
 successors. We intend this dedication to be an overt act of
 relinquishment in perpetuity of all present and future rights to this
 software under copyright law.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
package base.sync;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import base.json.JsonParser;
import base.text.StringHelper;

import static base.text.StringHelper.chomp;

/**
 * Synchronise database tables in two MySQL schemas. Expects a JSON config file as a parameter,
 * see documentation for details.
 */
public class Main {

	public static void main(String[] arg) throws ClassNotFoundException, SQLException, IOException {

		if(arg.length == 0) {
			System.err.println("Please specify JSON config string, or path to json config file.");
		}

		String config = arg[0];

		if(!chomp(config).startsWith("{") && new File(config).exists()) {
			config = new String(Files.readAllBytes(Paths.get(config)));
		}

		JsonParser parser = new JsonParser(config);
		Map conf = (Map) parser.parse();

		if(!conf.containsKey("source") && !conf.containsKey("target")) {
			System.err.println("Please specify valid config file.");
		}

		String[] skip = new String[]{};
		Map tableFilter;
		Map> primaryKeyHints;

		if(conf.containsKey("skip")) {
			skip = ((List)conf.get("skip")).toArray(new String[]{});
			System.out.println("Tables to skip: " + StringHelper.join(skip, ","));
		}
		if(conf.containsKey("table_filter")) {
			tableFilter = (Map) conf.get("table_filter");
		} else {
			tableFilter = new Hashtable<>();
		}
		if(conf.containsKey("primary_key_hints")) {
			System.out.println("Using primary key hints.");
			primaryKeyHints = (Map>) conf.get("primary_key_hints");
		} else {
			primaryKeyHints = new Hashtable<>();
		}
		System.out.println();

		base.sync.Sync sync = new base.sync.Sync(
				(String)((Map)conf.get("source")).get("host"),
				(String)((Map)conf.get("source")).get("schema"),
				(String)((Map)conf.get("source")).get("user"),
				(String)((Map)conf.get("source")).get("password"),
				(String)((Map)conf.get("target")).get("host"),
				(String)((Map)conf.get("target")).get("schema"),
				(String)((Map)conf.get("target")).get("user"),
				(String)((Map)conf.get("target")).get("password"),
				skip,
				primaryKeyHints,
				tableFilter
				);
		sync.sync();
	}

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy