2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.server.database;
22 import java.sql.Connection;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.glom.web.server.Log;
30 import org.glom.web.server.Utils;
31 import org.glom.web.server.libglom.Document;
32 import org.glom.web.shared.DataItem;
33 import org.glom.web.shared.libglom.Field;
34 import org.glom.web.shared.libglom.layout.LayoutItem;
35 import org.glom.web.shared.libglom.layout.LayoutItemField;
36 import org.glom.web.shared.libglom.layout.SortClause;
37 import org.glom.web.shared.libglom.layout.UsesRelationship;
39 import com.mchange.v2.c3p0.ComboPooledDataSource;
44 public abstract class ListDBAccess extends DBAccess {
45 protected List<LayoutItemField> fieldsToGet;
47 protected ListDBAccess(final Document document, final String documentID, final ComboPooledDataSource cpds,
48 final String tableName) {
49 super(document, documentID, cpds, tableName);
52 protected abstract String getSelectQuery(Connection connection, String quickFind, SortClause sortClause);
54 protected abstract String getCountQuery(Connection connection);
56 protected ArrayList<DataItem[]> getListData(final String quickFind, final int start, final int length,
57 final boolean useSortClause, final int sortColumnIndex, final boolean isAscending) {
59 // create a sort clause for the column we've been asked to sort
60 final SortClause sortClause = new SortClause();
62 final LayoutItem item = fieldsToGet.get(sortColumnIndex);
63 if (item instanceof LayoutItemField) {
64 final UsesRelationship layoutItemField = (UsesRelationship) item;
65 sortClause.add(new SortClause.SortField(layoutItemField, isAscending));
67 Log.error(documentID, tableName, "Error getting LayoutItemField for column index " + sortColumnIndex
68 + ". Cannot create a sort clause for this column.");
71 // create a sort clause for the primary key if we're not asked to sort a specific column
73 if(fieldsToGet != null) {
74 numItems = Utils.safeLongToInt(fieldsToGet.size());
77 for (int i = 0; i < numItems; i++) {
78 final LayoutItemField layoutItem = fieldsToGet.get(i);
79 final Field details = layoutItem.getFullFieldDetails();
80 if (details != null && details.getPrimaryKey()) {
81 sortClause.add(new SortClause.SortField(layoutItem, true)); // ascending
87 ArrayList<DataItem[]> rowsList = new ArrayList<DataItem[]>();
88 Connection conn = null;
92 // Setup the JDBC driver and get the query. Special care needs to be take to ensure that the results will be
93 // based on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount
94 // of data. Here's the relevant PostgreSQL documentation:
95 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
96 conn = cpds.getConnection();
97 conn.setAutoCommit(false);
98 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
99 st.setFetchSize(length);
100 final String query = getSelectQuery(conn, quickFind, sortClause) + " OFFSET " + start;
101 // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
102 // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
103 // memory footprint. Check the difference between this value before and after the query:
104 // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
105 // Test the execution time at the same time (see the todo item in getLayoutListTable()).
106 rs = st.executeQuery(query);
108 // get the results from the ResultSet
109 rowsList = convertResultSetToDTO(length, fieldsToGet, rs);
110 } catch (final SQLException e) {
111 Log.error(documentID, tableName, "Error executing database query.", e);
112 // TODO: somehow notify user of problem
114 // cleanup everything that has been used
122 } catch (final Exception e) {
123 Log.error(documentID, tableName,
124 "Error closing database resources. Subsequent database queries may not work.", e);
131 * Get the number of rows a query with the table name and layout fields would return. This is needed for the /* list
134 protected int getResultSizeOfSQLQuery() {
136 Connection conn = null;
140 // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
141 // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
142 // data. Here's the relevant PostgreSQL documentation:
143 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
144 conn = cpds.getConnection();
145 conn.setAutoCommit(false);
146 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
147 final String query = getCountQuery(conn);
148 // TODO Test execution time of this query with when the number of rows in the table is large (say >
149 // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
150 rs = st.executeQuery(query);
152 // get the number of rows in the query
156 } catch (final SQLException e) {
157 Log.error(documentID, tableName, "Error calculating number of rows in the query.", e);
160 // cleanup everything that has been used
168 } catch (final Exception e) {
169 Log.error(documentID, tableName,
170 "Error closing database resources. Subsequent database queries may not work.", e);