Commit 6ded8122 authored by Matija Obreza's avatar Matija Obreza
Browse files

JdbcRowReader with cursor access used in DataSourceLoaderImpl

parent f0f18312
......@@ -45,7 +45,7 @@ public class RowConverter {
for (int i = 0; i < columns.size(); i++) {
if (i >= row.length) {
_log.warn("Row has less than " + (i + 1) + " columns: " + ArrayUtils.toString(row));
_log.warn("Row has less than " + (i + 1) + " columns: len=" + row.length + " " + ArrayUtils.toString(row));
continue;
}
......
......@@ -108,10 +108,10 @@ public class AppWindow {
@Override
public Image getImage(Object element) {
_log.debug("getImage " + element);
if (element instanceof DataSourceFile) {
if (element instanceof IDataSource) {
return archiveIcon;
}
if (element instanceof DataSourceSheet) {
if (element instanceof IDataSourceSheet) {
return spreadsheetIcon;
}
return super.getImage(element);
......
......@@ -26,7 +26,10 @@ import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.genesys2.anno.model.DatabaseSettings;
import org.genesys2.anno.parser.RowReader;
import org.genesys2.anno.reader.JDBCRowReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
......@@ -36,6 +39,9 @@ public class DataSourceLoaderImpl implements DataSourceLoader {
private final List<DataSourceSheet> NO_SHEETS = new ArrayList<DataSourceSheet>();
private Set<DataSourceParser> parsers = new HashSet<DataSourceParser>();
@Autowired
private JDBCRowReader jdbcRowReader;
@Override
public void registerParser(DataSourceParser parser) {
_log.debug("Adding parser " + parser.getClass().getName());
......@@ -110,6 +116,17 @@ public class DataSourceLoaderImpl implements DataSourceLoader {
@Override
public RowReader createRowReader(IDataSourceSheet sheet) throws IOException, UnsupportedDataFormatException {
// Iterate over parsers and see if something comes out
if (sheet instanceof JdbcDataSourceSheet) {
JdbcDataSourceSheet jdbcSheet = (JdbcDataSourceSheet) sheet;
DatabaseSettings databaseSettings = jdbcSheet.getDatabaseSettings();
String url = databaseSettings.getUrl();
String user = databaseSettings.getUser();
String password = databaseSettings.getPassword();
return jdbcRowReader.createRowReader(jdbcSheet.getQuery(), url, user, password);
}
for (DataSourceParser parser : parsers) {
try {
if (parser.supports(sheet.getSourceFile())) {
......
......@@ -32,10 +32,17 @@ import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.*;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.dnd.DND;
......@@ -52,10 +59,21 @@ import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.genesys2.anno.converter.RowConverter;
import org.genesys2.anno.model.Column;
import org.genesys2.anno.model.ColumnDef;
import org.genesys2.anno.model.DatabaseSettings;
import org.genesys2.anno.reader.JDBCRowReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
......@@ -493,15 +511,15 @@ public class SheetDisplay extends Composite {
}
private void executeSqlQuery(String query, DSW dsw) {
IDataSourceSheet currentSheet = dsw.getDataSourceSheet();
String url = ((JdbcDataSourceSheet) currentSheet).getDatabaseSettings().getUrl();
String user = ((JdbcDataSourceSheet) currentSheet).getDatabaseSettings().getUser();
String password = ((JdbcDataSourceSheet) currentSheet).getDatabaseSettings().getPassword();
IDataSourceSheet currentSheet = dsw.getDataSourceSheet();
DatabaseSettings databaseSettings = ((JdbcDataSourceSheet) currentSheet).getDatabaseSettings();
String url = databaseSettings.getUrl();
String user = databaseSettings.getUser();
String password = databaseSettings.getPassword();
final List<Object[]> rows;
try {
rows = jdbcRowReader.getRows(query, url, user, password,100);
rows = jdbcRowReader.getRows(query, url, user, password, 300);
if (rows != null) {
currentSheet.updateData(rows);
}
......
package org.genesys2.anno.parser;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.genesys2.anno.util.ConnectionUtils;
public class JdbcRowReader implements RowReader {
private Connection conn = null;
private String query;
private PreparedStatement preparedStatement = null;
private ResultSet res;
private int columnCount;
public JdbcRowReader(String query, String url, String user, String password) {
this.query = query;
this.conn = ConnectionUtils.getConnection(url, user, password);
this.preparedStatement = null;
}
@Override
public List<Object[]> readRows(int rowsToRead) throws IOException {
List<Object[]> rows = new ArrayList<Object[]>(rowsToRead);
try {
if (this.preparedStatement == null) {
this.preparedStatement = conn.prepareStatement(query);
preparedStatement.setFetchSize(rowsToRead);
this.res = preparedStatement.executeQuery();
ResultSetMetaData metadata = res.getMetaData();
this.columnCount = metadata.getColumnCount();
}
while (res.next()) {
List<Object> row = new ArrayList<Object>();
for (int i = 1; i < columnCount; i++) {
Object val = res.getObject(i);
row.add(val);
}
rows.add(row.toArray());
}
return rows;
} catch (SQLException e) {
try {
this.preparedStatement.close();
} catch (SQLException e1) {
// NOOP
}
ConnectionUtils.close(this.conn);
throw new IOException(e.getMessage(), e);
}
}
@Override
public void setSkipRows(int skipRows) {
// ignored
}
@Override
public void close() throws IOException {
try {
this.preparedStatement.close();
} catch (SQLException e) {
// NOOP
}
ConnectionUtils.close(this.conn);
}
}
package org.genesys2.anno.reader;
import org.apache.log4j.Logger;
import org.eclipse.swt.custom.CTabItem;
import org.genesys2.anno.util.ConnectionUtils;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.genesys2.anno.parser.JdbcRowReader;
import org.genesys2.anno.parser.RowReader;
import org.genesys2.anno.util.ConnectionUtils;
public class JDBCRowReader {
private static final Logger _log = Logger.getLogger(JDBCRowReader.class);
public List<Object[]> getRows(String query, String url, String user, String password,int limit) throws SQLException {
List<Object[]> rows = new ArrayList<Object[]>();
Connection conn = ConnectionUtils.getConnection(url, user, password);
PreparedStatement ps = conn.prepareStatement(query);
ps.setMaxRows(limit);
ResultSet res = ps.executeQuery();
ResultSetMetaData metaData = res.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList<String> columns = new ArrayList<String>();
for (int i = 1; i <= columnCount; i++) {
columns.add(metaData.getColumnName(i));
}
rows.add(columns.toArray());
while (res.next()) {
List<Object> row = new ArrayList<Object>();
for (String columnName : columns) {
Object val = res.getObject(columnName);
row.add(val);
}
rows.add(row.toArray());
}
ConnectionUtils.close(conn);
return rows;
}
private static final Logger _log = Logger.getLogger(JDBCRowReader.class);
public List<Object[]> getRows(String query, String url, String user, String password, int limit) throws SQLException {
List<Object[]> rows = new ArrayList<Object[]>();
Connection conn = ConnectionUtils.getConnection(url, user, password);
try {
PreparedStatement ps = conn.prepareStatement(query);
ps.setMaxRows(limit);
ResultSet res = ps.executeQuery();
ResultSetMetaData metaData = res.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList<String> columns = new ArrayList<String>();
for (int i = 1; i <= columnCount; i++) {
columns.add(metaData.getColumnName(i));
}
rows.add(columns.toArray());
while (res.next()) {
List<Object> row = new ArrayList<Object>();
for (String columnName : columns) {
Object val = res.getObject(columnName);
row.add(val);
}
rows.add(row.toArray());
}
return rows;
} finally {
ConnectionUtils.close(conn);
}
}
public RowReader createRowReader(String query, String url, String user, String password) {
return new JdbcRowReader(query, url, user, password);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment