-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPreparedStatement.java
More file actions
183 lines (148 loc) · 4.13 KB
/
PreparedStatement.java
File metadata and controls
183 lines (148 loc) · 4.13 KB
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
package JDBCExcel;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PreparedStatement extends BasePreparedStatement {
ExcelReader excel;
Connection connection;
String query;
List<Object> parameters;
Statement statement;
ResultSet resultSet;
List<Object> resultSetRow = new ArrayList<>();
DatabaseMetaData dbmd;
SQL sql = new SQL();
Map<String,String> tableCols = new HashMap<>();
List<String> queryCols = new ArrayList<>();
Integer rowFrom;
Integer rowTo;
Integer rowNumber = 0;
Integer limit = Integer.MAX_VALUE;
Integer count = 0;
Integer colFrom;
Integer colTo;
public PreparedStatement (Connection con,String stmt) {
//System.err.println ("PreparedStatement " + stmt);
this.connection = con;
this.query = stmt;
this.parameters = new ArrayList<>();
}
@Override
public void close() {
this.resultSet = null;
this.sql = null;
this.tableCols = null;
this.queryCols = null;
try {
excel.closeSheet();
}
catch (Exception ex) {
System.err.println ("ExcelReader.close(): " + ex.getMessage());
}
}
@Override
public boolean execute() throws SQLException {
this.resultSet = executeQuery();
return this.resultSet != null;
}
@Override
public ResultSet executeQuery() throws SQLException {
// Parse the query.
sql.parse (this.query);
List<String> tbl = sql.getTables();
// Find the table worksheet.
String schemaName = "";
String tableName = tbl.get(0);
if (tableName.contains(".")) {
String[] parts = tableName.split("[.]");
schemaName = parts[0];
tableName = parts[1];
}
schemaName = Util.unQuote(schemaName);
tableName = Util.unQuote(tableName);
// Get the named sheet as a table.
excel = connection.excel;
try {
excel.openSheet (tableName);
}
catch (Exception ex) {
throw new SQLException ("Table [" + tableName + "] not found.");
}
// Get the columns for the FROM table.
List<String> validCols = new ArrayList<>();
try {
Map<String,String> row = excel.getRow();
for (Map.Entry<String,String> entry: row.entrySet()) {
String name = Util.unQuote(entry.getValue());
String ref = excel.getColumnRef(entry.getKey());
validCols.add (name);
tableCols.put (name,ref);
}
}
catch (Exception ex) {
throw new SQLException ("Error reading column headings.");
}
// Validate and expand column list.
sql.validate (validCols);
this.queryCols = sql.getColumnList();
this.limit = sql.getLimit();
this.count = 0;
// Add the column name or alias as the result set label.
ResultSet rs = new ResultSet (this);
for (String col: queryCols) {
String label = sql.getColumnLabel(col);
label = Util.unQuote(label);
rs.addColumn (label);
}
// Return the result set.
return rs;
}
@Override
public ResultSet getResultSet() {
return resultSet;
}
//--------------------------------------------------------------------------
// next() Return next row of values.
//--------------------------------------------------------------------------
public List<Object> next() {
Map<String,String> row = null;
// Get the next row from Excel
try {
row = excel.getRow();
}
catch (Exception ex) {
System.err.println ("ExcelReader.next(): " + ex.getMessage());
return null;
}
// Consider a blank line to be end.
if (row == null) return null;
// Return NULL if we've passed the query limit.
if (count >= limit) return null;
++ count;
// Set up the return values.
resultSetRow.clear();
for (String colName: queryCols) {
if (colName.startsWith("'")) {
colName = Util.unQuote(colName);
resultSetRow.add (colName);
}
else {
colName = Util.unQuote(colName);
String colRef = tableCols.get(colName);
String value = row.getOrDefault(colRef, null);
//System.err.println (colName + ' ' + colRef + ' ' + value);
resultSetRow.add (value);
}
}
return resultSetRow;
}
@Override
public void setFetchSize (int i) {
}
@Override
public void setObject (int parameterIndex,Object object) {
parameters.add (object);
}
}