Package nexj.core.scripting

Examples of nexj.core.scripting.Pair


    * @param query The query.
    * @return The order by expression.
    */
   public static Pair getOrderBy(Query query)
   {
      Pair orderBy = null;

      for (int i = query.getOrderByCount() - 1; i >= 0; i--)
      {
         Operator op = query.getOrderByOperator(i);
         boolean bAscending = query.isOrderByAscending(i);

         orderBy = new Pair(
            new Pair(op.getExpression(query), Boolean.valueOf(bAscending)),
            orderBy
         );
      }

      return orderBy;
View Full Code Here


    * @param query The query.
    * @return The attributes being queried, in Object'read attribute list form.
    */
   public static Pair getAttributes(Query query)
   {
      Pair attributes = null;
      Lookup polymorphicAttrMap = null;

      // Add primitive output attributes
      for (Field field = query.getFirstOutputField(); field != null; field = field.getNext())
      {
         Attribute attribute = field.getAttribute();
         Metaclass metaclass = attribute.getMetaclass();

         if (metaclass.isUpcast(query.getMetaclass()))
         {
            attributes = new Pair(attribute.getSymbol(), attributes);
         }
         else
         {
            // Attribute defined in subclass, add to polymorphic attribute list
            assert query.getMetaclass().isUpcast(metaclass);

            if (polymorphicAttrMap == null)
            {
               polymorphicAttrMap = new HashTab(2);
            }

            Pair subclassAttrs = (Pair)polymorphicAttrMap.get(metaclass);

            subclassAttrs = new Pair(attribute.getSymbol(), subclassAttrs);
            polymorphicAttrMap.put(metaclass, subclassAttrs);
         }
      }

      // Add non-primitive output attributes
      for (Iterator itr = query.getAssocIterator(Query.ASSOC_QUERY); itr.hasNext(); )
      {
         Query subQuery = (Query)itr.next();
         Attribute attribute = subQuery.getAttribute();
         Pair subQueryAttrs = getAttributes(subQuery);
         Metaclass metaclass = attribute.getMetaclass();
         Object itemToAdd = (subQueryAttrs == null) ? (Object)attribute.getSymbol() : new Pair(attribute.getSymbol(), subQueryAttrs);

         if (metaclass.isUpcast(query.getMetaclass()))
         {
            attributes = new Pair(itemToAdd, attributes);
         }
         else
         {
            // Attribute defined in subclass, add to polymorphic attribute list
            assert query.getMetaclass().isUpcast(metaclass);

            if (polymorphicAttrMap == null)
            {
               polymorphicAttrMap = new HashTab(2);
            }

            Pair subclassAttrs = (Pair)polymorphicAttrMap.get(metaclass);

            subclassAttrs = new Pair(itemToAdd, subclassAttrs);
            polymorphicAttrMap.put(metaclass, subclassAttrs);
         }
      }

      // Merge the polymorphic attributes
      if (polymorphicAttrMap != null)
      {
         for (Lookup.Iterator itr = (Lookup.Iterator)polymorphicAttrMap.iterator(); itr.hasNext(); )
         {
            Metaclass subClass = (Metaclass)itr.next();
            Pair subclassAttrs = (Pair)itr.getValue();

            attributes = new Pair(
               new Pair(Symbol.ATAT, new Pair(subClass.getSymbol(), subclassAttrs)),
               attributes
            );
         }
      }

View Full Code Here

   /**
    * @see nexj.core.meta.persistence.PersistenceMapping#getSortKeys(nexj.core.meta.Attribute[], nexj.core.meta.persistence.PersistenceMapping[], Attribute[])
    */
   public Pair getSortKeys(Attribute[] assocs, PersistenceMapping[] mappings, Attribute[] restrictions)
   {
      Pair sortKeys = null;
      Pair lastKey = null;

      for (int nTable = 0; nTable < m_nTableCount; ++nTable)
      {
         Table table = m_tableArray[nTable];
         RelationalPrimitiveMapping[] primitiveMappings = table.findMappingArray(this);
         BitSet filterSet = null;
         BitSet optionalSet = null;
         int nFilterCount = 0;

         if (primitiveMappings == null)
         {
            continue;
         }

         // Create a set of columns, which participate in equality
         // comparisons due to the associations.
         if (assocs != null && assocs.length > 0)
         {
            filterSet = new BitSet(table.getColumnCount());

            for (int i = 0; i < assocs.length; ++i)
            {
               PersistenceMapping mapping = mappings[i];

               if (mapping == null)
               {
                  continue;
               }

               AttributeMapping attributeMapping = mapping.getAttributeMapping(assocs[i]);

               if (attributeMapping instanceof RelationalClassMapping)
               {
                  RelationalClassMapping classMapping = (RelationalClassMapping)attributeMapping;

                  if (classMapping.getMapping() == this &&
                     classMapping.getDestinationKey() instanceof Index)
                  {
                     Index key = (Index)classMapping.getDestinationKey();

                     if (key.getTable() != table)
                     {
                        key = table.getPrimaryKey();
                     }

                     if (!key.isUnique())
                     {
                        for (int k = key.getIndexColumnCount() - 1; k >= 0; --k)
                        {
                           filterSet.set(key.getIndexColumn(k).getColumn().getOrdinal());
                        }
                     }
                  }
               }
            }

            nFilterCount = filterSet.cardinality();
         }

         // Create a set of columns, which participate in equality
         // comparisons and can be optionally excluded from the index.
         if (restrictions != null && restrictions.length > 0)
         {
            optionalSet = new BitSet(table.getColumnCount());

            for (int i = 0; i < restrictions.length; ++i)
            {
               AttributeMapping attributeMapping = getAttributeMapping(restrictions[i]);

               if (attributeMapping instanceof RelationalClassMapping)
               {
                  Index index = ((RelationalClassMapping)attributeMapping).getSourceKey();

                  for (int k = index.getIndexColumnCount() - 1; k >= 0; --k)
                  {
                     optionalSet.set(index.getIndexColumn(k).getColumn().getOrdinal());
                  }
               }
               else if (attributeMapping instanceof RelationalPrimitiveMapping)
               {
                  optionalSet.set(((RelationalPrimitiveMapping)attributeMapping).getColumn().getOrdinal());
               }
            }
         }

         // Collect the sort keys
         for (int nIndex = 0; nIndex < table.getIndexCount(); ++nIndex)
         {
            Index index = table.getIndex(nIndex);

            if (index.getType() != Index.CLUSTER &&
               index.getType() != Index.BTREE &&
               index.getType() != Index.QUERY)
            {
               continue;
            }

            // Ignore indexes that do not start with the columns specified
            // in the association filter, in arbitrary order
            if (nFilterCount > 0)
            {
               if (index.getIndexColumnCount() < nFilterCount)
               {
                  continue;
               }

               int i;

               for (i = 0; i < nFilterCount; ++i)
               {
                  if (!filterSet.get(index.getIndexColumn(i).getColumn().getOrdinal()))
                  {
                     break;
                  }
               }

               if (i < nFilterCount)
               {
                  continue;
               }
            }

            Index pk = table.getPrimaryKey();
            boolean bMatch = true;
            boolean bAscending = true;
            Pair key = null;

            // Check whether the primary key columns are a subset of the index columns
            for (int i = 0; i < pk.getIndexColumnCount(); ++i)
            {
               IndexColumn pkIndexColumn = pk.getIndexColumn(i);
               Column pkColumn = pkIndexColumn.getColumn();

               if (optionalSet != null &&
                  optionalSet.get(pkColumn.getOrdinal()) &&
                  index.findIndexColumn(pkColumn) != null)
               {
                  continue;
               }

               int k;

               for (k = 0; k < index.getIndexColumnCount(); ++k)
               {
                  IndexColumn indexColumn = index.getIndexColumn(k);

                  if (indexColumn.getColumn() == pkColumn)
                  {
                     if (i == 0)
                     {
                        bAscending = indexColumn.isAscending();
                     }

                     break;
                  }
               }

               if (k == index.getIndexColumnCount())
               {
                  bMatch = false;

                  break;
               }
            }

            // If the above is true, then append the primary key
            // at the end of the sort key
            if (bMatch)
            {
               key = new Pair(new Pair(new Pair(Symbol.AT),
                  Boolean.valueOf(!(pk.getIndexColumn(0).isAscending() ^ bAscending))));
            }
            else
            {
               pk = null;
            }

            int nCount;

            for (nCount = nFilterCount; nCount < index.getIndexColumnCount(); ++nCount)
            {
               int nOrdinal = index.getIndexColumn(nCount).getColumn().getOrdinal();

               if (primitiveMappings[nOrdinal] == null)
               {
                  if (optionalSet == null || !optionalSet.get(nOrdinal))
                  {
                     break;
                  }
               }
            }

            bMatch = true;

            if (pk != null)
            {
               if (nCount == index.getIndexColumnCount())
               {
                  key = null;
               }
               else
               {
                  for (int i = nCount; i < index.getIndexColumnCount(); ++i)
                  {
                     if (!index.getIndexColumn(i).getColumn().isPrimary())
                     {
                        bMatch = false;
                        break;
                     }
                  }
               }
            }

            if (!bMatch)
            {
               continue;
            }

            boolean bPrimitive = false;

            for (int i = nCount - 1; i >= nFilterCount; --i)
            {
               IndexColumn indexColumn = index.getIndexColumn(i);
               int nOrdinal = indexColumn.getColumn().getOrdinal();
               RelationalPrimitiveMapping mapping = primitiveMappings[nOrdinal];

               if (mapping != null)
               {
                  if (optionalSet == null || !optionalSet.get(nOrdinal))
                  {
                     key = new Pair(new Pair(mapping.getAttribute().getSymbol(),
                        Boolean.valueOf(indexColumn.isAscending())), key);
                     bPrimitive = true;
                  }
               }
            }

            // Append the new sort key to the end of the sort key list
            if (key != null)
            {
               lastKey = addKey(new Pair(Boolean.valueOf(pk != null ||
                  index.isUnique() && nCount == index.getIndexColumnCount()), key), sortKeys, lastKey);

               if (sortKeys == null)
               {
                  sortKeys = lastKey;
               }

               if (pk == index && bPrimitive)
               {
                  lastKey = addKey(new Pair(Boolean.TRUE,
                     new Pair(new Pair(new Pair(Symbol.AT), Boolean.TRUE))), sortKeys, lastKey);
               }
            }
         }
      }

View Full Code Here

    */
   protected static Pair addKey(Pair key, Pair head, Pair tail)
   {
      if (tail == null)
      {
         tail = new Pair(key);
      }
      else
      {
         Pair pair;

         // Filter out the duplicates
         for (pair = head; pair != null; pair = pair.getNext())
         {
            if (pair.getHead().equals(key))
            {
               break;
            }
         }

         if (pair == null)
         {
            key = new Pair(key);
            tail.setTail(key);
            tail = key;
         }
      }

View Full Code Here

   /**
    * @see nexj.core.meta.persistence.PersistenceMapping#getUniqueKeys()
    */
   public Pair getUniqueKeys()
   {
      Pair uniqueKeys = null; // The resulting key list
      int[] ordinalArray = null; // Contains the ordinal numbers of attributes
      boolean[] useArray = null; // Flags index columns as used

      for (int nTable = 0; nTable < m_nTableCount; ++nTable)
      {
View Full Code Here

               if (nSize > 0)
               {
                  if (nColumnCount + nSize == nIndexColumnCount)
                  {
                     Pair key = null;

                     for (int i = nTop; i >= 0; --i)
                     {
                        key = new ConstPair(m_metaclass.getInstanceAttribute(ordinalArray[i]).getSymbol(), key);
                     }
View Full Code Here

    * @return An attribute list.
    */
   public Pair getAttributes(BitSet bitmap)
   {
      Metaclass metaclass = m_virtualMapping.getMetaclass();
      Pair attrList = null;

      for (int i = bitmap.nextSetBit(0); i >= 0; i = bitmap.nextSetBit(i + 1))
      {
         attrList = new Pair(metaclass.getInstanceAttribute(i).getSymbol(), attrList);
      }

      return attrList;
   }
View Full Code Here

            }

            try
            {
               XMLMetadataHelper helper = loader.getHelper();
               Pair list = (Pair)helper.parse(sResource, true, null, null, msg.getMetadata().getGlobalEnvironment());

               while (list != null)
               {
                  Object resource = list.getHead();
                  String sSourceURL;
                  URL dstURL;

                  if (resource instanceof Pair)
                  {
                     Pair resMap = (Pair)resource;

                     sSourceURL = resMap.getOperator().getName();
                     dstURL = helper.getResource(resMap.getNext().getOperator().getName()).getURL();
                  }
                  else
                  {
                     sSourceURL = ((Symbol)resource).getName();
                     dstURL = helper.getResource(sSourceURL).getURL();
View Full Code Here

       *       )
       *    )
       *    '(() () ... ())
       * )
       */
      Pair cursorScript = new Pair(
         Pair.list(Symbol.LAMBDA, m_variables,
            Pair.list(Symbol.CONS,
               Pair.list(
                  Pair.list(Symbol.LAMBDA, Pair.list(READ_RESULT),
                     Pair.list(Symbol.IF, Pair.list(Symbol.INSTANCE_P, READ_RESULT, PCodeFunction.class),
                        Pair.list(GENERATOR_ITERATOR, READ_RESULT),
                        READ_RESULT
                     )
                  ),
                  Pair.list(
                     new Pair(Symbol.LAMBDA, new Pair(null, (m_readScript == null) ? new Pair(null) : m_readScript))
                  )
               ),
               new Pair(Symbol.LAMBDA, new Pair(null, (m_closeScript == null) ? new Pair(null) : m_closeScript))
            )
         ),
         getVariableInitializer()
      );

View Full Code Here

    * Gets a list of nulls for initializing the script variables.
    * @return A list of nulls for initializing the variables.
    */
   private Pair getVariableInitializer()
   {
      Pair result = null;
      Pair var = m_variables;

      while (var != null)
      {
         result = new Pair(null, result);
         var = var.getNext();
      }

      return result;
   }
View Full Code Here

TOP

Related Classes of nexj.core.scripting.Pair

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.