Skip to content

Commit

Permalink
Merge pull request #40 from CartoDB/39-tables-list-schema
Browse files Browse the repository at this point in the history
Fix schema returned by tables_list
  • Loading branch information
Mario de Frutos authored Sep 27, 2016
2 parents 18399f1 + 8eff573 commit 25e8053
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
21 changes: 16 additions & 5 deletions odbc_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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
Expand All @@ -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) )
Expand Down
6 changes: 3 additions & 3 deletions test/expected/hive_20_query_test.out
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions test/expected/mysql_20_query_test.out
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions test/expected/postgres_20_query_test.out
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions test/expected/sqlserver_20_query_test.out
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 25e8053

Please sign in to comment.