-
Notifications
You must be signed in to change notification settings - Fork 936
/
Copy pathMsSql2012Dialect.cs
90 lines (75 loc) · 2.3 KB
/
MsSql2012Dialect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using NHibernate.Dialect.Function;
using NHibernate.SqlCommand;
using NHibernate.SqlCommand.Parser;
namespace NHibernate.Dialect
{
public class MsSql2012Dialect : MsSql2008Dialect
{
public override bool SupportsSequences
{
get { return true; }
}
public override bool SupportsPooledSequences
{
get { return true; }
}
public override string GetCreateSequenceString(string sequenceName)
{
// by default sequence is created as bigint start with long.MinValue
return GetCreateSequenceString(sequenceName, 1, 1);
}
protected override string GetCreateSequenceString(string sequenceName, int initialValue, int incrementSize)
{
// by default sequence is created as bigint
return string.Format("create sequence {0} as int start with {1} increment by {2}", sequenceName, initialValue, incrementSize);
}
public override string GetDropSequenceString(string sequenceName)
{
string dropSequence = "IF EXISTS (SELECT * FROM sys.sequences WHERE object_id = OBJECT_ID(N'{0}')) DROP SEQUENCE {0}";
return string.Format(dropSequence, sequenceName);
}
public override string GetSequenceNextValString(string sequenceName)
{
return "select " + GetSelectSequenceNextValString(sequenceName) + " as seq";
}
public override string GetSelectSequenceNextValString(string sequenceName)
{
return "next value for " + sequenceName;
}
public override string QuerySequencesString
{
get { return "select name from sys.sequences"; }
}
protected override void RegisterFunctions()
{
base.RegisterFunctions();
RegisterFunction("iif", new StandardSafeSQLFunction("iif", 3));
}
public override SqlString GetLimitString(SqlString querySqlString, SqlString offset, SqlString limit)
{
using (var tokenEnum = new SqlTokenizer(querySqlString).GetEnumerator())
{
if (!tokenEnum.TryParseUntilFirstMsSqlSelectColumn()) return null;
var result = new SqlStringBuilder(querySqlString);
if (!tokenEnum.TryParseUntil("order"))
{
result.Add(" ORDER BY CURRENT_TIMESTAMP");
}
result.Add(" OFFSET ");
if (offset != null)
{
result.Add(offset).Add(" ROWS");
}
else
{
result.Add("0 ROWS");
}
if (limit != null)
{
result.Add(" FETCH FIRST ").Add(limit).Add(" ROWS ONLY");
}
return result.ToSqlString();
}
}
}
}