Vlado
Vlado

Reputation: 71

Parsing SQL Check Constraint

I'm reading column constraints from data base.
for example:

(substring([name],(1),(1))='P' OR len([name])>(2) AND len([name])>(4) AMD (len(name)>5 OR len(name)<4))

The idea is that every open bracket must be closed.
The brackets around the numbers and around the functions are automatically inserted by the SQL management studio when we are adding new Check Constraints. Can anyone suggest how to parse these strings?

My goal is to convert the Column Check Constraints into C# code so that I can build Entity validators. All database tables are mapped into Entities classes.

Upvotes: 2

Views: 1033

Answers (1)

rsenna
rsenna

Reputation: 11964

Simple answer: this will NOT be easy. At least not if you're trying to do it right...

You are talking about defining a DSL for defining constraints (which is "coincidentally" exactly like TSQL). IMHO you need to use some kind of parser tool in order to do that. Maybe you should consider using Irony.

It is an interesting idea though. I can see the advantages of it - I just don't think it will be easier than simply using the DB as a data repository (without any kind of data validation - maybe only foreign keys constraints) and focusing on defining all business rules into the .NET server-side...

Upvotes: 2

Related Questions