Package com.draagon.meta.manager.exp

Examples of com.draagon.meta.manager.exp.Expression


          results.add( o2 );
        }


        // Handle Expressions, Sorting, Ranging, and Options
        Expression exp = options.getExpression();
        SortOrder sort = options.getSortOrder();
        Range range = options.getRange();

        if ( exp != null )
            results = filterObjects( results, exp );
View Full Code Here


  protected final static int ACTION_AND = 1;
  protected final static int ACTION_OR  = 2;

  protected ExpResult parse( MetaClass mc, String str, int n, int scope ) throws ExpressionParseError
  {
    Expression a = null;

    int x = n;

    int action = 0;

    while( x < str.length() )
    {
      int c = str.charAt( x );

      if ( c == '(' ) {
        ExpResult r = parse( mc, str, x + 1, scope + 1 );
        Expression b = r.getExp();

        if ( a == null ) a = b;
        else if ( action == ACTION_AND ) a = a.and( b );
        else if ( action == ACTION_OR ) a = a.or( b );
        else throw new ExpressionParseError( "error.parse.unexpected", x, r.getEnd() );
        action = 0;

        x = r.getEnd() + 1;
        n = x;
      }
      else if ( c == ')' ) {
        if ( a == null ) throw new ExpressionParseError( "error.parse.empty", n, x );
        return new ExpResult( a.group(), x );
      }
      else if ( c == ' ' ) {
        x++;
      }
      else if ( c == 'a' && isAnd( str, x )){
        action = ACTION_AND;
        x += 3;
        if ( a == null ) throw new ExpressionParseError( "error.parse.unexpected", x, x+3 );
        n = x;
      }
      else if ( c == 'o' && isOr( str, x )){
        action = ACTION_OR;
        x += 2;
        if ( a == null ) throw new ExpressionParseError( "error.parse.unexpected", x, x+2 );
        n = x;
      }
      else {
        if ( a != null && action == 0 )
          throw new ExpressionParseError( "error.parse.noexp", n, x );

        ExpResult r = parseExpression( mc, str, x );
        Expression b = r.getExp();

        if ( a == null ) a = b;
        else if ( action == ACTION_AND a = a.and( b );
        else if ( action == ACTION_OR ) a = a.or( b );
        else throw new ExpressionParseError( "error.parse.unexpected", x, r.getEnd() );
View Full Code Here

      catch( Exception e ) {
        throw new ExpressionParseError( "error.parse.badvalue", n, x, name );
      }
    }

    Expression exp = null;

    if ( type == null ) throw new ExpressionParseError( "error.parse.notype", n, x );
    else if ( type.equals( ">" )) exp = new Expression( field, value, Expression.GREATER );
    else if ( type.equals( "<" )) exp = new Expression( field, value, Expression.LESSER );
    else if ( type.equals( "=" )) exp = new Expression( field, value, Expression.EQUAL );
    else if ( type.equals( ">=" )) exp = new Expression( field, value, Expression.EQUAL_GREATER );
    else if ( type.equals( "<=" )) exp = new Expression( field, value, Expression.EQUAL_LESSER );
    else if ( type.equals( "!=" ) || type.equals( "<>" )) exp = new Expression( field, value, Expression.NOT_EQUAL );
    else throw new ExpressionParseError( "error.parse.typeunknown", n, x, type );
    // else if ( type.equals( "!!" )) exp = new Expression( field, value, Expression.CONTAIN );

    return new ExpResult( exp, x );
  }
View Full Code Here

    mc.addMetaField( a );
    mc.addMetaField( b );
    mc.addMetaField( c );
    mc.addMetaField( d );

    Expression exp = ExpressionParser.getInstance().parse( mc, "( time > = '10/12/2006 14:55' and ( ( id = 5 or name = 'test me!' ) and value <> 20))" );
    System.out.println( "EXPRESSION: " + exp );
  }
View Full Code Here

  @Override
  public boolean update( Connection c, MetaClass mc, ObjectMappingDB omdb,
      Object o, Collection<MetaField> fields, Collection<MetaField> keys,
      MetaField dirtyField, Object dirtyValue ) throws SQLException {
   
    Expression exp = null;
   
    // Check if there is table inheritence going on, and if so delete the super table first
    BaseDef base = omdb.getDBDef();
    if ( base instanceof TableDef ) {
      TableDef table = (TableDef) base;
     
      InheritenceDef inheritence = table.getInheritence();
      if ( inheritence != null ) {

        ObjectMappingDB smom = (ObjectMappingDB) omdb.getSuperMapping();

        // Setup the key
        MetaField rf = omdb.getField( inheritence.getRefColumn() );
        Collection<MetaField> pkeys = new ArrayList<MetaField>();
        pkeys.add( rf );
       
        // Create the super classes table entry
        if ( !update( c, mc, smom, o, fields, pkeys, dirtyField, dirtyValue )) {
          return false;
          // throw new SQLException( "Super table entry could not be updated for mapping [" + smom + "]" );
        }
       
        // Set the mapping field
        MetaField f = omdb.getField( inheritence.getColumnName() );
       
        // Set the expression to delete
        exp = new Expression( f.getName(), rf.getObject( o ));
       
        // Can only have dirty fields on the highest level of inheritence
        dirtyField = null;
        dirtyValue = null;
      }
    }
   
    // If there wasn't inheritence, then generate the delete where clause
    if ( exp == null ) {     
      // Generate the keys expression
      for( MetaField mf : keys ) {
        //if ( omdb.isInThisMap( mf )) {
          Expression e = new Expression( mf.getName(), mf.getObject( o ));
          if ( exp == null ) exp = e;
          else exp = exp.and( e );
        //}
      }
    }
       
    // Add the dirty field expression
    if ( dirtyField != null ) {
     
      Expression e = new Expression( dirtyField.getName(), dirtyValue );
      if ( exp == null ) exp = e;
      else exp = exp.and( e );
    }
   
    //setAutoFields( c, mc, omdb, fields, o, ObjectManager.UPDATE );
View Full Code Here

        // Set the mapping field
        MetaField f = omdb.getField( inheritence.getColumnName() );
       
        // Set the expression to delete
        Expression exp = new Expression( f.getName(), rf.getObject( o ));

        // Delete the higher level table first
        boolean result = executeDelete( c, mc, omdb, exp );
       
        // Return a false if it was not deleteable
        if ( result == false ) return false;
       
        // Delete the super classes table entry
        if ( !delete( c, mc, smom, o, pkeys )) {
          throw new SQLException( "Super table entry could not be deleted for mapping [" + smom + "]" );
        }
       
        return result;
      }
    }
   
    // If there wasn't inheritence, then generate the delete where clause
   
    // Generate the keys expression
    Expression exp = null;
    for( MetaField mf : keys ) {
      Expression e = new Expression( mf.getName(), mf.getObject( o ));
      if ( exp == null ) exp = e;
      else exp = exp.and( e );
    }
   
    //setAutoFields( c, mc, omdb, fields, o, ObjectManager.UPDATE );
