File: ReverseEngineeringSybase.java

package info (click to toggle)
mysql-admin 1.2.5rc-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 80,944 kB
  • ctags: 43,103
  • sloc: sql: 295,916; pascal: 256,535; cpp: 74,487; ansic: 68,881; objc: 26,417; sh: 16,867; yacc: 10,755; java: 9,917; xml: 8,453; php: 2,806; python: 2,068; makefile: 1,252; perl: 3
file content (716 lines) | stat: -rw-r--r-- 23,623 bytes parent folder | download | duplicates (4)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
package com.mysql.grt.modules;

import java.sql.*;

import com.mysql.grt.*;
import com.mysql.grt.db.sybase.*;

/**
 * GRT Reverse Engineering Class for Sybase
 * 
 * @author MikeZ
 * @version 1.0, 06/23/06
 * 
 */
public class ReverseEngineeringSybase extends ReverseEngineeringGeneric {

	/**
	 * Static function to return information about this class to the GRT
	 * environment
	 * 
	 * @return returns a GRT XML string containing the infos about this class
	 */
	public static String getModuleInfo() {
		return Grt.getModuleInfoXml(ReverseEngineeringSybase.class,
				"ReverseEngineering");
	}

	private static String catalogsSelect = "SELECT name FROM master.dbo.sysdatabases "
			+ "ORDER BY 1";

	/**
	 * Returns a list of all catalogs from the given JDBC connection
	 * 
	 * @param dbConn
	 *            the connection to use
	 * 
	 * @return returns a GRT XML string containing a list of schemata names
	 */
	public static GrtStringList getCatalogs(
			com.mysql.grt.db.mgmt.Connection dbConn) throws Exception {
		GrtStringList catalogList = new GrtStringList();

		GrtStringHashMap paramValues = dbConn.getParameterValues();
		paramValues.add("database", "");

		// connect to the database
		Connection conn = establishConnection(dbConn);
		try {

			Grt.getInstance().addMsg("Fetching catalog list.");
			Grt.getInstance().addMsgDetail(catalogsSelect);
			Grt.getInstance().flushMessages();

			PreparedStatement stmt = conn.prepareStatement(catalogsSelect);
			try {
				ResultSet rset = stmt.executeQuery();
				while (rset.next()) {
					catalogList.add(rset.getString(1));
				}
			} finally {
				stmt.close();
			}
		} finally {
			conn.close();
		}

		return catalogList;
	}

	private static String schemataSelect = "SELECT USERNAME = name "
			+ "FROM sysusers WHERE gid != uid ORDER BY 1";

	/**
	 * Returns a list of all schemata from the given JDBC connection
	 * 
	 * @param dbConn
	 *            the connection to use
	 * 
	 * @return returns a GRT XML string containing a list of schemata names
	 */
	public static GrtStringList getSchemata(
			com.mysql.grt.db.mgmt.Connection dbConn) throws Exception {

		GrtStringList schemataList = new GrtStringList();

		// connect to the database
		Connection conn = establishConnection(dbConn);
		try {
			String catalog = dbConn.getParameterValues().get("database");

			Grt.getInstance().addMsg("Fetching schemata list.");
			Grt.getInstance().addMsgDetail(schemataSelect);
			Grt.getInstance().flushMessages();

			Statement stmt = conn.createStatement();
			try {
				ResultSet rset = stmt.executeQuery(schemataSelect);
				while (rset.next()) {
					schemataList.add(catalog + "." + rset.getString(1));
				}
			} finally {
				stmt.close();
			}
		} finally {
			conn.close();
		}

		Grt.getInstance().addMsg("Return schemata list.");
		Grt.getInstance().flushMessages();

		return schemataList;
	}

