public void Execute(ISqlInitPolicyExecutorInfo info)
 {
     //进行分部sql的合并
     foreach (var tableInfo in info.TableSqlInfos.Values)
     {
         var sqls = info.NewlySqls[tableInfo.Name];   //获取最新的sql
         foreach (var sqlPair in tableInfo.Sqls)
         {
             var sql = sqls[sqlPair.Key];
             //替换sql
             sqls[sqlPair.Key] = RecurReplaceSectionSql(info, info.GetPolicy(sqlPair.Value, tableInfo) as SqlSectionPolicy, tableInfo, sql);
         }
     }
 }
        /// <summary>
        /// 递归所关联的所有Sql片段(注意配置的Sql不要互相嵌套,那么就死循环了)
        /// </summary>
        protected string RecurReplaceSectionSql(ISqlInitPolicyExecutorInfo info, SqlSectionPolicy section,
                                                IConfigTableInfo tableInfo, string sql)
        {
            if (IsUsePolicy(section))
            {
                var            tagPrefix = string.IsNullOrEmpty(section.TagPrefix) ? SqlConfigConst.SqlSectionPrefixSymbol : section.TagPrefix;
                var            tagSuffix = string.IsNullOrEmpty(section.TagSuffix) ? SqlConfigConst.SqlSectionSuffixSymbol : section.TagSuffix;
                IConfigSqlInfo tempSqlModel;

                //同一Table下的
                if (section.SqlNames?.Count > 0)
                {
                    foreach (var sqlName in section.SqlNames)
                    {
                        tempSqlModel = tableInfo.Sqls[sqlName];
                        sql          = sql.Replace(tagPrefix + sqlName + tagSuffix,
                                                   //递归
                                                   RecurReplaceSectionSql(info, info.GetPolicy(tempSqlModel, tableInfo) as SqlSectionPolicy, tableInfo,
                                                   //获取最新的sql
                                                                          info.NewlySqls[tableInfo.Name][sqlName]));
                    }
                }

                //指定了Table Name的
                if (section.TableSqlNames?.Count > 0)
                {
                    IConfigTableInfo tempTableInfo;
                    foreach (var pair in section.TableSqlNames)
                    {
                        tempTableInfo = info.TableSqlInfos[pair.Key];
                        tempSqlModel  = tempTableInfo.Sqls[pair.Value];
                        sql           = sql.Replace(tagPrefix + pair.Key + "." + pair.Value + tagSuffix,
                                                    //递归
                                                    RecurReplaceSectionSql(info, info.GetPolicy(tempSqlModel, tempTableInfo) as SqlSectionPolicy, tempTableInfo,
                                                    //获取最新的sql
                                                                           info.NewlySqls[pair.Key][pair.Value]));
                    }
                }
            }

            return(sql);
        }
        public void Execute(ISqlInitPolicyExecutorInfo info)
        {
            TableNamePolicy policy;

            //进行替换表名
            foreach (var table in info.TableSqlInfos.Values)
            {
                var tableSqls = info.NewlySqls[table.Name]; //获取最新的sql
                foreach (var sqlPair in table.Sqls)
                {
                    var sql = tableSqls[sqlPair.Key];
                    policy = info.GetPolicy(sqlPair.Value, table) as TableNamePolicy;
                    if (IsUsePolicy(policy))
                    {
                        var tname = string.IsNullOrEmpty(policy.Tag) ? SqlConfigConst.TableNameLabel : policy.Tag;
                        //替换sql
                        tableSqls[sqlPair.Key] = sql.Replace(tname, policy.Prefix + table.Name + policy.Suffix);
                    }
                }
            }
        }
Пример #4
0
        public void Execute(ISqlInitPolicyExecutorInfo info)
        {
            InsertIntoPolicy policy;

            foreach (var table in info.TableSqlInfos.Values)
            {
                var tableSqls = info.NewlySqls[table.Name]; //获取最新的sql
                foreach (var sqlPair in table.Sqls)
                {
                    var sql = tableSqls[sqlPair.Key];
                    policy = info.GetPolicy(sqlPair.Value, table) as InsertIntoPolicy;
                    if (IsUsePolicy(policy))
                    {
                        var mdf = sqlPair.Value as IConfigSqlInfoModifier;  //获取sql的配置修改器
                        if (mdf != null)
                        {
                            //添加$$params的策略
                            mdf.Policies[SqlConfigConst.SqlForeachParamsPolicyName] = new SqlForeachParamsPolicy
                            {
                                IsKVSplit    = true,
                                IsToSqlParam = true,
                                KSeparator   = ",",
                                VSeparator   = ",",
                            };
                            var tname = string.IsNullOrEmpty(policy.Tag) ? "##insinto" : policy.Tag;
                            //替换sql
                            tableSqls[sqlPair.Key] = sql.Replace(tname,
                                                                 "insert into " + table.Name + "($$params.keys) values($$params.vals)");
                        }
                        else
                        {
                            throw new ArgumentException($"The table [{table.Name}] can not as IConfigTableInfoModifier");
                        }
                    }
                }
            }
        }
        public void Execute(ISqlInitPolicyExecutorInfo info)
        {
            IConfigTableInfoModifier tmodif;
            IConfigSqlInfoModifier   sqlmodif;

            foreach (var tpair in info.TableSqlInfos)
            {
                tmodif = tpair.Value as IConfigTableInfoModifier;
                //可以修改的才修改
                if (tmodif != null)
                {
                    ForeachPolicies(tmodif.Policies);
                    //Sql配置的
                    foreach (var sqlPair in tmodif.Sqls)
                    {
                        sqlmodif = sqlPair.Value as IConfigSqlInfoModifier;
                        if (sqlmodif != null)
                        {
                            ForeachPolicies(sqlmodif.Policies);
                        }
                    }
                }
            }
        }