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 Mon, 17 Dec 2001 16:02:22 GMT
froehlich    01/12/17 08:02:22

  Modified:    apps/db/src/sql/javacc BasicSQLParser.jj
  Log:
  create, insert, select + criteria is working 90%
  
  Revision  Changes    Path
  1.7       +97 -67    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- BasicSQLParser.jj	2001/12/15 18:48:09	1.6
  +++ BasicSQLParser.jj	2001/12/17 16:02:21	1.7
  @@ -32,6 +32,7 @@
   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.utils.StringUtils;
   
   import java.util.Vector;
   import java.util.Collection;
  @@ -52,6 +53,7 @@
       private StringBuffer mCriteriaBuffer;
       private Column[] mCols;
       private String mTableName;
  +    private int mIdx;
       
       public static void main(String args[]) {
           BasicSQLParser parser = new BasicSQLParser(System.in);
  @@ -71,7 +73,11 @@
       }
   
       public String getCriteria() {
  -        return mCriteriaBuffer.toString();
  +        if(mCriteriaBuffer == null) {
  +            return "";
  +        } else {
  +            return mCriteriaBuffer.toString();
  +        }
       }
   
       public Column[] getCols() {
  @@ -86,33 +92,39 @@
           return mInsertValues;
       }
   
  -    private Column createColumn(Token name, Token type, Token length, Token notnull, Token
index) throws ActionException {
  +    private Column createColumn(Token name,
  +                                Token type,
  +                                Token length,
  +                                Token notnull,
  +                                int idx) throws ActionException {
                   
   
           Column column = null;
  -        if (type.image.equals("varchar")) {
  +        String sType = StringUtils.normalize(type.image);
  +        
  +        if (sType.equals("varchar")) {
               column= new DefaultVarCharColumn(name.image, 
  -                                    new Integer(length.image).intValue());
  -        } else if (type.image.equals("integer")) {
  +                                    new Integer(length.image.substring(1,length.image.length()-1)).intValue());
  +        } else if (sType.equals("integer")) {
               column= new DefaultIntegerColumn(name.image);
  -        } else if (type.image.equals("date")) {
  +        } else if (sType.equals("date")) {
               column= new DefaultDateColumn(name.image);
  -        } else if (type.image.equals("time")) {
  +        } else if (sType.equals("time")) {
               column= new DefaultTimeColumn(name.image);
  -        } else if (type.image.equals("timestamp")) {
  +        } else if (sType.equals("timestamp")) {
               column= new DefaultTimestampColumn(name.image);
  -        } else if (type.image.equals("smallint")) {
  +        } else if (sType.equals("smallint")) {
               column= new DefaultSmallIntColumn(name.image);
  -        } else if (type.image.equals("real")) {
  +        } else if (sType.equals("real")) {
               column= new DefaultRealColumn(name.image);
  -        } else if (type.image.equals("bigint")) {
  +        } else if (sType.equals("bigint")) {
               column= new DefaultBigIntColumn(name.image);
  -        } else if (type.image.equals("decimal")) {
  +        } else if (sType.equals("decimal")) {
               column= new DefaultDecimalColumn(name.image);
           } else {
  -            throw new ActionException("Column type " + type.image + " not recognised");
  +            throw new ActionException("Column type " + sType + " not recognised");
           }
  -        //column.setIndex(ix);
  +        column.setIndex(idx);
           return column;
       }
       
  @@ -146,6 +158,7 @@
       | <SMALLINT:    "smallint">
       | <REAL:        "real">
       | <INTEGER:     "integer">
  +    | <INT:         "int">
       | <DECIMAL:     "decimal">
       | <DATE:        "date">
       | <BIGINT:      "bigint">
  @@ -196,6 +209,8 @@
       | <GREATERTHANOREQUAL:  ">=" >
       | <SMALLERTHANOREQUAL:  "<=" >
       | <LIKE:                "like" >
  +    | <NOTEQUAL1:             "!=" >
  +    | <NOTEQUAL2:             "<>" >
   }
   
   TOKEN : { /* Indentifiers */
  @@ -242,8 +257,8 @@
           | stm = ParseUpdate()
           | stm = ParseInsert()
           | stm = ParseDrop()
  -        | stm = ParseDelete()
           | stm = ParseCreateTable()
  +        | stm = ParseDelete()
           //| Commit()
           //| Alter()
           //| Rollback()
  @@ -257,8 +272,8 @@
   String ParseSelect() :
   { 
       String stm;
  -    Vector columns;
  -    Vector tables;
  +    Vector columns = new Vector();
  +    Vector tables = new Vector();
   }
   
   {
  @@ -268,15 +283,13 @@
                { 
                   mColumns = new String[columns.size()];
                   columns.toArray(mColumns);
  -                System.out.println("mColumns.length=" + mColumns.length);
                }
           
       <FROM>
           tables = ParseTables()
               { 
                   mTables = new String[tables.size()];
  -                columns.toArray(mTables);
  -                System.out.println("mTables.length=" + mTables.length);
  +                tables.toArray(mTables);
                }
       [ ParseWhere() ]
   
  @@ -293,11 +306,9 @@
           ParseTables()
       <SET>
           ( <IDENTIFIER> "=" ParseExprValue()
  -          [ "," { System.out.println(","); } ] )+
  +          [ "," ] )+
       [ ParseWhere() ]
  -    
  -    { System.out.println("Row(s) updated ;-)"); }
  - 
  +   
       { return stm; }
   }
   
  @@ -323,7 +334,6 @@
           columns=ParseColumns() {
              mColumns = new String[columns.size()];
              columns.toArray(mColumns);
  -           System.out.println("mColumns.length=" + mColumns.length);
           }
         ")" )+ ]
   
  @@ -332,7 +342,6 @@
               values = ParseValues() {
                  mInsertValues = new String[values.size()];
                  values.toArray(mInsertValues);
  -               System.out.println("mInsertValues.length=" + mColumns.length);
               }
           ")" )+
   
  @@ -354,8 +363,6 @@
           { stm = "DROP"; }
       <IDENTIFIER>
       
  -    { System.out.println("Table droped ;-)"); }
  -    
       { return stm; }
   }
   
  @@ -369,8 +376,6 @@
       <IDENTIFIER>
       [ ParseWhere() ]
   
  -    { System.out.println("Row(s) deleted ;-)"); }
  -
       { return stm; }
   }
   
  @@ -389,7 +394,7 @@
   String ParseCreateTable() : 
   {
       String stm;
  -    Vector columns;
  +    Vector columns = new Vector();
       Token name; 
   }
   {
  @@ -403,7 +408,6 @@
               columns.toArray(mCols);
           }
       ")")
  -
       { return stm; }
   }
   
  @@ -412,12 +416,8 @@
   {
       <WHERE>
           { 
  -            System.out.println("Parse where");
               mCriteriaBuffer = new StringBuffer();
  -            mCriteriaBuffer.append("if ( ");
               ParseOrExpr();
  -            mCriteriaBuffer.append(" )");
  -            System.out.println("mCriteriaBuffer=" + mCriteriaBuffer); 
           }
   }
   
  @@ -449,7 +449,7 @@
   {}
   
   {
  -    [ <NOT> { System.out.println("in not"); } ] 
  +    [ <NOT> ]
       CompareExpr() 
   }
   
  @@ -476,14 +476,22 @@
   
   {
       (
  -        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";

  +        token = <IDENTIFIER> {
  +            rs = " columnCheckLeft.getColumn(row,\"" 
  +                   + token.image + "\") "; 
  +        }
  +        |  token = <STRING_LITERAL> {
  +               rs = " columnCheckLeft.getColumn(row,\"" 
  +                   + token.image + "\") "; 
  +           }
  +        |  token = <INTEGER_LITERAL> {
  +           rs = " columnCheckLeft.getColumn(row,\"" 
  +                  + token.image + "\") "; 
              }
  +        |  token = <FLOATING_POINT_LITERAL> { 
  +               rs = " columnCheckLeft.getColumn(row,\"" 
  +                      + token.image + "\") ";
  +           }
       )
       { return rs; }
   }
  @@ -496,11 +504,22 @@
   
   {
       (
  -        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>");
}
  +        token = <IDENTIFIER> {
  +            rs = " columnCheckRight.getColumn(\"" 
  +                 + token.image + "\"),row ";   
  +        }
  +        |  token = <STRING_LITERAL> {
  +               rs = " columnCheckRight.getColumn(\"" 
  +                    + token.image + "\"),row "; 
  +           }
  +        |  token = <INTEGER_LITERAL> {
  +               rs = " columnCheckRight.getColumn(\"" 
  +                    + token.image + "\"),row ";  
  +           }
  +        |  token = <FLOATING_POINT_LITERAL> {
  +               rs = " columnCheckRight.getColumn(\"" 
  +                    + token.image + "\"),row ";  
  +           }
       )
   
       { return rs; }
  @@ -529,11 +548,13 @@
   
   {
       (
  -        <EQUAL> { rs = " testHelper.equal( "; }
  -        |  <GREATERTHAN> { System.out.println("GREATERTHAN"); }
  -        |  <SMALLERTHAN> { System.out.println("SMALLERTHAN"); }
  -        |  <GREATERTHANOREQUAL> { System.out.println("GREATERTHANOREQUAL"); }
  -        |  <SMALLERTHANOREQUAL> { System.out.println("SMALLERTHANOREQUAL"); }
  +        <EQUAL> { rs = " testHelper.testEqual( "; }
  +        |  <GREATERTHAN> {  rs = " testHelper.testGreaterThan( "; }
  +        |  <SMALLERTHAN> {  rs = " testHelper.testSmallerThan( "; }
  +        |  <GREATERTHANOREQUAL> {  rs = " testHelper.testGreaterThanOrEqual( "; }
  +        |  <SMALLERTHANOREQUAL> {  rs = " testHelper.testSmallerThanOrEqual( "; }
  +        |  <NOTEQUAL1>  {  rs = " testHelper.testNotEqual( "; }
  +        |  <NOTEQUAL2>  {  rs = " testHelper.testNotEqual( "; }
           |  <LIKE> { System.out.println("LIKE"); }
       )
   
  @@ -555,10 +576,10 @@
       Token column;
   }
   {
  -    ( "*" { System.out.println("all columns"); } )
  +    ( "*" { vector.add(new String("*"));; } )
       |
       column = <IDENTIFIER> { vector.add(column.image); }
  -    ( "," { System.out.println(",");}
  +    ( ","
           column = <IDENTIFIER> { vector.add(column.image); } )*
   
       { return vector; }
  @@ -571,9 +592,10 @@
       Token table;
   }
   {
  -    table = <IDENTIFIER> { tables.add(table.image); }
  +    table = <IDENTIFIER> { tables.add(new String(table.image)); }
  +        
       ( ","
  -      table = <IDENTIFIER> { tables.add(table.image); } )*
  +      table = <IDENTIFIER> { tables.add(new String(table.image)); } )*
   
       { return tables; }
   }
  @@ -585,7 +607,7 @@
   }
   {
       value = ParseUpdateValue() { values.add(value); }
  -    ( "," { System.out.println(","); }
  +    ( ","
           value = ParseUpdateValue() { values.add(value); } )+
   
       { return values; }
  @@ -597,6 +619,8 @@
       Column column;
   }
   {
  +
  +    { mIdx = 0; }
       column=ParseCreateColumn() {
           columns.add(column);
       }
  @@ -605,7 +629,7 @@
               columns.add(column);
           }
       )+
  -
  +    { mIdx = 0; }
      { return columns; }
      
   }
  @@ -625,43 +649,49 @@
           LOOKAHEAD(2) ident=<IDENTIFIER> type=<VARCHAR> length=<MAXVARCHAR>
