Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite sql where empty array parameter. #698

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -211,5 +211,59 @@ public void TestIsPlainTypeForAnoynmousType()
Assert.IsFalse(type.IsDictionaryStringObject());
Assert.IsFalse(type.GetEnumerableClassProperties().Any());
}

[TestMethod]
public void TestSqlConnectionExecuteQueryViaDynamicsWithEmptyArrayParameters()
{
if (DbSettingMapper.Get(typeof(CustomDbConnection)) == null)
{
DbSettingMapper.Add(typeof(CustomDbConnection), new CustomDbSetting(), true);
}
using (var connection = new CustomDbConnection())
{
var sql = @"
select * from someTable
where id in (@normalArray)
and id in (@emptyArray)
and id in (@nullArray)
and id in (@concat1ArrayA, @concat1ArrayB)
and id in (@concat2ArrayA, @concat2ArrayB)
and id in (@concat3ArrayA, @concat3ArrayB)";
var param = new
{
normalArray = new[] { 5, 6 },
emptyArray = Array.Empty<int>(),
nullArray = (IEnumerable<int>)null,
concat1ArrayA = new[] { 100, 101 }, concat1ArrayB = new[] { 102, 103 },
concat2ArrayA = Array.Empty<int>(), concat2ArrayB = new[] { 200, 201 },
concat3ArrayA = Array.Empty<int>(), concat3ArrayB = Array.Empty<int>()
};
var command = connection.CreateDbCommandForExecution(sql, param, skipCommandArrayParametersCheck: false);

var expectedSql = @"
select * from someTable
where id in (@normalArray0, @normalArray1)
and id in ((select @emptyArray where 1 = 0))
and id in (@nullArray)
and id in (@concat1ArrayA0, @concat1ArrayA1, @concat1ArrayB0, @concat1ArrayB1)
and id in ((select @concat2ArrayA where 1 = 0), @concat2ArrayB0, @concat2ArrayB1)
and id in ((select @concat3ArrayA where 1 = 0), (select @concat3ArrayB where 1 = 0))";
Assert.AreEqual(expectedSql, command.CommandText);
Assert.AreEqual(13, command.Parameters.Count);
Assert.AreEqual(5, command.Parameters["@normalArray0"].Value);
Assert.AreEqual(6, command.Parameters["@normalArray1"].Value);
Assert.AreEqual(DBNull.Value, command.Parameters["@emptyArray"].Value);
Assert.AreEqual(DBNull.Value, command.Parameters["@nullArray"].Value);
Assert.AreEqual(100, command.Parameters["@concat1ArrayA0"].Value);
Assert.AreEqual(101, command.Parameters["@concat1ArrayA1"].Value);
Assert.AreEqual(102, command.Parameters["@concat1ArrayB0"].Value);
Assert.AreEqual(103, command.Parameters["@concat1ArrayB1"].Value);
Assert.AreEqual(DBNull.Value, command.Parameters["@concat2ArrayA"].Value);
Assert.AreEqual(200, command.Parameters["@concat2ArrayB0"].Value);
Assert.AreEqual(201, command.Parameters["@concat2ArrayB1"].Value);
Assert.AreEqual(DBNull.Value, command.Parameters["@concat3ArrayA"].Value);
Assert.AreEqual(DBNull.Value, command.Parameters["@concat3ArrayB"].Value);
}
}
}
}
13 changes: 10 additions & 3 deletions RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs
Original file line number Diff line number Diff line change
@@ -121,10 +121,17 @@ private static void CreateParametersFromArray(this IDbCommand command,
{
var values = commandArrayParameter.Values.AsArray();

for (var i = 0; i < values.Length; i++)
if (values.Length == 0)
{
var name = string.Concat(commandArrayParameter.ParameterName, i).AsParameter(dbSetting);
command.Parameters.Add(command.CreateParameter(name, values[i], null));
command.Parameters.Add(command.CreateParameter(commandArrayParameter.ParameterName.AsParameter(dbSetting), null, null));
}
else
{
for (var i = 0; i < values.Length; i++)
{
var name = string.Concat(commandArrayParameter.ParameterName, i).AsParameter(dbSetting);
command.Parameters.Add(command.CreateParameter(name, values[i], null));
}
}
}

3 changes: 2 additions & 1 deletion RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs
Original file line number Diff line number Diff line change
@@ -3083,7 +3083,8 @@ internal static string GetRawSqlText(string commandText,
var items = values is IEnumerable<object> ? (IEnumerable<object>)values : values.WithType<object>();
if (items.Any() != true)
{
return commandText;
var parameter = parameterName.AsParameter(dbSetting);
return commandText.Replace(parameter, string.Concat("(select ", parameter, " where 1 = 0)"));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small comment, let us make all the SQL statement UPPERCASE like SELECT * FROM Table WHERE 1 = 0;.

}

// Get the variables needed