Example #1
0
  private QueryResults execute(AccessMode accessMode)
      throws QueryException, PersistenceException, TransactionNotInProgressException {
    org.exolab.castor.persist.QueryResults results;
    PersistenceQuery query;
    SQLEngine engine;
    QueryResults retVal = null;
    ;

    if (_expr == null && _spCall == null)
      throw new IllegalStateException("Must create query before using it");
    try {
      switch (_projectionType) {
        case ParseTreeWalker.PARENT_OBJECT:
        case ParseTreeWalker.DEPENDANT_OBJECT:
        case ParseTreeWalker.DEPENDANT_OBJECT_VALUE:

          // System.out.println("Executing object query");

          engine = (SQLEngine) _dbEngine.getPersistence(_objClass);
          if (_expr != null) {
            query = engine.createQuery(_expr, _bindTypes);
          } else {
            query = engine.createCall(_spCall, _bindTypes);
            if (query == null) throw new QueryException(Messages.message("query.callNotSupported"));
          }
          if (_bindValues != null) {
            for (int i = 0; i < _bindValues.length; ++i) query.setParameter(i, _bindValues[i]);
          }
          results = _dbImpl.getTransaction().query(_dbEngine, query, accessMode);
          _fieldNum = 0;

          // System.out.println( _projectionType );

          if (_projectionType == ParseTreeWalker.PARENT_OBJECT)
            retVal = new OQLEnumeration(results);
          else retVal = new OQLEnumeration(results, _pathInfo, _clsDesc);
          break;
        case ParseTreeWalker.DEPENDANT_VALUE:
        case ParseTreeWalker.AGGREGATE:
        case ParseTreeWalker.FUNCTION:

          // System.out.println("Executing simple query");

          SimpleQueryExecutor sqe = new SimpleQueryExecutor(_dbImpl);
          retVal = sqe.execute(_expr, _bindValues);
      }
    } catch (PersistenceException except) {
      throw except;
    }
    return (retVal);
  }
Example #2
0
  /** The simple parser for CALL-type queries (using stored procedured) */
  public void createCall(String oql) throws QueryException {
    StringBuffer sql;
    int as;
    int leftParen;
    int rightParen;
    int paramCnt;
    String objType;
    ParamInfo info;
    StringBuffer sb;
    Integer paramNo;

    if (!oql.startsWith("CALL ")) {
      throw new QueryException("Stored procedure call must start with CALL");
    }
    as = oql.indexOf(" AS ");
    if (as < 0) {
      throw new QueryException("Stored procedure call must end with \"AS <class-name>\"");
    }
    leftParen = oql.indexOf("(");
    rightParen = oql.indexOf(")");
    sql = new StringBuffer();
    paramCnt = 0;
    _paramInfo = new Hashtable();
    if (leftParen < 0 && rightParen < 0) {
      sql.append(oql.substring(5, as));
    } else {
      if ((leftParen < 0 && rightParen >= 0) || (leftParen > rightParen)) {
        throw new QueryException("Syntax error: parenthesis");
      }
      sql.append(oql.substring(5, leftParen));
      sql.append('(');
      for (int i = leftParen + 1; i < rightParen; i++) {
        if (oql.charAt(i) == '$') {
          // get parameter number if given
          sb = new StringBuffer();
          for (int j = i + 1; j < rightParen; j++) {
            char c;

            c = oql.charAt(j);
            if (c < '0' || c > '9') {
              break;
            }
            sb.append(c);
          }
          if (sb.length() > 0) {
            paramNo = Integer.valueOf(sb.toString());
          } else {
            paramNo = new Integer(paramCnt + 1);
          }
          info = (ParamInfo) _paramInfo.get(paramNo);
          if (info == null) {
            info = new ParamInfo("", "java.lang.Object");
          }
          info.mapToSQLParam(paramCnt + 1);
          _paramInfo.put(paramNo, info);
          paramCnt++;
        }
      }
      for (int i = 0; i < paramCnt; i++) {
        sql.append('?');
        if (i < paramCnt - 1) sql.append(',');
      }
      sql.append(')');
    }
    _spCall = sql.toString();
    _projectionType = ParseTreeWalker.PARENT_OBJECT;
    _bindTypes = new Class[paramCnt];
    for (int i = 0; i < paramCnt; i++) _bindTypes[i] = Object.class;

    objType = oql.substring(as + 4).trim();
    if (objType.length() == 0) {
      throw new QueryException("Missing object name");
    }
    try {
      if (_dbImpl.getClassLoader() == null) _objClass = Class.forName(objType);
      else _objClass = _dbImpl.getClassLoader().loadClass(objType);
    } catch (ClassNotFoundException except) {
      throw new QueryException("Could not find class " + objType);
    }
    _dbEngine = _dbImpl.getPersistenceEngine();
    if (_dbEngine == null || _dbEngine.getPersistence(_objClass) == null)
      throw new QueryException("Could not find an engine supporting class " + objType);
  }