Class SqlDialect

    • Field Detail

      • LOGGER

        protected static final org.slf4j.Logger LOGGER
      • BUILT_IN_OPERATORS_LIST

        protected static final java.util.Set<SqlOperator> BUILT_IN_OPERATORS_LIST
        Built-in scalar functions and operators common for every dialect.
      • identifierQuoteString

        private final java.lang.String identifierQuoteString
      • identifierEndQuoteString

        private final java.lang.String identifierEndQuoteString
      • identifierEscapedQuote

        private final java.lang.String identifierEscapedQuote
      • HEXITS

        private static final char[] HEXITS
    • Constructor Detail

      • SqlDialect

        @Deprecated
        public SqlDialect​(SqlDialect.DatabaseProduct databaseProduct,
                          java.lang.String databaseProductName,
                          java.lang.String identifierQuoteString)
        Deprecated.
      • SqlDialect

        @Deprecated
        public SqlDialect​(SqlDialect.DatabaseProduct databaseProduct,
                          java.lang.String databaseProductName,
                          java.lang.String identifierQuoteString,
                          NullCollation nullCollation)
        Deprecated.
        Creates a SqlDialect.
        Parameters:
        databaseProduct - Database product; may be UNKNOWN, never null
        databaseProductName - Database product name from JDBC driver
        identifierQuoteString - String to quote identifiers. Null if quoting is not supported. If "[", close quote is deemed to be "]".
        nullCollation - Whether NULL values appear first or last
      • SqlDialect

        public SqlDialect​(SqlDialect.Context context)
        Creates a SqlDialect.
        Parameters:
        context - All the information necessary to create a dialect
    • Method Detail

      • create

        @Deprecated
        public static SqlDialect create​(java.sql.DatabaseMetaData databaseMetaData)
        Deprecated.
        Replaced by SqlDialectFactory
        Creates a SqlDialect from a DatabaseMetaData.

        Does not maintain a reference to the DatabaseMetaData -- or, more importantly, to its Connection -- after this call has returned.

        Parameters:
        databaseMetaData - used to determine which dialect of SQL to generate
      • getProduct

        @Deprecated
        public static SqlDialect.DatabaseProduct getProduct​(java.lang.String productName,
                                                            java.lang.String productVersion)
        Deprecated.
        Converts a product name and version (per the JDBC driver) into a product enumeration.
        Parameters:
        productName - Product name
        productVersion - Product version
        Returns:
        database product
      • getTypeSystem

        public RelDataTypeSystem getTypeSystem()
        Returns the type system implementation for this dialect.
      • quoteIdentifier

        public java.lang.String quoteIdentifier​(java.lang.String val)
        Encloses an identifier in quotation marks appropriate for the current SQL dialect.

        For example, quoteIdentifier("emp") yields a string containing "emp" in Oracle, and a string containing [emp] in Access.

        Parameters:
        val - Identifier to quote
        Returns:
        Quoted identifier
      • quoteIdentifier

        public java.lang.StringBuilder quoteIdentifier​(java.lang.StringBuilder buf,
                                                       java.lang.String val)
        Encloses an identifier in quotation marks appropriate for the current SQL dialect, writing the result to a StringBuilder.

        For example, quoteIdentifier("emp") yields a string containing "emp" in Oracle, and a string containing [emp] in Access.

        Parameters:
        buf - Buffer
        val - Identifier to quote
        Returns:
        The buffer
      • quoteIdentifier

        public java.lang.StringBuilder quoteIdentifier​(java.lang.StringBuilder buf,
                                                       java.util.List<java.lang.String> identifiers)
        Quotes a multi-part identifier.
        Parameters:
        buf - Buffer
        identifiers - List of parts of the identifier to quote
        Returns:
        The buffer
      • identifierNeedsToBeQuoted

        public boolean identifierNeedsToBeQuoted​(java.lang.String val)
        Returns whether a given identifier needs to be quoted.
      • quoteStringLiteral

        public java.lang.String quoteStringLiteral​(java.lang.String val)
        Converts a string into a string literal. For example, can't run becomes 'can''t run'.
      • unparseCall

        public void unparseCall​(SqlWriter writer,
                                SqlCall call,
                                int leftPrec,
                                int rightPrec)
      • unparseSqlDatetimeArithmetic

        public void unparseSqlDatetimeArithmetic​(SqlWriter writer,
                                                 SqlCall call,
                                                 SqlKind sqlKind,
                                                 int leftPrec,
                                                 int rightPrec)
      • unparseSqlIntervalQualifier

        public void unparseSqlIntervalQualifier​(SqlWriter writer,
                                                SqlIntervalQualifier qualifier,
                                                RelDataTypeSystem typeSystem)
        Converts an interval qualifier to a SQL string. The default implementation returns strings such as INTERVAL '1 2:3:4' DAY(4) TO SECOND(4).
      • unparseSqlIntervalLiteral

        public void unparseSqlIntervalLiteral​(SqlWriter writer,
                                              SqlIntervalLiteral literal,
                                              int leftPrec,
                                              int rightPrec)
        Converts an interval literal to a SQL string. The default implementation returns strings such as INTERVAL '1 2:3:4' DAY(4) TO SECOND(4).
      • containsNonAscii

        private static boolean containsNonAscii​(java.lang.String s)
        Returns whether the string contains any characters outside the comfortable 7-bit ASCII range (32 through 127).
        Parameters:
        s - String
        Returns:
        Whether string contains any non-7-bit-ASCII characters
      • quoteStringLiteralUnicode

        public void quoteStringLiteralUnicode​(java.lang.StringBuilder buf,
                                              java.lang.String val)
        Converts a string into a unicode string literal. For example, can't{tab}run\ becomes u'can''t\0009run\\'.
      • unquoteStringLiteral

        public java.lang.String unquoteStringLiteral​(java.lang.String val)
        Converts a string literal back into a string. For example, 'can''t run' becomes can't run.
      • allowsAs

        protected boolean allowsAs()
      • requiresAliasForFromItems

        protected boolean requiresAliasForFromItems()
      • hasImplicitTableAlias

        public boolean hasImplicitTableAlias()
        Returns whether a qualified table in the FROM clause has an implicit alias which consists of just the table name.

        For example, in SqlDialect.DatabaseProduct.ORACLE

        SELECT * FROM sales.emp

        is equivalent to

        SELECT * FROM sales.emp AS emp

        and therefore

        SELECT emp.empno FROM sales.emp

        is valid. But SqlDialect.DatabaseProduct.DB2 does not have an implicit alias, so the previous query it not valid; you need to write

        SELECT sales.emp.empno FROM sales.emp

        Returns true for all databases except DB2.

      • quoteTimestampLiteral

        public java.lang.String quoteTimestampLiteral​(java.sql.Timestamp timestamp)
        Converts a timestamp to a SQL timestamp literal, e.g. TIMESTAMP '2009-12-17 12:34:56'.

        Timestamp values do not have a time zone. We therefore interpret them as the number of milliseconds after the UTC epoch, and the formatted value is that time in UTC.

        In particular,

        quoteTimestampLiteral(new Timestamp(0));

        returns TIMESTAMP '1970-01-01 00:00:00', regardless of the JVM's time zone.

        Parameters:
        timestamp - Timestamp
        Returns:
        SQL timestamp literal
      • getDatabaseProduct

        @Deprecated
        public SqlDialect.DatabaseProduct getDatabaseProduct()
        Deprecated.
        To be removed without replacement
        Returns the database this dialect belongs to, SqlDialect.DatabaseProduct.UNKNOWN if not known, never null.

        Please be judicious in how you use this method. If you wish to determine whether a dialect has a particular capability or behavior, it is usually better to add a method to SqlDialect and override that method in particular sub-classes of SqlDialect.

        Returns:
        Database product
      • supportsCharSet

        public boolean supportsCharSet()
        Returns whether the dialect supports character set names as part of a data type, for instance VARCHAR(30) CHARACTER SET `ISO-8859-1`.
      • supportsAggregateFunction

        public boolean supportsAggregateFunction​(SqlKind kind)
      • supportsWindowFunctions

        public boolean supportsWindowFunctions()
        Returns whether this dialect supports window functions (OVER clause).
      • supportsFunction

        public boolean supportsFunction​(SqlOperator operator,
                                        RelDataType type,
                                        java.util.List<RelDataType> paramTypes)
        Returns whether this dialect supports a given function or operator. It only applies to built-in scalar functions and operators, since user-defined functions and procedures should be read by JdbcSchema.
      • rewriteSingleValueExpr

        public SqlNode rewriteSingleValueExpr​(SqlNode aggCall)
        Rewrite SINGLE_VALUE into expression based on database variants E.g. HSQLDB, MYSQL, ORACLE, etc
      • emulateNullDirection

        public SqlNode emulateNullDirection​(SqlNode node,
                                            boolean nullsFirst,
                                            boolean desc)
        Returns the SqlNode for emulating the null direction for the given field or null if no emulation needs to be done.
        Parameters:
        node - The SqlNode representing the expression
        nullsFirst - Whether nulls should come first
        desc - Whether the sort direction is RelFieldCollation.Direction.DESCENDING or RelFieldCollation.Direction.STRICTLY_DESCENDING
        Returns:
        A SqlNode for null direction emulation or null if not required
      • emulateNullDirectionWithIsNull

        protected SqlNode emulateNullDirectionWithIsNull​(SqlNode node,
                                                         boolean nullsFirst,
                                                         boolean desc)
      • unparseFetchUsingAnsi

        protected final void unparseFetchUsingAnsi​(SqlWriter writer,
                                                   SqlNode offset,
                                                   SqlNode fetch)
        Unparses offset/fetch using ANSI standard "OFFSET offset ROWS FETCH NEXT fetch ROWS ONLY" syntax.
      • unparseFetchUsingLimit

        protected final void unparseFetchUsingLimit​(SqlWriter writer,
                                                    SqlNode offset,
                                                    SqlNode fetch)
        Unparses offset/fetch using "LIMIT fetch OFFSET offset" syntax.
      • supportsNestedAggregations

        public boolean supportsNestedAggregations()
        Returns whether the dialect supports nested aggregations, for instance SELECT SUM(SUM(1)) .
      • getNullCollation

        public NullCollation getNullCollation()
        Returns how NULL values are sorted if an ORDER BY item does not contain NULLS ASCENDING or NULLS DESCENDING.
      • supportsAliasedValues

        public boolean supportsAliasedValues()
        Returns whether the dialect supports VALUES in a sub-query with and an "AS t(column, ...)" values to define column names.

        Currently, only Oracle does not. For this, we generate "SELECT v0 AS c0, v1 AS c1 ... UNION ALL ...". We may need to refactor this method when we support VALUES for other dialects.