Synchronization code

This commit is contained in:
sunkaixuan 2023-05-23 19:47:51 +08:00
parent aa57f3f278
commit 00e9864bed
2 changed files with 110 additions and 75 deletions

View File

@ -14,44 +14,44 @@ namespace SqlSugar
public partial class DmProvider : AdoProvider public partial class DmProvider : AdoProvider
{ {
public DmProvider() { public DmProvider() {
this.FormatSql = sql => //this.FormatSql = sql =>
{ //{
sql = sql.Replace("+@", "+:"); // sql = sql.Replace("+@", "+:");
if (sql.HasValue() && sql.Contains("@")) // if (sql.HasValue() && sql.Contains("@"))
{ // {
var exceptionalCaseInfo = Regex.Matches(sql, @"\'[^\=]*?\@.*?\'|[\.,\w]+\@[\.,\w]+ | [\.,\w]+\@[\.,\w]+|[\.,\w]+\@[\.,\w]+ |\d+\@\d|\@\@"); // var exceptionalCaseInfo = Regex.Matches(sql, @"\'[^\=]*?\@.*?\'|[\.,\w]+\@[\.,\w]+ | [\.,\w]+\@[\.,\w]+|[\.,\w]+\@[\.,\w]+ |\d+\@\d|\@\@");
if (exceptionalCaseInfo != null) // if (exceptionalCaseInfo != null)
{ // {
foreach (var item in exceptionalCaseInfo.Cast<Match>()) // foreach (var item in exceptionalCaseInfo.Cast<Match>())
{ // {
if (item.Value != null && item.Value.IndexOf(",") == 1 && Regex.IsMatch(item.Value, @"^ \,\@\w+$")) // if (item.Value != null && item.Value.IndexOf(",") == 1 && Regex.IsMatch(item.Value, @"^ \,\@\w+$"))
{ // {
continue; // continue;
} // }
else if (item.Value != null && Regex.IsMatch(item.Value.Trim(), @"^\w+\,\@\w+\,$")) // else if (item.Value != null && Regex.IsMatch(item.Value.Trim(), @"^\w+\,\@\w+\,$"))
{ // {
continue; // continue;
} // }
else if (item.Value != null && item.Value.ObjToString().Contains("||") && Regex.IsMatch(item.Value.Replace(" ", "").Trim(), @"\|\|@\w+\|\|")) // else if (item.Value != null && item.Value.ObjToString().Contains("||") && Regex.IsMatch(item.Value.Replace(" ", "").Trim(), @"\|\|@\w+\|\|"))
{ // {
continue; // continue;
} // }
else if (item.Value != null && Regex.IsMatch(item.Value.Replace(" ", "").Trim(), @"\(\@\w+\,")) // else if (item.Value != null && Regex.IsMatch(item.Value.Replace(" ", "").Trim(), @"\(\@\w+\,"))
{ // {
continue; // continue;
} // }
else if (item.Value != null && item.Value.Contains("=") && Regex.IsMatch(item.Value, @"\w+ \@\w+[ ]{0,1}\=[ ]{0,1}\'")) // else if (item.Value != null && item.Value.Contains("=") && Regex.IsMatch(item.Value, @"\w+ \@\w+[ ]{0,1}\=[ ]{0,1}\'"))
{ // {
continue; // continue;
} // }
sql = sql.Replace(item.Value, item.Value.Replace("@", UtilConstants.ReplaceKey)); // sql = sql.Replace(item.Value, item.Value.Replace("@", UtilConstants.ReplaceKey));
} // }
} // }
sql = sql.Replace("@", ":"); // sql = sql.Replace("@", ":");
sql = sql.Replace(UtilConstants.ReplaceKey, "@"); // sql = sql.Replace(UtilConstants.ReplaceKey, "@");
} // }
return sql; // return sql;
}; //};
} }
public override string SqlParameterKeyWord public override string SqlParameterKeyWord
{ {
@ -180,6 +180,7 @@ namespace SqlSugar
private static string[] KeyWord =new string []{"@order", ":order", "@user", "@level", ":user", ":level",":type","@type"}; private static string[] KeyWord =new string []{"@order", ":order", "@user", "@level", ":user", ":level",":type","@type"};
private static string ReplaceKeyWordParameterName(string sql, SugarParameter[] parameters) private static string ReplaceKeyWordParameterName(string sql, SugarParameter[] parameters)
{ {
sql = ReplaceKeyWordWithAd(sql, parameters);
if (parameters.HasValue() && parameters.Count(it => it.ParameterName.ToLower().IsIn(KeyWord))>0) if (parameters.HasValue() && parameters.Count(it => it.ParameterName.ToLower().IsIn(KeyWord))>0)
{ {
int i = 0; int i = 0;
@ -197,7 +198,22 @@ namespace SqlSugar
return sql; return sql;
} }
private static string ReplaceKeyWordWithAd(string sql, SugarParameter[] parameters)
{
if (parameters != null && sql != null && sql.Contains("@"))
{
foreach (var item in parameters.OrderByDescending(it => it.ParameterName.Length))
{
if (item.ParameterName.StartsWith("@"))
{
item.ParameterName = ":" + item.ParameterName.TrimStart('@');
}
sql = Regex.Replace(sql, "@" + item.ParameterName.TrimStart(':'), item.ParameterName, RegexOptions.IgnoreCase);
}
}
return sql;
}
} }
} }

View File

@ -13,42 +13,42 @@ namespace SqlSugar
{ {
public OracleProvider() public OracleProvider()
{ {
this.FormatSql = sql => //this.FormatSql = sql =>
{ //{
sql = sql.Replace("+@", "+:"); // sql = sql.Replace("+@", "+:");
if (sql.HasValue()&&sql.Contains("@")) { // if (sql.HasValue()&&sql.Contains("@")) {
var exceptionalCaseInfo = Regex.Matches(sql, @"\'[^\=]*?\@.*?\'|[\.,\w]+\@[\.,\w]+ | [\.,\w]+\@[\.,\w]+|[\.,\w]+\@[\.,\w]+ |\d+\@\d|\@\@|\w{1,25}\.""\w{1,25}""\@\w{1,25}"); // var exceptionalCaseInfo = Regex.Matches(sql, @"\'[^\=]*?\@.*?\'|[\.,\w]+\@[\.,\w]+ | [\.,\w]+\@[\.,\w]+|[\.,\w]+\@[\.,\w]+ |\d+\@\d|\@\@|\w{1,25}\.""\w{1,25}""\@\w{1,25}");
if (exceptionalCaseInfo != null) { // if (exceptionalCaseInfo != null) {
foreach (var item in exceptionalCaseInfo.Cast<Match>()) // foreach (var item in exceptionalCaseInfo.Cast<Match>())
{ // {
if (item.Value != null && item.Value.IndexOf(",") == 1&&Regex.IsMatch(item.Value, @"^ \,\@\w+$")) // if (item.Value != null && item.Value.IndexOf(",") == 1&&Regex.IsMatch(item.Value, @"^ \,\@\w+$"))
{ // {
continue; // continue;
} // }
else if (item.Value != null &&Regex.IsMatch(item.Value.Trim(), @"^\w+\,\@\w+\,$")) // else if (item.Value != null &&Regex.IsMatch(item.Value.Trim(), @"^\w+\,\@\w+\,$"))
{ // {
continue; // continue;
} // }
else if (item.Value != null && item.Value.ObjToString().Contains("||") && Regex.IsMatch(item.Value.Replace(" ","").Trim(), @"\|\|@\w+\|\|")) // else if (item.Value != null && item.Value.ObjToString().Contains("||") && Regex.IsMatch(item.Value.Replace(" ","").Trim(), @"\|\|@\w+\|\|"))
{ // {
continue; // continue;
} // }
else if (item.Value != null&& Regex.IsMatch(item.Value.Replace(" ", "").Trim(), @"\(\@\w+\,")) // else if (item.Value != null&& Regex.IsMatch(item.Value.Replace(" ", "").Trim(), @"\(\@\w+\,"))
{ // {
continue; // continue;
} // }
else if (item.Value != null &&item.Value.Contains("=")&& Regex.IsMatch(item.Value, @"\w+ \@\w+[ ]{0,1}\=[ ]{0,1}\'")) // else if (item.Value != null &&item.Value.Contains("=")&& Regex.IsMatch(item.Value, @"\w+ \@\w+[ ]{0,1}\=[ ]{0,1}\'"))
{ // {
continue; // continue;
} // }
sql = sql.Replace(item.Value, item.Value.Replace("@", UtilConstants.ReplaceKey)); // sql = sql.Replace(item.Value, item.Value.Replace("@", UtilConstants.ReplaceKey));
} // }
} // }
sql = sql .Replace("@",":"); // sql = sql .Replace("@",":");
sql = sql.Replace(UtilConstants.ReplaceKey, "@"); // sql = sql.Replace(UtilConstants.ReplaceKey, "@");
} // }
return sql; // return sql;
}; //};
} }
public override string SqlParameterKeyWord public override string SqlParameterKeyWord
{ {
@ -124,6 +124,7 @@ namespace SqlSugar
private static string[] KeyWord = new string[] { "@order", ":order", "@user", "@level", ":user", ":level", ":type", "@type" }; private static string[] KeyWord = new string[] { "@order", ":order", "@user", "@level", ":user", ":level", ":type", "@type" };
private static string ReplaceKeyWordParameterName(string sql, SugarParameter[] parameters) private static string ReplaceKeyWordParameterName(string sql, SugarParameter[] parameters)
{ {
sql = ReplaceKeyWordWithAd(sql, parameters);
if (parameters.HasValue()) if (parameters.HasValue())
{ {
foreach (var Parameter in parameters) foreach (var Parameter in parameters)
@ -146,6 +147,24 @@ namespace SqlSugar
return sql; return sql;
} }
private static string ReplaceKeyWordWithAd(string sql, SugarParameter[] parameters)
{
if (parameters != null && sql != null&&sql.Contains("@"))
{
foreach (var item in parameters.OrderByDescending(it => it.ParameterName.Length))
{
if (item.ParameterName.StartsWith("@"))
{
item.ParameterName = ":"+item.ParameterName.TrimStart('@');
}
sql = Regex.Replace(sql,"@" + item.ParameterName.TrimStart(':'),item.ParameterName,RegexOptions.IgnoreCase);
}
}
return sql;
}
public override Action<SqlSugarException> ErrorEvent => it => public override Action<SqlSugarException> ErrorEvent => it =>
{ {
if (base.ErrorEvent != null) if (base.ErrorEvent != null)