org.bitbucket.bradleysmithllc.etlunit.feature.database.RelationalDataSet Maven / Gradle / Ivy
package org.bitbucket.bradleysmithllc.etlunit.feature.database;
import org.bitbucket.bradleysmithllc.etlunit.feature.database.json.database.extract.ExtractRequest;
import org.bitbucket.bradleysmithllc.etlunit.io.file.DataFileSchema;
import java.util.*;
public class RelationalDataSet
{
private final List columns = new ArrayList();
private final List keyColumns = new ArrayList();
private final List orderColumns = new ArrayList();
private final Map columnNames = new HashMap();
private final String schema;
private final String sql;
private final String name;
private final boolean relationalSet;
private final DatabaseConnection databaseConnection;
public RelationalDataSet(String schema, String name, DatabaseConnection databaseConnection)
{
relationalSet = true;
this.schema = schema;
this.name = name;
this.sql = null;
this.databaseConnection = databaseConnection;
}
public RelationalDataSet(String sql, DatabaseConnection databaseConnection, String name)
{
relationalSet = false;
this.schema = "none";
this.name = name;
this.sql = sql;
this.databaseConnection = databaseConnection;
}
private boolean sqlDataSet()
{
return !relationalSet;
}
public RelationalColumn getColumn(String name)
{
RelationalColumn column = columnNames.get(name);
if (column == null)
{
throw new IllegalArgumentException("Column not found: " + name);
}
return column;
}
public void addColumn(RelationalColumn str)
{
for (RelationalColumn column : columns)
{
if (column.getName().equals(str.getName()))
{
throw new IllegalArgumentException("Duplicate column name: " + str.getName());
}
}
columns.add(str);
columnNames.put(str.getName(), str);
}
public void addPrimaryKeyColumn(RelationalColumn str)
{
for (RelationalColumn column : keyColumns)
{
if (column.getName().equals(str.getName()))
{
throw new IllegalArgumentException("Duplicate Key column name: " + str.getName());
}
}
if (!columnNames.containsKey(str.getName()))
{
throw new IllegalArgumentException("Column " + str.getName() + " not found");
}
keyColumns.add(str);
}
public void addOrderByColumn(RelationalColumn str)
{
for (RelationalColumn column : orderColumns)
{
if (column.getName().equals(str.getName()))
{
throw new IllegalArgumentException("Duplicate Order by column name: " + str.getName());
}
}
if (!columnNames.containsKey(str.getName()))
{
throw new IllegalArgumentException("Column " + str.getName() + " not found");
}
orderColumns.add(str);
}
public RelationalDataSet createSubView(List list, ExtractRequest.ColumnListMode listMode)
{
if (sqlDataSet())
{
throw new UnsupportedOperationException("Can't create view of sql data set");
}
List actualList = new ArrayList(columns);
List actualKeys = new ArrayList(keyColumns);
List actualOrders = new ArrayList(orderColumns);
Map actualNames = new HashMap(columnNames);
if (actualKeys.size() == 0)
{
actualKeys.addAll(actualList);
}
if (list != null)
{
if (listMode == ExtractRequest.ColumnListMode.INCLUDE)
{
actualList.clear();
actualKeys.clear();
actualOrders.clear();
actualNames.clear();
}
for (String col : list)
{
RelationalColumn column = columnNames.get(col);
if (column == null)
{
throw new IllegalArgumentException("Column not exported: " + col);
}
switch (listMode)
{
case INCLUDE:
// check the column
actualList.add(column);
actualNames.put(column.getName(), column);
// check the primary key
if (keyColumns.contains(column))
{
actualKeys.add(column);
}
// check the order columns
if (orderColumns.contains(column))
{
actualOrders.add(column);
}
break;
case EXCLUDE:
// check the column
actualList.remove(column);
actualNames.remove(column.getName());
// check the primary key
if (actualKeys.contains(column))
{
actualKeys.remove(column);
}
// check the order columns
if (orderColumns.contains(column))
{
actualOrders.remove(column);
}
break;
default:
throw new RuntimeException("Refactor error");
}
}
}
RelationalDataSet rds = new RelationalDataSet(schema, name, databaseConnection);
rds.columns.addAll(actualList);
rds.keyColumns.addAll(actualKeys);
rds.orderColumns.addAll(actualOrders);
rds.columnNames.putAll(actualNames);
return rds;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy