提交 b02e8e74 编写于 作者: S Serge Rider 提交者: GitHub

Merge pull request #2819 from dbeaver/1599-mock-data_2

Merge branches '1599-mock-data_2' and 'devel' of https://github.com/dbeaver/dbeaver into 1599-mock-data_2

Former-commit-id: 56ed9f05
...@@ -81,7 +81,10 @@ import java.lang.reflect.Method; ...@@ -81,7 +81,10 @@ import java.lang.reflect.Method;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.Collection;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.SortedMap;
/** /**
* UI Utils * UI Utils
...@@ -133,6 +136,29 @@ public class UIUtils { ...@@ -133,6 +136,29 @@ public class UIUtils {
}; };
} }
public static VerifyListener getLongVerifyListener(Text text) {
return new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
// get old text and create new text by using the VerifyEvent.text
final String oldS = text.getText();
String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
boolean isLong = true;
try {
Long.parseLong(newS);
}
catch(NumberFormatException ex) {
isLong = false;
}
if(!isLong)
e.doit = false;
}
};
}
public static void createToolBarSeparator(ToolBar toolBar, int style) { public static void createToolBarSeparator(ToolBar toolBar, int style) {
Label label = new Label(toolBar, SWT.NONE); Label label = new Label(toolBar, SWT.NONE);
label.setImage(DBeaverIcons.getImage(UIIcon.DRAG_HANDLE)); label.setImage(DBeaverIcons.getImage(UIIcon.DRAG_HANDLE));
......
...@@ -20,6 +20,7 @@ package org.jkiss.dbeaver.ui.dialogs.tools; ...@@ -20,6 +20,7 @@ package org.jkiss.dbeaver.ui.dialogs.tools;
import org.eclipse.jface.wizard.WizardPage; import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.NLS; import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
...@@ -82,10 +83,18 @@ public class DatabaseWizardPageLog extends WizardPage { ...@@ -82,10 +83,18 @@ public class DatabaseWizardPageLog extends WizardPage {
public void run() { public void run() {
synchronized (DatabaseWizardPageLog.this) { synchronized (DatabaseWizardPageLog.this) {
if (!dumpLogText.isDisposed()) { if (!dumpLogText.isDisposed()) {
int caretOffset = dumpLogText.getCaretOffset();
dumpLogText.append(line); dumpLogText.append(line);
//dumpLogText.append(ContentUtils.getDefaultLineSeparator()); //dumpLogText.append(ContentUtils.getDefaultLineSeparator());
dumpLogText.setCaretOffset(dumpLogText.getCharCount()); dumpLogText.setCaretOffset(dumpLogText.getCharCount());
dumpLogText.showSelection(); dumpLogText.showSelection();
if (error) {
StyleRange style1Range = new StyleRange();
style1Range.start = caretOffset;
style1Range.length = line.length();
style1Range.foreground = dumpLogText.getDisplay().getSystemColor(SWT.COLOR_RED);
dumpLogText.setStyleRange(style1Range);
}
} }
} }
} }
......
...@@ -44,6 +44,8 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato ...@@ -44,6 +44,8 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
private static final Log log = Log.getLog(MockDataExecuteWizard.class); private static final Log log = Log.getLog(MockDataExecuteWizard.class);
public static final int BATCH_SIZE = 1000;
private MockDataWizardPageSettings settingsPage; private MockDataWizardPageSettings settingsPage;
private MockDataSettings mockDataSettings; private MockDataSettings mockDataSettings;
...@@ -123,7 +125,7 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato ...@@ -123,7 +125,7 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
batch.close(); batch.close();
} }
} catch (Exception e) { } catch (Exception e) {
String message = "Error removing the data."; String message = " Error removing the data: " + e.getMessage() + ".";
log.error(message, e); log.error(message, e);
logPage.appendLog(message, true); logPage.appendLog(message, true);
} finally { } finally {
...@@ -139,40 +141,59 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato ...@@ -139,40 +141,59 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
logPage.appendLog("\nInserting Mock Data into the '" + dataManipulator.getName() + "'.\n"); logPage.appendLog("\nInserting Mock Data into the '" + dataManipulator.getName() + "'.\n");
DBCStatistics insertStats = new DBCStatistics(); DBCStatistics insertStats = new DBCStatistics();
for (int i = 0; i < mockDataSettings.getRowsNumber(); i++) { long rowsNumber = mockDataSettings.getRowsNumber();
List<DBDAttributeValue> keyAttributes = new ArrayList<>(); long quotient = rowsNumber / BATCH_SIZE;
Collection<? extends DBSEntityAttribute> attributes = ((DBSEntity) dataManipulator).getAttributes(monitor); long modulo = rowsNumber % BATCH_SIZE;
for (DBSEntityAttribute attribute : attributes) { if (modulo > 0) {
Object value = attribute.getDefaultValue(); quotient++;
DBSDataType dataType = ((DBSTypedObjectEx) attribute).getDataType(); }
DBPDataKind dataKind = dataType.getDataKind(); int counter = 0;
switch (dataKind) {
case NUMERIC:
value = MockDataGenerator.generateNumeric((int) attribute.getMaxLength(), attribute.getPrecision(), attribute.getScale()); break;
case STRING:
value = MockDataGenerator.generateTextUpTo((int) attribute.getMaxLength()); break;
case DATETIME:
value = MockDataGenerator.generateDate(); break;
}
keyAttributes.add(new DBDAttributeValue(attribute, value));
}
try (DBSDataManipulator.ExecuteBatch batch = dataManipulator.insertData( DBSDataManipulator.ExecuteBatch batch = null;
session, for (int q = 0; q < quotient; q++) {
DBDAttributeValue.getAttributes(keyAttributes), try {
null, for (int i = 0; i < BATCH_SIZE; i++) {
executionSource)) List<DBDAttributeValue> attributeValues = new ArrayList<>();
{ Collection<? extends DBSEntityAttribute> attributes = ((DBSEntity) dataManipulator).getAttributes(monitor);
batch.add(DBDAttributeValue.getValues(keyAttributes)); for (DBSEntityAttribute attribute : attributes) {
Object value = attribute.getDefaultValue();
DBSDataType dataType = ((DBSTypedObjectEx) attribute).getDataType();
DBPDataKind dataKind = dataType.getDataKind();
switch (dataKind) {
case NUMERIC:
value = MockDataGenerator.generateNumeric((int) attribute.getMaxLength(), attribute.getPrecision(), attribute.getScale()); break;
case STRING:
value = MockDataGenerator.generateTextUpTo((int) attribute.getMaxLength()); break;
case DATETIME:
value = MockDataGenerator.generateDate(); break;
}
attributeValues.add(new DBDAttributeValue(attribute, value));
}
if (batch == null) {
batch = dataManipulator.insertData(
session,
DBDAttributeValue.getAttributes(attributeValues),
null,
executionSource);
}
if (counter++ < rowsNumber) {
batch.add(DBDAttributeValue.getValues(attributeValues));
}
}
insertStats.accumulate(batch.execute(session)); insertStats.accumulate(batch.execute(session));
} }
finally {
batch.close();
batch = null;
}
} }
logPage.appendLog(" Rows updated: " + insertStats.getRowsUpdated() + "\n"); logPage.appendLog(" Rows updated: " + insertStats.getRowsUpdated() + "\n");
logPage.appendLog(" Duration: " + insertStats.getExecuteTime() + "ms\n"); logPage.appendLog(" Duration: " + insertStats.getExecuteTime() + "ms\n");
} catch (DBException e) { } catch (DBException e) {
String message = "Error inserting Mock Data."; String message = " Error inserting Mock Data: " + e.getMessage() + ".";
log.error(message, e); log.error(message, e);
logPage.appendLog(message, true); logPage.appendLog(message, true);
} }
......
...@@ -20,7 +20,7 @@ package org.jkiss.dbeaver.ext.mockdata; ...@@ -20,7 +20,7 @@ package org.jkiss.dbeaver.ext.mockdata;
public class MockDataSettings { public class MockDataSettings {
private boolean removeOldData; private boolean removeOldData;
private int rowsNumber; private long rowsNumber = 10;
public boolean isRemoveOldData() { public boolean isRemoveOldData() {
return removeOldData; return removeOldData;
...@@ -30,11 +30,11 @@ public class MockDataSettings { ...@@ -30,11 +30,11 @@ public class MockDataSettings {
this.removeOldData = removeOldData; this.removeOldData = removeOldData;
} }
public int getRowsNumber() { public long getRowsNumber() {
return rowsNumber; return rowsNumber;
} }
public void setRowsNumber(int rowsNumber) { public void setRowsNumber(long rowsNumber) {
this.rowsNumber = rowsNumber; this.rowsNumber = rowsNumber;
} }
} }
...@@ -17,14 +17,13 @@ ...@@ -17,14 +17,13 @@
*/ */
package org.jkiss.dbeaver.ext.mockdata; package org.jkiss.dbeaver.ext.mockdata;
import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.*;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Spinner; import org.eclipse.swt.widgets.Text;
import org.jkiss.dbeaver.ui.UIUtils; import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.tools.AbstractToolWizardPage; import org.jkiss.dbeaver.ui.dialogs.tools.AbstractToolWizardPage;
...@@ -33,7 +32,7 @@ public class MockDataWizardPageSettings extends AbstractToolWizardPage<MockDataE ...@@ -33,7 +32,7 @@ public class MockDataWizardPageSettings extends AbstractToolWizardPage<MockDataE
private MockDataSettings mockDataSettings; private MockDataSettings mockDataSettings;
private Button removeOldDataCheck; private Button removeOldDataCheck;
private Spinner rowsSpinner; private Text rowsText;
protected MockDataWizardPageSettings(MockDataExecuteWizard wizard, MockDataSettings mockDataSettings) protected MockDataWizardPageSettings(MockDataExecuteWizard wizard, MockDataSettings mockDataSettings)
{ {
...@@ -65,9 +64,17 @@ public class MockDataWizardPageSettings extends AbstractToolWizardPage<MockDataE ...@@ -65,9 +64,17 @@ public class MockDataWizardPageSettings extends AbstractToolWizardPage<MockDataE
removeOldDataCheck.setLayoutData( removeOldDataCheck.setLayoutData(
new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 3, 1)); new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 3, 1));
this.rowsSpinner = UIUtils.createLabelSpinner(settingsGroup, "Rows", mockDataSettings.getRowsNumber(), 1, Integer.MAX_VALUE); this.rowsText = UIUtils.createLabelText(
rowsSpinner.addSelectionListener(changeListener); settingsGroup, "Rows", String.valueOf(mockDataSettings.getRowsNumber()), SWT.BORDER,
rowsSpinner.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 3, 1)); new GridData(110, SWT.DEFAULT));
rowsText.addSelectionListener(changeListener);
rowsText.addVerifyListener(UIUtils.getLongVerifyListener(rowsText));
rowsText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
updateState();
}
});
setControl(composite); setControl(composite);
...@@ -76,7 +83,7 @@ public class MockDataWizardPageSettings extends AbstractToolWizardPage<MockDataE ...@@ -76,7 +83,7 @@ public class MockDataWizardPageSettings extends AbstractToolWizardPage<MockDataE
private void updateState() private void updateState()
{ {
mockDataSettings.setRemoveOldData(removeOldDataCheck.getSelection()); mockDataSettings.setRemoveOldData(removeOldDataCheck.getSelection());
mockDataSettings.setRowsNumber(rowsSpinner.getSelection()); mockDataSettings.setRowsNumber(Long.parseLong(rowsText.getText()));
} }
@Override @Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册