Skip to content

Instantly share code, notes, and snippets.

@nathanlws
Last active December 18, 2015 14:29
Show Gist options
  • Select an option

  • Save nathanlws/5797500 to your computer and use it in GitHub Desktop.

Select an option

Save nathanlws/5797500 to your computer and use it in GitHub Desktop.

Revisions

  1. nathanlws revised this gist Oct 8, 2013. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions CustomCase.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    import com.akiban.sql.parser.*;
    import com.akiban.sql.parser.SQLParserContext.*;
    //
    // 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 {
  2. nathanlws revised this gist Jun 17, 2013. 1 changed file with 7 additions and 27 deletions.
    34 changes: 7 additions & 27 deletions CustomCase.java
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,14 @@
    import com.akiban.sql.parser.*;
    import com.akiban.sql.parser.SQLParserContext.*;

    /**
    * Demonstration of how to get custom identifier case behavior.
    *
    * Compiling and running:
    * $ javac CustomCase.java
    * $ java CustomCase
    * IdentifierCase: UPPER
    * QWER
    * ASDF
    * ZXCV
    * jKlM
    * IdentifierCase: LOWER
    * qwer
    * asdf
    * zxcv
    * jKlM
    * IdentifierCase: PRESERVE
    * qwer
    * aSdF
    * ZXCV
    * jKlM
    */
    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());
    System.out.println(" " + ref.getColumnName());
    }
    return visitable;
    }
    @@ -58,13 +36,15 @@ public IdentifierCase getIdentifierCase() {
    }

    public static void main(String[] args) throws Exception {
    final String s = "SELECT qwer, aSdF, ZXCV, \"jKlM\" FROM t";

    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("IdentifierCase: " + c);
    System.out.println(c);
    parser.setIdentifierCase(c);
    StatementNode stmt = parser.parseStatement(s);
    stmt.accept(new ColumnNamePrinter());
  3. nathanlws revised this gist Jun 17, 2013. 1 changed file with 22 additions and 20 deletions.
    42 changes: 22 additions & 20 deletions CustomCase.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,28 @@
    import com.akiban.sql.parser.*;
    import com.akiban.sql.parser.SQLParserContext.*;

    /**
    * Demonstration of how to get custom identifier case behavior.
    *
    * Compiling and running:
    * $ javac CustomCase.java
    * $ java CustomCase
    * IdentifierCase: UPPER
    * QWER
    * ASDF
    * ZXCV
    * jKlM
    * IdentifierCase: LOWER
    * qwer
    * asdf
    * zxcv
    * jKlM
    * IdentifierCase: PRESERVE
    * qwer
    * aSdF
    * ZXCV
    * jKlM
    */
    public class CustomCase {
    public static class ColumnNamePrinter implements Visitor {
    @Override
    @@ -49,23 +71,3 @@ public static void main(String[] args) throws Exception {
    }
    }
    }

    /*
    $ javac CustomCase.java
    $ java CustomCase
    IdentifierCase: UPPER
    QWER
    ASDF
    ZXCV
    jKlM
    IdentifierCase: LOWER
    qwer
    asdf
    zxcv
    jKlM
    IdentifierCase: PRESERVE
    qwer
    aSdF
    ZXCV
    jKlM
    */
  4. nathanlws renamed this gist Jun 17, 2013. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions gistfile1.java → CustomCase.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import com.akiban.sql.parser.*;
    import com.akiban.sql.parser.SQLParserContext.*;

    public class Quick {
    public class CustomCase {
    public static class ColumnNamePrinter implements Visitor {
    @Override
    public Visitable visit(Visitable visitable) {
    @@ -23,7 +23,6 @@ public Visitable visit(Visitable visitable) {
    public boolean skipChildren(Visitable node) { return false; }
    }


    public static class CustomSQLParser extends SQLParser {
    private IdentifierCase identCase = IdentifierCase.PRESERVE;

    @@ -36,7 +35,6 @@ public IdentifierCase getIdentifierCase() {
    }
    }


    public static void main(String[] args) throws Exception {
    final String s = "SELECT qwer, aSdF, ZXCV, \"jKlM\" FROM t";

    @@ -53,7 +51,8 @@ public static void main(String[] args) throws Exception {
    }

    /*
    Running prints:
    $ javac CustomCase.java
    $ java CustomCase
    IdentifierCase: UPPER
    QWER
    ASDF
  5. nathanlws created this gist Jun 17, 2013.
    72 changes: 72 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    import com.akiban.sql.parser.*;
    import com.akiban.sql.parser.SQLParserContext.*;

    public class Quick {
    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 {
    final String s = "SELECT qwer, aSdF, ZXCV, \"jKlM\" FROM t";

    CustomSQLParser parser = new CustomSQLParser();
    ColumnNamePrinter printer = new ColumnNamePrinter();

    for(IdentifierCase c : IdentifierCase.values()) {
    System.out.println("IdentifierCase: " + c);
    parser.setIdentifierCase(c);
    StatementNode stmt = parser.parseStatement(s);
    stmt.accept(new ColumnNamePrinter());
    }
    }
    }

    /*
    Running prints:
    IdentifierCase: UPPER
    QWER
    ASDF
    ZXCV
    jKlM
    IdentifierCase: LOWER
    qwer
    asdf
    zxcv
    jKlM
    IdentifierCase: PRESERVE
    qwer
    aSdF
    ZXCV
    jKlM
    */