Coverage Report - org.apache.tapestry.contrib.table.model.sql.SimpleSqlTableDataSource
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleSqlTableDataSource
0% 
0% 
1.5
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.contrib.table.model.sql;
 16  
 
 17  
 import java.sql.Connection;
 18  
 import java.sql.ResultSet;
 19  
 import java.sql.SQLException;
 20  
 import java.sql.Statement;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import org.apache.tapestry.contrib.table.model.ITablePagingState;
 25  
 import org.apache.tapestry.contrib.table.model.ITableSortingState;
 26  
 import org.apache.tapestry.contrib.table.model.simple.SimpleTableState;
 27  
 
 28  
 /**
 29  
  * @author mindbridge
 30  
  */
 31  
 public class SimpleSqlTableDataSource implements ISqlTableDataSource
 32  
 {
 33  
 
 34  0
     private static final Log LOG = LogFactory
 35  0
             .getLog(SimpleSqlTableDataSource.class);
 36  
 
 37  
     private ISqlConnectionSource m_objConnSource;
 38  
     private String m_strTableName;
 39  
     private String m_strWhereClause;
 40  
 
 41  
     public SimpleSqlTableDataSource(ISqlConnectionSource objConnSource,
 42  
             String strTableName)
 43  
     {
 44  0
         this(objConnSource, strTableName, null);
 45  0
     }
 46  
 
 47  
     public SimpleSqlTableDataSource(ISqlConnectionSource objConnSource,
 48  
             String strTableName, String strWhereClause)
 49  0
     {
 50  0
         setConnSource(objConnSource);
 51  0
         setTableName(strTableName);
 52  0
         setWhereClause(strWhereClause);
 53  0
     }
 54  
 
 55  
     /**
 56  
      * @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#getRowCount()
 57  
      */
 58  
     public int getRowCount()
 59  
         throws SQLException
 60  
     {
 61  0
         String strQuery = generateCountQuery();
 62  0
         LOG.trace("Invoking query to count rows: " + strQuery);
 63  
 
 64  0
         Connection objConn = getConnSource().obtainConnection();
 65  
         try
 66  
         {
 67  0
             Statement objStmt = objConn.createStatement();
 68  
             try
 69  
             {
 70  0
                 ResultSet objRS = objStmt.executeQuery(strQuery);
 71  0
                 objRS.next();
 72  0
                 return objRS.getInt(1);
 73  
             }
 74  
             finally
 75  
             {
 76  0
                 objStmt.close();
 77  
             }
 78  
         }
 79  
         finally
 80  
         {
 81  0
             getConnSource().returnConnection(objConn);
 82  
         }
 83  
     }
 84  
 
 85  
     /**
 86  
      * @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#getCurrentRows(SqlTableColumnModel,
 87  
      *      SimpleTableState)
 88  
      */
 89  
     public ResultSet getCurrentRows(SqlTableColumnModel objColumnModel,
 90  
             SimpleTableState objState)
 91  
         throws SQLException
 92  
     {
 93  0
         String strQuery = generateDataQuery(objColumnModel, objState);
 94  0
         LOG.trace("Invoking query to load current rows: " + strQuery);
 95  
 
 96  0
         Connection objConn = getConnSource().obtainConnection();
 97  0
         Statement objStmt = objConn.createStatement();
 98  0
         return objStmt.executeQuery(strQuery);
 99  
     }
 100  
 
 101  
     /**
 102  
      * @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#closeResultSet(ResultSet)
 103  
      */
 104  
     public void closeResultSet(ResultSet objResultSet)
 105  
     {
 106  
         try
 107  
         {
 108  0
             Statement objStmt = objResultSet.getStatement();
 109  0
             Connection objConn = objStmt.getConnection();
 110  
             try
 111  
             {
 112  0
                 objResultSet.close();
 113  0
                 objStmt.close();
 114  
             }
 115  0
             catch (SQLException e)
 116  
             {
 117  
                 // ignore
 118  0
             }
 119  0
             getConnSource().returnConnection(objConn);
 120  
         }
 121  0
         catch (SQLException e)
 122  
         {
 123  0
             LOG.warn("Error while closing the result set", e);
 124  0
         }
 125  0
     }
 126  
 
 127  
     protected String quoteObjectName(String strObject)
 128  
     {
 129  0
         return strObject;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Returns the tableName.
 134  
      * 
 135  
      * @return String
 136  
      */
 137  
     public String getTableName()
 138  
     {
 139  0
         return m_strTableName;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Sets the tableName.
 144  
      * 
 145  
      * @param tableName
 146  
      *            The tableName to set
 147  
      */
 148  
     public void setTableName(String tableName)
 149  
     {
 150  0
         m_strTableName = tableName;
 151  0
     }
 152  
 
 153  
     /**
 154  
      * Returns the connSource.
 155  
      * 
 156  
      * @return ISqlConnectionSource
 157  
      */
 158  
     public ISqlConnectionSource getConnSource()
 159  
     {
 160  0
         return m_objConnSource;
 161  
     }
 162  
 
 163  
     /**
 164  
      * Sets the connSource.
 165  
      * 
 166  
      * @param connSource
 167  
      *            The connSource to set
 168  
      */
 169  
     public void setConnSource(ISqlConnectionSource connSource)
 170  
     {
 171  0
         m_objConnSource = connSource;
 172  0
     }
 173  
 
 174  
     /**
 175  
      * Returns the whereClause.
 176  
      * 
 177  
      * @return String
 178  
      */
 179  
     public String getWhereClause()
 180  
     {
 181  0
         return m_strWhereClause;
 182  
     }
 183  
 
 184  
     /**
 185  
      * Sets the whereClause.
 186  
      * 
 187  
      * @param whereClause
 188  
      *            The whereClause to set
 189  
      */
 190  
     public void setWhereClause(String whereClause)
 191  
     {
 192  0
         m_strWhereClause = whereClause;
 193  0
     }
 194  
 
 195  
     protected String generateColumnList(SqlTableColumnModel objColumnModel)
 196  
     {
 197  
         // build the column selection
 198  0
         StringBuffer objColumnBuf = new StringBuffer();
 199  0
         for(int i = 0; i < objColumnModel.getColumnCount(); i++)
 200  
         {
 201  0
             SqlTableColumn objColumn = objColumnModel.getSqlColumn(i);
 202  0
             if (i > 0) objColumnBuf.append(", ");
 203  0
             objColumnBuf.append(quoteObjectName(objColumn.getColumnName()));
 204  
         }
 205  
 
 206  0
         return objColumnBuf.toString();
 207  
     }
 208  
 
 209  
     protected String generateWhereClause()
 210  
     {
 211  0
         String strWhereClause = getWhereClause();
 212  0
         if (strWhereClause == null || strWhereClause.equals("")) return "";
 213  0
         return "WHERE " + strWhereClause + " ";
 214  
     }
 215  
 
 216  
     protected String generateOrderByClause(ITableSortingState objSortingState)
 217  
     {
 218  
         // build the sorting clause
 219  0
         StringBuffer objSortingBuf = new StringBuffer();
 220  0
         if (objSortingState.getSortColumn() != null)
 221  
         {
 222  0
             objSortingBuf.append("ORDER BY ");
 223  0
             objSortingBuf.append(objSortingState.getSortColumn());
 224  0
             if (objSortingState.getSortOrder() == ITableSortingState.SORT_ASCENDING)
 225  0
                 objSortingBuf.append(" ASC ");
 226  0
             else objSortingBuf.append(" DESC ");
 227  
         }
 228  
 
 229  0
         return objSortingBuf.toString();
 230  
     }
 231  
 
 232  
     protected String generateLimitClause(ITablePagingState objPagingState)
 233  
     {
 234  0
         int nPageSize = objPagingState.getPageSize();
 235  0
         int nStart = objPagingState.getCurrentPage() * nPageSize;
 236  0
         String strPagingBuf = "LIMIT " + nPageSize + " OFFSET " + nStart + " ";
 237  0
         return strPagingBuf;
 238  
     }
 239  
 
 240  
     protected String generateDataQuery(SqlTableColumnModel objColumnModel,
 241  
             SimpleTableState objState)
 242  
     {
 243  0
         String strQuery = "SELECT " + generateColumnList(objColumnModel)
 244  
                 + " FROM " + getTableName() + " " + generateWhereClause()
 245  
                 + generateOrderByClause(objState.getSortingState())
 246  
                 + generateLimitClause(objState.getPagingState());
 247  
 
 248  0
         return strQuery;
 249  
     }
 250  
 
 251  
     protected String generateCountQuery()
 252  
     {
 253  0
         String strQuery = "SELECT COUNT(*) FROM " + getTableName() + " "
 254  
                 + generateWhereClause();
 255  
 
 256  0
         return strQuery;
 257  
     }
 258  
 
 259  
 }