You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by ha...@apache.org on 2001/11/01 10:22:01 UTC

cvs commit: jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/parser BaseBCELParser.java

hammant     01/11/01 01:22:01

  Added:       apps/db/src/java/org/apache/avalon/db/bcel/parser
                        BaseBCELParser.java
  Log:
  Split of BCEL parser into parent class and SQL impl
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/parser/BaseBCELParser.java
  
  Index: BaseBCELParser.java
  ===================================================================
  
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.avalon.db.bcel.parser;
  
  
  
  import org.apache.avalon.db.services.SQLParser;
  import org.apache.avalon.db.services.DatabasePersistor;
  import org.apache.avalon.db.actions.Action;
  import org.apache.avalon.db.actions.Select;
  import org.apache.avalon.db.actions.CreateTable;
  import org.apache.avalon.db.actions.Insert;
  import org.apache.avalon.db.transport.Request;
  import org.apache.avalon.db.transport.SelectRequest;
  import org.apache.avalon.db.transport.CreateTableRequest;
  import org.apache.avalon.db.transport.InsertRequest;
  import org.apache.avalon.db.bcel.actions.BCELCreateTable;
  import org.apache.avalon.db.bcel.actions.BCELInsertSingleValue;
  import org.apache.avalon.db.bcel.data.BCELTable;
  import org.apache.avalon.db.data.Table;
  import org.apache.avalon.db.data.Column;
  import org.apache.avalon.db.data.impl.DefaultColumn;
  import org.apache.avalon.phoenix.Block;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.logger.AbstractLoggable;
  import org.apache.bcel.Constants;
  import org.apache.bcel.generic.ConstantPoolGen;
  import org.apache.bcel.generic.Type;
  import org.apache.bcel.generic.ClassGen;
  import org.apache.bcel.generic.FieldGen;
  import org.apache.bcel.generic.InstructionList;
  import org.apache.bcel.generic.MethodGen;
  
  import org.apache.bcel.classfile.Field;
  
  import java.util.StringTokenizer;
  import java.util.Vector;
  import java.util.HashMap;
  
  
  /**
   * Class BCELSQLParser
   *
   *
   * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
   * @version $Revision: 1.1 $
   */
  public abstract class BaseBCELParser extends AbstractLoggable
          implements Block, SQLParser, Contextualizable, Composable, Configurable, Initializable {
  
      protected DatabasePersistor mDatabasePersistor;
      protected GeneratedObjectClassLoader mGenClassLoader = new GeneratedObjectClassLoader();
      protected static int mGenObjSeq;
      protected HashMap mGeneratedObjTranslation = new HashMap();
  
  
      /**
       * Method createAction
       *
       *
       * @param request
       *
       * @return
       *
       */
      public Action createAction(Request request) {
          return null;
      }
  
      /**
       * Method contextualize
       *
       *
       * @param context
       *
       * @throws ContextException
       *
       */
      public void contextualize(Context context) throws ContextException {}
  
      /**
       * Method compose
       *
       *
       * @param componentManager
       *
       * @throws ComponentException
       *
       */
      public void compose(ComponentManager componentManager) throws ComponentException {
          mDatabasePersistor =
              (DatabasePersistor) componentManager.lookup(DatabasePersistor.class.getName());
      }
  
      /**
       * Method configure
       *
       *
       * @param configuration
       *
       * @throws ConfigurationException
       *
       */
      public void configure(Configuration configuration) throws ConfigurationException {}
  
      /**
       * Method initialize
       *
       *
       * @throws Exception
       *
       */
      public void initialize() throws Exception {}
  
      /**
       * Method createSelectAction
       *
       *
       * @param request
       *
       * @return
       *
       */
      public Select createSelectAction(SelectRequest request) {
          return null;
      }
  
      protected String getWordOne(String str) {
  
          int ix0 = str.indexOf(' ');
  
          if (ix0 == -1) {
              ix0 = str.length();
          }
  
          return str.substring(0, ix0);
      }
  
      protected String getWordTwo(String str) {
  
          int ix0 = str.indexOf(' ');
          int ix1 = str.indexOf(' ', ix0 + 1);
  
          if (ix1 == -1) {
              ix1 = str.length();
          }
  
          return str.substring(ix0 + 1, ix1);
      }
  
      protected String getWordThree(String str) {
  
          int ix0 = str.indexOf(' ');
          int ix1 = str.indexOf(' ', ix0 + 1);
          int ix2 = str.indexOf(' ', ix1 + 1);
  
          if (ix2 == -1) {
              ix2 = str.length();
          }
  
          return str.substring(ix1 + 1, ix2);
      }
  
      protected String getWordFour(String str) {
  
          int ix0 = str.indexOf(' ');
          int ix1 = str.indexOf(' ', ix0 + 1);
          int ix2 = str.indexOf(' ', ix1 + 1);
          int ix3 = str.indexOf(' ', ix2 + 1);
  
          if (ix3 == -1) {
              ix3 = str.length();
          }
  
          return str.substring(ix2 + 1, ix3);
      }
  
      protected String getBracketedExpr(String str) {
  
          int ix0 = str.indexOf('(');
          int ix1 = str.lastIndexOf(')');
  
          System.out.println("str=" + str);
          System.out.println("ix0=" + ix0);
          System.out.println("ix1=" + ix1);
  
          return str.substring(ix0 + 1, ix1);
      }
  
  
      protected Type getType(String type) {
  
          System.out.println("Type=(" + type + ")");
  
          if (type.equals("varchar")) {
              return Type.STRING;
          } else if (type.equals("char")) {
              return Type.STRING;
          }
  
          return Type.VOID;
      }
  
      protected void createField(ConstantPoolGen cp, ClassGen cg, Type fieldType, String name) {
  
          System.out.println("fn=" + name);
          System.out.println("Type2=" + fieldType);
  
          FieldGen fg = new FieldGen(Constants.ACC_PUBLIC, fieldType, name, cp);
  
          cg.addField(fg.getField());
      }
  
      protected void createSetter(ConstantPoolGen cp, ClassGen cg, Type fieldType, String name,
                                String table) {
  
          InstructionList il = new InstructionList();
          MethodGen mg = new MethodGen(Constants.ACC_PUBLIC,    // access flags
                                       Type.VOID,               // return type
                                       new Type[]{ fieldType }, new String[]{ "value" },    // arg names
                                       "set" + name, table,     // method, class
                                       il, cp);
  
          mg.stripAttributes(true);
          mg.setMaxStack();
          mg.setMaxLocals();
          cg.addMethod(mg.getMethod());
      }
  
      //private Insert createNamedColumnsInsert(Table table, String sql ) {
      //}
      protected Insert createAllValuesInsert(Table table, String sql) {
  
          String values = getBracketedExpr(sql);
          StringTokenizer st = new StringTokenizer(values, ",");
          Vector valuesVec = new Vector();
  
          while (st.hasMoreTokens()) {
              valuesVec.add(st.nextToken().trim());
          }
  
          //TODO get columns from table.
          return new BCELInsertSingleValue(table, null, valuesVec);
      }
  
      //private Insert createSelectInsert(Table table, String sql) {
      //}
  
      public void addGeneratedClass(String name, byte[] bytes) {
          mGenClassLoader.addGeneratedClass(name, bytes);
      }
  
      public Object getGeneratedInstance(String name) {
          return mGenClassLoader.getGeneratedInstance(name);
      }
  
      public Class getGeneratedClass(String name) {
          return mGenClassLoader.findClass(name);
      }
  
      public String createUniqueClassName(String sqlObjectName){
          String className = "gen.A" + mGenObjSeq++;
          mGeneratedObjTranslation.put(sqlObjectName, className);
          return className;
      }
  
      public String getGeneratedClassName(String sqlObjectName){
          return (String) mGeneratedObjTranslation.get(sqlObjectName);
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>