Java Code Examples for com.mysql.cj.conf.ConnectionUrl#getMainHost()

The following examples show how to use com.mysql.cj.conf.ConnectionUrl#getMainHost() . 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: ConnectionUrlTest.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Tests fix for BUG#28150662, CONNECTOR/J 8 MALFORMED DATABASE URL EXCEPTION WHIT CORRECT URL STRING.
 */
@Test
public void testBug28150662() {
    List<String> connStr = new ArrayList<>();
    connStr.add(
            "jdbc:mysql://localhost:3306/db1?connectionCollation=utf8mb4_unicode_ci&user=user1&sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0");
    connStr.add(
            "jdbc:mysql://localhost:3306/db1?connectionCollation=utf8mb4_unicode_ci&sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0&user=user1");
    connStr.add(
            "jdbc:mysql://address=(host=localhost)(port=3306)(connectionCollation=utf8mb4_unicode_ci)(sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0)(user=user1)/db1");
    connStr.add(
            "jdbc:mysql://(host=localhost,port=3306,connectionCollation=utf8mb4_unicode_ci,sessionVariables=sql_mode='IGNORE_SPACE%2CANSI'%2CFOREIGN_KEY_CHECKS=0,user=user1)/db1");

    for (String cs : connStr) {
        ConnectionUrl url = ConnectionUrl.getConnectionUrlInstance(cs, null);
        HostInfo hi = url.getMainHost();
        assertEquals("utf8mb4_unicode_ci", hi.getHostProperties().get("connectionCollation"));
        assertEquals("user1", hi.getUser());
        assertEquals("sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0", hi.getHostProperties().get("sessionVariables"));
    }
}
 
Example 2
Source File: MySQLBackupProcessor.java    From gocd with Apache License 2.0 5 votes vote down vote up
private ProcessExecutor createProcessExecutor(File targetDir, DbProperties dbProperties) {
    ConnectionUrl connectionUrlInstance = ConnectionUrl.getConnectionUrlInstance(dbProperties.url(), dbProperties.connectionProperties());

    LinkedHashMap<String, String> env = new LinkedHashMap<>();
    if (isNotBlank(dbProperties.password())) {
        env.put("MYSQL_PWD", dbProperties.password());
    }
    // override with any user specified environment
    env.putAll(dbProperties.extraBackupEnv());

    ArrayList<String> argv = new ArrayList<>();
    argv.add("mysqldump");


    String dbName = connectionUrlInstance.getDatabase();
    HostInfo mainHost = connectionUrlInstance.getMainHost();

    if (mainHost != null) {
        argv.add("--host=" + mainHost.getHost());
        argv.add("--port=" + mainHost.getPort());
    }
    if (isNotBlank(dbProperties.user())) {
        argv.add("--user=" + dbProperties.user());
    }

    // append any user specified args for mysqldump
    if (isNotBlank(dbProperties.extraBackupCommandArgs())) {
        Collections.addAll(argv, Commandline.translateCommandline(dbProperties.extraBackupCommandArgs()));
    }

    argv.add("--result-file=" + new File(targetDir, "db." + dbName).toString());
    argv.add(connectionUrlInstance.getDatabase());

    ProcessExecutor processExecutor = new ProcessExecutor();
    processExecutor.redirectOutputAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.redirectErrorAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.environment(env);
    processExecutor.command(argv);
    return processExecutor;
}
 
