Re: performQuery generics

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Dec 21 2007 - 09:27:35 EST

  • Next message: Andrus Adamchik: "Re: performQuery generics"

    On Dec 21, 2007, at 2:30 PM, Tore Halset wrote:

    > A wrapper is fine, but do you think this is fixable in Query as well?

    Unfortunately I can't figure out how to make it work without breaking
    core Cayenne abstractions :-/

    BTW, here is a skeleton of a parameterized wrapper that I tried to
    sketch out. Not complete of course, but the general idea. In a way
    aligned with the JPA query (so we can reuse it in the JPA/Cayenne
    bridge), only more user-friendly, as we can supply all the "hints" via
    dedicated methods.

    // usage:
    ObjectContext context = ...
    SelectBuilder<Artist> query = SelectBuilder.create(context,
    Artist.class);
    List<Artist> artists = query.setQueryName("XYZ").addParameter("X",
    "1").fetch();

    // wrapper class:
    public class SelectBuilder<T> {

            static enum ResultType {
                    OBJECT, DATA_ROW, OBJECT_ARRAY, SCALAR
            };

            protected Class<?> entityClass;
            protected ObjectContext context;

            // factory methods
            public static <T> SelectBuilder<T> create(ObjectContext context,
                            Class<T> resultClass) {
                    return new SelectBuilder<T>(context, resultClass, ResultType.OBJECT);
            }

            public static SelectBuilder<DataRow> createForDataRows(
                            ObjectContext context, Class<?> resultClass) {
                    return new SelectBuilder<DataRow>(context, resultClass,
                                    ResultType.DATA_ROW);
            }

            private SelectBuilder(ObjectContext context, Class<?> entityClass,
                            ResultType type) {
                    ...
            }

            // configuration methods
            public SelectBuilder<T> setQueryName(String mappedQueryName) {
                    ...
                    return this;
            }

            public SelectBuilder<T> addParameter(String key, Object value) {
                    ...
                    return this;
            }

            // .... etc.

            // execution methods
            public List<T> fetch() {
                    ...
            }

            public T fetchSingleObject() {
                    ...
            }

            public T fetchByPk(int id) {
                    ...
            }
    }

    Andrus



    This archive was generated by hypermail 2.0.0 : Fri Dec 21 2007 - 09:28:11 EST