Пример #1
0
        /// <summary>
        /// 删除,按照主键进行删除
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int Delete(TableEntityBase info)
        {
            int res = 0;

            _DeleteChildren(info);
            IDbDataParameter[] dbprams = info.GetPrimaryKey().Select(a =>
            {
                if (a.GetAttribute <PrimaryKeyAttribute>() != null)
                {
                    if (string.IsNullOrEmpty(a.GetValue(info, null)?.ToString()))
                    {
                        throw new DataBaseException("字段:" + a.Name + "是主键,在删除时不能为空");
                    }
                }
                IDbDataParameter p = GetDbDataParameter(a, info);
                return(p);
            }).ToArray();
            string tablename = info.GetTableName();

            string where = string.Join(" and ", info.GetPrimaryKey().Select(a => a.Name + "=" + GetPramCols(a.Name)));
            if (string.IsNullOrEmpty(where))
            {
                throw new DataBaseException("没有主键,不能删除");
            }
            RunSql(_dbCommand =>
            {
                _dbCommand.Parameters.Clear();
                _dbCommand.Parameters.AddRange(dbprams);
                _dbCommand.CommandText = string.Format(_delete, tablename, where);
                res = _dbCommand.ExecuteNonQuery();
            });
            return(res);
        }
Пример #2
0
        /// <summary>
        /// 查询,按照所有不为空的条件查询
        /// </summary>
        /// <param name="Filter">查询类</param>
        /// <param name="TInfo">返回类型</param>
        /// <param name="TFilter">查询类型</param>
        /// <returns></returns>
        public List <TableEntityBase> SelectList(TableEntityBase Filter, Type TInfo, Type TFilter)
        {
            List <TableEntityBase> res     = new List <TableEntityBase>();
            TableEntityBase        refinfo = TableEntityBase.Create(TInfo);

            PropertyInfo[] Allprop = Filter.GetAllSelectField().Where(a =>
            {
                return(a.GetValue(Filter, null) != null);
            }).ToArray();
            IDbDataParameter[] dbprams = Allprop.Select(a =>
            {
                IDbDataParameter p = GetDbDataParameter(a, Filter);
                return(p);
            }).ToArray();
            RunSql(_dbCommand =>
            {
                _dbCommand.Parameters.Clear();
                _dbCommand.Parameters.AddRange(dbprams);
                string tablename       = refinfo.GetTableName();
                string select          = string.Join(",", refinfo.GetAllField().Select(a => GetFieldName(a.Name)));
                string where           = string.Join(" and ", Allprop.Select(a => a.Name + "=" + GetPramCols(a.Name)));
                _dbCommand.CommandText = string.Format(_select, select, tablename, where.IsEmpty(" 1=1 "));
                using (DbDataReader reader = _dbCommand.ExecuteReader())
                {
                    res = this.ReaderToEntity(TInfo, reader).ToList();
                }
            });
            res.ForEach(a => a = _SelectChildren(a));
            return(res);
        }
Пример #3
0
        /// <summary>
        /// 删除,按照非null的进行删除,全null抛出异常
        /// </summary>
        /// <exception cref="DataBaseException"></exception>
        /// 没有删除条件
        /// <param name="info"></param>
        /// <returns></returns>
        public int DeleteList(TableEntityBase info)
        {
            int res = 0;

            PropertyInfo[] Allprop = info.GetAllSelectField().Where(a =>
            {
                return(a.GetValue(info, null) != null);
            }).ToArray();
            IDbDataParameter[] dbprams = Allprop.Select(a =>
            {
                IDbDataParameter p = GetDbDataParameter(a, info);
                return(p);
            }).ToArray();
            string tablename = info.GetTableName();

            string where = string.Join(" and ", Allprop.Select(a => a.Name + "=" + GetPramCols(a.Name)));
            if (string.IsNullOrEmpty(where))
            {
                throw new DataBaseException("没有删除条件,不能删除");
            }
            RunSql(_dbCommand =>
            {
                _dbCommand.Parameters.Clear();
                _dbCommand.Parameters.AddRange(dbprams);
                _dbCommand.CommandText = string.Format(_delete, tablename, where);
                res = _dbCommand.ExecuteNonQuery();
            });
            return(res);
        }
