Search in sources :

Example 1 with MockGeneratorDescriptor

use of org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor in project dbeaver by dbeaver.

the class MockDataExecuteWizard method executeProcess.

@Override
public boolean executeProcess(DBRProgressMonitor monitor, DBSDataManipulator dataManipulator) throws IOException {
    DBCExecutionContext context = dataManipulator.getDataSource().getDefaultContext(true);
    try (DBCSession session = context.openSession(monitor, DBCExecutionPurpose.USER, MockDataMessages.tools_mockdata_generate_data_task)) {
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(session.getExecutionContext());
        boolean autoCommit;
        try {
            autoCommit = txnManager == null || txnManager.isAutoCommit();
        } catch (DBCException e) {
            log.error(e);
            autoCommit = true;
        }
        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");
            monitor.subTask("Cleanup old data");
            DBCStatistics deleteStats = new DBCStatistics();
            try {
                // TODO: truncate is much faster than delete
                try (DBSDataManipulator.ExecuteBatch batch = dataManipulator.deleteData(session, new DBSAttributeBase[] {}, executionSource)) {
                    batch.add(new Object[] {});
                    deleteStats.accumulate(batch.execute(session));
                }
                if (txnManager != null && !autoCommit) {
                    txnManager.commit(session);
                }
            } catch (Exception e) {
                success = false;
                String message = "    Error removing the data: " + e.getMessage();
                log.error(message, e);
                logPage.appendLog(message + "\n\n", true);
            }
            logPage.appendLog("    Rows updated: " + deleteStats.getRowsUpdated() + "\n");
            logPage.appendLog("    Duration: " + deleteStats.getExecuteTime() + "ms\n\n");
        } else {
            logPage.appendLog("Old data isn't removed.\n\n");
        }
        if (!success) {
            return true;
        }
        try {
            monitor.subTask("Insert data");
            logPage.appendLog("Inserting mock data into the '" + dataManipulator.getName() + "'.\n");
            DBCStatistics insertStats = new DBCStatistics();
            // build and init the generators
            generators.clear();
            DBSEntity dbsEntity = (DBSEntity) dataManipulator;
            Collection<? extends DBSAttributeBase> attributes = DBUtils.getRealAttributes(dbsEntity.getAttributes(monitor));
            for (DBSAttributeBase attribute : attributes) {
                MockGeneratorDescriptor generatorDescriptor = mockDataSettings.getGeneratorDescriptor(mockDataSettings.getAttributeGeneratorProperties(attribute).getSelectedGeneratorId());
                if (generatorDescriptor != null) {
                    MockValueGenerator generator = generatorDescriptor.createGenerator();
                    MockDataSettings.AttributeGeneratorProperties generatorPropertySource = this.mockDataSettings.getAttributeGeneratorProperties(attribute);
                    String selectedGenerator = generatorPropertySource.getSelectedGeneratorId();
                    Map<Object, Object> generatorProperties = generatorPropertySource.getGeneratorPropertySource(selectedGenerator).getPropertiesWithDefaults();
                    generator.init(dataManipulator, attribute, generatorProperties);
                    generators.put(attribute.getName(), generator);
                }
            }
            monitor.done();
            long rowsNumber = mockDataSettings.getRowsNumber();
            long quotient = rowsNumber / BATCH_SIZE;
            long modulo = rowsNumber % BATCH_SIZE;
            if (modulo > 0) {
                quotient++;
            }
            int counter = 0;
            monitor.beginTask("Insert data", (int) rowsNumber);
            // generate and insert the data
            session.enableLogging(false);
            DBSDataManipulator.ExecuteBatch batch = null;
            for (int q = 0; q < quotient; q++) {
                if (monitor.isCanceled()) {
                    break;
                }
                if (counter > 0) {
                    if (txnManager != null && !autoCommit) {
                        txnManager.commit(session);
                    }
                    monitor.subTask(String.valueOf(counter) + " rows inserted");
                    monitor.worked(BATCH_SIZE);
                }
                try {
                    for (int i = 0; (i < BATCH_SIZE && counter < rowsNumber); i++) {
                        if (monitor.isCanceled()) {
                            break;
                        }
                        List<DBDAttributeValue> attributeValues = new ArrayList<>();
                        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) {
                            batch = dataManipulator.insertData(session, DBDAttributeValue.getAttributes(attributeValues), null, executionSource);
                        }
                        if (counter++ < rowsNumber) {
                            batch.add(DBDAttributeValue.getValues(attributeValues));
                        }
                    }
                    if (batch != null) {
                        insertStats.accumulate(batch.execute(session));
                    }
                } catch (Exception e) {
                    processGeneratorException(e);
                    if (e instanceof DBException) {
                        throw e;
                    }
                } finally {
                    if (batch != null) {
                        batch.close();
                        batch = null;
                    }
                }
            }
            if (txnManager != null && !autoCommit) {
                txnManager.commit(session);
            }
            logPage.appendLog("    Rows updated: " + insertStats.getRowsUpdated() + "\n");
            logPage.appendLog("    Duration: " + insertStats.getExecuteTime() + "ms\n\n");
        } catch (DBException e) {
            String message = "    Error inserting mock data: " + e.getMessage();
            log.error(message, e);
            logPage.appendLog(message + "\n\n", true);
        }
    } finally {
        monitor.done();
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDAttributeValue(org.jkiss.dbeaver.model.data.DBDAttributeValue) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) MockGeneratorDescriptor(org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor) MockValueGenerator(org.jkiss.dbeaver.ext.mockdata.model.MockValueGenerator) IOException(java.io.IOException) DBException(org.jkiss.dbeaver.DBException) DBSDataManipulator(org.jkiss.dbeaver.model.struct.DBSDataManipulator) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Example 2 with MockGeneratorDescriptor

