Skip to content

Commit 852bbca

Browse files
committed
Simplify parens handling
1 parent bfb96a2 commit 852bbca

File tree

4 files changed

+30
-39
lines changed

4 files changed

+30
-39
lines changed

src/NHibernate.Test/Async/CompositeId/ClassWithCompositeIdFixture.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,10 @@ public async Task QueryOverInClauseAsync()
411411

412412
using (var s = OpenSession())
413413
{
414-
var results = await (s.QueryOver<ClassWithCompositeId>().WhereRestrictionOn(p => p.Id).IsIn(new[] {id, secondId}).ListAsync());
415-
Assert.That(results.Count, Is.EqualTo(2));
414+
var results1 = await (s.QueryOver<ClassWithCompositeId>().WhereRestrictionOn(p => p.Id).IsIn(new[] {id, secondId}).ListAsync());
415+
var results2 = await (s.QueryOver<ClassWithCompositeId>().WhereRestrictionOn(p => p.Id).IsIn(new[] {id}).ListAsync());
416+
Assert.That(results1.Count, Is.EqualTo(2));
417+
Assert.That(results2.Count, Is.EqualTo(1));
416418
}
417419
}
418420
}

src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,10 @@ public void QueryOverInClause()
400400

401401
using (var s = OpenSession())
402402
{
403-
var results = s.QueryOver<ClassWithCompositeId>().WhereRestrictionOn(p => p.Id).IsIn(new[] {id, secondId}).List();
404-
Assert.That(results.Count, Is.EqualTo(2));
403+
var results1 = s.QueryOver<ClassWithCompositeId>().WhereRestrictionOn(p => p.Id).IsIn(new[] {id, secondId}).List();
404+
var results2 = s.QueryOver<ClassWithCompositeId>().WhereRestrictionOn(p => p.Id).IsIn(new[] {id}).List();
405+
Assert.That(results1.Count, Is.EqualTo(2));
406+
Assert.That(results2.Count, Is.EqualTo(1));
405407
}
406408
}
407409
}

src/NHibernate/Criterion/InExpression.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ private SqlString GetSqlString(ICriteriaQuery criteriaQuery, SqlString[] columns
7878
{
7979
if (columns.Length <= 1 || criteriaQuery.Factory.Dialect.SupportsRowValueConstructorSyntaxInInList)
8080
{
81-
var parens = columns.Length > 1 ? new[] {new SqlString("("), new SqlString(")"),} : null;
81+
var wrapInParens = columns.Length > 1;
8282
SqlString comaSeparator = new SqlString(", ");
83-
var singleValueParam = SqlStringHelper.Repeat(new SqlString(bogusParam), columns.Length, comaSeparator, parens);
83+
var singleValueParam = SqlStringHelper.Repeat(new SqlString(bogusParam), columns.Length, comaSeparator, wrapInParens);
8484

85-
var parameters = SqlStringHelper.Repeat(singleValueParam, Values.Length, comaSeparator, null);
85+
var parameters = SqlStringHelper.Repeat(singleValueParam, Values.Length, comaSeparator, wrapInParens: false);
8686

8787
//single column: col1 in (?, ?)
8888
//multi column: (col1, col2) in ((?, ?), (?, ?))
8989
return new SqlString(
90-
parens?[0] ?? SqlString.Empty,
90+
wrapInParens ? StringHelper.OpenParen : string.Empty,
9191
SqlStringHelper.Join(comaSeparator, columns),
92-
parens?[1] ?? SqlString.Empty,
92+
wrapInParens ? StringHelper.ClosedParen : string.Empty,
9393
" in (",
9494
parameters,
9595
")");
@@ -100,7 +100,7 @@ private SqlString GetSqlString(ICriteriaQuery criteriaQuery, SqlString[] columns
100100
" ( ",
101101
SqlStringHelper.Join(new SqlString(" = ", bogusParam, " and "), columns),
102102
new SqlString("= ", bogusParam, " ) "));
103-
cols = SqlStringHelper.Repeat(cols, Values.Length, "or ", new[] {" ( ", " ) "});
103+
cols = SqlStringHelper.Repeat(cols, Values.Length, new SqlString(" or "), wrapInParens: Values.Length > 1);
104104
return cols;
105105
}
106106

src/NHibernate/SqlCommand/SqlStringHelper.cs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static bool IsNotEmpty(SqlString str)
5656
return !IsEmpty(str);
5757
}
5858

59+
5960
public static bool IsEmpty(SqlString str)
6061
{
6162
return str == null || str.Count == 0;
@@ -90,49 +91,35 @@ internal static SqlString ParametersList(List<Parameter> parameters)
9091
return builder.ToSqlString();
9192
}
9293

93-
internal static SqlString Repeat(SqlString placeholder, int count, string separator, string[] wrapResult)
94+
internal static SqlString Repeat(SqlString placeholder, int count, SqlString separator, bool wrapInParens)
9495
{
95-
return Repeat(
96-
placeholder,
97-
count,
98-
new SqlString(separator),
99-
wrapResult == null
100-
? null
101-
: new[]
102-
{
103-
new SqlString(wrapResult[0]),
104-
new SqlString(wrapResult[1]),
105-
});
106-
}
96+
if (count == 0)
97+
return SqlString.Empty;
10798

108-
internal static SqlString Repeat(SqlString placeholder, int count, SqlString separator, SqlString[] wrapResult)
109-
{
110-
if (wrapResult == null)
111-
{
112-
if (count == 0)
113-
return SqlString.Empty;
114-
if (count == 1)
115-
return placeholder;
116-
}
99+
if (count == 1)
100+
return wrapInParens
101+
? new SqlString("(", placeholder, ")")
102+
: placeholder;
117103

118-
var builder = new SqlStringBuilder(count * 2 + 1);
119-
if (wrapResult != null)
104+
var builder = new SqlStringBuilder((placeholder.Count + separator.Count) * count + 1);
105+
106+
if (wrapInParens)
120107
{
121-
builder.Add(wrapResult[0]);
108+
builder.Add("(");
122109
}
123110

124-
if (count > 0)
125-
builder.Add(placeholder);
111+
builder.Add(placeholder);
126112

127113
for (int i = 1; i < count; i++)
128114
{
129115
builder.Add(separator).Add(placeholder);
130116
}
131117

132-
if (wrapResult != null)
118+
if (wrapInParens)
133119
{
134-
builder.Add(wrapResult[1]);
120+
builder.Add(")");
135121
}
122+
136123
return builder.ToSqlString();
137124
}
138125
}

0 commit comments

Comments
 (0)