Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coverage, especially for PostgreSQL enum helper #1663

Merged
merged 11 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ public String getWidth() {
throw new NotLoadedException(this);
}

@Override
public boolean isColumnDataTypeKnown() {
return false;
}

/** {@inheritDoc} */
@Override
public boolean isAutoIncremented() {
throw new NotLoadedException(this);
}

@Override
public boolean isColumnDataTypeKnown() {
return false;
}

/** {@inheritDoc} */
@Override
public boolean isGenerated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,72 @@ void retrieveIndexes(final NamedObjectList<MutableTable> allTables) throws SQLEx
}
}

/**
* Retrieves index information from the database, in the INFORMATION_SCHEMA format.
*
* @throws SQLException On a SQL exception
*/
void retrieveIndexInformation() throws SQLException {
final InformationSchemaViews informationSchemaViews =
getRetrieverConnection().getInformationSchemaViews();

if (!informationSchemaViews.hasQuery(EXT_INDEXES)) {
LOGGER.log(
Level.INFO, "Not retrieving additional index information, since this was not requested");
LOGGER.log(Level.FINE, "Indexes information SQL statement was not provided");
return;
}

LOGGER.log(Level.INFO, "Retrieving additional index information");

final Query extIndexesInformationSql = informationSchemaViews.getQuery(EXT_INDEXES);
try (final Connection connection = getRetrieverConnection().getConnection();
final Statement statement = connection.createStatement();
final MetadataResultSet results =
new MetadataResultSet(extIndexesInformationSql, statement, getLimitMap()); ) {

while (results.next()) {
final String catalogName = normalizeCatalogName(results.getString("INDEX_CATALOG"));
final String schemaName = normalizeSchemaName(results.getString("INDEX_SCHEMA"));
final String tableName = results.getString("TABLE_NAME");
final String indexName = results.getString("INDEX_NAME");

final Optional<MutableTable> tableOptional =
lookupTable(catalogName, schemaName, tableName);
if (!tableOptional.isPresent()) {
LOGGER.log(
Level.FINE,
new StringFormat("Cannot find table <%s.%s.%s>", catalogName, schemaName, indexName));
continue;
}

LOGGER.log(Level.FINER, new StringFormat("Retrieving index information <%s>", indexName));
final MutableTable table = tableOptional.get();
final Optional<MutableIndex> indexOptional = table.lookupIndex(indexName);
if (!indexOptional.isPresent()) {
LOGGER.log(
Level.FINE,
new StringFormat(
"Cannot find index <%s.%s.%s.%s>",
catalogName, schemaName, tableName, indexName));
continue;
}

final MutableIndex index = indexOptional.get();

final String definition = results.getString("INDEX_DEFINITION");
final String remarks = results.getString("REMARKS");

index.appendDefinition(definition);
index.setRemarks(remarks);

index.addAttributes(results.getAttributes());
}
} catch (final Exception e) {
LOGGER.log(Level.WARNING, "Could not retrieve index information", e);
}
}

private void createIndexes(final MutableTable table, final MetadataResultSet results)
throws SQLException {
while (results.next()) {
Expand Down Expand Up @@ -217,70 +283,4 @@ private void retrieveIndexesFromMetadata(final NamedObjectList<MutableTable> all
}
}
}

