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

feat: support VIEW in metadata queries #633

Merged
merged 1 commit into from Oct 11, 2021
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
Expand Up @@ -15,16 +15,16 @@
*/

SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME,
CASE WHEN TABLE_SCHEMA = '' THEN 'TABLE' ELSE 'VIEW' END AS TABLE_TYPE,
CASE WHEN TABLE_TYPE = 'BASE TABLE' THEN 'TABLE' ELSE TABLE_TYPE END AS TABLE_TYPE,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures that we continue to report what is called BASE_TABLE in Spanner as TABLE in JDBC to retain backwards compatibility. So this basically means:

  • Return 'TABLE' if Spanner calls it a 'BASE TABLE'
  • Return the TABLE_TYPE in all other cases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When was this renamed?

NULL AS REMARKS, NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME,
NULL AS SELF_REFERENCING_COL_NAME, NULL AS REF_GENERATION
FROM INFORMATION_SCHEMA.TABLES AS T
NULL AS SELF_REFERENCING_COL_NAME, NULL AS REF_GENERATION
FROM INFORMATION_SCHEMA.TABLES AS T
WHERE UPPER(TABLE_CATALOG) LIKE ?
AND UPPER(TABLE_SCHEMA) LIKE ?
AND UPPER(TABLE_NAME) LIKE ?
AND (
(CASE WHEN TABLE_SCHEMA = '' THEN 'TABLE' ELSE 'VIEW' END) LIKE ?
OR
(CASE WHEN TABLE_SCHEMA = '' THEN 'TABLE' ELSE 'VIEW' END) LIKE ?
)
ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME
(CASE WHEN TABLE_TYPE = 'BASE TABLE' THEN 'TABLE' ELSE TABLE_TYPE END) LIKE ?
OR
(CASE WHEN TABLE_TYPE = 'BASE TABLE' THEN 'TABLE' ELSE TABLE_TYPE END) LIKE ?
)
ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME
Expand Up @@ -21,6 +21,10 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;

import com.google.cloud.spanner.ParallelIntegrationTest;
Expand Down Expand Up @@ -801,6 +805,19 @@ public void testGetPrimaryKeys() throws SQLException {
}
}

@Test
public void testGetViews() throws SQLException {
try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getTables("", "", null, new String[] {"VIEW"})) {
assertTrue(rs.next());
assertEquals(DEFAULT_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(DEFAULT_CATALOG, rs.getString("TABLE_CAT"));
assertEquals("SingersView", rs.getString("TABLE_NAME"));
assertFalse(rs.next());
}
}
}

@Test
public void testGetSchemas() throws SQLException {
try (Connection connection = createConnection()) {
Expand All @@ -820,9 +837,15 @@ public void testGetSchemas() throws SQLException {

private static final class Table {
private final String name;
private final String type;

private Table(String name) {
this(name, "TABLE");
}

private Table(String name, String type) {
this.name = name;
this.type = type;
}
}

Expand All @@ -831,6 +854,7 @@ private Table(String name) {
new Table("Albums"),
new Table("Concerts"),
new Table("Singers"),
new Table("SingersView", "VIEW"),
new Table("Songs"),
new Table("TableWithAllColumnTypes"),
new Table("TableWithRef"));
Expand All @@ -841,17 +865,17 @@ public void testGetTables() throws SQLException {
try (ResultSet rs =
connection.getMetaData().getTables(DEFAULT_CATALOG, DEFAULT_SCHEMA, null, null)) {
for (Table table : EXPECTED_TABLES) {
assertThat(rs.next(), is(true));
assertThat(rs.getString("TABLE_CAT"), is(equalTo(DEFAULT_CATALOG)));
assertThat(rs.getString("TABLE_SCHEM"), is(equalTo(DEFAULT_SCHEMA)));
assertThat(rs.getString("TABLE_NAME"), is(equalTo(table.name)));
assertThat(rs.getString("TABLE_TYPE"), is(equalTo("TABLE")));
assertThat(rs.getString("REMARKS"), is(nullValue()));
assertThat(rs.getString("TYPE_CAT"), is(nullValue()));
assertThat(rs.getString("TYPE_SCHEM"), is(nullValue()));
assertThat(rs.getString("TYPE_NAME"), is(nullValue()));
assertThat(rs.getString("SELF_REFERENCING_COL_NAME"), is(nullValue()));
assertThat(rs.getString("REF_GENERATION"), is(nullValue()));
assertTrue(rs.next());
assertEquals(DEFAULT_CATALOG, rs.getString("TABLE_CAT"));
assertEquals(DEFAULT_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(table.name, rs.getString("TABLE_NAME"));
assertEquals(table.type, rs.getString("TABLE_TYPE"));
assertNull(rs.getString("REMARKS"));
assertNull(rs.getString("TYPE_CAT"));
assertNull(rs.getString("TYPE_SCHEM"));
assertNull(rs.getString("TYPE_NAME"));
assertNull(rs.getString("SELF_REFERENCING_COL_NAME"));
assertNull(rs.getString("REF_GENERATION"));
}
assertThat(rs.next(), is(false));
}
Expand Down
Expand Up @@ -26,6 +26,11 @@ CREATE TABLE Singers (

CREATE INDEX SingersByFirstLastName ON Singers(FirstName, LastName);

CREATE VIEW SingersView SQL SECURITY INVOKER AS
SELECT s.SingerId AS SingerId, s.FirstName AS FirstName, s.LastName AS LastName
FROM Singers s
ORDER BY s.LastName, s.FirstName;

CREATE TABLE Albums (
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
Expand Down