diff --git a/AntJob.Extensions/SqlHandler.cs b/AntJob.Extensions/SqlHandler.cs
index b490580..197aecb 100644
--- a/AntJob.Extensions/SqlHandler.cs
+++ b/AntJob.Extensions/SqlHandler.cs
@@ -26,8 +26,8 @@ public class SqlHandler : Handler
///
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;
}
diff --git a/AntJob.Extensions/SqlSection.cs b/AntJob.Extensions/SqlSection.cs
index 04778a3..ae4194a 100644
--- a/AntJob.Extensions/SqlSection.cs
+++ b/AntJob.Extensions/SqlSection.cs
@@ -32,6 +32,12 @@ public class SqlSection
public String Sql { get; set; }
#endregion
+ #region 构造
+ /// 已重载
+ ///
+ public override String ToString() => $"{ConnName}[{Action}]:{Sql}";
+ #endregion
+
#region 解析
/// 分析sql语句集合,得到片段集合,以双换行分隔
///
@@ -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())
{
diff --git a/AntTest/SqlHandlerTests.cs b/AntTest/SqlHandlerTests.cs
index 05f3dfa..0161180 100644
--- a/AntTest/SqlHandlerTests.cs
+++ b/AntTest/SqlHandlerTests.cs
@@ -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);
}
}