From dca56df7d19f40f39deb92510747d23d3dac83a7 Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Tue, 24 Apr 2012 16:22:07 +0200 Subject: [PATCH] Use of jOOQ: Improve the code to COUNT a sub-select. * src/main/java/org/glom/web/server/SqlUtils.java: Move initial query creation into build_sql_select_step_with_where_clause(). build_sql_select_count_rows(): Use the jOOQ API instead of concatentating text, because a jOOQ Select*Step is a TableLike, which is what from() takes. --- ChangeLog | 11 ++++++++ src/main/java/org/glom/web/server/SqlUtils.java | 34 +++++++++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 45bd590..34539ea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2012-04-24 Murray Cumming + + Use of jOOQ: Improve the code to COUNT a sub-select. + + * src/main/java/org/glom/web/server/SqlUtils.java: + Move initial query creation into + build_sql_select_step_with_where_clause(). + build_sql_select_count_rows(): Use the jOOQ API instead of + concatentating text, because a jOOQ Select*Step is a TableLike, + which is what from() takes. + 2012-04-23 Murray Cumming Use jOOQ instead of Glom.build_sql*(), to avoid native calls. diff --git a/src/main/java/org/glom/web/server/SqlUtils.java b/src/main/java/org/glom/web/server/SqlUtils.java index a13f2e2..809f285 100644 --- a/src/main/java/org/glom/web/server/SqlUtils.java +++ b/src/main/java/org/glom/web/server/SqlUtils.java @@ -33,6 +33,7 @@ import org.glom.libglom.Relationship; import org.glom.libglom.SortClause; import org.glom.libglom.SortFieldPair; import org.glom.libglom.Value; +import org.jooq.AggregateFunction; import org.jooq.Condition; import org.jooq.SQLDialect; import org.jooq.SelectFinalStep; @@ -251,21 +252,27 @@ public class SqlUtils { } final String query = step.getQuery().getSQL(true); - //Log.info("Query: " + query); + // Log.info("Query: " + query); return query; } - private static SelectFinalStep build_sql_select_step_with_where_clause(final Connection connection, - final String tableName, final LayoutFieldVector fieldsToGet, final Condition whereClause, - final SortClause sortClause) { - + private static SelectSelectStep createSelect(final Connection connection) { final Factory factory = new Factory(connection, SQLDialect.POSTGRES); final Settings settings = factory.getSettings(); settings.setRenderNameStyle(RenderNameStyle.QUOTED); // TODO: This doesn't seem to have any effect. settings.setRenderKeywordStyle(RenderKeywordStyle.UPPER); // TODO: Just to make debugging nicer. - // Add the fields, and any necessary joins: final SelectSelectStep selectStep = factory.select(); + return selectStep; + } + + private static SelectFinalStep build_sql_select_step_with_where_clause(final Connection connection, + final String tableName, final LayoutFieldVector fieldsToGet, final Condition whereClause, + final SortClause sortClause) { + + final SelectSelectStep selectStep = createSelect(connection); + + // Add the fields, and any necessary joins: final List listRelationships = build_sql_select_add_fields_to_get(selectStep, tableName, fieldsToGet, sortClause, false /* extraJoin */); @@ -289,20 +296,25 @@ public class SqlUtils { final LayoutFieldVector fieldsToGet) { final SelectFinalStep selectInner = build_sql_select_step_with_where_clause(connection, tableName, fieldsToGet, null, null); - return build_sql_select_count_rows(selectInner); + return build_sql_select_count_rows(connection, selectInner); } public static String build_sql_count_select_with_where_clause(final Connection connection, final String tableName, final LayoutFieldVector fieldsToGet, final Condition whereClause) { final SelectFinalStep selectInner = build_sql_select_step_with_where_clause(connection, tableName, fieldsToGet, whereClause, null); - return build_sql_select_count_rows(selectInner); + return build_sql_select_count_rows(connection, selectInner); } - private static String build_sql_select_count_rows(final SelectFinalStep selectInner) { + private static String build_sql_select_count_rows(final Connection connection, final SelectFinalStep selectInner) { // TODO: Find a way to do this with the jOOQ API: - final String query = selectInner.getQuery().getSQL(true); - return "SELECT COUNT(*) FROM (" + query + ") AS glomarbitraryalias"; + final SelectSelectStep select = createSelect(connection); + + final org.jooq.Field field = Factory.field("*"); + final AggregateFunction count = Factory.count(field); + select.select(count).from(selectInner); + return select.getQuery().getSQL(true); + // return "SELECT COUNT(*) FROM (" + query + ") AS glomarbitraryalias"; } private static List build_sql_select_add_fields_to_get(SelectSelectStep step, -- 2.1.4