Skip to content
Closed
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
10 changes: 9 additions & 1 deletion src/NHibernate/Dialect/MsSql2005Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NHibernate.Mapping;
using NHibernate.SqlCommand;
using NHibernate.Util;
using NHibernate.Dialect.Function;

namespace NHibernate.Dialect
{
Expand Down Expand Up @@ -36,6 +37,13 @@ protected override void RegisterKeywords()
RegisterKeyword("xml");
}

protected override void RegisterFunctions()
{
base.RegisterFunctions();
RegisterFunction("extract", new SQLFunctionTemplate(NHibernateUtil.Int32, "datepart(?1, ?3)"));
RegisterFunction("bit_length", new SQLFunctionTemplate(NHibernateUtil.Int32, "datalength(?1) * 8"));
}

public override SqlString GetLimitString(SqlString queryString, SqlString offset, SqlString limit)
{
var result = new SqlStringBuilder();
Expand Down Expand Up @@ -170,7 +178,7 @@ private int GetAfterSelectInsertPoint(SqlString sql)
/// <param name="statement"> the statement to evaluate</param>
/// <returns>true if the statment contains no parenthesis or an equal number of
/// opening and closing parenthesis;otherwise false </returns>
private static bool HasMatchingParens(IEnumerable<char> statement)
protected static bool HasMatchingParens(IEnumerable<char> statement)
{
//unmatched paren count
int unmatchedParen = 0;
Expand Down
38 changes: 38 additions & 0 deletions src/NHibernate/Dialect/MsSql2012Dialect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Data;
using NHibernate.Cfg;
using NHibernate.Dialect.Function;
using NHibernate.Driver;
using NHibernate.SqlCommand;

namespace NHibernate.Dialect
{
public class MsSql2012Dialect : MsSql2008Dialect
{
public override SqlString GetLimitString(SqlString queryString, SqlString offset, SqlString limit)
{
var result = new SqlStringBuilder(queryString);

int orderIndex = queryString.LastIndexOfCaseInsensitive(" order by ");

//don't use the order index if it is contained within a larger statement(assuming
//a statement with non matching parenthesis is part of a larger block)
if (orderIndex < 0 || !HasMatchingParens(queryString.Substring(orderIndex).ToString()))
{
// Use order by first column if no explicit ordering is provided
result.Add( " ORDER BY ")
.Add("1");
}

result.Add(" OFFSET ")
.Add(offset ?? new SqlString("0"))
.Add(" ROWS");

if (limit != null)
{
result.Add(" FETCH FIRST ").Add(limit).Add(" ROWS ONLY");
}

return result.ToSqlString();
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate/NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="Connection\IConnectionProvider.cs" />
<Compile Include="Connection\UserSuppliedConnectionProvider.cs" />
<Compile Include="Criterion\IEnhancedProjection.cs" />
<Compile Include="Dialect\MsSql2012Dialect.cs" />
<Compile Include="Dialect\DB2Dialect.cs" />
<Compile Include="Dialect\Dialect.cs" />
<Compile Include="Dialect\FirebirdDialect.cs" />
Expand Down