Пример #4
0
 void _InsertChildren(TableEntityBase info)
 {
     PropertyInfo[] ForeignKeys = info.GetForeignKey();
     if (!ForeignKeys.IsEmpty())    //有子表,处理子表
     {
         ForeignKeys.ForEach(key => //主表里记录子表的列表
         {
             if (key.IsArray() && key.GetChildren().BaseOn <TableEntityBase>())
             {
                 key.ForEach(info, (TableEntityBase item) =>
                 {
                     List <ForeignKeyAttribute> attrs = key.GetAttributes <ForeignKeyAttribute>();
                     attrs.ForEach(a =>
                     {
                         item.SetPropertyValue(a.ChildrenKey, info.GetPropertyValue(a.ParentKey));
                     });
                     Insert(item);
                 });
             }
             else
             {
                 throw new Exception($"属性{key.Name}必须是EntityBase的广义数组");
             }
         });
     }
 }
Пример #5
0
 T _SelectChildren <T>(T info) where T : TableEntityBase
 {
     if (info == null)
     {
         return(info);
     }
     PropertyInfo[] ForeignKeys = info.GetForeignKey();
     if (!ForeignKeys.IsEmpty())    //有子表,处理子表
     {
         ForeignKeys.ForEach(key => //主表里记录子表的列表
         {
             if (key.IsArray() && key.GetChildren().BaseOn <TableEntityBase>())
             {
                 Type t = key.GetChildren();
                 TableEntityBase item             = TableEntityBase.Create(t);
                 List <ForeignKeyAttribute> attrs = key.GetAttributes <ForeignKeyAttribute>();
                 attrs.ForEach(a =>
                 {
                     item.SetPropertyValue(a.ChildrenKey, info.GetPropertyValue(a.ParentKey));
                 });
                 List <TableEntityBase> res = SelectList(item, t);
                 key.SetArrValue(info, res);
             }
             else
             {
                 throw new Exception($"属性{key.Name}必须是EntityBase的广义数组");
             }
         });
     }
     return(info);
 }
Пример #6
0
 void _DeleteChildren(TableEntityBase info)
 {
     PropertyInfo[] ForeignKeys = info.GetForeignKey();
     if (!ForeignKeys.IsEmpty())    //有子表,处理子表
     {
         ForeignKeys.ForEach(key => //主表里记录子表的列表
         {
             if (key.IsArray() && key.GetChildren().BaseOn <TableEntityBase>())
             {
                 List <ForeignKeyAttribute> attrs = key.GetAttributes <ForeignKeyAttribute>();
                 TableEntityBase edel             = TableEntityBase.Create(key.GetChildren());
                 attrs.ForEach(attr =>
                 {
                     edel.GetType().GetProperty(attr.ChildrenKey).SetValue(edel, info.GetPropertyValue(attr.ParentKey), null);
                 });
                 _DeleteChildren(edel);
                 DeleteList(edel);
             }
             else
             {
                 throw new Exception($"属性{key.Name}必须是EntityBase的广义数组");
             }
         });
     }
 }
Пример #7
0
        /// <summary>
        /// 通用单表删除方法
        /// </summary>
        /// <param name="info"></param>
        public void CommenDelete(TableEntityBase info)
        {
            PropertyInfo[] pis = info.GetPrimaryKey();
            if (pis == null || pis.Length != 1)
            {
                throw new LogicException("只有唯一主键的类适用于通用删除方法");
            }
            PropertyInfo pi  = pis.FirstOrDefault();
            string       key = pi.GetValue(info, null)?.ToString();

            if (string.IsNullOrWhiteSpace(key))
            {
                throw new LogicException("通用单表删除方法要求主键有值");
            }
            DbHelper.Delete(info);
        }
Пример #8
0
        /// <summary>
        /// 读取的内容转换为Entity
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        ///
        private List <TableEntityBase> ReaderToEntity(Type t, DbDataReader reader)
        {
            List <TableEntityBase> res = new List <TableEntityBase>();
            int fieldCount             = reader.FieldCount;

            while (reader.Read())
            {
                TableEntityBase entity = TableEntityBase.Create(t);
                for (int i = 0; i < fieldCount; i++)
                {
                    entity.SetPropertyValue(reader.GetName(i), reader.GetValue(i));
                }
                res.Add(entity);
            }
            reader.Close();
            return(res);
        }
