Package nexj.core.persistence

Examples of nexj.core.persistence.Cursor


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


            }
         }
         else if (attribute.isCollection())
         {
            // A composition attribute
            Cursor cursor = new VirtualCursor(getCompositionQuery(attribute, query), getIterator(value, metaclass));

            try
            {
               InstanceList instanceList = new InstanceArrayList();

               instanceList.setAssociation(instance, attribute, true);
               instanceList.setLazy(false);
               instance.setOldValueDirect(nOrdinal, instanceList);

               Instance assoc;

               while ((assoc = cursor.next()) != null)
               {
                  if (!instanceList.contains(assoc))
                  {
                     instanceList.add(assoc, InstanceList.DIRECT);
                  }
               }
            }
            finally
            {
               cursor.close();
            }
         }
         else if (value instanceof TransferObject)
         {
            Cursor cursor = new VirtualCursor(getCompositionQuery(attribute, query), new SingletonIterator(value));

            try
            {
               instance.setOldValueDirect(nOrdinal, cursor.next());
            }
            finally
            {
               cursor.close();
            }
         }
         else if (value instanceof OID)
         {
            // Associate an instance, creating a lazy instance if not loaded
View Full Code Here

      assertEquals("Felis catus", inst.getValue("scientificName"));
      inst = list.getInstance(2);
      assertEquals("Beaver", inst.getValue("commonName"));

      // Read all with a cursor
      Cursor cur = Query.createRead(animalClass, null, null, null, -1, 0, false, Query.SEC_ALL, m_context).openCursor();

      inst = cur.next();
      assertEquals("Dog", inst.getValue("commonName"));
      inst = cur.next();
      assertEquals("Cat", inst.getValue("commonName"));
      inst = cur.next();
      assertEquals("Spider", inst.getValue("commonName"));
      inst = cur.next();
      assertEquals("Beaver", inst.getValue("commonName"));
      assertNull(cur.next());

      // Read all with a cursor, canceling early
      cur = Query.createRead(animalClass, null, null, null, -1, 0, false, Query.SEC_ALL, m_context).openCursor();

      inst = cur.next();
      assertEquals("Dog", inst.getValue("commonName"));
      cur.close();
      assertNull(cur.next());
      assertNull(cur.next());

      // Read class that has 2-part OID
      Metaclass penClass = m_metadata.getMetaclass("AnimalPen");

      list = (InstanceArrayList)penClass.invoke("read", new Object[] {
View Full Code Here

      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,
View Full Code Here

   public void testConcurrentResultSets()
   {
      Query query = Query.createRead(getMetadata().getMetaclass("Contact"), parse("(firstName)"),
                                     null, null, -1, 0, false, Query.SEC_NONE, m_context);
      Cursor cursor1 = query.openCursor();
      Cursor cursor2 = query.openCursor();

      assertTrue(cursor2.next(-1).size() > 0);
      assertTrue(cursor1.next(-1).size() > 0);
      cursor1.close();
      cursor2.close();
   }
View Full Code Here

TOP

Related Classes of nexj.core.persistence.Cursor

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.