Search in sources :

Example 1 with IndexColumn

use of schemacrawler.schema.IndexColumn in project hale by halestudio.

the class JDBCSchemaReader method getOrCreateTableType.

/**
 * Get or create the type definition for the given table.
 *
 * @param schema the schema in which the table exists
 * @param table the table
 * @param overallNamespace the database namespace
 * @param namespace the schema namespace
 * @param types the type index
 * @param connection the database connection
 * @param reporter the reporter
 * @param catalog the catalog for access to other column types
 * @return the type definition for the given table
 */
private TypeDefinition getOrCreateTableType(schemacrawler.schema.Schema schema, Table table, String overallNamespace, String namespace, DefaultSchema types, Connection connection, IOReporter reporter, Catalog catalog) {
    QName typeName = new QName(namespace, unquote(table.getName()));
    // check for existing type
    TypeDefinition existingType = types.getType(typeName);
    if (existingType != null)
        return existingType;
    // create new type
    DefaultTypeDefinition type = new DefaultTypeDefinition(typeName);
    // set description if available
    if (table.getRemarks() != null && !table.getRemarks().isEmpty())
        type.setDescription(table.getRemarks());
    // configure type
    type.setConstraint(MappableFlag.ENABLED);
    type.setConstraint(MappingRelevantFlag.ENABLED);
    type.setConstraint(HasValueFlag.DISABLED);
    // set schema and table name
    DatabaseTable tableConstraint = new DatabaseTable(unquote(schema.getName()), unquote(table.getName()), useQuote);
    type.setConstraint(tableConstraint);
    // set SQL query constraint
    String query = "SELECT * FROM " + tableConstraint.getFullTableName();
    type.setConstraint(new SQLQuery(query));
    // set primary key if possible
    PrimaryKey key = null;
    try {
        key = table.getPrimaryKey();
    } catch (Exception e) {
        reporter.warn(new IOMessageImpl("Could not read primary key metadata for table: " + table.getFullName(), e));
    }
    if (key != null) {
        List<IndexColumn> columns = key.getColumns();
        if (columns.size() > 1) {
            reporter.warn(new IOMessageImpl("Primary keys over multiple columns are not yet supported.", null));
        } else if (columns.size() == 1) {
            // create constraint, get property definition for original table
            // column (maybe could use index column, too)
            type.setConstraint(new eu.esdihumboldt.hale.common.schema.model.constraint.type.PrimaryKey(Collections.<QName>singletonList(getOrCreateProperty(schema, type, table.getColumn(columns.get(0).getName()), overallNamespace, namespace, types, connection, reporter, catalog).getName())));
        }
    }
    // TODO validation of constraints? Most are about several instances (i.
    // e. UNIQUE)
    types.addType(type);
    return type;
}
Also used : QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PrimaryKey(schemacrawler.schema.PrimaryKey) SQLQuery(eu.esdihumboldt.hale.io.jdbc.constraints.SQLQuery) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) SchemaCrawlerException(schemacrawler.schemacrawler.SchemaCrawlerException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) IndexColumn(schemacrawler.schema.IndexColumn) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) DatabaseTable(eu.esdihumboldt.hale.io.jdbc.constraints.DatabaseTable)

Aggregations

IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)1 DatabaseTable (eu.esdihumboldt.hale.io.jdbc.constraints.DatabaseTable)1 SQLQuery (eu.esdihumboldt.hale.io.jdbc.constraints.SQLQuery)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 QName (javax.xml.namespace.QName)1 IndexColumn (schemacrawler.schema.IndexColumn)1 PrimaryKey (schemacrawler.schema.PrimaryKey)1 SchemaCrawlerException (schemacrawler.schemacrawler.SchemaCrawlerException)1