use of org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor in project dbeaver by dbeaver.

the class MockDataWizardPageSettings method selectGenerator.

private void selectGenerator(DBSAttributeBase attribute, String generatorName) {
    MockGeneratorDescriptor generatorForName = mockDataSettings.findGeneratorForName(attribute, generatorName);
    if (generatorForName != null) {
        saveGeneratorProperties();
        reloadProperties(attribute, generatorForName.getId());
    }
    columnsTableViewer.refresh(true, true);
}
Also used : MockGeneratorDescriptor(org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor)

Example 3 with MockGeneratorDescriptor

use of org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor in project dbeaver by dbeaver.

the class MockDataSettings method init.

// populate attribute generators properties map
public void init(MockDataExecuteWizard wizard) throws DBException {
    List<DBSDataManipulator> databaseObjects = wizard.getDatabaseObjects();
    // TODO only the first
    DBSDataManipulator dataManipulator = databaseObjects.iterator().next();
    entity = (DBSEntity) dataManipulator;
    attributes = new ArrayList<>();
    try {
        DBeaverUI.run(wizard.getContainer(), true, true, new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    attributes.addAll(DBUtils.getRealAttributes(entity.getAttributes(monitor)));
                    MockGeneratorRegistry generatorRegistry = MockGeneratorRegistry.getInstance();
                    for (DBSAttributeBase attribute : attributes) {
                        AttributeGeneratorProperties generatorProperties = new AttributeGeneratorProperties(attribute);
                        attributeGenerators.put(attribute.getName(), generatorProperties);
                        // ((JDBCColumnKeyType) attribute).isInUniqueKey()
                        List<DBSEntityReferrer> attributeReferrers = DBUtils.getAttributeReferrers(monitor, (DBSEntityAttribute) attribute);
                        if (!CommonUtils.isEmpty(attributeReferrers)) {
                            MockGeneratorDescriptor generator = generatorRegistry.getGenerator(FK_GENERATOR_ID);
                            putGenerator(generatorProperties, generator);
                        } else {
                            List<MockGeneratorDescriptor> generators = generatorRegistry.findAllGenerators(dataManipulator.getDataSource(), attribute);
                            for (MockGeneratorDescriptor generator : generators) {
                                putGenerator(generatorProperties, generator);
                            }
                        }
                    }
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        DBUserInterface.getInstance().showError("Transfer init failed", "Can't start data transfer", e.getTargetException());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) MockGeneratorDescriptor(org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor) MockGeneratorRegistry(org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 4 with MockGeneratorDescriptor

use of org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor in project dbeaver by dbeaver.

the class MockDataWizardPageSettings method reloadProperties.

private void reloadProperties(DBSAttributeBase attribute, String generatorId) {
    AttributeGeneratorProperties attributeGeneratorProperties = mockDataSettings.getAttributeGeneratorProperties(attribute);
    if (generatorId == null) {
        generatorId = attributeGeneratorProperties.getSelectedGeneratorId();
    }
    if (attribute == selectedAttribute) {
        String selectedGenerator = attributeGeneratorProperties.getSelectedGeneratorId();
        if (Objects.equals(selectedGenerator, generatorId)) {
            // do nothing
            return;
        }
    }
    selectedAttribute = attribute;
    mockDataSettings.setSelectedAttribute(attribute.getName());
    generatorId = attributeGeneratorProperties.setSelectedGeneratorId(generatorId);
    // set properties
    propertySource = attributeGeneratorProperties.getGeneratorPropertySource(generatorId);
    if (propertySource != null) {
        propsEditor.loadProperties(propertySource);
        propsEditor.setExpandMode(PropertyTreeViewer.ExpandMode.FIRST);
        propsEditor.expandAll();
    } else {
        propsEditor.clearProperties();
    }
    // generator combo & description
    List<String> generators = new ArrayList<>();
    for (String genId : attributeGeneratorProperties.getGenerators()) {
        generators.add(mockDataSettings.getGeneratorDescriptor(genId).getLabel());
    }
    generatorDescriptionLink.setVisible(false);
    if (!generators.isEmpty()) {
        generatorCombo.setItems(generators.toArray(new String[generators.size()]));
        MockGeneratorDescriptor generatorDescriptor = mockDataSettings.getGeneratorDescriptor(generatorId);
        generatorCombo.setText(generatorDescriptor.getLabel());
        generatorCombo.setEnabled(true);
        generatorDescriptionLabel.setText(generatorDescriptor.getDescription());
        if (!CommonUtils.isEmpty(generatorDescriptor.getLink())) {
            generatorDescriptionLink.setText("<a>" + generatorDescriptor.getLink() + "</a>");
            generatorLinkUrl = generatorDescriptor.getUrl();
            generatorDescriptionLink.setVisible(true);
        }
    } else {
        generatorCombo.setItems(new String[] { "Not found" });
        generatorCombo.setText("Not found");
        generatorCombo.setEnabled(false);
        generatorDescriptionLabel.setText("");
    }
    generatorDescriptionLink.getParent().layout();
}
Also used : MockGeneratorDescriptor(org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor) AttributeGeneratorProperties(org.jkiss.dbeaver.ext.mockdata.MockDataSettings.AttributeGeneratorProperties)

Aggregations

MockGeneratorDescriptor (org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor)4 DBException (org.jkiss.dbeaver.DBException)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AttributeGeneratorProperties (org.jkiss.dbeaver.ext.mockdata.MockDataSettings.AttributeGeneratorProperties)1 MockGeneratorRegistry (org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorRegistry)1 MockValueGenerator (org.jkiss.dbeaver.ext.mockdata.model.MockValueGenerator)1 DBDAttributeValue (org.jkiss.dbeaver.model.data.DBDAttributeValue)1 AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)1 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)1 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)1 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)1 DBSDataManipulator (org.jkiss.dbeaver.model.struct.DBSDataManipulator)1 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)1