public async Task <BusinessActionGroup> QueryByName(string name) { BusinessActionGroup group = null; await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _businessSecurityRuleConnectionFactory.CreateReadForBusinessSecurityRule(), 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} from BusinessActionGroup WITH (SNAPSHOT) where [name]=@name", StoreHelper.GetBusinessActionGroupSelectFields(string.Empty)) }) { var parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150) { Value = name }; commond.Parameters.Add(parameter); commond.Prepare(); SqlDataReader reader = null; using (reader = await commond.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { group = new BusinessActionGroup(); StoreHelper.SetBusinessActionGroupSelectFields(group, reader, string.Empty); } reader.Close(); } } }); return(group); }
public async Task <QueryResult <BusinessActionGroup> > QueryByName(string name, int page, int pageSize) { QueryResult <BusinessActionGroup> result = new QueryResult <BusinessActionGroup>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _businessSecurityRuleConnectionFactory.CreateReadForBusinessSecurityRule(), 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(@"set @currentpage=@page select @count= count(*) from BusinessActionGroup WITH (SNAPSHOT) 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 BusinessActionGroup WITH (SNAPSHOT) where [name] like @name order by [actsequence] offset (@pagesize * (@currentpage - 1)) rows fetch next @pagesize rows only;" , StoreHelper.GetBusinessActionGroupSelectFields(string.Empty)) }) { 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("@name", SqlDbType.NVarChar, 150) { Value = $"{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); commond.Prepare(); SqlDataReader reader = null; using (reader = await commond.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var group = new BusinessActionGroup(); StoreHelper.SetBusinessActionGroupSelectFields(group, reader, string.Empty); result.Results.Add(group); } reader.Close(); result.TotalCount = (int)commond.Parameters["@count"].Value; result.CurrentPage = (int)commond.Parameters["@currentpage"].Value; } } }); return(result); }
public async Task <BusinessAction> QueryById(Guid id) { BusinessAction action = null; await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _businessSecurityRuleConnectionFactory.CreateReadForBusinessSecurityRule(), 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} from BusinessAction WITH (SNAPSHOT) where [id]=@id", StoreHelper.GetBusinessActionSelectFields(string.Empty)) }) { var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = id }; commond.Parameters.Add(parameter); commond.Prepare(); using (var reader = await commond.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { action = new BusinessAction(); StoreHelper.SetBusinessActionSelectFields(action, reader, string.Empty); } reader.Close(); } } }); return(action); }
public async Task <QueryResult <BusinessAction> > QueryByNullRelationGroup(Guid groupId, int page, int pageSize) { QueryResult <BusinessAction> result = new QueryResult <BusinessAction>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _businessSecurityRuleConnectionFactory.CreateReadForBusinessSecurityRule(), 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(@"set @currentpage=@page select @count= count(*) from [dbo].[BusinessAction] WITH (SNAPSHOT) as act left outer join ( select act.id as existid from [dbo].[BusinessAction] WITH (SNAPSHOT) as act join [dbo].[BusinessActionGroupRelation] WITH (SNAPSHOT) as relation on act.id=relation.actionid where relation.groupid=@groupid )as t on act.id=t.existid where t.existid is null 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 [dbo].[BusinessAction] as act left outer join ( select act.id as existid from [dbo].[BusinessAction] WITH (SNAPSHOT) as act join [dbo].[BusinessActionGroupRelation] WITH (SNAPSHOT) as relation on act.id=relation.actionid where relation.groupid=@groupid )as t on act.id=t.existid where t.existid is null order by [actsequence] offset (@pagesize * (@currentpage - 1)) rows fetch next @pagesize rows only;" , StoreHelper.GetBusinessActionSelectFields("act")) }) { 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("@groupid", SqlDbType.UniqueIdentifier) { Value = groupId }; 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); commond.Prepare(); using (var reader = await commond.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var action = new BusinessAction(); StoreHelper.SetBusinessActionSelectFields(action, reader, "act"); result.Results.Add(action); } reader.Close(); result.TotalCount = (int)commond.Parameters["@count"].Value; result.CurrentPage = (int)commond.Parameters["@currentpage"].Value; } } }); return(result); }
public async Task QueryByGroup(Guid groupId, Func <BusinessAction, Task> callback) { List <BusinessAction> actionList = new List <BusinessAction>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _businessSecurityRuleConnectionFactory.CreateReadForBusinessSecurityRule(), async (conn, transaction) => { Int64?sequence = null; int pageSize = 500; while (true) { actionList.Clear(); SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } 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 BusinessAction WITH (SNAPSHOT) as act join BusinessActionGroupRelation WITH (SNAPSHOT) as relation on act.id=relation.[actionid] where relation.[groupid]=@groupid order by act.[sequence]", StoreHelper.GetBusinessActionSelectFields("act")); } else { commond.CommandText = string.Format(@"select top (@pagesize) {0} from BusinessAction WITH (SNAPSHOT) as act join BusinessActionGroupRelation WITH (SNAPSHOT) as relation on act.id=relation.[actionid] where relation.[groupid]=@groupid and act.[sequence]>@sequence order by act.[sequence]", StoreHelper.GetBusinessActionSelectFields("act")); } var parameter = new SqlParameter("@groupid", SqlDbType.UniqueIdentifier) { Value = groupId }; 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); } commond.Prepare(); using (var reader = await commond.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var action = new BusinessAction(); StoreHelper.SetBusinessActionSelectFields(action, reader, "act"); sequence = (Int64)reader["actsequence"]; actionList.Add(action); } reader.Close(); } } foreach (var actionItem in actionList) { await callback(actionItem); } if (actionList.Count != pageSize) { break; } } }); }