From f4481b14eea23621344e97d62e2133778ebba396 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Thu, 15 May 2014 16:38:28 +0900 Subject: [PATCH] Add support for foreign tables and matviews in SQLTables Regression tests are added in consequence. --- info.c | 66 +++++++++++++++++++++++++++++++++----- test/expected/catalogfunctions.out | 7 ++++ test/expected/sampletables.out | 8 +++++ test/sql/sampletables.sql | 11 +++++++ 4 files changed, 84 insertions(+), 8 deletions(-) diff --git a/info.c b/info.c index 81bfe6d..958e23d 100644 --- a/info.c +++ b/info.c @@ -1470,6 +1470,8 @@ mylog("adjust output=%s(%d)\n", dest, outlen); #define CSTR_SYS_TABLE "SYSTEM TABLE" #define CSTR_TABLE "TABLE" #define CSTR_VIEW "VIEW" +#define CSTR_FOREIGN_TABLE "FOREIGN TABLE" +#define CSTR_MATVIEW "MATVIEW" CSTR like_op_sp = "like "; CSTR like_op_ext = "like E"; @@ -1546,9 +1548,13 @@ PGAPI_Tables(HSTMT hstmt, int nprefixes; char show_system_tables, show_regular_tables, - show_views; + show_views, + show_matviews, + show_foreign_tables; char regular_table, view, + matview, + foreign_table, systable; int i; SQLSMALLINT internal_asis_type = SQL_C_CHAR, cbSchemaName; @@ -1626,7 +1632,19 @@ retry_public_schema: if (list_cat) strncpy_null(tables_query, "select NULL, NULL, NULL", sizeof(tables_query)); else if (list_table_types) - strncpy_null(tables_query, "select NULL, NULL, relkind from (select 'r' as relkind union select 'v') as a", sizeof(tables_query)); + { + /* + * Query relations depending on what is available: + * - 9.3 and newer versions have materialized views + * - 9.1 and newer versions have foreign tables + */ + strncpy_null(tables_query, + "select NULL, NULL, relkind from (select 'r' as relkind " + "union select 'v' " + "union select 'm' " + "union select 'f') as a", + sizeof(tables_query)); + } else if (list_schemas) { if (conn->schema_support) @@ -1637,10 +1655,15 @@ retry_public_schema: } else if (conn->schema_support) { - /* view is represented by its relkind since 7.1 */ - strcpy(tables_query, "select relname, nspname, relkind" - " from pg_catalog.pg_class c, pg_catalog.pg_namespace n"); - strcat(tables_query, " where relkind in ('r', 'v')"); + /* + * View is represented by its relkind since 7.1, + * Materialized views are added in 9.3, and foreign + * tables in 9.1. + */ + strncpy_null(tables_query, "select relname, nspname, relkind " + "from pg_catalog.pg_class c, pg_catalog.pg_namespace n " + "where relkind in ('r', 'v', 'm', 'f')", + sizeof(tables_query)); } else if (PG_VERSION_GE(conn, 7.1)) { @@ -1698,18 +1721,24 @@ retry_public_schema: show_system_tables = FALSE; show_regular_tables = FALSE; show_views = FALSE; + show_foreign_tables = FALSE; + show_matviews = FALSE; /* TABLE_TYPE */ if (!tableType) { show_regular_tables = TRUE; show_views = TRUE; + show_foreign_tables = TRUE; + show_matviews = TRUE; } #if (ODBCVER >= 0x0300) else if (list_some || stricmp(tableType, SQL_ALL_TABLE_TYPES) == 0) { show_regular_tables = TRUE; show_views = TRUE; + show_foreign_tables = TRUE; + show_matviews = TRUE; } #endif /* ODBCVER */ else @@ -1739,6 +1768,10 @@ retry_public_schema: show_regular_tables = TRUE; else if (strnicmp(typestr, CSTR_VIEW, strlen(CSTR_VIEW)) == 0) show_views = TRUE; + else if (strnicmp(typestr, CSTR_FOREIGN_TABLE, strlen(CSTR_FOREIGN_TABLE)) == 0) + show_foreign_tables = TRUE; + else if (strnicmp(typestr, CSTR_MATVIEW, strlen(CSTR_MATVIEW)) == 0) + show_matviews = TRUE; } } @@ -1902,10 +1935,13 @@ retry_public_schema: else view = (relkind_or_hasrules[0] == '1'); + /* Check for foreign tables and materialized views ... */ + foreign_table = (relkind_or_hasrules[0] == 'f'); + matview = (relkind_or_hasrules[0] == 'm'); + /* It must be a regular table */ regular_table = (!systable && !view); - /* Include the row in the result set if meets all criteria */ /* @@ -1914,6 +1950,8 @@ retry_public_schema: */ if ((systable && show_system_tables) || (view && show_views) || + (foreign_table && show_foreign_tables) || + (matview && show_matviews) || (regular_table && show_regular_tables)) { tuple = QR_AddNew(res); @@ -1943,7 +1981,19 @@ retry_public_schema: else set_tuplefield_string(&tuple[TABLES_TABLE_NAME], table_name); if (list_table_types || !list_some) - set_tuplefield_string(&tuple[TABLES_TABLE_TYPE], systable ? "SYSTEM TABLE" : (view ? "VIEW" : "TABLE")); + { + char buffer[64]; + if (systable) + set_tuplefield_string(&tuple[TABLES_TABLE_TYPE], CSTR_SYS_TABLE); + else if (view) + set_tuplefield_string(&tuple[TABLES_TABLE_TYPE], CSTR_VIEW); + else if (matview) + set_tuplefield_string(&tuple[TABLES_TABLE_TYPE], CSTR_MATVIEW); + else if (foreign_table) + set_tuplefield_string(&tuple[TABLES_TABLE_TYPE], CSTR_FOREIGN_TABLE); + else + set_tuplefield_string(&tuple[TABLES_TABLE_TYPE], CSTR_TABLE); + } else set_tuplefield_null(&tuple[TABLES_TABLE_TYPE]); set_tuplefield_string(&tuple[TABLES_REMARKS], NULL_STRING); diff --git a/test/expected/catalogfunctions.out b/test/expected/catalogfunctions.out index dba7d55..88c3290 100644 --- a/test/expected/catalogfunctions.out +++ b/test/expected/catalogfunctions.out @@ -34,6 +34,8 @@ Result set: contrib_regression public booltab TABLE contrib_regression public byteatab TABLE contrib_regression public intervaltable TABLE +contrib_regression public testforeign FOREIGN TABLE +contrib_regression public testmatview MATVIEW contrib_regression public testtab1 TABLE contrib_regression public testtab_fk TABLE Check for SQLColumns @@ -72,11 +74,16 @@ contrib_regression public byteatab t -3 bytea contrib_regression public intervaltable id 4 int4 contrib_regression public intervaltable iv 12 interval contrib_regression public intervaltable d 12 varchar +contrib_regression public testforeign c1 4 int4 +contrib_regression public testmatview id 4 int4 +contrib_regression public testmatview t 12 varchar contrib_regression public testtab1 id 4 int4 contrib_regression public testtab1 t 12 varchar contrib_regression public testtab1_pkey id 4 int4 contrib_regression public testtab_fk id 4 int4 contrib_regression public testtab_fk t 12 varchar +contrib_regression public testview id 4 int4 +contrib_regression public testview t 12 varchar Check for SQLSpecialColumns Result set metadata: SCOPE: SMALLINT(5) digits: 0, nullable diff --git a/test/expected/sampletables.out b/test/expected/sampletables.out index e4fcb71..7af70b9 100644 --- a/test/expected/sampletables.out +++ b/test/expected/sampletables.out @@ -23,6 +23,14 @@ INSERT INTO booltab VALUES (2, 'yes', true); INSERT INTO booltab VALUES (3, 'true', true); INSERT INTO booltab VALUES (4, 'false', false); INSERT INTO booltab VALUES (5, 'not', false); +-- View +CREATE VIEW testview AS SELECT * FROM testtab1; +-- Materialized view +CREATE MATERIALIZED VIEW testmatview AS SELECT * FROM testtab1; +-- Foreign table +CREATE FOREIGN DATA WRAPPER testfdw; +CREATE SERVER testserver FOREIGN DATA WRAPPER testfdw; +CREATE FOREIGN TABLE testforeign (c1 int) SERVER testserver; -- Procedure for catalog function checks CREATE FUNCTION simple_add(in int, in int, out int) AS $$ SELECT $1 + $2; diff --git a/test/sql/sampletables.sql b/test/sql/sampletables.sql index a917de7..94684f3 100644 --- a/test/sql/sampletables.sql +++ b/test/sql/sampletables.sql @@ -29,6 +29,17 @@ INSERT INTO booltab VALUES (3, 'true', true); INSERT INTO booltab VALUES (4, 'false', false); INSERT INTO booltab VALUES (5, 'not', false); +-- View +CREATE VIEW testview AS SELECT * FROM testtab1; + +-- Materialized view +CREATE MATERIALIZED VIEW testmatview AS SELECT * FROM testtab1; + +-- Foreign table +CREATE FOREIGN DATA WRAPPER testfdw; +CREATE SERVER testserver FOREIGN DATA WRAPPER testfdw; +CREATE FOREIGN TABLE testforeign (c1 int) SERVER testserver; + -- Procedure for catalog function checks CREATE FUNCTION simple_add(in int, in int, out int) AS $$ SELECT $1 + $2; -- 1.9.3