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

Improve error messages #126

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions odbc_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,39 +720,50 @@ odbcGetTableOptions(Oid foreigntableid, odbcFdwOptions *extracted_options)
odbcGetOptions(table->serverid, table->options, extracted_options);
}

#define MAX_ERROR_MSG_LENGTH 512
#define ERROR_MSG_SEP "\n"

static void
check_return(SQLRETURN ret, char *msg, SQLHANDLE handle, SQLSMALLINT type)
{
static char error_msg[MAX_ERROR_MSG_LENGTH+1];
int err_code = ERRCODE_SYSTEM_ERROR;

#ifdef DEBUG
strncpy(error_msg, msg, MAX_ERROR_MSG_LENGTH);

SQLINTEGER i = 0;
SQLINTEGER native;
SQLCHAR state[ 7 ];
SQLCHAR text[256];
SQLSMALLINT len;
SQLRETURN diag_ret;
#ifdef DEBUG
if (SQL_SUCCEEDED(ret))
elog(DEBUG1, "Successful result: %s", msg);
#endif
elog(DEBUG1, "Successful result: %s", error_msg);
#endif

if (!SQL_SUCCEEDED(ret))
{
#ifdef DEBUG
elog(DEBUG1, "Error result (%d): %s", ret, msg);
#ifdef DEBUG
elog(DEBUG1, "Error result (%d): %s", ret, error_msg);
#endif
if (handle)
{
do
{
diag_ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
sizeof(text), &len );
if (SQL_SUCCEEDED(diag_ret))
if (SQL_SUCCEEDED(diag_ret)) {
#ifdef DEBUG
elog(DEBUG1, " %s:%ld:%ld:%s\n", state, (long int) i, (long int) native, text);
#endif
strncat(error_msg, ERROR_MSG_SEP, MAX_ERROR_MSG_LENGTH - strlen(ERROR_MSG_SEP));
strncat(error_msg, text, MAX_ERROR_MSG_LENGTH - strlen(error_msg));
}
}
while( diag_ret == SQL_SUCCESS );
}
#endif
ereport(ERROR, (errcode(err_code), errmsg("%s", msg)));
ereport(ERROR, (errcode(err_code), errmsg("%s", error_msg)));
}
}

Expand Down Expand Up @@ -897,7 +908,7 @@ odbcGetTableSize(odbcFdwOptions* options, unsigned int *size)
elog_debug("Count query: %s", sql_str.data);

ret = SQLExecDirect(stmt, (SQLCHAR *) sql_str.data, SQL_NTS);
check_return(ret, "Executing ODBC query", stmt, SQL_HANDLE_STMT);
check_return(ret, "Executing ODBC query to get table size", stmt, SQL_HANDLE_STMT);
if (SQL_SUCCEEDED(ret))
{
SQLFetch(stmt);
Expand Down Expand Up @@ -1972,7 +1983,7 @@ odbcImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)

/* Retrieve a list of rows */
ret = SQLExecDirect(query_stmt, (SQLCHAR *) options.sql_query, SQL_NTS);
check_return(ret, "Executing ODBC query", query_stmt, SQL_HANDLE_STMT);
check_return(ret, "Executing ODBC query to get schema", query_stmt, SQL_HANDLE_STMT);

SQLNumResultCols(query_stmt, &result_columns);

Expand Down
4 changes: 3 additions & 1 deletion test/expected/postgres_20_query_test.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ SELECT * FROM query_postgres_test_table;
(1 row)

SELECT * FROM existent_table_in_schema_public;
ERROR: Executing ODBC query
ERROR: Executing ODBC query to get table size
ERROR: relation "nonexistent_schema.existent_table_in_schema_public" does not exist;
Error while executing the query
SELECT * FROM test_table_in_schema;
id | data
----+---------
Expand Down