	/**
	 * Does the reverse engineering of the given schematas over the JDBC
	 * connection and returns the GRT objects
	 * 
	 * @param jdbcDriver
	 *            the class name of the JDBC driver
	 * @param jdbcConnectionString
	 *            a JDBC connection string
	 * @param schemataList
	 *            list of schematas to be reverse engineered
	 * @return returns a GRT XML string containing a the reverse engineered
	 *         objects
	 */
	public static com.mysql.grt.db.Catalog reverseEngineer(
			com.mysql.grt.db.mgmt.Connection dbConn, GrtStringList schemataList)
			throws Exception {

		boolean reverseEngineerOnlyTableObjects = (Grt.getInstance()
				.getGrtGlobalAsInt(
						"/migration/applicationData/"
								+ "reverseEngineerOnlyTableObjects") == 1);

		// connect to the database
		Connection conn = establishConnection(dbConn);

		// create reveng instance
		ReverseEngineeringSybase revEng = new ReverseEngineeringSybase();

		Catalog catalog = new Catalog(null);
		catalog.setName(dbConn.getParameterValues().get("database"));

		catalog.setVersion(getVersion(dbConn));

		Grt.getInstance().addMsg("Build simple Sybase datatypes.");
		Grt.getInstance().flushMessages();
		revEng.buildSimpleDatatypes(dbConn, catalog);

		for (int i = 0; i < schemataList.size(); i++) {
			String schemaName = schemataList.get(i);
			schemaName = schemaName.substring(catalog.getName().length() + 1);

			Schema schema = new Schema(catalog);
			schema.setName(schemaName);
			catalog.getSchemata().add(schema);

			// Get Tables
			if (revEng.reverseEngineerTables(conn, catalog, schema) == 0
					&& !reverseEngineerOnlyTableObjects) {

				// Get Views
				revEng.reverseEngineerViews(conn, catalog, schema);

				// Get SPs
				revEng.reverseEngineerProcedures(conn, catalog, schema);
			}
		}

		// make sure the Fks use real references instead of
		// text names where possible
		revEng.reverseEngineerUpdateFkReferences(catalog);

		return catalog;
	}

	protected void buildSimpleDatatypes(
			com.mysql.grt.db.mgmt.Connection dbConn, Catalog catalog) {
		com.mysql.grt.db.mgmt.Driver driver = dbConn.getDriver();
		com.mysql.grt.db.mgmt.Rdbms rdbms = (com.mysql.grt.db.mgmt.Rdbms) driver
				.getOwner();
		com.mysql.grt.db.SimpleDatatypeList rdbmsDatatypeList = rdbms
				.getSimpleDatatypes();
		com.mysql.grt.db.SimpleDatatypeList schemaDatatypeList = new com.mysql.grt.db.SimpleDatatypeList();

		for (int i = 0; i < rdbmsDatatypeList.size(); i++) {
			schemaDatatypeList.add(rdbmsDatatypeList.get(i));
		}

		catalog.setSimpleDatatypes(schemaDatatypeList);
	}

	private static String tableCountSelect = "SELECT TABLE_COUNT = COUNT(*) "
			+ "FROM sysobjects O, sysindexes I "
			+ "WHERE USER_NAME(uid) = ? AND O.type = 'U' AND O.id = I.id AND I.indid IN (0, 1)";

	private static String tableSelect = "SELECT TABLE_NAME = O.name "
			+ "FROM sysobjects O, sysindexes I "
			+ "WHERE USER_NAME(uid) = ? AND O.type = 'U' AND O.id = I.id AND I.indid IN (0, 1) "
			+ "ORDER BY 1";

