Skip to content

Commit

Permalink
MONDRIAN: Format(<Value>, <String>) function uses connection's locale…
Browse files Browse the repository at this point in the history
…; and tests for the same. (Fixes bug 1618634; contributed by Bart Pappyn.)

[git-p4: depot-paths = "//open/mondrian/": change = 8373]
  • Loading branch information
julianhyde committed Dec 20, 2006
1 parent d9c1e07 commit 553593a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/main/mondrian/olap/Evaluator.java
Expand Up @@ -13,6 +13,8 @@

package mondrian.olap;

import java.util.Locale;

import mondrian.calc.ParameterSlot;

/**
Expand Down Expand Up @@ -109,6 +111,11 @@ public interface Evaluator {
* Returns parent evaluator.
*/
Evaluator getParent();

/**
* Returns the connection's locale.
*/
Locale getConnectionLocale();

/**
* Retrieves the value of property <code>name</code>. If more than one
Expand Down
5 changes: 2 additions & 3 deletions src/main/mondrian/olap/fun/FormatFunDef.java
Expand Up @@ -43,9 +43,8 @@ public FormatFunDef(FunDef dummyFunDef) {

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
final Exp[] args = call.getArgs();
final Calc calc =
compiler.compileScalar(call.getArg(0), true);
final Locale locale = Locale.getDefault(); // todo: use connection's locale
final Calc calc = compiler.compileScalar(call.getArg(0), true);
final Locale locale = compiler.getEvaluator().getConnectionLocale();
if (args[1] instanceof Literal) {
// Constant string expression: optimize by
// compiling format string.
Expand Down
4 changes: 4 additions & 0 deletions src/main/mondrian/rolap/RolapEvaluator.java
Expand Up @@ -542,6 +542,10 @@ private Format getFormat(String formatString) {
return Format.get(formatString, root.connection.getLocale());
}

public Locale getConnectionLocale() {
return root.connection.getLocale();
}

/**
* Converts a value of this member into a string according to this member's
* format specification.
Expand Down
50 changes: 50 additions & 0 deletions testsrc/main/mondrian/rolap/RolapConnectionTest.java
Expand Up @@ -14,6 +14,7 @@

import junit.framework.TestCase;
import mondrian.olap.Util;
import mondrian.olap.DriverManager;
import mondrian.test.TestContext;

import javax.sql.DataSource;
Expand Down Expand Up @@ -111,6 +112,55 @@ public void testNonPooledConnectionWithProperties() {
}
}
}

/**
* Tests that the FORMAT function uses the connection's locale.
*/
public void testFormatLocale() {
String expr = "FORMAT(1234.56, \"#,##.#\")";
checkLocale("es_ES", expr, "1.234,6", false);
checkLocale("es_MX", expr, "1,234.6", false);
checkLocale("en_US", expr, "1,234.6", false);
}

/**
* Tests that measures are formatted using the connection's locale.
*/
public void testFormatStringLocale() {
checkLocale("es_ES", "1234.56", "1.234,6", true);
checkLocale("es_MX", "1234.56", "1,234.6", true);
checkLocale("en_US", "1234.56", "1,234.6", true);
}

private static void checkLocale(
final String localeName, String expr, String expected, boolean isQuery) {
TestContext testContextSpain = new TestContext() {
public mondrian.olap.Connection getConnection() {
Util.PropertyList properties =
Util.parseConnectString(getConnectString());
properties.put(
RolapConnectionProperties.Locale.name(),
localeName);
return DriverManager.getConnection(properties, null, true);
}
};
if (isQuery) {
String query = "WITH MEMBER [Measures].[Foo] AS '" + expr + "',\n"
+ " FORMAT_STRING = '#,##.#' \n"
+ "SELECT {[MEasures].[Foo]} ON COLUMNS FROM [Sales]";
String expected2 =
TestContext.fold("Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Measures].[Foo]}\n" +
"Row #0: "
+ expected
+ "\n");
testContextSpain.assertQueryReturns(query, expected2);
} else {
testContextSpain.assertExprReturns(expr, expected);
}
}
}

// End RolapConnectionTest.java

0 comments on commit 553593a

Please sign in to comment.