/**
* Retrieves index information from the database, in the INFORMATION_SCHEMA format.
*
* @throws SQLException On a SQL exception
*/
void retrieveIndexInformation() throws SQLException {
final InformationSchemaViews informationSchemaViews =
getRetrieverConnection().getInformationSchemaViews();

if (!informationSchemaViews.hasQuery(EXT_INDEXES)) {
LOGGER.log(
Level.INFO, "Not retrieving additional index information, since this was not requested");
LOGGER.log(Level.FINE, "Indexes information SQL statement was not provided");
return;
}

LOGGER.log(Level.INFO, "Retrieving additional index information");

final Query extIndexesInformationSql = informationSchemaViews.getQuery(EXT_INDEXES);
try (final Connection connection = getRetrieverConnection().getConnection();
final Statement statement = connection.createStatement();
final MetadataResultSet results =
new MetadataResultSet(extIndexesInformationSql, statement, getLimitMap()); ) {

while (results.next()) {
final String catalogName = normalizeCatalogName(results.getString("INDEX_CATALOG"));
final String schemaName = normalizeSchemaName(results.getString("INDEX_SCHEMA"));
final String tableName = results.getString("TABLE_NAME");
final String indexName = results.getString("INDEX_NAME");

final Optional<MutableTable> tableOptional =
lookupTable(catalogName, schemaName, tableName);
if (!tableOptional.isPresent()) {
LOGGER.log(
Level.FINE,
new StringFormat("Cannot find table <%s.%s.%s>", catalogName, schemaName, indexName));
continue;
}

LOGGER.log(Level.FINER, new StringFormat("Retrieving index information <%s>", indexName));
final MutableTable table = tableOptional.get();
final Optional<MutableIndex> indexOptional = table.lookupIndex(indexName);
if (!indexOptional.isPresent()) {
LOGGER.log(
Level.FINE,
new StringFormat(
"Cannot find index <%s.%s.%s.%s>",
catalogName, schemaName, tableName, indexName));
continue;
}

final MutableIndex index = indexOptional.get();

final String definition = results.getString("INDEX_DEFINITION");
final String remarks = results.getString("REMARKS");

index.appendDefinition(definition);
index.setRemarks(remarks);

index.addAttributes(results.getAttributes());
}
} catch (final Exception e) {
LOGGER.log(Level.WARNING, "Could not retrieve index information", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ public boolean test(final Table table) {
return include;
}

private boolean checkIncludeForTables(final Table table) {
return grepTableInclusionRule != null && grepTableInclusionRule.test(table.getFullName());
}

private boolean checkIncludeForColumns(final Table table) {

final List<Column> columns = table.getColumns();
Expand Down Expand Up @@ -135,4 +131,8 @@ private boolean checkIncludeForDefinitions(final Table table) {
}
return false;
}

private boolean checkIncludeForTables(final Table table) {
return grepTableInclusionRule != null && grepTableInclusionRule.test(table.getFullName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@

package schemacrawler.schemacrawler;

import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.TemplatingUtility.expandTemplate;
import static us.fatehi.utility.Utility.isBlank;
import static us.fatehi.utility.database.DatabaseUtility.executeSql;
import static us.fatehi.utility.database.DatabaseUtility.executeSqlForLong;
import static us.fatehi.utility.database.DatabaseUtility.executeSqlForScalar;
Expand All @@ -44,13 +42,17 @@
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.isBlank;
import schemacrawler.inclusionrule.InclusionRule;
import schemacrawler.inclusionrule.InclusionRuleWithRegularExpression;
import schemacrawler.schema.Column;
import schemacrawler.schema.ColumnDataType;
import schemacrawler.schema.Schema;
import schemacrawler.schema.Table;
import schemacrawler.utility.MetaDataUtility;
import schemacrawler.utility.NamedObjectSort;
import us.fatehi.utility.IOUtility;
import us.fatehi.utility.UtilityMarker;
import us.fatehi.utility.string.StringFormat;

Expand All @@ -59,11 +61,22 @@ public final class QueryUtility {

private static final Logger LOGGER = Logger.getLogger(QueryUtility.class.getName());

public static ResultSet executeAgainstColumnDataType(
final Query query, final Statement statement, final ColumnDataType columnDataType)
throws SQLException {
requireNonNull(query, "No query provided");
final Map<String, String> variablesMap = makeVariablesMap(columnDataType);
final String sql = expandQuery(query, variablesMap);
LOGGER.log(Level.FINE, new StringFormat("Executing %s: %n%s", query.getName(), sql));
return executeSql(statement, sql);
}

public static ResultSet executeAgainstSchema(
final Query query, final Statement statement, final Map<String, InclusionRule> limitMap)
throws SQLException {
requireNonNull(query, "No query provided");
final String sql = getQuery(query, limitMap);
final Map<String, String> variablesMap = makeVariablesMap(limitMap);
final String sql = expandQuery(query, variablesMap);
LOGGER.log(Level.FINE, new StringFormat("Executing %s: %n%s", query.getName(), sql));
return executeSql(statement, sql);
}
Expand All @@ -76,9 +89,9 @@ public static ResultSet executeAgainstTable(
final Identifiers identifiers)
throws SQLException {
requireNonNull(query, "No query provided");
requireNonNull(identifiers, "No identifiers provided");

final String sql = getQuery(query, table, isAlphabeticalSortForTableColumns, identifiers);
final Map<String, String> variablesMap =
makeVariablesMap(table, isAlphabeticalSortForTableColumns, identifiers);
final String sql = expandQuery(query, variablesMap);
LOGGER.log(Level.FINE, new StringFormat("Executing %s: %n%s", query.getName(), sql));
return executeSql(statement, sql);
}
Expand All @@ -90,15 +103,16 @@ public static long executeForLong(
final Identifiers identifiers)
throws SQLException {
requireNonNull(query, "No query provided");
final String sql = getQuery(query, table, true, identifiers);
final Map<String, String> variablesMap = makeVariablesMap(table, true, identifiers);
final String sql = expandQuery(query, variablesMap);
LOGGER.log(Level.FINE, new StringFormat("Executing %s: %n%s", query.getName(), sql));
return executeSqlForLong(connection, sql);
}

public static Object executeForScalar(final Query query, final Connection connection)
throws SQLException {
requireNonNull(query, "No query provided");
final String sql = getQuery(query);
final String sql = expandQuery(query);
LOGGER.log(Level.FINE, new StringFormat("Executing %s: %n%s", query.getName(), sql));
return executeSqlForScalar(connection, sql);
}
Expand All @@ -110,11 +124,17 @@ public static Object executeForScalar(
final Identifiers identifiers)
throws SQLException {
requireNonNull(query, "No query provided");
final String sql = getQuery(query, table, true, identifiers);
final Map<String, String> variablesMap = makeVariablesMap(table, true, identifiers);
final String sql = expandQuery(query, variablesMap);
LOGGER.log(Level.FINE, new StringFormat("Executing %s: %n%s", query.getName(), sql));
return executeSqlForScalar(connection, sql);
}

public static Query getQueryFromResource(final String name, final String resource) {
final String sql = IOUtility.readResourceFully(resource);
return new Query(name, sql);
}

protected static void addInclusionRule(
final String limitType,
final InclusionRule schemaInclusionRule,
Expand All @@ -131,36 +151,44 @@ protected static void addInclusionRule(
}
}

private static String getQuery(final Query query) {
return expandTemplate(query.getQuery());
private static String expandQuery(final Query query) {
return expandQuery(query, null);
}

private static String expandQuery(final Query query, final Map<String, String> variablesMap) {
String sql = query.getQuery();
if (variablesMap != null && !variablesMap.isEmpty()) {
sql = expandTemplate(sql, variablesMap);
}
return expandTemplate(sql);
}

/**
* Gets the query with parameters substituted.
*
* @param schemaInclusionRule Schema inclusion rule
* @return Ready-to-execute query
*/
private static String getQuery(final Query query, final Map<String, InclusionRule> limitMap) {
final Map<String, String> properties = new HashMap<>();
private static Map<String, String> makeVariablesMap(final ColumnDataType columnDataType) {
requireNonNull(columnDataType, "No column data type provided");

requireNonNull(query, "No query provided");
final Map<String, String> variablesMap = new HashMap<>();
variablesMap.put("column-data-type", columnDataType.getName());
return variablesMap;
}

private static Map<String, String> makeVariablesMap(final Map<String, InclusionRule> limitMap) {
requireNonNull(limitMap, "No limit map provided");

final Map<String, String> variablesMap = new HashMap<>();

for (final Entry<String, InclusionRule> limit : limitMap.entrySet()) {
addInclusionRule(limit.getKey(), limit.getValue(), properties);
addInclusionRule(limit.getKey(), limit.getValue(), variablesMap);
}

String sql = query.getQuery();
sql = expandTemplate(sql, properties);
return expandTemplate(sql);
return variablesMap;
}

private static String getQuery(
final Query query,
private static Map<String, String> makeVariablesMap(
final Table table,
final boolean isAlphabeticalSortForTableColumns,
final Identifiers identifiers) {
requireNonNull(identifiers, "No identifiers provided");

final Map<String, String> tableProperties = new HashMap<>();
if (table != null) {
final NamedObjectSort columnsSort =
Expand All @@ -181,9 +209,7 @@ private static String getQuery(
tableProperties.put("tabletype", table.getTableType().toString());
}

String sql = query.getQuery();
sql = expandTemplate(sql, tableProperties);
return expandTemplate(sql);
return tableProperties;
}

private QueryUtility() {
Expand Down
Loading
Loading