forked from xgp/sql2java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.java
More file actions
executable file
·376 lines (329 loc) · 9.86 KB
/
Table.java
File metadata and controls
executable file
·376 lines (329 loc) · 9.86 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//$Id: Table.java,v 1.1 2005/10/12 18:44:24 framiere Exp $
package net.sourceforge.sql2java;
import java.util.*;
public class Table
{
private Hashtable colHash = new Hashtable();
private Vector cols = new Vector();
private Vector priKey = new Vector();
private Hashtable manyToManyHash = new Hashtable();
private String catalog, schema, name, type, remarks;
private Database db;
private Vector foreignKeys = new Vector();
private Vector importedKeys = new Vector();
public boolean isRelationTable()
{
return importedKeys.size() > 1;
}
/**
* Tells whether if one of this table's columns (imported key)
* points to one of the otherTable's pk.
*/
public boolean relationConnectsTo(Table otherTable)
{
if (this.equals(otherTable))
{
return false;
}
for (int i = 0; i < foreignKeys.size(); i++)
{
Column c = (Column) importedKeys.get(i);
if (c.getTableName().equals(otherTable.getName()))
{
return true;
}
}
return false;
}
/**
* Return, beside the passed table, the tables this table points to.
*/
public Table[] linkedTables(Database pDatabase, Table pTable)
{
Vector vector = new Vector();
for (int iIndex = 0; iIndex < importedKeys.size(); iIndex++)
{
Column pColumn = (Column) importedKeys.get(iIndex);
if (pColumn.getTableName().equals(pTable.getName()) == false)
{
Table pTableToAdd = pDatabase.getTable(pColumn.getTableName());
if (vector.contains(pTableToAdd) == false)
vector.add(pTableToAdd);
}
}
return (Table[])vector.toArray(new Table[0]);
}
/**
* Return the imported key pointing to the passed table.
*/
public Column getForeignKeyFor(Table pTable)
{
Vector vector = new Vector();
for (int iIndex = 0; iIndex < importedKeys.size(); iIndex++)
{
Column pColumn = (Column) importedKeys.get(iIndex);
if (pColumn.getTableName().equals(pTable.getName()))
return pColumn;
}
return null;
}
public void setDatabase(Database db)
{
this.db = db;
}
public void setCatalog(String catalog)
{
this.catalog = catalog;
}
public void setSchema(String schema)
{
this.schema = schema;
}
public void setName(String name)
{
this.name = name;
}
public void setType(String type)
{
this.type = type;
}
public void setRemarks(String remarks)
{
if (remarks!=null) {
this.remarks = remarks.replaceAll("/\\*", "SLASH*").replaceAll("\\*/", "*SLASH");
}
// else setRemarks("No remarks in this table /* escape me */ /** me too ****/ / * not me * /");
}
public String getCatalog()
{
return catalog;
}
public String getSchema()
{
return schema;
}
public String getName()
{
return name;
}
public String getType()
{
return type;
}
public String getRemarks()
{
return remarks==null?"":remarks;
}
public Column[] getColumns()
{
return (Column[])cols.toArray(new Column[0]);
}
public Column getColumn(String name)
{
return (Column) colHash.get(name.toLowerCase());
}
public void addColumn(Column column)
{
colHash.put(column.getName().toLowerCase(), column);
cols.addElement(column);
}
public void removeColumn(Column column)
{
cols.removeElement(column);
colHash.remove(column.getName().toLowerCase());
}
public Column[] getPrimaryKeys()
{
return (Column[])priKey.toArray(new Column[0]);
}
public void addPrimaryKey(Column column)
{
priKey.addElement(column);
column.isPrimaryKey(true);
}
public Column[] getImportedKeys()
{
return (Column[])importedKeys.toArray(new Column[0]);
}
public void addImportedKey(Column column)
{
if (importedKeys.contains(column) == false)
importedKeys.addElement(column);
}
/**
* Returns a 2-D array of the keys in this table that form a many
* to many relationship.
* <br>
* The size of the first dimension is based on the number of
* unique tables that are being managed. The second dimension
* is always 2 elements. The first element is the column in the
* relationship table itself. The second column is the primary
* key column in the target table.
*/
public Column[][] getManyToManyKeys()
{
// // it matters only if we have 2 entries.
// if (manyToManyHash.size()<=1) {
// return new Column[0][0];
// }
Column list[][] = new Column[manyToManyHash.size()][2];
int i = 0;
for (Enumeration e = manyToManyHash.keys(); e.hasMoreElements(); i++)
{
Column fk = (Column) e.nextElement();
Column pk = (Column) manyToManyHash.get(fk);
list[i][0] = fk;
list[i][1] = pk;
}
return list;
}
public void addManyToManyKey(Column fk, Column pk)
{
manyToManyHash.put(fk, pk);
}
public int countColumns()
{
return cols.size();
}
public int countPrimaryKeys()
{
return priKey.size();
}
public boolean hasPrimaryKey()
{
return countPrimaryKeys() > 0;
}
public int countImportedKeys()
{
return importedKeys.size();
}
public boolean hasImportedKeys()
{
return countImportedKeys() > 0;
}
public int countForeignKeys()
{
return foreignKeys.size();
}
public boolean hasForeignKeys()
{
return countForeignKeys() > 0;
}
public void addForeignKey(Column col)
{
if (foreignKeys.contains(col) == false)
foreignKeys.add(col);
}
public Column[] getForeignKeys()
{
return (Column[])foreignKeys.toArray(new Column[0]);
}
public boolean isForeignKey(Column col)
{
return foreignKeys.contains(col);
}
public Table[] getLinkedTables()
{
Vector vector = new Vector();
for (int iIndex = 0; iIndex < importedKeys.size(); iIndex++)
{
Column column = (Column) importedKeys.get(iIndex);
if (column.getTableName().equals(getName()) == false)
{
Table pTableToAdd = column.getTable();
if (vector.contains(pTableToAdd) == false)
vector.add(pTableToAdd);
}
}
for (int iIndex = 0; iIndex < foreignKeys.size(); iIndex++)
{
Column column = (Column) foreignKeys.get(iIndex);
column = column.getForeignColumn();
if (column.getTableName().equals(getName()) == false)
{
Table pTableToAdd = column.getTable();
if (vector.contains(pTableToAdd) == false)
vector.add(pTableToAdd);
}
}
return (Table[])vector.toArray(new Table[0]);
}
private Table[] getImportedTablesFromVector(Vector keys)
{
Vector vector = new Vector();
for (int iIndex = 0; iIndex < keys.size(); iIndex++)
{
Column column = (Column) keys.get(iIndex);
if (column.getTableName().equals(getName()) == false)
{
Table pTableToAdd = column.getTable();
if (vector.contains(pTableToAdd) == false)
vector.add(pTableToAdd);
}
}
return (Table[])vector.toArray(new Table[0]);
}
public Table[] getImportedTables()
{
return getImportedTablesFromVector(importedKeys);
}
public Table[] getForeignTables()
{
return getImportedTablesFromVector(foreignKeys);
}
public String[] getLinkedPackages()
{
Vector vector = new Vector();
Table[] linkedTables = getLinkedTables();
for (int iIndex = 0; iIndex < linkedTables.length; iIndex++)
{
if (vector.contains(linkedTables[iIndex].getPackage()) == false)
vector.add(linkedTables[iIndex].getPackage());
}
return (String[])vector.toArray(new String[0]);
}
public String getPackage()
{
String basePackage = CodeWriter.getProperty("mgrwriter.package");
// iterate in the subpackage.X.names
// starting at 1
int iterating = 1;
while(true)
{
String tablesProperty = "subpackage." + iterating + ".tables";
String packageNameProperty = "subpackage." + iterating + ".name";
String tables[] = CodeWriter.getPropertyExploded(tablesProperty);
for (int i = 0; i < tables.length; i ++)
{
if (getName().equalsIgnoreCase(tables[i]))
{
String packageName = CodeWriter.getProperty(packageNameProperty);
if (packageName == null)
return basePackage;
return basePackage + "." + packageName;
}
}
iterating ++;
// no tables found ?
// ok stop iterating
if (tables.length == 0)
break;
}
return basePackage;
}
public String getPackagePath()
{
return getPackage().replace('.', '/');
}
public Column[] getColumnsFor(String type)
{
Vector vector = new Vector();
for (int i = 0; i < cols.size(); i++)
{
Column c = (Column)cols.get(i);
if (c.columnFor(type))
vector.add(c);
}
return (Column[])vector.toArray(new Column[0]);
}
}