View Full Code Here

   */
  protected PreparedStatement getSelectStatementWhere( Connection c, MetaClass mc,
      ObjectMappingDB omdb, Collection<MetaField> fields, QueryOptions options )
      throws SQLException, MetaException {
   
    Expression where = options.getExpression();
    SortOrder order = options.getSortOrder();

    // Construct the SELECT query
    StringBuilder query = new StringBuilder();
    query.append("SELECT ");
View Full Code Here

    }

    try {
     
      // Create the Expression for the Primary Keys
      Expression exp = null;
      int i = 0;
      for( MetaField mf : getPrimaryKeys( mc )) {
        Expression e = new Expression( mf.getName(), ref.getIds()[ i ] );
        if ( exp == null ) exp = e;
        else exp = exp.and( e );
        i++;
      }
     
      if ( exp == null ) throw new PersistenceException( "MetaClass [" + mc + "] has no primary keys get object by reference [" + ref + "]" );
       
      // Create the QueryOptions and limit to the first 1
      QueryOptions qo = new QueryOptions();
      qo.setRange( 1, 1 );
     
      // Read the objects from the database driver
      Collection<Object> objects = getDatabaseDriver().readMany( conn, mc, readMap, qo );
     
      // Reset the object persistence states
      resetObjects( mc, objects );
     
      // Return the object if found
      if ( objects.size() > 0 ) return objects.iterator().next();
      else throw new ObjectNotFoundException( refStr );     
    }
    catch( SQLException e ) {
      //log.error( "Unable to load object [" + mc + "] with reference [" + ref + "]: " + e.getMessage(), e );
      throw new PersistenceException( "Unable to load object [" + mc + "] with reference [" + ref + "]: " + e.getMessage(), e );
    }
  }
View Full Code Here

    if ( log.isDebugEnabled() ) {
        log.debug( "Loading object [" + o + "] of class [" + mc + "]" );
    }

    // Create the Expression for the Primary Keys
    Expression exp = null;
    for( MetaField mf : getPrimaryKeys( mc )) {
      Expression e = new Expression( mf.getName(), mf.getObject( o ));
      if ( exp == null ) exp = e;
      else exp = exp.and( e );
    }
   
    if ( exp == null ) throw new PersistenceException( "MetaClass [" + mc + "] has no primary keys defined to load object [" + o + "]" );
   
    // Try to read the object
    try {
      // Read the object from the mapping
      boolean found = getDatabaseDriver().read( conn, mc, mapping, o, exp );
     
      // If not found throw an exception
      if ( !found ) throw new ObjectNotFoundException( o );       

      // Reset the object after it's loaded
      resetObject( mc, o );
    }
    catch( SQLException e )
    {
      //log.error( "Unable to load object [" + o + "]: " + e.getMessage(), e );
      throw new PersistenceException( "Unable to load object [" + o + "]: " + e.getMessage(), e );
    }
  }
View Full Code Here

        if ( !allowsDirtyWrites ) {
         
          mapping = (ObjectMappingDB) getReadMapping( mc );
         
          // Create the Expression for the Primary Keys
          Expression exp = null;
          for( MetaField mf : getPrimaryKeys( mc )) {
            Expression e = new Expression( mf.getName(), mf.getObject( obj ));
            if ( exp == null ) exp = e;
            else exp = exp.and( e );
          }
         
          if ( exp == null ) throw new PersistenceException( "MetaClass [" + mc + "] has no primary keys defined to update object [" + obj + "]" );
                   
          Collection<Object> results = getDatabaseDriver().readMany( conn, mc, mapping, new QueryOptions( exp ));
          if ( results.size() > 0 ) {
            throw new DirtyWriteException( obj );
          }
        }
       
        throw new ObjectNotFoundException( obj );
      }

      postPersistence( c, mc, obj, UPDATE );
    }
    catch( SQLException e ) {
      //log.error( "Unable to update object of class [" + mc + "]: " + e.getMessage() );
      throw new PersistenceException( "Unable to update object [" + obj + "] of class [" + mc + "]: " + e.getMessage(), e );
    }
  }
View Full Code Here

TOP

Related Classes of com.draagon.meta.manager.exp.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.