Package nexj.core.persistence

Examples of nexj.core.persistence.Query$ComparisonOperatorFactory


      }

      // 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()))
View Full Code Here


            bRead = (m_nFlags & LOAD_READ) != 0;
         }

         if (bRead)
         {
            Query query = Query.createRead(m_metaclass, attributes,
               new Pair(Symbol.AT).eq(m_oid), null, -1, 0, false,
               Query.SEC_NONE, m_context);

            for (int i = 0, nCount = m_metaclass.getInstanceAttributeCount(); i < nCount; ++i)
            {
               Attribute attribute = m_metaclass.getInstanceAttribute(i);

               if ((isLazy() || getValueDirect(attribute.getOrdinal()) == Undefined.VALUE) &&
                  !attribute.isLazy())
               {
                  query.addAttribute(Query.ASSOC_QUERY, attribute, null, false, Query.OUTPUT_EAGER);
               }
            }

            if (query.getFirstOutputField() != null &&
               (query.getFirstOutputField().getNext() != null ||
                  query.getFirstOutputField().getAttribute() !=
                  mapping.getLockingAttribute()) ||
               query.getAssocCount(Query.ASSOC_QUERY) > 0)
            {
               if (s_logger.isDebugEnabled())
               {
                  String sMsg = "Loading attributes " + attributes + " of " + this;

                  if (s_logger.isDumpEnabled())
                  {
                     StackTrace t = new StackTrace();

                     m_context.getMachine().updateStackTrace(t);
                     s_logger.dump(sMsg, t);
                  }
                  else
                  {
                     s_logger.debug(sMsg);
                  }
               }

               if (query.read().isEmpty())
               {
                  if (bSafe)
                  {
                     return attributes;
                  }
View Full Code Here

      if (where instanceof Collection)
      {
         return read(metaclass, attributes, (Collection)where, 128);
      }

      Query query = Query.createRead(metaclass, attributes, where, null, -1, 0, false, Query.SEC_NONE, m_context);

      query.setLimit(-1);
      query.reduce();

      return query.read();
   }
View Full Code Here

      m_query.visit(new Query.Visitor()
      {
         public boolean visit(final Query query)
         {
            boolean bHomogeneous = (query.getRoot() == m_query);
            Query primarySource = query, fieldSource = null;
            SQLJoin primaryJoin, fieldJoin = null;
            Index primaryKey, fieldKey = null;

            if (query.isRoot() && bHomogeneous)
            {
               primaryJoin = addTable(query, ((RelationalMapping)query.getPersistenceMapping()).getPrimaryTable());
               primaryJoin.isInner = true;
               primaryJoin.isEnabled = true;
               primaryKey = primaryJoin.table.getPrimaryKey();

               if (query.isJoin())
               {
                  Index destinationKey = (Index)query.getKey(true);
                  int nDKColumnCount = destinationKey.getIndexColumnCount();
                  Field[] dkFieldArray = new Field[nDKColumnCount];
                  SQLJoin join = addTable(query, destinationKey.getTable());

                  join.isInner = true;
                  join.isEnabled = true;

                  for (int i = 0; i < nDKColumnCount; ++i)
                  {
                     Column column = destinationKey.getIndexColumn(i).getColumn();

                     addOutputField(dkFieldArray[i] = new Field(query, join, column,
                        column.getValueType(), m_adapter.getConverter(column.getValueType(), column),
                        m_adapter.getBind(column), true));
                  }

                  query.setChildItem(dkFieldArray);
               }
            }
            else
            {
               Index sourceKey = (Index)query.getKey(false);
               SQLJoin parent;

               if (sourceKey.isObjectKey())
               {
                  parent = (SQLJoin)query.getParent().getMapping();
                  sourceKey = parent.table.getPrimaryKey();
               }
               else
               {
                  parent = addTable(query.getParent(), sourceKey.getTable());
               }

               parent.isEnabled = true;

               if (bHomogeneous)
               {
                  Index destinationKey = (Index)query.getKey(true);
                  SQLJoin join = addTable(query, destinationKey.getTable());

                  join.parent = parent;
                  join.sourceKey = sourceKey;
                  join.destinationKey = destinationKey;

                  // Decide whether we can use the left joined table
                  // source key instead of the right joined table primary key
                  // to reduce the number of indirections in the SQL query
                  // TODO: Traverse the parent list up for more potential savings

                  if (query.getWhere() == null &&
                     (destinationKey.isObjectKey() &&
                        (!query.isOutput() ||
                           (!sourceKey.isObjectKey() || query.isRequired()) &&
                           query.getFieldCount() == 0 &&
                           query.getAssocCount(Query.ASSOC_QUERY) == 0) ||
                     destinationKey.isObjectKeyPart() &&
                        !query.isOutput() &&
                        query.getFieldCount() == 0 &&
                        query.getAssocCount(Query.ASSOC_QUERY) == 0))
                  {
                     primarySource = query.getParent();
                     primaryJoin = parent;
                     primaryKey = sourceKey;
                  }
                  else
                  {
                     primaryJoin = join;
                     primaryKey = join.table.getPrimaryKey();

                     if (destinationKey.isObjectKeyPart() &&
                        (!sourceKey.isObjectKey() || query.isRequired()))
                     {
                        fieldSource = query.getParent();
                        fieldJoin = parent;
                        fieldKey = sourceKey;
                     }
                  }

                  if (join.parent.isInner && query.isRequired())
                  {
                     join.isInner = true;
                  }
               }
               else
               {
                  primarySource = query.getParent();
                  primaryJoin = parent;
                  primaryKey = sourceKey;
               }
            }

            primaryJoin.isEnabled = true;

            int nPKColumnCount = primaryKey.getIndexColumnCount();
            Field[] pkFieldArray = new Field[nPKColumnCount];

            for (int i = 0; i < nPKColumnCount; ++i)
            {
               Column column = primaryKey.getIndexColumn(i).getColumn();

               pkFieldArray[i] = new Field(primarySource, primaryJoin, column,
                  column.getValueType(), m_adapter.getConverter(column.getValueType(), column),
                  m_adapter.getBind(column), true);
            }

            if (query.getParent() != null && query.getParentItem() == null)
            {
               query.setParentItem(pkFieldArray);
            }

            if (bHomogeneous || (query.getRestriction() & Query.RESTRICTION_WHERE) != 0)
            {
               // The following conditional relies on the visit order
               // to avoid overwriting the PK in a hetero query
               if (query.getItem() == null)
               {
                  query.setItem(pkFieldArray);

                  if (fieldSource != null)
                  {
                     int nColumnCount = fieldKey.getIndexColumnCount();
                     Field[] fieldArray = new Field[nColumnCount];

                     for (int i = 0; i < nColumnCount; ++i)
                     {
                        Column column = fieldKey.getIndexColumn(i).getColumn();

                        fieldArray[i] = new Field(primarySource, fieldJoin, column,
                           column.getValueType(), m_adapter.getConverter(column.getValueType(), column),
                           m_adapter.getBind(column), true);
                     }

                     query.setFieldItem(fieldArray);
                  }
                  else
                  {
                     query.setFieldItem(pkFieldArray);
                  }
               }
            }

            if (query.isOutput() ||
               !bHomogeneous && (query.getRestriction() & Query.RESTRICTION_WHERE) == 0)
            {
               if (query.isIdentity() || !query.isOutput())
               {
                  // Output the primary key columns
                  for (int i = 0; i < nPKColumnCount; ++i)
                  {
                     addOutputField(pkFieldArray[i]);
                  }
               }

               m_query.addOutputQuery(query);
            }

            if (bHomogeneous)
            {
               // Bind the columns to the attributes
               for (Iterator itr = query.getFieldIterator(); itr.hasNext();)
               {
                  Field field = (Field)itr.next();

                  if (!field.isPersistent())
                  {
                     continue;
                  }

                  Operator op = field.getOperator();

                  if (op != null)
                  {
                     if (op.getType() instanceof Primitive)
                     {
                        field.setBind(m_adapter.getBind((Primitive)op.getType()));
                     }
                  }
                  else
                  {
                     Column column = ((RelationalPrimitiveMapping)field.getAttributeMapping()).getColumn();
                     SQLJoin join = addTable(query, column.getTable());

                     join.isEnabled = true;
                     field.setMapping(join);
                     field.setItem(column);
                     field.setConverter(m_adapter.getConverter((Primitive)field.getAttribute().getType(), column));
                     field.setBind(m_adapter.getBind(column));
                  }

                  // Add the output columns to the join
                  if (field.isOutput() && field.getBind() != null)
                  {
                     addOutputField(field);
                  }
               }

               assert query.getMapping() != null;
            }

            return true;
         }

         public boolean postVisit(final Query query)
         {
            if (query.isMatch()) // only postprocess operators if any require it
            {
               opVisit(query);
            }

            // Determine if a subquery should be generated
            Operator where = query.getWhere();

            if (where != null && !query.isRoot() && !query.isQuantorRoot())
            {
               for (Iterator itr = query.getAssocIterator(Query.ASSOC_WHERE); itr.hasNext();)
               {
                  Query assoc = (Query)itr.next();

                  if (assoc.isSameRoot(query))
                  {
                     SQLJoin join = (SQLJoin)assoc.getMapping();

                     do
                     {
                        if (join.isEnabled)
                        {
View Full Code Here

    * @param conj Where clause conjunction of SQLJoin and Operator. Can be null to lazy-allocate.
    * @return The where clause conjunction. Can be null.
    */
   protected Object[] appendReadSQL(final StringBuffer buf, final Query query, final int nSel, Object[] conj)
   {
      Query subquerySaved = m_subquery;
      boolean bGenWhere = (nSel != SEL_SUB ^ query.isSubquery());

      if (nSel == SEL_MAIN)
      {
         m_adapter.appendPrefixHint(buf, query);
View Full Code Here

         }
      }

      for (Iterator itr = query.getAssocIterator(Query.ASSOC_QUERY); itr.hasNext();)
      {
         Query assoc = (Query)itr.next();

         if (assoc.isSameRoot(query))
         {
            SQLJoin assocJoin = (SQLJoin)assoc.getMapping();

            appendSubqueryOutputKey(buf, assocJoin.parent, assocJoin.sourceKey);
         }
         else
         {
            pkFieldArray = (Field[])assoc.getParentItem();

            for (int i = 0, nCount = pkFieldArray.length; i < nCount; ++i)
            {
               Field field = pkFieldArray[i];
               SQLJoin mapping = (SQLJoin)field.getMapping();
View Full Code Here

    * @param join The join providing the column.
    * @param column The column.
    */
   protected void appendColumn(StringBuffer buf, SQLJoin join, Column column)
   {
      Query query = join.query;

      if (!query.isSubquery() || query == m_subquery)
      {
         buf.append(join.alias);
         buf.append('.');
         buf.append(column.getQuotedName());
      }
      else
      {
         buf.append(SUBQUERY_PREFIX);
         buf.append(((SQLJoin)query.getMapping()).alias);
         buf.append('.');
         buf.append(join.alias);
         buf.append('_');
         buf.append(getAlias(column.getOrdinal()));
      }
View Full Code Here

      buf.append(" from ");

      for (Iterator itr = quantor.getQuantorQuery().getAssocIterator(quantor); itr.hasNext();)
      {
         Query query = (Query)itr.next();

         if (bFirst)
         {
            bFirst = false;
         }
         else
         {
            buf.append(", ");
         }

         conj = appendReadSQL(buf, query, SEL_EXIST, conj);
      }

      if (conj == null)
      {
         bFirst = true;
      }
      else
      {
         buf.append(" where ");
         appendJoinConjunction(buf, conj);
         conj = null;
         bFirst = false;
      }

      for (Iterator itr = quantor.getQuantorQuery().getAssocIterator(quantor); itr.hasNext();)
      {
         Query query = (Query)itr.next();

         for (SQLJoin join = (SQLJoin)query.getMapping(); join != null; join = join.next)
         {
            if (!join.isEnabled)
            {
               continue;
            }

            if (bFirst)
            {
               buf.append(" where ");
               bFirst = false;
            }
            else
            {
               buf.append(" and ");
            }

            appendJoinCondition(buf, join, false);

            if (query.getWhere() != null && join == query.getMapping())
            {
               buf.append(" and ");
               appendWhereCondition(buf, query.getWhere(), GEN_WHERE, true);
            }
         }
      }

      if (bWhere && quantor.getOperand().getType() == Primitive.BOOLEAN)
View Full Code Here

         {
            addColumns((Field)source, columnSet);
         }
         else
         {
            Query assoc = source.getQuery();

            if (assoc.getParent() == query)
            {
               Index index = (Index)query.getKey(false);

               for (int i = 0, n = index.getIndexColumnCount(); i < n; ++i)
               {
View Full Code Here

    */
   public Object[] getValues(OID oid, Source source) throws PersistenceException
   {
      if (source instanceof Query)
      {
         Query query = (Query)source;

         if (query.getAttribute() != null)
         {
            Key key = query.getKey(true);

            if (key.isObjectKeyPart())
            {
               Field[] fieldArray = getFields(source);
               int nCount = fieldArray.length;
View Full Code Here

TOP

Related Classes of nexj.core.persistence.Query$ComparisonOperatorFactory

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.