SqlClr CURL

Implementation of CURL function using CLR assemblies hosted in SQL Server
This commit is contained in:
Jovan Popovic
2018-04-16 13:16:33 +02:00
parent 6c917e2766
commit c3004e2bd5
8 changed files with 445 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
*.cproj.user
.vs/*
.vscode/*
bin/*
obj/*
*.log
Properties/PublishProfiles/*
+47
View File
@@ -0,0 +1,47 @@
using Microsoft.SqlServer.Server;
using System;
using System.Data.SqlTypes;
using System.Net;
/// <summary>
/// Provides CURL-like functionalities in T-SQL code.
/// </summary>
public partial class Curl
{
[SqlFunction]
[return: SqlFacet(MaxSize = -1)]
public static SqlChars Get(SqlChars H, SqlChars url)
{
var client = new WebClient();
if (!H.IsNull)
{
var header = H.ToSqlString().Value;
if (!string.IsNullOrWhiteSpace(header))
client.Headers.Add(header);
}
return new SqlChars(
client.DownloadString(
Uri.EscapeUriString(url.ToSqlString().Value)
).ToCharArray());
}
[SqlProcedure]
public static void Post(SqlChars H, SqlChars d, SqlChars url)
{
var client = new WebClient();
if (!H.IsNull)
{
var header = H.ToSqlString().Value;
if (!string.IsNullOrWhiteSpace(header))
client.Headers.Add(header);
}
if(d.IsNull)
throw new ArgumentException("You must specify data that will be sent to the endpoint", "@d");
var response =
client.UploadString(
Uri.EscapeUriString(url.ToSqlString().Value),
d.ToSqlString().Value
);
SqlContext.Pipe.Send(response);
}
};
@@ -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("SqlClrCurl")]
[assembly: AssemblyDescription("Implementation of CURL command in SQL Server Database Engine")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Sql Server GitHub Samples")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[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")]
File diff suppressed because one or more lines are too long
@@ -0,0 +1,72 @@
<?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>SqlClr</RootNamespace>
<AssemblyName>SqlClrCurl</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>SqlClrCurl.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="Curl.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="README.md" />
<None Include="SqlClrCurl.pfx" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<Content Include=".gitignore" />
<Content Include="SqlClrCurl.sql">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>SqlClrCurl.tt</DependentUpon>
</Content>
<Content Include="SqlClrCurl.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>SqlClrCurl.sql</LastGenOutput>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
@@ -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}") = "SqlClrCurl", "SqlClrCurl.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,61 @@
-- Enable CLR
sp_configure 'clr enabled', 1;
RECONFIGURE WITH OVERRIDE;
GO
--Drop the functions if they already exist
DROP FUNCTION IF EXISTS CURL.XGET
GO
DROP PROCEDURE IF EXISTS CURL.XPOST
GO
--Drop the schema if it already exists
DROP SCHEMA IF EXISTS CURL;
GO
--Drop "trusted assembly flag" if it is set
DECLARE @hash VARBINARY(64);
SELECT @hash = hash
FROM sys.trusted_assemblies ta
WHERE ta.description = N'SqlClrCurl'
EXEC sp_drop_trusted_assembly @hash;
GO
--Drop the assembly if it already exists
DROP ASSEMBLY IF EXISTS SqlClrCurl;
GO
DECLARE @hash VARBINARY(64);
SELECT @hash = HASHBYTES('SHA2_512', BulkColumn)
FROM OPENROWSET(BULK 'C:\GitHub\sql-server-samples\samples\features\sql-clr\Curl\bin\Release\SqlClrCurl.dll', SINGLE_BLOB) AS assembly_content
IF(@hash IS NOT NULL)
EXEC sp_add_trusted_assembly @hash, N'SqlClrCurl'
ELSE
PRINT 'Cannot create hash for assembly!'
GO
--Create the assembly
CREATE ASSEMBLY SqlClrCurl
FROM 'C:\GitHub\sql-server-samples\samples\features\sql-clr\Curl\bin\Release\SqlClrCurl.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO
--Create the schema where CURL modules will be placed.
CREATE SCHEMA CURL;
GO
--Create the function/procedure
CREATE FUNCTION CURL.XGET (@H NVARCHAR(MAX), @url NVARCHAR(4000))
RETURNS NVARCHAR(MAX)
AS EXTERNAL NAME SqlClrCurl.Curl.Get;
GO
CREATE PROCEDURE CURL.XPOST (@H NVARCHAR(MAX), @d NVARCHAR(MAX), @url NVARCHAR(4000))
AS EXTERNAL NAME SqlClrCurl.Curl.Post;
GO
@@ -0,0 +1,63 @@
<#@output extension=".sql"#>
<#@ template language="C#" hostspecific="True" #>
-- Enable CLR
sp_configure 'clr enabled', 1;
RECONFIGURE WITH OVERRIDE;
GO
--Drop the functions if they already exist
DROP FUNCTION IF EXISTS CURL.XGET
GO
DROP PROCEDURE IF EXISTS CURL.XPOST
GO
--Drop the schema if it already exists
DROP SCHEMA IF EXISTS CURL;
GO
--Drop "trusted assembly flag" if it is set
DECLARE @hash VARBINARY(64);
SELECT @hash = hash
FROM sys.trusted_assemblies ta
WHERE ta.description = N'SqlClrCurl'
EXEC sp_drop_trusted_assembly @hash;
GO
--Drop the assembly if it already exists
DROP ASSEMBLY IF EXISTS SqlClrCurl;
GO
DECLARE @hash VARBINARY(64);
SELECT @hash = HASHBYTES('SHA2_512', BulkColumn)
FROM OPENROWSET(BULK '<#= this.Host.ResolvePath("bin\\Release\\SqlClrCurl.dll") #>', SINGLE_BLOB) AS assembly_content
IF(@hash IS NOT NULL)
EXEC sp_add_trusted_assembly @hash, N'SqlClrCurl'
ELSE
PRINT 'Cannot create hash for assembly!'
GO
--Create the assembly
CREATE ASSEMBLY SqlClrCurl
FROM '<#= this.Host.ResolvePath("bin\\Release\\SqlClrCurl.dll") #>'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO
--Create the schema where CURL modules will be placed.
CREATE SCHEMA CURL;
GO
--Create the function/procedure
CREATE FUNCTION CURL.XGET (@H NVARCHAR(MAX), @url NVARCHAR(4000))
RETURNS NVARCHAR(MAX)
AS EXTERNAL NAME SqlClrCurl.Curl.Get;
GO
CREATE PROCEDURE CURL.XPOST (@H NVARCHAR(MAX), @d NVARCHAR(MAX), @url NVARCHAR(4000))
AS EXTERNAL NAME SqlClrCurl.Curl.Post;
GO