Skip to content

Commit 4f34c1c

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 3087d48 commit 4f34c1c

File tree

9 files changed

+381
-198
lines changed

9 files changed

+381
-198
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+
}

src/NHibernate/Hql/Ast/ANTLR/Generated/HqlLexer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// $ANTLR 3.2 Sep 23, 2009 12:02:23 Hql.g 2011-05-22 07:45:50
1+
// $ANTLR 3.2 Sep 23, 2009 12:02:23 Hql.g 2014-07-05 12:15:51
22

33
// The variable 'variable' is assigned but its value is never used.
44
#pragma warning disable 168, 219
@@ -19,8 +19,8 @@ namespace NHibernate.Hql.Ast.ANTLR
1919
public partial class HqlLexer : Lexer {
2020
public const int LT = 109;
2121
public const int EXPONENT = 130;
22-
public const int STAR = 120;
2322
public const int FLOAT_SUFFIX = 131;
23+
public const int STAR = 120;
2424
public const int LITERAL_by = 56;
2525
public const int CASE = 57;
2626
public const int NEW = 37;

src/NHibernate/Hql/Ast/ANTLR/Generated/HqlParser.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// $ANTLR 3.2 Sep 23, 2009 12:02:23 Hql.g 2011-05-22 07:45:49
1+
// $ANTLR 3.2 Sep 23, 2009 12:02:23 Hql.g 2014-07-05 12:15:51
22

33
// The variable 'variable' is assigned but its value is never used.
44
#pragma warning disable 168, 219
@@ -1067,7 +1067,7 @@ public HqlParser.optionalFromTokenFromClause_return optionalFromTokenFromClause(
10671067

10681068

10691069
// AST REWRITE
1070-
// elements: path, asAlias
1070+
// elements: asAlias, path
10711071
// token labels:
10721072
// rule labels: retval
10731073
// token list labels:
@@ -3555,7 +3555,7 @@ public HqlParser.fromClassOrOuterQueryPath_return fromClassOrOuterQueryPath() //
35553555

35563556

35573557
// AST REWRITE
3558-
// elements: propertyFetch, asAlias, path
3558+
// elements: asAlias, propertyFetch, path
35593559
// token labels:
35603560
// rule labels: retval
35613561
// token list labels:
@@ -3801,7 +3801,7 @@ public HqlParser.inCollectionDeclaration_return inCollectionDeclaration() // thr
38013801

38023802

38033803
// AST REWRITE
3804-
// elements: alias, path
3804+
// elements: path, alias
38053805
// token labels:
38063806
// rule labels: retval
38073807
// token list labels:
@@ -3952,7 +3952,7 @@ public HqlParser.inCollectionElementsDeclaration_return inCollectionElementsDecl
39523952

39533953

39543954
// AST REWRITE
3955-
// elements: path, alias
3955+
// elements: alias, path
39563956
// token labels:
39573957
// rule labels: retval
39583958
// token list labels:
@@ -8084,7 +8084,7 @@ public HqlParser.caseExpression_return caseExpression() // throws RecognitionExc
80848084

80858085

80868086
// AST REWRITE
8087-
// elements: whenClause, CASE, elseClause
8087+
// elements: CASE, elseClause, whenClause
80888088
// token labels:
80898089
// rule labels: retval
80908090
// token list labels:

src/NHibernate/Hql/Ast/ANTLR/Generated/HqlSqlWalker.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// $ANTLR 3.2 Sep 23, 2009 12:02:23 HqlSqlWalker.g 2011-06-14 20:28:08
1+
// $ANTLR 3.2 Sep 23, 2009 12:02:23 HqlSqlWalker.g 2014-07-05 12:15:52
22

33
// The variable 'variable' is assigned but its value is never used.
44
#pragma warning disable 168, 219
@@ -180,8 +180,8 @@ public partial class HqlSqlWalker : TreeParser
180180
public const int SELECT_COLUMNS = 144;
181181
public const int LT = 109;
182182
public const int EXPONENT = 130;
183-
public const int STAR = 120;
184183
public const int FLOAT_SUFFIX = 131;
184+
public const int STAR = 120;
185185
public const int FILTERS = 147;
186186
public const int LITERAL_by = 56;
187187
public const int PROPERTY_REF = 142;
@@ -681,7 +681,7 @@ public HqlSqlWalker.updateStatement_return updateStatement() // throws Recogniti
681681

682682

683683
// AST REWRITE
684-
// elements: w, f, s, u
684+
// elements: u, w, s, f
685685
// token labels: u
686686
// rule labels: f, w, retval, s
687687
// token list labels:
@@ -1845,7 +1845,7 @@ public HqlSqlWalker.unionedQuery_return unionedQuery() // throws RecognitionExce
18451845

18461846

18471847
// AST REWRITE
1848-
// elements: sk, o, h, g, s, tk, w, f
1848+
// elements: sk, o, h, s, tk, g, w, f
18491849
// token labels:
18501850
// rule labels: f, w, g, sk, retval, s, o, tk, h
18511851
// token list labels:
@@ -2745,7 +2745,7 @@ public HqlSqlWalker.selectClause_return selectClause() // throws RecognitionExce
27452745

27462746

27472747
// AST REWRITE
2748-
// elements: x, d
2748+
// elements: d, x
27492749
// token labels: d
27502750
// rule labels: retval, x
27512751
// token list labels:
@@ -9179,7 +9179,7 @@ public HqlSqlWalker.addrExprIndex_return addrExprIndex(bool root) // throws Reco
91799179

91809180

91819181
// AST REWRITE
9182-
// elements: i, rhs2, lhs2
9182+
// elements: rhs2, i, lhs2
91839183
// token labels: i
91849184
// rule labels: retval, rhs2, lhs2
91859185
// token list labels:
@@ -9681,7 +9681,7 @@ public HqlSqlWalker.propertyRefPath_return propertyRefPath() // throws Recogniti
96819681

96829682

96839683
// AST REWRITE
9684-
// elements: d, rhs, lhs
9684+
// elements: rhs, lhs, d
96859685
// token labels: d
96869686
// rule labels: retval, rhs, lhs
96879687
// token list labels:

0 commit comments

Comments
 (0)