protected override Boolean Valid(RedisMessageQueue entity, DataObjectMethodType type, Boolean post)
        {
            if (!post)
            {
                return(base.Valid(entity, type, post));
            }

            // 必须提前写修改日志,否则修改后脏数据失效,保存的日志为空
            if (type == DataObjectMethodType.Update && (entity as IEntity).HasDirty)
            {
                LogProvider.Provider.WriteLog(type + "", entity);
            }

            var err = "";

            try
            {
                return(base.Valid(entity, type, post));
            }
            catch (Exception ex)
            {
                err = ex.Message;
                throw;
            }
            finally
            {
                if (type != DataObjectMethodType.Update)
                {
                    LogProvider.Provider.WriteLog(type + "", entity, err);
                }
            }
        }
        private void AddCustomAttributesToMethod(CodeMemberMethod dbMethod)
        {
            if (base.methodSource.EnableWebMethods)
            {
                CodeAttributeDeclaration declaration = new CodeAttributeDeclaration("System.Web.Services.WebMethod");
                declaration.Arguments.Add(new CodeAttributeArgument("Description", CodeGenHelper.Str(base.methodSource.WebMethodDescription)));
                dbMethod.CustomAttributes.Add(declaration);
            }
            DataObjectMethodType select = DataObjectMethodType.Select;

            if (base.methodSource.CommandOperation == CommandOperation.Update)
            {
                select = DataObjectMethodType.Update;
            }
            else if (base.methodSource.CommandOperation == CommandOperation.Delete)
            {
                select = DataObjectMethodType.Delete;
            }
            else if (base.methodSource.CommandOperation == CommandOperation.Insert)
            {
                select = DataObjectMethodType.Insert;
            }
            if (select != DataObjectMethodType.Select)
            {
                dbMethod.CustomAttributes.Add(new CodeAttributeDeclaration(CodeGenHelper.GlobalType(typeof(DataObjectMethodAttribute)), new CodeAttributeArgument[] { new CodeAttributeArgument(CodeGenHelper.Field(CodeGenHelper.GlobalTypeExpr(typeof(DataObjectMethodType)), select.ToString())), new CodeAttributeArgument(CodeGenHelper.Primitive(false)) }));
            }
        }
        public void Ctor_MethodType_IsDefault(DataObjectMethodType methodType, bool isDefault)
        {
            var attribute = new DataObjectMethodAttribute(methodType, isDefault);

            Assert.Equal(methodType, attribute.MethodType);
            Assert.Equal(isDefault, attribute.IsDefault);
        }
Exemplo n.º 4
0
        private void AddCustomAttributesToMethod(CodeMemberMethod dbMethod)
        {
            bool primitive            = false;
            DataObjectMethodType fill = DataObjectMethodType.Fill;

            if (base.methodSource.EnableWebMethods && base.getMethod)
            {
                CodeAttributeDeclaration declaration = new CodeAttributeDeclaration("System.Web.Services.WebMethod");
                declaration.Arguments.Add(new CodeAttributeArgument("Description", CodeGenHelper.Str(base.methodSource.WebMethodDescription)));
                dbMethod.CustomAttributes.Add(declaration);
            }
            if (!base.GeneratePagingMethod && (base.getMethod || (base.ContainerParameterType == typeof(DataTable))))
            {
                if (base.MethodSource == base.DesignTable.MainSource)
                {
                    primitive = true;
                }
                if (base.getMethod)
                {
                    fill = DataObjectMethodType.Select;
                }
                else
                {
                    fill = DataObjectMethodType.Fill;
                }
                dbMethod.CustomAttributes.Add(new CodeAttributeDeclaration(CodeGenHelper.GlobalType(typeof(DataObjectMethodAttribute)), new CodeAttributeArgument[] { new CodeAttributeArgument(CodeGenHelper.Field(CodeGenHelper.GlobalTypeExpr(typeof(DataObjectMethodType)), fill.ToString())), new CodeAttributeArgument(CodeGenHelper.Primitive(primitive)) }));
            }
        }
Exemplo n.º 5
0
        /// <summary>把SQL模版格式化为SQL语句</summary>
        /// <param name="entity">实体对象</param>
        /// <param name="methodType"></param>
        /// <param name="parameters">参数数组</param>
        /// <returns>SQL字符串</returns>
        String SQL(IEntity entity, DataObjectMethodType methodType, ref IDataParameter[] parameters)
        {
            //var op = EntityFactory.CreateOperate(entity.GetType());
            //var formatedTalbeName = op.FormatedTableName;

            //String sql;

            switch (methodType)
            {
            //case DataObjectMethodType.Fill:
            //    return String.Format("Select * From {0}", formatedTalbeName);
            //case DataObjectMethodType.Select:
            //    sql = DefaultCondition(entity);
            //    // 没有标识列和主键,返回取所有数据的语句
            //    if (String.IsNullOrEmpty(sql)) throw new XCodeException("实体类缺少主键!");
            //    return String.Format("Select * From {0} Where {1}", formatedTalbeName, sql);
            case DataObjectMethodType.Insert:
                return(InsertSQL(entity, ref parameters));

            case DataObjectMethodType.Update:
                return(UpdateSQL(entity, ref parameters));

            case DataObjectMethodType.Delete:
                return(DeleteSQL(entity, ref parameters));
            }
            return(null);
        }
