Пример #1
0
        internal static DataTable RunQueryForDataTable(IStatement statement, RequestScope request, ISession session, object parameterObject)
        {
            IResultStrategy resultStrategy = ResultStrategyFactory.Get(statement);
            DataTable       dataTable      = new DataTable("DataTable");

            using (IDbCommand command = request.IDbCommand)
            {
                IDataReader reader = command.ExecuteReader();

                try
                {
                    // Get Results
                    while (reader.Read())
                    {
                        DataRow dataRow = dataTable.NewRow();
                        dataTable.Rows.Add(dataRow);
                        resultStrategy.Process(request, ref reader, dataRow);
                    }
                }
                finally
                {
                    reader.Close();
                    reader.Dispose();
                }

                // do we need ??
                //ExecuteDelayedLoad(request);

                // do we need ??
                //RetrieveOutputParameters(request, session, command, parameterObject);
            }

            return(dataTable);
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MappedStatement"/> class.
 /// </summary>
 /// <param name="modelStore">The model store.</param>
 /// <param name="statement">The statement.</param>
 public MappedStatement(IModelStore modelStore, IStatement statement)
 {
     this.modelStore = modelStore;
     this.statement  = statement;
     preparedCommand = new DefaultPreparedCommand();
     resultStrategy  = ResultStrategyFactory.Get(this.statement);
 }
Пример #3
0
 internal MappedStatement(ISqlMapper sqlMap, IStatement statement)
 {
     this._sqlMap          = sqlMap;
     this._statement       = statement;
     this._preparedCommand = PreparedCommandFactory.GetPreparedCommand(false);
     this._resultStrategy  = ResultStrategyFactory.Get(this._statement);
 }
Пример #4
0
        private static IList <T> RunQueryForList <T>(RequestScope request, ISqlMapSession session, object parameterObject, IStatement _statement)
        {
            IList <T> list = new List <T>();

            using (IDbCommand command = request.IDbCommand)
            {
                list = (_statement.ListClass == null) ? (new List <T>()) : (_statement.CreateInstanceOfGenericListClass <T>());
                IDataReader reader = command.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        object obj = ResultStrategyFactory.Get(_statement).Process(request, ref reader, null);
                        if (obj != BaseStrategy.SKIP)
                        {
                            list.Add((T)obj);
                        }
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    reader.Close();
                    reader.Dispose();
                }
                ExecutePostSelect(request);
                RetrieveOutputParameters(request, session, command, parameterObject);
            }
            return(list);
        }
Пример #5
0
        internal static T RunQueryForObject <T>(IStatement statement, RequestScope request, ISession session, object parameterObject, T resultObject)
        {
            IResultStrategy resultStrategy = ResultStrategyFactory.Get(statement);
            T result = resultObject;

            using (IDbCommand command = request.IDbCommand)
            {
                IDataReader reader = command.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        object obj = resultStrategy.Process(request, ref reader, resultObject);
                        if (obj != BaseStrategy.SKIP)
                        {
                            result = (T)obj;
                        }
                    }
                }
                finally
                {
                    reader.Close();
                    reader.Dispose();
                }

                ExecuteDelayedLoad(request);

                #region remark
                // If you are using the OleDb data provider, you need to close the
                // DataReader before output parameters are visible.
                #endregion

                RetrieveOutputParameters(request, session, command, parameterObject);
            }

            return(result);
        }
Пример #6
0
        internal static IList <T> RunQueryForList <T>(IStatement statement, RequestScope request, ISession session, object parameterObject, IList <T> resultObject, RowDelegate <T> rowDelegate)
        {
            IResultStrategy resultStrategy = ResultStrategyFactory.Get(statement);
            IList <T>       list           = resultObject;

            using (IDbCommand command = request.IDbCommand)
            {
                if (resultObject == null)
                {
                    if (statement.ListClass == null)
                    {
                        list = new List <T>();
                    }
                    else
                    {
                        list = statement.CreateInstanceOfGenericListClass <T>();
                    }
                }

                IDataReader reader = command.ExecuteReader();
                try
                {
                    do
                    {
                        if (rowDelegate == null)
                        {
                            while (reader.Read())
                            {
                                try
                                {
                                    object obj = resultStrategy.Process(request, ref reader, null);
                                    if (obj != BaseStrategy.SKIP)
                                    {
                                        list.Add((T)obj);
                                    }
                                }
                                catch (Exception ed)
                                {
                                    string ms = ed.Message;
                                }
                            }
                        }
                        else
                        {
                            while (reader.Read())
                            {
                                T obj = (T)resultStrategy.Process(request, ref reader, null);
                                rowDelegate(obj, parameterObject, list);
                            }
                        }
                    }while (reader.NextResult());
                }
                finally
                {
                    reader.Close();
                    reader.Dispose();
                }

                ExecuteDelayedLoad(request);
                RetrieveOutputParameters(request, session, command, parameterObject);
            }

            return(list);
        }
Пример #7
0
        internal static IList RunQueryForList(IStatement statement, RequestScope request, ISession session, object parameterObject, IList resultObject, RowDelegate rowDelegate)
        {
            IResultStrategy resultStrategy = ResultStrategyFactory.Get(statement);
            IList           list           = resultObject;

            using (IDbCommand command = request.IDbCommand)
            {
                if (resultObject == null)
                {
                    if (statement.ListClass == null)
                    {
                        list = new ArrayList();
                    }
                    else
                    {
                        list = statement.CreateInstanceOfListClass();
                    }
                }

                IDataReader reader = command.ExecuteReader();

                try
                {
                    do
                    {
                        if (rowDelegate == null)
                        {
                            //***
                            IList currentList = null;
                            if (request.Statement.ResultsMap.Count == 1)
                            {
                                currentList = list;
                            }
                            else
                            {
                                if (request.CurrentResultMap != null)
                                {
                                    Type genericListType = typeof(List <>).MakeGenericType(new Type[] { request.CurrentResultMap.Class });
                                    currentList = (IList)Activator.CreateInstance(genericListType);
                                }
                                else
                                {
                                    currentList = new ArrayList();
                                }
                                list.Add(currentList);
                            }
                            //***
                            while (reader.Read())
                            {
                                //将reader当前行中的所有字段加入到IList对象中,即obj中
                                object obj = resultStrategy.Process(request, ref reader, null);
                                if (obj != BaseStrategy.SKIP)
                                {
                                    //list.Add(obj);
                                    currentList.Add(obj);
                                }
                            }
                        }
                        else
                        {
                            while (reader.Read())
                            {
                                object obj = resultStrategy.Process(request, ref reader, null);
                                rowDelegate(obj, parameterObject, list);
                            }
                        }
                    }while (reader.NextResult());
                }
                finally
                {
                    reader.Close();
                    reader.Dispose();
                }

                ExecuteDelayedLoad(request);
                RetrieveOutputParameters(request, session, command, parameterObject);
            }

            return(list);
        }