net.sf.log4jdbc.sql.resultsetcollector.ResultSetCollectorPrinter Maven / Gradle / Ivy
/**
* Copyright 2010 Tim Azzopardi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sf.log4jdbc.sql.resultsetcollector;
import java.util.List;
/***
* @author Tim Azzopardi
* @author Mathieu Seppey
*
* Update : changed printResultSet into getResultSetToPrint
*
*/
public class ResultSetCollectorPrinter {
/**
* A StringBuffer which is used to build the formatted table to print
*/
private StringBuffer table = new StringBuffer(); ;
/**
* Default constructor
*/
public ResultSetCollectorPrinter() {
}
/***
* Return a table which represents a ResultSet
,
* to be printed by a logger,
* based on the content of the provided resultSetCollector
.
*
* This method will be actually called by a SpyLogDelegator
* when the next()
method of the spied ResultSet
* return false
meaning that its end is reached.
* It will be also called if the ResultSet
is closed.
*
*
* @param resultSetCollector the ResultSetCollector which has collected the data we want to print
* @return A String
which contains the formatted table to print
*
* @see net.sf.log4jdbc.ResultSetSpy
* @see net.sf.log4jdbc.sql.resultsetcollector.DefaultResultSetCollector
* @see net.sf.log4jdbc.log.SpyLogDelegator
*
*/
public String getResultSetToPrint(ResultSetCollector resultSetCollector) {
this.table.append(System.getProperty("line.separator"));
int columnCount = resultSetCollector.getColumnCount();
int maxLength[] = new int[columnCount];
for (int column = 1; column <= columnCount; column++) {
maxLength[column - 1] = resultSetCollector.getColumnName(column)
.length();
}
if (resultSetCollector.getRows() != null) {
for (List