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

View File

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

View File

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