Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAY-2144 cdbimport always fails for databases which don't support catalogs #134

Merged
merged 2 commits into from Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,87 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.cayenne.tools;

import org.apache.cayenne.configuration.DataNodeDescriptor;
import org.apache.cayenne.configuration.server.DataSourceFactory;
import org.apache.cayenne.configuration.server.DbAdapterFactory;
import org.apache.cayenne.dba.DbAdapter;
import org.apache.cayenne.dbimport.Catalog;
import org.apache.cayenne.dbimport.ReverseEngineering;
import org.apache.cayenne.di.Injector;
import org.apache.cayenne.tools.dbimport.DbImportConfiguration;

import java.util.Collection;
import javax.sql.DataSource;

class DbImportConfigurationValidator implements Cloneable {
private final ReverseEngineering reverseEngineering;
private final DbImportConfiguration config;
private final Injector injector;

DbImportConfigurationValidator(ReverseEngineering reverseEngineering, DbImportConfiguration config, Injector injector) {
this.reverseEngineering = reverseEngineering;
this.config = config;
this.injector = injector;
}

void validate() throws Exception {
DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
DataSource dataSource;
DbAdapter adapter;

try {
dataSource = injector.getInstance(DataSourceFactory.class)
.getDataSource(dataNodeDescriptor);
adapter = injector.getInstance(DbAdapterFactory.class)
.createAdapter(dataNodeDescriptor, dataSource);
} catch (Exception ex) {
throw new Exception("Error creating DataSource or DbAdapter " +
"for DataNodeDescriptor (" + dataNodeDescriptor + ")", ex);
}

if (adapter != null
&& !adapter.supportsCatalogsOnReverseEngineering()
&& !isReverseEngineeringCatalogsEmpty()) {
String message = "Your database does not support catalogs on reverse engineering. " +
"It allows to connect to only one at the moment. " +
"Please don't note catalogs as param.";
throw new Exception(message);
}
}

private boolean isReverseEngineeringCatalogsEmpty() {
Collection<Catalog> catalogs = reverseEngineering.getCatalogs();
if (catalogs == null) {
return true;
}

if (catalogs.isEmpty()) {
return true;
}

for (Catalog catalog : catalogs) {
if (catalog.getName() != null) {
return false;
}
}

return true;
}
}
Expand Up @@ -18,12 +18,16 @@
****************************************************************/
package org.apache.cayenne.tools;

import org.apache.cayenne.configuration.DataNodeDescriptor;
import org.apache.cayenne.configuration.server.DataSourceFactory;
import org.apache.cayenne.configuration.server.DbAdapterFactory;
import org.apache.cayenne.conn.DataSourceInfo;
import org.apache.cayenne.dba.DbAdapter;
import org.apache.cayenne.dbimport.*;
import org.apache.cayenne.dbimport.Catalog;
import org.apache.cayenne.dbimport.ExcludeColumn;
import org.apache.cayenne.dbimport.ExcludeProcedure;
import org.apache.cayenne.dbimport.ExcludeTable;
import org.apache.cayenne.dbimport.IncludeColumn;
import org.apache.cayenne.dbimport.IncludeProcedure;
import org.apache.cayenne.dbimport.IncludeTable;
import org.apache.cayenne.dbimport.ReverseEngineering;
import org.apache.cayenne.dbimport.Schema;
import org.apache.cayenne.dbsync.DbSyncModule;
import org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator;
import org.apache.cayenne.dbsync.reverse.filters.FiltersConfigBuilder;
Expand All @@ -39,7 +43,6 @@
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

import javax.sql.DataSource;
import java.io.File;

public class DbImporterTask extends Task {
Expand Down Expand Up @@ -111,9 +114,16 @@ public void execute() {
config.setSkipPrimaryKeyLoading(reverseEngineering.getSkipPrimaryKeyLoading());
config.setTableTypes(reverseEngineering.getTableTypes());

Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger), new DbImportModule());
Injector injector = DIBootstrap.createInjector(new DbSyncModule(),
new ToolsModule(logger), new DbImportModule());

validateDbImportConfiguration(config, injector);
DbImportConfigurationValidator validator = new DbImportConfigurationValidator(
reverseEngineering, config, injector);
try {
validator.validate();
} catch (Exception ex) {
throw new BuildException(ex.getMessage(), ex);
}

