Exemple #1
0
        /// <summary>
        /// 结束计算任务,清理
        /// </summary>
        /// <returns></returns>
        public static bool StopCalcTask()
        {
            string taskId = FuncCalcTask.GetTaskId();

            //清理缓存
            return(FuncCache.Clear(taskId));
        }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the BaseMemoryContext class, using the specified assemblies for interfaces and implementation.
 /// </summary>
 protected BaseMemoryContext(InterfaceType.Factory iftFactory)
 {
     this.objects         = new ContextCache <int>(this, item => item.ID);
     this.iftFactoryCache = new FuncCache <Type, InterfaceType>(t => iftFactory(t));
     this._iftFactory     = t => iftFactoryCache.Invoke(t);
     ZetboxContextDebuggerSingleton.Created(this);
 }
        public async Task ExecuteAsync(IEnumerable <CommandData> commands, CancellationToken cancellationToken = default)
        {
            FieldMap fieldMap = new FieldMap();

            await using AsyncSession session = new AsyncSession(this.Options);

            foreach (CommandData commandData in commands.NotNull())
            {
                Command command = new Command(commandData, fieldMap);

                if (string.IsNullOrWhiteSpace(commandData.CommandText))
                {
                    continue;
                }

                await foreach (DbDataReader dataReader in session.ExecuteAsync(command, cancellationToken).ConfigureAwait(false))
                {
                    TableIdentity      tableInfo  = TableIdentity.FromRecord(dataReader);
                    FieldData[]        fields     = command.GetHeading(tableInfo);
                    MetadataIdentity[] attributes = fields.Select(f => f?.Attribute).ToArray();

                    var binder = FuncCache.GetFieldDataBinder(attributes, tableInfo);

                    if (await dataReader.ReadAsync().ConfigureAwait(false))
                    {
                        binder(dataReader, fields);
                    }
                }
            }

            foreach (FieldData fieldData in fieldMap)
            {
                fieldData.Bind();
            }
        }
        public void Execute(IEnumerable <CommandData> commands)
        {
            FieldMap fieldMap = new FieldMap();

            using SyncSession session = new SyncSession(this.Options);

            foreach (CommandData commandData in commands.NotNull())
            {
                Command command = new Command(commandData, fieldMap);

                if (string.IsNullOrWhiteSpace(commandData.CommandText))
                {
                    continue;
                }

                foreach (IDataReader reader in session.Execute(command))
                {
                    TableIdentity      tableInfo = TableIdentity.FromRecord(reader);
                    FieldData[]        fields    = command.GetHeading(tableInfo);
                    MetadataIdentity[] metadata  = fields.Select(f => f?.Attribute).ToArray();

                    var binder = FuncCache.GetFieldDataBinder(metadata, tableInfo);

                    if (reader.Read())
                    {
                        binder(reader, fields);
                    }
                }
            }

            foreach (FieldData fieldData in fieldMap)
            {
                fieldData.Bind();
            }
        }
Exemple #5
0
 public MemoryContext(InterfaceType.Factory iftFactory, Func <IFrozenContext> lazyCtx, MemoryImplementationType.MemoryFactory implTypeFactory)
     : base(iftFactory)
 {
     _lazyCtx = lazyCtx;
     _implTypeFactoryCache = new FuncCache <Type, MemoryImplementationType>(t => implTypeFactory(t));
     _implTypeFactory      = t => _implTypeFactoryCache.Invoke(t);
 }
Exemple #6
0
 //得到函数的插件配置信息
 public static FuncRefInfo GetFuncRefInfo(FuncInfo func)
 {
     if (func == null)
     {
         return(null);
     }
     return(FuncCache.GetFuncRefInfo(func.Name));
 }
            static async Task consumer(AdoHelper helper, DbDataReader reader)
            {
                TableIdentity tableInfo = TableIdentity.FromRecord(reader);

                FieldData[]        fields     = helper.GetHeading(tableInfo);
                MetadataIdentity[] attributes = fields.Select(f => f.Attribute).ToArray();

                var fun = FuncCache.GetFieldDataBinder(attributes, tableInfo);

                if (await reader.ReadAsync())
                {
                    fun(reader, fields);
                }
            }
