-
-
Notifications
You must be signed in to change notification settings - Fork 121
Added support for a default collation for all tables. #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thank you for your contribution. However, you write "The API for the default collation should probably look different, and the implementation could no doubt be stronger as well." Is there anything specific which bothers you and should be discussed? |
|
I think it's probably okay, but I threw it together pretty quickly, without considering many alternatives. Perhaps the biggest issue would be a lack of discoverability. Most people will probably use this library by doing Database.SetInitializer(new SqliteCreateDatabaseIfNotExists(modelBuilder));Now you have to know to do Database.SetInitializer(new SqliteCreateDatabaseIfNotExists(modelBuilder) { DefaultCollation = ... });Maybe it would be better if the collation data came as an optional parameter to the SqliteCreateDatabaseIfNotExists constructor? Or both? The other thing is the ICollationData interface. I added it so CollationAttribute and CollationData could be handled with the same code internally, but there's no reason to pass a CollationAttribute object around manually, so maybe it would be better to just use CollationData directly? The interface could still be there, but it would be more obvious how to pass the input data. Also, maybe Or maybe all of this is just splitting hairs, but you're probably in a better position to say what's right for your library. 😃 |
|
Or simply |
|
Thank you for your explanations I had a more granular look at it.
Well, I didn't use this library for years. Chances are high that others known it better than I do ;) I suggest doing the following:
Let me know what you think. |
|
Sounds good. 👍 I'll make the changes soon. |
- 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.
|
A few notes:
if (property.PrimitiveType.PrimitiveTypeKind != PrimitiveTypeKind.String)
{
return;
}
var collateAttribute = property.GetCustomAnnotation<CollateAttribute>();
var value = collateAttribute == null ? defaultCollation : collateAttribute.CollationData;
if (value != null)
{
columnStatement.ColumnConstraints.Add(new CollateConstraint { CollationFunction = value.CollationFunction, CustomCollationFunction = value.Function });
}If it's necessary to maintain the existing behavior, the function could be written like this instead: Collation value = null;
var collateAttribute = property.GetCustomAnnotation<CollateAttribute>();
if (collateAttribute != null)
{
value = collateAttribute.CollationData;
}
else if (property.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.String)
{
value = defaultCollation;
}
if (value != null)
{
columnStatement.ColumnConstraints.Add(new CollateConstraint { CollationFunction = value.CollationFunction, CustomCollationFunction = value.Function });
}The first version would be a breaking change, if adding CollateAttribute to non-strings is ever valid. But if that's the case I may also have to reconsider how the default collation works. A third option could be to explicitly throw an exception if CollateAttribute has been added to a field where it doesn't make sense. |
SQLite.CodeFirst/Public/Collation.cs
Outdated
| /// <summary> | ||
| /// The name of the custom collating function to use (CollationFunction.Custom). | ||
| /// </summary> | ||
| public string Function { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it make sense to name this CustomFunction and the property CollationFunction -> Function as this is in a class with the name Collation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, agreed.
|
I suggest to move all code from the attribute to the collation class. Because currently one only gets the checks when using the attribute and not when using the class. Also remove all setters but only use the ctor, to guide the usage. Like this (I also renamed it according to my comment in the PR): To your question: |
|
You might add also an additional bullet point to the readme smth like:
|
- Changed CollateAttribute to use Collation internally. - Throw an exception when CollateAttribute has been used on a non-string property. - Added a line about default collations to the readme.
|
I made the changes you recommended. Minor points:
|
Thank you for the hint. I'm aware of it. In theory this should result in incrementing the major version. However, I don't think this is used from someone, hence I will increment the minor.
Fine for me if you don't. |
Added support for setting a default collation that applies by default to all string fields without a specific collation. CollationAttribute takes precedence.
This PR is a proof of concept. The API for the default collation should probably look different, and the implementation could no doubt be stronger as well.