Skip to content

Commit

Permalink
feat: support VIEW in metadata queries (#633)
Browse files Browse the repository at this point in the history
- Report views with table type VIEW in metadata queries.
- Allow applications to filter for VIEWs in metadata queries

Fixes #632
  • Loading branch information
olavloite committed Oct 11, 2021
1 parent 0bf308a commit b929191
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
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,
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

0 comments on commit b929191

Please sign in to comment.