private void BindRemoveCloneFunction() { Func <PropertyInfo, bool> predicate = null; Func <DataFieldAttribute, bool> func2 = null; this.RemoveCloneFunction = null; if (this.LogicalDeleteField != null) { if (predicate == null) { predicate = p => p.Name == LogicalDeleteField.ObjectFieldName; } PropertyInfo property = this.PropertyInfos.FirstOrDefault <PropertyInfo>(predicate); if (func2 == null) { func2 = f => f.ObjectFieldName != LogicalDeleteField.ObjectFieldName; } DataFieldAttribute[] attributeArray = this.AllFields.Where <DataFieldAttribute>(func2).ToArray <DataFieldAttribute>(); Func <T, T, T> cloneFunction = this.GetCloneModelFunction((from t in attributeArray select t.ObjectFieldName).ToArray <string>()); this.RemoveCloneFunction = delegate(IDbContext context, T model) { DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(typeof(T)), true); object obj2 = model.GetValue(pkDynamicPropertyInfo.PropertyName); T local = context.QueryObject <T>(pkDynamicPropertyInfo.DataFieldName, obj2, false); if (local != null) { cloneFunction(model, local); property.SetValue(local, true, null); property.SetValue(model, true, null); } return(local); }; } }
public string ToFieldName(LambdaExpression field) { string dataFieldName = null; if (field != null) { MemberExpression body = field.Body as MemberExpression; UnaryExpression expression2 = field.Body as UnaryExpression; if ((body == null) && (expression2 != null)) { body = expression2.Operand as MemberExpression; } if (body != null) { Type[] genericArguments = field.Type.GetGenericArguments(); if ((body.Member.MemberType == MemberTypes.Property) && (body.Expression.NodeType == ExpressionType.Parameter)) { PropertyInfo member = body.Member as PropertyInfo; if (member != null) { dataFieldName = DbObjectTools.GetDataFieldName(genericArguments[0], member); } } } } if (string.IsNullOrEmpty(dataFieldName)) { throw new Exception("無法從 Lambda 表達式 {0} 找到其對應的字段名稱。"); } return(dataFieldName); }
private string ProcessValue(string valueStr, string propertyStr, Type type) { PropertyInfo property = DbObjectTools.GetProperty(type, propertyStr); if (((((property.PropertyType.Equals(typeof(byte)) || property.PropertyType.Equals(typeof(sbyte))) || (property.PropertyType.Equals(typeof(short)) || property.PropertyType.Equals(typeof(ushort)))) || ((property.PropertyType.Equals(typeof(int)) || property.PropertyType.Equals(typeof(uint))) || (property.PropertyType.Equals(typeof(long)) || property.PropertyType.Equals(typeof(ulong))))) || ((property.PropertyType.Equals(typeof(char)) || property.PropertyType.Equals(typeof(float))) || property.PropertyType.Equals(typeof(double)))) || property.PropertyType.Equals(typeof(decimal))) { return(valueStr); } if (property.PropertyType.Equals(typeof(bool))) { if ("TRUE".Equals(valueStr.ToUpper())) { return("1"); } if ("1".Equals(valueStr)) { return("1"); } return("0"); } if ("''".Equals(valueStr)) { valueStr = ""; } else { valueStr = valueStr.Replace("''", "'"); } return("'" + this.Escape(valueStr) + "'"); }
public virtual void Remove(string objXml) { DbObject obj2 = this.Parse(objXml); DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(obj2.GetType()), true); string str = obj2.GetValue(pkDynamicPropertyInfo.PropertyName).ToString(); this.InvokeGenericMethod(this.DbContext, obj2.GetType(), "Delete", new Type[] { typeof(string) }, new object[] { str }); }
private void SetLastUpdater(object obj, string userId) { DbObjectInfo dbObjectInfo = DbObjectTools.GetDbObjectInfo(obj.GetType()); if (DbObjectTools.GetDynamicPropertyInfo(obj.GetType(), "LastUpdateUserId") != null) { ((DbObject)obj).SetValue <string>("LastUpdateUserId", userId); } }
public static List <string> GetModelNames(Module module) { List <string> list = new List <string>(); foreach (Type type in module.GetTypes()) { DbObjectInfo dbObjectInfo = DbObjectTools.GetDbObjectInfo(type); list.Add(dbObjectInfo.TableName); } return(list); }
private string ProcessProperty(string propertyStr, Type type) { PropertyInfo property = DbObjectTools.GetProperty(type, propertyStr); if (property == null) { return(null); } DataFieldAttribute dataFieldAttribute = DbObjectTools.GetDataFieldAttribute(property); if (dataFieldAttribute == null) { return(null); } return(dataFieldAttribute.DataFieldName); }
public void Delete <T>(string id) where T : DbObject, new() { IDbObjectInfo <T> dbObjectInfo = this.DbRuleContext.GetDbObjectInfo <T>(); DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(typeof(T)), true); T model = this.QueryObject <T>(pkDynamicPropertyInfo.DataFieldName, id, true); dbObjectInfo.SetModifyDatetime(model); if (dbObjectInfo.IsLogicalDelete) { dbObjectInfo.SetLogicalDelete(model); this.DbObjectOperator.Update(model); } else { this.DbObjectOperator.Delete(model); } }
private void InitParameters <T>() where T : DbObject, new() { T local = Activator.CreateInstance <T>(); FieldInfo[] fields = DbObjectTools.GetFields(typeof(T)); string tableName = DbObjectTools.GetTableName(typeof(T)); foreach (FieldInfo info in fields) { DbParameterAttribute[] customAttributes = (DbParameterAttribute[])info.GetCustomAttributes(typeof(DbParameterAttribute), true); if ((customAttributes != null) && (customAttributes.Length > 0)) { string name = info.Name; object obj2 = info.GetValue(local) ?? ""; this.InitParameter(tableName, name, obj2); } } }
public virtual DbObject Update(User user, string objXml) { DbObject obj2 = this.Parse(objXml); DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(obj2.GetType()), true); string arg = obj2.GetValue(pkDynamicPropertyInfo.PropertyName).ToString(); SqlStringBuilder builder = new SqlStringBuilder(pkDynamicPropertyInfo.DataFieldName); builder.AppendFormat(" = '{0}'", arg); DbObject des = this.InvokeGenericMethod(this.DbContext, obj2.GetType(), "QueryObject", new Type[] { typeof(string), typeof(bool) }, new object[] { builder.ToString(), true }) as DbObject; if (user != null) { this.SetLastUpdater(obj2, user.Id); } A.Commons.Utils.CloneUtils.CloneObjectIgnoreId(obj2, des); this.InvokeGenericMethod(this.DbContext, obj2.GetType(), "Update", new Type[] { obj2.GetType() }, new object[] { des }); return(des); }
public override string ToSql(ExpressionConverter expressionRouter, params object[] skipParameters) { string condition = expressionRouter.ToCondition(this.Condition, skipParameters); if (this.selectField == null) { return(condition); } string fieldName = expressionRouter.ToFieldName(this.selectField); string viewOrTableName = DbObjectTools.GetViewOrTableName(typeof(T)); viewOrTableName = expressionRouter.SqlFormatter.FormatTableOrViewName(viewOrTableName); fieldName = expressionRouter.SqlFormatter.FormatFieldName(fieldName); if (this.methodFormat != null) { fieldName = string.Format(this.methodFormat, fieldName); } return(expressionRouter.SqlFormatter.FormatSelectSql(viewOrTableName, fieldName, condition)); }
private string MemberAccessToSql(MemberExpression m, ParameterExpression[] parameters, object[] parameterValues, ParameterExpression parameter) { if (((m.Member.MemberType == MemberTypes.Property) && (m.Expression != null)) && (m.Expression.NodeType == ExpressionType.Parameter)) { ParameterExpression expression = m.Expression as ParameterExpression; if (((parameters.Length == 1) || (expression == parameter)) || (expression.Name == parameter.Name)) { ParameterExpression expression2 = m.Expression as ParameterExpression; PropertyInfo member = m.Member as PropertyInfo; if (member != null) { DynamicPropertyInfo dynamicPropertyInfo = DbObjectTools.GetDynamicPropertyInfo(expression2.Type, member); if (dynamicPropertyInfo != null) { return(this.SqlFormatter.FormatFieldName(dynamicPropertyInfo.DataFieldName)); } } } } return(this.FormatValue(m, parameters, parameterValues)); }
private void BindUpdateCloneFunction() { this.UpdateCloneFunction = null; if (this.UpdateExceptFields.Count > 0) { Func <T, T, T> cloneFunction = this.GetCloneModelFunction((from t in this.UpdateExceptFields select t.ObjectFieldName).ToArray <string>()); this.UpdateCloneFunction = delegate(IDbContext context, T model, string[] updateFields) { Func <T, T, T> cloneModelFunction = cloneFunction; if (updateFields != null) { cloneModelFunction = this.GetCloneModelFunction((from t in this.AllFields where this.UpdateExceptFields.Contains(t) || (!t.ReadOnly && !updateFields.Contains <string>(t.ObjectFieldName)) select t.ObjectFieldName).ToArray <string>()); } DynamicPropertyInfo pkDynamicPropertyInfo = DbObjectTools.GetPkDynamicPropertyInfo(DbObjectTools.GetDbObjectInfo(typeof(T)), true); object obj2 = model.GetValue(pkDynamicPropertyInfo.PropertyName); T local = context.QueryObject <T>(pkDynamicPropertyInfo.DataFieldName, obj2, true); cloneModelFunction(model, local); return(local); }; } }
private IRuleMapper <T> Init <T>(IRuleMapper <T> ruleMapper = null) where T : DbObject, new() { Func <object, object> item = null; Func <object, object> func4 = null; Func <object, object> func5 = null; Func <object, bool> func6 = null; ruleMapper = ruleMapper ?? new RuleEngine <T>(); Type type = typeof(T); foreach (PropertyInfo info1 in type.GetProperties()) { Func <object, object> func = null; Func <object, object> func2 = null; List <Func <object, object> > funcs; MethodInfo getMethod; string fieldName; DynamicPropertyInfo info = DbObjectTools.GetDynamicPropertyInfo(type, info1); if (info != null) { Type propertyType = info.PropertyType; funcs = new List <Func <object, object> >(); if (!info.AllowDBNull) { if (item == null) { item = o => ValidateExtend.NotNull <object>(o); } funcs.Add(item); } if ((propertyType == typeof(string)) && (info.Length > 0)) { if (func == null) { func = delegate(object o) { if (o != null) { ValidateExtend.LengthBelowOrEqual((string)o, info.Length, false); } return(o); }; } funcs.Add(func); } if (((propertyType != typeof(DateTime)) && (propertyType != typeof(DateTime?))) && ((info.NumericPercision != -1) || (info.NumericScale != -1))) { if (func2 == null) { func2 = delegate(object o) { if (o != null) { ValidateExtend.IsDecimal <object>(o, info.NumericPercision, info.NumericScale); } return(o); }; } funcs.Add(func2); } if (propertyType == typeof(DateTime)) { if (func4 == null) { func4 = delegate(object o) { if (o != null) { ValidateExtend.GreaterThanOrEqual <DateTime>((DateTime)o, SqlDateTime.MinValue.Value); } return(o); }; } funcs.Add(func4); } if (propertyType == typeof(DateTime?)) { if (func5 == null) { func5 = delegate(object o) { if (o != null) { DateTime?nullable = (DateTime?)o; ValidateExtend.GreaterThanOrEqual <DateTime>(nullable.Value, SqlDateTime.MinValue.Value); } return(o); }; } funcs.Add(func5); } List <FieldRule> list = null; if (!ruleMapper.FieldRules.TryGetValue(info1.Name, out list)) { list = new List <FieldRule>(); ruleMapper.FieldRules.Add(info1.Name, list); } getMethod = info1.GetGetMethod(); fieldName = info1.Name; FieldRule rule2 = new FieldRule(); rule2.GetField = (t => getMethod.Invoke(t, null)); rule2.Validate = (delegate(object o, string action) { Action <Func <object, object> > action2 = null; object obj2; try { if (action2 == null) { action2 = delegate(Func <object, object> f) { f(o); }; } funcs.ForEach(action2); obj2 = o; } catch (ValidateException exception) { throw new FieldValidateException(exception.Message, fieldName, exception.FieldValue); } return(obj2); }); rule2.Actions = (new string[] { DbOperation.Create.ToString(), (DbOperation.None | DbOperation.Update).ToString() }); if (func6 == null) { func6 = t => true; } rule2.When = (func6); FieldRule rule = rule2; list.Insert(0, rule); } } return(ruleMapper); }