Package org.fastnate.generator.dialect
Class GeneratorDialect
java.lang.Object
org.fastnate.generator.dialect.GeneratorDialect
- Direct Known Subclasses:
H2Dialect,MsSqlDialect,MySqlDialect,OracleDialect,PostgresDialect
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddQuotedCharacter(StringBuilder result, char c) Adds a quoted character to the result string buffer.voidadjustNextIdentityValue(StatementsWriter writer, GeneratorTable table, GeneratorColumn column, long nextValue) Adjusts the next value of the given identity column to ensure that it is bigger than the last generated value.voidadjustNextSequenceValue(StatementsWriter writer, String sequenceName, long currentSequenceValue, long nextSequenceValue, int incrementSize) Adjusts the given sequence to ensure that the next value is exactly the given value.buildCurrentSequenceValue(String sequence, int incrementSize, boolean firstCall) Builds the SQL expression that is used for referencing the current value of the given sequence.buildNextSequenceValue(String sequence, int incrementSize) Builds the SQL expression resp. statement that is used for creating the next value of the given sequence.convertBooleanValue(boolean value) Converts a boolean value to an SQL expression for the current database type.convertNumberValue(Number value) Converts a numeric value to an SQL expression for the current database type.protected StringconvertTemporalValue(Date sqlDate) Builds the SQL expression for a date / time / timestamp.convertTemporalValue(Date value, jakarta.persistence.TemporalType type) Converts a date to an appropriate SQL expression.convertToDatabaseDate(Date value, jakarta.persistence.TemporalType type) protected StringcreateAddDateExpression(String referenceDate, long value, String unit) Creates an SQL expression to add a value to a date.createBlobExpression(byte[] blob) Converts the given byte array to an SQL expression for the current database.protected StringcreateHexBlobExpression(String prefix, byte[] blob, String suffix) Creates the hex presentation of the given blob.private static voidfinishPart(StringBuilder result, String value, int start, int end, boolean isOpen, boolean close, String concatOperator) The operator used to concat two Strings.The SQL expression to use in an empty insert statement.char[]Returns the string to use when no table is required, e.g. for "SELECT 1, 2 FROM DUAL" this would return "FROM DUAL".booleanIndicates that the empty string is equal tonullin this database.booleanIndicates that tables are used in place of sequences, ifGenerationType.SEQUENCEis defined for aGeneratedValue.booleanIndicates that the database usually faster when all statements are executed within an transaction.booleanIndicates that identity columns are supported by the database.booleanIndicates that a sequence may be updated in the insert statement.booleanIndicates that this dialect supports theschemanaming.booleanIndicates that this dialect may select from the same table in an insert statement.booleanIndicates that references to sequences inWHEREexpressions are supported.booleanIndicates that sequences are supported by the database.booleanIndicates if this dialect supports writing absolute values to anGenerationType.IDENTITYcolumn.quoteIdentifier(String name) Quotes an object name for the current database dialect.quoteString(String value) Quotes the given string.voidtruncateTable(StatementsWriter writer, GeneratorTable table) Adds a "truncate table" statement to the given writer.
-
Field Details
-
NOW
Deprecated.UseRelativeDate.NOWinstead -
letter
private final char[] letter
-
-
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
Adds a quoted character to the result string buffer.- Parameters:
result- the current result bufferc- 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 statementtable- the table of the columncolumn- the (auto increment) identity columnnextValue- 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 statementssequenceName- the fully qualified name of the sequencecurrentSequenceValue- the current value of the sequencenextSequenceValue- the next value of the sequenceincrementSize- the increment size of the sequence- Throws:
IOException- if the writer throws one
-
buildCurrentSequenceValue
Builds the SQL expression that is used for referencing the current value of the given sequence.- Parameters:
sequence- the fully qualified name of the sequenceincrementSize- the expected incrementSize, as given in the schema - used by some dialects to ensure that exactly that inrement is usedfirstCall- 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
Builds the SQL expression resp. statement that is used for creating the next value of the given sequence. Depending onisNextSequenceValueInInsertSupported()this will return an expression or a statement.- Parameters:
sequence- the fully qualified name of the sequenceincrementSize- 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
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
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
Builds the SQL expression for a date / time / timestamp.- Parameters:
sqlDate- the date, already converted to one fromjavax.sql.*- Returns:
- the SQL expression representing the value.
-
convertTemporalValue
Converts a date to an appropriate SQL expression.- Parameters:
value- the timestamp valuetype- the type- Returns:
- SQL expression representing the value.
-
convertToDatabaseDate
- Parameters:
value- the Java-Date objecttype- the type of the conversion- Returns:
- the corresponding java.sql specific date object
-
createAddDateExpression
Creates an SQL expression to add a value to a date.- Parameters:
referenceDate- the expression for the reference datevalue- the value to add to the dateunit- the unit of the value- Returns:
- the SQL expression that adds the value
-
createBlobExpression
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
Creates the hex presentation of the given blob.- Parameters:
prefix- the prefix to add to the hexblob- the binary blob to convertsuffix- the suffix to add to the hex- Returns:
- prefix + hex(blob) + suffix
-
getConcatOperator
The operator used to concat two Strings.- Returns:
- the operator to use for "string1 OPERATOR string2" (including any necessary whitespace) or
nullif this dialect does not have such an operator
-
getEmptyValuesExpression
The SQL expression to use in an empty insert statement.- Returns:
- the SQL expression if an insert statement contains no values
-
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 tonullin this database. Important forNotNullconstraints, where an empty string would result in the same constraint violation.- Returns:
trueif this database assumes that an empty string is the same asnull
-
isEmulatingSequences
public boolean isEmulatingSequences()Indicates that tables are used in place of sequences, ifGenerationType.SEQUENCEis defined for aGeneratedValue.- Returns:
trueif sequences are emulated with tables,falseif 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:
trueif an transaction is faster than auto commit
-
isIdentitySupported
public boolean isIdentitySupported()Indicates that identity columns are supported by the database.- Returns:
trueif the database supports identities
-
isNextSequenceValueInInsertSupported
public boolean isNextSequenceValueInInsertSupported()Indicates that a sequence may be updated in the insert statement.- Returns:
trueto indicate thatbuildNextSequenceValue(String,int)will return an expression that may be used in an INSERT statement,falseto indicate thatbuildNextSequenceValue(String,int)returns a full statement
-
isSchemaSupported
public boolean isSchemaSupported()Indicates that this dialect supports theschemanaming.- Returns:
trueif 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:
trueif "INSERT INTO MY_TABLE (a) VALUES (SELECT max(a) FROM MY_TABLE)" is supported
-
isSequenceInWhereSupported
public boolean isSequenceInWhereSupported()Indicates that references to sequences inWHEREexpressions are supported.- Returns:
trueif this database supports sequences inWHEREexpressions
-
isSequenceSupported
public boolean isSequenceSupported()Indicates that sequences are supported by the database.- Returns:
trueif the database supports sequences
-
isSettingIdentityAllowed
public boolean isSettingIdentityAllowed()Indicates if this dialect supports writing absolute values to anGenerationType.IDENTITYcolumn.- Returns:
trueif we can write fix values for identity columns
-
quoteIdentifier
Quotes an object name for the current database dialect.- Parameters:
name- the plain object name- Returns:
- the quoted object name
-
quoteString
Quotes the given string.- Parameters:
value- the value to quote- Returns:
- the quoted string
-
truncateTable
Adds a "truncate table" statement to the given writer.- Parameters:
writer- the target of the statementtable- the table to truncate- Throws:
IOException- if the writer throws one
-
getLetter
public char[] getLetter()
-
RelativeDate.NOWinstead