internal void HandleRelation(XmlElement node, bool fNested)
{
string strName;
string parentName;
string childName;
string[] parentNames;
string[] childNames;
string value;
bool fCreateConstraints = false; //if we have a relation,
//we do not have constraints
DataRelationCollection rels = _ds.Relations;
DataRelation relation;
DataColumn[] parentKey;
DataColumn[] childKey;
DataTable parent;
DataTable child;
int keyLength;
strName = XmlConvert.DecodeName(node.GetAttribute(Keywords.NAME));
for (int i = 0; i < rels.Count; ++i)
{
if (string.Equals(rels[i].RelationName, strName, StringComparison.Ordinal))
return;
}
parentName = node.GetAttribute(Keywords.MSD_PARENT, Keywords.MSDNS);
if (parentName == null || parentName.Length == 0)
throw ExceptionBuilder.RelationParentNameMissing(strName);
parentName = XmlConvert.DecodeName(parentName);
childName = node.GetAttribute(Keywords.MSD_CHILD, Keywords.MSDNS);
if (childName == null || childName.Length == 0)
throw ExceptionBuilder.RelationChildNameMissing(strName);
childName = XmlConvert.DecodeName(childName);
value = node.GetAttribute(Keywords.MSD_PARENTKEY, Keywords.MSDNS);
if (value == null || value.Length == 0)
throw ExceptionBuilder.RelationTableKeyMissing(strName);
parentNames = value.TrimEnd(null).Split(new char[] { Keywords.MSD_KEYFIELDSEP, Keywords.MSD_KEYFIELDOLDSEP });
value = node.GetAttribute(Keywords.MSD_CHILDKEY, Keywords.MSDNS);
if (value == null || value.Length == 0)
throw ExceptionBuilder.RelationChildKeyMissing(strName);
childNames = value.TrimEnd(null).Split(new char[] { Keywords.MSD_KEYFIELDSEP, Keywords.MSD_KEYFIELDOLDSEP });
keyLength = parentNames.Length;
if (keyLength != childNames.Length)
throw ExceptionBuilder.MismatchKeyLength();
parentKey = new DataColumn[keyLength];
childKey = new DataColumn[keyLength];
string parentNs = node.GetAttribute(Keywords.MSD_PARENTTABLENS, Keywords.MSDNS);
string childNs = node.GetAttribute(Keywords.MSD_CHILDTABLENS, Keywords.MSDNS);
parent = _ds.Tables.GetTableSmart(parentName, parentNs);
if (parent == null)
throw ExceptionBuilder.ElementTypeNotFound(parentName);
child = _ds.Tables.GetTableSmart(childName, childNs);
if (child == null)
throw ExceptionBuilder.ElementTypeNotFound(childName);
for (int i = 0; i < keyLength; i++)
{
parentKey[i] = parent.Columns[XmlConvert.DecodeName(parentNames[i])];
if (parentKey[i] == null)
throw ExceptionBuilder.ElementTypeNotFound(parentNames[i]);
childKey[i] = child.Columns[XmlConvert.DecodeName(childNames[i])];
if (childKey[i] == null)
throw ExceptionBuilder.ElementTypeNotFound(childNames[i]);
}
relation = new DataRelation(strName, parentKey, childKey, fCreateConstraints);
relation.Nested = fNested;
SetExtProperties(relation, node.Attributes);
_ds.Relations.Add(relation);
if (FromInference && relation.Nested)
{
_tableDictionary[relation.ParentTable].Add(relation.ChildTable);
}
}