	protected int reverseEngineerTables(Connection conn, Catalog catalog,
			Schema schema) throws Exception {

		int tableCount = 0;
		int currentTableNumber = 0;

		Grt.getInstance().addMsg(
				"Fetch the number of tables in the schema " + schema.getName()
						+ ".");
		Grt.getInstance().addMsgDetail(tableCountSelect);

		PreparedStatement stmt = conn.prepareStatement(tableCountSelect);
		stmt.setString(1, schema.getName());

		ResultSet tblRset = stmt.executeQuery();
		if (tblRset.next()) {
			tableCount = tblRset.getInt(1);
		}
		stmt.close();

		Grt.getInstance().addMsg(
				"Fetching " + tableCount + " table(s) of the schema "
						+ schema.getName() + ".");
		Grt.getInstance().addMsgDetail(tableSelect);
		Grt.getInstance().flushMessages();

		stmt = conn.prepareStatement(tableSelect);
		stmt.setString(1, schema.getName());

		tblRset = stmt.executeQuery();

		while (tblRset.next()) {
			// Create new table
			Table table = new Table(schema);
			schema.getTables().add(table);

			currentTableNumber++;

			table.setName(tblRset.getString("TABLE_NAME"));

			Grt.getInstance().addProgress(
					"Processing table " + table.getName() + ".",
					(currentTableNumber * 100) / tableCount);
			if (Grt.getInstance().flushMessages() != 0) {
				Grt.getInstance().addMsg("Migration canceled by user.");
				return 1;
			}

			reverseEngineerTableColumns(conn, catalog, schema, table);

			reverseEngineerTablePK(conn, catalog, schema, table);

			reverseEngineerTableIndices(conn, catalog, schema, table);

			reverseEngineerTableFKs(conn, catalog, schema, table);

		}

		Grt.getInstance().addProgress("", -1);
		Grt.getInstance().flushMessages();

		stmt.close();

		return 0;
	}

	private static String tableColumnSelect = "SELECT COLUMN_NAME = ISNULL(C.name, ''), "
			+ " DATA_TYPE = T.name, C.length, NUMERIC_PRECISION = C.prec, "
			+ " NUMERIC_SCALE = C.scale, "
			+ " IS_NULLABLE = CONVERT(BIT, (C.status & 0x08)) "
			+ "FROM syscolumns C, systypes T, sysobjects A "
			+ "WHERE USER_NAME(A.uid) = ? AND "
			+ " A.id = C.id AND C.id = OBJECT_ID(?) AND "
			+ "C.usertype*=T.usertype " + "ORDER BY C.colid";

	protected void reverseEngineerTableColumns(Connection conn,
			Catalog catalog, Schema schema, Table table) {

		try {
			Grt.getInstance().addMsg("Fetching column information.");
			Grt.getInstance().addMsgDetail(tableColumnSelect);

			PreparedStatement stmt = conn.prepareStatement(tableColumnSelect);
			stmt.setString(1, schema.getName());
			stmt.setString(2, table.getName());

			ResultSet colRset = stmt.executeQuery();
			while (colRset.next()) {
				// create new column
				Column column = new Column(table);
				table.getColumns().add(column);

				column.setName(colRset.getString("COLUMN_NAME"));
				column.setDatatypeName(colRset.getString("DATA_TYPE"));

				// Get Simple Type
				int datatypeIndex = catalog.getSimpleDatatypes()
						.getIndexOfName(column.getDatatypeName().toUpperCase());

				if (datatypeIndex > -1) {
					column.setSimpleType(catalog.getSimpleDatatypes().get(
							datatypeIndex));
				} else {
					column.setSimpleType(catalog.getSimpleDatatypes().get(
							catalog.getSimpleDatatypes().getIndexOfName(
									"VARCHAR")));
					column.setLength(255);

					Grt.getInstance().addMsg(
							"WARNING: The datatype " + column.getDatatypeName()
									+ " was not been defined yet.");
				}

				column.setLength(colRset.getInt("LENGTH"));
				column.setPrecision(colRset.getInt("NUMERIC_PRECISION"));
				column.setScale(colRset.getInt("NUMERIC_SCALE"));

				// Nullable
				if (colRset.getString("IS_NULLABLE").compareToIgnoreCase("1") == 0) {
					column.setIsNullable(1);
				} else {
					column.setIsNullable(0);
				}
			}

			stmt.close();
		} catch (Exception e) {
			Grt.getInstance().addErr(e.getMessage());
		}
	}

	private static String tablePKSP = "sp_pkeys @table_owner=?, @table_name=?";

