use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.
the class AbstractSQLSourceTab method getSource.
public String getSource() {
String source = null;
SQLConnection connection = null;
ResultSet rs = null;
Statement stmt = null;
PreparedStatement pStmt = null;
int timeOut = SQLExplorerPlugin.getDefault().getPluginPreferences().getInt(IConstants.INTERACTIVE_QUERY_TIMEOUT);
try {
connection = getNode().getSession().grabConnection();
Object[] params = getSQLParameters();
if (params == null || params.length == 0) {
// use normal statement
stmt = connection.createStatement();
stmt.setQueryTimeout(timeOut);
rs = stmt.executeQuery(getSQL());
} else {
// use prepared statement
pStmt = connection.prepareStatement(getSQL());
pStmt.setQueryTimeout(timeOut);
for (int i = 0; i < params.length; i++) {
if (params[i] instanceof String) {
pStmt.setString(i + 1, (String) params[i]);
} else if (params[i] instanceof Integer) {
pStmt.setInt(i + 1, ((Integer) params[i]).intValue());
} else if (params[i] instanceof String) {
pStmt.setLong(i + 1, ((Long) params[i]).longValue());
}
}
rs = pStmt.executeQuery();
}
// $NON-NLS-1$
source = "";
while (rs.next()) {
source = source + rs.getString(1);
}
rs.close();
} catch (Exception e) {
SQLExplorerPlugin.error(Messages.getString("AbstractSQLSourceTab.cannotLoadSource") + getNode().getName(), e);
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseRs"), e);
}
if (stmt != null)
try {
stmt.close();
} catch (SQLException e) {
SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseStmt"), e);
}
if (pStmt != null)
try {
pStmt.close();
} catch (SQLException e) {
SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseStmt"), e);
}
if (connection != null)
getNode().getSession().releaseConnection(connection);
}
return source;
}
use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.
the class ConnectionTreeLabelProvider method getText.
/**
* Return the text to display
*
* @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
*/
public String getText(Object element) {
if (element instanceof Alias) {
Alias alias = (Alias) element;
String label = alias.getName();
int numSessions = 0;
for (User user : alias.getUsers()) numSessions += user.getConnections().size();
if (numSessions == 1)
return label + " (" + numSessions + " " + Messages.getString("ConnectionsView.ConnectedAlias.single.Postfix") + // $NON-NLS-1$
")";
if (numSessions > 1)
return label + " (" + numSessions + " " + Messages.getString("ConnectionsView.ConnectedAlias.multiple.Postfix") + // $NON-NLS-1$
")";
return label;
} else if (element instanceof User) {
User user = (User) element;
return user.getUserName();
} else if (element instanceof SQLConnection) {
SQLConnection connection = (SQLConnection) element;
String label;
if (connection.getDescription() == null) {
// $NON-NLS-1$
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
// $NON-NLS-2$
label = Messages.getString("ConnectionsView.ConnectedAlias.ConnectedSince") + ' ' + fmt.format(new Date(connection.getCreatedTime()));
} else
// $NON-NLS-2$
label = Messages.getString("ConnectionsView.ConnectedAlias.Connection") + ' ' + connection.getDescription();
if (connection.isPooled())
// $NON-NLS-1$
label += ' ' + Messages.getString("ConnectionsView.ConnectedAlias.Pooled");
return label;
}
return null;
}
use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.
the class CloseConnectionAction method run.
public void run() {
boolean confirm = SQLExplorerPlugin.getDefault().getPluginPreferences().getBoolean(IConstants.CONFIRM_BOOL_CLOSE_CONNECTION);
for (SQLConnection connection : getView().getSelectedConnections(false)) {
Session session = connection.getSession();
if (session != null && !session.isConnectionInUse()) {
if (confirm) {
MessageDialogWithToggle dialog = MessageDialogWithToggle.openYesNoQuestion(getView().getSite().getShell(), Messages.getString("ConnectionsView.Actions.CloseAll.Confirm.Title"), Messages.getString("ConnectionsView.Actions.CloseAll.Confirm.Message"), Messages.getString("ConnectionsView.Actions.CloseAll.Confirm.Toggle"), false, null, null);
if (dialog.getToggleState() && dialog.getReturnCode() == IDialogConstants.YES_ID)
SQLExplorerPlugin.getDefault().getPluginPreferences().setValue(IConstants.CONFIRM_BOOL_CLOSE_CONNECTION, false);
if (dialog.getReturnCode() != IDialogConstants.YES_ID)
return;
}
session.disposeConnection();
} else if (session == null)
connection.getUser().releaseFromPool(connection);
}
getView().refresh();
}
use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.
the class SQLEditorCatalogSwitcher method createControl.
protected Control createControl(Composite parent) {
_catalogCombo = new Combo(parent, SWT.READ_ONLY);
_catalogCombo.setToolTipText(Messages.getString("SQLEditor.Actions.ChooseCatalog.ToolTip"));
_catalogCombo.setSize(200, _catalogCombo.getSize().y);
_catalogCombo.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
int selIndex = _catalogCombo.getSelectionIndex();
String newCat = _catalogCombo.getItem(selIndex);
if (_editor.getSession() != null) {
try {
_editor.getSession().setCatalog(newCat);
} catch (Exception e1) {
SQLExplorerPlugin.error("Error changing catalog", e1);
}
}
}
});
_catalogCombo.add("");
if (_editor.getSession() != null) {
try {
String[] catalogs = getMetaDataSession().getCatalogs();
User user = _editor.getSession().getUser();
// Get the connection directly from the user because the session may be busy with its one one
SQLConnection connection = user.getConnection();
try {
String currentCatalog = connection.getCatalog();
for (int i = 0; i < catalogs.length; i++) {
_catalogCombo.add(catalogs[i]);
if (currentCatalog.equals(catalogs[i])) {
_catalogCombo.select(_catalogCombo.getItemCount() - 1);
}
}
} finally {
if (connection != null)
user.releaseConnection(connection);
}
} catch (SQLException e) {
SQLExplorerPlugin.error(e);
}
}
return _catalogCombo;
}
use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.
the class ConnectionsView method createPartControl.
/**
* @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createPartControl(Composite parent) {
// $NON-NLS-1$
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, SQLExplorerPlugin.PLUGIN_ID + ".AliasView");
SQLExplorerPlugin.getDefault().getAliasManager().addListener(this);
// create outline
_treeViewer = new TreeViewer(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
getSite().setSelectionProvider(_treeViewer);
// create action bar
IToolBarManager toolBarMgr = getViewSite().getActionBars().getToolBarManager();
// PTODO qzhang delete the Context meun in the Connections View for feature 3519.
// toolBarMgr.add(new NewAliasAction());
// toolBarMgr.add(new NewEditorAction());
// toolBarMgr.add(new NewDatabaseStructureViewAction());
// toolBarMgr.add(new CloseAllConnectionsAction());
// toolBarMgr.add(new CloseConnectionAction());
// use hash lookup to improve performance
_treeViewer.setUseHashlookup(true);
// add content and label provider
_treeViewer.setContentProvider(new ConnectionTreeContentProvider());
_treeViewer.setLabelProvider(new ConnectionTreeLabelProvider());
// Add yyi 2010-09-15 14549: hide connections in SQL Explorer when a connection is moved to the trash bin
_treeViewer.addFilter(new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
try {
IFile file = SQLExplorerPlugin.getDefault().getPropertyFile().get(element);
if (null != file && file.exists()) {
// $NON-NLS-1$
return !FileUtils.readFileToString(file.getLocation().toFile()).contains("deleted=\"true\"");
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
});
// ~
// set input session
_treeViewer.setInput(SQLExplorerPlugin.getDefault().getAliasManager());
// doubleclick on alias opens session
_treeViewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
if (selection != null) {
User user = null;
Object selected = selection.getFirstElement();
if (selected instanceof Alias) {
Alias alias = (Alias) selection.getFirstElement();
user = alias.getDefaultUser();
} else if (selected instanceof User) {
user = (User) selected;
} else if (selected instanceof SQLConnection) {
user = ((SQLConnection) selected).getUser();
}
if (user != null) {
openNewEditor(user);
}
}
}
});
_treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
refreshToolbar();
}
});
// add context menu
final ConnectionTreeActionGroup actionGroup = new ConnectionTreeActionGroup();
// $NON-NLS-1$
MenuManager menuManager = new MenuManager("ConnectionTreeContextMenu");
menuManager.setRemoveAllWhenShown(true);
Menu contextMenu = menuManager.createContextMenu(_treeViewer.getTree());
_treeViewer.getTree().setMenu(contextMenu);
menuManager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
actionGroup.fillContextMenu(manager);
}
});
_treeViewer.expandToLevel(2);
parent.layout();
SQLExplorerPlugin.getDefault().startDefaultConnections(this);
}
Aggregations