提交 9b47720a 编写于 作者: S serge-rider

#1716 Number format fix. Minimum fraction digits number depends on column settings.

上级 4f7db504
......@@ -22,6 +22,7 @@ import org.jkiss.dbeaver.model.impl.preferences.SimplePreferenceStore;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceListener;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.model.preferences.DBPPropertyDescriptor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.utils.PrefUtils;
import org.jkiss.utils.CommonUtils;
......@@ -184,7 +185,7 @@ public class DataFormatterProfile implements DBDDataFormatterProfile, DBPPrefere
}
@Override
public DBDDataFormatter createFormatter(String typeId)
public DBDDataFormatter createFormatter(String typeId, DBSTypedObject type)
throws IllegalAccessException, InstantiationException, IllegalArgumentException
{
DataFormatterDescriptor descriptor = DataFormatterRegistry.getInstance().getDataFormatter(typeId);
......@@ -202,7 +203,7 @@ public class DataFormatterProfile implements DBDDataFormatterProfile, DBPPrefere
if (props != null && !props.isEmpty()) {
formatterProps.putAll(props);
}
formatter.init(locale, formatterProps);
formatter.init(type, locale, formatterProps);
return formatter;
}
......
......@@ -99,7 +99,7 @@ public class NumberInlineEditor extends BaseValueEditor<Text> {
curValue.getClass() :
valueController.getValueHandler().getValueObjectType(valueController.getValueType());
try {
return DBValueFormatting.convertStringToNumber(text, hintType, formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_NUMBER));
return DBValueFormatting.convertStringToNumber(text, hintType, formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_NUMBER, valueController.getValueType()));
} catch (Exception e) {
log.error(e);
return null;
......
......@@ -305,7 +305,7 @@ public class PrefPageDataFormat extends TargetPrefPage
if (props != null && !props.isEmpty()) {
formatterProps.putAll(props);
}
formatter.init(profileLocale, formatterProps);
formatter.init(null, profileLocale, formatterProps);
String sampleValue = formatter.formatValue(formatterDescriptor.getSample().getSampleValue());
sampleText.setText(sampleValue);
......
......@@ -36,8 +36,8 @@ public class DB2DecFloatValueHandler extends JDBCNumberValueHandler {
final static int DECFLOAT_SPECIALVALUE_ENCOUNTERED = -4231;
public DB2DecFloatValueHandler(DBDDataFormatterProfile formatterProfile) {
super(formatterProfile);
public DB2DecFloatValueHandler(DBSTypedObject type, DBDDataFormatterProfile formatterProfile) {
super(type, formatterProfile);
}
@Nullable
......
......@@ -37,7 +37,7 @@ public class DB2ValueHandlerProvider implements DBDValueHandlerProvider {
{
final String typeName = typedObject.getTypeName();
if (DB2Constants.TYPE_NAME_DECFLOAT.equals(typeName)) {
return new DB2DecFloatValueHandler(preferences.getDataFormatterProfile());
return new DB2DecFloatValueHandler(typedObject, preferences.getDataFormatterProfile());
} else if (typeName.contains("TIMESTAMP") || typedObject.getDataKind() == DBPDataKind.DATETIME) {
return new DB2TimestampValueHandler(preferences.getDataFormatterProfile());
}
......
......@@ -18,6 +18,7 @@
package org.jkiss.dbeaver.model.data;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import java.text.ParseException;
import java.util.Locale;
......@@ -33,7 +34,7 @@ public interface DBDDataFormatter {
String TYPE_NAME_TIME = "time"; //$NON-NLS-1$
String TYPE_NAME_TIMESTAMP = "timestamp"; //$NON-NLS-1$
void init(Locale locale, Map<Object, Object> properties);
void init(@Nullable DBSTypedObject type, Locale locale, Map<Object, Object> properties);
@Nullable
String getPattern();
......
......@@ -18,6 +18,7 @@
package org.jkiss.dbeaver.model.data;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import java.io.IOException;
import java.util.Locale;
......@@ -48,6 +49,6 @@ public interface DBDDataFormatterProfile {
void saveProfile() throws IOException;
DBDDataFormatter createFormatter(String typeId) throws IllegalAccessException, InstantiationException, IllegalArgumentException;
DBDDataFormatter createFormatter(String typeId, DBSTypedObject type) throws IllegalAccessException, InstantiationException, IllegalArgumentException;
}
......@@ -50,7 +50,7 @@ public abstract class DateTimeCustomValueHandler extends DateTimeValueHandler {
if (object == null) {
return null;
} else if (object instanceof Date) {
return (Date) (copy ? ((Date)object).clone() : object);
return copy ? ((Date)object).clone() : object;
} else if (object instanceof String) {
String strValue = (String)object;
try {
......@@ -89,10 +89,10 @@ public abstract class DateTimeCustomValueHandler extends DateTimeValueHandler {
}
}
protected DBDDataFormatter getFormatter(String typeId)
private DBDDataFormatter getFormatter(DBSTypedObject typedObject, String typeId)
{
try {
return formatterProfile.createFormatter(typeId);
return formatterProfile.createFormatter(typeId, typedObject);
} catch (Exception e) {
log.error("Can't create formatter for datetime value handler", e); //$NON-NLS-1$
return DefaultDataFormatter.INSTANCE;
......@@ -103,7 +103,7 @@ public abstract class DateTimeCustomValueHandler extends DateTimeValueHandler {
protected DBDDataFormatter getFormatter(DBSTypedObject column)
{
if (formatter == null) {
formatter = getFormatter(getFormatterId(column));
formatter = getFormatter(column, getFormatterId(column));
}
return formatter;
}
......
......@@ -17,6 +17,7 @@
package org.jkiss.dbeaver.model.impl.data.formatters;
import org.jkiss.dbeaver.model.data.DBDDataFormatter;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.time.ExtendedDateFormat;
......@@ -36,7 +37,7 @@ public class DateTimeDataFormatter implements DBDDataFormatter {
private FieldPosition position;
@Override
public void init(Locale locale, Map<Object, Object> properties)
public void init(DBSTypedObject type, Locale locale, Map<Object, Object> properties)
{
pattern = CommonUtils.toString(properties.get(PROP_PATTERN));
dateFormat = new ExtendedDateFormat(
......
......@@ -17,6 +17,7 @@
package org.jkiss.dbeaver.model.impl.data.formatters;
import org.jkiss.dbeaver.model.data.DBDDataFormatter;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import java.util.Locale;
import java.util.Map;
......@@ -30,7 +31,7 @@ public class DefaultDataFormatter implements DBDDataFormatter {
}
@Override
public void init(Locale locale, Map<Object, Object> properties)
public void init(DBSTypedObject type, Locale locale, Map<Object, Object> properties)
{
}
......
......@@ -18,6 +18,7 @@ package org.jkiss.dbeaver.model.impl.data.formatters;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.data.DBDDataFormatter;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.utils.CommonUtils;
import java.math.BigDecimal;
......@@ -37,7 +38,7 @@ public class NumberDataFormatter implements DBDDataFormatter {
private FieldPosition position;
@Override
public void init(Locale locale, Map<Object, Object> properties)
public void init(DBSTypedObject type, Locale locale, Map<Object, Object> properties)
{
numberFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
Object useGrouping = properties.get(NumberFormatSample.PROP_USE_GROUPING);
......@@ -68,6 +69,11 @@ public class NumberDataFormatter implements DBDDataFormatter {
// just skip it
}
}
if (type != null) {
if (type.getScale() != null && type.getScale() > 0) {
numberFormat.setMinimumFractionDigits(type.getScale());
}
}
buffer = new StringBuffer();
position = new FieldPosition(0);
}
......
......@@ -43,12 +43,14 @@ import java.sql.Types;
public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
private static final Log log = Log.getLog(JDBCNumberValueHandler.class);
private DBSTypedObject type;
private DBDDataFormatter formatter;
public JDBCNumberValueHandler(DBDDataFormatterProfile formatterProfile)
public JDBCNumberValueHandler(DBSTypedObject type, DBDDataFormatterProfile formatterProfile)
{
this.type = type;
try {
formatter = formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_NUMBER);
formatter = formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_NUMBER, type);
} catch (Exception e) {
log.error("Can't create formatter for number value handler", e); //$NON-NLS-1$
formatter = DefaultDataFormatter.INSTANCE;
......@@ -334,4 +336,4 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
}
}
}
\ No newline at end of file
}
......@@ -44,7 +44,7 @@ public class JDBCStandardValueHandlerProvider implements DBDValueHandlerProvider
return JDBCStringValueHandler.INSTANCE;
}
case NUMERIC:
return new JDBCNumberValueHandler(preferences.getDataFormatterProfile());
return new JDBCNumberValueHandler(typedObject, preferences.getDataFormatterProfile());
case DATETIME:
return new JDBCDateTimeValueHandler(preferences.getDataFormatterProfile());
case BINARY:
......
......@@ -157,11 +157,11 @@ public class JDBCPreparedStatementImpl extends JDBCStatementImpl<PreparedStateme
try {
DBDDataFormatterProfile formatterProfile = getSession().getDataSource().getDataFormatterProfile();
if (value instanceof Date) {
return SQLUtils.quoteString(connection.getDataSource(), formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_DATE).formatValue(value));
return SQLUtils.quoteString(connection.getDataSource(), formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_DATE, null).formatValue(value));
} else if (value instanceof Time) {
return SQLUtils.quoteString(connection.getDataSource(), formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_TIME).formatValue(value));
return SQLUtils.quoteString(connection.getDataSource(), formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_TIME, null).formatValue(value));
} else {
return SQLUtils.quoteString(connection.getDataSource(), formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_TIMESTAMP).formatValue(value));
return SQLUtils.quoteString(connection.getDataSource(), formatterProfile.createFormatter(DBDDataFormatter.TYPE_NAME_TIMESTAMP, null).formatValue(value));
}
} catch (Exception e) {
log.debug("Error formatting date [" + value + "]", e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册