/// <summary> /// Gets the child related data. /// </summary> /// <param name="childTableType">Type of the child table.</param> /// <param name="relation">The relation.</param> /// <returns></returns> protected TableMetadata[] GetChildRelatedTableData(Type childTableType, ChildTableRelation relation) { PersistentObject persistent = null; try { //create a instance of the table so we can check the relations between out table and this TableMetadata childTable = (TableMetadata)Activator.CreateInstance(childTableType); object value = GetField(relation.ForeignKeyName).fieldValue; //we have the relation between the 2 tables. persistent = new PersistentObject(this); TableMetadata[] data = (TableMetadata[])persistent.GetTableMetadata(relation.RelatedTableName, childTableType, value); return(data); } catch { throw; } finally { if (persistent != null) { persistent.Dispose(); } } }
/// <summary> /// Returns a DataSet which contains data from the related table /// </summary> /// <param name="relatedTableName">Name of the related table</param> /// <param name="foreignKeyValue">Value of the foreign key</param> /// <returns>DataSet containing data from the related table</returns> public DataSet GetDataSet(string relatedTableName, object foreignKeyValue) { SqlGenerator generator = new SqlGenerator(); DataSet ds = new DataSet(); ExecutionQuery selectQuery = new ExecutionQuery(); TableRelation[] relations = mappedObject.Relations; for (int i = 0; i < relations.Length; i++) { if (relations[i].RelatedTableName == relatedTableName.Trim()) { DatabaseField keyField; //check if we have a ParentRelation or a ChildRelation if (relations[i] is ParentTableRelation) { DatabaseField primaryKeyField = mappedObject.GetPrimaryKeyField(); //this is the parent so we select from the child table. keyField = new DatabaseField(primaryKeyField.fieldType, ((ParentTableRelation)relations[i]).ForeignKeyName, false, false, foreignKeyValue); } else { //child relation ChildTableRelation childRelation = (ChildTableRelation)relations[i]; //this is the child so get data from the parent keyField = new DatabaseField(mappedObject.GetPrimaryKeyField().fieldType, childRelation.RelatedTableKeyName, true, false, foreignKeyValue); } selectQuery = generator.GenerateSelectQuery(database, relations[i].RelatedTableName, keyField); break; } } if (selectQuery.Query == string.Empty) { throw new ArgumentException("Invalid relation name"); } //run the query in the associated context if (contextSession != null) { ds = execEngine.ExecuteDataSet(selectQuery); } else { using (ExecutionEngine e = new ExecutionEngine()) { ds = e.ExecuteDataSet(selectQuery); } } return(ds); }
/// <summary> /// Get data from a related table (doen't matter if parent of child) based /// on the relation name and the primary key's fieldValue from the related table. /// </summary> /// <param name="relatedTableName">The name of the related table class name</param> /// <param name="classType">Class type of the related TableMetadata entity</param> /// <param name="foreignKeyValue">Foreign key's fieldValue</param> /// <returns>TableMetadata array which contains the specified data </returns> public Array GetTableMetadata(string relatedTableName, Type classType, object foreignKeyValue) { ArrayList alList = null; SqlGenerator generator = new SqlGenerator(); try { ExecutionQuery selectQuery = new ExecutionQuery(); //hold the table's relations. TableRelation[] relations = mappedObject.Relations; //loop and get the relation for (int i = 0; i < relations.Length; i++) { if (relations[i].RelatedTableName == relatedTableName.Trim()) { DatabaseField keyField; //check if we habe a ParentRelation or a ChildRelation if (relations[i] is ParentTableRelation) { DatabaseField primaryKeyField = mappedObject.GetPrimaryKeyField(); //this is the parent so we select from the child table. keyField = new DatabaseField(primaryKeyField.fieldType, ((ParentTableRelation)relations[i]).ForeignKeyName, false, false, foreignKeyValue); } else { //child relation ChildTableRelation childRelation = (ChildTableRelation)relations[i]; //this is the child so get data from the parent keyField = new DatabaseField(mappedObject.GetPrimaryKeyField().fieldType, childRelation.RelatedTableKeyName, true, false, foreignKeyValue); } selectQuery = generator.GenerateSelectQuery(database, relations[i].RelatedTableName, keyField); break; } } if (selectQuery.Query == string.Empty) { throw new Exception("Invalid related table name"); } object tableMetadata = Activator.CreateInstance(classType); alList = MapDataReaderToTableMetadata(selectQuery, (TableMetadata)tableMetadata); Array array = Array.CreateInstance(classType, alList.Count); alList.CopyTo(array); return(array); } finally { if (alList != null) { alList.Clear(); } } }