Esempio n. 1
0
        /// <summary>
        /// 得到查询
        /// </summary>
        /// <param name="dbRoute"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        protected virtual IList <QueryInfo> GetHandleQueries(DbRouteInfo dbRoute, QueryInfo query)
        {
            if (dbRoute.GetQueryShardingHandle == null)
            {
                return(null);
            }
            var shardings = dbRoute.GetQueryShardingHandle(query);

            if (shardings != null && shardings.Count > 0)
            {
                var result = new List <QueryInfo>();
                if (shardings.Count == 1)
                {
                    var sharding = shardings[0];
                    query.TableIndex = string.IsNullOrWhiteSpace(sharding.TableIndex)
                        ? sharding.TableIndex
                        : sharding.TableIndex.ToLower();
                    query.GetDataBase = string.IsNullOrWhiteSpace(sharding.GetDataBase)
                        ? sharding.GetDataBase
                        : sharding.GetDataBase.ToLower();
                    result.Add(query);
                }
                else
                {
                    MergeQueries(result, dbRoute, query, shardings);
                }
                return(result);
            }
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// 得到分片
        /// </summary>
        /// <returns></returns>
        protected virtual IList <ShardingInfo> GetShardings(DbRouteInfo dbRoute, string proertyName, object value, bool isSave)
        {
            var key = string.Format("{0}{1}", dbRoute.Name, proertyName);

            if (DbPropertyRoutes.ContainsKey(key))
            {
                var result    = new List <ShardingInfo>();
                var shardings = DbPropertyRoutes[key];
                foreach (var sharding in shardings)
                {
                    var shardProperty = sharding.ShardProperties.FirstOrDefault(it => it.PropertyName == proertyName);
                    var tableIndex    = shardProperty.GetTableIndex(value, sharding);
                    sharding.TableIndex = tableIndex;
                    var getDataBase = isSave ? sharding.GetDataBase : shardProperty.GetQueryDataBaseIndex(value, sharding);
                    var setDataBase = isSave? shardProperty.GetSaveDataBaseIndex(value, sharding) : sharding.SetDataBase;
                    if (tableIndex != null)
                    {
                        result.Add(new ShardingInfo
                        {
                            TableIndex    = tableIndex,
                            MaxTableIndex = sharding.MaxTableIndex,
                            SetDataBase   = setDataBase,
                            GetDataBase   = getDataBase,
                            IsWrite       = sharding.IsWrite
                        });
                    }
                }
                if (result.Count > 0)
                {
                    return(result);
                }
            }
            return(null);
        }
Esempio n. 3
0
 /// <summary>
 /// 得到查询
 /// </summary>
 /// <param name="dbRoute"></param>
 /// <param name="shardings"></param>
 /// <param name="query"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 protected virtual void AppendQueryByShardings(IList <QueryInfo> result, DbRouteInfo dbRoute, IList <ShardingInfo> shardings, QueryInfo query)
 {
     if (shardings.Count == 1)
     {
         var sharding = shardings[0];
         query.TableIndex = string.IsNullOrWhiteSpace(sharding.TableIndex)
             ? sharding.TableIndex
             : sharding.TableIndex.ToLower();
         query.GetDataBase = string.IsNullOrWhiteSpace(sharding.GetDataBase)
             ? sharding.GetDataBase
             : sharding.GetDataBase.ToLower();
         result.Add(query);
     }
     else
     {
         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);
             }
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 得到数据库信息
        /// </summary>
        /// <param name="dbRoutes"></param>
        /// <param name="doc"></param>
        /// <returns></returns>
        protected virtual void LoadDbRouteByXml(IDictionary <string, DbRouteInfo> dbRoutes, XmlDocument doc)
        {
            XmlNodeList nodes = doc.SelectNodes("/configuration/Persistence/XmlDbRoute/Info");

            if (nodes != null)
            {
                foreach (XmlNode node in nodes)
                {
                    var dbroute = new DbRouteInfo {
                        Rules = new List <RuleInfo>(), Shardings = new List <ShardingInfo>()
                    };
                    if (node.Attributes == null)
                    {
                        return;
                    }
                    dbroute.Name = node.Attributes["Name"].Value.ToLower();
                    if (node.Attributes["TopCount"] != null)
                    {
                        dbroute.TopCount = Convert.ToInt32(node.Attributes["TopCount"].Value);
                    }
                    if (node.Attributes["IsReturnAllShardings"] != null)
                    {
                        dbroute.IsReturnAllShardings = Convert.ToBoolean(node.Attributes["IsReturnAllShardings"].Value);
                    }
                    if (node.Attributes["IsMapTableAutoSharding"] != null)
                    {
                        dbroute.IsMapTableAutoSharding = Convert.ToBoolean(node.Attributes["IsMapTableAutoSharding"].Value);
                    }
                    if (node.Attributes["ClassName"] != null)
                    {
                        var obj = CreateClass(node.Attributes["ClassName"].Value);
                        if (obj != null)
                        {
                            if (node.Attributes["GetQueryShardingHandle"] != null)
                            {
                                dbroute.GetQueryShardingHandle = (Func <QueryInfo, IList <ShardingInfo> >)Delegate.CreateDelegate(typeof(Func <QueryInfo, IList <ShardingInfo> >), obj, node.Attributes["GetQueryShardingHandle"].Value);
                            }
                            if (node.Attributes["GetSaveShardingHandle"] != null)
                            {
                                dbroute.GetSaveShardingHandle = (Func <object, ShardingInfo>)Delegate.CreateDelegate(typeof(Func <object, ShardingInfo>), obj, node.Attributes["GetSaveShardingHandle"].Value);
                            }
                        }
                    }
                    LoadRulesByXmlNode(dbroute, node);
                    LoadShardingsByXmlNode(dbroute, node);
                    if (dbRoutes.ContainsKey(dbroute.Name))
                    {
                        dbRoutes.Remove(dbroute.Name);
                    }
                    dbRoutes.Add(dbroute.Name, dbroute);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 根据节点得到OrmDataBase
        /// </summary>
        /// <param name="dbRoute"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        protected virtual void LoadShardingsByXmlNode(DbRouteInfo dbRoute, XmlNode node)
        {
            var nodes = node.SelectNodes("Sharding");

            if (nodes == null)
            {
                return;
            }
            foreach (XmlNode nd in nodes)
            {
                var sharding = new ShardingInfo {
                    ShardProperties = new List <ShardPropertyInfo>()
                };
                sharding.GetDataBase = nd.Attributes["GetDataBase"] != null
                                           ? nd.Attributes["GetDataBase"].Value.ToLower()
                                           : "";
                sharding.SetDataBase = nd.Attributes["SetDataBase"] != null
                                           ? nd.Attributes["SetDataBase"].Value.ToLower()
                                           : "";
                sharding.TableIndex = nd.Attributes["TableIndex"] != null
                                           ? nd.Attributes["TableIndex"].Value.ToLower()
                                           : "";
                sharding.MaxTableIndex = nd.Attributes["MaxTableIndex"] != null
                                       ? nd.Attributes["MaxTableIndex"].Value.ToLower()
                                       : "";
                sharding.TableTag = nd.Attributes["TableTag"] != null
                                       ? nd.Attributes["TableTag"].Value.ToLower()
                                       : "";
                sharding.IsWrite = nd.Attributes["IsWrite"] == null || Convert.ToBoolean(nd.Attributes["IsWrite"].Value);
                if (nd.Attributes["TableStepType"] != null)
                {
                    sharding.TableStepType = (TableStepType)Enum.Parse(typeof(TableStepType), nd.Attributes["TableStepType"].Value);
                }
                if (nd.Attributes["TableStep"] != null)
                {
                    sharding.TableStep = Convert.ToInt32(nd.Attributes["TableStep"].Value);
                }
                if (nd.Attributes["GetDataBaseTableCount"] != null)
                {
                    sharding.GetDataBaseTableCount = Convert.ToInt32(nd.Attributes["GetDataBaseTableCount"].Value);
                }
                if (nd.Attributes["SetDataBaseTableCount"] != null)
                {
                    sharding.SetDataBaseTableCount = Convert.ToInt32(nd.Attributes["SetDataBaseTableCount"].Value);
                }
                LoadShardingPropertiesByXmlNode(sharding, nd);
                dbRoute.Shardings.Add(sharding);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// 合并查询
 /// </summary>
 /// <param name="result"></param>
 /// <param name="dbRoute"></param>
 /// <param name="query"></param>
 /// <param name="shardings"></param>
 /// <returns></returns>
 protected virtual void MergeQueries(IList <QueryInfo> result, DbRouteInfo dbRoute, QueryInfo query,
                                     IList <ShardingInfo> shardings)
 {
     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);
         }
     }
 }
Esempio n. 7
0
        /// <summary>
        /// 根据节点得到OrmDataBase
        /// </summary>
        /// <param name="dbRoute"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        protected virtual void LoadRulesByXmlNode(DbRouteInfo dbRoute, XmlNode node)
        {
            var nodes = node.SelectNodes("Rule");

            if (nodes == null)
            {
                return;
            }
            foreach (XmlNode nd in nodes)
            {
                var rule = new RuleInfo();
                rule.IsHash       = nd.Attributes["IsHash"] != null && Convert.ToBoolean(nd.Attributes["IsHash"].Value);
                rule.IsSave       = nd.Attributes["IsSave"] != null && Convert.ToBoolean(nd.Attributes["IsSave"].Value);
                rule.PropertyName = nd.Attributes["PropertyName"] != null ? nd.Attributes["PropertyName"].Value : "";
                dbRoute.Rules.Add(rule);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 得到查询
        /// </summary>
        /// <param name="dbRoute"></param>
        /// <param name="query"></param>
        /// <param name="sharding"></param>
        /// <returns></returns>
        protected virtual QueryInfo GetQuery(DbRouteInfo dbRoute, QueryInfo query, ShardingInfo sharding)
        {
            if (sharding == null)
            {
                return(null);
            }
            var result = new QueryInfo
            {
                DataCount     = query.DataCount,
                FromExp       = query.FromExp,
                GroupByExp    = query.GroupByExp,
                HavingExp     = query.HavingExp,
                IsDisinct     = query.IsDisinct,
                IsGreedyLoad  = query.IsGreedyLoad,
                IsLazyLoad    = query.IsLazyLoad,
                OrderByExp    = query.OrderByExp,
                PageSize      = query.PageSize + query.PageIndex * query.PageSize,
                RemoteQueries = query.RemoteQueries,
                SelectExp     = query.SelectExp,
                WhereExp      = query.WhereExp,
                TableIndex    = string.IsNullOrWhiteSpace(sharding.TableIndex) ? sharding.TableIndex : sharding.TableIndex.ToLower(),
                GetDataBase   = string.IsNullOrWhiteSpace(sharding.GetDataBase)? sharding.GetDataBase: sharding.GetDataBase.ToLower(),
                Object        = query.Object,
                DbRoute       = query.DbRoute
            };

            if (query.Parameters != null)
            {
                result.Parameters = new Dictionary <string, object>();
                foreach (var parameter in query.Parameters)
                {
                    result.Parameters.Add(parameter.Key, parameter.Value);
                }
            }
            if (query.Cache != null)
            {
                result.Cache = new CacheInfo
                {
                    Time     = query.Cache.Time,
                    TimeSpan = query.Cache.TimeSpan
                };
            }
            return(result);
        }
Esempio n. 9
0
        /// <summary>
        /// 得到所有路由
        /// </summary>
        /// <param name="dbRoute"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        protected virtual IList <QueryInfo> GetAllQueries(DbRouteInfo dbRoute, QueryInfo query)
        {
            if (!dbRoute.IsReturnAllShardings)
            {
                return(null);
            }
            var result = new List <QueryInfo>();

            foreach (var readSharding in dbRoute.Shardings)
            {
                var shardings = readSharding.GetAllShardings();
                if (shardings == null)
                {
                    continue;
                }
                MergeQueries(result, dbRoute, query, shardings);
            }
            return(result);
        }