Skip to content

Commit d96a282

Browse files
Refactor further bitwise functions
1 parent a7a9fb1 commit d96a282

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

src/NHibernate/Dialect/Function/BitwiseFunctionOperation.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Linq;
34
using NHibernate.Engine;
45
using NHibernate.SqlCommand;
56
using NHibernate.Type;
@@ -15,10 +16,10 @@ public class BitwiseFunctionOperation : ISQLFunction
1516
private readonly string _functionName;
1617

1718
/// <summary>
18-
/// Creates an instance of this class using the provided function name
19+
/// Creates an instance of this class using the provided function name.
1920
/// </summary>
2021
/// <param name="functionName">
21-
/// The bitwise function name as defined by the SQL-Dialect
22+
/// The bitwise function name as defined by the SQL-Dialect.
2223
/// </param>
2324
public BitwiseFunctionOperation(string functionName)
2425
{
@@ -27,45 +28,41 @@ public BitwiseFunctionOperation(string functionName)
2728

2829
#region ISQLFunction Members
2930

31+
/// <inheritdoc />
3032
public IType ReturnType(IType columnType, IMapping mapping)
3133
{
3234
return NHibernateUtil.Int64;
3335
}
3436

35-
public bool HasArguments
36-
{
37-
get { return true; }
38-
}
37+
/// <inheritdoc />
38+
public bool HasArguments => true;
3939

40-
public bool HasParenthesesIfNoArguments
41-
{
42-
get { return true; }
43-
}
40+
/// <inheritdoc />
41+
public bool HasParenthesesIfNoArguments => true;
4442

43+
/// <inheritdoc />
4544
public SqlString Render(IList args, ISessionFactoryImplementor factory)
4645
{
47-
var filteredArgs = new Queue();
48-
foreach (var arg in args)
49-
{
50-
if (!IsParens(arg.ToString()))
51-
filteredArgs.Enqueue(arg);
52-
}
53-
5446
var sqlBuffer = new SqlStringBuilder();
5547

5648
sqlBuffer.Add(_functionName);
5749
sqlBuffer.Add("(");
58-
while (filteredArgs.Count > 0)
50+
var i = 0;
51+
foreach (var arg in args)
5952
{
60-
var arg = filteredArgs.Dequeue();
53+
i++;
54+
// The actual second argument may be surrounded by parentesis as additional arguments.
55+
// They have to be ignored, otherwise it would emit "functionName(firstArg, (, secondArg, ))"
56+
if (IsParens(arg.ToString()))
57+
continue;
6158
if (arg is Parameter || arg is SqlString)
6259
sqlBuffer.AddObject(arg);
6360
else
6461
sqlBuffer.Add(arg.ToString());
65-
if (filteredArgs.Count > 0)
66-
sqlBuffer.Add(", ");
62+
sqlBuffer.Add(", ");
6763
}
6864

65+
sqlBuffer.RemoveAt(sqlBuffer.Count - 1);
6966
sqlBuffer.Add(")");
7067

7168
return sqlBuffer.ToSqlString();

src/NHibernate/Dialect/Function/BitwiseNativeOperation.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,70 +13,70 @@ namespace NHibernate.Dialect.Function
1313
public class BitwiseNativeOperation : ISQLFunction
1414
{
1515
private readonly string _sqlOpToken;
16-
private readonly bool _isNot;
16+
private readonly bool _isUnary;
1717

1818
/// <summary>
19-
/// creates an instance using the giving token
19+
/// Creates an instance using the giving token.
2020
/// </summary>
2121
/// <param name="sqlOpToken">
22-
/// The operation token
22+
/// The operation token.
2323
/// </param>
2424
/// <remarks>
25-
/// Use this constructor only if the token DOES NOT represent a NOT-Operation
25+
/// Use this constructor only if the token DOES NOT represent an unary operator.
2626
/// </remarks>
2727
public BitwiseNativeOperation(string sqlOpToken)
2828
: this(sqlOpToken, false)
2929
{
3030
}
3131

3232
/// <summary>
33-
/// creates an instance using the giving token and the flag indicating a NOT-Operation
33+
/// Creates an instance using the giving token and the flag indicating if it is an unary operator.
3434
/// </summary>
35-
/// <param name="sqlOpToken"></param>
36-
/// <param name="isNot"></param>
37-
public BitwiseNativeOperation(string sqlOpToken, bool isNot)
35+
/// <param name="sqlOpToken">The operation token.</param>
36+
/// <param name="isUnary">Whether the operation is unary or not.</param>
37+
public BitwiseNativeOperation(string sqlOpToken, bool isUnary)
3838
{
3939
_sqlOpToken = sqlOpToken;
40-
_isNot = isNot;
40+
_isUnary = isUnary;
4141
}
4242

4343
#region ISQLFunction Members
4444

45+
/// <inheritdoc />
4546
public IType ReturnType(IType columnType, IMapping mapping)
4647
{
4748
return NHibernateUtil.Int64;
4849
}
4950

50-
public bool HasArguments
51-
{
52-
get { return true; }
53-
}
51+
/// <inheritdoc />
52+
public bool HasArguments => true;
5453

55-
public bool HasParenthesesIfNoArguments
56-
{
57-
get { return false; }
58-
}
54+
/// <inheritdoc />
55+
public bool HasParenthesesIfNoArguments => false;
5956

57+
/// <inheritdoc />
6058
public SqlString Render(IList args, ISessionFactoryImplementor factory)
6159
{
60+
if (args.Count == 0)
61+
throw new ArgumentException("Function argument list cannot be empty", nameof(args));
62+
6263
var sqlBuffer = new SqlStringBuilder();
63-
var arguments = new Queue(args);
6464

65-
if (_isNot == false)
66-
AddToBuffer(arguments.Dequeue(), sqlBuffer);
65+
if (!_isUnary)
66+
AddToBuffer(args[0], sqlBuffer);
6767

68-
AddToBuffer(string.Format(" {0} ", _sqlOpToken), sqlBuffer);
69-
while (arguments.Count > 0)
68+
sqlBuffer.Add(" ").Add(_sqlOpToken).Add(" ");
69+
for (var i = _isUnary ? 0 : 1; i < args.Count; i++)
7070
{
71-
AddToBuffer(arguments.Dequeue(), sqlBuffer);
71+
AddToBuffer(args[i], sqlBuffer);
7272
}
7373

7474
return sqlBuffer.ToSqlString();
7575
}
7676

7777
#endregion
7878

79-
private void AddToBuffer(object arg, SqlStringBuilder buffer)
79+
private static void AddToBuffer(object arg, SqlStringBuilder buffer)
8080
{
8181
if (arg is Parameter || arg is SqlString)
8282
buffer.AddObject(arg);

0 commit comments

Comments
 (0)