protected SoodaRelationTable GetSoodaRelationTable()
 {
     if (relationTable == null)
     {
         relationTable = transaction.GetRelationTable(relationType);
     }
     return(relationTable);
 }
        public override void Remove(object obj)
        {
            SoodaObject        so  = (SoodaObject)obj;
            object             pk  = so.GetPrimaryKeyValue();
            SoodaRelationTable rel = this.GetSoodaRelationTable();

            if (masterColumn == 0)
            {
                rel.Remove(pk, this.masterValue);
            }
            else
            {
                rel.Remove(this.masterValue, pk);
            }
            this.InternalRemove(so);
        }
        public override int Add(object obj)
        {
            SoodaObject        so  = (SoodaObject)obj;
            object             pk  = so.GetPrimaryKeyValue();
            SoodaRelationTable rel = this.GetSoodaRelationTable();

            if (masterColumn == 0)
            {
                rel.Add(pk, this.masterValue);
            }
            else
            {
                rel.Add(this.masterValue, pk);
            }
            return(this.InternalAdd(so));
        }
        protected override void LoadData()
        {
            bool   useCache = false; //transaction.CachingPolicy.ShouldCacheRelation(relationInfo, classInfo);
            string cacheKey = null;

            items      = new Dictionary <SoodaObject, int>();
            itemsArray = new List <SoodaObject>();

            if (useCache)
            {
                if (!transaction.HasBeenPrecommitted(relationInfo) && !transaction.HasBeenPrecommitted(classInfo))
                {
                    cacheKey = relationInfo.Name + " where " + relationInfo.Table.Fields[1 - masterColumn].Name + " = " + masterValue;
                }
                else
                {
                    logger.Debug("Cache miss. Cannot use cache for {0} where {1} = {2} because objects have been precommitted.", relationInfo.Name, relationInfo.Table.Fields[1 - masterColumn].Name, masterValue);
                    SoodaStatistics.Global.RegisterCollectionCacheMiss();
                    transaction.Statistics.RegisterCollectionCacheMiss();
                }
            }

            IEnumerable keysCollection = transaction.LoadCollectionFromCache(cacheKey, logger);

            if (keysCollection != null)
            {
                foreach (object o in keysCollection)
                {
                    SoodaObject obj = _factory.GetRef(transaction, o);
                    // this binds to cache
                    obj.EnsureFieldsInited();
                    InternalAdd(obj);
                }
            }
            else
            {
                LoadDataFromReader();
                if (cacheKey != null)
                {
                    TimeSpan expirationTimeout;
                    bool     slidingExpiration;

                    if (transaction.CachingPolicy.GetExpirationTimeout(
                            relationInfo, classInfo, itemsArray.Count, out expirationTimeout, out slidingExpiration))
                    {
                        transaction.StoreCollectionInCache(cacheKey, classInfo, itemsArray, new string[] { relationInfo.Name }, true, expirationTimeout, slidingExpiration);
                    }
                }
            }

            SoodaRelationTable rel = GetSoodaRelationTable();

            rel.OnTupleChanged += new SoodaRelationTupleChanged(this.OnTupleChanged);
            if (rel.TupleCount != 0)
            {
                SoodaRelationTable.Tuple[] tuples = rel.Tuples;
                int count = rel.TupleCount;

                if (masterColumn == 1)
                {
                    for (int i = 0; i < count; ++i)
                    {
                        if (tuples[i].ref1.Equals(masterValue))
                        {
                            SoodaObject obj = _factory.GetRef(transaction, tuples[i].ref2);
                            if (tuples[i].tupleMode > 0)
                            {
                                InternalAdd(obj);
                            }
                            else
                            {
                                InternalRemove(obj);
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < count; ++i)
                    {
                        if (tuples[i].ref2.Equals(masterValue))
                        {
                            SoodaObject obj = _factory.GetRef(transaction, tuples[i].ref1);
                            if (tuples[i].tupleMode > 0)
                            {
                                InternalAdd(obj);
                            }
                            else
                            {
                                InternalRemove(obj);
                            }
                        }
                    }
                }
            }
        }
 protected SoodaRelationTable GetSoodaRelationTable()
 {
     if (relationTable == null)
     {
         relationTable = transaction.GetRelationTable(relationType);
     }
     return relationTable;
 }