Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import java.io.Serializable;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.identifier.DBIdentifier;
Expand Down Expand Up @@ -948,10 +950,11 @@ protected Index createIndex(MetaDataContext context, String prefix,

// if no name provided by user info, make one
if (DBIdentifier.isNull(name)) {
if (tmplate != null)
if (tmplate != null && !DBIdentifier.isNull(tmplate.getIdentifier())) {
name = tmplate.getIdentifier();
else {
name = cols[0].getIdentifier();
} else {
name = DBIdentifier.newIndex(Arrays.stream(cols)
.map(c -> c.getIdentifier().getName()).collect(Collectors.joining("_")));
name = repos.getDBDictionary().getValidIndexName(name, table);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
, indexes = {@Index(name = "idx_index1", columnList = "INDEX1")
, @Index(name = "idx_long", columnList = "LONG_NAME", unique = true)
, @Index(name = "idx_wo_spaces", columnList = "INDEX1,COL2,COL3")
, @Index(name = "idx_with_spaces", columnList = " LONG_NAME , COL2, COL3 ")})
, @Index(name = "idx_with_spaces", columnList = " LONG_NAME , COL2, COL3 ")
, @Index(columnList = "LONG_NAME, COL2")})
public class EntityWithIndices {
@Id
@Column(name = "PK")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,40 @@ public void testIndicesCreated() {

// test multi column index without spaces
assertIndexColumns(table, "idx_wo_spaces", "INDEX1", "COL2", "COL3");

// test multi column index without spaces
assertIndexColumns(table, "idx_with_spaces", "LONG_NAME", "COL2", "COL3");
// test indexes without defined name
assertHasIndexWithColumns(table, "LONG_NAME", "COL2");
}

private void assertIndexColumns(Table table, String indexName, String... assertedColumnNames) {
Index idx = table.getIndex(DBIdentifier.newIndex(indexName));
assertNotNull("Defined index should exist", idx);

final List<String> indexColumnNames = Arrays.stream(idx.getColumns())
.map(c -> c.getIdentifier().getName())
.collect(Collectors.toList());

for (String assertedColumnName : assertedColumnNames) {
assertTrue("Column " + assertedColumnName + " does not exist in index " + indexName, indexColumnNames.contains(assertedColumnName));
}
}

private void assertHasIndexWithColumns(Table table, String...assertedColumnNames) {
for (Index idx: table.getIndexes()) {
Column[] cols = idx.getColumns();
if (cols.length != assertedColumnNames.length) {
continue;
} else {
String[] colNames = Arrays.stream(cols)
.map(Column::getIdentifier)
.map(DBIdentifier::getName)
.collect(Collectors.toList())
.toArray(String[]::new);
if (Arrays.equals(colNames, assertedColumnNames)) {
return;
}
}
}
fail("Could not find an unnamed index with the given columns.");
}

}