Пример #1
0
        public async Task <SQueue> QueryByCode(string groupName, bool isDead, int code)
        {
            SQueue queue = null;

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

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                    CommandText = string.Format(@"select {0} from SQueue where [groupname]=@groupname and [code]=@code and [isdead]=@isdead", StoreHelper.GetSQueueSelectFields(string.Empty))
                })
                {
                    var parameter = new SqlParameter("@groupname", SqlDbType.VarChar, 150)
                    {
                        Value = groupName
                    };
                    commond.Parameters.Add(parameter);

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

                    parameter = new SqlParameter("@isdead", SqlDbType.Bit)
                    {
                        Value = isDead
                    };
                    commond.Parameters.Add(parameter);


                    await commond.PrepareAsync();

                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            queue = new SQueue();
                            StoreHelper.SetSQueueSelectFields(queue, reader, string.Empty);
                        }

                        await reader.CloseAsync();
                    }
                }
            });

            return(queue);
        }
Пример #2
0
        public async Task <SQueue> QueryById(Guid id)
        {
            SQueue queue = null;

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

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                    CommandText = string.Format(@"select {0} from SQueue where [id]=@id", StoreHelper.GetSQueueSelectFields(string.Empty))
                })
                {
                    var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                    {
                        Value = id
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();

                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            queue = new SQueue();
                            StoreHelper.SetSQueueSelectFields(queue, reader, string.Empty);
                        }

                        await reader.CloseAsync();
                    }
                }
            });

            return(queue);
        }
Пример #3
0
        public async Task QueryByProceeGroup(string processGroupName, Func <SQueue, Task> callback)
        {
            List <SQueue> queueList = new List <SQueue>();

            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _messageQueueConnectionFactory.CreateReadForMessageQueueMain(), async (conn, transaction) =>
            {
                Int64?sequence = null;
                int pageSize   = 500;

                while (true)
                {
                    queueList.Clear();

                    SqlTransaction sqlTran = null;
                    if (transaction != null)
                    {
                        sqlTran = (SqlTransaction)transaction;
                    }

                    await using (SqlCommand commond = new SqlCommand()
                    {
                        Connection = (SqlConnection)conn,
                        CommandType = CommandType.Text,
                        Transaction = sqlTran
                    })
                    {
                        if (!sequence.HasValue)
                        {
                            commond.CommandText = string.Format(@"select top (@pagesize) {0} from SQueue as q join SQueueProcessGroup as g on q.processgroupid=g.id where g.[name]=@processgroupname order by [sequence]", StoreHelper.GetSQueueSelectFields("q"));
                        }
                        else
                        {
                            commond.CommandText = string.Format(@"select top (@pagesize) {0} from SQueue as q join SQueueProcessGroup as g on q.processgroupid=g.id where g.[name]=@processgroupname and [q.sequence]>@sequence order by [sequence]", StoreHelper.GetSQueueSelectFields("q"));
                        }

                        var parameter = new SqlParameter("@processgroupname", SqlDbType.VarChar, 150)
                        {
                            Value = processGroupName
                        };
                        commond.Parameters.Add(parameter);

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

                        if (sequence.HasValue)
                        {
                            parameter = new SqlParameter("@sequence", SqlDbType.BigInt)
                            {
                                Value = sequence
                            };
                            commond.Parameters.Add(parameter);
                        }

                        await commond.PrepareAsync();


                        SqlDataReader reader = null;

                        await using (reader = await commond.ExecuteReaderAsync())
                        {
                            while (await reader.ReadAsync())
                            {
                                var queue = new SQueue();
                                StoreHelper.SetSQueueSelectFields(queue, reader, string.Empty);
                                sequence = (Int64)reader["q.sequence"];
                                queueList.Add(queue);
                            }

                            await reader.CloseAsync();
                        }
                    }

                    foreach (var queueItem in queueList)
                    {
                        await callback(queueItem);
                    }

                    if (queueList.Count != pageSize)
                    {
                        break;
                    }
                }
            });
        }
Пример #4
0
        public async Task <QueryResult <SQueue> > QueryByProceeGroup(string processGroupName, int page, int pageSize)
        {
            QueryResult <SQueue> result = new QueryResult <SQueue>();

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

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                    CommandText = string.Format(@"set @currentpage=@page
		                           select @count= count(*) from SQueue as q join SQueueProcessGroup as g on q.processgroupid=g.id where g.[name]=@processgroupname
		                           if @pagesize*@page>=@count
			                          begin
				                           set @currentpage= @count/@pagesize
				                           if @count%@pagesize<>0
					                           begin
						                            set @currentpage=@currentpage+1
					                           end
				                           if @currentpage=0
					                           set @currentpage=1
			                          end
		                            else if @page<1 
			                           begin 
				                           set @currentpage=1
			                           end
	
                                    select {0} from SQueue as q join SQueueProcessGroup as g on q.processgroupid=g.id where g.[name]=@processgroupname
                                    order by [sequence]
		                            offset (@pagesize * (@currentpage - 1)) rows 
		                            fetch next @pagesize rows only;"        , StoreHelper.GetSQueueSelectFields("q"))
                })
                {
                    var parameter = new SqlParameter("@page", SqlDbType.Int)
                    {
                        Value = page
                    };
                    commond.Parameters.Add(parameter);

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

                    parameter = new SqlParameter("@processgroupname", SqlDbType.NVarChar, 100)
                    {
                        Value = processGroupName
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@count", SqlDbType.Int)
                    {
                        Direction = ParameterDirection.Output
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@currentpage", SqlDbType.Int)
                    {
                        Direction = ParameterDirection.Output
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();

                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            var queue = new SQueue();
                            StoreHelper.SetSQueueSelectFields(queue, reader, string.Empty);
                            result.Results.Add(queue);
                        }

                        await reader.CloseAsync();

                        result.TotalCount  = (int)commond.Parameters["@count"].Value;
                        result.CurrentPage = (int)commond.Parameters["@currentpage"].Value;
                    }
                }
            });

            return(result);
        }