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 System.Text;
|
23
|
|
using Seasar.Framework.Log;
|
24
|
|
|
25
|
|
namespace Seasar.Dao.Context
|
26
|
|
{
|
27
|
|
public class CommandContextImpl : ICommandContext
|
28
|
|
{
|
29
|
|
private static readonly Logger logger = Logger.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
30
|
|
|
31
|
|
Hashtable args = new Hashtable( new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer() );
|
32
|
|
Hashtable argTypes = new Hashtable( new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer() );
|
33
|
|
Hashtable argNames = new Hashtable( new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer() );
|
34
|
|
|
35
|
|
private StringBuilder sqlBuf = new StringBuilder(100);
|
36
|
|
private IList bindVariables = new ArrayList();
|
37
|
|
private IList bindVariableTypes = new ArrayList();
|
38
|
|
private IList bindVariableNames = new ArrayList();
|
39
|
|
private bool enabled = true;
|
40
|
|
private ICommandContext parent;
|
41
|
|
|
42
|
39
|
public CommandContextImpl()
|
43
|
|
{
|
44
|
|
}
|
45
|
|
|
46
|
6
|
public CommandContextImpl(ICommandContext parent)
|
47
|
|
{
|
48
|
6
|
this.parent = parent;
|
49
|
6
|
this.enabled = false;
|
50
|
|
}
|
51
|
|
|
52
|
65
|
public object GetArg(string name)
|
53
|
|
{
|
54
|
65
|
if (this.args.ContainsKey(name))
|
55
|
|
{
|
56
|
46
|
return this.args[name];
|
57
|
|
}
|
58
|
19
|
else if (this.parent != null)
|
59
|
|
{
|
60
|
14
|
return this.parent.GetArg(name);
|
61
|
|
}
|
62
|
|
else
|
63
|
|
{
|
64
|
5
|
if (this.args.Count == 1)
|
65
|
|
{
|
66
|
0
|
return this.args[0];
|
67
|
|
}
|
68
|
5
|
logger.Log("WDAO0001", new object[] { name });
|
69
|
5
|
return null;
|
70
|
|
}
|
71
|
|
}
|
72
|
|
|
73
|
34
|
public Type GetArgType(string name)
|
74
|
|
{
|
75
|
34
|
if (this.argTypes.ContainsKey(name))
|
76
|
|
{
|
77
|
28
|
return (Type) this.argTypes[name];
|
78
|
|
}
|
79
|
6
|
else if (this.parent != null)
|
80
|
|
{
|
81
|
6
|
return this.parent.GetArgType(name);
|
82
|
|
}
|
83
|
|
else
|
84
|
|
{
|
85
|
0
|
if (this.argTypes.Count == 1)
|
86
|
|
{
|
87
|
|
return (Type) this.argTypes[0];
|
88
|
|
}
|
89
|
0
|
logger.Log("WDAO0001", new object[] { name });
|
90
|
0
|
return null;
|
91
|
|
}
|
92
|
|
}
|
93
|
|
|
94
|
39
|
public void AddArg(string name, object arg, Type argType)
|
95
|
|
{
|
96
|
39
|
if (this.args.ContainsKey(name))
|
97
|
|
{
|
98
|
1
|
this.args.Remove(name);
|
99
|
|
}
|
100
|
39
|
this.args.Add(name, arg);
|
101
|
|
|
102
|
39
|
if (this.argTypes.ContainsKey(name))
|
103
|
|
{
|
104
|
1
|
this.argTypes.Remove(name);
|
105
|
|
}
|
106
|
39
|
this.argTypes.Add(name, argType);
|
107
|
|
|
108
|
39
|
if (this.argNames.ContainsKey(name))
|
109
|
|
{
|
110
|
1
|
this.argNames.Remove(name);
|
111
|
|
}
|
112
|
39
|
this.argNames.Add(name, name);
|
113
|
|
}
|
114
|
|
|
115
|
|
public string Sql
|
116
|
|
{
|
117
|
46
|
get { return this.sqlBuf.ToString(); }
|
118
|
|
}
|
119
|
|
|
120
|
|
public object[] BindVariables
|
121
|
|
{
|
122
|
25
|
get
|
123
|
|
{
|
124
|
25
|
object[] variables = new object[this.bindVariables.Count];
|
125
|
25
|
this.bindVariables.CopyTo(variables, 0);
|
126
|
25
|
return variables;
|
127
|
|
}
|
128
|
|
}
|
129
|
|
|
130
|
|
public Type[] BindVariableTypes
|
131
|
|
{
|
132
|
16
|
get
|
133
|
|
{
|
134
|
16
|
Type[] variables = new Type[this.bindVariableTypes.Count];
|
135
|
16
|
this.bindVariableTypes.CopyTo(variables, 0);
|
136
|
16
|
return variables;
|
137
|
|
}
|
138
|
|
}
|
139
|
|
|
140
|
|
public string[] BindVariableNames
|
141
|
|
{
|
142
|
16
|
get
|
143
|
|
{
|
144
|
16
|
string[] variableNames = new string[this.bindVariableNames.Count];
|
145
|
16
|
this.bindVariableNames.CopyTo(variableNames, 0);
|
146
|
16
|
return variableNames;
|
147
|
|
}
|
148
|
|
}
|
149
|
|
|
150
|
81
|
public ICommandContext AddSql(string sql)
|
151
|
|
{
|
152
|
81
|
this.sqlBuf.Append(sql);
|
153
|
81
|
return this;
|
154
|
|
}
|
155
|
|
|
156
|
45
|
public ICommandContext AddSql(string sql, object bindVariable,
|
157
|
|
Type bindVariableType, string bindVariableName)
|
158
|
|
{
|
159
|
|
|
160
|
45
|
this.sqlBuf.Append(sql);
|
161
|
45
|
this.bindVariables.Add(bindVariable);
|
162
|
45
|
this.bindVariableTypes.Add(bindVariableType);
|
163
|
45
|
this.bindVariableNames.Add(bindVariableName);
|
164
|
45
|
return this;
|
165
|
|
}
|
166
|
|
|
167
|
32
|
public ICommandContext AddSql(object bindVariable, Type bindVariableType, string bindVariableName)
|
168
|
|
{
|
169
|
32
|
AddSql("@" + bindVariableName, bindVariable, bindVariableType, bindVariableName);
|
170
|
32
|
return this;
|
171
|
|
}
|
172
|
|
|
173
|
5
|
public ICommandContext AddSql(string sql, object[] bindVariables,
|
174
|
|
Type[] bindVariableTypes, string[] bindVariableNames)
|
175
|
|
{
|
176
|
|
|
177
|
5
|
this.sqlBuf.Append(sql);
|
178
|
11
|
for (int i = 0; i < bindVariables.Length; ++i)
|
179
|
|
{
|
180
|
6
|
this.bindVariables.Add(bindVariables[i]);
|
181
|
6
|
this.bindVariableTypes.Add(bindVariableTypes[i]);
|
182
|
6
|
this.bindVariableNames.Add(bindVariableNames[i]);
|
183
|
|
}
|
184
|
5
|
return this;
|
185
|
|
}
|
186
|
|
|
187
|
13
|
public ICommandContext AppendSql(object bindVariable, Type bindVariableType, string bindVariableName)
|
188
|
|
{
|
189
|
13
|
AddSql(", @" + bindVariableName, bindVariable, bindVariableType, bindVariableName);
|
190
|
13
|
return this;
|
191
|
|
}
|
192
|
|
|
193
|
|
public bool IsEnabled
|
194
|
|
{
|
195
|
9
|
get { return this.enabled; }
|
196
|
20
|
set { this.enabled = value; }
|
197
|
|
|
198
|
|
}
|
199
|
|
}
|
200
|
|
}
|
201
|
|
|