public static void SaveAllContextOnThread()
        {
            if (!IsExistContextStackOnThread())
            {
                Thread.SetData(_slot, new Stack <ContextStack>());
            }
            ContextStack contextStack = new ContextStack();

            if (ConditionBeanContext.IsExistConditionBeanOnThread())
            {
                contextStack.ConditionBean = ConditionBeanContext.GetConditionBeanOnThread();
            }
            if (OutsideSqlContext.IsExistOutsideSqlContextOnThread())
            {
                contextStack.OutsideSqlContext = OutsideSqlContext.GetOutsideSqlContextOnThread();
            }
            if (FetchNarrowingBeanContext.IsExistFetchNarrowingBeanOnThread())
            {
                contextStack.FetchNarrowingBean = FetchNarrowingBeanContext.GetFetchNarrowingBeanOnThread();
            }
            if (InternalMapContext.IsExistInternalMapOnThread())
            {
                contextStack.InternalMap = InternalMapContext.GetInternalMap();
            }
            GetContextStackOnThread().Push(contextStack);
        }
        protected bool IsUseFetchNarrowingResultSetWrapper()
        {
            FetchNarrowingBean fnbean = FetchNarrowingBeanContext.GetFetchNarrowingBeanOnThread();

            // for safety result
            if (fnbean != null && fnbean.SafetyMaxResultSize > 0)
            {
                return(true);
            }

            // for unsupported paging (ConditionBean)
            if (fnbean != null && fnbean.IsFetchNarrowingEffective)
            {
                // for unsupported paging (Database)
                if (fnbean.IsFetchNarrowingSkipStartIndexEffective || fnbean.IsFetchNarrowingLoopCountEffective)
                {
                    return(true);
                }

                // for auto paging (OutsideSql)
                if (OutsideSqlContext.IsExistOutsideSqlContextOnThread())
                {
                    OutsideSqlContext outsideSqlContext = OutsideSqlContext.GetOutsideSqlContextOnThread();
                    if (outsideSqlContext.IsOffsetByCursorForcedly || outsideSqlContext.IsLimitByCursorForcedly)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
 // ===============================================================================
 //                                                                  Context Helper
 //                                                                  ==============
 protected void InitializeContext()
 {
     if (ConditionBeanContext.IsExistConditionBeanOnThread() ||
         OutsideSqlContext.IsExistOutsideSqlContextOnThread() ||
         FetchNarrowingBeanContext.IsExistFetchNarrowingBeanOnThread() ||
         InternalMapContext.IsExistInternalMapOnThread())      // means recursive invoking
     {
         SaveAllContextOnThread();
     }
     ClearAllCurrentContext();
 }
 protected void CloseContext()
 {
     if (FetchNarrowingBeanContext.IsExistFetchNarrowingBeanOnThread())
     {
         // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
         // Because there is possible that fetch narrowing has been ignored for manualPaging of outsideSql.
         // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
         FetchNarrowingBeanContext.GetFetchNarrowingBeanOnThread().RestoreIgnoredFetchNarrowing();
     }
     ClearAllCurrentContext();
     RestoreAllContextOnThreadIfExists();
 }
        protected void SetupOutsideSqlFetchNarrowingBean(Object pmb, OutsideSqlOption option)
        {
            if (pmb == null || !FetchNarrowingBeanContext.IsTheTypeFetchNarrowingBean(pmb.GetType()))
            {
                return;
            }
            FetchNarrowingBean fetchNarrowingBean = (FetchNarrowingBean)pmb;

            if (option.IsManualPaging)
            {
                fetchNarrowingBean.IgnoreFetchNarrowing();
            }
            FetchNarrowingBeanContext.SetFetchNarrowingBeanOnThread(fetchNarrowingBean);
        }
        // -------------------------------------------------
        //                                     ConditionBean
        //                                     -------------
        protected ConditionBean PreprocessConditionBean(IMethodInvocation invocation)
        {
            OutsideSqlContext outsideSqlContext = GetOutsideSqlContext();

            if (outsideSqlContext != null)
            {
                return(null); // Because it has already finished setting up fetchNarrowingBean for outsideSql here.
            }

            ConditionBean cb = null;

            {
                Object[] args = invocation.Arguments;
                if (args == null || !(args.Length >= 1))
                {
                    return(null);
                }
                Object arg0 = args[0];
                if (arg0 == null)
                {
                    return(null);
                }

                if (!ConditionBeanContext.IsTheArgumentConditionBean(arg0))  // The argument is not condition-bean...
                {
                    if (FetchNarrowingBeanContext.IsTheArgumentFetchNarrowingBean(arg0) && !IsSelectCountIgnoreFetchScopeMethod(invocation))
                    {
                        // Fetch-narrowing-bean and Not select count!
                        FetchNarrowingBeanContext.SetFetchNarrowingBeanOnThread((FetchNarrowingBean)arg0);
                    }
                    return(null);
                }

                cb = (ConditionBean)arg0;
            }

            if (IsSelectCountIgnoreFetchScopeMethod(invocation))
            {
                cb.xsetupSelectCountIgnoreFetchScope();
            }
            else
            {
                FetchNarrowingBeanContext.SetFetchNarrowingBeanOnThread(cb);
            }

            ConditionBeanContext.SetConditionBeanOnThread(cb);
            return(cb);
        }
        public static void RestoreAllContextOnThreadIfExists()
        {
            if (!IsExistContextStackOnThread())
            {
                return;
            }
            Stack <ContextStack> stackOnThread = GetContextStackOnThread();

            if (stackOnThread.Count == 0)
            {
                ClearContextStackOnThread();
                return;
            }
            ContextStack  contextStack = stackOnThread.Pop();
            ConditionBean cb           = contextStack.ConditionBean;

            if (cb != null)
            {
                ConditionBeanContext.SetConditionBeanOnThread(cb);
            }
            OutsideSqlContext outsideSqlContext = contextStack.OutsideSqlContext;

            if (outsideSqlContext != null)
            {
                OutsideSqlContext.SetOutsideSqlContextOnThread(outsideSqlContext);
            }
            FetchNarrowingBean fetchNarrowingBean = contextStack.FetchNarrowingBean;

            if (fetchNarrowingBean != null)
            {
                FetchNarrowingBeanContext.SetFetchNarrowingBeanOnThread(fetchNarrowingBean);
            }
            IDictionary <String, Object> internalMap = contextStack.InternalMap;

            if (internalMap != null)
            {
                InternalMapContext.ClearInternalMapOnThread();
                foreach (String key in internalMap.Keys)
                {
                    Object value = internalMap[key];
                    InternalMapContext.SetObject(key, value);
                }
            }
        }
 public static void ClearAllCurrentContext()
 {
     if (ConditionBeanContext.IsExistConditionBeanOnThread())
     {
         ConditionBeanContext.ClearConditionBeanOnThread();
     }
     if (OutsideSqlContext.IsExistOutsideSqlContextOnThread())
     {
         OutsideSqlContext.ClearOutsideSqlContextOnThread();
     }
     if (FetchNarrowingBeanContext.IsExistFetchNarrowingBeanOnThread())
     {
         FetchNarrowingBeanContext.ClearFetchNarrowingBeanOnThread();
     }
     if (InternalMapContext.IsExistInternalMapOnThread())
     {
         InternalMapContext.ClearInternalMapOnThread();
     }
 }
        public IDataReader CreateDataReader(IDataSource dataSource, IDbCommand cmd)
        {
            IDataReader dataReader = ExecuteReader(dataSource, cmd);

            if (!IsUseFetchNarrowingResultSetWrapper())
            {
                return(dataReader);
            }
            FetchNarrowingBean             fnbean  = FetchNarrowingBeanContext.GetFetchNarrowingBeanOnThread();
            FetchNarrowingResultSetWrapper wrapper = null;

            if (OutsideSqlContext.IsExistOutsideSqlContextOnThread())
            {
                OutsideSqlContext outsideSqlContext = OutsideSqlContext.GetOutsideSqlContextOnThread();
                wrapper = new FetchNarrowingResultSetWrapper(dataReader, fnbean
                                                             , outsideSqlContext.IsOffsetByCursorForcedly
                                                             , outsideSqlContext.IsLimitByCursorForcedly);
            }
            else
            {
                wrapper = new FetchNarrowingResultSetWrapper(dataReader, fnbean, false, false);
            }
            return(wrapper);
        }