提交 b3542eae 编写于 作者: J jurgen

Oracle ROWNUM limit transformer

Former-commit-id: 1fe08840
上级 927ef507
......@@ -42,6 +42,7 @@ Export-Package: org.jkiss.dbeaver,
org.jkiss.dbeaver.model.qm,
org.jkiss.dbeaver.model.runtime,
org.jkiss.dbeaver.model.sql,
org.jkiss.dbeaver.model.sql.parser,
org.jkiss.dbeaver.model.struct,
org.jkiss.dbeaver.model.struct.rdb,
org.jkiss.dbeaver.model.virtual,
......
......@@ -81,24 +81,13 @@ public class DBDRowIdentifier implements DBPObject {
DBDAttributeBinding binding = DBUtils.findBinding(bindings, cColumn.getAttribute());
if (binding != null) {
this.attributes.add(binding);
} else {
// If at least one attribute is missing - this ID won't work anyway
// so let's just clean it up
this.attributes.clear();
break;
}
// for (DBDAttributeBinding binding : bindings) {
//
// if (binding.matches(cColumn.getAttribute(), false)) {
// this.attributes.add(binding);
// break;
// }
// }
}
}
/*
public Object[] getKeyValues(Object[] row) {
Object[] keyValues = new Object[keyColumns.size()];
for (DBSTableColumn column : keyColumns) {
keyColumns
}
return keyValues;
}
*/
}
......@@ -229,7 +229,7 @@ public class JDBCStatementImpl<STATEMENT extends Statement> implements JDBCState
}
/*if (connection.getDataSource().getInfo().supportsResultSetLimit())*/ {
try {
getOriginal().setMaxRows(totalRows);
setMaxRows(totalRows);
}
catch (SQLException e) {
// [JDBC:ODBC] Probably setMaxRows is not supported. Just log this error
......
......@@ -104,18 +104,8 @@ public class SQLSemanticProcessor {
if (filter.hasConditions()) {
StringBuilder whereString = new StringBuilder();
SQLUtils.appendConditionString(filter, dataSource, tableAlias, whereString, true);
Expression filterWhere;
try {
filterWhere = CCJSqlParserUtil.parseCondExpression(whereString.toString());
} catch (JSQLParserException e) {
throw new JSQLParserException("Bad query condition: [" + whereString + "]", e);
}
Expression sourceWhere = select.getWhere();
if (sourceWhere == null) {
select.setWhere(filterWhere);
} else {
select.setWhere(new AndExpression(new Parenthesis(sourceWhere), filterWhere));
}
String condString = whereString.toString();
addWhereToSelect(select, condString);
}
// ORDER
if (filter.hasOrdering()) {
......@@ -140,4 +130,19 @@ public class SQLSemanticProcessor {
}
public static void addWhereToSelect(PlainSelect select, String condString) throws JSQLParserException {
Expression filterWhere;
try {
filterWhere = CCJSqlParserUtil.parseCondExpression(condString);
} catch (JSQLParserException e) {
throw new JSQLParserException("Bad query condition: [" + condString + "]", e);
}
Expression sourceWhere = select.getWhere();
if (sourceWhere == null) {
select.setWhere(filterWhere);
} else {
select.setWhere(new AndExpression(new Parenthesis(sourceWhere), filterWhere));
}
}
}
......@@ -1817,10 +1817,12 @@ public class ResultSetViewer extends Viewer
// Check attributes of non-virtual identifier
DBDRowIdentifier rowIdentifier = model.getVisibleAttribute(0).getRowIdentifier();
if (rowIdentifier == null) {
// We shouldn't be here ever!
// Virtual id should be created if we missing natural one
UIUtils.showErrorDialog(
viewerPanel.getShell(),
"No entity identifier",
"Entity " + entity.getName() + " has no identifier");
"Entity " + entity.getName() + " has no unique key");
return false;
} else if (CommonUtils.isEmpty(rowIdentifier.getAttributes())) {
UIUtils.showErrorDialog(
......
......@@ -11,7 +11,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.jface.text,
org.jkiss.dbeaver.core,
org.eclipse.ui.workbench.texteditor,
com.oracle.jdbc
com.oracle.jdbc,
net.sf.jsqlparser
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Vendor: JKISS
......
/*
* Copyright (C) 2010-2015 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.ext.oracle.data;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import org.jkiss.dbeaver.core.Log;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCQueryTransformer;
import org.jkiss.dbeaver.model.exec.DBCStatement;
import org.jkiss.dbeaver.model.sql.SQLQuery;
import org.jkiss.dbeaver.model.sql.parser.SQLSemanticProcessor;
/**
* Query transformer for ROWNUM
*/
public class QueryTransformerRowNum implements DBCQueryTransformer {
static final Log log = Log.getLog(QueryTransformerRowNum.class);
private Number offset;
private Number length;
@Override
public void setParameters(Object... parameters) {
this.offset = (Number) parameters[0];
this.length = (Number) parameters[1];
}
@Override
public String transformQueryString(SQLQuery query) throws DBCException {
long totalRows = offset.longValue() + length.longValue();
if (query.isPlainSelect()) {
try {
Statement statement = query.getStatement();
if (statement instanceof Select) {
Select select = (Select) statement;
if (select.getSelectBody() instanceof PlainSelect) {
SQLSemanticProcessor.addWhereToSelect(
(PlainSelect) select.getSelectBody(),
"ROWNUM < " + totalRows);
return statement.toString();
}
}
} catch (Throwable e) {
// ignore
log.debug(e);
}
}
return query.getQuery();
}
@Override
public void transformStatement(DBCStatement statement, int parameterIndex) throws DBCException {
statement.setLimit(offset.longValue(), length.longValue());
}
}
......@@ -25,6 +25,7 @@ import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.core.Log;
import org.jkiss.dbeaver.ext.oracle.OracleDataSourceProvider;
import org.jkiss.dbeaver.ext.oracle.data.QueryTransformerRowNum;
import org.jkiss.dbeaver.ext.oracle.model.plan.OraclePlanAnalyser;
import org.jkiss.dbeaver.ext.oracle.oci.OCIUtils;
import org.jkiss.dbeaver.model.*;
......@@ -513,6 +514,15 @@ public class OracleDataSource extends JDBCDataSource
}
}
@Nullable
@Override
public DBCQueryTransformer createQueryTransformer(DBCQueryTransformType type) {
if (type == DBCQueryTransformType.RESULT_SET_LIMIT) {
return new QueryTransformerRowNum();
}
return super.createQueryTransformer(type);
}
@Override
public void readServerOutput(DBRProgressMonitor monitor, DBCExecutionContext context, PrintWriter output) throws DBCException {
JDBCSession session = openSession(monitor, DBCExecutionPurpose.UTIL, "Read DBMS output");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册