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

de.akquinet.jbosscc.guttenbase.tools.ReadTableDataTool Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package de.akquinet.jbosscc.guttenbase.tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.akquinet.jbosscc.guttenbase.configuration.SourceDatabaseConfiguration;
import de.akquinet.jbosscc.guttenbase.connector.Connector;
import de.akquinet.jbosscc.guttenbase.hints.ColumnOrderHint;
import de.akquinet.jbosscc.guttenbase.mapping.ColumnNameMapper;
import de.akquinet.jbosscc.guttenbase.mapping.TableNameMapper;
import de.akquinet.jbosscc.guttenbase.meta.ColumnMetaData;
import de.akquinet.jbosscc.guttenbase.meta.ColumnType;
import de.akquinet.jbosscc.guttenbase.meta.TableMetaData;
import de.akquinet.jbosscc.guttenbase.repository.ConnectorRepository;
import de.akquinet.jbosscc.guttenbase.statements.SelectStatementCreator;

/**
 * Read data from given table and put into a map.
 * 
 * 

* © 2012 akquinet tech@spree *

* * @author M. Dahm */ public class ReadTableDataTool { private final ConnectorRepository _connectorRepository; public ReadTableDataTool(final ConnectorRepository connectorRepository) { assert connectorRepository != null : "connectorRepository != null"; _connectorRepository = connectorRepository; } /** * @param lines * -1 means read all lines * @return map containing table data */ public List> readTableData(final String connectorId, final TableMetaData tableMetaData, final int lines) throws SQLException { final SourceDatabaseConfiguration sourceDatabaseConfiguration = _connectorRepository.getSourceDatabaseConfiguration(connectorId); final Connector connector = _connectorRepository.createConnector(connectorId); final Connection connection = connector.openConnection(); sourceDatabaseConfiguration.initializeSourceConnection(connection, connectorId); final List> result = readTableData(connection, connectorId, sourceDatabaseConfiguration, tableMetaData, lines < 0 ? Integer.MAX_VALUE : lines); sourceDatabaseConfiguration.finalizeSourceConnection(connection, connectorId); connector.closeConnection(); return result; } private List> readTableData(final Connection connection, final String connectorId, final SourceDatabaseConfiguration sourceConfiguration, final TableMetaData tableMetaData, final int lines) throws SQLException { final List> result = new ArrayList>(); final String tableName = _connectorRepository.getConnectorHint(connectorId, TableNameMapper.class).getValue() .mapTableName(tableMetaData); final CommonColumnTypeResolverTool commonColumnTypeResolver = new CommonColumnTypeResolverTool(_connectorRepository); final ColumnNameMapper sourceColumnNameMapper = _connectorRepository.getConnectorHint(connectorId, ColumnNameMapper.class).getValue(); final PreparedStatement selectStatement = new SelectStatementCreator(_connectorRepository, connectorId).createSelectStatement( tableName, tableMetaData, connection); selectStatement.setFetchSize(lines); sourceConfiguration.beforeSelect(connection, connectorId, tableMetaData); final ResultSet resultSet = selectStatement.executeQuery(); sourceConfiguration.afterSelect(connection, connectorId, tableMetaData); final List orderedSourceColumns = ColumnOrderHint.getSortedColumns(_connectorRepository, connectorId, tableMetaData); int rowIndex = 1; try { while (resultSet.next() && rowIndex <= lines) { final Map rowData = new HashMap(); for (int columnIndex = 1; columnIndex <= orderedSourceColumns.size(); columnIndex++) { final ColumnMetaData sourceColumn = orderedSourceColumns.get(columnIndex - 1); final String columnName = sourceColumnNameMapper.mapColumnName(sourceColumn); final ColumnType sourceColumnType = commonColumnTypeResolver.getColumnType(connectorId, sourceColumn); final Object data = sourceColumnType.getValue(resultSet, columnIndex); rowData.put(columnName, data); } result.add(rowData); rowIndex++; } } finally { try { resultSet.close(); selectStatement.close(); } catch (final Exception e) { } } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy