001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ui.apps.dbmanager.sqlconsole; 029 030import java.io.IOException; 031import java.io.StringWriter; 032import java.util.ArrayList; 033import java.util.List; 034 035import au.com.bytecode.opencsv.CSVWriter; 036 037/** 038 * Class for storing query results.<p> 039 */ 040public class CmsSqlConsoleResults { 041 042 /** The column names. */ 043 private List<String> m_columns; 044 045 /** The row data from the result set. */ 046 private List<List<Object>> m_data; 047 048 /** 049 * Creates a new instance.<p> 050 * 051 * @param columns the column names 052 * @param data the row data 053 */ 054 public CmsSqlConsoleResults(List<String> columns, List<List<Object>> data) { 055 056 m_columns = columns; 057 m_data = data; 058 059 } 060 061 /** 062 * Gets the column names.<p> 063 * 064 * @return the list of column names 065 */ 066 public List<String> getColumns() { 067 068 return m_columns; 069 } 070 071 /** 072 * Gets the type to use for the Vaadin table column corresponding to the c-th column in this result. 073 * 074 * @param c the column index 075 * @return the class to use for the c-th Vaadin table column 076 */ 077 public Class<?> getColumnType(int c) { 078 079 for (int r = 0; r < m_data.size(); r++) { 080 Object val = m_data.get(r).get(c); 081 if (val != null) { 082 return val.getClass(); 083 } 084 } 085 return Object.class; 086 } 087 088 /** 089 * Converts the results to CSV data. 090 * 091 * @return the CSV data 092 */ 093 public String getCsv() { 094 095 StringWriter writer = new StringWriter(); 096 try (CSVWriter csv = new CSVWriter(writer)) { 097 List<String> headers = new ArrayList<>(); 098 for (String col : m_columns) { 099 headers.add(col); 100 } 101 csv.writeNext(headers.toArray(new String[] {})); 102 for (List<Object> row : m_data) { 103 List<String> colCsv = new ArrayList<>(); 104 for (Object col : row) { 105 colCsv.add(String.valueOf(col)); 106 } 107 csv.writeNext(colCsv.toArray(new String[] {})); 108 } 109 return writer.toString(); 110 } catch (IOException e) { 111 return null; 112 } 113 } 114 115 /** 116 * Gets the row data 117 * 118 * @return the row data 119 */ 120 public List<List<Object>> getData() { 121 122 return m_data; 123 } 124 125}