LCOV - code coverage report
Current view: top level - src/odbc/unittests - prepare_results.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 55 71 77.5 %
Date: 2025-02-21 09:36:06 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             : int
      65          10 : main(void)
      66             : {
      67             :         SQLSMALLINT count;
      68             : 
      69          10 :         odbc_connect();
      70             : 
      71          10 :         odbc_command("create table #odbctestdata (i int, c char(20), n numeric(34,12) )");
      72             : 
      73             :         /* reset state */
      74          10 :         odbc_command("select * from #odbctestdata");
      75          10 :         SQLFetch(odbc_stmt);
      76          10 :         SQLMoreResults(odbc_stmt);
      77             : 
      78             :         /* test query returns column information for select without param */
      79          10 :         odbc_reset_statement();
      80          10 :         CHKPrepare(T("select * from #odbctestdata"), SQL_NTS, "S");
      81             : 
      82          10 :         count = -1;
      83          10 :         CHKNumResultCols(&count, "S");
      84             : 
      85          10 :         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          10 :         odbc_reset_statement();
      92          10 :         CHKPrepare(T("select c,n from #odbctestdata where i=?"), SQL_NTS, "S");
      93             : 
      94          10 :         count = -1;
      95          10 :         CHKNumResultCols(&count, "S");
      96             : 
      97          10 :         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          10 :         odbc_reset_statement();
     104          10 :         CHKPrepare(T("update #odbctestdata set i = 20"), SQL_NTS, "S");
     105             : 
     106          10 :         count = -1;
     107          10 :         CHKNumResultCols(&count, "S");
     108             : 
     109          10 :         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          10 :         if (odbc_driver_is_freetds()) {
     115          10 :                 if (odbc_db_is_microsoft() && odbc_tds_version() >= 0x704) {
     116           2 :                         odbc_disconnect();
     117           2 :                         putenv("TDSVER=7.3");
     118           2 :                         odbc_connect();
     119           2 :                         odbc_command("create table #odbctestdata (i int, c char(20), n numeric(34,12) )");
     120             :                 }
     121          10 :                 if (!odbc_db_is_microsoft() || odbc_tds_version() < 0x704) {
     122          10 :                         Test(0);
     123          10 :                         odbc_reset_statement();
     124          10 :                         Test(1);
     125             :                 }
     126             :         }
     127             : 
     128             :         /* TODO test SQLDescribeParam (when implemented) */
     129          10 :         odbc_command("drop table #odbctestdata");
     130             : 
     131          10 :         odbc_disconnect();
     132             : 
     133          10 :         printf("Done.\n");
     134             :         return 0;
     135             : }

Generated by: LCOV version 1.13