1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Collections;
|
21
|
|
using System.Reflection;
|
22
|
|
using Seasar.Dao.Attrs;
|
23
|
|
using Seasar.Framework.Util;
|
24
|
|
|
25
|
|
namespace Seasar.Dao.Id
|
26
|
|
{
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
public class IdentifierGeneratorFactory
|
31
|
|
{
|
32
|
|
private static Hashtable generatorTypes = new Hashtable();
|
33
|
|
|
34
|
1
|
static IdentifierGeneratorFactory()
|
35
|
|
{
|
36
|
1
|
AddIdentifierGeneratorType("assigned", typeof(AssignedIdentifierGenerator));
|
37
|
1
|
AddIdentifierGeneratorType("identity", typeof(IdentityIdentifierGenerator));
|
38
|
1
|
AddIdentifierGeneratorType("sequence", typeof(SequenceIdentifierGenerator));
|
39
|
|
}
|
40
|
|
|
41
|
0
|
private IdentifierGeneratorFactory()
|
42
|
|
{
|
43
|
|
}
|
44
|
|
|
45
|
3
|
public static void AddIdentifierGeneratorType(string name, Type type)
|
46
|
|
{
|
47
|
3
|
generatorTypes[name] = type;
|
48
|
|
}
|
49
|
|
|
50
|
42
|
public static IIdentifierGenerator CreateIdentifierGenerator(
|
51
|
|
string propertyName, IDbms dbms)
|
52
|
|
{
|
53
|
42
|
return CreateIdentifierGenerator(propertyName, dbms, null);
|
54
|
|
}
|
55
|
|
|
56
|
47
|
public static IIdentifierGenerator CreateIdentifierGenerator(
|
57
|
|
string propertyName, IDbms dbms, IDAttribute idAttr)
|
58
|
|
{
|
59
|
47
|
if(idAttr == null)
|
60
|
43
|
return new AssignedIdentifierGenerator(propertyName, dbms);
|
61
|
4
|
Type type = GetGeneratorType(idAttr.ID);
|
62
|
4
|
IIdentifierGenerator generator = CreateIdentifierGenerator(type, propertyName, dbms);
|
63
|
4
|
if(idAttr.SequenceName != null)
|
64
|
2
|
SetProperty(generator, "SequenceName", idAttr.SequenceName);
|
65
|
4
|
return generator;
|
66
|
|
}
|
67
|
|
|
68
|
4
|
protected static Type GetGeneratorType(string name)
|
69
|
|
{
|
70
|
4
|
Type type = (Type) generatorTypes[name];
|
71
|
4
|
if(type != null) return type;
|
72
|
0
|
return ClassUtil.ForName(name, AppDomain.CurrentDomain.GetAssemblies());
|
73
|
|
}
|
74
|
|
|
75
|
4
|
protected static IIdentifierGenerator CreateIdentifierGenerator(
|
76
|
|
Type type, string propertyName, IDbms dbms)
|
77
|
|
{
|
78
|
4
|
ConstructorInfo constructor =
|
79
|
|
ClassUtil.GetConstructorInfo(type, new Type[] { typeof(string), typeof(IDbms) });
|
80
|
4
|
return (IIdentifierGenerator)
|
81
|
|
ConstructorUtil.NewInstance(constructor, new object[] { propertyName, dbms });
|
82
|
|
}
|
83
|
|
|
84
|
2
|
protected static void SetProperty(IIdentifierGenerator generator, string propertyName, string value)
|
85
|
|
{
|
86
|
2
|
PropertyInfo property = generator.GetType().GetProperty(propertyName);
|
87
|
2
|
property.SetValue(generator, value, null);
|
88
|
|
}
|
89
|
|
}
|
90
|
|
}
|
91
|
|
|