Ejemplo n.º 1
0
        /// <summary>
        /// 得到查询分片
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public virtual IList <ShardingInfo> GetQuerySharding(QueryInfo query)
        {
            var names = query.GetRouteParameters(query.WhereExp);

            if (names == null)
            {
                return(null);
            }
            var tableIndexs = GetQueryTableIndex(names);

            if (tableIndexs == null)
            {
                return(null);
            }
            var dataBaseIndex = GetQueryDataBaseIndex(names);
            var getDataBase   = string.Format("BeeantAccountRead{0}", dataBaseIndex);
            var result        = new List <ShardingInfo>();

            foreach (var tableIndex in tableIndexs)
            {
                result.Add(new ShardingInfo
                {
                    GetDataBase = getDataBase,
                    TableIndex  = tableIndex
                });
            }
            return(result);
        }
Ejemplo n.º 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));
        }