Skip to content

Commit a7a9fb1

Browse files
Move bitwise functions to Function folder
1 parent fd331ad commit a7a9fb1

File tree

8 files changed

+192
-157
lines changed

8 files changed

+192
-157
lines changed
Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,16 @@
11
using System;
2-
using System.Collections;
3-
using NHibernate.Dialect.Function;
4-
using NHibernate.Engine;
5-
using NHibernate.SqlCommand;
6-
using NHibernate.Type;
72

83
namespace NHibernate.Dialect
94
{
10-
/// <summary>
11-
/// Treats bitwise operations as SQL function calls.
12-
/// </summary>
5+
/// <inheritdoc />
136
[Serializable]
14-
public class BitwiseFunctionOperation : ISQLFunction
7+
// Since 5.2
8+
[Obsolete("Use NHibernate.Dialect.Function.BitwiseFunctionOperation instead")]
9+
public class BitwiseFunctionOperation : Function.BitwiseFunctionOperation
1510
{
16-
private readonly string _functionName;
17-
18-
/// <summary>
19-
/// Creates an instance of this class using the provided function name
20-
/// </summary>
21-
/// <param name="functionName">
22-
/// The bitwise function name as defined by the SQL-Dialect
23-
/// </param>
24-
public BitwiseFunctionOperation(string functionName)
25-
{
26-
_functionName = functionName;
27-
}
28-
29-
#region ISQLFunction Members
30-
31-
public IType ReturnType(IType columnType, IMapping mapping)
32-
{
33-
return NHibernateUtil.Int64;
34-
}
35-
36-
public bool HasArguments
37-
{
38-
get { return true; }
39-
}
40-
41-
public bool HasParenthesesIfNoArguments
42-
{
43-
get { return true; }
44-
}
45-
46-
public SqlString Render(IList args, ISessionFactoryImplementor factory)
47-
{
48-
var filteredArgs = new Queue();
49-
foreach (var arg in args)
50-
{
51-
if (!IsParens(arg.ToString()))
52-
filteredArgs.Enqueue(arg);
53-
}
54-
55-
var sqlBuffer = new SqlStringBuilder();
56-
57-
sqlBuffer.Add(_functionName);
58-
sqlBuffer.Add("(");
59-
while (filteredArgs.Count > 0)
60-
{
61-
var arg = filteredArgs.Dequeue();
62-
if (arg is Parameter || arg is SqlString)
63-
sqlBuffer.AddObject(arg);
64-
else
65-
sqlBuffer.Add(arg.ToString());
66-
if (filteredArgs.Count > 0)
67-
sqlBuffer.Add(", ");
68-
}
69-
70-
sqlBuffer.Add(")");
71-
72-
return sqlBuffer.ToSqlString();
73-
}
74-
75-
#endregion
76-
77-
private static bool IsParens(string candidate)
11+
/// <inheritdoc />
12+
public BitwiseFunctionOperation(string functionName): base(functionName)
7813
{
79-
return candidate == "(" || candidate == ")";
8014
}
8115
}
8216
}
Lines changed: 8 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,21 @@
11
using System;
2-
using System.Collections;
3-
using NHibernate.Dialect.Function;
4-
using NHibernate.Engine;
5-
using NHibernate.SqlCommand;
6-
using NHibernate.Type;
72

