forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleValue.cs
390 lines (337 loc) · 9.44 KB
/
SimpleValue.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Engine;
using NHibernate.Id;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Mapping
{
/// <summary>
/// Any value that maps to columns.
/// </summary>
[Serializable]
public class SimpleValue : IKeyValue
{
private readonly List<ISelectable> columns = new List<ISelectable>();
private IType type;
private IDictionary<string, string> typeParameters;
private IDictionary<string, string> identifierGeneratorProperties;
private string identifierGeneratorStrategy = "assigned";
private string nullValue;
private Table table;
private string foreignKeyName;
private bool cascadeDeleteEnabled;
private bool isAlternateUniqueKey;
private string typeName;
public SimpleValue()
{
}
public SimpleValue(Table table)
{
this.table = table;
}
public virtual IEnumerable<Column> ConstraintColumns => columns.OfType<Column>();
public string ForeignKeyName
{
get { return foreignKeyName; }
set { foreignKeyName = value; }
}
public Table Table
{
get { return table; }
set { table = value; }
}
public IDictionary<string, string> IdentifierGeneratorProperties
{
get { return identifierGeneratorProperties; }
set { identifierGeneratorProperties = value; }
}
public string IdentifierGeneratorStrategy
{
get { return identifierGeneratorStrategy; }
set { identifierGeneratorStrategy = value; }
}
public virtual bool IsComposite
{
get { return false; }
}
#region IKeyValue Members
public virtual void CreateForeignKeyOfEntity(string entityName)
{
if (!HasFormula && ! "none".Equals(ForeignKeyName, StringComparison.OrdinalIgnoreCase))
{
ForeignKey fk = table.CreateForeignKey(ForeignKeyName, ConstraintColumns, entityName);
fk.CascadeDeleteEnabled = cascadeDeleteEnabled;
}
}
public bool IsCascadeDeleteEnabled
{
get { return cascadeDeleteEnabled; }
set { cascadeDeleteEnabled = value; }
}
public bool IsIdentityColumn(Dialect.Dialect dialect)
{
return IdentifierGeneratorFactory.GetIdentifierGeneratorClass(identifierGeneratorStrategy, dialect) == typeof (IdentityGenerator);
}
public string NullValue
{
get { return nullValue; }
set { nullValue = value; }
}
public virtual bool IsUpdateable
{
get { return true; }//needed to satisfy IKeyValue
}
public virtual bool IsTypeSpecified
{
get { return typeName != null; }
}
public IDictionary<string, string> TypeParameters
{
get { return typeParameters; }
set
{
if (!CollectionHelper.DictionaryEquals(typeParameters, value))
{
typeParameters = value;
type = null; // invalidate type
}
}
}
public string TypeName
{
get { return typeName; }
set
{
if ((typeName == null && value != null) || (typeName != null && !typeName.Equals(value)))
{
// the property change
typeName = value;
type = null; // invalidate type
}
}
}
public IIdentifierGenerator CreateIdentifierGenerator(Dialect.Dialect dialect, string defaultCatalog,
string defaultSchema, RootClass rootClass)
{
var @params = new Dictionary<string, string>();
//if the hibernate-mapping did not specify a schema/catalog, use the defaults
//specified by properties - but note that if the schema/catalog were specified
//in hibernate-mapping, or as params, they will already be initialized and
//will override the values set here (they are in identifierGeneratorProperties)
if (!string.IsNullOrEmpty(defaultSchema))
{
@params[PersistentIdGeneratorParmsNames.Schema] = defaultSchema;
}
if (!string.IsNullOrEmpty(defaultCatalog))
{
@params[PersistentIdGeneratorParmsNames.Catalog] = defaultCatalog;
}
//pass the entity-name, if not a collection-id
if (rootClass != null)
{
@params[IdGeneratorParmsNames.EntityName] = rootClass.EntityName;
}
//init the table here instead of earlier, so that we can get a quoted table name
//TODO: would it be better to simply pass the qualified table name, instead of
// splitting it up into schema/catalog/table names
string tableName = Table.GetQuotedName(dialect);
@params[PersistentIdGeneratorParmsNames.Table] = tableName;
//pass the column name (a generated id almost always has a single column and is not a formula)
string columnName = ((Column)ColumnIterator.First()).GetQuotedName(dialect);
@params[PersistentIdGeneratorParmsNames.PK] = columnName;
if (rootClass != null)
{
StringBuilder tables = new StringBuilder();
bool commaNeeded = false;
foreach (Table identityTable in rootClass.IdentityTables)
{
if (commaNeeded)
tables.Append(StringHelper.CommaSpace);
commaNeeded = true;
tables.Append(identityTable.GetQuotedName(dialect));
}
@params[PersistentIdGeneratorParmsNames.Tables] = tables.ToString();
}
else
{
@params[PersistentIdGeneratorParmsNames.Tables] = tableName;
}
if (identifierGeneratorProperties != null)
{
ArrayHelper.AddAll(@params, identifierGeneratorProperties);
}
return IdentifierGeneratorFactory.Create(identifierGeneratorStrategy, Type, @params, dialect);
}
#endregion
#region IValue Members
public virtual int ColumnSpan
{
get { return columns.Count; }
}
public virtual IEnumerable<ISelectable> ColumnIterator
{
get { return columns; }
}
public virtual IType Type
{
get
{
// NH: Different implementation
// we use the internal instance of IType to have better performance
if (type == null)
{
if (string.IsNullOrEmpty(typeName))
{
throw new MappingException("No type name specified");
}
type = GetHeuristicType();
if (type == null)
{
string msg = "Could not determine type for: " + typeName;
if (columns != null && columns.Count > 0)
{
msg += (", for columns: " + StringHelper.CollectionToString(columns));
}
throw new MappingException(msg);
}
}
return type;
}
}
private IType GetHeuristicType()
{
// NH different behavior
// If the mapping has a type as "Double(10,5)" our SqlType will be created with all information
// including the right SqlType specification, but when the length/precision/scale was specified
// through attributes the SqlType is wrong (does not include length/precision/scale specification)
IType result = null;
if (ColumnSpan == 1 && !columns[0].IsFormula)
{
var col = (Column)columns[0];
if (col.IsLengthDefined())
{
result = TypeFactory.BuiltInType(typeName, col.Length);
if (result == null)
result = TypeFactory.HeuristicType(typeName, typeParameters, col.Length);
}
else if (col.IsPrecisionDefined())
{
result = TypeFactory.BuiltInType(typeName, Convert.ToByte(col.Precision), Convert.ToByte(col.Scale));
}
else if (col.IsScaleDefined())
{
result = TypeFactory.BuiltInType(typeName, col.Scale);
}
}
return result ?? TypeFactory.HeuristicType(typeName, typeParameters);
}
public bool HasFormula
{
get
{
foreach (ISelectable s in ColumnIterator)
{
if (s.IsFormula)
return true;
}
return false;
}
}
public virtual bool IsNullable
{
get
{
// NH : Different implementation (to iterate the collection only one time)
foreach (ISelectable selectable in ColumnIterator)
{
if (selectable.IsFormula)
return true;
if (!((Column)selectable).IsNullable)
return false;
}
return true;
}
}
public virtual bool[] ColumnUpdateability
{
get { return ColumnInsertability; }
}
public virtual bool[] ColumnInsertability
{
get
{
bool[] result = new bool[ColumnSpan];
int i = 0;
foreach (ISelectable s in ColumnIterator)
{
result[i++] = !s.IsFormula;
}
return result;
}
}
public bool IsSimpleValue
{
get { return true; }
}
public virtual bool IsValid(IMapping mapping)
{
return ColumnSpan == Type.GetOwnerColumnSpan(mapping);
}
public virtual void CreateForeignKey()
{
}
public virtual FetchMode FetchMode
{
get { return FetchMode.Select; }
// Needed so that subclasses have something to override
set { throw new NotSupportedException(); }
}
public bool IsAlternateUniqueKey
{
get { return isAlternateUniqueKey; }
set { isAlternateUniqueKey = value; }
}
public virtual void SetTypeUsingReflection(string className, string propertyName, string accesorName)
{
if (typeName == null)
{
if (className == null)
{
throw new MappingException("you must specify types for a dynamic entity: " + propertyName);
}
try
{
typeName = ReflectHelper.ReflectedPropertyClass(className, propertyName, accesorName).AssemblyQualifiedName;
}
catch (HibernateException he)
{
throw new MappingException("Problem trying to set property type by reflection", he);
}
}
}
public virtual object Accept(IValueVisitor visitor)
{
return visitor.Accept(this);
}
#endregion
public virtual void AddColumn(Column column)
{
if (!columns.Contains(column))
columns.Add(column);
column.Value = this;
column.TypeIndex = columns.Count - 1;
type = null; // invalidate type
}
public virtual void AddFormula(Formula formula)
{
columns.Add(formula);
}
public override string ToString()
{
return string.Format("{0}({1})", GetType().FullName, StringHelper.CollectionToString(columns));
}
}
}