Exemplo n.º 1
0
        /// <summary>
        /// 绑定数据。
        /// </summary>
        /// <param name="dataContext">数据上下文对象。</param>
        /// <param name="reader">数据查询读取器。</param>
        /// <param name="entity">当前实体对象。</param>
        /// <param name="field">当前字段。</param>
        /// <param name="type">实体中字段的类型。</param>
        /// <param name="cache">缓存。</param>
        /// <returns>返回绑定的数据。</returns>
        public override object Bind(IDataContext dataContext, IDataQueryReader reader, object entity, string field, Type type, IDataBinderObjectCache cache)
        {
            bool isSingleValue = (type == typeof(string) || type.IsValueType || TypeExtensions.IsNullableType(type));

            if (isSingleValue && (string.IsNullOrEmpty(Field) || Field == "*"))
            {
                Field = "id";
            }

            using (var builder = dataContext.CreateSelect(SourceName)) {
                //PreSelectBuilder(dataContext, dataReader, entity, builder, cache);
                if (isSingleValue)
                {
                    builder.Select(Field);
                }
                var conditiion = MapObject(Condition, dataContext, entity, reader);
                builder.Query(conditiion).Sort(Sorter);
                return(CacheFunc(cache, builder, "once", type, () => {
                    object value = null;
                    if (builder.WhereCommandText.Length > 0)
                    {
                        var q = dataContext.CreateQuery(type, builder.CommandText, builder.Parameters);
                        q.DataBinderObjectCache = cache;
                        value = q.FirstOrDefault();
                    }
                    if (value == null && type.IsValueType)
                    {
                        return TypeExtensions.DefaultValue(type);
                    }
                    return TypeExtensions.Convert(value, type);
                }));
            }
        }
Exemplo n.º 2
0
 public void Dispose()
 {
     _dataContext = null;
     _entity      = null;
     _reader      = null;
     _fastObject  = null;
 }
Exemplo n.º 3
0
 public ObjectMapper(string expression, IDataContext dataContext, object entity, IDataQueryReader reader)
 {
     _fastObject  = expression;
     _dataContext = dataContext;
     _entity      = entity;
     _reader      = reader;
 }
Exemplo n.º 4
0
 /// <summary>
 /// 创建实例。
 /// </summary>
 /// <param name="query">数据查询。</param>
 /// <param name="reader">数据查询读取器。</param>
 /// <param name="type">类型。</param>
 public DataQueryEnumerator(IDataQuery <T> query, IDataQueryReader reader, System.Type type)
 {
     _query  = query;
     _reader = reader;
     _dataBinderObjectCache = query?.DataBinderObjectCache;
     _type = type;
     query?.DataContext.DisposableObjects.Add(this);
 }
Exemplo n.º 5
0
        /// <summary>
        /// 绑定数据。
        /// </summary>
        /// <param name="dataContext">数据上下文对象。</param>
        /// <param name="reader">数据查询读取器。</param>
        /// <param name="entity">当前实体对象。</param>
        /// <param name="field">当前字段。</param>
        /// <param name="type">实体中字段的类型。</param>
        /// <param name="cache">缓存。</param>
        /// <returns>返回绑定的数据。</returns>
        public override object Bind(IDataContext dataContext, IDataQueryReader reader, object entity, string field, Type type, IDataBinderObjectCache cache)
        {
            var list = (System.Collections.Generic.IList <string>)base.Bind(dataContext, reader, entity, field, typeof(System.Collections.Generic.List <string>), cache);

#if net20 || net35
            return(StringExtensions.Join(list, Spliter));
#else
            return(string.Join(Spliter, list));
#endif
        }
