Exemplo n.º 1
0
 /// <summary>
 /// 执行查询语句
 /// </summary>
 /// <param name="sql">sql语句</param>
 /// <param name="parameters">查询参数</param>
 /// <param name="hints">指令扩展属性</param>
 /// <param name="operationType">操作类型,读写分离,默认从master库读取</param>
 /// <returns>IDataReader</returns>
 /// <exception cref="DalException">数据访问框架异常</exception>
 public IList <IDataReader> SelectDataReader(String sql, StatementParameterCollection parameters, IDictionary hints, OperationType operationType)
 {
     try
     {
         if (!IsShardEnabled)
         {
             Statement statement = SqlBuilder.GetSqlStatement(LogicDbName, ShardingStrategy, sql, parameters, hints, operationType).Single();
             AddSqlToExtendParams(statement, hints);
             return(new List <IDataReader> {
                 DatabaseBridge.Instance.ExecuteReader(statement)
             });
         }
         else
         {
             var statements = ShardingUtil.GetShardStatement(LogicDbName, ShardingStrategy, parameters, hints, newHints => ShardingUtil.GetQueryStatement(LogicDbName, sql, ShardingStrategy, parameters, newHints, operationType), SqlStatementType.SELECT);
             return(ShardingExecutor.GetShardingDataReaderList(statements));
         }
     }
     catch (Exception ex)
     {
         throw;
     }
     finally
     {
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 执行查询语句
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">查询参数</param>
        /// <param name="hints">指令扩展属性</param>
        /// <param name="operationType">操作类型,读写分离,默认从master库读取</param>
        /// <returns>IDataReader</returns>
        /// <exception cref="DalException">数据访问框架异常</exception>
        public IList <IDataReader> SelectDataReader(String sql, StatementParameterCollection parameters, IDictionary hints, OperationType operationType)
        {
            if (!IsShardEnabled)
            {
                Statement statement = SqlBuilder.GetSqlStatement(LogicDbName, ShardingStrategy, sql, parameters, hints, operationType).Single();
                AddSqlToExtendParams(statement, hints);
                try
                {
                    var reader = DatabaseBridge.Instance.ExecuteReader(statement);
                    return(new List <IDataReader> {
                        reader
                    });
                }
                finally
                {
                    RunTimeDetail runTimeDetail = new RunTimeDetail
                    {
                        DbName   = statement.DbName,
                        Duration = statement.Duration,
                        Server   = statement.HostName
                    };
                    if (hints != null)
                    {
                        hints.Add(DALExtStatementConstant.EXCUTE_TIME, new List <RunTimeDetail> {
                            runTimeDetail
                        });
                    }
                }
            }
            else
            {
                var statements = ShardingUtil.GetShardStatement(LogicDbName, ShardingStrategy, parameters, hints, newHints => ShardingUtil.GetQueryStatement(LogicDbName, sql, ShardingStrategy, parameters, newHints, operationType), SqlStatementType.SELECT);

                try
                {
                    var reader = ShardingExecutor.GetShardingDataReaderList(statements);
                    return(reader);
                }
                finally
                {
                    var runTimeList = new List <RunTimeDetail>();
                    foreach (var statement in statements)
                    {
                        RunTimeDetail runTimeDetail = new RunTimeDetail
                        {
                            DbName   = statement.DbName,
                            Duration = statement.Duration,
                            Server   = statement.HostName
                        };
                        runTimeList.Add(runTimeDetail);
                    }
                    if (hints != null)
                    {
                        hints.Add(DALExtStatementConstant.EXCUTE_TIME, runTimeList);
                    }
                }
            }
        }