Exemplo n.º 6
0
        private void AddCustomAttributesToMethod(CodeMemberMethod dbMethod)
        {
            DataObjectMethodType update = DataObjectMethodType.Update;

            if (base.methodSource.EnableWebMethods)
            {
                CodeAttributeDeclaration declaration = new CodeAttributeDeclaration("System.Web.Services.WebMethod");
                declaration.Arguments.Add(new CodeAttributeArgument("Description", CodeGenHelper.Str(base.methodSource.WebMethodDescription)));
                dbMethod.CustomAttributes.Add(declaration);
            }
            if (base.MethodType != MethodTypeEnum.GenericUpdate)
            {
                if (base.activeCommand == base.methodSource.DeleteCommand)
                {
                    update = DataObjectMethodType.Delete;
                }
                else if (base.activeCommand == base.methodSource.InsertCommand)
                {
                    update = DataObjectMethodType.Insert;
                }
                else if (base.activeCommand == base.methodSource.UpdateCommand)
                {
                    update = DataObjectMethodType.Update;
                }
                dbMethod.CustomAttributes.Add(new CodeAttributeDeclaration(CodeGenHelper.GlobalType(typeof(DataObjectMethodAttribute)), new CodeAttributeArgument[] { new CodeAttributeArgument(CodeGenHelper.Field(CodeGenHelper.GlobalTypeExpr(typeof(DataObjectMethodType)), update.ToString())), new CodeAttributeArgument(CodeGenHelper.Primitive(true)) }));
            }
        }
        public void Ctor_MethodType(DataObjectMethodType methodType)
        {
            var attribute = new DataObjectMethodAttribute(methodType);

            Assert.Equal(methodType, attribute.MethodType);
            Assert.False(attribute.IsDefault);
        }
