cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nikita Timofeev (JIRA)" <>
Subject [jira] [Created] (CAY-2187) Support for the scalar and aggregate SQL functions in ObjectSelect API
Date Wed, 04 Jan 2017 11:39:58 GMT
Nikita Timofeev created CAY-2187:

             Summary: Support for the scalar and aggregate SQL functions in ObjectSelect API
                 Key: CAY-2187
             Project: Cayenne
          Issue Type: Task
          Components: Core Library
    Affects Versions: 4.0.M5
            Reporter: Nikita Timofeev
            Assignee: Nikita Timofeev

For now only options to use SQL functions (including aggregate) are:
 # plain SQL

Both of it lack any type-checking or support from the API side as user needs to wright and
debug queries with plain text.

So Cayenne needs API for that functionality in order to have more control.
It is suggested to add following features and new API in ObjectSelect query:
# New expressions for function calls and new factory as convenient method to create them:
        Expression substrExp = FunctionExpressionFactory.substringExp(Artist.ARTIST_NAME.path(),
10, 15);     
        Expression modExp = FunctionExpressionFactory.modExp(Artist.ARTIST_NAME.path(), 10);
# Enhanced Property class with Expression support and explicit type,
they can be declared in persistent object like normal Cayenne properties or adhoc for specific
query: {code}
        Property<String> namePart = Property.create("namePart", substrExp, String.class);
        Property<Long> artistCount = Property.create("artistCount", countExp, Long.class);
        Property<Integer> minSalary = Property.create("minSalary", minExp, Integer.class);
# New methods in ObjectSelect for specifying return columns for the query: {code}        
        // Single column
        long totalCount = ObjectSelect.query(Artist.class)
        // Several columns
        List<Object[]> result = ObjectSelect.query(Artist.class)
                                    .columns(artistCount, minSalary, namePart)
        for(Object[] r : result) {
            long count = (long)r[0];
            int min = (int)r[1];
            String namePart = (String)r[2];
# New having() method in ObjectSelect: {code}
        // Full query example:
        List<Object[]> result = ObjectSelect.query(Artist.class)
                // result
                .columns(artistCount, minSalary, namePart)
                // WHERE clause
                .where( Date())) 
                // additional condition in WHERE clause
                // HAVING clause
                // additional condition in HAVING clause

        for(Object[] r : result) {
            long count = (long)r[0];
            int min = (int)r[1];
            String name = (String)r[2];

        // Aggregate on toMany relationship:
        Expression paintingCountExp = FunctionExpressionFactory.countExp(Artist.PAINTING_ARRAY.path());
        Property<Long> paintingCountProperty = Property.create("paintingCount", paintingCountExp,
        List<Object[]> result2 = ObjectSelect.query(Artist.class)
                .columns(Artist.ARTIST_NAME, paintingCountProperty)
        for(Object[] r : result2) {
            String artistName = (String)r[0];
            long paintingCount = (long)r[1];

This message was sent by Atlassian JIRA

View raw message