Class GeneratorDialect

java.lang.Object
org.fastnate.generator.dialect.GeneratorDialect
Direct Known Subclasses:
H2Dialect, MsSqlDialect, MySqlDialect, OracleDialect, PostgresDialect

public abstract class GeneratorDialect extends Object
Handles database specific conversions. The conversions are limited, for some of the known limitations, see the database specific implementations.
Author:
Tobias Liefke
See Also:
  • Field Details

  • Constructor Details

    • GeneratorDialect

      public GeneratorDialect()
  • Method Details

    • finishPart

      private static void finishPart(StringBuilder result, String value, int start, int end, boolean isOpen, boolean close, String concatOperator)
    • addQuotedCharacter

      protected void addQuotedCharacter(StringBuilder result, char c)
      Adds a quoted character to the result string buffer.
      Parameters:
      result - the current result buffer
      c - the character to quote
    • adjustNextIdentityValue

      public void adjustNextIdentityValue(StatementsWriter writer, GeneratorTable table, GeneratorColumn column, long nextValue) throws IOException
      Adjusts the next value of the given identity column to ensure that it is bigger than the last generated value.
      Parameters:
      writer - the target of any generated statement
      table - the table of the column
      column - the (auto increment) identity column
      nextValue - the next value of the identity column
      Throws:
      IOException - if the writer throws one
    • adjustNextSequenceValue

      public void adjustNextSequenceValue(StatementsWriter writer, String sequenceName, long currentSequenceValue, long nextSequenceValue, int incrementSize) throws IOException
      Adjusts the given sequence to ensure that the next value is exactly the given value.
      Parameters:
      writer - the target of the generated statements
      sequenceName - the fully qualified name of the sequence
      currentSequenceValue - the current value of the sequence
      nextSequenceValue - the next value of the sequence
      incrementSize - the increment size of the sequence
      Throws:
      IOException - if the writer throws one
    • buildCurrentSequenceValue

      public String buildCurrentSequenceValue(String sequence, int incrementSize, boolean firstCall)
      Builds the SQL expression that is used for referencing the current value of the given sequence.
      Parameters:
      sequence - the fully qualified name of the sequence
      incrementSize - the expected incrementSize, as given in the schema - used by some dialects to ensure that exactly that inrement is used
      firstCall - indicates that the sequence was not updated before in this session - we may need to use a different approach to get the current value
      Returns:
      the SQL expression to use in statement
    • buildNextSequenceValue

      public String buildNextSequenceValue(String sequence, int incrementSize)
      Builds the SQL expression resp. statement that is used for creating the next value of the given sequence. Depending on isNextSequenceValueInInsertSupported() this will return an expression or a statement.
      Parameters:
      sequence - the fully qualified name of the sequence
      incrementSize - the expected incrementSize, as given in the schema - used by some dialects to ensure that exactly that inrement is used
      Returns:
      the SQL to use in resp. before the insert / update statement
    • convertBooleanValue

      public String convertBooleanValue(boolean value)
      Converts a boolean value to an SQL expression for the current database type.
      Parameters:
      value - the value to convert
      Returns:
      the SQL representation of the value
    • convertNumberValue

      public String convertNumberValue(Number value)
      Converts a numeric value to an SQL expression for the current database type.
      Parameters:
      value - the numeric value
      Returns:
      the SQL expression of that value
    • convertTemporalValue

      protected String convertTemporalValue(Date sqlDate)
      Builds the SQL expression for a date / time / timestamp.
      Parameters:
      sqlDate - the date, already converted to one from javax.sql.*
      Returns:
      the SQL expression representing the value.
    • convertTemporalValue

      public String convertTemporalValue(Date value, jakarta.persistence.TemporalType type)
      Converts a date to an appropriate SQL expression.
      Parameters:
      value - the timestamp value
      type - the type
      Returns:
      SQL expression representing the value.
    • convertToDatabaseDate

      public Date convertToDatabaseDate(Date value, jakarta.persistence.TemporalType type)
      Converts any Date value to a java.sql specific date object.
      Parameters:
      value - the Java-Date object
      type - the type of the conversion
      Returns:
      the corresponding java.sql specific date object
    • createAddDateExpression

      protected String createAddDateExpression(String referenceDate, long value, String unit)
      Creates an SQL expression to add a value to a date.
      Parameters:
      referenceDate - the expression for the reference date
      value - the value to add to the date
      unit - the unit of the value
      Returns:
      the SQL expression that adds the value
    • createBlobExpression

      public String createBlobExpression(byte[] blob)
      Converts the given byte array to an SQL expression for the current database.
      Parameters:
      blob - the bytes to convert
      Returns:
      the expression for the bytes
    • createHexBlobExpression

      protected String createHexBlobExpression(String prefix, byte[] blob, String suffix)
      Creates the hex presentation of the given blob.
      Parameters:
      prefix - the prefix to add to the hex
      blob - the binary blob to convert
      suffix - the suffix to add to the hex
      Returns:
      prefix + hex(blob) + suffix
    • getConcatOperator

      public String getConcatOperator()
      The operator used to concat two Strings.
      Returns:
      the operator to use for "string1 OPERATOR string2" (including any necessary whitespace) or null if this dialect does not have such an operator
    • getEmptyValuesExpression

      public Object getEmptyValuesExpression()
      The SQL expression to use in an empty insert statement.
      Returns:
      the SQL expression if an insert statement contains no values
    • getOptionalTable

      public String getOptionalTable()
      Returns the string to use when no table is required, e.g. for "SELECT 1, 2 FROM DUAL" this would return "FROM DUAL".
      Returns:
      the SQL to use in "SELECT x [OPTIONALTABLE] WHERE ..."
    • isEmptyStringEqualToNull

      public boolean isEmptyStringEqualToNull()
      Indicates that the empty string is equal to null in this database. Important for NotNull constraints, where an empty string would result in the same constraint violation.
      Returns:
      true if this database assumes that an empty string is the same as null
    • isEmulatingSequences

      public boolean isEmulatingSequences()
      Indicates that tables are used in place of sequences, if GenerationType.SEQUENCE is defined for a GeneratedValue.
      Returns:
      true if sequences are emulated with tables, false if sequences are supported
    • isFastInTransaction

      public boolean isFastInTransaction()
      Indicates that the database usually faster when all statements are executed within an transaction. For example Oracle is approx. 25% faster but H2 is 200% slower compared to auto commit. Only relevant if executed against a running database.
      Returns:
      true if an transaction is faster than auto commit
    • isIdentitySupported

      public boolean isIdentitySupported()
      Indicates that identity columns are supported by the database.
      Returns:
      true if the database supports identities
    • isNextSequenceValueInInsertSupported

      public boolean isNextSequenceValueInInsertSupported()
      Indicates that a sequence may be updated in the insert statement.
      Returns:
      true to indicate that buildNextSequenceValue(String,int) will return an expression that may be used in an INSERT statement, false to indicate that buildNextSequenceValue(String,int) returns a full statement
    • isSchemaSupported

      public boolean isSchemaSupported()
      Indicates that this dialect supports the schema naming.
      Returns:
      true if we support the name of schema when referencing a table
    • isSelectFromSameTableInInsertSupported

      public boolean isSelectFromSameTableInInsertSupported()
      Indicates that this dialect may select from the same table in an insert statement.
      Returns:
      true if "INSERT INTO MY_TABLE (a) VALUES (SELECT max(a) FROM MY_TABLE)" is supported
    • isSequenceInWhereSupported

      public boolean isSequenceInWhereSupported()
      Indicates that references to sequences in WHERE expressions are supported.
      Returns:
      true if this database supports sequences in WHERE expressions
    • isSequenceSupported

      public boolean isSequenceSupported()
      Indicates that sequences are supported by the database.
      Returns:
      true if the database supports sequences
    • isSettingIdentityAllowed

      public boolean isSettingIdentityAllowed()
      Indicates if this dialect supports writing absolute values to an GenerationType.IDENTITY column.
      Returns:
      true if we can write fix values for identity columns
    • quoteIdentifier

      public String quoteIdentifier(String name)
      Quotes an object name for the current database dialect.
      Parameters:
      name - the plain object name
      Returns:
      the quoted object name
    • quoteString

      public String quoteString(String value)
      Quotes the given string.
      Parameters:
      value - the value to quote
      Returns:
      the quoted string
    • truncateTable

      public void truncateTable(StatementsWriter writer, GeneratorTable table) throws IOException
      Adds a "truncate table" statement to the given writer.
      Parameters:
      writer - the target of the statement
      table - the table to truncate
      Throws:
      IOException - if the writer throws one
    • getLetter

      public char[] getLetter()