LCOV - code coverage report
Current view: top level - src/odbc/unittests - stats.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 58 64 90.6 %
Date: 2025-04-23 23:01:24 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : static SQLLEN cnamesize;
       4             : static char output[256];
       5             : 
       6             : static void
       7          60 : ReadCol(int i)
       8             : {
       9          60 :         memset(output, 'x', sizeof(output));
      10          60 :         strcpy(output, "NULL");
      11          60 :         CHKGetData(i, SQL_C_CHAR, output, sizeof(output), &cnamesize, "S");
      12          60 : }
      13             : 
      14             : static const char *proc = "stat_proc";
      15             : static const char *table = "stat_proc";
      16             : static const char *column = "@t";
      17             : 
      18             : #define LEN(x) (x) ? (SQLSMALLINT) strlen(x) : 0
      19             : 
      20             : static void
      21          20 : TestProc(const char *catalog, const char *type, const char *expected)
      22             : {
      23             :         char sql[256];
      24          20 :         char *schema = NULL;
      25             : 
      26          20 :         odbc_command("IF OBJECT_ID('stat_proc') IS NOT NULL DROP PROC stat_proc");
      27             : 
      28          20 :         sprintf(sql, "CREATE PROC stat_proc(@t %s) AS RETURN 0", type);
      29          20 :         odbc_command(sql);
      30             : 
      31          20 :         column = "@t";
      32          20 :         CHKProcedureColumns(T(catalog), LEN(catalog), T(schema), LEN(schema), T(proc), LEN(proc), T(column), LEN(column), "SI");
      33             : 
      34          20 :         CHKFetch("SI");
      35             : 
      36          20 :         ReadCol(6);
      37          20 :         if (strcmp(output, expected) != 0) {
      38           0 :                 fprintf(stderr, "Got \"%s\" expected \"%s\"\n", output, expected);
      39           0 :                 odbc_disconnect();
      40           0 :                 exit(1);
      41             :         }
      42             : 
      43          20 :         CHKCloseCursor("SI");
      44          20 :         ODBC_FREE();
      45          20 : }
      46             : 
      47             : static void
      48          40 : TestTable(const char *catalog, const char *type, const char *expected)
      49             : {
      50             :         char sql[256];
      51          40 :         char *schema = NULL;
      52             : 
      53          40 :         if (catalog) {
      54          20 :                 schema = "dbo";
      55          20 :                 sprintf(sql, "IF OBJECT_ID('%s.%s.stat_t') IS NOT NULL DROP TABLE %s.%s.stat_t", catalog, schema, catalog, schema);
      56          20 :                 odbc_command(sql);
      57          20 :                 sprintf(sql, "CREATE TABLE %s.%s.stat_t(t %s)", catalog, schema, type);
      58             :         } else {
      59          20 :                 odbc_command("IF OBJECT_ID('stat_t') IS NOT NULL DROP TABLE stat_t");
      60          20 :                 sprintf(sql, "CREATE TABLE stat_t(t %s)", type);
      61             :         }
      62             : 
      63          40 :         odbc_command(sql);
      64             : 
      65          40 :         column = "t";
      66          40 :         table = "stat_t";
      67          40 :         CHKColumns(T(catalog), LEN(catalog), T(schema), LEN(schema), T(table), LEN(table), T(column), LEN(column), "SI");
      68             : 
      69          40 :         CHKFetch("SI");
      70             : 
      71          40 :         ReadCol(5);
      72          40 :         if (strcmp(output, expected) != 0) {
      73           0 :                 fprintf(stderr, "Got \"%s\" expected \"%s\"\n", output, expected);
      74           0 :                 odbc_disconnect();
      75           0 :                 exit(1);
      76             :         }
      77             : 
      78          40 :         CHKCloseCursor("SI");
      79          40 :         ODBC_FREE();
      80          40 : }
      81             : 
      82             : 
      83             : #define STR(n) str(int_buf, n)
      84             : 
      85             : static const char *
      86             : str(char *buf, int n)
      87             : {
      88          60 :         sprintf(buf, "%d", n);
      89             :         return buf;
      90             : }
      91             : 
      92             : static void
      93          20 : set_dbname(const char *dbname)
      94             : {
      95          20 :         CHKSetConnectAttr(SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER) T(dbname), strlen(dbname)*sizeof(SQLTCHAR), "SI");
      96          20 : }
      97             : 
      98          10 : TEST_MAIN()
      99             : {
     100             :         char int_buf[32];
     101             : 
     102          10 :         odbc_use_version3 = 0;
     103          10 :         odbc_connect();
     104             : 
     105          10 :         TestProc(NULL, "DATETIME", STR(SQL_TIMESTAMP));
     106          10 :         TestTable(NULL, "DATETIME", STR(SQL_TIMESTAMP));
     107          10 :         set_dbname("master");
     108          10 :         TestTable(common_pwd.database, "DATETIME", STR(SQL_TIMESTAMP));
     109             : 
     110          10 :         odbc_disconnect();
     111             : 
     112             : 
     113          10 :         odbc_use_version3 = 1;
     114          10 :         odbc_connect();
     115             : 
     116          10 :         TestProc(NULL, "DATETIME", STR(SQL_TYPE_TIMESTAMP));
     117          10 :         TestTable(NULL, "DATETIME", STR(SQL_TYPE_TIMESTAMP));
     118          10 :         set_dbname("master");
     119          10 :         TestTable(common_pwd.database, "DATETIME", STR(SQL_TYPE_TIMESTAMP));
     120             : 
     121          10 :         odbc_disconnect();
     122             : 
     123          10 :         printf("Done.\n");
     124          10 :         return 0;
     125             : }

Generated by: LCOV version 1.13