123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using Ant.Core;
- using Ant.Data;
- using Ant.Mapper;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- namespace Ant.Query.Internals
- {
- static class QueryEnumeratorCreator
- {
- public static IEnumerator<T> CreateEnumerator<T>(DataAccess dbSession, DbCommandFactor commandFactor)
- {
- return new QueryEnumerator<T>(dbSession, commandFactor);
- }
- internal struct QueryEnumerator<T> : IEnumerator<T>
- {
- DataAccess db;
- DbCommandFactor _commandFactor;
- IObjectActivator _objectActivator;
- IDataReader _reader;
- T _current;
- bool _hasFinished;
- bool _disposed;
- public QueryEnumerator(DataAccess dbSession, DbCommandFactor commandFactor)
- {
- this.db = dbSession;
- this._commandFactor = commandFactor;
- this._objectActivator = commandFactor.ObjectActivator;
- this._reader = null;
- this._current = default(T);
- this._hasFinished = false;
- this._disposed = false;
- }
- public T Current { get { return this._current; } }
- object IEnumerator.Current { get { return this._current; } }
- /// <summary>
- /// 指针获取数据
- /// </summary>
- /// <returns></returns>
- public bool MoveNext()
- {
- if (this._hasFinished || this._disposed)
- return false;
- if (this._reader == null)
- {
- //TODO 执行 sql 语句,获取 DataReader
- this._reader = this.db.ExecuteReader(this._commandFactor.CommandText, this._commandFactor.Parameters, CommandBehavior.Default, CommandType.Text);
- }
- if (this._reader.Read())
- {
- this._current = (T)this._objectActivator.CreateInstance(this._reader);
- return true;
- }
- else
- {
- this._reader.Close();
- this.db.Close();
- this._current = default(T);
- this._hasFinished = true;
- return false;
- }
- }
- /// <summary>
- /// 释放内存
- /// </summary>
- public void Dispose()
- {
- if (this._disposed)
- return;
- if (this._reader != null)
- {
- if (!this._reader.IsClosed)
- this._reader.Close();
- this._reader.Dispose();
- this._reader = null;
- }
- if (!this._hasFinished)
- {
- this.db.Close();
- this._hasFinished = true;
- }
- this._current = default(T);
- this._disposed = true;
- }
- public void Reset()
- {
- throw new NotSupportedException();
- }
- }
- }
- }
|