Class SqlGenerationVisitor
- java.lang.Object
-
- com.exasol.adapter.dialects.rewriting.SqlGenerationVisitor
-
- All Implemented Interfaces:
SqlGenerator,com.exasol.adapter.sql.SqlNodeVisitor<String>
public class SqlGenerationVisitor extends Object implements com.exasol.adapter.sql.SqlNodeVisitor<String>, SqlGenerator
This class has the logic to generate SQL queries based on a graph ofSqlNodeelements. It uses the visitor pattern. This class interacts with the dialects in some situations, e.g. to find out how to handle quoting, case-sensitivity.If this class is not sufficiently customizable for your use case, you can extend this class and override the required methods. You also have to return your custom visitor class then in the method
Note on operator associativity and parenthesis generation: Currently we almost always use parenthesis. Without parenthesis, twoSqlDialect.getSqlGenerator(SqlGenerationContext).SqlNodegraphs with different semantic lead toselect 1 = 1 - 1 + 1. AlsoSELECT NOT NOT TRUEneeds to be written asSELECT NOT (NOT TRUE)to work at all, whereasSELECT NOT TRUEworks fine without parentheses. Currently we make inflationary use of parenthesis to enforce the right semantic, but hopefully there is a better way.
-
-
Constructor Summary
Constructors Constructor Description SqlGenerationVisitor(SqlDialect dialect, SqlGenerationContext context)Creates a new instance of theSqlGenerationVisitor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckDialectAliases()Check if dialect provided invalid aliases, which would never be applied.protected StringcreateExplicitColumnsSelectList(com.exasol.adapter.sql.SqlSelectList selectList)Create a list of explicitly specified columns (where columns can also be expressions) for aSELECTlist.StringgenerateSqlFor(com.exasol.adapter.sql.SqlNode sqlNode)Generate a SQL representation of the passSqlNode.protected SqlDialectgetDialect()Get the SQL dialect.protected StringgetTypeNameFromColumn(com.exasol.adapter.sql.SqlColumn column)Get the type name for a columnprotected booleanisDirectlyInSelectList(com.exasol.adapter.sql.SqlColumn column)Check if a column is directly in the select list.protected StringrepresentAnyColumnInSelectList()Represent "any column" in theSELECTlist.Stringvisit(com.exasol.adapter.sql.SqlColumn column)Stringvisit(com.exasol.adapter.sql.SqlFunctionAggregate function)Stringvisit(com.exasol.adapter.sql.SqlFunctionAggregateGroupConcat function)Stringvisit(com.exasol.adapter.sql.SqlFunctionAggregateListagg sqlFunctionAggregateListagg)Stringvisit(com.exasol.adapter.sql.SqlFunctionScalar function)Stringvisit(com.exasol.adapter.sql.SqlFunctionScalarCase function)Stringvisit(com.exasol.adapter.sql.SqlFunctionScalarCast function)Stringvisit(com.exasol.adapter.sql.SqlFunctionScalarExtract function)Stringvisit(com.exasol.adapter.sql.SqlFunctionScalarJsonValue function)Stringvisit(com.exasol.adapter.sql.SqlGroupBy groupBy)Stringvisit(com.exasol.adapter.sql.SqlJoin join)Stringvisit(com.exasol.adapter.sql.SqlLimit limit)Stringvisit(com.exasol.adapter.sql.SqlLiteralBool literal)Stringvisit(com.exasol.adapter.sql.SqlLiteralDate literal)Stringvisit(com.exasol.adapter.sql.SqlLiteralDouble literal)Get a value of the double converted to an E-notation format.Stringvisit(com.exasol.adapter.sql.SqlLiteralExactnumeric literal)Formats a value of the exactnumeric as a plain string without E notation.Stringvisit(com.exasol.adapter.sql.SqlLiteralInterval literal)Stringvisit(com.exasol.adapter.sql.SqlLiteralNull literal)Stringvisit(com.exasol.adapter.sql.SqlLiteralString literal)Stringvisit(com.exasol.adapter.sql.SqlLiteralTimestamp literal)Stringvisit(com.exasol.adapter.sql.SqlLiteralTimestampUtc literal)Stringvisit(com.exasol.adapter.sql.SqlOrderBy orderBy)Stringvisit(com.exasol.adapter.sql.SqlPredicateAnd predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateBetween predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateEqual predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateInConstList predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateIsJson function)Stringvisit(com.exasol.adapter.sql.SqlPredicateIsNotJson function)Stringvisit(com.exasol.adapter.sql.SqlPredicateIsNotNull predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateIsNull predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateLess predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateLessEqual predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateLike predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateLikeRegexp predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateNot predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateNotEqual predicate)Stringvisit(com.exasol.adapter.sql.SqlPredicateOr predicate)Stringvisit(com.exasol.adapter.sql.SqlSelectList selectList)Stringvisit(com.exasol.adapter.sql.SqlStatementSelect select)Stringvisit(com.exasol.adapter.sql.SqlTable table)
-
-
-
Constructor Detail
-
SqlGenerationVisitor
public SqlGenerationVisitor(SqlDialect dialect, SqlGenerationContext context)
Creates a new instance of theSqlGenerationVisitor.- Parameters:
dialect- SQl dialectcontext- SQL generation context
-
-
Method Detail
-
generateSqlFor
public String generateSqlFor(com.exasol.adapter.sql.SqlNode sqlNode) throws com.exasol.adapter.AdapterException
Description copied from interface:SqlGeneratorGenerate a SQL representation of the passSqlNode.- Specified by:
generateSqlForin interfaceSqlGenerator- Parameters:
sqlNode- sqlNode- Returns:
- SQL representation
- Throws:
com.exasol.adapter.AdapterException- if an error occurs while generating the SQL representation.
-
getDialect
protected SqlDialect getDialect()
Get the SQL dialect.- Returns:
- SQL dialect.
-
checkDialectAliases
protected void checkDialectAliases()
Check if dialect provided invalid aliases, which would never be applied.
-
isDirectlyInSelectList
protected boolean isDirectlyInSelectList(com.exasol.adapter.sql.SqlColumn column)
Check if a column is directly in the select list.- Parameters:
column- column name- Returns:
trueif the column is directly in the select list of a query
-
visit
public String visit(com.exasol.adapter.sql.SqlStatementSelect select) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlSelectList selectList) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
representAnyColumnInSelectList
protected String representAnyColumnInSelectList()
Represent "any column" in theSELECTlist.A typical example are queries where the only thing that matters if what you searched for exists or not. Different databases have different preferred ways of expressing this kind of result.
- Returns:
- always
"true"
-
createExplicitColumnsSelectList
protected String createExplicitColumnsSelectList(com.exasol.adapter.sql.SqlSelectList selectList) throws com.exasol.adapter.AdapterException
Create a list of explicitly specified columns (where columns can also be expressions) for aSELECTlist.- Parameters:
selectList- list of columns (or expressions) in theSELECTpart- Returns:
- string representing the
SELECTlist - Throws:
com.exasol.adapter.AdapterException- in case the expressions in the list cannot be rendered to SQL
-
visit
public String visit(com.exasol.adapter.sql.SqlColumn column) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlTable table)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlJoin join) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlFunctionAggregateListagg sqlFunctionAggregateListagg) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlGroupBy groupBy) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlFunctionAggregate function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlFunctionAggregateGroupConcat function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlFunctionScalar function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlFunctionScalarCase function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlFunctionScalarCast function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlFunctionScalarExtract function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlFunctionScalarJsonValue function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlLimit limit)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralBool literal)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralDate literal)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralDouble literal)
Get a value of the double converted to an E-notation format.For example: 1.234 becomes 1.234E0
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralExactnumeric literal)
Formats a value of the exactnumeric as a plain string without E notation.For example: 1E-35 becomes 0.00000000000000000000000000000000001
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralNull literal)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralString literal)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralTimestamp literal)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralTimestampUtc literal)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlLiteralInterval literal)
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>
-
visit
public String visit(com.exasol.adapter.sql.SqlOrderBy orderBy) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateAnd predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateBetween predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateEqual predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateInConstList predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateIsJson function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateIsNotJson function) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateLess predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateLessEqual predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateLike predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateLikeRegexp predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateNot predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateNotEqual predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateOr predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateIsNull predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
visit
public String visit(com.exasol.adapter.sql.SqlPredicateIsNotNull predicate) throws com.exasol.adapter.AdapterException
- Specified by:
visitin interfacecom.exasol.adapter.sql.SqlNodeVisitor<String>- Throws:
com.exasol.adapter.AdapterException
-
getTypeNameFromColumn
protected String getTypeNameFromColumn(com.exasol.adapter.sql.SqlColumn column) throws com.exasol.adapter.AdapterException
Get the type name for a column- Parameters:
column- column- Returns:
- type name
- Throws:
com.exasol.adapter.AdapterException- if something goes wrong
-
-