public async Task <SMessageTypeListener> QueryByTypeRelation(Guid typeId, Guid id)
        {
            SMessageTypeListener listener = 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},{1} from SMessageTypeListener as listener join SMessageExecuteType as mtype on listener.messagetypeid=mtype.id where listener.[id]=@id and mtype.[id]=@typeid", StoreHelper.GetSMessageTypeListenerSelectFields("listener"), StoreHelper.GetSMessageExecuteTypeSelectFields("mtype"))
                })
                {
                    var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                    {
                        Value = id
                    };
                    commond.Parameters.Add(parameter);


                    parameter = new SqlParameter("@typeid", SqlDbType.UniqueIdentifier)
                    {
                        Value = typeId
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();

                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            listener = new SMessageTypeListener();
                            StoreHelper.SetSMessageTypeListenerSelectFields(listener, reader, "listener");
                            listener.MessageType = new SMessageExecuteType();
                            StoreHelper.SetSMessageExecuteTypeSelectFields(listener.MessageType, reader, "mtype");
                        }

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

            return(listener);
        }
        public async Task <QueryResult <SMessageTypeListener> > QueryByType(Guid typeId, int page, int pageSize)
        {
            QueryResult <SMessageTypeListener> result = new QueryResult <SMessageTypeListener>();

            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 SMessageTypeListener where messagetypeid=@typeid
		                           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},{1} from SMessageTypeListener as listener join SMessageExecuteType as mtype where listener.messagetypeid=@typeid
                                    order by listener.[sequence]
		                            offset (@pagesize * (@currentpage - 1)) rows 
		                            fetch next @pagesize rows only;"        , StoreHelper.GetSMessageTypeListenerSelectFields("listener"), StoreHelper.GetSMessageExecuteTypeSelectFields("mtype"))
                })
                {
                    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("@typeid", SqlDbType.UniqueIdentifier)
                    {
                        Value = typeId
                    };
                    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 listener = new SMessageTypeListener();
                            StoreHelper.SetSMessageTypeListenerSelectFields(listener, reader, "listener");
                            listener.MessageType = new SMessageExecuteType();
                            StoreHelper.SetSMessageExecuteTypeSelectFields(listener.MessageType, reader, "mtype");
                            result.Results.Add(listener);
                        }

                        await reader.CloseAsync();

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

            return(result);
        }
Example #3
0
        public async Task <SMessageExecuteType> QueryByName(string name)
        {
            SMessageExecuteType type = 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 SMessageExecuteType where [name]=@name", StoreHelper.GetSMessageExecuteTypeSelectFields(string.Empty))
                })
                {
                    var parameter = new SqlParameter("@name", SqlDbType.VarChar, 150)
                    {
                        Value = name
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();

                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            type = new SMessageExecuteType();
                            StoreHelper.SetSMessageExecuteTypeSelectFields(type, reader, string.Empty);
                        }

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

            return(type);
        }
        public async Task QueryByType(Guid typeId, Func <SMessageTypeListener, Task> callback)
        {
            List <SMessageTypeListener> listenerList = new List <SMessageTypeListener>();

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

                while (true)
                {
                    listenerList.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},{1} from SMessageTypeListener as listener join SMessageExecuteType as mtype on listener.messagetypeid=mtype.id where mtype.id=@typeid order by listener.[sequence]", StoreHelper.GetSMessageTypeListenerSelectFields("listener"), StoreHelper.GetSMessageExecuteTypeSelectFields("mtype"));
                        }
                        else
                        {
                            commond.CommandText = string.Format(@"select top (@pagesize) {0},{1} from SMessageTypeListener as listener join SMessageExecuteType as mtype on listener.messagetypeid=mtype.id where mtype.id=@typeid and listener.[sequence]>@sequence order by listener.[sequence]", StoreHelper.GetSMessageTypeListenerSelectFields("listener"), StoreHelper.GetSMessageExecuteTypeSelectFields("mtype"));
                        }

                        var parameter = new SqlParameter("@typeid", SqlDbType.UniqueIdentifier)
                        {
                            Value = typeId
                        };
                        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 listener = new SMessageTypeListener();
                                StoreHelper.SetSMessageTypeListenerSelectFields(listener, reader, "listener");
                                sequence             = (Int64)reader["listenersequence"];
                                listener.MessageType = new SMessageExecuteType();
                                StoreHelper.SetSMessageExecuteTypeSelectFields(listener.MessageType, reader, "mtype");
                                listenerList.Add(listener);
                            }

                            await reader.CloseAsync();
                        }
                    }

                    foreach (var listenerItem in listenerList)
                    {
                        await callback(listenerItem);
                    }

                    if (listenerList.Count != pageSize)
                    {
                        break;
                    }
                }
            });
        }
Example #5
0
        public async Task <QueryResult <SMessageExecuteType> > Query(string name, int page, int pageSize)
        {
            QueryResult <SMessageExecuteType> result = new QueryResult <SMessageExecuteType>();

            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 SMessageExecuteType where [name] like @name
		                           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 SMessageExecuteType where [name] like @name
                                    order by [sequence] desc 
		                            offset (@pagesize * (@currentpage - 1)) rows 
		                            fetch next @pagesize rows only;"        , StoreHelper.GetSMessageExecuteTypeSelectFields(string.Empty))
                })
                {
                    var parameter = new SqlParameter("@page", SqlDbType.Int)
                    {
                        Value = page
                    };
                    commond.Parameters.Add(parameter);

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

                    parameter = new SqlParameter("@name", SqlDbType.VarChar, 200)
                    {
                        Value = string.Format("{0}%", name.ToSqlLike())
                    };
                    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 type = new SMessageExecuteType();
                            StoreHelper.SetSMessageExecuteTypeSelectFields(type, reader, string.Empty);
                            result.Results.Add(type);
                        }

                        await reader.CloseAsync();

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

            return(result);
        }