Example 3
Source File: NonRegisteringDriver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The getPropertyInfo method is intended to allow a generic GUI tool to
 * discover what properties it should prompt a human for in order to get
 * enough information to connect to a database.
 * 
 * <p>
 * Note that depending on the values the human has supplied so far, additional values may become necessary, so it may be necessary to iterate through
 * several calls to getPropertyInfo
 * </p>
 * 
 * @param url
 *            the Url of the database to connect to
 * @param info
 *            a proposed list of tag/value pairs that will be sent on
 *            connect open.
 * 
 * @return An array of DriverPropertyInfo objects describing possible
 *         properties. This array may be an empty array if no properties are
 *         required
 * 
 * @exception SQLException
 *                if a database-access error occurs
 * 
 * @see java.sql.Driver#getPropertyInfo
 */
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
    String host = "";
    String port = "";
    String database = "";
    String user = "";
    String password = "";

    if (!isNullOrEmpty(url)) {
        ConnectionUrl connStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        if (connStr.getType() == Type.SINGLE_CONNECTION) {
            HostInfo hostInfo = connStr.getMainHost();
            info = hostInfo.exposeAsProperties();
        }
    }

    if (info != null) {
        host = info.getProperty(PropertyDefinitions.HOST_PROPERTY_KEY);
        port = info.getProperty(PropertyDefinitions.PORT_PROPERTY_KEY);
        database = info.getProperty(PropertyDefinitions.DBNAME_PROPERTY_KEY);
        user = info.getProperty(PropertyDefinitions.PNAME_user);
        password = info.getProperty(PropertyDefinitions.PNAME_password);
    }

    DriverPropertyInfo hostProp = new DriverPropertyInfo(PropertyDefinitions.HOST_PROPERTY_KEY, host);
    hostProp.required = true;
    hostProp.description = Messages.getString("NonRegisteringDriver.3");

    DriverPropertyInfo portProp = new DriverPropertyInfo(PropertyDefinitions.PORT_PROPERTY_KEY, port);
    portProp.required = false;
    portProp.description = Messages.getString("NonRegisteringDriver.7");

    DriverPropertyInfo dbProp = new DriverPropertyInfo(PropertyDefinitions.DBNAME_PROPERTY_KEY, database);
    dbProp.required = false;
    dbProp.description = Messages.getString("NonRegisteringDriver.10");

    DriverPropertyInfo userProp = new DriverPropertyInfo(PropertyDefinitions.PNAME_user, user);
    userProp.required = true;
    userProp.description = Messages.getString("NonRegisteringDriver.13");

    DriverPropertyInfo passwordProp = new DriverPropertyInfo(PropertyDefinitions.PNAME_password, password);
    passwordProp.required = true;
    passwordProp.description = Messages.getString("NonRegisteringDriver.16");

    DriverPropertyInfo[] dpi;
    dpi = new JdbcPropertySetImpl().exposeAsDriverPropertyInfo(info, 5);

    dpi[0] = hostProp;
    dpi[1] = portProp;
    dpi[2] = dbProp;
    dpi[3] = userProp;
    dpi[4] = passwordProp;

    return dpi;
}
 
Example 4
Source File: NonRegisteringDriver.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
    String host = "";
    String port = "";
    String database = "";
    String user = "";
    String password = "";

    if (!isNullOrEmpty(url)) {
        ConnectionUrl connStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        if (connStr.getType() == Type.SINGLE_CONNECTION) {
            HostInfo hostInfo = connStr.getMainHost();
            info = hostInfo.exposeAsProperties();
        }
    }

    if (info != null) {
        host = info.getProperty(PropertyKey.HOST.getKeyName());
        port = info.getProperty(PropertyKey.PORT.getKeyName());
        database = info.getProperty(PropertyKey.DBNAME.getKeyName());
        user = info.getProperty(PropertyKey.USER.getKeyName());
        password = info.getProperty(PropertyKey.PASSWORD.getKeyName());
    }

    DriverPropertyInfo hostProp = new DriverPropertyInfo(PropertyKey.HOST.getKeyName(), host);
    hostProp.required = true;
    hostProp.description = Messages.getString("NonRegisteringDriver.3");

    DriverPropertyInfo portProp = new DriverPropertyInfo(PropertyKey.PORT.getKeyName(), port);
    portProp.required = false;
    portProp.description = Messages.getString("NonRegisteringDriver.7");

    DriverPropertyInfo dbProp = new DriverPropertyInfo(PropertyKey.DBNAME.getKeyName(), database);
    dbProp.required = false;
    dbProp.description = Messages.getString("NonRegisteringDriver.10");

    DriverPropertyInfo userProp = new DriverPropertyInfo(PropertyKey.USER.getKeyName(), user);
    userProp.required = true;
    userProp.description = Messages.getString("NonRegisteringDriver.13");

    DriverPropertyInfo passwordProp = new DriverPropertyInfo(PropertyKey.PASSWORD.getKeyName(), password);
    passwordProp.required = true;
    passwordProp.description = Messages.getString("NonRegisteringDriver.16");

    DriverPropertyInfo[] dpi;
    dpi = new JdbcPropertySetImpl().exposeAsDriverPropertyInfo(info, 5);

    dpi[0] = hostProp;
    dpi[1] = portProp;
    dpi[2] = dbProp;
    dpi[3] = userProp;
    dpi[4] = passwordProp;

    return dpi;
}
 
Example 5
Source File: SessionFactory.java    From FoxTelem with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a {@link Session} using the information contained in the given properties.
 * 
 * @param properties
 *            the {@link Properties} instance that contains the session components.
 * @return a {@link Session} instance.
 */
public Session getSession(Properties properties) {
    ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(ConnectionUrl.Type.XDEVAPI_SESSION.getScheme(), properties);

    return new SessionImpl(connUrl.getMainHost());
}