Skip to content

Commit

Permalink
Merge pull request #4714 from berkarslan-xo/bugfix/Issue-4673
Browse files Browse the repository at this point in the history
Issue-4673 - SQL Console raises error for variable declarations with "@" character
  • Loading branch information
mitchelsellers authored Jun 22, 2021
2 parents db80b65 + 444527d commit 8cfd6a7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
22 changes: 22 additions & 0 deletions DNN Platform/Library/Data/PetaPoco/PetaPocoExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static IDataReader ExecuteReader(this Database database, string sql, para
try
{
database.OpenSharedConnection();
sql = NormalizeSql(sql, args);

using (IDbCommand command = database.CreateCommand(database.Connection, sql, args))
{
Expand All @@ -36,5 +37,26 @@ public static IDataReader ExecuteReader(this Database database, string sql, para

return reader;
}

/// <summary>
/// Escapes the "@" character if the arguments are empty and the sql string contains any "@" characters.
/// </summary>
/// <param name="sql">Sql string to normalize.</param>
/// <param name="args">Sql command arguments.</param>
/// <returns>Normalized sql string.</returns>
private static string NormalizeSql(string sql, object[] args)
{
const string ReplaceFrom = "@";
const string ReplaceTo = "@@";

if (
(args != null && args.Length != 0) ||
string.IsNullOrWhiteSpace(sql))
{
return sql;
}

return sql.Replace(ReplaceFrom, ReplaceTo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<Compile Include="DataAssert.cs" />
<Compile Include="DatabaseConnectionProviderTests.cs" />
<Compile Include="Fakes\FakeDbConnectionProvider.cs" />
<Compile Include="PetaPocoExtTests.cs" />
<Compile Include="RepositoryBaseTests.cs" />
<Compile Include="DataContextTests.cs" />
<Compile Include="DataProviderTests.cs" />
Expand Down
53 changes: 53 additions & 0 deletions DNN Platform/Tests/DotNetNuke.Tests.Data/PetaPocoExtTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace DotNetNuke.Tests.Data
{
using System.Reflection;
using DotNetNuke.Data.PetaPoco;
using NUnit.Framework;

[TestFixture]
public class PetaPocoExtTests
{
[TestCase(null, null, null)]
[TestCase(null, new object[0], null)]
[TestCase(null, new object[] { "" }, null)]
[TestCase("", null, "")]
[TestCase("", new object[0], "")]
[TestCase("", new object[] { "" }, "")]
[TestCase(
"declare @test varchar(20) = 'test@test.com'\nselect @test",
null,
"declare @@test varchar(20) = 'test@@test.com'\nselect @@test")]
[TestCase(
"declare @test varchar(20) = 'test@test.com'\nselect @test",
new object[0],
"declare @@test varchar(20) = 'test@@test.com'\nselect @@test")]
[TestCase(
"declare @test varchar(20) = 'test@test.com'\nselect @test",
new object[] { "" },
"declare @test varchar(20) = 'test@test.com'\nselect @test")]
public void NormalizeSql_WithAtCharAndEmptyArguments_EscapesAtCharSuccessfully(
string sql,
object[] args,
string expected)
{
// Arrange
const string NormalizeSqlMethodName = "NormalizeSql";
var normalizeSqlMethod = typeof(PetaPocoExt)
.GetMethod(
NormalizeSqlMethodName,
BindingFlags.Static | BindingFlags.NonPublic);

// Act
var result = normalizeSqlMethod.Invoke(
null,
new object[]
{
sql,
args
});

// Assert
Assert.AreEqual(expected, result);
}
}
}

0 comments on commit 8cfd6a7

Please # to comment.