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

Commit 371ca51

Browse files
committed
Moved SQLServer Collate before Nullable check and implemented BucketCount & Indexing on Mem-Opt tables.
1 parent 8ad8ec2 commit 371ca51

File tree

3 files changed

+67
-33
lines changed

3 files changed

+67
-33
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))

0 commit comments

Comments
 (0)