Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }