Exemple #1
0
        public async Task QueryByRecordUniqueName(string storeGroupName, string hashInfo, string recordUniqueName, int status, Func <DTOperationData, Task> action)
        {
            string strConn = _transactionConnectionFactory.CreateAllForDTOperationData();

            var(connNames, tableName) = await getStoreInfo(storeGroupName, hashInfo);

            if (connNames != null)
            {
                strConn = _transactionConnectionFactory.CreateAllForDTOperationData(connNames);
            }
            else
            {
                tableName = "DTOperationData";
            }

            List <DTOperationData> dataList = new List <DTOperationData>();

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

                while (true)
                {
                    dataList.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 {1} where status=@status and recorduniquename=@recorduniquename order by [sequence]", StoreHelper.GetDTOperationDataSelectFields(string.Empty), tableName);
                        }
                        else
                        {
                            commond.CommandText = string.Format(@"select top (@pagesize) {0} from {1} where status=@status and recorduniquename=@recorduniquename and [sequence]>@sequence order by [sequence]", StoreHelper.GetDTOperationDataSelectFields(string.Empty), tableName);
                        }

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

                        parameter = new SqlParameter("@recorduniquename", SqlDbType.VarChar, 150)
                        {
                            Value = recordUniqueName
                        };
                        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 data = new DTOperationData();
                                StoreHelper.SetDTOperationDataSelectFields(data, reader, string.Empty);
                                sequence = (Int64)reader["sequence"];
                                dataList.Add(data);
                            }

                            await reader.CloseAsync();
                        }
                    }

                    foreach (var dataItem in dataList)
                    {
                        await action(dataItem);
                    }

                    if (dataList.Count != pageSize)
                    {
                        break;
                    }
                }
            });
        }
        public async Task <List <DTOperationRecord> > QueryBySkip(string storeInfo, int status, int skip, int take)
        {
            var info = JsonSerializerHelper.Deserialize <StoreInfo>(storeInfo);

            if (info.DBConnectionNames == null || info.TableNames == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.StoreInfoTypeError,
                    DefaultFormatting = "存储信息{0}要求的格式为{1},发生位置{2}",
                    ReplaceParameters = new List <object>()
                    {
                        storeInfo, typeof(StoreInfo).ToString(), $"{this.GetType().FullName}.QueryBySkip"
                    }
                };

                throw new UtilityException((int)Errors.StoreInfoTypeError, fragment);
            }

            if (!info.TableNames.TryGetValue(_entityName, out string tableName))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundEntityNameInStoreInfo,
                    DefaultFormatting = "存储信息{0}中缺少实体名称为{1}的实体表映射",
                    ReplaceParameters = new List <object>()
                    {
                        storeInfo, _entityName
                    }
                };

                throw new UtilityException((int)Errors.NotFoundEntityNameInStoreInfo, fragment);
            }


            List <DTOperationRecord> dataList = new List <DTOperationRecord>();

            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _transactionConnectionFactory.CreateAllForDTOperationRecord(info.DBConnectionNames), 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
                })
                {
                    commond.CommandText = string.Format(@"select {0} from {1} where [status]=@status order by [sequence] OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY", StoreHelper.GetDTOperationRecordSelectFields(string.Empty));


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

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

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

                    await commond.PrepareAsync();


                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            var data = new DTOperationRecord();
                            StoreHelper.SetDTOperationRecordSelectFields(data, reader, string.Empty);
                            dataList.Add(data);
                        }

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

            return(dataList);
        }
Exemple #3
0
        public async Task <DTOperationData> QueryByID(string storeGroupName, string hashInfo, Guid id)
        {
            string strConn = _transactionConnectionFactory.CreateAllForDTOperationData();

            var(connNames, tableName) = await getStoreInfo(storeGroupName, hashInfo);

            if (connNames != null)
            {
                strConn = _transactionConnectionFactory.CreateAllForDTOperationData(connNames);
            }
            else
            {
                tableName = "DTOperationData";
            }


            DTOperationData data = null;

            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, strConn, 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 {1} where [id]=@id", StoreHelper.GetDTOperationDataSelectFields(string.Empty), tableName)
                })
                {
                    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())
                        {
                            data = new DTOperationData();
                            StoreHelper.SetDTOperationDataSelectFields(data, reader, string.Empty);
                        }

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

            return(data);
        }
        public async Task <DTOperationRecord> QueryByUniqueName(string storeGroupName, string hashInfo, string uniqueName)
        {
            string strConn = _transactionConnectionFactory.CreateAllForDTOperationRecord();

            var(connNames, tableName) = await getStoreInfo(storeGroupName, hashInfo);

            if (connNames != null)
            {
                strConn = _transactionConnectionFactory.CreateAllForDTOperationRecord(connNames);
            }
            else
            {
                tableName = "DTOperationRecord";
            }

            DTOperationRecord record = null;

            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, strConn, 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 {1} where [uniquename]=@uniquename", StoreHelper.GetDTOperationRecordSelectFields(string.Empty), tableName)
                })
                {
                    var parameter = new SqlParameter("@uniquename", SqlDbType.VarChar, 150)
                    {
                        Value = uniqueName
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();

                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            record = new DTOperationRecord();
                            StoreHelper.SetDTOperationRecordSelectFields(record, reader, string.Empty);
                        }

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

            return(record);
        }