diff options
| author | Brian S. O'Neill <bronee@gmail.com> | 2007-10-26 21:24:43 +0000 | 
|---|---|---|
| committer | Brian S. O'Neill <bronee@gmail.com> | 2007-10-26 21:24:43 +0000 | 
| commit | 8a6e27686f0a891cf6c2370893abbde3325f7a98 (patch) | |
| tree | 580547ae01534e443d72af142f0fd9883e2bb873 /src | |
| parent | 924f0db8a087a3ae396ceeec88dc78d826a50f2a (diff) | |
Added support for outer joins.
Diffstat (limited to 'src')
4 files changed, 522 insertions, 1 deletions
diff --git a/src/test/java/com/amazon/carbonado/cursor/TestFilteredCursor.java b/src/test/java/com/amazon/carbonado/cursor/TestFilteredCursor.java index 6f78c42..53c3171 100644 --- a/src/test/java/com/amazon/carbonado/cursor/TestFilteredCursor.java +++ b/src/test/java/com/amazon/carbonado/cursor/TestFilteredCursor.java @@ -616,6 +616,14 @@ public class TestFilteredCursor extends TestCase {          assertEquals(2, matches.size());
          assertEquals(2, matches.get(0).getOrderItemID());
          assertEquals(3, matches.get(1).getOrderItemID());
 +
 +        // Outer join, done in the "correct" way.
 +        matches = items.query("itemPrice = ? & (order).orderTotal = ?")
 +            .with(5).with(100)
 +            .fetch().toList();
 +        assertEquals(2, matches.size());
 +        assertEquals(2, matches.get(0).getOrderItemID());
 +        assertEquals(3, matches.get(1).getOrderItemID());
      }
      public void testExistsWithParams() throws Exception {
 diff --git a/src/test/java/com/amazon/carbonado/filter/TestFilterExists.java b/src/test/java/com/amazon/carbonado/filter/TestFilterExists.java index 05aa535..07f2df4 100644 --- a/src/test/java/com/amazon/carbonado/filter/TestFilterExists.java +++ b/src/test/java/com/amazon/carbonado/filter/TestFilterExists.java @@ -144,7 +144,7 @@ public class TestFilterExists extends TestCase {          {
              Filter<Order> f1 = Filter.filterFor(Order.class, "!address(addressCity = ?)");
              assertFalse(f1 instanceof ExistsFilter);
 -            assertEquals(Filter.filterFor(Order.class, "address.addressCity != ?"), f1);
 +            assertEquals(Filter.filterFor(Order.class, "(address).addressCity != ?"), f1);
          }
      }
 diff --git a/src/test/java/com/amazon/carbonado/filter/TestOuterJoin.java b/src/test/java/com/amazon/carbonado/filter/TestOuterJoin.java new file mode 100644 index 0000000..5da01a3 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/filter/TestOuterJoin.java @@ -0,0 +1,164 @@ +/*
 + * Copyright 2007 Amazon Technologies, Inc. or its affiliates.
 + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 + * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 + *
 + * Licensed under the Apache License, Version 2.0 (the "License");
 + * you may not use this file except in compliance with the License.
 + * You may obtain a copy of the License at
 + *
 + *     http://www.apache.org/licenses/LICENSE-2.0
 + *
 + * Unless required by applicable law or agreed to in writing, software
 + * distributed under the License is distributed on an "AS IS" BASIS,
 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 + * See the License for the specific language governing permissions and
 + * limitations under the License.
 + */
 +
 +package com.amazon.carbonado.filter;
 +
 +import junit.framework.TestCase;
 +import junit.framework.TestSuite;
 +
 +import com.amazon.carbonado.MalformedFilterException;
 +import com.amazon.carbonado.stored.*;
 +
 +/**
 + * 
 + *
 + * @author Brian S O'Neill
 + */
 +public class TestOuterJoin extends TestCase {
 +    public static void main(String[] args) {
 +        junit.textui.TestRunner.run(suite());
 +    }
 +
 +    public static TestSuite suite() {
 +        return new TestSuite(TestOuterJoin.class);
 +    }
 +
 +    public TestOuterJoin(String name) {
 +        super(name);
 +    }
 +
 +    public void test_prime() {
 +        Filter<Order> filter = Filter.filterFor(Order.class, "orderID = ?");
 +        Filter<Order> filter2 = Filter.filterFor(Order.class, "orderID != ?");
 +
 +        assertTrue(filter2 == filter.not());
 +
 +        assertFalse(((PropertyFilter) filter).getChainedProperty().isOuterJoin(0));
 +        assertFalse(((PropertyFilter) filter2).getChainedProperty().isOuterJoin(0));
 +
 +        try {
 +            filter = Filter.filterFor(Order.class, "(orderID) = ?");
 +            fail();
 +        } catch (MalformedFilterException e) {
 +        }
 +
 +        filter = Filter.filterFor(Order.class, "(orderID = ?)");
 +        filter2 = Filter.filterFor(Order.class, "(orderID != ?)");
 +
 +        assertTrue(filter2 == filter.not());
 +
 +        filter = Filter.filterFor(Order.class, "((orderID = ?))");
 +        assertTrue(filter2 == filter.not());
 +
 +        try {
 +            filter = Filter.filterFor(Order.class, "((orderID) = ?)");
 +            fail();
 +        } catch (MalformedFilterException e) {
 +        }
 +
 +        filter = Filter.filterFor(Order.class, "!orderID = ?");
 +        assertTrue(filter2 == filter);
 +
 +        filter = Filter.filterFor(Order.class, "!(orderID = ?)");
 +        assertTrue(filter2 == filter);
 +
 +        filter = Filter.filterFor(Order.class, "((((!((orderID = ?))))))");
 +        assertTrue(filter2 == filter);
 +
 +        try {
 +            filter = Filter.filterFor(Order.class, "!(orderID) = ?");
 +            fail();
 +        } catch (MalformedFilterException e) {
 +        }
 +    }
 +
 +    public void test_shortChain() {
 +        Filter<Order> filter = Filter.filterFor(Order.class, "address.addressCity > ?");
 +        Filter<Order> filter2 = Filter.filterFor(Order.class, "(address).addressCity <= ?");
 +
 +        assertTrue(filter2 == filter.not());
 +
 +        assertFalse(((PropertyFilter) filter).getChainedProperty().isOuterJoin(0));
 +        assertFalse(((PropertyFilter) filter).getChainedProperty().isOuterJoin(1));
 +        assertTrue(((PropertyFilter) filter2).getChainedProperty().isOuterJoin(0));
 +        assertFalse(((PropertyFilter) filter2).getChainedProperty().isOuterJoin(1));
 +
 +        try {
 +            filter = Filter.filterFor(Order.class, "address.(addressCity) > ?");
 +            fail();
 +        } catch (MalformedFilterException e) {
 +        }
 +
 +        try {
 +            filter = Filter.filterFor(Order.class, "(address).(addressCity) > ?");
 +            fail();
 +        } catch (MalformedFilterException e) {
 +        }
 +
 +        filter = Filter.filterFor(Order.class, "(address.addressCity > ?)");
 +        assertTrue(filter2 == filter.not());
 +
 +        filter = Filter.filterFor(Order.class, "((address.addressCity > ?))");
 +        assertTrue(filter2 == filter.not());
 +
 +        filter2 = Filter.filterFor(Order.class, "((address).addressCity <= ?)");
 +        assertTrue(filter2 == filter.not());
 +
 +        filter2 = Filter.filterFor(Order.class, "(((address).addressCity <= ?))");
 +        assertTrue(filter2 == filter.not());
 +
 +        filter = Filter.filterFor(Order.class, "!(address).addressCity <= ?");
 +        assertTrue(filter2 == filter.not());
 +
 +        filter = Filter.filterFor(Order.class, "!((address).addressCity <= ?)");
 +        assertTrue(filter2 == filter.not());
 +    }
 +
 +    public void test_longChain() {
 +        Filter<OrderItem> filter = Filter.filterFor
 +            (OrderItem.class, "shipment.(order).(address).addressState = ?");
 +        Filter<OrderItem> filter2 = Filter.filterFor
 +            (OrderItem.class, "(shipment).order.address.addressState != ?");
 +
 +        assertTrue(filter2 == filter.not());
 +
 +        assertFalse(((PropertyFilter) filter).getChainedProperty().isOuterJoin(0));
 +        assertTrue(((PropertyFilter) filter).getChainedProperty().isOuterJoin(1));
 +        assertTrue(((PropertyFilter) filter).getChainedProperty().isOuterJoin(2));
 +        assertFalse(((PropertyFilter) filter).getChainedProperty().isOuterJoin(3));
 +
 +        assertTrue(((PropertyFilter) filter2).getChainedProperty().isOuterJoin(0));
 +        assertFalse(((PropertyFilter) filter2).getChainedProperty().isOuterJoin(1));
 +        assertFalse(((PropertyFilter) filter2).getChainedProperty().isOuterJoin(2));
 +        assertFalse(((PropertyFilter) filter2).getChainedProperty().isOuterJoin(3));
 +    }
 +
 +    public void test_complex() {
 +        Filter<OrderItem> filter = Filter.filterFor
 +            (OrderItem.class,
 +             "shipment.(order).(address).addressState = ? & " +
 +             "(shipment).(order).address.addressState > ?");
 +
 +        Filter<OrderItem> filter2 = Filter.filterFor
 +            (OrderItem.class,
 +             "(shipment).order.address.addressState != ? | " +
 +             "shipment.order.(address).addressState <= ?");
 +
 +        assertTrue(filter2 == filter.not());
 +    }
 +}
 diff --git a/src/test/java/com/amazon/carbonado/info/TestChainedProperty.java b/src/test/java/com/amazon/carbonado/info/TestChainedProperty.java new file mode 100644 index 0000000..959a974 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/info/TestChainedProperty.java @@ -0,0 +1,349 @@ +/*
 + * Copyright 2007 Amazon Technologies, Inc. or its affiliates.
 + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 + * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 + *
 + * Licensed under the Apache License, Version 2.0 (the "License");
 + * you may not use this file except in compliance with the License.
 + * You may obtain a copy of the License at
 + *
 + *     http://www.apache.org/licenses/LICENSE-2.0
 + *
 + * Unless required by applicable law or agreed to in writing, software
 + * distributed under the License is distributed on an "AS IS" BASIS,
 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 + * See the License for the specific language governing permissions and
 + * limitations under the License.
 + */
 +
 +package com.amazon.carbonado.info;
 +
 +import junit.framework.TestCase;
 +import junit.framework.TestSuite;
 +
 +import com.amazon.carbonado.*;
 +import com.amazon.carbonado.stored.*;
 +
 +/**
 + * 
 + *
 + * @author Brian S O'Neill
 + */
 +public class TestChainedProperty extends TestCase {
 +    public static void main(String[] args) {
 +        junit.textui.TestRunner.run(suite());
 +    }
 +
 +    public static TestSuite suite() {
 +        return new TestSuite(TestChainedProperty.class);
 +    }
 +
 +    public TestChainedProperty(String name) {
 +        super(name);
 +    }
 +
 +    public void test_parsePrime() {
 +        StorableInfo<Order> info = StorableIntrospector.examine(Order.class);
 +
 +        ChainedProperty<Order> p, p2;
 +
 +        p = ChainedProperty.parse(info, "orderID");
 +        assertEquals(info.getAllProperties().get("orderID"), p.getPrimeProperty());
 +        assertEquals(info.getAllProperties().get("orderID"), p.getLastProperty());
 +        assertEquals(long.class, p.getType());
 +        assertEquals(0, p.getChainCount());
 +        try {
 +            p.getChainedProperty(0);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +        assertFalse(p.isOuterJoin(0));
 +        try {
 +            p.isOuterJoin(1);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +
 +        assertEquals("orderID", p.toString());
 +
 +        p2 = ChainedProperty.parse(info, "    orderID");
 +        assertTrue(p == p2);
 +
 +        p = ChainedProperty.parse(info, "(orderID)");
 +        assertEquals(info.getAllProperties().get("orderID"), p.getPrimeProperty());
 +        assertEquals(info.getAllProperties().get("orderID"), p.getLastProperty());
 +        assertEquals(long.class, p.getType());
 +        assertEquals(0, p.getChainCount());
 +        try {
 +            p.getChainedProperty(0);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +        assertTrue(p.isOuterJoin(0));
 +        try {
 +            p.isOuterJoin(1);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +
 +        assertEquals("(orderID)", p.toString());
 +
 +        p2 = ChainedProperty.parse(info, "    (orderID ) ");
 +        assertTrue(p == p2);
 +    }
 +
 +    public void test_parseChained() {
 +        StorableInfo<Order> info = StorableIntrospector.examine(Order.class);
 +        StorableInfo<Address> info2 = StorableIntrospector.examine(Address.class);
 +
 +        ChainedProperty<Order> p, p2;
 +
 +        p = ChainedProperty.parse(info, "address.addressCity");
 +        assertEquals(info.getAllProperties().get("address"), p.getPrimeProperty());
 +        assertEquals(info2.getAllProperties().get("addressCity"), p.getLastProperty());
 +        assertEquals(String.class, p.getType());
 +        assertEquals(1, p.getChainCount());
 +        assertEquals(info2.getAllProperties().get("addressCity"), p.getChainedProperty(0));
 +        try {
 +            p.getChainedProperty(1);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +        assertFalse(p.isOuterJoin(0));
 +        assertFalse(p.isOuterJoin(1));
 +        try {
 +            p.isOuterJoin(2);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +
 +        assertEquals("address.addressCity", p.toString());
 +
 +        p2 = ChainedProperty.parse(info, "address   .    addressCity  ");
 +        assertTrue(p == p2);
 +
 +        p = ChainedProperty.parse(info, "(address).addressCity");
 +        assertEquals(info.getAllProperties().get("address"), p.getPrimeProperty());
 +        assertEquals(info2.getAllProperties().get("addressCity"), p.getLastProperty());
 +        assertEquals(String.class, p.getType());
 +        assertEquals(1, p.getChainCount());
 +        assertEquals(info2.getAllProperties().get("addressCity"), p.getChainedProperty(0));
 +        try {
 +            p.getChainedProperty(1);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +        assertTrue(p.isOuterJoin(0));
 +        assertFalse(p.isOuterJoin(1));
 +        try {
 +            p.isOuterJoin(2);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +
 +        assertEquals("(address).addressCity", p.toString());
 +
 +        p2 = ChainedProperty.parse(info, "(address )   .    addressCity  ");
 +        assertTrue(p == p2);
 +
 +        p = ChainedProperty.parse(info, "address.(addressCity)");
 +        assertEquals(info.getAllProperties().get("address"), p.getPrimeProperty());
 +        assertEquals(info2.getAllProperties().get("addressCity"), p.getLastProperty());
 +        assertEquals(String.class, p.getType());
 +        assertEquals(1, p.getChainCount());
 +        assertEquals(info2.getAllProperties().get("addressCity"), p.getChainedProperty(0));
 +        try {
 +            p.getChainedProperty(1);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +        assertFalse(p.isOuterJoin(0));
 +        assertTrue(p.isOuterJoin(1));
 +        try {
 +            p.isOuterJoin(2);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +
 +        assertEquals("address.(addressCity)", p.toString());
 +
 +        p2 = ChainedProperty.parse(info, " address    .    (  addressCity )  ");
 +        assertTrue(p == p2);
 +
 +        p = ChainedProperty.parse(info, "(address).(addressCity)");
 +        assertEquals(info.getAllProperties().get("address"), p.getPrimeProperty());
 +        assertEquals(info2.getAllProperties().get("addressCity"), p.getLastProperty());
 +        assertEquals(String.class, p.getType());
 +        assertEquals(1, p.getChainCount());
 +        assertEquals(info2.getAllProperties().get("addressCity"), p.getChainedProperty(0));
 +        try {
 +            p.getChainedProperty(1);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +        assertTrue(p.isOuterJoin(0));
 +        assertTrue(p.isOuterJoin(1));
 +        try {
 +            p.isOuterJoin(2);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +
 +        assertEquals("(address).(addressCity)", p.toString());
 +
 +        p2 = ChainedProperty.parse(info, " (address)    .    (  addressCity )  ");
 +        assertTrue(p == p2);
 +
 +        StorableInfo<OrderItem> info3 = StorableIntrospector.examine(OrderItem.class);
 +        StorableInfo<Shipment> info4 = StorableIntrospector.examine(Shipment.class);
 +
 +        ChainedProperty<OrderItem> p3 =
 +            ChainedProperty.parse(info3, "shipment.order.   address.addressState");
 +        assertEquals(info3.getAllProperties().get("shipment"), p3.getPrimeProperty());
 +        assertEquals(info2.getAllProperties().get("addressState"), p3.getLastProperty());
 +        assertEquals(String.class, p3.getType());
 +        assertEquals(3, p3.getChainCount());
 +        assertEquals(info4.getAllProperties().get("order"), p3.getChainedProperty(0));
 +        assertEquals(info.getAllProperties().get("address"), p3.getChainedProperty(1));
 +        assertEquals(info2.getAllProperties().get("addressState"), p3.getChainedProperty(2));
 +        try {
 +            p3.getChainedProperty(3);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +        assertFalse(p3.isOuterJoin(0));
 +        assertFalse(p3.isOuterJoin(1));
 +        assertFalse(p3.isOuterJoin(2));
 +        assertFalse(p3.isOuterJoin(3));
 +        try {
 +            p3.isOuterJoin(4);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +
 +        assertEquals("shipment.order.address.addressState", p3.toString());
 +
 +        p3 = ChainedProperty.parse(info3, "shipment.(order).(address  ).addressState");
 +        assertEquals(info3.getAllProperties().get("shipment"), p3.getPrimeProperty());
 +        assertEquals(info2.getAllProperties().get("addressState"), p3.getLastProperty());
 +        assertEquals(String.class, p3.getType());
 +        assertEquals(3, p3.getChainCount());
 +        assertEquals(info4.getAllProperties().get("order"), p3.getChainedProperty(0));
 +        assertEquals(info.getAllProperties().get("address"), p3.getChainedProperty(1));
 +        assertEquals(info2.getAllProperties().get("addressState"), p3.getChainedProperty(2));
 +        try {
 +            p3.getChainedProperty(3);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +        assertFalse(p3.isOuterJoin(0));
 +        assertTrue(p3.isOuterJoin(1));
 +        assertTrue(p3.isOuterJoin(2));
 +        assertFalse(p3.isOuterJoin(3));
 +        try {
 +            p3.isOuterJoin(4);
 +            fail();
 +        } catch (IndexOutOfBoundsException e) {
 +        }
 +
 +        assertEquals("shipment.(order).(address).addressState", p3.toString());
 +    }
 +
 +    public void test_append() {
 +        StorableInfo<OrderItem> info = StorableIntrospector.examine(OrderItem.class);
 +        StorableInfo<Shipment> info2 = StorableIntrospector.examine(Shipment.class);
 +        StorableInfo<Order> info3 = StorableIntrospector.examine(Order.class);
 +        StorableInfo<Address> info4 = StorableIntrospector.examine(Address.class);
 +
 +        ChainedProperty<OrderItem> p, p2, p3, p4;
 +
 +        p = ChainedProperty.get(info.getAllProperties().get("shipment"));
 +        p2 = p.append(info2.getAllProperties().get("order"), true);
 +        p3 = p2.append(info3.getAllProperties().get("address"), true);
 +        p4 = p3.append(info4.getAllProperties().get("addressState"), false);
 +
 +        assertEquals("shipment", p.toString());
 +        assertEquals("shipment.(order)", p2.toString());
 +        assertEquals("shipment.(order).(address)", p3.toString());
 +        assertEquals("shipment.(order).(address).addressState", p4.toString());
 +
 +        ChainedProperty<Order> p5 = ChainedProperty.parse(info3, "(address)");
 +        assertEquals(p3, p2.append(p5));
 +
 +        p5 = ChainedProperty.parse(info3, "(address).addressState");
 +        assertEquals(p4, p2.append(p5));
 +    }
 +
 +    public void test_trim() {
 +        StorableInfo<OrderItem> info = StorableIntrospector.examine(OrderItem.class);
 +
 +        ChainedProperty<OrderItem> p, p2, p3, p4;
 +
 +        p = ChainedProperty.parse(info, "shipment.(order).(address).addressState");
 +        p2 = p.trim();
 +        p3 = p2.trim();
 +        p4 = p3.trim();
 +
 +        assertEquals("shipment.(order).(address)", p2.toString());
 +        assertEquals("shipment.(order)", p3.toString());
 +        assertEquals("shipment", p4.toString());
 +
 +        try {
 +            p4.trim();
 +            fail();
 +        } catch (IllegalStateException e) {
 +        }
 +
 +        p = ChainedProperty.parse(info, "(shipment).order.address.(addressState)");
 +        p2 = p.trim();
 +        p3 = p2.trim();
 +        p4 = p3.trim();
 +
 +        assertEquals("(shipment).order.address", p2.toString());
 +        assertEquals("(shipment).order", p3.toString());
 +        assertEquals("(shipment)", p4.toString());
 +
 +        try {
 +            p4.trim();
 +            fail();
 +        } catch (IllegalStateException e) {
 +        }
 +    }
 +
 +    public void test_tail() {
 +        StorableInfo<OrderItem> info = StorableIntrospector.examine(OrderItem.class);
 +
 +        ChainedProperty<OrderItem> p;
 +        ChainedProperty p2, p3, p4;
 +
 +        p = ChainedProperty.parse(info, "shipment.(order).(address).addressState");
 +        p2 = p.tail();
 +        p3 = p2.tail();
 +        p4 = p3.tail();
 +
 +        assertEquals("(order).(address).addressState", p2.toString());
 +        assertEquals("(address).addressState", p3.toString());
 +        assertEquals("addressState", p4.toString());
 +
 +        try {
 +            p4.tail();
 +            fail();
 +        } catch (IllegalStateException e) {
 +        }
 +
 +        p = ChainedProperty.parse(info, "(shipment).order.address.(addressState)");
 +        p2 = p.tail();
 +        p3 = p2.tail();
 +        p4 = p3.tail();
 +
 +        assertEquals("order.address.(addressState)", p2.toString());
 +        assertEquals("address.(addressState)", p3.toString());
 +        assertEquals("(addressState)", p4.toString());
 +
 +        try {
 +            p4.tail();
 +            fail();
 +        } catch (IllegalStateException e) {
 +        }
 +    }
 +}
  | 
