java.lang.Object
org.tentackle.sql.backends.AbstractBackend
org.tentackle.sql.backends.AbstractSql92Backend
org.tentackle.sql.backends.Postgres
- All Implemented Interfaces:
Backend
Backend for Postgres.
- Author:
- harald
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringpostgres' default schema.static final String[]array of additional reserved words.static final StringLIMIT string.static final StringLIMIT ? string.static final StringOFFSET string.static final StringOFFSET ? string.Fields inherited from class org.tentackle.sql.backends.AbstractSql92Backend
RESERVED_WORDS_SQL92, SQL_COALESCE, SQL_FOR_UPDATEFields inherited from class org.tentackle.sql.backends.AbstractBackend
TYPE_BIGINT, TYPE_BIT, TYPE_BLOB, TYPE_BOOL, TYPE_BOOLEAN, TYPE_BYTE, TYPE_BYTEA, TYPE_CHAR_1, TYPE_CLOB, TYPE_DATE, TYPE_DATETIME, TYPE_DATETIME_YEAR_TO_SECOND, TYPE_DECIMAL, TYPE_DECIMAL_19, TYPE_DOUBLE, TYPE_DOUBLE_PRECISION, TYPE_FLOAT, TYPE_FLOAT4, TYPE_FLOAT8, TYPE_INT, TYPE_INT2, TYPE_INT4, TYPE_INT8, TYPE_INTEGER, TYPE_NCHAR_1, TYPE_NUMBER, TYPE_NUMBER_1, TYPE_NUMBER_10, TYPE_NUMBER_19, TYPE_NUMBER_5, TYPE_NVARCHAR, TYPE_NVARCHAR_MAX, TYPE_REAL, TYPE_SMALLFLOAT, TYPE_SMALLINT, TYPE_TEXT, TYPE_TIME, TYPE_TIMESTAMP, TYPE_TINYINT, TYPE_UUID, TYPE_VARBINARY_MAX, TYPE_VARCHARFields inherited from interface org.tentackle.sql.Backend
SQL_ALLSTAR, SQL_ALTER_COLUMN, SQL_ALTER_INDEX, SQL_ALTER_TABLE, SQL_AND, SQL_ANDNOT, SQL_ARRAY_ALL, SQL_ARRAY_ALL_PAR, SQL_ARRAY_ANY, SQL_ARRAY_ANY_PAR, SQL_ARRAY_IN, SQL_ARRAY_IN_PAR, SQL_ARRAY_NOT_IN, SQL_ARRAY_NOT_IN_PAR, SQL_ARRAY_PAR, SQL_COMMA, SQL_COMMA_PAR, SQL_DELETE, SQL_DOT_STAR, SQL_EQUAL, SQL_EQUAL_PAR, SQL_EQUAL_PAR_COMMA, SQL_EQUAL_ZERO, SQL_EXISTS, SQL_FROM, SQL_GREATER, SQL_GREATER_PAR, SQL_GREATEROREQUAL, SQL_GREATEROREQUAL_PAR, SQL_GROUPBY, SQL_IF_EXISTS, SQL_INSERT_INTO, SQL_INSERT_VALUES, SQL_ISNOTNULL, SQL_ISNULL, SQL_LEFT_PARENTHESIS, SQL_LESS, SQL_LESS_PAR, SQL_LESSOREQUAL, SQL_LESSOREQUAL_PAR, SQL_LIKE, SQL_LIKE_PAR, SQL_MAX, SQL_MIN, SQL_NOT, SQL_NOTEQUAL, SQL_NOTEQUAL_PAR, SQL_NOTEQUAL_ZERO, SQL_NOTLIKE, SQL_NOTLIKE_PAR, SQL_OR, SQL_ORDERBY, SQL_ORNOT, SQL_PAR, SQL_PAR_COMMA, SQL_PLUS_ONE, SQL_RIGHT_PARENTHESIS, SQL_SELECT, SQL_SELECT_ALL_FROM, SQL_SET, SQL_SORTASC, SQL_SORTDESC, SQL_UPDATE, SQL_WHERE, SQL_WHEREALL, SQL_WHEREAND, SQL_WHERENOTHING, SQL_WHEREOR -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbuildSelectSql(StringBuilder sqlBuilder, boolean writeLock, int limit, int offset) Creates a select statement string from an inner sql string.createColumnMetaData(TableMetaData tableMetaData) Creates a column meta data instance.createIndexColumnMetaData(IndexMetaData indexMetaData) Creates an index column meta data instance.createIndexMetaData(TableMetaData tableMetaData) Creates an index meta data instance.createScriptRunner(Connection connection) Creates a script runner instance.protected StringextractWhereClause(String sql, int whereOffset) Extracts the where clause from a given sql.getBackendId(Connection connection) Determines the unique id or name used by the backend for a given connection.Gets the default schema name.
Some backends store the objects in a default schema, if no explicit schema is given.intgetDefaultSize(SqlType sqlType) Gets the default size if no size given in model.Gets the JDBC driver class name.intgetMaxSize(SqlType sqlType) Returns the maximum size for a given SQL-type.getMigrationStrategy(ColumnMetaData column, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue) Determines the best migration strategy.getName()Gets the name of the backend.Gets the list of reserved words for this backend.booleanReturns whether backend requires an alias for a sub-select.booleanisArrayOperatorSupported(String operator) Returns whether the backend supports given array operator.booleanReturns whether the backend support CLOB types.
If not, it will be emulated as VARCHAR with the maximum possible size.protected booleanAdds "IF EXISTS" to drop clauses, if supported by the backend.
Makes migration a little more robust in case the same SQL scripts are applied to different databases than those used to generate the migration scripts for.booleanReturns whether backend supports filtered indexes.booleanReturns whether function based indexes are supported.booleanisMatchingUrl(String url) Checks whether the backend belongs to the given jdbc url.
If multiple backend implementations are provided for the same database type, only one backend should match the URL.booleanReturns whether the backend supports releasing savepoints explicitly.booleanisReservedSchemaName(String name) Checks whether this is a reserved schema name for this backend.booleanTrue if backend provides locking-free sequences.
Sequences must not take part in transactions, i.e.booleanDetermines whether exception is transient and retrying the transaction may succeed.
Typical transient exceptions are thrown when a deadlock is detected or when serialized transaction isolation is used.booleanChecks for the (postgres) bug that fetch-size is ignored if not within a new transaction.booleanReturns whether the backend supports theUUIDnatively via JDBC setObject/getObject.SqlType[]jdbcTypeToSqlType(int jdbcType, int size, int scale) Converts the JDBC data type integer to a tentackle backend SqlTypes.intsetLeadingSelectParameters(BackendPreparedStatement stmt, int limit, int offset) Sets optional parameters before applying the original parameters.intsetTrailingSelectParameters(BackendPreparedStatement stmt, int index, int limit, int offset) Sets optional parameters after applying the original parameters.sqlAlterColumnNullConstraint(String tableName, String columnName, boolean nullable) Generates sql code to change the null-constraint of a column.sqlAlterColumnType(String tableName, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue) Generates sql code to change the datatype of a column.sqlCreateColumnComment(String tableName, String columnName, String comment) Generates SQL code to create the comment for an attribute.sqlCreateSequence(String name, Long start, Long increment) Creates the SQL string to create a sequence.sqlCreateSequenceComment(String name, String comment) Creates the SQL string to comment a sequence.sqlCreateTableComment(String tableName, String comment) Generates SQL code to create the comment for a table.sqlNextFromSequence(String name) Creates the SQL string to retrieve the next id from a sequence.sqlTypeToString(SqlType sqlType, int size) Converts the java SQL-type to the database type name.toInternalType(String sqlTypeName) Converts a backend specific type name to an internal name used by driver specific methods.Methods inherited from class org.tentackle.sql.backends.AbstractSql92Backend
getCoalesceKeyword, isLeadingSelectMissing, sqlCreateForeignKey, sqlDropForeignKeyMethods inherited from class org.tentackle.sql.backends.AbstractBackend
assertValidName, buildSelectSql, columnTypeNullDefaultToString, columnTypeToString, createConnection, createPassword, createTableMetaData, getBlockCommentBegin, getBlockCommentBegins, getBlockCommentEnd, getBlockCommentEnds, getEmptyString, getMaxScale, getMetaData, getModelMetaData, getSingleLineComment, getSingleLineComments, getStatementSeparator, getStatementSeparators, getTableHeaders, getTableMetaData, isCommunicationLinkException, isConstraintException, isDatabaseInMemory, isDefaultEqual, isDeprecated, isDropIfExistsEnabled, isExceptionErrorCodeMatching, isExceptionStateMatching, isExceptionStateStartingWith, isExpressionReferringToTableBeingUpdatedSupported, isExtraCommitRequired, isMatchingName, isPosixEscapeSyntaxSupported, isReservedTableName, isReservedWord, isSchemaSupported, isTemporaryName, normalizeDefault, optimizeSql, setArray, setDropIfExistsEnabled, sqlAddColumn, sqlAlterColumnComment, sqlAlterColumnDefault, sqlAlterTableComment, sqlAsBeforeTableAlias, sqlComment, sqlCreateColumn, sqlCreateIndex, sqlCreateSchema, sqlCreateTableAttributeWithoutComment, sqlCreateTableClosing, sqlCreateTableIntro, sqlCreateTableIntroWithoutComment, sqlDropColumn, sqlDropIndex, sqlDropTable, sqlFunction, sqlJoin, sqlJoinSelects, sqlJoinSelects, sqlRenameAndAlterColumnType, sqlRenameColumn, sqlRenameIndex, sqlRenameTable, sqlUpdateToNotNull, toQuotedString, toString, validateVersion, valueToLiteral
-
Field Details
-
DEFAULT_SCHEMA
postgres' default schema.- See Also:
-
SQL_LIMIT
LIMIT string.- See Also:
-
SQL_OFFSET
OFFSET string.- See Also:
-
SQL_LIMIT_PAR
LIMIT ? string.- See Also:
-
SQL_OFFSET_PAR
OFFSET ? string.- See Also:
-
RESERVED_WORDS_PG
array of additional reserved words.
-
-
Constructor Details
-
Postgres
public Postgres()Creates the postgres backend.
-
-
Method Details
-
getReservedWords
Description copied from interface:BackendGets the list of reserved words for this backend.- Specified by:
getReservedWordsin interfaceBackend- Overrides:
getReservedWordsin classAbstractSql92Backend- Returns:
- the set of reserved words
-
isMatchingUrl
Description copied from interface:BackendChecks whether the backend belongs to the given jdbc url.
If multiple backend implementations are provided for the same database type, only one backend should match the URL. The others must be selected via name.- Parameters:
url- the jdbc url- Returns:
- true if matches
-
getName
Description copied from interface:BackendGets the name of the backend.- Returns:
- the name
-
getDriverClassName
Description copied from interface:BackendGets the JDBC driver class name.- Returns:
- the class name
-
getBackendId
Description copied from interface:BackendDetermines the unique id or name used by the backend for a given connection.Useful to figure out the corresponding
ManagedConnectionfrom the backend's logfiles.- Parameters:
connection- the jdbc connection- Returns:
- the backend id, null if none
-
isReleaseSavepointSupported
public boolean isReleaseSavepointSupported()Description copied from interface:BackendReturns whether the backend supports releasing savepoints explicitly. If not, the savepoints are released when the transaction finishs.- Specified by:
isReleaseSavepointSupportedin interfaceBackend- Overrides:
isReleaseSavepointSupportedin classAbstractBackend- Returns:
- true if release is supported, else false
-
isReservedSchemaName
Description copied from interface:BackendChecks whether this is a reserved schema name for this backend.- Specified by:
isReservedSchemaNamein interfaceBackend- Overrides:
isReservedSchemaNamein classAbstractBackend- Parameters:
name- the schema name- Returns:
- true if reserved by backend
-
isTransientTransactionException
Description copied from interface:BackendDetermines whether exception is transient and retrying the transaction may succeed.
Typical transient exceptions are thrown when a deadlock is detected or when serialized transaction isolation is used.Other transient errors are not covered by this method (e.g. connection timeouts, etc...).
- Specified by:
isTransientTransactionExceptionin interfaceBackend- Overrides:
isTransientTransactionExceptionin classAbstractBackend- Parameters:
ex- the exception- Returns:
- true if retrying the transaction may succeed
-
isClobSupported
public boolean isClobSupported()Description copied from interface:BackendReturns whether the backend support CLOB types.
If not, it will be emulated as VARCHAR with the maximum possible size.- Specified by:
isClobSupportedin interfaceBackend- Overrides:
isClobSupportedin classAbstractBackend- Returns:
- true if supported, false if treat as varchar
-
isUUIDSupported
public boolean isUUIDSupported()Description copied from interface:BackendReturns whether the backend supports theUUIDnatively via JDBC setObject/getObject.- Specified by:
isUUIDSupportedin interfaceBackend- Overrides:
isUUIDSupportedin classAbstractBackend- Returns:
- true if supported, false if mapped to VARCHAR(36)
-
isFilteredIndexSupported
public boolean isFilteredIndexSupported()Description copied from interface:BackendReturns whether backend supports filtered indexes.- Specified by:
isFilteredIndexSupportedin interfaceBackend- Overrides:
isFilteredIndexSupportedin classAbstractBackend- Returns:
- true if supported, false if model will raise an error if filtered index is used
-
isFunctionBasedIndexSupported
public boolean isFunctionBasedIndexSupported()Description copied from interface:BackendReturns whether function based indexes are supported.- Specified by:
isFunctionBasedIndexSupportedin interfaceBackend- Overrides:
isFunctionBasedIndexSupportedin classAbstractBackend- Returns:
- true if supported
-
isTxRequiredForFetchsize
public boolean isTxRequiredForFetchsize()Description copied from interface:BackendChecks for the (postgres) bug that fetch-size is ignored if not within a new transaction.- Specified by:
isTxRequiredForFetchsizein interfaceBackend- Overrides:
isTxRequiredForFetchsizein classAbstractBackend- Returns:
- true if start tx
-
isAliasRequiredForSubSelect
public boolean isAliasRequiredForSubSelect()Description copied from interface:BackendReturns whether backend requires an alias for a sub-select.- Specified by:
isAliasRequiredForSubSelectin interfaceBackend- Overrides:
isAliasRequiredForSubSelectin classAbstractBackend- Returns:
- true if sub-select needs an alias.
-
buildSelectSql
Description copied from interface:BackendCreates a select statement string from an inner sql string.sqlBuilderis anything as[SELECT] FROM... WHERE... ORDER BY....
It is decorated with a leadingSELECT, if missing, plus optional clauses like for the given parameters.- Specified by:
buildSelectSqlin interfaceBackend- Overrides:
buildSelectSqlin classAbstractSql92Backend- Parameters:
sqlBuilder- the sql builder, initially containing the inner sql without leadingSELECT.writeLock- true select should write locklimit- the limit value, ≤ 0 if no limit clauseoffset- the offset value, ≤ 0 if no offset clause
-
setLeadingSelectParameters
Description copied from interface:BackendSets optional parameters before applying the original parameters.- Specified by:
setLeadingSelectParametersin interfaceBackend- Overrides:
setLeadingSelectParametersin classAbstractSql92Backend- Parameters:
stmt- the prepared statementlimit- the limit value, ≤ 0 if no limit clauseoffset- the offset value, ≤ 0 if no offset clause- Returns:
- the next parameter index
-
setTrailingSelectParameters
public int setTrailingSelectParameters(BackendPreparedStatement stmt, int index, int limit, int offset) Description copied from interface:BackendSets optional parameters after applying the original parameters.- Specified by:
setTrailingSelectParametersin interfaceBackend- Overrides:
setTrailingSelectParametersin classAbstractSql92Backend- Parameters:
stmt- the prepared statementindex- the first parameter indexlimit- the limit value, ≤ 0 if no limit clauseoffset- the offset value, ≤ 0 if no offset clause- Returns:
- the next parameter index
-
createColumnMetaData
Description copied from interface:BackendCreates a column meta data instance.- Specified by:
createColumnMetaDatain interfaceBackend- Overrides:
createColumnMetaDatain classAbstractBackend- Parameters:
tableMetaData- the table metadata this column belongs to- Returns:
- the meta data
-
createIndexMetaData
Description copied from interface:BackendCreates an index meta data instance.- Specified by:
createIndexMetaDatain interfaceBackend- Overrides:
createIndexMetaDatain classAbstractBackend- Parameters:
tableMetaData- the table metadata this index belongs to- Returns:
- the meta data
-
createIndexColumnMetaData
Description copied from interface:BackendCreates an index column meta data instance.- Specified by:
createIndexColumnMetaDatain interfaceBackend- Overrides:
createIndexColumnMetaDatain classAbstractBackend- Parameters:
indexMetaData- the meta meta data this column belongs to- Returns:
- the meta data
-
getMaxSize
Description copied from interface:BackendReturns the maximum size for a given SQL-type.- Specified by:
getMaxSizein interfaceBackend- Overrides:
getMaxSizein classAbstractBackend- Parameters:
sqlType- the SQL-type- Returns:
- the maximum size, 0 if unlimited, -1 if type without size
-
getDefaultSize
Description copied from interface:BackendGets the default size if no size given in model.- Specified by:
getDefaultSizein interfaceBackend- Overrides:
getDefaultSizein classAbstractBackend- Parameters:
sqlType- the SQL-type- Returns:
- the default size, 0 if no default size
-
getDefaultSchema
Description copied from interface:BackendGets the default schema name.
Some backends store the objects in a default schema, if no explicit schema is given.- Specified by:
getDefaultSchemain interfaceBackend- Overrides:
getDefaultSchemain classAbstractBackend- Returns:
- the default schema, null if none
-
jdbcTypeToSqlType
Description copied from interface:BackendConverts the JDBC data type integer to a tentackle backend SqlTypes.There may be more than one sqltype returned!
- Specified by:
jdbcTypeToSqlTypein interfaceBackend- Overrides:
jdbcTypeToSqlTypein classAbstractBackend- Parameters:
jdbcType- the jdbc data typesize- the column sizescale- the column's scale- Returns:
- the SqlTypes, empty array if no mapping available or no exact match (not generated by TT)
- See Also:
-
sqlTypeToString
Description copied from interface:BackendConverts the java SQL-type to the database type name.- Specified by:
sqlTypeToStringin interfaceBackend- Overrides:
sqlTypeToStringin classAbstractBackend- Parameters:
sqlType- the java sql typesize- the column's size- Returns:
- the corresponding type string
-
isSequenceSupported
public boolean isSequenceSupported()Description copied from interface:BackendTrue if backend provides locking-free sequences.
Sequences must not take part in transactions, i.e. no read- or write-locks!- Specified by:
isSequenceSupportedin interfaceBackend- Overrides:
isSequenceSupportedin classAbstractSql92Backend- Returns:
- true if database supports sequences
-
sqlNextFromSequence
Description copied from interface:BackendCreates the SQL string to retrieve the next id from a sequence.- Specified by:
sqlNextFromSequencein interfaceBackend- Overrides:
sqlNextFromSequencein classAbstractSql92Backend- Parameters:
name- the name of the sequence- Returns:
- the SQL code
-
sqlCreateSequence
Description copied from interface:BackendCreates the SQL string to create a sequence.- Specified by:
sqlCreateSequencein interfaceBackend- Overrides:
sqlCreateSequencein classAbstractSql92Backend- Parameters:
name- the sequence namestart- the optional start value, defaults to 1increment- the optional increment, defaults to 1- Returns:
- the SQL code
-
sqlCreateSequenceComment
Description copied from interface:BackendCreates the SQL string to comment a sequence.- Specified by:
sqlCreateSequenceCommentin interfaceBackend- Overrides:
sqlCreateSequenceCommentin classAbstractSql92Backend- Parameters:
name- the sequence namecomment- the comment- Returns:
- the SQL code
-
sqlCreateTableComment
Description copied from interface:BackendGenerates SQL code to create the comment for a table.- Specified by:
sqlCreateTableCommentin interfaceBackend- Overrides:
sqlCreateTableCommentin classAbstractBackend- Parameters:
tableName- the table namecomment- optional comment, null if none- Returns:
- the SQL code, empty string if comment is created via
Backend.sqlCreateTableIntro(java.lang.String, java.lang.String)orBackend.sqlCreateTableClosing(java.lang.String, java.lang.String)
-
sqlCreateColumnComment
Description copied from interface:BackendGenerates SQL code to create the comment for an attribute.- Specified by:
sqlCreateColumnCommentin interfaceBackend- Overrides:
sqlCreateColumnCommentin classAbstractBackend- Parameters:
tableName- the table namecolumnName- the column namecomment- optional comment, null if none- Returns:
- the SQL code, empty string if comment is created via
Backend.sqlCreateColumn(java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object, boolean, boolean)
-
sqlAlterColumnType
public String sqlAlterColumnType(String tableName, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue) Description copied from interface:BackendGenerates sql code to change the datatype of a column.- Specified by:
sqlAlterColumnTypein interfaceBackend- Overrides:
sqlAlterColumnTypein classAbstractBackend- Parameters:
tableName- the tablenamecolumnName- the new column namecomment- optional comment, null if nonesqlType- the JDBC sql typesize- the optional sizescale- the optional scalenullable- true if NULL, else NOT NULLdefaultValue- the optional default value- Returns:
- the SQL code
-
sqlAlterColumnNullConstraint
Description copied from interface:BackendGenerates sql code to change the null-constraint of a column.- Specified by:
sqlAlterColumnNullConstraintin interfaceBackend- Overrides:
sqlAlterColumnNullConstraintin classAbstractBackend- Parameters:
tableName- the tablenamecolumnName- the new column namenullable- true if NULL, else NOT NULL- Returns:
- the SQL code, null if need
Backend.sqlAlterColumnType(java.lang.String, java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object)
-
toInternalType
Description copied from interface:BackendConverts a backend specific type name to an internal name used by driver specific methods.- Specified by:
toInternalTypein interfaceBackend- Overrides:
toInternalTypein classAbstractBackend- Parameters:
sqlTypeName- the original type used in SQL scripts- Returns:
- the JDBC internal name
-
isArrayOperatorSupported
Description copied from interface:BackendReturns whether the backend supports given array operator.- Specified by:
isArrayOperatorSupportedin interfaceBackend- Overrides:
isArrayOperatorSupportedin classAbstractBackend- Parameters:
operator- the array operator- Returns:
- true if supported
-
getMigrationStrategy
public MigrationStrategy[] getMigrationStrategy(ColumnMetaData column, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue) Description copied from interface:BackendDetermines the best migration strategy.- Specified by:
getMigrationStrategyin interfaceBackend- Overrides:
getMigrationStrategyin classAbstractBackend- Parameters:
column- old column meta datacolumnName- new column namecomment- new commentsqlType- new sql typesize- new sizescale- new scalenullable- new nullabledefaultValue- new default- Returns:
- the strategies in order to achieve the migration
-
createScriptRunner
Description copied from interface:BackendCreates a script runner instance.- Specified by:
createScriptRunnerin interfaceBackend- Overrides:
createScriptRunnerin classAbstractBackend- Parameters:
connection- the SQL-connection
-
extractWhereClause
Description copied from class:AbstractBackendExtracts the where clause from a given sql.- Overrides:
extractWhereClausein classAbstractBackend- Parameters:
sql- the sql statementwhereOffset- the location of WHERE in sql- Returns:
- the where clause without the keyword WHERE
-
isDropIfExistsSupported
protected boolean isDropIfExistsSupported()Description copied from class:AbstractBackendAdds "IF EXISTS" to drop clauses, if supported by the backend.
Makes migration a little more robust in case the same SQL scripts are applied to different databases than those used to generate the migration scripts for.- Overrides:
isDropIfExistsSupportedin classAbstractBackend- Returns:
- true if supported
-