LCOV - code coverage report
Current view: top level - src/odbc/unittests - mars1.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 63 66 95.5 %
Date: 2024-03-23 08:24:27 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* first MARS test, test 2 concurrent recordset */
       4             : 
       5             : #define SET_STMT(n) do { \
       6             :         if (pcur_stmt != &n) { \
       7             :                 if (pcur_stmt) *pcur_stmt = odbc_stmt; \
       8             :                 pcur_stmt = &n; \
       9             :                 odbc_stmt = *pcur_stmt; \
      10             :         } \
      11             : } while(0)
      12             : 
      13             : static void
      14           1 : AutoCommit(int onoff)
      15             : {
      16           1 :         CHKSetConnectAttr(SQL_ATTR_AUTOCOMMIT, TDS_INT2PTR(onoff), 0, "S");
      17           1 : }
      18             : 
      19             : static void
      20           1 : EndTransaction(SQLSMALLINT type)
      21             : {
      22           1 :         CHKEndTran(SQL_HANDLE_DBC, odbc_conn, type, "S");
      23           1 : }
      24             : 
      25             : 
      26             : static void
      27           8 : my_attrs(void)
      28             : {
      29           8 :         SQLSetConnectAttr(odbc_conn, 1224 /*SQL_COPT_SS_MARS_ENABLED*/, (SQLPOINTER) 1 /*SQL_MARS_ENABLED_YES*/, SQL_IS_UINTEGER);
      30           8 : }
      31             : 
      32             : int
      33           8 : main(void)
      34             : {
      35             :         SQLINTEGER len, out;
      36             :         int i;
      37             :         SQLHSTMT stmt1, stmt2;
      38           8 :         SQLHSTMT *pcur_stmt = NULL;
      39             : 
      40           8 :         odbc_use_version3 = 1;
      41           8 :         odbc_set_conn_attr = my_attrs;
      42           8 :         odbc_connect();
      43             : 
      44           8 :         stmt1 = odbc_stmt;
      45             : 
      46           8 :         out = 0;
      47           8 :         len = sizeof(out);
      48           8 :         CHKGetConnectAttr(1224, (SQLPOINTER) &out, sizeof(out), &len, "SE");
      49             : 
      50             :         /* test we really support MARS on this connection */
      51             :         /* TODO should out be correct ?? */
      52           8 :         printf("Following row can contain an error due to MARS detection (is expected)\n");
      53           8 :         if (!out || odbc_command2("BEGIN TRANSACTION", "SNoE") != SQL_ERROR) {
      54           7 :                 printf("MARS not supported for this connection\n");
      55           7 :                 odbc_disconnect();
      56           7 :                 odbc_test_skipped();
      57           0 :                 return 0;
      58             :         }
      59           1 :         odbc_read_error();
      60           1 :         if (!strstr(odbc_err, "MARS")) {
      61           0 :                 fprintf(stderr, "Error message invalid \"%s\"\n", odbc_err);
      62           0 :                 return 1;
      63             :         }
      64             : 
      65             :         /* create a test table with some data */
      66           1 :         odbc_command("create table #mars1 (n int, v varchar(100))");
      67          61 :         for (i = 0; i < 60; ++i) {
      68             :                 char cmd[120], buf[80];
      69          60 :                 memset(buf, 'a' + (i % 26), sizeof(buf));
      70          60 :                 buf[i * 7 % 73] = 0;
      71          60 :                 sprintf(cmd, "insert into #mars1 values(%d, '%s')", i, buf);
      72          60 :                 odbc_command(cmd);
      73             :         }
      74             : 
      75             :         /* and another to avid locking problems */
      76           1 :         odbc_command("create table #mars2 (n int, v varchar(100))");
      77             : 
      78           1 :         AutoCommit(SQL_AUTOCOMMIT_OFF);
      79             : 
      80             :         /* try to do a select which return a lot of data (to test server didn't cache everything) */
      81           1 :         odbc_command("select a.n, b.n, a.v from #mars1 a, #mars1 b order by a.n, b.n");
      82           1 :         CHKFetch("S");
      83             : 
      84             :         /* try to do other commands */
      85           1 :         CHKAllocStmt(&stmt2, "S");
      86           1 :         SET_STMT(stmt2);
      87           1 :         odbc_command("insert into #mars2 values(1, 'foo')");
      88           1 :         SET_STMT(stmt1);
      89             : 
      90           1 :         CHKFetch("S");
      91             : 
      92             :         /* reset statements */
      93           1 :         odbc_reset_statement();
      94           1 :         SET_STMT(stmt2);
      95           1 :         odbc_reset_statement();
      96             : 
      97             :         /* now to 2 select with prepare/execute */
      98           1 :         CHKPrepare(T("select a.n, b.n, a.v from #mars1 a, #mars1 b order by a.n, b.n"), SQL_NTS, "S");
      99           1 :         SET_STMT(stmt1);
     100           1 :         CHKPrepare(T("select a.n, b.n, a.v from #mars1 a, #mars1 b order by a.n desc, b.n"), SQL_NTS, "S");
     101           1 :         SET_STMT(stmt2);
     102           1 :         CHKExecute("S");
     103           1 :         SET_STMT(stmt1);
     104           1 :         CHKExecute("S");
     105           1 :         SET_STMT(stmt2);
     106           1 :         CHKFetch("S");
     107           1 :         SET_STMT(stmt1);
     108           1 :         CHKFetch("S");
     109           1 :         SET_STMT(stmt2);
     110           1 :         CHKFetch("S");
     111           1 :         odbc_reset_statement();
     112           1 :         SET_STMT(stmt1);
     113           1 :         CHKFetch("S");
     114           1 :         odbc_reset_statement();
     115             : 
     116           1 :         EndTransaction(SQL_COMMIT);
     117             : 
     118             :         /* TODO test receiving large data should not take much memory */
     119             : 
     120           1 :         odbc_disconnect();
     121           1 :         return 0;
     122             : }
     123             : 

Generated by: LCOV version 1.13