Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add ConnectionInit-SQL based on NLS_LANG
Remove NLS_LANG from Locale-Initialization
Fixes #101
  • Loading branch information
pesse committed Oct 11, 2018
commit 8d832617451cc70924bbeec10857a744a2903d9d
16 changes: 7 additions & 9 deletions src/main/java/org/utplsql/cli/LocaleInitializer.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.utplsql.cli;

import org.utplsql.api.EnvironmentVariableUtil;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** This class makes sure the java locale is set according to the environment variables LC_ALL and LANG
* We experienced that, in some cases, the locale was not set as expected, therefore this class implements some clear
* rules:
* 1. If environment variable NLS_LANG is set, we try to parse its content and set locale according to its value if valid
* 2. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid
* 3. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid
* 4. Otherwise we use default locale
* 1. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid
* 2. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid
* 3. Otherwise we use default locale
*
* @author pesse
*/
Expand All @@ -23,12 +24,9 @@ class LocaleInitializer {
*/
static void initLocale() {

boolean localeChanged = setDefaultLocale(System.getenv("NLS_LANG"));

if ( !localeChanged )
localeChanged = setDefaultLocale(System.getenv("LC_ALL"));
boolean localeChanged = setDefaultLocale(EnvironmentVariableUtil.getEnvValue("LC_ALL"));
if ( !localeChanged )
setDefaultLocale(System.getenv("LANG"));
setDefaultLocale(EnvironmentVariableUtil.getEnvValue("LANG"));
}

/** Set the default locale from a given string like LC_ALL or LANG environment variable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.utplsql.cli.datasource;

import com.zaxxer.hikari.HikariDataSource;
import org.utplsql.api.EnvironmentVariableUtil;
import org.utplsql.cli.ConnectionConfig;
import org.utplsql.cli.exception.DatabaseConnectionFailed;

Expand All @@ -9,6 +10,8 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestedDataSourceProvider {

Expand All @@ -32,12 +35,13 @@ public HikariDataSource getDataSource() throws SQLException {

HikariDataSource ds = new HikariDataSource();

setInitSql(ds);
testAndSetJdbcUrl(ds);

return ds;
}

public void testAndSetJdbcUrl( HikariDataSource ds ) throws SQLException
private void testAndSetJdbcUrl( HikariDataSource ds ) throws SQLException
{
List<String> errors = new ArrayList<>();
Throwable lastException = null;
Expand All @@ -55,6 +59,33 @@ public void testAndSetJdbcUrl( HikariDataSource ds ) throws SQLException
throw new DatabaseConnectionFailed(lastException);
}

private void setInitSql( HikariDataSource ds ) {
String nls_lang = EnvironmentVariableUtil.getEnvValue("NLS_LANG");

if ( nls_lang != null ) {
Pattern pattern = Pattern.compile("^([a-zA-Z ]+)?_?([a-zA-Z ]+)?\\.?([a-zA-Z0-9]+)?$");
Matcher matcher = pattern.matcher(nls_lang);

List<String> sqlCommands = new ArrayList<>(2);
if (matcher.matches()) {
if ( matcher.group(1) != null)
sqlCommands.add(String.format("ALTER SESSION SET NLS_LANGUAGE='%s'", matcher.group(1)));
if ( matcher.group(2) != null)
sqlCommands.add(String.format("ALTER SESSION SET NLS_TERRITORY='%s'", matcher.group(2)));

if ( sqlCommands.size() > 0 ) {
StringBuilder sb = new StringBuilder();
sb.append("BEGIN\n");
for (String command : sqlCommands)
sb.append(String.format("EXECUTE IMMEDIATE q'[%s]';\n", command));
sb.append("END;");

ds.setConnectionInitSql(sb.toString());
}
}
}
}

private static class ThickConnectStringPossibility implements ConnectStringPossibility {
@Override
public String getConnectString(ConnectionConfig config) {
Expand Down
64 changes: 38 additions & 26 deletions src/test/java/org/utplsql/cli/DataSourceProviderIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,67 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

public class DataSourceProviderIT {
class DataSourceProviderIT {

@Test
public void connectToDatabase() throws IOException, SQLException {

ConnectionConfig config = new ConnectionConfig(TestHelper.getConnectionString());

DataSource dataSource = new TestedDataSourceProvider(config).getDataSource();
void connectToDatabase() throws SQLException {
DataSource dataSource = getDataSource();

assertNotNull(dataSource);
}

@Test
public void initNlsLang() throws SQLException {
ConnectionConfig config = new ConnectionConfig(TestHelper.getConnectionString());
void initNlsLang() throws SQLException {
System.setProperty("NLS_LANG", "BRAZILIAN PORTUGUESE_BRAZIL.WE8ISO8859P1");
DataSource dataSource = getDataSource();

assertNotNull(dataSource);
checkNlsSessionParameter(dataSource, "NLS_LANGUAGE", "BRAZILIAN PORTUGUESE");
checkNlsSessionParameter(dataSource, "NLS_TERRITORY", "BRAZIL");
}

DataSource dataSource = new TestedDataSourceProvider(config).getDataSource();
@Test
void initPartialNlsLangTerritory() throws SQLException {
System.setProperty("NLS_LANG", "_SOMALIA");
DataSource dataSource = getDataSource();

assertNotNull(dataSource);

try ( Connection con = dataSource.getConnection() ) {
try (PreparedStatement stmt = con.prepareStatement("select value from nls_session_parameters where parameter = 'NLS_LANGUAGE'")) {
ResultSet rs = stmt.executeQuery();
if ( rs.next() ) {
assertEquals("BRAZILIAN PORTUGUESE", rs.getString(1));
}
}
}
checkNlsSessionParameter(dataSource, "NLS_TERRITORY", "SOMALIA");
}

@Test
public void initPartialNlsLang() throws SQLException {
ConnectionConfig config = new ConnectionConfig(TestHelper.getConnectionString());
System.setProperty("NLS_LANG", "_SOMALIA");
void initPartialNlsLangLanguage() throws SQLException {
System.setProperty("NLS_LANG", "HINDI");
DataSource dataSource = getDataSource();

assertNotNull(dataSource);
checkNlsSessionParameter(dataSource, "NLS_LANGUAGE", "HINDI");
}

DataSource dataSource = new TestedDataSourceProvider(config).getDataSource();
@Test
void initNlsLangEmpty() throws SQLException {
System.setProperty("NLS_LANG", "");
DataSource dataSource = getDataSource();

assertNotNull(dataSource);
}

private DataSource getDataSource() throws SQLException {
ConnectionConfig config = new ConnectionConfig(TestHelper.getConnectionString());
return new TestedDataSourceProvider(config).getDataSource();
}

private void checkNlsSessionParameter( DataSource dataSource, String parameterName, String expectedValue ) throws SQLException {
try ( Connection con = dataSource.getConnection() ) {
try (PreparedStatement stmt = con.prepareStatement("select value from nls_session_parameters where parameter = 'NLS_TERRITORY'")) {
try (PreparedStatement stmt = con.prepareStatement("select value from nls_session_parameters where parameter = ?")) {
stmt.setString(1, parameterName);
ResultSet rs = stmt.executeQuery();
if ( rs.next() ) {
assertEquals("SOMALIA", rs.getString(1));
}
if ( rs.next() )
assertEquals(expectedValue, rs.getString(1));
else
fail("Could not get NLS Session parameter value for '" + parameterName + "'");
}
}
}
Expand Down