Skip to content

Commit b8ef0de

Browse files
Merge pull request #312 from JocaPC/master
Sample that shows how to create Regex functions using CLR
2 parents 09de300 + cd50154 commit b8ef0de

File tree

8 files changed

+305
-0
lines changed

8 files changed

+305
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.cproj.user
2+
.vs/*
3+
.vscode/*
4+
bin/*
5+
obj/*
6+
*.log
7+
Properties/PublishProfiles/*
8+
SqlClrRegEx.sql
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SqlClrRegEx")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SqlClrRegEx")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("265e0bc3-ad5f-44f3-bb17-c61f77b9847f")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Implementing Regular Expressions in SQL Server using CLR UDF
2+
SQL Database don't have built-in support for regular expressions, so the only workaround is to use Regular Expressions that exist in .Net framework and expose them as T-SQL functions.
3+
This code sample demonstrates how to create CLR User-Defined functions that expose regular expression functionalities that exist in .Net framework.
4+
5+
### Contents
6+
7+
[About this sample](#about-this-sample)<br/>
8+
[Build the CLR/RegEx functions](#build-functions)<br/>
9+
[Add RegEx functions to your SQL database](#add-functions)<br/>
10+
[Test the functions](#test)<br/>
11+
[Disclaimers](#disclaimers)<br/>
12+
13+
<a name=about-this-sample></a>
14+
15+
## About this sample
16+
1. **Applies to:** SQL Server 2005+ Enterprise / Developer / Evaluation Edition
17+
2. **Key features:**
18+
- CLR
19+
3. **Programming Language:** .NET C#
20+
4. **Author:** Jovan Popovic [jovanpop-msft]
21+
22+
<a name=build-functions></a>
23+
24+
## Build the CLR/RegEx functions
25+
26+
1. Download the source code and open the solution using Visual Studio.
27+
2. Change the password in .pfk file and rebuild the solution in Retail mode.
28+
3. Open and save SqlClrRegEx.tt to generate output T-SQL file that will contain script that inserts .dll file with the Regex functions, and exposes them as T-SQL/CLR functions.
29+
30+
<a name=add-functions></a>
31+
## Add RegEx functions to your SQL database
32+
33+
File SqlClrRegEx.sql contains the code that will import functions into SQL Database.
34+
35+
If you have not added CLR assemblies in your database, you should use the following script to enable CLR:
36+
```
37+
sp_configure @configname=clr_enabled, @configvalue=1
38+
GO
39+
RECONFIGURE
40+
GO
41+
```
42+
43+
Once you enable CLR, you can use the T-SQL script to add the regex functions. The script depends on the location where you have built the project, and might look like:
44+
```
45+
--Create the assembly
46+
CREATE ASSEMBLY SqlClrRegEx FROM 'D:\GitHub\sql-server-samples\samples\features\sql-clr\RegEx\bin\Release\SqlClrRegEx.dll' WITH PERMISSION_SET = SAFE
47+
GO
48+
49+
CREATE SCHEMA REGEX;
50+
GO
51+
52+
--Create the functions
53+
CREATE FUNCTION REGEX.MATCH (@src NVARCHAR(MAX), @regex NVARCHAR(4000))
54+
RETURNS BIT
55+
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledMatch
56+
GO
57+
CREATE FUNCTION REGEX.SUBSTRING (@src NVARCHAR(MAX), @regex NVARCHAR(4000))
58+
RETURNS NVARCHAR(4000)
59+
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledSubstring
60+
GO
61+
CREATE FUNCTION REGEX.REPLACE (@src NVARCHAR(MAX), @regex NVARCHAR(MAX), @value NVARCHAR(4000))
62+
RETURNS NVARCHAR(MAX)
63+
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledReplace
64+
GO
65+
```
66+
67+
This code will import assembly in SQL Database and add three functions that provide RegEx functionalities.
68+
69+
<a name=test></a>
70+
71+
## Test the functions
72+
73+
Once you create the assembly and expose the functions, you can use regular expression functionalities in T-SQL code:
74+
75+
```
76+
IF( REGEX.MATCH('tst123test', '[0-9]+') = 1 )
77+
SELECT REGEX.SUBSTRING('tst123test', '[0-9]+'), REGEX.REPLACE('tst123test', '[0-9]+', 'XXX')
78+
```
79+
80+
<a name=disclaimers></a>
81+
82+
## Disclaimers
83+
The code included in this sample is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this sample.
84+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.SqlServer.Server;
2+
using System.Text.RegularExpressions;
3+
4+
/// <summary>
5+
/// https://blogs.msdn.microsoft.com/sqlclr/2005/06/29/working-with-regular-expressions/
6+
/// </summary>
7+
public partial class RegEx
8+
{
9+
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
10+
public static bool Match(string source, string pattern)
11+
{
12+
Regex r1 = new Regex(pattern);
13+
return r1.Match(source).Success;
14+
}
15+
16+
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
17+
public static bool CompiledMatch(string source, string pattern)
18+
{
19+
return Regex.Match(source, pattern, RegexOptions.Compiled).Success;
20+
}
21+
22+
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
23+
public static string Substring(string source, string pattern)
24+
{
25+
Regex r1 = new Regex(pattern);
26+
return r1.Match(source).Value;
27+
}
28+
29+
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
30+
public static string CompiledSubstring(string source, string pattern)
31+
{
32+
return Regex.Match(source, pattern, RegexOptions.Compiled).Value;
33+
}
34+
35+
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
36+
public static string Replace(string source, string pattern, string value)
37+
{
38+
return Regex.Replace(source, pattern, value);
39+
}
40+
41+
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
42+
public static string CompiledReplace(string source, string pattern, string value)
43+
{
44+
return Regex.Replace(source, pattern, value, RegexOptions.Compiled);
45+
}
46+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SqlClrRegEx</RootNamespace>
11+
<AssemblyName>SqlClrRegEx</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<PropertyGroup>
33+
<SignAssembly>true</SignAssembly>
34+
</PropertyGroup>
35+
<PropertyGroup>
36+
<AssemblyOriginatorKeyFile>SqlClrRegEx.pfx</AssemblyOriginatorKeyFile>
37+
</PropertyGroup>
38+
<ItemGroup>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Net.Http" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="Properties\AssemblyInfo.cs" />
50+
<Content Include="SqlClrRegEx.sql">
51+
<AutoGen>True</AutoGen>
52+
<DesignTime>True</DesignTime>
53+
<DependentUpon>SqlClrRegEx.tt</DependentUpon>
54+
</Content>
55+
<Content Include="SqlClrRegEx.tt">
56+
<Generator>TextTemplatingFileGenerator</Generator>
57+
<LastGenOutput>SqlClrRegEx.sql</LastGenOutput>
58+
</Content>
59+
<Compile Include="RegEx.cs" />
60+
</ItemGroup>
61+
<ItemGroup>
62+
<None Include="SqlClrRegEx.pfx" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
66+
</ItemGroup>
67+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
68+
</Project>
1.72 KB
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26730.16
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlClrRegEx", "SqlClrRegEx.csproj", "{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {54C7E749-77A3-460D-A89A-5AB7943A1F15}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<#@output extension=".sql"#>
2+
<#@ template language="C#" hostspecific="True" #>
3+
4+
--Drop the functions if they already exist
5+
DROP FUNCTION IF EXISTS REGEX.MATCH
6+
GO
7+
DROP FUNCTION IF EXISTS REGEX.SUBSTRING
8+
GO
9+
DROP FUNCTION IF EXISTS REGEX.REPLACE
10+
GO
11+
12+
DROP SCHEMA IF EXISTS REGEX;
13+
GO
14+
15+
--Drop the assembly if it already exists
16+
DROP ASSEMBLY IF EXISTS SqlClrRegEx
17+
GO
18+
19+
--Create the assembly
20+
CREATE ASSEMBLY SqlClrRegEx FROM '<#= this.Host.ResolvePath("bin\\Release\\SqlClrRegEx.dll") #>' WITH PERMISSION_SET = SAFE
21+
GO
22+
23+
CREATE SCHEMA REGEX;
24+
GO
25+
26+
--Create the functions
27+
CREATE FUNCTION REGEX.MATCH (@src NVARCHAR(MAX), @regex NVARCHAR(4000))
28+
RETURNS BIT
29+
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledMatch
30+
GO
31+
CREATE FUNCTION REGEX.SUBSTRING (@src NVARCHAR(MAX), @regex NVARCHAR(4000))
32+
RETURNS NVARCHAR(4000)
33+
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledSubstring
34+
GO
35+
CREATE FUNCTION REGEX.REPLACE (@src NVARCHAR(MAX), @regex NVARCHAR(MAX), @value NVARCHAR(4000))
36+
RETURNS NVARCHAR(MAX)
37+
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledReplace
38+
GO

0 commit comments

Comments
 (0)