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

Commit 8f53368

Browse files
committed
2 parents 4f6d602 + 371ca51 commit 8f53368

9 files changed

+320
-158
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServer2012OrmLiteDialectProvider.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ public override string GetColumnDefinition(FieldDefinition fieldDef)
5656
var sql = StringBuilderCache.Allocate();
5757
sql.Append($"{GetQuotedColumnName(fieldDef.FieldName)} {fieldDefinition}");
5858

59+
if (fieldDef.FieldType == typeof(string))
60+
{
61+
// https://msdn.microsoft.com/en-us/library/ms184391.aspx
62+
var collation = fieldDef.PropertyInfo.FirstAttribute<SqlServerCollateAttribute>()?.Collation;
63+
if (!string.IsNullOrEmpty(collation))
64+
{
65+
sql.Append($" COLLATE {collation}");
66+
}
67+
}
68+
5969
if (fieldDef.IsPrimaryKey)
6070
{
6171
sql.Append(" PRIMARY KEY");
@@ -69,13 +79,6 @@ public override string GetColumnDefinition(FieldDefinition fieldDef)
6979
sql.Append(fieldDef.IsNullable ? " NULL" : " NOT NULL");
7080
}
7181

72-
// https://msdn.microsoft.com/en-us/library/ms184391.aspx
73-
var collation = fieldDef.PropertyInfo.FirstAttribute<SqlServerCollateAttribute>()?.Collation;
74-
if (!string.IsNullOrEmpty(collation))
75-
{
76-
sql.Append($" COLLATE {collation}");
77-
}
78-
7982
var defaultValue = GetDefaultValue(fieldDef);
8083
if (!string.IsNullOrEmpty(defaultValue))
8184
{

src/ServiceStack.OrmLite.SqlServer/SqlServer2014OrmLiteDialectProvider.cs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,74 @@ public override string GetColumnDefinition(FieldDefinition fieldDef)
1515
return $"{fieldDef.FieldName} rowversion NOT NULL";
1616

1717
var fieldDefinition = fieldDef.CustomFieldDefinition ??
18-
GetColumnTypeDefinition(fieldDef.ColumnType, fieldDef.FieldLength, fieldDef.Scale);
18+
GetColumnTypeDefinition(fieldDef.ColumnType, fieldDef.FieldLength, fieldDef.Scale);
19+
20+
var memTableAttrib = fieldDef.PropertyInfo.ReflectedType.FirstAttribute<SqlServerMemoryOptimizedAttribute>();
21+
var isMemoryTable = memTableAttrib != null;
1922

2023
var sql = StringBuilderCache.Allocate();
2124
sql.Append($"{GetQuotedColumnName(fieldDef.FieldName)} {fieldDefinition}");
2225

23-
if (fieldDef.IsPrimaryKey)
26+
if (fieldDef.FieldType == typeof(string))
2427
{
25-
var isMemoryTable = fieldDef.PropertyInfo.DeclaringType.FirstAttribute<SqlServerMemoryOptimizedAttribute>() != null;
28+
// https://msdn.microsoft.com/en-us/library/ms184391.aspx
29+
var collation = fieldDef.PropertyInfo.FirstAttribute<SqlServerCollateAttribute>()?.Collation;
30+
if (!string.IsNullOrEmpty(collation))
31+
{
32+
sql.Append($" COLLATE {collation}");
33+
}
34+
}
35+
36+
var bucketCount = fieldDef.PropertyInfo.FirstAttribute<SqlServerBucketCountAttribute>()?.Count;
2637

38+
if (fieldDef.IsPrimaryKey)
39+
{
2740
if (isMemoryTable)
2841
{
29-
sql.Append(fieldDef.IsNullable ? " NULL" : " NOT NULL");
30-
sql.Append(" PRIMARY KEY NONCLUSTERED");
31-
32-
var bucketCount = fieldDef.PropertyInfo.FirstAttribute<SqlServerBucketCountAttribute>()?.Count;
33-
if (bucketCount.HasValue)
34-
{
35-
sql.Append($" HASH WITH (BUCKET_COUNT = {bucketCount.Value})");
36-
}
42+
sql.Append($" NOT NULL PRIMARY KEY NONCLUSTERED");
3743
}
3844
else
3945
{
4046
sql.Append(" PRIMARY KEY");
47+
48+
if (fieldDef.IsNonClustered)
49+
sql.Append(" NONCLUSTERED");
4150
}
4251

4352
if (fieldDef.AutoIncrement)
4453
{
4554
sql.Append(" ").Append(AutoIncrementDefinition);
4655
}
56+
57+
if (isMemoryTable && bucketCount.HasValue)
58+
{
59+
sql.Append($" HASH WITH (BUCKET_COUNT = {bucketCount.Value})");
60+
}
4761
}
48-
else
62+
else if (!isMemoryTable && fieldDef.IsUnique)
4963
{
50-
sql.Append(fieldDef.IsNullable ? " NULL" : " NOT NULL");
51-
}
64+
sql.Append(" UNIQUE");
5265

53-
// https://msdn.microsoft.com/en-us/library/ms184391.aspx
54-
var collation = fieldDef.PropertyInfo.FirstAttribute<SqlServerCollateAttribute>()?.Collation;
55-
if (!string.IsNullOrEmpty(collation))
66+
if (fieldDef.IsNonClustered)
67+
sql.Append(" NONCLUSTERED");
68+
}
69+
else
5670
{
57-
sql.Append($" COLLATE {collation}");
71+
if (isMemoryTable && bucketCount.HasValue)
72+
{
73+
sql.Append($" NOT NULL INDEX {GetQuotedColumnName("IDX_" + fieldDef.FieldName)}");
74+
75+
if (fieldDef.IsNonClustered)
76+
{
77+
sql.Append(" NONCLUSTERED");
78+
}
79+
80+
sql.Append($" HASH WITH (BUCKET_COUNT = {bucketCount.Value})");
81+
}
82+
else
83+
{
84+
sql.Append(fieldDef.IsNullable ? " NULL" : " NOT NULL");
85+
}
5886
}
5987

