2025迁移版本,多项规则修改
This commit is contained in:
@@ -5,6 +5,11 @@ namespace MesETL.Shared.Helper;
|
||||
|
||||
public static class DatabaseHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建一个MySql连接
|
||||
/// </summary>
|
||||
/// <param name="connStr"></param>
|
||||
/// <returns></returns>
|
||||
public static MySqlConnection CreateConnection(string connStr)
|
||||
{
|
||||
var newConnStr = new MySqlConnectionStringBuilder(connStr)
|
||||
@@ -15,6 +20,13 @@ public static class DatabaseHelper
|
||||
return new MySqlConnection(newConnStr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用语句查询数据库
|
||||
/// </summary>
|
||||
/// <param name="connStr"></param>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="ct"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<DataSet> QueryTableAsync(string connStr, string sql, CancellationToken ct = default)
|
||||
{
|
||||
await using var conn = CreateConnection(connStr);
|
||||
@@ -27,6 +39,13 @@ public static class DatabaseHelper
|
||||
return ds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用语句进行标量查询
|
||||
/// </summary>
|
||||
/// <param name="connStr"></param>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="ct"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<object?> QueryScalarAsync(string connStr, string sql, CancellationToken ct = default)
|
||||
{
|
||||
await using var conn = CreateConnection(connStr);
|
||||
@@ -37,6 +56,13 @@ public static class DatabaseHelper
|
||||
return await cmd.ExecuteScalarAsync(ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行非查询语句
|
||||
/// </summary>
|
||||
/// <param name="connStr"></param>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="ct"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<int> NonQueryAsync(string connStr, string sql, CancellationToken ct = default)
|
||||
{
|
||||
await using var conn = CreateConnection(connStr);
|
||||
@@ -47,6 +73,13 @@ public static class DatabaseHelper
|
||||
return await cmd.ExecuteNonQueryAsync(ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在事务中执行语句
|
||||
/// </summary>
|
||||
/// <param name="connStr"></param>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<int> TransactionAsync(string connStr, string sql, params MySqlParameter[] parameters)
|
||||
{
|
||||
await using var conn = CreateConnection(connStr);
|
||||
|
63
MesETL.Shared/Helper/Extensions.Lang.cs
Normal file
63
MesETL.Shared/Helper/Extensions.Lang.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
namespace Azusa.Shared.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 使用Range作为参数的迭代器方法
|
||||
/// <br/>
|
||||
/// 扩展foreach关键字来实现类似<c>foreach (var i in 1..5)</c>的效果
|
||||
/// </summary>
|
||||
public static class ForeachExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 拓展Range结构实现GetEnumerator方法供foreach读取,实现foreach(var i in x..y)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static CustomIntEnumerator GetEnumerator(this Range range)
|
||||
{
|
||||
return new CustomIntEnumerator(range);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拓展int类实现GetEnumerator方法供foreach读取,实现foreach(var i in x)
|
||||
/// </summary>
|
||||
/// <param name="end"></param>
|
||||
/// <returns></returns>
|
||||
public static CustomIntEnumerator GetEnumerator(this int end)
|
||||
{
|
||||
return new CustomIntEnumerator(end);
|
||||
return new CustomIntEnumerator(new Range(0, end));//在执行空函数时性能比上一句低10倍,Why
|
||||
}
|
||||
}
|
||||
|
||||
//使用引用结构体增强性能
|
||||
public ref struct CustomIntEnumerator
|
||||
{
|
||||
private int _current;
|
||||
private readonly int _end;
|
||||
|
||||
public CustomIntEnumerator(Range range)
|
||||
{
|
||||
//避免某些时候从结尾开始编制
|
||||
// x.. 时会产生Range(x,^0)
|
||||
if (range.End.IsFromEnd)
|
||||
{
|
||||
throw new NotSupportedException("不支持从结尾编制索引");
|
||||
}
|
||||
_current = range.Start.Value - 1;
|
||||
_end = range.End.Value - 1;//迭代器不包含范围的尾部
|
||||
}
|
||||
|
||||
public CustomIntEnumerator(int end)
|
||||
{
|
||||
_current = -1;
|
||||
_end = end;
|
||||
}
|
||||
|
||||
/* 注意,供foeach使用的迭代器不需要实现IEnumerator接口,只需要提供Current属性以及MoveNext方法即可,*/
|
||||
public int Current => _current;
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
_current++;
|
||||
return _current <= _end;
|
||||
}
|
||||
}
|
22
MesETL.Shared/Helper/Helper.Compress.cs
Normal file
22
MesETL.Shared/Helper/Helper.Compress.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace MesETL.Shared.Helper;
|
||||
|
||||
public class CompressHelper
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] CompressDeflate(byte[] data)
|
||||
{
|
||||
using var src = new MemoryStream(data);
|
||||
|
||||
using var outStream = new MemoryStream();
|
||||
using var gzip = new DeflateStream(outStream, CompressionMode.Compress);
|
||||
src.CopyTo(gzip);
|
||||
gzip.Flush();
|
||||
return outStream.ToArray();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user