ch.vorburger.mariadb4j.DB Java Examples

The following examples show how to use ch.vorburger.mariadb4j.DB. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractEmbeddedDataSourceHelper.java    From freeacs with MIT License 8 votes vote down vote up
public static void setUpBeforeClass() throws ManagedProcessException {
    randomFolder = "./db/" + UUID.randomUUID();
    DBConfigurationBuilder configBuilder = DBConfigurationBuilder.newBuilder();
    configBuilder.setPort(3307);
    configBuilder.setDataDir(randomFolder);
    db = DB.newEmbeddedDB(configBuilder.build());
    db.start();
    db.createDB("acs", "acs", "acs");
    db.source("install.sql", "acs", "acs", "acs");
    String url = configBuilder.getURL("acs");
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDataSourceClassName("org.mariadb.jdbc.MariaDbDataSource");
    hikariConfig.addDataSourceProperty("url", url);
    hikariConfig.addDataSourceProperty("user", "acs");
    hikariConfig.addDataSourceProperty("password", "acs");
    dataSource = new HikariDataSource(hikariConfig);
}
 
Example #2
Source File: MySQLTestCore.java    From rmlmapper-java with MIT License 6 votes vote down vote up
protected static void stopDBs(DB mysqlDB) throws ManagedProcessException {
    if (mysqlDB != null) {
        mysqlDB.stop();
    }

    // Make sure all tempFiles are removed
    int counter = 0;
    while (!tempFiles.isEmpty()) {
        for (Iterator<String> i = tempFiles.iterator(); i.hasNext(); ) {
            try {
                if (new File(i.next()).delete()) {
                    i.remove();
                }
            } catch (Exception ex) {
                counter++;
                ex.printStackTrace();
                // Prevent infinity loops
                if (counter > 100) {
                    throw new Error("Could not remove all temp mapping files.");
                }
            }
        }
    }
}
 
Example #3
Source File: MySQLTestCore.java    From rmlmapper-java with MIT License 5 votes vote down vote up
protected static DB setUpMySQLDBInstance(int portNumber) throws ManagedProcessException {
    DBConfigurationBuilder configBuilder = DBConfigurationBuilder.newBuilder();
    configBuilder.setPort(portNumber);
    configBuilder.addArg("--user=root");
    configBuilder.addArg("--sql-mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ANSI_QUOTES");
    DB mysqlDB = DB.newEmbeddedDB(configBuilder.build());
    mysqlDB.start();

    return mysqlDB;
}
 
Example #4
Source File: TestDatabase.java    From BungeeChat2 with GNU General Public License v3.0 5 votes vote down vote up
@SneakyThrows(ManagedProcessException.class)
@SuppressFBWarnings( // TODO: Remove when fixed in SpotBugs
    value = "RV_RETURN_VALUE_IGNORED",
    justification = "Return values can be safely ignored as they are for chaining only.")
public static void startDatabase() {
  final int limit = 100;
  int count = 0;
  String actualBaseDir;
  String actualDataDir;

  do {
    actualBaseDir = baseDir + count;
  } while ((++count < limit) && (new File(actualBaseDir)).exists());

  Preconditions.checkElementIndex(count, limit, "count must be less than " + limit);

  actualDataDir = actualBaseDir + "/data";
  final DBConfiguration config =
      DBConfigurationBuilder.newBuilder()
          .setPort(0)
          .setSocket(localhost)
          .setBaseDir(actualBaseDir)
          .setDataDir(actualDataDir)
          .build();
  databaseInstance = DB.newEmbeddedDB(config);
  databaseInstance.start();

  port = databaseInstance.getConfiguration().getPort();
  host = localhost + ':' + port;

  databaseInstance.createDB("test");
}
 
Example #5
Source File: App.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
private DB createDatabase() throws ManagedProcessException {

        DBConfigurationBuilder configBuilder = DBConfigurationBuilder.newBuilder();
        configBuilder.setPort(3306);
        configBuilder.setDataDir("target/mariaDB");
        configBuilder.setBaseDir("target/mariaDB");
        DB database = DB.newEmbeddedDB(configBuilder.build());
        database.start();

        return database;
    }
 
Example #6
Source File: DatabaseRule.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void before() throws Exception {
    config = DBConfigurationBuilder.newBuilder();
    config.addArg("--user=root");
    config.setPort(0); // 0 => autom. detect free port
    db = DB.newEmbeddedDB(config.build());
    db.start();
    db.createDB(dbName);
    db.source(initFile, username, password, dbName);
}
 
Example #7
Source File: MariaDB_IT_Base.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    driverProperties = DriverProperties.load("database/maria.properties", "maria");
    int embeddedDBPort = Integer.parseInt(driverProperties.getProperty(EMBEDDED_DB_PORT_KEY));
    TEST_DATABASE = DB.newEmbeddedDB(embeddedDBPort);
    TEST_DATABASE.start();
    TEST_DATABASE.createDB("test");
    TEST_DATABASE.source("jdbc/mariadb/init.sql");
}
 
Example #8
Source File: MariaDBEmbeddedDb.java    From mdw with Apache License 2.0 4 votes vote down vote up
public void startup() throws SQLException {
    try {
        if (db != null)
            db.stop();

        boolean firstTime = false;
        File baseDir =  new File(config.getBaseDir());
        if (!baseDir.exists() || baseDir.listFiles().length == 0) {
            firstTime = true;
            if (!baseDir.exists() && !baseDir.mkdirs())
                throw new IOException("Cannot create db base dir: " + baseDir);
            ZipHelper.unzip(dbJar, baseDir, binariesSubLoc, null, false);
            if (!config.isWindows()) {
                Util.forceExecutable(new File(baseDir, "bin/my_print_defaults"));
                Util.forceExecutable(new File(baseDir, "bin/mysql_install_db"));
                Util.forceExecutable(new File(baseDir, "bin/mysqld"));
                Util.forceExecutable(new File(baseDir, "bin/mysql"));
            }
        }

        if (!firstTime) {
            File dataDir =  new File(config.getDataDir());
            if (!dataDir.exists() || dataDir.listFiles().length == 0)
                firstTime = true;
        }

        db = DB.newEmbeddedDB(config);
        db.start();
        if (firstTime) {
            String rootPass = System.getenv("MDW_EMBEDDED_DB_ROOT_PASSWORD");
            if (rootPass == null)
                rootPass = "mdwchangeme";  // can only connect from localhost, so hardwired is okay
            db.run("CREATE DATABASE IF NOT EXISTS `" + dbName + "`;", "root", null, null);
            // set a password on the root account
            db.run("SET PASSWORD FOR 'root'@'localhost' = PASSWORD('" + rootPass + "');", "root", null, null);
            // create the app user account and grant permissions
            db.run("GRANT ALL ON " + dbName + ".* to '" + user + "'@'%' IDENTIFIED BY '" + password + "'", "root", rootPass, null);
            db.run("GRANT ALL ON " + dbName + ".* to '" + user + "'@'localhost' IDENTIFIED BY '" + password + "'", "root", rootPass, null);
        }
    }
    catch (Exception ex) {
        throw new SQLException("MariaDB4j startup error: " + ex, ex);
    }
}
 
Example #9
Source File: MariaDB_IT_Base.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
private static DB testDBStop() throws ManagedProcessException {
    if (TEST_DATABASE != null) {
        TEST_DATABASE.stop();
    }
    return null;
}