Search in sources :

Example 1 with HibernateError

use of org.hibernate.HibernateError in project hibernate-reactive by hibernate.

the class DefaultSqlClientPoolConfiguration method connectOptions.

@Override
public SqlConnectOptions connectOptions(URI uri) {
    String scheme = uri.getScheme();
    String path = scheme.equals("oracle") ? oraclePath(uri) : uri.getPath();
    String database = path.length() > 0 ? path.substring(1) : "";
    if (scheme.equals("db2") && database.indexOf(':') > 0) {
        // DB2 URLs are a bit odd and have the format:
        // jdbc:db2://<HOST>:<PORT>/<DB>:key1=value1;key2=value2;
        database = database.substring(0, database.indexOf(':'));
    }
    String host = scheme.equals("oracle") ? oracleHost(uri) : uri.getHost();
    int port = scheme.equals("oracle") ? oraclePort(uri) : uri.getPort();
    int index = uri.toString().indexOf(';');
    if (scheme.equals("sqlserver") && index > 0) {
        // SQL Server separates parameters in the url with a semicolon (';')
        // and the URI class doesn't get the right value for host and port when the url
        // contains parameters
        URI uriWithoutParams = URI.create(uri.toString().substring(0, index));
        host = uriWithoutParams.getHost();
        port = uriWithoutParams.getPort();
    }
    if (port == -1) {
        port = defaultPort(scheme);
    }
    // see if the credentials were specified via properties
    String username = user;
    String password = pass;
    Map<String, String> extraProps = new HashMap<>();
    if (username == null || password == null) {
        // if not, look for URI-style user info first
        String userInfo = uri.getUserInfo();
        if (userInfo != null) {
            String[] bits = userInfo.split(":");
            username = bits[0];
            if (bits.length > 1) {
                password = bits[1];
            }
        } else {
            // check the query for named parameters
            // in case it's a JDBC-style URL
            String[] params = {};
            // jdbc:db2://<HOST>:<PORT>/<DB>:key1=value1;key2=value2;
            if (scheme.equals("db2")) {
                int queryIndex = uri.getPath().indexOf(':') + 1;
                if (queryIndex > 0) {
                    params = uri.getPath().substring(queryIndex).split(";");
                }
            } else if (scheme.contains("sqlserver")) {
                // SQL Server separates parameters in the url with a semicolon (';')
                // Ex: jdbc:sqlserver://<server>:<port>;<database>=AdventureWorks;user=<user>;password=<password>
                String query = uri.getQuery();
                String rawQuery = uri.getRawQuery();
                String s = uri.toString();
                int queryIndex = s.indexOf(';') + 1;
                if (queryIndex > 0) {
                    params = s.substring(queryIndex).split(";");
                }
            } else {
                final String query = scheme.equals("oracle") ? oracleQuery(uri) : uri.getQuery();
                if (query != null) {
                    params = query.split("&");
                }
            }
            for (String param : params) {
                if (param.startsWith("user=")) {
                    username = param.substring(5);
                } else if (param.startsWith("pass=")) {
                    password = param.substring(5);
                } else if (param.startsWith("password=")) {
                    password = param.substring(9);
                } else if (param.startsWith("database=")) {
                    database = param.substring(9);
                } else {
                    final int position = param.indexOf("=");
                    if (position != -1) {
                        // We assume the first '=' is the one separating key and value
                        String key = param.substring(0, position);
                        String value = param.substring(position + 1);
                        extraProps.put(key, value);
                    } else {
                        // A key without a value
                        extraProps.put(param, null);
                    }
                }
            }
        }
    }
    if (username == null) {
        throw new HibernateError("database username not specified (set the property 'javax.persistence.jdbc.user', or include it as a parameter in the connection URL)");
    }
    SqlConnectOptions connectOptions = new SqlConnectOptions().setHost(host).setPort(port).setDatabase(database).setUser(username);
    if (password != null) {
        connectOptions.setPassword(password);
    }
    for (String key : extraProps.keySet()) {
        connectOptions.addProperty(key, extraProps.get(key));
    }
    // enable the prepared statement cache by default
    connectOptions.setCachePreparedStatements(true);
    if (cacheMaxSize != null) {
        if (cacheMaxSize <= 0) {
            LOG.preparedStatementCacheDisabled();
            connectOptions.setCachePreparedStatements(false);
        } else {
            LOG.preparedStatementCacheMaxSize(cacheMaxSize);
            connectOptions.setCachePreparedStatements(true);
            connectOptions.setPreparedStatementCacheMaxSize(cacheMaxSize);
        }
    }
    if (sqlLimit != null) {
        LOG.preparedStatementCacheSQLLimit(sqlLimit);
        connectOptions.setPreparedStatementCacheSqlLimit(sqlLimit);
    }
    return connectOptions;
}
Also used : HibernateError(org.hibernate.HibernateError) HashMap(java.util.HashMap) SqlConnectOptions(io.vertx.sqlclient.SqlConnectOptions) ConfigurationHelper.getString(org.hibernate.internal.util.config.ConfigurationHelper.getString) URI(java.net.URI)

Example 2 with HibernateError

use of org.hibernate.HibernateError in project hibernate-reactive by hibernate.

the class JdbcUrlParserTest method missingUser.

@Test
public void missingUser() {
    final HibernateError error = assertThrows(HibernateError.class, () -> {
        String url = createJdbcUrl("localhost", dbType().getDefaultPort(), DEFAULT_DB, Map.of());
        URI uri = DefaultSqlClientPool.parse(url);
        new DefaultSqlClientPoolConfiguration().connectOptions(uri);
    });
    assertThat(error.getMessage()).contains("database username not specified");
}
Also used : HibernateError(org.hibernate.HibernateError) DefaultSqlClientPoolConfiguration(org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration) URI(java.net.URI) Test(org.junit.Test)

Example 3 with HibernateError

use of org.hibernate.HibernateError in project hibernate-reactive by hibernate.

the class JdbcUrlParserTest method exceptionWhenNull.

@Test
public void exceptionWhenNull() {
    final HibernateError error = assertThrows(HibernateError.class, () -> {
        DefaultSqlClientPool.parse(null);
        fail("Null should be an illegal argument");
    });
    assertThat(error.getMessage()).contains("was not provided");
}
Also used : HibernateError(org.hibernate.HibernateError) Test(org.junit.Test)

Aggregations

HibernateError (org.hibernate.HibernateError)3 URI (java.net.URI)2 Test (org.junit.Test)2 SqlConnectOptions (io.vertx.sqlclient.SqlConnectOptions)1 HashMap (java.util.HashMap)1 ConfigurationHelper.getString (org.hibernate.internal.util.config.ConfigurationHelper.getString)1 DefaultSqlClientPoolConfiguration (org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration)1