public AbsBatch() { dbTag = DJTools.GetParaTagByDbDialect(DataAdapter.dbDialect); new Task(() => { int pulse = 1000; isRun = true; while (isRun) { if (0 < deDic.Count) { foreach (KeyValuePair <string, DataEle> item in deDic) { if (false == item.Value.Enabled) { continue; } item.Value.nTime++; if (waitSecond == item.Value.nTime) { item.Value.toBuffer(); } } } Thread.Sleep(pulse); } }).Start(); }
static void init() { if (string.IsNullOrEmpty(rootPath)) { return; } matchRules = MatchRules(); TempImpl tempImpl = new TempImpl(); tempImpl.SetRootPath(rootPath); tempImpl.IsShowCodeOfDataResourceDLL = dbInfo.IsShowCode; DataAdapter.SetConfig(dbInfo.ConnectionString, dbInfo.DatabaseType); assemblies = DJTools.GetAssemblyCollection(rootPath); AutoCall.AssemblyCollection = assemblies; AutoCall.SetDataProviderAssemble(dbInfo.SqlProviderRelativePathOfDll); AutoCall.RootPath = rootPath; AutoCall.GetDllAbsolutePathByRelativePath = GetDllAbsolutePathByRelativePath; DbList <Data.Common.DbParameter> .dataServerProvider = dataServerProvider; if (null != DbHelper) { DbHelper.connectString = dbInfo.ConnectionString; DbHelper.optByBatchMaxNumber = dbInfo.optByBatchMaxNumber; DbHelper.optByBatchWaitSecond = dbInfo.optByBatchWaitSecond; DbHelper.sqlMaxLengthForBatch = dbInfo.sqlMaxLengthForBatch; DbHelper.disposableAndClose = dbInfo.close; } }
string GetInterfaceName(Type interfaceType, AutoCall autoCall) { string resetKeyName = DJTools.GetClassName(interfaceType, true); //string fn1 = autoCall.MatchRuleOrClassName; //fn1 = null == fn1 ? "" : fn1; //fn1 = fn1.Trim(); //fn1 = string.IsNullOrEmpty(fn1) ? "" : ("+" + fn1); //resetKeyName += fn1; return(resetKeyName); }
/// <summary> /// 用数据实体中属于基本类型(int,string,bool等)的属性及其值来创建DbParameter集合 /// </summary> /// <param name="dataType">数据类型(数据实体属性类型)</param> /// <param name="dbTag">数据源参数类型(如:sqlserver参数类型为@, mysql参数类型为?, oracle为:)</param> /// <param name="data">数据源属性值</param> /// <param name="fieldName">sql语句中字段名称(同时也是数据实体属性名称)</param> /// <param name="dbParas">DbParameter集合</param> public void GetDbParaByBaseType(Type dataType, string dbTag, object data, string fieldName, DbList <DbParameter> dbParas) { if (!DJTools.IsBaseType(dataType)) { return; } object val = DJTools.ConvertTo(data, dataType); val = null == val ? DBNull.Value : val; dbParas.Add(fieldName, val); }
static bool isDotNetCore() { string code = ""; DJTools.append(ref code, "public class testFramework"); DJTools.append(ref code, "{"); DJTools.append(ref code, 1, "public void say(){ }"); DJTools.append(ref code, "}"); Assembly asse = null; IInstanceCodeCompiler framework = loadInterfaceInstance <IInstanceCodeCompiler>(@"DJ\.Framework", null, ref asse); if (null == framework) { return(false); } string err = ""; Assembly ass1 = framework.TranslateCode(null, "", code, "", ref err); return(null == ass1); }
/// <summary> /// 配置文件配置规则(多项配置换行): /// {DllRelativePathOfImpl="BLL.dll",ImplementNameSpace="BLL.SaleOrder",MatchImplExpression="^data.+",InterFaceName="IGetSaleOrderInfo",IgnoreCase=true} /// {DllRelativePathOfImpl="BLL.dll",ImplementNameSpace="BLL.MemberManage.impl",MatchImplExpression="^produce.+",InterFaceName="BLL.MemberManage.IProduceMaintain",IgnoreCase=true} /// </summary> /// <returns></returns> private static EList <CKeyValue> MatchRules() { EList <CKeyValue> list = new EList <CKeyValue>(); string file = Path.Combine(rootPath, configFile); if (!File.Exists(file)) { defaultConfig(file); } if (!File.Exists(file)) { return(list); } LogsRange logsRange1 = new LogsRange(); string[] arr = File.ReadAllLines(file); MatchRule mr = null; string FieldName = ""; string FieldValue = ""; int n = 0; Match m = null; PropertyInfo pi = null; object v = null; RuleType tag = RuleType.none; object entity = null; string s = "";// @"(?<FieldName>[^\{\=\,\s]+)\s*\=\s*((""(?<FieldValue>[^""\}\,]+)"")|(?<FieldValue>[^""\}\=\,\s]+))"; s = @"(?<FieldName>[^\{\=\,\s]+)\s*\=\s*((""(?<FieldValue>[^""]+)"")|(?<FieldValue>[^""\}\=\,\s]+))"; Regex rg = new Regex(s, RegexOptions.IgnoreCase); foreach (string item in arr) { s = item; n = 0; tag = RuleType.none; while (rg.IsMatch(s) && 20 > n) { m = rg.Match(s); FieldName = m.Groups["FieldName"].Value; FieldValue = m.Groups["FieldValue"].Value; if (0 == n) { pi = GetPropertyInfoByName(typeof(DbInfo), FieldName); tag = null != pi ? RuleType.DbInfo : tag; if (null == pi) { pi = GetPropertyInfoByName(typeof(MatchRule), FieldName); tag = null != pi ? RuleType.MatchRule : tag; if (RuleType.MatchRule == tag) { mr = new MatchRule(); } } if (null == pi) { pi = GetPropertyInfoByName(typeof(LogsRange), FieldName); tag = null != pi ? RuleType.LogsRange : tag; } } if (RuleType.DbInfo == tag) { pi = GetPropertyInfoByName(typeof(DbInfo), FieldName); entity = ImplementAdapter.dbInfo; } else if (RuleType.MatchRule == tag) { pi = GetPropertyInfoByName(typeof(MatchRule), FieldName); entity = mr; } else if (RuleType.LogsRange == tag) { pi = GetPropertyInfoByName(typeof(LogsRange), FieldName); entity = logsRange1; } if (null != pi) { v = DJTools.ConvertTo(FieldValue, pi.PropertyType); try { entity.GetType().GetProperty(pi.Name).SetValue(entity, v, null); } catch { } } s = s.Replace(m.Groups[0].Value, ""); n++; } if (RuleType.MatchRule == tag) { if (string.IsNullOrEmpty(mr.MatchImplExpression)) { continue; } if (string.IsNullOrEmpty(mr.InterFaceName)) { continue; } list.Add(new CKeyValue() { Key = mr.InterFaceName, Value = mr }); mr = null; } } errorLevels1.Clear(); ErrorLevels el1 = ErrorLevels.severe; bool bool1 = Enum.TryParse(logsRange1.upperLimit, out el1); if (!bool1) { el1 = ErrorLevels.severe; } errorLevels1.Add(el1); ErrorLevels el2 = ErrorLevels.debug; bool1 = Enum.TryParse(logsRange1.lowerLimit, out el2); if (!bool1) { el2 = ErrorLevels.debug; } errorLevels1.Add(el2); return(list); }
void ExecuteSqlCode(MethodInformation method, LeftSpaceLevel leftSpaceLevel, DataOptType dataOptType, string sqlVarName, string paraListVarName, string dbHelperVarName, string returnVarName, ref string code) { if (DataOptType.select == dataOptType) { return; } //method.append(ref code, leftSpaceLevel, ""); string methodName = ""; if (DataOptType.none == method.sqlExecType) { switch (dataOptType) { case DataOptType.insert: methodName = "insert"; break; case DataOptType.update: methodName = "update"; break; case DataOptType.delete: methodName = "delete"; break; } } else { methodName = Enum.GetName(typeof(DataOptType), method.sqlExecType); } string autCall = method.AutoCallVarName; string enabledBuffer = method.methodComponent.EnabledBuffer.ToString().ToLower(); enabledBuffer = method.methodComponent.IsAsync ? "true" : enabledBuffer; method.append(ref code, leftSpaceLevel, "if(null != {0})", dbHelperVarName); method.append(ref code, leftSpaceLevel, "{"); if (DataOptType.select == method.sqlExecType || (DataOptType.procedure == dataOptType && DJTools.IsBaseType(method.methodComponent.ResultType))) { string dbTag = DJTools.GetParaTagByDbDialect(DataAdapter.dbDialect); method.append(ref code, leftSpaceLevel + 1, "{0} = {0}.Replace(\"{1}\", \"{2}\");", sqlVarName, procParaSign, dbTag); method.append(ref code, leftSpaceLevel + 1, "dbHelper.query({0}, {1}, {2}, {3}, dataTable =>", autCall, sqlVarName, paraListVarName, enabledBuffer); method.append(ref code, leftSpaceLevel + 1, "{"); #region ***** action method.append(ref code, leftSpaceLevel + 2, "dataTable = null == dataTable ? new System.Data.DataTable() : dataTable;"); method.append(ref code, leftSpaceLevel + 2, "if(0 < dataTable.Rows.Count)"); method.append(ref code, leftSpaceLevel + 2, "{"); method.append(ref code, leftSpaceLevel + 3, "string valStr = System.DBNull.Value == dataTable.Rows[0][0] ? \"\" : dataTable.Rows[0][0].ToString();"); if (baseTypeWithResult(method)) { method.append(ref code, leftSpaceLevel + 3, "int tempVal = 0;"); method.append(ref code, leftSpaceLevel + 3, "int.TryParse(valStr, out tempVal);"); method.append(ref code, leftSpaceLevel + 3, "{0} = tempVal;", returnVarName); } else if (typeof(string) == method.methodComponent.ResultType) { method.append(ref code, leftSpaceLevel + 3, "{0} = valStr;", returnVarName); } else { method.append(ref code, leftSpaceLevel + 3, "{0}.TryParse(valStr, out {1});", method.methodComponent.ResultType.FullName, returnVarName); } method.append(ref code, leftSpaceLevel + 2, "}"); method.append(ref code, leftSpaceLevel + 2, "else"); method.append(ref code, leftSpaceLevel + 2, "{"); method.append(ref code, leftSpaceLevel + 3, "{0} = 0;", returnVarName); method.append(ref code, leftSpaceLevel + 2, "}"); if (null != method.methodComponent.ActionType) { method.append(ref code, leftSpaceLevel + 2, "{0}({1});", method.methodComponent.ActionParaName, returnVarName); } #endregion method.append(ref code, leftSpaceLevel + 1, "}, ref err);"); } else { method.append(ref code, leftSpaceLevel + 1, "{0} += dbHelper.{1}({2}, {3}, {4},{5}, resultNum =>", returnVarName, methodName, autCall, sqlVarName, paraListVarName, enabledBuffer); method.append(ref code, leftSpaceLevel + 1, "{"); #region ***** action method.append(ref code, leftSpaceLevel + 2, "{0} += resultNum;", returnVarName); if (null != method.methodComponent.ActionType) { method.append(ref code, leftSpaceLevel + 2, "{0}(resultNum);", method.methodComponent.ActionParaName, returnVarName); } #endregion method.append(ref code, leftSpaceLevel + 1, "}, ref err);"); } method.append(ref code, leftSpaceLevel + 1, ""); method.append(ref code, leftSpaceLevel + 1, "if(!string.IsNullOrEmpty(err))"); method.append(ref code, leftSpaceLevel + 1, "{"); method.append(ref code, leftSpaceLevel + 2, "throw new Exception(err);"); method.append(ref code, leftSpaceLevel + 1, "}"); method.append(ref code, leftSpaceLevel, "}"); }
public void GetSegmentFromSql(MethodInformation method, Type dataType, ref string insertFields, ref string insertParas, ref string updateSets, ref string procParas) { string dbTag = DJTools.GetParaTagByDbDialect(DataAdapter.dbDialect); string insertFields1 = ""; string insertParas1 = ""; string updateSets1 = ""; string procParas1 = ""; bool mbool = false; string[] _fields = method.fields; FieldsType fieldsType = method.fieldsType; _fields = null == _fields ? new string[] { } : _fields; Action <string, string, string> action = (fn1, fn2, fn_lower) => { mbool = true; foreach (string e in _fields) { if (e.ToLower().Equals(fn_lower)) { mbool = false; break; } } if (FieldsType.Contain == fieldsType && 0 < _fields.Length) { mbool = !mbool; } if (mbool) { fn2 = string.IsNullOrEmpty(fn2) ? fn1 : fn2; insertFields1 += "," + fn2; insertParas1 += "," + dbTag + fn1; updateSets1 += "," + fn2 + "=" + dbTag + fn1; procParas1 += "," + procParaSign + fn2 + "=" + dbTag + fn1; } }; string fn = ""; string fn_2 = ""; if (typeof(DataEntity <DataElement>) == dataType || typeof(List <DataEntity <DataElement> >) == dataType) { object paraVal = null; if (typeof(List <DataEntity <DataElement> >) == dataType) { List <DataEntity <DataElement> > dataElements1 = (List <DataEntity <DataElement> >)method.paraList[0].ParaValue; if (0 < dataElements1.Count) { paraVal = dataElements1[0]; } } else { if (null != method.paraList[0].ParaValue) { paraVal = (DataEntity <DataElement>)method.paraList[0].ParaValue; } } if (null == paraVal) { paraVal = new DataEntity <DataElement>(); } DataEntity <DataElement> dataElements = (DataEntity <DataElement>)paraVal; foreach (DataElement item in dataElements) { if (!string.IsNullOrEmpty(item.fieldNameOfSourceTable)) { continue; } fn = item.name.ToLower(); action(item.name, fn_2, fn); } } else if (null != dataType.GetInterface("IDictionary")) { // } else { Type t = dataType; if (typeof(Collections.IEnumerable) == dataType.GetInterface("IEnumerable")) { Type[] types = dataType.GetGenericArguments(); if (null != dataType.GetInterface("IList")) { t = types[0]; } else if (dataType.IsArray) { t = dataType.GetElementType(); } } PropertyInfo[] piArr = t.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo item in piArr) { fn_2 = FieldMapping.GetFieldMapping(item); fn = item.Name.ToLower(); action(item.Name, fn_2, fn); } } if (!string.IsNullOrEmpty(insertFields1)) { insertFields1 = insertFields1.Substring(1); insertParas1 = insertParas1.Substring(1); updateSets1 = updateSets1.Substring(1); procParas1 = procParas1.Substring(1); } insertFields = insertFields1; insertParas = insertParas1; updateSets = updateSets1; procParas = procParas1; }
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); }
/// <summary> /// 解析 insert / update 与接口方法参数名称对应的sql语句, 注:该参数必须是一个数据实体List或是单个数据实体, /// 例: insert into UserInfo values({UserInfos}), update UserInfo set {UserInfos} where id=@id /// {UserInfos} -- UserInfos 对应方法参数名称,例: int insert(List<UserInfo> UserInfos) /// </summary> /// <param name="method"></param> /// <param name="dataOptType"></param> /// <param name="sqlVarName"></param> /// <param name="sql"></param> public void AnalyzeSql(MethodInformation method, DataOptType dataOptType, string sqlVarName, ref string sql) { if (string.IsNullOrEmpty(sql)) { return; } if (dataOptType == DataOptType.select || dataOptType == DataOptType.delete) { return; } AuthenticateSql(ref sql); string sql1 = sql.ToLower(); string fn = ""; PList <Para> paras = method.paraList; Para para = null; foreach (Para item in paras) { if (DJTools.IsBaseType(item.ParaType)) { continue; } fn = "{" + item.ParaName.ToLower() + "}"; if (-1 != sql1.IndexOf(fn)) { fn = "{" + item.ParaName + "}"; para = item; break; } } if (null == para) { return; } string dbTag = DJTools.GetParaTagByDbDialect(DataAdapter.dbDialect); string insertFields = ""; string insertParas = ""; string updateSets = ""; string procParas = ""; if (dataOptType == DataOptType.insert) { Regex regex = new Regex(@"[0-9a-z_]+\s*\((?<insertFields>[0-9a-z_\[\]\,\s]+)\)", RegexOptions.IgnoreCase); if (regex.IsMatch(sql1)) { insertFields = regex.Match(sql1).Groups["insertFields"].Value; string[] arr = null; string fn1 = ""; if (-1 != insertFields.IndexOf(",")) { arr = insertFields.Split(','); } else { arr = new string[] { insertFields }; } foreach (var item in arr) { fn1 = item.Trim(); fn1 = fn1.Replace("[", ""); fn1 = fn1.Replace("]", ""); insertParas += "," + dbTag + fn1; } if (!string.IsNullOrEmpty(insertParas)) { insertParas = insertParas.Substring(1); sql = sql.Replace(fn, insertParas); return; } } } Type dataType = GetDataTypeByPara(para, fn); if (null != dataType) { if (DJTools.IsBaseType(dataType)) { return; } GetSegmentFromSql(method, dataType, ref insertFields, ref insertParas, ref updateSets, ref procParas); UseSegmentToReplaceTagInSql(para, dataOptType, insertFields, insertParas, updateSets, procParas, ref sql); } }
/// <summary> /// AOP机制,执行接口方法发生异常时调用,拦截异常信息 /// </summary> /// <param name="interfaceType">接口类型</param> /// <param name="implement">接口实例</param> /// <param name="methodName">当前执行的接口方法</param> /// <param name="paras">接口参数</param> /// <param name="ex">异常信息</param> /// <param name="errorLevels">异常信息等级: lesser[次要的], normal[普通的], dangerous[危险的], severe[严重的]</param> public virtual void ExecuteExcption(Type interfaceType, object implement, string methodName, PList <Para> paras, Exception ex, ErrorLevels errorLevels) { lock (_excObj) { LoadErrorLevel(); if (errorLevels < errorLevels1[0] || errorLevels > errorLevels1[1]) { return; } //异常处理 string rootPath = null == RootPath ? "" : RootPath; string dir = Path.Combine(rootPath, "Logs"); if (!Directory.Exists(dir)) { try { Directory.CreateDirectory(dir); } catch { } } if (!Directory.Exists(dir)) { return; } string txt = ""; string date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); string msg = date; DJTools.append(ref msg, "The exception level: [{0}]{1}", ((int)errorLevels).ToString(), Enum.GetName(typeof(ErrorLevels), errorLevels)); DJTools.append(ref msg, "Interface: {0}", (null == interfaceType ? "" : interfaceType.FullName)); DJTools.append(ref msg, "Instance: {0}", (null == implement ? "" : implement.GetType().FullName)); DJTools.append(ref msg, "Method name: {0}", (string.IsNullOrEmpty(methodName) ? "" : methodName)); DJTools.append(ref msg, "Parameters:"); if (null != paras) { foreach (var item in paras) { DJTools.append(ref msg, 1, "{0} = {1}\t{2}", item.ParaName, (null == item.ParaValue ? "emply" : item.ParaValue.ToString()), item.ParaTypeName); } } DJTools.append(ref msg, ""); DJTools.append(ref msg, ex.ToString()); string fName = "err_" + DateTime.Now.ToString("yyyyMMddHH") + ".txt"; string file = Path.Combine(dir, fName); if (File.Exists(file)) { txt = File.ReadAllText(file); txt = msg + "\r\n\r\n" + txt; } else { txt = msg; } File.WriteAllText(file, txt); } }