avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From froehl...@apache.org
Subject cvs commit: jakarta-avalon-cornerstone/apps/db/src/sql/javacc BasicSQLParser.jj
Date Sat, 15 Dec 2001 18:48:09 GMT
froehlich    01/12/15 10:48:09

  Modified:    apps/db/src/sql/javacc BasicSQLParser.jj
  Log:
  further work on the SQL parser
  
  Revision  Changes    Path
  1.6       +362 -99   jakarta-avalon-cornerstone/apps/db/src/sql/javacc/BasicSQLParser.jj
  
  Index: BasicSQLParser.jj
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/sql/javacc/BasicSQLParser.jj,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BasicSQLParser.jj	2001/11/30 15:59:52	1.5
  +++ BasicSQLParser.jj	2001/12/15 18:48:09	1.6
  @@ -6,44 +6,53 @@
    * the LICENSE file.
    */
   
  +options {
  +  STATIC = false;
  +  //IGNORE_CASE = true;
  +  DEBUG_PARSER = false;
  +}
  +
   PARSER_BEGIN(BasicSQLParser)
   
  -package org.apache.avalon.db.basic.parser;
  +package org.apache.avalon.db.basic.handlers.sql;
   
  -import org.apache.avalon.db.server.sql.AbstractSQLParser;
  +import org.apache.avalon.db.data.Column;
  +import org.apache.avalon.db.data.impl.DefaultVarCharColumn;
  +import org.apache.avalon.db.data.impl.DefaultIntegerColumn;
  +import org.apache.avalon.db.data.impl.DefaultDateColumn;
  +import org.apache.avalon.db.data.impl.DefaultTimeColumn;
  +import org.apache.avalon.db.data.impl.DefaultTimestampColumn;
  +import org.apache.avalon.db.data.impl.DefaultSmallIntColumn;
  +import org.apache.avalon.db.data.impl.DefaultRealColumn;
  +import org.apache.avalon.db.data.impl.DefaultBigIntColumn;
  +import org.apache.avalon.db.data.impl.DefaultDecimalColumn;
  +import org.apache.avalon.db.server.AbstractRequestHandler;
   import org.apache.avalon.db.server.sql.ParseException;
  +import org.apache.avalon.db.actions.ActionException;
   import org.apache.avalon.db.server.sql.SimpleCharStream;
   import org.apache.avalon.db.server.sql.Token;
   import org.apache.avalon.db.server.sql.TokenMgrError;
  -import org.apache.avalon.db.server.AbstractRequestHandler;
  -import org.apache.avalon.db.server.stdhandlers.PingHandler;
  -import org.apache.avalon.db.server.stdhandlers.CatalogHandler;
  -import org.apache.avalon.db.server.stdhandlers.EstablishConnectionHandler;
  -import org.apache.avalon.db.transport.Request;
  -import org.apache.avalon.db.basic.handlers.BasicCloseResultSetHandler;
  -import org.apache.avalon.db.actions.Action;
  -import org.apache.avalon.db.actions.ActionException;
   
  -import java.util.ArrayList;
  +import java.util.Vector;
  +import java.util.Collection;
   
   /**
  - * Class BasicSQLParser
  + * This class is responsible for parsing SQL Statements
  + * This class was generated by JavaCC 
  + *
  + * Please look at the BasicSQLParser.jj file which is
  + * what controls the generation of this class.
    *
    * @author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">g-froehlich@gmx.de</a>
    */
  -public class BasicSQLParser extends AbstractSQLParser {
  -
  -    protected void getCustomRequestHandlers(AbstractRequestHandler[] handlers) {
  -    }
  -
  -    public BasicSQLParser() { }
  -
  -    public void initializeAction(Action action) throws ActionException {
  -        action.setDatabasePersistor(mDatabasePersistor);
  -        setupLogger(action);
  -        action.initialize();
  -    }
  -
  +public class BasicSQLParser {
  +    private String[] mColumns;
  +    private String[] mTables;
  +    private String[] mInsertValues;
  +    private StringBuffer mCriteriaBuffer;
  +    private Column[] mCols;
  +    private String mTableName;
  +    
       public static void main(String args[]) {
           BasicSQLParser parser = new BasicSQLParser(System.in);
           try {
  @@ -52,6 +61,61 @@
               pe.printStackTrace();
           }
       }
  +
  +    public String[] getColumns() {
  +        return mColumns;
  +    }
  +
  +    public String[] getTables() {
  +        return mTables;
  +    }
  +
  +    public String getCriteria() {
  +        return mCriteriaBuffer.toString();
  +    }
  +
  +    public Column[] getCols() {
  +        return mCols;
  +    }
  +
  +    public String getTableName() {
  +        return mTableName;
  +    }
  +
  +    public String[] getInsertValues() {
  +        return mInsertValues;
  +    }
  +
  +    private Column createColumn(Token name, Token type, Token length, Token notnull, Token
index) throws ActionException {
  +                
  +
  +        Column column = null;
  +        if (type.image.equals("varchar")) {
  +            column= new DefaultVarCharColumn(name.image, 
  +                                    new Integer(length.image).intValue());
  +        } else if (type.image.equals("integer")) {
  +            column= new DefaultIntegerColumn(name.image);
  +        } else if (type.image.equals("date")) {
  +            column= new DefaultDateColumn(name.image);
  +        } else if (type.image.equals("time")) {
  +            column= new DefaultTimeColumn(name.image);
  +        } else if (type.image.equals("timestamp")) {
  +            column= new DefaultTimestampColumn(name.image);
  +        } else if (type.image.equals("smallint")) {
  +            column= new DefaultSmallIntColumn(name.image);
  +        } else if (type.image.equals("real")) {
  +            column= new DefaultRealColumn(name.image);
  +        } else if (type.image.equals("bigint")) {
  +            column= new DefaultBigIntColumn(name.image);
  +        } else if (type.image.equals("decimal")) {
  +            column= new DefaultDecimalColumn(name.image);
  +        } else {
  +            throw new ActionException("Column type " + type.image + " not recognised");
  +        }
  +        //column.setIndex(ix);
  +        return column;
  +    }
  +    
   }
   
   PARSER_END(BasicSQLParser)
  @@ -121,6 +185,8 @@
       | <AND:         "and">
       | <OR:          "or">
       | <NOT:         "not">
  +    | <NULL:        "null">
  +    | <NOTNULL:    "not null">
   }
   
   TOKEN : { /* Operators */
  @@ -136,6 +202,7 @@
       <IDENTIFIER: <LETTER> ( <LETTER> | <DIGIT> )* >
       | <#LETTER: ["a"-"z", "A"-"Z", "_"] >
       | <#DIGIT: ["0"-"9"]>
  +    | <MAXVARCHAR: "(" (["0"-"9"])+ ")" >
   }
   
   TOKEN : {  /* Literals */
  @@ -161,21 +228,22 @@
   { }
   {
       ( 
  -        parseSQLString() 
  +        ParseSQLString() 
       )
   }
   
  -String parseSQLString() :
  +String ParseSQLString() :
   {
       String stm;
   }
   {
       ( 
  -        stm = Select()
  -        | stm = Update()
  -        | stm = Insert()
  -        | stm = Drop()
  -        | stm = Delete()
  +        stm = ParseSelect()
  +        | stm = ParseUpdate()
  +        | stm = ParseInsert()
  +        | stm = ParseDrop()
  +        | stm = ParseDelete()
  +        | stm = ParseCreateTable()
           //| Commit()
           //| Alter()
           //| Rollback()
  @@ -186,72 +254,98 @@
       { return stm; }
   }
   
  -String Select() :
  +String ParseSelect() :
   { 
       String stm;
  +    Vector columns;
  +    Vector tables;
   }
   
   {
       <SELECT>
           { stm = "SELECT"; }
  -        getColumns()
  +        columns = ParseColumns() 
  +             { 
  +                mColumns = new String[columns.size()];
  +                columns.toArray(mColumns);
  +                System.out.println("mColumns.length=" + mColumns.length);
  +             }
  +        
       <FROM>
  -        getTables()
  -    [ Where() ]
  +        tables = ParseTables()
  +            { 
  +                mTables = new String[tables.size()];
  +                columns.toArray(mTables);
  +                System.out.println("mTables.length=" + mTables.length);
  +             }
  +    [ ParseWhere() ]
   
       { return stm; }
   }
   
  -String Update() :
  +String ParseUpdate() :
   {
        String stm;
   }
   {
       <UPDATE>
           { stm = "UPDATE"; }
  -        getTables()
  +        ParseTables()
       <SET>
  -        ( <IDENTIFIER> "=" ExprValue()
  +        ( <IDENTIFIER> "=" ParseExprValue()
             [ "," { System.out.println(","); } ] )+
  -    [ Where() ]
  +    [ ParseWhere() ]
       
       { System.out.println("Row(s) updated ;-)"); }
    
       { return stm; }
   }
   
  -void Rollback() :
  +void ParseRollback() :
   {}
   {
       <EOF>   
   }
   
  -String Insert() :
  +String ParseInsert() :
   {
       String stm;
  +    Token name;
  +    Vector columns = new Vector();
  +    Vector values = new Vector();
   }
   {
       <INSERT><INTO> { stm = "INSERT"; }
  -    <IDENTIFIER>
  +    name=<IDENTIFIER> {
  +        mTableName = name.image;
  +    }
       [ ( "("
  -        getColumns()
  +        columns=ParseColumns() {
  +           mColumns = new String[columns.size()];
  +           columns.toArray(mColumns);
  +           System.out.println("mColumns.length=" + mColumns.length);
  +        }
         ")" )+ ]
   
       ( <VALUES>
           "("
  -            getValues()
  +            values = ParseValues() {
  +               mInsertValues = new String[values.size()];
  +               values.toArray(mInsertValues);
  +               System.out.println("mInsertValues.length=" + mColumns.length);
  +            }
           ")" )+
   
       { return stm; }
   }
   
  -void Grant() :
  +void ParseGrant() :
   {}
   {
       <EOF>   
   }
   
  -String Drop() :
  +String ParseDrop() :
   {
       String stm;
   }
  @@ -265,7 +359,7 @@
       { return stm; }
   }
   
  -String Delete() :
  +String ParseDelete() :
   {
       String stm;
   }
  @@ -273,52 +367,85 @@
       <DELETE><FROM> 
           { stm = "DELETE"; }
       <IDENTIFIER>
  -    [ Where() ]
  +    [ ParseWhere() ]
   
       { System.out.println("Row(s) deleted ;-)"); }
   
       { return stm; }
   }
   
  -void Commit() :
  +void ParseCommit() :
   {}
   {
       <EOF>   
   }
   
  -void Alter() :
  +void ParseAlter() :
   {}
   {
       <EOF>   
   }
   
  -void Where() :
  +String ParseCreateTable() : 
  +{
  +    String stm;
  +    Vector columns;
  +    Token name; 
  +}
  +{
  +    <CREATE><TABLE> {
  +        stm="CREATETABLE";
  +    }
  +    name=<IDENTIFIER> { mTableName = name.image; }
  +    ("("
  +        columns=ParseCreateTblValues() {
  +            mCols = new Column[columns.size()];
  +            columns.toArray(mCols);
  +        }
  +    ")")
  +
  +    { return stm; }
  +}
  +
  +void ParseWhere() :
   {} 
   {
       <WHERE>
  -        { OrExpr(); }
  +        { 
  +            System.out.println("Parse where");
  +            mCriteriaBuffer = new StringBuffer();
  +            mCriteriaBuffer.append("if ( ");
  +            ParseOrExpr();
  +            mCriteriaBuffer.append(" )");
  +            System.out.println("mCriteriaBuffer=" + mCriteriaBuffer); 
  +        }
   }
   
  -void OrExpr() :
  +void ParseOrExpr() :
   {}
   {
  -    AndExpr()
  -    ( <OR> { System.out.println("in or"); }
  -        AndExpr() )*
  +    ParseAndExpr()
  +    ( <OR> 
  +        { 
  +            mCriteriaBuffer.append(" || "); 
  +        }
  +        ParseAndExpr() )*
   }
   
   
  -void AndExpr() :
  +void ParseAndExpr() :
   {}
   
   {
  -    NotExpr()
  +    ParseNotExpr()
       ( <AND>
  -        { System.out.println("in and"); }
  -      NotExpr() )*
  +        { 
  +            mCriteriaBuffer.append(" && "); 
  +        }
  +      ParseNotExpr() )*
   }
   
  -void NotExpr() :
  +void ParseNotExpr() :
   {}
   
   {
  @@ -327,81 +454,217 @@
   }
   
   void CompareExpr() :
  -{}
  +{
  +    String rs_comp;
  +    String rs_right;
  +    String rs_left;
  +}
   
   {
  -    ExprValue() CompareOps() ExprValue()
  -    | Term()
  +    rs_left=ParseExprValue() rs_comp=CompareOps() rs_right=ParseRightExprValue()
  +    {
  +        mCriteriaBuffer.append(rs_comp + rs_left + "," +  rs_right + " )");
  +    }
  +    | ParseTerm()
   }
   
  -void ExprValue() :
  -{}
  +String ParseExprValue() :
  +{
  +    Token token;
  +    String rs = new String();
  +}
   
   {
  -    <IDENTIFIER> { System.out.println("expr is <INDENTIFIER>"); }
  -    | <STRING_LITERAL> { System.out.println("expr is <STRING_LITERAL>"); }
  -    | <INTEGER_LITERAL> { System.out.println("expr is <INTEGER_LITERAL>");
}
  -    | <FLOATING_POINT_LITERAL> { System.out.println("expr is <FLOATING_POINT_LITERAL>");
}
  +    (
  +        token = <IDENTIFIER> { rs = " columnCheckLeft.getColumn(row,\"" 
  +                                    + token.image + "\") "; }
  +        |  token = <STRING_LITERAL> { System.out.println("expr is <STRING_LITERAL>");
rs="bla"; }
  +        |  token = <INTEGER_LITERAL> { System.out.println("expr is <INTEGER_LITERAL>");
rs="bla"; }
  +        |  token = <FLOATING_POINT_LITERAL> 
  +           { 
  +                System.out.println("expr is <FLOATING_POINT_LITERAL>"); rs="bla";

  +           }
  +    )
  +    { return rs; }
   }
   
  -void UpdateValue() :
  -{}
  +String ParseRightExprValue() :
  +{
  +    Token token;
  +    String rs = new String();
  +}
   
   {
  -    <STRING_LITERAL> { System.out.println("value is <STRING_LITERAL>"); }
  -    | <INTEGER_LITERAL> { System.out.println("value is <INTEGER_LITERAL>");
}
  -    | <FLOATING_POINT_LITERAL> { System.out.println("value expr is <FLOATING_POINT_LITERAL>");
}
  +    (
  +        token = <IDENTIFIER> { System.out.println("expr is <INDENTIFIER>");
 }
  +        |  token = <STRING_LITERAL> { rs = " columnCheckRight.getColumn(row,\"" 
  +                                           + token.image + "\") "; }
  +        |  token = <INTEGER_LITERAL> { System.out.println("expr is <INTEGER_LITERAL>");
}
  +        |  token = <FLOATING_POINT_LITERAL> { System.out.println("expr is <FLOATING_POINT_LITERAL>");
}
  +    )
  +
  +    { return rs; }
   }
   
  -void CompareOps() :
  -{}
  +String ParseUpdateValue() :
  +{
  +    Token value;
  +    String stringval;
  +}
   
   {
  -    <EQUAL> { System.out.println("EQUAL"); }
  -    |  <GREATERTHAN> { System.out.println("GREATERTHAN"); }
  -    |  <SMALLERTHAN> { System.out.println("SMALLERTHAN"); }
  -    |  <GREATERTHANOREQUAL> { System.out.println("GREATERTHANOREQUAL"); }
  -    |  <SMALLERTHANOREQUAL> { System.out.println("SMALLERTHANOREQUAL"); }
  -    |  <LIKE> { System.out.println("LIKE"); }
  +    (
  +        value = <STRING_LITERAL> { stringval = value.image; }
  +        | value = <INTEGER_LITERAL> { stringval = value.image; }
  +        | value = <FLOATING_POINT_LITERAL> { stringval = value.image; }
  +    )
  +
  +    { return stringval; }
  +}
  +
  +String CompareOps() :
  +{
  +    String rs = new String();
  +}
  +
  +{
  +    (
  +        <EQUAL> { rs = " testHelper.equal( "; }
  +        |  <GREATERTHAN> { System.out.println("GREATERTHAN"); }
  +        |  <SMALLERTHAN> { System.out.println("SMALLERTHAN"); }
  +        |  <GREATERTHANOREQUAL> { System.out.println("GREATERTHANOREQUAL"); }
  +        |  <SMALLERTHANOREQUAL> { System.out.println("SMALLERTHANOREQUAL"); }
  +        |  <LIKE> { System.out.println("LIKE"); }
  +    )
  +
  +    { return rs; }
   }
   
  -void Term() :
  +void ParseTerm() :
   {}
   {
  -     ( "(" { System.out.println("("); }
  -       OrExpr()
  -       ")" { System.out.println(")"); } )
  +     ( "(" { mCriteriaBuffer.append("("); }
  +       ParseOrExpr()
  +       ")" { mCriteriaBuffer.append(")"); } )
   }
   
  -void getColumns() :
  +Vector ParseColumns() :
   {
       String col;
  +    Vector vector = new Vector();
       Token column;
   }
   {
       ( "*" { System.out.println("all columns"); } )
       |
  -    column = <IDENTIFIER> {System.out.println("column=" + column.image); }
  +    column = <IDENTIFIER> { vector.add(column.image); }
       ( "," { System.out.println(",");}
  -        column = <IDENTIFIER> {System.out.println("column=" + column.image); } )*
  +        column = <IDENTIFIER> { vector.add(column.image); } )*
  +
  +    { return vector; }
   }
   
  -void getTables() :
  +Vector ParseTables() :
   {
       String tab;
  +    Vector tables = new Vector();
       Token table;
   }
   {
  -    table = <IDENTIFIER> {System.out.println("table=" + table.image); }
  -    ( "," { System.out.println(",");}
  -        table = <IDENTIFIER> {System.out.println("table=" + table.image); } )*
  +    table = <IDENTIFIER> { tables.add(table.image); }
  +    ( ","
  +      table = <IDENTIFIER> { tables.add(table.image); } )*
  +
  +    { return tables; }
   }
   
  -void getValues() :
  -{ }
  +Vector ParseValues() :
  +{ 
  +    Vector values = new Vector();
  +    String value;
  +}
   {
  -    UpdateValue()
  +    value = ParseUpdateValue() { values.add(value); }
       ( "," { System.out.println(","); }
  -        UpdateValue() )+
  +        value = ParseUpdateValue() { values.add(value); } )+
  +
  +    { return values; }
   }
   
  +Vector ParseCreateTblValues() : 
  +{
  +    Vector columns = new Vector();
  +    Column column;
  +}
  +{
  +    column=ParseCreateColumn() {
  +        columns.add(column);
  +    }
  +    ( "," 
  +        column=ParseCreateColumn() {
  +            columns.add(column);
  +        }
  +    )+
  +
  +   { return columns; }
  +   
  +}
  +
  +Column ParseCreateColumn() :
  +{
  +    Token ident;
  +    Token type;
  +    Token length;
  +    Token notnull = null;
  +    Column column = null;
  +    int iLength;
  +    boolean bNotnull;
  +}
  +{
  +    (
  +        LOOKAHEAD(2) ident=<IDENTIFIER> type=<VARCHAR> length=<MAXVARCHAR>
[notnull=<NOTNULL>]
  +        {
  +            try {
  +                column = createColumn(ident,type,length,notnull,null);   
  +            } catch (ActionException ae) {}
  +        }
  +        | LOOKAHEAD(2) ident=<IDENTIFIER> type=<TIMESTAMP> [notnull=<NOTNULL>]
  +         {
  +            try {
  +                column = createColumn(ident,type,null,notnull,null);   
  +            } catch (ActionException ae) {}
  +        }
  +        | LOOKAHEAD(2) ident=<IDENTIFIER> type=<SMALLINT> [notnull=<NOTNULL>]
  +         {
  +            try {
  +                column = createColumn(ident,type,null,notnull,null);   
  +            } catch (ActionException ae) {}
  +        }
  +        | LOOKAHEAD(2) ident=<IDENTIFIER> type=<REAL> [notnull=<NOTNULL>]
  +         {
  +            try {
  +                column = createColumn(ident,type,null,notnull,null);   
  +            } catch (ActionException ae) {}
  +        }
  +        | LOOKAHEAD(2) ident=<IDENTIFIER> type=<INTEGER> [notnull=<NOTNULL>]
  +         {
  +            try {
  +                column = createColumn(ident,type,null,notnull,null);   
  +            } catch (ActionException ae) {}
  +        }
  +        | LOOKAHEAD(2) ident=<IDENTIFIER> type=<DECIMAL> [notnull=<NOTNULL>]
  +         {
  +            try {
  +                column = createColumn(ident,type,null,notnull,null);   
  +            } catch (ActionException ae) {}
  +        }
  +        | ident=<IDENTIFIER> type=<DATE> [notnull=<NOTNULL>]
  +         {
  +            try {
  +                column = createColumn(ident,type,null,notnull,null);   
  +            } catch (ActionException ae) {}
  +        }
  +    )
  +
  +    { return column; }
  +}
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message