Skip to content

Commit

Permalink
fix(jdbc): Address issues with manual backport of "MySQL ConnectorJ a…
Browse files Browse the repository at this point in the history
…nd use MariaDb instead" (#2507)

* feat(jdbc): Remove MySQL ConnectorJ and use MariaDb instead

Backporting from #2502

* feat(jdbc): Remove MySQL ConnectorJ and use MariaDb instead

Backporting from #2502
  • Loading branch information
ev-codes committed May 8, 2024
1 parent ccbd444 commit 8fefc2e
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. Licensed under a proprietary license.
* See the License.txt file for more information. You may not use this file
* except in compliance with the proprietary license.
*/
package io.camunda.connector.jdbc.utils;

import java.net.URI;
import java.net.URISyntaxException;

/**
* Helper class to add parameters to a URL. Parameters values can be added to the URL as well, but
* it is optional.
*
* @see ConnectionParameterHelperTest for usage examples.
*/
public class ConnectionParameterHelper {

public static String addQueryParameterToURL(String urlString, String paramName)
throws URISyntaxException {
return addQueryParameterToURL(urlString, paramName, null);
}

public static String addQueryParameterToURL(String urlString, String paramName, String paramValue)
throws URISyntaxException {
URI uri = new URI(urlString);
// Check if the URL already has query parameters
int queryParamsIndex = urlString.indexOf('?');
String query;
if (queryParamsIndex == -1) {
// No query parameters
query = "?";
} else {
// Query parameters already exist let's add the new one
query = "&";
}
query += paramName;
// Value is optional
if (paramValue != null) {
query += "=" + paramValue;
}
// jdbc:mysql//localhost:3306?paramName=paramValue for instance is not detected as a regular
// URI,
// so we need to reconstruct the URI using the scheme and the scheme specific part
return new URI(uri.getScheme() + ":" + uri.getSchemeSpecificPart() + query).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. Licensed under a proprietary license.
* See the License.txt file for more information. You may not use this file
* except in compliance with the proprietary license.
*/
package io.camunda.connector.jdbc.utils;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class ConnectionParameterHelperTest {
@Test
void shouldCreateQueryParameters_whenNoExistingQueryParameters() throws Exception {
String urlString = "jdbc:mysql//localhost:3306";
String paramName = "paramName";
String paramValue = "paramValue";
String result =
ConnectionParameterHelper.addQueryParameterToURL(urlString, paramName, paramValue);
assertThat(result).isEqualTo(urlString + "?paramName=paramValue");
}

@Test
void shouldNotCreateQueryParameters_whenExistingQueryParameters() throws Exception {
String urlString = "jdbc:mysql//localhost:3306?existingParam=existingValue";
String paramName = "paramName";
String paramValue = "paramValue";
String result =
ConnectionParameterHelper.addQueryParameterToURL(urlString, paramName, paramValue);
assertThat(result).isEqualTo(urlString + "&paramName=paramValue");
}

@Test
void shouldCreateQueryParameters_whenNoParamValue() throws Exception {
String urlString = "jdbc:mysql//localhost:3306";
String paramName = "paramName";
String result = ConnectionParameterHelper.addQueryParameterToURL(urlString, paramName);
assertThat(result).isEqualTo(urlString + "?paramName");
}

@Test
void shouldCreateQueryParameters_whenNoParamValueAndExistingQueryParameters() throws Exception {
String urlString = "jdbc:mysql//localhost:3306?existingParam=existingValue";
String paramName = "paramName";
String result = ConnectionParameterHelper.addQueryParameterToURL(urlString, paramName);
assertThat(result).isEqualTo(urlString + "&paramName");
}

@Test
void shouldCreateQueryParametersAfterPath_whenQueryPathExistsAndNoParamValue() throws Exception {
String urlString = "jdbc:mysql//localhost:3306/database";
String paramName = "paramName";
String result = ConnectionParameterHelper.addQueryParameterToURL(urlString, paramName);
assertThat(result).isEqualTo(urlString + "?paramName");
}

@Test
void shouldCreateQueryParametersAfterPath_whenQueryPathExistsAndQueryParametersExist()
throws Exception {
String urlString = "jdbc:mysql//localhost:3306/database?existingParam=existingValue";
String paramName = "paramName";
String result = ConnectionParameterHelper.addQueryParameterToURL(urlString, paramName);
assertThat(result).isEqualTo(urlString + "&paramName");
}

@Test
void shouldCreateQueryParametersAfterPath_whenEmptyPath() throws Exception {
String urlString = "jdbc:mysql//localhost:3306/";
String paramName = "paramName";
String result = ConnectionParameterHelper.addQueryParameterToURL(urlString, paramName);
assertThat(result).isEqualTo(urlString + "?paramName");
}
}

0 comments on commit 8fefc2e

Please sign in to comment.