Java Code Examples for liquibase.database.Database#escapeColumnName()
The following examples show how to use
liquibase.database.Database#escapeColumnName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PerconaModifyDataTypeChange.java From liquibase-percona with Apache License 2.0 | 6 votes |
@Override public String generateAlterStatement(Database database) { StringBuilder alter = new StringBuilder(); alter.append("MODIFY "); String columnName = database.escapeColumnName(getCatalogName(), getSchemaName(), getTableName(), getColumnName()); alter.append(columnName); alter.append(' '); if (getNewDataType() != null) { String dataType = String.valueOf(DataTypeFactory.getInstance().fromDescription(getNewDataType(), database).toDatabaseDataType(database)); alter.append(dataType); } return alter.toString(); }
Example 2
Source File: PerconaAddColumnChange.java From liquibase-percona with Apache License 2.0 | 5 votes |
String convertColumnToSql(AddColumnConfig column, Database database) { String nullable = ""; ConstraintsConfig constraintsConfig = column.getConstraints(); if (constraintsConfig != null && !constraintsConfig.isNullable()) { nullable = " NOT NULL"; } else { nullable = " NULL"; } String defaultValue = ""; if (column.getDefaultValueObject() != null) { defaultValue = " DEFAULT " + DataTypeFactory.getInstance().fromObject(column.getDefaultValueObject(), database).objectToSql(column.getDefaultValueObject(), database); } String comment = ""; if (StringUtil.isNotEmpty(column.getRemarks())) { comment += " COMMENT '" + column.getRemarks() + "'"; } String after = ""; if (StringUtil.isNotEmpty(column.getAfterColumn())) { after += " AFTER " + database.escapeColumnName(null, null, null, column.getAfterColumn()); } String constraints = ""; constraints += addForeignKeyConstraint(column, database); constraints += addUniqueKeyConstraint(column, database); return "ADD COLUMN " + database.escapeColumnName(null, null, null, column.getName()) + " " + DataTypeFactory.getInstance().fromDescription(column.getType(), database).toDatabaseDataType(database) + nullable + defaultValue + comment + after + constraints; }
Example 3
Source File: PerconaAddColumnChange.java From liquibase-percona with Apache License 2.0 | 5 votes |
private String addForeignKeyConstraint(AddColumnConfig column, Database database) { String result = ""; ConstraintsConfig constraintsConfig = column.getConstraints(); if (constraintsConfig != null && (StringUtil.isNotEmpty(constraintsConfig.getReferences()) || StringUtil.isNotEmpty(constraintsConfig.getReferencedTableName()) )) { result += ", ADD "; if (StringUtil.isNotEmpty(constraintsConfig.getForeignKeyName())) { result += "CONSTRAINT " + database.escapeConstraintName(constraintsConfig.getForeignKeyName()) + " "; } result += "FOREIGN KEY (" + database.escapeColumnName(null, null, null, column.getName()) + ") REFERENCES "; String referencedTable; String referencedColumn; if (StringUtil.isNotEmpty(constraintsConfig.getReferences())) { Matcher references = Pattern.compile("([\\w\\._]+)\\(([\\w_]+)\\)").matcher(constraintsConfig.getReferences()); if (!references.matches()) { throw new UnexpectedLiquibaseException("Unable to get table name and column name from " + constraintsConfig.getReferences()); } referencedTable = references.group(1); referencedColumn = references.group(2); } else { referencedTable = constraintsConfig.getReferencedTableName(); referencedColumn = constraintsConfig.getReferencedColumnNames(); } referencedTable = PerconaChangeUtil.resolveReferencedPerconaTableName(getTableName(), referencedTable); result += database.escapeTableName(null, null, referencedTable) + "("; result += database.escapeColumnName(null, null, null, referencedColumn); result += ")"; } return result; }
Example 4
Source File: PerconaAddColumnChange.java From liquibase-percona with Apache License 2.0 | 5 votes |
private String addUniqueKeyConstraint(AddColumnConfig column, Database database) { String result = ""; ConstraintsConfig constraintsConfig = column.getConstraints(); if (constraintsConfig != null && constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) { result += ", ADD "; if (StringUtil.isNotEmpty(constraintsConfig.getUniqueConstraintName())) { result += "CONSTRAINT " + database.escapeConstraintName(constraintsConfig.getUniqueConstraintName()) + " "; } result += "UNIQUE (" + database.escapeColumnName(null, null, null, column.getName()) + ")"; } return result; }
Example 5
Source File: CustomLockDatabaseChangeLogGenerator.java From keycloak with Apache License 2.0 | 5 votes |
private Sql generateSelectForUpdate(Database database, int id) { String catalog = database.getLiquibaseCatalogName(); String schema = database.getLiquibaseSchemaName(); String rawLockTableName = database.getDatabaseChangeLogLockTableName(); String lockTableName = database.escapeTableName(catalog, schema, rawLockTableName); String idColumnName = database.escapeColumnName(catalog, schema, rawLockTableName, "ID"); String sqlBase = "SELECT " + idColumnName + " FROM " + lockTableName; String sqlWhere = " WHERE " + idColumnName + "=" + id; String sql; if (database instanceof MySQLDatabase || database instanceof PostgresDatabase || database instanceof H2Database || database instanceof OracleDatabase) { sql = sqlBase + sqlWhere + " FOR UPDATE"; } else if (database instanceof MSSQLDatabase) { sql = sqlBase + " WITH (UPDLOCK, ROWLOCK)" + sqlWhere; } else if (database instanceof DB2Database) { sql = sqlBase + sqlWhere + " FOR READ ONLY WITH RS USE AND KEEP UPDATE LOCKS"; } else { sql = sqlBase + sqlWhere; logger.warnf("No direct support for database %s . Database lock may not work correctly", database.getClass().getName()); } logger.debugf("SQL command for pessimistic lock: %s", sql); return new UnparsedSql(sql); }
Example 6
Source File: MetastoreAddColumnGenerator.java From liquibase-impala with Apache License 2.0 | 4 votes |
@Override protected String generateSingleColumnSQL(AddColumnStatement statement, Database database) { DatabaseDataType databaseColumnType = DataTypeFactory.getInstance().fromDescription(statement.getColumnType(), database).toDatabaseDataType(database); return " ADD COLUMNS (" + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + databaseColumnType + ")"; }
Example 7
Source File: ImpalaAddColumnGenerator.java From liquibase-impala with Apache License 2.0 | 4 votes |
@Override protected String generateSingleColumnSQL(AddColumnStatement statement, Database database) { DatabaseDataType databaseColumnType = DataTypeFactory.getInstance().fromDescription(statement.getColumnType(), database).toDatabaseDataType(database); return " ADD COLUMNS (" + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + databaseColumnType + ")"; }