From afa7d801f8467676f86eff85cd942a1417c96933 Mon Sep 17 00:00:00 2001 From: Julian Hyde Date: Thu, 24 Jan 2008 07:21:25 +0000 Subject: [PATCH] Add constructor for IdentifierNode which takes a list (convenient for calling with the result from IdentifierNode.parseIdentifier); Test case for building MDX parse tree programmatically and executing as query. git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@66 c6a108a4-781c-0410-a6c6-c2d559e19af0 --- src/org/olap4j/mdx/AxisNode.java | 10 +++- src/org/olap4j/mdx/IdentifierNode.java | 2 +- testsrc/org/olap4j/ConnectionTest.java | 78 ++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/src/org/olap4j/mdx/AxisNode.java b/src/org/olap4j/mdx/AxisNode.java index 0e04594..cfce0f0 100644 --- a/src/org/olap4j/mdx/AxisNode.java +++ b/src/org/olap4j/mdx/AxisNode.java @@ -11,6 +11,7 @@ import java.io.PrintWriter; import java.util.List; +import java.util.Collections; import org.olap4j.Axis; import org.olap4j.type.Type; @@ -28,7 +29,7 @@ public class AxisNode implements ParseTreeNode { private ParseTreeNode expression; private final Axis axis; - private final List dimensionProperties; + private final List dimensionProperties; /** * Creates an axis. @@ -38,7 +39,8 @@ public class AxisNode implements ParseTreeNode { * are all empty * @param expression Expression to populate the axis * @param axisDef Which axis (ROWS, COLUMNS, etc.) - * @param dimensionProperties List of dimension properties + * @param dimensionProperties List of dimension properties; if null, + * empty list is assumed */ public AxisNode( ParseRegion region, @@ -48,10 +50,12 @@ public AxisNode( List dimensionProperties) { this.region = region; - assert dimensionProperties != null; this.nonEmpty = nonEmpty; this.expression = expression; this.axis = axisDef; + if (dimensionProperties == null) { + dimensionProperties = Collections.emptyList(); + } this.dimensionProperties = dimensionProperties; } diff --git a/src/org/olap4j/mdx/IdentifierNode.java b/src/org/olap4j/mdx/IdentifierNode.java index 582b54c..369ce51 100644 --- a/src/org/olap4j/mdx/IdentifierNode.java +++ b/src/org/olap4j/mdx/IdentifierNode.java @@ -42,7 +42,7 @@ public IdentifierNode(IdentifierNode.Segment... segments) { * * @param segments List of segments */ - private IdentifierNode(List segments) { + public IdentifierNode(List segments) { if (segments.size() < 1) { throw new IllegalArgumentException(); } diff --git a/testsrc/org/olap4j/ConnectionTest.java b/testsrc/org/olap4j/ConnectionTest.java index cf780f2..89d7c49 100644 --- a/testsrc/org/olap4j/ConnectionTest.java +++ b/testsrc/org/olap4j/ConnectionTest.java @@ -1744,6 +1744,84 @@ public void testCellSetWithCalcMember() throws SQLException { "Row #1: $3.94\n" + "Row #2: $3.93\n"), TestContext.toString(cellSet)); } + + public void testBuildQuery() throws SQLException { + Connection connection = tester.createConnection(); + OlapConnection olapConnection = + ((OlapWrapper) connection).unwrap(OlapConnection.class); + buildQuery(olapConnection, true); + buildQuery(olapConnection, false); + } + + private void buildQuery( + OlapConnection olapConnection, + boolean useCubeObject) + throws OlapException + { + final String catalogName; + switch (tester.getFlavor()) { + case MONDRIAN: + catalogName = "LOCALDB"; + break; + case XMLA: + default: + catalogName = "FoodMart"; + break; + } + Catalog catalog = olapConnection.getCatalogs().get(catalogName); + Schema schema = catalog.getSchemas().get("FoodMart"); + Cube cube = schema.getCubes().get("Sales"); + SelectNode query = new SelectNode(); + ParseTreeNode cubeNode; + if (useCubeObject) { + cubeNode = new IdentifierNode( + IdentifierNode.parseIdentifier(cube.getUniqueName())); + } else { + cubeNode = new CubeNode(null, cube); + } + query.setFrom(cubeNode); + AxisNode columnAxis = + new AxisNode( + null, false, + new CallNode( + null, "MEMBERS", Syntax.Property, + new IdentifierNode( + IdentifierNode.parseIdentifier("[Gender]"))), + Axis.COLUMNS, null); + AxisNode rowAxis = + new AxisNode( + null, false, + new CallNode( + null, "CHILDREN", Syntax.Property, + new IdentifierNode( + IdentifierNode.parseIdentifier("[Customers].[USA]"))), + Axis.ROWS, null); + query.getAxisList().add(columnAxis); + query.getAxisList().add(rowAxis); + OlapStatement statement = olapConnection.createStatement(); + CellSet cellSet = statement.executeOlapQuery(query); + TestContext.assertEqualsVerbose(TestContext.fold( + "Axis #0:\n" + + "{[Measures].[Unit Sales], [Store].[All Stores], [Store Size in SQFT].[All Store Size in SQFTs], [Store Type].[All Store Types], [Time].[1997], [Product].[All Products], [Promotion Media].[All Media], [Promotions].[All Promotions], [Education Level].[All Education Levels], [Marital Status].[All Marital Status], [Yearly Income].[All Yearly Incomes]}\n" + + "Axis #1:\n" + + "{[Gender].[All Gender]}\n" + + "{[Gender].[All Gender].[F]}\n" + + "{[Gender].[All Gender].[M]}\n" + + "Axis #2:\n" + + "{[Customers].[All Customers].[USA].[CA]}\n" + + "{[Customers].[All Customers].[USA].[OR]}\n" + + "{[Customers].[All Customers].[USA].[WA]}\n" + + "Row #0: 74,748\n" + + "Row #0: 36,759\n" + + "Row #0: 37,989\n" + + "Row #1: 67,659\n" + + "Row #1: 33,036\n" + + "Row #1: 34,623\n" + + "Row #2: 124,366\n" + + "Row #2: 61,763\n" + + "Row #2: 62,603\n"), + TestContext.toString(cellSet)); + } } // End ConnectionTest.java