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

Merge pull request #3073 from dbeaver/#1599-mock-data_20

#1599-mock-data_20
......@@ -52,10 +52,11 @@
label="FK"
description="Values from associated table (FK)"
class="org.jkiss.dbeaver.ext.mockdata.generator.FKGenerator">
<!--
<propertyGroup label="General">
<property id="numberRefRecords" label="Ref Records" type="integer" defaultValue="100" description="Size of referenced keys dictionary"/>
<property id="numberRefRecords" label="Ref Records" type="integer" defaultValue="1000000" description="Size limit of referenced keys dictionary"/>
</propertyGroup>
-->
</generator>
<generator
......
......@@ -165,6 +165,7 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
}
AbstractExecutionSource executionSource = new AbstractExecutionSource(dataManipulator, session.getExecutionContext(), this);
boolean success = true;
monitor.beginTask("Generate Mock Data", 3);
if (mockDataSettings.isRemoveOldData()) {
logPage.appendLog("Removing old data from the '" + dataManipulator.getName() + "'.\n");
......@@ -180,7 +181,8 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
txnManager.commit(session);
}
} catch (Exception e) {
String message = " Error removing the data: " + e.getMessage() + ".";
success = false;
String message = " Error removing the data: " + e.getMessage();
log.error(message, e);
logPage.appendLog(message + "\n\n", true);
}
......@@ -190,8 +192,13 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
logPage.appendLog("Old data isn't removed.\n\n");
}
monitor.subTask("Insert data");
if (!success) {
return true;
}
try {
monitor.subTask("Insert data");
logPage.appendLog("Inserting mock data into the '" + dataManipulator.getName() + "'.\n");
DBCStatistics insertStats = new DBCStatistics();
......@@ -247,13 +254,18 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
break;
}
List<DBDAttributeValue> attributeValues = new ArrayList<>();
for (DBSAttributeBase attribute : attributes) {
MockValueGenerator generator = generators.get(attribute.getName());
if (generator != null) {
//((AbstractMockValueGenerator) generator).checkUnique(monitor);
Object value = generator.generateValue(monitor);
attributeValues.add(new DBDAttributeValue(attribute, value));
try {
for (DBSAttributeBase attribute : attributes) {
MockValueGenerator generator = generators.get(attribute.getName());
if (generator != null) {
//((AbstractMockValueGenerator) generator).checkUnique(monitor);
Object value = generator.generateValue(monitor);
attributeValues.add(new DBDAttributeValue(attribute, value));
}
}
} catch (DBException e) {
processGeneratorException(e);
return true;
}
if (batch == null) {
......@@ -272,9 +284,7 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
}
}
catch (Exception e) {
String message = " Error generating mock data: " + e.getMessage() + ".";
log.error(message, e);
logPage.appendLog(message + "\n\n", true);
processGeneratorException(e);
if (e instanceof DBException) {
throw e;
}
......@@ -295,7 +305,7 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
logPage.appendLog(" Duration: " + insertStats.getExecuteTime() + "ms\n\n");
} catch (DBException e) {
String message = " Error inserting mock data: " + e.getMessage() + ".";
String message = " Error inserting mock data: " + e.getMessage();
log.error(message, e);
logPage.appendLog(message + "\n\n", true);
}
......@@ -306,4 +316,10 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
return true;
}
private void processGeneratorException(Exception e) {
String message = " Error generating mock data: " + e.getMessage();
log.error(message, e);
logPage.appendLog(message + "\n\n", true);
}
}
......@@ -22,6 +22,7 @@ import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.PlatformUI;
......@@ -68,6 +69,7 @@ public class MockDataWizardPageSettings extends ActiveWizardPage<MockDataExecute
private Combo presetCombo;
private Label generatorDescriptionLabel;
private Link generatorDescriptionLink;
private Font boldFont;
private String generatorLinkUrl;
......@@ -154,6 +156,7 @@ public class MockDataWizardPageSettings extends ActiveWizardPage<MockDataExecute
}
});
VoidProgressMonitor monitor = new VoidProgressMonitor(); // TODO VoidProgressMonitor
CellLabelProvider labelProvider = new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
......@@ -162,6 +165,13 @@ public class MockDataWizardPageSettings extends ActiveWizardPage<MockDataExecute
if (cell.getColumnIndex() == 0) {
cell.setImage(DBeaverIcons.getImage(DBValueFormatting.getTypeImage(attribute)));
cell.setText(attribute.getName());
try {
if (DBUtils.checkUnique(monitor, mockDataSettings.getEntity(), attribute)) {
cell.setFont(boldFont);
}
} catch (DBException e) {
log.error("Error checking the attribute '" + attribute.getName() + "' properties", e);
}
if (attributeGeneratorProperties != null && attributeGeneratorProperties.isEmpty()) {
cell.setForeground(table.getDisplay().getSystemColor(SWT.COLOR_RED));
noGeneratorInfoLabel.setVisible(true);
......@@ -356,6 +366,8 @@ public class MockDataWizardPageSettings extends ActiveWizardPage<MockDataExecute
}
setControl(composite);
boldFont = UIUtils.makeBoldFont(columnsTableViewer.getControl().getFont());
}
private void selectGenerator(DBSAttributeBase attribute, String generatorName) {
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
* Copyright (C) 2010-2017 Eugene Fradkin (eugene.fradkin@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ext.mockdata.generator;
import org.jkiss.dbeaver.DBException;
......@@ -7,7 +24,10 @@ import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.data.DBDLabelValuePair;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.dbeaver.model.struct.DBSAttributeEnumerable;
import org.jkiss.dbeaver.model.struct.DBSDataManipulator;
import org.jkiss.dbeaver.model.struct.DBSEntity;
import java.io.IOException;
import java.util.*;
......@@ -61,7 +81,7 @@ public abstract class AbstractMockValueGenerator implements MockValueGenerator {
public Object generateValue(DBRProgressMonitor monitor) throws DBException, IOException {
if (isFirstRun) {
isFirstRun = false;
isUnique = checkUnique(monitor);
isUnique = DBUtils.checkUnique(monitor, dbsEntity, attribute);
if (isUnique && (attribute instanceof DBSAttributeEnumerable)) {
uniqueValues = new HashSet<>();
Collection<DBDLabelValuePair> valuePairs = readColumnValues(monitor, dbsEntity.getDataSource(), (DBSAttributeEnumerable) attribute, UNIQUE_VALUES_SET_SIZE);
......@@ -76,7 +96,8 @@ public abstract class AbstractMockValueGenerator implements MockValueGenerator {
Object value = null;
while (value == null || uniqueValues.contains(value)) {
if (attempts > UNIQUE_VALUE_GEN_ATTEMPTS) {
return null;
throw new DBException("\n Can't generate appropriate unique value for the '" + attribute.getName() + "' <" + attribute.getFullTypeName() + "> attribute.\n" +
" Try to change the generator or its parameters.\n");
}
if (monitor.isCanceled()) {
return null;
......@@ -107,16 +128,4 @@ public abstract class AbstractMockValueGenerator implements MockValueGenerator {
return column.getValueEnumeration(session, null, number);
}
private boolean checkUnique(DBRProgressMonitor monitor) throws DBException {
for (DBSEntityConstraint constraint : dbsEntity.getConstraints(monitor)) {
DBSEntityConstraintType constraintType = constraint.getConstraintType();
if (constraintType == DBSEntityConstraintType.PRIMARY_KEY || constraintType.isUnique()) {
DBSEntityAttributeRef constraintAttribute = DBUtils.getConstraintAttribute(monitor, ((DBSEntityReferrer) constraint), attribute.getName());
if (constraintAttribute != null && constraintAttribute.getAttribute() == attribute) {
return true;
}
}
}
return false;
}
}
......@@ -30,9 +30,11 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
public class FKGenerator extends AbstractMockValueGenerator {
public class FKGenerator extends AbstractMockValueGenerator
{
private static final int UNIQ_REF_RECORDS_LIMIT = 100000000;
private static final int REF_RECORDS_LIMIT = 100000;
private int numberRefRecords = 100;
private List<Object> refValues = null;
@Override
......@@ -40,11 +42,12 @@ public class FKGenerator extends AbstractMockValueGenerator {
super.init(container, attribute, properties);
nullsPersent = 0;
/*
Integer numberRefRecords = (Integer) properties.get("numberRefRecords"); //$NON-NLS-1$
if (numberRefRecords != null) {
this.numberRefRecords = numberRefRecords;
}
*/
}
@Override
......@@ -56,6 +59,7 @@ public class FKGenerator extends AbstractMockValueGenerator {
List<? extends DBSEntityAttributeRef> references = ((DBSEntityReferrer) fk).getAttributeReferences(monitor);
DBSTableForeignKeyColumn column = (DBSTableForeignKeyColumn) references.iterator().next(); // TODO only the first !!!
int numberRefRecords = DBUtils.checkUnique(monitor, dbsEntity, attribute) ? 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());
......
......@@ -1515,4 +1515,16 @@ public final class DBUtils {
return true;
}
public static boolean 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;
}
}
}
return false;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册