6088
var defaultValue = GetDefaultValue(fieldDef);

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ public override string GetColumnDefinition(FieldDefinition fieldDef)
211211
var sql = StringBuilderCache.Allocate();
212212
sql.Append($"{GetQuotedColumnName(fieldDef.FieldName)} {fieldDefinition}");
213213

214+
if (fieldDef.FieldType == typeof(string))
215+
{
216+
// https://msdn.microsoft.com/en-us/library/ms184391.aspx
217+
var collation = fieldDef.PropertyInfo.FirstAttribute<SqlServerCollateAttribute>()?.Collation;
218+
if (!string.IsNullOrEmpty(collation))
219+
{
220+
sql.Append($" COLLATE {collation}");
221+
}
222+
}
223+
214224
if (fieldDef.IsPrimaryKey)
215225
{
216226
sql.Append(" PRIMARY KEY");
@@ -222,14 +232,7 @@ public override string GetColumnDefinition(FieldDefinition fieldDef)
222232
else
223233
{
224234
sql.Append(fieldDef.IsNullable ? " NULL" : " NOT NULL");
225-
}
226-
227-
// https://msdn.microsoft.com/en-us/library/ms184391.aspx
228-
var collation = fieldDef.PropertyInfo.FirstAttribute<SqlServerCollateAttribute>()?.Collation;
229-
if (!string.IsNullOrEmpty(collation))
230-
{
231-
sql.Append($" COLLATE {collation}");
232-
}
235+
}
233236

234237
var defaultValue = GetDefaultValue(fieldDef);
235238
if (!string.IsNullOrEmpty(defaultValue))