try {
injector.getInstance(DbImportAction.class).execute(config);
Expand All @@ -133,27 +143,6 @@ public void execute() {
}
}

private void validateDbImportConfiguration(DbImportConfiguration config, Injector injector) throws BuildException {
DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
DataSource dataSource = null;
DbAdapter adapter = null;

try {
dataSource = injector.getInstance(DataSourceFactory.class).getDataSource(dataNodeDescriptor);
adapter = injector.getInstance(DbAdapterFactory.class).createAdapter(dataNodeDescriptor, dataSource);

if (!adapter.supportsCatalogsOnReverseEngineering() &&
reverseEngineering.getCatalogs() != null && !reverseEngineering.getCatalogs().isEmpty()) {
String message = "Your database does not support catalogs on reverse engineering. " +
"It allows to connect to only one at the moment. Please don't note catalogs as param.";
throw new BuildException(message);
}
} catch (Exception e) {
throw new BuildException("Error creating DataSource ("
+ dataSource + ") or DbAdapter (" + adapter + ") for DataNodeDescriptor (" + dataNodeDescriptor + ")", e);
}
}

/**
* Validates attributes that are not related to internal
* DefaultClassGenerator. Throws BuildException if attributes are invalid.
Expand Down
1 change: 1 addition & 0 deletions docs/doc/src/main/resources/RELEASE-NOTES.txt
Expand Up @@ -68,6 +68,7 @@ CAY-2128 Modeler stored procedures are not imported
CAY-2131 Modeler NullPointerException in reverse engineering when importing different catalogs in one datamap
CAY-2138 NVARCHAR, LONGNVARCHAR and NCLOB types are missing from Firebird types.xml
CAY-2143 NPE in BaseSchemaUpdateStrategy
CAY-2144 cdbimport always fails for databases which don't support catalogs

----------------------------------
Release: 4.0.M3
Expand Down
Expand Up @@ -18,10 +18,6 @@
****************************************************************/
package org.apache.cayenne.tools;

import org.apache.cayenne.configuration.DataNodeDescriptor;
import org.apache.cayenne.configuration.server.DataSourceFactory;
import org.apache.cayenne.configuration.server.DbAdapterFactory;
import org.apache.cayenne.dba.DbAdapter;
import org.apache.cayenne.dbimport.ReverseEngineering;
import org.apache.cayenne.dbsync.DbSyncModule;
import org.apache.cayenne.dbsync.reverse.filters.FiltersConfigBuilder;
Expand All @@ -37,7 +33,6 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import javax.sql.DataSource;
import java.io.File;

/**
Expand Down Expand Up @@ -163,9 +158,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
Log logger = new MavenLogger(this);

DbImportConfiguration config = createConfig(logger);
Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger), new DbImportModule());
Injector injector = DIBootstrap.createInjector(
new DbSyncModule(), new ToolsModule(logger), new DbImportModule());

validateDbImportConfiguration(config, injector);
DbImportConfigurationValidator validator = new DbImportConfigurationValidator(
reverseEngineering, config, injector);
try {
validator.validate();
} catch (Exception ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
}

try {
injector.getInstance(DbImportAction.class).execute(config);
Expand All @@ -183,27 +185,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

private void validateDbImportConfiguration(DbImportConfiguration config, Injector injector) throws MojoExecutionException {
DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
DataSource dataSource = null;
DbAdapter adapter = null;

try {
dataSource = injector.getInstance(DataSourceFactory.class).getDataSource(dataNodeDescriptor);
adapter = injector.getInstance(DbAdapterFactory.class).createAdapter(dataNodeDescriptor, dataSource);

if (!adapter.supportsCatalogsOnReverseEngineering() &&
reverseEngineering.getCatalogs() != null && !reverseEngineering.getCatalogs().isEmpty()) {
String message = "Your database does not support catalogs on reverse engineering. " +
"It allows to connect to only one at the moment. Please don't note catalogs as param.";
throw new MojoExecutionException(message);
}
} catch (Exception e) {
throw new MojoExecutionException("Error creating DataSource ("
+ dataSource + ") or DbAdapter (" + adapter + ") for DataNodeDescriptor (" + dataNodeDescriptor + ")", e);
}
}

DbImportConfiguration createConfig(Log logger) {

DbImportConfiguration config = new DbImportConfiguration();
Expand Down