83
namespace NHibernate.Dialect
94
{
10-
/// <summary>
11-
/// Treats bitwise operations as native operations.
12-
/// </summary>
5+
/// <inheritdoc />
136
[Serializable]
14-
public class BitwiseNativeOperation : ISQLFunction
7+
// Since 5.2
8+
[Obsolete("Use NHibernate.Dialect.Function.BitwiseNativeOperation instead")]
9+
public class BitwiseNativeOperation : Function.BitwiseNativeOperation
1510
{
16-
private readonly string _sqlOpToken;
17-
private readonly bool _isNot;
18-
19-
/// <summary>
20-
/// creates an instance using the giving token
21-
/// </summary>
22-
/// <param name="sqlOpToken">
23-
/// The operation token
24-
/// </param>
25-
/// <remarks>
26-
/// Use this constructor only if the token DOES NOT represent a NOT-Operation
27-
/// </remarks>
28-
public BitwiseNativeOperation(string sqlOpToken)
29-
: this(sqlOpToken, false)
30-
{
31-
}
32-
33-
/// <summary>
34-
/// creates an instance using the giving token and the flag indicating a NOT-Operation
35-
/// </summary>
36-
/// <param name="sqlOpToken"></param>
37-
/// <param name="isNot"></param>
38-
public BitwiseNativeOperation(string sqlOpToken, bool isNot)
39-
{
40-
_sqlOpToken = sqlOpToken;
41-
_isNot = isNot;
42-
}
43-
44-
#region ISQLFunction Members
45-
46-
public IType ReturnType(IType columnType, IMapping mapping)
47-
{
48-
return NHibernateUtil.Int64;
49-
}
50-
51-
public bool HasArguments
11+
/// <inheritdoc />
12+
public BitwiseNativeOperation(string sqlOpToken) : base(sqlOpToken)
5213
{
53-
get { return true; }
5414
}
5515

56-
public bool HasParenthesesIfNoArguments
57-
{
58-
get { return false; }
59-
}
60-
61-
public SqlString Render(IList args, ISessionFactoryImplementor factory)
62-
{
63-
var sqlBuffer = new SqlStringBuilder();
64-
var arguments = new Queue(args);
65-
66-
if (_isNot == false)
67-
AddToBuffer(arguments.Dequeue(), sqlBuffer);
68-
69-
AddToBuffer(string.Format(" {0} ", _sqlOpToken), sqlBuffer);
70-
while (arguments.Count > 0)
71-
{
72-
AddToBuffer(arguments.Dequeue(), sqlBuffer);
73-
}
74-
75-
return sqlBuffer.ToSqlString();
76-
}
77-
78-
#endregion
79-
80-
private void AddToBuffer(object arg, SqlStringBuilder buffer)
16+
/// <inheritdoc />
17+
public BitwiseNativeOperation(string sqlOpToken, bool isNot) : base(sqlOpToken, isNot)
8118
{
82-
if (arg is Parameter || arg is SqlString)
83-
buffer.AddObject(arg);
84-
else
85-
buffer.Add(arg.ToString());
8619
}
8720
}
8821
}

src/NHibernate/Dialect/Dialect.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ protected Dialect()
117117
RegisterFunction("year", new SQLFunctionTemplate(NHibernateUtil.Int32, "extract(year from ?1)"));
118118

119119
// Bitwise operations
120-
RegisterFunction("band", new BitwiseNativeOperation("&"));
121-
RegisterFunction("bor", new BitwiseNativeOperation("|"));
122-
RegisterFunction("bxor", new BitwiseNativeOperation("^"));
123-
RegisterFunction("bnot", new BitwiseNativeOperation("~", true));
120+
RegisterFunction("band", new Function.BitwiseNativeOperation("&"));
121+
RegisterFunction("bor", new Function.BitwiseNativeOperation("|"));
122+
RegisterFunction("bxor", new Function.BitwiseNativeOperation("^"));
123+
RegisterFunction("bnot", new Function.BitwiseNativeOperation("~", true));
124124

125125
RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as char)"));
126126

src/NHibernate/Dialect/FirebirdDialect.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,10 @@ private void OverrideStandardHQLFunctions()
422422
RegisterFunction("sysdate", new CastedFunction("today", NHibernateUtil.Date));
423423
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "cast(?1 as date)"));
424424
// Bitwise operations
425-
RegisterFunction("band", new BitwiseFunctionOperation("bin_and"));
426-
RegisterFunction("bor", new BitwiseFunctionOperation("bin_or"));
427-
RegisterFunction("bxor", new BitwiseFunctionOperation("bin_xor"));
428-
RegisterFunction("bnot", new BitwiseFunctionOperation("bin_not"));
425+
RegisterFunction("band", new Function.BitwiseFunctionOperation("bin_and"));
426+
RegisterFunction("bor", new Function.BitwiseFunctionOperation("bin_or"));
427+
RegisterFunction("bxor", new Function.BitwiseFunctionOperation("bin_xor"));
428+
RegisterFunction("bnot", new Function.BitwiseFunctionOperation("bin_not"));
429429
}
430430

