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

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Test for data format returned from SQLPrepare */
       4             : 
       5             : static void
       6          16 : Test(int use_ird)
       7             : {
       8             :         SQLSMALLINT count, namelen, type, digits, nullable;
       9             :         SQLULEN size;
      10             :         SQLHDESC desc;
      11             :         SQLINTEGER ind;
      12             :         SQLTCHAR name[128];
      13          16 :         char *cname = NULL;
      14             : 
      15             :         /* test query returns column information */
      16          16 :         CHKPrepare(T("select * from #odbctestdata select * from #odbctestdata"), SQL_NTS, "S");
      17             : 
      18          16 :         SQLNumParams(odbc_stmt, &count);
      19          16 :         if (count != 0) {
      20           0 :                 fprintf(stderr, "Wrong number of params returned. Got %d expected 0\n", (int) count);
      21           0 :                 exit(1);
      22             :         }
      23             : 
      24          16 :         if (use_ird) {
      25             :                 /* get IRD */
      26           8 :                 CHKGetStmtAttr(SQL_ATTR_IMP_ROW_DESC, &desc, sizeof(desc), &ind, "S");
      27             : 
      28           8 :                 CHKGetDescField(desc, 0, SQL_DESC_COUNT, &count, sizeof(count), &ind, "S");
      29             :         } else {
      30           8 :                 CHKNumResultCols(&count, "S");
      31             :         }
      32             : 
      33          16 :         if (count != 3) {
      34           0 :                 fprintf(stderr, "Wrong number of columns returned. Got %d expected 3\n", (int) count);
      35           0 :                 exit(1);
      36             :         }
      37             : 
      38          16 :         CHKDescribeCol(1, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
      39             : 
      40          16 :         cname = (char*) C(name);
      41          16 :         if (type != SQL_INTEGER || strcmp(cname, "i") != 0) {
      42           0 :                 fprintf(stderr, "wrong column 1 information (type %d name '%s' size %d)\n", (int) type, cname, (int) size);
      43           0 :                 exit(1);
      44             :         }
      45             : 
      46          16 :         CHKDescribeCol(2, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
      47             : 
      48          16 :         cname = (char*) C(name);
      49          16 :         if (type != SQL_CHAR || strcmp(cname, "c") != 0 || (size != 20 && (odbc_db_is_microsoft() || size != 40))) {
      50           0 :                 fprintf(stderr, "wrong column 2 information (type %d name '%s' size %d)\n", (int) type, cname, (int) size);
      51           0 :                 exit(1);
      52             :         }
      53             : 
      54          16 :         CHKDescribeCol(3, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
      55             : 
      56          16 :         cname = (char*) C(name);
      57          16 :         if (type != SQL_NUMERIC || strcmp(cname, "n") != 0 || size != 34 || digits != 12) {
      58           0 :                 fprintf(stderr, "wrong column 3 information (type %d name '%s' size %d)\n", (int) type, cname, (int) size);
      59           0 :                 exit(1);
      60             :         }
      61          16 :         ODBC_FREE();
      62          16 : }
      63             : 
      64             : int
      65           8 : main(void)
      66             : {
      67             :         SQLSMALLINT count;
      68             : 
      69           8 :         odbc_connect();
      70             : 
      71           8 :         odbc_command("create table #odbctestdata (i int, c char(20), n numeric(34,12) )");
      72             : 
      73             :         /* reset state */
      74           8 :         odbc_command("select * from #odbctestdata");
      75           8 :         SQLFetch(odbc_stmt);
      76           8 :         SQLMoreResults(odbc_stmt);
      77             : 
      78             :         /* test query returns column information for select without param */
      79           8 :         odbc_reset_statement();
      80           8 :         CHKPrepare(T("select * from #odbctestdata"), SQL_NTS, "S");
      81             : 
      82           8 :         count = -1;
      83           8 :         CHKNumResultCols(&count, "S");
      84             : 
      85           8 :         if (count != 3) {
      86           0 :                 fprintf(stderr, "Wrong number of columns returned. Got %d expected 3\n", (int) count);
      87           0 :                 exit(1);
      88             :         }
      89             : 
      90             :         /* test query returns column information for select with param */
      91           8 :         odbc_reset_statement();
      92           8 :         CHKPrepare(T("select c,n from #odbctestdata where i=?"), SQL_NTS, "S");
      93             : 
      94           8 :         count = -1;
      95           8 :         CHKNumResultCols(&count, "S");
      96             : 
      97           8 :         if (count != 2) {
      98           0 :                 fprintf(stderr, "Wrong number of columns returned. Got %d expected 2\n", (int) count);
      99           0 :                 exit(1);
     100             :         }
     101             : 
     102             :         /* test query returns column information for update */
     103           8 :         odbc_reset_statement();
     104           8 :         CHKPrepare(T("update #odbctestdata set i = 20"), SQL_NTS, "S");
     105             : 
     106           8 :         count = -1;
     107           8 :         CHKNumResultCols(&count, "S");
     108             : 
     109           8 :         if (count != 0) {
     110           0 :                 fprintf(stderr, "Wrong number of columns returned. Got %d expected 0\n", (int) count);
     111           0 :                 exit(1);
     112             :         }
     113             : 
     114           8 :         if (odbc_driver_is_freetds()) {
     115           8 :                 if (odbc_db_is_microsoft() && odbc_tds_version() >= 0x704) {
     116           0 :                         odbc_disconnect();
     117           0 :                         putenv("TDSVER=7.3");
     118           0 :                         odbc_connect();
     119           0 :                         odbc_command("create table #odbctestdata (i int, c char(20), n numeric(34,12) )");
     120             :                 }
     121           8 :                 if (!odbc_db_is_microsoft() || odbc_tds_version() < 0x704) {
     122           8 :                         Test(0);
     123           8 :                         odbc_reset_statement();
     124           8 :                         Test(1);
     125             :                 }
     126             :         }
     127             : 
     128             :         /* TODO test SQLDescribeParam (when implemented) */
     129           8 :         odbc_command("drop table #odbctestdata");
     130             : 
     131           8 :         odbc_disconnect();
     132             : 
     133           8 :         printf("Done.\n");
     134             :         return 0;
     135             : }

Generated by: LCOV version 1.13