All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.etlunit.feature.database.RelationalDataSet Maven / Gradle / Ivy

There is a newer version: 1.6.9
Show newest version
package org.etlunit.feature.database;

import org.etlunit.io.file.DataFileSchema;

import java.util.*;

public class RelationalDataSet
{
	private final List columns = new ArrayList();
	private final List publicColumns = Collections.unmodifiableList(columns);

	private final List keyColumns = new ArrayList();
	private final List publicKeyColumns = Collections.unmodifiableList(keyColumns);

	private final List orderColumns = new ArrayList();
	private final List publicOrderColumns = Collections.unmodifiableList(orderColumns);

	private final Map columnNames = new HashMap();
	private final Map publicColumnNames = Collections.unmodifiableMap(columnNames);

	private final String schema;
	private final String sql;
	private final String name;

	private final boolean relationalSet;

	private final DatabaseConnection databaseConnection;
	private final DatabaseImplementation databaseImplementation;

	private DataFileSchema fileSchema;

	public RelationalDataSet(String schema, String name, DatabaseConnection databaseConnection, DatabaseImplementation databaseImplementation)
	{
		relationalSet = true;
		this.schema = schema;
		this.name = name;
		this.sql = null;
		this.databaseConnection = databaseConnection;
		this.databaseImplementation = databaseImplementation;
	}

	public RelationalDataSet(String sql, DatabaseConnection databaseConnection, DatabaseImplementation databaseImplementation, String name)
	{
		relationalSet = false;
		this.schema = "none";
		this.name = name;
		this.sql = sql;
		this.databaseConnection = databaseConnection;
		this.databaseImplementation = databaseImplementation;
	}

	public boolean hasIdentityColumns()
	{
		for (RelationalColumn column : columns)
		{
			if (column.isAutoIncrement())
			{
				return true;
			}
		}

		return false;
	}

	public boolean sqlDataSet()
	{
		return !relationalSet;
	}

	public String getSql()
	{
		return sql;
	}

	public String getSchema()
	{
		return schema;
	}

	public DataFileSchema getFileSchema()
	{
		if (fileSchema == null)
		{
			throw new IllegalStateException("No schema");
		}

		return fileSchema;
	}

	public void setFileSchema(DataFileSchema fileSchema)
	{
		this.fileSchema = fileSchema;
	}

	public String getName()
	{
		return name;
	}

	public void setColumns(List cols)
	{
		columns.addAll(cols);
	}

	public List getColumns()
	{
		return publicColumns;
	}

	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 setPrimaryKeyColumns(List cols)
	{
		keyColumns.addAll(cols);
	}

	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 resetPrimaryKeyColumns()
	{
		keyColumns.clear();
	}

	public void resetOrderColumns()
	{
		orderColumns.clear();
	}

	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 List getPrimaryKeyColumns()
	{
		return publicKeyColumns;
	}

	public List getPrimaryKeyColumnNames()
	{
		List list = new ArrayList();

		for (RelationalColumn col : keyColumns)
		{
			list.add(col.getName());
		}

		return list;
	}

	public List getOrderByColumns()
	{
		return publicOrderColumns;
	}

	public int getColumnType(String name)
	{
		RelationalColumn relationalColumn = columnNames.get(name);

		if (relationalColumn == null)
		{
			throw new IllegalArgumentException("Column " + name + " not found");
		}

		return relationalColumn.getType();
	}

	public void setColumnType(String name, int type)
	{
		RelationalColumn column = columnNames.get(name);

		if (column == null)
		{
			throw new IllegalArgumentException("Column " + name + " not found");
		}

		column.setType(type);
	}

	public DatabaseConnection getDatabaseConnection()
	{
		return databaseConnection;
	}

	public DatabaseImplementation getDatabaseImplementation()
	{
		return databaseImplementation;
	}

	public String createSQLSelect()
	{
		return createSQLSelect(true);
	}

	public String createSQLSelect(boolean ordered)
	{
		List actualList = new ArrayList(columns);
		List actualOrderBy = new ArrayList(keyColumns);

		if (orderColumns.size() != 0)
		{
			actualOrderBy.clear();
			actualOrderBy.addAll(orderColumns);
		}
		else if (actualOrderBy.size() == 0)
		{
			actualOrderBy.addAll(actualList);
		}

		if (!sqlDataSet())
		{
			StringBuffer buffer = new StringBuffer("SELECT\n");

			for (int i = 0; i < actualList.size(); i++)
			{
				buffer.append('\t');
				buffer.append(actualList.get(i).getName());

				if (i != (actualList.size() - 1))
				{
					buffer.append(',');
				}

				buffer.append('\n');
			}

			buffer.append("FROM\n");
			buffer.append('\t');

			if (schema != null)
			{
				buffer.append(schema);
				buffer.append('.');
			}

			buffer.append(name);

			if (ordered)
			{
				buffer.append("\nORDER BY\n");

				for (int i = 0; i < actualOrderBy.size(); i++)
				{
					buffer.append('\t');
					buffer.append(actualOrderBy.get(i).getName());

					if (i != (actualOrderBy.size() - 1))
					{
						buffer.append(',');
					}

					buffer.append('\n');
				}
			}

			return buffer.toString();
		}

		return sql;
	}

	public String createSQLInsert()
	{
		if (sqlDataSet())
		{
			throw new UnsupportedOperationException("Cannot insert into a sql data set");
		}

		StringBuffer buffer = new StringBuffer("INSERT INTO\n");
		buffer.append('\t');

		if (schema != null)
		{
			buffer.append(schema);
			buffer.append('.');
		}

		buffer.append(name);
		buffer.append("\n(\n");

		for (int i = 0; i < columns.size(); i++)
		{
			buffer.append('\t');
			buffer.append(columns.get(i).getName());

			if (i != (columns.size() - 1))
			{
				buffer.append(',');
			}

			buffer.append('\n');
		}

		buffer.append(")\n");

		buffer.append("VALUES\n(\n\t");

		for (int i = 0; i < columns.size(); i++)
		{
			if (i != 0)
			{
				buffer.append(',');
			}

			buffer.append('?');
		}
		buffer.append("\n)");

		return buffer.toString();
	}

	public RelationalDataSet createSubView(List list, ExtractOperation.columnListMode_enum 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 == ExtractOperation.columnListMode_enum.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, databaseImplementation);

		rds.columns.addAll(actualList);
		rds.keyColumns.addAll(actualKeys);
		rds.orderColumns.addAll(actualOrders);
		rds.columnNames.putAll(actualNames);

		return rds;
	}

	public String getQualifiedName()
	{
		return getSchema() + "." + getName();
	}

	public Map getColumnTypes()
	{
		Map columnTypes = new HashMap();

		for (RelationalColumn col : columns)
		{
			columnTypes.put(col.getName(), col.getType());
		}

		return columnTypes;
	}

	public List getColumnNames()
	{
		List columnNames = new ArrayList();

		for (RelationalColumn col : columns)
		{
			columnNames.add(col.getName());
		}

		return columnNames;
	}

	public List getOrderByColumnNames()
	{
		List columnNames = new ArrayList();

		for (RelationalColumn col : orderColumns)
		{
			columnNames.add(col.getName());
		}

		return columnNames;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy