Repository: sqoop Updated Branches: refs/heads/sqoop2 d11e83393 -> a7c705938
SQOOP-2530: Sqoop2: Add utility methods to get object name from object id (Dian Fu via Jarek Jarcec Cecho) Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/a7c70593 Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/a7c70593 Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/a7c70593 Branch: refs/heads/sqoop2 Commit: a7c705938f2b7f4f71c223dcb1fd9978beb8da2b Parents: d11e833 Author: Jarek Jarcec Cecho <[email protected]> Authored: Thu Aug 27 09:08:42 2015 -0700 Committer: Jarek Jarcec Cecho <[email protected]> Committed: Thu Aug 27 09:08:42 2015 -0700 ---------------------------------------------------------------------- .../sqoop/error/code/CommonRepositoryError.java | 4 +- .../apache/sqoop/repository/JdbcRepository.java | 13 ++ .../sqoop/repository/JdbcRepositoryHandler.java | 11 ++ .../org/apache/sqoop/repository/Repository.java | 9 + .../common/CommonRepositoryHandler.java | 166 ++++++++++--------- ...RepositoryInsertUpdateDeleteSelectQuery.java | 68 ++++---- .../repository/derby/TestConnectorHandling.java | 17 +- .../repository/mysql/TestConnectorHandling.java | 20 ++- .../postgresql/TestConnectorHandling.java | 20 ++- .../org/apache/sqoop/handler/HandlerUtils.java | 77 +++++++++ .../apache/sqoop/server/common/ServerError.java | 2 + 11 files changed, 286 insertions(+), 121 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/common/src/main/java/org/apache/sqoop/error/code/CommonRepositoryError.java ---------------------------------------------------------------------- diff --git a/common/src/main/java/org/apache/sqoop/error/code/CommonRepositoryError.java b/common/src/main/java/org/apache/sqoop/error/code/CommonRepositoryError.java index 9f4a0f8..37eb04a 100644 --- a/common/src/main/java/org/apache/sqoop/error/code/CommonRepositoryError.java +++ b/common/src/main/java/org/apache/sqoop/error/code/CommonRepositoryError.java @@ -28,8 +28,8 @@ public enum CommonRepositoryError implements ErrorCode { /** The system was unable to query the repository for given entity */ COMMON_0001("Unable to retrieve entity data"), - /** The repository contains more than one connector with same name */ - COMMON_0002("Invalid entity state - multiple connectors with name"), + /** The repository contains more than one connector with same name or id*/ + COMMON_0002("Invalid entity state - multiple connectors found"), /** The system does not support the given input type.*/ COMMON_0003("Unknown input type encountered"), http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java b/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java index 9a4853b..d7aa8ef 100644 --- a/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java +++ b/core/src/main/java/org/apache/sqoop/repository/JdbcRepository.java @@ -191,6 +191,19 @@ public class JdbcRepository extends Repository { * {@inheritDoc} */ @Override + public MConnector findConnector(final long id) { + return (MConnector) doWithConnection(new DoWithConnection() { + @Override + public Object doIt(Connection conn) throws Exception { + return handler.findConnector(id, conn); + } + }); + } + + /** + * {@inheritDoc} + */ + @Override public MConnector findConnector(final String shortName) { return (MConnector) doWithConnection(new DoWithConnection() { @Override http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java b/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java index f690887..5cbeda8 100644 --- a/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java +++ b/core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryHandler.java @@ -42,6 +42,17 @@ public abstract class JdbcRepositoryHandler { public abstract void initialize(JdbcRepositoryContext repoContext); /** + * Search for connector with given id in repository. + * And return corresponding connector entity. + * + * @param connectorId Connector id + * @param conn JDBC link for querying repository. + * @return null if connector is not yet registered in repository or + * loaded representation. + */ + public abstract MConnector findConnector(long connectorId, Connection conn); + + /** * Search for connector with given name in repository. * And return corresponding connector entity. * http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/core/src/main/java/org/apache/sqoop/repository/Repository.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/sqoop/repository/Repository.java b/core/src/main/java/org/apache/sqoop/repository/Repository.java index 610876c..10af697 100644 --- a/core/src/main/java/org/apache/sqoop/repository/Repository.java +++ b/core/src/main/java/org/apache/sqoop/repository/Repository.java @@ -104,6 +104,15 @@ public abstract class Repository { public abstract MDriver registerDriver(MDriver mDriverConfig, boolean autoUpgrade); /** + * Search for connector with given id in repository. + * + * @param id Connector id + * @return null if connector is not yet registered in repository or + * loaded representation. + */ + public abstract MConnector findConnector(long id); + + /** * Search for connector with given name in repository. * * And return corresponding entity structure. http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryHandler.java ---------------------------------------------------------------------- diff --git a/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryHandler.java b/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryHandler.java index af9324f..ae1e1b2 100644 --- a/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryHandler.java +++ b/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryHandler.java @@ -97,32 +97,59 @@ public abstract class CommonRepositoryHandler extends JdbcRepositoryHandler { * {@inheritDoc} */ @Override + public MConnector findConnector(long connectorId, Connection conn) { + if (LOG.isDebugEnabled()) { + LOG.debug("Looking up connector: " + connectorId); + } + try (PreparedStatement connectorFetchStmt = conn + .prepareStatement(crudQueries.getStmtSelectFromConfigurableById())) { + connectorFetchStmt.setLong(1, connectorId); + + return findConnectorInternal(connectorFetchStmt, conn, + String.valueOf(connectorId)); + } catch (SQLException ex) { + logException(ex, connectorId); + throw new SqoopException(CommonRepositoryError.COMMON_0001, + String.valueOf(connectorId), ex); + } + } + + /** + * {@inheritDoc} + */ + @Override public MConnector findConnector(String shortName, Connection conn) { if (LOG.isDebugEnabled()) { LOG.debug("Looking up connector: " + shortName); } - try (PreparedStatement connectorFetchStmt = conn.prepareStatement(crudQueries.getStmtSelectFromConfigurable())) { - connectorFetchStmt.setString(1, shortName); - - List<MConnector> connectors = loadConnectors(connectorFetchStmt, conn); - if (connectors.size() == 0) { - LOG.debug("Looking up connector with name: " + shortName + ", no connector found"); - return null; - } else if (connectors.size() == 1) { - MConnector mc = connectors.get(0); - LOG.debug("Looking up connector with name: " + shortName + ", found: " + mc); - return mc; - } else { - throw new SqoopException(CommonRepositoryError.COMMON_0002, shortName); - } + try (PreparedStatement connectorFetchStmt = conn + .prepareStatement(crudQueries.getStmtSelectFromConfigurableByName())) { + connectorFetchStmt.setString(1, shortName); + return findConnectorInternal(connectorFetchStmt, conn, shortName); } catch (SQLException ex) { logException(ex, shortName); throw new SqoopException(CommonRepositoryError.COMMON_0001, shortName, ex); } } + private MConnector findConnectorInternal(PreparedStatement stmt, + Connection conn, String connectorIdentifier) throws SQLException { + List<MConnector> connectors = loadConnectors(stmt, conn); + + if (connectors.size() == 0) { + LOG.debug("Looking up connector: " + connectorIdentifier + ", no connector found"); + return null; + } else if (connectors.size() == 1) { + MConnector mc = connectors.get(0); + LOG.debug("Looking up connector: " + connectorIdentifier + ", found: " + mc); + return mc; + } else { + throw new SqoopException(CommonRepositoryError.COMMON_0002, connectorIdentifier); + } + } + /** * {@inheritDoc} */ @@ -277,7 +304,7 @@ public abstract class CommonRepositoryHandler extends JdbcRepositoryHandler { public MDriver findDriver(String shortName, Connection conn) { LOG.debug("Looking up Driver and config "); MDriver mDriver; - try (PreparedStatement driverFetchStmt = conn.prepareStatement(crudQueries.getStmtSelectFromConfigurable()); + try (PreparedStatement driverFetchStmt = conn.prepareStatement(crudQueries.getStmtSelectFromConfigurableByName()); PreparedStatement driverConfigFetchStmt = conn.prepareStatement(crudQueries.getStmtSelectConfigForConfigurable()); PreparedStatement driverConfigInputFetchStmt = conn.prepareStatement(crudQueries.getStmtSelectInput());) { @@ -483,24 +510,11 @@ public abstract class CommonRepositoryHandler extends JdbcRepositoryHandler { */ @Override public MLink findLink(long linkId, Connection conn) { - try (PreparedStatement linkFetchStmt = conn.prepareStatement(crudQueries.getStmtSelectLinkSingle())) { + try (PreparedStatement linkFetchStmt = conn.prepareStatement(crudQueries + .getStmtSelectLinkSingleById())) { linkFetchStmt.setLong(1, linkId); - List<MLink> links = loadLinks(linkFetchStmt, conn); - - if (links.size() == 0) { - LOG.debug("Looking up link with id: " + linkId + ", no link found"); - return null; - } else if (links.size() == 1) { - // Return the first and only one link object with the given id - MLink link = links.get(0); - LOG.debug("Looking up link with id: " + linkId + ", found: " + link); - return link; - } else { - throw new SqoopException(CommonRepositoryError.COMMON_0021, - String.valueOf(linkId)); - } - + return findLinkInternal(linkFetchStmt, conn, String.valueOf(linkId)); } catch (SQLException ex) { logException(ex, linkId); throw new SqoopException(CommonRepositoryError.COMMON_0020, ex); @@ -512,29 +526,35 @@ public abstract class CommonRepositoryHandler extends JdbcRepositoryHandler { */ @Override public MLink findLink(String linkName, Connection conn) { - try (PreparedStatement linkFetchStmt = conn.prepareStatement(crudQueries.getStmtSelectLinkSingleByName())) { + try (PreparedStatement linkFetchStmt = conn.prepareStatement(crudQueries + .getStmtSelectLinkSingleByName())) { linkFetchStmt.setString(1, linkName); - List<MLink> links = loadLinks(linkFetchStmt, conn); - - if (links.size() == 0) { - LOG.debug("Looking up link with name: " + linkName + ", no link found"); - return null; - } else if (links.size() == 1) { - // Return the first and only one link object with the given name - MLink link = links.get(0); - LOG.debug("Looking up link with name: " + linkName + ", found: " + link); - return link; - } else { - throw new SqoopException(CommonRepositoryError.COMMON_0021, linkName); - } - + return findLinkInternal(linkFetchStmt, conn, linkName); } catch (SQLException ex) { logException(ex, linkName); throw new SqoopException(CommonRepositoryError.COMMON_0020, ex); } } + private MLink findLinkInternal(PreparedStatement stmt, Connection conn, + String linkIdentifier) throws SQLException { + List<MLink> links = loadLinks(stmt, conn); + + if (links.size() == 0) { + LOG.debug("Looking up link: " + linkIdentifier + ", no link found"); + return null; + } else if (links.size() == 1) { + // Return the first and only one link object with the given name or id + MLink link = links.get(0); + LOG.debug("Looking up link: " + linkIdentifier + ", found: " + link); + return link; + } else { + throw new SqoopException(CommonRepositoryError.COMMON_0021, + linkIdentifier); + } + } + /** * {@inheritDoc} */ @@ -745,24 +765,11 @@ public abstract class CommonRepositoryHandler extends JdbcRepositoryHandler { */ @Override public MJob findJob(long jobId, Connection conn) { - try (PreparedStatement stmt = conn.prepareStatement(crudQueries.getStmtSelectJobSingleById())) { + try (PreparedStatement stmt = conn.prepareStatement(crudQueries + .getStmtSelectJobSingleById())) { stmt.setLong(1, jobId); - List<MJob> jobs = loadJobs(stmt, conn); - - if (jobs.size() == 0) { - LOG.debug("Looking up job with id: " + jobId + ", no job found"); - return null; - } else if (jobs.size() == 1) { - // Return the first and only one job object with the given id - MJob job = jobs.get(0); - LOG.debug("Looking up job with id: " + jobId + ", found: " + job); - return job; - } else { - throw new SqoopException(CommonRepositoryError.COMMON_0027, - String.valueOf(jobId)); - } - + return findJobInternal(stmt, conn, String.valueOf(jobId)); } catch (SQLException ex) { logException(ex, jobId); throw new SqoopException(CommonRepositoryError.COMMON_0028, ex); @@ -774,29 +781,34 @@ public abstract class CommonRepositoryHandler extends JdbcRepositoryHandler { */ @Override public MJob findJob(String name, Connection conn) { - try (PreparedStatement stmt = conn.prepareStatement(crudQueries.getStmtSelectJobSingleByName())) { + try (PreparedStatement stmt = conn.prepareStatement(crudQueries + .getStmtSelectJobSingleByName())) { stmt.setString(1, name); - List<MJob> jobs = loadJobs(stmt, conn); - - if (jobs.size() == 0) { - LOG.debug("Looking up job with name: " + name + ", no job found"); - return null; - } else if (jobs.size() == 1) { - // Return the first and only one job object with the given id - MJob job = jobs.get(0); - LOG.debug("Looking up job with name: " + name + ", found: " + job); - return job; - } else { - throw new SqoopException(CommonRepositoryError.COMMON_0027, name); - } - + return findJobInternal(stmt, conn, name); } catch (SQLException ex) { logException(ex, name); throw new SqoopException(CommonRepositoryError.COMMON_0028, ex); } } + private MJob findJobInternal(PreparedStatement stmt, Connection conn, + String jobIdentifier) throws SQLException { + List<MJob> jobs = loadJobs(stmt, conn); + + if (jobs.size() == 0) { + LOG.debug("Looking up job: " + jobIdentifier + ", no job found"); + return null; + } else if (jobs.size() == 1) { + // Return the first and only one job object with the given name or id + MJob job = jobs.get(0); + LOG.debug("Looking up job: " + jobIdentifier + ", found: " + job); + return job; + } else { + throw new SqoopException(CommonRepositoryError.COMMON_0027, jobIdentifier); + } + } + /** * {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryInsertUpdateDeleteSelectQuery.java ---------------------------------------------------------------------- diff --git a/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryInsertUpdateDeleteSelectQuery.java b/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryInsertUpdateDeleteSelectQuery.java index 4f9e547..560fd58 100644 --- a/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryInsertUpdateDeleteSelectQuery.java +++ b/repository/repository-common/src/main/java/org/apache/sqoop/repository/common/CommonRepositoryInsertUpdateDeleteSelectQuery.java @@ -36,24 +36,28 @@ public class CommonRepositoryInsertUpdateDeleteSelectQuery { /** * ******CONFIGURABLE TABLE ************** */ - //DML: Get configurable by given name - private static final String STMT_SELECT_FROM_CONFIGURABLE = + //DML: Get all configurables + private static final String STMT_SELECT_FROM_CONFIGURABLE_ALL = "SELECT " + CommonRepoUtils.escapeColumnName(COLUMN_SQC_ID) + ", " + CommonRepoUtils.escapeColumnName(COLUMN_SQC_NAME) + ", " + CommonRepoUtils.escapeColumnName(COLUMN_SQC_CLASS) + ", " + CommonRepoUtils.escapeColumnName(COLUMN_SQC_VERSION) - + " FROM " + CommonRepoUtils.getTableName(SCHEMA_SQOOP, TABLE_SQ_CONFIGURABLE_NAME) + + " FROM " + CommonRepoUtils.getTableName(SCHEMA_SQOOP, TABLE_SQ_CONFIGURABLE_NAME); + + //DML: Get configurable by given name + private static final String STMT_SELECT_FROM_CONFIGURABLE_BY_ID = + STMT_SELECT_FROM_CONFIGURABLE_ALL + + " WHERE " + CommonRepoUtils.escapeColumnName(COLUMN_SQC_ID) + " = ?"; + + //DML: Get configurable by given name + private static final String STMT_SELECT_FROM_CONFIGURABLE_BY_NAME = + STMT_SELECT_FROM_CONFIGURABLE_ALL + " WHERE " + CommonRepoUtils.escapeColumnName(COLUMN_SQC_NAME) + " = ?"; //DML: Get all configurables for a given type private static final String STMT_SELECT_CONFIGURABLE_ALL_FOR_TYPE = - "SELECT " - + CommonRepoUtils.escapeColumnName(COLUMN_SQC_ID) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQC_NAME) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQC_CLASS) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQC_VERSION) - + " FROM " + CommonRepoUtils.getTableName(SCHEMA_SQOOP, TABLE_SQ_CONFIGURABLE_NAME) + STMT_SELECT_FROM_CONFIGURABLE_ALL + " WHERE " + CommonRepoUtils.escapeColumnName(COLUMN_SQC_TYPE) + " = ?"; //DML: Insert into configurable @@ -293,8 +297,8 @@ public class CommonRepositoryInsertUpdateDeleteSelectQuery { "DELETE FROM " + CommonRepoUtils.getTableName(SCHEMA_SQOOP, TABLE_SQ_LINK_NAME) + " WHERE " + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_ID) + " = ?"; - // DML: Select one specific link - private static final String STMT_SELECT_LINK_SINGLE = + // DML: Select all links + private static final String STMT_SELECT_LINK_ALL = "SELECT " + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_ID) + ", " + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_NAME) + ", " @@ -304,36 +308,18 @@ public class CommonRepositoryInsertUpdateDeleteSelectQuery { + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_CREATION_DATE) + ", " + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_UPDATE_USER) + ", " + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_UPDATE_DATE) - + " FROM " + CommonRepoUtils.getTableName(SCHEMA_SQOOP, TABLE_SQ_LINK_NAME) + + " FROM " + CommonRepoUtils.getTableName(SCHEMA_SQOOP, TABLE_SQ_LINK_NAME); + + // DML: Select one specific link by name by id + private static final String STMT_SELECT_LINK_SINGLE_BY_ID = + STMT_SELECT_LINK_ALL + " WHERE " + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_ID) + " = ?"; // DML: Select one specific link by name private static final String STMT_SELECT_LINK_SINGLE_BY_NAME = - "SELECT " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_ID) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_NAME) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_CONFIGURABLE) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_ENABLED) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_CREATION_USER) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_CREATION_DATE) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_UPDATE_USER) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_UPDATE_DATE) - + " FROM " + CommonRepoUtils.getTableName(SCHEMA_SQOOP, TABLE_SQ_LINK_NAME) + STMT_SELECT_LINK_ALL + " WHERE " + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_NAME) + " = ?"; - // DML: Select all links - private static final String STMT_SELECT_LINK_ALL = - "SELECT " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_ID) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_NAME) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_CONFIGURABLE) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_ENABLED) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_CREATION_USER) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_CREATION_DATE) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_UPDATE_USER) + ", " - + CommonRepoUtils.escapeColumnName(COLUMN_SQ_LNK_UPDATE_DATE) - + " FROM " + CommonRepoUtils.getTableName(SCHEMA_SQOOP, TABLE_SQ_LINK_NAME); - // DML: Select all links for a specific connector. private static final String STMT_SELECT_LINK_FOR_CONNECTOR_CONFIGURABLE = "SELECT " @@ -773,8 +759,8 @@ public class CommonRepositoryInsertUpdateDeleteSelectQuery { return STMT_DELETE_LINK; } - public String getStmtSelectLinkSingle() { - return STMT_SELECT_LINK_SINGLE; + public String getStmtSelectLinkSingleById() { + return STMT_SELECT_LINK_SINGLE_BY_ID; } public String getStmtSelectLinkSingleByName() { @@ -949,8 +935,12 @@ public class CommonRepositoryInsertUpdateDeleteSelectQuery { return STMT_SELECT_SQD_NAME_BY_SQD_ID; } - public String getStmtSelectFromConfigurable() { - return STMT_SELECT_FROM_CONFIGURABLE; + public String getStmtSelectFromConfigurableById() { + return STMT_SELECT_FROM_CONFIGURABLE_BY_ID; + } + + public String getStmtSelectFromConfigurableByName() { + return STMT_SELECT_FROM_CONFIGURABLE_BY_NAME; } public String getStmtSelectConfigurableAllForType() { http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectorHandling.java ---------------------------------------------------------------------- diff --git a/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectorHandling.java b/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectorHandling.java index 8864d5d..b92e5e5 100644 --- a/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectorHandling.java +++ b/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectorHandling.java @@ -44,7 +44,22 @@ public class TestConnectorHandling extends DerbyTestCase { } @Test - public void testFindConnector() throws Exception { + public void testFindConnectorById() throws Exception { + // On empty repository, no connectors should be there + assertNull(handler.findConnector(1L, getDerbyDatabaseConnection())); + // Load connector into repository + addConnectorA(); + // Retrieve it + MConnector connector = handler.findConnector(1L, getDerbyDatabaseConnection()); + assertNotNull(connector); + // Get original structure + MConnector original = getConnector(); + // And compare them + assertEquals(original, connector); + } + + @Test + public void testFindConnectorByName() throws Exception { // On empty repository, no connectors should be there assertNull(handler.findConnector("A", getDerbyDatabaseConnection())); // Load connector into repository http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/repository/repository-mysql/src/test/java/org/apache/sqoop/integration/repository/mysql/TestConnectorHandling.java ---------------------------------------------------------------------- diff --git a/repository/repository-mysql/src/test/java/org/apache/sqoop/integration/repository/mysql/TestConnectorHandling.java b/repository/repository-mysql/src/test/java/org/apache/sqoop/integration/repository/mysql/TestConnectorHandling.java index 8e1b3d1..4d30b5e 100644 --- a/repository/repository-mysql/src/test/java/org/apache/sqoop/integration/repository/mysql/TestConnectorHandling.java +++ b/repository/repository-mysql/src/test/java/org/apache/sqoop/integration/repository/mysql/TestConnectorHandling.java @@ -30,7 +30,25 @@ import org.testng.annotations.Test; public class TestConnectorHandling extends MySqlTestCase { @Test - public void testFindConnector() throws Exception { + public void testFindConnectorById() throws Exception { + // On empty repository, no connectors should be there + assertNull(handler.findConnector(1L, provider.getConnection())); + + // Register a single connector + handler.registerConnector( + getConnector("A", "org.apache.sqoop.test.A", "1.0-test", true, true), + provider.getConnection()); + + // Retrieve it and compare with original + MConnector connector = handler.findConnector(1L, provider.getConnection()); + assertNotNull(connector); + assertEquals( + getConnector("A", "org.apache.sqoop.test.A", "1.0-test", true, true), + connector); + } + + @Test + public void testFindConnectorByName() throws Exception { // On empty repository, no connectors should be there assertNull(handler.findConnector("A", provider.getConnection())); http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/repository/repository-postgresql/src/test/java/org/apache/sqoop/integration/repository/postgresql/TestConnectorHandling.java ---------------------------------------------------------------------- diff --git a/repository/repository-postgresql/src/test/java/org/apache/sqoop/integration/repository/postgresql/TestConnectorHandling.java b/repository/repository-postgresql/src/test/java/org/apache/sqoop/integration/repository/postgresql/TestConnectorHandling.java index 2dcab48..9e19526 100644 --- a/repository/repository-postgresql/src/test/java/org/apache/sqoop/integration/repository/postgresql/TestConnectorHandling.java +++ b/repository/repository-postgresql/src/test/java/org/apache/sqoop/integration/repository/postgresql/TestConnectorHandling.java @@ -34,7 +34,25 @@ import static org.testng.Assert.assertNull; public class TestConnectorHandling extends PostgresqlTestCase { @Test - public void testFindConnector() throws Exception { + public void testFindConnectorById() throws Exception { + // On empty repository, no connectors should be there + assertNull(handler.findConnector(1L, provider.getConnection())); + + // Register a single connector + handler.registerConnector( + getConnector("A", "org.apache.sqoop.test.A", "1.0-test", true, true), + provider.getConnection()); + + // Retrieve it and compare with original + MConnector connector = handler.findConnector(1L, provider.getConnection()); + assertNotNull(connector); + assertEquals( + getConnector("A", "org.apache.sqoop.test.A", "1.0-test", true, true), + connector); + } + + @Test + public void testFindConnectorByName() throws Exception { // On empty repository, no connectors should be there assertNull(handler.findConnector("A", provider.getConnection())); http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/server/src/main/java/org/apache/sqoop/handler/HandlerUtils.java ---------------------------------------------------------------------- diff --git a/server/src/main/java/org/apache/sqoop/handler/HandlerUtils.java b/server/src/main/java/org/apache/sqoop/handler/HandlerUtils.java index 5fcde52..69bccf7 100644 --- a/server/src/main/java/org/apache/sqoop/handler/HandlerUtils.java +++ b/server/src/main/java/org/apache/sqoop/handler/HandlerUtils.java @@ -49,6 +49,33 @@ public class HandlerUtils { return jobId; } + public static String getJobNameFromIdentifier(String identifier) { + // support jobName or jobId for the api + // NOTE: jobId is a fallback for older sqoop clients if any, since we want + // to primarily use unique jobNames + Repository repository = RepositoryManager.getInstance().getRepository(); + MJob job = repository.findJob(identifier); + if (job == null) { + long jobId; + try { + jobId = Long.parseLong(identifier); + } catch (NumberFormatException ex) { + // this means name nor Id existed and we want to throw a user friendly + // message than a number format exception + throw new SqoopException(ServerError.SERVER_0005, "Invalid job: " + identifier + + " requested"); + } + + job = repository.findJob(jobId); + if (job == null) { + throw new SqoopException(ServerError.SERVER_0006, "Job: " + identifier + + " doesn't exist"); + } + } + + return job.getName(); + } + public static long getLinkIdFromIdentifier(String identifier) { // support linkName or linkId for the api // NOTE: linkId is a fallback for older sqoop clients if any, since we want @@ -71,6 +98,33 @@ public class HandlerUtils { return linkId; } + public static String getLinkNameFromIdentifier(String identifier) { + // support linkName or linkId for the api + // NOTE: linkId is a fallback for older sqoop clients if any, since we want + // to primarily use unique linkNames + Repository repository = RepositoryManager.getInstance().getRepository(); + MLink link = repository.findLink(identifier); + if (link == null) { + long linkId; + try { + linkId = Long.parseLong(identifier); + } catch (NumberFormatException ex) { + // this means name nor Id existed and we want to throw a user friendly + // message than a number format exception + throw new SqoopException(ServerError.SERVER_0005, "Invalid link: " + identifier + + " requested"); + } + + link = repository.findLink(linkId); + if (link == null) { + throw new SqoopException(ServerError.SERVER_0006, "Link: " + identifier + + " doesn't exist"); + } + } + + return link.getName(); + } + public static long getConnectorIdFromIdentifier(String identifier) { long connectorId; Repository repository = RepositoryManager.getInstance().getRepository(); @@ -90,4 +144,27 @@ public class HandlerUtils { return connectorId; } + public static String getConnectorNameFromIdentifier(String identifier) { + Repository repository = RepositoryManager.getInstance().getRepository(); + MConnector connector = repository.findConnector(identifier); + if (connector == null) { + long connectorId; + try { + connectorId = Long.parseLong(identifier); + } catch (NumberFormatException ex) { + // this means name nor Id existed and we want to throw a user friendly + // message than a number format exception + throw new SqoopException(ServerError.SERVER_0005, "Invalid connector: " + identifier + + " requested"); + } + + connector = repository.findConnector(connectorId); + if (connector == null) { + throw new SqoopException(ServerError.SERVER_0006, "Connector: " + identifier + + " doesn't exist"); + } + } + + return connector.getUniqueName(); + } } http://git-wip-us.apache.org/repos/asf/sqoop/blob/a7c70593/server/src/main/java/org/apache/sqoop/server/common/ServerError.java ---------------------------------------------------------------------- diff --git a/server/src/main/java/org/apache/sqoop/server/common/ServerError.java b/server/src/main/java/org/apache/sqoop/server/common/ServerError.java index c68ab57..57bb5c0 100644 --- a/server/src/main/java/org/apache/sqoop/server/common/ServerError.java +++ b/server/src/main/java/org/apache/sqoop/server/common/ServerError.java @@ -39,6 +39,8 @@ public enum ServerError implements ErrorCode { /** Invalid entity requested */ SERVER_0005("Invalid entity requested"), + /** Entity requested doesn't exist*/ + SERVER_0006("Entity requested doesn't exist"), ; private final String message;
