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.Data;
|
22
|
|
using System.Reflection;
|
23
|
|
using Seasar.Extension.ADO;
|
24
|
|
using Seasar.Extension.ADO.Impl;
|
25
|
|
using Seasar.Framework.Log;
|
26
|
|
using Seasar.Framework.Util;
|
27
|
|
|
28
|
|
namespace Seasar.Dao.Impl
|
29
|
|
{
|
30
|
|
public abstract class AbstractAutoHandler : BasicHandler, IUpdateHandler
|
31
|
|
{
|
32
|
|
private static Logger logger = Logger.GetLogger(typeof(AbstractAutoHandler));
|
33
|
|
private IBeanMetaData beanMetaData;
|
34
|
|
private object[] bindVariables;
|
35
|
|
private Type[] bindVariableTypes;
|
36
|
|
private string[] bindVariableNames;
|
37
|
|
private DateTime timestamp = DateTime.MinValue;
|
38
|
|
private Int32 versionNo = Int32.MinValue;
|
39
|
|
private IPropertyType[] propertyTypes;
|
40
|
|
|
41
|
14
|
public AbstractAutoHandler(IDataSource dataSource, ICommandFactory commandFactory,
|
42
|
|
IBeanMetaData beanMetaData, IPropertyType[] propertyTypes)
|
43
|
|
{
|
44
|
14
|
this.DataSource = dataSource;
|
45
|
14
|
this.CommandFactory = commandFactory;
|
46
|
14
|
this.beanMetaData = beanMetaData;
|
47
|
14
|
this.propertyTypes = propertyTypes;
|
48
|
|
}
|
49
|
|
|
50
|
|
public IBeanMetaData BeanMetaData
|
51
|
|
{
|
52
|
103
|
get { return beanMetaData; }
|
53
|
|
}
|
54
|
|
|
55
|
0
|
protected static Logger Logger
|
56
|
|
{
|
57
|
|
get { return logger; }
|
58
|
|
}
|
59
|
|
|
60
|
|
protected object[] BindVariables
|
61
|
|
{
|
62
|
0
|
get { return bindVariables; }
|
63
|
14
|
set { bindVariables = value; }
|
64
|
|
}
|
65
|
|
|
66
|
|
protected Type[] BindVariableTypes
|
67
|
|
{
|
68
|
0
|
get { return bindVariableTypes; }
|
69
|
14
|
set { bindVariableTypes = value; }
|
70
|
|
}
|
71
|
|
|
72
|
|
protected string[] BindVariableNames
|
73
|
|
{
|
74
|
0
|
get { return bindVariableNames; }
|
75
|
14
|
set { bindVariableNames = value; }
|
76
|
|
}
|
77
|
|
|
78
|
|
protected DateTime Timestamp
|
79
|
|
{
|
80
|
11
|
get { return timestamp; }
|
81
|
0
|
set { timestamp = value; }
|
82
|
|
}
|
83
|
|
|
84
|
|
protected int VersionNo
|
85
|
|
{
|
86
|
15
|
get { return versionNo; }
|
87
|
2
|
set { versionNo = value; }
|
88
|
|
}
|
89
|
|
|
90
|
0
|
protected IPropertyType[] PropertyTypes
|
91
|
|
{
|
92
|
|
get { return propertyTypes; }
|
93
|
|
set { propertyTypes = value; }
|
94
|
|
}
|
95
|
|
|
96
|
|
#region IUpdateHandler メンバ
|
97
|
|
|
98
|
14
|
public int Execute(object[] args)
|
99
|
|
{
|
100
|
14
|
IDbConnection connection = this.Connection;
|
101
|
0
|
if(connection.State != ConnectionState.Open) connection.Open();
|
102
|
|
|
103
|
14
|
try
|
104
|
|
{
|
105
|
14
|
return Execute(connection, args[0]);
|
106
|
|
}
|
107
|
|
finally
|
108
|
|
{
|
109
|
14
|
DataSourceUtil.CloseConnection(this.DataSource, connection);
|
110
|
|
}
|
111
|
|
}
|
112
|
|
|
113
|
0
|
public int Execute(object[] args, Type[] argTypes)
|
114
|
|
{
|
115
|
|
return Execute(args);
|
116
|
|
}
|
117
|
|
|
118
|
0
|
public int Execute(object[] args, Type[] argTypes, string[] argNames)
|
119
|
|
{
|
120
|
|
return Execute(args);
|
121
|
|
}
|
122
|
|
|
123
|
|
#endregion
|
124
|
|
|
125
|
14
|
protected int Execute(IDbConnection connection, object bean)
|
126
|
|
{
|
127
|
14
|
PreUpdateBean(bean);
|
128
|
14
|
SetupBindVariables(bean);
|
129
|
0
|
if(logger.IsDebugEnabled) logger.Debug(GetCompleteSql(bindVariables));
|
130
|
14
|
IDbCommand cmd = Command(connection);
|
131
|
14
|
int ret = -1;
|
132
|
14
|
try
|
133
|
|
{
|
134
|
14
|
BindArgs(cmd, bindVariables, bindVariableTypes, bindVariableNames);
|
135
|
14
|
ret = CommandUtil.ExecuteNonQuery(this.DataSource, cmd);
|
136
|
|
}
|
137
|
|
finally
|
138
|
|
{
|
139
|
14
|
CommandUtil.Close(cmd);
|
140
|
|
}
|
141
|
14
|
PostUpdateBean(bean);
|
142
|
14
|
return ret;
|
143
|
|
}
|
144
|
|
|
145
|
9
|
protected virtual void PreUpdateBean(object bean)
|
146
|
|
{
|
147
|
|
}
|
148
|
|
|
149
|
3
|
protected virtual void PostUpdateBean(object bean)
|
150
|
|
{
|
151
|
|
}
|
152
|
|
|
153
|
|
protected abstract void SetupBindVariables(object bean);
|
154
|
|
|
155
|
5
|
protected void SetupInsertBindVariables(object bean)
|
156
|
|
{
|
157
|
5
|
ArrayList varList = new ArrayList();
|
158
|
5
|
ArrayList varTypeList = new ArrayList();
|
159
|
5
|
ArrayList varNameList = new ArrayList();
|
160
|
25
|
for(int i = 0; i < propertyTypes.Length; ++i)
|
161
|
|
{
|
162
|
20
|
IPropertyType pt = propertyTypes[i];
|
163
|
20
|
if(string.Compare(pt.PropertyName, BeanMetaData.TimestampPropertyName, true) == 0)
|
164
|
|
{
|
165
|
0
|
this.Timestamp = DateTime.Now;
|
166
|
0
|
varList.Add(this.Timestamp);
|
167
|
|
}
|
168
|
20
|
else if(pt.PropertyName.Equals(this.BeanMetaData.VersionNoPropertyName))
|
169
|
|
{
|
170
|
0
|
this.VersionNo = 0;
|
171
|
0
|
varList.Add(this.VersionNo);
|
172
|
|
}
|
173
|
|
else
|
174
|
|
{
|
175
|
20
|
varList.Add(pt.PropertyInfo.GetValue(bean, null));
|
176
|
|
}
|
177
|
20
|
varTypeList.Add(pt.PropertyInfo.PropertyType);
|
178
|
20
|
varNameList.Add(pt.ColumnName);
|
179
|
|
}
|
180
|
5
|
BindVariables = varList.ToArray();
|
181
|
5
|
BindVariableTypes = (Type[]) varTypeList.ToArray(typeof(Type));
|
182
|
5
|
BindVariableNames = (string[]) varNameList.ToArray(typeof(string));
|
183
|
|
}
|
184
|
|
|
185
|
6
|
protected void SetupUpdateBindVariables(object bean)
|
186
|
|
{
|
187
|
6
|
ArrayList varList = new ArrayList();
|
188
|
6
|
ArrayList varTypeList = new ArrayList();
|
189
|
6
|
ArrayList varNameList = new ArrayList();
|
190
|
25
|
for(int i = 0; i < propertyTypes.Length; ++i)
|
191
|
|
{
|
192
|
19
|
IPropertyType pt = propertyTypes[i];
|
193
|
19
|
if(string.Compare(pt.PropertyName, BeanMetaData.TimestampPropertyName, true) == 0)
|
194
|
|
{
|
195
|
0
|
this.Timestamp = DateTime.Now;
|
196
|
0
|
varList.Add(this.Timestamp);
|
197
|
|
}
|
198
|
19
|
else if(string.Compare(pt.PropertyName, BeanMetaData.VersionNoPropertyName, true) ==0)
|
199
|
|
{
|
200
|
2
|
object value = pt.PropertyInfo.GetValue(bean, null);
|
201
|
2
|
int intValue = Convert.ToInt32(value) + 1;
|
202
|
2
|
this.VersionNo = intValue;
|
203
|
2
|
varList.Add(this.VersionNo);
|
204
|
|
}
|
205
|
|
else
|
206
|
|
{
|
207
|
17
|
varList.Add(pt.PropertyInfo.GetValue(bean, null));
|
208
|
|
}
|
209
|
19
|
varTypeList.Add(pt.PropertyInfo.PropertyType);
|
210
|
19
|
varNameList.Add(pt.ColumnName);
|
211
|
|
}
|
212
|
6
|
AddAutoUpdateWhereBindVariables(varList, varTypeList, varNameList, bean);
|
213
|
6
|
BindVariables = varList.ToArray();
|
214
|
6
|
BindVariableTypes = (Type[]) varTypeList.ToArray(typeof(Type));
|
215
|
6
|
BindVariableNames = (string[]) varNameList.ToArray(typeof(string));
|
216
|
|
}
|
217
|
|
|
218
|
3
|
protected void SetupDeleteBindVariables(object bean)
|
219
|
|
{
|
220
|
3
|
ArrayList varList = new ArrayList();
|
221
|
3
|
ArrayList varTypeList = new ArrayList();
|
222
|
3
|
ArrayList varNameList = new ArrayList();
|
223
|
3
|
AddAutoUpdateWhereBindVariables(varList, varTypeList, varNameList, bean);
|
224
|
3
|
BindVariables = varList.ToArray();
|
225
|
3
|
BindVariableTypes = (Type[]) varTypeList.ToArray(typeof(Type));
|
226
|
3
|
BindVariableNames = (string[]) varNameList.ToArray(typeof(string));
|
227
|
|
}
|
228
|
|
|
229
|
9
|
protected void AddAutoUpdateWhereBindVariables(ArrayList varList, ArrayList varTypeList,
|
230
|
|
ArrayList varNameList, object bean)
|
231
|
|
{
|
232
|
9
|
IBeanMetaData bmd = this.BeanMetaData;
|
233
|
18
|
for(int i = 0; i < bmd.PrimaryKeySize; ++i)
|
234
|
|
{
|
235
|
9
|
IPropertyType pt = bmd.GetPropertyTypeByColumnName(bmd.GetPrimaryKey(i));
|
236
|
9
|
PropertyInfo pi = pt.PropertyInfo;
|
237
|
9
|
varList.Add(pi.GetValue(bean, null));
|
238
|
9
|
varTypeList.Add(pi.PropertyType);
|
239
|
9
|
varNameList.Add(pt.ColumnName);
|
240
|
|
}
|
241
|
9
|
if(bmd.HasVersionNoPropertyType)
|
242
|
|
{
|
243
|
4
|
IPropertyType pt = bmd.VersionNoPropertyType;
|
244
|
4
|
PropertyInfo pi = pt.PropertyInfo;
|
245
|
4
|
varList.Add(pi.GetValue(bean, null));
|
246
|
4
|
varTypeList.Add(pi.PropertyType);
|
247
|
4
|
varNameList.Add(BeanMetaData.VersionNoBindingName);
|
248
|
|
}
|
249
|
9
|
if(bmd.HasTimestampPropertyType)
|
250
|
|
{
|
251
|
0
|
IPropertyType pt = bmd.TimestampPropertyType;
|
252
|
0
|
PropertyInfo pi = pt.PropertyInfo;
|
253
|
0
|
varList.Add(pi.GetValue(bean, null));
|
254
|
0
|
varTypeList.Add(pi.PropertyType);
|
255
|
0
|
varNameList.Add(BeanMetaData.TimestampBindingName);
|
256
|
|
}
|
257
|
|
}
|
258
|
|
|
259
|
11
|
protected void UpdateTimestampIfNeed(object bean)
|
260
|
|
{
|
261
|
11
|
if(Timestamp != DateTime.MinValue)
|
262
|
|
{
|
263
|
0
|
PropertyInfo pi = BeanMetaData.TimestampPropertyType.PropertyInfo;
|
264
|
0
|
pi.SetValue(bean, Timestamp, null);
|
265
|
|
}
|
266
|
|
}
|
267
|
|
|
268
|
11
|
protected void UpdateVersionNoIfNeed(object bean)
|
269
|
|
{
|
270
|
11
|
if(VersionNo != Int32.MinValue)
|
271
|
|
{
|
272
|
2
|
PropertyInfo pi = BeanMetaData.VersionNoPropertyType.PropertyInfo;
|
273
|
2
|
pi.SetValue(bean, VersionNo, null);
|
274
|
|
}
|
275
|
|
}
|
276
|
|
}
|
277
|
|
}
|
278
|
|
|