示例#1
0
        /// <summary>
        /// 通过主键和指定类型从数据库获取对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="keyValue">主键值(通常为Guid),不能为空</param>
        /// <returns>从数据库中得到的对象,如果数据库中没找到对象会返回一个空对象</returns>
        /// 前置条件
        /// 1.keyValue不能为空
        /// 2.传入的对象T只能有一个主键
        public T GetByKey <T>(object keyValue)
            where T : new()
        {
            PreconditionAssert.IsNotNull(keyValue, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.ObjectType = typeof(T);
            dbCommandCreator.KeyValue   = keyValue;
            dbCommandCreator.SelectType = SelectType.GetOneByKeyValue;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();
            DataTable dt        = _dbAccess.GetDataTableByCommand(dbCommand);

            if (dt.Rows.Count == 0)
            {
                return(default(T));
            }
            else if (dt.Rows.Count == 1)
            {
                return(ORMHelper.ConvertDataRowToEntity <T>(dt.Rows[0]));
            }
            else
            {
                //引发数据获取结果错误
                throw new ORMException(ErrorMessages.ResultNotUniqueMessage);
            }
        }
示例#2
0
        /// <summary>
        /// 通过对象已有的主键值(可以多个主键)和指定类型从数据库获取对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="entity">外部构造的对象,不能为空</param>
        /// <returns>从数据库中得到的对象,如果数据库中没找到对象会返回一个空对象</returns>
        /// 前置条件
        /// 传入的entity对象不能为空
        public T GetByKeys <T>(T entity) where T : new()
        {
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);

            ORMHelper.CheckEntityKey(entity);
            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.Entity     = entity;
            dbCommandCreator.SelectType = SelectType.GetOneByEntityKey;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();
            DataTable dt        = _dbAccess.GetDataTableByCommand(dbCommand);

            if (dt.Rows.Count == 0)
            {
                return(default(T));
            }
            else if (dt.Rows.Count == 1)
            {
                return(ORMHelper.ConvertDataRowToEntity <T>(dt.Rows[0]));
            }
            else
            {
                throw new ORMException(ErrorMessages.ResultNotUniqueMessage);
            }
        }
示例#3
0
        public IList <T> GetList <T>(string sql, params object[] paramList)
            where T : new()
        {
            var dt = _dbAccess.GetDataTableBySQL(sql, paramList);

            return(ORMHelper.DataTableToList <T>(dt));
        }
示例#4
0
        /// <summary>
        /// 获取数据库中所有的数据总数
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <returns>数据总数</returns>
        public int GetAllCount <T>()
        {
            ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.ObjectType = typeof(T);
            dbCommandCreator.SelectType = SelectType.GetAllCount;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();

            return((int)_dbAccess.GetRC1ByCommand(dbCommand));
        }
示例#5
0
        /// <summary>
        /// 根据表达式删除指定类型的数据
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="cri">表达式</param>
        /// 前置条件
        /// 参数cri不允许为空
        public void Delete <T>(Criteria cri)
        {
            PreconditionAssert.IsNotNull(cri, ErrorMessages.NullReferenceException);
            ORMHelper.CheckEntityIsNotReadOnly(typeof(T), ErrorMessages.EntityReadOnly);

            DeleteCommandCreator dcc = new DeleteCommandCreator(_dbAccess);

            dcc.EntityType = typeof(T);
            dcc.criteria   = cri;
            dcc.deleteType = DeleteType.DeleteByCriteria;
            DbCommand dbCommand = dcc.GetDbCommand();

            _dbAccess.ExecCommand(dbCommand);
        }
示例#6
0
        /// <summary>
        /// 修改一个数据库中已有的对象,该对象的主键必须有值
        /// 该对象为空或主键为空或修改失败会抛出异常
        /// </summary>
        /// <param name="entity">需要修改的实体对象</param>
        /// 前置条件
        /// 参数entity不允许为空
        public void Modify(object entity)
        {
            //断言参入的参数为null或者空字符串(RErrorCode.NullReference - 0x00000001)
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);
            ORMHelper.CheckEntityIsNotReadOnly(entity.GetType(), ErrorMessages.EntityReadOnly);

            ModifyCommandCreator mcc = new ModifyCommandCreator(_dbAccess);

            mcc.Entity = entity;
            DbCommand dbCommand = mcc.GetDbCommand();

            _dbAccess.ExecCommand(dbCommand);
        }
示例#7
0
        /// <summary>
        /// 通过指定类型,过滤条件和排序方式从数据库中获取该指定类型的数据条数
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="filterProterties">过滤条件</param>
        /// <returns>指定类型的数据条数</returns>
        /// 前置条件
        /// 传入的参数entity不允许为空
        /// filterProterties,orderBy可以为空,如果为空则没有where条件查询和不进行排序
        public int GetCount(object entity, string[] filterProterties)
        {
            //断言传入的entity对象为null或者为空字符串,出现空引用异常
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.Entity           = entity;
            dbCommandCreator.FilterProterties = filterProterties;
            dbCommandCreator.SelectType       = SelectType.GetCount;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();

            return((int)_dbAccess.GetRC1ByCommand(dbCommand));
        }
