From e33489c40f068d1b03571065c53151ced32e723d Mon Sep 17 00:00:00 2001 From: Julian Hyde Date: Sat, 2 May 2009 07:28:35 +0000 Subject: [PATCH] In SimpleQuerySample, use Axis.axisOrdinal not Axis.ordinal(); fix driver class name; add example of using a CellSetFormatter. git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@205 c6a108a4-781c-0410-a6c6-c2d559e19af0 --- src/org/olap4j/sample/SimpleQuerySample.java | 58 ++++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/org/olap4j/sample/SimpleQuerySample.java b/src/org/olap4j/sample/SimpleQuerySample.java index 7319c02..1203fb4 100644 --- a/src/org/olap4j/sample/SimpleQuerySample.java +++ b/src/org/olap4j/sample/SimpleQuerySample.java @@ -3,7 +3,7 @@ // This software is subject to the terms of the Common Public License // Agreement, available at the following URL: // http://www.opensource.org/licenses/cpl.html. -// Copyright (C) 2006-2008 Julian Hyde +// Copyright (C) 2006-2009 Julian Hyde // All Rights Reserved. // You must accept the terms of that agreement to use this software. */ @@ -15,8 +15,10 @@ import org.olap4j.mdx.*; import org.olap4j.metadata.Dimension; import org.olap4j.metadata.Member; +import org.olap4j.query.RectangularCellSetFormatter; import org.olap4j.type.MemberType; +import java.io.PrintWriter; import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -29,6 +31,11 @@ * @since Aug 22, 2006 */ public class SimpleQuerySample { + /** + * Main command-line entry. + * + * @param args Command line arguments + */ public static void main(String[] args) { try { new SimpleQuerySample().simpleStatement(); @@ -43,13 +50,13 @@ public static void main(String[] args) { /** * Simple example which connects to an OLAP server, executes an OLAP - * statement and prints the result. + * statement and prints the resulting cell set. */ void simpleStatement() throws SQLException, ClassNotFoundException { // Register driver. - Class.forName("mondrian.olap4j.Driver"); + Class.forName("mondrian.olap4j.MondrianOlap4jDriver"); // Create connection. Connection connection = @@ -59,17 +66,17 @@ void simpleStatement() // Execute a statement. OlapStatement statement = olapConnection.createStatement(); - CellSet result = + CellSet cellSet = statement.executeOlapQuery( - "select {[Measures].[Unit Sales]} on columns,\n" + - " CrossJoin([Store].Children, [Gender].Members) on rows\n" + - "from [Sales]"); + "select {[Measures].[Unit Sales]} on columns,\n" + + " CrossJoin([Store].Children, [Gender].Members) on rows\n" + + "from [Sales]"); - List cellSetAxes = result.getAxes(); + List cellSetAxes = cellSet.getAxes(); // Print headings. System.out.print("\t"); - CellSetAxis columnsAxis = cellSetAxes.get(Axis.COLUMNS.ordinal()); + CellSetAxis columnsAxis = cellSetAxes.get(Axis.COLUMNS.axisOrdinal()); for (Position position : columnsAxis.getPositions()) { Member measure = position.getMembers().get(0); System.out.print(measure.getName()); @@ -94,12 +101,12 @@ void simpleStatement() // Access the cell via its ordinal. The ordinal is kept in step // because we increment the ordinal once for each row and // column. - Cell cell = result.getCell(cellOrdinal); + Cell cell = cellSet.getCell(cellOrdinal); // Just for kicks, convert the ordinal to a list of coordinates. // The list matches the row and column positions. List coordList = - result.ordinalToCoordinates(cellOrdinal); + cellSet.ordinalToCoordinates(cellOrdinal); assert coordList.get(0) == rowPosition.getOrdinal(); assert coordList.get(1) == columnPosition.getOrdinal(); @@ -111,6 +118,12 @@ void simpleStatement() System.out.println(); } + // Now, nicely formatted. + System.out.println(); + final PrintWriter pw = new PrintWriter(System.out); + new RectangularCellSetFormatter(false).format(cellSet, pw); + pw.flush(); + // Close the statement and connection. statement.close(); connection.close(); @@ -152,9 +165,9 @@ void preparedStatement() throws SQLException, ClassNotFoundException { statement.setObject(1, memberSeattle); statement.setInt(2, 10); - // Execute, and print result. - CellSet result = statement.executeQuery(); - printResult(result); + // Execute, and print cell set. + CellSet cellSet = statement.executeQuery(); + printCellSet(cellSet); // Close the statement and connection. statement.close(); @@ -185,11 +198,19 @@ void statementFromParseTree() throws ClassNotFoundException, SQLException { // Create statement. OlapStatement statement = olapConnection.createStatement(); CellSet cellSet = statement.executeOlapQuery(query); - printResult(cellSet); + printCellSet(cellSet); } - private void printResult(CellSet result) { - List cellSetAxes = result.getAxes(); + /** + * Prints a cell set. + * + *

For more sophisticated printing of cell sets, see + * {@link org.olap4j.query.CellSetFormatter}. + * + * @param cellSet Cell set + */ + private void printCellSet(CellSet cellSet) { + List cellSetAxes = cellSet.getAxes(); // Print headings. System.out.print("\t"); @@ -221,7 +242,7 @@ private void printResult(CellSet result) { for (Position columnPosition : columnsAxis.getPositions()) { assert columnPosition.getOrdinal() == column; coordList.set(1, column++); - Cell cell = result.getCell(coordList); + Cell cell = cellSet.getCell(coordList); System.out.print('\t'); System.out.print(cell.getFormattedValue()); } @@ -267,6 +288,7 @@ void executeSelectNode(OlapConnection connection) { CellSet cset; try { cset = stmt.executeOlapQuery(query); + printCellSet(cset); } catch (OlapException e) { System.out.println("Execution failed: " + e); }