diff --git a/src/main/java/com/samskivert/depot/Query.java b/src/main/java/com/samskivert/depot/Query.java index 9a2f721..57ec32f 100644 --- a/src/main/java/com/samskivert/depot/Query.java +++ b/src/main/java/com/samskivert/depot/Query.java @@ -428,8 +428,8 @@ public class Query */ public List> select (SQLExpression exp1, SQLExpression exp2) { - return _ctx.invoke(new FindAllQuery.Projection>( - _ctx, Projector.create(_pclass, exp1, exp2), getClauses())); + Builder2,V1,V2> builder = Tuple2.builder(); + return select(builder, exp1, exp2); } /** @@ -438,8 +438,8 @@ public class Query public List> select ( SQLExpression exp1, SQLExpression exp2, SQLExpression exp3) { - return _ctx.invoke(new FindAllQuery.Projection>( - _ctx, Projector.create(_pclass, exp1, exp2, exp3), getClauses())); + Builder3,V1,V2,V3> builder = Tuple3.builder(); + return select(builder, exp1, exp2, exp3); } /** @@ -449,9 +449,8 @@ public class Query SQLExpression exp1, SQLExpression exp2, SQLExpression exp3, SQLExpression exp4) { - return _ctx.invoke(new FindAllQuery.Projection>( - _ctx, Projector.create(_pclass, exp1, exp2, exp3, exp4), - getClauses())); + Builder4,V1,V2,V3,V4> builder = Tuple4.builder(); + return select(builder, exp1, exp2, exp3, exp4); } /** @@ -461,13 +460,15 @@ public class Query SQLExpression exp1, SQLExpression exp2, SQLExpression exp3, SQLExpression exp4, SQLExpression exp5) { - return _ctx.invoke(new FindAllQuery.Projection>( - _ctx, Projector.create(_pclass, exp1, exp2, exp3, exp4, exp5), - getClauses())); + Builder5,V1,V2,V3,V4,V5> builder = Tuple5.builder(); + return select(builder, exp1, exp2, exp3, exp4, exp5); } /** - * Returns just the supplied expression from the rows matching the query. + * Selects the supplied expressions and writes their values into the supplied result class. The + * result expressions will be matched to the fields of the result class in declaration order. + * The fields of the result class must match the types of the selected expressions modulo the + * automatic conversions applied by reflective assignment (unboxing and widening). */ public List selectInto (Class resultClass, SQLExpression... selexps) { @@ -475,6 +476,53 @@ public class Query return _ctx.invoke(new FindAllQuery.Projection(_ctx, proj, getClauses())); } + /** + * Selects the supplied expressions and constructs a result record using their values using the + * supplied record builder. + */ + public List select ( + Builder2 builder, SQLExpression exp1, SQLExpression exp2) + { + Projector proj = Projector.create(_pclass, builder, exp1, exp2); + return _ctx.invoke(new FindAllQuery.Projection(_ctx, proj, getClauses())); + } + + /** + * Selects the supplied expressions and constructs a result record using their values using the + * supplied record builder. + */ + public List select ( + Builder3 builder, SQLExpression exp1, SQLExpression exp2, + SQLExpression exp3) + { + Projector proj = Projector.create(_pclass, builder, exp1, exp2, exp3); + return _ctx.invoke(new FindAllQuery.Projection(_ctx, proj, getClauses())); + } + + /** + * Selects the supplied expressions and constructs a result record using their values using the + * supplied record builder. + */ + public List select ( + Builder4 builder, SQLExpression exp1, SQLExpression exp2, + SQLExpression exp3, SQLExpression exp4) + { + Projector proj = Projector.create(_pclass, builder, exp1, exp2, exp3, exp4); + return _ctx.invoke(new FindAllQuery.Projection(_ctx, proj, getClauses())); + } + + /** + * Selects the supplied expressions and constructs a result record using their values using the + * supplied record builder. + */ + public List select ( + Builder5 builder, SQLExpression exp1, SQLExpression exp2, + SQLExpression exp3, SQLExpression exp4, SQLExpression exp5) + { + Projector proj = Projector.create(_pclass, builder, exp1, exp2, exp3, exp4, exp5); + return _ctx.invoke(new FindAllQuery.Projection(_ctx, proj, getClauses())); + } + /** * Deletes the records that match the configured query clauses. Note that only the where * clauses are used to evaluate a deletion. Attempts to use other clauses will result in diff --git a/src/main/java/com/samskivert/depot/impl/Projector.java b/src/main/java/com/samskivert/depot/impl/Projector.java index 9ece3ed..754d0ab 100644 --- a/src/main/java/com/samskivert/depot/impl/Projector.java +++ b/src/main/java/com/samskivert/depot/impl/Projector.java @@ -45,63 +45,63 @@ public abstract class Projector }; } - public static Projector> create ( - Class ptype, SQLExpression col1, SQLExpression col2) + public static Projector create ( + Class ptype, final Builder2 builder, + SQLExpression col1, SQLExpression col2) { - return new Projector>(ptype, new SQLExpression[] { col1, col2 }) { - @Override public Tuple2 createObject (Object[] results) { + return new Projector(ptype, new SQLExpression[] { col1, col2 }) { + @Override public R createObject (Object[] results) { @SuppressWarnings("unchecked") V1 r1 = (V1)results[0]; @SuppressWarnings("unchecked") V2 r2 = (V2)results[1]; - return new Tuple2(r1, r2); + return builder.build(r1, r2); } }; } - public static Projector> create ( - Class ptype, SQLExpression col1, SQLExpression col2, SQLExpression col3) + public static Projector create ( + Class ptype, final Builder3 builder, + SQLExpression col1, SQLExpression col2, SQLExpression col3) { - return new Projector>( + return new Projector( ptype, new SQLExpression[] { col1, col2, col3 }) { - @Override public Tuple3 createObject (Object[] results) { + @Override public R createObject (Object[] results) { @SuppressWarnings("unchecked") V1 r1 = (V1)results[0]; @SuppressWarnings("unchecked") V2 r2 = (V2)results[1]; @SuppressWarnings("unchecked") V3 r3 = (V3)results[2]; - return new Tuple3(r1, r2, r3); + return builder.build(r1, r2, r3); } }; } - public static - Projector> create ( - Class ptype, SQLExpression col1, SQLExpression col2, SQLExpression col3, - SQLExpression col4) + public static Projector create ( + Class ptype, final Builder4 builder, + SQLExpression col1, SQLExpression col2, SQLExpression col3, + SQLExpression col4) { - return new Projector>( - ptype, new SQLExpression[] { col1, col2, col3, col4 }) { - @Override public Tuple4 createObject (Object[] results) { + return new Projector(ptype, new SQLExpression[] { col1, col2, col3, col4 }) { + @Override public R createObject (Object[] results) { @SuppressWarnings("unchecked") V1 r1 = (V1)results[0]; @SuppressWarnings("unchecked") V2 r2 = (V2)results[1]; @SuppressWarnings("unchecked") V3 r3 = (V3)results[2]; @SuppressWarnings("unchecked") V4 r4 = (V4)results[3]; - return new Tuple4(r1, r2, r3, r4); + return builder.build(r1, r2, r3, r4); } }; } - public static - Projector> create ( - Class ptype, SQLExpression col1, SQLExpression col2, SQLExpression col3, - SQLExpression col4, SQLExpression col5) + public static Projector create ( + Class ptype, final Builder5 builder, + SQLExpression col1, SQLExpression col2, SQLExpression col3, + SQLExpression col4, SQLExpression col5) { - return new Projector>( - ptype, new SQLExpression[] { col1, col2, col3, col4, col5 }) { - @Override public Tuple5 createObject (Object[] results) { + return new Projector(ptype, new SQLExpression[] { col1, col2, col3, col4, col5 }) { + @Override public R createObject (Object[] results) { @SuppressWarnings("unchecked") V1 r1 = (V1)results[0]; @SuppressWarnings("unchecked") V2 r2 = (V2)results[1]; @SuppressWarnings("unchecked") V3 r3 = (V3)results[2]; @SuppressWarnings("unchecked") V4 r4 = (V4)results[3]; @SuppressWarnings("unchecked") V5 r5 = (V5)results[4]; - return new Tuple5(r1, r2, r3, r4, r5); + return builder.build(r1, r2, r3, r4, r5); } }; } diff --git a/src/main/java/com/samskivert/depot/util/Builder2.java b/src/main/java/com/samskivert/depot/util/Builder2.java new file mode 100644 index 0000000..14d0e63 --- /dev/null +++ b/src/main/java/com/samskivert/depot/util/Builder2.java @@ -0,0 +1,32 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2010 Michael Bayne and Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.util; + +/** + * A type-safe builder used to construct objects from the columns selected from Depot queries. + */ +public interface Builder2 +{ + /** + * Builds an instance, using the supplied data. + */ + public T build (A a, B b); +} diff --git a/src/main/java/com/samskivert/depot/util/Builder3.java b/src/main/java/com/samskivert/depot/util/Builder3.java new file mode 100644 index 0000000..d02d2a0 --- /dev/null +++ b/src/main/java/com/samskivert/depot/util/Builder3.java @@ -0,0 +1,32 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2010 Michael Bayne and Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.util; + +/** + * A type-safe builder used to construct objects from the columns selected from Depot queries. + */ +public interface Builder3 +{ + /** + * Builds an instance, using the supplied data. + */ + public T build (A a, B b, C c); +} diff --git a/src/main/java/com/samskivert/depot/util/Builder4.java b/src/main/java/com/samskivert/depot/util/Builder4.java new file mode 100644 index 0000000..e83a86a --- /dev/null +++ b/src/main/java/com/samskivert/depot/util/Builder4.java @@ -0,0 +1,32 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2010 Michael Bayne and Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.util; + +/** + * A type-safe builder used to construct objects from the columns selected from Depot queries. + */ +public interface Builder4 +{ + /** + * Builds an instance, using the supplied data. + */ + public T build (A a, B b, C c, D d); +} diff --git a/src/main/java/com/samskivert/depot/util/Builder5.java b/src/main/java/com/samskivert/depot/util/Builder5.java new file mode 100644 index 0000000..d787e71 --- /dev/null +++ b/src/main/java/com/samskivert/depot/util/Builder5.java @@ -0,0 +1,32 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2010 Michael Bayne and Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.util; + +/** + * A type-safe builder used to construct objects from the columns selected from Depot queries. + */ +public interface Builder5 +{ + /** + * Builds an instance, using the supplied data. + */ + public T build (A a, B b, C c, D d, E e); +} diff --git a/src/main/java/com/samskivert/depot/util/Tuple2.java b/src/main/java/com/samskivert/depot/util/Tuple2.java index b998429..4a7b6e5 100644 --- a/src/main/java/com/samskivert/depot/util/Tuple2.java +++ b/src/main/java/com/samskivert/depot/util/Tuple2.java @@ -43,6 +43,16 @@ public class Tuple2 implements Serializable return new Tuple2(a, b); } + /** Creates a builder for 2-tuples. */ + public static Builder2, A, B> builder () + { + return new Builder2, A, B>() { + public Tuple2 build (A a, B b) { + return create(a, b); + } + }; + } + /** Constructs an initialized two tuple. */ public Tuple2 (A a, B b) { diff --git a/src/main/java/com/samskivert/depot/util/Tuple3.java b/src/main/java/com/samskivert/depot/util/Tuple3.java index 9000ed0..e82e1b6 100644 --- a/src/main/java/com/samskivert/depot/util/Tuple3.java +++ b/src/main/java/com/samskivert/depot/util/Tuple3.java @@ -46,6 +46,16 @@ public class Tuple3 implements Serializable return new Tuple3(a, b, c); } + /** Creates a builder for 3-tuples. */ + public static Builder3, A, B, C> builder () + { + return new Builder3, A, B, C>() { + public Tuple3 build (A a, B b, C c) { + return create(a, b, c); + } + }; + } + /** Constructs an initialized tuple. */ public Tuple3 (A a, B b, C c) { diff --git a/src/main/java/com/samskivert/depot/util/Tuple4.java b/src/main/java/com/samskivert/depot/util/Tuple4.java index 09932d6..a40f79d 100644 --- a/src/main/java/com/samskivert/depot/util/Tuple4.java +++ b/src/main/java/com/samskivert/depot/util/Tuple4.java @@ -49,6 +49,16 @@ public class Tuple4 implements Serializable return new Tuple4(a, b, c, d); } + /** Creates a builder for 4-tuples. */ + public static Builder4, A, B, C, D> builder () + { + return new Builder4, A, B, C, D>() { + public Tuple4 build (A a, B b, C c, D d) { + return create(a, b, c, d); + } + }; + } + /** Constructs an initialized tuple. */ public Tuple4 (A a, B b, C c, D d) { diff --git a/src/main/java/com/samskivert/depot/util/Tuple5.java b/src/main/java/com/samskivert/depot/util/Tuple5.java index c12f271..55711e1 100644 --- a/src/main/java/com/samskivert/depot/util/Tuple5.java +++ b/src/main/java/com/samskivert/depot/util/Tuple5.java @@ -52,6 +52,16 @@ public class Tuple5 implements Serializable return new Tuple5(a, b, c, d, e); } + /** Creates a builder for 5-tuples. */ + public static Builder5, A, B, C, D, E> builder () + { + return new Builder5, A, B, C, D, E>() { + public Tuple5 build (A a, B b, C c, D d, E e) { + return create(a, b, c, d, e); + } + }; + } + /** Constructs an initialized tuple. */ public Tuple5 (A a, B b, C c, D d, E e) {