珂珂的个人博客 - 一个程序猿的个人网站

自己来实现linq查询(二)

自己来实现linq查询(一)

自己来实现linq查询(二)

自己来实现linq查询(三)

上一篇全部是ExpressionVisitor

这一篇再贴一些类出来

DbContext类


using System; using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;

namespace FYJ.Data.Linq
{
    public partial class DbContext: System.Data.Linq.DataContext where T : class
    {
        private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

        #region 可扩展性方法定义
        partial void OnCreated();
        partial void Insert(T instance) ;
        partial void Update(T instance);
        partial void Delete(T instance);
        #endregion

        #region  构造函数
        public DbContext(string connectionString) : 
				base(connectionString, mappingSource)
		{
			OnCreated();
		}
		
		public DbContext(System.Data.IDbConnection connection) : 
				base(connection, mappingSource)
		{
			OnCreated();
		}
		
		public DbContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
				base(connection, mappingSource)
		{
			OnCreated();
		}

        public DbContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
				base(connection, mappingSource)
		{
			OnCreated();
        }
        #endregion

        public System.Data.Linq.TableTables
        {
            get
            {
                return this.GetTable();
            }
        }
    }
}
DbQueryable类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace FYJ.Data.Linq
{
    public class DbQueryable: IQueryable, IOrderedQueryable{
        public DbQueryable(FYJ.Data.IDbHelper db=null)
      {
          Provider = new DbQueryProvider(typeof(T),db);
          Expression = Expression.Constant(this);
      }

        public DbQueryable(IQueryProvider provider)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            Provider = provider;
            Expression = Expression.Constant(this);
        }

        public DbQueryable(IQueryProvider provider, Expression expression)
    {
        if (provider == null)
        {
            throw new ArgumentNullException("provider");
        }

        if (expression == null)
        {
            throw new ArgumentNullException("expression");
        }

        if (!typeof(IQueryable).IsAssignableFrom(expression.Type))
        {
            throw new ArgumentOutOfRangeException("expression");
        }

        Provider = provider;
        Expression = expression;
    }


        #region IEnumerable成员

        public IEnumeratorGetEnumerator()
        {
            return (Provider.Execute(Expression)).GetEnumerator();
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Provider.Execute(Expression).GetEnumerator();
        }

        #endregion

        #region IQueryable 成员

        public Type ElementType
        {
            get { return typeof(T); }
        }

        public Expression Expression
        {
            get;
            private set;
        }

        public IQueryProvider Provider
        {
            get;
            private set;
        }

        #endregion

       
    }
}DbQueryProvider类
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace FYJ.Data.Linq
{
   public class DbQueryProvider :QueryProviderAbstract
    {
     
       public DbQueryProvider(Type elementType, IDbHelper db)
           : base(elementType,db)
       {
         
       }


       public override System.Data.DataTable ExecuteDataTable(string sql)
       {
           DataTable dt = db.GetDataTable(sql);

           if (dt.Columns.Contains("rownum"))
           {
               dt.Columns.Remove("rownum");
           }
           return dt;
       }
    }
}ObjectReader 类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FYJ.Data.Linq
{
    internal class ObjectReader: IEnumerable, IEnumerable 
    {
        Enumerator enumerator;
        internal ObjectReader(DataTable dt)
        {
            this.enumerator = new Enumerator(dt);
        }

        internal ObjectReader(DataTable dt, Funcprojector)
        {
            this.enumerator = new Enumerator(dt, projector);
        }

       
        #region IEnumerable成员

        public IEnumeratorGetEnumerator()
        {
            Enumerator e = this.enumerator;
            if (e == null)
            {
                throw new InvalidOperationException("Cannot enumerate more than once");
            }

            this.enumerator = null;
            return e;
        }

        #endregion

        #region IEnumerable 成员
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        #endregion

        class Enumerator :ProjectionRow,IEnumerator, IEnumerator, IDisposable
        {
            Funcprojector;
            private DataTable dt;
            T current;
            private int currentIndex = 0; //当前索引
          
            internal Enumerator(DataTable dt)
            {
                this.dt = dt;
            }

            internal Enumerator(DataTable dt, Funcprojector)
            {
                this.dt = dt;
                this.projector = projector;
            }

            public T Current
            {
                get { return this.current; }
            }

            object IEnumerator.Current
            {
                get 
                {
                    if (typeof(T) == typeof(DataTable))
                    {
                        return dt;
                    }

                    return this.current;
                }
            }

            public override object GetValue(int index) //这里的index是列索引
            {
                if (index >= 0)
                {
                    if (dt == null)
                    {
                        return null;
                    }

                    if (typeof(T) == typeof(DataTable))
                    {
                        return dt;
                    }

                    object obj = null;
                    obj = this.dt.Rows[this.currentIndex][index];
                    if (obj == DBNull.Value)
                    {
                        return null;
                    }

                    return obj;
                }
                throw new IndexOutOfRangeException();
            }

          
            public void Dispose()
            {

            }

            #region IEnumerator 成员
            public bool MoveNext()
            {
                if (typeof(T) == typeof(DataTable))
                {
                    return false;
                }

                if (dt==null||currentIndex >= dt.Rows.Count)
                {
                    return false;
                }

                if (this.projector != null)
                {
                    this.current = this.projector(this);
                }
                else
                {
                   this.current=(T)DataTableToObject(this.dt, Activator.CreateInstance(typeof(T)), this.currentIndex);
                }

                currentIndex++;
                return true;
            }

            public void Reset()
            {
                this.currentIndex = 0;
            }
            #endregion

            private object DataTableToObject(DataTable dt, object obj,int currentIndex)
            {
                if (dt != null && dt.Rows.Count > 0&¤tIndex<dt.rows.count) {="" foreach="" (propertyinfo="" info="" in="" obj.gettype().getproperties())="" if="" (dt.columns.contains(info.name))="" (dt.rows[currentindex][info.name]="" !="DBNull.Value)" info.setvalue(obj,="" dt.rows[currentindex][info.name],="" null);="" }="" return="" obj;="" }



上一篇:URL 签名验证

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