Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit bc91262

Browse files
committed
Added MemoryOptimized attribute to SqlServer provider
1 parent 9373ef2 commit bc91262

7 files changed

+185
-1
lines changed

src/ServiceStack.OrmLite.SqlServer/ServiceStack.OrmLite.SqlServer.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
<Compile Include="Converters\SqlServerSpecialConverters.cs" />
103103
<Compile Include="Converters\SqlServerStringConverters.cs" />
104104
<Compile Include="Converters\SqlServerTimeConverter.cs" />
105+
<Compile Include="SqlServer2016OrmLiteDialectProvider.cs" />
106+
<Compile Include="SqlServer2014OrmLiteDialectProvider.cs" />
105107
<Compile Include="SqlServer2012OrmLiteDialectProvider.cs" />
106108
<Compile Include="SqlServerDialect.cs" />
107109
<Compile Include="SqlServerExpression.cs" />

src/ServiceStack.OrmLite.SqlServer/SqlServer2012OrmLiteDialectProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ServiceStack.OrmLite.SqlServer
66
{
77
public class SqlServer2012OrmLiteDialectProvider : SqlServerOrmLiteDialectProvider
88
{
9-
public static SqlServer2012OrmLiteDialectProvider Instance = new SqlServer2012OrmLiteDialectProvider();
9+
public static new SqlServer2012OrmLiteDialectProvider Instance = new SqlServer2012OrmLiteDialectProvider();
1010

1111
public override string ToSelectStatement(ModelDefinition modelDef,
1212
string selectExpression,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Data;
3+
using System.Text;
4+
using ServiceStack.Text;
5+
using ServiceStack.DataAnnotations;
6+
7+
namespace ServiceStack.OrmLite.SqlServer
8+
{
9+
public class SqlServer2014OrmLiteDialectProvider : SqlServer2012OrmLiteDialectProvider
10+
{
11+
12+
public override string ToCreateTableStatement(Type tableType)
13+
{
14+
var sbColumns = StringBuilderCache.Allocate();
15+
var sbConstraints = StringBuilderCacheAlt.Allocate();
16+
var sbMemOptimized = StringBuilderCacheAlt.Allocate();
17+
18+
var modelDef = OrmLiteUtils.GetModelDefinition(tableType);
19+
foreach (var fieldDef in modelDef.FieldDefinitions)
20+
{
21+
if (fieldDef.CustomSelect != null)
22+
continue;
23+
24+
var columnDefinition = GetColumnDefinition(
25+
fieldDef.FieldName,
26+
fieldDef.ColumnType,
27+
fieldDef.IsPrimaryKey,
28+
fieldDef.AutoIncrement,
29+
fieldDef.IsNullable,
30+
fieldDef.IsRowVersion,
31+
fieldDef.FieldLength,
32+
fieldDef.Scale,
33+
GetDefaultValue(fieldDef),
34+
fieldDef.CustomFieldDefinition);
35+
36+
if (columnDefinition == null)
37+
continue;
38+
39+
if (sbColumns.Length != 0)
40+
sbColumns.Append(", \n ");
41+
42+
sbColumns.Append(columnDefinition);
43+
44+
if (fieldDef.ForeignKey == null) continue;
45+
46+
var refModelDef = OrmLiteUtils.GetModelDefinition(fieldDef.ForeignKey.ReferenceType);
47+
sbConstraints.Append(
48+
$", \n\n CONSTRAINT {GetQuotedName(fieldDef.ForeignKey.GetForeignKeyName(modelDef, refModelDef, NamingStrategy, fieldDef))} " +
49+
$"FOREIGN KEY ({GetQuotedColumnName(fieldDef.FieldName)}) " +
50+
$"REFERENCES {GetQuotedTableName(refModelDef)} ({GetQuotedColumnName(refModelDef.PrimaryKey.FieldName)})");
51+
52+
sbConstraints.Append(GetForeignKeyOnDeleteClause(fieldDef.ForeignKey));
53+
sbConstraints.Append(GetForeignKeyOnUpdateClause(fieldDef.ForeignKey));
54+
}
55+
56+
{
57+
sbMemOptimized.Append(" WITH (MEMORY_OPTIMIZED=ON");
58+
if (attrib.Durability == TableDurability.SCHEMA_ONLY)
59+
sbMemOptimized.Append(", DURABILITY=SCHEMA_ONLY");
60+
else if (attrib.Durability == TableDurability.SCHEMA_AND_DATA)
61+
sbMemOptimized.Append(", DURABILITY=SCHEMA_AND_DATA");
62+
sbMemOptimized.Append(")");
63+
}
64+
65+
var sql = $"CREATE TABLE {GetQuotedTableName(modelDef)} " +
66+
$"\n(\n {StringBuilderCache.ReturnAndFree(sbColumns)}{StringBuilderCacheAlt.ReturnAndFree(sbConstraints)} \n){StringBuilderCache.ReturnAndFree(sbMemOptimized)}; \n";
67+
68+
return sql;
69+
}
70+
}
71+
}
72+
73+
namespace ServiceStack.DataAnnotations
74+
{
75+
// SQL 2014: https://msdn.microsoft.com/en-us/library/dn553122(v=sql.120).aspx
76+
// SQL 2016: https://msdn.microsoft.com/en-us/library/dn553122(v=sql.130).aspx
77+
{
78+
79+
80+
public TableDurability? Durability { get; set; }
81+
}
82+
83+
public enum TableDurability
84+
{
85+
SCHEMA_ONLY, // (non-durable table) recreated upon server restart, data is lost, no transaction logging and checkpoints
86+
SCHEMA_AND_DATA // (durable table) data persists upon server restart
87+
}
88+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Data;
3+
using System.Text;
4+
using ServiceStack.Text;
5+
6+
namespace ServiceStack.OrmLite.SqlServer
7+
{
8+
public class SqlServer2016OrmLiteDialectProvider : SqlServer2014OrmLiteDialectProvider
9+
{
10+
}
11+
}

src/ServiceStack.OrmLite.SqlServer/SqlServerDialect.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ public static class SqlServer2012Dialect
1111
{
1212
public static IOrmLiteDialectProvider Provider => SqlServer2012OrmLiteDialectProvider.Instance;
1313
}
14+
15+
public static class SqlServer2014Dialect
16+
{
17+
public static IOrmLiteDialectProvider Provider => SqlServer2014OrmLiteDialectProvider.Instance;
18+
}
19+
20+
public static class SqlServer2016Dialect
21+
{
22+
public static IOrmLiteDialectProvider Provider => SqlServer2016OrmLiteDialectProvider.Instance;
23+
}
1424
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using NUnit.Framework;
2+
using ServiceStack.DataAnnotations;
3+
4+
namespace ServiceStack.OrmLite.SqlServerTests
5+
{
6+
[TestFixture]
7+
{
8+
[TestFixtureSetUp]
9+
public void Setup()
10+
{
11+
OrmLiteConfig.DialectProvider = SqlServer2014Dialect.Provider;
12+
13+
using (var dbConn = OpenDbConnection())
14+
{
15+
dbConn.DropTable<TypeWithMemTableNoDurability>();
16+
dbConn.DropTable<TypeWithMemTableSchemaOnlyDurability>();
17+
dbConn.DropTable<TypeWithMemTableSchemaAndDataDurability>();
18+
}
19+
}
20+
21+
[Test]
22+
public void CanCreateMemoryOptimizedTable()
23+
{
24+
using (var dbConn = OpenDbConnection())
25+
{
26+
dbConn.CreateTable<TypeWithMemTableNoDurability>(true);
27+
}
28+
}
29+
30+
[Test]
31+
public void CanCreateMemoryOptimizedTableWithSchemaOnlyDurability()
32+
{
33+
using (var dbConn = OpenDbConnection())
34+
{
35+
dbConn.CreateTable<TypeWithMemTableSchemaOnlyDurability>(true);
36+
}
37+
}
38+
39+
[Test]
40+
public void CanCreateMemoryOptimizedTableWithSchemaAndDurability()
41+
{
42+
using (var dbConn = OpenDbConnection())
43+
{
44+
dbConn.CreateTable<TypeWithMemTableSchemaAndDataDurability>(true);
45+
}
46+
}
47+
}
48+
49+
public class TypeWithMemTableNoDurability
50+
{
51+
[AutoIncrement]
52+
public int Id { get; set; }
53+
54+
public string Name { get; set; }
55+
}
56+
57+
public class TypeWithMemTableSchemaOnlyDurability
58+
{
59+
[AutoIncrement]
60+
public int Id { get; set; }
61+
62+
public string Name { get; set; }
63+
}
64+
65+
public class TypeWithMemTableSchemaAndDataDurability
66+
{
67+
[AutoIncrement]
68+
public int Id { get; set; }
69+
70+
public string Name { get; set; }
71+
}
72+
}

src/ServiceStack.OrmLite.SqlServerTests/ServiceStack.OrmLite.SqlServerTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="Issues\DeleteWithGeoTypesIssue.cs" />
9191
<Compile Include="Issues\JamesGeoIssue.cs" />
9292
<Compile Include="Issues\SerializationTests.cs" />
93+
<Compile Include="MemoryOptimizedAttributeTests.cs" />
9394
<Compile Include="NestedTransactions.cs" />
9495
<Compile Include="EnumTests.cs" />
9596
<Compile Include="Expressions\AdditiveExpressionsTest.cs" />

0 commit comments

Comments
 (0)