[fix]修正Sql处理器解析sql的bug

This commit is contained in:
大石头 2024-10-06 23:51:15 +08:00
parent ae24ba9964
commit 7c8f1c88b2
3 changed files with 48 additions and 14 deletions

View File

@ -26,8 +26,8 @@ public class SqlHandler : Handler
/// <returns></returns> /// <returns></returns>
public override Int32 Execute(JobContext ctx) public override Int32 Execute(JobContext ctx)
{ {
//var sqls = ctx.Task.Data as String; var sqls = ctx.Task.Data;
var sqls = Job.Data; //var sqls = Job.Data;
sqls = TemplateHelper.Build(sqls, ctx.Task.DataTime, ctx.Task.End); sqls = TemplateHelper.Build(sqls, ctx.Task.DataTime, ctx.Task.End);
// 向调度中心返回解析后的Sql语句 // 向调度中心返回解析后的Sql语句
ctx.Remark = sqls; ctx.Remark = sqls;
@ -54,12 +54,17 @@ public class SqlHandler : Handler
// 打开事务 // 打开事务
foreach (var item in sections) foreach (var item in sections)
if (item.Action != SqlActions.Query) DAL.Create(item.ConnName).BeginTransaction(); {
if (item.Action != SqlActions.Query)
DAL.Create(item.ConnName).BeginTransaction();
}
try try
{ {
// 按顺序执行处理Sql语句 // 按顺序执行处理Sql语句
DbTable dt = null; DbTable dt = null;
foreach (var section in sections) foreach (var section in sections)
{
switch (section.Action) switch (section.Action)
{ {
case SqlActions.Query: case SqlActions.Query:
@ -79,16 +84,23 @@ public class SqlHandler : Handler
default: default:
break; break;
} }
}
// 提交事务 // 提交事务
foreach (var item in sections) foreach (var item in sections)
if (item.Action != SqlActions.Query) DAL.Create(item.ConnName).Commit(); {
if (item.Action != SqlActions.Query)
DAL.Create(item.ConnName).Commit();
}
} }
catch catch
{ {
// 回滚事务 // 回滚事务
foreach (var item in sections) foreach (var item in sections)
if (item.Action != SqlActions.Query) DAL.Create(item.ConnName).Rollback(); {
if (item.Action != SqlActions.Query)
DAL.Create(item.ConnName).Rollback();
}
throw; throw;
} }

View File

@ -32,6 +32,12 @@ public class SqlSection
public String Sql { get; set; } public String Sql { get; set; }
#endregion #endregion
#region
/// <summary>已重载</summary>
/// <returns></returns>
public override String ToString() => $"{ConnName}[{Action}]:{Sql}";
#endregion
#region #region
/// <summary>分析sql语句集合得到片段集合以双换行分隔</summary> /// <summary>分析sql语句集合得到片段集合以双换行分隔</summary>
/// <param name="sqls"></param> /// <param name="sqls"></param>
@ -103,9 +109,25 @@ public class SqlSection
// 解析数据表,如果目标表不存在,则返回 // 解析数据表,如果目标表不存在,则返回
var tableName = ""; var tableName = "";
if (Sql.StartsWithIgnoreCase("delete ")) if (Sql.StartsWithIgnoreCase("delete "))
tableName = Sql.Substring(" from ", " ")?.Trim(); {
else if (Sql.StartsWithIgnoreCase("udpate ")) var sep = " from ";
tableName = Sql.Substring("udpate ", " ")?.Trim(); var p1 = Sql.IndexOf(sep);
if (p1 < 0) throw new InvalidDataException();
p1 += sep.Length;
var p2 = Sql.IndexOf(" ", p1);
tableName = p2 > 0 ? Sql[p1..p2].Trim() : Sql[p1..].Trim();
}
else if (Sql.StartsWithIgnoreCase("update "))
{
var sep = "update ";
var p1 = Sql.IndexOf(sep);
if (p1 < 0) throw new InvalidDataException();
p1 += sep.Length;
var p2 = Sql.IndexOf(" ", p1);
tableName = p2 > 0 ? Sql[p1..p2].Trim() : Sql[p1..].Trim();
}
if (!tableName.IsNullOrEmpty()) if (!tableName.IsNullOrEmpty())
{ {

View File

@ -1,9 +1,7 @@
using System; using System.Linq;
using System.Linq;
using AntJob; using AntJob;
using AntJob.Data; using AntJob.Data;
using AntJob.Extensions; using AntJob.Extensions;
using NewLife.Reflection;
using XCode.DataAccessLayer; using XCode.DataAccessLayer;
using XCode.Membership; using XCode.Membership;
using Xunit; using Xunit;
@ -44,11 +42,13 @@ public class SqlHandlerTests
Task = task, Task = task,
}; };
var method = handler.GetType().GetMethodEx("OnProcess", typeof(JobContext)); //var method = handler.GetType().GetMethodEx("OnProcess", typeof(JobContext));
method.Invoke(handler, new Object[] { ctx }); //method.Invoke(handler, new Object[] { ctx });
var rs = handler.Execute(ctx);
Assert.Equal(4, ctx.Total); Assert.Equal(4, ctx.Total);
//Assert.Equal(4, ctx.Success); //Assert.Equal(4, ctx.Success);
Assert.True(ctx.Success > 0); Assert.Equal(8, rs);
//Assert.True(ctx.Success > 0);
} }
} }