{
input = new NoCaseSensitiveStream(reader);
}
catch (IOException ex)
{
throw new ExprValidationException("IOException lexing query SQL '" + querySQL + '\'', ex);
}
int whereIndex = -1;
int groupbyIndex = -1;
int havingIndex = -1;
int orderByIndex = -1;
List<Integer> unionIndexes = new ArrayList<Integer>();
EsperEPL2GrammarLexer lex = new EsperEPL2GrammarLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lex);
List tokenList = tokens.getTokens();
for (int i = 0; i < tokenList.size(); i++)
{
Token token = (Token) tokenList.get(i);
if ((token == null) || token.getText() == null)
{
break;
}
String text = token.getText().toLowerCase().trim();
if (text.equals("where"))
{
whereIndex = token.getCharPositionInLine() + 1;
}
if (text.equals("group"))
{
groupbyIndex = token.getCharPositionInLine() + 1;
}
if (text.equals("having"))
{
havingIndex = token.getCharPositionInLine() + 1;
}
if (text.equals("order"))
{
orderByIndex = token.getCharPositionInLine() + 1;
}
if (text.equals("union"))
{
unionIndexes.add(token.getCharPositionInLine() + 1);
}
}
// If we have a union, break string into subselects and process each
if (unionIndexes.size() != 0)
{
StringWriter changedSQL = new StringWriter();
int lastIndex = 0;
for (int i = 0; i < unionIndexes.size(); i++)
{
int index = unionIndexes.get(i);
String fragment;
if (i > 0)
{
fragment = querySQL.substring(lastIndex + 5, index - 1);
}
else
{
fragment = querySQL.substring(lastIndex, index - 1);
}
String lexedFragment = lexSampleSQL(fragment);
if (i > 0)
{
changedSQL.append("union ");
}
changedSQL.append(lexedFragment);
lastIndex = index - 1;
}
// last part after last union
String fragment = querySQL.substring(lastIndex + 5, querySQL.length());
String lexedFragment = lexSampleSQL(fragment);
changedSQL.append("union ");
changedSQL.append(lexedFragment);
return changedSQL.toString();
}
// Found a where clause, simplest cases
if (whereIndex != -1)
{
StringWriter changedSQL = new StringWriter();
String prefix = querySQL.substring(0, whereIndex + 5);
String suffix = querySQL.substring(whereIndex + 5, querySQL.length());
changedSQL.write(prefix);
changedSQL.write("1=0 and ");
changedSQL.write(suffix);
return changedSQL.toString();
}
// No where clause, find group-by
int insertIndex;
if (groupbyIndex != -1)
{
insertIndex = groupbyIndex;
}
else if (havingIndex != -1)
{
insertIndex = havingIndex;
}
else if (orderByIndex != -1)
{
insertIndex = orderByIndex;
}
else
{
StringWriter changedSQL = new StringWriter();
changedSQL.write(querySQL);
changedSQL.write(" where 1=0 ");
return changedSQL.toString();
}
try
{
StringWriter changedSQL = new StringWriter();
String prefix = querySQL.substring(0, insertIndex - 1);
changedSQL.write(prefix);
changedSQL.write("where 1=0 ");
String suffix = querySQL.substring(insertIndex - 1, querySQL.length());
changedSQL.write(suffix);
return changedSQL.toString();
}
catch (Exception ex)
{
String text = "Error constructing sample SQL to retrieve metadata for JDBC-drivers that don't support metadata, consider using the " + SAMPLE_WHERECLAUSE_PLACEHOLDER + " placeholder or providing a sample SQL";
log.error(text, ex);
throw new ExprValidationException(text, ex);
}
}