forked from rondunn/JDBCExcel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DatabaseMetaData.java
280 lines (231 loc) · 6.95 KB
/
DatabaseMetaData.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
package JDBCExcel;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class DatabaseMetaData extends BaseDatabaseMetaData {
protected Connection connection;
protected ResultSet rs;
protected Integer rowIndex;
protected List<List<Object>> rows = new ArrayList<>();
/**
* Constructor
* @param con Database connection
*/
public DatabaseMetaData (Connection con) {
this.connection = con;
}
/**
* Get the next row from a metadata ResultSet.
* @return
*/
public List<Object> next() {
if (rowIndex < rows.size()) {
return rows.get(rowIndex++);
}
return null;
}
/**
* Return a list of catalogs for this DBMS. This driver treats a spreadsheet
* file as a database.
* @return
*/
@Override
public ResultSet getCatalogs() {
rows.clear();
rowIndex = 0;
rs = new ResultSet(this);
rs.addColumn ("TABLE_CAT");
File dir = new File(connection.getServer());
File[] files = dir.listFiles((d, name) -> name.endsWith(".xlsx"));
for (File f: files) {
List<Object> row = new ArrayList<>();
row.add (f.getName());
rows.add (row);
}
return rs;
}
/**
* Return a list of columns for a schema/table combination. Columns are
* mapped from the spreadheet columns for a workbook.
* @param catalog
* @param schemaPattern
* @param tableNamePattern
* @param columnNamePattern
* @return
*/
@Override
public ResultSet getColumns (String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) {
rows.clear();
rowIndex = 0;
String databaseName = connection.getDatabase();
String schemaName = "Excel";
String sx = null;
if (tableNamePattern != null) sx = Util.sqlLikeToRegex(schemaPattern);
String rx = null;
if (tableNamePattern != null) rx = Util.sqlLikeToRegex(tableNamePattern);
String cx = null;
if (columnNamePattern != null) cx = Util.sqlLikeToRegex(columnNamePattern);
try {
ExcelReader excel = this.connection.excel;
List<String> sheetNames = excel.getSheetNames();
for (String sheetName: sheetNames) {
Boolean tableMatch = true;
if (sx != null && !schemaName.matches(sx)) tableMatch = false;
if (rx != null && !sheetName.matches(rx)) tableMatch = false;
if (tableMatch == true) {
excel.openSheet (sheetName);
Map<String,String> sheetRow = excel.getRow();
Integer colCount = 1;
for (Map.Entry<String,String> entry: sheetRow.entrySet()) {
String columnName = entry.getValue();
String columnRef = excel.getColumnRef(entry.getKey());
int type = java.sql.Types.VARCHAR;
String typeName = "VARCHAR";
int colSize = 255;
int decimals = 0;
List<Object> row = new ArrayList<>();
row.add (databaseName);
row.add (schemaName);
row.add (sheetName);
row.add (columnRef); // Extension
row.add (columnName);
row.add (type);
row.add (typeName);
row.add (colSize);
row.add (null);
row.add (decimals);
row.add (10);
row.add (DatabaseMetaData.columnNullable);
row.add ("");
row.add (null);
row.add (null);
row.add (null);
row.add (colSize);
row.add (colCount++);
row.add ("YES");
row.add (null);
row.add (null);
row.add (null);
row.add (null);
row.add ("");
row.add ("");
rows.add (row);
}
excel.closeSheet();
}
}
}
catch (Exception ex) {
System.err.println (ex.getMessage());
return null;
}
rs = new ResultSet(this);
rs.addColumn ("TABLE_CAT");
rs.addColumn ("TABLE_SCHEM");
rs.addColumn ("TABLE_NAME");
rs.addColumn ("COLUMN_REF");
rs.addColumn ("COLUMN_NAME");
rs.addColumn ("DATA_TYPE");
rs.addColumn ("TYPE_NAME");
rs.addColumn ("COLUMN_SIZE");
rs.addColumn ("BUFFER_LENGTH");
rs.addColumn ("DECIMAL_DIGITS");
rs.addColumn ("NUM_PREC_RADIX");
rs.addColumn ("NULLABLE");
rs.addColumn ("REMARKS");
rs.addColumn ("COLUMN_DEF");
rs.addColumn ("SQL_DATA_TYPE");
rs.addColumn ("SQL_DATETIME_SUB");
rs.addColumn ("CHAR_OCTET_LENGTH");
rs.addColumn ("ORDINAL_POSITION");
rs.addColumn ("IS_NULLABLE");
rs.addColumn ("SCOPE_CATALOG");
rs.addColumn ("SCOPE_SCHEMA");
rs.addColumn ("SCOPE_TABLE");
rs.addColumn ("SOURCE_DATA_TYPE");
rs.addColumn ("IS_AUTOINCREMENT");
rs.addColumn ("IS_GENERATED_COLUMN");
return rs;
}
/**
* Return list of schemas in database. This driver only supports one schema
* per database, named "Excel".
* @return List of schemas
*/
@Override
public ResultSet getSchemas () {
rows.clear();
rowIndex = 0;
String server = connection.getServer();
String database = connection.getDatabase();
String fileName = server + database;
List<Object> row = new ArrayList<>();
row.add (database);
row.add ("Excel");
rows.add (row);
rs = new ResultSet (this);
rs.addColumn ("TABLE_CATALOG");
rs.addColumn ("TABLE_SCHEM");
return rs;
}
/**
* Return a list of tables in the database. This driver maps each workbook
* page to a table.
* @param catalog
* @param schemaPattern
* @param tableNamePattern
* @param types
* @return
*/
@Override
public ResultSet getTables (String catalog, String schemaPattern, String tableNamePattern, String[] types) {
rows.clear();
rowIndex = 0;
String databaseName = connection.getDatabase();
String schemaName = "Excel";
String sx = null;
if (tableNamePattern != null) sx = Util.sqlLikeToRegex(schemaPattern);
String rx = null;
if (tableNamePattern != null) rx = Util.sqlLikeToRegex(tableNamePattern);
try {
ExcelReader excel = this.connection.excel;
List<String> sheetNames = excel.getSheetNames();
for (String sheetName: sheetNames) {
Boolean match = true;
if (sx != null && !schemaName.matches(sx)) match = false;
if (rx != null && !sheetName.matches(rx)) match = false;
if (match == true) {
List<Object> row = new ArrayList<>();
row.add (databaseName);
row.add (schemaName);
row.add (sheetName);
row.add ("TABLE");
row.add (null);
row.add (null);
row.add (null);
row.add (null);
row.add (null);
row.add (null);
rows.add (row);
}
}
}
catch (Exception ex) {
System.err.println (ex.getMessage());
return null;
}
rs = new ResultSet(this);
rs.addColumn ("TABLE_CAT");
rs.addColumn ("TABLE_SCHEM");
rs.addColumn ("TABLE_NAME");
rs.addColumn ("TABLE_TYPE");
rs.addColumn ("REMARKS");
rs.addColumn ("TYPE_CAT");
rs.addColumn ("TYPE_SCHEM");
rs.addColumn ("TYPE_NAME");
rs.addColumn ("SELF_REFERENCING_COL_NAME");
rs.addColumn ("REF_GENERATION");
return rs;
}
}