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;
}
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;
}
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;
}
Aggregations