Skip to content

Instantly share code, notes, and snippets.

@nathanlws
Last active December 18, 2015 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanlws/5797500 to your computer and use it in GitHub Desktop.
Save nathanlws/5797500 to your computer and use it in GitHub Desktop.
FoundationDB SQL Parser IdentifierCase
//
// See: https://github.com/foundationdb/sql-parser
//
import com.foundationdb.sql.parser.*;
import com.foundationdb.sql.parser.SQLParserContext.*;
public class CustomCase {
public static class ColumnNamePrinter implements Visitor {
@Override
public Visitable visit(Visitable visitable) {
QueryTreeNode node = (QueryTreeNode)visitable;
if(node.getNodeType() == NodeTypes.COLUMN_REFERENCE) {
ColumnReference ref = (ColumnReference)node;
System.out.println(" " + ref.getColumnName());
}
return visitable;
}
@Override
public boolean visitChildrenFirst(Visitable node) { return false; }
@Override
public boolean stopTraversal() { return false; }
@Override
public boolean skipChildren(Visitable node) { return false; }
}
public static class CustomSQLParser extends SQLParser {
private IdentifierCase identCase = IdentifierCase.PRESERVE;
public void setIdentifierCase(IdentifierCase newCase) {
identCase = newCase;
}
public IdentifierCase getIdentifierCase() {
return identCase;
}
}
public static void main(String[] args) throws Exception {
if(args.length != 1) {
System.err.println("Expected query argument");
System.exit(1);
}
final String s = args[0];
CustomSQLParser parser = new CustomSQLParser();
ColumnNamePrinter printer = new ColumnNamePrinter();
for(IdentifierCase c : IdentifierCase.values()) {
System.out.println(c);
parser.setIdentifierCase(c);
StatementNode stmt = parser.parseStatement(s);
stmt.accept(new ColumnNamePrinter());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment