Skip to content

Commit 098e336

Browse files
author
jeffshumphreys@gmail.com
committed
Added Proper MSTest project. Wrote some tests. Will be moving away from using the Program to test. Using CodeRush with MSTest. Added Mid function to SQL output. Added some documentation to sql deployment script.
1 parent 7d0e06e commit 098e336

13 files changed

+288
-217
lines changed

Adaptors.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@ namespace MySQLCLRFunctions
1616
{
1717
public static class Adaptors
1818
{
19-
/// <summary>
20-
///
21-
/// Converts a hex string to a VARBINARY string, I think.
22-
///
23-
/// </summary>
24-
/// <param name="InputAsHex"></param>
25-
/// <returns></returns>
19+
// Converts a hex string to a VARBINARY string, I think.
20+
2621
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
2722
public static string VarBin2Hex(SqlBytes InputAsHex)
2823
{
24+
if (InputAsHex == null) return null;
25+
2926
return BitConverter.ToString(InputAsHex.Buffer);
3027
}
3128

@@ -40,7 +37,7 @@ public static string VarBin2Hex(SqlBytes InputAsHex)
4037
{
4138
return DateTime.ParseExact(InputAsStringDateTime.Substring(0, 14), "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
4239
}
43-
catch (FormatException fe)
40+
catch (FormatException)
4441
{
4542
return null;
4643
}

Humanization.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
using System;
2-
using System.Collections;
3-
using Microsoft.SqlServer.Server;
4-
using System.Data.SqlTypes;
5-
using System.Text.RegularExpressions;
6-
using System.Text;
7-
using System.Net.NetworkInformation;
8-
using System.Net;
9-
using System.Net.Sockets;
1+
using Microsoft.SqlServer.Server;
2+
using System;
103
using System.Linq;
11-
using System.IO;
12-
using System.Xml.Schema;
134

145
namespace MySQLCLRFunctions
156
{

MySQLCLRFunctions.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
SQLCLR_Register.sql.txt = SQLCLR_Register.sql.txt
1313
EndProjectSection
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySQLCLRFunctionsMSTests", "..\MySQLCLRFunctionsTests\MySQLCLRFunctionsMSTests.csproj", "{9DC997C0-C2D7-4339-9E1C-893937A32EEA}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -26,6 +28,10 @@ Global
2628
{A084D459-BEE6-46BB-BB2C-4756879B2599}.Debug|Any CPU.Build.0 = Debug|Any CPU
2729
{A084D459-BEE6-46BB-BB2C-4756879B2599}.Release|Any CPU.ActiveCfg = Release|Any CPU
2830
{A084D459-BEE6-46BB-BB2C-4756879B2599}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{9DC997C0-C2D7-4339-9E1C-893937A32EEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{9DC997C0-C2D7-4339-9E1C-893937A32EEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{9DC997C0-C2D7-4339-9E1C-893937A32EEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{9DC997C0-C2D7-4339-9E1C-893937A32EEA}.Release|Any CPU.Build.0 = Release|Any CPU
2935
EndGlobalSection
3036
GlobalSection(SolutionProperties) = preSolution
3137
HideSolutionNode = FALSE

NetworkCollect.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public static SqlString GetHostAliases(SqlString MachineOrAlias)
8181
// Named instance
8282
}
8383

84+
#pragma warning disable CS0618 // Type or member is obsolete
8485
IPHostEntry truehost = Dns.Resolve(host);
86+
#pragma warning restore CS0618 // Type or member is obsolete
8587

8688
string hosts = string.Join<string>(";", truehost.Aliases);
8789
return new SqlString(hosts);
@@ -100,7 +102,9 @@ public static SqlString GetHostRealName(SqlString MachineOrAlias)
100102
}
101103
try
102104
{
105+
#pragma warning disable CS0618 // Type or member is obsolete
103106
IPHostEntry truehost = Dns.Resolve(host);
107+
#pragma warning restore CS0618 // Type or member is obsolete
104108

105109
host = truehost.HostName;
106110
}

StringBuildOut.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using Microsoft.SqlServer.Server;
22
using System;
3-
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
74

85
namespace MySQLCLRFunctions
96
{

StringExtract.cs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace MySQLCLRFunctions
77
public static class StringExtract
88
{
99
private const int NOT_FOUND = -1;
10-
private const int CHARACTER_AFTER_MARKER = 1;
1110
private const int BACKSET_FOR_ZEROBASED = -1;
1211

1312
/***************************************************************************************************************************************************************************************************
@@ -87,7 +86,7 @@ public static string LeftMOfNth(string input, string marker, int n, int howmany)
8786
for (int j = 1; j <= n; j++)
8887
{
8988
if (i >= input.IndexOfLastChar()) return string.Empty;
90-
pointsfound[j+BACKSET_FOR_ZEROBASED] = i;
89+
pointsfound[j + BACKSET_FOR_ZEROBASED] = i;
9190
i = input.IndexOf(marker, i + marker.Length);
9291
if (i == NOT_FOUND) return string.Empty;
9392
if (j == howmany)
@@ -151,16 +150,27 @@ public static string RightOfAny(string input, string markercharacters)
151150

152151
/***************************************************************************************************************************************************************************************************
153152
*
154-
* Extract a snippet of a string given a starting and ending position, rather than substring with starting and length to return.
153+
* This is not at all the BASIC function Mid, but what the hey. I need a snippet from a string, and I want the cleverness of supporting negatives as it's intuitive.
154+
* Matter of fact, I need substring to be a little smarter too.
155155
*
156-
***************************************************************************************************************************************************************************************************/
156+
**************************************************************************************************************************************************************************************/
157157
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
158-
public static string Cut(string input, int from, int to)
158+
public static string Mid(this string input, int from, int to)
159159
{
160-
if (to - from <= 0) return String.Empty;
161-
if (to > input.Length) return input;
160+
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
161+
if (from < 0) return input;
162+
if (from >= 0 && to >= 0 && from > to) return input;
163+
164+
if (to < 0)
165+
{
166+
string x = input.Substring(from);
167+
int i = -to;
168+
x = x.TrimEnd((int)i);
169+
return x;
170+
}
171+
if (to > from) return input.Substring(from, input.Length - (from + to));
162172

163-
return input.Substring(from, to - from);
173+
return input;
164174
}
165175

166176
/***************************************************************************************************************************************************************************************************
@@ -227,7 +237,7 @@ public static string FirstWord(string input)
227237
{
228238
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
229239
return input.Split(@"\W")[0];
230-
}
240+
}
231241

232242
/***************************************************************************************************************************************************************************************************
233243
*
@@ -323,29 +333,5 @@ public static string EverythingAfter(string input, string marker)
323333
if (i + marker.Length > input.Length) return string.Empty;
324334
return input.Substring(i + marker.Length);
325335
}
326-
327-
/***************************************************************************************************************************************************************************************************
328-
*
329-
* This is not at all the BASIC function Mid, but what the hey. I need a snippet from a string, and I want the cleverness of supporting negatives as it's intuitive.
330-
* Matter of fact, I need substring to be a little smarter too.
331-
*
332-
**************************************************************************************************************************************************************************************/
333-
public static string Mid(this string input, int from, int to)
334-
{
335-
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
336-
if (from < 0) return input;
337-
if (from >= 0 && to >= 0 && from > to) return input;
338-
339-
if (to < 0)
340-
{
341-
string x = input.Substring(from);
342-
int i = -to;
343-
x = x.TrimEnd((int)i);
344-
return x;
345-
}
346-
if (to > from) return input.Substring(from, input.Length - (from + to));
347-
348-
return input;
349-
}
350336
}
351337
}

StringFormat.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.SqlServer.Server;
2-
using System.Text;
32
using System.Text.RegularExpressions;
43
/*
54
* Take measurements from a string. Not actual values from a string as Extract does.
@@ -70,7 +69,7 @@ public static string Title(string input)
7069
{
7170
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
7271

73-
return Regex.Replace(input, @"\b[a-z]\w+", delegate (Match match)
72+
return Regex.Replace(input.ToLower(), @"\b[a-z]\w+", delegate (Match match)
7473
{
7574
string v = match.ToString();
7675
return char.ToUpper(v[0]) + v.Substring(1);

0 commit comments

Comments
 (0)