use of eu.esdihumboldt.hale.io.jdbc.constraints.DatabaseTable 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;
}
use of eu.esdihumboldt.hale.io.jdbc.constraints.DatabaseTable in project hale by halestudio.
the class DatabaseTableFactory method restore.
@Override
public DatabaseTable restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
String schema = null;
String table = null;
boolean useQuote = true;
ValueProperties props = value.as(ValueProperties.class);
if (props != null) {
schema = props.getSafe(NAME_SCHEMA).as(String.class);
table = props.getSafe(NAME_TABLE).as(String.class);
useQuote = props.getSafe(USE_QUOTE).as(Boolean.class);
}
if (table == null) {
throw new IllegalStateException("Database table constraint w/o table name");
}
return new DatabaseTable(schema, table, useQuote);
}
Aggregations