示例#8
0
        /// <summary>
        /// 根据表达式,查询对象集合
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="criteria">表达式</param>
        /// <param name="orderBy">排序</param>
        /// <returns>指定类型的数据集合</returns>
        public IList <T> GetList <T>(Criteria criteria, params SortInfo[] orderBy)
            where T : new()
        {
            PreconditionAssert.IsNotNull(criteria, ErrorMessages.NullReferenceException);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.OrderBy    = orderBy;
            dbCommandCreator.ObjectType = typeof(T);
            dbCommandCreator.Cri        = criteria;
            dbCommandCreator.SelectType = SelectType.GetListByExpression;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();
            DataTable dt        = _dbAccess.GetDataTableByCommand(dbCommand);

            return(ORMHelper.DataTableToList <T>(dt));
        }
示例#9
0
        /// <summary>
        /// 获取数据表中的所有数据集合,并按指定排序方式进行排序
        /// </summary>
        /// <typeparam name="T">指定的类型</typeparam>
        /// <param name="orderBy">排序方式</param>
        /// <returns>数据表中的所有数据集合</returns>
        /// 前置条件
        /// 传入的orderBy对象可以为空,为空的时候则不进行排序
        public IList <T> GetAll <T>(params SortInfo[] orderBy) where T : new()
        {
            PreconditionAssert.IsNotNull(orderBy, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.ObjectType = typeof(T);
            dbCommandCreator.OrderBy    = orderBy;
            dbCommandCreator.SelectType = SelectType.GetAll;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();

            DataTable dt = _dbAccess.GetDataTableByCommand(dbCommand);

            return(ORMHelper.DataTableToList <T>(dt));
        }
示例#10
0
        protected DbCommand GetDbCommandByKeyValue(TypeSchema entityInfo, object keyValue)
        {
            DbCommand dbCommand = DbAccess.CreateDbCommand();

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                Type type = mfi.ProInfo.PropertyType;
                type = Nullable.GetUnderlyingType(type) ?? type;
                DbType      dbType    = ORMHelper.GetDbTypeByName(type.Name);
                DbParameter parameter = DbAccess.CreateDbParameter();
                parameter.ParameterName = "@" + mfi.ProInfo.Name;
                parameter.DbType        = dbType;

                //parameter.Value = Convert.ChangeType(keyValue, fieldProperty.PropertyType);
                parameter.Value = Convert.ChangeType(keyValue, type);
                dbCommand.Parameters.Add(parameter);
            }
            return(dbCommand);
        }
示例#11
0
        /// <summary>
        /// 通过指定类型,过滤条件和排序方式从数据库中获取该指定类型的数据集合
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="filterProterties">字段名称,可包含多个</param>
        /// <param name="orderBy">排序方式</param>
        /// <returns>指定类型的数据集合</returns>
        /// 前置条件
        /// 传入的参数entity,filterProterties不允许为空
        /// orderBy可以为空,如果为空则不进行排序
        public IList <T> GetList <T>(T entity, string[] filterProterties, params SortInfo[] orderBy)
            where T : new()
        {
            //断言传入的entity对象为null或者为空字符串,出现空引用异常
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            PreconditionAssert.IsNotNull(filterProterties, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.Entity           = entity;
            dbCommandCreator.FilterProterties = filterProterties;
            dbCommandCreator.OrderBy          = orderBy;
            dbCommandCreator.SelectType       = SelectType.GetList;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();
            DataTable dt        = _dbAccess.GetDataTableByCommand(dbCommand);

            return(ORMHelper.DataTableToList <T>(dt));
        }
示例#12
0
        internal virtual void Init(Type objectType, IDbAccess dbAccess)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(objectType);
            SchemaItem fieldInfo  = entityInfo.GetFieldInfoByPropertyName(this._propertyName);

            if (fieldInfo == null)
            {
                throw new ArgumentException(string.Format("{0} - Property \"{1}\" not define a mapping field.", this.GetType(), _propertyName));
            }
            Type type = fieldInfo.ProInfo.PropertyType;

            type = Nullable.GetUnderlyingType(type) ?? type;
            if (type.IsEnum)
            {
                this._dbType = DbType.Int32;
            }
            else
            {
                this._dbType = ORMHelper.GetDbTypeByName(type.Name);
            }
            this._fieldName = fieldInfo.MappingFieldAttribute.FieldName;
            this._dbAccess  = dbAccess;
        }
示例#13
0
        protected DbParameter GetDbParameter(PropertyInfo fieldProperty, object entity)
        {
            DbParameter parameter = DbAccess.CreateDbParameter();
            Type        tempType  = fieldProperty.PropertyType;
            DbType      dbType;

            if (tempType.IsEnum)
            {
                dbType = DbType.Int32;
            }
            else
            {
                if (Nullable.GetUnderlyingType(tempType) != null)//如果是 int? 类型则进行处理
                {
                    tempType = Nullable.GetUnderlyingType(tempType);
                }
                dbType = ORMHelper.GetDbTypeByName(tempType.Name);
            }
            parameter.ParameterName = "@" + fieldProperty.Name;
            parameter.DbType        = dbType;
            parameter.Value         = fieldProperty.GetValue(entity, null) ?? DBNull.Value;

            return(parameter);
        }