-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringAggregate.cs
69 lines (59 loc) · 1.87 KB
/
StringAggregate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.SqlServer.Server; // Added for Format.UserDefined
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQLCLRFunctions
{
public static class StringAggregate
{
// Product
// Median
// with "AND" so "X, Y, and Z" or "OR"
}
// Stolen from the desk of https://github.com/enriquecatala/SQLCLRUtils/blob/master/SQLCLRUtils/clr/CONCAT_AGG.cs
// Using as a baseline for new ideas since 2019 has STRING_AGG already, with multi-column sorting. A distinct aggregate, for example.
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(Format.UserDefined,
IsInvariantToNulls = true,
IsInvariantToOrder = false,
IsInvariantToDuplicates = false,
IsNullIfEmpty = true,
MaxByteSize = -1,
Name = nameof(StringTogether))]
public struct StringTogether : IBinarySerialize
{
private StringBuilder result;
public void Init()
{
// Put your code here
result = new StringBuilder();
}
public void Accumulate(SqlString Value)
{
if (Value.IsNull)
return;
result.Append(Value.Value).Append(';');
}
public void Merge(StringTogether Group)
{
result.Append(Group.result);
}
public SqlString Terminate()
{
String trimmedResult = null;
if (result?.Length > 0)
trimmedResult = result.ToString(0, result.Length - 1);
return new SqlString(trimmedResult);
}
public void Read(System.IO.BinaryReader r)
{
result = new StringBuilder(r.ReadString());
}
public void Write(System.IO.BinaryWriter w)
{
w.Write(result.ToString());
}
}
}