lbehnke / h2database

Automatically exported from code.google.com/p/h2database
0 stars 0 forks source link

JaQu: creates wrong WHERE condition on some inputs #119

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
(simple SQL scripts or simple standalone applications are preferred)

See the following test case:

public class AliasMapTest extends TestBase {

    public static void main(String[] args) throws Exception {
        new AliasMapTest().test();
    }

    public void test() throws Exception {
        Db db = Db.open("jdbc:h2:mem:;TRACE_LEVEL_SYSTEM_OUT=2", "sa", "sa");
        db.insertAll(Product.getList());

        Product p = new Product();
        List<Product> products = db
            .from(p)
            .where(p.unitsInStock).is(9)
            .orderBy(p.productId).select();

        assertEquals("[]", products.toString());
    }
}

What is the expected output? What do you see instead?

Expected: empty product list, because there is no product with unitsInStock=9
Got: All products

What version of the product are you using? On what operating system, file
system, and virtual machine?

H2 rev. 1849
Windows XP, JDK 1.5.0.14

Do you know a workaround?

Yes, see below.

How important/urgent is the problem for you?

Not important.

In your view, is this a defect or a feature request?

Defect.

Please provide any additional information below.

JaQu generates the following SQL statement:

SELECT productId, productName, category, unitPrice, unitsInStock FROM
Product WHERE unitsInStock =unitsInStock  ORDER BY productId;

Of course the correct statement should read:

... WHERE unitsInStock =?

where ? is bound to 9.

JaQu inserts "unitsInStock" here, because at the time of the execution, the
internal identifier object for the field "unitsInStock" happens to be 9.
The method Query.appendSQL(SQLStatement stat, Object x) finds that number
in its aliasMap and mistakenly handles it as a column identifier instead of
a placeholder for a value.

I think this issue could be fixed by replacing the HashMap Query.aliasMap
with an IdentityHashMap. Changing line 34 in Query.java to

    private final Map<Object, SelectColumn<T>> aliasMap =
Utils.newIdentityHashMap();

fixes the issue.

Original issue reported on code.google.com by dmoeb...@gmx.net on 18 Sep 2009 at 4:43

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks a lot for your help! I will apply your patch in the next release.

Original comment by thomas.t...@gmail.com on 19 Sep 2009 at 9:00

GoogleCodeExporter commented 9 years ago
This should be fixed in version 1.1.119

Original comment by thomas.t...@gmail.com on 26 Sep 2009 at 11:33