forked from xgp/sql2java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
executable file
·342 lines (290 loc) · 11.5 KB
/
Database.java
File metadata and controls
executable file
·342 lines (290 loc) · 11.5 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//$Id: Database.java,v 1.1 2005/10/12 18:44:24 framiere Exp $
package net.sourceforge.sql2java;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.util.Vector;
import java.util.Hashtable;
public class Database
{
private String tableTypes[];
private Connection pConnection;
private DatabaseMetaData meta;
private Vector tables;
private Hashtable tableHash;
private String driver, url, username, password, catalog, schema, tablenamepattern;
private boolean retrieveRemarks = true;
public void setOracleRetrieveRemarks(boolean retrieveRemarks) { this.retrieveRemarks = retrieveRemarks;}
public void setDriver(String driver) { this.driver = driver; }
public void setUrl(String url) { this.url = url; }
public void setUsername(String username) { this.username = username; }
public void setPassword(String password) { this.password = password; }
public void setCatalog(String catalog) { this.catalog = catalog; }
public void setTableNamePattern(String tablenamepattern) { this.tablenamepattern = tablenamepattern; }
public void setTableTypes(String[] tt) { this.tableTypes = tt; }
public boolean getOracleRetrieveRemarks() { return this.retrieveRemarks; }
public String getDriver() { return driver; }
public String getUrl() { return url; }
public String getUsername() { return username; }
public String getPassword() { return password; }
public String getCatalog() { return catalog; }
public String getSchema() { return schema; }
public String getTableNamePattern() { return tablenamepattern; }
public String[] getTableTypes() { return tableTypes; }
public void setSchema(String schema)
{
if ("null".equalsIgnoreCase(schema))
this.schema = null;
else
this.schema = schema;
}
/**
* Return an array of tables having foreign key pointing to the
* passed table.
*/
public Table[] getRelationTable(Table table)
{
Vector vector = new Vector();
for (int iIndex = 0; iIndex < tables.size(); iIndex ++)
{
Table tempTable = (Table)tables.get(iIndex);
// skip itself
if (table.equals(tempTable))
continue;
// check only for relation table
if (tempTable.isRelationTable())
{
if (tempTable.relationConnectsTo(table))
{
if (!vector.contains(tempTable))
vector.add(tempTable);
}
}
}
return (Table[])vector.toArray(new Table[0]);
}
public void load() throws SQLException, ClassNotFoundException
{
// Connect to the database
Class.forName(driver);
System.out.println("Connecting to " + username + " on " + url + " ...");
pConnection = DriverManager.getConnection(url, username, password);
System.out.println(" Connected.");
// if (pConnection instanceof oracle.jdbc.driver.OracleConnection)
// ((oracle.jdbc.driver.OracleConnection)pConnection).setRemarksReporting(getOracleRetrieveRemarks());
meta = pConnection.getMetaData();
System.out.println(" Database server :" + meta.getDatabaseProductName() + ".");
tables = new Vector();
tableHash = new Hashtable();
loadTables();
loadColumns();
loadPrimaryKeys();
// loadImportedKeys();
// loadManyToMany();
// loadIndexes(); // experimental
}
public Table[] getTables()
{
return (Table[])tables.toArray(new Table[0]);
}
private void addTable(Table t) {
tables.addElement(t);
tableHash.put(t.getName(), t);
}
public Table getTable(String name) {
return (Table)tableHash.get(name);
}
/**
* Load all the tables for this schema.
*/
private void loadTables() throws SQLException
{
System.out.println("Loading table list according to pattern " + tablenamepattern + " ...");
// tablenamepattern is now a comma-separated list of patterns
java.util.StringTokenizer st = new java.util.StringTokenizer(tablenamepattern, ",; \t");
while(st.hasMoreTokens()) {
String pattern = ((String)st.nextToken()).trim();
ResultSet resultSet = meta.getTables(catalog, schema, pattern, tableTypes);
while(resultSet.next())
{
Table table = new Table();
table.setDatabase(this);
table.setCatalog(resultSet.getString("TABLE_CAT"));
table.setSchema(resultSet.getString("TABLE_SCHEM"));
table.setName(resultSet.getString("TABLE_NAME"));
table.setType(resultSet.getString("TABLE_TYPE"));
table.setRemarks(resultSet.getString("REMARKS"));
addTable(table);
System.out.println(" table " + table.getName() + " found");
}
resultSet.close();
}
}
/**
* For each table, load all the columns.
*/
private void loadColumns() throws SQLException
{
Table tables[] = getTables();
System.out.println("Loading columns ...");
boolean b = false;
for(int i = 0; i < tables.length; i++)
{
Table table = tables[i];
ResultSet resultSet = meta.getColumns(catalog, schema, table.getName(), "%");
Column c = null;
while(resultSet.next())
{
c = new Column();
c.setDatabase(this);
c.setCatalog(resultSet.getString("TABLE_CAT"));
c.setSchema(resultSet.getString("TABLE_SCHEM"));
c.setTableName(resultSet.getString("TABLE_NAME"));
c.setName(resultSet.getString("COLUMN_NAME"));
c.setType(resultSet.getShort("DATA_TYPE"));
c.setSize(resultSet.getInt("COLUMN_SIZE"));
c.setDecimalDigits(resultSet.getInt("DECIMAL_DIGITS"));
c.setRadix(resultSet.getInt("NUM_PREC_RADIX"));
c.setNullable(resultSet.getInt("NULLABLE"));
c.setRemarks(resultSet.getString("REMARKS"));
c.setDefaultValue(resultSet.getString("COLUMN_DEF"));
c.setOrdinalPosition(resultSet.getInt("ORDINAL_POSITION"));
table.addColumn(c);
}
System.out.println(" " + table.getName() + " found " + table.countColumns() + " columns");
resultSet.close();
}
}
/**
* For each table, load the primary keys.
*/
private void loadPrimaryKeys() throws SQLException
{
System.out.println("Loading primary keys ...");
Table tables[] = getTables();
for(int i = 0; i < tables.length; i++)
{
Table table = tables[i];
ResultSet resultSet = meta.getPrimaryKeys(catalog, schema, table.getName());
while(resultSet.next())
{
Column col = table.getColumn(resultSet.getString("COLUMN_NAME"));
table.addPrimaryKey(col);
System.out.println(" " + col.getFullName() + " found");
}
resultSet.close();
}
}
/**
* For each table, load the imported key.
* <br>
* An imported key is the other's table column clone. Its
* ForeignKeyColName corresponds to the table's column name that
* points to the other's table.
*/
private void loadImportedKeys() throws SQLException
{
System.out.println("Loading imported keys ...");
Table tables[] = getTables();
for(int i = 0; i < tables.length; i++)
{
Table table = tables[i];
ResultSet resultSet = meta.getImportedKeys(catalog, schema, table.getName());
while(resultSet.next())
{
String tabName = resultSet.getString("FKTABLE_NAME");
String colName = resultSet.getString("FKCOLUMN_NAME");
String foreignTabName= resultSet.getString("PKTABLE_NAME");
String foreignColName= resultSet.getString("PKCOLUMN_NAME");
Column col = getTable(tabName).getColumn(colName);
Column foreignCol = getTable(foreignTabName).getColumn(foreignColName);
col.addForeignKey(foreignCol);
foreignCol.addImportedKey(col);
//getTable(foreignTabName).addImportedKey(col);
System.out.println(" " + col.getFullName() + " -> " + foreignCol.getFullName() + " found ");
}
resultSet.close();
}
}
//
// could avoid db call.
// In each current table an entry is:
// other table column (that points to current table) | current pk column
// [other table has nb col. == pk length]
private void loadManyToMany() throws SQLException
{
System.out.println("Loading many to many relationships...");
Table tables[] = getTables();
for(int i = 0; i < tables.length; i++)
{
Table table = tables[i];
// if(table.getColumns().length == table.getPrimaryKeys().length)
{
ResultSet resultSet = meta.getImportedKeys(catalog, schema, table.getName());
while(resultSet.next())
{
String tabName = resultSet.getString("PKTABLE_NAME");
String colName = resultSet.getString("PKCOLUMN_NAME");
System.out.println(" many to many " + tabName + " " + colName);
Table pkTable = getTable(tabName);
Column fkCol = table.getColumn(resultSet.getString("FKCOLUMN_NAME"));
if(pkTable != null)
{
Column pkCol = pkTable.getColumn(colName);
if(pkCol != null && fkCol != null)
{
pkTable.addManyToManyKey(fkCol, pkCol);
}
}
}
resultSet.close();
}
}
}
/**
* For each table, load the indexes.
*/
private void loadIndexes() throws SQLException
{
System.out.println("Loading indexes ...");
Table tables[] = getTables();
for(int i = 0; i < tables.length; i++)
{
Table table = tables[i];
ResultSet resultSet = meta.getIndexInfo(catalog,
schema,
table.getName(),
true,
true);
while(resultSet.next())
{
String colName = resultSet.getString("COLUMN_NAME");
String indName = resultSet.getString("INDEX_NAME");
if (colName != null && indName != null) {
Column col = table.getColumn(colName);
if (!col.isPrimaryKey())
System.out.println(" Found interesting index " + indName + " on " +
colName + " for table " + table.getName());
}
}
resultSet.close();
}
}
public String[] getAllPackages()
{
Vector vector = new Vector();
for (int iIndex = 0; iIndex < tables.size(); iIndex ++)
{
Table table = (Table)tables.get(iIndex);
String packages[] = table.getLinkedPackages();
for (int i = 0; i < packages.length; i++)
{
if (vector.contains(packages[i]) == false)
{
vector.add(packages[i]);
}
}
}
return (String[])vector.toArray(new String[0]);
}
}