Skip to content

Commit 1212374

Browse files
magnusbakken-zetadisplaymsallin
authored andcommitted
Adjustments:
- Renamed CollationData to Collation. - Removed the ICollationData interface. - Moved Collation and CollationFunction out of Attributes. - Changed the way the default collation is passed around to use the Collation class.
1 parent a305e99 commit 1212374

File tree

12 files changed

+51
-50
lines changed

12 files changed

+51
-50
lines changed

SQLite.CodeFirst.Test/IntegrationTests/SqlGenerationDefaultCollationTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void SqliteSqlGeneratorWithDefaultCollationTest()
5757
// This is important! Else the in memory database will not work.
5858
connection.Open();
5959

60-
var defaultCollation = new CollationData() { Collation = CollationFunction.Custom, Function = "custom_collate" };
60+
var defaultCollation = new Collation() { CollationFunction = CollationFunction.Custom, Function = "custom_collate" };
6161
using (var context = new DummyDbContext(connection, defaultCollation))
6262
{
6363
// ReSharper disable once UnusedVariable
@@ -77,9 +77,9 @@ private static string RemoveLineEndings(string input)
7777

7878
private class DummyDbContext : DbContext
7979
{
80-
private readonly ICollationData defaultCollation;
80+
private readonly Collation defaultCollation;
8181

82-
public DummyDbContext(DbConnection connection, ICollationData defaultCollation = null)
82+
public DummyDbContext(DbConnection connection, Collation defaultCollation = null)
8383
: base(connection, false)
8484
{
8585
this.defaultCollation = defaultCollation;
@@ -97,9 +97,9 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
9797

9898
private class AssertInitializer : SqliteInitializerBase<DummyDbContext>
9999
{
100-
private readonly ICollationData defaultCollation;
100+
private readonly Collation defaultCollation;
101101

102-
public AssertInitializer(DbModelBuilder modelBuilder, ICollationData defaultCollation)
102+
public AssertInitializer(DbModelBuilder modelBuilder, Collation defaultCollation)
103103
: base(modelBuilder)
104104
{
105105
this.defaultCollation = defaultCollation;

SQLite.CodeFirst/Internal/Builder/ColumnStatementCollectionBuilder.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ internal class ColumnStatementCollectionBuilder : IStatementBuilder<ColumnStatem
1111
{
1212
private readonly IEnumerable<EdmProperty> properties;
1313
private readonly IEnumerable<EdmProperty> keyMembers;
14-
private readonly ICollationData defaultCollation;
14+
private readonly Collation defaultCollation;
1515

16-
public ColumnStatementCollectionBuilder(IEnumerable<EdmProperty> properties, IEnumerable<EdmProperty> keyMembers, ICollationData defaultCollation)
16+
public ColumnStatementCollectionBuilder(IEnumerable<EdmProperty> properties, IEnumerable<EdmProperty> keyMembers, Collation defaultCollation)
1717
{
1818
this.properties = properties;
1919
this.keyMembers = keyMembers;
@@ -75,18 +75,19 @@ private static void AddNullConstraintIfNecessary(EdmProperty property, ColumnSta
7575
}
7676
}
7777

78-
private static void AddCollationConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement, ICollationData defaultCollation)
78+
private static void AddCollationConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement, Collation defaultCollation)
7979
{
80-
ICollationData value = property.GetCustomAnnotation<CollateAttribute>();
81-
if (value == null && defaultCollation != null && property.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.String)
80+
if (property.PrimitiveType.PrimitiveTypeKind != PrimitiveTypeKind.String)
8281
{
83-
// Use default collation if one is given and the property is a string.
84-
value = defaultCollation;
82+
return;
8583
}
8684

85+
var collateAttribute = property.GetCustomAnnotation<CollateAttribute>();
86+
var value = collateAttribute == null ? defaultCollation : collateAttribute.CollationData;
87+
8788
if (value != null)
8889
{
89-
columnStatement.ColumnConstraints.Add(new CollateConstraint { CollationFunction = value.Collation, CustomCollationFunction = value.Function });
90+
columnStatement.ColumnConstraints.Add(new CollateConstraint { CollationFunction = value.CollationFunction, CustomCollationFunction = value.Function });
9091
}
9192
}
9293

SQLite.CodeFirst/Internal/Builder/CreateDatabaseStatementBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace SQLite.CodeFirst.Builder
99
internal class CreateDatabaseStatementBuilder : IStatementBuilder<CreateDatabaseStatement>
1010
{
1111
private readonly EdmModel edmModel;
12-
private readonly ICollationData defaultCollation;
12+
private readonly Collation defaultCollation;
1313

14-
public CreateDatabaseStatementBuilder(EdmModel edmModel, ICollationData defaultCollation)
14+
public CreateDatabaseStatementBuilder(EdmModel edmModel, Collation defaultCollation)
1515
{
1616
this.edmModel = edmModel;
1717
this.defaultCollation = defaultCollation;

SQLite.CodeFirst/Internal/Builder/CreateTableStatementBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ internal class CreateTableStatementBuilder : IStatementBuilder<CreateTableStatem
1212
{
1313
private readonly EntitySet entitySet;
1414
private readonly AssociationTypeContainer associationTypeContainer;
15-
private readonly ICollationData defaultCollation;
15+
private readonly Collation defaultCollation;
1616

17-
public CreateTableStatementBuilder(EntitySet entitySet, AssociationTypeContainer associationTypeContainer, ICollationData defaultCollation)
17+
public CreateTableStatementBuilder(EntitySet entitySet, AssociationTypeContainer associationTypeContainer, Collation defaultCollation)
1818
{
1919
this.entitySet = entitySet;
2020
this.associationTypeContainer = associationTypeContainer;

SQLite.CodeFirst/Public/Attributes/CollateAttribute.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,18 @@ namespace SQLite.CodeFirst
88
/// It is possible to specify a custom collating function. Set <see cref="CollationFunction"/> to <see cref="CollationFunction.Custom"/> and specify the name using the function parameter.
99
/// </summary>
1010
[AttributeUsage(AttributeTargets.Property)]
11-
public sealed class CollateAttribute : Attribute, ICollationData
11+
public sealed class CollateAttribute : Attribute
1212
{
1313
public CollateAttribute()
14+
: this(CollationFunction.None)
1415
{
15-
Collation = CollationFunction.None;
1616
}
1717

1818
public CollateAttribute(CollationFunction collation)
19+
: this(collation, null)
1920
{
20-
if (collation == CollationFunction.Custom)
21-
{
22-
throw new ArgumentException("If the collation is set to CollationFunction.Custom a function must be specified.", nameof(collation));
23-
}
24-
25-
Collation = collation;
2621
}
22+
2723
public CollateAttribute(CollationFunction collation, string function)
2824
{
2925
if (collation != CollationFunction.Custom && !string.IsNullOrEmpty(function))
@@ -38,6 +34,7 @@ public CollateAttribute(CollationFunction collation, string function)
3834

3935
Collation = collation;
4036
Function = function;
37+
CollationData = new Collation() { CollationFunction = collation, Function = function };
4138
}
4239

4340
public CollationFunction Collation { get; }
@@ -46,5 +43,7 @@ public CollateAttribute(CollationFunction collation, string function)
4643
/// The name of the custom collating function to use (CollationFunction.Custom).
4744
/// </summary>
4845
public string Function { get; }
46+
47+
public Collation CollationData { get; }
4948
}
5049
}

SQLite.CodeFirst/Public/Attributes/CollationData.cs

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

SQLite.CodeFirst/Public/Attributes/ICollationData.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace SQLite.CodeFirst
2+
{
3+
/// <summary>
4+
/// This class can be used to specify the default collation for the database. Explicit Collate attributes will take precendence.
5+
/// When SQLite compares two strings, it uses a collating sequence or collating function (two words for the same thing)
6+
/// to determine which string is greater or if the two strings are equal. SQLite has three built-in collating functions (see <see cref="CollationFunction"/>).
7+
/// Set <see cref="CollationFunction"/> to <see cref="CollationFunction.Custom"/> and specify the name using the function parameter.
8+
/// </summary>
9+
public class Collation
10+
{
11+
public CollationFunction CollationFunction { get; set; }
12+
13+
/// <summary>
14+
/// The name of the custom collating function to use (CollationFunction.Custom).
15+
/// </summary>
16+
public string Function { get; set; }
17+
}
18+
}

SQLite.CodeFirst/Public/Attributes/CollationFunction.cs renamed to SQLite.CodeFirst/Public/CollationFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
/// <summary>
44
/// The collation function to use for this column.
5-
/// Is used together with the <see cref="CollateAttribute" />.
5+
/// Is used together with the <see cref="CollateAttribute" />, and when setting a default collation for the database.
66
/// </summary>
77
public enum CollationFunction
88
{

SQLite.CodeFirst/Public/DbInitializers/SqliteInitializerBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ namespace SQLite.CodeFirst
2424
public abstract class SqliteInitializerBase<TContext> : IDatabaseInitializer<TContext>
2525
where TContext : DbContext
2626
{
27-
protected SqliteInitializerBase(DbModelBuilder modelBuilder)
27+
protected SqliteInitializerBase(DbModelBuilder modelBuilder, Collation defaultCollation = null)
2828
{
2929
ModelBuilder = modelBuilder ?? throw new ArgumentNullException(nameof(modelBuilder));
30+
DefaultCollation = defaultCollation;
3031

3132
// This convention will crash the SQLite Provider before "InitializeDatabase" gets called.
3233
// See https://github.com/msallin/SQLiteCodeFirst/issues/7 for details.
@@ -55,7 +56,7 @@ protected SqliteInitializerBase(DbModelBuilder modelBuilder)
5556
}
5657
}
5758

58-
public ICollationData DefaultCollation { get; set; }
59+
public Collation DefaultCollation { get; }
5960

6061
protected DbModelBuilder ModelBuilder { get; }
6162

0 commit comments

Comments
 (0)