use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class ProjectImportWizard method importProjects.
private void importProjects(DBRProgressMonitor monitor) throws IOException, DBException {
try (ZipFile zipFile = new ZipFile(data.getImportFile(), ZipFile.OPEN_READ)) {
ZipEntry metaEntry = zipFile.getEntry(ExportConstants.META_FILENAME);
if (metaEntry == null) {
throw new DBException("Cannot find meta file");
}
final Map<String, String> libMap = new HashMap<>();
final Map<String, String> driverMap = new HashMap<>();
InputStream metaStream = zipFile.getInputStream(metaEntry);
if (metaStream == null) {
//$NON-NLS-2$
throw new DBException("Cannot open meta file '" + metaEntry.getName() + "'");
}
try {
final Document metaDocument = XMLUtils.parseDocument(metaStream);
{
// Read libraries map
final Element libsElement = XMLUtils.getChildElement(metaDocument.getDocumentElement(), ExportConstants.TAG_LIBRARIES);
if (libsElement != null) {
final Collection<Element> libList = XMLUtils.getChildElementList(libsElement, RegistryConstants.TAG_FILE);
monitor.beginTask(CoreMessages.dialog_project_import_wizard_monitor_load_libraries, libList.size());
for (Element libElement : libList) {
libMap.put(libElement.getAttribute(ExportConstants.ATTR_PATH), libElement.getAttribute(ExportConstants.ATTR_FILE));
monitor.worked(1);
}
monitor.done();
}
}
{
// Collect drivers to import
final Element driversElement = XMLUtils.getChildElement(metaDocument.getDocumentElement(), RegistryConstants.TAG_DRIVERS);
if (driversElement != null) {
final Collection<Element> driverList = XMLUtils.getChildElementList(driversElement, RegistryConstants.TAG_DRIVER);
monitor.beginTask(CoreMessages.dialog_project_import_wizard_monitor_import_drivers, driverList.size());
for (Element driverElement : driverList) {
if (monitor.isCanceled()) {
break;
}
importDriver(monitor, driverElement, zipFile, libMap, driverMap);
monitor.worked(1);
}
// Save drivers
DataSourceProviderRegistry.getInstance().saveDrivers();
monitor.done();
}
}
{
// Import projects
final Element projectsElement = XMLUtils.getChildElement(metaDocument.getDocumentElement(), ExportConstants.TAG_PROJECTS);
if (projectsElement != null) {
final Collection<Element> projectList = XMLUtils.getChildElementList(projectsElement, ExportConstants.TAG_PROJECT);
monitor.beginTask(CoreMessages.dialog_project_import_wizard_monitor_import_projects, projectList.size());
for (Element projectElement : projectList) {
if (monitor.isCanceled()) {
break;
}
importProject(monitor, projectElement, zipFile, driverMap);
monitor.worked(1);
}
monitor.done();
}
}
} catch (XMLException e) {
throw new DBException("Cannot parse meta file", e);
} catch (CoreException e) {
throw new DBException("Cannot persist project", e);
} finally {
metaStream.close();
}
}
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class DatabaseTransferConsumer method generateTargetTableDDL.
public static String generateTargetTableDDL(DBRProgressMonitor monitor, DBPDataSource dataSource, DBSObjectContainer schema, DatabaseMappingContainer containerMapping) throws DBException {
monitor.subTask("Create table " + containerMapping.getTargetName());
StringBuilder sql = new StringBuilder(500);
if (!(dataSource instanceof SQLDataSource)) {
throw new DBException("Data source doesn't support SQL");
}
SQLDataSource targetDataSource = (SQLDataSource) dataSource;
String tableName = DBObjectNameCaseTransformer.transformName(targetDataSource, containerMapping.getTargetName());
containerMapping.setTargetName(tableName);
sql.append("CREATE TABLE ");
if (schema instanceof DBSSchema || schema instanceof DBSCatalog) {
sql.append(DBUtils.getQuotedIdentifier(schema));
sql.append(targetDataSource.getSQLDialect().getCatalogSeparator());
}
sql.append(DBUtils.getQuotedIdentifier(targetDataSource, tableName)).append("(\n");
Map<DBSAttributeBase, DatabaseMappingAttribute> mappedAttrs = new HashMap<>();
for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
if (attr.getMappingType() != DatabaseMappingType.create) {
continue;
}
if (!mappedAttrs.isEmpty())
sql.append(",\n");
appendAttributeClause(dataSource, sql, attr);
mappedAttrs.put(attr.getSource(), attr);
}
if (containerMapping.getSource() instanceof DBSEntity) {
// Make primary key
Collection<? extends DBSEntityAttribute> identifier = DBUtils.getBestTableIdentifier(monitor, (DBSEntity) containerMapping.getSource());
if (!CommonUtils.isEmpty(identifier)) {
boolean idMapped = true;
for (DBSEntityAttribute idAttr : identifier) {
if (!mappedAttrs.containsKey(idAttr)) {
idMapped = false;
break;
}
}
if (idMapped) {
sql.append(",\nPRIMARY KEY (");
boolean hasAttr = false;
for (DBSEntityAttribute idAttr : identifier) {
DatabaseMappingAttribute mappedAttr = mappedAttrs.get(idAttr);
if (hasAttr)
sql.append(",");
sql.append(mappedAttr.getTargetName());
hasAttr = true;
}
sql.append(")\n");
}
}
}
sql.append(")");
return sql.toString();
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class DatabaseTransferConsumer method startTransfer.
@Override
public void startTransfer(DBRProgressMonitor monitor) throws DBException {
// Create all necessary database objects
monitor.beginTask("Create necessary database objects", 1);
try {
DBSObjectContainer container = settings.getContainer();
if (container == null) {
throw new DBException("No target datasource");
}
boolean hasNewObjects = false;
DBPDataSource dataSource = container.getDataSource();
try (DBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Create target metadata")) {
for (DatabaseMappingContainer containerMapping : settings.getDataMappings().values()) {
switch(containerMapping.getMappingType()) {
case create:
createTargetTable(session, containerMapping);
hasNewObjects = true;
break;
case existing:
for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
if (attr.getMappingType() == DatabaseMappingType.create) {
createTargetAttribute(session, attr);
hasNewObjects = true;
}
}
break;
}
}
}
if (hasNewObjects) {
// Refresh node
monitor.subTask("Refresh navigator model");
settings.getContainerNode().refreshNode(monitor, this);
// Reflect database changes in mappings
for (DatabaseMappingContainer containerMapping : settings.getDataMappings().values()) {
switch(containerMapping.getMappingType()) {
case create:
DBSObject newTarget = container.getChild(monitor, containerMapping.getTargetName());
if (newTarget == null) {
throw new DBCException("New table " + containerMapping.getTargetName() + " not found in container " + DBUtils.getObjectFullName(container, DBPEvaluationContext.UI));
} else if (!(newTarget instanceof DBSDataManipulator)) {
throw new DBCException("New table " + DBUtils.getObjectFullName(newTarget, DBPEvaluationContext.UI) + " doesn't support data manipulation");
}
containerMapping.setTarget((DBSDataManipulator) newTarget);
containerMapping.setMappingType(DatabaseMappingType.existing);
// ! Fall down is ok here
case existing:
for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
if (attr.getMappingType() == DatabaseMappingType.create) {
attr.updateMappingType(monitor);
if (attr.getTarget() == null) {
throw new DBCException("Can't find target attribute '" + attr.getTargetName() + "' in '" + containerMapping.getTargetName() + "'");
}
}
}
break;
}
}
}
} finally {
monitor.done();
}
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class CompareObjectsExecutor method compareChildren.
private void compareChildren(DBRProgressMonitor monitor, List<DBNDatabaseNode> nodes) throws DBException, InterruptedException {
// Compare children
int nodeCount = nodes.size();
List<DBNDatabaseNode[]> allChildren = new ArrayList<>(nodeCount);
for (int i = 0; i < nodeCount; i++) {
DBNDatabaseNode node = nodes.get(i);
// Cache structure if possible
if (node.getObject() instanceof DBSObjectContainer) {
((DBSObjectContainer) node.getObject()).cacheStructure(monitor, DBSObjectContainer.STRUCT_ALL);
}
try {
DBNDatabaseNode[] children = node.getChildren(monitor);
allChildren.add(children);
} catch (Exception e) {
log.warn("Error reading child nodes for compare", e);
allChildren.add(null);
}
}
Set<String> allChildNames = new LinkedHashSet<>();
for (DBNDatabaseNode[] childList : allChildren) {
if (childList == null)
continue;
for (DBNDatabaseNode child : childList) {
DBXTreeNode meta = child.getMeta();
if (meta.isVirtual()) {
// Skip virtual nodes
continue;
}
if (settings.isSkipSystemObjects() && child.getObject() instanceof DBPSystemObject && ((DBPSystemObject) child.getObject()).isSystem()) {
// Skip system objects
continue;
}
allChildNames.add(child.getNodeName());
}
}
for (String childName : allChildNames) {
int[] childIndexes = new int[nodeCount];
for (int i = 0; i < nodeCount; i++) {
childIndexes[i] = -1;
DBNDatabaseNode[] childList = allChildren.get(i);
if (childList == null)
continue;
for (int k = 0; k < childList.length; k++) {
DBNDatabaseNode child = childList[k];
if (child.getNodeName().equals(childName)) {
childIndexes[i] = k;
break;
}
}
}
List<DBNDatabaseNode> nodesToCompare = new ArrayList<>(nodeCount);
for (int i = 0; i < nodeCount; i++) {
if (childIndexes[i] == -1) {
// Missing
} else {
for (int k = 0; k < nodeCount; k++) {
if (k != i && childIndexes[k] != childIndexes[i]) {
// Wrong index - add to report
break;
}
}
final DBNDatabaseNode[] childList = allChildren.get(i);
if (childList != null) {
nodesToCompare.add(childList[childIndexes[i]]);
}
}
}
// Compare children recursively
compareNodes(monitor, nodesToCompare);
}
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class CompareObjectsWizard method performFinish.
@Override
public boolean performFinish() {
// Save settings
getSettings().saveTo(getDialogSettings());
showError(null);
// Compare
final CompareObjectsExecutor executor = new CompareObjectsExecutor(settings);
try {
DBeaverUI.run(getContainer(), true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
CompareReport report = generateReport(monitor, executor);
renderReport(monitor, report);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}
});
UIUtils.showMessageBox(getShell(), "Objects compare", "Objects compare finished", SWT.ICON_INFORMATION);
} catch (InvocationTargetException e) {
if (executor.getInitializeError() != null) {
showError(executor.getInitializeError().getMessage());
} else {
log.error(e.getTargetException());
showError(e.getTargetException().getMessage());
}
return false;
} catch (InterruptedException e) {
showError("Compare interrupted");
return false;
} finally {
executor.dispose();
}
// Done
return true;
}
Aggregations