public static DataEntity <DataElement> GetDynamicEntityBy(this DataRow dataRow) { DataEntity <DataElement> dataElements = new DataEntity <DataElement>(); if (null == dataRow) { return(dataElements); } foreach (DataColumn dc in dataRow.Table.Columns) { dataElements.Add(dc.ColumnName, dataRow[dc.ColumnName]); } return(dataElements); }
public static List <DataEntity <DataElement> > GetDynamicEntitiesBy(this DataTable dataTable) { List <DataEntity <DataElement> > dataElements = new List <DataEntity <DataElement> >(); if (null == dataTable) { return(dataElements); } DataEntity <DataElement> dataElements1 = null; foreach (DataRow item in dataTable.Rows) { dataElements1 = item.GetDynamicEntityBy(); dataElements.Add(dataElements1); } return(dataElements); }
void dynamicWhere(MethodInformation method, ref string sql) { if (string.IsNullOrEmpty(sql)) { return; } if (null == method.paraList) { return; } if (0 == method.paraList.Count) { return; } Para para = method.paraList[0]; DataEntity <DataElement> dataElements = para.ParaValue as DataEntity <DataElement>; if (null == dataElements) { return; } Regex rg = new Regex(@"\swhere\s+\{" + para.ParaName + @"\}", RegexOptions.IgnoreCase); if (!rg.IsMatch(sql)) { return; } string whereStr = getDynamicWhere(method, dataElements); whereStr = string.IsNullOrEmpty(whereStr) ? "1=1" : whereStr; whereStr = " where " + whereStr; sql = sql.Replace(rg.Match(sql).Groups[0].Value, whereStr); }
public IEnumeratorImpl(DataEntity <TT> dataEntity) { this.dataEntity = dataEntity; }
object dataTableTo(MethodInformation method, DataTable dt, Type paraType) { object result = null; if (null == dt) { return(result); } if (0 == dt.Rows.Count) { return(result); } object v = null; if (DJTools.IsBaseType(paraType)) { v = dt.Rows[0][0]; v = DJTools.ConvertTo(v, paraType); } else if (typeof(DataEntity <DataElement>) == paraType) { DataEntity <DataElement> dataElements = new DataEntity <DataElement>(); DataRow dr = dt.Rows[0]; DataColumnCollection columns = dt.Columns; foreach (DataColumn item in columns) { if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) && 0 < method.methodComponent.Interval) { Thread.Sleep(method.methodComponent.Interval); } v = dr[item.ColumnName]; dataElements.Add(item.ColumnName, v); } v = dataElements; } else if (typeof(List <DataEntity <DataElement> >) == paraType || typeof(IList <DataEntity <DataElement> >) == paraType) { List <DataEntity <DataElement> > dataElements = new List <DataEntity <DataElement> >(); DataEntity <DataElement> dataElements1 = null; DataColumnCollection columns = dt.Columns; foreach (DataRow dr in dt.Rows) { if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) && 0 < method.methodComponent.Interval) { Thread.Sleep(method.methodComponent.Interval); } dataElements1 = new DataEntity <DataElement>(); foreach (DataColumn dc in columns) { if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) && 0 < method.methodComponent.Interval) { Thread.Sleep(method.methodComponent.Interval); } v = dr[dc.ColumnName]; dataElements1.Add(dc.ColumnName, v); } dataElements.Add(dataElements1); } v = dataElements; } else if (typeof(DataTable) == paraType) { v = dt; } else if (typeof(DataSet) == paraType) { v = new DataSet(); ((DataSet)v).Tables.Add(dt); } else if (typeof(Collections.IEnumerable) == paraType.GetInterface("IEnumerable") && typeof(string) != paraType) { if (typeof(IList) == paraType.GetInterface("IList") || typeof(Array) == paraType.BaseType) { if (paraType.BaseType == typeof(Array)) { v = createArrayByType(paraType, dt.Rows.Count); arrayAdd(method, v, dt); } else { v = createListByType(paraType); listAdd(method, v, dt); } } else { //Dictionary } } else { v = Activator.CreateInstance(paraType); DataRowToEntity(method, dt.Rows[0], v); } if (null != v) { result = v; } return(result); }
DbList <DbParameter> GetDbParameters(MethodInformation method, DataEntity <DataElement> dataElements, string sql) { DbList <DbParameter> dbParameters = new DbList <DbParameter>(); if (string.IsNullOrEmpty(sql)) { return(dbParameters); } if (null == dataElements) { return(dbParameters); } string msg = ""; Regex rg = DynamicCodeChange.rgParaField; if (rg.IsMatch(sql) && 0 < method.paraList.Count) { if (null == method.paraList[0].ParaValue) { msg = "The parameter value cann't be null."; ((AutoCall)method.AutoCall).e(msg); throw new Exception(msg); } string LeftSign = ""; string DbTag = ""; string FieldName = ""; string EndSign = ""; object v = null; string sql1 = sql; Match m = null; int num = 0; //DataEntity<DataElement> dataElements = (DataEntity<DataElement>)method.paraList[0].ParaValue; MatchCollection mc = rg.Matches(sql); while (rg.IsMatch(sql1) && 1000 > num) { if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) && 0 < method.methodComponent.Interval) { Thread.Sleep(method.methodComponent.Interval); } m = rg.Match(sql1); LeftSign = m.Groups["LeftSign"].Value; if (!DynamicCodeChange.isEnabledField(LeftSign)) { continue; } DbTag = m.Groups["DbTag"].Value; FieldName = m.Groups["FieldName"].Value; EndSign = m.Groups["EndSign"].Value; v = dataElements[FieldName].value; dbParameters.Add(FieldName, v); sql1 = sql1.Replace(m.Groups[0].Value, ""); sql1 = EndSign + sql1; num++; } } return(dbParameters); }
string getDynamicWhere(MethodInformation method, DataEntity <DataElement> dataElements) { string sWhere = ""; string sWhere1 = ""; string sVal = ""; bool mbool = false; foreach (var item in dataElements) { if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) && 0 < method.methodComponent.Interval) { Thread.Sleep(method.methodComponent.Interval); } if (!string.IsNullOrEmpty(item.fieldNameOfSourceTable)) { mbool = true; if (null != item.IsEnabledContion) { if (!item.IsEnabledContion(item)) { mbool = false; } } if (mbool) { if (!string.IsNullOrEmpty(sWhere)) { sWhere += " " + Enum.GetName(typeof(AndOr), item.logicUnion) + " "; } sVal = item.ToString(); if (item.compareSign.ToLower().Contains("like")) { if (db_dialect.oracle == DataAdapter.dbDialect) { sWhere += "0<instr(" + item.fieldNameOfSourceTable + ",'" + sVal + "')"; } else { sVal = "'%" + sVal + "%'"; sWhere += item.fieldNameOfSourceTable + " " + item.compareSign + " " + sVal; } } else { if (item.valueIsChar) { sVal = "'" + sVal + "'"; } sWhere += item.fieldNameOfSourceTable + " " + item.compareSign + " " + sVal; } } } if (0 < item.groupCondition.Count) { sWhere1 = getDynamicWhere(method, item.groupCondition); if (!string.IsNullOrEmpty(sWhere1)) { if (!string.IsNullOrEmpty(sWhere)) { sWhere += " " + Enum.GetName(typeof(AndOr), item.groupLogicUnion) + " (" + sWhere1 + ")"; } else { sWhere = "(" + sWhere1 + ")"; } } } } return(sWhere); }
public T Exec <T>(MethodInformation method, DataOptType dataOptType, Action <T> action, string sql) { T result = default(T); AutoCall autoCall = (AutoCall)method.AutoCall; dynamicWhere(method, ref sql); IList <DataEntity <DataElement> > dataElements = null; DataEntity <DataElement> dataElements1 = null; DbList <DbParameter> dbParameters = null; if (0 < method.paraList.Count) { foreach (Para item in method.paraList) { if (null != item.ParaValue) { if (null != item.ParaValue as IList <DataEntity <DataElement> > ) { dataElements = item.ParaValue as IList <DataEntity <DataElement> >; break; } else if (null != item.ParaValue as DataEntity <DataElement> ) { dataElements = new List <DataEntity <DataElement> >(); dataElements1 = item.ParaValue as DataEntity <DataElement>; dataElements.Add(dataElements1); break; } } } } if (null == dataElements) { dataElements = new List <DataEntity <DataElement> >(); } string err = ""; bool EnabledBuffer = method.methodComponent.EnabledBuffer; EnabledBuffer = method.methodComponent.IsAsync ? true : EnabledBuffer; if (DataOptType.select == dataOptType || DataOptType.count == dataOptType || DataOptType.procedure == dataOptType) { if (0 < dataElements.Count) { dataElements1 = dataElements[0]; } dbParameters = GetDbParameters(method, dataElements1, sql); GetSqlByProvider(method, dbParameters, ref sql); dbHelper.query(autoCall, sql, dbParameters, EnabledBuffer, (dt) => { DynamicCodeAutoCall dynamicCodeAutoCall = new DynamicCodeAutoCall(); string execClassPath = ""; string execMethodName = ""; bool paraExsit = false; Type returnType = dynamicCodeAutoCall.GetParaTypeOfResultExecMethod(method, ref execClassPath, ref execMethodName, ref paraExsit); if (paraExsit) { Type resultExecMethodType = execClassPath.GetClassTypeByPath(); if (null == resultExecMethodType) { err = "The ClassPath '" + execClassPath + "' is not exist."; autoCall.e(err, ErrorLevels.dangerous); throw new Exception(err); } object clsObj = null; MethodInfo methodInfo = resultExecMethodType.GetMethod(execMethodName); if (null != methodInfo) { if (methodInfo.ReturnType != typeof(T)) { err = ""; string msg = "The return value type of the method '{0}' of the class '{1}' and the method '{2}' of the interface '{3}' are not the same."; method.append(ref err, 0, msg, execMethodName, execClassPath, method.methodInfo.Name, method.methodInfo.DeclaringType.FullName); autoCall.e(err, ErrorLevels.dangerous); throw new Exception(err); } object vData = dataTableTo(method, dt, returnType); try { clsObj = Activator.CreateInstance(resultExecMethodType); result = (T)methodInfo.Invoke(clsObj, new object[] { vData }); } catch (Exception ex) { autoCall.e(ex.ToString(), ErrorLevels.dangerous); //throw; } } } else { result = dataTableTo <T>(method, dt); } if (null != action) { action(result); } }, ref err); } else { int n = 0; Func <int, T> funcResult = n1 => { object v = n1; if (typeof(bool) == typeof(T)) { v = 0 < n1; } return((T)v); }; foreach (DataEntity <DataElement> item in dataElements) { dbParameters = GetDbParameters(method, item, sql); switch (dataOptType) { case DataOptType.insert: dbHelper.insert(autoCall, sql, dbParameters, EnabledBuffer, (num) => { n += num; result = funcResult(n); if (null != action) { action(result); } }, ref err); break; case DataOptType.update: dbHelper.update(autoCall, sql, dbParameters, EnabledBuffer, (num) => { n += num; result = funcResult(n); if (null != action) { action(result); } }, ref err); break; case DataOptType.delete: dbHelper.delete(autoCall, sql, dbParameters, EnabledBuffer, (num) => { n += num; result = funcResult(n); if (null != action) { action(result); } }, ref err); break; } } result = funcResult(n); } return(result); }