forked from RunningJon/outsourcer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLServer.java
executable file
·877 lines (719 loc) · 25.1 KB
/
SQLServer.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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
import java.sql.*;
import java.net.*;
import java.io.*;
public class SQLServer
{
private static String myclass = "SQLServer";
public static boolean debug = false;
private static String getSQLInject(Connection conn, String sourceDatabase) throws SQLException
{
String method = "getSQLInject";
int location = 1000;
try
{
location = 2000;
String SQLInject = "SET TRANSACTION ISOLATION LEVEL READ ";
String level = "UNCOMMITTED";
location = 2100;
String strSQL = "SELECT NULL \n" +
"FROM [" + sourceDatabase + "].sys.databases \n" +
"WHERE name = '" + sourceDatabase + "' \n" +
" AND is_read_committed_snapshot_on = 1";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2200;
Statement stmt = conn.createStatement();
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
level = "COMMITTED";
}
SQLInject = SQLInject + level + " \n";
location = 2400;
return SQLInject;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
private static String getSQLForColumns(String sourceDatabase, 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 [" + sourceDatabase + "].INFORMATION_SCHEMA.COLUMNS \n" +
"WHERE TABLE_NAME = '" + sourceTable + "' \n" +
" AND TABLE_SCHEMA = '" + sourceSchema + "' \n" +
" AND DATA_TYPE NOT IN ('binary', 'image', 'timestamp', 'xml', 'varbinary', 'text', 'ntext', 'sql_variant') \n" +
"ORDER BY ORDINAL_POSITION";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2200;
return strSQL;
}
catch (Exception ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getSQLForData(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable, String refreshType, String appendColumnName, String appendColumnMax) throws SQLException
{
String method = "getSQLForData";
int location = 1000;
try
{
//Create SQL Statement for getting the column names
location = 2000;
String strSQL = getSQLForColumns(sourceDatabase, sourceSchema, sourceTable);
//Execute SQL Statement AND format columns for next SELECT statement
location = 2100;
String columnSQL = CommonDB.formatSQLForColumnName(conn, strSQL);
//Create SQL Statement for retrieving data FROM table
location = 2200;
strSQL = getSQLInject(conn, sourceDatabase);
location = 2300;
strSQL = strSQL + "SELECT " + columnSQL + "\n " +
"FROM [" + sourceDatabase + "].[" + sourceSchema + "].[" + sourceTable + "] \n";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
//Add filter for append refreshType
if (debug)
Logger.printMsg("About to refreshType: " + refreshType);
location = 2400;
if (refreshType.equals("append"))
{
location = 2500;
//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 + "\" > '" + appendColumnMax + "'"; //greater than what is in GP currently
}
}
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2600;
return strSQL;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getSQLForCreateTable(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable) throws SQLException
{
String method = "getSQLForCreateTable";
int location = 1000;
try
{
location = 2000;
String strSQL = "SELECT sub.column_name, \n" +
" CASE WHEN sub.datatype = 'char' THEN 'character' \n" +
" WHEN sub.datatype = 'nchar' THEN 'character' \n" +
" WHEN sub.datatype = 'datetime' THEN 'timestamp' \n" +
" WHEN sub.datatype = 'datetime2' THEN 'timestamp' \n" +
" WHEN sub.datatype = 'datetimeoffset' THEN 'timestamptz' \n" +
" WHEN sub.datatype = 'decimal' THEN 'numeric' \n" +
" WHEN sub.datatype = 'float' THEN 'float8' \n" +
" WHEN sub.datatype = 'real' THEN 'float8' \n" +
" WHEN sub.datatype = 'int' THEN 'integer' \n" +
" WHEN sub.datatype = 'bit' THEN 'boolean' \n" +
" WHEN sub.datatype = 'nvarchar' THEN 'varchar' \n" +
" WHEN sub.datatype = 'smalldatetime' THEN 'timestamp' \n" +
" WHEN sub.datatype = 'smallmoney' THEN 'numeric' \n" +
" WHEN sub.datatype = 'money' THEN 'numeric' \n" +
" WHEN sub.datatype = 'sysname' THEN 'varchar' \n" +
" WHEN sub.datatype = 'tinyint' THEN 'smallint' \n" +
" WHEN sub.datatype = 'uniqueidentifier' THEN 'varchar(36)' \n" +
" ELSE sub.datatype END + CASE WHEN sub.datatype in ('nchar', 'char', 'varchar', 'nvarchar', 'sysname') \n" +
" AND sub.length <> -1 THEN '(' + cast(sub.length as varchar) + ')' \n" +
" ELSE '' END as datatype \n" +
"FROM (SELECT REPLACE(REPLACE(LOWER(sc.name), '\"', ''), '.', '_') column_name, \n" +
" st.name as datatype, \n" +
" sc.max_length as length, \n" +
" sc.column_id \n" +
" FROM [" + sourceDatabase + "].sys.objects so \n" +
" JOIN [" + sourceDatabase + "].sys.columns sc ON so.object_id = sc.object_id \n" +
" JOIN [" + sourceDatabase + "].sys.schemas su ON so.schema_id = su.schema_id \n" +
" JOIN [" + sourceDatabase + "].sys.types st ON sc.system_type_id = st.system_type_id AND st.system_type_id = st.user_type_id \n" +
" WHERE so.type in ('U', 'V') \n" +
" AND su.name = '" + sourceSchema + "' \n" +
" AND so.name = '" + sourceTable + "' ) sub \n" +
"WHERE sub.datatype not in ('binary', 'image', 'timestamp', 'xml', 'varbinary', 'text', 'ntext', 'sql_variant', 'hierarchyid') \n" +
"ORDER BY sub.column_id";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2100;
return strSQL;
}
catch (Exception ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getSQLForDistribution(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable) throws SQLException
{
String method = "getSQLForDistribution";
int location = 1000;
try
{
location = 2000;
String strSQL = "SELECT '\"' + LOWER(c.name) + '\"' as COLUMN_NAME \n" +
"FROM [" + sourceDatabase + "].sys.objects o \n" +
"JOIN [" + sourceDatabase + "].sys.schemas s ON o.schema_id = s.schema_id \n" +
"JOIN [" + sourceDatabase + "].sys.indexes i ON o.object_id = i.object_id \n" +
"JOIN [" + sourceDatabase + "].sys.index_columns ic ON o.object_id = ic.object_id AND i.index_id = ic.index_id \n" +
"JOIN [" + sourceDatabase + "].sys.columns c ON o.object_id = c.object_id AND ic.column_id = c.column_id \n" +
"WHERE o.name = '" + sourceTable + "' \n" +
" AND s.name = '" + sourceSchema + "' \n" +
" AND i.is_primary_key = 1 \n" +
"ORDER BY ic.key_ordinal";
if (debug)
Logger.printMsg("Executing SQL: " + 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";
location = 2200;
Statement stmt = conn.createStatement();
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2400;
while (rs.next())
{
msg = "Success!";
}
location = 2500;
return msg;
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static String getMaxId(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable, String columnName) throws SQLException
{
String method = "getMaxId";
int location = 1000;
try
{
location = 2000;
String maxId = "-1";
String strSQL = getSQLInject(conn, sourceDatabase);
location = 2100;
strSQL = strSQL + "SELECT MAX([" + columnName + "]) \n" +
"FROM [" + sourceDatabase + "].[" + sourceSchema + "].[" + sourceTable + "]";
location = 2200;
Statement stmt = conn.createStatement();
location = 2300;
ResultSet rs = stmt.executeQuery(strSQL);
location = 2400;
while (rs.next())
{
maxId = rs.getString(1);
}
location = 2500;
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 = "sqlserver";
String replTable = GP.getReplTableName(sourceType, sourceTable);
//1. drop triggers if exists
//2. drop replTable if exists
//3. create replTable
//4. create triggers
location = 2100;
Statement stmt = conn.createStatement();
String strSQL = "USE [" + sourceDatabase + "]";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2200;
stmt.executeUpdate(strSQL);
/////////////////////////////
//1. drop triggers if exists
/////////////////////////////
location = 3100;
strSQL = "IF EXISTS (SELECT NULL FROM [" + sourceDatabase + "].[sys].[objects] \n" +
"WHERE name = 'T_" + replTable + "_I' AND type = 'TR') \n " +
" DROP TRIGGER [" + sourceSchema + "].[T_" + replTable + "_I]";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 3200;
stmt.executeUpdate(strSQL);
location = 3300;
strSQL = "IF EXISTS (SELECT NULL FROM [" + sourceDatabase + "].[sys].[objects] \n" +
"WHERE name = 'T_" + replTable + "_U' AND type = 'TR') \n " +
" DROP TRIGGER [" + sourceSchema + "].[T_" + replTable + "_U] \n";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 3400;
stmt.executeUpdate(strSQL);
location = 3500;
strSQL = "IF EXISTS (SELECT NULL FROM [" + sourceDatabase + "].[sys].[objects] \n" +
"WHERE name = 'T_" + replTable + "_D' AND type = 'TR') \n " +
" DROP TRIGGER [" + sourceSchema + "].[T_" + replTable + "_D] \n";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 3600;
stmt.executeUpdate(strSQL);
/////////////////////////////
//2. drop replTable if exists
/////////////////////////////
location = 4000;
strSQL = "IF EXISTS (SELECT NULL FROM [" + sourceDatabase + "].[sys].[objects] \n" +
"WHERE object_id = OBJECT_ID(N'[" + sourceDatabase + "].[" + sourceSchema + "].[" + replTable + "]" + "') AND type = (N'U')) \n" +
" DROP TABLE [" + sourceDatabase + "].[" + sourceSchema + "].[" + replTable + "]";
location = 4100;
stmt.executeUpdate(strSQL);
/////////////////////////////
//3. create replTable
/////////////////////////////
location = 5000;
strSQL = "SELECT COLUMN_NAME, \n" +
" DATA_TYPE + \n" +
" CASE WHEN DATA_TYPE in ('nchar', 'char', 'varchar', 'nvarchar', 'sysname') AND CHARACTER_MAXIMUM_LENGTH <> -1 THEN '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(8000)) + ')' \n" +
" WHEN DATA_TYPE in ('nchar', 'char', 'varchar', 'nvarchar') AND CHARACTER_MAXIMUM_LENGTH = -1 THEN '(MAX)' \n" +
" WHEN DATA_TYPE in ('decimal', 'numeric') THEN '(' + CAST(NUMERIC_PRECISION AS VARCHAR(8000)) + ',' + CAST(NUMERIC_SCALE AS VARCHAR(8000)) + ')' \n" +
" ELSE '' END + \n" +
" CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE ' NULL' END AS ATTRIBUTES \n" +
"FROM [" + sourceDatabase + "].INFORMATION_SCHEMA.COLUMNS \n" +
"WHERE TABLE_CATALOG = '" + sourceDatabase + "' \n " +
" AND TABLE_SCHEMA = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + sourceTable + "' \n" +
" AND DATA_TYPE NOT IN ('binary', 'image', 'timestamp', 'xml', 'varbinary', 'text', 'ntext', 'sql_variant') \n" +
"ORDER BY ORDINAL_POSITION";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
//Get Column names
String columnName = "";
String attributes = "";
String columns = "";
location = 5200;
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
columnName = rs.getString(1);
attributes = rs.getString(2);
if (rs.getRow() == 1)
{
location = 5300;
strSQL = "CREATE TABLE [" + sourceDatabase + "].[" + sourceSchema + "].[" + replTable + "] \n" +
"([" + appendColumnName + "] bigint IDENTITY NOT NULL PRIMARY KEY, \n" +
"change_type char(1) not null, \n";
strSQL = strSQL + "[" + columnName + "] " + attributes;
columns = " [" + columnName + "]";
}
else
{
strSQL = strSQL + ", \n [" + columnName + "] " + attributes;
columns = columns + ", \n [" + columnName + "]";
}
}
location = 5400;
strSQL = strSQL + ");";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 5500;
stmt.executeUpdate(strSQL);
/////////////////////////////
//4. create triggers
/////////////////////////////
location = 6000;
strSQL = "CREATE TRIGGER [" + sourceSchema + "].[T_" + replTable + "_I] \n" +
"ON [" + sourceSchema + "].[" + sourceTable + "] \n" +
"AFTER INSERT AS \n" +
"BEGIN \n" +
"SET NOCOUNT ON; \n" +
"INSERT INTO [" + sourceDatabase + "].[" + sourceSchema + "].[" + replTable + "] \n" +
" (change_type, \n" +
columns + ") \n" +
"SELECT 'I', \n" +
columns + " \n" +
"FROM INSERTED \n" +
"END";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 6100;
stmt.executeUpdate(strSQL);
location = 7000;
strSQL = "CREATE TRIGGER [" + sourceSchema + "].[T_" + replTable + "_U] \n" +
"ON [" + sourceSchema + "].[" + sourceTable + "] \n" +
"AFTER UPDATE AS \n" +
"BEGIN \n" +
"SET NOCOUNT ON; \n" +
"INSERT INTO [" + sourceDatabase + "].[" + sourceSchema + "].[" + replTable + "] \n" +
" (change_type, \n" +
columns + ") \n" +
"SELECT 'U', \n" +
columns + " \n" +
"FROM INSERTED \n" +
"END";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 7100;
stmt.executeUpdate(strSQL);
location = 8000;
strSQL = "CREATE TRIGGER [" + sourceSchema + "].[T_" + replTable + "_D] \n" +
"ON [" + sourceSchema + "].[" + sourceTable + "] \n" +
"AFTER DELETE AS \n" +
"BEGIN \n" +
"SET NOCOUNT ON; \n" +
"INSERT INTO [" + sourceDatabase + "].[" + sourceSchema + "].[" + replTable + "] \n" +
" (change_type, \n" +
columns + ") \n" +
"SELECT 'D', \n" +
columns + " \n" +
"FROM DELETED \n" +
"END";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 8100;
stmt.executeUpdate(strSQL);
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static boolean snapshotSourceTable(Connection conn, String sourceDatabase, String sourceSchema, String sourceTable) throws SQLException
{
String method = "snapshotSourceTable";
int location = 1000;
try
{
location = 2000;
String sourceType = "sqlserver";
String replTable = GP.getReplTableName(sourceType, sourceTable);
boolean found = false;
location = 2100;
Statement stmt = conn.createStatement();
//SQL Server has three distinct triggers instead of just one
location = 2300;
String strSQL = "SELECT COUNT(*) AS counter \n" +
"FROM [" + sourceDatabase + "].sys.objects trig \n" +
"JOIN [" + sourceDatabase + "].sys.objects tab on trig.parent_object_id = tab.object_id \n" +
"JOIN [" + sourceDatabase + "].sys.schemas sch on trig.schema_id = sch.schema_id \n" +
"WHERE trig.type = 'TR' \n" +
" AND sch.name = '" + sourceSchema + "' \n" +
" AND tab.name = '" + sourceTable + "' \n" +
" AND trig.name in ('T_" + replTable + "_I', 'T_" + replTable + "_U', 'T_" + replTable + "_D')";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2400;
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
if (rs.getInt(1) == 3)
{
location = 2500;
found = true;
}
}
if (found)
{
location = 2600;
found = false;
location = 2700;
strSQL = "SELECT NULL \n" +
"FROM [" + sourceDatabase + "].INFORMATION_SCHEMA.TABLES \n" +
"WHERE TABLE_SCHEMA = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + replTable + "' \n" +
" AND TABLE_TYPE = 'BASE TABLE'";
if (debug)
Logger.printMsg("Executing SQL: " + strSQL);
location = 2800;
rs = stmt.executeQuery(strSQL);
while (rs.next())
{
location = 2900;
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 [" + sourceDatabase + "].INFORMATION_SCHEMA.SCHEMATA \n" +
"WHERE SCHEMA_NAME = '" + 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 [" + sourceDatabase + "].INFORMATION_SCHEMA.TABLES \n" +
"WHERE TABLE_SCHEMA = '" + sourceSchema + "' \n" +
" AND TABLE_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 [" + sourceDatabase + "].INFORMATION_SCHEMA.COLUMNS \n" +
"WHERE TABLE_SCHEMA = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + sourceTable + "' \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 [" + sourceDatabase + "].INFORMATION_SCHEMA.COLUMNS \n" +
"WHERE TABLE_SCHEMA = '" + sourceSchema + "' \n" +
" AND TABLE_NAME = '" + sourceTable + "' \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 [" + sourceDatabase + "].INFORMATION_SCHEMA.TABLE_CONSTRAINTS \n " +
"WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' \n" +
" AND TABLE_SCHEMA = '" + sourceSchema + "' \n" +
" AND TABLE_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("Primary key required on SourceTable: \"" + sourceSchema + "\".\"" + sourceTable + "\" for replication!");
}
}
catch (SQLException ex)
{
throw new SQLException("(" + myclass + ":" + method + ":" + location + ":" + ex.getMessage() + ")");
}
}
public static ResultSet getDatabaseList(Connection conn) throws SQLException
{
String method = "getDatabaseList";
int location = 1000;
try
{
location = 2000;
String strSQL = "SELECT name FROM sys.databases WHERE HAS_DBACCESS(name) = 1 ORDER BY name";
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 getSchemaList(Connection conn, String sourceDatabase) throws SQLException
{
String method = "getSchemaList";
int location = 1000;
try
{
location = 2000;
String strSQL = "SELECT DISTINCT TABLE_SCHEMA FROM [" + sourceDatabase + "].INFORMATION_SCHEMA.TABLES ORDER BY TABLE_SCHEMA";
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 sourceDatabase, String sourceSchema) throws SQLException
{
String method = "getTableList";
int location = 1000;
try
{
location = 2000;
String strSQL = "SELECT TABLE_NAME\n" +
"FROM [" + sourceDatabase + "].INFORMATION_SCHEMA.TABLES\n" +
"WHERE TABLE_SCHEMA = '" + sourceSchema + "'\n" +
"ORDER BY TABLE_NAME";
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() + ")");
}
}
}