LCOV - code coverage report
Current view: top level - src/odbc/unittests - connect2.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 61 66 92.4 %
Date: 2024-04-18 21:21:48 Functions: 6 6 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /*
       4             :  * Test setting current "catalog" before and after connection using
       5             :  * either SQLConnect and SQLDriverConnect
       6             :  */
       7             : 
       8             : static int failed = 0;
       9             : 
      10             : static void init_connect(void);
      11             : 
      12             : static void
      13          32 : init_connect(void)
      14             : {
      15          32 :         CHKAllocEnv(&odbc_env, "S");
      16          32 :         CHKAllocConnect(&odbc_conn, "S");
      17          32 : }
      18             : 
      19             : static void
      20          16 : normal_connect(void)
      21             : {
      22          16 :         CHKConnect(T(odbc_server), SQL_NTS, T(odbc_user), SQL_NTS, T(odbc_password), SQL_NTS, "SI");
      23          16 : }
      24             : 
      25             : static void
      26          16 : driver_connect(const char *conn_str)
      27             : {
      28             :         SQLTCHAR tmp[1024];
      29             :         SQLSMALLINT len;
      30             : 
      31          16 :         CHKDriverConnect(NULL, T(conn_str), SQL_NTS, tmp, TDS_VECTOR_SIZE(tmp), &len, SQL_DRIVER_NOPROMPT, "SI");
      32          16 : }
      33             : 
      34             : static void
      35          48 : check_dbname(const char *dbname)
      36             : {
      37             :         SQLINTEGER len;
      38             :         SQLTCHAR out[512];
      39             :         char sql[1024];
      40             : 
      41          48 :         len = sizeof(out);
      42          48 :         CHKGetConnectAttr(SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER) out, sizeof(out), &len, "SI");
      43             : 
      44          48 :         if (strcmp(C(out), dbname) != 0) {
      45           0 :                 fprintf(stderr, "Current database (%s) is not %s\n", C(out), dbname);
      46           0 :                 failed = 1;
      47             :         }
      48             : 
      49          48 :         sprintf(sql, "IF DB_NAME() <> '%s' SELECT 1", dbname);
      50          48 :         CHKAllocStmt(&odbc_stmt, "S");
      51          48 :         odbc_check_no_row(sql);
      52          48 :         SQLFreeStmt(odbc_stmt, SQL_DROP);
      53          48 :         odbc_stmt = SQL_NULL_HSTMT;
      54          48 : }
      55             : 
      56             : static void
      57          40 : set_dbname(const char *dbname)
      58             : {
      59          40 :         CHKSetConnectAttr(SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER) T(dbname), strlen(dbname)*sizeof(SQLTCHAR), "SI");
      60          40 : }
      61             : 
      62             : int
      63           8 : main(int argc, char *argv[])
      64             : {
      65             :         char tmp[1024*3];
      66             : 
      67           8 :         if (odbc_read_login_info())
      68           0 :                 exit(1);
      69             : 
      70             :         /* try setting db name before connect */
      71           8 :         printf("SQLConnect before 1..\n");
      72           8 :         init_connect();
      73           8 :         set_dbname("master");
      74           8 :         normal_connect();
      75           8 :         check_dbname("master");
      76             : 
      77             :         /* check change after connection */
      78           8 :         printf("SQLConnect after..\n");
      79           8 :         set_dbname("tempdb");
      80           8 :         check_dbname("tempdb");
      81             : 
      82           8 :         printf("SQLConnect after not existing..\n");
      83           8 :         strcpy(tmp, "IDontExist");
      84           8 :         CHKSetConnectAttr(SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER) tmp, strlen(tmp), "E");
      85           8 :         check_dbname("tempdb");
      86             : 
      87           8 :         odbc_disconnect();
      88             : 
      89             :         /* try setting db name before connect */
      90           8 :         printf("SQLConnect before 2..\n");
      91           8 :         init_connect();
      92           8 :         set_dbname("tempdb");
      93           8 :         normal_connect();
      94           8 :         check_dbname("tempdb");
      95           8 :         odbc_disconnect();
      96             : 
      97             :         /* try connect string with using DSN */
      98           8 :         printf("SQLDriverConnect before 1..\n");
      99           8 :         sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;", odbc_server, odbc_user, odbc_password, odbc_database);
     100           8 :         init_connect();
     101           8 :         set_dbname("master");
     102           8 :         driver_connect(tmp);
     103           8 :         check_dbname(odbc_database);
     104           8 :         odbc_disconnect();
     105             : 
     106             :         /* try connect string with using DSN */
     107           8 :         printf("SQLDriverConnect before 2..\n");
     108           8 :         sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;", odbc_server, odbc_user, odbc_password);
     109           8 :         init_connect();
     110           8 :         set_dbname("tempdb");
     111           8 :         driver_connect(tmp);
     112           8 :         check_dbname("tempdb");
     113           8 :         odbc_disconnect();
     114             : 
     115           8 :         if (failed) {
     116           0 :                 printf("Some tests failed\n");
     117           0 :                 return 1;
     118             :         }
     119             : 
     120           8 :         printf("Done.\n");
     121           8 :         return 0;
     122             : }

Generated by: LCOV version 1.13