-
Notifications
You must be signed in to change notification settings - Fork 1
/
SqlStrategy.cs
28 lines (21 loc) · 946 Bytes
/
SqlStrategy.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
using DocuTest.Shared.Interfaces;
namespace DocuTest.Shared.Strategies
{
public abstract class SqlStrategy<T> : IDataStrategy<T>
{
private const string TRUE = "1";
private const string FALSE = "0";
public string ColumnName { get; private set; }
public (string Value, bool Take)[] Records { get; private set; }
public SqlStrategy(string column, params (string Value, bool Take)[] records)
{
this.ColumnName = column;
this.Records = records;
}
public string Expression() => @$"CASE [{this.ColumnName}] {ToSql(this.Records)} END = 1";
public abstract bool Allows(T value);
private string ToSql(bool value) => value ? TRUE : FALSE;
private string ToSql(IEnumerable<(string Value, bool Take)> records) =>
string.Join(" ", records.Select(record => $"WHEN '{record.Value}' THEN {ToSql(record.Take)}"));
}
}