Пример #9
0
        public int Save(TableEntityBase info)
        {
            IDbDataParameter[] dbprams = info.GetPrimaryKey().Select(a =>
            {
                if (a.GetAttribute <PrimaryKeyAttribute>() != null)
                {
                    if (string.IsNullOrEmpty(a.GetValue(info, null)?.ToString()))
                    {
                        throw new DataBaseException("字段:" + a.Name + "是主键,在保存时不能为空");
                    }
                }
                IDbDataParameter p = GetDbDataParameter(a, info);
                object value       = a.GetValue(info, null);
                p.Value            = (value == null || string.IsNullOrEmpty(value.ToString()) ? DBNull.Value : value);
                return(p);
            }).ToArray();
            int i = 0;

            RunSql(_dbCommand =>
            {
                _dbCommand.Parameters.Clear();
                _dbCommand.Parameters.AddRange(dbprams);
                string tablename       = info.GetTableName();
                string where           = string.Join(" and ", info.GetPrimaryKey().Select(a => a.Name + "=" + GetPramCols(a.Name)));
                _dbCommand.CommandText = string.Format(_selectcount, tablename, where);
                using (DbDataReader reader = _dbCommand.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        i = reader.GetValue(0).ToString().ToInt();
                    }
                }
            });
            if (i == 0)
            {
                return(Insert(info));
            }
            else
            {
                return(Update(info));
            }
        }
Пример #10
0
        /// <summary>
        /// 通用的单表存储方式
        /// </summary>
        /// <param name="info"></param>
        public string CommonSave(TableEntityBase info)
        {
            string key = "";

            PropertyInfo[] pis = info.GetPrimaryKey();
            if (pis == null || pis.Length != 1)
            {
                throw new LogicException("只有唯一主键的类适用于通用存储方法");
            }
            PropertyInfo pi = pis.FirstOrDefault();

            key = pi.GetValue(info, null)?.ToString();
            if (string.IsNullOrWhiteSpace(key))
            {
                key = NewINC(info);
                pi.SetValue(info, key, null);
            }
            DbHelper.Save(info);
            return(key);
        }
Пример #11
0
        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int Insert(TableEntityBase info)
        {
            int res = 0;

            IDbDataParameter[] dbprams = info.GetInserrtField().Select(a =>
            {
                IDbDataParameter p = GetDbDataParameter(a, info);
                return(p);
            }).ToArray();
            RunSql(_dbCommand =>
            {
                _dbCommand.Parameters.Clear();
                _dbCommand.Parameters.AddRange(dbprams);
                _dbCommand.CommandText = string.Format(_insert, info.GetTableName(),
                                                       string.Join(",", dbprams.Select(a => a.ParameterName)),
                                                       string.Join(",", dbprams.Select(a => GetPramCols(a.ParameterName)))
                                                       );
                res = _dbCommand.ExecuteNonQuery();
            });
            #region 处理子表
            _InsertChildren(info);
            #endregion
            return(res);
        }
Пример #12
0
 /// <summary>
 /// 查询,按照所有不为空的条件查询
 /// </summary>
 /// <param name="info"></param>
 /// <param name="T">指定查询类型</param>
 /// <returns></returns>
 public List <TableEntityBase> SelectList(TableEntityBase info, Type T)
 {
     return(SelectList(info, T, T));
 }
Пример #13
0
 /// <summary>
 /// 获取新的记录编号
 /// </summary>
 /// <returns></returns>
 public string NewINC(TableEntityBase info)
 {
     return(NewINC(info.GetTableName()));
 }
Пример #14
0
 /// <summary>
 /// 快速删除
 /// </summary>
 /// <param name="info"></param>
 public void CommenDelete(TableEntityBase info)
 {
     service.CommonService.CommenDelete(info);
 }
Пример #15
0
 /// <summary>
 /// 快速保存
 /// </summary>
 /// <param name="info"></param>
 public string CommonSave(TableEntityBase info)
 {
     return(service.CommonService.CommonSave(info));
 }