OPENJPA-51 Incorrect SQL with syntax error for JPQL subqueries

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@676467 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Catalina Wei 2008-07-14 03:46:44 +00:00
parent 85b0b0b45b
commit b2812a4cce
5 changed files with 441 additions and 2 deletions

View File

@ -573,6 +573,59 @@ public class SelectImpl
else
_joinSyntax = _parent._joinSyntax;
}
if (_parent.getAliases() == null || _subPath == null)
return;
// resolve aliases for subselect from parent
Set<Map.Entry> entries = _parent.getAliases().entrySet();
for (Map.Entry entry : entries) {
Object key = entry.getKey();
Integer alias = (Integer) entry.getValue();
if (key.toString().indexOf(_subPath) != -1) {
if (_aliases == null)
_aliases = new HashMap();
_aliases.put(key, alias);
Object tableString = _parent.getTables().get(alias);
if (_tables == null)
_tables = new TreeMap();
_tables.put(alias, tableString);
_removedAliasFromParent.set(alias.intValue());
}
}
if (_aliases != null) {
// aliases moved into subselect should be removed from parent
entries = _aliases.entrySet();
for (Map.Entry entry : entries) {
Object key = entry.getKey();
Integer alias = (Integer) entry.getValue();
if (key.toString().indexOf(_subPath) != -1) {
_parent.removeAlias(key);
Object tableString = _parent.getTables().get(alias);
_parent.removeTable(alias);
}
}
}
}
public Map getAliases() {
return _aliases;
}
public void removeAlias(Object key) {
_aliases.remove(key);
}
public Map getTables() {
return _tables;
}
public void removeTable(Object key) {
_tables.remove(key);
}
public Select getFromSelect() {
@ -1645,8 +1698,13 @@ public class SelectImpl
return;
if (_parent._joins != null && !_parent._joins.isEmpty()) {
boolean removed = false;
if (!_removedAliasFromParent.isEmpty())
removed = _parent._joins.joins().removeAll(pj.joins());
if (!_removedAliasFromParent.isEmpty()) {
for (Iterator itr = pj.joins().iterator(); itr.hasNext();) {
Join jn = (Join) itr.next();
if (_aliases.containsValue(jn.getIndex1()))
removed = _parent._joins.joins().remove(jn);
}
}
if (!removed)
pj.joins().removeAll(_parent._joins.joins());
}

View File

@ -0,0 +1,120 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.openjpa.persistence.query;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.ArrayList;
@Entity
@Table(name="TCUSTOMER")
public class Customer {
@Embeddable
public static class CustomerKey implements Serializable {
public String countryCode;
public int id;
public CustomerKey(){}
public CustomerKey(String cc, int id){
countryCode=cc;
this.id=id;
}
public String toString() {
return countryCode+"/"+id;
}
@Override
public boolean equals(Object obj){
if (obj==this) return true;
if ( ! (obj instanceof CustomerKey) ) return false;
CustomerKey key = (CustomerKey)obj;
if (key.countryCode.equals(this.countryCode) &&
key.id==this.id) return true;
return false;
}
@Override
public int hashCode() {
return this.countryCode.hashCode()
^ this.id;
}
}
public enum CreditRating { POOR, GOOD, EXCELLENT };
@EmbeddedId
CustomerKey cid;
@Column(length=30)
@Basic
String name;
@Enumerated
@Basic
CreditRating creditRating;
@Version
long version;
@OneToMany(fetch=FetchType.EAGER, mappedBy="customer")
private Collection<Order> orders = new ArrayList<Order>();
public Customer() {}
public Customer(CustomerKey cid, String name, CreditRating rating){
this.cid=cid;
this.name=name;
this.creditRating=rating;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CreditRating getRating() {
return creditRating;
}
public void setRating(CreditRating rating) {
this.creditRating = rating;
}
public Collection<Order> getOrders() {
return orders;
}
public void setOrders(Collection<Order> orders) {
this.orders = orders;
}
public String toString() {
return "Customer:"+cid+" name:"+name;
}
public CustomerKey getCid() {
return cid;
}
public void setCid(CustomerKey cid) {
this.cid = cid;
}
}

View File

@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.openjpa.persistence.query;
import javax.persistence.*;
import java.util.Collection;
import java.util.ArrayList;
@Entity
@Table(name="TORDER")
public class Order {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int oid;
double amount;
boolean delivered;
@ManyToOne (fetch=FetchType.EAGER)
Customer customer;
@OneToMany (fetch=FetchType.EAGER , mappedBy="order")
Collection<OrderItem> lineitems = new ArrayList<OrderItem>();
@Version
long version;
public Order(){}
public Order( double amt, boolean delivered, Customer c){
amount=amt;
this.delivered=delivered;
customer=c;
if (c!=null) c.getOrders().add(this);
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public boolean isDelivered() {
return delivered;
}
public void setDelivered(boolean delivered) {
this.delivered = delivered;
}
public int getOid() {
return oid;
}
public String toString(){
return "Order:"+oid+" amount:"+amount+" delivered:"+delivered+" customer:"+
( customer!=null ? customer.getCid() : -1 );
}
public Collection<OrderItem> getLineitems() {
return lineitems;
}
public void setLineitems(Collection<OrderItem> lineitems) {
this.lineitems = lineitems;
}
}

View File

@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.openjpa.persistence.query;
import javax.persistence.*;
@Entity
@Table(name="TORDERITEM")
public class OrderItem {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int lid;
int quantity;
double cost;
@ManyToOne (fetch=FetchType.EAGER)
Order order;
@Version
long version;
public OrderItem(){}
public OrderItem( int quantity, double cost, Order o){
this.quantity=quantity;
this.cost=cost;
order = o;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public int getLid() {
return lid;
}
public void setLid(int lid) {
this.lid = lid;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}

View File

@ -0,0 +1,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.openjpa.persistence.query;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test JPQL subquery
*/
public class TestSubquery
extends SingleEMFTestCase {
public void setUp() {
setUp(Customer.class, Customer.CustomerKey.class,
Order.class, OrderItem.class, CLEAR_TABLES);
}
static String[] querys = new String[] {
"select o.oid from Order o where o.customer.name =" +
" (select max(o2.customer.name) from Order o2" +
" where o.customer.cid.id = o2.customer.cid.id)",
"select o from Order o where o.customer.name =" +
" (select max(o2.customer.name) from Order o2" +
" where o.customer.cid.id = o2.customer.cid.id)",
"select o.oid from Order o where o.amount >" +
" (select count(i) from o.lineitems i)",
"select o.oid from Order o where o.amount >" +
" (select count(o.amount) from Order o)",
"select o.oid from Order o where o.amount >" +
" (select count(o.oid) from Order o)",
"select o.oid from Order o where o.amount >" +
" (select avg(o.amount) from Order o)",
"select c.name from Customer c where exists" +
" (select o from c.orders o where o.oid = 1) or exists" +
" (select o from c.orders o where o.oid = 2)",
"select c.name from Customer c, in(c.orders) o where o.amount between" +
" (select max(o.amount) from Order o) and" +
" (select avg(o.amount) from Order o) ",
"select o.oid from Order o where o.amount >" +
" (select sum(o2.amount) from Customer c, in(c.orders) o2) ",
"select o.oid from Order o where o.amount between" +
" (select avg(o2.amount) from Customer c, in(c.orders) o2)" +
" and (select min(o2.amount) from Customer c, in(c.orders) o2)",
"select o.oid from Customer c, in(c.orders)o where o.amount >" +
" (select sum(o2.amount) from c.orders o2)",
// outstanding problem subqueries:
//"select o from Order o where o.amount > (select count(o) from Order o)",
//"select o from Order o where o.amount > (select count(o2) from Order o2)",
// "select c from Customer c left join c.orders p where not exists"
// + " (select o2 from c.orders o2 where o2 = o",
};
static String[] updates = new String[] {
"update Order o set o.amount = 1000 where o.customer.name = " +
" (select max(o2.customer.name) from Order o2 " +
" where o.customer.cid.id = o2.customer.cid.id)",
};
public void testSubquery() {
EntityManager em = emf.createEntityManager();
for (int i = 0; i < querys.length; i++) {
String q = querys[i];
List rs = em.createQuery(q).getResultList();
assertEquals(0, rs.size());
}
em.getTransaction().begin();
for (int i = 0; i < updates.length; i++) {
int updateCount = em.createQuery(updates[i]).executeUpdate();
assertEquals(0, updateCount);
}
em.getTransaction().rollback();
em.close();
}
}