Exemplo n.º 1
0
        private DivisionContext GetDivisionContext(QueryPara parameter)
        {
            DivisionContext context = new DivisionContext();

            context.IDbSchema     = IDbSchema;
            context.Database      = this;
            context.ITableSchemas = new List <ITableSchema>();
            context.QueryPara     = parameter;

            //提取表名
            Regex regex  = new Regex(@"『(?<tableName>.+?)』", RegexOptions.Multiline);
            var   matchs = regex.Matches(parameter.Sql);

            foreach (Match match in matchs)
            {
                string tableName = match.Groups["tableName"].Value;
                var    table     = Tables[tableName];

                if (!context.ITableSchemas.Contains(table.ITableSchema))
                {
                    context.ITableSchemas.Add(table.ITableSchema);
                }
            }

            //找到粒度最小的
            var      divisionType = context.ITableSchemas.Count > 0 ? context.ITableSchemas.Max(o => o.Table.DivisionType) : DivisionType;
            DateTime tempTime1    = parameter.TargetTime1;
            DateTime tempTime2    = parameter.TargetTime2;

            divisionType.SetTargetTime(ref tempTime1, ref tempTime2);

            var tempQueryItems = new List <QueryItem>();

            while (true)
            {
                QueryItem queryItem = new QueryItem();
                queryItem.ExecSql = parameter.Sql;
                var suffix = "";
                queryItem.DatabaseName   = GetRealName(tempTime1, ref suffix);
                queryItem.DatabaseSuffix = suffix;

                //需要替换的表
                foreach (var tableSchema in context.ITableSchemas)
                {
                    suffix = "";
                    string tableName = tableSchema.Table.GetRealName(tempTime1, ref suffix);
                    var    tablePack = new TablePack
                    {
                        DatabaseName   = queryItem.DatabaseName,
                        DatabaseSuffix = queryItem.DatabaseSuffix,
                        TableName      = tableName,
                        TableSuffix    = suffix,
                        queryItem      = queryItem
                    };
                    queryItem.TableNames.Add(tableSchema, tablePack);
                    queryItem.ExecSql = queryItem.ExecSql.Replace($"『{tableSchema.Table.Name}』", $" {tableName} ");
                }
                queryItem.TargetTime = tempTime1;

                tempQueryItems.Add(queryItem);

                divisionType.PlusTargetTime(ref tempTime1);

                if (tempTime1 > tempTime2)
                {
                    break;
                }
            }

            context.QueryItems = tempQueryItems.GroupBy(o => o.DatabaseName).ToDictionary(g => g.Key, g => g.ToList());

            return(context);
        }