mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
SQL CLR Regex utilities
Added sample that shows how to create CLR functions that expose .NEt Regular Expression classes.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
*.cproj.user
|
||||
.vs/*
|
||||
.vscode/*
|
||||
bin/*
|
||||
obj/*
|
||||
*.log
|
||||
Properties/PublishProfiles/*
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("SqlClrRegEx")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("SqlClrRegEx")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("265e0bc3-ad5f-44f3-bb17-c61f77b9847f")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,46 @@
|
||||
using Microsoft.SqlServer.Server;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
/// <summary>
|
||||
/// https://blogs.msdn.microsoft.com/sqlclr/2005/06/29/working-with-regular-expressions/
|
||||
/// </summary>
|
||||
public partial class RegEx
|
||||
{
|
||||
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
||||
public static bool Match(string source, string pattern)
|
||||
{
|
||||
Regex r1 = new Regex(pattern);
|
||||
return r1.Match(source).Success;
|
||||
}
|
||||
|
||||
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
||||
public static bool CompiledMatch(string source, string pattern)
|
||||
{
|
||||
return Regex.Match(source, pattern, RegexOptions.Compiled).Success;
|
||||
}
|
||||
|
||||
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
||||
public static string Substring(string source, string pattern)
|
||||
{
|
||||
Regex r1 = new Regex(pattern);
|
||||
return r1.Match(source).Value;
|
||||
}
|
||||
|
||||
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
||||
public static string CompiledSubstring(string source, string pattern)
|
||||
{
|
||||
return Regex.Match(source, pattern, RegexOptions.Compiled).Value;
|
||||
}
|
||||
|
||||
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
||||
public static string Replace(string source, string pattern, string value)
|
||||
{
|
||||
return Regex.Replace(source, pattern, value);
|
||||
}
|
||||
|
||||
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
||||
public static string CompiledReplace(string source, string pattern, string value)
|
||||
{
|
||||
return Regex.Replace(source, pattern, value, RegexOptions.Compiled);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>SqlClrRegEx</RootNamespace>
|
||||
<AssemblyName>SqlClrRegEx</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<AssemblyOriginatorKeyFile>SqlClrRegEx.pfx</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Content Include="SqlClrRegEx.sql">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>SqlClrRegEx.tt</DependentUpon>
|
||||
</Content>
|
||||
<Content Include="SqlClrRegEx.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>SqlClrRegEx.sql</LastGenOutput>
|
||||
</Content>
|
||||
<Compile Include="RegEx.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="SqlClrRegEx.pfx" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26730.16
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlClrRegEx", "SqlClrRegEx.csproj", "{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{265E0BC3-AD5F-44F3-BB17-C61F77B9847F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {54C7E749-77A3-460D-A89A-5AB7943A1F15}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,38 @@
|
||||
<#@output extension=".sql"#>
|
||||
<#@ template language="C#" hostspecific="True" #>
|
||||
|
||||
--Drop the functions if they already exist
|
||||
DROP FUNCTION IF EXISTS clr.REGEX_MATCH
|
||||
GO
|
||||
DROP FUNCTION IF EXISTS clr.REGEX_SUBSTRING
|
||||
GO
|
||||
DROP FUNCTION IF EXISTS clr.REGEX_REPLACE
|
||||
GO
|
||||
|
||||
DROP SCHEMA IF EXISTS clr;
|
||||
GO
|
||||
|
||||
--Drop the assembly if it already exists
|
||||
DROP ASSEMBLY IF EXISTS SqlClrRegEx
|
||||
GO
|
||||
|
||||
--Create the assembly
|
||||
CREATE ASSEMBLY SqlClrRegEx FROM '<#= this.Host.ResolvePath("bin\\Release\\SqlClrRegEx.dll") #>' WITH PERMISSION_SET = SAFE
|
||||
GO
|
||||
|
||||
CREATE SCHEMA clr;
|
||||
GO
|
||||
|
||||
--Create the functions
|
||||
CREATE FUNCTION clr.REGEX_MATCH (@src NVARCHAR(MAX), @regex NVARCHAR(MAX))
|
||||
RETURNS BIT
|
||||
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledMatch
|
||||
GO
|
||||
CREATE FUNCTION clr.REGEX_SUBSTRING (@src NVARCHAR(MAX), @regex NVARCHAR(MAX))
|
||||
RETURNS NVARCHAR(4000)
|
||||
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledSubstring
|
||||
GO
|
||||
CREATE FUNCTION clr.REGEX_REPLACE (@src NVARCHAR(MAX), @regex NVARCHAR(MAX), @value NVARCHAR(4000))
|
||||
RETURNS NVARCHAR(MAX)
|
||||
AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledReplace
|
||||
GO
|
||||
Reference in New Issue
Block a user