Skip to main content
Commonmark migration
Source Link

Most of what I see in your example is {column value} should be greater than or equal to 0. You can create a simple parser with [regular expressions][1];regular expressions; the following [regular expression][1]regular expression would capture it:

(See [Regular Expression Language - Quick Reference | Microsoft Docs][2]Regular Expression Language - Quick Reference | Microsoft Docs)

After a [Match()][3]Match() on DEFINITION, you can use [.Groups][4][[groupname]][5].Groups[groupname] on the returned [Match][6]Match. Use [Success][7]Success property to check whether a group was matched. Use [Value][8]Value to get the matched value as a string.

These can be used to build [Expressions][9]Expressions:

Note: this is whiteboard quality code; I currently don't have the equipment to compile or run the code. [1]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.7.2 "Regex Class (System.Text.RegularExpressions) | Microsoft Docs" [2]: https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference?view=netframework-4.7.2 "Regular Expression Language - Quick Reference | Microsoft Docs" [3]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match?view=netframework-4.7.2#System_Text_RegularExpressions_Regex_Match_System_String_ "Regex.Match Method (System.Text.RegularExpressions) | Microsoft Docs" [4]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match.groups?view=netframework-4.7.2#System_Text_RegularExpressions_Match_Groups "Match.Groups Property (System.Text.RegularExpressions) | Microsoft Docs" [5]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.groupcollection.item?view=netframework-4.7.2#System_Text_RegularExpressions_GroupCollection_Item_System_String_ "GroupCollection.Item[String] Property (System.Text.RegularExpressions) | Microsoft Docs" [6]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match?view=netframework-4.7.2 "Match Class (System.Text.RegularExpressions) | Microsoft Docs" [7]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.group.success?view=netframework-4.7.2#System_Text_RegularExpressions_Group_Success "Group.Success Property (System.Text.RegularExpressions) | Microsoft Docs" [8]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.capture.value?view=netframework-4.7.2#System_Text_RegularExpressions_Capture_Value "Capture.Value Property (System.Text.RegularExpressions) | Microsoft Docs" [9]: https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression?view=netframework-4.7.2 "Expression Class (System.Linq.Expressions) | Microsoft Docs"

Most of what I see in your example is {column value} should be greater than or equal to 0. You can create a simple parser with [regular expressions][1]; the following [regular expression][1] would capture it:

(See [Regular Expression Language - Quick Reference | Microsoft Docs][2])

After a [Match()][3] on DEFINITION, you can use [.Groups][4][[groupname]][5] on the returned [Match][6]. Use [Success][7] property to check whether a group was matched. Use [Value][8] to get the matched value as a string.

These can be used to build [Expressions][9]:

Note: this is whiteboard quality code; I currently don't have the equipment to compile or run the code. [1]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.7.2 "Regex Class (System.Text.RegularExpressions) | Microsoft Docs" [2]: https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference?view=netframework-4.7.2 "Regular Expression Language - Quick Reference | Microsoft Docs" [3]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match?view=netframework-4.7.2#System_Text_RegularExpressions_Regex_Match_System_String_ "Regex.Match Method (System.Text.RegularExpressions) | Microsoft Docs" [4]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match.groups?view=netframework-4.7.2#System_Text_RegularExpressions_Match_Groups "Match.Groups Property (System.Text.RegularExpressions) | Microsoft Docs" [5]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.groupcollection.item?view=netframework-4.7.2#System_Text_RegularExpressions_GroupCollection_Item_System_String_ "GroupCollection.Item[String] Property (System.Text.RegularExpressions) | Microsoft Docs" [6]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match?view=netframework-4.7.2 "Match Class (System.Text.RegularExpressions) | Microsoft Docs" [7]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.group.success?view=netframework-4.7.2#System_Text_RegularExpressions_Group_Success "Group.Success Property (System.Text.RegularExpressions) | Microsoft Docs" [8]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.capture.value?view=netframework-4.7.2#System_Text_RegularExpressions_Capture_Value "Capture.Value Property (System.Text.RegularExpressions) | Microsoft Docs" [9]: https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression?view=netframework-4.7.2 "Expression Class (System.Linq.Expressions) | Microsoft Docs"

