Skip to content

Commit 013b5da

Browse files
committed
Mapping\Column.cs: Fix the problem with non-letter characters in alias (part of NH-3899).
This is done by porting the current method from Hibernate's fix for HHH-8073. hibernate/hibernate-orm@79073a9
1 parent 82c104c commit 013b5da

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/NHibernate/Mapping/Column.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ public string GetQuotedName(Dialect.Dialect d)
115115
return IsQuoted ? d.QuoteForColumnName(_name) : _name;
116116
}
117117

118-
/**
119-
* For any column name, generate an alias that is unique
120-
* to that column name, and also 10 characters or less
121-
* in length.
122-
*/
123118

119+
/// <summary>
120+
/// For any column name, generate an alias that is unique to that
121+
/// column name, and also take Dialect.MaxAliasLength into account.
122+
/// </summary>
124123
public string GetAlias(Dialect.Dialect dialect)
125124
{
126125
string alias = _name;
127-
string suffix = UniqueInteger.ToString() + '_';
126+
string suffix = UniqueInteger.ToString() + StringHelper.Underscore;
127+
128128
int lastLetter = StringHelper.LastIndexOfLetter(_name);
129129
if (lastLetter == -1)
130130
{
@@ -134,22 +134,31 @@ public string GetAlias(Dialect.Dialect dialect)
134134
{
135135
alias = _name.Substring(0, lastLetter + 1);
136136
}
137-
if (alias.Length > dialect.MaxAliasLength)
138-
{
139-
alias = alias.Substring(0, dialect.MaxAliasLength - suffix.Length);
140-
}
141-
bool useRawName = _name.Equals(alias) &&
142-
!_quoted &&
143-
!StringHelper.EqualsCaseInsensitive(_name, "rowid");
144137

145-
if (useRawName)
138+
// Updated logic ported from Hibernate's fix for HHH-8073.
139+
// https://github.com/hibernate/hibernate-orm/commit/79073a98f0e4ed225fe4608b67594196f86d48d7
140+
// To my mind it is weird - since the suffix is now always used, it
141+
// seems "useRawName" is a misleading choice of variable name. For the same
142+
// reason, the checks for "_quoted" and "rowid" looks redundant. If you remove
143+
// those checks, then the double checks for total length can be reduced to one.
144+
// But I will leave it like this for now to make it look similar. /Oskar 2016-08-20
145+
bool useRawName = _name.Length + suffix.Length <= dialect.MaxAliasLength &&
146+
!_quoted &&
147+
!StringHelper.EqualsCaseInsensitive(_name, "rowid");
148+
if (!useRawName)
146149
{
147-
return alias;
148-
}
149-
else
150-
{
151-
return alias + suffix;
150+
if (suffix.Length >= dialect.MaxAliasLength)
151+
{
152+
throw new MappingException(
153+
string.Format(
154+
"Unique suffix {0} length must be less than maximum {1} characters.",
155+
suffix,
156+
dialect.MaxAliasLength));
157+
}
158+
if (alias.Length + suffix.Length > dialect.MaxAliasLength)
159+
alias = alias.Substring(0, dialect.MaxAliasLength - suffix.Length);
152160
}
161+
return alias + suffix;
153162
}
154163

155164
public string GetAlias(Dialect.Dialect dialect, Table table)

0 commit comments

Comments
 (0)