Skip to content

Commit c3004e2

Browse files
committed
SqlClr CURL
Implementation of CURL function using CLR assemblies hosted in SQL Server
1 parent 6c917e2 commit c3004e2

8 files changed

Lines changed: 445 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.cproj.user
2+
.vs/*
3+
.vscode/*
4+
bin/*
5+
obj/*
6+
*.log
7+
Properties/PublishProfiles/*
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.SqlServer.Server;
2+
using System;
3+
using System.Data.SqlTypes;
4+
using System.Net;
5+
6+
/// <summary>
7+
/// Provides CURL-like functionalities in T-SQL code.
8+
/// </summary>
9+
public partial class Curl
10+
{
11+
[SqlFunction]
12+
[return: SqlFacet(MaxSize = -1)]
13+
public static SqlChars Get(SqlChars H, SqlChars url)
14+
{
15+
var client = new WebClient();
16+
if (!H.IsNull)
17+
{
18+
var header = H.ToSqlString().Value;
19+
if (!string.IsNullOrWhiteSpace(header))
20+
client.Headers.Add(header);
21+
}
22+
return new SqlChars(
23+
client.DownloadString(
24+
Uri.EscapeUriString(url.ToSqlString().Value)
25+
).ToCharArray());
26+
}
27+
28+
[SqlProcedure]
29+
public static void Post(SqlChars H, SqlChars d, SqlChars url)
30+
{
31+
var client = new WebClient();
32+
if (!H.IsNull)
33+
{
34+
var header = H.ToSqlString().Value;
35+
if (!string.IsNullOrWhiteSpace(header))
36+
client.Headers.Add(header);
37+
}
38+
if(d.IsNull)
39+
throw new ArgumentException("You must specify data that will be sent to the endpoint", "@d");
40+
var response =
41+
client.UploadString(
42+
Uri.EscapeUriString(url.ToSqlString().Value),
43+
d.ToSqlString().Value
44+
);
45+
SqlContext.Pipe.Send(response);
46+
}
47+
};
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("SqlClrCurl")]
9+
[assembly: AssemblyDescription("Implementation of CURL command in SQL Server Database Engine")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("Sql Server GitHub Samples")]
13+
[assembly: AssemblyCopyright("Copyright © 2018")]
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")]

samples/features/sql-clr/Curl/README.md

Lines changed: 134 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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>SqlClr</RootNamespace>
11+
<AssemblyName>SqlClrCurl</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>SqlClrCurl.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="Curl.cs" />
50+
<Compile Include="Properties\AssemblyInfo.cs" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<None Include="README.md" />
54+
<None Include="SqlClrCurl.pfx" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<Content Include=".gitignore" />
61+
<Content Include="SqlClrCurl.sql">
62+
<AutoGen>True</AutoGen>
63+
<DesignTime>True</DesignTime>
64+
<DependentUpon>SqlClrCurl.tt</DependentUpon>
65+
</Content>
66+
<Content Include="SqlClrCurl.tt">
67+
<Generator>TextTemplatingFileGenerator</Generator>
68+
<LastGenOutput>SqlClrCurl.sql</LastGenOutput>
69+
</Content>
70+
</ItemGroup>
71+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
72+
</Project>
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}") = "SqlClrCurl", "SqlClrCurl.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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
-- Enable CLR
3+
sp_configure 'clr enabled', 1;
4+
RECONFIGURE WITH OVERRIDE;
5+
GO
6+
7+
--Drop the functions if they already exist
8+
DROP FUNCTION IF EXISTS CURL.XGET
9+
GO
10+
DROP PROCEDURE IF EXISTS CURL.XPOST
11+
GO
12+
13+
--Drop the schema if it already exists
14+
DROP SCHEMA IF EXISTS CURL;
15+
GO
16+
17+
--Drop "trusted assembly flag" if it is set
18+
19+
DECLARE @hash VARBINARY(64);
20+
21+
SELECT @hash = hash
22+
FROM sys.trusted_assemblies ta
23+
WHERE ta.description = N'SqlClrCurl'
24+
25+
EXEC sp_drop_trusted_assembly @hash;
26+
GO
27+
28+
--Drop the assembly if it already exists
29+
DROP ASSEMBLY IF EXISTS SqlClrCurl;
30+
GO
31+
32+
33+
DECLARE @hash VARBINARY(64);
34+
SELECT @hash = HASHBYTES('SHA2_512', BulkColumn)
35+
FROM OPENROWSET(BULK 'C:\GitHub\sql-server-samples\samples\features\sql-clr\Curl\bin\Release\SqlClrCurl.dll', SINGLE_BLOB) AS assembly_content
36+
37+
IF(@hash IS NOT NULL)
38+
EXEC sp_add_trusted_assembly @hash, N'SqlClrCurl'
39+
ELSE
40+
PRINT 'Cannot create hash for assembly!'
41+
GO
42+
43+
--Create the assembly
44+
CREATE ASSEMBLY SqlClrCurl
45+
FROM 'C:\GitHub\sql-server-samples\samples\features\sql-clr\Curl\bin\Release\SqlClrCurl.dll'
46+
WITH PERMISSION_SET = EXTERNAL_ACCESS;
47+
GO
48+
49+
--Create the schema where CURL modules will be placed.
50+
CREATE SCHEMA CURL;
51+
GO
52+
53+
--Create the function/procedure
54+
CREATE FUNCTION CURL.XGET (@H NVARCHAR(MAX), @url NVARCHAR(4000))
55+
RETURNS NVARCHAR(MAX)
56+
AS EXTERNAL NAME SqlClrCurl.Curl.Get;
57+
GO
58+
59+
CREATE PROCEDURE CURL.XPOST (@H NVARCHAR(MAX), @d NVARCHAR(MAX), @url NVARCHAR(4000))
60+
AS EXTERNAL NAME SqlClrCurl.Curl.Post;
61+
GO
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<#@output extension=".sql"#>
2+
<#@ template language="C#" hostspecific="True" #>
3+
4+
-- Enable CLR
5+
sp_configure 'clr enabled', 1;
6+
RECONFIGURE WITH OVERRIDE;
7+
GO
8+
9+
--Drop the functions if they already exist
10+
DROP FUNCTION IF EXISTS CURL.XGET
11+
GO
12+
DROP PROCEDURE IF EXISTS CURL.XPOST
13+
GO
14+
15+
--Drop the schema if it already exists
16+
DROP SCHEMA IF EXISTS CURL;
17+
GO
18+
19+
--Drop "trusted assembly flag" if it is set
20+
21+
DECLARE @hash VARBINARY(64);
22+
23+
SELECT @hash = hash
24+
FROM sys.trusted_assemblies ta
25+
WHERE ta.description = N'SqlClrCurl'
26+
27+
EXEC sp_drop_trusted_assembly @hash;
28+
GO
29+
30+
--Drop the assembly if it already exists
31+
DROP ASSEMBLY IF EXISTS SqlClrCurl;
32+
GO
33+
34+
35+
DECLARE @hash VARBINARY(64);
36+
SELECT @hash = HASHBYTES('SHA2_512', BulkColumn)
37+
FROM OPENROWSET(BULK '<#= this.Host.ResolvePath("bin\\Release\\SqlClrCurl.dll") #>', SINGLE_BLOB) AS assembly_content
38+
39+
IF(@hash IS NOT NULL)
40+
EXEC sp_add_trusted_assembly @hash, N'SqlClrCurl'
41+
ELSE
42+
PRINT 'Cannot create hash for assembly!'
43+
GO
44+
45+
--Create the assembly
46+
CREATE ASSEMBLY SqlClrCurl
47+
FROM '<#= this.Host.ResolvePath("bin\\Release\\SqlClrCurl.dll") #>'
48+
WITH PERMISSION_SET = EXTERNAL_ACCESS;
49+
GO
50+
51+
--Create the schema where CURL modules will be placed.
52+
CREATE SCHEMA CURL;
53+
GO
54+
55+
--Create the function/procedure
56+
CREATE FUNCTION CURL.XGET (@H NVARCHAR(MAX), @url NVARCHAR(4000))
57+
RETURNS NVARCHAR(MAX)
58+
AS EXTERNAL NAME SqlClrCurl.Curl.Get;
59+
GO
60+
61+
CREATE PROCEDURE CURL.XPOST (@H NVARCHAR(MAX), @d NVARCHAR(MAX), @url NVARCHAR(4000))
62+
AS EXTERNAL NAME SqlClrCurl.Curl.Post;
63+
GO

0 commit comments

Comments
 (0)