Skip to content

Commit dd757fa

Browse files
committed
added comments and minor refactoring
1 parent 66f6711 commit dd757fa

File tree

3 files changed

+133
-94
lines changed

3 files changed

+133
-94
lines changed

src/NHibernate/Dialect/BitwiseFunctionOperation.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77

88
namespace NHibernate.Dialect
99
{
10+
/// <summary>
11+
/// Treats bitwise operations as SQL function calls.
12+
/// </summary>
1013
[Serializable]
1114
public class BitwiseFunctionOperation : ISQLFunction
1215
{
1316
private readonly string _functionName;
1417
private SqlStringBuilder _sqlBuffer;
1518
private Queue _args;
1619

20+
/// <summary>
21+
/// Creates an instance of this class using the provided function name
22+
/// </summary>
23+
/// <param name="functionName">
24+
/// The bitwise function name as defined by the SQL-Dialect
25+
/// </param>
1726
public BitwiseFunctionOperation(string functionName)
1827
{
1928
_functionName = functionName;
@@ -40,12 +49,12 @@ public SqlString Render(IList args, ISessionFactoryImplementor factory)
4049
{
4150
Prepare(args);
4251

43-
Function();
52+
AddFunctionName();
4453
OpenParens();
45-
Arguments();
54+
AddArguments();
4655
CloseParens();
4756

48-
return _sqlBuffer.ToSqlString();
57+
return SqlResult();
4958
}
5059

5160
#endregion
@@ -66,7 +75,7 @@ private static bool IsParens(string candidate)
6675
return candidate == "(" || candidate == ")";
6776
}
6877

69-
private void Function()
78+
private void AddFunctionName()
7079
{
7180
_sqlBuffer.Add(_functionName);
7281
}
@@ -76,7 +85,7 @@ private void OpenParens()
7685
_sqlBuffer.Add("(");
7786
}
7887

79-
private void Arguments()
88+
private void AddArguments()
8089
{
8190
while (_args.Count > 0)
8291
{
@@ -94,5 +103,10 @@ private void CloseParens()
94103
{
95104
_sqlBuffer.Add(")");
96105
}
106+
107+
private SqlString SqlResult()
108+
{
109+
return _sqlBuffer.ToSqlString();
110+
}
97111
}
98112
}
Lines changed: 109 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,109 @@
1-
using System;
2-
using System.Collections;
3-
using NHibernate.Dialect.Function;
4-
using NHibernate.Engine;
5-
using NHibernate.SqlCommand;
6-
using NHibernate.Type;
7-
8-
namespace NHibernate.Dialect
9-
{
10-
[Serializable]
11-
public class NativeBitwiseOperation : ISQLFunction
12-
{
13-
private readonly string _sqlToken;
14-
private Queue _args;
15-
private SqlStringBuilder _sqlBuffer;
16-
17-
public NativeBitwiseOperation(string sqlToken)
18-
{
19-
_sqlToken = sqlToken;
20-
}
21-
22-
#region ISQLFunction Members
23-
24-
public IType ReturnType(IType columnType, IMapping mapping)
25-
{
26-
return NHibernateUtil.Int64;
27-
}
28-
29-
public bool HasArguments
30-
{
31-
get { return true; }
32-
}
33-
34-
public bool HasParenthesesIfNoArguments
35-
{
36-
get { return false; }
37-
}
38-
39-
public SqlString Render(IList args, ISessionFactoryImplementor factory)
40-
{
41-
Prepare(args);
42-
if (_sqlToken != "~")
43-
AddFirstArgument();
44-
AddToken();
45-
AddRestOfArguments();
46-
47-
return _sqlBuffer.ToSqlString();
48-
}
49-
50-
#endregion
51-
52-
private void Prepare(IList args)
53-
{
54-
_sqlBuffer = new SqlStringBuilder();
55-
_args = new Queue(args);
56-
}
57-
58-
private void AddFirstArgument()
59-
{
60-
AddToBuffer(_args.Dequeue());
61-
}
62-
63-
private void AddToken()
64-
{
65-
AddToBuffer(string.Format(" {0} ", _sqlToken));
66-
}
67-
68-
private void AddRestOfArguments()
69-
{
70-
while (_args.Count > 0)
71-
{
72-
AddToBuffer(_args.Dequeue());
73-
}
74-
}
75-
76-
private void AddToBuffer(object arg)
77-
{
78-
if (arg is Parameter || arg is SqlString)
79-
_sqlBuffer.AddObject(arg);
80-
else
81-
_sqlBuffer.Add(arg.ToString());
82-
}
83-
}
84-
}
1+
using System;
2+
using System.Collections;
3+
using NHibernate.Dialect.Function;
4+
using NHibernate.Engine;
5+
using NHibernate.SqlCommand;
6+
using NHibernate.Type;
7+
8+
namespace NHibernate.Dialect
9+
{
10+
/// <summary>
11+
/// Treats bitwise operations as native operations.
12+
/// </summary>
13+
/// </remarks>
14+
[Serializable]
15+
public class BitwiseNativeOperation : ISQLFunction
16+
{
17+
private readonly string _sqlOpToken;
18+
private readonly bool _isNot;
19+
private Queue _args;
20+
private SqlStringBuilder _sqlBuffer;
21+
22+
/// <summary>
23+
/// creates an instance using the giving token
24+
/// </summary>
25+
/// <param name="sqlOpToken">
26+
/// The operation token
27+
/// </param>
28+
/// <remarks>
29+
/// Use this constructor only if the token DOES NOT represent a NOT-Operation
30+
/// </remarks>
31+
public BitwiseNativeOperation(string sqlOpToken)
32+
: this(sqlOpToken, false)
33+
{
34+
}
35+
36+
/// <summary>
37+
/// creates an instance using the giving token and the flag indicating a NOT-Operation
38+
/// </summary>
39+
/// <param name="sqlOpToken"></param>
40+
/// <param name="isNot"></param>
41+
public BitwiseNativeOperation(string sqlOpToken, bool isNot)
42+
{
43+
_sqlOpToken = sqlOpToken;
44+
_isNot = isNot;
45+
}
46+
47+
#region ISQLFunction Members
48+
49+
public IType ReturnType(IType columnType, IMapping mapping)
50+
{
51+
return NHibernateUtil.Int64;
52+
}
53+
54+
public bool HasArguments
55+
{
56+
get { return true; }
57+
}
58+
59+
public bool HasParenthesesIfNoArguments
60+
{
61+
get { return false; }
62+
}
63+
64+
public SqlString Render(IList args, ISessionFactoryImplementor factory)
65+
{
66+
Prepare(args);
67+
if (_isNot == false)
68+
AddFirstArgument();
69+
AddToken();
70+
AddRestOfArguments();
71+
72+
return _sqlBuffer.ToSqlString();
73+
}
74+
75+
#endregion
76+
77+
private void Prepare(IList args)
78+
{
79+
_sqlBuffer = new SqlStringBuilder();
80+
_args = new Queue(args);
81+
}
82+
83+
private void AddFirstArgument()
84+
{
85+
AddToBuffer(_args.Dequeue());
86+
}
87+
88+
private void AddToken()
89+
{
90+
AddToBuffer(string.Format(" {0} ", _sqlOpToken));
91+
}
92+
93+
private void AddRestOfArguments()
94+
{
95+
while (_args.Count > 0)
96+
{
97+
AddToBuffer(_args.Dequeue());
98+
}
99+
}
100+
101+
private void AddToBuffer(object arg)
102+
{
103+
if (arg is Parameter || arg is SqlString)
104+
_sqlBuffer.AddObject(arg);
105+
else
106+
_sqlBuffer.Add(arg.ToString());
107+
}
108+
}
109+
}

src/NHibernate/Dialect/Dialect.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ protected Dialect()
113113
RegisterFunction("month", new SQLFunctionTemplate(NHibernateUtil.Int32, "extract(month from ?1)"));
114114
RegisterFunction("year", new SQLFunctionTemplate(NHibernateUtil.Int32, "extract(year from ?1)"));
115115

116-
//RegisterFunction("band", new SQLFunctionTemplate(NHibernateUtil.Int64, "?1 & ?2"));
117-
RegisterFunction("band", new NativeBitwiseOpetration("&"));
118-
RegisterFunction("bor", new NativeBitwiseOpetration("|"));
119-
RegisterFunction("bxor", new NativeBitwiseOpetration("^"));
120-
RegisterFunction("bnot", new NativeBitwiseOpetration("~"));
116+
// Bitwise operations
117+
RegisterFunction("band", new BitwiseNativeOperation("&"));
118+
RegisterFunction("bor", new BitwiseNativeOperation("|"));
119+
RegisterFunction("bxor", new BitwiseNativeOperation("^"));
120+
RegisterFunction("bnot", new BitwiseNativeOperation("~", true));
121121

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

0 commit comments

Comments
 (0)