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.Data;
|
21
|
|
using System.Data.SqlTypes;
|
22
|
|
using System.Reflection;
|
23
|
|
using System.Text.RegularExpressions;
|
24
|
|
using Seasar.Extension.ADO;
|
25
|
|
using Seasar.Extension.ADO.Types;
|
26
|
|
using Seasar.Framework.Exceptions;
|
27
|
|
using Seasar.Framework.Util;
|
28
|
|
using Nullables;
|
29
|
|
|
30
|
|
namespace Seasar.Extension.ADO.Impl
|
31
|
|
{
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
public class BasicHandler
|
36
|
|
{
|
37
|
|
private IDataSource dataSource;
|
38
|
|
private string sql;
|
39
|
|
private MatchCollection sqlParameters;
|
40
|
|
private ICommandFactory commandFactory = BasicCommandFactory.INSTANCE;
|
41
|
|
private int commandTimeout = -1;
|
42
|
|
|
43
|
16
|
public BasicHandler()
|
44
|
|
{
|
45
|
|
}
|
46
|
|
|
47
|
10
|
public BasicHandler(IDataSource ds, string sql)
|
48
|
|
: this(ds, sql, BasicCommandFactory.INSTANCE)
|
49
|
|
{
|
50
|
|
}
|
51
|
|
|
52
|
10
|
public BasicHandler(IDataSource ds, string sql, ICommandFactory commandFactory)
|
53
|
|
{
|
54
|
10
|
this.DataSource = ds;
|
55
|
10
|
this.Sql = sql;
|
56
|
10
|
this.CommandFactory = commandFactory;
|
57
|
|
}
|
58
|
|
|
59
|
|
public IDataSource DataSource
|
60
|
|
{
|
61
|
52
|
get { return this.dataSource; }
|
62
|
42
|
set { this.dataSource = value; }
|
63
|
|
}
|
64
|
|
|
65
|
|
public string Sql
|
66
|
|
{
|
67
|
0
|
get { return this.sql; }
|
68
|
42
|
set
|
69
|
|
{
|
70
|
42
|
this.sql = value;
|
71
|
42
|
Regex regex = new Regex(@"(@\w+|:\w+|\?)");
|
72
|
42
|
sqlParameters = regex.Matches(sql);
|
73
|
|
}
|
74
|
|
}
|
75
|
|
|
76
|
|
public ICommandFactory CommandFactory
|
77
|
|
{
|
78
|
0
|
get { return commandFactory; }
|
79
|
26
|
set { commandFactory = value; }
|
80
|
|
}
|
81
|
|
|
82
|
0
|
public int CommandTimeout
|
83
|
|
{
|
84
|
|
get { return commandTimeout; }
|
85
|
|
set { commandTimeout = value; }
|
86
|
|
}
|
87
|
|
|
88
|
|
protected IDbConnection Connection
|
89
|
|
{
|
90
|
26
|
get
|
91
|
|
{
|
92
|
0
|
if(this.dataSource == null) throw new EmptyRuntimeException("dataSource");
|
93
|
26
|
return DataSourceUtil.GetConnection(this.dataSource);
|
94
|
|
}
|
95
|
|
}
|
96
|
|
|
97
|
26
|
protected virtual IDbCommand Command(IDbConnection connection)
|
98
|
|
{
|
99
|
0
|
if(this.sql == null) throw new EmptyRuntimeException("sql");
|
100
|
26
|
IDbCommand cmd = connection.CreateCommand();
|
101
|
26
|
string changeSignSql = this.sql;
|
102
|
26
|
switch (DataProviderUtil.GetBindVariableType(cmd))
|
103
|
|
{
|
104
|
|
case BindVariableType.AtmarkWithParam:
|
105
|
26
|
changeSignSql = GetChangeSignCommandText(this.sql, "@");
|
106
|
26
|
break;
|
107
|
|
case BindVariableType.Question:
|
108
|
0
|
changeSignSql = GetCommandText(this.sql);
|
109
|
0
|
break;
|
110
|
|
case BindVariableType.QuestionWithParam:
|
111
|
0
|
changeSignSql = GetChangeSignCommandText(this.sql, "?");
|
112
|
0
|
break;
|
113
|
|
case BindVariableType.ColonWithParam:
|
114
|
0
|
changeSignSql = GetChangeSignCommandText(this.sql, ":");
|
115
|
0
|
break;
|
116
|
|
case BindVariableType.ColonWithParamToLower:
|
117
|
0
|
changeSignSql = GetChangeSignCommandText(this.sql, ":");
|
118
|
0
|
changeSignSql = changeSignSql.ToLower();
|
119
|
0
|
break;
|
120
|
|
}
|
121
|
26
|
cmd.CommandText = changeSignSql;
|
122
|
0
|
if (this.commandTimeout > -1) cmd.CommandTimeout = this.commandTimeout;
|
123
|
26
|
return cmd;
|
124
|
|
}
|
125
|
|
|
126
|
26
|
protected void BindArgs(IDbCommand command, object[] args, Type[] argTypes,
|
127
|
|
string[] argNames)
|
128
|
|
{
|
129
|
26
|
if(args == null) return;
|
130
|
39
|
for(int i = 0; i < args.Length; ++i)
|
131
|
|
{
|
132
|
25
|
IValueType valueType = ValueTypes.GetValueType(argTypes[i]);
|
133
|
25
|
try
|
134
|
|
{
|
135
|
25
|
valueType.BindValue(command, argNames[i], args[i]);
|
136
|
|
}
|
137
|
|
catch(Exception ex)
|
138
|
|
{
|
139
|
0
|
throw new SQLRuntimeException(ex);
|
140
|
|
}
|
141
|
|
}
|
142
|
|
}
|
143
|
|
|
144
|
20
|
protected Type[] GetArgTypes(object[] args)
|
145
|
|
{
|
146
|
20
|
if (args == null)
|
147
|
|
{
|
148
|
12
|
return null;
|
149
|
|
}
|
150
|
8
|
Type[] argTypes = new Type[args.Length];
|
151
|
22
|
for (int i = 0; i < args.Length; ++i)
|
152
|
|
{
|
153
|
14
|
object arg = args[i];
|
154
|
14
|
if (arg != null)
|
155
|
|
{
|
156
|
11
|
argTypes[i] = arg.GetType();
|
157
|
|
}
|
158
|
|
}
|
159
|
8
|
return argTypes;
|
160
|
|
}
|
161
|
|
|
162
|
20
|
protected string[] GetArgNames()
|
163
|
|
{
|
164
|
20
|
string[] argNames = new string[sqlParameters.Count];
|
165
|
34
|
for (int i = 0; i < argNames.Length; ++i)
|
166
|
|
{
|
167
|
14
|
argNames[i] = (sqlParameters[i].Value.Length == 1) ? Convert.ToString(i) : sqlParameters[i].Value.Substring(1);
|
168
|
|
}
|
169
|
20
|
return argNames;
|
170
|
|
}
|
171
|
|
|
172
|
26
|
protected string GetCompleteSql(object[] args)
|
173
|
|
{
|
174
|
26
|
if(args == null || args.Length == 0) return this.sql;
|
175
|
14
|
return GetCompleteSql(sql, args);
|
176
|
|
}
|
177
|
|
|
178
|
14
|
private string GetCompleteSql(string sql, object[] args)
|
179
|
|
{
|
180
|
14
|
return ReplaceSql(sql, args, sqlParameters);
|
181
|
|
}
|
182
|
|
|
183
|
14
|
private string ReplaceSql(string sql, object[] args, MatchCollection matches)
|
184
|
|
{
|
185
|
39
|
for(int i = 0; i < matches.Count; ++i)
|
186
|
|
{
|
187
|
25
|
string capture = matches[i].Captures[0].Value;
|
188
|
25
|
sql = ReplaceAtFirstElement(sql, capture, GetBindVariableText(args[i]));
|
189
|
|
}
|
190
|
14
|
return sql;
|
191
|
|
}
|
192
|
|
|
193
|
0
|
private string GetCommandText(string sql)
|
194
|
|
{
|
195
|
|
return ReplaceSql(sql, "?", sqlParameters);
|
196
|
|
}
|
197
|
|
|
198
|
26
|
private string GetChangeSignCommandText(string sql, string sign)
|
199
|
|
{
|
200
|
26
|
string text = sql;
|
201
|
51
|
for (int i = 0; i < sqlParameters.Count; ++i)
|
202
|
|
{
|
203
|
0
|
if (!sqlParameters[i].Success) continue;
|
204
|
25
|
string parameterName = (sqlParameters[i].Value.Length == 1) ? sign + i : sign + sqlParameters[i].Value.Substring(1);
|
205
|
25
|
text = ReplaceAtFirstElement(text, sqlParameters[i].Value, parameterName);
|
206
|
|
}
|
207
|
26
|
return text;
|
208
|
|
}
|
209
|
|
|
210
|
0
|
private string ReplaceSql(string sql, string newValue, MatchCollection matches)
|
211
|
|
{
|
212
|
|
for(int i = 0; i < matches.Count; ++i)
|
213
|
|
{
|
214
|
|
string capture = matches[i].Captures[0].Value;
|
215
|
|
sql = ReplaceAtFirstElement(sql, capture, newValue);
|
216
|
|
}
|
217
|
|
return sql;
|
218
|
|
}
|
219
|
|
|
220
|
50
|
private string ReplaceAtFirstElement(string source, string original, string replace)
|
221
|
|
{
|
222
|
50
|
string pattern = original.Replace("?", "\\?");
|
223
|
50
|
Regex regexp = new Regex(pattern, RegexOptions.IgnoreCase);
|
224
|
50
|
return regexp.Replace(source, replace, 1);
|
225
|
|
}
|
226
|
|
|
227
|
25
|
protected string GetBindVariableText(object bindVariable)
|
228
|
|
{
|
229
|
25
|
if(bindVariable is INullable)
|
230
|
|
{
|
231
|
0
|
INullable nullable = bindVariable as INullable;
|
232
|
0
|
if (nullable.IsNull)
|
233
|
|
{
|
234
|
|
return GetBindVariableText(null);
|
235
|
|
}
|
236
|
|
else
|
237
|
|
{
|
238
|
|
PropertyInfo pi = bindVariable.GetType().GetProperty("Value");
|
239
|
|
return GetBindVariableText(pi.GetValue(bindVariable, null));
|
240
|
|
}
|
241
|
|
}
|
242
|
25
|
else if (bindVariable is INullableType)
|
243
|
|
{
|
244
|
0
|
INullableType nullable = bindVariable as INullableType;
|
245
|
0
|
if (!nullable.HasValue)
|
246
|
|
{
|
247
|
|
return GetBindVariableText(null);
|
248
|
|
}
|
249
|
|
else
|
250
|
|
{
|
251
|
|
PropertyInfo pi = bindVariable.GetType().GetProperty("Value");
|
252
|
|
return GetBindVariableText(pi.GetValue(bindVariable, null));
|
253
|
|
}
|
254
|
|
}
|
255
|
25
|
else if(bindVariable is string)
|
256
|
|
{
|
257
|
7
|
return "'" + bindVariable + "'";
|
258
|
|
}
|
259
|
18
|
else if(bindVariable == null)
|
260
|
|
{
|
261
|
4
|
return "null";
|
262
|
|
}
|
263
|
14
|
else if(bindVariable.GetType().IsPrimitive)
|
264
|
|
{
|
265
|
12
|
return bindVariable.ToString();
|
266
|
|
}
|
267
|
2
|
else if(bindVariable is decimal)
|
268
|
|
{
|
269
|
2
|
return bindVariable.ToString();
|
270
|
|
}
|
271
|
0
|
else if(bindVariable is DateTime)
|
272
|
|
{
|
273
|
|
if((DateTime) bindVariable == ((DateTime) bindVariable).Date)
|
274
|
|
{
|
275
|
|
return "'" + ((DateTime) bindVariable).ToString("yyyy-MM-dd") + "'";
|
276
|
|
}
|
277
|
|
else
|
278
|
|
{
|
279
|
|
return "'" + ((DateTime) bindVariable).ToString("yyyy-MM-dd HH.mm.ss") + "'";
|
280
|
|
}
|
281
|
|
}
|
282
|
|
else if(bindVariable is bool)
|
283
|
|
{
|
284
|
|
return bindVariable.ToString();
|
285
|
|
}
|
286
|
|
else
|
287
|
|
{
|
288
|
|
return "'" + bindVariable.ToString() + "'";
|
289
|
|
}
|
290
|
|
}
|
291
|
|
}
|
292
|
|
}
|
293
|
|
|