@@ -16,12 +16,9 @@ public class ConstantColumns extends AbstractFeature {
16
16
public enum Property {
17
17
COLUMN_NAMES ,
18
18
COLUMN_TYPES ,
19
- COLUMN_VALUES
20
- }
21
-
22
- public enum Function {
23
- TARGET_PK_WITHOUT_CONSTANTS ,
24
- TEST_FUNCTION
19
+ COLUMN_VALUES ,
20
+ SPLIT_REGEX ,
21
+ WHERE_CLAUSE
25
22
}
26
23
27
24
private boolean valid = true ;
@@ -34,31 +31,30 @@ public boolean initialize(PropertyHelper propertyHelper) {
34
31
List <MigrateDataType > columnTypes = propertyHelper .getMigrationTypeList (KnownProperties .CONSTANT_COLUMN_TYPES );
35
32
putMigrateDataTypeList (Property .COLUMN_TYPES , columnTypes );
36
33
34
+ String splitRegex = propertyHelper .getString (KnownProperties .CONSTANT_COLUMN_SPLIT_REGEX );
35
+ putString (Property .SPLIT_REGEX , splitRegex );
36
+
37
37
List <String > columnValues = columnValues (
38
38
propertyHelper .getString (KnownProperties .CONSTANT_COLUMN_VALUES ),
39
- propertyHelper . getString ( KnownProperties . CONSTANT_COLUMN_SPLIT_REGEX ) );
39
+ splitRegex );
40
40
putStringList (Property .COLUMN_VALUES , columnValues );
41
41
42
+ setWhereClause (propertyHelper );
43
+
44
+ valid = isValid ();
42
45
isInitialized = true ;
43
- valid = isValid (propertyHelper );
44
46
isEnabled = valid && null !=columnNames && !columnNames .isEmpty ();
45
47
return valid ;
46
48
}
47
49
48
50
@ Override
49
- public Object featureFunction (Enum <?> function , Object ... args ) {
50
- switch ((Function ) function ) {
51
- case TARGET_PK_WITHOUT_CONSTANTS :
52
- // args[] should be List<MigrateDataType> targetPrimaryKeyTypes, List<String> targetPrimaryKeyNames
53
- if (null ==args || args .length !=2 || null ==args [0 ] || null ==args [1 ])
54
- throw new IllegalArgumentException ("Expected 2 not-null arguments, got " + (null ==args ? "1" : args .length ));
55
- if (!(args [0 ] instanceof List <?>) || ((List <?>) args [0 ]).isEmpty () || !(((List <?>) args [0 ]).get (0 ) instanceof MigrateDataType ))
56
- throw new IllegalArgumentException ("First argument should be a non-empty List<MigrateDataType>, got " + args [0 ]);
57
- if (!(args [1 ] instanceof List <?>) || ((List <?>) args [1 ]).isEmpty () || !(((List <?>) args [1 ]).get (0 ) instanceof String ))
58
- throw new IllegalArgumentException ("Second argument should be a non-empty List<String>, got " + args [1 ]);
59
- return targetPrimaryKeyTypesWithoutConstantColumns ((List <MigrateDataType >)args [0 ], (List <String >)args [1 ]);
60
- }
61
- return null ;
51
+ public PropertyHelper alterProperties (PropertyHelper helper ) {
52
+ if (!valid ) return null ;
53
+ if (!isEnabled ) return helper ;
54
+
55
+ clean_targetPK (helper );
56
+
57
+ return helper ;
62
58
}
63
59
64
60
private List <String > columnValues (String columnValueString , String regexString ) {
@@ -75,26 +71,56 @@ private List<String> columnValues(String columnValueString, String regexString)
75
71
return columnValues ;
76
72
}
77
73
78
- private List <MigrateDataType > targetPrimaryKeyTypesWithoutConstantColumns (List <MigrateDataType > targetPrimaryKeyTypes , List <String > targetPrimaryKeyNames ) {
79
- if (!isEnabled ) return targetPrimaryKeyTypes ;
80
- if (!valid ) return null ;
74
+ private void setWhereClause (PropertyHelper helper ) {
75
+ if (!isValid ()) return ;
81
76
82
- // As this is valid, we know that the column names, types, and values are all the same size
83
77
List <String > columnNames = getRawStringList (Property .COLUMN_NAMES );
78
+ List <String > columnValues = getRawStringList (Property .COLUMN_VALUES );
84
79
85
- List <MigrateDataType > rtn = new ArrayList <>();
86
- for (String keyName : targetPrimaryKeyNames ) {
87
- if (!columnNames .contains (keyName )) {
88
- rtn .add (targetPrimaryKeyTypes .get (targetPrimaryKeyNames .indexOf (keyName )));
80
+ if (null != columnNames && !columnNames .isEmpty ()
81
+ && null != columnValues && !columnValues .isEmpty ()) {
82
+ List <String > targetPKNames = helper .getStringList (KnownProperties .TARGET_PRIMARY_KEY );
83
+
84
+ String whereClause = "" ;
85
+ for (String columnName : columnNames ) {
86
+ if (null !=targetPKNames && targetPKNames .contains (columnName )) {
87
+ if (!whereClause .isEmpty ())
88
+ whereClause += " AND " ;
89
+ whereClause += columnName + "=" + columnValues .get (columnNames .indexOf (columnName ));
90
+ }
89
91
}
92
+ putString (Property .WHERE_CLAUSE , " AND " + whereClause );
90
93
}
91
- return rtn ;
92
94
}
93
95
94
- private boolean isValid (PropertyHelper propertyHelper ) {
96
+ // Constant columns do not belong on the PK, as they are hard-coded to WHERE_CLAUSE
97
+ private void clean_targetPK (PropertyHelper helper ) {
98
+ List <String > constantColumnNames = getRawStringList (Property .COLUMN_NAMES );
99
+ List <String > currentPKNames = helper .getStringList (KnownProperties .TARGET_PRIMARY_KEY );
100
+ List <MigrateDataType > currentPKTypes = helper .getMigrationTypeList (KnownProperties .TARGET_PRIMARY_KEY_TYPES );
101
+
102
+ List <String > newPKNames = new ArrayList <>();
103
+ List <MigrateDataType > newPKTypes = new ArrayList <>();
104
+
105
+ for (String keyName : currentPKNames ) {
106
+ if (!constantColumnNames .contains (keyName )) {
107
+ newPKNames .add (keyName );
108
+ newPKTypes .add (currentPKTypes .get (currentPKNames .indexOf (keyName )));
109
+ }
110
+ else {
111
+ logger .info ("Removing constant column {} from target PK" , keyName );
112
+ }
113
+ }
114
+
115
+ helper .setProperty (KnownProperties .TARGET_PRIMARY_KEY , newPKNames );
116
+ helper .setProperty (KnownProperties .TARGET_PRIMARY_KEY_TYPES , newPKTypes );
117
+ }
118
+
119
+ private boolean isValid () {
95
120
List <String > columnNames = getRawStringList (Property .COLUMN_NAMES );
96
121
List <MigrateDataType > columnTypes = getRawMigrateDataTypeList (Property .COLUMN_TYPES );
97
122
List <String > columnValues = getRawStringList (Property .COLUMN_VALUES );
123
+ String regexString = getRawString (Property .SPLIT_REGEX );
98
124
99
125
boolean haveColumnNames = null !=columnNames && !columnNames .isEmpty ();
100
126
boolean haveColumnTypes = null !=columnTypes && !columnTypes .isEmpty ();
@@ -110,7 +136,7 @@ private boolean isValid(PropertyHelper propertyHelper) {
110
136
KnownProperties .CONSTANT_COLUMN_NAMES , columnNames ,
111
137
KnownProperties .CONSTANT_COLUMN_TYPES , columnTypes ,
112
138
KnownProperties .CONSTANT_COLUMN_VALUES , columnValues ,
113
- KnownProperties .CONSTANT_COLUMN_SPLIT_REGEX , propertyHelper . getAsString ( KnownProperties . CONSTANT_COLUMN_SPLIT_REGEX ) );
139
+ KnownProperties .CONSTANT_COLUMN_SPLIT_REGEX , regexString );
114
140
valid = false ;
115
141
}
116
142
@@ -121,7 +147,7 @@ private boolean isValid(PropertyHelper propertyHelper) {
121
147
KnownProperties .CONSTANT_COLUMN_NAMES , columnNames ,
122
148
KnownProperties .CONSTANT_COLUMN_TYPES , columnTypes ,
123
149
KnownProperties .CONSTANT_COLUMN_VALUES , columnValues ,
124
- KnownProperties .CONSTANT_COLUMN_SPLIT_REGEX , propertyHelper . getAsString ( KnownProperties . CONSTANT_COLUMN_SPLIT_REGEX ) );
150
+ KnownProperties .CONSTANT_COLUMN_SPLIT_REGEX , regexString );
125
151
valid = false ;
126
152
}
127
153
0 commit comments