Skip to content

Commit 60108ab

Browse files
committed
change SqlGenerator to use dialect specfic bitwise operations identified as "band", "bor", "bxor" and "bnot" which can be overriden by Subclasses of Dialect
1 parent 2becb5b commit 60108ab

File tree

9 files changed

+33552
-39394
lines changed

9 files changed

+33552
-39394
lines changed

src/NHibernate/Dialect/Dialect.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ 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("~"));
121+
116122
RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as char)"));
117123

118124
// register hibernate types for default use in scalar sqlquery type auto detection
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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+
[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+
}

0 commit comments

Comments
 (0)