Exemple #8
0
        /// <summary>
        /// 加载系统的公式信息
        /// </summary>
        /// <param name="catalogs">int类型以逗号分隔的字符串</param>
        public static void LoadFuncs(string catalogs)
        {
            if (string.IsNullOrEmpty(catalogs))
            {
                return;
            }

            string[]      catalog = catalogs.Split(new char[] { ',' });
            StringBuilder sb      = new StringBuilder();

            foreach (var item in catalog)
            {
                string value = FuncCache.GetCatalog(item);
                if (string.IsNullOrEmpty(value))
                {
                    sb.Append(item);
                    sb.Append(",");
                }
            }
            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - 1, 1); //去掉最后一个逗号
                //缓存目录
                catalog = sb.ToString().Split(new char[] { ',' });
                Array.ForEach(catalog, s => { FuncCache.AddCatalog(s); });

                var funcInfos = LoadFuncInfos(sb.ToString());
                foreach (var key in funcInfos.Keys)
                {
                    FuncCache.AddFuncInfoMetaDataByCatalog(key, funcInfos[key]);
                    funcInfos[key].ToList().ForEach(p => { FuncCache.AddFuncInfoMetaData(p.Name, p); });
                }

                var funcInfoRefs = LoadFuncInfoRefs(sb.ToString());
                funcInfoRefs.ToList().ForEach(p => { FuncCache.AddFuncRefInfo(p.FuncName, p); });

                var droplists = LoadDropDownLists(sb.ToString());
                foreach (var key in droplists.Keys)
                {
                    FuncCache.AddDropDownList(key, droplists[key]);
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// 取多个函数的值
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static string GetFuncsValue(string xml)
        {
            //生成本次报表运算的GUID
            IList <FuncInfo> funcs = XmlHelper.GetFuncs(xml);

            IList <FuncCalcResult> results = new List <FuncCalcResult>();


            funcs.ToList().ForEach(p => {
                var result = FuncFactory.GetFuncValue(p);
                if (result.Status != EnumFuncActionStatus.Failure)
                {
                    //设置返回值的数据类型
                    result.ResultDataType = FuncCache.GetFuncInfoMetaData(p.Name).ResultType;
                }
                results.Add(result);
            });
            //返回结果集
            return(XmlHelper.GetFuncResult(results));
        }
Exemple #10
0
        /// <summary>
        /// Initializes a new instance of the BaseZetboxDataContext class using the specified <see cref="Identity"/>.
        /// </summary>
        /// <param name="metaDataResolver">the IMetaDataResolver for this context.</param>
        /// <param name="identity">the identity of this context. if this is null, the context does no security checks</param>
        /// <param name="config"></param>
        /// <param name="lazyCtx"></param>
        /// <param name="iftFactory"></param>
        protected BaseZetboxDataContext(IMetaDataResolver metaDataResolver, Identity identity, ZetboxConfig config, Func <IFrozenContext> lazyCtx, InterfaceType.Factory iftFactory)
        {
            if (metaDataResolver == null)
            {
                throw new ArgumentNullException("metaDataResolver");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (iftFactory == null)
            {
                throw new ArgumentNullException("iftFactory");
            }

            this.metaDataResolver = metaDataResolver;
            this.identityStore    = identity;
            this.config           = config;
            this.iftFactoryCache  = new FuncCache <Type, InterfaceType>(r => iftFactory(r));
            this.iftFactory       = t => iftFactoryCache.Invoke(t);
            this.lazyCtx          = lazyCtx;
        }
Exemple #11
0
        /// <summary>
        /// 开始计算任务
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool StartCalcTask(FuncContext context)
        {
            string taskId = FuncCalcTask.GetTaskId();

            return(FuncCache.AddContext(taskId, context));
        }
Exemple #12
0
        public override IDictionary <string, string> GetWhere()
        {
            //参数化的思路

            IDictionary <string, string> whereDic = new Dictionary <string, string>();

            //转换成需要的数组,也可以直接处理
            FuncParameter[] funcParams = Func.Paras.ToArray();

            if (funcParams.Length < 8)
            {
                throw new FuncException("函数的参数小于指定的数量8");
            }

            //注意PB的数组下标是从1开始,

            //当前公式计算的上下文
            var context = FuncCache.GetContext();

            int[] accpers = GitCommonHelper.GetAccperFromArea(funcParams[2].Value);


            StringBuilder where1 = new StringBuilder();
            StringBuilder where2 = new StringBuilder();

            where1.Append("1=1");
            where2.Append("1=1");


            //判断会计期
            if (accpers.Length < 1)
            {
                //不是会计区间
                string s_mon = funcParams[1].Value;
                if (!string.IsNullOrEmpty(s_mon))
                {
                    where1.Append(" and  invo_bill.mon = " + GitCommonHelper.ToValue(s_mon));
                    where2.Append(" and  invo_bill.mon = " + GitCommonHelper.ToValue(s_mon));
                }
            }
            else
            {
                string s_mon = accpers.Last().ToString();
                if (!string.IsNullOrEmpty(s_mon))
                {
                    where1.Append(" and  invo_bill.mon = " + GitCommonHelper.ToArray(accpers));
                    where2.Append(" and  invo_bill.mon = " + GitCommonHelper.ToArray(accpers));
                }
            }

            //年度的判断
            string s_year = funcParams[0].Value;

            if (string.IsNullOrEmpty(s_year))
            {
                s_year = GitCommonHelper.GetCurrentYear();
            }

            where1.Append(" and invo_bill.uyear = " + GitCommonHelper.ToValue(s_year));
            where2.Append(" and invo_bill.uyear = " + GitCommonHelper.ToValue(s_year));

            decimal d_invo_rate = Convert.ToDecimal(funcParams[2].Value);

            if (d_invo_rate != 0)
            {
                where1.Append(" and invo_bill.invo_rate = " + GitCommonHelper.ToValue(d_invo_rate));
                where2.Append(" and invo_split.invo_rate = " + GitCommonHelper.ToValue(d_invo_rate));
            }


            string s_imposecode = funcParams[3].Value;

            if (!string.IsNullOrEmpty(s_imposecode))
            {
                where1.Append(" and invo_bill.imposecode = " + GitCommonHelper.ToValue(s_imposecode));
                where2.Append(" and invo_split.imposecodedet = " + GitCommonHelper.ToValue(s_imposecode));
            }


            string s_invo_type = funcParams[4].Value;

            if (!string.IsNullOrEmpty(s_invo_type))
            {
                where1.Append(" and invo_bill.invo_type  = " + GitCommonHelper.ToValue(s_invo_type));
                where2.Append(" and invo_bill.invo_type  = " + GitCommonHelper.ToValue(s_invo_type));
            }


            string s_taxes_type = funcParams[5].Value;

            if (!string.IsNullOrEmpty(s_taxes_type))
            {
                where1.Append(" and invo_bill.taxes_type  = " + GitCommonHelper.ToValue(s_taxes_type));
                where2.Append(" and invo_bill.taxes_type  = " + GitCommonHelper.ToValue(s_taxes_type));
            }

            string s_own_compcode = funcParams[6].Value;

            if (!string.IsNullOrEmpty(s_own_compcode))
            {
                where1.Append(" and invo_bill.own_compcode  = " + GitCommonHelper.ToValue(s_own_compcode));
                where2.Append(" and invo_bill.own_compcode  = " + GitCommonHelper.ToValue(s_own_compcode));
            }


            string s_invo_proj = funcParams[7].Value;

            if (!string.IsNullOrEmpty(s_invo_proj))
            {
                where1.Append(" and invo_bill.invo_proj  = " + GitCommonHelper.ToValue(s_invo_proj));
                where2.Append(" and invo_bill.invo_proj  = " + GitCommonHelper.ToValue(s_invo_proj));
            }


            whereDic.Add("where1", where1.ToString());
            whereDic.Add("where2", where2.ToString());
            return(whereDic);
        }
Exemple #13
0
        //报表格式有可能会产生变化
        internal static FuncCalcResult GetFuncValue(FuncInfo func)
        {
            if (func == null)
            {
                throw new FuncException("要进行计算的函数为空");
            }

            FuncCalcResult calcResult = null;

            //本次计算KEY
            string taskId = FuncCalcTask.GetTaskId();

            if (string.IsNullOrEmpty(taskId))
            {
                throw new FuncException("函数计算任务的taskId不能为空");
            }

            var funcInfoMateData = FuncCache.GetFuncInfoMetaData(func.Name);

            if (funcInfoMateData == null)
            {
                //计算结果
                calcResult = new FuncCalcResult {
                    Status = EnumFuncActionStatus.Failure
                };
                calcResult.Fault = FaultBuilder.Fault("错误", func.Name + "没有注册函数信息");
                return(calcResult);
            }


            //没有缓存则计算
            var refInfo = FuncConfigure.GetFuncRefInfo(func);

            if (refInfo == null)
            {
                //计算结果
                calcResult = new FuncCalcResult {
                    Status = EnumFuncActionStatus.Failure
                };
                calcResult.Fault = FaultBuilder.Fault("错误", func.Name + "没有注册函数实现类信息");
                return(calcResult);
            }

            try
            {
                //反射调用
                var assembly = Assembly.Load(refInfo.AssemblyName);
                var clazz    = assembly.GetType(refInfo.ClassName);

                //使用新封装的接口,所有的参数都是通过request取
                var calcObject = Activator.CreateInstance(clazz) as IFunction;
                if (calcObject == null)
                {
                    //计算结果
                    calcResult = new FuncCalcResult {
                        Status = EnumFuncActionStatus.Failure
                    };

                    calcResult.Fault = FaultBuilder.Fault("错误", func.Name + "没有实现接口IFuncCalc");
                    return(calcResult);
                }

                if (calcObject != null)
                {
                    calcObject.Func = func;
                    var funcInfo = calcObject.PreHandle(); //返回预处理结果,主要是参数的处理

                    var cache = FuncCache.GetFunc(taskId, funcInfo);
                    if (cache != null)
                    {
                        //已经有值,表示已经缓存,则直接取数
                        return(cache.CalcResult);
                    }
                    else
                    {
                        var funcCalc = calcObject as IFuncCalc;
                        var result   = funcCalc.GetValue();

                        //同时加入到缓存中,对象是否要clone
                        funcInfo.CalcResult = result;
                        FuncCache.AddFunc(taskId, funcInfo);
                        return(result);
                    }
                }
            }
            catch (Exception ex)
            {
                //计算结果
                calcResult = new FuncCalcResult {
                    Status = EnumFuncActionStatus.Failure
                };
                calcResult.Fault = FaultBuilder.Fault("错误", func.Name + "反射调用异常");
            }

            return(calcResult);
        }
Exemple #14
0
        /// <summary>
        /// 生成supcan报表控件函数的描述信息
        /// </summary>
        /// <returns></returns>
        public static string SupcanFuncsXmlDescriptor(string catalogs)
        {
            if (string.IsNullOrEmpty(catalogs))
            {
                return(string.Empty);
            }

            FuncConfigure.LoadFuncs(catalogs);
            string[] catalog = catalogs.Split(',');

            IList <DropDownList> dropLists = new List <DropDownList>();

            var xmlDoc = new XmlDocument();

            var xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");

            xmlDoc.AppendChild(xmlDecl);

            var xmlRoot  = xmlDoc.CreateElement("root");
            var xmlFuncs = xmlDoc.CreateElement("functions");

            xmlRoot.AppendChild(xmlFuncs);
            xmlDoc.AppendChild(xmlRoot);

            foreach (var s in catalog)
            {
                //取得所有的函数
                var funcList = FuncCache.GetFuncInfoMetaDataByCatalog(s);
                if (funcList == null)
                {
                    continue;
                }
                //catalogid对应的目录名
                if (funcList.Count > 0)
                {
                    string catalogName = funcList[0].Module;
                    //输出XML
                    var xmlCatalog = xmlDoc.CreateElement("catagory");
                    xmlCatalog.SetAttribute("name", catalogName);
                    xmlFuncs.AppendChild(xmlCatalog);

                    funcList.ToList().ForEach(p => {
                        var xmlFunc = xmlDoc.CreateElement("function");
                        xmlFunc.SetAttribute("name", p.Name);
                        xmlCatalog.AppendChild(xmlFunc);

                        var xmlUsage       = xmlDoc.CreateElement("usage");
                        xmlUsage.InnerText = p.Usage;
                        xmlFunc.AppendChild(xmlUsage);

                        var xmlDetail       = xmlDoc.CreateElement("detail");
                        xmlDetail.InnerText = p.Detail;
                        xmlFunc.AppendChild(xmlDetail);

                        //函数参数
                        p.Paras.ToList().ForEach(parm =>
                        {
                            var xmlParam = xmlDoc.CreateElement("para");
                            xmlParam.SetAttribute("datatype", parm.ParamType);
                            xmlParam.SetAttribute("droplistID", parm.DroplistCode);
                            xmlParam.SetAttribute("edittype", parm.EditType);
                            xmlParam.InnerText = parm.Name;

                            //
                            xmlFunc.AppendChild(xmlParam);
                        });

                        var xmlExample       = xmlDoc.CreateElement("example");
                        xmlExample.InnerText = p.Example;
                        xmlFunc.AppendChild(xmlExample);
                    });
                }

                //不同的模块的函数,下拉列表也有可能是一样的
                var dropList = FuncCache.GetDropDownList(s);
                dropList.ToList().ForEach(p => { dropLists.Add(p); });
            }
            //输出dropDwonList
            var xmlDroplists = xmlDoc.CreateElement("DropLists");

            xmlRoot.AppendChild(xmlDroplists);


            dropLists.Distinct().ToList().ForEach(p =>
            {
                var xmlDropList = xmlDoc.CreateElement("DropList");
                xmlDropList.SetAttribute("DisplayCol", p.DisplayCol);
                xmlDropList.SetAttribute("DataCol", p.DataCol);
                xmlDropList.SetAttribute("dataUrl", p.DataUrl);
                xmlDropList.SetAttribute("treelist", p.TreelistUrl);
                xmlDropList.SetAttribute("id", p.Id);

                //多个字符串参数解析
                foreach (var key in p.ParamsDic.AllKeys)
                {
                    xmlDropList.SetAttribute(key, p.ParamsDic[key]);
                }
                xmlDroplists.AppendChild(xmlDropList);
            });

            return(ConvertXmlToString(xmlDoc));
        }
Exemple #15
0
        private static Func <IDataRecord, dynamic> GetFunction(MySqlDataReader reader)
        {
            var activeFuncCach = new FuncCache();
            var fieldCount     = reader.FieldCount;

            for (int i = 0; i < fieldCount; i++)
            {
                var arg = new ArgCache();

                arg.Name = reader.GetName(i);
                arg.Type = reader.GetFieldType(i);

                activeFuncCach.ArgCache.Add(arg);
            }

            int length = ReaderCached.Count;

            for (int i = 0; i < length; i++)
            {
                if (ReaderCached[i].ArgCache.Count == fieldCount)
                {
                    bool found = true;
                    for (int k = 0; k < fieldCount; k++)
                    {
                        if (activeFuncCach.ArgCache[k].Type != ReaderCached[i].ArgCache[k].Type)
                        {
                            found = false;
                            break;
                        }
                        if (string.CompareOrdinal(activeFuncCach.ArgCache[k].Name,
                                                  ReaderCached[i].ArgCache[k].Name) != 0)
                        {
                            found = false;
                            break;
                        }
                    }

                    if (found)
                    {
                        return(ReaderCached[i].Func);
                    }
                }
            }

            ReaderCached.Add(activeFuncCach);

            var builder = new StringBuilder();

            for (int i = 0; i < fieldCount; i++)
            {
                var arg = activeFuncCach.ArgCache[i];

                string setString = "";
                if (arg.Type == typeof(string))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? null : idata.GetString(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(int))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? 0 : idata.GetInt32(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(byte))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? 0 : idata.GetByte(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(DateTime))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? DateTime.MinValue : idata.GetDateTime(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(bool))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? false : idata.GetBoolean(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(short))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? 0 : idata.GetInt16(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(long))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? 0 : idata.GetInt64(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(decimal))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? 0 : idata.GetDecimal(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(float))
                {
                    setString = "idata.IsDBNull(" + i.ToString() + ") ? 0 : idata.GetFloat(" + i.ToString() + ")";
                }
                else if (arg.Type == typeof(byte[]))
                {
                    setString =
                        @"if(idata.IsDBNull(" + i.ToString() + @"))
{
    dynm." + arg.Name + @" = null;
}else
{
    var file_" + i.ToString() + @" = idata.GetInt32(" + i.ToString() + @");
    var data_" + i.ToString() + @" = new byte[file_" + i.ToString() + @"];

    idata.GetBytes(" + i.ToString() + @", 0, data_" + i.ToString() + @", 0, file_" + i.ToString() + @");
    dynm." + arg.Name + @" = data_" + i.ToString() + @";
}";

                    builder.AppendLine(setString);
                    continue;
                }

                builder.AppendLine("dynm." + arg.Name + " = " + setString + ";");
            }

            activeFuncCach.Func = CSScript.Evaluator
                                  .LoadDelegate <Func <IDataRecord, dynamic> >(
                @"

using System.Data;

dynamic funcPrivate(System.Data.IDataRecord idata)
{
    dynamic dynm = new System.Dynamic.ExpandoObject();    
" + builder.ToString() + @"    
    return dynm;
}");

            return(activeFuncCach.Func);
        }