forked from wangguanquan/eec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatementSheet.java
286 lines (264 loc) · 8.52 KB
/
StatementSheet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (c) 2017, guanquan.wang@yandex.com All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ttzero.excel.entity;
import org.ttzero.excel.processor.ParamProcessor;
import java.io.IOException;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Statement is one of the worksheet data sources, it's
* extends from {@link ResultSetSheet}, and will be obtained from
* MetaData if no header information is given. When MetaData
* cannot be obtained, the header name will be setting as 1, 2,
* and 3...
* <p>
* The Connection will not be actively closed, but the {@link java.sql.Statement}
* and {@link ResultSet} will be closed with worksheet.
*
* @see ResultSetSheet
*
* @author guanquan.wang on 2017/9/26.
*/
public class StatementSheet extends ResultSetSheet {
private PreparedStatement ps;
/**
* Constructor worksheet
*/
public StatementSheet() {
super();
}
/**
* Constructor worksheet
*
* @param name the worksheet name
*/
public StatementSheet(String name) {
super(name);
}
/**
* Constructor worksheet
*
* @param name the worksheet name
* @param columns the header info
*/
public StatementSheet(String name, final org.ttzero.excel.entity.Column... columns) {
super(name, columns);
}
/**
* Constructor worksheet
*
* @param name the worksheet name
* @param waterMark the water mark
* @param columns the header info
*/
public StatementSheet(String name, WaterMark waterMark, final org.ttzero.excel.entity.Column... columns) {
super(name, waterMark, columns);
}
/**
* Constructor worksheet
*
* @param con the Connection
* @param sql the query SQL string
*/
public StatementSheet(Connection con, String sql) {
this(null, con, sql);
}
/**
* Constructor worksheet
*
* @param name the worksheet name
* @param con the Connection
* @param sql the query SQL string
*/
public StatementSheet(String name, Connection con, String sql) {
super(name);
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
ps.setFetchSize(Integer.MIN_VALUE);
ps.setFetchDirection(ResultSet.FETCH_REVERSE);
} catch (SQLException e) {
// what("Not support fetch size value of " + Integer.MIN_VALUE);
}
if (ps == null) {
throw new ExcelWriteException("Constructor worksheet error.\nMiss the parameter Statement");
}
this.ps = ps;
}
/**
* Constructor worksheet
*
* @param con the Connection
* @param sql the query SQL string
* @param pp the sql parameter replacement function-interface
*/
public StatementSheet(Connection con, String sql, ParamProcessor pp) {
this(null, con, sql, pp);
}
/**
* Constructor worksheet
*
* @param name the worksheet name
* @param con the Connection
* @param sql the query SQL string
* @param pp the sql parameter replacement function-interface
*/
public StatementSheet(String name, Connection con, String sql, ParamProcessor pp) {
super(name);
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
ps.setFetchSize(Integer.MIN_VALUE);
ps.setFetchDirection(ResultSet.FETCH_REVERSE);
} catch (SQLException e) {
// what("Not support fetch size value of " + Integer.MIN_VALUE);
}
if (ps == null) {
throw new ExcelWriteException("Constructor worksheet error.\nMiss the parameter Statement");
}
if (pp != null) {
try {
pp.build(ps);
} catch (SQLException e) {
throw new ExcelWriteException(e);
}
}
this.ps = ps;
}
/**
* Constructor worksheet
*
* @param con the Connection
* @param sql the sql string
* @param columns the header column
*/
public StatementSheet(Connection con, String sql, org.ttzero.excel.entity.Column... columns) {
this(null, con, sql, columns);
}
/**
* Constructor worksheet
*
* @param name the worksheet name
* @param con the Connection
* @param sql the sql string
* @param columns the header column
*/
public StatementSheet(String name, Connection con, String sql, org.ttzero.excel.entity.Column... columns) {
this(name, con, sql, null, columns);
}
/**
* Constructor worksheet
*
* @param con the Connection
* @param sql the sql string
* @param pp the sql parameter replacement function-interface
* @param columns the header column
*/
public StatementSheet(Connection con, String sql, ParamProcessor pp, org.ttzero.excel.entity.Column... columns) {
this(null, con, sql, pp, columns);
}
/**
* Constructor worksheet
*
* @param name the worksheet name
* @param con the Connection
* @param sql the sql string
* @param pp the sql parameter replacement function-interface
* @param columns the header column
*/
public StatementSheet(String name, Connection con, String sql, ParamProcessor pp, org.ttzero.excel.entity.Column... columns) {
super(name, columns);
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
ps.setFetchSize(Integer.MIN_VALUE);
ps.setFetchDirection(ResultSet.FETCH_REVERSE);
} catch (SQLException e) {
// what("Not support fetch size value of " + Integer.MIN_VALUE);
}
if (ps == null) {
throw new ExcelWriteException("Constructor worksheet error.\nMiss the parameter Statement");
}
if (pp != null) {
try {
pp.build(ps);
} catch (SQLException e) {
throw new ExcelWriteException(e);
}
}
this.ps = ps;
}
/**
* Setting PreparedStatement
*
* @param ps PreparedStatement
* @return the {@link StatementSheet}
*/
public StatementSheet setPs(PreparedStatement ps) {
this.ps = ps;
return this;
}
/**
* Release resources
* will close the {@code Statement} and {@code ResultSet}
*
* @throws IOException if I/O error occur
*/
@Override
public void close() throws IOException {
super.close();
if (shouldClose && ps != null) {
try {
ps.close();
} catch (SQLException e) {
what("9006", e.getMessage());
}
}
}
/**
* write worksheet data to path
*
* @param path the storage path
* @throws IOException if I/O error occur
*/
@Override
public void writeTo(Path path) throws IOException {
if (sheetWriter != null) {
if (!copySheet) {
if (ps == null) {
throw new ExcelWriteException("Constructor worksheet error.\nMiss the parameter Statement");
}
// Execute query
try {
rs = ps.executeQuery();
} catch (SQLException e) {
throw new ExcelWriteException(e);
}
// Check the header information is exists
getAndSortHeaderColumns();
}
if (rowBlock == null) {
rowBlock = new RowBlock(getRowBlockSize());
} else rowBlock.reopen();
sheetWriter.writeTo(path);
} else {
throw new ExcelWriteException("Worksheet writer is not instanced.");
}
}
}