Example #1
0
        /// <summary>
        /// 执行非查询指令
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">查询参数</param>
        /// <param name="hints">指令参数:如timeout</param>
        /// <param name="operationType">操作类型,读写分离,默认从master库读取</param>
        /// <returns>影响行数</returns>
        /// <exception cref="DalException">数据访问框架异常</exception>
        public Int32 ExecNonQuery(String sql, StatementParameterCollection parameters, IDictionary hints, OperationType operationType)
        {
            try
            {
                Int32 result;

                if (!IsShardEnabled)
                {
                    Statement statement = SqlBuilder.GetNonQueryStatement(LogicDbName, ShardingStrategy, sql, parameters, hints, operationType).Single();
                    AddSqlToExtendParams(statement, hints);
                    result = DatabaseBridge.Instance.ExecuteNonQuery(statement);
                }
                else
                {
                    var statements = ShardingUtil.GetShardStatement(LogicDbName, ShardingStrategy, parameters, hints,
                                                                    newHints => SqlBuilder.GetNonQueryStatement(LogicDbName, ShardingStrategy, sql, parameters, newHints, operationType, SqlStatementType.UNKNOWN), SqlStatementType.UNKNOWN);

                    result = ShardingExecutor.ExecuteShardingNonQuery(statements).Sum();
                }

                return(result);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
            }
        }
Example #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="sql">SQL语句</param>
 /// <param name="parameters">查询参数</param>
 /// <param name="hints">指令的扩展属性</param>
 /// <param name="operationType">操作类型,读写分离,默认从master库读取</param>
 /// <returns>DataSet</returns>
 /// <exception cref="DalException">数据访问框架异常</exception>
 public DataSet SelectDataSet(String sql, StatementParameterCollection parameters, IDictionary hints, OperationType operationType)
 {
     try
     {
         DataSet dataSet;
         if (!IsShardEnabled)
         {
             Statement statement = SqlBuilder.GetSqlStatement(LogicDbName, ShardingStrategy, sql, parameters,
                                                              hints, operationType).Single();
             AddSqlToExtendParams(statement, hints);
             dataSet = DatabaseBridge.Instance.ExecuteDataSet(statement);
         }
         else
         {
             var statements = ShardingUtil.GetShardStatement(LogicDbName, ShardingStrategy, null, hints, newHints => ShardingUtil.GetQueryStatement(LogicDbName, sql, ShardingStrategy, parameters, newHints, operationType), SqlStatementType.SELECT);
             dataSet = ShardingExecutor.ExecuteShardingDataSet(statements);
         }
         return(dataSet);
     }
     catch (Exception ex)
     {
         throw;
     }
     finally
     {
     }
 }
Example #3
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
     {
     }
 }
Example #4
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);
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// 执行单返回值聚集查询指令
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">查询参数</param>
        /// <param name="hints">指令参数:如timeout</param>
        /// <param name="operationType">操作类型,读写分离,默认从master库读取</param>
        /// <returns>object</returns>
        /// <exception cref="DalException">数据访问框架异常</exception>
        public Object ExecScalar(String sql, StatementParameterCollection parameters, IDictionary hints, OperationType operationType)
        {
            try
            {
                Object result = null;

                if (!IsShardEnabled)
                {
                    Statement statement = SqlBuilder.GetScalarStatement(LogicDbName, ShardingStrategy, sql, parameters, hints, operationType).Single();
                    AddSqlToExtendParams(statement, hints);
                    result = DatabaseBridge.Instance.ExecuteScalar(statement);
                }
                else
                {
                    var statements = ShardingUtil.GetShardStatement(LogicDbName, ShardingStrategy, parameters, hints,
                                                                    newHints => SqlBuilder.GetScalarStatement(LogicDbName, ShardingStrategy, sql, parameters, newHints, operationType), SqlStatementType.SELECT);

                    var temp = ShardingExecutor.ExecuteShardingScalar(statements);

                    if (temp.Count > 0)
                    {
                        if (temp.Count == 1)
                        {
                            result = temp[0];
                        }
                        else
                        {
                            throw new DalException("ExecScalar exception:more than one shard.");
                        }
                    }
                }
                return(result);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
            }
        }
Example #6
0
        /// <summary>
        /// 执行单返回值聚集查询指令
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">查询参数</param>
        /// <param name="hints">指令参数:如timeout</param>
        /// <param name="operationType">操作类型,读写分离,默认从master库读取</param>
        /// <returns>object</returns>
        /// <exception cref="DalException">数据访问框架异常</exception>
        public Object ExecScalar(String sql, StatementParameterCollection parameters, IDictionary hints, OperationType operationType)
        {
            Object result = null;

            if (!IsShardEnabled)
            {
                Statement statement = SqlBuilder.GetScalarStatement(LogicDbName, ShardingStrategy, sql, parameters, hints, operationType).Single();
                AddSqlToExtendParams(statement, hints);
                try
                {
                    result = DatabaseBridge.Instance.ExecuteScalar(statement);
                    return(result);
                }
                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 => SqlBuilder.GetScalarStatement(LogicDbName, ShardingStrategy, sql, parameters, newHints, operationType), SqlStatementType.SELECT);
                try
                {
                    var temp = ShardingExecutor.ExecuteShardingScalar(statements);
                    if (temp.Count == 1)
                    {
                        result = temp[0];
                        return(result);
                    }
                    else
                    {
                        throw new DalException("ExecScalar exception:more than one shard.");
                    }
                }
                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);
                    }
                }
            }
        }