Package nexj.core.persistence

Examples of nexj.core.persistence.Operator$Visitor


   public Operator normalize(int nFlags)
   {
      verifyNesting();
      normalizeOperand(nFlags);

      Operator op = getOperand();
      Type type = op.getType();

      m_type = type;

      if (op.isConstant())
      {
         setConstantValue(op.getValue());
      }
      else
      {
         m_bConstant = false;
View Full Code Here


   public Operator normalize(int nFlags)
   {
      verifyNesting();
      normalizeOperand(nFlags);

      Operator op = getOperand();
      Type type = op.getType();

      if (type == null)
      {
         setConstantValue(null);
         m_type = null;
      }
      else
      {
         if (type instanceof Primitive)
         {
            switch (((Primitive)type).getOrdinal())
            {
               case Primitive.INTEGER_ORDINAL:
               case Primitive.LONG_ORDINAL:
               case Primitive.FLOAT_ORDINAL:
               case Primitive.DOUBLE_ORDINAL:
               case Primitive.DECIMAL_ORDINAL:
                  m_bConstant = false;
                  m_type = type;
                  break;

               default:
                  throw new TypeMismatchException(getSymbol());
            }

            if (op.isConstant())
            {
               foldConstant();
            }
         }
         else
View Full Code Here

   /**
    * @see nexj.core.persistence.operator.AggregateOperator#foldConstant()
    */
   protected void foldConstant()
   {
      Operator op = getOperand();

      if (op.isConstant() && op.getValue() == null)
      {
         setConstantValue(null);
      }
   }
View Full Code Here

      boolean bMixed = false;

      for (int i = 0; i < m_nOperandCount; ++i)
      {
         Operator op = m_operandArray[i];

         if (op.getType() == null)
         {
            removeOperand(i--);
         }
         else
         {
            if (op.getType() != Primitive.BOOLEAN)
            {
               throw new TypeMismatchException(getSymbol());
            }

            if (op.isConstant())
            {
               if (Boolean.FALSE.equals(op.getValue()))
               {
                  setConstantValue(Boolean.FALSE);

                  return this;
               }

               removeOperand(i--);
            }
            else
            {
               m_bConstant = false;
               bMixed = addSource(op, bMixed);

               if (op.getOrdinal() == ORDINAL)
               {
                  int nCount = mergeOperand(i);

                  if (nCount != 0)
                  {
                     i += nCount - 1;
                  }
               }
            }
         }
      }

      if (m_nOperandCount == 0)
      {
         setConstantValue(Boolean.TRUE);
      }
      else if (m_nOperandCount == 1)
      {
         Operator op = m_operandArray[0];

         op.setParent(m_parent);

         return op;
      }

      setSource(m_source, nFlags);
View Full Code Here

    */
   public Operator normalize(int nFlags)
   {
      if ((nFlags & NORMALIZE_NORECUR) == 0)
      {
         Operator attribute = m_attribute.normalize(nFlags);

         setAttribute((attribute instanceof AttributeOperator)
                      ? (AttributeOperator)attribute : null); // null check will throw error
      }

View Full Code Here

      if (m_left.isConstant() && !m_right.isConstant())
      {
         if (isSymmetric())
         {
            Operator left = m_left;
            m_left = m_right;
            m_right = left;
         }
         else
         {
            ComparisonOperator op = createSymmetric();

            op.setParent(m_parent);
            op.setLeft(m_right);
            op.setRight(m_left);

            return op.normalize(nFlags | NORMALIZE_NORECUR);
         }
      }

      Type leftType = m_left.getType();
      Type rightType = m_right.getType();

      if (leftType == null)
      {
         assert m_left.isConstant();
         assert m_right.isConstant();

         setConstantValue(Boolean.valueOf(nullPredicate(m_right.getValue() == null)));

         return this;
      }

      if (rightType == null)
      {
         rightType = leftType;
         m_right.setType(rightType);
      }

      if (!isEquivalence())
      {
         if (rightType == null || m_right.isConstant() && m_right.getValue() == null)
         {
            Operator op = createNullComparison();

            op.setParent(m_parent);

            if (op.getOrdinal() != ConstantOperator.ORDINAL)
            {
               ComparisonOperator cmpOp = (ComparisonOperator)op;

               cmpOp.setLeft(m_left);
               cmpOp.setRight(m_right);
            }

            return op.normalize(nFlags | NORMALIZE_NORECUR);
         }
      }

      setSource(findCommonSource(m_left, m_right), nFlags);

      if (leftType.isPrimitive())
      {
         if (rightType != null)
         {
            if (!rightType.isPrimitive())
            {
               throw new TypeMismatchException(getSymbol());
            }
         }

         if (leftType == Primitive.ANY && m_left.isConstant() && m_right.isConstant())
         {
            foldObjectComparison();
            return this;
         }

         ConversionMapper mapper = null;
         BinaryDescriptor descriptor = null;

         if (m_source != null)
         {
            mapper = m_source.getAdapter().getConversionMapper();
            descriptor = mapper.getBinaryDescriptor(this);
         }

         if (descriptor == null)
         {
            m_source = null;
            mapper = getConversionMapper();
            descriptor = mapper.getBinaryDescriptor(this);

            if (descriptor == null)
            {
               throw new TypeMismatchException(getSymbol());
            }
         }

         Primitive rightPrimitive = (Primitive)rightType;

         if (mapper.getType(rightPrimitive) != mapper.getType(descriptor.getRightType()))
         {
            setRight(new TypeConversionOperator(descriptor.getRightType(), m_right));
            m_right = m_right.normalize(nFlags | NORMALIZE_NORECUR);
         }

         Primitive leftPrimitive = (Primitive)leftType;

         if (mapper.getType(leftPrimitive) != mapper.getType(descriptor.getLeftType()))
         {
            if (m_right.isConstant() && Primitive.isOrderPreserved(leftPrimitive, descriptor.getLeftType()))
            {
               if (m_right.getValue() != null)
               {
                  m_bConstant = false;

                  if (unconvertConstantComparison(leftPrimitive, rightPrimitive, m_right.getValue()))
                  {
                     if (m_bConstant)
                     {
                        return this;
                     }
                  }
                  else
                  {
                     setLeft(new TypeConversionOperator(descriptor.getLeftType(), m_left));
                     m_left = m_left.normalize(nFlags | NORMALIZE_NORECUR);
                  }
               }
            }
            else
            {
               setLeft(new TypeConversionOperator(descriptor.getLeftType(), m_left));
               m_left = m_left.normalize(nFlags | NORMALIZE_NORECUR);
            }
         }

         if (m_left.isConstant())
         {
            assert m_right.isConstant();

            setConstantValue(evaluate());
         }
         else if (m_right.isConstant())
         {
            if (m_left.getOrdinal() == AttributeOperator.ORDINAL)
            {
               AttributeOperator left = (AttributeOperator)m_left;
               Converter converter = left.getConverter();

               if (converter != null && (isEquivalence() ||
                  Primitive.isOrderPreserved(converter.getSourceType(), converter.getDestinationType())))
               {
                  left.setType(converter.getSourceType());
                  left.setNoConversion(true);
                  m_right.setValue(converter.getInverseFunction().invoke(m_right.getValue()));
                  m_right.setType(converter.getSourceType());
               }
            }
            else if (m_left.getType() == Primitive.BOOLEAN && m_right.getValue() != null)
            {
               switch (booleanPredicate(((Boolean)m_right.getValue()).booleanValue()))
               {
                  case BOOL_LHS:
                     return m_left;

                  case BOOL_NOT:
                     NotOperator op = new NotOperator();

                     op.setOperand(m_left);
                     op.normalize(nFlags | NORMALIZE_NORECUR);

                     return op;

                  case BOOL_TRUE:
                     setConstantValue(Boolean.TRUE);
                     break;

                  case BOOL_FALSE:
                     setConstantValue(Boolean.FALSE);
                     break;
               }
            }
         }
      }
      else
      {
         assert m_left.getOrdinal() == AttributeOperator.ORDINAL;

         if (rightType != null)
         {
            if (rightType.isPrimitive() &&
               (rightType != Primitive.ANY || !m_right.isConstant() ||
                m_right.getValue() != null && !(m_right.getValue() instanceof OIDHolder)))
            {
               throw new TypeMismatchException(getSymbol());
            }
         }

         if (m_left.isConstant())
         {
            foldObjectComparison();
            return this;
         }

         if ((nFlags & NORMALIZE_PERSISTENCE) != 0 && m_source != null)
         {
            PersistenceAdapter adapter = m_source.getAdapter();
            Field[] leftFieldArray = adapter.getFields(m_left.getSource());

            if (leftFieldArray != null)
            {
               Field[] rightFieldArray = null;
               Object[] valueArray = null;

               if (m_right.isConstant())
               {
                  if (m_right.getValue() != null)
                  {
                     OID oid = ((OIDHolder)m_right.getValue()).getOID();

                     if (oid != null)
                     {
                        valueArray = adapter.getValues(oid, m_left.getSource());

                        if (leftFieldArray.length != valueArray.length)
                        {
                           throw new TypeMismatchException(getSymbol());
                        }
                     }
                     else if (isEquivalence())
                     {
                        setConstantValue(Boolean.valueOf(nullPredicate(false)));

                        return this;
                     }
                  }
               }
               else
               {
                  assert m_right.getOrdinal() == AttributeOperator.ORDINAL;

                  rightFieldArray = adapter.getFields((Query)m_right.getSource());

                  assert rightFieldArray != null;

                  if (leftFieldArray.length != rightFieldArray.length)
                  {
                     throw new TypeMismatchException(getSymbol());
                  }
               }

               assert leftFieldArray.length > 0;

               Operator result = null;

               if (leftFieldArray.length == 1)
               {
                  Field field = leftFieldArray[0];

                  if (m_left.getQuery().getConstraint() != this)
                  {
                     m_left.setSource(field);
                     m_left.setType(field.getType());

                     if (rightFieldArray != null)
                     {
                        field = rightFieldArray[0];
                        m_right.setSource(field);
                        m_right.setType(field.getType());
                     }
                     else
                     {
                        ConstantOperator cons;
                        Object value = (valueArray == null) ? null : valueArray[0];

                        if (m_right.getOrdinal() == ConstantOperator.ORDINAL)
                        {
                           cons = (ConstantOperator)m_right;
                           cons.setType(Primitive.primitiveOf(value));
                           cons.setValue(value);
                        }
                        else
                        {
                           cons = new ConstantOperator(value);
                           setRight(cons);
                        }
                     }

                     result = this;
                  }
                  else
                  {
                     ComparisonOperator cmp = createSame();

                     cmp.setParent(m_parent);
                     cmp.setLeft(new AttributeOperator(field));

                     if (rightFieldArray != null)
                     {
                        cmp.setRight(new AttributeOperator(rightFieldArray[0]));
                     }
                     else
                     {
                        cmp.setRight(new ConstantOperator((valueArray == null) ? null : valueArray[0]));
                     }

                     result = cmp;
                  }
               }
               else if (isEquivalence())
               {
                  MultiArgOperator combinator;

                  if (rightFieldArray == null && valueArray == null)
                  {
                     combinator = new AndOperator();
                  }
                  else
                  {
                     if (isCombinatorDisjunctive())
                     {
                        combinator = new OrOperator();
                     }
                     else
                     {
                        combinator = new AndOperator();
                     }
                  }

                  combinator.setParent(m_parent);

                  for (int i = 0; i < leftFieldArray.length; ++i)
                  {
                     ComparisonOperator cmp = createSame();

                     cmp.setLeft(new AttributeOperator(leftFieldArray[i]));

                     if (rightFieldArray != null)
                     {
                        cmp.setRight(new AttributeOperator(rightFieldArray[i]));
                     }
                     else
                     {
                        cmp.setRight(new ConstantOperator((valueArray == null) ? null : valueArray[i]));
                     }

                     combinator.addOperand(cmp);
                  }

                  result = combinator;
               }
               else
               {
                  OrOperator or = new OrOperator();

                  or.setParent(m_parent);

                  int nCount = leftFieldArray.length - 1;

                  for (int i = 0; i <= nCount; ++i)
                  {
                     MultiArgOperator combinator;

                     if (i == 0)
                     {
                        combinator = or;
                     }
                     else
                     {
                        combinator = new AndOperator();
                        or.addOperand(combinator);
                     }

                     for (int k = 0; k <= i; ++k)
                     {
                        ComparisonOperator cmp;

                        if (k == i)
                        {
                           if (i == nCount)
                           {
                              cmp = createSame();
                           }
                           else
                           {
                              cmp = createStrict();
                           }
                        }
                        else
                        {
                           cmp = new EqualsOperator();
                        }

                        cmp.setLeft(new AttributeOperator(leftFieldArray[i]));

                        if (rightFieldArray != null)
                        {
                           cmp.setRight(new AttributeOperator(rightFieldArray[i]));
                        }
                        else
                        {
                           cmp.setRight(new ConstantOperator((valueArray == null) ? null : valueArray[i]));
                        }

                        combinator.addOperand(cmp);
                     }
                  }

                  result = or;
               }

               return result.normalize(nFlags & ~NORMALIZE_NORECUR);
            }
         }
      }

      return this;
View Full Code Here

      boolean bConstant = true;

      for (int i = 0; i < m_nOperandCount; ++i)
      {
         Operator op = getOperand(i);
         Type fromType = op.getType();

         if (fromType != null)
         {
            if (!fromType.isPrimitive())
            {
               throw new TypeMismatchException(getSymbol());
            }

            Primitive toType = m_argTypeArray[i];

            if (toType != fromType)
            {
               if (toType.findStrictConverter((Primitive)fromType) == null)
               {
                  throw new TypeMismatchException(getSymbol());
               }

               op = new TypeConversionOperator(toType, op);
               setOperand(i, op);
               op = op.normalize(nFlags | NORMALIZE_NORECUR);
               setOperand(i, op);
            }

            if (!op.isConstant())
            {
               bConstant = false;
            }
         }
      }
View Full Code Here

         {
            m_operandArray[i] = m_operandArray[i].normalize(nFlags);
         }
      }

      Operator first = m_operandArray[0];
      Type type = first.getType();

      if (type == null || m_nOperandCount <= 1)
      {
         setConstantValue(Boolean.FALSE);

         for (int i = 1; i < m_nOperandCount; ++i)
         {
            Operator op = m_operandArray[i];

            if (!op.isConstant())
            {
               throw new InvalidQueryException("err.persistence.variableInList");
            }

            if (op.getValue() == null)
            {
               m_value = Boolean.TRUE;
               break;
            }
         }

         return this;
      }

      setSource(first.getSource(), nFlags);

      if (type.isPrimitive())
      {
         if (type == Primitive.ANY && first.isConstant())
         {
            foldObjectIn(first);
            return this;
         }

         Primitive firstType = (Primitive)type;
         boolean bNull = false;

         for (int i = 1; i < m_nOperandCount; ++i)
         {
            Operator op = m_operandArray[i];

            if (!op.isConstant() || op.getType() != null && !op.getType().isPrimitive())
            {
               throw new InvalidQueryException("err.persistence.variableInList");
            }

            Primitive opType = (Primitive)op.getType();

            if (firstType.findEQFunction(opType) == null)
            {
               throw new TypeMismatchException(getSymbol());
            }

            op.setValue(firstType.getConverter(opType).invoke(op.getValue()));
            op.setType(firstType);

            if (op.getValue() == null && !first.isConstant())
            {
               bNull = true;
               removeOperand(i--);
            }
         }

         if (first.isConstant())
         {
            setConstantValue(evaluate());
         }
         else
         {
            if (first.getOrdinal() == AttributeOperator.ORDINAL)
            {
               AttributeOperator aop = (AttributeOperator)first;
               Converter converter = aop.getConverter();

               if (converter != null)
               {
                  aop.setType(converter.getSourceType());
                  aop.setNoConversion(true);

                  for (int i = 1; i < m_nOperandCount; ++i)
                  {
                     Operator op = m_operandArray[i];

                     op.setValue(converter.getInverseFunction().invoke(op.getValue()));
                     op.setType(converter.getSourceType());
                  }
               }
            }

            if (bNull)
            {
               if (m_nOperandCount > 1)
               {
                  OrOperator or = new OrOperator();

                  or.setParent(m_parent);

                  EqualsOperator eq = new EqualsOperator();

                  eq.setParent(or);
                  eq.setLeft((Operator)first.clone());
                  eq.setRight(new ConstantOperator(null));

                  or.addOperand(eq.normalize(nFlags | NORMALIZE_NORECUR));
                  or.addOperand(this);

                  return or.normalize(nFlags | NORMALIZE_NORECUR);
               }
               else
               {
                  EqualsOperator eq = new EqualsOperator();

                  eq.setParent(m_parent);
                  eq.setLeft(first);
                  eq.setRight(new ConstantOperator(null));
                  eq.normalize(nFlags | NORMALIZE_NORECUR);

                  return eq;
               }
            }
         }

      }
      else
      {
         assert first.getOrdinal() == AttributeOperator.ORDINAL;

         for (int i = 1; i < m_nOperandCount; ++i)
         {
            Operator op = m_operandArray[i];

            if (op.getType() != null)
            {
               if (op.getType().isPrimitive() &&
                  (op.getType() != Primitive.ANY || !op.isConstant() ||
                   op.getValue() != null && !(op.getValue() instanceof OIDHolder)))
               {
                  throw new TypeMismatchException(getSymbol());
               }
            }
         }

         if (first.isConstant())
         {
            foldObjectIn(first);
            return this;
         }

         if ((nFlags & NORMALIZE_PERSISTENCE) != 0 && m_source != null)
         {
            PersistenceAdapter adapter = m_source.getAdapter();
            Field[] fieldArray = adapter.getFields(m_source);

            if (fieldArray != null)
            {
               int nCount = fieldArray.length;

               assert nCount > 0;

               if (nCount == 1)
               {
                  Field field = fieldArray[0];

                  first.setSource(field);
                  first.setType(field.getType());

                  for (int i = 1; i < m_nOperandCount; ++i)
                  {
                     Operator op = m_operandArray[i];

                     if (op.getValue() == null)
                     {
                        op.setType(null);
                     }
                     else
                     {
                        OID oid = ((OIDHolder)op.getValue()).getOID();

                        if (oid != null)
                        {
                           Object[] valueArray = adapter.getValues(oid, m_source);

                           if (valueArray.length != 1)
                           {
                              throw new TypeMismatchException(getSymbol());
                           }

                           op.setValue(valueArray[0]);
                           op.setType(Primitive.primitiveOf(op.getValue()));
                        }
                        else
                        {
                           removeOperand(i--);
                        }
                     }
                  }

                  return normalize(nFlags | NORMALIZE_NORECUR);
               }
               else
               {
                  OrOperator or = new OrOperator();

                  for (int i = 1; i < m_nOperandCount; ++i)
                  {
                     Operator op = m_operandArray[i];
                     Object[] valueArray = null;

                     if (op.getValue() != null)
                     {
                        OID oid = ((OIDHolder)op.getValue()).getOID();

                        if (oid != null)
                        {
                           valueArray = adapter.getValues(oid, m_source);

                           if (valueArray.length != nCount)
                           {
                              throw new TypeMismatchException(getSymbol());
                           }
                        }
                        else
                        {
                           continue;
                        }
                     }

                     AndOperator and = new AndOperator();

                     for (int k = 0; k < nCount; ++k)
                     {
                        EqualsOperator eq = new EqualsOperator();

                        eq.setLeft(new AttributeOperator(fieldArray[k]));
                        eq.setRight(new ConstantOperator((valueArray == null) ? null : valueArray[k]));
                        and.addOperand(eq);
                     }

                     or.addOperand(and);
                  }

                  if (or.getOperandCount() == 1)
                  {
                     Operator op = or.getOperand(0);

                     op.setParent(m_parent);

                     return op.normalize(nFlags & ~NORMALIZE_NORECUR);
                  }
                  else
                  {
                     or.setParent(m_parent);
View Full Code Here

         }
      }

      for (int i = 1; i < m_nOperandCount; ++i)
      {
         Operator op = m_operandArray[i];
         Object[] opValues = null;
         boolean bReference = false;

         if (op.getValue() != null)
         {
            if (!(op.getValue() instanceof OIDHolder))
            {
               throw new TypeMismatchException(getSymbol());
            }

            OID oid = ((OIDHolder)op.getValue()).getOID();

            if (oid != null)
            {
               opValues = oid.getValueArray();
            }
            else
            {
               bReference = true;
            }
         }

         if (bFirstReference & bReference)
         {
            m_value = Boolean.valueOf(first.getValue() == op.getValue());
         }
         else if (firstValues != null)
         {
            int nCount = firstValues.length;
View Full Code Here

   /**
    * @see nexj.core.persistence.Operator#evaluate()
    */
   protected Object evaluate()
   {
      Operator first = m_operandArray[0];
      Object firstValue = first.getValue();
      Type type = first.getType();

      if (type.isPrimitive())
      {
         Primitive primitiveType = (Primitive)type;
         BinaryFunction f = primitiveType.getEQFunction(primitiveType);
View Full Code Here

TOP

Related Classes of nexj.core.persistence.Operator$Visitor

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.