forked from RunningJon/outsourcer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonDB.java
executable file
·801 lines (649 loc) · 20.7 KB
/
CommonDB.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
import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class CommonDB
{
private static String myclass = "CommonDB";
public static boolean debug = false;
public static void checkSourceObjects(String sourceType, String sourceServer, String sourceInstance, int sourcePort, String sourceDatabase, String sourceSchema, String sourceTable, String sourceUser, String sourcePass, String columnName, String refreshType) throws Exception
{
String method = "checkSourceObjects";
int location = 1000;
Connection extConn = null;
try
{
location = 2000;
//schema
//table
//append column if applicable
if (sourceType.equals("sqlserver"))
{
location = 3000;
extConn = connectSQLServer(sourceServer, sourceInstance, sourceUser, sourcePass);
location = 3100;
SQLServer.checkSourceSchema(extConn, sourceDatabase, sourceSchema);
location = 3200;
SQLServer.checkSourceTable(extConn, sourceDatabase, sourceSchema, sourceTable);
location = 3300;
if (refreshType.equals("append"))
{
location = 3400;
SQLServer.checkAppendColumnName(extConn, sourceDatabase, sourceSchema, sourceTable, columnName);
}
else if (refreshType.equals("replication"))
{
location = 3500;
SQLServer.checkReplAppendColumnName(extConn, sourceDatabase, sourceSchema, sourceTable, columnName);
location = 3600;
SQLServer.checkReplPrimaryKey(extConn, sourceDatabase, sourceSchema, sourceTable);
}
location = 3700;
extConn.close();
}
else if (sourceType.equals("oracle"))
{
location = 4000;
extConn = connectOracle(sourceServer, sourceDatabase, sourcePort, sourceUser, sourcePass, 10);
location = 4100;
Oracle.checkSourceSchema(extConn, sourceDatabase, sourceSchema);
location = 4200;
Oracle.checkSourceTable(extConn, sourceDatabase, sourceSchema, sourceTable);
location = 4300;
if (refreshType.equals("append"))
{
location = 4400;
Oracle.checkAppendColumnName(extConn, sourceDatabase, sourceSchema, sourceTable, columnName);
}
else if (refreshType.equals("replication"))
{
location = 4500;
Oracle.checkReplAppendColumnName(extConn, sourceDatabase, sourceSchema, sourceTable, columnName);
location = 4600;
Oracle.checkReplPrimaryKey(extConn, sourceDatabase, sourceSchema, sourceTable);
}
location = 4700;
extConn.close();
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
finally
{
if (extConn != null)
extConn.close();
}
}
public static String formatSQLForColumnName(Connection conn, String strSQL) throws SQLException
{
String method = "formatSQLForColumnName";
int location = 1000;
try
{
location = 2000;
Statement stmt = conn.createStatement();
location = 2100;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2200;
String columnSQL = "";
location = 2300;
while (rs.next())
{
location = 3000;
//Create the list of columns for a SQL statement below
if (columnSQL == "")
{
columnSQL = rs.getString(1);
}
else
{
columnSQL = columnSQL + ", " + rs.getString(1);
}
}
location = 3500;
return columnSQL;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getGPCreateTableDDL(Connection conn, String targetSchema, String targetTable, boolean targetAppendOnly, boolean targetCompressed, boolean targetRowOrientation, String strSQL) throws SQLException
{
String method = "getGPCreateTableDDL";
int location = 1000;
try
{
location = 2000;
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2050;
//Get Column names
String columnName = "";
String dataType = "";
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
ResultSet rs = stmt.executeQuery(strSQL);
String output = "";
location = 2300;
while (rs.next())
{
columnName = rs.getString(1);
dataType = rs.getString(2);
if (rs.getRow() == 1)
output = "CREATE TABLE \"" + targetSchema + "\".\"" + targetTable + "\" \n" +
"(\"" + columnName + "\" " + dataType + " null";
else
output = output + ", \n" +
"\"" + columnName + "\" " + dataType + " null";
}
output = output + ")\n";
location = 3000;
//set the Append-Only, Compression, and Orientation
if (targetAppendOnly)
{
output += "WITH (APPENDONLY=TRUE";
if (ExternalDataD.dbVersion.equals("HAWQ"))
{
if (targetCompressed)
{
output += ", COMPRESSTYPE=snappy";
}
if (targetRowOrientation)
output += ", ORIENTATION=row";
else
output += ", ORIENTATION=parquet";
} else if (ExternalDataD.dbVersion.equals("GPDB"))
{
if (targetCompressed)
output += ", COMPRESSTYPE=quicklz";
if (targetRowOrientation)
output += ", ORIENTATION=row";
else
output += ", ORIENTATION=column";
}
output += ")\n";
}
return output;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
private static String getDistributionDDL(Connection conn, String targetSchema, String targetTable, String strSQL) throws SQLException
{
String method = "getDistributionDDL";
int location = 1000;
try
{
location = 2000;
String distributedBy = "";
if (ExternalDataD.dbVersion.equals("HAWQ"))
{
distributedBy = "DISTRIBUTED RANDOMLY; \n";
}
else
{
if (debug)
Logger.printMsg("Execting SQL: " + strSQL);
location = 2100;
Statement stmt = conn.createStatement();
location = 2200;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2300;
String columnName = "";
location = 2400;
while (rs.next())
{
columnName = rs.getString(1);
if (rs.getRow() == 1)
{
distributedBy = "DISTRIBUTED BY (" + columnName;
}
else
{
distributedBy = distributedBy + ", " + columnName;
}
}
if (!(distributedBy.equals("")))
{
distributedBy = distributedBy + "); \n";
}
else
{
distributedBy = "DISTRIBUTED RANDOMLY; \n";
}
}
location = 3000;
return distributedBy;
}
catch (SQLException ex)
{
//throw ex;
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getGPTableDDL(String sourceType, String sourceServer, String sourceInstance, int sourcePort, String sourceDatabase, String sourceSchema, String sourceTable, String sourceUser, String sourcePass, String targetSchema, String targetTable, boolean targetAppendOnly, boolean targetCompressed, boolean targetRowOrientation) throws Exception
{
String method = "GetGPTableDDL";
int location = 1000;
Connection extConn = null;
try
{
String strSQL = "";
String output = "";
if (sourceType.equals("sqlserver"))
{
location = 3000;
extConn = connectSQLServer(sourceServer, sourceInstance, sourceUser, sourcePass);
location = 3100;
//Get the SQL needed to generate CREATE TABLE
strSQL = SQLServer.getSQLForCreateTable(extConn, sourceDatabase, sourceSchema, sourceTable);
//Get the GP DDL based on source DDL
location = 3200;
output = getGPCreateTableDDL(extConn, targetSchema, targetTable, targetAppendOnly, targetCompressed, targetRowOrientation, strSQL);
location = 3300;
strSQL = SQLServer.getSQLForDistribution(extConn, sourceDatabase, sourceSchema, sourceTable);
//Get the table distribution based on Primary Key of source
location = 3400;
output = output + getDistributionDDL(extConn, targetSchema, targetTable, strSQL);
location = 3500;
extConn.close();
}
else if (sourceType.equals("oracle"))
{
location = 4000;
extConn = connectOracle(sourceServer, sourceDatabase, sourcePort, sourceUser, sourcePass, 10);
location = 4100;
//Get the SQL needed to generate CREATE TABLE
strSQL = Oracle.getSQLForCreateTable(extConn, sourceSchema, sourceTable);
location = 4200;
//Get the GP DDL based on source DDL
output = getGPCreateTableDDL(extConn, targetSchema, targetTable, targetAppendOnly, targetCompressed, targetRowOrientation, strSQL);
location = 4300;
//Get the SQL needed to generate the table distribution based on Primary Key of source
strSQL = Oracle.getSQLForDistribution(extConn, sourceSchema, sourceTable);
location = 4400;
//Get the table distribution based on Primary Key of source
output = output + getDistributionDDL(extConn, targetSchema, targetTable, strSQL);
location = 4500;
extConn.close();
}
//output the DDL for the table to be created in GP
location = 5000;
return output;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
finally
{
if (extConn != null)
extConn.close();
}
}
public static boolean checkSourceReplObjects(String sourceType, String sourceServer, String sourceInstance, int sourcePort, String sourceDatabase, String sourceSchema, String sourceTable, String sourceUser, String sourcePass, String columnName) throws Exception
{
String method = "checkSourceReplObjects";
int location = 1000;
Connection extConn = null;
try
{
location = 2000;
boolean found = false;
if (sourceType.equals("sqlserver"))
{
location = 3000;
extConn = connectSQLServer(sourceServer, sourceInstance, sourceUser, sourcePass);
location = 3100;
found = SQLServer.snapshotSourceTable(extConn, sourceDatabase, sourceSchema, sourceTable);
location = 3200;
extConn.close();
}
else if (sourceType.equals("oracle"))
{
location = 4000;
extConn = connectOracle(sourceServer, sourceDatabase, sourcePort, sourceUser, sourcePass, 10);
location = 4100;
found = Oracle.snapshotSourceTable(extConn, sourceSchema, sourceTable);
location = 4200;
extConn.close();
}
location = 5000;
return found;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
finally
{
if (extConn != null)
extConn.close();
}
}
public static void configureReplication(String sourceType, String sourceServer, String sourceInstance, int sourcePort, String sourceDatabase, String sourceSchema, String sourceTable, String sourceUser, String sourcePass, String columnName) throws Exception
{
String method = "configureReplication";
int location = 1000;
Connection extConn = null;
try
{
location = 2000;
if (sourceType.equals("sqlserver"))
{
location = 2100;
extConn = connectSQLServer(sourceServer, sourceInstance, sourceUser, sourcePass);
location = 2200;
SQLServer.configureReplication(extConn, sourceDatabase, sourceSchema, sourceTable, columnName);
location = 2300;
extConn.close();
}
else if (sourceType.equals("oracle"))
{
location = 3100;
extConn = connectOracle(sourceServer, sourceDatabase, sourcePort, sourceUser, sourcePass, 10);
location = 3200;
Oracle.configureReplication(extConn, sourceDatabase, sourceSchema, sourceTable, columnName);
location = 3300;
extConn.close();
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
finally
{
if (extConn != null)
extConn.close();
}
}
public static String getReplMaxId(String sourceType, String sourceServer, String sourceInstance, int sourcePort, String sourceDatabase, String sourceSchema, String sourceTable, String sourceUser, String sourcePass, String columnName) throws Exception
{
String method = "getReplMaxId";
int location = 1000;
Connection extConn = null;
try
{
location = 2000;
String replTable = GP.getReplTableName(sourceType, sourceTable);
String maxId = "-1";
if (sourceType.equals("sqlserver"))
{
location = 3000;
extConn = connectSQLServer(sourceServer, sourceInstance, sourceUser, sourcePass);
location = 3100;
maxId = SQLServer.getMaxId(extConn, sourceDatabase, sourceSchema, replTable, columnName);
location = 3200;
extConn.close();
}
else if (sourceType.equals("oracle"))
{
location = 4000;
extConn = connectOracle(sourceServer, sourceDatabase, sourcePort, sourceUser, sourcePass, 10);
location = 4100;
maxId = Oracle.getMaxId(extConn, sourceSchema, replTable, columnName);
location = 4200;
extConn.close();
}
location = 5000;
return maxId;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
finally
{
if (extConn != null)
extConn.close();
}
}
public static String getMaxId(String sourceType, String sourceServer, String sourceInstance, int sourcePort, String sourceDatabase, String sourceSchema, String sourceTable, String sourceUser, String sourcePass, String columnName) throws Exception
{
String method = "getMaxId";
int location = 1000;
Connection extConn = null;
try
{
location = 2000;
String maxId = "-1";
if (sourceType.equals("sqlserver"))
{
location = 3000;
extConn = connectSQLServer(sourceServer, sourceInstance, sourceUser, sourcePass);
location = 3100;
maxId = SQLServer.getMaxId(extConn, sourceDatabase, sourceSchema, sourceTable, columnName);
location = 3200;
extConn.close();
}
else if (sourceType.equals("oracle"))
{
location = 4000;
extConn = connectOracle(sourceServer, sourceDatabase, sourcePort, sourceUser, sourcePass, 10);
location = 4100;
maxId = Oracle.getMaxId(extConn, sourceSchema, sourceTable, columnName);
location = 4200;
extConn.close();
}
location = 5000;
return maxId;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
finally
{
if (extConn != null)
extConn.close();
}
}
public static void outputData(Connection conn, String strSQL) throws SQLException
{
String method = "outputData";
int location = 1000;
try
{
location = 2000;
if (debug)
Logger.printMsg("Execting SQL: " + strSQL);
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(strSQL);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String output;
String columnValue = "";
location = 2500;
while (rs.next())
{
output="";
// Get the column names; column indices start from 1
for (int i=1; i<numberOfColumns+1; i++)
{
//Extra try/catch block because of an Oracle problem at Virgin Airlines.
//Getting an "ArrayIndexOutOfBoundsException" error which might be caused by the Oracle JDBC driver
try
{
columnValue = rs.getString(i);
}
catch (Exception e)
{
columnValue = (String) rs.getObject(i);
}
if (columnValue != null)
{
if (debug)
Logger.printMsg(rsmd.getColumnName(i) + ":" + rsmd.getColumnTypeName(i) + ":" + i + ":" + columnValue);
//Oracle has the DATE data type (SQL Server has date and datetime)
//The range for Oracle DATE is January 1, 4712 BCE through December 31, 4712 CE
if (rsmd.getColumnTypeName(i) == "DATE" || rsmd.getColumnTypeName(i) == "TIMESTAMP" )
{
columnValue = df.format(rs.getTimestamp(i));
}
//Filter out \ and | from the columnValue for not null records. the rest will default to "null"
columnValue = columnValue.replace("\\", "\\\\");
columnValue = columnValue.replace("|", "\\|");
columnValue = columnValue.replace("\r", " ");
columnValue = columnValue.replace("\n", " ");
columnValue = columnValue.replace("\0", "");
}
if (i == 1)
output = columnValue;
else
output = output + "|" + columnValue;
}
System.out.println(output);
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static Connection connectSQLServer(String sourceServer, String sourceInstance, String sourceUser, String sourcePass) throws Exception
{
String method = "connectSQLServer";
int location = 1000;
try
{
if (debug)
Logger.printMsg("try");
location = 2000;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
if (debug)
Logger.printMsg("loaded classforname");
String connectionUrl = null;
if (sourceInstance != null)
{
location = 3000;
if (sourceInstance.compareTo("") != 0)
{
location = 3100;
connectionUrl = "jdbc:sqlserver://" + sourceServer + ";instanceName=" + sourceInstance + ";CODEPAGE=65001;responseBuffering=adaptive;selectMethod=cursor;";
}
else
{
location = 4100;
connectionUrl = "jdbc:sqlserver://" + sourceServer + ";CODEPAGE=65001;responseBuffering=adaptive;selectMethod=cursor;";
}
}
else
{
location = 5100;
connectionUrl = "jdbc:sqlserver://" + sourceServer + ";CODEPAGE=65001;responseBuffering=adaptive;selectMethod=cursor;";
}
if (debug)
Logger.printMsg("connectionUrl: " + connectionUrl);
location = 6000;
if (debug)
Logger.printMsg("attempting to connect");
Connection extConn = DriverManager.getConnection(connectionUrl, sourceUser, sourcePass);
location = 6100;
return extConn;
}
catch(ClassNotFoundException e)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":ClassNotFound! " + e.getMessage() + ")");
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static Connection connectOracle(String sourceServer, String sourceDatabase, int sourcePort, String sourceUser, String sourcePass, int fetchSize) throws Exception
{
String method = "connectOracle";
int location = 1000;
try
{
location = 2000;
Class.forName("oracle.jdbc.driver.OracleDriver");
location = 2100;
System.setProperty("java.security.egd", "file:///dev/urandom");
location = 2200;
Properties props = new Properties();
location = 2210;
props.put("user", sourceUser);
location = 2220;
props.put("password", sourcePass);
location = 2230;
String fetchSizeString = Integer.toString(fetchSize);
location = 2235;
props.put("defaultRowPrefetch", fetchSizeString);
location = 2300;
String connectionUrl = "jdbc:oracle:thin:@" + sourceServer + ":" + sourcePort + "/" + sourceDatabase;
location = 2400;
Connection extConn = DriverManager.getConnection(connectionUrl, props);
return extConn;
}
catch(ClassNotFoundException e)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":ClassNotFound! " + e.getMessage() + ")");
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static Connection connectGP(String configFile) throws SQLException
{
String method = "connectGP";
int location = 1000;
try
{
Connection conn = connectGP(configFile, null, null);
return conn;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static Connection connectGP(String configFile, String gpUserName, String gpPassword) throws SQLException
{
String method = "connectGP";
int location = 1000;
try
{
try
{
location = 2000;
Class.forName("org.postgresql.Driver");
}
catch (Exception e)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + e.getMessage() + ")");
}
location = 2100;
OSProperties.getPropValues(configFile);
location = 2200;
String connectionUrl = "jdbc:postgresql://" + OSProperties.gpServer + ":" + OSProperties.gpPort + "/" + OSProperties.gpDatabase;
location = 2300;
if (gpUserName == null)
{
gpUserName = OSProperties.gpUserName;
gpPassword = OSProperties.gpPassword;
}
Connection conn = DriverManager.getConnection(connectionUrl, gpUserName, gpPassword);
location = 2400;
return conn;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
catch (IOException iex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + iex.getMessage() + ")");
}
}
}