Skip to content

Commit 9d4a42a

Browse files
committed
Added Firebird Bitwise operations
1 parent 4fde32a commit 9d4a42a

File tree

5 files changed

+110
-11
lines changed

5 files changed

+110
-11
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System.Collections;
2+
using NHibernate.Dialect.Function;
3+
using NHibernate.Engine;
4+
using NHibernate.SqlCommand;
5+
using NHibernate.Type;
6+
7+
namespace NHibernate.Dialect
8+
{
9+
public class BitwiseFunctionOperation : ISQLFunction
10+
{
11+
private readonly string _functionName;
12+
private SqlStringBuilder _sqlBuffer;
13+
private Queue _args;
14+
15+
public BitwiseFunctionOperation(string functionName)
16+
{
17+
_functionName = functionName;
18+
}
19+
20+
#region ISQLFunction Members
21+
22+
public IType ReturnType(IType columnType, IMapping mapping)
23+
{
24+
return NHibernateUtil.Int64;
25+
}
26+
27+
public bool HasArguments
28+
{
29+
get { return true; }
30+
}
31+
32+
public bool HasParenthesesIfNoArguments
33+
{
34+
get { return true; }
35+
}
36+
37+
public SqlString Render(IList args, ISessionFactoryImplementor factory)
38+
{
39+
Prepare(args);
40+
41+
Function();
42+
OpenParens();
43+
Arguments();
44+
CloseParens();
45+
46+
return _sqlBuffer.ToSqlString();
47+
}
48+
49+
#endregion
50+
51+
private void Prepare(IList args)
52+
{
53+
_sqlBuffer = new SqlStringBuilder();
54+
_args = new Queue();
55+
foreach (var arg in args)
56+
{
57+
if (!IsParens(arg.ToString()))
58+
_args.Enqueue(arg);
59+
}
60+
}
61+
62+
private static bool IsParens(string candidate)
63+
{
64+
return candidate == "(" || candidate == ")";
65+
}
66+
67+
private void Function()
68+
{
69+
_sqlBuffer.Add(_functionName);
70+
}
71+
72+
private void OpenParens()
73+
{
74+
_sqlBuffer.Add("(");
75+
}
76+
77+
private void Arguments()
78+
{
79+
while (_args.Count > 0)
80+
{
81+
var arg = _args.Dequeue();
82+
if (arg is Parameter || arg is SqlString)
83+
_sqlBuffer.AddObject(arg);
84+
else
85+
_sqlBuffer.Add(arg.ToString());
86+
if (_args.Count > 0)
87+
_sqlBuffer.Add(", ");
88+
}
89+
}
90+
91+
private void CloseParens()
92+
{
93+
_sqlBuffer.Add(")");
94+
}
95+
}
96+
}

src/NHibernate/Dialect/NativeBitwiseOperation.cs renamed to src/NHibernate/Dialect/BitwiseNativeOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
namespace NHibernate.Dialect
99
{
1010
[Serializable]
11-
public class NativeBitwiseOperation : ISQLFunction
11+
public class BitwiseNativeOperation : ISQLFunction
1212
{
1313
private readonly string _sqlToken;
1414
private Queue _args;
1515
private SqlStringBuilder _sqlBuffer;
1616

17-
public NativeBitwiseOperation(string sqlToken)
17+
public BitwiseNativeOperation(string sqlToken)
1818
{
1919
_sqlToken = sqlToken;
2020
}

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("~"));
121121

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

src/NHibernate/Dialect/FirebirdDialect.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ private void OverrideStandardHQLFunctions()
322322
RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as VARCHAR(255))"));
323323
RegisterFunction("sysdate", new CastedFunction("today", NHibernateUtil.Date));
324324
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "cast(?1 as date)"));
325+
// Bitwise operations
326+
RegisterFunction("band", new BitwiseFunctionOperation("bin_and"));
327+
RegisterFunction("bor", new BitwiseFunctionOperation("bin_or"));
328+
RegisterFunction("bxor", new BitwiseFunctionOperation("bin_xor"));
329+
RegisterFunction("bnot", new BitwiseFunctionOperation("bin_not"));
325330
}
326331

327332
private void RegisterFirebirdServerEmbeddedFunctions()
@@ -349,9 +354,6 @@ private void RegisterExternalFbAndIbStandardUDFs()
349354
private void RegisterMathematicalFunctions()
350355
{
351356
RegisterFunction("abs", new StandardSQLFunction("abs", NHibernateUtil.Double));
352-
RegisterFunction("bin_and", new StandardSQLFunction("bin_and", NHibernateUtil.Int32));
353-
RegisterFunction("bin_or", new StandardSQLFunction("bin_or", NHibernateUtil.Int32));
354-
RegisterFunction("bin_xor", new StandardSQLFunction("bin_xor", NHibernateUtil.Int32));
355357
RegisterFunction("ceiling", new StandardSQLFunction("ceiling", NHibernateUtil.Double));
356358
RegisterFunction("div", new StandardSQLFunction("div", NHibernateUtil.Double));
357359
RegisterFunction("dpower", new StandardSQLFunction("dpower", NHibernateUtil.Double));

src/NHibernate/NHibernate.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
<Compile Include="Connection\DriverConnectionProvider.cs" />
138138
<Compile Include="Connection\IConnectionProvider.cs" />
139139
<Compile Include="Connection\UserSuppliedConnectionProvider.cs" />
140+
<Compile Include="Dialect\BitwiseFunctionOperation.cs" />
140141
<Compile Include="Dialect\DB2Dialect.cs" />
141142
<Compile Include="Dialect\Dialect.cs" />
142143
<Compile Include="Dialect\FirebirdDialect.cs" />
@@ -156,7 +157,7 @@
156157
<Compile Include="Dialect\MySQL55InnoDBDialect.cs" />
157158
<Compile Include="Dialect\MySQL5InnoDBDialect.cs" />
158159
<Compile Include="Dialect\MySQLDialect.cs" />
159-
<Compile Include="Dialect\NativeBitwiseOperation.cs" />
160+
<Compile Include="Dialect\BitwiseNativeOperation.cs" />
160161
<Compile Include="Dialect\PostgreSQLDialect.cs" />
161162
<Compile Include="Dialect\Schema\PostgreSQLMetadata.cs" />
162163
<Compile Include="Dialect\Schema\SchemaHelper.cs" />

0 commit comments

Comments
 (0)