Skip to content

Commit

Permalink
In SimpleQuerySample, use Axis.axisOrdinal not Axis.ordinal();
Browse files Browse the repository at this point in the history
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
  • Loading branch information
julianhyde committed May 2, 2009
1 parent baf3322 commit e33489c
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions src/org/olap4j/sample/SimpleQuerySample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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 =
Expand All @@ -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<CellSetAxis> cellSetAxes = result.getAxes();
List<CellSetAxis> 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());
Expand All @@ -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<Integer> coordList =
result.ordinalToCoordinates(cellOrdinal);
cellSet.ordinalToCoordinates(cellOrdinal);
assert coordList.get(0) == rowPosition.getOrdinal();
assert coordList.get(1) == columnPosition.getOrdinal();

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<CellSetAxis> cellSetAxes = result.getAxes();
/**
* Prints a cell set.
*
* <p>For more sophisticated printing of cell sets, see
* {@link org.olap4j.query.CellSetFormatter}.
*
* @param cellSet Cell set
*/
private void printCellSet(CellSet cellSet) {
List<CellSetAxis> cellSetAxes = cellSet.getAxes();

// Print headings.
System.out.print("\t");
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit e33489c

Please sign in to comment.