Skip to content

Commit

Permalink
Fixes bug 3042937. Escaped single quotes that were nested in a single…
Browse files Browse the repository at this point in the history
… quoted section were unparsed as unescaped single quotes.

git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@348 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
lucboudreau committed Aug 11, 2010
1 parent 4114437 commit c6702a9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/org/olap4j/mdx/IdentifierNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ public <T> T accept(ParseTreeVisitor<T> visitor) {
}

public void unparse(ParseTreeWriter writer) {
writer.getPrintWriter().print(toString());
String str = toString();
if(writer.isInsideSingleQuote()) {
str = str.replace("'", "''");
}
writer.getPrintWriter().print(str);
}

public String toString() {
Expand Down
9 changes: 9 additions & 0 deletions src/org/olap4j/mdx/ParseTreeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ParseTreeWriter {
private final PrintWriter pw;
private int linePrefixLength;
private String linePrefix;
private boolean isInsideSingleQuote;

private static final int INDENT = 4;
private static String bigString = " ";
Expand Down Expand Up @@ -112,6 +113,14 @@ private static synchronized String spaces(int n)
}
return bigString.substring(0, n);
}

boolean isInsideSingleQuote() {
return isInsideSingleQuote;
}

void setInsideSingleQuote(boolean isInsideSingleQuote) {
this.isInsideSingleQuote = isInsideSingleQuote;
}
}

// End ParseTreeWriter.java
7 changes: 6 additions & 1 deletion src/org/olap4j/mdx/WithMemberNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public void unparse(ParseTreeWriter writer) {
pw.print("MEMBER ");
name.unparse(writer);
pw.print(" AS '");
expression.unparse(writer);
writer.setInsideSingleQuote(true);
try {
expression.unparse(writer);
} finally {
writer.setInsideSingleQuote(false);
}
pw.print("'");
if (memberPropertyList != null) {
for (PropertyValueNode memberProperty : memberPropertyList) {
Expand Down
23 changes: 23 additions & 0 deletions testsrc/org/olap4j/mdx/MdxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

import java.util.*;

import org.olap4j.mdx.parser.MdxParser;
import org.olap4j.mdx.parser.impl.DefaultMdxParserImpl;
import org.olap4j.test.TestContext;

/**
* Testcase for org.olap4j.mdx package.
*
Expand Down Expand Up @@ -109,6 +113,25 @@ public void testParseIdentifier() {
e.getMessage());
}
}

/**
* Tests that escaped single quotes ('') nested inside a quoted
* part of a query are unparsed as escaped quotes as well.
*/
public void testQuoteEscaping() {
final String query =
"WITH\n"
+ "MEMBER [CustomerDim].[CustomerName].[XL_QZX] AS 'Aggregate"
+ "({[CustomerDim].[CustomerName].&[ABC INT''L],"
+ " [CustomerDim].[CustomerName].&[XYZ]})'\n"
+ "SELECT\n"
+ "{[Measures].[Sales]} ON COLUMNS\n"
+ "FROM [cube]\n"
+ "WHERE ([CustomerDim].[CustomerName].[XL_QZX])";
final MdxParser parser = new DefaultMdxParserImpl();
final SelectNode rootNode = parser.parseSelect(query);
assertEquals(query,TestContext.unfold(rootNode.toString()));
}
}

// End MdxTest.java
19 changes: 19 additions & 0 deletions testsrc/org/olap4j/test/TestContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ public static SafeString fold(String string) {
}
}

/**
* Reverses the effect of {@link #fold}; converts platform-specific line
* endings in a string info linefeeds.
*
* @param string String where all linefeeds have been converted to
* platform-specific (CR+LF on Windows, LF on Unix/Linux)
* @return String where line endings are represented as linefeed "\n"
*/
public static String unfold(String string) {
if (!NL.equals("\n")) {
string = Olap4jUtil.replace(string, NL, "\n");
}
if (string == null) {
return null;
} else {
return string;
}
}

/**
* Converts an MDX parse tree to an MDX string
*
Expand Down

0 comments on commit c6702a9

Please sign in to comment.