Skip to content

Commit cf2dc29

Browse files
author
jeffshumphreys@gmail.com
committed
This may be a contestable change. SqlDateTime can hold nulls, but .NET DateTime maps to SQL DATETIME2. I staged this in VS. Will fix to DateTime?
1 parent cd7523e commit cf2dc29

15 files changed

+618
-358
lines changed

Humanization.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
using Microsoft.SqlServer.Server;
22
using System;
3+
using System.Data.SqlTypes;
34

45
namespace MySQLCLRFunctions
56
{
67
public static class Humanization
78
{
9+
/*
10+
* This needs a lot more work. And when do I use this? I need use cases.
11+
*/
812
[SqlFunction(DataAccess = DataAccessKind.None)]
9-
public static string HumanizeDateTimeDiff(DateTime from)
13+
public static string HumanizeDateTimeDiff(SqlDateTime from)
1014
{
11-
if (from == null) return null;
12-
TimeSpan ts = DateTime.Now.Subtract(from);
15+
if (from.IsNull) return null;
16+
17+
TimeSpan ts = DateTime.Now.Subtract(from.Value);
1318

1419
// The trick: make variable contain date and time representing the desired timespan,
1520
// having +1 in each date component.
@@ -46,6 +51,5 @@ private static string ProcessPeriod(int value, int subValue, string name, string
4651
? String.Format("{0} {1}s ago", value, name)
4752
: String.Format("About {0} {1}s ago", value, name);
4853
}
49-
5054
}
5155
}

MySQLCLRFunctions.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<RootNamespace>MySQLCLRFunctions</RootNamespace>
1111
<AssemblyName>MySQLCLRFunctions</AssemblyName>
1212
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
13+
<LangVersion>8.0</LangVersion>
1314
<FileAlignment>512</FileAlignment>
1415
<Deterministic>true</Deterministic>
1516
<TargetFrameworkProfile />
@@ -51,13 +52,16 @@
5152
<Compile Include="Adaptors.cs" />
5253
<Compile Include="CharacterExtract.cs" />
5354
<Compile Include="CharacterTest.cs" />
55+
<Compile Include="CharacterTransform.cs" />
5456
<Compile Include="Compares.cs" />
5557
<Compile Include="Environmental.cs" />
5658
<Compile Include="FileNameExtract.cs" />
5759
<Compile Include="Files.cs" />
60+
<Compile Include="GlobalSuppressions.cs" />
5861
<Compile Include="Humanization.cs" />
5962
<Compile Include="NetworkCollect.cs" />
6063
<Compile Include="NetworkTest.cs" />
64+
<Compile Include="_NewFunctionsToTry.cs" />
6165
<Compile Include="_SharedConstants.cs" />
6266
<Compile Include="StringBuildOut.cs" />
6367
<Compile Include="StringDecode.cs" />
@@ -71,11 +75,20 @@
7175
<Compile Include="StringTransformStripDownCustomizations.cs" />
7276
<Compile Include="StringTransformTSQLSpecific.cs" />
7377
<Compile Include="Properties\AssemblyInfo.cs" />
78+
<Compile Include="_VERIFY_CSHARP8.cs" />
7479
</ItemGroup>
7580
<ItemGroup>
81+
<PackageReference Include="Microsoft.Net.Compilers.Toolset">
82+
<Version>3.6.0</Version>
83+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
84+
<PrivateAssets>all</PrivateAssets>
85+
</PackageReference>
7686
<PackageReference Include="NUnit">
7787
<Version>3.12.0</Version>
7888
</PackageReference>
89+
<PackageReference Include="System.Collections">
90+
<Version>4.3.0</Version>
91+
</PackageReference>
7992
</ItemGroup>
8093
<ItemGroup>
8194
<None Include=".editorconfig" />

MySQLCLRFunctions.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{912B914B-FAA0-4CB8-B38C-202E9B9965ED}"
99
ProjectSection(SolutionItems) = preProject
1010
.editorconfig = .editorconfig
11+
.gitattributes = .gitattributes
12+
.gitignore = .gitignore
13+
_config.yml = _config.yml
1114
SQLCLR_Register.sql = SQLCLR_Register.sql
1215
TestValueSettings.localuseonly = TestValueSettings.localuseonly
1316
EndProjectSection

NetworkTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ private static bool PingHost(string nameOrAddress)
4040
}
4141
finally
4242
{
43-
if (pinger != null)
44-
{
45-
pinger.Dispose();
46-
}
43+
pinger?.Dispose();
4744
}
4845

