-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathNpgsqlSqlGenerationHelper.cs
More file actions
119 lines (105 loc) · 4.39 KB
/
NpgsqlSqlGenerationHelper.cs
File metadata and controls
119 lines (105 loc) · 4.39 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.RegularExpressions;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
/// <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 NpgsqlSqlGenerationHelper : RelationalSqlGenerationHelper
{
private static readonly HashSet<string> ReservedWords;
static NpgsqlSqlGenerationHelper()
{
// https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html
using (var conn = new NpgsqlConnection())
{
ReservedWords =
[..conn.GetSchema("ReservedWords").Rows.Cast<DataRow>().Select(r => (string)r["ReservedWord"])];
}
}
/// <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 NpgsqlSqlGenerationHelper(RelationalSqlGenerationHelperDependencies dependencies)
: base(dependencies)
{
}
/// <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 override string DelimitIdentifier(string identifier)
=> RequiresQuoting(identifier) ? base.DelimitIdentifier(identifier) : identifier;
/// <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 override void DelimitIdentifier(StringBuilder builder, string identifier)
{
if (RequiresQuoting(identifier))
{
base.DelimitIdentifier(builder, identifier);
}
else
{
builder.Append(identifier);
}
}
/// <summary>
/// Returns whether the given string can be used as an unquoted identifier in PostgreSQL, without quotes.
/// </summary>
private static bool RequiresQuoting(string identifier)
{
var first = identifier[0];
if (!char.IsLower(first) && first != '_')
{
return true;
}
for (var i = 1; i < identifier.Length; i++)
{
var c = identifier[i];
if (char.IsLower(c))
{
continue;
}
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '_':
case '$': // yes it's true
continue;
}
return true;
}
if (ReservedWords.Contains(identifier.ToUpperInvariant()))
{
return true;
}
return false;
}
/// <inheritdoc />
public override string DelimitJsonPathElement(string pathElement)
=> !char.IsAsciiLetter(pathElement[0])
? $"\"{EscapeJsonPathElement(pathElement)}\""
: base.DelimitJsonPathElement(pathElement);
}