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

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Test cursors */
       4             : 
       5             : int
       6           8 : main(void)
       7             : {
       8             : #define ROWS 3
       9             : #define C_LEN 10
      10             : 
      11             :         SQLUINTEGER n[ROWS];
      12             :         char c[ROWS][C_LEN];
      13             :         SQLLEN c_len[ROWS], n_len[ROWS];
      14             : 
      15             :         SQLUSMALLINT statuses[ROWS];
      16             :         SQLUSMALLINT i;
      17             :         SQLULEN num_row;
      18             :         int i_test;
      19             : 
      20             :         typedef struct _test
      21             :         {
      22             :                 SQLUSMALLINT type;
      23             :                 SQLINTEGER irow;
      24             :                 int start;
      25             :                 int num;
      26             :         } TEST;
      27             : 
      28             :         static const TEST tests[] = {
      29             :                 {SQL_FETCH_NEXT, 0, 1, 3},
      30             :                 {SQL_FETCH_NEXT, 0, 4, 2},
      31             :                 {SQL_FETCH_PRIOR, 0, 1, 3},
      32             :                 {SQL_FETCH_NEXT, 0, 4, 2},
      33             :                 {SQL_FETCH_NEXT, 0, -1, -1},
      34             :                 {SQL_FETCH_FIRST, 0, 1, 3},
      35             :                 {SQL_FETCH_ABSOLUTE, 3, 3, 3},
      36             :                 {SQL_FETCH_RELATIVE, 1, 4, 2},
      37             :                 {SQL_FETCH_LAST, 0, 3, 3}
      38             :         };
      39           8 :         const int num_tests = TDS_VECTOR_SIZE(tests);
      40             : 
      41           8 :         odbc_use_version3 = 1;
      42             : 
      43           8 :         odbc_connect();
      44           8 :         odbc_check_cursor();
      45             : 
      46             :         /* create test table */
      47           6 :         odbc_command("IF OBJECT_ID('tempdb..#test') IS NOT NULL DROP TABLE #test");
      48           6 :         odbc_command("CREATE TABLE #test(i int, c varchar(6))");
      49           6 :         odbc_command("INSERT INTO #test(i, c) VALUES(1, 'a')");
      50           6 :         odbc_command("INSERT INTO #test(i, c) VALUES(2, 'bb')");
      51           6 :         odbc_command("INSERT INTO #test(i, c) VALUES(3, 'ccc')");
      52           6 :         odbc_command("INSERT INTO #test(i, c) VALUES(4, 'dddd')");
      53           6 :         odbc_command("INSERT INTO #test(i, c) VALUES(5, 'eeeee')");
      54             : 
      55             :         /* set cursor options */
      56           6 :         odbc_reset_statement();
      57           6 :         CHKSetStmtAttr(SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_ROWVER, 0, "S");
      58           6 :         CHKSetStmtAttr(SQL_ATTR_CURSOR_SCROLLABLE, (SQLPOINTER) SQL_SCROLLABLE, 0, "S");
      59           6 :         CHKSetStmtAttr(SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_DYNAMIC, 0, "S");
      60           6 :         CHKSetStmtAttr(SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) ROWS, 0, "S");
      61           6 :         CHKSetStmtAttr(SQL_ATTR_ROW_STATUS_PTR, (SQLPOINTER) statuses, 0, "S");
      62           6 :         CHKSetStmtAttr(SQL_ATTR_ROWS_FETCHED_PTR, &num_row, 0, "S");
      63             : 
      64             :         /* */
      65           6 :         CHKExecDirect(T("SELECT i, c FROM #test"), SQL_NTS, "S");
      66             : 
      67             :         /* bind some rows at a time */
      68           6 :         CHKBindCol(1, SQL_C_ULONG, n, 0, n_len, "S");
      69           6 :         CHKBindCol(2, SQL_C_CHAR, c, C_LEN, c_len, "S");
      70             : 
      71          66 :         for (i_test = 0; i_test < num_tests; ++i_test) {
      72          54 :                 const TEST *t = &tests[i_test];
      73             : 
      74          54 :                 printf("Test %d\n", i_test + 1);
      75             : 
      76          54 :                 if (t->start == -1) {
      77           6 :                         CHKFetchScroll(t->type, t->irow, "No");
      78             :                 } else {
      79          48 :                         CHKFetchScroll(t->type, t->irow, "S");
      80             : 
      81          48 :                         if (t->start < 1) {
      82           0 :                                 fprintf(stderr, "Rows not expected\n");
      83           0 :                                 exit(1);
      84             :                         }
      85             : 
      86             :                         /* print, just for debug */
      87         126 :                         for (i = 0; i < num_row; ++i)
      88         126 :                                 printf("row %d i %d c %s\n", (int) (i + 1), (int) n[i], c[i]);
      89          48 :                         printf("---\n");
      90             : 
      91          48 :                         if (num_row != t->num) {
      92           0 :                                 fprintf(stderr, "Expected %d rows, got %d\n", t->num, (int) num_row);
      93           0 :                                 exit(1);
      94             :                         }
      95             : 
      96         126 :                         for (i = 0; i < num_row; ++i) {
      97             :                                 char name[10];
      98             : 
      99         126 :                                 memset(name, 0, sizeof(name));
     100         126 :                                 memset(name, 'a' - 1 + i + t->start, i + t->start);
     101         126 :                                 if (n[i] != i + t->start || c_len[i] != strlen(name) || strcmp(c[i], name) != 0) {
     102           0 :                                         fprintf(stderr, "Wrong row returned\n");
     103           0 :                                         fprintf(stderr, "\tn %d %d\n", (int) n[i], i + t->start);
     104           0 :                                         fprintf(stderr, "\tc len %d %d\n", (int) c_len[i], (int) strlen(name));
     105           0 :                                         fprintf(stderr, "\tc %s %s\n", c[i], name);
     106           0 :                                         exit(1);
     107             :                                 }
     108             :                         }
     109             :                 }
     110             :         }
     111             : 
     112           6 :         odbc_reset_statement();
     113             : 
     114           6 :         odbc_disconnect();
     115             :         return 0;
     116             : }

Generated by: LCOV version 1.13