You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/07/20 10:18:31 UTC

[shardingsphere] branch master updated: fix oracle bug which will occur when a database has multi users (#6366)

This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 198fed5  fix oracle bug which will occur when a database has multi users (#6366)
198fed5 is described below

commit 198fed53ee24f46aab3ad72bbe9e9bad005f3f3f
Author: feiweihy <36...@users.noreply.github.com>
AuthorDate: Mon Jul 20 18:18:09 2020 +0800

    fix oracle bug which will occur when a database has multi users (#6366)
    
    * Update JdbcUtil.java
    
    fix oracle bug which will occur when a database has multi users
    
    * Create SchemaMetaDataLoaderTest.java
    
    unittest for "fix oracle bug which will occur when a database has multi users"
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    remove sout
    
    * Delete SchemaMetaDataLoaderTest.java
    
    ci test can not pass for this unit test, remove it
    
    * Update JdbcUtil.java
    
    * Create SchemaMetaDataLoaderTest.java
    
    * Update JdbcUtil.java
    
    * Update JdbcUtil.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update JdbcUtil.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update JdbcUtil.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
    
    * Update SchemaMetaDataLoaderTest.java
---
 .../sql/parser/binder/metadata/util/JdbcUtil.java  |  9 ++-
 .../metadata/table/SchemaMetaDataLoaderTest.java   | 75 ++++++++++++++++++++++
 2 files changed, 83 insertions(+), 1 deletion(-)

diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/util/JdbcUtil.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/util/JdbcUtil.java
index 49419bf..3360465 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/util/JdbcUtil.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/util/JdbcUtil.java
@@ -18,7 +18,9 @@
 package org.apache.shardingsphere.sql.parser.binder.metadata.util;
 
 import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
+import java.util.Optional;
 
 /**
  * JDBC util.
@@ -36,7 +38,12 @@ public class JdbcUtil {
         String result = null;
         try {
             if ("Oracle".equals(databaseType)) {
-                return null;
+                DatabaseMetaData metaData = connection.getMetaData();
+                if (null != metaData) {
+                    return Optional.ofNullable(metaData.getUserName()).map(String::toUpperCase).orElse(null);
+                } else {
+                    return null;
+                }
             }
             result = connection.getSchema();
         } catch (final SQLException ignore) {
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/SchemaMetaDataLoaderTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/SchemaMetaDataLoaderTest.java
new file mode 100644
index 0000000..19522cb
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/SchemaMetaDataLoaderTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.shardingsphere.sql.parser.binder.metadata.table;
+
+import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
+import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaDataLoader;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class SchemaMetaDataLoaderTest {
+    private static final String TEST_CATALOG = "catalog";
+
+    private static final String TABLE_TYPE = "TABLE";
+
+    private static final int MAX_CONNECTION_COUNT = 5;
+
+    private static final String DATABASE_TYPE_ORACLE = "Oracle";
+
+    @Mock
+    private DataSource dataSource;
+
+    @Mock
+    private Connection connection;
+
+    @Mock
+    private DatabaseMetaData databaseMetaData;
+
+    @Mock
+    private ResultSet tableExistResultSet;
+
+    @Before
+    public void setUp() throws SQLException {
+        when(dataSource.getConnection()).thenReturn(connection);
+        when(connection.getCatalog()).thenReturn(TEST_CATALOG);
+        when(connection.getMetaData()).thenReturn(databaseMetaData);
+        when(databaseMetaData.getTables(TEST_CATALOG, null, null, new String[]{TABLE_TYPE})).thenReturn(tableExistResultSet);
+    }
+
+    @Test
+    public void assertLoadAllTableNamesForOracle() throws SQLException {
+        SchemaMetaData schemaMetaData = SchemaMetaDataLoader.load(dataSource, MAX_CONNECTION_COUNT, DATABASE_TYPE_ORACLE);
+        Collection<String> allTableNames = schemaMetaData.getAllTableNames();
+        assertThat(allTableNames.size(), is(0));
+    }
+}