-
Notifications
You must be signed in to change notification settings - Fork 16
/
Oracle.java
executable file
·810 lines (674 loc) · 22.2 KB
/
Oracle.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
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
import java.sql.*;
import java.net.*;
import java.io.*;
public class Oracle
{
private static String myclass = "Oracle";
public static boolean debug = false;
private static String getSQLForColumns(String sourceSchema, String sourceTable) throws SQLException
{
String method = "getSQLForColumns";
int location = 1000;
try
{
location = 2000;
String strSQL = "SELECT '\"' || COLUMN_NAME || '\"' AS COLUMN_NAME \n" +
"FROM ALL_TAB_COLUMNS \n" +
"WHERE TABLE_NAME = '" + sourceTable + "' \n" +
" AND OWNER = '" + sourceSchema + "' \n" +
" AND DATA_TYPE NOT IN ('BLOB', 'BFILE', 'RAW', 'LONG RAW', 'MLSLABEL', 'BFILE', 'XMLTYPE') \n" +
"ORDER BY COLUMN_ID";
if (debug)
Logger.printMsg("Returning: " + strSQL);
location = 2100;
return strSQL;
}
catch (Exception ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getSQLForData(Connection conn, String sourceSchema, String sourceTable, String refreshType, String appendColumnName, String appendColumnMax) throws SQLException
{
String method = "getSQLForData";
int location = 1000;
try
{
location = 2000;
//Create SQL Statement for getting the column names
String strSQL = getSQLForColumns(sourceSchema, sourceTable);
location = 2100;
//Execute SQL Statement and format columns for next SELECT statement
String columnSQL = CommonDB.formatSQLForColumnName(conn, strSQL);
location = 2200;
//Create SQL Statement for retrieving data from table
strSQL = "SELECT " + columnSQL + " \n" +
"FROM \"" + sourceSchema + "\".\"" + sourceTable + "\" \n";
location = 2300;
//Add filter for append refreshType
if (debug)
Logger.printMsg("About to refreshType: " + refreshType);
if (refreshType.equals("append"))
{
location = 2400;
//check to see if appendColumnMax is numeric or not
try
{
double d = Double.parseDouble(appendColumnMax);
strSQL = strSQL + "WHERE \"" + appendColumnName + "\" > " + appendColumnMax; //greater than what is in GP currently
}
catch(NumberFormatException nfe)
{
strSQL = strSQL + "WHERE \"" + appendColumnName + "\" > to_date('" + appendColumnMax + "', 'yyyy-mm-dd hh24:mi:ss')"; //greater than what is in GP currently
}
}
if (debug)
Logger.printMsg("Returning: " + strSQL);
location = 2500;
return strSQL;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getSQLForCreateTable(Connection conn, String sourceSchema, String sourceTable) throws SQLException
{
String method = "getSQLForCreateTable";
int location = 1000;
try
{
location = 2000;
String strSQL = "SELECT REPLACE(REPLACE(LOWER(COLUMN_NAME), '\"', ''), '.', '_') AS COLUMN_NAME, \n" +
"CASE WHEN DATA_TYPE = 'BINARY_DOUBLE' THEN 'float8' \n" +
" WHEN DATA_TYPE = 'BINARY_FLOAT' THEN 'float8' \n" +
" WHEN DATA_TYPE = 'NUMBER' THEN 'numeric' \n" +
" WHEN DATA_TYPE = 'DATE' THEN 'timestamp' \n" +
" WHEN DATA_TYPE = 'CHAR' THEN 'character' \n" +
" WHEN DATA_TYPE = 'NCHAR' THEN 'character' \n" +
" WHEN DATA_TYPE = 'VARCHAR' THEN 'varchar' \n" +
" WHEN DATA_TYPE = 'VARCHAR2' THEN 'varchar' \n" +
" WHEN DATA_TYPE = 'NVARCHAR2' THEN 'varchar' \n" +
" WHEN DATA_TYPE = 'ROWID' THEN 'varchar(18)' \n" +
" WHEN DATA_TYPE = 'UROWID' THEN 'varchar' \n" +
" WHEN DATA_TYPE = 'LONG' THEN 'text' \n" +
" WHEN DATA_TYPE = 'CLOB' THEN 'text' \n" +
" WHEN DATA_TYPE = 'NCLOB' THEN 'text' \n" +
" WHEN DATA_TYPE LIKE 'TIMESTAMP%' AND DATA_TYPE LIKE '%TIME ZONE' THEN 'timestamptz' \n" +
" WHEN DATA_TYPE LIKE 'TIMESTAMP%' AND DATA_TYPE NOT LIKE '%TIME ZONE' THEN 'timestamp' \n" +
" WHEN DATA_TYPE LIKE 'INTERVAL%' THEN 'interval' \n" +
" ELSE DATA_TYPE end || \n" +
" CASE WHEN DATA_TYPE IN ('CHAR', 'NCHAR', 'VARCHAR', 'VARCHAR2', 'NVARCHAR2', 'UROWID') THEN '(' || TO_CHAR(DATA_LENGTH) || ') ' \n " +
" ELSE ' ' END AS DATATYPE \n" +
"FROM ALL_TAB_COLUMNS \n" +
"WHERE TABLE_NAME = '" + sourceTable + "' \n" +
" AND OWNER = '" + sourceSchema + "' \n" +
" AND DATA_TYPE NOT IN ('BLOB', 'BFILE', 'RAW', 'LONG RAW', 'MLSLABEL', 'BFILE', 'XMLTYPE') \n" +
"ORDER BY COLUMN_ID";
if (debug)
Logger.printMsg("Returning: " + strSQL);
location = 2100;
return strSQL;
}
catch (Exception ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getSQLForDistribution(Connection conn, String sourceSchema, String sourceTable) throws SQLException
{
String method = "getSQLForDistribution";
int location = 1000;
try
{
location = 2000;
String strSQL = "SELECT '\"' || LOWER(dcc.COLUMN_NAME) || '\"' as COLUMN_NAME \n" +
"FROM ALL_CONSTRAINTS dc \n" +
"JOIN ALL_CONS_COLUMNS dcc ON dc.CONSTRAINT_NAME = dcc.CONSTRAINT_NAME and dc.OWNER = dcc.OWNER \n" +
"WHERE dc.OWNER = '" + sourceSchema + "' \n" +
" AND dc.TABLE_NAME = '" + sourceTable + "' \n" +
" AND dc.CONSTRAINT_TYPE = 'P' \n" +
"ORDER BY dcc.POSITION";
if (debug)
Logger.printMsg("Returning: " + strSQL);
location = 2100;
return strSQL;
}
catch (Exception ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String validate(Connection conn) throws SQLException
{
String method = "validate";
int location = 1000;
try
{
location = 2000;
String msg = "";
String strSQL = "SELECT VERSION FROM V$INSTANCE";
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2300;
while (rs.next())
{
msg = "Success!";
}
location = 2400;
return msg;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getMaxId(Connection conn, String sourceSchema, String sourceTable, String columnName) throws SQLException
{
String method = "getMaxId";
int location = 1000;
try
{
location = 2000;
String maxId = "-1";
String strSQL = "SELECT MAX(\"" + columnName + "\") \n" +
"FROM \"" + sourceSchema + "\".\"" + sourceTable + "\"";
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2300;
while (rs.next())
{
maxId = rs.getString(1);
}
location = 2400;
if (maxId == null)
{
maxId = "-1";
}
return maxId;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static void configureReplication(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable, String appendColumnName) throws SQLException
{
String method = "configureReplication";
int location = 1000;
try
{
location = 2000;
String sourceType = "oracle";
String replTable = GP.getReplTableName(sourceType, sourceTable);
//1. drop triggers if exists
//2. drop replTable if exists
//3. drop sequence if exists
//4. create sequence
//5. create replTable
//6. create trigger to capture changes
Statement stmt = conn.createStatement();
String strSQL = "";
/////////////////////////////
//1. drop trigger if exists
/////////////////////////////
location = 2100;
strSQL = "BEGIN \n" +
"FOR x IN (SELECT * FROM DUAL WHERE EXISTS (SELECT NULL FROM ALL_TRIGGERS \n" +
" WHERE TABLE_OWNER = '" + sourceSchema + "' \n" +
" AND TRIGGER_NAME = 'T_" + replTable + "_AIUD') ) LOOP \n" +
" EXECUTE IMMEDIATE('DROP TRIGGER \"" + sourceSchema + "\".\"T_" + replTable + "_AIUD\"'); \n" +
"END LOOP; \n" +
"END;";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2200;
stmt.executeUpdate(strSQL);
/////////////////////////////
//2. drop replTable if exists
/////////////////////////////
location = 3100;
strSQL = "BEGIN \n" +
"FOR x IN (SELECT * FROM DUAL WHERE EXISTS (SELECT NULL FROM ALL_TABLES \n" +
" WHERE OWNER = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + replTable + "') ) LOOP \n" +
" EXECUTE IMMEDIATE('DROP TABLE \"" + sourceSchema + "\".\"" + replTable + "\"'); \n" +
"END LOOP; \n" +
"END;";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 3200;
stmt.executeUpdate(strSQL);
/////////////////////////////
//3. drop sequence if exists
/////////////////////////////
location = 4100;
strSQL = "BEGIN \n" +
"FOR x IN (SELECT * FROM DUAL WHERE EXISTS (SELECT NULL FROM ALL_SEQUENCES \n" +
" WHERE SEQUENCE_OWNER = '" + sourceSchema + "' \n" +
" AND SEQUENCE_NAME = 'SEQ_" + replTable + "') ) LOOP \n" +
" EXECUTE IMMEDIATE('DROP SEQUENCE \"" + sourceSchema + "\".\"SEQ_" + replTable + "\"'); \n" +
"END LOOP; \n" +
"END;";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 4200;
stmt.executeUpdate(strSQL);
/////////////////////////////
//4. create sequence
/////////////////////////////
location = 5100;
strSQL = "CREATE SEQUENCE \"" + sourceSchema + "\".\"SEQ_" + replTable + "\" ORDER";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 5200;
stmt.executeUpdate(strSQL);
/////////////////////////////
//5. create replTable
/////////////////////////////
location = 6100;
strSQL = "SELECT COLUMN_NAME, \n" +
" DATA_TYPE || \n" +
" CASE WHEN DATA_TYPE IN ('CHAR', 'NCHAR', 'VARCHAR', 'VARCHAR2', 'NVARCHAR2') \n" +
" THEN '(' || TO_CHAR(DATA_LENGTH) || ') ' \n " +
" ELSE ' ' END || \n" +
" CASE WHEN NULLABLE = 'Y' THEN 'NULL' ELSE 'NOT NULL' END AS ATTRIBUTES \n" +
"FROM ALL_TAB_COLUMNS \n" +
"WHERE OWNER = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + sourceTable + "' \n" +
" AND DATA_TYPE NOT IN ('BLOB', 'BFILE', 'RAW', 'LONG RAW', 'MLSLABEL', 'BFILE', 'XMLTYPE') \n" +
"ORDER BY COLUMN_ID";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
//Get Column names
String columnName = "";
String attributes = "";
String oldColumns = "";
String newColumns = "";
String columns = "";
location = 6200;
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
columnName = rs.getString(1);
attributes = rs.getString(2);
if (rs.getRow() == 1)
{
location = 6300;
strSQL = "CREATE TABLE \"" + sourceSchema + "\".\"" + replTable + "\" \n" +
"(\"" + appendColumnName + "\" NUMBER NOT NULL PRIMARY KEY, \n" +
"change_type CHAR(1) NOT NULL, \n";
strSQL = strSQL + "\"" + columnName + "\" " + attributes;
oldColumns = " :OLD.\"" + columnName + "\"";
newColumns = " :NEW.\"" + columnName + "\"";
columns = " \"" + columnName + "\"";
}
else
{
strSQL = strSQL + ", \n \"" + columnName + "\" " + attributes;
oldColumns = oldColumns + ", \n :OLD.\"" + columnName + "\"";
newColumns = newColumns + ", \n :NEW.\"" + columnName + "\"";
columns = columns + ", \n \"" + columnName + "\"";
}
}
location = 6400;
strSQL = strSQL + ")";
if (debug)
Logger.printMsg("Executing SQL: \n" + strSQL);
location = 6500;
stmt.executeUpdate(strSQL);
/////////////////////////////
//6. create trigger to capture changes
/////////////////////////////
location = 7100;
strSQL = "CREATE TRIGGER T_" + replTable + "_AIUD \n" +
"AFTER INSERT OR UPDATE OR DELETE \n" +
"ON \"" + sourceSchema + "\".\"" + sourceTable + "\" \n" +
"REFERENCING \n" +
" NEW AS NEW \n" +
" OLD AS OLD \n" +
"FOR EACH ROW \n" +
"DECLARE \n" +
" v_id NUMBER; \n" +
"\n" +
"BEGIN \n" +
" SELECT \"" + sourceSchema + "\".\"SEQ_" + replTable + "\".NEXTVAL \n" +
" INTO v_id \n" +
" FROM DUAL; \n" +
"\n" +
" IF INSERTING THEN \n " +
" INSERT INTO \"" + sourceSchema + "\".\"" + replTable + "\" \n" +
" (\"" + appendColumnName + "\", \n" +
" change_type, \n" +
columns + ") \n" +
" VALUES (v_id, \n" +
" 'I', \n" +
newColumns + "); \n" +
"\n" +
" ELSIF UPDATING THEN \n " +
" INSERT INTO \"" + sourceSchema + "\".\"" + replTable + "\" \n" +
" (\"" + appendColumnName + "\", \n" +
" change_type, \n" +
columns + ") \n" +
" VALUES (v_id, \n" +
" 'U', \n" +
newColumns + "); \n" +
"\n" +
" ELSIF DELETING THEN \n " +
" INSERT INTO \"" + sourceSchema + "\".\"" + replTable + "\" \n" +
" (\"" + appendColumnName + "\", \n" +
" change_type, \n" +
columns + ") \n" +
" VALUES (v_id, \n" +
" 'D', \n" +
oldColumns + "); \n" +
"\n" +
" END IF; \n " +
"END;";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 7200;
stmt.executeUpdate(strSQL);
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static boolean snapshotSourceTable(Connection conn, String sourceSchema, String sourceTable) throws SQLException
{
String method = "snapshotSourceTable";
int location = 1000;
try
{
location = 2000;
String sourceType = "oracle";
String replTable = GP.getReplTableName(sourceType, sourceTable);
boolean found = false;
location = 2100;
Statement stmt = conn.createStatement();
//Check for the trigger
//for the trigger to exist, the table must exist too
location = 2200;
String strSQL = "SELECT NULL \n" +
"FROM ALL_TRIGGERS \n" +
"WHERE TABLE_OWNER = '" + sourceSchema + "' \n" +
" AND TRIGGER_NAME = 'T_" + replTable + "_AIUD'";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
found = true;
}
if (found)
{
location = 2400;
found = false;
//Check for the OS table
//for the trigger to exist, the table must exist too
strSQL = "SELECT NULL \n" +
"FROM ALL_TABLES \n" +
"WHERE OWNER = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + replTable + "'";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2500;
rs = stmt.executeQuery(strSQL);
while (rs.next())
{
found = true;
}
}
location = 3000;
return found;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static void checkSourceSchema(Connection conn, String sourceDatabase, String sourceSchema) throws SQLException
{
String method = "checkSourceSchema";
int location = 1000;
try
{
location = 2000;
boolean found = false;
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
String strSQL = "SELECT NULL \n" +
"FROM ALL_USERS \n" +
"WHERE USERNAME = '" + sourceSchema + "'";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2400;
while (rs.next())
{
location = 2500;
found = true;
}
if (!(found))
{
throw new SQLException("SourceSchema: \"" + sourceSchema + "\" NOT FOUND!");
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static void checkSourceTable(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable) throws SQLException
{
String method = "checkSourceTable";
int location = 1000;
try
{
location = 2000;
boolean found = false;
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
String strSQL = "SELECT NULL \n" +
"FROM ALL_TABLES \n" +
"WHERE OWNER = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + sourceTable + "' \n" +
"UNION \n" +
"SELECT NULL \n" +
"FROM ALL_VIEWS \n" +
"WHERE OWNER = '" + sourceSchema + "' \n" +
" AND VIEW_NAME = '" + sourceTable + "'";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2400;
while (rs.next())
{
location = 2500;
found = true;
}
if (!(found))
{
throw new SQLException("SourceTable: \"" + sourceSchema + "\".\"" + sourceTable + "\" NOT FOUND!");
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static void checkAppendColumnName(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable, String appendColumnName) throws SQLException
{
String method = "checkAppendColumnName";
int location = 1000;
try
{
location = 2000;
boolean found = false;
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
String strSQL = "SELECT NULL \n" +
"FROM ALL_TAB_COLUMNS \n" +
"WHERE TABLE_NAME = '" + sourceTable + "' \n" +
" AND OWNER = '" + sourceSchema + "' \n" +
" AND COLUMN_NAME = '" + appendColumnName + "'";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2400;
while (rs.next())
{
location = 2500;
found = true;
}
if (!(found))
{
throw new SQLException("AppendColumnName: \"" + appendColumnName + "\" does not exist in the SourceTable: \"" + sourceSchema + "\".\"" + sourceTable + "\"!");
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static void checkReplAppendColumnName(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable, String appendColumnName) throws SQLException
{
String method = "checkReplAppendColumnName";
int location = 1000;
try
{
location = 2000;
boolean found = false;
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
String strSQL = "SELECT NULL \n" +
"FROM ALL_TAB_COLUMNS \n" +
"WHERE TABLE_NAME = '" + sourceTable + "' \n" +
" AND OWNER = '" + sourceSchema + "' \n" +
" AND COLUMN_NAME = '" + appendColumnName + "'";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2400;
while (rs.next())
{
location = 2500;
found = true;
}
//For replication, you need to specify a NEW column that doesn't already exist
// If found, throw an error
if (found)
{
throw new SQLException("Replication ColumnName: \"" + appendColumnName + "\" exists in the SourceTable: \"" + sourceSchema + "\".\"" + sourceTable + "\"! Provide a NEW column name for Replication that doesn't exist in the SourceTable.");
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static void checkReplPrimaryKey(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable) throws SQLException
{
String method = "checkReplPrimaryKey";
int location = 1000;
try
{
location = 2000;
boolean found = false;
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
String strSQL = "SELECT NULL \n" +
"FROM ALL_CONSTRAINTS dc \n" +
"WHERE OWNER = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + sourceTable + "' \n" +
" AND CONSTRAINT_TYPE = 'P'";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2400;
while (rs.next())
{
location = 2500;
found = true;
}
if (!(found))
{
throw new SQLException("Primary key required on SourceTable: \"" + sourceSchema + "\".\"" + sourceTable + "\" for replication!");
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static ResultSet getSchemaList(Connection conn) throws SQLException
{
String method = "getSchemaList";
int location = 1000;
try
{
location = 2000;
//only get schemas in which there is a table or view
String strSQL = "SELECT DISTINCT t.OWNER AS SCHEMA_NAME\n" +
"FROM ALL_TABLES t\n" +
"JOIN DBA_USERS u ON t.OWNER = u.USERNAME\n" +
"WHERE u.DEFAULT_TABLESPACE NOT IN ('SYSTEM', 'SYSAUX')\n" +
"UNION\n" +
"SELECT DISTINCT v.OWNER\n" +
"FROM ALL_VIEWS v\n" +
"JOIN DBA_USERS u ON v.OWNER = u.USERNAME\n" +
"WHERE u.DEFAULT_TABLESPACE NOT IN ('SYSTEM', 'SYSAUX')";
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
ResultSet rs = stmt.executeQuery(strSQL);
location = 3000;
return rs;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static ResultSet getTableList(Connection conn, String sourceSchema) throws SQLException
{
String method = "getTableList";
int location = 1000;
try
{
location = 2000;
//only get tables in which there is a table or view
String strSQL = "SELECT t.TABLE_NAME\n" +
"FROM ALL_TABLES t\n" +
"JOIN DBA_USERS u ON t.OWNER = u.USERNAME\n" +
"WHERE u.DEFAULT_TABLESPACE NOT IN ('SYSTEM', 'SYSAUX')\n" +
"AND u.USERNAME = '" + sourceSchema + "'";
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
ResultSet rs = stmt.executeQuery(strSQL);
location = 3000;
return rs;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
}