Search in sources :

Example 16 with InsertStatement

use of liquibase.statement.core.InsertStatement in project liquibase by liquibase.

the class InsertSetGenerator method generateHeader.

public void generateHeader(StringBuffer sql, InsertSetStatement statement, Database database) {
    InsertStatement insert = statement.peek();
    getInsertGenerator(database).generateHeader(sql, insert, database);
}
Also used : InsertStatement(liquibase.statement.core.InsertStatement)

Example 17 with InsertStatement

use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.

the class AbstractUserFedToComponent method convertFedMapperToComponent.

protected void convertFedMapperToComponent(String realmId, String parentId, String newMapperType) throws CustomChangeException {
    try {
        PreparedStatement statement = jdbcConnection.prepareStatement("select ID, NAME, FEDERATION_MAPPER_TYPE from " + getTableName("USER_FEDERATION_MAPPER") + " WHERE FEDERATION_PROVIDER_ID=?");
        statement.setString(1, parentId);
        try {
            ResultSet resultSet = statement.executeQuery();
            try {
                while (resultSet.next()) {
                    String id = resultSet.getString(1);
                    String mapperName = resultSet.getString(2);
                    String fedMapperType = resultSet.getString(3);
                    InsertStatement insertComponent = new InsertStatement(null, null, database.correctObjectName("COMPONENT", Table.class)).addColumnValue("ID", id).addColumnValue("REALM_ID", realmId).addColumnValue("PARENT_ID", parentId).addColumnValue("NAME", mapperName).addColumnValue("PROVIDER_ID", fedMapperType).addColumnValue("PROVIDER_TYPE", newMapperType);
                    statements.add(insertComponent);
                    PreparedStatement configStatement = jdbcConnection.prepareStatement("select name, VALUE from " + getTableName("USER_FEDERATION_MAPPER_CONFIG") + " WHERE USER_FEDERATION_MAPPER_ID=?");
                    configStatement.setString(1, id);
                    try {
                        ResultSet configSet = configStatement.executeQuery();
                        try {
                            while (configSet.next()) {
                                String name = configSet.getString(1);
                                String value = configSet.getString(2);
                                statements.add(componentConfigStatement(id, name, value));
                            }
                        } finally {
                            configSet.close();
                        }
                    } finally {
                        configStatement.close();
                    }
                    DeleteStatement configDelete = new DeleteStatement(null, null, database.correctObjectName("USER_FEDERATION_MAPPER_CONFIG", Table.class));
                    configDelete.setWhere("USER_FEDERATION_MAPPER_ID=?");
                    configDelete.addWhereParameters(id);
                    statements.add(configDelete);
                    DeleteStatement deleteStatement = new DeleteStatement(null, null, database.correctObjectName("USER_FEDERATION_MAPPER", Table.class));
                    deleteStatement.setWhere("ID=?");
                    deleteStatement.addWhereParameters(id);
                    statements.add(deleteStatement);
                }
            } finally {
                resultSet.close();
            }
        } finally {
            statement.close();
        }
        confirmationMessage.append("Updated " + statements.size() + " records in USER_FEDERATION_MAPPER table for " + parentId + " conversion to component model");
    } catch (Exception e) {
        throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e);
    }
}
Also used : Table(liquibase.structure.core.Table) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) PreparedStatement(java.sql.PreparedStatement) DeleteStatement(liquibase.statement.core.DeleteStatement) InsertStatement(liquibase.statement.core.InsertStatement) CustomChangeException(liquibase.exception.CustomChangeException)

Example 18 with InsertStatement

use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.

the class AuthzResourceUseMoreURIs method generateStatementsImpl.

@Override
protected void generateStatementsImpl() throws CustomChangeException {
    try {
        PreparedStatement statement = jdbcConnection.prepareStatement("select ID,URI from " + getTableName("RESOURCE_SERVER_RESOURCE") + " where URI is not null");
        try {
            ResultSet resultSet = statement.executeQuery();
            try {
                while (resultSet.next()) {
                    String resourceId = resultSet.getString(1);
                    String resourceUri = resultSet.getString(2);
                    InsertStatement insertComponent = new InsertStatement(null, null, database.correctObjectName("RESOURCE_URIS", Table.class)).addColumnValue("RESOURCE_ID", resourceId).addColumnValue("VALUE", resourceUri);
                    statements.add(insertComponent);
                }
            } finally {
                resultSet.close();
            }
        } finally {
            statement.close();
        }
        confirmationMessage.append("Moved " + statements.size() + " records from RESOURCE_SERVER_RESOURCE to RESOURCE_URIS table");
    } catch (Exception e) {
        throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e);
    }
}
Also used : Table(liquibase.structure.core.Table) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) PreparedStatement(java.sql.PreparedStatement) InsertStatement(liquibase.statement.core.InsertStatement) CustomChangeException(liquibase.exception.CustomChangeException)

Example 19 with InsertStatement

use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.

the class ExtractRealmKeysFromRealmTable method generateStatementsImpl.