431431
private void RegisterFirebirdServerEmbeddedFunctions()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections;
3+
using NHibernate.Engine;
4+
using NHibernate.SqlCommand;
5+
using NHibernate.Type;
6+
7+
namespace NHibernate.Dialect.Function
8+
{
9+
/// <summary>
10+
/// Treats bitwise operations as SQL function calls.
11+
/// </summary>
12+
[Serializable]
13+
public class BitwiseFunctionOperation : ISQLFunction
14+
{
15+
private readonly string _functionName;
16+
17+
/// <summary>
18+
/// Creates an instance of this class using the provided function name
19+
/// </summary>
20+
/// <param name="functionName">
21+
/// The bitwise function name as defined by the SQL-Dialect
22+
/// </param>
23+
public BitwiseFunctionOperation(string functionName)
24+
{
25+
_functionName = functionName;
26+
}
27+
28+
#region ISQLFunction Members
29+
30+
public IType ReturnType(IType columnType, IMapping mapping)
31+
{
32+
return NHibernateUtil.Int64;
33+
}
34+
35+
public bool HasArguments
36+
{
37+
get { return true; }
38+
}
39+
40+
public bool HasParenthesesIfNoArguments
41+
{
42+
get { return true; }
43+
}
44+
45+
public SqlString Render(IList args, ISessionFactoryImplementor factory)
46+
{
47+
var filteredArgs = new Queue();
48+
foreach (var arg in args)
49+
{
50+
if (!IsParens(arg.ToString()))
51+
filteredArgs.Enqueue(arg);
52+
}
53+
54+
var sqlBuffer = new SqlStringBuilder();
55+
56+
sqlBuffer.Add(_functionName);
57+
sqlBuffer.Add("(");
58+
while (filteredArgs.Count > 0)
59+
{
60+
var arg = filteredArgs.Dequeue();
61+
if (arg is Parameter || arg is SqlString)
62+
sqlBuffer.AddObject(arg);
63+
else
64+
sqlBuffer.Add(arg.ToString());
65+
if (filteredArgs.Count > 0)
66+
sqlBuffer.Add(", ");
67+
}
68+
69+
sqlBuffer.Add(")");
70+
71+
return sqlBuffer.ToSqlString();
72+
}
73+
74+
#endregion
75+
76+
private static bool IsParens(string candidate)
77+
{
78+
return candidate == "(" || candidate == ")";
79+
}
80+
}
81+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections;
3+
using NHibernate.Engine;
4+
using NHibernate.SqlCommand;
5+
using NHibernate.Type;
6+
7+
namespace NHibernate.Dialect.Function
8+
{
9+
/// <summary>
10+
/// Treats bitwise operations as native operations.
11+
/// </summary>
12+
[Serializable]
13+
public class BitwiseNativeOperation : ISQLFunction
14+
{
15+
private readonly string _sqlOpToken;
16+
private readonly bool _isNot;
17+
18+
/// <summary>
19+
/// creates an instance using the giving token
20+
/// </summary>
21+
/// <param name="sqlOpToken">
22+
/// The operation token
23+
/// </param>
24+
/// <remarks>
25+
/// Use this constructor only if the token DOES NOT represent a NOT-Operation
26+
/// </remarks>
27+
public BitwiseNativeOperation(string sqlOpToken)
28+
: this(sqlOpToken, false)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// creates an instance using the giving token and the flag indicating a NOT-Operation
34+
/// </summary>
35+
/// <param name="sqlOpToken"></param>
36+
/// <param name="isNot"></param>
37+
public BitwiseNativeOperation(string sqlOpToken, bool isNot)
38+
{
39+
_sqlOpToken = sqlOpToken;
40+
_isNot = isNot;
41+
}
42+
43+
#region ISQLFunction Members
44+
45+
public IType ReturnType(IType columnType, IMapping mapping)
46+
{
47+
return NHibernateUtil.Int64;
48+
}
49+
50+
public bool HasArguments
51+
{
52+
get { return true; }
53+
}
54+
55+
public bool HasParenthesesIfNoArguments
56+
{
57+
get { return false; }
58+
}
59+
60+
public SqlString Render(IList args, ISessionFactoryImplementor factory)
61+
{
62+
var sqlBuffer = new SqlStringBuilder();
63+
var arguments = new Queue(args);
64+
65+
if (_isNot == false)
66+
AddToBuffer(arguments.Dequeue(), sqlBuffer);
67+
68+
AddToBuffer(string.Format(" {0} ", _sqlOpToken), sqlBuffer);
69+
while (arguments.Count > 0)
70+
{
71+
AddToBuffer(arguments.Dequeue(), sqlBuffer);
72+
}
73+
74+
return sqlBuffer.ToSqlString();
75+
}
76+
77+
#endregion
78+
79+
private void AddToBuffer(object arg, SqlStringBuilder buffer)
80+
{
81+
if (arg is Parameter || arg is SqlString)
82+
buffer.AddObject(arg);
83+
else
84+
buffer.Add(arg.ToString());
85+
}
86+
}
87+
}

src/NHibernate/Dialect/Oracle8iDialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ protected virtual void RegisterFunctions()
303303

304304
RegisterFunction("iif", new SQLFunctionTemplate(null, "case when ?1 then ?2 else ?3 end"));
305305

306-
RegisterFunction("band", new BitwiseFunctionOperation("bitand"));
306+
RegisterFunction("band", new Function.BitwiseFunctionOperation("bitand"));
307307
RegisterFunction("bor", new SQLFunctionTemplate(null, "?1 + ?2 - BITAND(?1, ?2)"));
308308
RegisterFunction("bxor", new SQLFunctionTemplate(null, "?1 + ?2 - BITAND(?1, ?2) * 2"));
309309
RegisterFunction("bnot", new SQLFunctionTemplate(null, "(-1 - ?1)"));

0 commit comments

Comments
 (0)