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 Seasar.Extension.ADO;
|
22
|
|
using Seasar.Framework.Exceptions;
|
23
|
|
using Seasar.Framework.Log;
|
24
|
|
using Seasar.Framework.Util;
|
25
|
|
|
26
|
|
namespace Seasar.Extension.ADO.Impl
|
27
|
|
{
|
28
|
|
public class BasicSelectHandler : BasicHandler, ISelectHandler
|
29
|
|
{
|
30
|
|
private static readonly Logger logger = Logger.GetLogger(typeof(BasicSelectHandler));
|
31
|
|
|
32
|
|
private IDataReaderFactory dataReaderFactory = BasicDataReaderFactory.INSTANCE;
|
33
|
|
private IDataReaderHandler dataReaderHandler;
|
34
|
|
private int fetchSize = 100;
|
35
|
|
private int maxRows = -1;
|
36
|
|
|
37
|
0
|
public BasicSelectHandler()
|
38
|
|
{
|
39
|
|
}
|
40
|
|
|
41
|
16
|
public BasicSelectHandler(IDataSource dataSource, string sql,
|
42
|
|
IDataReaderHandler dataReaderHandler)
|
43
|
|
: this(dataSource, sql, dataReaderHandler,
|
44
|
|
BasicCommandFactory.INSTANCE, BasicDataReaderFactory.INSTANCE)
|
45
|
|
{
|
46
|
16
|
this.DataSource = dataSource;
|
47
|
16
|
this.Sql = sql;
|
48
|
16
|
this.DataReaderHandler = dataReaderHandler;
|
49
|
|
}
|
50
|
|
|
51
|
16
|
public BasicSelectHandler(IDataSource dataSource, string sql,
|
52
|
|
IDataReaderHandler dataReaderHandler,
|
53
|
|
ICommandFactory commandFactory, IDataReaderFactory dataReaderFactory)
|
54
|
|
{
|
55
|
16
|
DataSource = dataSource;
|
56
|
16
|
Sql = sql;
|
57
|
16
|
DataReaderHandler = dataReaderHandler;
|
58
|
16
|
CommandFactory = commandFactory;
|
59
|
16
|
DataReaderFactory = dataReaderFactory;
|
60
|
|
}
|
61
|
|
|
62
|
|
public IDataReaderFactory DataReaderFactory
|
63
|
|
{
|
64
|
0
|
get { return dataReaderFactory; }
|
65
|
16
|
set { dataReaderFactory = value; }
|
66
|
|
}
|
67
|
|
|
68
|
|
public IDataReaderHandler DataReaderHandler
|
69
|
|
{
|
70
|
0
|
get { return this.dataReaderHandler; }
|
71
|
32
|
set { this.dataReaderHandler = value; }
|
72
|
|
}
|
73
|
|
|
74
|
0
|
public int FetchSize
|
75
|
|
{
|
76
|
|
get { return this.fetchSize; }
|
77
|
|
set { this.fetchSize = value; }
|
78
|
|
}
|
79
|
|
|
80
|
0
|
public int MaxRows
|
81
|
|
{
|
82
|
|
get { return this.maxRows; }
|
83
|
|
set { this.maxRows = value; }
|
84
|
|
}
|
85
|
|
|
86
|
16
|
public object Execute(object[] args)
|
87
|
|
{
|
88
|
16
|
Type[] argTypes = GetArgTypes(args);
|
89
|
16
|
string[] argNames = GetArgNames();
|
90
|
16
|
return Execute(args, argTypes, argNames);
|
91
|
|
}
|
92
|
|
|
93
|
16
|
public object Execute(object[] args, Type[] argTypes, string[] argNames)
|
94
|
|
{
|
95
|
16
|
if(logger.IsDebugEnabled) logger.Debug(this.GetCompleteSql(args));
|
96
|
16
|
IDbConnection con = this.Connection;
|
97
|
16
|
try
|
98
|
|
{
|
99
|
16
|
return this.Execute(con, args, argTypes, argNames);
|
100
|
|
}
|
101
|
|
catch(Exception ex)
|
102
|
|
{
|
103
|
0
|
throw new SQLRuntimeException(ex);
|
104
|
|
}
|
105
|
|
finally
|
106
|
|
{
|
107
|
16
|
DataSourceUtil.CloseConnection(this.DataSource, con);
|
108
|
|
}
|
109
|
|
}
|
110
|
|
|
111
|
16
|
protected object Execute(IDbConnection connection, object[] args, Type[] argTypes,
|
112
|
|
string[] argNames)
|
113
|
|
{
|
114
|
16
|
IDbCommand cmd = null;
|
115
|
16
|
try
|
116
|
|
{
|
117
|
16
|
cmd = this.Command(connection);
|
118
|
16
|
this.BindArgs(cmd, args, argTypes, argNames);
|
119
|
16
|
return this.Execute(cmd);
|
120
|
|
}
|
121
|
|
finally
|
122
|
|
{
|
123
|
16
|
CommandUtil.Close(cmd);
|
124
|
|
}
|
125
|
|
}
|
126
|
|
|
127
|
0
|
protected object[] Setup(IDbConnection con, object[] args)
|
128
|
|
{
|
129
|
|
return args;
|
130
|
|
}
|
131
|
|
|
132
|
16
|
protected override IDbCommand Command(IDbConnection connection)
|
133
|
|
{
|
134
|
16
|
IDbCommand cmd = base.Command(connection);
|
135
|
|
|
136
|
|
|
137
|
16
|
return cmd;
|
138
|
|
}
|
139
|
|
|
140
|
16
|
protected object Execute(IDbCommand cmd)
|
141
|
|
{
|
142
|
16
|
if(dataReaderHandler == null)
|
143
|
0
|
throw new EmptyRuntimeException("dataReaderHandler");
|
144
|
16
|
IDataReader dataReader = null;
|
145
|
16
|
try
|
146
|
|
{
|
147
|
16
|
if(dataReaderHandler is ObjectDataReaderHandler)
|
148
|
0
|
return CommandUtil.ExecuteScalar(this.DataSource, cmd);
|
149
|
|
else
|
150
|
|
{
|
151
|
16
|
dataReader = this.CreateDataReader(cmd);
|
152
|
16
|
return dataReaderHandler.Handle(dataReader);
|
153
|
|
}
|
154
|
|
}
|
155
|
|
finally
|
156
|
|
{
|
157
|
16
|
DataReaderUtil.Close(dataReader);
|
158
|
|
}
|
159
|
|
}
|
160
|
|
|
161
|
0
|
protected void SetupDataTable(DataTable dataTable)
|
162
|
|
{
|
163
|
|
}
|
164
|
|
|
165
|
16
|
protected IDataReader CreateDataReader(IDbCommand cmd)
|
166
|
|
{
|
167
|
16
|
return dataReaderFactory.CreateDataReader(this.DataSource, cmd);
|
168
|
|
}
|
169
|
|
}
|
170
|
|
}
|
171
|
|
|