[notnull=<NOTNULL>]
           {
               try {
  -                column = createColumn(ident,type,length,notnull,null);   
  +                column = createColumn(ident,type,length,notnull,mIdx++);   
               } catch (ActionException ae) {}
           }
           | LOOKAHEAD(2) ident=<IDENTIFIER> type=<TIMESTAMP> [notnull=<NOTNULL>]
            {
               try {
  -                column = createColumn(ident,type,null,notnull,null);   
  +                column = createColumn(ident,type,null,notnull,mIdx++);   
               } catch (ActionException ae) {}
           }
           | LOOKAHEAD(2) ident=<IDENTIFIER> type=<SMALLINT> [notnull=<NOTNULL>]
            {
               try {
  -                column = createColumn(ident,type,null,notnull,null);   
  +                column = createColumn(ident,type,null,notnull,mIdx++);   
               } catch (ActionException ae) {}
           }
           | LOOKAHEAD(2) ident=<IDENTIFIER> type=<REAL> [notnull=<NOTNULL>]
            {
               try {
  -                column = createColumn(ident,type,null,notnull,null);   
  +                column = createColumn(ident,type,null,notnull,mIdx++);   
               } catch (ActionException ae) {}
           }
           | LOOKAHEAD(2) ident=<IDENTIFIER> type=<INTEGER> [notnull=<NOTNULL>]
            {
  +            try {
  +                column = createColumn(ident,type,null,notnull,mIdx++);   
  +            } catch (ActionException ae) {}
  +        }
  +        | LOOKAHEAD(2) ident=<IDENTIFIER> type=<INT> [notnull=<NOTNULL>]
  +         {
               try {
  -                column = createColumn(ident,type,null,notnull,null);   
  +                column = createColumn(ident,type,null,notnull,mIdx++);   
               } catch (ActionException ae) {}
           }
           | LOOKAHEAD(2) ident=<IDENTIFIER> type=<DECIMAL> [notnull=<NOTNULL>]
            {
               try {
  -                column = createColumn(ident,type,null,notnull,null);   
  +                column = createColumn(ident,type,null,notnull,mIdx++);   
               } catch (ActionException ae) {}
           }
           | ident=<IDENTIFIER> type=<DATE> [notnull=<NOTNULL>]
            {
               try {
  -                column = createColumn(ident,type,null,notnull,null);   
  +                column = createColumn(ident,type,null,notnull,mIdx++);   
               } catch (ActionException ae) {}
           }
       )
  
  
  

--
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