提交 1b730dd2 编写于 作者: J jurgen

Select row count for custom query

上级 31b0c854
......@@ -97,6 +97,8 @@ command.org.jkiss.dbeaver.ui.editors.sql.run.script.name=Execute SQL Script
command.org.jkiss.dbeaver.ui.editors.sql.run.script.description=Execute script
command.org.jkiss.dbeaver.ui.editors.sql.run.scriptNew.name=Execute Script In Separate Tabs
command.org.jkiss.dbeaver.ui.editors.sql.run.scriptNew.description=Execute script's statements in separate results tabs
command.org.jkiss.dbeaver.ui.editors.sql.run.count.name=Select row count for query
command.org.jkiss.dbeaver.ui.editors.sql.run.count.description=Select row count for query under cursor
command.org.jkiss.dbeaver.ui.editors.sql.run.explain.name=Explain Execution Plan
command.org.jkiss.dbeaver.ui.editors.sql.run.explain.description=Explain execution plan
command.org.jkiss.dbeaver.ui.editors.sql.run.validate.name=Validate Query
......
......@@ -203,6 +203,7 @@
<command id="org.jkiss.dbeaver.ui.editors.sql.run.explain" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.explain.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.explain.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.run.validate" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.validate.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.validate.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.run.analyse" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.analyse.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.analyse.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.run.count" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.count.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.count.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.open.file" name="%command.org.jkiss.dbeaver.ui.editors.sql.open.file.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.open.file.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.save.file" name="%command.org.jkiss.dbeaver.ui.editors.sql.save.file.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.save.file.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.comment.single" name="%command.org.jkiss.dbeaver.ui.editors.sql.comment.single.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.comment.single.description" categoryId="org.jkiss.dbeaver.core.sql"/>
......@@ -772,6 +773,15 @@
</with>
</enabledWhen>
</handler>
<handler commandId="org.jkiss.dbeaver.ui.editors.sql.run.count" class="org.jkiss.dbeaver.ui.editors.sql.handlers.ExecuteStatementCountHandler">
<enabledWhen>
<with variable="activePart">
<adapt type="org.jkiss.dbeaver.ui.editors.sql.SQLEditor">
<test property="org.jkiss.dbeaver.ui.editors.sql.canExecute" value="statement"/>
</adapt>
</with>
</enabledWhen>
</handler>
<handler commandId="org.jkiss.dbeaver.ui.editors.sql.run.explain" class="org.jkiss.dbeaver.ui.editors.sql.handlers.ExplainPlanHandler">
<enabledWhen>
<with variable="activePart">
......@@ -1516,6 +1526,13 @@
</with>
</visibleWhen>
</command>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.count">
<visibleWhen>
<with variable="activeEditor">
<instanceof value="org.jkiss.dbeaver.ui.editors.sql.SQLEditor"/>
</with>
</visibleWhen>
</command>
<separator name="query" visible="true"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.explain">
<visibleWhen>
......@@ -1576,6 +1593,7 @@
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.statementNew"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.script"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.scriptNew"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.count"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.export.data"/>
<separator name="other" visible="true"/>
</menuContribution>
......@@ -1649,6 +1667,11 @@
contextId="org.jkiss.dbeaver.ui.editors.sql"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="CTRL+ALT+X"/>
<key
commandId="org.jkiss.dbeaver.ui.editors.sql.run.count"
contextId="org.jkiss.dbeaver.ui.editors.sql"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="CTRL+ALT+C"/>
<key
commandId="org.jkiss.dbeaver.ui.editors.sql.run.explain"
contextId="org.jkiss.dbeaver.ui.editors.sql"
......
......@@ -112,6 +112,10 @@ public class SQLQuery {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public Statement getStatement() {
return statement;
}
......
/*
* Copyright (C) 2010-2014 Serge Rieder
* serge@jkiss.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.model.sql;
/**
* SQLQuery transformer callback
*/
public interface SQLQueryTransformer {
void transformQuery(SQLQuery query);
}
......@@ -52,8 +52,6 @@ public class SQLSemanticProcessor {
private static final String NESTED_QUERY_AlIAS = "z_q";
static final Log log = LogFactory.getLog(SQLUtils.class);
public static boolean isSelectQuery(String query)
{
try {
......
......@@ -65,6 +65,7 @@ import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.model.sql.SQLDataSource;
import org.jkiss.dbeaver.model.sql.SQLQuery;
import org.jkiss.dbeaver.model.sql.SQLQueryResult;
import org.jkiss.dbeaver.model.sql.SQLQueryTransformer;
import org.jkiss.dbeaver.model.struct.DBSDataContainer;
import org.jkiss.dbeaver.model.struct.DBSDataSourceContainer;
import org.jkiss.dbeaver.model.struct.DBSObject;
......@@ -507,7 +508,11 @@ public class SQLEditor extends SQLEditorBase
}
}
public void processSQL(boolean newTab, boolean script)
public void processSQL(boolean newTab, boolean script) {
processSQL(newTab, script, null);
}
public void processSQL(boolean newTab, boolean script, SQLQueryTransformer transformer)
{
IDocument document = getDocument();
if (document == null) {
......@@ -516,20 +521,29 @@ public class SQLEditor extends SQLEditorBase
}
if (script) {
// Execute all SQL statements consequently
List<SQLQuery> statementInfos;
List<SQLQuery> queries;
ITextSelection selection = (ITextSelection) getSelectionProvider().getSelection();
if (selection.getLength() > 1) {
statementInfos = extractScriptQueries(selection.getOffset(), selection.getLength());
queries = extractScriptQueries(selection.getOffset(), selection.getLength());
} else {
statementInfos = extractScriptQueries(0, document.getLength());
queries = extractScriptQueries(0, document.getLength());
}
if (transformer != null) {
for (SQLQuery query : queries) {
transformer.transformQuery(query);
}
processQueries(statementInfos, newTab, false);
}
processQueries(queries, newTab, false);
} else {
// Execute statement under cursor or selected text (if selection present)
SQLQuery sqlQuery = extractActiveQuery();
if (sqlQuery == null) {
setStatus(CoreMessages.editors_sql_status_empty_query_string, true);
} else {
if (transformer != null) {
transformer.transformQuery(sqlQuery);
}
processQueries(Collections.singletonList(sqlQuery), newTab, false);
}
}
......
/*
* Copyright (C) 2010-2014 Serge Rieder
* serge@jkiss.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.ui.editors.sql.handlers;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.sql.SQLQuery;
import org.jkiss.dbeaver.model.sql.SQLQueryTransformer;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ExecuteStatementCountHandler extends AbstractHandler implements SQLQueryTransformer {
static final Log log = LogFactory.getLog(ExecuteStatementCountHandler.class);
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
SQLEditor editor = RuntimeUtils.getObjectAdapter(HandlerUtil.getActiveEditor(event), SQLEditor.class);
if (editor != null) {
editor.processSQL(false, false, this);
}
return null;
}
@Override
public void transformQuery(SQLQuery query) {
try {
Statement statement = CCJSqlParserUtil.parse(query.getQuery());
if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
List<SelectItem> selectItems = new ArrayList<SelectItem>();
Function countFunc = new Function();
countFunc.setName("count");
countFunc.setParameters(new ExpressionList(Collections.<Expression>singletonList(new Column("*"))));
SelectItem countItem = new SelectExpressionItem(countFunc);
selectItems.add(countItem);
select.setSelectItems(selectItems);
query.setQuery(select.toString());
} else {
throw new DBException("Query [" + query.getQuery() + "] can't be modified");
}
} catch (Exception e) {
log.error(e);
}
System.out.println(query);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册