db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattba...@apache.org
Subject cvs commit: jakarta-ojb/src/test/org/apache/ojb/odmg OQLOrOnForeignKeyTest.java
Date Tue, 07 Jan 2003 01:38:51 GMT
mattbaird    2003/01/06 17:38:51

  Added:       src/test/org/apache/ojb/odmg OQLOrOnForeignKeyTest.java
  Log:
  illustrates a SQL generation issue that is not fixed yet
  
  Revision  Changes    Path
  1.1                  jakarta-ojb/src/test/org/apache/ojb/odmg/OQLOrOnForeignKeyTest.java
  
  Index: OQLOrOnForeignKeyTest.java
  ===================================================================
  package org.apache.ojb.odmg;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import junit.framework.TestCase;
  import org.apache.ojb.broker.TestHelper;
  import org.odmg.Database;
  import org.odmg.Implementation;
  import org.odmg.OQLQuery;
  import org.odmg.Transaction;
  
  import java.util.Iterator;
  import java.util.List;
  
  /**
   * @author Matthew.Baird
   *
   * Illustrates a problem with OJB SQL Generation:
   *
   * 1. OJB will generate the following SQL when items are mapped to the same table:
   *
   * SELECT A0.FATHER_ID,A0.MOTHER_ID,A0.LASTNAME,A0.FIRSTNAME,A0.ID FROM
   * FAMILY_MEMBER A0 INNER JOIN FAMILY_MEMBER A2 ON A0.FATHER_ID=A2.ID
   * INNER JOIN FAMILY_MEMBER A1 ON A0.MOTHER_ID=A1.ID
   * WHERE A1.ID =  ?  OR  (A2.ID =  ? )
   *
   * When it should generate:
   * SELECT A0.FATHER_ID,A0.MOTHER_ID,A0.LASTNAME,A0.FIRSTNAME,A0.ID FROM
   * FAMILY_MEMBER A0
   * WHERE A0.FATHER_ID =  ?  OR  (A0.MOTHER_ID =  ? )
   *
   */
  public class OQLOrOnForeignKeyTest extends TestCase
  {
  
  	private String databaseName;
  
  	/**
  	 * Insert the method's description here.
  	 * Creation date: (24.12.2000 00:33:40)
  	 */
  	public OQLOrOnForeignKeyTest(String name)
  	{
  		super(name);
  	}
  
  	/**
  	 * Insert the method's description here.
  	 * Creation date: (06.12.2000 21:58:53)
  	 */
  	public void setUp()
  	{
  		databaseName = TestHelper.DEF_JCD_ALIAS;
  	}
  
  	/**
  	 * Insert the method's description here.
  	 * Creation date: (06.12.2000 21:59:14)
  	 */
  	public void tearDown() throws Exception
  	{
  
  		Implementation odmg = OJB.getInstance();
  
  		Database db = odmg.newDatabase();
  		db.open(databaseName, Database.OPEN_READ_WRITE);
  		Transaction tx = odmg.newTransaction();
  		tx.begin();
  		OQLQuery query = odmg.newOQLQuery();
  		query.create("select person from " + PersonImpl.class.getName());
  		List persons = (List) query.execute();
  		Iterator it = persons.iterator();
  		while (it.hasNext())
  		{
  			db.deletePersistent(it.next());
  		}
  		tx.commit();
  
  		tx = odmg.newTransaction();
  		tx.begin();
  		query = odmg.newOQLQuery();
  		query.create("select a from " + TestClassA.class.getName());
  		List As = (List) query.execute();
  		it = As.iterator();
  		while (it.hasNext())
  		{
  			db.deletePersistent(it.next());
  		}
  		tx.commit();
  		databaseName = null;
  
  	}
  
  	public void testOrReferenceOnSameTable() throws Exception
  	{
  		int jimmyID = 5;
  		int joeID = 6;
  		int motherID = 12;
  		int fatherID = 13;
  		PersonImpl jimmy = new PersonImpl();
  		PersonImpl joe = new PersonImpl();
  		PersonImpl father = new PersonImpl();
  		PersonImpl mother = new PersonImpl();
  
  		mother.setId(motherID);
  		father.setId(fatherID);
  		mother.setFirstname("mom");
  		father.setFirstname("dad");
  		jimmy.setMother(mother);
  		jimmy.setFirstname("jimmy");
  		jimmy.setId(jimmyID);
  
  		joe.setFather(father);
  		jimmy.setFirstname("joe");
  		jimmy.setId(joeID);
  
  
  		Implementation odmg = OJB.getInstance();
  
  		Database db = odmg.newDatabase();
  		db.open(databaseName, Database.OPEN_READ_WRITE);
  		Transaction tx = odmg.newTransaction();
  		tx.begin();
  		db.makePersistent(father);
  		db.makePersistent(mother);
  		db.makePersistent(jimmy);
  		db.makePersistent(joe);
  		tx.commit();
  
  		tx = odmg.newTransaction();
  		tx.begin();
  		OQLQuery query = odmg.newOQLQuery();
  		query.create("select person from " + PersonImpl.class.getName() +
  					 " where (mother.id=$1 or father.id=$2)");
  		query.bind(new Integer(motherID));
  		query.bind(new Integer(fatherID));
  		List persons = (List) query.execute();
  		assertTrue(persons.size() == 2);
  		tx.commit();
  
  	}
  
  	public void testOrReferenceOnDifferentTables() throws Exception
  	{
  
  		TestClassA a1 = new TestClassA();
  		TestClassA a2 = new TestClassA();
  
  		TestClassB b1 = new TestClassB();
  		TestClassB b2 = new TestClassB();
  		a1.setB(b1);
  		a2.setB(b2);
  
  		Implementation odmg = OJB.getInstance();
  
  		Database db = odmg.newDatabase();
  		db.open(databaseName, Database.OPEN_READ_WRITE);
  		Transaction tx = odmg.newTransaction();
  		tx.begin();
  		db.makePersistent(a1);
  		db.makePersistent(a2);
  		db.makePersistent(b1);
  		db.makePersistent(b2);
  		tx.commit();
  		tx = odmg.newTransaction();
  		tx.begin();
  		// get the right values
  		OQLQuery query = odmg.newOQLQuery();
  		query.create("select a from " + TestClassA.class.getName());
  		List As = (List) query.execute();
  		Iterator asIterator = As.iterator();
  		TestClassA temp = null;
  
  		temp = (TestClassA) asIterator.next();
  		String bID1 = temp.getB().getOid();
  		temp = (TestClassA) asIterator.next();
  		String bID2 = temp.getB().getOid();
  
  		query = odmg.newOQLQuery();
  		query.create("select a from " + TestClassA.class.getName() +
  					 " where (b.oid=$1 or b.oid=$2)");
  		query.bind(bID1);
  		query.bind(bID2);
  		As = (List) query.execute();
  		assertTrue(As.size() == 2);
  		tx.commit();
  
  	}
  }
  
  
  

Mime
View raw message