提交 a90054f4 编写于 作者: S Serge Rider

Data filters model refactoring

上级 17fed714
......@@ -83,7 +83,6 @@ public class SQLiteValueHandler extends JDBCAbstractValueHandler {
if (format == DBDDisplayFormat.NATIVE || format == DBDDisplayFormat.EDIT) {
return DBValueFormatting.convertNumberToNativeString((Number) value);
} else {
if (numberFormatter == null) {
try {
numberFormatter = formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_NUMBER, type);
......
......@@ -395,6 +395,11 @@ public final class DBUtils {
return null;
}
@Nullable
public static <T extends DBPNamedObject> T findObject(@Nullable Collection<T> theList, String objectName) {
return findObject(theList, objectName, false);
}
/**
* Finds object by its name (case insensitive)
*
......@@ -403,11 +408,10 @@ public final class DBUtils {
* @return object or null
*/
@Nullable
public static <T extends DBPNamedObject> T findObject(@Nullable Collection<T> theList, String objectName)
{
public static <T extends DBPNamedObject> T findObject(@Nullable Collection<T> theList, String objectName, boolean caseInsensitive) {
if (theList != null && !theList.isEmpty()) {
for (T object : theList) {
if (object.getName().equalsIgnoreCase(objectName)) {
if (caseInsensitive ? object.getName().equalsIgnoreCase(objectName) : object.getName().equals(objectName)) {
return object;
}
}
......@@ -415,16 +419,17 @@ public final class DBUtils {
return null;
}
/**
* Find object (case-sensitive)
*/
@Nullable
public static <T extends DBPNamedObject> T findObject(@Nullable List<T> theList, String objectName)
public static <T extends DBPNamedObject> T findObject(@Nullable T[] theList, String objectName)
{
if (theList != null) {
int size = theList.size();
for (int i = 0; i < size; i++) {
if (theList.get(i).getName().equalsIgnoreCase(objectName)) {
return theList.get(i);
if (theList != null && theList.length > 0 ) {
for (T object : theList) {
if (object.getName().equals(objectName)) {
return object;
}
}
}
return null;
......
......@@ -17,6 +17,7 @@
package org.jkiss.dbeaver.model.data;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.utils.CommonUtils;
......@@ -25,35 +26,47 @@ import org.jkiss.utils.CommonUtils;
*/
public class DBDAttributeConstraint extends DBDAttributeConstraintBase {
private final DBSAttributeBase attribute;
private final int originalVisualPosition;
private DBSAttributeBase attribute;
private String attributeName;
private int originalVisualPosition;
public DBDAttributeConstraint(DBDAttributeBinding attribute)
{
this.attribute = attribute;
this.originalVisualPosition = attribute.getOrdinalPosition();
public DBDAttributeConstraint(DBDAttributeBinding attribute) {
setAttribute(attribute);
setVisualPosition(this.originalVisualPosition);
}
public DBDAttributeConstraint(DBSAttributeBase attribute, int visualPosition)
{
this.attribute = attribute;
this.originalVisualPosition = visualPosition;
public DBDAttributeConstraint(DBSAttributeBase attribute, int visualPosition) {
setAttribute(attribute);
setVisualPosition(this.originalVisualPosition);
}
public DBDAttributeConstraint(DBDAttributeConstraint source)
{
public DBDAttributeConstraint(String attributeName, int originalVisualPosition) {
this.attribute = null;
this.attributeName = attributeName;
this.originalVisualPosition = originalVisualPosition;
}
public DBDAttributeConstraint(DBDAttributeConstraint source) {
super(source);
this.attribute = source.attribute;
this.attributeName = source.attributeName;
this.originalVisualPosition = source.originalVisualPosition;
}
public DBSAttributeBase getAttribute()
{
public DBSAttributeBase getAttribute() {
return attribute;
}
void setAttribute(@NotNull DBSAttributeBase binding) {
this.attribute = binding;
this.attributeName = this.attribute.getName();
this.originalVisualPosition = attribute.getOrdinalPosition();
}
public String getAttributeName() {
return attributeName;
}
public int getOriginalVisualPosition() {
return originalVisualPosition;
}
......@@ -63,34 +76,30 @@ public class DBDAttributeConstraint extends DBDAttributeConstraintBase {
return super.hasFilter() || originalVisualPosition != getVisualPosition();
}
public void reset()
{
public void reset() {
super.reset();
setVisualPosition(originalVisualPosition);
}
public boolean equalFilters(DBDAttributeConstraintBase obj, boolean compareOrders)
{
public boolean equalFilters(DBDAttributeConstraintBase obj, boolean compareOrders) {
return
obj instanceof DBDAttributeConstraint &&
CommonUtils.equalObjects(this.attribute, ((DBDAttributeConstraint)obj).attribute) &&
super.equalFilters(obj, compareOrders);
CommonUtils.equalObjects(this.attribute, ((DBDAttributeConstraint) obj).attribute) &&
super.equalFilters(obj, compareOrders);
}
@Override
public int hashCode()
{
public int hashCode() {
return this.attribute.hashCode();
}
@Override
public boolean equals(Object obj)
{
public boolean equals(Object obj) {
if (obj instanceof DBDAttributeConstraint) {
DBDAttributeConstraint source = (DBDAttributeConstraint) obj;
return
CommonUtils.equalObjects(this.attribute, source.attribute) &&
super.equals(obj);
super.equals(obj);
} else {
return false;
}
......@@ -106,6 +115,8 @@ public class DBDAttributeConstraint extends DBDAttributeConstraintBase {
public boolean matches(DBSAttributeBase attr, boolean matchByName) {
return attribute == attr ||
(attribute instanceof DBDAttributeBinding && ((DBDAttributeBinding) attribute).matches(attr, matchByName));
(attribute instanceof DBDAttributeBinding && ((DBDAttributeBinding) attribute).matches(attr, matchByName)) ||
(matchByName && attributeName.equals(attr.getName()));
}
}
......@@ -18,6 +18,7 @@
package org.jkiss.dbeaver.model.data;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.utils.CommonUtils;
......@@ -84,7 +85,7 @@ public class DBDDataFilter {
public DBDAttributeConstraint getConstraint(String name)
{
for (DBDAttributeConstraint co : constraints) {
if (CommonUtils.equalObjects(co.getAttribute().getName(), name)) {
if (CommonUtils.equalObjects(co.getAttributeName(), name)) {
return co;
}
}
......@@ -103,13 +104,7 @@ public class DBDDataFilter {
visibleConstraints.add(constraint);
}
}
Collections.sort(visibleConstraints, new Comparator<DBDAttributeConstraint>() {
@Override
public int compare(DBDAttributeConstraint o1, DBDAttributeConstraint o2)
{
return o1.getVisualPosition() - o2.getVisualPosition();
}
});
visibleConstraints.sort(Comparator.comparingInt(DBDAttributeConstraintBase::getVisualPosition));
List<DBSAttributeBase> attributes = new ArrayList<>(visibleConstraints.size());
for (DBDAttributeConstraint constraint : visibleConstraints) {
attributes.add(constraint.getAttribute());
......@@ -196,15 +191,9 @@ public class DBDDataFilter {
}
}
if (result != null && result.size() > 1) {
Collections.sort(result, new Comparator<DBDAttributeConstraint>() {
@Override
public int compare(DBDAttributeConstraint o1, DBDAttributeConstraint o2)
{
return o1.getOrderPosition() - o2.getOrderPosition();
}
});
result.sort(Comparator.comparingInt(DBDAttributeConstraintBase::getOrderPosition));
}
return result == null ? Collections.<DBDAttributeConstraint>emptyList() : result;
return result == null ? Collections.emptyList() : result;
}
public int getMaxOrderingPosition()
......@@ -236,6 +225,15 @@ public class DBDDataFilter {
this.where = null;
}
public void bindAttributes(DBDAttributeBinding[] bindings) {
for (DBDAttributeConstraint constr : constraints) {
DBDAttributeBinding attrBinding = DBUtils.findObject(bindings, constr.getAttributeName());
if (attrBinding != null) {
constr.setAttribute(attrBinding);
}
}
}
@Override
public boolean equals(Object obj)
{
......@@ -286,7 +284,7 @@ public class DBDDataFilter {
public boolean hasNameDuplicates(String name) {
int count = 0;
for (DBDAttributeConstraint c : constraints) {
if (name.equalsIgnoreCase(c.getAttribute().getName())) {
if (name.equalsIgnoreCase(c.getAttributeName())) {
count++;
}
}
......
......@@ -450,8 +450,10 @@ public final class SQLUtils {
// Most likely it is an expression so we don't want to quote it
attrName = binding.getMetaAttribute().getName();
}
} else {
} else if (cAttr != null) {
attrName = DBUtils.getObjectFullName(dataSource, cAttr, DBPEvaluationContext.DML);
} else {
attrName = DBUtils.getQuotedIdentifier(dataSource, constraint.getAttributeName());
}
query.append(attrName).append(' ').append(condition);
}
......
......@@ -134,7 +134,7 @@ public class SQLSemanticProcessor {
select.setOrderByElements(orderByElements);
}
for (DBDAttributeConstraint co : filter.getOrderConstraints()) {
String columnName = co.getAttribute().getName();
String columnName = co.getAttributeName();
boolean forceNumeric = filter.hasNameDuplicates(columnName) || !SQLUtils.PATTERN_SIMPLE_NAME.matcher(columnName).matches();
Expression orderExpr = getOrderConstraintExpression(dataSource, select, co, forceNumeric);
OrderByElement element = new OrderByElement();
......@@ -152,9 +152,9 @@ public class SQLSemanticProcessor {
private static Expression getOrderConstraintExpression(DBPDataSource dataSource, PlainSelect select, DBDAttributeConstraint co, boolean forceNumeric) throws JSQLParserException {
Expression orderExpr;
String attrName = DBUtils.getQuotedIdentifier(dataSource, co.getAttribute().getName());
String attrName = DBUtils.getQuotedIdentifier(dataSource, co.getAttributeName());
if (forceNumeric || attrName.isEmpty()) {
orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
orderExpr = new LongValue(co.getOrderPosition() + 1);
} else if (CommonUtils.isJavaIdentifier(attrName)) {
// Use column table only if there are multiple source tables (joins)
Table orderTable = CommonUtils.isEmpty(select.getJoins()) ? null : getConstraintTable(select, co);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册