Search in sources :

Example 11 with Query

use of mondrian.olap.Query in project mondrian by pentaho.

the class TestContext method databaseIsValid.

/**
 * Tests whether the database is valid. Allows tests that depend on optional databases to figure out whether to
 * proceed.
 *
 * @return whether a database is present and correct
 */
public boolean databaseIsValid() {
    try {
        Connection connection = getConnection();
        String cubeName = getDefaultCubeName();
        if (cubeName.indexOf(' ') >= 0) {
            cubeName = Util.quoteMdxIdentifier(cubeName);
        }
        Query query = connection.parseQuery("select from " + cubeName);
        Result result = connection.execute(query);
        Util.discard(result);
        connection.close();
        return true;
    } catch (RuntimeException e) {
        Util.discard(e);
        return false;
    }
}
Also used : Query(mondrian.olap.Query) OlapConnection(org.olap4j.OlapConnection) Connection(mondrian.olap.Connection) Result(mondrian.olap.Result)

Example 12 with Query

use of mondrian.olap.Query in project mondrian by pentaho.

the class SqlConstraintUtilsTest method testExpandSupportedCalculatedMembers2.

// test with a placeholder member
public void testExpandSupportedCalculatedMembers2() {
    final TestContext testContext = TestContext.instance();
    final Connection connection = testContext.getConnection();
    final String queryText = "SELECT {[Measures].[Customer Count]} ON 0 " + "FROM [Sales] " + "WHERE [Time].[1997]";
    final Query query = connection.parseQuery(queryText);
    final QueryAxis querySlicerAxis = query.getSlicerAxis();
    final Member slicerMember = ((MemberExpr) querySlicerAxis.getSet()).getMember();
    final RolapHierarchy slicerHierarchy = ((RolapCube) query.getCube()).getTimeHierarchy(null);
    final Execution execution = new Execution(query.getStatement(), 0L);
    final RolapEvaluatorRoot rolapEvaluatorRoot = new RolapEvaluatorRoot(execution);
    final RolapEvaluator rolapEvaluator = new RolapEvaluator(rolapEvaluatorRoot);
    final Member expectedMember = slicerMember;
    setSlicerContext(rolapEvaluator, expectedMember);
    RolapResult.CompoundSlicerRolapMember placeHolderMember = Mockito.mock(RolapResult.CompoundSlicerRolapMember.class);
    Mockito.doReturn(slicerHierarchy).when(placeHolderMember).getHierarchy();
    Member endMember0 = makeNoncalculatedMember("0");
    // (0, placeholder)
    Member[] argMembers = new Member[] { endMember0, placeHolderMember };
    Member[] expectedMembers = new Member[] { endMember0, slicerMember };
    Member[] expectedMembersOnDisjoin = new Member[] { endMember0 };
    assertApartExpandSupportedCalculatedMembers("(0, placeholder)", expectedMembers, expectedMembersOnDisjoin, argMembers, rolapEvaluator);
}
Also used : Query(mondrian.olap.Query) SqlQuery(mondrian.rolap.sql.SqlQuery) TestContext(mondrian.test.TestContext) Connection(mondrian.olap.Connection) Execution(mondrian.server.Execution) MemberExpr(mondrian.mdx.MemberExpr) Member(mondrian.olap.Member) TestMember(mondrian.olap.fun.TestMember) QueryAxis(mondrian.olap.QueryAxis)

Example 13 with Query

use of mondrian.olap.Query in project mondrian by pentaho.

the class RolapConnectionTest method testDataSourceOverrideUserPass.

