Package org.apache.cayenne.exp

Examples of org.apache.cayenne.exp.Expression


     * Cayenne should trim it.
     */
    public void testCharInQualifier() throws Exception {
        createArtistsDataSet();

        Expression e = ExpressionFactory.matchExp("artistName", "artist1");
        SelectQuery q = new SelectQuery(Artist.class, e);
        List<Artist> artists = context.performQuery(q);
        assertEquals(1, artists.size());
    }
View Full Code Here


        return list;
    }

    @SuppressWarnings("unchecked")
    public List<Customer> getCustomers(Date from, Date to) {
        Expression qual = ExpressionFactory.noMatchExp(Customer.DATE_JOINED_PROPERTY, null);

        if (from != null) {
            qual = qual.andExp(ExpressionFactory.greaterOrEqualExp(Customer.DATE_JOINED_PROPERTY, from));
        }
        if (to != null) {
            qual = qual.andExp(ExpressionFactory.lessOrEqualExp(Customer.DATE_JOINED_PROPERTY, to));
        }

        SelectQuery query = new SelectQuery(Customer.class, qual);
        query.addOrdering(Customer.DATE_JOINED_PROPERTY, true);
View Full Code Here

        }
    }

    @SuppressWarnings("unchecked")
    public List<Customer> getCustomersForName(String value) {
        Expression template = Expression.fromString("name likeIgnoreCase $name");
        Expression e = template.expWithParameters(toMap(Customer.NAME_PROPERTY, "%" + value + "%"));
        return  performQuery(new SelectQuery(Customer.class, e));
    }