@Override
protected void generateStatementsImpl() throws CustomChangeException {
    try {
        PreparedStatement statement = jdbcConnection.prepareStatement("select ID, PRIVATE_KEY, CERTIFICATE from " + getTableName("REALM"));
        try {
            ResultSet resultSet = statement.executeQuery();
            try {
                while (resultSet.next()) {
                    String realmId = resultSet.getString(1);
                    String privateKeyPem = resultSet.getString(2);
                    String certificatePem = resultSet.getString(3);
                    String componentId = KeycloakModelUtils.generateId();
                    InsertStatement insertComponent = new InsertStatement(null, null, database.correctObjectName("COMPONENT", Table.class)).addColumnValue("ID", componentId).addColumnValue("REALM_ID", realmId).addColumnValue("PARENT_ID", realmId).addColumnValue("NAME", "rsa").addColumnValue("PROVIDER_ID", "rsa").addColumnValue("PROVIDER_TYPE", KeyProvider.class.getName());
                    statements.add(insertComponent);
                    statements.add(componentConfigStatement(componentId, "priority", "100"));
                    statements.add(componentConfigStatement(componentId, "privateKey", privateKeyPem));
                    statements.add(componentConfigStatement(componentId, "certificate", certificatePem));
                }
            } finally {
                resultSet.close();
            }
        } finally {
            statement.close();
        }
        confirmationMessage.append("Updated " + statements.size() + " records in USER_FEDERATION_PROVIDER table");
    } catch (Exception e) {
        throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e);
    }
}
Also used : KeyProvider(org.keycloak.keys.KeyProvider) Table(liquibase.structure.core.Table) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) PreparedStatement(java.sql.PreparedStatement) InsertStatement(liquibase.statement.core.InsertStatement) CustomChangeException(liquibase.exception.CustomChangeException)

Example 20 with InsertStatement

use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.

the class JpaUpdate13_0_0_MigrateDefaultRoles method generateStatementsImpl.

@Override
protected void generateStatementsImpl() throws CustomChangeException {
    extractRealmIdsAndNames("SELECT ID,NAME FROM " + getTableName("REALM"));
    String clientTable = getTableName("CLIENT");
    String clientDefaultRolesTable = getTableName("CLIENT_DEFAULT_ROLES");
    String compositeRoleTable = getTableName("COMPOSITE_ROLE");
    for (Map.Entry<String, String> entry : realmIdsAndNames.entrySet()) {
        String id = UUID.randomUUID().toString();
        String roleName = determineDefaultRoleName(entry.getKey(), entry.getValue());
        statements.add(// create new default role
        new InsertStatement(null, null, database.correctObjectName("KEYCLOAK_ROLE", Table.class)).addColumnValue("ID", id).addColumnValue("CLIENT_REALM_CONSTRAINT", entry.getKey()).addColumnValue("CLIENT_ROLE", Boolean.FALSE).addColumnValue("DESCRIPTION", "${role_" + roleName + "}").addColumnValue("NAME", roleName).addColumnValue("REALM_ID", entry.getKey()).addColumnValue("REALM", entry.getKey()));
        statements.add(// assign the role to the realm
        new UpdateStatement(null, null, database.correctObjectName("REALM", Table.class)).addNewColumnValue("DEFAULT_ROLE", id).setWhereClause("REALM.ID=?").addWhereParameter(entry.getKey()));
        statements.add(// copy data from REALM_DEFAULT_ROLES to COMPOSITE_ROLE
        new RawSqlStatement("INSERT INTO " + compositeRoleTable + " (COMPOSITE, CHILD_ROLE) " + "SELECT '" + id + "', ROLE_ID FROM " + getTableName("REALM_DEFAULT_ROLES") + " WHERE REALM_ID = '" + database.escapeStringForDatabase(entry.getKey()) + "'"));
        statements.add(// copy data from CLIENT_DEFAULT_ROLES to COMPOSITE_ROLE
        new RawSqlStatement("INSERT INTO " + compositeRoleTable + " (COMPOSITE, CHILD_ROLE) " + "SELECT '" + id + "', " + clientDefaultRolesTable + ".ROLE_ID FROM " + clientDefaultRolesTable + " INNER JOIN " + clientTable + " ON " + clientTable + ".ID = " + clientDefaultRolesTable + ".CLIENT_ID AND " + clientTable + ".REALM_ID = '" + database.escapeStringForDatabase(entry.getKey()) + "'"));
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) Table(liquibase.structure.core.Table) Map(java.util.Map) HashMap(java.util.HashMap) InsertStatement(liquibase.statement.core.InsertStatement)

Aggregations

InsertStatement (liquibase.statement.core.InsertStatement)27 PreparedStatement (java.sql.PreparedStatement)9 ResultSet (java.sql.ResultSet)9 Sql (liquibase.sql.Sql)8 CustomChangeException (liquibase.exception.CustomChangeException)6 SqlStatement (liquibase.statement.SqlStatement)6 Table (liquibase.structure.core.Table)6 ArrayList (java.util.ArrayList)4 ColumnConfig (liquibase.change.ColumnConfig)4 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)4 Map (java.util.Map)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 DeleteStatement (liquibase.statement.core.DeleteStatement)3 HashMap (java.util.HashMap)2 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)2 UnparsedSql (liquibase.sql.UnparsedSql)2 SequenceCurrentValueFunction (liquibase.statement.SequenceCurrentValueFunction)2 UpdateStatement (liquibase.statement.core.UpdateStatement)2 Column (liquibase.structure.core.Column)2