Skip to content

Commit

Permalink
Fix #415
Browse files Browse the repository at this point in the history
  • Loading branch information
hmiguim committed Jan 21, 2025
1 parent c50d5b6 commit ce178d9
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import com.databasepreservation.model.data.ArrayCell;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -79,6 +79,7 @@
import com.databasepreservation.common.server.index.utils.SolrUtils;
import com.databasepreservation.common.utils.LobManagerUtils;
import com.databasepreservation.common.utils.ViewerUtils;
import com.databasepreservation.model.data.ArrayCell;
import com.databasepreservation.model.data.BinaryCell;
import com.databasepreservation.model.data.Cell;
import com.databasepreservation.model.data.ComposedCell;
Expand Down Expand Up @@ -382,7 +383,7 @@ private static ViewerView getView(ViewerDatabaseFromToolkit vdb, ViewStructure v
result.setName(view.getName());
result.setUuid(SolrUtils.randomUUID());
try {
result.setColumns(getColumns(view.getColumns()));
result.setColumns(getColumns(view.getColumns(), null, new ArrayList<>()));
} catch (ViewerException e) {
LOGGER.error("Could not convert the columns for view {}", view, e);
result.setColumns(new ArrayList<>());
Expand Down Expand Up @@ -466,7 +467,7 @@ private static ViewerTable getTable(ViewerDatabaseFromToolkit vdb, TableStructur
result.setCountRows(table.getRows());
result.setSchemaName(table.getSchema());
result.setSchemaUUID(vdb.getSchema(result.getSchemaName()).getUuid());
result.setColumns(getColumns(table.getColumns()));
result.setColumns(getColumns(table.getColumns(), table.getPrimaryKey(), table.getForeignKeys()));
if (!simpleMetadata) {
result.setTriggers(getTriggers(table.getTriggers()));
result.setPrimaryKey(getPrimaryKey(table, references));
Expand Down Expand Up @@ -611,23 +612,30 @@ private static ViewerTrigger getTrigger(Trigger trigger) {
return result;
}

private static List<ViewerColumn> getColumns(List<ColumnStructure> columns) throws ViewerException {
private static List<ViewerColumn> getColumns(List<ColumnStructure> columns, PrimaryKey primaryKey,
List<ForeignKey> foreignKeys) throws ViewerException {
List<ViewerColumn> result = new ArrayList<>();
int index = 0;
for (ColumnStructure column : columns) {
result.add(getColumn(column, index++));
result.add(getColumn(column, index++, primaryKey, foreignKeys));
}
return result;
}

private static ViewerColumn getColumn(ColumnStructure column, int index) throws ViewerException {
private static ViewerColumn getColumn(ColumnStructure column, int index, PrimaryKey primaryKey,
List<ForeignKey> foreignKeys) throws ViewerException {
ViewerColumn result = new ViewerColumn();
Type columnType = column.getType();

boolean isPrimaryKey = primaryKey != null && primaryKey.getColumnNames().contains(column.getName());
boolean isForeignKey = !foreignKeys.isEmpty() && foreignKeys.stream().map(
m -> m.getReferences().stream().map(Reference::getColumn).collect(Collectors.toSet()).contains(column.getName()))
.findFirst().orElse(false);

ViewerType columnViewerType = getType(columnType);
result.setType(columnViewerType);
if (!simpleMetadata) {
result.setSolrName(getColumnSolrName(index, columnType, columnViewerType));
result.setSolrName(getColumnSolrName(index, columnType, columnViewerType, isPrimaryKey, isForeignKey));
result.setColumnIndexInEnclosingTable(index);
}
result.setDisplayName(column.getName());
Expand All @@ -649,7 +657,8 @@ private static ViewerColumn getColumn(ColumnStructure column, int index) throws
* @return the column name
* @throws ViewerException
*/
private static String getColumnSolrName(int index, Type type, ViewerType viewerType) throws ViewerException {
private static String getColumnSolrName(int index, Type type, ViewerType viewerType, boolean isPrimaryKey,
boolean isForeignKey) throws ViewerException {
// suffix must always be set before being used
String suffix;
String prefix = ViewerConstants.SOLR_INDEX_ROW_COLUMN_NAME_PREFIX;
Expand Down Expand Up @@ -694,7 +703,7 @@ private static String getColumnSolrName(int index, Type type, ViewerType viewerT
throw new ViewerException("Unknown type: " + type.toString());
}

return prefix + index + suffix;
return prefix + index + (isPrimaryKey || isForeignKey ? ViewerConstants.SOLR_DYN_STRING : suffix);
}

private static ViewerType getType(Type type) throws ViewerException {
Expand Down

0 comments on commit ce178d9

Please sign in to comment.