src/ServiceStack.OrmLite.SqlServerTests/Converters/ConvertersOrmLiteTestBase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Configuration;
3-
using System.Data;
43
using NUnit.Framework;
54
using ServiceStack.Logging;
65
using ServiceStack.OrmLite.SqlServer.Converters;
@@ -44,7 +43,7 @@ public override void TestFixtureSetUp()
4443
// Appending the Sql Server Type System Version to use SqlServerSpatial110.dll (2012) assembly
4544
// Sql Server defaults to SqlServerSpatial100.dll (2008 R2) even for versions greater
4645
// https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
47-
ConnectionString = ConfigurationManager.ConnectionStrings["testDb"].ConnectionString + "Type System Version=SQL Server 2014;";
46+
ConnectionString = ConfigurationManager.ConnectionStrings["testDb"].ConnectionString + "Type System Version=SQL Server 2012;";
4847

4948
var dialectProvider = SqlServerConverters.Configure(SqlServer2014Dialect.Provider);
5049

@@ -69,7 +68,7 @@ public override void TestFixtureSetUp()
6968
// Appending the Sql Server Type System Version to use SqlServerSpatial110.dll (2012) assembly
7069
// Sql Server defaults to SqlServerSpatial100.dll (2008 R2) even for versions greater
7170
// https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
72-
ConnectionString = ConfigurationManager.ConnectionStrings["testDb"].ConnectionString + "Type System Version=SQL Server 2016;";
71+
ConnectionString = ConfigurationManager.ConnectionStrings["testDb"].ConnectionString + "Type System Version=SQL Server 2012;";
7372

7473
var dialectProvider = SqlServerConverters.Configure(SqlServer2016Dialect.Provider);
7574

src/ServiceStack.OrmLite.SqlServerTests/MemoryOptimizedAttributeTests.cs

Lines changed: 0 additions & 76 deletions
This file was deleted.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080
<Compile Include="..\GlobalAssemblyInfo.cs">
8181
<Link>Properties\GlobalAssemblyInfo.cs</Link>
8282
</Compile>
83-
<Compile Include="Converters\FileStreamTests.cs" />
83+
<Compile Include="TableOptionAttributes\TableOptionsOrmLiteTestBase.cs" />
84+
<Compile Include="TableOptionAttributes\FileStreamTests.cs" />
8485
<Compile Include="Converters\SqlGeometryTests.cs" />
8586
<Compile Include="CustomSqlTests.cs" />
8687
<Compile Include="Datetime2Tests.cs" />
@@ -91,7 +92,7 @@
9192
<Compile Include="Issues\DeleteWithGeoTypesIssue.cs" />
9293
<Compile Include="Issues\JamesGeoIssue.cs" />
9394
<Compile Include="Issues\SerializationTests.cs" />
94-
<Compile Include="MemoryOptimizedAttributeTests.cs" />
95+
<Compile Include="TableOptionAttributes\MemoryOptimizedAttributeTests.cs" />
9596
<Compile Include="NestedTransactions.cs" />
9697
<Compile Include="EnumTests.cs" />
9798
<Compile Include="Expressions\AdditiveExpressionsTest.cs" />

src/ServiceStack.OrmLite.SqlServerTests/Converters/FileStreamTests.cs renamed to src/ServiceStack.OrmLite.SqlServerTests/TableOptionAttributes/FileStreamTests.cs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,48 @@
22
using Microsoft.SqlServer.Types;
33
using NUnit.Framework;
44
using ServiceStack.DataAnnotations;
5-
using ServiceStack.OrmLite.SqlServer.Converters;
6-
using ServiceStack.Text;
75

