All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jbehaviour.plugins.jdbc.JBehaviourResultSet Maven / Gradle / Ivy

/**
 *  Copyright 2012 Yannick Roffin
 *
 *   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 org.jbehaviour.plugins.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;

/**
 * implements a light and simple result set
 */
public class JBehaviourResultSet implements IBehaviourResultSet {
	private List columns = new ArrayList();
	private List> values = new ArrayList>();
	private ArrayList current;
	StringBuilder sbFormat = new StringBuilder();
	private Formatter formatter = null;
	private int columnLength = 16;

	/**
	 * create it from rs
	 * @param columnLength 
	 * @param rs
	 * @throws SQLException
	 */
	public JBehaviourResultSet(int _columnLength, ResultSet rs) throws SQLException {
		columnLength = _columnLength;
		formatter = new Formatter(sbFormat);

		/**
		 * compute columns
		 */
		int iMaxCol = rs.getMetaData().getColumnCount() + 1;
		for(int iCol=1;iCol line : values) {
			int iColValue=0;
			for(String column : line) {
				sb.append(format(iColValue++,column));
			}
			sb.append("\n");
		}
		sb.append(header);
		return sb.toString();
	}

	@Override
	public void addColumns(String name) {
		columns.add(name);
	}
	
	@Override
	public void addValues(String value, boolean first, boolean last) {
		if(first) {
			current = new ArrayList();
		}
		current.add(value);
		if(last) {
			values.add(current);
			current = null;
		}
	}

	@Override
	public Integer getRows() {
		return values.size();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy