Package nexj.core.persistence

Examples of nexj.core.persistence.Query$Visitor


            }
         }
      }
      else
      {
         Query query = source.getQuery();

         if (query.getParent() != null)
         {
            Key key = query.getKey(false);

            if (key.isObjectKeyPart())
            {
               int nCount = key.getPartCount();
               Object[] valueArray = new Object[nCount];
View Full Code Here


            }

            // Stores raw data from composition-mapped attributes for use in VirtualCursor.internalComplete
            for (int nQuery = 1, nQueryCount = m_query.getOutputQueryCount(); nQuery < nQueryCount; nQuery++)
            {
               Query query = m_query.getOutputQuery(nQuery);
               Object generator = query.getGenerator();

               if (generator instanceof VirtualJoinTab)
               {
                  ((VirtualJoinTab)generator).addChildObjects(oid, tobj.findValue(query.getAttribute().getName()));
               }
            }
         }

         return true;
View Full Code Here

    */
   protected void completeQuery()
   {
      for (int nQuery = 1, nQueryCount = m_query.getOutputQueryCount(); nQuery < nQueryCount; nQuery++)
      {
         Query query = m_query.getOutputQuery(nQuery);
         Object generator = query.getGenerator();

         if (!(generator instanceof VirtualJoinTab))
         {
            continue;
         }

         VirtualJoinTab joinTab = (VirtualJoinTab)generator;

         for (Iterator itr = joinTab.getParentOIDIterator(); itr.hasNext(); )
         {
            OID parentOID = (OID)itr.next();
            List childList = joinTab.getChildObjects(parentOID);

            if (childList == null)
            {
               continue;
            }

            Set containerSet = query.getParentInstances(parentOID);

            // composition data are stored *in* a parent, so must have one and only one
            assert containerSet.size() == 1;

            Instance container = (Instance)containerSet.iterator().next();
            Cursor cursor = new VirtualCursor(query, childList.iterator())// creates Instances
            Attribute assoc = query.getAttribute();

            if (assoc.isCollection())
            {
               // Add composition instances to collection on parent
               Instance childInst;
               InstanceList childInstList = (InstanceList)container.getOldValueDirect(
                  assoc.getOrdinal());

               while ((childInst = cursor.next()) != null)
               {
                  childInstList.setLazy(false);
                  childInstList.add(childInst, InstanceList.DIRECT | InstanceList.REPLACE);
               }
            }
            else
            {
               assert childList.size() == 1;

               container.setOldValueDirect(assoc.getOrdinal(), cursor.next());

               assert cursor.next() == null;
            }
         }

         joinTab.clear();
         query.completeParentInstances();
      }

      // Process heterogeneous joins
      super.completeQuery();
   }
View Full Code Here

    * @param parent The parent query.
    * @return The composition query.
    */
   protected static Query getCompositionQuery(Attribute attribute, Query parent)
   {
      Query query = new Query((Metaclass)attribute.getType(), parent.getInvocationContext());

      query.setOutput(Query.OUTPUT_EAGER);

      if (parent.getSecurity() == Query.SEC_ALL)
      {
         query.setSecurity(Query.SEC_ALL);
      }

      if ((parent.getRestriction() & Query.RESTRICTION_PARENT) != 0)
      {
         query.addRestriction(parent.getRestriction() & ~Query.RESTRICTION_PARENT);
      }

      query.setCursor(false);
      query.prepare(false);

      return query;
   }
View Full Code Here

   public void testMatch() throws Exception
   {
      super.testMatch();

      Pair attributes = parse("(firstName (addresses city))");
      Query query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(and (match? (@ addresses city) '(\"Toronto\\\" \\\"Roma\") 0.0) (= classCode \"CON\"))"),
         null, -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals(0, query.read().size()); // test " escape
   }
View Full Code Here

         commit(context);
        
         // Read (NOT FOUND)
         assertNull(context.beginTransaction());
           
         Query query = Query.createRead(testFPA, null, parse("(= (@ id) \"" + sOidName + "\")"), null, 10, 0, false, Query.SEC_NONE, context);
         InstanceList resultList = query.read();
           
         assertEquals(0, resultList.getCount());
        
         commit(context);
View Full Code Here

         //Now try to do a query.
         assertNull(context.beginTransaction());
        
         Metaclass testFPA = metadata.getMetaclass("TestFPA");
         Object where = parse("(= (@ id) \"u\")");
         Query query = Query.createRead(testFPA, null, where, null, 10, 0, false, Query.SEC_NONE, context);
        
         InstanceList resultList = query.read();
        
         assertEquals(1, resultList.size());
        
         Instance result = resultList.getInstance(0);
        
View Full Code Here

         //Now try to do a query.
         assertNull(context.beginTransaction());
        
         Metaclass testFPA = metadata.getMetaclass("TestFPA");
         Object where = parse("(= (@ id) \"d\")");
         Query query = Query.createRead(testFPA, null, where, null, 10, 0, false, Query.SEC_NONE, context);
        
         InstanceList resultList = query.read();
        
         assertEquals(1, resultList.size());
        
         Instance result = resultList.getInstance(0);
        
View Full Code Here

   }

   public void testRead()
   {
      Pair attributes;
      Query query;

      // Lazy association type adjustment
      Instance instance = new Instance(getMetadata().getMetaclass("Principal"), true, m_context);

      instance.cache(new OID(new Object[]{Binary.parse("00000000000000000000000000000002")}));
      query = Query.createRead(getMetadata().getMetaclass("UserGroupAssoc"), parse("(user)"),
         parse("(= (@) (oid #z00000000000000000000000000000003))"),
         null, -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals("User", ((Instance)query.read().getInstance(0).getValue("user")).getLazyMetaclass().getName());
      m_context.removeInstance(instance);
      instance = null;

      // Unsupported where clause
      try
      {
         Query.createRead(getMetadata().getMetaclass("Contact"), null, parse("(> (myfun firstName) 0)"), null, -1, 0, false, Query.SEC_NONE, m_context);
         fail("Expected InvalidQueryException");
      }
      catch (InvalidQueryException e)
      {
         assertEquals("err.persistence.unsupportedExpression", e.getErrorCode());
      }
     
      attributes = parse("(firstName lastName (addresses country city street code) (user name))");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"),
         parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("read.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(and (null? (@ noAddresses city)) (= classCode \"CON\"))"),
         parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("read.xml"), query.read(), attributes);

      String sSQL = ((SQLGenerator)query.getGenerator()).getSQL();

      query = Query.createRead(getMetadata().getMetaclass("Contact"),
         parse("((user name) lastName (addresses country city street code) firstName)"),
         parse("(and (null? (@ noAddresses city)) (= classCode \"CON\"))"),
         parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("read.xml"), query.read(), attributes);

      assertEquals(sSQL, ((SQLGenerator)query.getGenerator()).getSQL());

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"),
         parse("((firstName . #t)(lastName . #t)((@) . #t)((@ addresses) . #t))"),
         2, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCount.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"),
         parse("((firstName . #t)(lastName . #t)((@) . #t)((@ addresses) . #t))"),
         2, 2, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readOffset.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(and (like? firstName \"J*\") (= lastName \"Smith\") (in? (@ readPrincipal name) \"users\" \"QA\" \"jsmith\")))"),
         parse("((firstName . #t)(lastName . #t)((@) . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readWhere.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(and (like? firstName \"J*\") (= lastName \"Smith\") (in? (@ readPrincipal name) \"users\" \"QA\" \"jsmith\" " +
                "\"1\" \"2\" \"3\" \"4\" \"5\" \"6\" \"7\" \"8\" \"9\" \"10\" \"11\" \"12\" \"13\" \"14\" \"15\" \"16\" " +
                "\"17\" \"18\" \"19\" \"20\" \"21\" \"22\" \"23\" \"24\" \"25\" \"26\" \"27\" \"28\" \"29\" \"30\")))"),
         parse("((firstName . #t)(lastName . #t)((@) . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readWhere.xml"), query.read(), attributes);

      List inList = new ArrayList(3000);

      for (int i = 0; i < 3000; ++i)
      {
         inList.add(String.valueOf(i));
      }

      inList.add("users");
      inList.add("QA");
      inList.add("jsmith");

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(and (like? firstName \"J*\") (= lastName \"Smith\"))")
            .and(Pair.attribute("readPrincipal name").in(new Pair(inList))),
         parse("((firstName . #t)(lastName . #t)((@) . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readWhere.xml"), query.read(), attributes);
      inList = null;

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes, parse("(= lastName ())"),
         parse("((firstName . #t)(lastName . #t)((@) . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NODE, m_context);
      AssertUtil.assertEquals(getURL("readWhereNull.xml"), query.read(), attributes);

      attributes = parse("(type country city)");
      query = Query.createRead(getMetadata().getMetaclass("Address"), attributes,
         new Pair(Symbol.EQ,
            new Pair(parse("(@@ Contact addresses)"),
            new Pair(new OID(new Object[]{Binary.parse("00000000000000000000000000000001")})))),
         parse("((type . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readReverse.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Address"), attributes,
         new Pair(Symbol.OR, new Pair(Boolean.FALSE, new Pair(
            new Pair(Symbol.EQ,
               new Pair(parse("(@@ User contact addresses)"),
                  new Pair(new OID(new Object[]{Binary.parse("00000000000000000000000000000001")}))))))),
         parse("((type . #t) ((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readReverse2.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Address"), attributes,
               parse("(= (@@ Contact businessAddress2) (oid #z00000000000000000000000000000001))"),
         parse("((type . #t) ((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readReverse3.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("BusinessAddress"),
         parse("(type country city street code)"),
         parse("(like? city \"T*\")"), parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readClassWhere.xml"), query.read(), attributes);

      attributes = parse("(firstName (addresses city))");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(and (like? (@ addresses city) \"T*\") (= classCode \"CON\"))"),
         parse("(((@ addresses city) . #t) ((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCollectionWhere.xml"), query.read(), attributes);

      attributes = parse("(firstName lastName (addresses country city street code) (businessAddress2 country city street code) (user name))");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"),
         parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readAssocWhere.xml"), query.read(), attributes);

      attributes = parse("(firstName lastName (qaUser name) (user name))");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"),
         parse("((firstName . #t)(lastName . #t))"), -1, 0, false, Query.SEC_NONE, m_context);

      InstanceList list = query.read();

      AssertUtil.assertEquals(getURL("readAssocWhere2.xml"), list, attributes);

      query.setMaxCount(1000000);

      Cursor cursor = query.openCursor();

      try
      {
         int i = 0;

         while ((instance = cursor.next()) != null)
         {
            assertSame(list.getInstance(i++), instance);
         }

         assertEquals(list.size(), i);
      }
      finally
      {
         cursor.close();
      }

      attributes = parse("(addresses)");
      cursor = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), null, -1, 0, false, Query.SEC_NONE, m_context).openCursor();

      try
      {
         cursor.next();
         cursor.next();
         instance = cursor.next();
         assertEquals(2, ((InstanceList)instance.getValue("addresses")).size());
      }
      finally
      {
         cursor.close();
      }
     
      attributes = parse("(fullName)");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(any (= (@ addresses city) \"Toronto\"))"),
         parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readAny.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(any (= (@ businessAddress3 city) \"Toronto\"))"),
         parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readAny2.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(any (= (@) ()))"), parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals(0, query.read().size());

      attributes = parse("(name typeCode)");
      query = Query.createRead(getMetadata().getMetaclass("Principal"), attributes, null,
         parse("((name . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readPolymorphic.xml"), query.read(), attributes);
     
      attributes = parse("(fullName)");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(instance? (@) Patient)"),
         parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readSubclass.xml"), query.read(), attributes);

      query  = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(and (instance? (@) Patient) (not (like? (@ lastName) \"[S]*\")))"),
         parse("((firstName . #t)(lastName . #t)((@ addresses) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readSubclass.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Address"), null,
         parse("(= typeEnum (oid \"address\" \"en\" \"Business\"))"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readEnum.xml"), query.read(), null);

      query = Query.createRead(getMetadata().getMetaclass("Address"), null,
         parse("(= (@ typeEnum type) \"Business\")"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readEnum.xml"), query.read(), null);

      query = Query.createRead(getMetadata().getMetaclass("Address"), null,
         parse("(and (= typeEnum (oid \"address\" \"en\" \"Business\")) (= (@ typeEnum type) \"Business\"))"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readEnum.xml"), query.read(), null);

      query = Query.createRead(getMetadata().getMetaclass("Address"), null,
         parse("(and (= typeEnum (oid \"address\" \"en\" \"Business\")) (= (@ typeEnum type) \"1\"))"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals(0, query.read().getCount());

      query = Query.createRead(getMetadata().getMetaclass("ExternalVisit"), null,
         parse("(= patient (oid #z00000000000000000000000000000006))"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readExternal.xml"), query.read(), null);

      query = Query.createRead(getMetadata().getMetaclass("Patient"), parse("(externalVisits)"),
         parse("(and (= firstName \"Sarah\") (= lastName \"Johnson\"))"),
         null, -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals(3, ((InstanceList)query.read().getInstance(0).getValue("externalVisits")).size());

      // Comparison of LOB values, class is not important as long as the attribute is LOB

      Query.createRead(getMetadata().getMetaclass("SysRule"), parse("(name)"),
         parse("(= action \"test\")"),
         null, -1, 0, false, Query.SEC_NONE, m_context).read();
      Query.createRead(getMetadata().getMetaclass("SysRule"), parse("(name)"),
         parse("(not (= action \"test\"))"),
         null, -1, 0, false, Query.SEC_NONE, m_context).read();
      Query.createRead(getMetadata().getMetaclass("SysTimer"), parse("(period)"),
         parse("(= data #z0)"),
         null, -1, 0, false, Query.SEC_NONE, m_context).read();
      Query.createRead(getMetadata().getMetaclass("SysTimer"), parse("(period)"),
         parse("(not (= data #z0))"),
         null, -1, 0, false, Query.SEC_NONE, m_context).read();

      // Dynamic derived associations

      attributes = parse("(contact)");
      query = Query.createRead(getMetadata().getMetaclass("Address"), attributes,
         parse("(!= (@ contact (instance? (@) Patient) birthdate) ())"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readDynDerived.xml"), query.read(), attributes);

      attributes = parse("(lastName)");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(!= (@ (instance? (@) Patient) birthdate) ())"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readDynTypeCast.xml"), query.read(), attributes);

      list = Query.createRead(getMetadata().getMetaclass("Contact"), parse("(lastName)"),
         parse("(!= (@ addresses (= (@ country) \"Canada\") contact (= (@ firstName) \"Joe\") addresses) ())"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context).read();
      assertEquals(1, list.size());
      assertEquals("Joe", list.getInstance(0).getValue("firstName"));

      query = Query.createRead(getMetadata().getMetaclass("Visit"), null,
         parse("(= (@@ Address contact (instance? (@) Patient) visits) (oid #z00000000000000000000000000000008))"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readDynReverse.xml"), query.read(), null);

      // Calculated attributes
      attributes = parse("(isTorontoBasedEmployee tax lastNameLengthPlus1 lastNameInitial)");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCalculated.xml"), query.read(), attributes);

      attributes = parse("((businessAddress3 deletable))");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCalculatedSubq.xml"), query.read(), attributes);

      Query.createRead(getMetadata().getMetaclass("Patient"), parse("(visibleDescription)"),
         null, null, 1, 0, false, Query.SEC_NONE, m_context);

      list = Query.createRead(getMetadata().getMetaclass("Contact"), null,
         Pair.attribute("visible"), null, -1, 0, false, Query.SEC_NONE, m_context).read();

      for (int i = 0; i < list.size(); ++i)
      {
         assertEquals(Boolean.TRUE, list.getInstance(i).getValue("visible"));
      }

      assertEquals(2, Query.createRead(getMetadata().getMetaclass("Contact"), null,
         parse("(and (= classCode \"CON\") (> (/ businessAddressCount 2) 0))"), null, -1, 0, false,
         Query.SEC_NONE, m_context).read().size());

      assertEquals(2, Query.createRead(getMetadata().getMetaclass("Address"), null,
         parse("(= (@@ User addresses) (oid #z00000000000000000000000000000001))"), null, -1, 0, false,
         Query.SEC_NONE, m_context).read().size());

      // Annotations
      attributes = parse("(firstName (: _fnLen (string-length (@ firstName))) (: _n 123) (: _ln (@ lastName)) (: _x2 (let ((x 2)) (* x x))) (: _f (lambda (x) 1)))");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readAnnotations.xml"), query.read(), attributes);

      // Annotated queries with cached classes (no caching)
      attributes = parse("((: l (string-length (@ name))))");
      query = Query.createRead(getMetadata().getMetaclass("Country"), attributes,
         null, parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readAnnotationsCached.xml"), query.read(), attributes);

      // Aggregate functions
      attributes = parse("(firstName lastName addressCount (: _countryCnt (count (@ addresses country))) " +
         "(: _uniqueCountryCnt (count (unique (@ addresses country)))) (: _nullCodeCnt (count (null? (@ addresses code)))) " +
         "(: _avg (average (string-length (@ addresses city)))) (: _min (minimum (string-length (@ addresses city)))) " +
         "(: _max (maximum (@ addresses city))) (: _sum (sum (string-length (@ addresses city)))))");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), parse("(((@) . #t))"),
         -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readAggregate.xml"), query.read(), attributes);

      // Aggregate queries
      list = Query.createAggregate(getMetadata().getMetaclass("Contact"), null,
         parse("(= classCode \"CON\")"), null, null, null, -1, 0, Query.SEC_NONE, m_context).read();
      assertEquals(1, list.size());
      instance = list.getInstance(0);
      assertEquals("Object", instance.getClassName());
      assertNull(instance.getOID());

      attributes = parse("((: c (count (@))) (: n 1))");
      query = Query.createAggregate(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), null, null, null, -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByCountAll.xml"), query.read(), attributes);

      attributes = parse("((: c (* 2 (count (@)))))");
      query = Query.createAggregate(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), parse("((@))"), null, parse("(((@) . #t))"), -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByIndentity.xml"), query.read(), attributes);

      query = Query.createAggregate(getMetadata().getMetaclass("Contact"), parse("(classCode (: c (* 2 (count (@)))) (: n 1))"),
         parse("(= classCode \"CON\")"), parse("(classCode)"), null, parse("((classCode . #t))"),
         -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByAttribute.xml"), query.read(),
         parse("((: classCode (@ classCode)) (: c (* 2 (count (@)))) (: n 1))"));

      attributes = parse("((: fnl (string-length (@ firstName))))");
      query = Query.createAggregate(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), parse("(classCode (string-length (@ firstName)))"), null,
         parse("((classCode . #t) ((string-length (@ firstName)) . #t))"), -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByExpr.xml"), query.read(), attributes);

      attributes = parse("((: c (* 2 (count (@)))) (: ac (count (@ addresses))))");
      query = Query.createAggregate(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), parse("((@))"), null,
         parse("(((@) . #t))"), -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByIdentityCountAssoc.xml"), query.read(), attributes);

      attributes = parse("((: classCode (@ classCode)) (: c (* 2 (count (@)))) (: ac (count (@ addresses))))");
      query = Query.createAggregate(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), parse("(classCode)"), parse("(> (count (@ addresses)) 1)"),
         parse("((classCode . #t))"), -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByCountAssoc.xml"), query.read(), attributes);

      attributes = parse("((: c (* 2 (count (@)))) (: a (@ addresses)))");
      query = Query.createAggregate(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), parse("((@) addresses)"), null,
         parse("((addresses . #t) ((@) . #t))"), -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByAssoc.xml"), query.read(), attributes);

      query = Query.createAggregate(getMetadata().getMetaclass("Contact"),
         parse("(firstName (: c (count (@))))"),
         parse("(= classCode \"CON\")"), parse("(firstName)"), null,
         parse("((firstName . #t))"), -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByCase.xml"), query.read(),
         parse("((: firstName (@ firstName)) (: c (count (@))))"));

      attributes = parse("((: c (* 2 (count (@)))))");
      query = Query.createAggregate(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= classCode \"CON\")"), parse("(addresses)"), null,
         parse("((addresses . #t))"), -1, 0, Query.SEC_NONE, m_context);
      cursor = query.openCursor();

      try
      {
         AssertUtil.assertEquals(getURL("readGroupByCursorCol.xml"), cursor.next(1024), attributes);
      }
      finally
      {
         cursor.close();
      }

      // Aggregate queries with cached classes (no caching)

      query = Query.createAggregate(getMetadata().getMetaclass("Country"), null,
         null, null, null, null, -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByCachedDefault.xml"), query.read(), null);

      attributes = parse("((: c (count (@))))");
      query = Query.createAggregate(getMetadata().getMetaclass("Country"), attributes,
         null, null, null, null, -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByCachedCountAll.xml"), query.read(), attributes);

      attributes = parse("((: m (maximum (@ countryEnum name))))");
      query = Query.createAggregate(getMetadata().getMetaclass("Address"), attributes,
         null, parse("((@))"), null, parse("(((@) . #t))"), -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByCachedAssoc.xml"), query.read(), attributes);

      attributes = parse("((: c (count (@))))");
      query = Query.createAggregate(getMetadata().getMetaclass("Address"), attributes,
         null, parse("((@ countryEnum name))"), null, parse("(((@ countryEnum name) . #t))"), -1, 0, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readGroupByAssocCached.xml"), query.read(), attributes);

      // Nested aggregate functions
      try
      {
         Query.createAggregate(getMetadata().getMetaclass("Contact"), parse("((: ac (minimum (count (@ addresses)))))"),
            null, parse("((@))"), null, null, -1, 0, Query.SEC_NONE, m_context);
         fail("Expected InvalidQueryException");
      }
      catch (InvalidQueryException e)
      {
         assertEquals("err.persistence.nestedAggregate", e.getErrorCode());
      }

      // Unsupported expression
      try
      {
         Query.createAggregate(getMetadata().getMetaclass("Contact"), parse("((: fnl (sin (string-length (@ firstName)))))"),
            null, parse("(classCode (sin (string-length (@ firstName))))"), null, null, -1, 0, Query.SEC_NONE, m_context);
         fail("Expected InvalidQueryException");
      }
      catch (InvalidQueryException e)
      {
         assertEquals("err.persistence.unsupportedExpression", e.getErrorCode());
      }

      // Invalid having clause
      try
      {
         Query.createAggregate(getMetadata().getMetaclass("Contact"), null, null,
            parse("(addresses)"), parse("(> (@) (oid #z1))"), null, -1, 0, Query.SEC_NONE, m_context).read();
         fail("Expected InvalidQueryException");
      }
      catch (InvalidQueryException e)
      {
         assertEquals("err.persistence.ungroupedHaving", e.getErrorCode());
      }

      // Query macros
      assertEquals(2, Query.createRead(getMetadata().getMetaclass("Address"), null,
         parse("(and isQuery (= contact (oid #z00000000000000000000000000000001)))"),
         null, -1, 0, false, Query.SEC_NONE, m_context).read().size());

      assertEquals("R", Query.createRead(getMetadata().getMetaclass("Address"), parse("(cityq)"),
         parse("(= contact (oid #z00000000000000000000000000000001))"),
         parse("((city . #t))"), -1, 0, false, Query.SEC_NONE, m_context).read().getInstance(0).getValue("cityq"));

      assertEquals("Richmond Hill", ((InstanceList)Query.createRead(getMetadata().getMetaclass("Contact"), parse("((addresses cityq))"),
         parse("(= (@) (oid #z00000000000000000000000000000001))"),
         parse("(((@ addresses city) . #t))"), -1, 0, false, Query.SEC_NONE, m_context)
         .read().getInstance(0).getValue("addresses")).getInstance(0).getValue("cityq"));

      // Class caching
      attributes = parse("(name)");
      query = Query.createRead(getMetadata().getMetaclass("Country"), attributes,
         parse("(= name \"Canada\")"), null, -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedInstance.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Country"), attributes,
         parse("(= name \"Canada\")"), null, -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedInstance.xml"), query.read(), attributes);

      // Instance caching
      attributes = parse("(name country)");
      query = Query.createRead(getMetadata().getMetaclass("City"), attributes,
         parse("(= name \"Toronto\")"), null, -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedClass.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("City"), attributes,
         parse("(= name \"Toronto\")"), null, -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedClass.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("City"), attributes,
         parse("(= (@) (oid \"Toronto\"))"), null, -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedClass.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("City"), attributes,
         parse("(= (@) (oid \"Toronto1\"))"), null, -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals(0, query.read().size());

      query = Query.createRead(getMetadata().getMetaclass("City"), attributes,
         parse("(like? name \"Toro*\")"), null, -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedClass.xml"), query.read(), attributes);

      attributes = parse("((countryEnum name) (cityEnum name country))");
      query = Query.createRead(getMetadata().getMetaclass("Address"), attributes,
         null, parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedJoin.xml"), query.read(), attributes);

      query = Query.createRead(getMetadata().getMetaclass("Address"), attributes,
         null, parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedJoin.xml"), query.read(), attributes);

      attributes = parse("((primaryAddress cityEnum))");
      query = Query.createRead(getMetadata().getMetaclass("Contact"), attributes,
         parse("(= (@ primaryAddress cityEnum name) \"Richmond Hill\")"),
         parse("(((@) . #t))"), -1, 0, false, Query.SEC_NONE, m_context);
      AssertUtil.assertEquals(getURL("readCachedMix.xml"), query.read(), attributes);
     
      // Wrong subclass
      query = Query.createRead(getMetadata().getMetaclass("Patient"), null,
         parse("(= (@) (oid #z00000000000000000000000000000001))"), null, -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals(0, query.read().size());

      // Reverse assoc subclass, just create the query
      query = Query.createRead(getMetadata().getMetaclass("BusinessAddress"), null,
         parse("(= (@@ Contact addresses) '())"), null, -1, 0, false, Query.SEC_NONE, m_context);
     
      query = Query.createRead(getMetadata().getMetaclass("Principal"), null,
         parse("(= (@@ User) ())"), null, -1, 0, false, Query.SEC_NONE, m_context);

      // Overriden attribute type, just create the query
      query = Query.createRead(getMetadata().getMetaclass("Patient"), parse("((businessAddress2 main))"),
         null, null, -1, 0, false, Query.SEC_NONE, m_context);

      // SysReader with dispatch

      Instance r1 = new Instance(getMetadata().getMetaclass("SysReader"), Instance.NEW, m_context);

      r1.setValue("class", Symbol.define("Principal"));
      r1.setValue("attributes", parse("(name)"));
      r1.setValue("where", parse("(= name \"users\")"));
      r1.invoke("read");

      Instance r2 = new Instance(getMetadata().getMetaclass("SysReader"), Instance.NEW, m_context);

      r2.setValue("class", Symbol.define("UserGroupAssoc"));
      r2.setValue("attributes", parse("(user group)"));
      r2.setValue("orderBy", parse("(((@) . #t))"));
     
      InstanceList p = new InstanceArrayList();
      p.add(r1);
      r2.setValue("parents", p);
     
      List a = new ArrayList();
      a.add(parse("(ugassocs)"));
      r2.setValue("associations", a);
      r2.setValue("dispatchParent", r1);
      r2.setValue("dispatchAttribute", null);
      r2.setValue("dispatchValues", parse("(Group)"));
     
      AssertUtil.assertEquals(getURL("readFMDispatch1.xml"), r2.invoke("read"), parse("(user group)"));

      r2 = new Instance(getMetadata().getMetaclass("SysReader"), Instance.NEW, m_context);

      r2.setValue("class", Symbol.define("UserGroupAssoc"));
      r2.setValue("attributes", parse("(user group)"));
      r2.setValue("orderBy", parse("(((@) . #t))"));

      p = new InstanceArrayList();
      p.add(r1);
      r2.setValue("parents", p);

      a = new ArrayList();
      a.add(parse("(ugassocs)"));
      r2.setValue("associations", a);
      r2.setValue("dispatchParent", r1);
      r2.setValue("dispatchAttribute", parse("(typeCode)"));
      r2.setValue("dispatchValues", parse("(\"G\")"));

      AssertUtil.assertEquals(getURL("readFMDispatch2.xml"), r2.invoke("read"), parse("(user group)"));

      Instance r3 = new Instance(getMetadata().getMetaclass("SysReader"), Instance.NEW, m_context);

      r3.setValue("class", Symbol.define("Principal"));
      r3.setValue("attributes", parse("(name)"));
      r3.setValue("orderBy", parse("(((@ name) . #t))"));
     
      p = new InstanceArrayList();
      p.add(r1);
      r3.setValue("parents", p);
     
      a = new ArrayList();
      a.add(null);
      r3.setValue("associations", a);
      r3.setValue("dispatchParent", r1);
      r3.setValue("dispatchAttribute", null);
      r3.setValue("dispatchValues", parse("(Principal)"));
      r3.setValue("dispatchGroup", r2);
      r3.setValue("dispatched", Boolean.FALSE);
     
      r2.setValue("dispatched", Boolean.TRUE);

      AssertUtil.assertEquals(getURL("readFMDispatch3.xml"), r3.invoke("read"), parse("(name)"));
      assertEquals(Boolean.TRUE, r2.getValue("dispatched"));
      assertEquals(Boolean.FALSE, r3.getValue("dispatched"));
     
      r2.setValue("dispatched", Boolean.FALSE);

      AssertUtil.assertEquals(getURL("readFMDispatch4.xml"), r3.invoke("read"), parse("(name)"));
      assertEquals(Boolean.TRUE, r2.getValue("dispatched"));
      assertEquals(Boolean.TRUE, r3.getValue("dispatched"));

      // nameAttribute
      assertEquals("Joe Test [Employee] {jtest}", m_context.getUser().getName());

      // Query CLOB field to see if it is null/not null
      // (Direct IS NULL queries on LOBs fail in Sybase 12.5.4)
      query = Query.createRead(getMetadata().getMetaclass("SysRule"), null,
         parse("(null? condition)"),
         null, -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals(2, query.read().getCount());

      query = Query.createRead(getMetadata().getMetaclass("SysRule"), null,
         parse("(not (null? condition))"),
         null, -1, 0, false, Query.SEC_NONE, m_context);
      assertEquals(4, query.read().getCount());     
   }
View Full Code Here

   }

   public void testLoad()
   {
      Metaclass metaclass = getMetadata().getMetaclass("Patient");
      Query query = Query.createRead(metaclass, null, parse("(= lastName \"Johnson\")"),
         null, -1, 0, false, Query.SEC_NONE, m_context);
      Instance instance = query.read().getInstance(0);
      Pair attributes = Pair.fromArray(Pair.toArray(parse("(type (@@ Contact fullName user) (@@ Contact user) (addresses))")));

      instance.invoke("load", attributes);

      String[] names = new String[]{"type" , "fullName", "user", "addresses"};

      for (int i = 0; i < names.length; ++i)
      {
         assertFalse(instance.getValueDirect(metaclass.getAttribute(names[i]).getOrdinal()) instanceof Undefined);
      }

      reset();

      instance = query.read().getInstance(0);
      instance.load(Pair.fromArray(Pair.toArray(attributes))); // copy the attributes as load() modifies the list

      for (int i = 0; i < names.length; ++i)
      {
         assertFalse(instance.getValueDirect(metaclass.getAttribute(names[i]).getOrdinal()) instanceof Undefined);
View Full Code Here

TOP

Related Classes of nexj.core.persistence.Query$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.