Beispiel #1
0
        /// <summary>
        /// 条件删除
        /// </summary>
        /// <param name="where">删除条件</param>
        public int Delete <T>(Expression <Func <T, bool> > where) where T : class, new()
        {
            ExHelper.ThrowIfNull(where, "删除条件对象不能为空.");
            int         rowCount = 0;
            QueryFilter filter   = QueryFilterHelper.GetFilter(where);

            using (DbCommand dbCommand = dbProvider.GetDeleteDbCmd <T>(filter, this.GetSubTableArg <T>()))
            {
                try
                {
                    dbCommand.Connection = GetDbConnection();
                    rowCount             = dbCommand.ExecuteNonQuery();
                    if (rowCount <= 0)
                    {
                        //throw new Exception("没有删除任何记录.");
                    }
                    CloseDbConnection(dbCommand);
                }
                catch (Exception ex)
                {
                    CloseDbConnection(dbCommand);
                    throw ex;
                }
            }
            return(rowCount);
        }
Beispiel #2
0
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="dto">对象</param>
        public int Delete <T>(T dto) where T : class, new()
        {
            ExHelper.ThrowIfNull(dto, "操作对象不能为空.");

            int rowCount = 0;

            using (DbCommand dbCommand = dbProvider.GetDeleteDbCmd <T>(dto, this.GetSubTableArg <T>()))
            {
                try
                {
                    dbCommand.Connection = GetDbConnection();
                    rowCount             = dbCommand.ExecuteNonQuery();
                    if (rowCount <= 0)
                    {
                        //throw new Exception("没有删除任何记录.");
                    }
                    CloseDbConnection(dbCommand);
                }
                catch (Exception ex)
                {
                    CloseDbConnection(dbCommand);
                    throw ex;
                }
            }
            return(rowCount);
        }
Beispiel #3
0
        /// <summary>
        /// 更新数据对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dto">实体对象</param>
        /// <param name="select">更新属性</param>
        public int Update <T>(T dto, Expression <Func <T, object> > select = null) where T : class, new()
        {
            ExHelper.ThrowIfNull(dto, "操作对象不能为空.");
            int rowCount = 0;

            DtoMapping   dtoDbMapping = DtoMappingHelper.GetDtoMapping <T>();
            List <PiMap> piMapList    = DtoMappingHelper.GetPiMapList <T>(select);

            using (DbCommand dbCommand = dbProvider.GetUpdateDbCmd(dto, piMapList, this.GetSubTableArg <T>()))
            {
                try
                {
                    dbCommand.Connection = GetDbConnection();
                    rowCount             = dbCommand.ExecuteNonQuery();
                    if (rowCount <= 0)
                    {
                        //throw new Exception("没有更新任何记录.");
                    }
                    CloseDbConnection(dbCommand);
                }
                catch (Exception ex)
                {
                    CloseDbConnection(dbCommand);
                    throw ex;
                }
            }
            return(rowCount);
        }
Beispiel #4
0
        /// <summary>
        /// 保存or更新数据对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dto">实体对象</param>
        /// <param name="select">查询属性</param>
        public int Set <T>(T dto, Expression <Func <T, object> > select = null) where T : class, new()
        {
            ExHelper.ThrowIfNull(dto, "操作对象不能为空.");
            int        rowCount     = 0;
            DtoMapping dtoDbMapping = DtoMappingHelper.GetDtoMapping <T>();
            object     pkValue      = dtoDbMapping.PkMap.Pi.GetValue(dto);

            if (ExHelper.IsNullOrEmpty(pkValue))
            {
                rowCount = Add(dto, select);
            }
            else
            {
                rowCount = Update(dto, select);
            }
            return(rowCount);
        }
Beispiel #5
0
        /// <summary>
        /// 保存数据对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dto">实体对象</param>
        /// <param name="select">查询属性</param>
        public int Add <T>(T dto, Expression <Func <T, object> > select = null) where T : class, new()
        {
            ExHelper.ThrowIfNull(dto, "操作对象不能为空.");

            int          rowCount     = 0;
            DtoMapping   dtoDbMapping = DtoMappingHelper.GetDtoMapping <T>();
            List <PiMap> piMapList    = DtoMappingHelper.GetPiMapList <T>(select);

            if (dtoDbMapping.TableAttr.AutoCreate)
            {   //如果是自动建表
                if (!CheckTableExists <T>())
                {
                    CreateTable <T>();
                }
            }
            object pkValue = dtoDbMapping.PkMap.Pi.GetValue(dto);

            if (!dtoDbMapping.IsAutoIncrementPk)
            {     //Guid Id
                if (pkValue == null || (Guid)pkValue == Guid.Empty)
                { //生成Guid
                    dtoDbMapping.PkMap.Pi.SetValue(dto, Guid.NewGuid());
                }
                if (!piMapList.Contains(dtoDbMapping.PkMap))
                {
                    piMapList.Insert(0, dtoDbMapping.PkMap);
                }
            }
            using (DbCommand dbCommand = dbProvider.GetInsertDbCmd(dto, piMapList, this.GetSubTableArg <T>()))
            {
                try
                {
                    dbCommand.Connection = GetDbConnection();
                    if ((dtoDbMapping.PkMap.PropertyType == typeof(int) ||
                         dtoDbMapping.PkMap.PropertyType == typeof(int?)) &&
                        (pkValue == null || (int)pkValue == 0))
                    {
                        dbCommand.CommandText += ";" + dbProvider.GetAutoIdDbCommand().CommandText;
                        object valueObj = dbCommand.ExecuteScalar();
                        if (valueObj == null || valueObj == DBNull.Value)
                        {
                            throw new Exception("插入记录失败.");
                        }
                        dtoDbMapping.PkMap.Pi.SetValue(dto, Convert.ToInt32(valueObj));
                    }
                    else if ((dtoDbMapping.PkMap.PropertyType == typeof(long) ||
                              dtoDbMapping.PkMap.PropertyType == typeof(long?)) &&
                             (pkValue == null || (long)pkValue == 0))
                    {
                        dbCommand.CommandText = $"{dbCommand.CommandText};{dbProvider.GetAutoIdDbCommand().CommandText}";
                        object valueObj = dbCommand.ExecuteScalar();
                        if (valueObj == null || valueObj == DBNull.Value)
                        {
                            throw new Exception("插入记录失败.");
                        }
                        dtoDbMapping.PkMap.Pi.SetValue(dto, Convert.ToInt64(valueObj));
                    }
                    else
                    {
                        rowCount = dbCommand.ExecuteNonQuery();
                        if (rowCount <= 0)
                        {
                            throw new Exception("插入记录失败.");
                        }
                    }
                    CloseDbConnection(dbCommand);
                }
                catch (Exception ex)
                {
                    CloseDbConnection(dbCommand);
                    throw ex;
                }
            }
            return(rowCount);
        }
Beispiel #6
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="dbContext">dbContext</param>
 /// <param name="subTableArg">分表参数</param>
 public IQuery(DbContext dbContext, string subTableArg = null)
 {
     ExHelper.ThrowIfNull(dbContext, "IQuery初始化失败,dbContext对象不能为空.");
     this.dbContext   = dbContext;
     this.subTableArg = subTableArg;
 }