NamedNodeMap attrs = node.getAttributes();
String name = attrs.getNamedItem("name").getNodeValue();
String typeName = attrs.getNamedItem("type").getNodeValue();
ConstraintType type = ConstraintType.valueOf(typeName);
if (type == null) {
throw this.m_compiler.new VoltCompilerException("Invalid constraint type '" + typeName + "'");
}
// The constraint is backed by an index, therefore we need to create it
// TODO: We need to be able to use indexes for foreign keys. I am purposely
// leaving those out right now because HSQLDB just makes too many of them.
Constraint catalog_const = null;
if (attrs.getNamedItem("index") != null) {
String indexName = attrs.getNamedItem("index") .getNodeValue();
Index catalog_index = indexMap.get(indexName);
if (catalog_index != null) {
// If this is a foreign key, then we *do not* want to include an index for it
// since we don't actually do anything to enforce these constraints
if (type == ConstraintType.FOREIGN_KEY) {
boolean ret = table.getIndexes().remove(catalog_index);
LOG.debug("Removing foreign key index " + catalog_index.fullName() + " [" + ret + "]");
indexMap.remove(indexName);
catalog_index = null;
}
else {
// if the constraint name contains index type hints, exercise them (giant hack)
String constraintNameNoCase = name.toLowerCase();
if (constraintNameNoCase.contains("tree"))
catalog_index.setType(IndexType.BALANCED_TREE.getValue());
if (constraintNameNoCase.contains("array"))
catalog_index.setType(IndexType.ARRAY.getValue());
}
}
catalog_const = table.getConstraints().add(name);
if (catalog_index != null) {
catalog_const.setIndex(catalog_index);
catalog_index.setUnique(type == ConstraintType.UNIQUE || type == ConstraintType.PRIMARY_KEY);
}
} else {
catalog_const = table.getConstraints().add(name);
}
catalog_const.setType(type.getValue());
// Foreign Keys
if (type == ConstraintType.FOREIGN_KEY) {
String fkey_table_name = attrs.getNamedItem("foreignkeytable").getNodeValue();
Table catalog_fkey_tbl = ((Database)table.getParent()).getTables().getIgnoreCase(fkey_table_name);