forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutineSourceParameterFormatter.cs
More file actions
101 lines (83 loc) · 3.23 KB
/
Copy pathRoutineSourceParameterFormatter.cs
File metadata and controls
101 lines (83 loc) · 3.23 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Globalization;
using System.Text;
using Npgsql;
namespace NpgsqlRest;
public class RoutineSourceParameterFormatter : IRoutineSourceParameterFormatter
{
public bool IsFormattable { get; } = false;
public string AppendCommandParameter(NpgsqlRestParameter parameter, int index)
{
var suffix = parameter.TypeDescriptor.IsCastToText() ?
string.Concat(Consts.DoubleColon, parameter.TypeDescriptor.OriginalType) :
string.Empty;
if (index == 0)
{
return parameter.ActualName is null ?
string.Concat(Consts.FirstParam, suffix) :
string.Concat(parameter.ActualName, Consts.FirstNamedParam, suffix);
}
var indexStr = (index + 1).ToString(CultureInfo.InvariantCulture);
return parameter.ActualName is null ?
string.Concat(Consts.Comma, Consts.Dollar, indexStr, suffix) :
string.Concat(Consts.Comma, parameter.ActualName, Consts.NamedParam, indexStr, suffix);
}
public string? AppendEmpty() => Consts.CloseParenthesisStr;
}
public class RoutineSourceCustomTypesParameterFormatter : IRoutineSourceParameterFormatter
{
public bool IsFormattable { get; } = true;
public string FormatCommand(Routine routine, NpgsqlParameterCollection parameters)
{
var sb = new StringBuilder(routine.Expression, routine.Expression.Length + parameters.Count * 20);
var count = parameters.Count;
var culture = CultureInfo.InvariantCulture;
for (var i = 0; i < count; i++)
{
var parameter = (NpgsqlRestParameter)parameters[i];
var typeDescriptor = parameter.TypeDescriptor;
var suffix = typeDescriptor.IsCastToText() ?
string.Concat(Consts.DoubleColon, typeDescriptor.OriginalType) :
string.Empty;
if (i > 0)
{
sb.Append(Consts.Comma);
}
var indexStr = (i + 1).ToString(culture);
if (typeDescriptor.CustomType is null)
{
if (parameter.ActualName is null)
{
sb.Append(Consts.Dollar)
.Append(indexStr)
.Append(suffix);
}
else
{
sb.Append(parameter.ActualName)
.Append(Consts.NamedParam)
.Append(indexStr);
}
}
else
{
if (typeDescriptor.CustomTypePosition == 1)
{
sb.Append(typeDescriptor.OriginalParameterName)
.Append(Consts.OpenRow);
}
sb.Append(Consts.Dollar)
.Append(indexStr)
.Append(suffix);
if (i == count - 1 ||
typeDescriptor.CustomTypePosition !=
((NpgsqlRestParameter)parameters[i + 1]).TypeDescriptor.CustomTypePosition - 1)
{
sb.Append(Consts.CloseRow)
.Append(typeDescriptor.CustomType);
}
}
}
sb.Append(Consts.CloseParenthesis);
return sb.ToString();
}
}