未验证 提交 5901cae5 编写于 作者: S Serge Rider 提交者: GitHub

Merge pull request #3454 from dbeaver/3425-mock-data

3425-mock-data
......@@ -216,7 +216,7 @@
label="UUID"
description="UUID random values"
class="org.jkiss.dbeaver.ext.mockdata.generator.StringUuidGenerator"
tags="uuid">
tags="id,uuid">
<type kind="STRING"/>
</generator>
......
......@@ -34,10 +34,7 @@ import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.impl.AbstractExecutionSource;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.dbeaver.model.struct.DBSDataManipulator;
import org.jkiss.dbeaver.model.struct.DBSEntity;
import org.jkiss.dbeaver.model.struct.DBSEntityAttribute;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.tools.AbstractToolWizard;
import org.jkiss.utils.CommonUtils;
......@@ -239,6 +236,21 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
monitor.beginTask("Insert data", (int) rowsNumber);
boolean hasMiltiUniqs = false;
Set<String> miltiUniqColumns = new HashSet<>();
for (DBSAttributeBase attribute : attributes) {
if (DBUtils.checkUnique(monitor, dbsEntity, attribute) == DBUtils.UNIQ_TYPE.MULTI) {
hasMiltiUniqs = true;
// collect the columns from multi-uniqs
DBSEntityReferrer constraint = (DBSEntityReferrer) DBUtils.getConstraint(monitor, dbsEntity, attribute);
for (DBSEntityAttributeRef attributeRef : constraint.getAttributeReferences(monitor)) {
miltiUniqColumns.add(attributeRef.getAttribute().getName());
}
}
}
List<List<DBDAttributeValue>> valuesCacheForUniqs = new ArrayList<>();
// generate and insert the data
session.enableLogging(false);
DBSDataManipulator.ExecuteBatch batch = null;
......@@ -275,6 +287,31 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
return true;
}
// skip duplicate records for uniqs
if (hasMiltiUniqs) {
boolean collision = false;
for (List<DBDAttributeValue> valueList : valuesCacheForUniqs) {
boolean theSame = true;
for (int j = 0; j < valueList.size(); j++) {
if (miltiUniqColumns.contains(valueList.get(j).getAttribute().getName())) {
if (!CommonUtils.equalObjects(valueList.get(j), attributeValues.get(j))) {
theSame = false;
break;
}
}
}
if (theSame) {
collision = true;
break;
}
}
if (collision) {
continue;
} else {
valuesCacheForUniqs.add(attributeValues);
}
}
if (batch == null) {
batch = dataManipulator.insertData(
session,
......
......@@ -186,7 +186,7 @@ public class MockDataWizardPageSettings extends ActiveWizardPage<MockDataExecute
cell.setImage(DBeaverIcons.getImage(DBValueFormatting.getTypeImage(attribute)));
cell.setText(attribute.getName());
try {
if (DBUtils.checkUnique(mockDataSettings.getMonitor(), mockDataSettings.getEntity(), attribute)) {
if (DBUtils.checkUnique(mockDataSettings.getMonitor(), mockDataSettings.getEntity(), attribute) != null) {
cell.setFont(boldFont);
}
} catch (DBException e) {
......
......@@ -81,7 +81,7 @@ public abstract class AbstractMockValueGenerator implements MockValueGenerator {
public Object generateValue(DBRProgressMonitor monitor) throws DBException, IOException {
if (isFirstRun) {
isFirstRun = false;
isUnique = DBUtils.checkUnique(monitor, dbsEntity, attribute);
isUnique = (DBUtils.checkUnique(monitor, dbsEntity, attribute) == DBUtils.UNIQ_TYPE.SINGLE);
if (isUnique && (attribute instanceof DBSAttributeEnumerable)) {
uniqueValues = new HashSet<>();
Collection<DBDLabelValuePair> valuePairs = readColumnValues(monitor, dbsEntity.getDataSource(), (DBSAttributeEnumerable) attribute, UNIQUE_VALUES_SET_SIZE);
......
......@@ -72,7 +72,7 @@ public class FKGenerator extends AbstractMockValueGenerator
throw new DBException("Can't find reference column for '" + attribute.getName() + "'");
}
int numberRefRecords = DBUtils.checkUnique(monitor, dbsEntity, attribute) ? UNIQ_REF_RECORDS_LIMIT : REF_RECORDS_LIMIT;
int numberRefRecords = (DBUtils.checkUnique(monitor, dbsEntity, attribute) == DBUtils.UNIQ_TYPE.SINGLE) ? UNIQ_REF_RECORDS_LIMIT : REF_RECORDS_LIMIT;
Collection<DBDLabelValuePair> values = readColumnValues(monitor, fk.getDataSource(), (DBSAttributeEnumerable) column.getReferencedColumn(), numberRefRecords);
for (DBDLabelValuePair value : values) {
refValues.add(value.getValue());
......
......@@ -1554,16 +1554,35 @@ public final class DBUtils {
return true;
}
public static boolean checkUnique(DBRProgressMonitor monitor, DBSEntity dbsEntity, DBSAttributeBase attribute) throws DBException {
public enum UNIQ_TYPE {
SINGLE, MULTI
}
public static UNIQ_TYPE checkUnique(DBRProgressMonitor monitor, DBSEntity dbsEntity, DBSAttributeBase attribute) throws DBException {
for (DBSEntityConstraint constraint : dbsEntity.getConstraints(monitor)) {
DBSEntityConstraintType constraintType = constraint.getConstraintType();
if (constraintType.isUnique()) {
DBSEntityAttributeRef constraintAttribute = getConstraintAttribute(monitor, ((DBSEntityReferrer) constraint), attribute.getName());
if (constraintAttribute != null && constraintAttribute.getAttribute() == attribute) {
return true;
List<? extends DBSEntityAttributeRef> refColumns = ((DBSEntityReferrer) constraint).getAttributeReferences(monitor);
if (refColumns.size() > 1) {
return UNIQ_TYPE.MULTI;
} else {
return UNIQ_TYPE.SINGLE;
}
}
}
}
return false;
return null;
}
public static DBSEntityConstraint getConstraint(DBRProgressMonitor monitor, DBSEntity dbsEntity, DBSAttributeBase attribute) throws DBException {
for (DBSEntityConstraint constraint : dbsEntity.getConstraints(monitor)) {
DBSEntityAttributeRef constraintAttribute = getConstraintAttribute(monitor, ((DBSEntityReferrer) constraint), attribute.getName());
if (constraintAttribute != null && constraintAttribute.getAttribute() == attribute) {
return constraint;
}
}
return null;
}
}
......@@ -19,6 +19,7 @@ package org.jkiss.dbeaver.model.data;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.utils.CommonUtils;
import java.util.List;
......@@ -71,4 +72,17 @@ public class DBDAttributeValue {
return values;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof DBDAttributeValue)) {
return false;
}
if (!CommonUtils.equalObjects(value, ((DBDAttributeValue) obj).value)) {
return false;
}
if (!CommonUtils.equalObjects(attribute.getName(), ((DBDAttributeValue) obj).attribute.getName())) {
return false;
}
return true;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册