-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFullColumnName.cs
More file actions
134 lines (108 loc) · 4.35 KB
/
FullColumnName.cs
File metadata and controls
134 lines (108 loc) · 4.35 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
namespace JankSQL
{
public class FullColumnName
{
private readonly string columnName;
private readonly string? serverName;
private readonly string? schemaName;
private FullColumnName(string columnName)
{
this.columnName = columnName;
}
private FullColumnName(string? serverName, string? schemaName, string? tableName, string columnName)
{
this.serverName = serverName;
this.schemaName = schemaName;
this.TableNameOnly = tableName;
this.columnName = columnName;
}
public string? TableNameOnly { get; }
public override bool Equals(object? o)
{
if (o is not FullColumnName other)
return false;
// InvariantCultureIgnoreCase so that identifier names can be localized
if (other.serverName != null && !other.serverName.Equals(this.serverName, StringComparison.InvariantCultureIgnoreCase))
return false;
if (other.schemaName != null && !other.schemaName.Equals(this.schemaName, StringComparison.InvariantCultureIgnoreCase))
return false;
if (other.TableNameOnly != null && !other.TableNameOnly.Equals(this.TableNameOnly, StringComparison.InvariantCultureIgnoreCase))
return false;
bool ret = other.columnName.Equals(this.columnName, StringComparison.InvariantCultureIgnoreCase);
return ret;
}
public override int GetHashCode()
{
int hash = 19;
if (serverName != null)
hash = (hash * 31) + serverName.GetHashCode();
if (schemaName != null)
hash = (hash * 31) + schemaName.GetHashCode();
if (TableNameOnly != null)
hash = (hash * 31) + TableNameOnly.GetHashCode();
hash = (hash * 31) + columnName.GetHashCode();
return hash;
}
public string ColumnNameOnly()
{
return columnName;
}
public override string ToString()
{
//REVIEW: is this right? could be that serverName != null but schemaName == null, and ...
string ret = string.Empty;
if (serverName != null)
ret += $"[{serverName}]";
if (schemaName != null)
{
if (ret.Length > 0)
ret += ".";
ret += $"[{schemaName}]";
}
if (TableNameOnly != null)
{
if (ret.Length > 0)
ret += ".";
ret += $"[{TableNameOnly}]";
}
if (ret.Length > 0)
ret += ".";
ret += $"[{columnName}]";
return ret;
}
internal static FullColumnName FromContext(TSqlParser.Full_column_nameContext context)
{
string columnName = ParseHelpers.StringFromIDContext(context.id_());
string? serverName = null;
string? schemaName = null;
string? tableName = null;
if (context.full_table_name() != null)
{
serverName = ParseHelpers.PossibleStringFromIDContext(context.full_table_name().server);
schemaName = ParseHelpers.PossibleStringFromIDContext(context.full_table_name().schema);
tableName = ParseHelpers.PossibleStringFromIDContext(context.full_table_name().table);
}
return new FullColumnName(serverName, schemaName, tableName, columnName);
}
internal static FullColumnName FromIDContext(TSqlParser.Id_Context context)
{
var r = new FullColumnName(ParseHelpers.StringFromIDContext(context));
return r;
}
internal static FullColumnName FromColumnName(string columnName)
{
var r = new FullColumnName(columnName);
return r;
}
internal static FullColumnName FromTableColumnName(string tableName, string columnName)
{
var r = new FullColumnName(null, null, tableName, columnName);
return r;
}
internal FullColumnName ApplyTableAlias(string newTableName)
{
FullColumnName fcnNew = new (serverName, schemaName, newTableName, columnName);
return fcnNew;
}
}
}