8-
namespace ServiceStack.OrmLite.SqlServerTests.Converters
6+
namespace ServiceStack.OrmLite.SqlServerTests.TableOptions
97
{
108
[TestFixture]
11-
public class FileStreamTests : SqlServer2012ConvertersOrmLiteTestBase
9+
public class FileTableAttributeTests : SqlServer2012TableOptionsOrmLiteTestBase
1210
{
1311
[Explicit("Requires FileGroups enabled in DB")]
1412
[Test]
15-
public void Can_select_from_FileStream()
13+
public void Can_select_from_FileTable()
1614
{
15+
Db.DropAndCreateTable<FileTable>();
1716

18-
ConnectionString = "Data Source=localhost;Initial Catalog=test2;User Id=test;Password=test;Connect Timeout=120;MultipleActiveResultSets=True;Type System Version=SQL Server 2012;";
19-
var dialectProvider = SqlServerConverters.Configure(SqlServer2012Dialect.Provider);
20-
var dbFactory = new OrmLiteConnectionFactory(ConnectionString, dialectProvider);
21-
22-
using (var db = dbFactory.OpenDbConnection())
17+
var ft = new FileTable
2318
{
24-
db.DropTable<TestFile>();
25-
db.CreateTable<TestFile>();
26-
27-
db.Insert(new TestFile { Contents = "contents".ToUtf8Bytes() });
28-
29-
db.Select<TestFile>().PrintDump();
30-
31-
//db.DropTable<FileStream>();
32-
//db.CreateTable<FileStream>();
19+
Name = "content.txt",
20+
Path = SqlHierarchyId.Parse("/1/"),
21+
FileContent = "contents".ToUtf8Bytes(),
22+
FileType = MimeTypes.PlainText
23+
};
24+
Db.Insert(ft);
3325

34-
//db.Insert(new FileStream
35-
//{
36-
// Name = "file.txt",
37-
// Path = SqlHierarchyId.Parse("/1/2/3/"),
38-
// ParentPath = SqlHierarchyId.Parse("/1/2/"),
39-
// FileContent = "contents".ToUtf8Bytes(),
40-
// FileType = MimeTypes.PlainText,
41-
//});
26+
var fileTable = Db.Single<FileTable>(x => x.Name == "content.txt");
4227

43-
//var q = db.From<FileStream>();
44-
//db.Select(q);
45-
}
28+
Assert.IsNotNull(fileTable);
29+
Assert.IsTrue(ft.FileContent.EquivalentTo(fileTable.FileContent));
4630
}
4731
}
4832

49-
public class TestFile
33+
34+
public class FileWithCustomSelect : FileTable
5035
{
51-
[PrimaryKey]
52-
[CustomField("uniqueidentifier ROWGUIDCOL NOT NULL")]
53-
public Guid Id { get; set; }
54-
55-
[CustomField("varbinary(max) FILESTREAM")]
56-
public byte[] Contents { get; set; }
57-
58-
public bool IsDirectory { get; set; }
59-
6036
[CustomSelect("Contents.GetFileNamespacePath() + (CASE WHEN is_directory = 1 THEN '\' ELSE '' END)")]
6137
public string FullPath { get; set; }
6238
}
6339

64-
public class FileStream
40+
[SqlServerFileTable(directory: "FILESTREAM_DIR")]
41+
public class FileTable
6542
{
6643
[PrimaryKey]
6744
[CustomField("uniqueidentifier ROWGUIDCOL NOT NULL")]
45+
[Default("newuid()")]
46+
[Alias("stream_id")]
6847
public Guid Id { get; set; }
6948

7049
[CustomField("varbinary(max) FILESTREAM")]
@@ -81,6 +60,7 @@ public class FileStream
8160

8261
//[ForeignKey(typeof(FileStream))]
8362
[Alias("parent_path_locator")]
63+
[Compute]
8464
public SqlHierarchyId? ParentPath { get; set; }
8565

8666
[Alias("file_type")]
@@ -121,8 +101,5 @@ public class FileStream
121101

122102
[Alias("is_temporary")]
123103
public bool IsTemporary { get; set; }
124-
125-
[CustomSelect("file_stream.GetFileNamespacePath() + (CASE WHEN is_directory = 1 THEN '\' ELSE '' END)")]
126-
public string FullPath { get; set; }
127104
}
128105
}

0 commit comments

Comments
 (0)