	protected void reverseEngineerTablePK(Connection conn, Catalog catalog,
			Schema schema, Table table) {

		// String sql;

		try {
			Grt.getInstance().addMsg("Fetching primary key information.");
			Grt.getInstance().addMsgDetail(tablePKSP);

			PreparedStatement stmt = conn.prepareCall(tablePKSP);
			stmt.setString(1, schema.getName());
			stmt.setString(2, table.getName());

			ResultSet colRset = stmt.executeQuery();

			Index primaryKey = null;

			while (colRset.next()) {
				if (primaryKey == null) {
					primaryKey = new Index(table);
					primaryKey.setName("PRIMARY");

					primaryKey.setIsPrimary(1);

					table.getIndices().add(primaryKey);

					table.setPrimaryKey(primaryKey);
				}

				IndexColumn indexColumn = new IndexColumn(primaryKey);
				indexColumn.setName(colRset.getString("COLUMN_NAME"));
				indexColumn.setColumnLength(0);
				indexColumn.setDescend(0);

				// find reference table column
				for (int j = 0; j < table.getColumns().size(); j++) {
					Column column = (Column) (table.getColumns().get(j));

					if (column.getName().compareToIgnoreCase(
							indexColumn.getName()) == 0) {
						indexColumn.setReferedColumn(column);
						break;
					}
				}

				primaryKey.getColumns().add(indexColumn);
			}

			stmt.close();
		} catch (Exception e) {
			Grt.getInstance().addErr(e.getMessage());
		}
	}

