Package org.apache.torque.criteria

Examples of org.apache.torque.criteria.Criteria


     * @throws Exception if the test fails
     */
    public void testMultiplePk() throws Exception
    {
        // clean table
        Criteria criteria = new Criteria();
        criteria.where(MultiPkPeer.PK1, (Object) null, Criteria.NOT_EQUAL);
        MultiPkPeer.doDelete(criteria);

        // do test
        MultiPk mpk = new MultiPk();
        mpk.setPrimaryKey("Svarchar:N5:Schar:N3:N-42:N3:N4:N5:N6:D9999999999:");
View Full Code Here


        for (int j = 0; j < validTitles.length; j++)
        {
            titleSet.add(validTitles[j]);
        }

        Criteria crit = new Criteria();
        Criterion c = new Criterion(BookPeer.TITLE,
                "Book 6 - Author 1", Criteria.GREATER_EQUAL);
        c.and(new Criterion(BookPeer.TITLE,
                "Book 8 - Author 3", Criteria.LESS_EQUAL));
        crit.where(c);
        crit.addDescendingOrderByColumn(BookPeer.BOOK_ID);
        crit.setLimit(10);
        crit.setOffset(5);
        List<Book> books = BookPeer.doSelect(crit);
        assertTrue("List should have 10 books, not " + books.size(),
                books.size() == 10);
        for (Book book : books)
        {
            String title = book.getTitle();
            assertTrue("Incorrect title: " + title,
                    titleSet.contains(title));
        }

        // Test limit of zero works
        crit = new Criteria();
        crit.setLimit(0);
        try
        {
            books = BookPeer.doSelect(crit);
            assertTrue("List should have 0 books, not " + books.size(),
                    books.size() == 0);
        }
        catch (TorqueException e)
        {
            if (defaultAdapter.supportsNativeLimit())
            {
                if (!(defaultAdapter instanceof DerbyAdapter))
                {
                    throw e;
                }
                else
                {
                    log.error("testLimitOffset(): "
                            + "A limit of 0 is not supported for Derby");
                }
            }
            else
            {
                log.error("testLimitOffset(): "
                        + "A limit of 0 is not supported for Databases "
                        + "without native limit support");
            }
        }

        // check that Offset also works without limit
        crit = new Criteria();
        crit.setOffset(5);
        books = BookPeer.doSelect(crit);
        assertTrue("List should have 95 books, not " + books.size(),
                books.size() == 95);

        // Check that limiting also works if a table with an equal column name
        // is joined. This is problematic for oracle, see TORQUE-10.

        crit = new Criteria();
        crit.setLimit(10);
        crit.setOffset(5);
        books = BookPeer.doSelectJoinAuthor(crit);
        assertTrue("List should have 10 books, not " + books.size(),
                books.size() == 10);
    }
