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;
28 import org.glom.libglom.Document;
29 import org.glom.libglom.Field;
30 import org.glom.libglom.LayoutFieldVector;
31 import org.glom.libglom.LayoutItem_Field;
32 import org.glom.libglom.SortClause;
33 import org.glom.libglom.SortFieldPair;
34 import org.glom.web.server.Log;
35 import org.glom.web.server.Utils;
36 import org.glom.web.shared.DataItem;
38 import com.mchange.v2.c3p0.ComboPooledDataSource;
41 * @author Ben Konrath <ben@bagu.org>
44 public abstract class ListDBAccess extends DBAccess {
45 protected LayoutFieldVector fieldsToGet;
47 protected ListDBAccess(Document document, String documentID, ComboPooledDataSource cpds, String tableName) {
48 super(document, documentID, cpds, tableName);
51 protected abstract String getSelectQuery(SortClause sortClause);
53 protected abstract String getCountQuery();
55 protected ArrayList<DataItem[]> getListData(int start, int length, boolean useSortClause, int sortColumnIndex,
56 boolean isAscending) {
58 // Add a LayoutItem_Field for the primary key to the end of the LayoutFieldVector if it doesn't already contain
60 if (getPrimaryKeyIndex() < 0) {
61 fieldsToGet.add(getPrimaryKeyLayoutItemField());
64 // create a sort clause for the column we've been asked to sort
65 SortClause sortClause = new SortClause();
67 org.glom.libglom.LayoutItem item = fieldsToGet.get(sortColumnIndex);
68 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(item);
69 if (layoutItemField != null)
70 sortClause.addLast(new SortFieldPair(layoutItemField, isAscending));
72 Log.error(documentID, tableName, "Error getting LayoutItem_Field for column index " + sortColumnIndex
73 + ". Cannot create a sort clause for this column.");
76 // create a sort clause for the primary key if we're not asked to sort a specific column
77 int numItems = Utils.safeLongToInt(fieldsToGet.size());
78 for (int i = 0; i < numItems; i++) {
79 LayoutItem_Field layoutItem = fieldsToGet.get(i);
80 Field details = layoutItem.get_full_field_details();
81 if (details != null && details.get_primary_key()) {
82 sortClause.addLast(new SortFieldPair(layoutItem, true)); // ascending
88 ArrayList<DataItem[]> rowsList = new ArrayList<DataItem[]>();
89 Connection conn = null;
93 // Setup the JDBC driver and get the query. Special care needs to be take to ensure that the results will be
94 // based on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount
95 // of data. Here's the relevant PostgreSQL documentation:
96 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
97 conn = cpds.getConnection();
98 conn.setAutoCommit(false);
99 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
100 st.setFetchSize(length);
101 String query = getSelectQuery(sortClause) + " OFFSET " + start;
102 // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
103 // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
104 // memory footprint. Check the difference between this value before and after the query:
105 // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
106 // Test the execution time at the same time (see the todo item in getLayoutListTable()).
107 rs = st.executeQuery(query);
109 // get the results from the ResultSet
110 rowsList = convertResultSetToDTO(length, fieldsToGet, rs);
111 } catch (SQLException e) {
112 Log.error(documentID, tableName, "Error executing database query.", e);
113 // TODO: somehow notify user of problem
115 // cleanup everything that has been used
123 } catch (Exception e) {
124 Log.error(documentID, tableName,
125 "Error closing database resources. Subsequent database queries may not work.", e);
132 * Get the number of rows a query with the table name and layout fields would return. This is needed for the /* list
135 protected int getResultSizeOfSQLQuery() {
137 // Add a LayoutItem_Field for the primary key to the end of the LayoutFieldVector if it doesn't already contain
139 if (getPrimaryKeyIndex() < 0) {
140 fieldsToGet.add(getPrimaryKeyLayoutItemField());
143 Connection conn = null;
147 // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
148 // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
149 // data. Here's the relevant PostgreSQL documentation:
150 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
151 conn = cpds.getConnection();
152 conn.setAutoCommit(false);
153 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
154 String query = getCountQuery();
155 // TODO Test execution time of this query with when the number of rows in the table is large (say >
156 // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
157 rs = st.executeQuery(query);
159 // get the number of rows in the query
163 } catch (SQLException e) {
164 Log.error(documentID, tableName, "Error calculating number of rows in the query.", e);
167 // cleanup everything that has been used
175 } catch (Exception e) {
176 Log.error(documentID, tableName,
177 "Error closing database resources. Subsequent database queries may not work.", e);
183 * Gets the primary key index of this list layout.
185 * @return index of primary key or -1 if a primary key was not found
187 public int getPrimaryKeyIndex() {
188 for (int i = 0; i < fieldsToGet.size(); i++) {
189 LayoutItem_Field layoutItemField = fieldsToGet.get(i);
190 Field field = layoutItemField.get_full_field_details();
191 if (field != null && field.get_primary_key())