4946
return pingable;

SQLCLR_Register.sql

Lines changed: 110 additions & 63 deletions
Large diffs are not rendered by default.

StringExtract.cs

Lines changed: 163 additions & 145 deletions
Large diffs are not rendered by default.

StringFormat.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public static class StringFormat
1313
*
1414
**************************************************************************************************************************************************************************************/
1515
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
16-
public static string RPad(string input, int padToLen)
16+
public static string RPad(string input, int length)
1717
{
18-
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
18+
if (StringTest.IsNull(input)) return input;
1919

20-
return input.PadRight(padToLen);
20+
return input.PadRight(length);
2121
}
2222

2323
/***************************************************************************************************************************************************************************************************
@@ -26,11 +26,11 @@ public static string RPad(string input, int padToLen)
2626
*
2727
**************************************************************************************************************************************************************************************/
2828
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
29-
public static string LPadChar(string input, int padToLen, char padCh)
29+
public static string LPadC(string input, int length, char character)
3030
{
31-
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
31+
if (StringTest.IsNull(input)) return input;
3232

33-
return input.PadLeft(padToLen, padCh);
33+
return input.PadLeft(length, character);
3434
}
3535

3636
/***************************************************************************************************************************************************************************************************
@@ -39,11 +39,11 @@ public static string LPadChar(string input, int padToLen, char padCh)
3939
*
4040
**************************************************************************************************************************************************************************************/
4141
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
42-
public static string RPadChar(string input, int padToLen, char padCh)
42+
public static string RPadChar(string input, int length, char character)
4343
{
44-
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
44+
if (StringTest.IsNull(input)) return input;
4545

46-
return input.PadRight(padToLen, padCh);
46+
return input.PadRight(length, character);
4747
}
4848

4949
/***************************************************************************************************************************************************************************************************
@@ -52,11 +52,11 @@ public static string RPadChar(string input, int padToLen, char padCh)
5252
*
5353
**************************************************************************************************************************************************************************************/
5454
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
55-
public static string LPad(string input, int padToLen)
55+
public static string LPad(string input, int length)
5656
{
57-
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
57+
if (StringTest.IsNull(input)) return input;
5858

59-
return input.PadLeft(padToLen);
59+
return input.PadLeft(length);
6060
}
6161

6262
/***************************************************************************************************************************************************************************************************
@@ -69,7 +69,7 @@ public static string Title(string input)
6969
{
7070
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
7171

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

StringMeasure.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using Microsoft.SqlServer.Server;
2-
using static MySQLCLRFunctions.StringTest;
32
using System;
43
using System.Text.RegularExpressions;
54
using System.Data.SqlTypes;
5+
using static MySQLCLRFunctions.StringTest;
6+
using static MySQLCLRFunctions._SharedConstants;
7+
68
/*
79
* Take measurements from a string. Not actual values from a string as Extract does, and not a "changed" (immutable) string like Transform or Pivot, or Format, Reduce.
10+
*
11+
* WARNING SqlDateTime is not the same as datetime. SqlDateTime maps to DATETIME, DateTime maps to DATETIME2
812
*/
913
namespace MySQLCLRFunctions
1014
{
@@ -18,7 +22,6 @@ public static class StringMeasure
1822
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
1923
public static SqlInt32 HowManyS(string input, string marker)
2024
{
21-
// General Rule: Follow SQL rules around null
2225
if (IsNull(input) || IsNull(marker)) return SqlInt32.Null;
2326
if (IsEmpty(marker)) throw new ArgumentOutOfRangeException("Empty strings would result in infinite loop.");
2427
if (IsEmpty(input)) return 0;

0 commit comments

Comments
 (0)