Exemple #1
0
        /// <summary>
        /// 通过ID获取指定类型的对象,支持分表数据的获取
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type">返回对象类型</param>
        /// <returns></returns>
        public object GetById(object id, Type type)
        {
            DBTable table = MapHelper.GetDBTable(type);

            if (table.PrimaryKey.Count > 1)
            {
                throw new Exception("联合主建,不支持GetById方法");
            }

            //获取对应的数据库表名
            string tbName = TableSeparate.GetTableName(table, id);

            string pName = this.FormatParameterName(table.PrimaryKey[0].AliasName);
            string sql   = string.Format("select * from {0} where {1}={2}", tbName, table.PrimaryKey[0].Name, pName);

            Command.Parameters.Clear();
            //添加查询条件参数
            AddParameter(this.FormatParameterName(table.PrimaryKey[0].AliasName), ParameterDirection.Input, id);
            Command.CommandText = sql;
            Command.CommandType = CommandType.Text;

            using (IDataReader reader = Command.ExecuteReader())
            {
                while (reader.Read())
                {
                    return(FullDataReader.CreateDegFullMapObj(reader, type)(reader));
                }
            }
            return(null);
        }
        /// <summary>
        /// 批量新增
        /// 目前仅支持Oracle Sql Server
        /// </summary>
        /// <param name="list"></param>
        /// <param name="type">映射类型</param>
        /// <param name="pageSize">批量新增时单次写入的页大小</param>
        /// <param name="date">要写入表的拆分日期。注意该日期必须与Id中的日期对应。比如按年拆分的表,Id中的年份与该日期中的年份必须相同</param>
        /// <returns></returns>
        public virtual int InsertBatch(List <object> list, Type type, int pageSize = 100, DateTime?date = null)
        {
            if (list.Count < 1)
            {
                return(0);
            }
            if (list.Count > pageSize && this.Command.Transaction == null)
            {
                throw new Exception(string.Format("批量新增条数大于{0}时必须开启事务", pageSize));
            }

            DBTable table = MapHelper.GetDBTable(type);

            //取表名。如果是拆分表,则获取拆分表名
            string tbName = table.Name;

            if (table.SeparateType != SeparateType.None)
            {
                //如果传入时间为空,则取myId中的时间
                if (!date.HasValue || date.Value == DateTime.MinValue)
                {
                    throw new Exception("分区表批量新增时,必须指定表的拆分日期");
                }
                //获取数据库表名
                tbName = TableSeparate.GetTableName(table, date.Value);
            }

            if (list.Count > 100)
            {
                int total     = list.Count;
                int totalPage = (int)Math.Ceiling(total / (double)pageSize);
                int resCont   = 0;
                for (int i = 0; i < totalPage; i++)
                {
                    int beginIndex = i * pageSize;
                    int getCont    = pageSize;
                    if (total - beginIndex < pageSize)
                    {
                        getCont = total - beginIndex;
                    }
                    List <object> nList = list.GetRange(beginIndex, getCont);

                    Command.Parameters.Clear();
                    string sql = GetInsertBatchSql(nList, table, tbName);
                    Command.CommandText = sql;
                    Command.CommandType = CommandType.Text;
                    resCont            += Command.ExecuteNonQuery();
                }
                return(resCont);
            }
            else
            {
                Command.Parameters.Clear();
                string sql = GetInsertBatchSql(list, table, tbName);
                Command.CommandText = sql;
                Command.CommandType = CommandType.Text;
                return(Command.ExecuteNonQuery());
            }
        }
        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="type">映射表类型</param>
        /// <param name="date">要写入表的拆分日期。注意该日期必须与Id中的日期对应。比如按年拆分的表,Id中的年份与该日期中的年份必须相同</param>
        public void Insert(object instance, Type type, DateTime date)
        {
            DBTable table = MapHelper.GetDBTable(type);

            //取主键值
            object primaryVal = null;

            if (table.PrimaryKey[0].DBPrimaryType != DBPrimaryType.Identity)
            {
                primaryVal = this.GetValue(table.PrimaryKey[0], instance);
                if (primaryVal == null)
                {
                    throw new MyDBException("新增对象,非自增长表主键不能为空");
                }
            }

            //取表名。如果是拆分表,则获取拆分表名
            string tbName = table.Name;

            if (table.SeparateType != SeparateType.None)
            {
                //如果传入时间为空,则取myId中的时间
                if (date == DateTime.MinValue)
                {
                    date = MyIdMake.GetMyIdDate(primaryVal);
                }

                //获取数据库表名
                tbName = TableSeparate.GetTableName(table, date);
            }

            //获取该数据库表 对应的DBSql中的Insert语句
            DBSql dbsql = DBSqlHelper.GetDBSql(tbName, _dbContext.DataType);

            if (string.IsNullOrEmpty(dbsql.InsertSql))//如果该表的新增语句为空,则生成该表的Insert语句
            {
                dbsql.InsertSql = GetInsertSql(table, tbName);
            }

            //将数据写入数据库
            Insert(instance, primaryVal, table, dbsql.InsertSql);
        }
        /// <summary>
        /// 修改对象,支持分表数据的修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <returns>受影响的行数</returns>
        public int Update <T>(T instance)
        {
            Type    type  = typeof(T);
            DBTable table = MapHelper.GetDBTable(type);

            //获取修改对象的主键
            object primaryVal = this.GetValue(table.PrimaryKey[0], instance);

            //获取主键对应的数据库表名
            string tbName = TableSeparate.GetTableName(table, primaryVal);

            DBSql dbsql = DBSqlHelper.GetDBSql(tbName, _dbContext.DataType);

            if (string.IsNullOrEmpty(dbsql.UpdateSql))//如果该表的修改语句为空,则生成该表的update语句
            {
                dbsql.UpdateSql = GetUpdateSql(table, tbName);
            }

            //修改数据
            int retVal = Update <T>(instance, table, dbsql.UpdateSql, primaryVal);

            return(retVal);
        }
        /// <summary>
        /// 根据主键删除,支持分表数据的删除
        /// </summary>
        /// <typeparam name="T">要删除的类型</typeparam>
        /// <typeparam name="IdT">主键类型</typeparam>
        /// <param name="id">主键值</param>
        /// <returns>影响行数</returns>
        public int Delete <T, IdT>(IdT id)
        {
            Type    type  = typeof(T);
            DBTable table = MapHelper.GetDBTable(type);

            if (table.PrimaryKey.Count > 1)
            {
                throw new Exception("联合主键表,不支持根据主键删除");
            }

            //获取对应的数据库表名
            string tbName = TableSeparate.GetTableName(table, id);

            DBSql dbsql = DBSqlHelper.GetDBSql(tbName, _dbContext.DataType);

            if (string.IsNullOrEmpty(dbsql.DeleteSql))//如果该表的修改语句为空,则生成该表的update语句
            {
                dbsql.DeleteSql = GetDeleteSql(table, tbName);
            }

            int retVal = Delete <T, IdT>(id, table, dbsql.DeleteSql);

            return(retVal);
        }