Beispiel #1
0
        /// <summary>
        /// 得到写的路由
        /// </summary>
        /// <param name="save"></param>
        /// <returns></returns>
        public virtual void SetRouteSaveInfo(SaveInfo save)
        {
            if (string.IsNullOrEmpty(save.Object.RouteName) || !DbRoutes.ContainsKey(save.Object.RouteName))
            {
                return;
            }
            var dbRoute = DbRoutes[save.Object.RouteName];

            if (dbRoute.GetSaveShardingHandle != null)
            {
                var sharding = dbRoute.GetSaveShardingHandle(save.Entity);
                if (sharding != null)
                {
                    save.SetDataBase = string.IsNullOrWhiteSpace(sharding.SetDataBase)? sharding.SetDataBase: sharding.SetDataBase.ToLower();
                    save.TableIndex  = string.IsNullOrWhiteSpace(sharding.TableIndex) ? sharding.TableIndex : sharding.TableIndex.ToLower();
                    return;
                }
            }
            if (dbRoute.Rules == null || dbRoute.Rules.Count == 0 || dbRoute.Shardings == null ||
                dbRoute.Shardings.Count == 0)
            {
                return;
            }
            var rules = dbRoute.Rules.Where(it => it.IsSave);

            foreach (var rule in rules)
            {
                var valueObj = save.Entity.GetProperty(rule.PropertyName);
                if (valueObj == null)
                {
                    continue;
                }
                var value = rule.IsHash
                    ? GenerateLongId(valueObj.ToString())
                    : (long)valueObj;
                value = Math.Abs(value);
                var shardings = GetShardings(dbRoute, rule.PropertyName, value, true);
                if (shardings == null || shardings.Count == 0)
                {
                    continue;
                }
                var sharding = shardings.FirstOrDefault(it => it.IsWrite);
                if (sharding == null)
                {
                    continue;
                }
                save.SetDataBase = string.IsNullOrWhiteSpace(sharding.SetDataBase) ? sharding.SetDataBase : sharding.SetDataBase.ToLower();
                save.TableIndex  = string.IsNullOrWhiteSpace(sharding.TableIndex) ? sharding.TableIndex : sharding.TableIndex.ToLower();
                break;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 得到读的路由
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public virtual IList <QueryInfo> GetRouteQueries(QueryInfo query)
        {
            if (string.IsNullOrEmpty(query.Object.RouteName) || !DbRoutes.ContainsKey(query.Object.RouteName))
            {
                return(null);
            }
            var dbRoute = DbRoutes[query.Object.RouteName];

            query.DbRoute = dbRoute;
            var handleQueries = GetHandleQueries(dbRoute, query);

            if (handleQueries != null && handleQueries.Count > 0)
            {
                return(handleQueries);
            }
            if (dbRoute.Rules == null || dbRoute.Rules.Count == 0 || dbRoute.Shardings == null || dbRoute.Shardings.Count == 0)
            {
                return(null);
            }
            var names = query.GetRouteParameters(query.WhereExp);

            if (names != null && names.Count > 0)
            {
                foreach (var rule in dbRoute.Rules)
                {
                    if (!names.ContainsKey(rule.PropertyName))
                    {
                        continue;
                    }
                    var paramterValues = names[rule.PropertyName];
                    var result         = new List <QueryInfo>();
                    foreach (var paramterValue in paramterValues)
                    {
                        if (paramterValue is Array)
                        {
                            var arr = paramterValue as Array;
                            foreach (var ar in arr)
                            {
                                if (ar == null)
                                {
                                    continue;
                                }
                                var value     = rule.IsHash ? GenerateLongId(ar.ToString()) : ar;
                                var shardings = GetShardings(dbRoute, rule.PropertyName, value, false);
                                if (shardings == null || shardings.Count == 0)
                                {
                                    continue;
                                }
                                foreach (var sharding in shardings)
                                {
                                    var cloneQuery = GetQuery(dbRoute, query, sharding);
                                    if (cloneQuery != null &&
                                        result.Count(
                                            it =>
                                            it.GetDataBase == cloneQuery.GetDataBase &&
                                            it.GetTableName == cloneQuery.GetTableName) == 0)
                                    {
                                        result.Add(cloneQuery);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (paramterValue == null)
                            {
                                continue;
                            }
                            var value     = rule.IsHash ? GenerateLongId(paramterValue.ToString()) : paramterValue;
                            var shardings = GetShardings(dbRoute, rule.PropertyName, value, false);
                            if (shardings == null || shardings.Count == 0)
                            {
                                continue;
                            }
                            AppendQueryByShardings(result, dbRoute, shardings, query);
                        }
                    }
                    if (result.Count == 1)
                    {
                        result[0].PageIndex = query.PageIndex;
                        result[0].PageSize  = query.PageSize;
                    }
                    else if (result.Count > 1 && dbRoute.TopCount > 0 && (query.PageSize + query.PageIndex * query.PageSize) > dbRoute.TopCount)
                    {
                        throw new LimitCountOverflowException(string.Format("Limit Count Is {0}", dbRoute.TopCount));
                    }
                    if (result.Count > 0)
                    {
                        return(result);
                    }
                }
            }
            if (dbRoute.TopCount > 0 && (query.PageSize + query.PageIndex * query.PageSize) > dbRoute.TopCount)
            {
                throw new LimitCountOverflowException(string.Format("Limit Count Is {0}", dbRoute.TopCount));
            }

            return(GetAllQueries(dbRoute, query));
        }