Skip to content

Commit c5bf8e1

Browse files
committed
NH-3195 - Port-back.
Add support for variable limit with TOP (n) clause to MsSqlCeDialect
1 parent c853c2b commit c5bf8e1

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/NHibernate/Dialect/MsSqlCe40Dialect.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ namespace NHibernate.Dialect
44
{
55
public class MsSqlCe40Dialect : MsSqlCeDialect
66
{
7-
public override bool SupportsLimit
8-
{
9-
get { return true; }
10-
}
11-
127
public override bool SupportsLimitOffset
138
{
149
get { return true; }

src/NHibernate/Dialect/MsSqlCeDialect.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System;
12
using System.Data;
23
using System.Data.Common;
34
using System.Text;
45
using NHibernate.Dialect.Function;
56
using NHibernate.Dialect.Schema;
7+
using NHibernate.SqlCommand;
68
using NHibernate.Util;
79
using Environment = NHibernate.Cfg.Environment;
810

@@ -111,7 +113,7 @@ public override string SelectGUIDString
111113

112114
public override bool SupportsLimit
113115
{
114-
get { return false; }
116+
get { return true; }
115117
}
116118

117119
public override bool SupportsLimitOffset
@@ -129,6 +131,17 @@ public override IDataBaseSchema GetDataBaseSchema(DbConnection connection)
129131
return new MsSqlCeDataBaseSchema(connection);
130132
}
131133

134+
public override SqlString GetLimitString(SqlString querySqlString, SqlString offset, SqlString limit)
135+
{
136+
var top = new SqlStringBuilder()
137+
.Add(" top (")
138+
.Add(limit)
139+
.Add(")")
140+
.ToSqlString();
141+
142+
return querySqlString.Insert(GetAfterSelectInsertPoint(querySqlString), top);
143+
}
144+
132145
public override string Qualify(string catalog, string schema, string table)
133146
{
134147
// SQL Server Compact doesn't support Schemas. So join schema name and table name with underscores
@@ -174,5 +187,18 @@ public override string Qualify(string catalog, string schema, string table)
174187
name = OpenQuote + name + CloseQuote;
175188
return qualifiedName.Append(name).ToString();
176189
}
177-
}
190+
191+
private static int GetAfterSelectInsertPoint(SqlString sql)
192+
{
193+
if (sql.StartsWithCaseInsensitive("select distinct"))
194+
{
195+
return 15;
196+
}
197+
if (sql.StartsWithCaseInsensitive("select"))
198+
{
199+
return 6;
200+
}
201+
throw new NotSupportedException("The query should start with 'SELECT' or 'SELECT DISTINCT'");
202+
}
203+
}
178204
}

0 commit comments

Comments
 (0)