Package org.apache.cayenne.query

Examples of org.apache.cayenne.query.SQLTemplate


    }

    public void testRetainUnreferencedModified() throws Exception {
        final DataContext context = createDataContext();
        context.performGenericQuery(new SQLTemplate(
                Artist.class,
                "insert into ARTIST (ARTIST_ID, ARTIST_NAME) values (1, 'aa')"));

        assertEquals(0, context.getObjectStore().registeredObjectsCount());
        Artist a = Cayenne.objectForPK(context, Artist.class, 1);
View Full Code Here


        // Delete data from entities one by one
        Iterator it = entities.iterator();
        while (it.hasNext()) {
            DbEntity entity = (DbEntity) it.next();

            Query query = new SQLTemplate(entity, "DELETE FROM "
                    + entity.getFullyQualifiedName());

            // notify delegate that delete is about to happen
            if (delegate != null) {
                query = delegate.willCleanData(this, entity, query);
View Full Code Here

        // create alternative context making sure that no cache is flushed
        DataContext altContext = createDataContextWithSharedCache(false);
       
        // update artist using raw SQL
        SQLTemplate query = getSQLTemplateBuilder().createSQLTemplate(
                Artist.class,
                "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) "
                        + "WHERE ARTIST_NAME = #bind($oldName)");

        Map map = new HashMap(3);
        map.put("newName", newName);
        map.put("oldName", originalName);
        query.setParameters(map);
        context.performNonSelectingQuery(query);

        // fetch updated artist into the new context, and see if the original
        // one gets updated
        Expression qual = ExpressionFactory.matchExp("artistName", newName);
View Full Code Here

                .getCachedSnapshot(artist.getObjectId());
        assertNotNull(oldSnapshot);
        assertEquals(originalName, oldSnapshot.get("ARTIST_NAME"));

        // update artist using raw SQL
        SQLTemplate update = getSQLTemplateBuilder()
                .createSQLTemplate(
                        Artist.class,
                        "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) WHERE ARTIST_NAME = #bind($oldName)");
        Map map = new HashMap(3);
        map.put("newName", newName);
        map.put("oldName", originalName);
        update.setParameters(map);
        context.performNonSelectingQuery(update);

        // fetch updated artist without refreshing
        Expression qual = ExpressionFactory.matchExp("artistName", newName);
        SelectQuery query = new SelectQuery(Artist.class, qual);
View Full Code Here

        assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(
                artist.getObjectId()));

        // now replace the row in the database
        String template = "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) WHERE ARTIST_NAME = #bind($oldName)";
        SQLTemplate update = new SQLTemplate(Artist.class, template);

        Map map = new HashMap(3);
        map.put("newName", backendName);
        map.put("oldName", originalName);
        update.setParameters(map);
        context.performNonSelectingQuery(update);

        context.commitChanges();

        assertEquals(newName, artist.getArtistName());
View Full Code Here

     * Testing behavior of Cayenne when a database exception is thrown in SELECT query.
     */
    public void testSelectException() {
        DataContext context = createDataContext();

        SQLTemplate q = new SQLTemplate(Artist.class, "SELECT * FROM NON_EXISTENT_TABLE");

        try {
            context.performGenericQuery(q);
            fail("Query was invalid and was supposed to fail.");
        }
View Full Code Here

                CompoundPkTestEntity.class,
                pk);

        String template = "SELECT * FROM COMPOUND_FK_TEST t0"
                + " WHERE #bindObjectEqual($a [ 't0.F_KEY1', 't0.F_KEY2' ] [ 'KEY1', 'KEY2' ] ) ORDER BY PKEY";
        SQLTemplate query = new SQLTemplate(CompoundFkTestEntity.class, template);
        query.setColumnNamesCapitalization(CapsStrategy.UPPER);
        query.setParameters(Collections.singletonMap("a", a));

        List<CompoundFkTestEntity> objects = context.performQuery(query);
        assertEquals(1, objects.size());

        CompoundFkTestEntity p = objects.get(0);
View Full Code Here

                CompoundPkTestEntity.class,
                pk);

        String template = "SELECT * FROM COMPOUND_FK_TEST t0"
                + " WHERE #bindObjectNotEqual($a [ 't0.F_KEY1', 't0.F_KEY2' ] [ 'KEY1', 'KEY2' ] ) ORDER BY PKEY";
        SQLTemplate query = new SQLTemplate(CompoundFkTestEntity.class, template);
        query.setColumnNamesCapitalization(CapsStrategy.UPPER);
        query.setParameters(Collections.singletonMap("a", a));

        List<CompoundFkTestEntity> objects = context.performQuery(query);
        assertEquals(1, objects.size());

        CompoundFkTestEntity p = objects.get(0);
View Full Code Here

    public void testBindObjectNotEqualNull() throws Exception {
        createFourArtistsAndThreePaintingsDataSet();

        String template = "SELECT * FROM PAINTING t0"
                + " WHERE #bindObjectNotEqual($a [ 't0.ARTIST_ID' ] [ 'ARTIST_ID' ] ) ORDER BY PAINTING_ID";
        SQLTemplate query = new SQLTemplate(Painting.class, template);
        query.setColumnNamesCapitalization(CapsStrategy.UPPER);
        query.setParameters(Collections.singletonMap("a", null));

        List<Painting> objects = context.performQuery(query);
        assertEquals(2, objects.size());

        Painting p1 = objects.get(0);
View Full Code Here

    public void testBindEqualNull() throws Exception {
        createFourArtistsAndThreePaintingsDataSet();

        String template = "SELECT * FROM PAINTING t0"
                + " WHERE t0.ARTIST_ID #bindEqual($id) ORDER BY PAINTING_ID";
        SQLTemplate query = new SQLTemplate(Painting.class, template);
        query.setColumnNamesCapitalization(CapsStrategy.UPPER);
        query.setParameters(Collections.singletonMap("id", null));

        List<Painting> objects = context.performQuery(query);
        assertEquals(1, objects.size());

        Painting p = objects.get(0);
View Full Code Here

TOP

Related Classes of org.apache.cayenne.query.SQLTemplate

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.