Skip to content

Commit

Permalink
[fix]分析sql语句表名时,表名前面可能有schema
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Mar 22, 2024
1 parent 4108ca0 commit 7f59106
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions XCode/DataAccessLayer/DAL_DbOperate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ private async Task<TResult> InvokeAsync<T1, T2, T3, TResult>(T1 k1, T2 k2, T3 k3
}
}

private static readonly Regex reg_table = new("(?:\\s+from|insert\\s+into|update|\\s+join|drop\\s+table|truncate\\s+table)\\s+[`'\"\\[]?([\\w]+)[`'\"\\[]?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex reg_table = new("""(?:\s+from|insert\s+into|update|\s+join|drop\s+table|truncate\s+table)\s+(?:[`'"\[]?[\w]+[`'"\]]?\.)?[`'"\[]?([\w]+)[`'"\]]?""", RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary>从Sql语句中截取表名</summary>
/// <param name="sql">Sql语句</param>
/// <param name="trimShard">是否去掉表名后面的分表信息。如日期分表</param>
Expand All @@ -615,12 +615,12 @@ public static String[] GetTables(String sql, Boolean trimShard)
{
//list.Add(item.Groups[1].Value);
var tableName = item.Groups[1].Value;
if (trimShard && tableName.Contains("_"))
if (trimShard)
{
var p = tableName.LastIndexOf('_');
if (p > 0 && tableName.Substring(p + 1).ToInt() > 0)
if (p > 0 && tableName[(p + 1)..].ToInt() > 0)
{
tableName = tableName.Substring(0, p);
tableName = tableName[..p];
}
}
if (!list.Contains(tableName)) list.Add(tableName);
Expand Down
25 changes: 25 additions & 0 deletions XUnitTest.XCode/DataAccessLayer/DALTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,29 @@ public void NullableType()
var type3 = Nullable.GetUnderlyingType(type2);
Assert.Null(type3);
}

[Theory]
[InlineData("select * from user", "user")]
[InlineData("select * from 'user' where id=123", "user")]
[InlineData("select * from `user` where id=123", "user")]
[InlineData("select * from \"user\" where id=123", "user")]
[InlineData("select * from [user] where id=123", "user")]
[InlineData("select * from member.user", "user")]
[InlineData("select * from `member`.'user' where id=123", "user")]
[InlineData("select * from \"member\".`user` where id=123", "user")]
[InlineData("select * from [member].\"user\" where id=123", "user")]
[InlineData("select * from 'member'.[user] where id=123", "user")]
[InlineData("insert into \"member\".`user`(xxx)", "user")]
[InlineData("update \"member\".`user` set xxx", "user")]
[InlineData("select * from 'member'.[user] left join data.[role] on user.roleid=role.id where id=123", "user,role")]
[InlineData("truncate table \"member\".`user`", "user")]
public void GetTables(String sql, String tableName)
{
var ts = DAL.GetTables(sql, false);
Assert.NotNull(ts);

var tables = tableName.Split(",");
Assert.Equal(tables.Length, ts.Length);
Assert.Equal(tableName, ts.Join(","));
}
}

0 comments on commit 7f59106

Please sign in to comment.