Bug fix: SQLColAttribute returns wrong column name - In some circumstances it was...
authorAdrian Grucza <adrian.grucza@iress.com>
Fri, 23 Jun 2023 01:10:57 +0000 (11:10 +1000)
committerHiroshi Inoue <h-inoue@dream.email.ne.jp>
Fri, 23 Jun 2023 01:56:53 +0000 (10:56 +0900)
results.c
test/expected/multistmt.out
test/src/catalogfunctions-test.c
test/src/common.c
test/src/common.h
test/src/multistmt-test.c

index ec89600f771fee66315451bdeb3c1e570e0e6e4b..546edd229e2d911390f74dee172d1fc3caa827da 100644 (file)
--- 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);
 
index 68a9e9207d5d19b0a93ad0eec7d82077611c245a..781a936acaa15f2bdd389e578eabc65d3217c58d 100644 (file)
@@ -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
index efbcfa4f59ddc563580074518478e146fb9e5d68..8bbae353f3fb34c5daaf26a9fb8c5a996063c463 100644 (file)
@@ -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)
index 076d7ff8218db4d8eb0a388c4721c09014e289c7..656754c3fe61c5184d12cbab6a619c86074d7c07 100644 (file)
@@ -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);
+}
index 4794736d664cfe18a2d4d9e552fb3ebfd57a5e02..1e275c055be15d0f0a4e0339d489eef71a1447ba 100644 (file)
@@ -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);
index 6a4236c5b8686686c22a94af70d3038a44f9b6b6..ac0d062379055ab7a8f05350adf7133ae68428d5 100644 (file)
@@ -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);