Skip to content

Commit 8d83261

Browse files
committed
Add ConnectionInit-SQL based on NLS_LANG
Remove NLS_LANG from Locale-Initialization Fixes #101
1 parent 3513f49 commit 8d83261

File tree

3 files changed

+77
-36
lines changed

3 files changed

+77
-36
lines changed

src/main/java/org/utplsql/cli/LocaleInitializer.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package org.utplsql.cli;
22

3+
import org.utplsql.api.EnvironmentVariableUtil;
4+
35
import java.util.Locale;
46
import java.util.regex.Matcher;
57
import java.util.regex.Pattern;
68

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

26-
boolean localeChanged = setDefaultLocale(System.getenv("NLS_LANG"));
27-
28-
if ( !localeChanged )
29-
localeChanged = setDefaultLocale(System.getenv("LC_ALL"));
27+
boolean localeChanged = setDefaultLocale(EnvironmentVariableUtil.getEnvValue("LC_ALL"));
3028
if ( !localeChanged )
31-
setDefaultLocale(System.getenv("LANG"));
29+
setDefaultLocale(EnvironmentVariableUtil.getEnvValue("LANG"));
3230
}
3331

3432
/** Set the default locale from a given string like LC_ALL or LANG environment variable

src/main/java/org/utplsql/cli/datasource/TestedDataSourceProvider.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utplsql.cli.datasource;
22

33
import com.zaxxer.hikari.HikariDataSource;
4+
import org.utplsql.api.EnvironmentVariableUtil;
45
import org.utplsql.cli.ConnectionConfig;
56
import org.utplsql.cli.exception.DatabaseConnectionFailed;
67

@@ -9,6 +10,8 @@
910
import java.sql.SQLException;
1011
import java.util.ArrayList;
1112
import java.util.List;
13+
import java.util.regex.Matcher;
14+
import java.util.regex.Pattern;
1215

1316
public class TestedDataSourceProvider {
1417

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

3336
HikariDataSource ds = new HikariDataSource();
3437

38+
setInitSql(ds);
3539
testAndSetJdbcUrl(ds);
3640

3741
return ds;
3842
}
3943

40-
public void testAndSetJdbcUrl( HikariDataSource ds ) throws SQLException
44+
private void testAndSetJdbcUrl( HikariDataSource ds ) throws SQLException
4145
{
4246
List<String> errors = new ArrayList<>();
4347
Throwable lastException = null;
@@ -55,6 +59,33 @@ public void testAndSetJdbcUrl( HikariDataSource ds ) throws SQLException
5559
throw new DatabaseConnectionFailed(lastException);
5660
}
5761

62+
private void setInitSql( HikariDataSource ds ) {
63+
String nls_lang = EnvironmentVariableUtil.getEnvValue("NLS_LANG");
64+
65+
if ( nls_lang != null ) {
66+
Pattern pattern = Pattern.compile("^([a-zA-Z ]+)?_?([a-zA-Z ]+)?\\.?([a-zA-Z0-9]+)?$");
67+
Matcher matcher = pattern.matcher(nls_lang);
68+
69+
List<String> sqlCommands = new ArrayList<>(2);
70+
if (matcher.matches()) {
71+
if ( matcher.group(1) != null)
72+
sqlCommands.add(String.format("ALTER SESSION SET NLS_LANGUAGE='%s'", matcher.group(1)));
73+
if ( matcher.group(2) != null)
74+
sqlCommands.add(String.format("ALTER SESSION SET NLS_TERRITORY='%s'", matcher.group(2)));
75+
76+
if ( sqlCommands.size() > 0 ) {
77+
StringBuilder sb = new StringBuilder();
78+
sb.append("BEGIN\n");
79+
for (String command : sqlCommands)
80+
sb.append(String.format("EXECUTE IMMEDIATE q'[%s]';\n", command));
81+
sb.append("END;");
82+
83+
ds.setConnectionInitSql(sb.toString());
84+
}
85+
}
86+
}
87+
}
88+
5889
private static class ThickConnectStringPossibility implements ConnectStringPossibility {
5990
@Override
6091
public String getConnectString(ConnectionConfig config) {

src/test/java/org/utplsql/cli/DataSourceProviderIT.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,67 @@
1212

1313
import static org.junit.jupiter.api.Assertions.assertEquals;
1414
import static org.junit.jupiter.api.Assertions.assertNotNull;
15+
import static org.junit.jupiter.api.Assertions.fail;
1516

16-
public class DataSourceProviderIT {
17+
class DataSourceProviderIT {
1718

1819
@Test
19-
public void connectToDatabase() throws IOException, SQLException {
20-
21-
ConnectionConfig config = new ConnectionConfig(TestHelper.getConnectionString());
22-
23-
DataSource dataSource = new TestedDataSourceProvider(config).getDataSource();
20+
void connectToDatabase() throws SQLException {
21+
DataSource dataSource = getDataSource();
2422

2523
assertNotNull(dataSource);
2624
}
2725

2826
@Test
29-
public void initNlsLang() throws SQLException {
30-
ConnectionConfig config = new ConnectionConfig(TestHelper.getConnectionString());
27+
void initNlsLang() throws SQLException {
3128
System.setProperty("NLS_LANG", "BRAZILIAN PORTUGUESE_BRAZIL.WE8ISO8859P1");
29+
DataSource dataSource = getDataSource();
3230

31+
assertNotNull(dataSource);
32+
checkNlsSessionParameter(dataSource, "NLS_LANGUAGE", "BRAZILIAN PORTUGUESE");
33+
checkNlsSessionParameter(dataSource, "NLS_TERRITORY", "BRAZIL");
34+
}
3335

34-
DataSource dataSource = new TestedDataSourceProvider(config).getDataSource();
36+
@Test
37+
void initPartialNlsLangTerritory() throws SQLException {
38+
System.setProperty("NLS_LANG", "_SOMALIA");
39+
DataSource dataSource = getDataSource();
3540

3641
assertNotNull(dataSource);
37-
38-
try ( Connection con = dataSource.getConnection() ) {
39-
try (PreparedStatement stmt = con.prepareStatement("select value from nls_session_parameters where parameter = 'NLS_LANGUAGE'")) {
40-
ResultSet rs = stmt.executeQuery();
41-
if ( rs.next() ) {
42-
assertEquals("BRAZILIAN PORTUGUESE", rs.getString(1));
43-
}
44-
}
45-
}
42+
checkNlsSessionParameter(dataSource, "NLS_TERRITORY", "SOMALIA");
4643
}
4744

4845
@Test
49-
public void initPartialNlsLang() throws SQLException {
50-
ConnectionConfig config = new ConnectionConfig(TestHelper.getConnectionString());
51-
System.setProperty("NLS_LANG", "_SOMALIA");
46+
void initPartialNlsLangLanguage() throws SQLException {
47+
System.setProperty("NLS_LANG", "HINDI");
48+
DataSource dataSource = getDataSource();
5249

50+
assertNotNull(dataSource);
51+
checkNlsSessionParameter(dataSource, "NLS_LANGUAGE", "HINDI");
52+
}
5353

54-
DataSource dataSource = new TestedDataSourceProvider(config).getDataSource();
54+
@Test
55+
void initNlsLangEmpty() throws SQLException {
56+
System.setProperty("NLS_LANG", "");
57+
DataSource dataSource = getDataSource();
5558

5659
assertNotNull(dataSource);
60+
}
61+
62+
private DataSource getDataSource() throws SQLException {
63+
ConnectionConfig config = new ConnectionConfig(TestHelper.getConnectionString());
64+
return new TestedDataSourceProvider(config).getDataSource();
65+
}
5766

67+
private void checkNlsSessionParameter( DataSource dataSource, String parameterName, String expectedValue ) throws SQLException {
5868
try ( Connection con = dataSource.getConnection() ) {
59-
try (PreparedStatement stmt = con.prepareStatement("select value from nls_session_parameters where parameter = 'NLS_TERRITORY'")) {
69+
try (PreparedStatement stmt = con.prepareStatement("select value from nls_session_parameters where parameter = ?")) {
70+
stmt.setString(1, parameterName);
6071
ResultSet rs = stmt.executeQuery();
61-
if ( rs.next() ) {
62-
assertEquals("SOMALIA", rs.getString(1));
63-
}
72+
if ( rs.next() )
73+
assertEquals(expectedValue, rs.getString(1));
74+
else
75+
fail("Could not get NLS Session parameter value for '" + parameterName + "'");
6476
}
6577
}
6678
}

0 commit comments

Comments
 (0)