整理代码

This commit is contained in:
2024-01-15 17:26:44 +08:00
parent 0984853c79
commit 78cd833617
7 changed files with 114 additions and 139 deletions

View File

@@ -17,7 +17,7 @@ public class MySqlDestination : IDisposable, IAsyncDisposable
private readonly bool _prettyOutput;
private readonly int _maxAllowPacket;
private readonly ProcessContext _context;
private static StringBuilder recordSb = new StringBuilder();
public MySqlDestination(string connStr, ILogger logger, ProcessContext context,bool prettyOutput = false)
{
_conn = new MySqlConnection(connStr);
@@ -53,29 +53,27 @@ public class MySqlDestination : IDisposable, IAsyncDisposable
if (_recordCache.Count == 0)
return;
//var cmd = _conn.CreateCommand();
//cmd.CommandTimeout = 3 * 60;
var cmd = _conn.CreateCommand();
cmd.CommandTimeout = 3 * 60;
var excuseList = GetExcuseList(_recordCache, maxAllowPacket, _prettyOutput);
try
{
var excuseList = GetExcuseList(_recordCache, maxAllowPacket, _prettyOutput);
//foreach (var insertSql in excuseList)
//{
// //cmd.CommandText = insertSql;
// //await cmd.ExecuteNonQueryAsync();
// //_logger.LogInformation(@"do insert completed!size:{Length}", cmd.CommandText.Length);
//}
foreach (var insertSql in excuseList)
{
cmd.CommandText = insertSql;
await cmd.ExecuteNonQueryAsync();
}
_recordCache.Clear();
}
catch (Exception e)
{
//_logger.LogCritical(e, "Error when flushing records, sql: {Sql}", cmd.CommandText.Omit(1000));
_logger.LogCritical(e, "Error when flushing records, sql: {Sql}", cmd.CommandText.Omit(1000));
_context.AddException(e);
throw;
}
finally
{
//await cmd.DisposeAsync();
await cmd.DisposeAsync();
}
}
@@ -83,25 +81,24 @@ public class MySqlDestination : IDisposable, IAsyncDisposable
bool prettyOutput = false)
{
var resultList = new List<string>();
var headerSb = string.Empty;
//var recordSb = new StringBuilder();
recordSb.Clear();
var headerSb = new StringBuilder();
var recordSb = new StringBuilder();
foreach (var (tableName, records) in tableRecords)
{
if (records.Count == 0)
continue;
headerSb=$"INSERT INTO `{tableName}`(";
headerSb.Append($"INSERT INTO `{tableName}`(");
for (var i = 0; i < records[0].Headers.Length; i++)
{
var header = records[0].Headers[i];
headerSb+=$"`{header}`";
headerSb.Append($"`{header}`");
if (i != records[0].Headers.Length - 1)
headerSb.Append(',');
}
headerSb+=") VALUES ";
headerSb.Append(") VALUES ");
if (prettyOutput)
headerSb+="/r/n";
headerSb.AppendLine();
var sbList = new List<string>();
var currentLength = headerSb.Length;
@@ -112,6 +109,11 @@ public class MySqlDestination : IDisposable, IAsyncDisposable
for (var j = 0; j < record.Fields.Length; j++)
{
var field = record.Fields[j];
if (record.TableName == "order_block_plan_result" && j == 2)
{
recordSb.Append("0x"+field);
}
else
recordSb.Append(field);
if (j != record.Fields.Length - 1)
recordSb.Append(',');
@@ -126,12 +128,12 @@ public class MySqlDestination : IDisposable, IAsyncDisposable
if (currentLength + recordSb.Length >= maxAllowPacket)
{
var insertSb = headerSb;
var insertSb = new StringBuilder(headerSb.ToString());
insertSb+=string.Join(",", sbList);
insertSb += ";";
resultList.Add(insertSb);
insertSb=String.Empty;
insertSb.Append(string.Join(",", sbList));
insertSb.Append(";");
resultList.Add(insertSb.ToString());
insertSb.Clear();
sbList.Clear();
currentLength = headerSb.Length;
sbList.Add(recordSb.ToString());
@@ -145,15 +147,18 @@ public class MySqlDestination : IDisposable, IAsyncDisposable
}
if (sbList.Count > 0)
{
var insertSb = headerSb.ToString();
insertSb += string.Join(",", sbList);
insertSb += ";";
var insertSb = new StringBuilder(headerSb.ToString());
insertSb.Append(string.Join(",", sbList));
insertSb.Append(";");
resultList.Add(insertSb.ToString());
insertSb=string.Empty;
insertSb.Clear();
}
headerSb=string.Empty;
headerSb.Clear();
}
if (resultList.Count == 2)
{
var a = 1;
}
return resultList;
}