Last active
December 18, 2015 14:29
-
-
Save nathanlws/5797500 to your computer and use it in GitHub Desktop.
FoundationDB SQL Parser IdentifierCase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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