Skip to content

Commit 90e8331

Browse files
committed
Implemented to add a join key in a select statement to resolve the issue
of [join key] field in reference. Fixed #28
1 parent ee93eaf commit 90e8331

File tree

1 file changed

+133
-47
lines changed

1 file changed

+133
-47
lines changed

src/main/java/ca/sqlpower/architect/etl/kettle/KettleJob.java

Lines changed: 133 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import ca.sqlpower.sqlobject.SQLObjectException;
6969
import ca.sqlpower.sqlobject.SQLRelationship;
7070
import ca.sqlpower.sqlobject.SQLRelationship.ColumnMapping;
71+
import ca.sqlpower.sqlobject.SQLRelationship.SQLImportedKey;
7172
import ca.sqlpower.sqlobject.SQLTable;
7273
import ca.sqlpower.util.Monitorable;
7374
import ca.sqlpower.util.MonitorableImpl;
@@ -213,38 +214,60 @@ public void doExport(List<SQLTable> tableList, SQLDatabase targetDB ) throws SQL
213214
}
214215
// if quoting and not a PostgresDDLGenerator then adding square brackets for SQLServer
215216
String columnName = column.getName();
216-
if ((isQuoting) && ddlGeneratorClass!= null && !ddlGeneratorClass.equals(PostgresDDLGenerator.class.getName())
217-
&& !(columnName.startsWith("[") && columnName.endsWith("]"))) {
218-
columnName = "["+columnName+"]";
219-
} else if ((isQuoting) && !(columnName.startsWith("\"") && columnName.endsWith("\""))) {
220-
//else if quoting for PostgresDDLGenerator
221-
columnName = "\""+columnName+"\"";
222-
}
217+
columnName = getUpdatedName(isQuoting, columnName,ddlGeneratorClass);
223218
logger.debug(" Quoted columnName: "+ columnName);
224219
if (column.getSourceColumn() == null) {
225-
// if we have no source table then we will get nulls as
226-
// placeholders from the target table.
220+
/** if we have no source table then we will get nulls as
221+
* placeholder from the target table.
222+
*/
227223
sourceTable = table;
228224
sourceColumn = "null";
229225
noMappingForColumn.add(columnName);
230226
} else {
231227
sourceTable = column.getSourceColumn().getParent();
232228
sourceColumn = column.getSourceColumn().getName();
233229
}
234-
// if quoting and not a PostgresDDLGenerator then adding square brackets for SQLServer
235-
if ((isQuoting) && ddlGeneratorClass!= null && !ddlGeneratorClass.equals(PostgresDDLGenerator.class.getName())
236-
&& !(sourceColumn.startsWith("[") && sourceColumn.endsWith("]"))) {
237-
sourceColumn = "["+sourceColumn+"]";
238-
} else if ((isQuoting) && !(sourceColumn.startsWith("\"") && sourceColumn.endsWith("\""))) {
239-
//else if quoting for PostgresDDLGenerator
240-
sourceColumn = "\""+sourceColumn+"\"";
241-
}
230+
/** if quoting and not a PostgresDDLGenerator then adding square brackets for SQLServer */
231+
sourceColumn = getUpdatedName(isQuoting,sourceColumn,ddlGeneratorClass);
242232
if(column.getSourceColumn() != null ) {
243233
if (!tableMapping.containsKey(sourceTable)) {
234+
List<String> pkCols = new ArrayList<String>();
235+
List<String> fkCols = new ArrayList<String>();
244236
StringBuffer buffer = new StringBuffer();
245237
buffer.append("SELECT ");
246238
buffer.append(sourceColumn);
247239
buffer.append(" AS ").append(columnName);
240+
pkCols.add(sourceColumn);
241+
fkCols.add(sourceColumn);
242+
List<SQLRelationship> exportedKeys = getPlaypenExportedKeys(tableList, sourceTable);
243+
for(SQLRelationship exportedKey : exportedKeys) {
244+
for (ColumnMapping mapping: exportedKey.getMappings()) {
245+
SQLColumn pkCol = mapping.getPkColumn();
246+
if(pkCol != null && !pkCol.getName().isEmpty()) {
247+
String updatedPkColName = getUpdatedName(isQuoting, pkCol.getName(),ddlGeneratorClass);
248+
if(!pkCols.contains(updatedPkColName) ){
249+
buffer.append(", ").append
250+
(updatedPkColName).append(" AS ").append(updatedPkColName);
251+
pkCols.add(updatedPkColName);
252+
}
253+
}
254+
}
255+
}
256+
List<SQLImportedKey> importedKeys = getPlaypenImportedKeys(tableList, sourceTable);
257+
for(SQLImportedKey importedKey : importedKeys) {
258+
SQLRelationship relationShip = importedKey.getRelationship();
259+
for (ColumnMapping mapping: relationShip.getMappings()) {
260+
SQLColumn fkCol = mapping.getFkColumn();
261+
if(fkCol != null && !fkCol.getName().isEmpty()) {
262+
String updatedFkColName = getUpdatedName(isQuoting, fkCol.getName(),ddlGeneratorClass);
263+
if(!fkCols.contains(updatedFkColName) ){
264+
buffer.append(", ").append
265+
(updatedFkColName).append(" AS ").append(updatedFkColName);
266+
fkCols.add(updatedFkColName);
267+
}
268+
}
269+
}
270+
}
248271
tableMapping.put(sourceTable, buffer);
249272
} else {
250273
tableMapping.get(sourceTable).append(", ").append
@@ -278,36 +301,20 @@ public void doExport(List<SQLTable> tableList, SQLDatabase targetDB ) throws SQL
278301
}
279302

280303
for (SQLTable sourceTable: tableMapping.keySet()) {
281-
List<String> keys1 = new LinkedList<String>();
282-
List<String> keys2 = new LinkedList<String>();
283-
/**
284-
* Exported keys are different for table in Database then table id PlayPen when user create new Relationship
285-
* manually which doesn't exists in database table.
286-
* Here When user create a kettle job it based on Playpen tables. User might create new relationship. So to get the correct exported keys
287-
* for Playpen Tables we are getting the table whose parent is PlayPen.
288-
* So here tables in tableMapping Map has the table from database (parent is database). While table from the tableList has the parent as a PlayPen.
289-
* So even though the table is dragged from database it can have different exported keys (after dragging it in the playpen)specially when user create new relationship manually.
290-
*/
291-
SQLTable playpenTable = null;
292-
for(SQLTable pTable :tableList) {
293-
if (sourceTable.getName().equalsIgnoreCase(pTable.getName())) {
294-
playpenTable = pTable;
295-
break;
296-
}
297-
}
298-
logger.debug("playpenTable name:" + (playpenTable != null ?playpenTable.getName(): "null"));
299-
if (playpenTable != null) {
300-
for (SQLRelationship exportedKeys : playpenTable.getExportedKeys()) {
301-
for (ColumnMapping mapping: exportedKeys.getMappings()) {
302-
SQLColumn pkCol = mapping.getPkColumn();
303-
SQLColumn fkCol = mapping.getFkColumn();
304-
if(pkCol != null && fkCol!= null) {
305-
keys1.add(pkCol.getName());
306-
keys2.add(fkCol.getName());
307-
}
308-
}
309-
}
310-
}
304+
List<String> keys1 = new LinkedList<String>();
305+
List<String> keys2 = new LinkedList<String>();
306+
List<SQLRelationship> exportedKeys = getPlaypenExportedKeys(tableList, sourceTable);
307+
for (SQLRelationship exportedKey : exportedKeys) {
308+
for (ColumnMapping mapping: exportedKey.getMappings()) {
309+
SQLColumn pkCol = mapping.getPkColumn();
310+
SQLColumn fkCol = mapping.getFkColumn();
311+
if(pkCol != null && fkCol!= null) {
312+
keys1.add(pkCol.getName());
313+
keys2.add(fkCol.getName());
314+
}
315+
}
316+
}
317+
311318
keyFields1.add(keys1.toArray(new String[keys1.size()]));
312319
keyFields2.add(keys2.toArray(new String[keys2.size()]));
313320
JDBCDataSource source = sourceTable.getParentDatabase().getDataSource();
@@ -1077,4 +1084,83 @@ public boolean isSplittingJob() {
10771084
public void setSplittingJob(boolean newValue) {
10781085
settings.setSplittingJob(newValue);
10791086
}
1087+
1088+
/**
1089+
* Method to get the latest exported keys in a playpen.
1090+
* Exported keys are different for table in Database then table in PlayPen
1091+
* when user create new Relationship manually which doesn't exists in database table.
1092+
* When user create a kettle job with tables in a Playpen, user might create
1093+
* new relationship which doesn't exists in database and limited to playpen only.
1094+
* So getting the correct/latest exported keys from the relationship from the table whose parent is PlayPen.
1095+
* Note: Even though the table is dragged from database tree it can have different exported keys
1096+
* (after dragging it in the playpen)specially when user create new relationship manually.
1097+
* @param tableList
1098+
* List of Tables in a playpen
1099+
* @param sourceTable
1100+
* Table whose parent is Database(dbo)
1101+
* @return
1102+
*/
1103+
private List<SQLRelationship> getPlaypenExportedKeys(List<SQLTable> tableList, SQLTable sourceTable) {
1104+
List<SQLRelationship> exportedKeys = new ArrayList<>();
1105+
SQLTable playpenTable = null;
1106+
for(SQLTable pTable :tableList) {
1107+
if (sourceTable.getName().equalsIgnoreCase(pTable.getName())) {
1108+
playpenTable = pTable;
1109+
break;
1110+
}
1111+
}
1112+
if(playpenTable != null) {
1113+
try {
1114+
exportedKeys = playpenTable.getExportedKeys();
1115+
} catch (SQLObjectException e) {
1116+
e.printStackTrace();
1117+
}
1118+
}
1119+
return exportedKeys;
1120+
}
1121+
1122+
/**
1123+
* Method to get the latest imported keys in a playpen.
1124+
* @param tableList
1125+
* List of Tables in a playpen
1126+
* @param sourceTable
1127+
* Table whose parent is Database(dbo)
1128+
* @return
1129+
*/
1130+
private List<SQLImportedKey> getPlaypenImportedKeys(List<SQLTable> tableList, SQLTable sourceTable) {
1131+
List<SQLImportedKey> importedKeys = new ArrayList<>();
1132+
SQLTable playpenTable = null;
1133+
for(SQLTable pTable :tableList) {
1134+
if (sourceTable.getName().equalsIgnoreCase(pTable.getName())) {
1135+
playpenTable = pTable;
1136+
break;
1137+
}
1138+
}
1139+
if(playpenTable != null) {
1140+
try {
1141+
importedKeys = playpenTable.getImportedKeys();
1142+
} catch (SQLObjectException e) {
1143+
e.printStackTrace();
1144+
}
1145+
}
1146+
return importedKeys;
1147+
}
1148+
1149+
/**
1150+
* Get the updated Colum,n name based on
1151+
* @param isQuoting
1152+
* @param columnName
1153+
* @param ddlGeneratorClass
1154+
* @return
1155+
*/
1156+
private String getUpdatedName(boolean isQuoting, String columnName, String ddlGeneratorClass) {
1157+
if ((isQuoting) && ddlGeneratorClass!= null && !ddlGeneratorClass.equals(PostgresDDLGenerator.class.getName())
1158+
&& !(columnName.startsWith("[") && columnName.endsWith("]"))) {
1159+
columnName = "["+columnName+"]";
1160+
} else if ((isQuoting) && !(columnName.startsWith("\"") && columnName.endsWith("\""))) {
1161+
//else if quoting for PostgresDDLGenerator
1162+
columnName = "\""+columnName+"\"";
1163+
}
1164+
return columnName;
1165+
}
10801166
}

0 commit comments

Comments
 (0)