Search code examples
activejdbc

ActiveJDBC , How can i query some columns i interest with in a single table


when i query a single table , i do not want all columns , i just want some column that i interest in.

For example, when i use where method to query a table, it will query all columns in a table like

public class SubjectSpecimenType extends Model {

}
SubjectSpecimenType.where("SUBJECT_ID = ? AND SITE_ID = ?", subjectId, siteId);

i don't know if there has a method named select that i can use to query some column like

SubjectSpecimenType.select("SUBJECT_NAME", "SITE_NAME").where("SUBJECT_ID = ? AND SITE_ID = ?", subjectId, siteId);

there are the source code in LazyList.java

/**
     * Use to see what SQL will be sent to the database.
     *
     * @param showParameters true to see parameter values, false not to.
     * @return SQL in a dialect for current connection which will be used if you start querying this
     * list.
     */
    public String toSql(boolean showParameters) {
        String sql;
        if(forPaginator){
            sql = metaModel.getDialect().formSelect(null, null, fullQuery, orderBys, limit, offset);
        }else{
            sql = fullQuery != null ? fullQuery
                    : metaModel.getDialect().formSelect(metaModel.getTableName(), null, subQuery, orderBys, limit, offset);
        }
        if (showParameters) {
            StringBuilder sb = new StringBuilder(sql).append(", with parameters: ");
            join(sb, params, ", ");
            sql = sb.toString();
        }
        return sql;
    }

when call formSelect method, the second param columns always be null

is there a unfinish TODO ?


Solution

  • When operating on Models, ActiveJDBC always selects all columns, because if you load a model and it has partial attributes loaded, then you have a deficient model. The columns are specified in some edge cases, as in the RawPaginator: https://github.com/javalite/javalite/blob/e91ebdd1e4958bc0965d7ee99e6b7debc59a7b85/activejdbc/src/main/java/org/javalite/activejdbc/RawPaginator.java#L141

    There is nothing to finish here, the behavior is intentional.