DatabaseImpl implements Database { Properties driverProperties(String url, String user, String password) { var properties = new Properties(); if (user != null) properties.setProperty("user", user); if (password != null) properties.setProperty("password", password); if (url.startsWith("jdbc:mysql:")) { String timeoutValue = String.valueOf(timeout.toMillis()); properties.setProperty(PropertyKey.connectTimeout.getKeyName(), timeoutValue); properties.setProperty(PropertyKey.socketTimeout.getKeyName(), timeoutValue); properties.setProperty(PropertyKey.rewriteBatchedStatements.getKeyName(), "true"); properties.setProperty(PropertyKey.queryInterceptors.getKeyName(), MySQLQueryInterceptor.class.getName()); properties.setProperty(PropertyKey.logger.getKeyName(), "Slf4JLogger"); int index = url.indexOf('?'); if (index == -1 || url.indexOf("useSSL=", index + 1) == -1) properties.setProperty(PropertyKey.useSSL.getKeyName(), "false"); if (index == -1 || url.indexOf("characterEncoding=", index + 1) == -1) properties.setProperty(PropertyKey.characterEncoding.getKeyName(), "utf-8"); } return properties; } DatabaseImpl(String name); void close(); void timeout(Duration timeout); void url(String url); void view(Class<T> viewClass); Repository<T> repository(Class<T> entityClass); @Override Transaction beginTransaction(); @Override List<T> select(String sql, Class<T> viewClass, Object... params); @Override Optional<T> selectOne(String sql, Class<T> viewClass, Object... params); @Override int execute(String sql, Object... params); @Override int[] batchExecute(String sql, List<Object[]> params); final Pool<Connection> pool; final DatabaseOperation operation; public String user; public String password; public int maxOperations; public int tooManyRowsReturnedThreshold; public long slowOperationThresholdInNanos; public IsolationLevel isolationLevel; }
@Test void driverProperties() { Properties properties = database.driverProperties("jdbc:mysql: assertThat(properties) .doesNotContainKeys("user", "password") .containsEntry("useSSL", "false") .containsEntry("characterEncoding", "utf-8"); properties = database.driverProperties("jdbc:mysql: assertThat(properties).doesNotContainKeys("useSSL", "characterEncoding"); properties = database.driverProperties("jdbc:mysql: assertThat(properties) .doesNotContainKeys("useSSL") .containsEntry("characterEncoding", "utf-8"); }