View Full Code Here

    }

    protected Expression extractQualifier() {
        Query q = queryAssembler.getQuery();

        Expression qualifier = ((QualifiedQuery) q).getQualifier();

        // append Entity qualifiers, taking inheritance into account
        ObjEntity entity = getObjEntity();

        if (entity != null) {

            ClassDescriptor descriptor = queryAssembler
                    .getEntityResolver()
                    .getClassDescriptor(entity.getName());
            Expression entityQualifier = descriptor
                    .getEntityInheritanceTree()
                    .qualifierForEntityAndSubclasses();
            if (entityQualifier != null) {
                qualifier = (qualifier != null)
                        ? qualifier.andExp(entityQualifier)
                        : entityQualifier;
            }
        }

        /**
         * Attaching root Db entity's qualifier
         */
        if (getDbEntity() != null) {
            Expression dbQualifier = getDbEntity().getQualifier();
            if (dbQualifier != null) {
                dbQualifier = dbQualifier.transform(new DbEntityQualifierTransformer());

                qualifier = qualifier == null ? dbQualifier : qualifier
                        .andExp(dbQualifier);
            }
        }
View Full Code Here

    public void testShortInQualifier() throws Exception {
        createTestData("testShortInQualifier");

        // test
        Expression qual = ExpressionFactory.matchExp("smallintCol", new Short("9999"));
        List objects = context.performQuery(new SelectQuery(
                SmallintTestEntity.class,
                qual));
        assertEquals(1, objects.size());
View Full Code Here

    public void testTinyintInQualifier() throws Exception {
        createTestData("testTinyintInQualifier");

        // test
        Expression qual = ExpressionFactory.matchExp("tinyintCol", new Byte((byte) 81));
        List objects = context
                .performQuery(new SelectQuery(TinyintTestEntity.class, qual));
        assertEquals(1, objects.size());

        TinyintTestEntity object = (TinyintTestEntity) objects.get(0);
View Full Code Here

        // this will clear cache as a side effect
        context = createDataContext();

        // fetch true...
        Expression trueQ = ExpressionFactory.matchExp("bitColumn", Boolean.TRUE);
        List trueResult = context
                .performQuery(new SelectQuery(BitTestEntity.class, trueQ));
        assertEquals(1, trueResult.size());

        BitTestEntity trueRefetched = (BitTestEntity) trueResult.get(0);
        assertEquals(Boolean.TRUE, trueRefetched.getBitColumn());

        // CAY-320. Simplifying the use of booleans to allow identity comparison.
        assertNotSame(trueRefetched, trueObject);
        assertSame(Boolean.TRUE, trueRefetched.getBitColumn());

        // fetch false
        Expression falseQ = ExpressionFactory.matchExp("bitColumn", Boolean.FALSE);
        List falseResult = context.performQuery(new SelectQuery(
                BitTestEntity.class,
                falseQ));
        assertEquals(1, falseResult.size());
View Full Code Here

        // this will clear cache as a side effect
        context = createDataContext();

        // fetch true...
        Expression trueQ = ExpressionFactory.matchExp("booleanColumn", Boolean.TRUE);
        List trueResult = context.performQuery(new SelectQuery(
                BooleanTestEntity.class,
                trueQ));
        assertEquals(1, trueResult.size());

        BooleanTestEntity trueRefetched = (BooleanTestEntity) trueResult.get(0);
        assertEquals(Boolean.TRUE, trueRefetched.getBooleanColumn());

        // CAY-320. Simplifying the use of booleans to allow identity comparison.
        assertNotSame(trueRefetched, trueObject);
        assertSame(Boolean.TRUE, trueRefetched.getBooleanColumn());

        // fetch false
        Expression falseQ = ExpressionFactory.matchExp("booleanColumn", Boolean.FALSE);
        List falseResult = context.performQuery(new SelectQuery(
                BooleanTestEntity.class,
                falseQ));
        assertEquals(1, falseResult.size());
View Full Code Here

        if (query instanceof PrefetchSelectQuery) {

            // for each relationship path add PK of the target entity...
            for (String path : ((PrefetchSelectQuery) query).getResultPaths()) {

                Expression pathExp = oe.translateToDbPath(Expression.fromString(path));

                // add joins and find terminating element

                resetJoinStack();

                PathComponent<DbAttribute, DbRelationship> lastComponent = null;
                for (PathComponent<DbAttribute, DbRelationship> component : table
                        .resolvePath(pathExp, getPathAliases())) {

                    if (component.getRelationship() != null) {
                        dbRelationshipAdded(component.getRelationship(), component
                                .getJoinType(), null);
                    }

                    lastComponent = component;
                }

                // process terminating element
                if (lastComponent != null) {

                    DbRelationship relationship = lastComponent.getRelationship();

                    if (relationship != null) {

                        String labelPrefix = pathExp.toString().substring("db:".length());
                        DbEntity targetEntity = (DbEntity) relationship.getTargetEntity();

                        for (DbAttribute pk : targetEntity.getPrimaryKeys()) {

                            // note that we my select a source attribute, but label it as
                            // target for simplified snapshot processing
                            appendColumn(columns, null, pk, attributes, labelPrefix
                                    + '.'
                                    + pk.getName());
                        }
                    }
                }
            }
        }

        // handle joint prefetches directly attached to this query...
        if (query.getPrefetchTree() != null) {

            for (PrefetchTreeNode prefetch : query.getPrefetchTree().adjacentJointNodes()) {

                // for each prefetch add all joins plus columns from the target entity
                Expression prefetchExp = Expression.fromString(prefetch.getPath());
                Expression dbPrefetch = oe.translateToDbPath(prefetchExp);

                resetJoinStack();
                DbRelationship r = null;
                for (PathComponent<DbAttribute, DbRelationship> component : table
                        .resolvePath(dbPrefetch, getPathAliases())) {
                    r = component.getRelationship();
                    dbRelationshipAdded(r, JoinType.LEFT_OUTER, null);
                }

                if (r == null) {
                    throw new CayenneRuntimeException("Invalid joint prefetch '"
                            + prefetch
                            + "' for entity: "
                            + oe.getName());
                }

                // add columns from the target entity, including those that are matched
                // against the FK of the source entity. This is needed to determine
                // whether optional relationships are null

                // go via target OE to make sure that Java types are mapped correctly...
                ObjRelationship targetRel = (ObjRelationship) prefetchExp.evaluate(oe);
                Iterator<ObjAttribute> targetObjAttrs = (Iterator<ObjAttribute>) targetRel
                        .getTargetEntity()
                        .getAttributes()
                        .iterator();

                String labelPrefix = dbPrefetch.toString().substring("db:".length());
                while (targetObjAttrs.hasNext()) {
                    ObjAttribute oa = targetObjAttrs.next();
                    Iterator<CayenneMapEntry> dbPathIterator = oa.getDbPathIterator();
                    while (dbPathIterator.hasNext()) {
                        Object pathPart = dbPathIterator.next();
View Full Code Here

        createTwoArtistsAndTwoPaintingsDataSet();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name1", "artist2");
        params.put("name2", "artist3");
        Expression e = Expression
                .fromString("artistName = $name1 or artistName = $name2");
        SelectQuery q = new SelectQuery("Artist", e.expWithParameters(params));
        q.addPrefetch(Artist.PAINTING_ARRAY_PROPERTY);

        final List<Artist> artists = context.performQuery(q);

        queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
View Full Code Here

TOP

Related Classes of org.apache.cayenne.exp.Expression

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.