You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2018/10/12 12:14:12 UTC
ignite git commit: IGNITE-9606: JDBC: fixed primary key column name
resolution in metadata. This closes #4906. This closes #4948.
Repository: ignite
Updated Branches:
refs/heads/master 52991f066 -> ae7b9900e
IGNITE-9606: JDBC: fixed primary key column name resolution in metadata. This closes #4906. This closes #4948.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/ae7b9900
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/ae7b9900
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/ae7b9900
Branch: refs/heads/master
Commit: ae7b9900e5597b0fd77bfa2ccfba2c6bf9bd5632
Parents: 52991f0
Author: pkonstantinov <pk...@gridgain.com>
Authored: Fri Oct 12 15:14:04 2018 +0300
Committer: devozerov <vo...@gridgain.com>
Committed: Fri Oct 12 15:14:04 2018 +0300
----------------------------------------------------------------------
.../jdbc/suite/IgniteJdbcDriverTestSuite.java | 2 +
.../JdbcThinMetadataPrimaryKeysSelfTest.java | 152 +++++++++++++++++++
.../jdbc/thin/JdbcThinMetadataSelfTest.java | 2 +-
.../odbc/jdbc/JdbcRequestHandler.java | 5 +-
4 files changed, 159 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ignite/blob/ae7b9900/modules/clients/src/test/java/org/apache/ignite/jdbc/suite/IgniteJdbcDriverTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/jdbc/suite/IgniteJdbcDriverTestSuite.java b/modules/clients/src/test/java/org/apache/ignite/jdbc/suite/IgniteJdbcDriverTestSuite.java
index 2e98d68..747b13e 100644
--- a/modules/clients/src/test/java/org/apache/ignite/jdbc/suite/IgniteJdbcDriverTestSuite.java
+++ b/modules/clients/src/test/java/org/apache/ignite/jdbc/suite/IgniteJdbcDriverTestSuite.java
@@ -66,6 +66,7 @@ import org.apache.ignite.jdbc.thin.JdbcThinInsertStatementSkipReducerOnUpdateSel
import org.apache.ignite.jdbc.thin.JdbcThinLocalQueriesSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinMergeStatementSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinMergeStatementSkipReducerOnUpdateSelfTest;
+import org.apache.ignite.jdbc.thin.JdbcThinMetadataPrimaryKeysSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinMetadataSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinMissingLongArrayResultsTest;
import org.apache.ignite.jdbc.thin.JdbcThinNoDefaultSchemaTest;
@@ -166,6 +167,7 @@ public class IgniteJdbcDriverTestSuite extends TestSuite {
suite.addTest(new TestSuite(JdbcThinSchemaCaseTest.class));
suite.addTest(new TestSuite(JdbcThinEmptyCacheSelfTest.class));
suite.addTest(new TestSuite(JdbcThinMetadataSelfTest.class));
+ suite.addTest(new TestSuite(JdbcThinMetadataPrimaryKeysSelfTest.class));
suite.addTest(new TestSuite(JdbcThinErrorsSelfTest.class));
suite.addTest(new TestSuite(JdbcThinInsertStatementSelfTest.class));
http://git-wip-us.apache.org/repos/asf/ignite/blob/ae7b9900/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataPrimaryKeysSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataPrimaryKeysSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataPrimaryKeysSelfTest.java
new file mode 100644
index 0000000..5733b72
--- /dev/null
+++ b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataPrimaryKeysSelfTest.java
@@ -0,0 +1,152 @@
+/*
+ * 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.ignite.jdbc.thin;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+
+/**
+ * Verifies that primary keys in the metadata are valid.
+ */
+public class JdbcThinMetadataPrimaryKeysSelfTest extends GridCommonAbstractTest {
+ /** Url. */
+ private static final String URL = "jdbc:ignite:thin://127.0.0.1";
+
+ /** COLUMN_NAME column index in the metadata table. */
+ private static final int COL_NAME_IDX = 4;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTestsStarted() throws Exception {
+ super.beforeTestsStarted();
+
+ startGrid(1);
+ }
+
+ /**
+ * Execute update sql operation using new connection.
+ *
+ * @param sql update SQL query.
+ * @return update count.
+ * @throws SQLException on error.
+ */
+ private int executeUpdate(String sql) throws SQLException {
+ try (Connection conn = DriverManager.getConnection(URL)) {
+ try (PreparedStatement stmt = conn.prepareStatement(sql)) {
+ return stmt.executeUpdate();
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ executeUpdate("DROP TABLE IF EXISTS TEST;");
+ }
+
+ /**
+ * Checks for PK that contains single unwrapped field.
+ */
+ public void testSingleUnwrappedKey() throws Exception {
+ executeUpdate("CREATE TABLE TEST (ID LONG PRIMARY KEY, NAME VARCHAR);");
+
+ checkPKFields("TEST", "ID");
+ }
+
+ /**
+ * Checks for PK that contains single field. Key is forcibly wrapped.
+ */
+ public void testSingleWrappedKey() throws Exception {
+ executeUpdate("CREATE TABLE TEST (" +
+ "ID LONG PRIMARY KEY, " +
+ "NAME VARCHAR) " +
+ "WITH \"wrap_key=true\";");
+
+ checkPKFields("TEST", "ID");
+ }
+
+ /**
+ * Checks for composite (so implicitly wrapped) primary key.
+ */
+ public void testCompositeKey() throws Exception {
+ executeUpdate("CREATE TABLE TEST (" +
+ "ID LONG, " +
+ "SEC_ID LONG, " +
+ "NAME VARCHAR, " +
+ "PRIMARY KEY (ID, SEC_ID));");
+
+ checkPKFields("TEST", "ID", "SEC_ID");
+ }
+
+ /**
+ * Checks for composite (so implicitly wrapped) primary key. Additionally, affinity key is used.
+ */
+ public void testCompositeKeyWithAK() throws Exception {
+ final String tpl = "CREATE TABLE TEST (" +
+ "ID LONG, " +
+ "SEC_ID LONG, " +
+ "NAME VARCHAR, " +
+ "PRIMARY KEY (ID, SEC_ID)) " +
+ "WITH \"affinity_key=%s\";";
+
+ executeUpdate(String.format(tpl, "ID"));
+
+ checkPKFields("TEST", "ID", "SEC_ID");
+
+ executeUpdate("DROP TABLE TEST;");
+
+ executeUpdate(String.format(tpl, "SEC_ID"));
+
+ checkPKFields("TEST", "ID", "SEC_ID");
+ }
+
+ /**
+ * Checks that field names in the metadata matches specified expected fields.
+ *
+ * @param tabName part of the sql query after CREATE TABLE TESTER.
+ * @param expPKFields Expected primary key fields.
+ */
+ private void checkPKFields(String tabName, String... expPKFields) throws Exception {
+ try (Connection conn = DriverManager.getConnection(URL)) {
+ DatabaseMetaData md = conn.getMetaData();
+
+ ResultSet rs = md.getPrimaryKeys(conn.getCatalog(), "", tabName);
+
+ List<String> colNames = new ArrayList<>();
+
+ while (rs.next())
+ colNames.add(rs.getString(COL_NAME_IDX));
+
+ assertEquals("Field names in the primary key are not correct",
+ Arrays.asList(expPKFields), colNames);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTestsStopped() throws Exception {
+ stopAllGrids();
+
+ super.afterTestsStopped();
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/ae7b9900/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java
index 59382f1..35832b7 100644
--- a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java
+++ b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java
@@ -555,7 +555,7 @@ public class JdbcThinMetadataSelfTest extends JdbcThinAbstractSelfTest {
"PUBLIC.TEST.PK_PUBLIC_TEST.ID",
"PUBLIC.TEST.PK_PUBLIC_TEST.NAME",
"PUBLIC.Quoted.PK_PUBLIC_Quoted.Id",
- "PUBLIC.TEST_DECIMAL_COLUMN.ID._KEY"));
+ "PUBLIC.TEST_DECIMAL_COLUMN.ID.ID"));
Set<String> actualPks = new HashSet<>(expectedPks.size());
http://git-wip-us.apache.org/repos/asf/ignite/blob/ae7b9900/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java
index d59788c..d5a277e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java
@@ -1013,8 +1013,11 @@ public class JdbcRequestHandler implements ClientListenerRequestHandler {
table.keyFieldName();
if (fields.isEmpty()) {
+ String keyColName =
+ table.keyFieldName() == null ? QueryUtils.KEY_FIELD_NAME : table.keyFieldName();
+
meta.add(new JdbcPrimaryKeyMeta(table.schemaName(), table.tableName(), keyName,
- Collections.singletonList("_KEY")));
+ Collections.singletonList(keyColName)));
}
else
meta.add(new JdbcPrimaryKeyMeta(table.schemaName(), table.tableName(), keyName, fields));