Skip to content

Bitwise operator adjustments #1691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/NHibernate.Test/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,19 +447,6 @@ protected DateTime RoundForDialect(DateTime value)
{"locate", new HashSet<System.Type> {typeof (SQLiteDialect)}},
{"bit_length", new HashSet<System.Type> {typeof (SQLiteDialect)}},
{"extract", new HashSet<System.Type> {typeof (SQLiteDialect)}},
{
"bxor",
new HashSet<System.Type>
{
// Could be supported like Oracle, with a template
typeof (SQLiteDialect),
// Could be supported by overriding registration with # instead of ^
typeof (PostgreSQLDialect),
typeof (PostgreSQL81Dialect),
typeof (PostgreSQL82Dialect),
typeof (PostgreSQL83Dialect)
}
},
{
"nullif",
new HashSet<System.Type>
Expand Down
8 changes: 4 additions & 4 deletions src/NHibernate/Dialect/Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ protected Dialect()
RegisterFunction("year", new SQLFunctionTemplate(NHibernateUtil.Int32, "extract(year from ?1)"));

// Bitwise operations
RegisterFunction("band", new BitwiseNativeOperation("&"));
RegisterFunction("bor", new BitwiseNativeOperation("|"));
RegisterFunction("bxor", new BitwiseNativeOperation("^"));
RegisterFunction("bnot", new BitwiseNativeOperation("~", true));
RegisterFunction("band", new Function.BitwiseNativeOperation("&"));
RegisterFunction("bor", new Function.BitwiseNativeOperation("|"));
RegisterFunction("bxor", new Function.BitwiseNativeOperation("^"));
RegisterFunction("bnot", new Function.BitwiseNativeOperation("~", true));

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

Expand Down
8 changes: 4 additions & 4 deletions src/NHibernate/Dialect/FirebirdDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ private void OverrideStandardHQLFunctions()
RegisterFunction("sysdate", new CastedFunction("today", NHibernateUtil.Date));
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "cast(?1 as date)"));
// Bitwise operations
RegisterFunction("band", new BitwiseFunctionOperation("bin_and"));
RegisterFunction("bor", new BitwiseFunctionOperation("bin_or"));
RegisterFunction("bxor", new BitwiseFunctionOperation("bin_xor"));
RegisterFunction("bnot", new BitwiseFunctionOperation("bin_not"));
RegisterFunction("band", new Function.BitwiseFunctionOperation("bin_and"));
RegisterFunction("bor", new Function.BitwiseFunctionOperation("bin_or"));
RegisterFunction("bxor", new Function.BitwiseFunctionOperation("bin_xor"));
RegisterFunction("bnot", new Function.BitwiseFunctionOperation("bin_not"));
}

private void RegisterFirebirdServerEmbeddedFunctions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
using System;
using System.Collections;
using NHibernate.Dialect.Function;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;

// 6.0 TODO: remove NHibernate.Dialect.BitwiseFunctionOperation,
// and remove "Function." prefix where the non obsolete one is used.
namespace NHibernate.Dialect
{
/// <inheritdoc />
[Serializable]
// Since 5.2
[Obsolete("Use NHibernate.Dialect.Function.BitwiseFunctionOperation instead")]
public class BitwiseFunctionOperation : Function.BitwiseFunctionOperation
{
/// <inheritdoc />
public BitwiseFunctionOperation(string functionName): base(functionName)
{
}
}
}

