/
CockroachDbInterleaveInParent.cs
168 lines (145 loc) · 6.86 KB
/
CockroachDbInterleaveInParent.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Text;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class CockroachDbInterleaveInParent
{
private const string AnnotationName = CockroachDbAnnotationNames.InterleaveInParent;
private readonly IReadOnlyAnnotatable _annotatable;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Annotatable Annotatable => (Annotatable)_annotatable;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public CockroachDbInterleaveInParent(IReadOnlyAnnotatable annotatable)
=> _annotatable = annotatable;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string? ParentTableSchema
{
get => GetData().ParentTableSchema;
set
{
(var _, var parentTableName, var interleavePrefix) = GetData();
SetData(value, parentTableName, interleavePrefix);
}
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string ParentTableName
{
get => GetData().ParentTableName;
set
{
(var parentTableSchema, var _, var interleavePrefix) = GetData();
SetData(parentTableSchema, value, interleavePrefix);
}
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual List<string> InterleavePrefix
{
get => GetData().InterleavePrefix;
set
{
(var parentTableSchema, var parentTableName, var _) = GetData();
SetData(parentTableSchema, parentTableName, value);
}
}
private (string? ParentTableSchema, string ParentTableName, List<string> InterleavePrefix) GetData()
{
var str = Annotatable[AnnotationName] as string;
return str is null
? (null, null!, null!)
: Deserialize(str);
}
private void SetData(string? parentTableSchema, string parentTableName, List<string> interleavePrefix)
=> Annotatable[AnnotationName] = Serialize(parentTableSchema, parentTableName, interleavePrefix);
private static string Serialize(string? parentTableSchema, string parentTableName, List<string> interleavePrefix)
{
var builder = new StringBuilder();
EscapeAndQuote(builder, parentTableSchema);
builder.Append(", ");
EscapeAndQuote(builder, parentTableName);
if (interleavePrefix is not null && interleavePrefix.Count > 0)
{
builder.Append(", ");
for (var i = 0; i < interleavePrefix.Count; i++)
{
EscapeAndQuote(builder, interleavePrefix[i]);
if (i < interleavePrefix.Count - 1)
{
builder.Append(", ");
}
}
}
return builder.ToString();
}
internal static (string? ParentTableSchema, string ParentTableName, List<string> InterleavePrefix) Deserialize(string value)
{
Check.NotEmpty(value, nameof(value));
try
{
var position = 0;
var parentTableSchema = ExtractValue(value, ref position);
var parentTableName = ExtractValue(value, ref position)!;
var interleavePrefix = new List<string>();
while (position < value.Length - 1)
{
interleavePrefix.Add(ExtractValue(value, ref position)!);
}
return (parentTableSchema, parentTableName, interleavePrefix);
}
catch (Exception ex)
{
throw new ArgumentException($"Couldn't deserialize {nameof(CockroachDbInterleaveInParent)} from annotation", ex);
}
}
private static string? ExtractValue(string value, ref int position)
{
position = value.IndexOf('\'', position) + 1;
var end = value.IndexOf('\'', position);
while ((end + 1 < value.Length)
&& (value[end + 1] == '\''))
{
end = value.IndexOf('\'', end + 2);
}
var extracted = value.Substring(position, end - position).Replace("''", "'");
position = end + 1;
return extracted.Length == 0 ? null : extracted;
}
private static void EscapeAndQuote(StringBuilder builder, object? value)
{
builder.Append("'");
if (value is not null)
{
builder.Append(value.ToString()!.Replace("'", "''"));
}
builder.Append("'");
}
}