LCOV - code coverage report
Current view: top level - src/odbc/unittests - prepare_results.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 56 72 77.8 %
Date: 2025-04-22 08:52:13 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          20 : 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          20 :         char *cname = NULL;
      14             : 
      15             :         /* test query returns column information */
      16          20 :         CHKPrepare(T("select * from #odbctestdata select * from #odbctestdata"), SQL_NTS, "S");
      17             : 
      18          20 :         SQLNumParams(odbc_stmt, &count);
      19          20 :         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          20 :         if (use_ird) {
      25             :                 /* get IRD */
      26          10 :                 CHKGetStmtAttr(SQL_ATTR_IMP_ROW_DESC, &desc, sizeof(desc), &ind, "S");
      27             : 
      28          10 :                 CHKGetDescField(desc, 0, SQL_DESC_COUNT, &count, sizeof(count), &ind, "S");
      29             :         } else {
      30          10 :                 CHKNumResultCols(&count, "S");
      31             :         }
      32             : 
      33          20 :         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          20 :         CHKDescribeCol(1, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
      39             : 
      40          20 :         cname = (char*) C(name);
      41          20 :         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          20 :         CHKDescribeCol(2, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
      47             : 
      48          20 :         cname = (char*) C(name);
      49          20 :         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          20 :         CHKDescribeCol(3, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
      55             : 
      56          20 :         cname = (char*) C(name);
      57          20 :         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          20 :         ODBC_FREE();
      62          20 : }
      63             : 
      64          10 : TEST_MAIN()
      65             : {
      66             :         SQLSMALLINT count;
      67             : 
      68          10 :         odbc_connect();
      69             : 
      70          10 :         odbc_command("create table #odbctestdata (i int, c char(20), n numeric(34,12) )");
      71             : 
      72             :         /* reset state */
      73          10 :         odbc_command("select * from #odbctestdata");
      74          10 :         SQLFetch(odbc_stmt);
      75          10 :         SQLMoreResults(odbc_stmt);
      76             : 
      77             :         /* test query returns column information for select without param */
      78          10 :         odbc_reset_statement();
      79          10 :         CHKPrepare(T("select * from #odbctestdata"), SQL_NTS, "S");
      80             : 
      81          10 :         count = -1;
      82          10 :         CHKNumResultCols(&count, "S");
      83             : 
      84          10 :         if (count != 3) {
      85           0 :                 fprintf(stderr, "Wrong number of columns returned. Got %d expected 3\n", (int) count);
      86           0 :                 exit(1);
      87             :         }
      88             : 
      89             :         /* test query returns column information for select with param */
      90          10 :         odbc_reset_statement();
      91          10 :         CHKPrepare(T("select c,n from #odbctestdata where i=?"), SQL_NTS, "S");
      92             : 
      93          10 :         count = -1;
      94          10 :         CHKNumResultCols(&count, "S");
      95             : 
      96          10 :         if (count != 2) {
      97           0 :                 fprintf(stderr, "Wrong number of columns returned. Got %d expected 2\n", (int) count);
      98           0 :                 exit(1);
      99             :         }
     100             : 
     101             :         /* test query returns column information for update */
     102          10 :         odbc_reset_statement();
     103          10 :         CHKPrepare(T("update #odbctestdata set i = 20"), SQL_NTS, "S");
     104             : 
     105          10 :         count = -1;
     106          10 :         CHKNumResultCols(&count, "S");
     107             : 
     108          10 :         if (count != 0) {
     109           0 :                 fprintf(stderr, "Wrong number of columns returned. Got %d expected 0\n", (int) count);
     110           0 :                 exit(1);
     111             :         }
     112             : 
     113          10 :         if (odbc_driver_is_freetds()) {
     114          10 :                 if (odbc_db_is_microsoft() && odbc_tds_version() >= 0x704) {
     115           2 :                         odbc_disconnect();
     116           2 :                         putenv("TDSVER=7.3");
     117           2 :                         odbc_connect();
     118           2 :                         odbc_command("create table #odbctestdata (i int, c char(20), n numeric(34,12) )");
     119             :                 }
     120          10 :                 if (!odbc_db_is_microsoft() || odbc_tds_version() < 0x704) {
     121          10 :                         Test(0);
     122          10 :                         odbc_reset_statement();
     123          10 :                         Test(1);
     124             :                 }
     125             :         }
     126             : 
     127             :         /* TODO test SQLDescribeParam (when implemented) */
     128          10 :         odbc_command("drop table #odbctestdata");
     129             : 
     130          10 :         odbc_disconnect();
     131             : 
     132          10 :         printf("Done.\n");
     133          10 :         return 0;
     134             : }

Generated by: LCOV version 1.13