From e683578a5051e31fe5b891b11327e961e510fece Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Tue, 27 Sep 2016 14:55:03 +0200 Subject: [PATCH 1/2] Fix schema returned by tables_list Fixes #39 --- odbc_fdw.c | 21 ++++++++++++++++----- test/expected/hive_20_query_test.out | 6 +++--- test/expected/mysql_20_query_test.out | 6 +++--- test/expected/postgres_20_query_test.out | 6 +++--- test/expected/sqlserver_20_query_test.out | 6 +++--- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/odbc_fdw.c b/odbc_fdw.c index 16d1705..a37227c 100644 --- a/odbc_fdw.c +++ b/odbc_fdw.c @@ -72,6 +72,17 @@ PG_MODULE_MAGIC; /* Maximum GetData buffer size */ #define MAXIMUM_BUFFER_SIZE 8192 +/* + * Numbers of the columns returned by SQLTables: + * 1: TABLE_CAT (ODBC 3.0) TABLE_QUALIFIER (ODBC 2.0) -- database name + * 2: TABLE_SCHEM (ODBC 3.0) TABLE_OWNER (ODBC 2.0) -- schema name + * 3: TABLE_NAME + * 4: TABLE_TYPE + * 5: REMARKS + */ +#define SQLTABLES_SCHEMA_COLUMN 2 +#define SQLTABLES_NAME_COLUMN 3 + #define ODBC_SQLSTATE_FRACTIONAL_TRUNCATION "01S07" typedef struct odbcFdwOptions { @@ -1013,7 +1024,7 @@ Datum odbc_tables_list(PG_FUNCTION_ARGS) MemoryContext oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); datafctx = (TableDataCtx *) palloc(sizeof(TableDataCtx)); tableResult = (DataBinding*) palloc( numColumns * sizeof(DataBinding) ); - + char *serverName = text_to_cstring(PG_GETARG_TEXT_PP(0)); int serverOid = oid_from_server_name(serverName); @@ -1072,8 +1083,8 @@ Datum odbc_tables_list(PG_FUNCTION_ARGS) values = (char **) palloc(2 * sizeof(char *)); values[0] = (char *) palloc(256 * sizeof(char)); values[1] = (char *) palloc(256 * sizeof(char)); - snprintf(values[0], 256, "%s", (char *)tableResult[0].TargetValuePtr); - snprintf(values[1], 256, "%s", (char *)tableResult[2].TargetValuePtr); + snprintf(values[0], 256, "%s", (char *)tableResult[SQLTABLES_SCHEMA_COLUMN-1].TargetValuePtr); + snprintf(values[1], 256, "%s", (char *)tableResult[SQLTABLES_NAME_COLUMN-1].TargetValuePtr); tuple = BuildTupleFromCStrings(attinmeta, values); result = HeapTupleGetDatum(tuple); currentRow++; @@ -2026,7 +2037,7 @@ odbcImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid) { int excluded = FALSE; TableName = (SQLCHAR *) palloc(sizeof(SQLCHAR) * MAXIMUM_TABLE_NAME_LEN); - ret = SQLGetData(tables_stmt, 3, SQL_C_CHAR, TableName, MAXIMUM_TABLE_NAME_LEN, &indicator); + ret = SQLGetData(tables_stmt, SQLTABLES_NAME_COLUMN, SQL_C_CHAR, TableName, MAXIMUM_TABLE_NAME_LEN, &indicator); check_return(ret, "Reading table name", tables_stmt, SQL_HANDLE_STMT); /* Since we're not filtering the SQLTables call by schema @@ -2036,7 +2047,7 @@ odbcImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid) So we only reject tables for which the schema is not blank and different from the desired schema: */ - ret = SQLGetData(tables_stmt, 2, SQL_C_CHAR, table_schema, MAXIMUM_SCHEMA_NAME_LEN, &indicator); + ret = SQLGetData(tables_stmt, SQLTABLES_SCHEMA_COLUMN, SQL_C_CHAR, table_schema, MAXIMUM_SCHEMA_NAME_LEN, &indicator); if (SQL_SUCCESS == ret) { if (!is_blank_string((char*)table_schema) && strcmp((char*)table_schema, schema_name) ) diff --git a/test/expected/hive_20_query_test.out b/test/expected/hive_20_query_test.out index 7b0a29d..12ae805 100644 --- a/test/expected/hive_20_query_test.out +++ b/test/expected/hive_20_query_test.out @@ -11,9 +11,9 @@ SELECT * FROM query_hive_test_table; (1 row) SELECT * FROM ODBCTablesList('hive_fdw', 1); - schema | name ---------+----------------- - HIVE | hive_test_table + schema | name +---------+----------------- + default | hive_test_table (1 row) SELECT * FROM ODBCTableSize('hive_fdw', 'hive_test_table'); diff --git a/test/expected/mysql_20_query_test.out b/test/expected/mysql_20_query_test.out index 64769cf..e1ac82f 100644 --- a/test/expected/mysql_20_query_test.out +++ b/test/expected/mysql_20_query_test.out @@ -11,9 +11,9 @@ SELECT * FROM query_mysql_test_table; (1 row) SELECT * FROM ODBCTablesList('mysql_fdw', 1); - schema | name ------------+------------------ - fdw_tests | mysql_test_table + schema | name +--------+------------------ + | mysql_test_table (1 row) SELECT * FROM ODBCTableSize('mysql_fdw', 'mysql_test_table'); diff --git a/test/expected/postgres_20_query_test.out b/test/expected/postgres_20_query_test.out index 6245c7d..19264c6 100644 --- a/test/expected/postgres_20_query_test.out +++ b/test/expected/postgres_20_query_test.out @@ -19,9 +19,9 @@ SELECT * FROM test_table_in_schema; (1 row) SELECT * FROM ODBCTablesList('postgres_fdw', 1); - schema | name ------------+--------------------------------- - fdw_tests | existent_table_in_schema_public + schema | name +--------+--------------------------------- + public | existent_table_in_schema_public (1 row) SELECT * FROM ODBCTableSize('postgres_fdw', 'postgres_test_table'); diff --git a/test/expected/sqlserver_20_query_test.out b/test/expected/sqlserver_20_query_test.out index ff3fb9d..3b7bde3 100644 --- a/test/expected/sqlserver_20_query_test.out +++ b/test/expected/sqlserver_20_query_test.out @@ -5,9 +5,9 @@ SELECT * FROM sqlserver_test_table; (1 row) SELECT * FROM ODBCTablesList('sqlserver_fdw', 1); - schema | name ------------+---------------------- - fdw_tests | sqlserver_test_table + schema | name +--------+---------------------- + dbo | sqlserver_test_table (1 row) SELECT * FROM ODBCTableSize('sqlserver_fdw', 'sqlserver_test_table'); From 8eff5735e07f97681b04517b84eb321635fb7df5 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Tue, 27 Sep 2016 15:47:37 +0200 Subject: [PATCH 2/2] Fix tests --- test/expected/hive_20_query_test.out | 2 +- test/expected/mysql_20_query_test.out | 2 +- test/expected/postgres_20_query_test.out | 2 +- test/expected/sqlserver_20_query_test.out | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/expected/hive_20_query_test.out b/test/expected/hive_20_query_test.out index 12ae805..724f5c4 100644 --- a/test/expected/hive_20_query_test.out +++ b/test/expected/hive_20_query_test.out @@ -11,7 +11,7 @@ SELECT * FROM query_hive_test_table; (1 row) SELECT * FROM ODBCTablesList('hive_fdw', 1); - schema | name + schema | name ---------+----------------- default | hive_test_table (1 row) diff --git a/test/expected/mysql_20_query_test.out b/test/expected/mysql_20_query_test.out index e1ac82f..e6837f3 100644 --- a/test/expected/mysql_20_query_test.out +++ b/test/expected/mysql_20_query_test.out @@ -11,7 +11,7 @@ SELECT * FROM query_mysql_test_table; (1 row) SELECT * FROM ODBCTablesList('mysql_fdw', 1); - schema | name + schema | name --------+------------------ | mysql_test_table (1 row) diff --git a/test/expected/postgres_20_query_test.out b/test/expected/postgres_20_query_test.out index 19264c6..b239003 100644 --- a/test/expected/postgres_20_query_test.out +++ b/test/expected/postgres_20_query_test.out @@ -19,7 +19,7 @@ SELECT * FROM test_table_in_schema; (1 row) SELECT * FROM ODBCTablesList('postgres_fdw', 1); - schema | name + schema | name --------+--------------------------------- public | existent_table_in_schema_public (1 row) diff --git a/test/expected/sqlserver_20_query_test.out b/test/expected/sqlserver_20_query_test.out index 3b7bde3..b3016c6 100644 --- a/test/expected/sqlserver_20_query_test.out +++ b/test/expected/sqlserver_20_query_test.out @@ -5,7 +5,7 @@ SELECT * FROM sqlserver_test_table; (1 row) SELECT * FROM ODBCTablesList('sqlserver_fdw', 1); - schema | name + schema | name --------+---------------------- dbo | sqlserver_test_table (1 row)