View Full Code Here

    /**
     * Checks whether the setSingleRecord() method in criteria works
     */
    public void testSingleRecord() throws Exception
    {
        Criteria criteria = new Criteria();
        criteria.setSingleRecord(true);
        criteria.setLimit(1);
        criteria.setOffset(5);
        List<Book> books = BookPeer.doSelect(criteria);
        assertTrue("List should have 1 books, not " + books.size(),
                books.size() == 1);

        criteria = new Criteria();
        criteria.setSingleRecord(true);
        criteria.setLimit(2);
        try
        {
            books = BookPeer.doSelect(criteria);
            fail("doSelect should have failed "
                    + "because two records were selected "
View Full Code Here

     * @throws Exception if the test fails
     */
    public void testNullSelects() throws Exception
    {
        // clean table
        VarcharTypePeer.doDelete(new Criteria());
        IntegerTypePeer.doDelete(new Criteria());

        // add test data
        VarcharType varcharType = new VarcharType();
        varcharType.setId("text2");
        varcharType.setVarcharValue("text2");
        varcharType.save();
        varcharType = new VarcharType();
        varcharType.setId("text");
        varcharType.save();

        IntegerType integerTypeNotNull = new IntegerType();
        integerTypeNotNull.setIntegerObjectValue(1);
        integerTypeNotNull.save();
        IntegerType integerTypeNull = new IntegerType();
        integerTypeNull.save();

        // check for comparison NOT_EQUAL and value null
        Criteria criteria = new Criteria();
        criteria.where(VarcharTypePeer.ID, null, Criteria.NOT_EQUAL)
            .and(VarcharTypePeer.VARCHAR_VALUE, null, Criteria.NOT_EQUAL);
        List<VarcharType> varcharResult = VarcharTypePeer.doSelect(criteria);
        assertEquals(1, varcharResult.size());
        assertEquals("text2", varcharResult.get(0).getId());

        criteria = new Criteria();
        criteria.where(IntegerTypePeer.ID, null, Criteria.NOT_EQUAL)
            .and(IntegerTypePeer.INTEGER_OBJECT_VALUE, null, Criteria.NOT_EQUAL);
        List<IntegerType> integerResult = IntegerTypePeer.doSelect(criteria);
        assertEquals(1, integerResult.size());
        assertEquals(integerTypeNotNull.getId(), integerResult.get(0).getId());

        // check for comparison EQUAL and value null
        criteria = new Criteria();
        criteria.where(VarcharTypePeer.VARCHAR_VALUE, null, Criteria.EQUAL);
        varcharResult = VarcharTypePeer.doSelect(criteria);
        assertEquals(1, varcharResult.size());
        assertEquals("text", varcharResult.get(0).getId());

        criteria = new Criteria();
        criteria.where(IntegerTypePeer.INTEGER_OBJECT_VALUE, null, Criteria.EQUAL);
        integerResult = IntegerTypePeer.doSelect(criteria);
        assertEquals(1, integerResult.size());
        assertEquals(integerTypeNull.getId(), integerResult.get(0).getId());
    }
View Full Code Here

        Connection connection = Transaction.begin(AuthorPeer.DATABASE_NAME);
        author.setName("NewName2");
        AuthorPeer.doUpdate(author);
        Transaction.commit(connection);

        Criteria criteria = new Criteria();
        criteria.addAscendingOrderByColumn(AuthorPeer.NAME);

        List<Author> authors = AuthorPeer.doSelect(criteria);
        assertEquals("List should contain 2 authors", 2, authors.size());
        assertEquals("First Author's name should be \"NewName2\"",
                "NewName2",
                authors.get(0).getName());
        assertEquals("Second Author's name should be \"OtherName\"",
                "OtherName",
                authors.get(1).getName());

        author.setName("NewName3");
        AuthorPeer.doUpdate(author);

        criteria = new Criteria();
        criteria.addAscendingOrderByColumn(AuthorPeer.NAME);

        authors = AuthorPeer.doSelect(criteria);
        assertEquals("List should contain 2 authors", 2, authors.size());
        assertEquals("First Author's name should be \"NewName3\"",
                "NewName3",
View Full Code Here

     * @throws Exception if the test fails
     */
    public void testSelectClause() throws Exception
    {
        // test double functions in select columns
        Criteria criteria = new Criteria();
        criteria.addSelectColumn(
                new ColumnImpl("count(distinct(" + BookPeer.BOOK_ID + "))"));
        BasePeer.doSelect(criteria, new IntegerMapper());

        // test qualifiers in function in select columns
        criteria = new Criteria();
        criteria.addSelectColumn(
                new ColumnImpl("count(distinct " + BookPeer.BOOK_ID + ")"));
        BasePeer.doSelect(criteria, new IntegerMapper());
    }
View Full Code Here

     * test if a select from the "default" database works
     * @throws Exception (NPE) if the test fails
     */
    public void testSelectFromDefault() throws Exception
    {
        Criteria criteria = new Criteria("default");

        criteria.addSelectColumn(BookPeer.BOOK_ID);

        BasePeer.doSelect(criteria, new IntegerMapper());
    }
View Full Code Here

     */
    public void testNullConnection() throws Exception
    {
        try
        {
            Criteria criteria = new Criteria();
            AuthorPeer.doSelect(criteria, new IntegerMapper(), null);
            fail("NullPointerException expected");
        }
        catch (NullPointerException e)
        {
            //expected
        }

        try
        {
            Criteria criteria = new Criteria();
            criteria.where(BookPeer.BOOK_ID, (Long) null, Criteria.NOT_EQUAL);
            BookPeer.doDelete(criteria, (Connection) null);
            fail("NullPointerException expected");
        }
        catch (NullPointerException e)
        {
View Full Code Here

            book.setIsbn("unknown");
            book.save();
        }

        // test simple ascending order by
        Criteria criteria = new Criteria();
        criteria.addAscendingOrderByColumn(BookPeer.TITLE);
        List<Book> bookList = BookPeer.doSelect(criteria);
        if (bookList.size() != 4)
        {
            fail("Ascending Order By: "
                     + "incorrect numbers of books found : "
                     + bookList.size()
                     + ", should be 4");
        }
        if (! "Book 1".equals(bookList.get(0).getTitle()))
        {
            fail("Ascending Order By: "
                     + "Title of first Book is "
                     + bookList.get(0).getTitle()
                     + ", should be \"Book 1\"");
        }
        if (! "Book 4".equals(bookList.get(3).getTitle()))
        {
            fail("Ascending Order By: "
                     + "Title of fourth Book is "
                     + bookList.get(3).getTitle()
                     + ", should be \"Book 4\"");
        }

        // test simple descending order by
        criteria = new Criteria();
        criteria.addDescendingOrderByColumn(BookPeer.TITLE);
        bookList = BookPeer.doSelect(criteria);
        if (bookList.size() != 4)
        {
            fail("Descending Order By: "
                     + "incorrect numbers of books found : "
                     + bookList.size()
                     + ", should be 4");
        }
        if (! "Book 1".equals(bookList.get(3).getTitle()))
        {
            fail("Descending Order By: "
                     + "Title of fourth Book is "
                     + bookList.get(3).getTitle()
                     + ", should be \"Book 1\"");
        }
        if (! "Book 4".equals((bookList.get(0)).getTitle()))
        {
            fail("Descending Order By: "
                     + "Title of first Book is "
                     + bookList.get(0).getTitle()
                     + ", should be \"Book 4\"");
        }

        criteria = new Criteria();
        criteria.addAlias("b", BookPeer.TABLE_NAME);
        criteria.addJoin(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID);
        criteria.addJoin(
                AuthorPeer.AUTHOR_ID,
                new ColumnImpl("b." + BookPeer.AUTHOR_ID.getColumnName()));
        criteria.addAscendingOrderByColumn(
                new ColumnImpl("b." + BookPeer.TITLE.getColumnName()));
        criteria.addDescendingOrderByColumn(BookPeer.TITLE);
        // the retrieved columns are
        // author    book   b
        // author1  book1   book1
        // author2  book4   book2
        // author2  book3   book2
        // author2  book2   book2
        // author2  book4   book3
        // ...
        bookList = BookPeer.doSelect(criteria);
        if (bookList.size() != 10)
        {
            fail("ordering by Aliases: "
                     + "incorrect numbers of books found : "
                     + bookList.size()
                     + ", should be 10");
        }
        if (!"Book 4".equals(bookList.get(1).getTitle()))
        {
            fail("ordering by Aliases: "
                     + "Title of second Book is "
                     + bookList.get(1).getTitle()
                     + ", should be \"Book 4\"");
        }
        if (!"Book 3".equals(bookList.get(2).getTitle()))
        {
            fail("ordering by Aliases: "
                     + "Title of third Book is "
                     + bookList.get(2).getTitle()
                     + ", should be \"Book 3\"");
        }

        criteria = new Criteria();
        criteria.addAlias("b", BookPeer.TABLE_NAME);
        criteria.addJoin(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID);
        criteria.addJoin(
                AuthorPeer.AUTHOR_ID,
                new ColumnImpl("b." + BookPeer.AUTHOR_ID.getColumnName()));
        criteria.addAscendingOrderByColumn(BookPeer.TITLE);
        criteria.addDescendingOrderByColumn(
                new ColumnImpl("b." + BookPeer.TITLE.getColumnName()));
        // the retrieved columns are
        // author    book   b
        // author1  book1   book1
        // author2  book2   book4
        // author2  book2   book3
        // author2  book2   book2
        // author2  book3   book4
        // ...
        bookList = BookPeer.doSelect(criteria);
        if (bookList.size() != 10)
        {
            fail("ordering by Aliases (2): "
                     + "incorrect numbers of books found : "
                     + bookList.size()
                     + ", should be 10");
        }
        if (!"Book 2".equals(bookList.get(1).getTitle()))
        {
            fail("ordering by Aliases (2, PS): "
                     + "Title of second Book is "
                     + bookList.get(1).getTitle()
                     + ", should be \"Book 2\"");
        }
        if (!"Book 2".equals(bookList.get(2).getTitle()))
        {
            fail("ordering by Aliases (2, PS): "
                     + "Title of third Book is "
                     + bookList.get(2).getTitle()
                     + ", should be \"Book 2\"");
        }

        // test usage of Expressions in order by
        criteria = new Criteria();
        criteria.addAscendingOrderByColumn(
                new ColumnImpl("UPPER(" + BookPeer.TITLE + ")"));
        criteria.setIgnoreCase(true);
        BookPeer.doSelect(criteria);
    }
View Full Code Here

        // check ignore case in selects
        Author author = new Author();
        author.setName("AuTHor");
        author.save();

        Criteria criteria = new Criteria();
        criteria.where(AuthorPeer.NAME, author.getName().toLowerCase());
        criteria.setIgnoreCase(true);
        List<Author> result = AuthorPeer.doSelect(criteria);
        assertTrue("Size of result is not 1, but " + result.size(),
                result.size() == 1);

        // LIKE treatment might be different (e.g. postgres), so check extra
        criteria = new Criteria();
        criteria.where(
                AuthorPeer.NAME,
                author.getName().toLowerCase().replace('r', '%'),
                Criteria.LIKE);
        criteria.setIgnoreCase(true);
        result = AuthorPeer.doSelect(criteria);
        assertTrue("Size of result is not 1, but " + result.size(),
                result.size() == 1);

        // Test ignore case in criterion
        criteria = new Criteria();
        Criterion criterion1 = new Criterion(
                AuthorPeer.NAME,
                author.getName().toLowerCase(),
                Criteria.EQUAL);
        criterion1.setIgnoreCase(true);
        Criterion criterion2 = new Criterion(
                AuthorPeer.AUTHOR_ID, null, Criteria.NOT_EQUAL);
        criterion1.and(criterion2);

        result = AuthorPeer.doSelect(criteria);

        // ignore case should not be set either in Criteria
        // nor in other criterions
        assertFalse(criteria.isIgnoreCase());
        assertFalse(criterion2.isIgnoreCase());
        assertTrue("Size of result is not 1, but " + result.size(),
                result.size() == 1);


        // Test ignore case in attached criterion
        criteria = new Criteria();
        criterion1 = new Criterion(
                AuthorPeer.AUTHOR_ID, null, Criteria.NOT_EQUAL);
        criterion2 = new Criterion(
                AuthorPeer.NAME,
                author.getName().toLowerCase(),
                Criteria.EQUAL);
        criterion2.setIgnoreCase(true);
        criterion1.and(criterion2);

        result = AuthorPeer.doSelect(criteria);

        // ignore case should not be set either in Criteria
        // nor in other criterions
        assertFalse(criteria.isIgnoreCase());
        assertFalse(criterion1.isIgnoreCase());

        assertTrue("Size of result is not 1, but " + result.size(),
                result.size() == 1);

        // ignore case in "in" query
        {
            criteria = new Criteria();
            Set<String> names = new HashSet<String>();
            names.add(author.getName().toLowerCase());
            criteria.where(AuthorPeer.NAME, names, Criteria.IN);
            criteria.setIgnoreCase(true);

            result = AuthorPeer.doSelect(criteria);
            assertEquals("Expected result of size 1 but got " + result.size(),
                    result.size(),
                    1);
        }

        // Check that case is not ignored if ignoreCase is not set
        // This is known not to work for mysql
        author = new Author();
        author.setName("author");
        author.save();

        Adapter adapter = Torque.getAdapter(Torque.getDefaultDB());
        if (adapter instanceof MysqlAdapter
                || adapter instanceof MssqlAdapter)
        {
            log.error("testIgnoreCase(): "
                    + "Case sensitive comparisons are known not to work"
                    + " with Mysql and MSSQL");
            // failing is "expected", so bypass without error
        }
        else
        {
            criteria = new Criteria();
            criteria.where(AuthorPeer.NAME, author.getName());
            result = AuthorPeer.doSelect(criteria);
            assertTrue("Size of result is not 1, but " + result.size(),
                    result.size() == 1);

            // again check LIKE treatment
            criteria = new Criteria();
            criteria.where(
                    AuthorPeer.NAME,
                    author.getName().replace('r', '%'),
                    Criteria.LIKE);
            result = AuthorPeer.doSelect(criteria);
            assertTrue("Size of result is not 1, but " + result.size(),
                    result.size() == 1);

            // Test different ignore cases in criterions
            criteria = new Criteria();
            criterion1 = new Criterion(
                    AuthorPeer.NAME,
                    author.getName().toLowerCase(),
                    Criteria.NOT_EQUAL);
            criterion2 = new Criterion(
                    AuthorPeer.NAME,
                    author.getName().toLowerCase(),
                    Criteria.EQUAL);
            criterion2.setIgnoreCase(true);
            criterion1.and(criterion2);
            criteria.where(criterion1);

            result = AuthorPeer.doSelect(criteria);
            assertTrue("Size of result is not 1, but " + result.size(),
                    result.size() == 1);

            // ignore case in "in" query
            {
                criteria = new Criteria();
                Set<String> names = new HashSet<String>();
                names.add(author.getName());
                criteria.where(AuthorPeer.NAME, names, Criteria.IN);

                result = AuthorPeer.doSelect(criteria);
                assertEquals("Expected result of size 1 but got " + result.size(),
                        result.size(),
                        1);
            }
        }

        cleanBookstore();
        author = new Author();
        author.setName("AA");
        author.save();
        author = new Author();
        author.setName("BB");
        author.save();
        author = new Author();
        author.setName("ba");
        author.save();
        author = new Author();
        author.setName("ab");
        author.save();

        // check ignoreCase in Criteria
        criteria = new Criteria();
        criteria.setIgnoreCase(true);
        criteria.addAscendingOrderByColumn(AuthorPeer.NAME);
        result = AuthorPeer.doSelect(criteria);
        assertTrue("Size of result is not 4, but " + result.size(),
                result.size() == 4);
        assertEquals(result.get(0).getName(), "AA");
        assertEquals(result.get(1).getName(), "ab");
        assertEquals(result.get(2).getName(), "ba");
        assertEquals(result.get(3).getName(), "BB");

        // check ignoreCase in orderBy
        criteria = new Criteria();
        criteria.addAscendingOrderByColumn(AuthorPeer.NAME, true);
        result = AuthorPeer.doSelect(criteria);
        assertTrue("Size of result is not 4, but " + result.size(),
                result.size() == 4);
        assertEquals(result.get(0).getName(), "AA");
        assertEquals(result.get(1).getName(), "ab");
View Full Code Here

TOP

Related Classes of org.apache.torque.criteria.Criteria

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.