@@ -115,16 +115,21 @@ public string GetQuotedName(Dialect.Dialect d)
115
115
return IsQuoted ? d . QuoteForColumnName ( _name ) : _name ;
116
116
}
117
117
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
- */
123
118
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>
124
123
public string GetAlias ( Dialect . Dialect dialect )
124
+ {
125
+ return GetAlias ( dialect . MaxAliasLength ) ;
126
+ }
127
+
128
+ private string GetAlias ( int maxAliasLength )
125
129
{
126
130
string alias = _name ;
127
- string suffix = UniqueInteger . ToString ( ) + '_' ;
131
+ string suffix = UniqueInteger . ToString ( ) + StringHelper . Underscore ;
132
+
128
133
int lastLetter = StringHelper . LastIndexOfLetter ( _name ) ;
129
134
if ( lastLetter == - 1 )
130
135
{
@@ -134,27 +139,38 @@ public string GetAlias(Dialect.Dialect dialect)
134
139
{
135
140
alias = _name . Substring ( 0 , lastLetter + 1 ) ;
136
141
}
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" ) ;
144
142
145
- if ( useRawName )
146
- {
147
- return alias ;
148
- }
149
- else
143
+ // Updated logic ported from Hibernate's fix for HHH-8073.
144
+ // https://github.com/hibernate/hibernate-orm/commit/79073a98f0e4ed225fe4608b67594196f86d48d7
145
+ // To my mind it is weird - since the suffix is now always used, it
146
+ // seems "useRawName" is a misleading choice of variable name. For the same
147
+ // reason, the checks for "_quoted" and "rowid" looks redundant. If you remove
148
+ // those checks, then the double checks for total length can be reduced to one.
149
+ // But I will leave it like this for now to make it look similar. /Oskar 2016-08-20
150
+ bool useRawName = _name . Length + suffix . Length <= maxAliasLength &&
151
+ ! _quoted &&
152
+ ! StringHelper . EqualsCaseInsensitive ( _name , "rowid" ) ;
153
+ if ( ! useRawName )
150
154
{
151
- return alias + suffix ;
155
+ if ( suffix . Length >= maxAliasLength )
156
+ {
157
+ throw new MappingException (
158
+ string . Format (
159
+ "Unique suffix {0} length must be less than maximum {1} characters." ,
160
+ suffix ,
161
+ maxAliasLength ) ) ;
162
+ }
163
+ if ( alias . Length + suffix . Length > maxAliasLength )
164
+ alias = alias . Substring ( 0 , maxAliasLength - suffix . Length ) ;
152
165
}
166
+ return alias + suffix ;
153
167
}
154
168
155
169
public string GetAlias ( Dialect . Dialect dialect , Table table )
156
170
{
157
- return GetAlias ( dialect ) + table . UniqueInteger + StringHelper . Underscore ;
171
+ string suffix = table . UniqueInteger . ToString ( ) + StringHelper . Underscore ;
172
+ int maxAliasLength = dialect . MaxAliasLength - suffix . Length ;
173
+ return GetAlias ( maxAliasLength ) + suffix ;
158
174
}
159
175
160
176
0 commit comments