Skip to content

Commit 90a21c2

Browse files
authored
Merge pull request #105 from msallin/#96
#96 support custom collation.
2 parents 4c9a70b + 8090504 commit 90a21c2

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

SQLite.CodeFirst/Internal/Builder/ColumnStatementCollectionBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private static void AddCollationConstraintIfNecessary(EdmProperty property, Colu
7878
var value = property.GetCustomAnnotation<CollateAttribute>();
7979
if (value != null)
8080
{
81-
columnStatement.ColumnConstraints.Add(new CollateConstraint { CollationFunction = value.Collation });
81+
columnStatement.ColumnConstraints.Add(new CollateConstraint { CollationFunction = value.Collation, CustomCollationFunction = value.Function });
8282
}
8383
}
8484

SQLite.CodeFirst/Internal/Statement/ColumnConstraint/CollateConstraint.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ internal class CollateConstraint : IColumnConstraint
88

99
public CollationFunction CollationFunction { get; set; }
1010

11+
public string CustomCollationFunction { get; set; }
12+
1113
public string CreateStatement()
1214
{
1315
if (CollationFunction == CollationFunction.None)
@@ -17,8 +19,9 @@ public string CreateStatement()
1719

1820
var sb = new StringBuilder(Template);
1921

20-
sb.Replace("{collation-name}", CollationFunction.ToString().ToUpperInvariant());
21-
22+
string name = CollationFunction == CollationFunction.Custom ? CustomCollationFunction : CollationFunction.ToString().ToUpperInvariant();
23+
sb.Replace("{collation-name}", name);
24+
2225
return sb.ToString().Trim();
2326
}
2427
}

SQLite.CodeFirst/Public/Attributes/CollateAttribute.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace SQLite.CodeFirst
55
/// <summary>
66
/// When SQLite compares two strings, it uses a collating sequence or collating function (two words for the same thing)
77
/// to determine which string is greater or if the two strings are equal. SQLite has three built-in collating functions (see <see cref="CollationFunction"/>).
8+
/// 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.
89
/// </summary>
910
[AttributeUsage(AttributeTargets.Property)]
1011
public sealed class CollateAttribute : Attribute
@@ -16,9 +17,34 @@ public CollateAttribute()
1617

1718
public CollateAttribute(CollationFunction collation)
1819
{
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+
1925
Collation = collation;
2026
}
27+
public CollateAttribute(CollationFunction collation, string function)
28+
{
29+
if (collation != CollationFunction.Custom && !string.IsNullOrEmpty(function))
30+
{
31+
throw new ArgumentException("If the collation is not set to CollationFunction.Custom a function must not be specified.", nameof(function));
32+
}
33+
34+
if (collation == CollationFunction.Custom && string.IsNullOrEmpty(function))
35+
{
36+
throw new ArgumentException("If the collation is set to CollationFunction.Custom a function must be specified.", nameof(function));
37+
}
38+
39+
Collation = collation;
40+
Function = function;
41+
}
42+
43+
public CollationFunction Collation { get; }
2144

22-
public CollationFunction Collation { get; private set; }
45+
/// <summary>
46+
/// The name of the custom collating function to use (CollationFunction.Custom).
47+
/// </summary>
48+
public string Function { get; }
2349
}
2450
}

SQLite.CodeFirst/Public/Attributes/CollationFunction.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public enum CollationFunction
99
None,
1010

1111
/// <summary>
12-
/// Compares string data using memcmp(), regardless of text encoding.
13-
/// </summary>
12+
/// The same as binary, except that trailing space characters are ignored.
13+
/// </summary>
1414
RTrim,
1515

1616
/// <summary>
@@ -21,8 +21,13 @@ public enum CollationFunction
2121
NoCase,
2222

2323
/// <summary>
24-
/// The same as binary, except that trailing space characters are ignored.
24+
/// Compares string data using memcmp(), regardless of text encoding.
25+
/// </summary>
26+
Binary,
27+
28+
/// <summary>
29+
/// An application can register additional collating functions using the sqlite3_create_collation() interface.
2530
/// </summary>
26-
Binary
31+
Custom
2732
}
2833
}

0 commit comments

Comments
 (0)