Exemplo n.º 8
0
 /// <summary>把SQL模版格式化为SQL语句</summary>
 /// <param name="session">实体会话</param>
 /// <param name="entity">实体对象</param>
 /// <param name="methodType"></param>
 /// <param name="parameters">参数数组</param>
 /// <returns>SQL字符串</returns>
 String SQL(IEntitySession session, IEntity entity, DataObjectMethodType methodType, ref IDataParameter[] parameters)
 {
     return(methodType switch
     {
         DataObjectMethodType.Insert => InsertSQL(session, entity, ref parameters),
         DataObjectMethodType.Update => UpdateSQL(session, entity, ref parameters),
         DataObjectMethodType.Delete => DeleteSQL(session, entity, ref parameters),
         _ => null,
     });
Exemplo n.º 9
0
        /// <summary>修改数据时,唤醒作业服务跟进</summary>
        /// <param name="entity"></param>
        /// <param name="type"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        protected override Boolean Valid(CronJob entity, DataObjectMethodType type, Boolean post)
        {
            if (post)
            {
                JobService.Wake();
            }

            return(base.Valid(entity, type, post));
        }
Exemplo n.º 10
0
 /// <summary>把SQL模版格式化为SQL语句</summary>
 /// <param name="entity">实体对象</param>
 /// <param name="methodType"></param>
 /// <param name="parameters">参数数组</param>
 /// <returns>SQL字符串</returns>
 String SQL(IEntity entity, DataObjectMethodType methodType, ref IDataParameter[] parameters)
 {
     switch (methodType)
     {
         case DataObjectMethodType.Insert: return InsertSQL(entity, ref parameters);
         case DataObjectMethodType.Update: return UpdateSQL(entity, ref parameters);
         case DataObjectMethodType.Delete: return DeleteSQL(entity, ref parameters);
     }
     return null;
 }
Exemplo n.º 11
0
        /// <summary>验证实体对象</summary>
        /// <param name="entity"></param>
        /// <param name="type"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        protected override Boolean Valid(User entity, DataObjectMethodType type, Boolean post)
        {
            if (!post && type == DataObjectMethodType.Update)
            {
                // 清空密码,不向浏览器输出
                //entity.Password = null;
                entity["Password"] = null;
            }

            return(base.Valid(entity, type, post));
        }
Exemplo n.º 12
0
        protected override Boolean Valid(PrincipalAgent entity, DataObjectMethodType type, Boolean post)
        {
            if (!post && type == DataObjectMethodType.Insert)
            {
                entity.Enable = true;
                entity.Times  = 1;
                entity.Expire = DateTime.Now.AddMinutes(20);
            }

            return(base.Valid(entity, type, post));
        }
Exemplo n.º 13
0
        private MethodInfo GetDataMethod(DataObjectMethodType methodType, string methodName)
        {
            if (methodName == null)
            {
                methodName = String.Empty;
            }
            Type      thisType = this.GetType();
            Hashtable methods  = _dataMethodCache[thisType] as Hashtable;

            if (methods == null)
            {
                methods = Hashtable.Synchronized(new Hashtable());
                _dataMethodCache[thisType] = methods;
            }
            Hashtable infos = methods[methodType] as Hashtable;

            if (infos == null)
            {
                infos = Hashtable.Synchronized(new Hashtable());
                methods[methodType] = infos;
            }
            MethodInfo method;

            if (!infos.ContainsKey(methodName))
            {
                MemberFilter filter      = new MemberFilter(FilterMethodByDataObjectMethodType);
                MemberInfo[] dataMethods = thisType.FindMembers(
                    MemberTypes.Method,
                    PublicBindingFlags,
                    filter,
                    new MethodTypeAndName(methodType, methodName, false));
                if (dataMethods.Length == 0)
                {
                    dataMethods = thisType.FindMembers(
                        MemberTypes.Method,
                        PublicBindingFlags,
                        filter,
                        new MethodTypeAndName(methodType, methodName, true));
                    if (dataMethods.Length == 0)
                    {
                        infos[methodName] = null;
                        return(null);
                    }
                }
                method            = (MethodInfo)dataMethods[0];
                infos[methodName] = method;
            }
            else
            {
                method = (MethodInfo)infos[methodName];
            }
            return(method);
        }
Exemplo n.º 14
0
        /// <summary>验证实体对象</summary>
        /// <param name="entity"></param>
        /// <param name="type"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        protected override Boolean Valid(Menu entity, DataObjectMethodType type, Boolean post)
        {
            var rs = base.Valid(entity, type, post);

            // 清空缓存
            if (post)
            {
                Menu.Meta.Session.ClearCache($"{type}-{entity}", true);
            }

            return(rs);
        }
Exemplo n.º 15
0
        protected override Boolean Valid(Story entity, DataObjectMethodType type, Boolean post)
        {
            // 默认当前用户作为负责人
            if (!post && type == DataObjectMethodType.Insert)
            {
                var member = Member.FindByUserId(ManageProvider.User.ID);
                if (member != null)
                {
                    entity.MemberId = member.ID;
                }
            }

            return(base.Valid(entity, type, post));
        }
Exemplo n.º 16
0
        /// <summary>验证权限</summary>
        /// <param name="entity">实体对象</param>
        /// <param name="type">操作类型</param>
        /// <param name="post">是否提交数据阶段</param>
        /// <returns></returns>
        protected override Boolean ValidPermission(UserToken entity, DataObjectMethodType type, Boolean post)
        {
            var user = ManageProvider.Provider?.Current;

            // 系统角色拥有特权
            if (user is IUser user2 && user2.Roles.Any(e => e.IsSystem))
            {
                return(true);
            }

            // 特殊处理添加操作
            if (type == DataObjectMethodType.Insert && entity.UserID <= 0)
            {
                entity.UserID = user.ID;
            }

            return(entity.UserID == user.ID);
        }
Exemplo n.º 17
0
        /// <summary>验证实体对象</summary>
        /// <param name="entity">实体对象</param>
        /// <param name="type">操作类型</param>
        /// <param name="post">是否提交数据阶段</param>
        /// <returns></returns>
        protected virtual Boolean Valid(TEntity entity, DataObjectMethodType type, Boolean post)
        {
            if (!ValidPermission(entity, type, post))
            {
                switch (type)
                {
                case DataObjectMethodType.Select: throw new NoPermissionException(PermissionFlags.Detail, "无权查看数据");

                case DataObjectMethodType.Update: throw new NoPermissionException(PermissionFlags.Update, "无权更新数据");

                case DataObjectMethodType.Insert: throw new NoPermissionException(PermissionFlags.Insert, "无权新增数据");

                case DataObjectMethodType.Delete: throw new NoPermissionException(PermissionFlags.Delete, "无权删除数据");
                }
            }

            return(true);
        }
Exemplo n.º 18
0
        private MethodInfo GetMethod(string methodName, DataObjectMethodType methodType, int parametersCount)
        {
            List <MethodInfo> methods = new List <MethodInfo>();

            if (!string.IsNullOrEmpty(methodName))
            {
                foreach (MethodInfo method in Type.GetMethods())
                {
                    if (String.Compare(method.Name, methodName, true) == 0)
                    {
                        methods.Add(method);
                    }
                }
            }
            else
            {
                foreach (MethodInfo method in Type.GetMethods())
                {
                    object[] attributes = method.GetCustomAttributes(typeof(DataObjectMethodAttribute), true);
                    if (attributes.Length > 0)
                    {
                        foreach (DataObjectMethodAttribute attribute in attributes)
                        {
                            if (attribute.MethodType == methodType)
                            {
                                methods.Add(method);
                            }
                        }
                    }
                }
            }
            if (methods.Count == 0)
            {
                return(null);
            }
            foreach (MethodInfo method in methods)
            {
                if (method.GetParameters().Length == parametersCount)
                {
                    return(method);
                }
            }
            return(methods[0]);
        }
Exemplo n.º 19
0
        /// <summary>验证实体对象</summary>
        /// <param name="entity"></param>
        /// <param name="type"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        protected override Boolean Valid(User entity, DataObjectMethodType type, Boolean post)
        {
            if (!post && type == DataObjectMethodType.Update)
            {
                // 清空密码,不向浏览器输出
                //entity.Password = null;
                entity["Password"] = null;
            }

            if (post && type == DataObjectMethodType.Update)
            {
                if ((entity as IEntity).Dirtys["Password"])
                {
                    entity.Password = ManageProvider.Provider.PasswordProvider.Hash(entity.Password);
                }
            }

            return(base.Valid(entity, type, post));
        }
 private bool FilterMethod(System.Reflection.MethodInfo methodInfo, DataObjectMethodType methodType)
 {
     if (methodType == DataObjectMethodType.Select)
     {
         if (methodInfo.ReturnType == typeof(void))
         {
             return false;
         }
     }
     else
     {
         ParameterInfo[] parameters = methodInfo.GetParameters();
         if ((parameters == null) || (parameters.Length == 0))
         {
             return false;
         }
     }
     return true;
 }
Exemplo n.º 21
0
 private bool FilterMethod(System.Reflection.MethodInfo methodInfo, DataObjectMethodType methodType)
 {
     if (methodType == DataObjectMethodType.Select)
     {
         if (methodInfo.ReturnType == typeof(void))
         {
             return(false);
         }
     }
     else
     {
         ParameterInfo[] parameters = methodInfo.GetParameters();
         if ((parameters == null) || (parameters.Length == 0))
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 22
0
        /// <summary>修改数据时,唤醒作业服务跟进</summary>
        /// <param name="entity"></param>
        /// <param name="type"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        protected override Boolean Valid(CronJob entity, DataObjectMethodType type, Boolean post)
        {
            if (post)
            {
                var cron = new Cron();
                if (!cron.Parse(entity.Cron))
                {
                    throw new ArgumentException("Cron表达式有误!", nameof(entity.Cron));
                }

                // 重算下一次的时间
                if (entity is IEntity e && !e.Dirtys[nameof(entity.Name)])
                {
                    entity.NextTime = cron.GetNext(DateTime.Now);
                }

                JobService.Wake();
            }

            return(base.Valid(entity, type, post));
        }
Exemplo n.º 23
0
        protected override Boolean Valid(App entity, DataObjectMethodType type, Boolean post)
        {
            if (!post)
            {
                return(base.Valid(entity, type, post));
            }

            var act = type switch
            {
                DataObjectMethodType.Update => "修改",
                DataObjectMethodType.Insert => "添加",
                DataObjectMethodType.Delete => "删除",
                _ => type + "",
            };

            // 必须提前写修改日志,否则修改后脏数据失效,保存的日志为空
            if (type == DataObjectMethodType.Update && (entity as IEntity).HasDirty)
            {
                LogProvider.Provider.WriteLog(act, entity);
            }

            var err = "";

            try
            {
                return(base.Valid(entity, type, post));
            }
            catch (Exception ex)
            {
                err = ex.Message;
                throw;
            }
            finally
            {
                LogProvider.Provider.WriteLog(act, entity, err);
            }
        }
Exemplo n.º 24
0
        /// <summary>把SQL模版格式化为SQL语句</summary>
        /// <param name="entity">实体对象</param>
        /// <param name="methodType"></param>
        /// <param name="parameters"></param>
        /// <returns>SQL字符串</returns>
        static String SQL(IEntity entity, DataObjectMethodType methodType, ref DbParameter[] parameters)
        {
            IEntityOperate op = EntityFactory.CreateOperate(entity.GetType());

            String sql;
            StringBuilder sbNames;
            StringBuilder sbValues;

            // sbParams用于存储参数化操作时格式化的参数名,参数化和非参数化同时使用,如果存在大字段是,才使用参数化
            //StringBuilder sbParams;
            List<DbParameter> dps;
            //Boolean hasBigField = false;

            Boolean isFirst = true;
            switch (methodType)
            {
                case DataObjectMethodType.Fill:
                    return String.Format("Select * From {0}", op.FormatName(op.TableName));
                case DataObjectMethodType.Select:
                    sql = DefaultCondition(entity);
                    // 没有标识列和主键,返回取所有数据的语句
                    if (String.IsNullOrEmpty(sql)) throw new XCodeException("实体类缺少主键!");
                    return String.Format("Select * From {0} Where {1}", op.FormatName(op.TableName), sql);
                case DataObjectMethodType.Insert:
                    sbNames = new StringBuilder();
                    sbValues = new StringBuilder();
                    //sbParams = new StringBuilder();
                    dps = new List<DbParameter>();
                    // 只读列没有插入操作
                    foreach (var fi in op.Fields)
                    {
                        // 标识列不需要插入,别的类型都需要
                        String idv = null;
                        if (fi.IsIdentity)
                        {
                            idv = DAL.Create(op.ConnName).Db.FormatIdentity(fi.Field, entity[fi.Name]);
                            //if (String.IsNullOrEmpty(idv)) continue;
                            // 允许返回String.Empty作为插入空
                            if (idv == null) continue;
                        }

                        // 有默认值,并且没有设置值时,不参与插入操作
                        if (!String.IsNullOrEmpty(fi.DefaultValue) && !entity.Dirtys[fi.Name]) continue;

                        if (!isFirst) sbNames.Append(", ");
                        var name = op.FormatName(fi.ColumnName);
                        sbNames.Append(name);
                        if (!isFirst)
                            sbValues.Append(", ");
                        else
                            isFirst = false;

                        //// 可空类型插入空
                        //if (!obj.Dirtys[fi.Name] && fi.DataObjectField.IsNullable)
                        //    sbValues.Append("null");
                        //else
                        //sbValues.Append(SqlDataFormat(obj[fi.Name], fi)); // 数据

                        if (!fi.IsIdentity)
                        {
                            if (!UseParam(fi))
                                sbValues.Append(op.FormatValue(fi, entity[fi.Name]));
                            else
                            {
                                var paraname = op.FormatParameterName(fi.ColumnName);
                                sbValues.Append(paraname);

                                var dp = op.CreateParameter();
                                dp.ParameterName = paraname;
                                dp.Value = entity[fi.Name] ?? DBNull.Value;
                                dp.IsNullable = fi.IsNullable;
                                dps.Add(dp);
                            }
                        }
                        else
                            sbValues.Append(idv);
                    }

                    if (sbNames.Length <= 0) return null;

                    if (dps.Count > 0) parameters = dps.ToArray();
                    return String.Format("Insert Into {0}({1}) Values({2})", op.FormatName(op.TableName), sbNames, sbValues);
                case DataObjectMethodType.Update:
                    sbNames = new StringBuilder();
                    //sbParams = new StringBuilder();
                    dps = new List<DbParameter>();
                    // 只读列没有更新操作
                    foreach (FieldItem fi in op.Fields)
                    {
                        if (fi.IsIdentity) continue;

                        //脏数据判断
                        if (!entity.Dirtys[fi.Name]) continue;

                        if (!isFirst)
                            sbNames.Append(", "); // 加逗号
                        else
                            isFirst = false;

                        var name = op.FormatName(fi.ColumnName);
                        sbNames.Append(name);
                        sbNames.Append("=");
                        //sbNames.Append(SqlDataFormat(obj[fi.Name], fi)); // 数据

                        if (!UseParam(fi))
                            sbNames.Append(op.FormatValue(fi, entity[fi.Name])); // 数据
                        else
                        {
                            var paraname = op.FormatParameterName(fi.ColumnName);
                            sbNames.Append(paraname);

                            var dp = op.CreateParameter();
                            dp.ParameterName = paraname;
                            dp.Value = entity[fi.Name] ?? DBNull.Value;
                            dp.IsNullable = fi.IsNullable;
                            dps.Add(dp);
                        }
                    }

                    if (sbNames.Length <= 0) return null;

                    sql = DefaultCondition(entity);
                    if (String.IsNullOrEmpty(sql)) return null;

                    if (dps.Count > 0) parameters = dps.ToArray();
                    return String.Format("Update {0} Set {1} Where {2}", op.FormatName(op.TableName), sbNames, sql);
                case DataObjectMethodType.Delete:
                    // 标识列作为删除关键字
                    sql = DefaultCondition(entity);
                    if (String.IsNullOrEmpty(sql))
                        return null;
                    return String.Format("Delete From {0} Where {1}", op.FormatName(op.TableName), sql);
            }
            return null;
        }
Exemplo n.º 25
0
 /// <summary>把SQL模版格式化为SQL语句</summary>
 /// <param name="entity">实体对象</param>
 /// <param name="methodType"></param>
 /// <returns>SQL字符串</returns>
 public virtual String GetSql(IEntity entity, DataObjectMethodType methodType) { DbParameter[] dps = null; return SQL(entity, methodType, ref dps); }
 public DataObjectMethodAttribute(DataObjectMethodType methodType, bool isDefault)
 {
 }
Exemplo n.º 27
0
 /// <summary>把SQL模版格式化为SQL语句</summary>
 /// <param name="entity">实体对象</param>
 /// <param name="methodType"></param>
 /// <returns>SQL字符串</returns>
 public virtual String GetSql(IEntity entity, DataObjectMethodType methodType)
 {
     IDataParameter[] dps = null;
     return(SQL(entity, methodType, ref dps));
 }
 public DataObjectMethodAttribute(DataObjectMethodType methodType)
 {
 }
Exemplo n.º 29
0
 private MethodInfo GetDataMethod(DataObjectMethodType methodType)
 {
     return(GetDataMethod(methodType, null));
 }
Exemplo n.º 30
0
 private MethodInfo GetDataMethod(DataObjectMethodType methodType) {
     return GetDataMethod(methodType, null);
 }
Exemplo n.º 31
0
        /// <summary>把SQL模版格式化为SQL语句</summary>
        /// <param name="entity">实体对象</param>
        /// <param name="methodType"></param>
        /// <param name="parameters"></param>
        /// <returns>SQL字符串</returns>
        String SQL(IEntity entity, DataObjectMethodType methodType, ref DbParameter[] parameters)
        {
            IEntityOperate op = EntityFactory.CreateOperate(entity.GetType());

            String        sql;
            StringBuilder sbNames;
            StringBuilder sbValues;

            // sbParams用于存储参数化操作时格式化的参数名,参数化和非参数化同时使用,如果存在大字段是,才使用参数化
            //StringBuilder sbParams;
            List <DbParameter> dps;
            //Boolean hasBigField = false;

            Boolean isFirst = true;

            switch (methodType)
            {
            case DataObjectMethodType.Fill:
                return(String.Format("Select * From {0}", op.FormatName(op.TableName)));

            case DataObjectMethodType.Select:
                sql = DefaultCondition(entity);
                // 没有标识列和主键,返回取所有数据的语句
                if (String.IsNullOrEmpty(sql))
                {
                    throw new XCodeException("实体类缺少主键!");
                }
                return(String.Format("Select * From {0} Where {1}", op.FormatName(op.TableName), sql));

            case DataObjectMethodType.Insert:
                #region Insert
                sbNames  = new StringBuilder();
                sbValues = new StringBuilder();
                //sbParams = new StringBuilder();
                dps = new List <DbParameter>();
                // 只读列没有插入操作
                foreach (var fi in op.Fields)
                {
                    // 标识列不需要插入,别的类型都需要
                    String idv = null;
                    if (fi.IsIdentity)
                    {
                        if (op.AllowInsertIdentity)
                        {
                            idv = "" + entity[fi.Name];
                        }
                        else
                        {
                            idv = DAL.Create(op.ConnName).Db.FormatIdentity(fi.Field, entity[fi.Name]);
                        }
                        //if (String.IsNullOrEmpty(idv)) continue;
                        // 允许返回String.Empty作为插入空
                        if (idv == null)
                        {
                            continue;
                        }
                    }

                    // 有默认值,并且没有设置值时,不参与插入操作
                    // 20120509增加,同时还得判断是否相同数据库或者数据库默认值,比如MSSQL数据库默认值不是GetDate,那么其它数据库是不可能使用的
                    if (!String.IsNullOrEmpty(fi.DefaultValue) && !entity.Dirtys[fi.Name] && CanUseDefault(fi, op))
                    {
                        continue;
                    }

                    if (!isFirst)
                    {
                        sbNames.Append(", ");
                    }
                    var name = op.FormatName(fi.ColumnName);
                    sbNames.Append(name);
                    if (!isFirst)
                    {
                        sbValues.Append(", ");
                    }
                    else
                    {
                        isFirst = false;
                    }

                    //// 可空类型插入空
                    //if (!obj.Dirtys[fi.Name] && fi.DataObjectField.IsNullable)
                    //    sbValues.Append("null");
                    //else
                    //sbValues.Append(SqlDataFormat(obj[fi.Name], fi)); // 数据

                    if (!fi.IsIdentity)
                    {
                        if (!UseParam(fi))
                        {
                            sbValues.Append(op.FormatValue(fi, entity[fi.Name]));
                        }
                        else
                        {
                            var paraname = op.FormatParameterName(fi.ColumnName);
                            sbValues.Append(paraname);

                            var dp = op.CreateParameter();
                            dp.ParameterName = paraname;
                            //dp.Value = entity[fi.Name] ?? DBNull.Value;
                            dp.Value      = FormatParamValue(fi, entity[fi.Name], op);
                            dp.IsNullable = fi.IsNullable;
                            dps.Add(dp);
                        }
                    }
                    else
                    {
                        sbValues.Append(idv);
                    }
                }

                if (sbNames.Length <= 0)
                {
                    return(null);
                }

                if (dps.Count > 0)
                {
                    parameters = dps.ToArray();
                }
                return(String.Format("Insert Into {0}({1}) Values({2})", op.FormatName(op.TableName), sbNames, sbValues));

                #endregion
            case DataObjectMethodType.Update:
                #region Update
                sbNames = new StringBuilder();
                //sbParams = new StringBuilder();
                dps = new List <DbParameter>();
                // 只读列没有更新操作
                foreach (FieldItem fi in op.Fields)
                {
                    if (fi.IsIdentity)
                    {
                        continue;
                    }

                    //脏数据判断
                    if (!entity.Dirtys[fi.Name])
                    {
                        continue;
                    }

                    if (!isFirst)
                    {
                        sbNames.Append(", ");     // 加逗号
                    }
                    else
                    {
                        isFirst = false;
                    }

                    var name = op.FormatName(fi.ColumnName);
                    sbNames.Append(name);
                    sbNames.Append("=");
                    //sbNames.Append(SqlDataFormat(obj[fi.Name], fi)); // 数据

                    if (!UseParam(fi))
                    {
                        // 检查累加
                        Object  addvalue = null;
                        Boolean sign;
                        if (entity.TryGetAdditionalValue(fi.Name, out addvalue, out sign))
                        {
                            if (sign)
                            {
                                sbNames.AppendFormat("{0}+{1}", name, addvalue);
                            }
                            else
                            {
                                sbNames.AppendFormat("{0}-{1}", name, addvalue);
                            }
                        }
                        else
                        {
                            sbNames.Append(op.FormatValue(fi, entity[fi.Name]));     // 数据
                        }
                    }
                    else
                    {
                        var paraname = op.FormatParameterName(fi.ColumnName);
                        sbNames.Append(paraname);

                        var dp = op.CreateParameter();
                        dp.ParameterName = paraname;
                        dp.Value         = FormatParamValue(fi, entity[fi.Name], op);
                        dp.IsNullable    = fi.IsNullable;
                        dps.Add(dp);
                    }
                }

                if (sbNames.Length <= 0)
                {
                    return(null);
                }

                sql = DefaultCondition(entity);
                if (String.IsNullOrEmpty(sql))
                {
                    return(null);
                }

                if (dps.Count > 0)
                {
                    parameters = dps.ToArray();
                }
                return(String.Format("Update {0} Set {1} Where {2}", op.FormatName(op.TableName), sbNames, sql));

                #endregion
            case DataObjectMethodType.Delete:
                // 标识列作为删除关键字
                sql = DefaultCondition(entity);
                if (String.IsNullOrEmpty(sql))
                {
                    return(null);
                }
                return(String.Format("Delete From {0} Where {1}", op.FormatName(op.TableName), sql));
            }
            return(null);
        }
Exemplo n.º 32
0
 public DataObjectMethodAttribute(DataObjectMethodType methodType, bool isDefault)
 {
     MethodType = methodType;
     IsDefault  = isDefault;
 }
Exemplo n.º 33
0
 public DataObjectMethodAttribute(DataObjectMethodType methodType) : this(methodType, false)
 {
 }
Exemplo n.º 34
0
 private MethodInfo GetDataMethod(DataObjectMethodType methodType, string methodName) {
     if (methodName == null) {
         methodName = String.Empty;
     }
     Type thisType = this.GetType();
     Hashtable methods = _dataMethodCache[thisType] as Hashtable;
     if (methods == null) {
         methods = Hashtable.Synchronized(new Hashtable());
         _dataMethodCache[thisType] = methods;
     }
     Hashtable infos = methods[methodType] as Hashtable;
     if (infos == null) {
         infos = Hashtable.Synchronized(new Hashtable());
         methods[methodType] = infos;
     }
     MethodInfo method;
     if (!infos.ContainsKey(methodName)) {
         MemberFilter filter = new MemberFilter(FilterMethodByDataObjectMethodType);
         MemberInfo[] dataMethods = thisType.FindMembers(
             MemberTypes.Method,
             PublicBindingFlags,
             filter,
             new MethodTypeAndName(methodType, methodName, false));
         if (dataMethods.Length == 0) {
             dataMethods = thisType.FindMembers(
                 MemberTypes.Method,
                 PublicBindingFlags,
                 filter,
                 new MethodTypeAndName(methodType, methodName, true));
             if (dataMethods.Length == 0) {
                 infos[methodName] = null;
                 return null;
             }
         }
         method = (MethodInfo)dataMethods[0];
         infos[methodName] = method;
     }
     else {
         method = (MethodInfo)infos[methodName];
     }
     return method;
 }
Exemplo n.º 35
0
 public MethodTypeAndName(DataObjectMethodType methodType, string methodName, bool firstFoundIsDefault)
 {
     _methodType          = methodType;
     _name                = methodName;
     _firstFoundIsDefault = firstFoundIsDefault;
 }
Exemplo n.º 36
0
 public MethodTypeAndName(DataObjectMethodType methodType, string methodName, bool firstFoundIsDefault) {
     _methodType = methodType;
     _name = methodName;
     _firstFoundIsDefault = firstFoundIsDefault;
 }
        public void SetMethodInformation(System.Reflection.MethodInfo[] methods, string selectedMethodName, ParameterCollection selectedParameters, DataObjectMethodType methodType, System.Type dataObjectType)
        {
            try
            {
                this._signatureTextBox.Text = string.Empty;
                switch (methodType)
                {
                    case DataObjectMethodType.Select:
                        this._helpLabel.Text = System.Design.SR.GetString("ObjectDataSourceMethodEditor_SelectHelpLabel");
                        break;

                    case DataObjectMethodType.Update:
                        this._helpLabel.Text = System.Design.SR.GetString("ObjectDataSourceMethodEditor_UpdateHelpLabel");
                        break;

                    case DataObjectMethodType.Insert:
                        this._helpLabel.Text = System.Design.SR.GetString("ObjectDataSourceMethodEditor_InsertHelpLabel");
                        break;

                    case DataObjectMethodType.Delete:
                        this._helpLabel.Text = System.Design.SR.GetString("ObjectDataSourceMethodEditor_DeleteHelpLabel");
                        break;
                }
                this._methodComboBox.BeginUpdate();
                this._methodComboBox.Items.Clear();
                MethodItem item = null;
                bool flag = false;
                foreach (System.Reflection.MethodInfo info in methods)
                {
                    if (this.FilterMethod(info, methodType))
                    {
                        bool flag2 = false;
                        DataObjectMethodAttribute attribute = Attribute.GetCustomAttribute(info, typeof(DataObjectMethodAttribute), true) as DataObjectMethodAttribute;
                        if ((attribute != null) && (attribute.MethodType == methodType))
                        {
                            if (!flag)
                            {
                                this._methodComboBox.Items.Clear();
                            }
                            flag = true;
                            flag2 = true;
                        }
                        else if (!flag)
                        {
                            flag2 = true;
                        }
                        bool flag3 = ObjectDataSourceDesigner.IsMatchingMethod(info, selectedMethodName, selectedParameters, dataObjectType);
                        if (flag2 || flag3)
                        {
                            MethodItem item2 = new MethodItem(info);
                            this._methodComboBox.Items.Add(item2);
                            if (flag3)
                            {
                                item = item2;
                            }
                            else if (((attribute != null) && (attribute.MethodType == methodType)) && (attribute.IsDefault && (selectedMethodName.Length == 0)))
                            {
                                item = item2;
                            }
                        }
                    }
                }
                if (methodType != DataObjectMethodType.Select)
                {
                    this._methodComboBox.Items.Insert(0, new MethodItem(null));
                }
                this._methodComboBox.InvalidateDropDownWidth();
                this._methodComboBox.SelectedItem = item;
            }
            finally
            {
                this._methodComboBox.EndUpdate();
            }
        }
Exemplo n.º 38
0
 private MethodInfo GetMethod(string methodName, DataObjectMethodType methodType, int parametersCount)
 {
     List<MethodInfo> methods = new List<MethodInfo>();
     if (!string.IsNullOrEmpty(methodName))
     {
         foreach (MethodInfo method in Type.GetMethods())
         {
             if (String.Compare(method.Name, methodName, true) == 0)
             {
                 methods.Add(method);
             }
         }
     }
     else
     {
         foreach (MethodInfo method in Type.GetMethods())
         {
             object[] attributes = method.GetCustomAttributes(typeof(DataObjectMethodAttribute), true);
             if (attributes.Length > 0)
             {
                 foreach (DataObjectMethodAttribute attribute in attributes)
                 {
                     if (attribute.MethodType == methodType)
                     {
                         methods.Add(method);
                     }
                 }
             }
         }
     }
     if (methods.Count == 0)
     {
         return null;
     }
     foreach (MethodInfo method in methods)
     {
         if (method.GetParameters().Length == parametersCount)
         {
             return method;
         }
     }
     return methods[0];
 }
		MethodInfo GetObjectMethod (string methodName, IOrderedDictionary parameters, DataObjectMethodType methodType)
		{
			MemberInfo[] methods = ObjectType.GetMember (methodName, MemberTypes.Method, BindingFlags.Instance | 
										 BindingFlags.Static | 
										 BindingFlags.Public | 
										 BindingFlags.IgnoreCase |
										 BindingFlags.FlattenHierarchy);
			if (methods.Length > 1) {
				// MSDN: The ObjectDataSource resolves method overloads by method name and number
				// of parameters; the names and types of the parameters are not considered.
				// LAMESPEC: the tests show otherwise
				DataObjectMethodAttribute methodAttribute = null;
				MethodInfo methodInfo = null;
				bool hasConflict = false;
				foreach (MethodInfo me in methods) { // we look for methods only
					ParameterInfo [] pinfos = me.GetParameters ();
					if (pinfos.Length == parameters.Count) {
						object [] attrs = me.GetCustomAttributes (typeof (DataObjectMethodAttribute), true);
						DataObjectMethodAttribute domAttr = (attrs != null && attrs.Length > 0) ? (DataObjectMethodAttribute) attrs [0] : null;
						if (domAttr != null && domAttr.MethodType != methodType)
							continue;

						bool paramsMatch = true;
						foreach (ParameterInfo pinfo in pinfos) {
							if (!parameters.Contains (pinfo.Name)) {
								paramsMatch = false;
								break;
							}
						}

						if (!paramsMatch)
							continue;

						if (domAttr != null) {
							if (methodAttribute != null) {
								if (methodAttribute.IsDefault) {
									if (domAttr.IsDefault) {
										methodInfo = null;
										break; //fail due to a conflict
									}
									else
										continue; //existing matches better
								}
								else {
									methodInfo = null; //we override
									hasConflict = !domAttr.IsDefault;
								}
							}
							else
								methodInfo = null; //we override
						}

						if (methodInfo == null) {
							methodAttribute = domAttr;
							methodInfo = me;
							continue;
						}

						hasConflict = true;
					}
				}

				if (!hasConflict && methodInfo != null)
					return methodInfo;
			}
			else if (methods.Length == 1) {
				MethodInfo me = methods[0] as MethodInfo;
				if (me != null && me.GetParameters().Length == parameters.Count)
					return me;
			}
			
			throw CreateMethodException (methodName, parameters);
		}
Exemplo n.º 40
0
 /// <summary>验证实体对象</summary>
 /// <param name="entity">实体对象</param>
 /// <param name="type">操作类型</param>
 /// <param name="post">是否提交数据阶段</param>
 /// <returns></returns>
 protected virtual Boolean ValidPermission(TEntity entity, DataObjectMethodType type, Boolean post) => true;
Exemplo n.º 41
0
		public DataObjectMethodAttribute (DataObjectMethodType methodType) : this (methodType, false) { }
Exemplo n.º 42
0
        /// <summary>
        /// 取得指定类的数据操作SQL模版。
        /// 静态缓存。
        /// </summary>
        /// <param name="t">实体类型</param>
        /// <param name="methodType">方法的数据操作类型</param>
        /// <returns></returns>
        public static String SQL(Type t, DataObjectMethodType methodType)
        {
            String mt = "Select";

            if (methodType == DataObjectMethodType.Fill)
            {
                mt = "Fill";
            }
            else if (methodType == DataObjectMethodType.Insert)
            {
                mt = "Insert";
            }
            else if (methodType == DataObjectMethodType.Update)
            {
                mt = "Update";
            }
            else if (methodType == DataObjectMethodType.Delete)
            {
                mt = "Delete";
            }
            // 因为methodType.ToString()非常耗资源,所以不再使用,估计内部是反射实现的
            String key = String.Format("{0}_{1}", t.FullName, mt);

            if (_SQL.ContainsKey(key))
            {
                return(_SQL[key]);
            }
            lock (_SQL)
            {
                if (_SQL.ContainsKey(key))
                {
                    return(_SQL[key]);
                }

                String        sql      = "";
                FieldItem[]   ps       = Fields(t);
                StringBuilder sbNames  = new StringBuilder();
                StringBuilder sbValues = new StringBuilder();
                Int32         i        = 0;
                switch (methodType)
                {
                case DataObjectMethodType.Fill:
                    //sql = String.Format("Select {0} From {1}", Selects(t), TableName(t));
                    sql = String.Format("Select * From {0}", TableName(t));
                    break;

                case DataObjectMethodType.Select:
                    // 标识列作为查询关键字
                    foreach (FieldItem fi in ps)
                    {
                        if (fi.DataObjectField.IsIdentity)
                        {
                            //sql = String.Format("Select {0} From {1} Where {2}={{0}}", Selects(t), TableName(t), fi.ColumnName);
                            sql = String.Format("Select * From {0} Where {1}={{0}}", TableName(t), fi.ColumnName);
                            break;
                        }
                    }
                    break;

                case DataObjectMethodType.Insert:
                    // 只读列没有插入操作
                    foreach (FieldItem fi in ps)
                    {
                        // 只有整型的标识列不需要插入,GUID和别的类型都需要
                        if (!fi.DataObjectField.IsIdentity || fi.Property.PropertyType != typeof(Int32))
                        {
                            if (i > 0)
                            {
                                sbNames.Append(", ");            // 加逗号
                            }
                            //sbNames.Append(fi.ColumnName);
                            sbNames.AppendFormat("{0}", fi.ColumnName);
                            if (i > 0)
                            {
                                sbValues.Append(", ");            // 加逗号
                            }
                            sbValues.Append("{" + i++ + "}");     // 位置必须递增
                        }
                    }
                    sql = String.Format("Insert Into {0}({1}) Values({2})", TableName(t), sbNames.ToString(), sbValues.ToString());
                    break;

                case DataObjectMethodType.Update:
                    // 只读列没有更新操作
                    String keyColumn = null;
                    foreach (FieldItem fi in ps)
                    {
                        if (!fi.DataObjectField.IsIdentity)
                        {
                            if (i > 0)
                            {
                                sbNames.Append(", ");            // 加逗号
                            }
                            sbNames.AppendFormat("{0}=", fi.ColumnName);
                            sbNames.Append("{" + ++i + "}");     // 不能使用0开始,主键要用
                        }
                        else
                        {
                            keyColumn = String.Format("{0}={{0}}", fi.ColumnName);
                        }
                    }
                    if (String.IsNullOrEmpty(keyColumn))
                    {
                        break;
                    }
                    sql = String.Format("Update {0} Set {1} Where {2}", TableName(t), sbNames.ToString(), keyColumn);
                    break;

                case DataObjectMethodType.Delete:
                    // 标识列作为删除关键字
                    foreach (FieldItem fi in ps)
                    {
                        if (fi.DataObjectField.IsIdentity)
                        {
                            sql = String.Format("Delete From {0} Where {1}={{0}}", TableName(t), fi.ColumnName);
                        }
                    }
                    break;
                }
                _SQL.Add(key, sql);
                return(sql);
            }
        }
Exemplo n.º 43
0
		public DataObjectMethodAttribute (DataObjectMethodType methodType, bool isDefault) {
			_methodType = methodType;
			_isDefault = isDefault;
		}