Exemplo n.º 1
0
        public void GetDbParaByDataRow(DataRow row, DbList <DbParameter> dbParas, EList <CKeyValue> sqlParaNameList, EList <CKeyValue> tableColumns)
        {
            dbParas.Clear();
            if (null == row)
            {
                return;
            }
            CKeyValue kv          = null;
            Type      type        = null;
            object    vObj        = null;
            string    paraTypeStr = "";

            foreach (var item in sqlParaNameList)
            {
                kv = tableColumns[item.Key];
                if (null == kv)
                {
                    continue;
                }

                if (string.IsNullOrEmpty(paraTypeStr))
                {
                    paraTypeStr = item.other.ToString();
                }

                vObj = row[kv.Value.ToString()];
                type = kv.other as Type;
                type = null == type ? typeof(string) : type;
                GetDbParaByBaseType(type, paraTypeStr, vObj, item.Value.ToString(), dbParas);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据数据实体(接口方法参数中包含)和带参数的sql语句中包含的参数集合来创建DbParameter集合
        /// </summary>
        /// <param name="entity">数据实体(接口方法参数)</param>
        /// <param name="dbParas">DbParameter集合</param>
        /// <param name="paraNameList">带参数的sql包含的参数集合</param>
        public void GetDbParaListByEntity(object entity, DbList <DbParameter> dbParas, EList <CKeyValue> sqlParaNameList)
        {
            dbParas.Clear();
            if (null == entity)
            {
                return;
            }
            CKeyValue kv          = null;
            object    vObj        = null;
            string    paraTypeStr = "";

            PropertyInfo[] piArr = entity.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo item in piArr)
            {
                kv = sqlParaNameList[item.Name.ToLower()];
                if (null == kv)
                {
                    continue;
                }
                if (string.IsNullOrEmpty(paraTypeStr))
                {
                    paraTypeStr = kv.other.ToString();
                }

                //throw new Exception("未引入与[" + paraTypeStr + "]类型相关的程序集");
                vObj = item.GetValue(entity, null);

                GetDbParaByBaseType(item.PropertyType, paraTypeStr, vObj, kv.Value.ToString(), dbParas);
            }
        }
Exemplo n.º 3
0
        CKeyValue GetKvByInterfaceType(Type interfaceType)
        {
            CKeyValue kv            = null;
            string    interfaceNS   = interfaceType.Namespace;
            string    interfaceName = interfaceType.Name;
            string    interfaceFile = interfaceNS + "." + interfaceName;

            kv = matchRules[interfaceFile];
            if (null == kv)
            {
                kv = matchRules[interfaceName];
            }

            if (null != kv)
            {
                return(kv);
            }

            int    n  = 0;
            string s  = "";
            string s1 = "";

            while (-1 != interfaceFile.IndexOf(".") && 20 > n)
            {
                s += "." + interfaceFile.Substring(0, interfaceFile.IndexOf("."));
                s1 = s.Substring(1);
                kv = matchRules[s1];
                if (null != kv)
                {
                    break;
                }
                interfaceFile = interfaceFile.Substring(interfaceFile.IndexOf(".") + 1);
                n++;
            }

            return(kv);
        }
Exemplo n.º 4
0
        /// <summary>
        /// upb kes and value in CKeysValues by Changes
        /// </summary>
        protected static List <CKeyValue> ChangeKeyValue(CWhatIsChange Changes, List <CKeyValue> KeysValues)
        {
            List <CKeyValue> OUTKeysValues = new List <CKeyValue>();

            foreach (CKeyValue KVD in KeysValues)
            {
                CKeyValue KeyValueDesc = new CKeyValue();

                if (KVD.Key == Changes.Key)
                {
                    KeyValueDesc.Key   = Changes.ModifiedKey;
                    KeyValueDesc.Value = Changes.ModifiedValue;
                    if (Changes.Description != null)
                    {
                        if ((Changes.ModifiedDescription != null) && (Changes.ModifiedDescription != ""))
                        {
                            if (!Changes.ModifiedDescription.StartsWith(";"))
                            {
                                KeyValueDesc.Description = ";" + Changes.ModifiedDescription;
                            }
                            else
                            {
                                KeyValueDesc.Description = Changes.ModifiedDescription;
                            }
                        }
                    }
                }
                else
                {
                    KeyValueDesc = KVD;
                }

                OUTKeysValues.Add(KeyValueDesc);
            }

            return(OUTKeysValues);
        }
Exemplo n.º 5
0
        void Adapter()
        {
            if (string.IsNullOrEmpty(rootPath))
            {
                return;
            }

            object[] arr = null;

            string    resetKeyName  = "";
            Type      interfaceType = null;
            Type      implType      = null;
            CKeyValue kv            = null;
            MatchRule mr            = null;
            object    impl          = null;
            object    implNew       = null;
            object    impl_1        = null;

            bool        isShowCode         = false;
            bool        isSingleCall       = false;
            bool        isSingleInstance   = false;
            bool        isUnSingleInstance = false;
            InstanceObj instanceObj        = null;

            string unSingleInstanceStr = typeof(IUnSingleInstance).FullName;

            TempImpl temp = new TempImpl();

            temp.codeCompiler = codeCompiler;

            bool enableCompiler = false;

            if (null != codeCompiler && null != dataServerProvider && null != DbHelper)
            {
                enableCompiler = true;
            }

            Action <object, Action> action = (obj, action1) =>
            {
                if (null != obj)
                {
                    if (null != (obj as IUnSingleInstance))
                    {
                        isUnSingleInstance = true;
                        isSingleCall       = false;
                        isSingleInstance   = false;
                    }

                    if ((null != (obj as ISingleInstance) || isSingleCall) && false == isUnSingleInstance)
                    {
                        isSingleCall     = true;
                        isSingleInstance = true;
                        action1?.Invoke();
                    }
                }
            };

            Regex  rg           = null;
            string implName     = "";
            bool   isIgnoreCase = false;

            AutoCall autoCall = null;

            FieldInfo[] fArr = null; // currentObj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

            List <Type> objList  = new List <Type>();
            Type        _objType = currentObj.GetType();
            int         n        = 0;

            while (20 > n && typeof(object) != _objType && typeof(ImplementAdapter) != _objType)
            {
                objList.Add(_objType);
                _objType = _objType.BaseType;
                n++;
            }

            if (0 == objList.Count && null != currentObj)
            {
                objList.Add(currentObj.GetType());
            }

            foreach (Type typeItem in objList)
            {
                fArr = typeItem.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                foreach (FieldInfo p in fArr)
                {
                    arr = p.GetCustomAttributes(typeof(AutoCall), true);
                    if (0 == arr.Length)
                    {
                        continue;
                    }

                    autoCall = null;
                    foreach (var item in arr)
                    {
                        if (null != (item as AutoCall))
                        {
                            autoCall = (AutoCall)item;
                            break;
                        }
                    }

                    interfaceType = p.FieldType;

                    if (!autoCall.LoadBeforeFilter(interfaceType))
                    {
                        continue;
                    }
                    autoCall.errorLevels1 = errorLevels1;

                    impl               = null;
                    impl_1             = null;
                    implType           = null;
                    resetKeyName       = null;
                    isSingleInstance   = false;
                    isUnSingleInstance = false;
                    instanceObj        = null;

                    arr          = p.GetCustomAttributes(typeof(SingleCall), true);
                    isSingleCall = 0 < arr.Length;

                    resetKeyName = GetInterfaceName(interfaceType, autoCall);
                    interfaceImplements.TryGetValue(resetKeyName, out instanceObj);
                    if (null != instanceObj)
                    {
                        impl = instanceObj.newInstance;
                    }
                    if (null != instanceObj)
                    {
                        implName     = "";
                        rg           = null;
                        isIgnoreCase = false;
                        AutoCallMatch(autoCall, ref implName, ref rg, ref isIgnoreCase);
                        if (null != rg)
                        {
                            if (!MatchImpl(rg, instanceObj.oldInstanceType, implName, isIgnoreCase))
                            {
                                impl = null;
                            }
                        }
                    }
                    impl_1 = impl;

                    action(impl, () =>
                    {
                        impl = null;
                    });

                    if (null == impl)
                    {
                        kv         = GetKvByInterfaceType(interfaceType);
                        mr         = null == kv ? null : ((MatchRule)kv.Value);
                        isShowCode = false;
                        if (interfaceType.IsInterface)
                        {
                            if (null != mr)
                            {
                                isShowCode = mr.IsShowCode;
                                implType   = LoadImplementTypeByMatchRule(mr, interfaceType, autoCall);
                            }
                            else
                            {
                                implType = LoadImplementTypeByInterface(interfaceType, autoCall);
                            }

                            if (null == implType)
                            {
                                implType = LoadImplementTypeByAssemblies(interfaceType, autoCall);
                            }

                            if (enableCompiler && null == (autoCall as ExistCall))
                            {
                                implNew = temp.NewImplement(interfaceType, implType, autoCall, isShowCode);
                                if (null != implNew)
                                {
                                    impl = implNew;
                                }
                            }

                            if (null != implType && null == impl)
                            {
                                try
                                {
                                    impl = Activator.CreateInstance(implType);
                                }
                                catch (Exception ex)
                                {
                                    string err = "[" + implType.FullName + "] 实例可能缺少一个无参构造函数(或该类访问权限不够)\r\n" + ex.ToString();
                                    autoCall.ExecuteExcption(interfaceType, null, null, null, new Exception(err));
                                    //throw;
                                }
                            }
                        }
                        else
                        {
                            if (enableCompiler)
                            {
                                implType      = interfaceType;
                                interfaceType = typeof(IEmplyInterface);
                                implNew       = temp.NewImplement(interfaceType, implType, autoCall, isShowCode);
                                if (null != implNew)
                                {
                                    impl = implNew;
                                }
                            }

                            if (null == impl && null != implType)
                            {
                                try
                                {
                                    impl = Activator.CreateInstance(implType);
                                }
                                catch (Exception ex)
                                {
                                    string err = "[" + implType.FullName + "] 实例可能缺少一个无参构造函数(或该类访问权限不够)\r\n" + ex.ToString();
                                    autoCall.ExecuteExcption(interfaceType, null, null, null, new Exception(err));
                                    //throw;
                                }
                            }
                        }

                        if (false == isUnSingleInstance)
                        {
                            action(impl, null);
                        }

                        if (null != impl && null == impl_1 && false == isSingleCall)
                        {
                            lock (_adapterOfImplement)
                            {
                                interfaceImplements.TryGetValue(resetKeyName, out instanceObj);
                                if (null == instanceObj)
                                {
                                    interfaceImplements.Add(resetKeyName, new InstanceObj()
                                    {
                                        newInstance     = impl,
                                        oldInstanceType = implType
                                    });
                                }
                                else
                                {
                                    impl = instanceObj.newInstance;
                                }
                            }
                        }
                    }

                    if (null == impl)
                    {
                        continue;
                    }

                    isSingleInstance = false;
                    if (null != (impl as ISingleInstance))
                    {
                        isSingleInstance = true;
                    }

                    if (isSingleInstance)
                    {
                        if (null == ((ISingleInstance)impl).Instance)
                        {
                            ((ISingleInstance)impl).Instance = impl;
                        }
                    }

                    if (!autoCall.LoadAfterFilter(impl))
                    {
                        continue;
                    }

                    p.SetValue(currentObj, impl);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// read full date from ini
        /// </summary>
        protected static CFile LoadDataFrominiFile(string fileName)
        {
            CFile Fileini = new CFile();

            Fileini.Filename = fileName;
            Fileini.Sections = new List <CSection>();

            Encoding enc = Encoding.GetEncoding(1251);

            string[] lines = File.ReadAllLines(fileName, enc);

            int i = 0;

            while (i < lines.Count())
            {
                string dataString = lines[i].Trim();
                if (string.IsNullOrEmpty(dataString))
                {
                    continue;
                }
                if ((dataString.StartsWith("[")) && (dataString.EndsWith("]")))
                {
                    CSection Cection = new CSection();
                    Cection.SectionName = dataString;
                    i++;
                    dataString       = lines[i].Trim();
                    Cection.KeyValue = new List <CKeyValue>();
                    while (!dataString.StartsWith("["))
                    {
                        CKeyValue KeyValue = new CKeyValue();

                        if (dataString.StartsWith(";"))
                        {
                            KeyValue.Description = dataString;
                            i++;
                            dataString = lines[i].Trim();
                        }

                        if (dataString.Contains("="))
                        {
                            int    pos   = dataString.IndexOf("=");
                            string key   = dataString.Substring(0, pos).Trim();
                            string value = "";
                            if ((pos + 1) < dataString.Length)
                            {
                                value = dataString.Substring(pos + 1, dataString.Length - pos - 1).Trim();
                            }

                            // save date to colection
                            KeyValue.Key   = key;
                            KeyValue.Value = value;
                        }


                        Cection.KeyValue.Add(KeyValue);
                        i++;

                        if (i < lines.Count())
                        {
                            dataString = lines[i].Trim();
                        }
                        else
                        {
                            break;
                        }
                    }
                    Fileini.Sections.Add(Cection);
                }
            }
            return(Fileini);
        }
Exemplo n.º 7
0
        public string GetParametersBySqlParameter(string sql, string sqlVarName, MethodInformation method, DataOptType dataOptType, ref string dbParaListVarName)
        {
            string code            = "";
            string sql1            = sql;
            string executeDbHelper = "{ExecuteDbHelper#}";
            string leftSpace       = "";
            Regex  rg = rgParaField;

            if (string.IsNullOrEmpty(dbParaListVarName) || dbParaListVarName.ToLower().Equals("null"))
            {
                dbParaListVarName = "dbParaList";
                method.append(ref code, LeftSpaceLevel.one, "DbList<System.Data.Common.DbParameter> {0} = new DbList<System.Data.Common.DbParameter>();", dbParaListVarName);
            }

            string dbHelperVarName   = "dbHelper";
            string resultVarName     = "successVal";
            string funcResultVarName = method.methodComponent.ResultVariantName;

            if (baseTypeWithResult(method))
            {
                method.append(ref code, LeftSpaceLevel.one, "int {0} = 0;", resultVarName);
            }
            else
            {
                method.append(ref code, LeftSpaceLevel.one, "{0} {1} = default({0});", method.methodComponent.ResultType.FullName, resultVarName);
            }
            method.append(ref code, LeftSpaceLevel.one, "string err = \"\";");
            method.append(ref code, LeftSpaceLevel.one, "IDbHelper {0} = ImplementAdapter.DbHelper;", dbHelperVarName);

            if (rg.IsMatch(sql) && 0 < method.paraList.Count)
            {
                string LeftSign     = "";
                string EndSign      = "";
                string DbTag        = "";
                string FieldName    = "";
                string autoCallName = method.AutoCallVarName;
                Match  match        = null;

                EList <CKeyValue> sqlParaList1    = new EList <CKeyValue>();
                string            sqlParasVarName = "sqlParaList";
                method.append(ref code, LeftSpaceLevel.one, "EList<CKeyValue> {0} = new EList<CKeyValue>();", sqlParasVarName);
                int n = 0;
                while (rg.IsMatch(sql1) && 200 > n)
                {
                    match    = rg.Match(sql1);
                    LeftSign = match.Groups["LeftSign"].Value;
                    EndSign  = match.Groups["EndSign"].Value;
                    if (isEnabledField(LeftSign))
                    {
                        if (string.IsNullOrEmpty(DbTag))
                        {
                            DbTag = match.Groups["DbTag"].Value;
                        }
                        FieldName = match.Groups["FieldName"].Value;
                        method.append(ref code, LeftSpaceLevel.one, "{0}.Add(new CKeyValue(){ Key = \"{1}\", Value = \"{2}\", other = \"{3}\" });", sqlParasVarName, FieldName.ToLower(), FieldName, DbTag);
                        sqlParaList1.Add(new CKeyValue()
                        {
                            Key = FieldName.ToLower(), Value = FieldName, other = DbTag
                        });
                    }

                    sql1 = sql1.Replace(match.Groups[0].Value, "");
                    sql1 = EndSign + sql1;
                    n++;
                }

                Regex        rg1           = new Regex(@"\`[0-9]+\[");
                CKeyValue    kv            = null;
                PList <Para> paraList      = method.paraList;
                string       paraClassName = ""; //DJTools.GetParamertClassNameByDbTag(DbTag);

                foreach (Para para in paraList)
                {
                    if (para.ParaType.BaseType == typeof(System.MulticastDelegate) && rg1.IsMatch(para.ParaType.ToString()))
                    {
                        continue;
                    }
                    if (DJTools.IsBaseType(para.ParaType))
                    {
                        //方法参数为基本类型 string, int, bool 等
                        kv = sqlParaList1[para.ParaName.ToLower()];
                        if (null == kv)
                        {
                            continue;
                        }
                        method.append(ref code, LeftSpaceLevel.one, "{0}.Add(\"{2}\", {3});", dbParaListVarName, paraClassName, kv.Value.ToString(), para.ParaName);
                    }
                    else
                    {
                        LeftSpaceLevel leftSpaceLevel = LeftSpaceLevel.one;
                        if (null != para.ParaType.GetInterface("IEnumerable"))
                        {
                            //集合复杂类型
                            //ICollection collection = (ICollection)para.ParaValue;
                            method.append(ref code, LeftSpaceLevel.one, "");
                            method.append(ref code, LeftSpaceLevel.one, "System.Collections.ICollection collection = (System.Collections.ICollection){0};", para.ParaName);

                            method.append(ref code, LeftSpaceLevel.one, "");

                            if (null != para.ParaType.GetInterface("IDictionary"))
                            {
                                method.append(ref code, leftSpaceLevel, "//键值对情况");
                                method.append(ref code, leftSpaceLevel, "object vObj = null;");
                                method.append(ref code, leftSpaceLevel, "string key = \"\";");
                                method.append(ref code, leftSpaceLevel, "foreach (var item in collection)");
                                method.append(ref code, leftSpaceLevel, "{");
                                method.append(ref code, leftSpaceLevel + 1, "key = item.GetType().GetProperty(\"Key\").GetValue(item, null).ToString();");
                                method.append(ref code, leftSpaceLevel + 1, "vObj = item.GetType().GetProperty(\"Value\").GetValue(item, null);");
                                method.append(ref code, leftSpaceLevel + 1, "if (DJTools.IsBaseType(vObj.GetType())) break;");
                                method.append(ref code, leftSpaceLevel + 1, "if (null != vObj.GetType().GetInterface(\"IEnumerable\")) break;");
                                method.append(ref code, leftSpaceLevel + 1, "//值必须是简单类型:int, string, bool, float, double等");
                                method.append(ref code, leftSpaceLevel + 1, "");
                                //Type dataType, string dbTag, object data, string fieldName, List<DbParameter> dbParas
                                method.append(ref code, leftSpaceLevel + 1, "{0}.GetDbParaByBaseType(typeof(string),\"{1}\",vObj,key,{2});", autoCallName, DbTag, dbParaListVarName);
                                method.append(ref code, leftSpaceLevel + 1, "");

                                method.append(ref code, leftSpaceLevel, "}"); //foreach (var item in collection)
                            }
                            else if (null != para.ParaType.GetInterface("IList") || para.ParaType.IsArray)
                            {
                                leftSpace = method.StartSpace + method.getSpace((int)leftSpaceLevel);
                                method.append(ref code, leftSpaceLevel, "//List集合情况 或 数组情况");
                                method.append(ref code, leftSpaceLevel, "foreach (var item in collection)");
                                method.append(ref code, leftSpaceLevel, "{");
                                method.append(ref code, leftSpaceLevel + 1, "if (DJTools.IsBaseType(item.GetType())) break;");
                                method.append(ref code, leftSpaceLevel + 1, "if (null != item.GetType().GetInterface(\"IEnumerable\")) break;");
                                method.append(ref code, leftSpaceLevel + 1, "//集合元素必须是单体复杂对象(数据实体)");
                                //object entity, List<DbParameter> dbParas, EList<CKeyValue> paraNameList
                                method.append(ref code, leftSpaceLevel + 1, "{0}.GetDbParaListByEntity(item,{1},{2});", autoCallName, dbParaListVarName, sqlParasVarName);
                                code += "\r\n" + executeDbHelper;
                                method.append(ref code, leftSpaceLevel + 1, "");
                                method.append(ref code, leftSpaceLevel, "}"); //foreach (var item in collection)
                            }
                        }
                        else if (typeof(DataTable) == para.ParaType)
                        {
                            leftSpace = method.StartSpace + method.getSpace((int)leftSpaceLevel);
                            method.append(ref code, LeftSpaceLevel.one, "");
                            method.append(ref code, LeftSpaceLevel.one, "{0} = null == {0} ? new System.Data.DataTable() : {0};", para.ParaName);
                            method.append(ref code, LeftSpaceLevel.one, "System.Data.DataTable dtable = (System.Data.DataTable){0};", para.ParaName);
                            method.append(ref code, LeftSpaceLevel.one, "EList<CKeyValue> tableColumns = new EList<CKeyValue>();");
                            method.append(ref code, LeftSpaceLevel.one, "");

                            method.append(ref code, leftSpaceLevel, "foreach(System.Data.DataColumn c in dtable.Columns)");
                            method.append(ref code, leftSpaceLevel, "{");
                            method.append(ref code, leftSpaceLevel + 1, "tableColumns.Add(new CKeyValue(){ Key = c.ColumnName.ToLower(), Value = c.ColumnName, other = c.DataType });");
                            method.append(ref code, leftSpaceLevel, "}");
                            method.append(ref code, leftSpaceLevel, "");

                            method.append(ref code, leftSpaceLevel, "foreach(System.Data.DataRow dr in dtable.Rows)");
                            method.append(ref code, leftSpaceLevel, "{");
                            //GetDbParaByDataRow(DataRow row, List<DbParameter> dbParas, EList<CKeyValue> sqlParaNameList, EList<CKeyValue> tableColumns)
                            method.append(ref code, leftSpaceLevel + 1, "{0}.GetDbParaByDataRow(dr,{1},{2},tableColumns);", autoCallName, dbParaListVarName, sqlParasVarName);
                            code += "\r\n" + executeDbHelper;
                            method.append(ref code, leftSpaceLevel + 1, "");
                            method.append(ref code, leftSpaceLevel, "}");//foreach(var dr in dtable.Rows)
                        }
                        else
                        {
                            //单体复杂类型(数据实体)
                            //object entity, List<DbParameter> dbParas, EList<CKeyValue> paraNameList
                            method.append(ref code, LeftSpaceLevel.one, "");
                            method.append(ref code, LeftSpaceLevel.one, "//单体复杂类型(数据实体) ");
                            method.append(ref code, LeftSpaceLevel.one, "{0}.GetDbParaListByEntity({1},{2},{3});", autoCallName, para.ParaName, dbParaListVarName, sqlParasVarName);
                            method.append(ref code, LeftSpaceLevel.one, "");
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(leftSpace))
            {
                method.append(ref code, LeftSpaceLevel.one, "");
                ExecuteSqlCode(method, LeftSpaceLevel.one, dataOptType, sqlVarName, dbParaListVarName, dbHelperVarName, resultVarName, ref code);
            }
            else
            {
                string code1 = "";
                ExecuteSqlCode(method, LeftSpaceLevel.two, dataOptType, sqlVarName, dbParaListVarName, dbHelperVarName, resultVarName, ref code1);
                code = code.Replace(executeDbHelper, code1);
            }

            if (!string.IsNullOrEmpty(funcResultVarName))
            {
                method.append(ref code, LeftSpaceLevel.one, "");
                if (null == method.methodComponent.ActionType)
                {
                    ReturnResult(method, LeftSpaceLevel.one, dataOptType, funcResultVarName, resultVarName, ref code);
                }
                else
                {
                    method.append(ref code, LeftSpaceLevel.one, "{0} = {1};", funcResultVarName, resultVarName);
                }
            }

            return(code);
        }