internal void HandleKeyref(XmlSchemaKeyref keyref)
{
string refer = XmlConvert.DecodeName(keyref.Refer.Name); // check here!!!
string name = XmlConvert.DecodeName(keyref.Name);
name = GetStringAttribute(keyref, "ConstraintName", /*default:*/ name);
// we do not process key defined outside the current node
string tableName = GetTableName(keyref);
string tableNs = GetMsdataAttribute(keyref, Keywords.MSD_TABLENS);
DataTable table = _ds.Tables.GetTableSmart(tableName, tableNs);
if (table == null)
return;
if (refer == null || refer.Length == 0)
throw ExceptionBuilder.MissingRefer(name);
ConstraintTable key = (ConstraintTable)_constraintNodes[refer];
if (key == null)
{
throw ExceptionBuilder.InvalidKey(name);
}
DataColumn[] pKey = BuildKey(key.constraint, key.table);
DataColumn[] fKey = BuildKey(keyref, table);
ForeignKeyConstraint fkc = null;
if (GetBooleanAttribute(keyref, Keywords.MSD_CONSTRAINTONLY, /*default:*/ false))
{
int iExisting = fKey[0].Table.Constraints.InternalIndexOf(name);
if (iExisting > -1)
{
if (fKey[0].Table.Constraints[iExisting].ConstraintName != name)
iExisting = -1;
}
if (iExisting < 0)
{
fkc = new ForeignKeyConstraint(name, pKey, fKey);
fKey[0].Table.Constraints.Add(fkc);
}
}
else
{
string relName = XmlConvert.DecodeName(GetStringAttribute(keyref, Keywords.MSD_RELATIONNAME, keyref.Name));
if (relName == null || relName.Length == 0)
relName = name;
int iExisting = fKey[0].Table.DataSet.Relations.InternalIndexOf(relName);
if (iExisting > -1)
{
if (fKey[0].Table.DataSet.Relations[iExisting].RelationName != relName)
iExisting = -1;
}
DataRelation relation = null;
if (iExisting < 0)
{
relation = new DataRelation(relName, pKey, fKey);
SetExtProperties(relation, keyref.UnhandledAttributes);
pKey[0].Table.DataSet.Relations.Add(relation);
if (FromInference && relation.Nested)
{
if (_tableDictionary.ContainsKey(relation.ParentTable))
{
_tableDictionary[relation.ParentTable].Add(relation.ChildTable);
}
}
fkc = relation.ChildKeyConstraint;
fkc.ConstraintName = name;
}
else
{
relation = fKey[0].Table.DataSet.Relations[iExisting];
}
if (GetBooleanAttribute(keyref, Keywords.MSD_ISNESTED, /*default:*/ false))
{
relation.Nested = true;
}
}
string acceptRejectRule = GetMsdataAttribute(keyref, Keywords.MSD_ACCEPTREJECTRULE);
string updateRule = GetMsdataAttribute(keyref, Keywords.MSD_UPDATERULE);
string deleteRule = GetMsdataAttribute(keyref, Keywords.MSD_DELETERULE);
if (fkc != null)
{
if (acceptRejectRule != null)
fkc.AcceptRejectRule = TranslateAcceptRejectRule(acceptRejectRule);
if (updateRule != null)
fkc.UpdateRule = TranslateRule(updateRule);
if (deleteRule != null)
fkc.DeleteRule = TranslateRule(deleteRule);
SetExtProperties(fkc, keyref.UnhandledAttributes);
}
}