use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.
the class GenerateSelectSQLAction method run.
/**
* Generate select statement
*
* @see org.eclipse.jface.action.IAction#run()
*/
@Override
public void run() {
try {
String query = null;
if (_selectedNodes[0] instanceof ColumnNode) {
query = createColumnSelect();
}
if (_selectedNodes[0] instanceof TableNode) {
query = createTableSelect();
}
if (query == null) {
return;
}
SQLEditorInput input = new SQLEditorInput(// $NON-NLS-1$
"SQL Editor (" + SQLExplorerPlugin.getDefault().getEditorSerialNo() + // $NON-NLS-1$
").sql");
input.setUser(_selectedNodes[0].getSession().getUser());
IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
SQLEditor editorPart = (SQLEditor) page.openEditor(input, SQLEditor.class.getName());
editorPart.setText(query);
} catch (Throwable e) {
SQLExplorerPlugin.error("Could generate sql.", e);
}
}
use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.
the class AppendToEditorAction method run.
public void run() {
try {
TableItem[] ti = _table.getSelection();
if (ti == null || ti.length == 0) {
return;
}
String queryDelimiter = SQLExplorerPlugin.getDefault().getPluginPreferences().getString(IConstants.SQL_QRY_DELIMITER);
StringBuffer copiedText = new StringBuffer();
for (int i = 0; i < ti.length; i++) {
SQLHistoryElement el = (SQLHistoryElement) ti[i].getData();
copiedText.append(el.getRawSQLString());
copiedText.append(queryDelimiter);
if (ti.length > 1) {
copiedText.append("\n");
}
}
IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
if (page == null) {
return;
}
SQLEditor editorPart = (SQLEditor) page.getActiveEditor();
editorPart.setText(editorPart.getSQLToBeExecuted() + "\n" + copiedText.toString());
} catch (Throwable e) {
SQLExplorerPlugin.error("Error creating sql editor", e);
}
}
use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.
the class CreateTableScriptAction method run.
/**
* Create table script for selected node.
*
* @see org.eclipse.jface.action.IAction#run()
*/
public void run() {
TableNode tableNode = (TableNode) _selectedNodes[0];
ITableInfo info = tableNode.getTableInfo();
StringBuffer buf = new StringBuffer(4 * 1024);
String sep = System.getProperty("line.separator");
try {
SQLDatabaseMetaData metaData = tableNode.getSession().getMetaData();
ArrayList<String> pks = new ArrayList<String>();
PrimaryKeyInfo[] pksInfo = metaData.getPrimaryKey(info);
for (PrimaryKeyInfo pkInfo : pksInfo) pks.add(pkInfo.getColumnName());
TableColumnInfo[] columnsInfo = metaData.getColumnInfo(info);
String tableName = _selectedNodes[0].getQualifiedName();
buf.append("CREATE TABLE ");
buf.append(tableName);
buf.append("(");
for (TableColumnInfo col : columnsInfo) {
// String columnName = resultSet.getString(4);
// String typeName = resultSet.getString(6);
// String columnSize = resultSet.getString(7);
// String decimalDigits = resultSet.getString(9);
// String defaultValue = resultSet.getString(13);
boolean notNull = "NO".equalsIgnoreCase(col.isNullable());
String sLower = col.getColumnName().toLowerCase();
buf.append(sep);
buf.append(col.getColumnName() + " ");
buf.append(col.getTypeName());
boolean bNumeric = false;
if (sLower.equals("numeric") || sLower.equals("number") || sLower.equals("decimal"))
bNumeric = true;
if (sLower.indexOf("char") != -1 || sLower.indexOf("int") != -1) {
buf.append("(");
buf.append(col.getColumnSize());
buf.append(")");
} else if (bNumeric) {
buf.append("(");
buf.append(col.getColumnSize());
if (col.getDecimalDigits() > 0)
buf.append(col.getDecimalDigits());
buf.append(")");
}
if (pks.size() == 1 && pks.get(0).equals(col.getColumnName())) {
buf.append(" PRIMARY KEY");
}
String defaultValue = col.getDefaultValue();
if (defaultValue != null && !defaultValue.equals("")) {
buf.append(" default ");
boolean isSystemValue = bNumeric;
if (defaultValue.equalsIgnoreCase("CURRENT_TIMESTAMP")) {
isSystemValue = true;
}
if (!isSystemValue)
buf.append("'");
buf.append(defaultValue);
if (!isSystemValue)
buf.append("'");
}
if (notNull) {
buf.append(" not null");
}
buf.append(",");
}
buf.deleteCharAt(buf.length() - 1);
buf.append(")" + sep);
SQLEditorInput input = new SQLEditorInput("SQL Editor (" + SQLExplorerPlugin.getDefault().getEditorSerialNo() + ").sql");
input.setUser(_selectedNodes[0].getSession().getUser());
IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
SQLEditor editorPart = (SQLEditor) page.openEditor((IEditorInput) input, "net.sourceforge.sqlexplorer.plugin.editors.SQLEditor");
editorPart.setText(buf.toString());
} catch (SQLException e) {
SQLExplorerPlugin.error("Error creating export script", e);
} catch (PartInitException e) {
SQLExplorerPlugin.error("Error creating export script", e);
}
}
use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.
the class SqlexplorerService method runInDQViewer.
/**
* open the sql editor and run it.
*
* @param alias
* @param databaseConnection
* @param lEditorName
* @param query
*/
private void runInDQViewer(Alias alias, DatabaseConnection databaseConnection, String lEditorName, String query) {
String url = JavaSqlFactory.getURL(databaseConnection);
String username = JavaSqlFactory.getUsername(databaseConnection);
String password = JavaSqlFactory.getPassword(databaseConnection);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
SQLEditorInput input = new SQLEditorInput("SQL Editor (" + alias.getName() + "." + lEditorName + ").sql");
User user = alias.getUser(username);
if (PluginConstant.EMPTY_STRING.equals(username)) {
// get the user both the dbtype and username are the same.
if (!alias.getUrl().equals(url)) {
user = new net.sourceforge.sqlexplorer.dbproduct.User(username, password);
user.setAlias(alias);
alias.addUser(user);
}
} else {
if (user == null) {
user = alias.getDefaultUser();
}
}
alias.setDefaultUser(user);
// create the hive connection
if (databaseConnection != null) {
user.setDatabaseConnection(databaseConnection);
// if ManagedDriver class is not Loaded,check if it lack jars then update the realted jar.
updateDriverIfClassNotLoad(databaseConnection);
}
input.setUser(user);
// TDQ-9533 append a "limit X" in sql query for vertica database.
if (EDatabaseTypeName.VERTICA.getProduct().equals(databaseConnection.getProductId())) {
String maxPref = SQLExplorerPlugin.getDefault().getPreferenceStore().getString(IConstants.MAX_SQL_ROWS);
int maxNum = maxPref == null ? 100 : Integer.parseInt(maxPref);
query = query + " limit " + maxNum;
}
IWorkbenchPage page = SQLExplorerPlugin.getDefault().getActivePage();
try {
SQLEditor editorPart = (SQLEditor) page.openEditor(input, SQLEditor.class.getName());
editorPart.setText(query);
new ExecSQLAction(editorPart).run();
} catch (PartInitException e) {
log.error(e, e);
}
}
use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.
the class OpenInEditorAction method run.
public void run() {
try {
TableItem[] ti = _table.getSelection();
if (ti == null || ti.length == 0) {
return;
}
String queryDelimiter = SQLExplorerPlugin.getDefault().getPluginPreferences().getString(IConstants.SQL_QRY_DELIMITER);
StringBuffer copiedText = new StringBuffer();
for (int i = 0; i < ti.length; i++) {
SQLHistoryElement el = (SQLHistoryElement) ti[i].getData();
copiedText.append(el.getRawSQLString());
if (ti.length > 0) {
copiedText.append(queryDelimiter);
copiedText.append("\n");
}
}
SQLHistoryElement sqlHistoryElement = (SQLHistoryElement) ti[0].getData();
User user = sqlHistoryElement.getUser();
Alias alias;
if (user != null)
alias = user.getAlias();
else {
alias = sqlHistoryElement.getAlias();
if (alias != null)
user = alias.getDefaultUser();
if (user == null) {
ConnectionsView view = SQLExplorerPlugin.getDefault().getConnectionsView();
if (view != null)
user = view.getDefaultUser();
}
}
if (user != null && !user.hasAuthenticated()) {
boolean okToOpen = MessageDialog.openConfirm(_table.getShell(), Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Title"), Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Message.Prefix") + " " + user.getDescription() + Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Message.Postfix"));
if (okToOpen) {
OpenPasswordConnectDialogAction openDlgAction = new OpenPasswordConnectDialogAction(alias, user, false);
openDlgAction.run();
}
}
SQLEditorInput input = new SQLEditorInput("SQL Editor (" + SQLExplorerPlugin.getDefault().getEditorSerialNo() + ").sql");
input.setUser(user);
IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
if (page == null)
return;
SQLEditor editorPart = (SQLEditor) page.openEditor(input, SQLEditor.class.getName());
editorPart.setText(copiedText.toString());
} catch (Throwable e) {
SQLExplorerPlugin.error("Error creating sql editor", e);
}
}
Aggregations