Package mondrian.rolap.sql
Class SqlQuery
- java.lang.Object
-
- mondrian.rolap.sql.SqlQuery
-
public class SqlQuery extends Object
SqlQueryallows us to build aselectstatement and generate it in database-specific SQL syntax.Notable differences in database syntax are:
- Identifier quoting
- Oracle (and all JDBC-compliant drivers) uses double-quotes,
for example
select * from "emp". Access prefers brackets, for exampleselect * from [emp]. mySQL allows single- and double-quotes for string literals, and therefore does not allow identifiers to be quoted, for exampleselect 'foo', "bar" from emp. - AS in from clause
- Oracle doesn't like AS in the from * clause, for example
select from emp as evs.select * from emp e. - Column aliases
- Some databases require that every column in the select list
has a valid alias. If the expression is an expression containing
non-alphanumeric characters, an explicit alias is needed. For example,
Oracle will barfs at
select empno + 1 from emp. - Parentheses around table names
- Oracle doesn't like
select * from (emp) - Queries in FROM clause
- PostgreSQL and hsqldb don't allow, for example,
select * from (select * from emp) as e. - Uniqueness of index names
- In PostgreSQL and Oracle, index names must be unique within the database; in Access and hsqldb, they must merely be unique within their table
- Datatypes
- In Oracle, BIT is CHAR(1), TIMESTAMP is DATE. In PostgreSQL, DOUBLE is DOUBLE PRECISION, BIT is BOOL.
NOTE: Instances of this class are NOT thread safe so the user must make sure this is accessed by only one thread at a time.
- Author:
- jhyde
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSqlQuery.CodeSetCollection of alternative code for alternative dialects.
-
Constructor Summary
Constructors Constructor Description SqlQuery(Dialect dialect)Creates a SqlQuery using a given dialect and inheriting the formatting preferences fromMondrianProperties.GenerateFormattedSqlproperty.SqlQuery(Dialect dialect, boolean formatted)Base constructor used by all other constructors to create an empty instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddFrom(MondrianDef.RelationOrJoin relation, String alias, boolean failIfExists)Adds a relation to a query, adding appropriate join conditions, unless it is already present.voidaddFrom(SqlQuery sqlQuery, String alias, boolean failIfExists)booleanaddFromQuery(String query, String alias, boolean failIfExists)Adds a subquery to the FROM clause of this Query with a given alias.voidaddGroupBy(String expression)voidaddGroupBy(String expression, String alias)voidaddGroupingFunction(String columnExpr)voidaddGroupingSet(List<String> groupingColumnsExpr)voidaddHaving(String expression)voidaddOrderBy(String expr, boolean ascending, boolean prepend, boolean nullable)Adds an item to the ORDER BY clause.voidaddOrderBy(String expr, String alias, boolean ascending, boolean prepend, boolean nullable, boolean collateNullsLast)Adds an item to the ORDER BY clause.StringaddSelect(String expression, SqlStatement.Type type)Adds an expression to the select clause, automatically creating a column alias.StringaddSelect(String expression, SqlStatement.Type type, String alias)Adds an expression to the select clause, with a specified type and column alias.StringaddSelectGroupBy(String expression, SqlStatement.Type type)Adds an expression to the SELECT and GROUP BY clauses.voidaddWhere(String expression)voidaddWhere(String exprLeft, String exprMid, String exprRight)voidaddWhere(RolapStar.Condition joinCondition)SqlQuerycloneEmpty()Creates an emptySqlQuerywith the same environment as this one.StringgetAlias(String expression)intgetCurrentSelectListSize()DialectgetDialect()booleanisSupported()static SqlQuerynewQuery(DataSource dataSource, String err)StringnextColumnAlias()voidregisterRootRelation(MondrianDef.RelationOrJoin root)voidsetAllowHints(boolean t)Chooses whether table optimization hints may be used (assuming the dialect supports it).voidsetDistinct(boolean distinct)voidsetSupported(boolean supported)voidtoBuffer(StringBuilder buf, String prefix)Writes this SqlQuery to a StringBuilder with each clause on a separate line, and with the specified indentation prefix.Pair<String,List<SqlStatement.Type>>toSqlAndTypes()StringtoString()
-
-
-
Constructor Detail
-
SqlQuery
public SqlQuery(Dialect dialect, boolean formatted)
Base constructor used by all other constructors to create an empty instance.- Parameters:
dialect- Dialectformatted- Whether to generate SQL formatted on multiple lines
-
SqlQuery
public SqlQuery(Dialect dialect)
Creates a SqlQuery using a given dialect and inheriting the formatting preferences fromMondrianProperties.GenerateFormattedSqlproperty.- Parameters:
dialect- Dialect
-
-
Method Detail
-
cloneEmpty
public SqlQuery cloneEmpty()
Creates an emptySqlQuerywith the same environment as this one. (As per the Gang of Four 'prototype' pattern.)
-
setDistinct
public void setDistinct(boolean distinct)
-
setAllowHints
public void setAllowHints(boolean t)
Chooses whether table optimization hints may be used (assuming the dialect supports it).- Parameters:
t- True to allow hints to be used, false otherwise
-
addFromQuery
public boolean addFromQuery(String query, String alias, boolean failIfExists)
Adds a subquery to the FROM clause of this Query with a given alias. If the query already exists it either, depending onfailIfExists, throws an exception or does not add the query and returns false.- Parameters:
query- Subqueryalias- (if not null, must not be zero length).failIfExists- if true, throws exception if alias already exists- Returns:
- true if query *was* added
-
addFrom
public boolean addFrom(MondrianDef.RelationOrJoin relation, String alias, boolean failIfExists)
Adds a relation to a query, adding appropriate join conditions, unless it is already present.Returns whether the relation was added to the query.
- Parameters:
relation- Relation to addalias- Alias of relation. If null, uses relation's alias.failIfExists- Whether to fail if relation is already present- Returns:
- true, if relation *was* added to query
-
addSelect
public String addSelect(String expression, SqlStatement.Type type)
Adds an expression to the select clause, automatically creating a column alias.
-
addSelectGroupBy
public String addSelectGroupBy(String expression, SqlStatement.Type type)
Adds an expression to the SELECT and GROUP BY clauses. Uses the alias in the GROUP BY clause, if the dialect requires it.- Parameters:
expression- Expression- Returns:
- Alias of expression
-
getCurrentSelectListSize
public int getCurrentSelectListSize()
-
nextColumnAlias
public String nextColumnAlias()
-
addSelect
public String addSelect(String expression, SqlStatement.Type type, String alias)
Adds an expression to the select clause, with a specified type and column alias.- Parameters:
expression- Expressiontype- Java type to be used to hold cursor columnalias- Column alias (or null for no alias)- Returns:
- Column alias
-
addWhere
public void addWhere(RolapStar.Condition joinCondition)
-
addWhere
public void addWhere(String expression)
-
addGroupBy
public void addGroupBy(String expression)
-
addHaving
public void addHaving(String expression)
-
addOrderBy
public void addOrderBy(String expr, boolean ascending, boolean prepend, boolean nullable)
Adds an item to the ORDER BY clause.- Parameters:
expr- the expr to order byascending- sort directionprepend- whether to prepend to the current list of itemsnullable- whether the expression might be null
-
addOrderBy
public void addOrderBy(String expr, String alias, boolean ascending, boolean prepend, boolean nullable, boolean collateNullsLast)
Adds an item to the ORDER BY clause.- Parameters:
expr- the expr to order byalias- the alias of the column, as returned by addSelectascending- sort directionprepend- whether to prepend to the current list of itemsnullable- whether the expression might be nullcollateNullsLast- whether null values should appear first or last.
-
toBuffer
public void toBuffer(StringBuilder buf, String prefix)
Writes this SqlQuery to a StringBuilder with each clause on a separate line, and with the specified indentation prefix.- Parameters:
buf- String builderprefix- Prefix for each line
-
getDialect
public Dialect getDialect()
-
newQuery
public static SqlQuery newQuery(DataSource dataSource, String err)
-
addGroupingFunction
public void addGroupingFunction(String columnExpr)
-
toSqlAndTypes
public Pair<String,List<SqlStatement.Type>> toSqlAndTypes()
-
registerRootRelation
public void registerRootRelation(MondrianDef.RelationOrJoin root)
-
isSupported
public boolean isSupported()
-
setSupported
public void setSupported(boolean supported)
-
-