Exemplo n.º 6
0
        /// <summary>
        /// 绑定数据。
        /// </summary>
        /// <param name="dataContext">数据上下文对象。</param>
        /// <param name="reader">数据查询读取器。</param>
        /// <param name="entity">当前实体对象。</param>
        /// <param name="field">当前字段。</param>
        /// <param name="type">实体中字段的类型。</param>
        /// <param name="cache">缓存。</param>
        /// <returns>返回绑定的数据。</returns>
        public override object Bind(IDataContext dataContext, IDataQueryReader reader, object entity, string field, Type type, IDataBinderObjectCache cache)
        {
            using (var builder = dataContext.CreateSelect(SourceName)) {
                builder.Count();

                var condition = MapObject(Condition, dataContext, entity, reader);
                builder.Query(condition);
                return(CacheFunc(cache, builder, "count", type, () => {
                    var value = dataContext.ExecuteScalar(builder.CommandText, builder.Parameters);
                    if (value == null && type.IsValueType)
                    {
                        return TypeExtensions.DefaultValue(type);
                    }
                    return TypeExtensions.Convert(value, type);
                }));
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 绑定数据。
        /// </summary>
        /// <param name="dataContext">数据上下文对象。</param>
        /// <param name="reader">数据读取对象。</param>
        /// <param name="entity">当前实体对象。</param>
        /// <param name="type">类型。</param>
        /// <param name="cache">缓存。</param>
        public static void Bind(IDataContext dataContext, IDataQueryReader reader, object entity, System.Type type, IDataBinderObjectCache cache)
        {
            if (entity == null)
            {
                return;
            }
            var list = TryGetBind(type);

            if (list == null || list.Count == 0)
            {
                return;
            }
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Bind(dataContext, reader, entity, cache);
            }
        }
Exemplo n.º 8
0
 public void Bind(IDataContext dataContext, IDataQueryReader reader, object entity, IDataBinderObjectCache cache)
 {
     if (cache != null && binder.AllowCache)
     {
         object value;
         if (!cache.Get(entity, propertyInfo.Name, out value))
         {
             value = binder.Bind(dataContext, reader, entity, propertyInfo.Name, propertyInfo.PropertyType, cache);
             cache.Set(entity, propertyInfo.Name, value);
         }
         propertyInfo.SetValue(entity, value, null);
     }
     else
     {
         var value2 = binder.Bind(dataContext, reader, entity, propertyInfo.Name, propertyInfo.PropertyType, cache);
         propertyInfo.SetValue(entity, value2, null);
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// 绑定数据。
        /// </summary>
        /// <param name="dataContext">数据上下文对象。</param>
        /// <param name="reader">数据查询读取器。</param>
        /// <param name="entity">当前实体对象。</param>
        /// <param name="field">当前字段。</param>
        /// <param name="type">实体中字段的类型。</param>
        /// <param name="cache">缓存。</param>
        /// <returns>返回绑定的数据。</returns>
        public override object Bind(IDataContext dataContext, IDataQueryReader reader, object entity, string field, Type type, IDataBinderObjectCache cache)
        {
            var  elementType   = type.IsArray ? type.GetElementType() : type.GetGenericArguments()[0];
            bool isSingleValue = (elementType == typeof(string) || elementType.IsValueType || TypeExtensions.IsNullableType(elementType));

            if (isSingleValue && (string.IsNullOrEmpty(Field) || Field == "*"))
            {
                Field = "id";
            }

            string fix = "__";

            using (var builder = dataContext.CreateSelect(SourceName)) {
                //PreSelectBuilder(dataContext, dataReader, entity, builder, cache);
                if (isSingleValue)
                {
                    builder.Select(builder.Dialect.PreName(new string[] { fix, Field }));
                }
                else
                {
                    builder.Select(builder.Dialect.PreName(fix) + ".*");
                }
                builder.Refer(NoSQL.Refer.Begin().Refence(fix, TargetName, TargetField, SourceName, SourceField).Json());
                var conditiion = MapObject(Condition, dataContext, entity, reader);
                builder.Query(conditiion).Sort(Sorter);
                return(CacheFunc(cache, builder, "list.ref", type, () => {
                    var list = (System.Collections.IList)FastWrapper.CreateInstance(type);
                    var q = dataContext.CreateQuery(elementType, builder.CommandText, builder.Parameters);
                    q.DataBinderObjectCache = cache;
                    foreach (var item in q)
                    {
                        list.Add(item);
                    }
                    if (type.IsArray)
                    {
                        var array = Array.CreateInstance(elementType, list.Count);
                        list.CopyTo(array, 0);
                        return array;
                    }
                    return list;
                }));
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// 创建数据查询迭代器。
 /// </summary>
 /// <param name="reader">数据查询读取器。</param>
 /// <returns>返回数据查询迭代器。</returns>
 protected virtual IDataQueryEnumerator <T> CreateEnumerator(IDataQueryReader reader)
 {
     return(new DataQueryEnumerator <T>(this, reader, Type));
 }
Exemplo n.º 11
0
        /// <summary>
        /// 映射对象值
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <param name="dataContext"></param>
        /// <param name="entity">实体对象</param>
        /// <param name="reader">数据查询读取器。</param>
        /// <returns></returns>
        public static object MapObject(string expression, IDataContext dataContext, object entity, IDataQueryReader reader)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return(null);
            }
            var mapper = new ObjectMapper(expression, dataContext, entity, reader);
            var result = mapper.Map();

            mapper.Dispose();
            return(result);
        }
Exemplo n.º 12
0
 /// <summary>
 /// 绑定数据。
 /// </summary>
 /// <param name="dataContext">数据上下文对象。</param>
 /// <param name="reader">数据查询读取器。</param>
 /// <param name="entity">当前实体对象。</param>
 /// <param name="field">当前字段。</param>
 /// <param name="type">实体中字段的类型。</param>
 /// <param name="cache">缓存。</param>
 /// <returns>返回绑定的数据。</returns>
 public abstract object Bind(IDataContext dataContext, IDataQueryReader reader, object entity, string field, Type type, IDataBinderObjectCache cache);