public void testDataSourceOverrideUserPass() throws SQLException, NamingException {
    // use the datasource property to connect to the database
    Util.PropertyList properties = spy(TestContext.instance().getConnectionProperties().clone());
    final Dialect dialect = TestContext.instance().getDialect();
    if (dialect.getDatabaseProduct() == Dialect.DatabaseProduct.ACCESS) {
        // Access doesn't accept user/password, so this test is pointless.
        return;
    }
    final String jdbcUser = properties.get(RolapConnectionProperties.JdbcUser.name());
    final String jdbcPassword = properties.get(RolapConnectionProperties.JdbcPassword.name());
    if (jdbcUser == null || jdbcPassword == null) {
        // Can only run this test if username and password are explicit.
        return;
    }
    // Define a data source with bogus user and password.
    properties.put(RolapConnectionProperties.JdbcUser.name(), "bogususer");
    properties.put(RolapConnectionProperties.JdbcPassword.name(), "boguspassword");
    properties.put(RolapConnectionProperties.PoolNeeded.name(), "false");
    final StringBuilder buf = new StringBuilder();
    final DataSource dataSource = RolapConnection.createDataSource(null, properties, buf);
    final String desc = buf.toString();
    assertTrue(desc, desc.startsWith("Jdbc="));
    assertTrue(desc, desc.indexOf("JdbcUser=bogususer") >= 0);
    verify(properties, atLeastOnce()).get(RolapConnectionProperties.JdbcPassword.name());
    final String jndiName = "jndiDataSource";
    THREAD_INITIAL_CONTEXT.set(new InitialContext() {

        public Object lookup(String str) {
            return str.equals(jndiName) ? dataSource : null;
        }
    });
    // Create a property list that we will use for the actual mondrian
    // connection. Replace the original JDBC info with the data source we
    // just created.
    final Util.PropertyList properties2 = new Util.PropertyList();
    for (Pair<String, String> entry : properties) {
        properties2.put(entry.getKey(), entry.getValue());
    }
    properties2.remove(RolapConnectionProperties.Jdbc.name());
    properties2.put(RolapConnectionProperties.DataSource.name(), jndiName);
    // With JdbcUser and JdbcPassword credentials in the mondrian connect
    // string, the data source's "user" and "password" properties are
    // overridden and the connection succeeds.
    properties2.put(RolapConnectionProperties.JdbcUser.name(), jdbcUser);
    properties2.put(RolapConnectionProperties.JdbcPassword.name(), jdbcPassword);
    mondrian.olap.Connection connection = null;
    try {
        connection = DriverManager.getConnection(properties2, null);
        Query query = connection.parseQuery("select from [Sales]");
        final Result result = connection.execute(query);
        assertNotNull(result);
    } finally {
        if (connection != null) {
            connection.close();
            connection = null;
        }
    }
    // If we don't specify JdbcUser and JdbcPassword in the mondrian
    // connection properties, mondrian uses the data source's
    // bogus credentials, and the connection fails.
    properties2.remove(RolapConnectionProperties.JdbcUser.name());
    properties2.remove(RolapConnectionProperties.JdbcPassword.name());
    for (String poolNeeded : Arrays.asList("false", "true")) {
        // Important to test with & without pooling. Connection pools
        // typically do not let you change user, so it's important that
        // mondrian handles these right.
        properties2.put(RolapConnectionProperties.PoolNeeded.name(), poolNeeded);
        try {
            connection = DriverManager.getConnection(properties2, null);
            fail("Expected exception");
        } catch (MondrianException e) {
            final String s = TestContext.getStackTrace(e);
            assertTrue(s, s.indexOf("Error while creating SQL connection: " + "DataSource=jndiDataSource") >= 0);
            switch(dialect.getDatabaseProduct()) {
                case DERBY:
                    assertTrue(s, s.indexOf("Caused by: java.sql.SQLException: " + "Schema 'BOGUSUSER' does not exist") >= 0);
                    break;
                case ORACLE:
                    assertTrue(s, s.indexOf("Caused by: java.sql.SQLException: ORA-01017: " + "invalid username/password; logon denied") >= 0);
                    break;
                case MYSQL:
                case MARIADB:
                    assertTrue(s, s.indexOf("Caused by: java.sql.SQLException: Access denied " + "for user 'bogususer'") >= 0);
                    break;
                case POSTGRESQL:
                    assertTrue(s, s.indexOf("Caused by: org.postgresql.util.PSQLException: " + "FATAL: password authentication failed for " + "user \"bogususer\"") >= 0);
                    break;
            }
        } finally {
            if (connection != null) {
                connection.close();
                connection = null;
            }
        }
    }
}
Also used : Query(mondrian.olap.Query) Util(mondrian.olap.Util) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) Result(mondrian.olap.Result) Dialect(mondrian.spi.Dialect) MondrianException(mondrian.olap.MondrianException)

Example 14 with Query

use of mondrian.olap.Query in project mondrian by pentaho.

the class SqlConstraintUtilsTest method testReplaceCompoundSlicerPlaceholder.

