Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* eobjects.org AnalyzerBeans
* Copyright (C) 2010 eobjects.org
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.eobjects.analyzer.storage;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.eobjects.analyzer.data.InputColumn;
import org.eobjects.analyzer.data.InputRow;
import org.eobjects.analyzer.data.MockInputRow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SqlDatabaseRowAnnotationFactory implements RowAnnotationFactory {
private final static Logger logger = LoggerFactory.getLogger(SqlDatabaseRowAnnotationFactory.class);
private final Map, String> _inputColumnNames = new LinkedHashMap, String>();
private final Map _annotationColumnNames = new HashMap();
private final Connection _connection;
private final String _tableName;
private final AtomicInteger _nextColumnIndex = new AtomicInteger(1);
public SqlDatabaseRowAnnotationFactory(Connection connection, String tableName) {
_connection = connection;
_tableName = tableName;
String intType = SqlDatabaseUtils.getSqlType(Integer.class);
performUpdate(SqlDatabaseUtils.CREATE_TABLE_PREFIX + tableName + " (id " + intType
+ " PRIMARY KEY, distinct_count " + intType + ")");
}
@Override
protected void finalize() throws Throwable {
super.finalize();
performUpdate("DROP TABLE " + _tableName);
}
private void performUpdate(String sql) {
SqlDatabaseUtils.performUpdate(_connection, sql);
}
@Override
public RowAnnotation createAnnotation() {
return new RowAnnotationImpl();
}
private boolean containsRow(InputRow row) {
ResultSet rs = null;
PreparedStatement st = null;
try {
st = _connection.prepareStatement("SELECT COUNT(*) FROM " + _tableName + " WHERE id = ?");
boolean contains;
st.setInt(1, row.getId());
rs = st.executeQuery();
if (rs.next()) {
int count = rs.getInt(1);
if (count == 0) {
contains = false;
} else if (count == 1) {
contains = true;
} else {
throw new IllegalStateException(count + " rows with id=" + row.getId() + " exists in database!");
}
} else {
contains = false;
}
return contains;
} catch (SQLException e) {
throw new IllegalStateException(e);
} finally {
SqlDatabaseUtils.safeClose(rs, st);
}
}
@Override
public void annotate(InputRow[] rows, RowAnnotation annotation) {
for (InputRow row : rows) {
annotate(row, 1, annotation);
}
}
@Override
public synchronized void annotate(InputRow row, int distinctCount, RowAnnotation annotation) {
RowAnnotationImpl a = (RowAnnotationImpl) annotation;
List> inputColumns = row.getInputColumns();
List columnNames = new ArrayList(inputColumns.size());
List