Пример #1
0
        /// <summary>
        ///     Gets the data from a related table in a ManyToMany relation.
        /// </summary>
        /// <param name="relatedTableType">Type of the related entity</param>
        /// <param name="intermediaryRelatedTableType">Type of the intermediary table</param>
        /// <returns>TableMetadata array which contains the results</returns>
        protected Array GetRelatedTableData(Type relatedTableType, Type intermediaryRelatedTableType)
        {
            PersistentObject        persistent = null;
            ManyToManyTableRelation relation   = null;
            DataSet ds = null;

            try
            {
                //create a instance of the table so we can check the relations between out table and this
                TableMetadata relatedTable      = (TableMetadata)Activator.CreateInstance(relatedTableType);
                TableMetadata intermediateTable = (TableMetadata)Activator.CreateInstance(intermediaryRelatedTableType);

                object primaryKeyValue = GetPrimaryKeyField().fieldValue;

                //first check the fieldValue of the primary key
                if (GetPrimaryKeyField().fieldValue == null)
                {
                    throw new InvalidOperationException("The primary key does not have a fieldValue");
                }

                //get the relation
                foreach (TableRelation var in Relations)
                {
                    if (var is ManyToManyTableRelation)
                    {
                        if (var.RelatedTableName == relatedTable.TableName)
                        {
                            relation = (ManyToManyTableRelation)var;
                            break;
                        }
                    }
                }

                //check if we got the relation
                if (relation == null)
                {
                    throw new ArgumentException("A relation cannot be found between the tables");
                }

                //we have the relation between the 2 tables.
                persistent = new PersistentObject(this);

                QueryCriteria qcThis = new QueryCriteria(TableName, GetPrimaryKeyField());
                qcThis.Add(CriteriaOperator.Equality, GetPrimaryKeyField(), primaryKeyValue);

                //generate here the inner join
                QueryCriteria qcIntermediaryTable = new QueryCriteria(intermediateTable.TableName, intermediateTable.GetField(relation.IntermediaryKeyFieldFromParentTable));

                QueryCriteria qcChild = new QueryCriteria(relatedTable);

                qcThis.AddJoin(JoinType.Inner, TableName, GetPrimaryKeyField(), intermediateTable.TableName,
                               intermediateTable.GetField(relation.IntermediaryKeyFieldFromParentTable), qcIntermediaryTable);

                qcThis.AddJoin(JoinType.Inner, intermediateTable.TableName, intermediateTable.GetField(relation.IntermediaryKeyFieldFromChildTable), relatedTable.TableName,
                               relatedTable.GetField(relation.IntermediaryKeyFieldFromChildTable), qcChild);

                ds = persistent.GetDataSet(qcThis);

                Array data = Array.CreateInstance(relatedTableType, ds.Tables[0].Rows.Count);

                for (int i = 0; i < data.Length; i++)
                {
                    object instance = Activator.CreateInstance(relatedTableType);
                    data.SetValue(instance, i);

                    //loop thru dataset
                    for (int j = START_INDEX_FIELD; j < ds.Tables[0].Columns.Count; j++)
                    {
                        Type tp = data.GetValue(i).GetType();

                        object[] args = new[] { ds.Tables[0].Columns[j].ColumnName, ds.Tables[0].Rows[i][j] };
                        tp.InvokeMember("SetFieldValue", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, instance, args);
                    }
                }

                return(data);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (ds != null)
                {
                    ds.Dispose();
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Returns data from a related table in Parent -> Child relationship. This is
        ///     the underlying implementation of the generated GetXXX tables.
        /// </summary>
        /// <param name="childTableType">Type of the related entity</param>
        /// <returns>Array with the result</returns>
        protected TableMetadata[] GetRelatedTableData(Type childTableType)
        {
            PersistentObject persistent = null;

            TableRelation relation = 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);

                //first check the fieldValue of the primary key
                if (GetPrimaryKeyField().fieldValue == null)
                {
                    throw new InvalidOperationException("The primary key does not have a fieldValue");
                }

                //get the relation
                foreach (TableRelation var in Relations)
                {
                    if (var.RelatedTableName == childTable.TableName)
                    {
                        relation = var;
                        break;
                    }
                }

                if (relation is ChildTableRelation)
                {
                    return(GetChildRelatedTableData(childTableType, (ChildTableRelation)relation));
                }

                //check if we got the relation
                if (relation == null)
                {
                    throw new ArgumentException("A relation cannot be found between the tables");
                }

                //we have the relation between the 2 tables.
                persistent = new PersistentObject(this);

                TableMetadata[] data = null;

                if (relation.RelationCardinality == TableRelationCardinality.OneToOne)
                {
                    ParentTableRelation pr = (ParentTableRelation)(relation);
                    //take the fk value
                    data = (TableMetadata[])persistent.GetTableMetadata(relation.RelatedTableName, childTableType, GetField(pr.ForeignKeyName).fieldValue);
                }
                else
                {
                    //take the pk value
                    data = (TableMetadata[])persistent.GetTableMetadata(relation.RelatedTableName, childTableType, GetPrimaryKeyField().fieldValue);
                }

                return(data);
            }
            catch
            {
                throw;
            }
        }