LCOV - code coverage report
Current view: top level - src/odbc/unittests - cursor6.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 56 60 93.3 %
Date: 2024-03-23 08:24:27 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Test SQLFetchScroll with no bound columns */
       4             : 
       5             : static int bind_all = 0;
       6             : static int normal_fetch = 0;
       7             : static int use_cursors = 1;
       8             : 
       9          96 : static void Test(void)
      10             : {
      11             : #define ROWS 5
      12             :         struct data_t {
      13             :                 SQLINTEGER i;
      14             :                 SQLLEN ind_i;
      15             :                 char c[20];
      16             :                 SQLLEN ind_c;
      17             :         } data[ROWS];
      18             :         SQLUSMALLINT statuses[ROWS];
      19             :         SQLLEN num_row;
      20             : 
      21          96 :         odbc_reset_statement();
      22             : 
      23             :         /* this should not fail or return warnings */
      24          96 :         if (use_cursors) {
      25          48 :                 CHKSetStmtAttr(SQL_ATTR_CONCURRENCY, TDS_INT2PTR(SQL_CONCUR_READ_ONLY), 0, "S");
      26          48 :                 CHKSetStmtAttr(SQL_ATTR_CURSOR_TYPE, TDS_INT2PTR(SQL_CURSOR_STATIC), 0, "S");
      27             :         }
      28          96 :         CHKPrepare(T("SELECT c, i FROM #cursor6_test"), SQL_NTS, "S");
      29          96 :         CHKExecute("S");
      30          96 :         CHKSetStmtAttr(SQL_ATTR_ROW_BIND_TYPE, TDS_INT2PTR(sizeof(data[0])), 0, "S");
      31          96 :         CHKSetStmtAttr(SQL_ATTR_ROW_ARRAY_SIZE, TDS_INT2PTR(ROWS), 0, "S");
      32          96 :         CHKSetStmtAttr(SQL_ATTR_ROW_STATUS_PTR, statuses, 0, "S");
      33          96 :         CHKSetStmtAttr(SQL_ATTR_ROWS_FETCHED_PTR, &num_row, 0, "S");
      34          96 :         if (bind_all)
      35          48 :                 CHKBindCol(1, SQL_C_CHAR, &data[0].c, sizeof(data[0].c), &data[0].ind_c, "S");
      36          96 :         CHKBindCol(2, SQL_C_LONG, &data[0].i, sizeof(data[0].i), &data[0].ind_i, "S");
      37             : 
      38             : #define FILL(s, n) do { \
      39             :         int _n; for (_n = 0; _n < TDS_VECTOR_SIZE(s); ++_n) s[_n] = n; \
      40             : } while(0)
      41          96 :         FILL(statuses, 9876);
      42          96 :         num_row = -3;
      43          96 :         data[0].i = (SQLINTEGER) 0xdeadbeef;
      44          96 :         data[1].i = (SQLINTEGER) 0xdeadbeef;
      45          96 :         if (normal_fetch)
      46          48 :                 CHKFetch("S");
      47             :         else
      48          48 :                 CHKFetchScroll(SQL_FETCH_NEXT, 0, "S");
      49             : 
      50             :         /* now check row numbers */
      51         288 :         printf("num_row %ld statuses[0] %d statuses[1] %d odbc3 %d\n", (long int) num_row,
      52         192 :                 (int) statuses[0], (int) statuses[1], odbc_use_version3);
      53             : 
      54          96 :         if (odbc_use_version3 || !normal_fetch) {
      55          72 :                 if (num_row != ROWS || statuses[0] != SQL_ROW_SUCCESS || statuses[1] != SQL_ROW_SUCCESS) {
      56           0 :                         fprintf(stderr, "result error 1\n");
      57           0 :                         exit(1);
      58             :                 }
      59             :         } else {
      60          24 :                 if (data[0].i != 1 || data[1].i != 0xdeadbeef) {
      61           0 :                         fprintf(stderr, "result error 2\n");
      62           0 :                         exit(1);
      63             :                 }
      64             :         }
      65             : 
      66         480 :         FILL(statuses, 8765);
      67          96 :         num_row = -3;
      68          96 :         if (normal_fetch)
      69          48 :                 CHKFetch("S");
      70             :         else
      71          48 :                 CHKFetchScroll(SQL_FETCH_NEXT, 0, "S");
      72          96 : }
      73             : 
      74          12 : static void Init(void)
      75             : {
      76             :         int i;
      77             :         char sql[128];
      78             : 
      79          12 :         odbc_command("CREATE TABLE #cursor6_test (i INT, c VARCHAR(20))");
      80         132 :         for (i = 1; i <= 10; ++i) {
      81         120 :                 sprintf(sql, "INSERT INTO #cursor6_test(i,c) VALUES(%d, 'a%db%dc%d')", i, i, i, i);
      82         120 :                 odbc_command(sql);
      83             :         }
      84             : 
      85          12 : }
      86             : 
      87             : int
      88           8 : main(void)
      89             : {
      90           8 :         odbc_use_version3 = 1;
      91           8 :         odbc_connect();
      92             : 
      93           8 :         odbc_check_cursor();
      94             : 
      95           6 :         Init();
      96             : 
      97             : #define ALL(n) for (n = 0; n < 2; ++n)
      98          18 :         ALL(use_cursors)
      99          36 :                 ALL(bind_all)
     100          72 :                         ALL(normal_fetch)
     101          48 :                                 Test();
     102             : 
     103           6 :         odbc_disconnect();
     104             : 
     105           6 :         odbc_use_version3 = 0;
     106             : 
     107           6 :         odbc_connect();
     108           6 :         Init();
     109             : 
     110          18 :         ALL(use_cursors)
     111          36 :                 ALL(bind_all)
     112          72 :                         ALL(normal_fetch)
     113          48 :                                 Test();
     114             : 
     115           6 :         odbc_disconnect();
     116             :         
     117             :         return 0;
     118             : }

Generated by: LCOV version 1.13