Skip to content

Commit

Permalink
0004421: Primary key ordering based on the sequence of the PK
Browse files Browse the repository at this point in the history
  • Loading branch information
jumpmind-josh committed Jun 11, 2020
1 parent 9c21f64 commit b8aec9f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Expand Up @@ -211,6 +211,8 @@ public static Table nextTable(XmlPullParser parser, String catalog, String schem
column.setName(attributeValue);
} else if (attributeName.equalsIgnoreCase("primaryKey")) {
column.setPrimaryKey(FormatUtils.toBoolean(attributeValue));
} else if (attributeName.equalsIgnoreCase("primaryKeySeq")) {
column.setPrimaryKeySequence(Integer.parseInt(attributeValue));
} else if (attributeName.equalsIgnoreCase("required")) {
column.setRequired(FormatUtils.toBoolean(attributeValue));
} else if (attributeName.equalsIgnoreCase("type")) {
Expand Down Expand Up @@ -466,6 +468,8 @@ public static void write(Table table, Writer output) {
output.write("\t\t<column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
if (column.isPrimaryKey()) {
output.write(" primaryKey=\"" + column.isPrimaryKey() + "\"");
output.write(" primaryKeySeq=\"" + column.getPrimaryKeySequence() + "\"");

}
if (column.isRequired()) {
output.write(" required=\"" + column.isRequired() + "\"");
Expand Down Expand Up @@ -532,6 +536,7 @@ public static void write(Table table, Writer output) {
output.write("/>\n");
}
}


for (ForeignKey fk : table.getForeignKeys()) {
output.write("\t\t<foreign-key name=\"" + StringEscapeUtils.escapeXml(fk.getName()) + "\" foreignTable=\""
Expand Down
12 changes: 12 additions & 0 deletions symmetric-db/src/main/java/org/jumpmind/db/model/Column.java
Expand Up @@ -111,6 +111,8 @@ public class Column implements Cloneable, Serializable {

private Map<String, PlatformColumn> platformColumns;

private int primaryKeySequence;

// private String[] enumValues;

public Column() {
Expand Down Expand Up @@ -762,4 +764,14 @@ public void setCharOctetLength(int charOctetLength) {
this.charOctetLength = charOctetLength;
}

public int getPrimaryKeySequence() {
return primaryKeySequence;
}

public void setPrimaryKeySequence(int primaryKeySequence) {
this.primaryKeySequence = primaryKeySequence;
}



}
3 changes: 3 additions & 0 deletions symmetric-db/src/main/java/org/jumpmind/db/model/Table.java
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
Expand Down Expand Up @@ -836,6 +837,8 @@ public List<Column> getPrimaryKeyColumnsAsList() {
}
}
}
selectedColumns = selectedColumns.stream().sorted((c1, c2) ->
c1.getPrimaryKeySequence() - c2.getPrimaryKeySequence()).collect(Collectors.toList());
return selectedColumns;
} else {
return new ArrayList<Column>(0);
Expand Down
Expand Up @@ -702,10 +702,13 @@ protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaDat

Collection<String> primaryKeys = readPrimaryKeyNames(metaData, tableName);

int primaryKeySequence = 1;
for (Iterator<String> it = primaryKeys.iterator(); it.hasNext();) {
Column column = table.findColumn(it.next(), true);
if (column != null) {
column.setPrimaryKey(true);
column.setPrimaryKeySequence(primaryKeySequence);
primaryKeySequence++;
}
}

Expand Down Expand Up @@ -1027,7 +1030,13 @@ protected Collection<String> readPrimaryKeyNames(DatabaseMetaDataWrapper metaDat
while (pkData.next()) {
Map<String, Object> values = readMetaData(pkData, getColumnsForPK());

pks.add(readPrimaryKeyName(metaData, values));
int pkSequence = 1;
try {
pkSequence = readPrimaryKeySequence(metaData, values);
} catch (Exception e) {
log.info("Unable to read primary key sequence.");
}
pks.add(pkSequence - 1, readPrimaryKeyName(metaData, values));
}
} finally {
close(pkData);
Expand All @@ -1049,6 +1058,21 @@ protected String readPrimaryKeyName(DatabaseMetaDataWrapper metaData, Map<String
throws SQLException {
return (String) values.get(getName("COLUMN_NAME"));
}

/*
* Extracts a primary key sequence from the result set.
*
* @param metaData The database meta data
*
* @param values The primary key meta data values as defined by {@link
* #getColumnsForPK()}
*
* @return The primary key sequence
*/
protected int readPrimaryKeySequence(DatabaseMetaDataWrapper metaData, Map<String, Object> values)
throws SQLException {
return (Integer) values.get(getName("KEY_SEQ"));
}

/*
* Retrieves the foreign keys of the indicated table.
Expand Down

0 comments on commit b8aec9f

Please sign in to comment.