Repository: ignite Updated Branches: refs/heads/master f9be39185 -> bc8e64567
IGNITE-5409: JDBC thin: added schema to connection URL. This closes #2179. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/bc8e6456 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/bc8e6456 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/bc8e6456 Branch: refs/heads/master Commit: bc8e64567eebfb4bfa30e989ee63ddae65b20693 Parents: f9be391 Author: tledkov-gridgain <[email protected]> Authored: Thu Aug 31 11:03:18 2017 +0300 Committer: devozerov <[email protected]> Committed: Thu Aug 31 11:03:18 2017 +0300 ---------------------------------------------------------------------- .../jdbc/thin/JdbcThinConnectionSelfTest.java | 22 ++++++ .../jdbc/thin/JdbcThinNoDefaultSchemaTest.java | 82 +++++++++++++++++++- .../org/apache/ignite/IgniteJdbcThinDriver.java | 20 +++-- .../internal/jdbc/thin/JdbcThinConnection.java | 11 ++- .../internal/jdbc/thin/JdbcThinUtils.java | 2 +- 5 files changed, 123 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/bc8e6456/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionSelfTest.java index a2e7be1..2306a2b 100644 --- a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionSelfTest.java +++ b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionSelfTest.java @@ -287,6 +287,28 @@ public class JdbcThinConnectionSelfTest extends JdbcThinAbstractSelfTest { } /** + * Test schema property in URL. + * + * @throws Exception If failed. + */ + public void testSchema() throws Exception { + assertInvalid("jdbc:ignite:thin://127.0.0.1/qwe/qwe", + "Invalid URL format (only schema name is allowed in URL path parameter 'host:port[/schemaName]')" ); + + try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/public")) { + assertEquals("Invalid schema", "public", conn.getSchema()); + } + + try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/" + DEFAULT_CACHE_NAME)) { + assertEquals("Invalid schema", DEFAULT_CACHE_NAME, conn.getSchema()); + } + + try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/_not_exist_schema_")) { + assertEquals("Invalid schema", "_not_exist_schema_", conn.getSchema()); + } + } + + /** * Get client socket for connection. * * @param conn Connection. http://git-wip-us.apache.org/repos/asf/ignite/blob/bc8e6456/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinNoDefaultSchemaTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinNoDefaultSchemaTest.java b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinNoDefaultSchemaTest.java index ff49742..7b226e5 100644 --- a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinNoDefaultSchemaTest.java +++ b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinNoDefaultSchemaTest.java @@ -20,7 +20,9 @@ package org.apache.ignite.jdbc.thin; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; +import java.util.concurrent.Callable; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.configuration.CacheConfiguration; @@ -28,6 +30,7 @@ import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; +import org.apache.ignite.testframework.GridTestUtils; import org.jetbrains.annotations.NotNull; /** @@ -119,11 +122,9 @@ public class JdbcThinNoDefaultSchemaTest extends JdbcThinAbstractSelfTest { /** * @throws Exception If failed. */ - public void testNoCacheNameQuery() throws Exception { + public void testSchemaNameInQuery() throws Exception { Connection conn = DriverManager.getConnection(URL); - conn.setSchema("cache1"); - Statement stmt = conn.createStatement(); assertNotNull(stmt); @@ -155,4 +156,79 @@ public class JdbcThinNoDefaultSchemaTest extends JdbcThinAbstractSelfTest { stmt.close(); } + + /** + * @throws Exception If failed. + */ + public void testSchemaInUrl() throws Exception { + try(Connection conn = DriverManager.getConnection(URL + "/cache1")) { + Statement stmt = conn.createStatement(); + + stmt.execute("select t._key, t._val from Integer t"); + + ResultSet rs = stmt.getResultSet(); + + while (rs.next()) + assertEquals(rs.getInt(2), rs.getInt(1) * 2); + } + + try(Connection conn = DriverManager.getConnection(URL + "/cache2")) { + Statement stmt = conn.createStatement(); + + stmt.execute("select t._key, t._val from Integer t"); + + ResultSet rs = stmt.getResultSet(); + + while (rs.next()) + assertEquals(rs.getInt(2), rs.getInt(1) * 3); + } + } + + /** + * @throws Exception If failed. + */ + public void testSchemaInUrlAndInQuery() throws Exception { + try(Connection conn = DriverManager.getConnection(URL + "/cache2")) { + Statement stmt = conn.createStatement(); + + stmt.execute("select t._key, t._val, v._val " + + "from \"cache1\".Integer t join Integer v on t._key = v._key"); + + ResultSet rs = stmt.getResultSet(); + + while (rs.next()) { + assertEquals(rs.getInt(2), rs.getInt(1) * 2); + assertEquals(rs.getInt(3), rs.getInt(1) * 3); + } + } + } + + /** + * @throws Exception If failed. + */ + public void testSetSchema() throws Exception { + try(Connection conn = DriverManager.getConnection(URL)) { + // Try to execute query without set schema + GridTestUtils.assertThrows(log, new Callable<Object>() { + @Override public Object call() throws Exception { + Statement stmt = conn.createStatement(); + + stmt.execute("select t._key, t._val from Integer t"); + + return null; + } + }, SQLException.class, "Failed to query Ignite"); + + conn.setSchema("cache1"); + + Statement stmt = conn.createStatement(); + + stmt.execute("select t._key, t._val from Integer t"); + + ResultSet rs = stmt.getResultSet(); + + while (rs.next()) + assertEquals(rs.getInt(2), rs.getInt(1) * 2); + } + } } http://git-wip-us.apache.org/repos/asf/ignite/blob/bc8e6456/modules/core/src/main/java/org/apache/ignite/IgniteJdbcThinDriver.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteJdbcThinDriver.java b/modules/core/src/main/java/org/apache/ignite/IgniteJdbcThinDriver.java index 7e23d7a..423223c 100644 --- a/modules/core/src/main/java/org/apache/ignite/IgniteJdbcThinDriver.java +++ b/modules/core/src/main/java/org/apache/ignite/IgniteJdbcThinDriver.java @@ -166,9 +166,9 @@ public class IgniteJdbcThinDriver implements Driver { if (!acceptsURL(url)) return null; - parseUrl(url, props); + String schema = parseUrl(url, props); - return new JdbcThinConnection(url, props); + return new JdbcThinConnection(url, props, schema); } /** {@inheritDoc} */ @@ -218,9 +218,10 @@ public class IgniteJdbcThinDriver implements Driver { * * @param props Properties. * @param url URL. + * @return Scheme name. {@code null} in case the schema isn't specified in the url. * @throws SQLException On error. */ - private void parseUrl(String url, Properties props) throws SQLException { + private String parseUrl(String url, Properties props) throws SQLException { if (F.isEmpty(url)) throw new SQLException("URL cannot be null or empty."); @@ -234,10 +235,9 @@ public class IgniteJdbcThinDriver implements Driver { if (nakedUrlParts.length > 2) throw new SQLException("Invalid URL format (only one ? character is allowed): " + url); - String endpoint = nakedUrlParts[0]; + String[] pathParts = nakedUrlParts[0].split("/"); - if (endpoint.endsWith("/")) - endpoint = endpoint.substring(0, endpoint.length() - 1); + String endpoint = pathParts[0]; String[] endpointParts = endpoint.split(":"); @@ -251,6 +251,14 @@ public class IgniteJdbcThinDriver implements Driver { if (nakedUrlParts.length == 2) parseParameters(nakedUrlParts[1], props); + + if (pathParts.length > 2) { + throw new SQLException("Invalid URL format (only schema name is allowed in URL path parameter " + + "'host:port[/schemaName]'): " + url); + } + + // Gets schema from URL string & returns. + return pathParts.length == 2 ? pathParts[1] : null; } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/bc8e6456/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinConnection.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinConnection.java b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinConnection.java index 1ed0844..370cfa4 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinConnection.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinConnection.java @@ -69,7 +69,7 @@ public class JdbcThinConnection implements Connection { private String url; /** Schema name. */ - private String schemaName; + private String schema; /** Closed flag. */ private boolean closed; @@ -100,9 +100,10 @@ public class JdbcThinConnection implements Connection { * * @param url Connection URL. * @param props Additional properties. + * @param schema Schema name. * @throws SQLException In case Ignite client failed to start. */ - public JdbcThinConnection(String url, Properties props) throws SQLException { + public JdbcThinConnection(String url, Properties props, String schema) throws SQLException { assert url != null; assert props != null; @@ -112,6 +113,8 @@ public class JdbcThinConnection implements Connection { autoCommit = true; txIsolation = Connection.TRANSACTION_NONE; + this.schema = schema; + String host = extractHost(props); int port = extractPort(props); @@ -522,12 +525,12 @@ public class JdbcThinConnection implements Connection { /** {@inheritDoc} */ @Override public void setSchema(String schema) throws SQLException { - schemaName = schema; + this.schema = schema; } /** {@inheritDoc} */ @Override public String getSchema() throws SQLException { - return schemaName; + return schema; } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/ignite/blob/bc8e6456/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinUtils.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinUtils.java index 675cf2d..bb6eb14 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinUtils.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinUtils.java @@ -46,7 +46,7 @@ public class JdbcThinUtils { public static final String URL_PREFIX = "jdbc:ignite:thin://"; /** Prefix for property names. */ - public static final String PROP_PREFIX = "ignite.jdbc"; + public static final String PROP_PREFIX = "ignite.jdbc."; /** Port number property name. */ public static final String PROP_PORT = PROP_PREFIX + "port";
