use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.
the class JpaUpdate1_2_0_Beta1 method addDefaultProtocolMappers.
protected void addDefaultProtocolMappers() throws SQLException, DatabaseException {
String protocolMapperTableName = database.correctObjectName("PROTOCOL_MAPPER", Table.class);
String protocolMapperCfgTableName = database.correctObjectName("PROTOCOL_MAPPER_CONFIG", Table.class);
PreparedStatement statement = jdbcConnection.prepareStatement("select ID, NAME, ALLOWED_CLAIMS_MASK from " + getTableName("CLIENT"));
try {
ResultSet resultSet = statement.executeQuery();
try {
boolean first = true;
while (resultSet.next()) {
if (first) {
confirmationMessage.append("Migrating claimsMask to protocol mappers for clients: ");
first = false;
}
Object acmObj = resultSet.getObject("ALLOWED_CLAIMS_MASK");
long mask = (acmObj != null) ? ((Number) acmObj).longValue() : ClaimMask.ALL;
MigrationProvider migrationProvider = this.kcSession.getProvider(MigrationProvider.class);
List<ProtocolMapperRepresentation> protocolMappers = migrationProvider.getMappersForClaimMask(mask);
for (ProtocolMapperRepresentation protocolMapper : protocolMappers) {
String mapperId = KeycloakModelUtils.generateId();
InsertStatement insert = new InsertStatement(null, null, protocolMapperTableName).addColumnValue("ID", mapperId).addColumnValue("PROTOCOL", protocolMapper.getProtocol()).addColumnValue("NAME", protocolMapper.getName()).addColumnValue("CONSENT_REQUIRED", false).addColumnValue("PROTOCOL_MAPPER_NAME", protocolMapper.getProtocolMapper()).addColumnValue("CLIENT_ID", resultSet.getString("ID"));
statements.add(insert);
for (Map.Entry<String, String> cfgEntry : protocolMapper.getConfig().entrySet()) {
InsertStatement cfgInsert = new InsertStatement(null, null, protocolMapperCfgTableName).addColumnValue("PROTOCOL_MAPPER_ID", mapperId).addColumnValue("NAME", cfgEntry.getKey()).addColumnValue("VALUE", cfgEntry.getValue());
statements.add(cfgInsert);
}
}
confirmationMessage.append(resultSet.getString("NAME") + ", ");
}
// It means that some provider where processed
if (!first) {
confirmationMessage.append(". ");
}
} finally {
resultSet.close();
}
} finally {
statement.close();
}
}
use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.
the class JpaUpdate1_2_0_Beta1 method convertSocialToIdFedRealms.
protected void convertSocialToIdFedRealms() throws SQLException, DatabaseException {
String identityProviderTableName = database.correctObjectName("IDENTITY_PROVIDER", Table.class);
String idpConfigTableName = database.correctObjectName("IDENTITY_PROVIDER_CONFIG", Table.class);
String realmSocialConfigTable = getTableName("REALM_SOCIAL_CONFIG");
String realmTableName = getTableName("REALM");
PreparedStatement statement = jdbcConnection.prepareStatement("select RSC.NAME, VALUE, REALM_ID, UPDATE_PROFILE_ON_SOC_LOGIN from " + realmSocialConfigTable + " RSC," + realmTableName + " REALM where RSC.REALM_ID = REALM.ID ORDER BY RSC.REALM_ID, RSC.NAME");
try {
ResultSet resultSet = statement.executeQuery();
try {
boolean providerInProgress = false;
String socialProviderId = null;
String clientId = null;
String clientSecret;
String realmId = null;
boolean updateProfileOnSocialLogin = false;
boolean first = true;
while (resultSet.next()) {
if (first) {
confirmationMessage.append("Migrating social to identity providers: ");
first = false;
}
if (!providerInProgress) {
String key = resultSet.getString("NAME");
int keyIndex = key.indexOf(".key");
if (keyIndex == -1) {
throw new IllegalStateException("Can't parse the provider from column: " + key);
}
socialProviderId = key.substring(0, keyIndex);
clientId = resultSet.getString("VALUE");
realmId = resultSet.getString("REALM_ID");
updateProfileOnSocialLogin = resultSet.getBoolean("UPDATE_PROFILE_ON_SOC_LOGIN");
providerInProgress = true;
} else {
clientSecret = resultSet.getString("VALUE");
String internalId = KeycloakModelUtils.generateId();
InsertStatement idpInsert = new InsertStatement(null, null, identityProviderTableName).addColumnValue("INTERNAL_ID", internalId).addColumnValue("ENABLED", true).addColumnValue("PROVIDER_ALIAS", socialProviderId).addColumnValue("PROVIDER_ID", socialProviderId).addColumnValue("UPDATE_PROFILE_FIRST_LOGIN", updateProfileOnSocialLogin).addColumnValue("STORE_TOKEN", false).addColumnValue("AUTHENTICATE_BY_DEFAULT", false).addColumnValue("REALM_ID", realmId);
InsertStatement clientIdInsert = new InsertStatement(null, null, idpConfigTableName).addColumnValue("IDENTITY_PROVIDER_ID", internalId).addColumnValue("NAME", "clientId").addColumnValue("VALUE", clientId);
InsertStatement clientSecretInsert = new InsertStatement(null, null, idpConfigTableName).addColumnValue("IDENTITY_PROVIDER_ID", internalId).addColumnValue("NAME", "clientSecret").addColumnValue("VALUE", clientSecret);
statements.add(idpInsert);
statements.add(clientIdInsert);
statements.add(clientSecretInsert);
confirmationMessage.append(socialProviderId + " in realm " + realmId + ", ");
providerInProgress = false;
}
}
// It means that some provider where processed
if (!first) {
confirmationMessage.append(". ");
}
} finally {
resultSet.close();
}
} finally {
statement.close();
}
}
use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.
the class JpaUpdate4_0_0_DefaultClientScopes method generateStatementsImpl.
@Override
protected void generateStatementsImpl() throws CustomChangeException {
String clientTableName = database.correctObjectName("CLIENT", Table.class);
String clientScopeClientTableName = database.correctObjectName("CLIENT_SCOPE_CLIENT", Table.class);
try (PreparedStatement statement = jdbcConnection.prepareStatement("SELECT ID, CLIENT_TEMPLATE_ID FROM " + clientTableName);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
String clientId = rs.getString(1);
String clientTemplateId = rs.getString(2);
if (clientId == null || clientId.trim().isEmpty()) {
continue;
}
if (clientTemplateId == null || clientTemplateId.trim().isEmpty()) {
continue;
}
statements.add(new InsertStatement(null, null, clientScopeClientTableName).addColumnValue("CLIENT_ID", clientId.trim()).addColumnValue("SCOPE_ID", clientTemplateId.trim()).addColumnValue("DEFAULT_SCOPE", Boolean.TRUE));
}
confirmationMessage.append("Updated " + statements.size() + " records in CLIENT_SCOPE_CLIENT table");
} catch (Exception e) {
throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e);
}
}
use of liquibase.statement.core.InsertStatement in project liquibase-db2i by liquibase.
the class InsertSetGeneratorDB2i method generateSql.
@Override
public Sql[] generateSql(InsertSetStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
if (statement.peek() == null) {
return new UnparsedSql[0];
}
StringBuilder sql = new StringBuilder();
generateHeader(sql, statement, database);
ArrayList<Sql> result = new ArrayList<Sql>();
int index = 0;
for (InsertStatement sttmnt : statement.getStatements()) {
index++;
getInsertGenerator(database).generateValues(sql, sttmnt, database);
sql.append(",");
if (index > statement.getBatchThreshold()) {
result.add(completeStatement(statement, sql));
index = 0;
sql = new StringBuilder();
generateHeader(sql, statement, database);
}
}
if (index > 0) {
result.add(completeStatement(statement, sql));
}
return result.toArray(new UnparsedSql[result.size()]);
}
use of liquibase.statement.core.InsertStatement in project liquibase by liquibase.
the class InsertOrUpdateGeneratorPostgresTest method testInsertSequenceValWithSchema.
@Test
public void testInsertSequenceValWithSchema() {
PostgresDatabase postgresDatabase = new PostgresDatabase();
InsertGenerator generator = new InsertGenerator();
InsertStatement statement = new InsertStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setValueSequenceNext(new SequenceNextValueFunction(SCHEMA_NAME, SEQUENCE_NAME));
columnConfig.setName("col3");
statement.addColumn(columnConfig);
Sql[] sql = generator.generateSql(statement, postgresDatabase, null);
String theSql = sql[0].toSql();
assertEquals(String.format("INSERT INTO %s.%s (col3) VALUES (nextval('%s.%s'))", SCHEMA_NAME, TABLE_NAME, SCHEMA_NAME, SEQUENCE_NAME), theSql);
}
Aggregations