use of mondrian.olap.Query in project mondrian by pentaho.
the class BatchTestCase method executeQuery.
protected Result executeQuery(String mdx, Connection connection) {
Query query = connection.parseQuery(mdx);
query.setResultStyle(ResultStyle.LIST);
return connection.execute(query);
}
use of mondrian.olap.Query in project mondrian by pentaho.
the class BatchTestCase method assertQuerySqlOrNot.
/**
* During MDX query parse and execution, checks that the query results
* (or does not result) in a particular SQL statement being generated.
*
* <p>Parses and executes the MDX query once for each SQL
* pattern in the current dialect. If there are multiple patterns, runs the
* MDX query multiple times, and expects to see each SQL statement appear.
* If there are no patterns in this dialect, the test trivially succeeds.
*
* @param testContext non-default test context if required
* @param mdxQuery MDX query
* @param patterns Set of patterns
* @param negative false to assert if SQL is generated;
* true to assert if SQL is NOT generated
* @param bypassSchemaCache whether to grab a new connection and bypass the
* schema cache before parsing the MDX query
* @param clearCache whether to clear cache before executing the MDX query
*/
protected void assertQuerySqlOrNot(TestContext testContext, String mdxQuery, SqlPattern[] patterns, boolean negative, boolean bypassSchemaCache, boolean clearCache) {
Connection connection = testContext.getConnection();
mdxQuery = testContext.upgradeQuery(mdxQuery);
// Run the test once for each pattern in this dialect.
// (We could optimize and run it once, collecting multiple queries, and
// comparing all queries at the end.)
Dialect dialect = testContext.getDialect();
Dialect.DatabaseProduct d = dialect.getDatabaseProduct();
boolean patternFound = false;
for (SqlPattern sqlPattern : patterns) {
if (!sqlPattern.hasDatabaseProduct(d)) {
// message if required.
continue;
}
patternFound = true;
String sql = sqlPattern.getSql();
String trigger = sqlPattern.getTriggerSql();
sql = dialectize(d, sql);
trigger = dialectize(d, trigger);
// Create a dummy DataSource which will throw a 'bomb' if it is
// asked to execute a particular SQL statement, but will otherwise
// behave exactly the same as the current DataSource.
final TriggerHook hook = new TriggerHook(trigger);
RolapUtil.setHook(hook);
Bomb bomb = null;
try {
if (bypassSchemaCache) {
connection = testContext.withSchemaPool(false).getConnection();
}
final Query query = connection.parseQuery(mdxQuery);
if (clearCache) {
clearCache((RolapCube) query.getCube());
}
final Result result = connection.execute(query);
Util.discard(result);
bomb = null;
} catch (Bomb e) {
bomb = e;
} catch (RuntimeException e) {
// Walk up the exception tree and see if the root cause
// was a SQL bomb.
bomb = Util.getMatchingCause(e, Bomb.class);
if (bomb == null) {
throw e;
}
} finally {
RolapUtil.setHook(null);
}
if (negative) {
if (bomb != null || hook.foundMatch()) {
fail("forbidden query [" + sql + "] detected");
}
} else {
if (bomb == null && !hook.foundMatch()) {
fail("expected query [" + sql + "] did not occur");
}
if (bomb != null) {
assertEquals(replaceQuotes(sql.replaceAll("\r\n", "\n")), replaceQuotes(bomb.sql.replaceAll("\r\n", "\n")));
}
}
}
// dialect.
if (!patternFound) {
String warnDialect = MondrianProperties.instance().WarnIfNoPatternForDialect.get();
if (warnDialect.equals(d.toString())) {
System.out.println("[No expected SQL statements found for dialect \"" + dialect.toString() + "\" and test not run]");
}
}
}
use of mondrian.olap.Query in project mondrian by pentaho.
the class TestContext method assertExprDependsOn.
/**
* Asserts that an MDX expression depends upon a given list of dimensions.
*/
public void assertExprDependsOn(String expr, String hierList) {
// Construct a query, and mine it for a parsed expression.
// Use a fresh connection, because some tests define their own dims.
final Connection connection = getConnection();
final String queryString = "WITH MEMBER [Measures].[Foo] AS " + Util.singleQuoteString(expr) + " SELECT FROM [Sales]";
final Query query = connection.parseQuery(queryString);
query.resolve();
final Formula formula = query.getFormulas()[0];
final Exp expression = formula.getExpression();
// Build a list of the dimensions which the expression depends upon,
// and check that it is as expected.
checkDependsOn(query, expression, hierList, true);
}
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;
}
}
use of mondrian.olap.Query in project mondrian by pentaho.
the class TestContext method assertParameterizedExprReturns.
/**
* Asserts that an expression, with a given set of parameter bindings, returns a given result.
*
* @param expr Scalar MDX expression
* @param expected Expected result
* @param paramValues Array of parameter names and values
*/
public void assertParameterizedExprReturns(String expr, String expected, Object... paramValues) {
Connection connection = getConnection();
String queryString = generateExpression(expr);
Query query = connection.parseQuery(queryString);
assert paramValues.length % 2 == 0;
for (int i = 0; i < paramValues.length; ) {
final String paramName = (String) paramValues[i++];
final Object value = paramValues[i++];
query.setParameter(paramName, value);
}
final Result result = connection.execute(query);
final Cell cell = result.getCell(new int[] { 0 });
if (expected == null) {
// null values are formatted as empty string
expected = "";
}
assertEqualsVerbose(expected, cell.getFormattedValue());
}
Aggregations