Ejemplo n.º 1
0
        public async Task <HashGroup> QueryById(Guid id)
        {
            HashGroup group = null;

            //获取只读连接字符串
            var strConn = _hashConnectionFactory.CreateReadForHash();


            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, strConn, async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                    CommandText = string.Format(@"select {0},{1} from HashGroup as g join HashGroupStrategy as s 
                                                  on g.strategyid=s.id
                                                  where g.[id]=@id", StoreHelper.GetHashGroupSelectFields("g"), StoreHelper.GetHashGroupStrategySelectFields("s"))
                })
                {
                    var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                    {
                        Value = id
                    };
                    commond.Parameters.Add(parameter);

                    commond.Prepare();

                    SqlDataReader reader = null;

                    using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            group = new HashGroup();
                            StoreHelper.SetHashGroupSelectFields(group, reader, "g");
                            group.Strategy = new HashGroupStrategy();
                            StoreHelper.SetHashGroupStrategySelectFields(group.Strategy, reader, "s");
                        }

                        reader.Close();
                    }
                }
            });

            return(group);
        }
Ejemplo n.º 2
0
        public async Task QueryByAll(Guid groupId, Func <HashRealNode, Task> callback)
        {
            List <HashRealNode> result = new List <HashRealNode>();
            int  size    = 500;
            int  perSize = 500;
            Guid?nodeId  = null;

            //获取只读连接字符串
            var strConn = _hashConnectionFactory.CreateReadForHash();

            while (perSize == size)
            {
                result.Clear();

                await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, strConn, async (conn, transaction) =>
                {
                    SqlTransaction sqlTran = null;
                    if (transaction != null)
                    {
                        sqlTran = (SqlTransaction)transaction;
                    }

                    using (SqlCommand commond = new SqlCommand()
                    {
                        Connection = (SqlConnection)conn,
                        CommandType = CommandType.Text,
                        Transaction = sqlTran,
                        CommandText = string.Format(@"if @nodeid is null
                                                            begin
                                                                select top (@size) {0},{1},{2} from HashRealNode as n join HashGroup as g
                                                                on n.groupid=g.id
                                                                join HashGroupStrategy as s 
                                                                on g.strategyid=s.id                                                
                                                                where n.[groupid]=@groupid
                                                                order by n.id
                                                            end
                                                       else
                                                            begin
                                                                select top (@size) {0},{1},{2} from HashRealNode as n join HashGroup as g
                                                                on n.groupid=g.id
                                                                join HashGroupStrategy as s 
                                                                on g.strategyid=s.id                                                
                                                                where n.[groupid]=@groupid and n.id>@nodeid
                                                                order by n.id                                  
                                                            end", StoreHelper.GetHashRealNodeSelectFields("n"), StoreHelper.GetHashGroupSelectFields("g"), StoreHelper.GetHashGroupStrategySelectFields("s"))
                    })
                    {
                        var parameter = new SqlParameter("@groupid", SqlDbType.UniqueIdentifier)
                        {
                            Value = groupId
                        };
                        commond.Parameters.Add(parameter);

                        if (nodeId == null)
                        {
                            parameter = new SqlParameter("@nodeid", SqlDbType.UniqueIdentifier)
                            {
                                Value = DBNull.Value
                            };
                        }
                        else
                        {
                            parameter = new SqlParameter("@nodeid", SqlDbType.UniqueIdentifier)
                            {
                                Value = nodeId
                            };
                        }
                        commond.Parameters.Add(parameter);

                        parameter = new SqlParameter("@size", SqlDbType.Int)
                        {
                            Value = size
                        };
                        commond.Parameters.Add(parameter);


                        commond.Prepare();


                        SqlDataReader reader = null;

                        using (reader = await commond.ExecuteReaderAsync())
                        {
                            while (await reader.ReadAsync())
                            {
                                var record = new HashRealNode();
                                StoreHelper.SetHashRealNodeSelectFields(record, reader, "n");
                                record.Group = new HashGroup();
                                StoreHelper.SetHashGroupSelectFields(record.Group, reader, "g");
                                record.Group.Strategy = new HashGroupStrategy();
                                StoreHelper.SetHashGroupStrategySelectFields(record.Group.Strategy, reader, "s");
                                result.Add(record);

                                nodeId = record.ID;
                            }

                            reader.Close();
                        }
                    }
                });

                perSize = result.Count;

                foreach (var item in result)
                {
                    await callback(item);
                }
            }
        }