From: Adrian Grucza Date: Fri, 23 Jun 2023 01:10:57 +0000 (+1000) Subject: Bug fix: SQLColAttribute returns wrong column name - In some circumstances it was... X-Git-Tag: REL-15_00_0000~2 X-Git-Url: http://git.postgresql.org/gitweb/?a=commitdiff_plain;h=f03d56ea6876885d4033fa0d19d90b469f16543e;p=psqlodbc.git Bug fix: SQLColAttribute returns wrong column name - In some circumstances it was returning a name from the previous result --- diff --git a/results.c b/results.c index ec89600..546edd2 100644 --- a/results.c +++ b/results.c @@ -630,6 +630,10 @@ MYLOG(DETAIL_LOG_LEVEL, "answering bookmark info\n"); ti = fi->ti; field_type = getEffectiveOid(conn, fi); } + else + { + fi = NULL; + } MYLOG(0, "col %d field_type=%d fi,ti=%p,%p\n", col_idx, field_type, fi, ti); diff --git a/test/expected/multistmt.out b/test/expected/multistmt.out index 68a9e92..781a936 100644 --- a/test/expected/multistmt.out +++ b/test/expected/multistmt.out @@ -1,23 +1,86 @@ connected ---1 Result set: +--1 +Result set metadata: +?column?: INTEGER(10) digits: 0, nullable +Result set: +?column? 1 ---2 Result set: +--2 +Result set metadata: +?column?: INTEGER(10) digits: 0, nullable +Result set: +?column? 2 ---1 Result set: +--1 +Result set metadata: +?column?: INTEGER(10) digits: 0, nullable +Result set: +?column? 1 ---2 Result set: +--2 +Result set metadata: +?column?: LONGVARCHAR(8190) digits: 0, nullable +?column?: LONGVARCHAR(8190) digits: 0, nullable +Result set: +?column? ?column? foo bar ---3 Result set: +--3 +Result set metadata: +?column?: INTEGER(10) digits: 0, nullable +Result set: +?column? 3 ---4 Result set: +--4 +Result set metadata: +?column?: INTEGER(10) digits: 0, nullable +Result set: +?column? 4 ---1 Result set: +--1 +Result set metadata: +?column?: LONGVARCHAR(8190) digits: 0, nullable +?column?: LONGVARCHAR(8190) digits: 0, nullable +Result set: +?column? ?column? foo bar ---2 Result set: +--2 +Result set metadata: +?column?: LONGVARCHAR(8190) digits: 0, nullable +Result set: +?column? foobar +--1 +Result set metadata: +id: INTEGER(10) digits: 0, not nullable +t: VARCHAR(20) digits: 0, nullable +Result set: +id t +1 foo +2 bar +3 foobar +--2 +Result set metadata: +t: VARCHAR(20) digits: 0, nullable +result: INTEGER(10) digits: 0, nullable +Result set: +t result +foo 2 +bar 2 +foobar 2 # of result cols: 3 ---1 Result set: +--1 +Result set metadata: +?column?: LONGVARCHAR(8190) digits: 0, nullable +id: INTEGER(10) digits: 0, not nullable +t: VARCHAR(20) digits: 0, nullable +Result set: +?column? id t first result set 1 foo ---2 Result set: +--2 +Result set metadata: +?column?: LONGVARCHAR(8190) digits: 0, nullable +t: VARCHAR(20) digits: 0, nullable +Result set: +?column? t second result set bar disconnecting diff --git a/test/src/catalogfunctions-test.c b/test/src/catalogfunctions-test.c index efbcfa4..8bbae35 100644 --- a/test/src/catalogfunctions-test.c +++ b/test/src/catalogfunctions-test.c @@ -21,7 +21,7 @@ #include "common.h" /* define a macro to simplify function calls */ -#define PRINT_RESULT_SERIES(hstmt, idarray, rowcount) print_result_series(hstmt, idarray, sizeof(idarray)/sizeof(idarray[0]), rowcount) +#define PRINT_RESULT_SERIES(hstmt, idarray, rowcount) print_result_series(hstmt, idarray, sizeof(idarray)/sizeof(idarray[0]), rowcount, FALSE) int main(int argc, char **argv) diff --git a/test/src/common.c b/test/src/common.c index 076d7ff..656754c 100644 --- a/test/src/common.c +++ b/test/src/common.c @@ -297,12 +297,31 @@ invalidate_buf(char *buf, size_t len) * Print result only for the selected columns. */ void -print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols, SQLINTEGER rowcount) +print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols, SQLINTEGER rowcount, BOOL printcolnames) { SQLRETURN rc; SQLINTEGER rowc = 0; + char buf[40]; + int i; printf("Result set:\n"); + + if (printcolnames) + { + for (i = 1; i <= numcols; i++) + { + invalidate_buf(buf, sizeof(buf)); + rc = SQLColAttribute(hstmt, i, SQL_DESC_LABEL, buf, sizeof(buf), NULL, NULL); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("SQLColAttribute failed", SQL_HANDLE_STMT, hstmt); + return; + } + printf("%s%s", (i > 1) ? "\t" : "", buf); + } + printf("\n"); + } + while (rowcount <0 || rowc < rowcount) { rc = SQLFetch(hstmt); @@ -310,8 +329,6 @@ print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols, SQLIN break; if (rc == SQL_SUCCESS) { - char buf[40]; - int i; SQLLEN ind; rowc++; @@ -348,7 +365,7 @@ print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols, SQLIN * Print result on all the columns */ void -print_result(HSTMT hstmt) +print_result_all(HSTMT hstmt, BOOL printcolnames) { SQLRETURN rc; SQLSMALLINT numcols, i; @@ -364,6 +381,24 @@ print_result(HSTMT hstmt) colids = (SQLSMALLINT *) malloc(numcols * sizeof(SQLSMALLINT)); for (i = 0; i < numcols; i++) colids[i] = i + 1; - print_result_series(hstmt, colids, numcols, -1); + print_result_series(hstmt, colids, numcols, -1, printcolnames); free(colids); } + +/* + * Print result on all the columns (without column names) + */ +void +print_result(HSTMT hstmt) +{ + print_result_all(hstmt, FALSE); +} + +/* + * Print result on all the columns (with column names) + */ +void +print_result_with_column_names(HSTMT hstmt) +{ + print_result_all(hstmt, TRUE); +} diff --git a/test/src/common.h b/test/src/common.h index 4794736..1e275c0 100644 --- a/test/src/common.h +++ b/test/src/common.h @@ -44,8 +44,10 @@ extern void print_result_meta_series(HSTMT hstmt, extern void print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols, - SQLINTEGER rowcount); + SQLINTEGER rowcount, + BOOL printcolnames); extern void print_result_meta(HSTMT hstmt); extern void print_result(HSTMT hstmt); +extern void print_result_with_column_names(HSTMT hstmt); extern const char *datatype_str(SQLSMALLINT datatype); extern const char *nullable_str(SQLSMALLINT nullable); diff --git a/test/src/multistmt-test.c b/test/src/multistmt-test.c index 6a4236c..ac0d062 100644 --- a/test/src/multistmt-test.c +++ b/test/src/multistmt-test.c @@ -10,8 +10,10 @@ static void print_all_results(HSTMT hstmt) int rc = SQL_SUCCESS; for (i = 1; rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO; i++) { - printf("--%d ", i); - print_result(hstmt); + /*** Verify column metadata/name and row data in each result ***/ + printf("--%d\n", i); + print_result_meta(hstmt); + print_result_with_column_names(hstmt); rc = SQLMoreResults(hstmt); } @@ -65,6 +67,15 @@ int main(int argc, char **argv) rc = SQLFreeStmt(hstmt, SQL_CLOSE); CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); + /*** Different column name, type and source between results for the same column index ***/ + + rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT id, t FROM testtab1; SELECT t, 2 result FROM testtab1", SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + print_all_results(hstmt); + + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); + /*** Prepare/Execute a multi-statement with parameters ***/ rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT 'first result set', id, t FROM testtab1 WHERE t = ?; SELECT 'second result set', t FROM testtab1 WHERE t = ?", SQL_NTS);