Most of what I see in your example is {column value} should be greater than or equal to 0. You can create a simple parser with regular expressions; the following regular expression would capture it:

(See Regular Expression Language - Quick Reference | Microsoft Docs)

After a Match() on DEFINITION, you can use .Groups[groupname] on the returned Match. Use Success property to check whether a group was matched. Use Value to get the matched value as a string.

These can be used to build Expressions:

Note: this is whiteboard quality code; I currently don't have the equipment to compile or run the code.

Source Link

You may end up with needing to write a parser for the human readable SQL Server constraint syntax.

A possible shortcut is to parse only the constructs you have encountered and update your program when someone introduces a more complex constraint.

Most of what I see in your example is {column value} should be greater than or equal to 0. You can create a simple parser with [regular expressions][1]; the following [regular expression][1] would capture it:

(?<greaterEqualConstraint>\(\[(?<columnName>\w+)\]\>\=\((?<value>\d+)\))

(See [Regular Expression Language - Quick Reference | Microsoft Docs][2])

After a [Match()][3] on DEFINITION, you can use [.Groups][4][[groupname]][5] on the returned [Match][6]. Use [Success][7] property to check whether a group was matched. Use [Value][8] to get the matched value as a string.

These can be used to build [Expressions][9]:

Expression<Func<int, bool>> Parse(Match constraintDefinition)
{
    if (constraintDefinition.Groups["greaterEqualConstraint"].Success &&
        constraintDefinition.Groups["columnName"].Success &&
        constraintDefinition.Groups["value"].Success)
    {
        ParameterExpression columnExpr = Expression.Parameter(
            typeof(int) /* TODO use the database schema to set the correct type */,
            constraintDefinition.Groups["columnName"].Value);
        return Expression.Lambda<Func<int,bool> /* TODO again this might depend on the schema */>(
            Expression.GreaterThanOrEqual(
                columnExpr,
                Expression.ConstantExpression(
                    int.Parse(constraintDefinition.Groups["value"].Value))),
            tailCall: false,
            parameters: new ParameterExpression[] { columnExpr });
    }
    else
    {
        throw new NotImplementedException($"Marco Siffert, database schema contains a not implemented kind of constraint: {constraintDefinition}");
    }
}

Note: this is whiteboard quality code; I currently don't have the equipment to compile or run the code. [1]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.7.2 "Regex Class (System.Text.RegularExpressions) | Microsoft Docs" [2]: https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference?view=netframework-4.7.2 "Regular Expression Language - Quick Reference | Microsoft Docs" [3]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match?view=netframework-4.7.2#System_Text_RegularExpressions_Regex_Match_System_String_ "Regex.Match Method (System.Text.RegularExpressions) | Microsoft Docs" [4]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match.groups?view=netframework-4.7.2#System_Text_RegularExpressions_Match_Groups "Match.Groups Property (System.Text.RegularExpressions) | Microsoft Docs" [5]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.groupcollection.item?view=netframework-4.7.2#System_Text_RegularExpressions_GroupCollection_Item_System_String_ "GroupCollection.Item[String] Property (System.Text.RegularExpressions) | Microsoft Docs" [6]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match?view=netframework-4.7.2 "Match Class (System.Text.RegularExpressions) | Microsoft Docs" [7]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.group.success?view=netframework-4.7.2#System_Text_RegularExpressions_Group_Success "Group.Success Property (System.Text.RegularExpressions) | Microsoft Docs" [8]: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.capture.value?view=netframework-4.7.2#System_Text_RegularExpressions_Capture_Value "Capture.Value Property (System.Text.RegularExpressions) | Microsoft Docs" [9]: https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression?view=netframework-4.7.2 "Expression Class (System.Linq.Expressions) | Microsoft Docs"