提交 7ae70952 编写于 作者: S Serge Rider

Attribute presentation override by transformers

上级 373e469f
......@@ -30,6 +30,7 @@ import org.jkiss.dbeaver.model.data.DBDRowIdentifier;
import org.jkiss.dbeaver.model.data.DBDValueHandler;
import org.jkiss.dbeaver.model.exec.DBCAttributeMetaData;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.data.IAttributeController;
import org.jkiss.dbeaver.ui.data.IRowController;
......@@ -162,7 +163,7 @@ public class ResultSetValueController implements IAttributeController, IRowContr
@Override
public IValueManager getValueManager() {
DBSTypedObject valueType = getValueType();
DBSAttributeBase valueType = binding.getPresentationAttribute();
return DataManagerRegistry.findValueManager(
getDataSourceContainer(),
valueType,
......
......@@ -44,15 +44,14 @@ public abstract class DBDAttributeBinding implements DBSObject, DBSAttributeBase
@NotNull
protected DBDValueHandler valueHandler;
//@NotNull
//protected DBDValueRenderer valueRenderer;
@Nullable
protected DBSAttributeBase presentationAttribute;
@Nullable
private List<DBDAttributeBinding> nestedBindings;
protected DBDAttributeBinding(@NotNull DBDValueHandler valueHandler)
{
this.valueHandler = valueHandler;
//this.valueRenderer = valueHandler;
}
@Nullable
......@@ -85,11 +84,39 @@ public abstract class DBDAttributeBinding implements DBSObject, DBSAttributeBase
public abstract DBCAttributeMetaData getMetaAttribute();
/**
* Entity attribute (may be null)
* Entity attribute (may be null).
* It is always null if {@link #lateBinding(DBCSession, List)} wasn't called
*/
@Nullable
public abstract DBSEntityAttribute getEntityAttribute();
/**
* Most valuable attribute reference.
* @return resolved entity attribute or just meta attribute
*/
@NotNull
public DBSAttributeBase getAttribute()
{
DBSEntityAttribute attr = getEntityAttribute();
return attr == null ? getMetaAttribute() : attr;
}
/**
* Presentation attribute.
* Usually the same as {@link #getAttribute()} but may be explicitly set by attribute transformers.
*/
@NotNull
public DBSAttributeBase getPresentationAttribute() {
if (presentationAttribute != null) {
return presentationAttribute;
}
return getAttribute();
}
public void setPresentationAttribute(@Nullable DBSAttributeBase presentationAttribute) {
this.presentationAttribute = presentationAttribute;
}
/**
* Row identifier (may be null)
*/
......@@ -120,18 +147,6 @@ public abstract class DBDAttributeBinding implements DBSObject, DBSAttributeBase
return valueHandler;
}
// public void setValueRenderer(@NotNull DBDValueRenderer renderer) {
// this.valueRenderer = renderer;
// }
@NotNull
public DBSAttributeBase getAttribute()
{
DBSEntityAttribute attr = getEntityAttribute();
return attr == null ? getMetaAttribute() : attr;
}
public boolean matches(DBSAttributeBase attr, boolean searchByName) {
if (attr != null && (this == attr || getMetaAttribute() == attr || getEntityAttribute() == attr)) {
return true;
......
......@@ -21,13 +21,16 @@ import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataKind;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.data.*;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.impl.data.ProxyValueHandler;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.utils.CommonUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
......@@ -42,6 +45,8 @@ public class EpochTimeAttributeTransformer implements DBDAttributeTransformer {
static final Log log = Log.getLog(EpochTimeAttributeTransformer.class);
private static final String PROP_UNIT = "unit";
private static final SimpleDateFormat DEFAULT_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
enum EpochUnit {
seconds,
milliseconds,
......@@ -50,7 +55,9 @@ public class EpochTimeAttributeTransformer implements DBDAttributeTransformer {
@Override
public void transformAttribute(@NotNull DBCSession session, @NotNull DBDAttributeBinding attribute, @NotNull List<Object[]> rows, @NotNull Map<String, String> options) throws DBException {
// TODO: Change attribute type (to DATETIME)
attribute.setPresentationAttribute(
new TransformerPresentationAttribute(attribute, "EpochTime", -1, DBPDataKind.DATETIME));
EpochUnit unit = EpochUnit.milliseconds;
if (options.containsKey(PROP_UNIT)) {
try {
......@@ -78,10 +85,22 @@ public class EpochTimeAttributeTransformer implements DBDAttributeTransformer {
case seconds: dateValue *= 1000; break;
case nanoseconds: dateValue /= 1000; break;
}
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).format(
new Date(dateValue));
return DEFAULT_TIME_FORMAT.format(new Date(dateValue));
}
return DBUtils.getDefaultValueDisplayString(value, format);
}
@Nullable
@Override
public Object getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, @Nullable Object object, boolean copy) throws DBCException {
if (object instanceof String) {
try {
return DEFAULT_TIME_FORMAT.parse((String) object).getTime();
} catch (ParseException e) {
log.error("Error parsing time value", e);
}
}
return super.getValueFromObject(session, type, object, copy);
}
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2016 Serge Rieder (serge@jkiss.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (version 2)
* as published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jkiss.dbeaver.model.impl.data.transformers;
import org.jkiss.dbeaver.model.DBPDataKind;
import org.jkiss.dbeaver.model.data.DBDAttributeBinding;
import org.jkiss.dbeaver.model.impl.struct.AbstractAttribute;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
/**
* TransformerPresentationAttribute
*/
public class TransformerPresentationAttribute extends AbstractAttribute {
private final DBPDataKind dataKind;
public TransformerPresentationAttribute(
DBDAttributeBinding attribute,
String typeName,
int typeId,
DBPDataKind dataKind)
{
super(
attribute.getName(),
typeName,
typeId,
attribute.getOrdinalPosition(),
attribute.getMaxLength(),
attribute.getScale(),
attribute.getPrecision(),
attribute.isRequired(),
attribute.isAutoGenerated());
this.dataKind = dataKind;
}
@Override
public DBPDataKind getDataKind() {
return dataKind;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册