Java Code Examples for liquibase.database.Database#correctObjectName()

The following examples show how to use liquibase.database.Database#correctObjectName() . 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: SpatialIndexExistsPrecondition.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * Generates the {@link Index} example (taken from {@link IndexExistsPrecondition}).
 *
 * @param database
 *           the database instance.
 * @param schema
 *           the schema instance.
 * @param tableName
 *           the table name of the index.
 * @return the index example.
 */
protected Index getIndexExample(final Database database, final Schema schema,
      final String tableName) {
   final Index example = new Index();
   if (tableName != null) {
      example.setTable((Table) new Table().setName(
            database.correctObjectName(getTableName(), Table.class)).setSchema(schema));
   }
   example.setName(database.correctObjectName(getIndexName(), Index.class));
   if (StringUtils.trimToNull(getColumnNames()) != null) {
      for (final String columnName : getColumnNames().split("\\s*,\\s*")) {
         final Column column = new Column(database.correctObjectName(columnName, Column.class));
         example.getColumns().add(column);
      }
   }
   return example;
}
 
Example 2
Source File: GeometryColumnsUtils.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * Indicates if the <code>GEOMETRY_COLUMNS</code> table or view exists.
 * 
 * @param database
 *           the database to check.
 * @return <code>true</code> if the table or view exists.
 */
public static boolean geometryColumnsExists(final Database database) {
   String geometryColumnsName = database.correctObjectName(
         "geometry_columns", Table.class);
   DatabaseObject example = null;
   if (database instanceof DerbyDatabase || database instanceof H2Database) {
      final Table tableExample = new Table();
      tableExample.setName(geometryColumnsName);
      tableExample.setSchema(database.getDefaultCatalogName(),
            database.getDefaultSchemaName());
      example = tableExample;
   } else if (database instanceof PostgresDatabase) {
      final View viewExample = new View();
      viewExample.setName(geometryColumnsName);
      viewExample.setSchema(database.getDefaultCatalogName(), "public");
      example = viewExample;
   }
   try {
      return example != null
            && SnapshotGeneratorFactory.getInstance().has(example, database);
   } catch (final LiquibaseException e) {
      throw new UnexpectedLiquibaseException(
            "Failed to determine if the geometry_columns table or view exists",
            e);
   }
}
 
Example 3
Source File: SpatialIndexExistsPrecondition.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an example of the database object for which to check.
 *
 * @param database
 *           the database instance.
 * @param tableName
 *           the table name of the index.
 * @return the database object example.
 */
public DatabaseObject getExample(final Database database, final String tableName) {
   final Schema schema = new Schema(getCatalogName(), getSchemaName());
   final DatabaseObject example;

   // For GeoDB, the index is another table.
   if (database instanceof DerbyDatabase || database instanceof H2Database) {
      final String correctedTableName = database.correctObjectName(getHatboxTableName(),
            Table.class);
      example = new Table().setName(correctedTableName).setSchema(schema);
   } else {
      example = getIndexExample(database, schema, tableName);
   }
   return example;
}
 
Example 4
Source File: AddRealmCodeSecret.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public SqlStatement[] generateStatements(Database database) throws CustomChangeException {
    try {
        StringBuilder sb = new StringBuilder();
        sb.append("Generated codeSecret for realms: ");

        Connection connection = ((JdbcConnection) (database.getConnection())).getWrappedConnection();
        ArrayList<SqlStatement> statements = new ArrayList<SqlStatement>();

        String correctedTableName = database.correctObjectName("REALM", Table.class);
        String correctedSchemaName = database.escapeObjectName(database.getDefaultSchemaName(), Schema.class);

        if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) {
            try (Statement st = connection.createStatement(); ResultSet resultSet = st.executeQuery("SELECT ID FROM " + LiquibaseJpaUpdaterProvider.getTable(correctedTableName, correctedSchemaName) + " WHERE CODE_SECRET IS NULL")) {
                while (resultSet.next()) {
                    String id = resultSet.getString(1);
                    
                    UpdateStatement statement = new UpdateStatement(null, null, correctedTableName)
                            .addNewColumnValue("CODE_SECRET", KeycloakModelUtils.generateCodeSecret())
                            .setWhereClause("ID=?").addWhereParameters(id);
                    statements.add(statement);
                    
                    if (!resultSet.isFirst()) {
                        sb.append(", ");
                    }
                    sb.append(id);
                }

                if (!statements.isEmpty()) {
                    confirmationMessage = sb.toString();
                }
            }
        }

        return statements.toArray(new SqlStatement[statements.size()]);
    } catch (Exception e) {
        throw new CustomChangeException("Failed to add realm code secret", e);
    }
}