org.etlunit.feature.database.SQLAggregatorImpl Maven / Gradle / Ivy
package org.etlunit.feature.database;
import org.etlunit.TestExecutionError;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SQLAggregatorImpl implements SQLAggregator
{
private final List lineList = new ArrayList();
private final List statementList = new ArrayList();
private final String text;
public SQLAggregatorImpl(String ddlRef, SQLLocator locator, DatabaseConnection databaseConnection) throws IOException, TestExecutionError
{
text = makeText(ddlRef, locator, databaseConnection);
// Use the output of the line processor and break it into statements since JDBC doesn't support multiple in one statement
splitLines();
}
public SQLAggregatorImpl(String text) throws IOException, TestExecutionError
{
this.text = text;
// Use the output of the line processor and break it into statements since JDBC doesn't support multiple in one statement
splitLines();
}
private void splitLines()
{
String [] sts = text.split(";;");
for (int i = 0; i < sts.length; i++)
{
// remove blanks
if (!sts[i].trim().equals(""))
{
statementList.add(new FileRefImpl("synthetic", i, sts[i]));
}
}
}
private String makeText(String ddlRef, SQLLocator locator, DatabaseConnection databaseConnection) throws TestExecutionError, IOException
{
String ptext = locator.locate(ddlRef, databaseConnection);
DDLDirectiveNameExpression ddldne = new DDLDirectiveNameExpression(ptext);
Map includedReferences = new HashMap();
Map provideMap = new HashMap();
Map requireMap = new HashMap();
while (ddldne.hasNext())
{
String subText = "";
String directive = ddldne.getDirective();
String ddlReference = ddldne.getDdlReference();
if (directive.equals("include"))
{
if (!includedReferences.containsKey(ddlReference))
{
subText = locator.locate(ddlReference, databaseConnection);
includedReferences.put(ddlReference, "");
}
}
else if (directive.equals("require"))
{
requireMap.put(ddlReference, ddlReference);
}
else if (directive.equals("provide"))
{
provideMap.put(ddlReference, ddlReference);
}
else
{
throw new TestExecutionError("Directive '" + directive + "' not understood", DatabaseConstants.ERR_BAD_DIRECTIVE);
}
int startIndex = ddldne.start();
int endIndex = ddldne.end();
// insert
ptext = (startIndex == 0 ? "" : ptext.substring(0, startIndex - 1)) + subText + ptext.substring(endIndex);
ddldne = new DDLDirectiveNameExpression(ptext);
}
// validate that all 'require's have been met
for (String require : requireMap.keySet())
{
if (!provideMap.containsKey(require))
{
throw new TestExecutionError("Script requires '" + require + "' but it was not provided", DatabaseConstants.ERR_UNMET_REQUIRE);
}
}
return ptext;
}
public String getText()
{
return text;
}
public Aggregator getLineAggregator()
{
return new Aggregator()
{
int index = 0;
public boolean hasNext()
{
return index < lineList.size();
}
public FileRef next()
{
return lineList.get(index++);
}
};
}
public Aggregator getStatementAggregator()
{
return new Aggregator()
{
int index = 0;
public boolean hasNext()
{
return index < statementList.size();
}
public FileRef next()
{
return statementList.get(index++);
}
};
}
}