namespace NHibernate.Dialect.Function
{
/// <summary>
/// Treats bitwise operations as SQL function calls.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
using System;
using System.Collections;
using NHibernate.Dialect.Function;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;

// 6.0 TODO: remove NHibernate.Dialect.BitwiseNativeOperation,
// and remove "Function." prefix where the non obsolete one is used.
namespace NHibernate.Dialect
{
/// <inheritdoc />
[Serializable]
// Since 5.2
[Obsolete("Use NHibernate.Dialect.Function.BitwiseNativeOperation instead")]
public class BitwiseNativeOperation : Function.BitwiseNativeOperation
{
/// <inheritdoc />
public BitwiseNativeOperation(string sqlOpToken) : base(sqlOpToken)
{
}

/// <inheritdoc />
public BitwiseNativeOperation(string sqlOpToken, bool isNot) : base(sqlOpToken, isNot)
{
}
}
}

namespace NHibernate.Dialect.Function
{
/// <summary>
/// Treats bitwise operations as native operations.
Expand All @@ -14,7 +35,7 @@ namespace NHibernate.Dialect
public class BitwiseNativeOperation : ISQLFunction
{
private readonly string _sqlOpToken;
private readonly bool _isNot;
private readonly bool _isUnary;

/// <summary>
/// Creates an instance using the giving token.
Expand All @@ -34,11 +55,11 @@ public BitwiseNativeOperation(string sqlOpToken)
/// Creates an instance using the giving token and the flag indicating if it is an unary operator.
/// </summary>
/// <param name="sqlOpToken">The operation token.</param>
/// <param name="isNot">Whether the operation is unary or not.</param>
public BitwiseNativeOperation(string sqlOpToken, bool isNot)
/// <param name="isUnary">Whether the operation is unary or not.</param>
public BitwiseNativeOperation(string sqlOpToken, bool isUnary)
{
_sqlOpToken = sqlOpToken;
_isNot = isNot;
_isUnary = isUnary;
}

#region ISQLFunction Members
Expand All @@ -63,11 +84,11 @@ public SqlString Render(IList args, ISessionFactoryImplementor factory)

var sqlBuffer = new SqlStringBuilder();

if (!_isNot)
if (!_isUnary)
AddToBuffer(args[0], sqlBuffer);

sqlBuffer.Add(" ").Add(_sqlOpToken).Add(" ");
for (var i = _isNot ? 0 : 1; i < args.Count; i++)
for (var i = _isUnary ? 0 : 1; i < args.Count; i++)
{
AddToBuffer(args[i], sqlBuffer);
}
Expand Down
8 changes: 4 additions & 4 deletions src/NHibernate/Dialect/HanaDialectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,10 @@ protected virtual void RegisterFunctions()

protected virtual void RegisterNHibernateFunctions()
{
RegisterFunction("band", new BitwiseFunctionOperation("bitand"));
RegisterFunction("bor", new BitwiseFunctionOperation("bitor"));
RegisterFunction("bxor", new BitwiseFunctionOperation("bitxor"));
RegisterFunction("bnot", new BitwiseFunctionOperation("bitnot"));
RegisterFunction("band", new Function.BitwiseFunctionOperation("bitand"));
RegisterFunction("bor", new Function.BitwiseFunctionOperation("bitor"));
RegisterFunction("bxor", new Function.BitwiseFunctionOperation("bitxor"));
RegisterFunction("bnot", new Function.BitwiseFunctionOperation("bitnot"));
RegisterFunction("bit_length", new SQLFunctionTemplate(NHibernateUtil.Int32, "length(to_binary(?1))*8"));
RegisterFunction("ceiling", new StandardSQLFunction("ceil"));
RegisterFunction("chr", new StandardSQLFunction("char", NHibernateUtil.AnsiChar));
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Dialect/Oracle8iDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ protected virtual void RegisterFunctions()

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

RegisterFunction("band", new BitwiseFunctionOperation("bitand"));
RegisterFunction("band", new Function.BitwiseFunctionOperation("bitand"));
RegisterFunction("bor", new SQLFunctionTemplate(null, "?1 + ?2 - BITAND(?1, ?2)"));
RegisterFunction("bxor", new SQLFunctionTemplate(null, "?1 + ?2 - BITAND(?1, ?2) * 2"));
RegisterFunction("bnot", new SQLFunctionTemplate(null, "(-1 - ?1)"));
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Dialect/PostgreSQLDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public PostgreSQLDialect()
RegisterFunction("atan2", new StandardSQLFunction("atan2", NHibernateUtil.Double));

RegisterFunction("power", new StandardSQLFunction("power", NHibernateUtil.Double));
RegisterFunction("bxor", new BitwiseNativeOperation("#"));
RegisterFunction("bxor", new Function.BitwiseNativeOperation("#"));

RegisterFunction("floor", new StandardSQLFunction("floor"));
RegisterFunction("ceiling", new StandardSQLFunction("ceiling"));
Expand Down
4 changes: 4 additions & 0 deletions src/NHibernate/Dialect/SQLiteDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ protected virtual void RegisterFunctions()

RegisterFunction("round", new StandardSQLFunction("round"));

// SQLite has no built-in support of bitwise xor, but can emulate it.
// http://sqlite.1065341.n5.nabble.com/XOR-operator-td98004.html
RegisterFunction("bxor", new SQLFunctionTemplate(null, "((?1 | ?2) - (?1 & ?2))"));

// NH-3787: SQLite requires the cast in SQL too for not defaulting to string.
RegisterFunction("transparentcast", new CastFunction());
}
Expand Down