You write data validation code in your user interfaces to provide a good user experience. Then you write similar validation code in your services because other clients may not implement those validations in the future. Then you write the same data validations again in the database, well, let's face it, because DBAs are justifiably paranoid about the quality of their data. During this talk, Kevin Hazzard will show you how implement your data validations in a way that they can be shared across all the tiers of your enterprise applications.
2. Problem Statement Getting your data validation synchronized across all the tiers of an application isn't easy Storing metadata about validations that need to occur as pure data can be less than ideal
3. One Possible Solution Extend SQL Server to support Regular Expression matching Store validation metadata as a set of extended properties in SQL Server Expose your validation metadata as a service
4. What are extended properties? SQL_VARIANT metadata that can be applied to many SQL database objects Examples Add a DATETIME stamp or integer serial number to a table definition to mark its schema version Add Spanish and English description properties to a view as documentation for consumers Add a tag to a public key certificate to record where the private key is escrowed and by whom Add a regular expression to a table's column definition to record the pattern (input mask) that should be used to validate the data
40. Listing Extended Properties for a Column SELECT objtype, objname, name, value FROM fn_listextendedproperty( NULL, 'schema', 'SCHMNAME', 'table', 'TBLNAME', 'column', default );
41. Querying sys.properties SELECT ep.major_id, ep.minor_id, t.name AS [Table Name], ep.name AS [ExtProp Name], c.name AS [Column Name], ep.value AS [Extended Property] FROM sys.extended_properties AS ep INNER JOIN sys.tables AS t ON ep.major_id = t.object_id INNER JOIN sys.columns AS c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id WHERE ep.class = 1;
43. Using Regular Expression in SQL Server to Validate Data We can extend SQL Server by adding functions written in C# or Visual Basic The .NET Framework has a great Regular Expressions implementation – let's use it! public class UserDefinedFunctions { [SqlFunction(IsDeterministic = true)] public static SqlBooleanRegexIsMatch( SqlString pattern, SqlString input) { varrex = new Regex(pattern.Value); return rex.IsMatch(input.Value); } };
44. Don't Forget This You must enable the SQL CLR It's disabled by default sp_configure'clr enabled', 1RECONFIGURE WITH OVERRIDEGO
45. Demonsration 2 Adding a SQL CLR User-Defined Function for Regular Expression Matching
46. Pulling It All Together What if we could save regular expressions as metadata to validate our OLTP data? What if we had conventions for storing and accessing that metadata? We could write helper functions that took full advantage of those conventions We could write other kinds of assertions and constraints in the database that depended on them, too We could expose that metadata as a service and use it in multiple application tiers
47. Demonstrations 3 & 4 Baking Validation Into SQL Using Metadata and Constraints Implementing a WCF Service to Expose Validation Metadata
48. For More Information Kevin Hazzard's Bloghttp://tinyurl.com/KevinHazzard Kevin Hazzard'sSlideshare Sitehttp://slideshare.net/wkhazzard Kevin Hazzard'sSpeakerRate Sitehttp://speakerrate.com/wkhazzard Phil Factor's RegEx Functions for SQLCLRhttp://tinyurl.com/dmsmm2