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);
}
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);
}
}
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);
}
}
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);
}
}
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()) + "'"));
}
}
Aggregations