LCOV - code coverage report
Current view: top level - src/odbc/unittests - cursor7.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 41 41 100.0 %
Date: 2025-01-18 11:50:39 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Test SQLFetchScroll with a non-unitary rowset, using bottom-up direction */
       4             : 
       5             : static void
       6           6 : Test(void)
       7             : {
       8             :         enum { ROWS=5 };
       9             :         struct data_t {
      10             :                 SQLINTEGER i;
      11             :                 SQLLEN ind_i;
      12             :                 char c[20];
      13             :                 SQLLEN ind_c;
      14             :         } data[ROWS];
      15             :         SQLUSMALLINT statuses[ROWS];
      16             :         SQLULEN num_row;
      17             : 
      18             :         int i;
      19             :         SQLRETURN RetCode;
      20             : 
      21           6 :         odbc_reset_statement();
      22             : 
      23           6 :         CHKSetStmtAttr(SQL_ATTR_CONCURRENCY, TDS_INT2PTR(SQL_CONCUR_READ_ONLY), 0, "S");
      24           6 :         CHKSetStmtAttr(SQL_ATTR_CURSOR_TYPE, TDS_INT2PTR(SQL_CURSOR_STATIC), 0, "S");
      25             : 
      26           6 :         CHKPrepare(T("SELECT c, i FROM #cursor7_test"), SQL_NTS, "S");
      27           6 :         CHKExecute("S");
      28           6 :         CHKSetStmtAttr(SQL_ATTR_ROW_BIND_TYPE, TDS_INT2PTR(sizeof(data[0])), 0, "S");
      29           6 :         CHKSetStmtAttr(SQL_ATTR_ROW_ARRAY_SIZE, TDS_INT2PTR(ROWS), 0, "S");
      30           6 :         CHKSetStmtAttr(SQL_ATTR_ROW_STATUS_PTR, statuses, 0, "S");
      31           6 :         CHKSetStmtAttr(SQL_ATTR_ROWS_FETCHED_PTR, &num_row, 0, "S");
      32             : 
      33           6 :         CHKBindCol(1, SQL_C_CHAR, &data[0].c, sizeof(data[0].c), &data[0].ind_c, "S");
      34           6 :         CHKBindCol(2, SQL_C_LONG, &data[0].i, sizeof(data[0].i), &data[0].ind_i, "S");
      35             : 
      36             :         /* Read records from last to first */
      37           6 :         printf("\n\nReading records from last to first:\n");
      38           6 :         RetCode = CHKFetchScroll(SQL_FETCH_LAST, -ROWS, "SINo");
      39          30 :         while (RetCode != SQL_NO_DATA) {
      40             :                 SQLULEN RowNumber;
      41             : 
      42             :                 /* Print this set of rows */
      43          90 :                 for (i = ROWS - 1; i >= 0; i--) {
      44          90 :                         if (statuses[i] != SQL_ROW_NOROW)
      45          72 :                                 printf("\t %d, %s\n", (int) data[i].i, data[i].c);
      46             :                 }
      47          18 :                 printf("\n");
      48             : 
      49          18 :                 CHKGetStmtAttr(SQL_ROW_NUMBER, (SQLPOINTER)(&RowNumber), sizeof(RowNumber), NULL, "S");
      50          18 :                 printf("---> We are in record No: %u\n", (unsigned int) RowNumber);
      51             : 
      52             :                 /* Read next rowset */
      53          18 :                 if ( (RowNumber>1) && (RowNumber<ROWS) ) {
      54           6 :                         RetCode = CHKFetchScroll(SQL_FETCH_RELATIVE, 1-RowNumber, "SINo"); 
      55          24 :                         for (i=RowNumber-1; i<ROWS; ++i)
      56          18 :                                 statuses[i] = SQL_ROW_NOROW;
      57             :                 } else {
      58          12 :                         RetCode = CHKFetchScroll(SQL_FETCH_RELATIVE, -ROWS, "SINo");
      59             :                 }
      60             :         }
      61           6 : }
      62             : 
      63             : static void
      64           6 : Init(void)
      65             : {
      66             :         int i;
      67             :         char sql[128];
      68             : 
      69           6 :         printf("\n\nCreating table #cursor7_test with 12 records.\n");
      70             : 
      71           6 :         odbc_command("\tCREATE TABLE #cursor7_test (i INT, c VARCHAR(20))");
      72          78 :         for (i = 1; i <= 12; ++i) {
      73          72 :                 sprintf(sql, "\tINSERT INTO #cursor7_test(i,c) VALUES(%d, 'a%db%dc%d')", i, i, i, i);
      74          72 :                 odbc_command(sql);
      75             :         }
      76             : 
      77           6 : }
      78             : 
      79             : int
      80           8 : main(void)
      81             : {
      82           8 :         odbc_use_version3 = 1;
      83           8 :         odbc_connect();
      84             : 
      85           8 :         odbc_check_cursor();
      86             : 
      87           6 :         Init();
      88             : 
      89           6 :         Test();
      90             : 
      91           6 :         odbc_disconnect();
      92             : 
      93             :         return 0;
      94             : }

Generated by: LCOV version 1.13