Search in sources :

Example 1 with SQLQuery

use of eu.esdihumboldt.hale.io.jdbc.constraints.SQLQuery in project hale by halestudio.

the class SQLSchemaReader method addTableType.

/**
 * Create the type definition for a query.
 *
 * @param query the SQL query
 * @param namespace the namespace for the type
 * @param types the schema to add the type to
 * @param connection the database connection
 * @param reporter the reporter
 * @param typename the name to use for the type
 *
 * @return the type definition for the given table
 */
private TypeDefinition addTableType(String query, String namespace, DefaultSchema types, Connection connection, IOReporter reporter, String typename) {
    QName typeName = new QName(namespace, unquote(typename));
    // check for existing type
    TypeDefinition existingType = types.getType(typeName);
    if (existingType != null)
        return existingType;
    // create new type
    DefaultTypeDefinition type = new DefaultTypeDefinition(typeName);
    // set SQL query as description
    type.setDescription(query);
    type.setConstraint(new SQLQuery(query));
    // configure type
    type.setConstraint(MappableFlag.ENABLED);
    type.setConstraint(MappingRelevantFlag.ENABLED);
    type.setConstraint(HasValueFlag.DISABLED);
    types.addType(type);
    return type;
}
Also used : DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) QName(javax.xml.namespace.QName) SQLQuery(eu.esdihumboldt.hale.io.jdbc.constraints.SQLQuery) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 2 with SQLQuery

use of eu.esdihumboldt.hale.io.jdbc.constraints.SQLQuery 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)

Example 3 with SQLQuery

use of eu.esdihumboldt.hale.io.jdbc.constraints.SQLQuery in project hale by halestudio.

the class SQLQueryFactory method restore.

@Override
public SQLQuery restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
    String query;
    Text text = value.as(Text.class);
    if (text != null) {
        query = text.getText();
    } else {
        query = value.as(String.class);
    }
    if (query != null) {
        return new SQLQuery(query);
    }
    return SQLQuery.NO_QUERY;
}
Also used : Text(eu.esdihumboldt.hale.common.core.io.Text) SQLQuery(eu.esdihumboldt.hale.io.jdbc.constraints.SQLQuery)

Aggregations

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