public void testReplaceCompoundSlicerPlaceholder() {
    final TestContext testContext = TestContext.instance();
    final Connection connection = testContext.getConnection();
    final String queryText = "SELECT {[Measures].[Customer Count]} ON 0 " + "FROM [Sales] " + "WHERE [Time].[1997]";
    final Query query = connection.parseQuery(queryText);
    final QueryAxis querySlicerAxis = query.getSlicerAxis();
    final Member slicerMember = ((MemberExpr) querySlicerAxis.getSet()).getMember();
    final RolapHierarchy slicerHierarchy = ((RolapCube) query.getCube()).getTimeHierarchy(null);
    final Execution execution = new Execution(query.getStatement(), 0L);
    final RolapEvaluatorRoot rolapEvaluatorRoot = new RolapEvaluatorRoot(execution);
    final RolapEvaluator rolapEvaluator = new RolapEvaluator(rolapEvaluatorRoot);
    final Member expectedMember = slicerMember;
    setSlicerContext(rolapEvaluator, expectedMember);
    RolapResult.CompoundSlicerRolapMember placeHolderMember = Mockito.mock(RolapResult.CompoundSlicerRolapMember.class);
    Mockito.doReturn(slicerHierarchy).when(placeHolderMember).getHierarchy();
    // tested call
    Member r = SqlConstraintUtils.replaceCompoundSlicerPlaceholder(placeHolderMember, rolapEvaluator);
    // test
    Assert.assertSame(expectedMember, r);
}
Also used : Query(mondrian.olap.Query) SqlQuery(mondrian.rolap.sql.SqlQuery) TestContext(mondrian.test.TestContext) Connection(mondrian.olap.Connection) Execution(mondrian.server.Execution) MemberExpr(mondrian.mdx.MemberExpr) Member(mondrian.olap.Member) TestMember(mondrian.olap.fun.TestMember) QueryAxis(mondrian.olap.QueryAxis)

Example 15 with Query

use of mondrian.olap.Query in project mondrian by pentaho.

the class PerformanceTest method checkVeryLargeExplicitSet.

private void checkVeryLargeExplicitSet(Statistician[] statisticians, TestContext testContext) {
    Result result;
    long start = System.currentTimeMillis();
    final Axis axis = testContext.executeAxis("Customers.Members");
    statisticians[0].record(start);
    final List<Position> positionList = axis.getPositions();
    assertEquals(10407, positionList.size());
    // Take customers 0-2000 and 5000-7000. Using contiguous bursts,
    // Mondrian has a chance to optimize how it reads cells from the
    // database.
    List<Member> memberList = new ArrayList<Member>();
    for (int i = 0; i < positionList.size(); i++) {
        Position position = positionList.get(i);
        ++i;
        if (i < 2000 || i >= 5000 && i < 7000) {
            memberList.add(position.get(0));
        }
    }
    // Build a query with an explcit member list.
    if (LOGGER.isDebugEnabled()) {
        StringBuilder buf = new StringBuilder();
        for (Member member : memberList) {
            if (buf.length() > 0) {
                buf.append(", ");
            }
            buf.append(member);
        }
        final String mdx = "WITH SET [Selected Customers] AS {" + buf + "}\n" + "SELECT {[Measures].[Unit Sales],\n" + "        [Measures].[Store Sales]} on 0,\n" + "  [Selected Customers] on 1\n" + "FROM [Sales]";
        start = System.currentTimeMillis();
        result = testContext.executeQuery(mdx);
        statisticians[1].record(start);
        assertEquals(memberList.size(), result.getAxes()[1].getPositions().size());
    }
    // Much more efficient technique. Use a parameter, and bind to array.
    // Cuts out a lot of parsing, so takes 2.4s as opposed to 65s.
    Query query = testContext.getConnection().parseQuery("WITH SET [Selected Customers]\n" + "  AS Parameter('Foo', [Customers], {}, 'Description')\n" + "SELECT {[Measures].[Unit Sales],\n" + "        [Measures].[Store Sales]} on 0,\n" + "  [Selected Customers] on 1\n" + "FROM [Sales]");
    query.setParameter("Foo", memberList);
    start = System.currentTimeMillis();
    result = testContext.getConnection().execute(query);
    statisticians[2].record(start);
    assertEquals(memberList.size(), result.getAxes()[1].getPositions().size());
}
Also used : Query(mondrian.olap.Query) Position(mondrian.olap.Position) ArrayList(java.util.ArrayList) Member(mondrian.olap.Member) Axis(mondrian.olap.Axis) Result(mondrian.olap.Result)

Aggregations

Query (mondrian.olap.Query)26 Connection (mondrian.olap.Connection)17 OlapConnection (org.olap4j.OlapConnection)14 Result (mondrian.olap.Result)9 RolapConnection (mondrian.rolap.RolapConnection)7 Member (mondrian.olap.Member)6 Exp (mondrian.olap.Exp)5 ArrayList (java.util.ArrayList)3 MemberExpr (mondrian.mdx.MemberExpr)3 MondrianException (mondrian.olap.MondrianException)3 SqlQuery (mondrian.rolap.sql.SqlQuery)3 Execution (mondrian.server.Execution)3 HashSet (java.util.HashSet)2 TupleList (mondrian.calc.TupleList)2 ListTupleList (mondrian.calc.impl.ListTupleList)2 Formula (mondrian.olap.Formula)2 Hierarchy (mondrian.olap.Hierarchy)2 QueryAxis (mondrian.olap.QueryAxis)2 TestMember (mondrian.olap.fun.TestMember)2 Dialect (mondrian.spi.Dialect)2