	private static String tableIndexSelect = "SELECT INDEX_NAME = A.name, "
			+ " INDEX_TYPE = CASE WHEN ((A.status&16) = 16 OR (A.status2&512) = 512) THEN 'Clustered' "
			+ "  WHEN (A.indid = 255) THEN 'Text/Image' "
			+ "  ELSE 'NonClustered' END, "
			+ " IS_UNIQUE = CASE WHEN ((A.status&2) = 2) THEN 1 ELSE 0 END, "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 1), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 2), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 3), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 4), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 5), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 6), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 7), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 8), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 9), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 10), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 11), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 12), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 13), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 14), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 15), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 16), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 17), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 18), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 19), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 20), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 21), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 22), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 23), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 24), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 25), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 26), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 27), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 28), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 29), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 30), "
			+ " INDEX_COL(USER_NAME(B.uid) + '.' + B.name, indid, 31) "
			+ "FROM sysindexes A,  sysobjects B "
			+ "WHERE A.indid > 0 AND A.indid < 255 AND A.status2 & 2 != 2 AND "
			+ " B.id = A.id AND B.type = 'U' AND "
			+ " USER_NAME(B.uid) = ? AND B.name=? " + "ORDER BY 1, 2, 3";

	protected void reverseEngineerTableIndices(Connection conn,
			Catalog catalog, Schema schema, Table table) {

		try {
			Grt.getInstance().addMsg("Fetching indices information.");
			Grt.getInstance().addMsgDetail(tableIndexSelect);

			PreparedStatement stmt = conn.prepareStatement(tableIndexSelect);
			stmt.setString(1, schema.getName());
			stmt.setString(2, table.getName());

			ResultSet rset = stmt.executeQuery();
			Index index = null;

			while (rset.next()) {
				String newIndexName = rset.getString("INDEX_NAME");

				index = new Index(table);
				index.setName(newIndexName);

				if (rset.getInt("IS_UNIQUE") != 0)
					index.setUnique(1);
				else
					index.setUnique(0);

				index.setDeferability(0);

				String indexType = rset.getString("INDEX_TYPE");
				if (indexType.equalsIgnoreCase("Clustered"))
					index.setClustered(1);
				else
					index.setClustered(0);

				int indexColumnPos = 4;
				String indexColumnName = rset.getString(indexColumnPos++);

				while (indexColumnPos < 31 + 4 && indexColumnName != null) {
					IndexColumn indexColumn = new IndexColumn(index);
					indexColumn.setName(indexColumnName);
					indexColumn.setColumnLength(0);
					indexColumn.setDescend(0);

					// find reference table column
					for (int j = 0; j < table.getColumns().size(); j++) {
						Column column = (Column) (table.getColumns().get(j));

						if (column.getName().compareToIgnoreCase(
								indexColumn.getName()) == 0) {
							indexColumn.setReferedColumn(column);
							break;
						}
					}

					index.getColumns().add(indexColumn);

					indexColumnName = rset.getString(indexColumnPos++);
				}

				table.getIndices().add(index);
			}

			stmt.close();
		} catch (Exception e) {
			Grt.getInstance().addErr(e.getMessage());
		}
	}

	private static String tableFKSelect = "SELECT CONSTRAINT_NAME = OBJECT_NAME(A.constrid), "
			+ " REF_CATALOG_NAME = ISNULL(A.pmrydbname,DB_NAME()), "
			+ " REF_SCHEMA_NAME = USER_NAME(B.uid), "
			+ " REF_TABLE_NAME = OBJECT_NAME(A.reftabid), "
			+ " COLUMN_COUNT = A.keycnt, "
			+ " FK_COLUMN1 = COL_NAME(A.tableid, A.fokey1, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey2, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey3, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey4, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey5, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey6, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey7, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey8, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey9, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey10, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey11, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey12, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey13, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey14, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey15, DB_ID(A.frgndbname)), "
			+ " COL_NAME(A.tableid, A.fokey16, DB_ID(A.frgndbname)), "
			+ " REFERENCED_COLUMN1 = COL_NAME(A.reftabid, A.refkey1, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey2, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey3, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey4, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey5, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey6, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey7, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey8, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey9, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey10, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey11, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey12, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey13, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey14, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey15, DB_ID(A.pmrydbname)), "
			+ " COL_NAME(A.reftabid, A.refkey16, DB_ID(A.pmrydbname)) "
			+ "FROM sysreferences A, sysobjects B "
			+ "WHERE USER_NAME(B.uid) = ? AND "
			+ " A.tableid = OBJECT_ID(?) AND "
			+ " A.reftabid = B.id AND (A.pmrydbname IS NULL) ";

	protected void reverseEngineerTableFKs(Connection conn, Catalog catalog,
			Schema schema, Table table) {

		try {
			Grt.getInstance().addMsg("Fetching FK information.");
			Grt.getInstance().addMsgDetail(tableFKSelect);

			PreparedStatement stmt = conn.prepareStatement(tableFKSelect);
			stmt.setString(1, schema.getName());
			stmt.setString(2, table.getName());

			ResultSet rset = stmt.executeQuery();

			ForeignKey foreignKey = null;

			while (rset.next()) {
				String newFkName = rset.getString("CONSTRAINT_NAME");

				foreignKey = new ForeignKey(table);
				foreignKey.setName(newFkName);

				foreignKey.setDeferability(0);

				foreignKey.setDeleteRule("NO ACTION");
				foreignKey.setUpdateRule("NO ACTION");

				foreignKey.setReferedTableSchemaName(rset
						.getString("REF_SCHEMA_NAME"));
				foreignKey
						.setReferedTableName(rset.getString("REF_TABLE_NAME"));

				int fkColumnPos = 6;
				int fkColumnCount = rset.getInt("COLUMN_COUNT");
				String refColumnName = rset.getString(fkColumnPos + 16);
				String fkColumnName = rset.getString(fkColumnPos);

				while (fkColumnPos < fkColumnCount + 6 && fkColumnName != null) {
					foreignKey.getReferedColumnNames().add(refColumnName);

					// find reference table column
					for (int j = 0; j < table.getColumns().size(); j++) {
						Column column = (Column) (table.getColumns().get(j));

						if (column.getName().compareToIgnoreCase(fkColumnName) == 0)
							foreignKey.getColumns().add(column);
					}

					refColumnName = rset.getString(fkColumnPos + 16);
					fkColumnName = rset.getString(fkColumnPos++);
				}

				table.getForeignKeys().add(foreignKey);
			}

			stmt.close();
		} catch (Exception e) {
			Grt.getInstance().addErr(e.getMessage());
		}
	}

	private static String viewSelect = "SELECT VIEW_NAME = B.name, VIEW_DEFINITION = A.text "
			+ "FROM syscomments A,sysobjects B "
			+ "WHERE USER_NAME(B.uid) = ? AND " + "B.type='V' AND A.id=B.id";

	protected void reverseEngineerViews(Connection conn, Catalog catalog,
			Schema schema) throws Exception {

		Grt.getInstance().addMsg(
				"Fetch all views of the schema " + schema.getName() + ".");
		Grt.getInstance().addMsgDetail(viewSelect);
		Grt.getInstance().flushMessages();

		PreparedStatement stmt = conn.prepareStatement(viewSelect);
		stmt.setString(1, schema.getName());

		ResultSet rset = stmt.executeQuery();

		while (rset.next()) {
			// Create new view
			View view = new View(schema);
			schema.getViews().add(view);

			view.setName(rset.getString("VIEW_NAME"));

			Grt.getInstance().addMsg("Processing view " + view.getName() + ".");

			view.setQueryExpression(rset.getString("VIEW_DEFINITION"));

			view.setWithCheckCondition(0);
		}

		stmt.close();

		Grt.getInstance().addMsg("Views fetched.");
	}

	private static String procedureCountSelect = "SELECT NUM = COUNT(*) "
			+ "FROM sysobjects O "
			+ "WHERE USER_NAME(O.uid) = ? AND O.type = 'P'";

	private static String procedureSelect = "SELECT ROUTINE_NAME = O.name, "
			+ " ROUTINE_DEFINITION = A.text "
			+ "FROM syscomments A, sysobjects O "
			+ "WHERE USER_NAME(O.uid) = ? AND A.id=O.id AND O.type = 'P'";

	protected int reverseEngineerProcedures(Connection conn, Catalog catalog,
			Schema schema) throws Exception {
		int spCount = 0;
		int currentSpNumber = 0;

		Grt.getInstance().addMsg(
				"Fetch count of stored procedures of the schema "
						+ schema.getName() + ".");
		Grt.getInstance().addMsgDetail(procedureCountSelect);

		try {
			PreparedStatement stmt = conn
					.prepareStatement(procedureCountSelect);
			stmt.setString(1, schema.getName());

			ResultSet rset = stmt.executeQuery();

			if (rset.next()) {
				spCount = rset.getInt("NUM");
			}

			Grt.getInstance().addMsg(
					"Fetching " + spCount
							+ " stored procedure(s) of the schema "
							+ schema.getName() + ".");
			Grt.getInstance().addMsgDetail(procedureSelect);
			Grt.getInstance().flushMessages();

			stmt = conn.prepareStatement(procedureSelect);
			stmt.setString(1, schema.getName());

			rset = stmt.executeQuery();

			while (rset.next()) {
				// Create new view
				Routine proc = new Routine(schema);
				schema.getRoutines().add(proc);

				proc.setName(rset.getString("ROUTINE_NAME"));

				currentSpNumber++;

				Grt.getInstance().addProgress(
						"Processing procedure " + proc.getName() + ".",
						(currentSpNumber * 100) / spCount);
				if (Grt.getInstance().flushMessages() != 0) {
					Grt.getInstance().addMsg("Migration canceled by user.");
					return 1;
				}

				Grt.getInstance().addMsg(
						"Processing procedure " + proc.getName() + ".");

				proc.setRoutineType("PROCEDURE");

				proc.setRoutineCode(rset.getString("ROUTINE_DEFINITION"));
			}

			stmt.close();

			Grt.getInstance().addMsg("Routines fetched.");

			Grt.getInstance().addProgress("", -1);

		} catch (Exception e) {
			Grt.getInstance().addMsg("Stored procedures cannot be fetched.");
			Grt.getInstance().addMsgDetail(e.getMessage());
		}

		return 0;
	}
}