/** * Retrieves a check constraint information from the database, in the INFORMATION_SCHEMA format. * * @throws SQLException On a SQL exception */ void retrieveCheckConstraintInformation() throws SQLException { final Map<String, MutableCheckConstraint> checkConstraintsMap = new HashMap<>(); final InformationSchemaViews informationSchemaViews = getRetrieverConnection().getInformationSchemaViews(); if (!informationSchemaViews.hasTableConstraintsSql()) { LOGGER.log(Level.FINE, "Table constraints SQL statement was not provided"); return; } final String tableConstraintsInformationSql = informationSchemaViews.getTableConstraintsSql(); final Connection connection = getDatabaseConnection(); try (final Statement statement = connection.createStatement(); final MetadataResultSet results = new MetadataResultSet(statement.executeQuery(tableConstraintsInformationSql)); ) { while (results.next()) { final String catalogName = quotedName(results.getString("CONSTRAINT_CATALOG")); final String schemaName = quotedName(results.getString("CONSTRAINT_SCHEMA")); final String constraintName = quotedName(results.getString("CONSTRAINT_NAME")); LOGGER.log(Level.FINER, "Retrieving constraint: " + constraintName); // "TABLE_CATALOG", "TABLE_SCHEMA" final String tableName = quotedName(results.getString("TABLE_NAME")); final MutableTable table = lookupTable(catalogName, schemaName, tableName); if (table == null) { LOGGER.log( Level.FINE, String.format("Cannot find table, %s.%s.%s", catalogName, schemaName, tableName)); continue; } final String constraintType = results.getString("CONSTRAINT_TYPE"); final boolean deferrable = results.getBoolean("IS_DEFERRABLE"); final boolean initiallyDeferred = results.getBoolean("INITIALLY_DEFERRED"); if (constraintType.equalsIgnoreCase("check")) { final MutableCheckConstraint checkConstraint = new MutableCheckConstraint(table, constraintName); checkConstraint.setDeferrable(deferrable); checkConstraint.setInitiallyDeferred(initiallyDeferred); checkConstraint.addAttributes(results.getAttributes()); // Add to map, since we will need this later final String constraintFullName = table.getSchema().getFullName() + "." + constraintName; checkConstraintsMap.put(constraintFullName, checkConstraint); } } } catch (final Exception e) { LOGGER.log(Level.WARNING, "Could not retrieve check constraint information", e); return; } if (!informationSchemaViews.hasCheckConstraintsSql()) { LOGGER.log(Level.FINE, "Check constraints SQL statement was not provided"); return; } final String checkConstraintInformationSql = informationSchemaViews.getCheckConstraintsSql(); // Get check constraint definitions try (final Statement statement = connection.createStatement(); final MetadataResultSet results = new MetadataResultSet(statement.executeQuery(checkConstraintInformationSql)); ) { while (results.next()) { final String catalogName = quotedName(results.getString("CONSTRAINT_CATALOG")); final String schemaName = quotedName(results.getString("CONSTRAINT_SCHEMA")); final String constraintName = quotedName(results.getString("CONSTRAINT_NAME")); LOGGER.log(Level.FINER, "Retrieving constraint definition: " + constraintName); String definition = results.getString("CHECK_CLAUSE"); final String constraintFullName = new SchemaReference(catalogName, schemaName) + "." + constraintName; final MutableCheckConstraint checkConstraint = checkConstraintsMap.get(constraintFullName); if (checkConstraint == null) { LOGGER.log(Level.FINEST, "Could not add check constraint to table: " + constraintName); continue; } final String text = checkConstraint.getDefinition(); if (!Utility.isBlank(text)) { definition = checkConstraint.getDefinition() + definition; } checkConstraint.setDefinition(definition); } } catch (final Exception e) { LOGGER.log(Level.WARNING, "Could not retrieve check constraints", e); } // Add check constraints to tables final Collection<MutableCheckConstraint> checkConstraintsCollection = checkConstraintsMap.values(); for (final MutableCheckConstraint checkConstraint : checkConstraintsCollection) { final MutableTable table = (MutableTable) checkConstraint.getParent(); table.addCheckConstraint(checkConstraint); } }