LCOV - code coverage report
Current view: top level - src/odbc/unittests - tables.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 99 105 94.3 %
Date: 2025-02-21 09:36:06 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : #ifdef _WIN32
       4             : #undef strcasecmp
       5             : #define strcasecmp stricmp
       6             : #endif
       7             : 
       8             : static SQLLEN cnamesize;
       9             : static char output[256];
      10             : 
      11             : static void
      12        5654 : ReadCol(int i)
      13             : {
      14        5654 :         strcpy(output, "NULL");
      15        5654 :         CHKGetData(i, SQL_C_CHAR, output, sizeof(output), &cnamesize, "S");
      16        5654 : }
      17             : 
      18             : static void
      19         640 : TestName(int index, const char *expected_name)
      20             : {
      21             :         SQLTCHAR name[128];
      22             :         char buf[256];
      23             :         SQLSMALLINT len, type;
      24             : 
      25             : #define NAME_TEST \
      26             :         do { \
      27             :                 if (strcmp(C(name), expected_name) != 0) \
      28             :                 { \
      29             :                         sprintf(buf, "wrong name in column %d expected '%s' got '%s'", index, expected_name, C(name)); \
      30             :                         ODBC_REPORT_ERROR(buf); \
      31             :                 } \
      32             :         } while(0)
      33             : 
      34             :         /* retrieve with SQLDescribeCol */
      35         640 :         CHKDescribeCol(index, name, TDS_VECTOR_SIZE(name), &len, &type, NULL, NULL, NULL, "S");
      36         640 :         NAME_TEST;
      37             : 
      38             :         /* retrieve with SQLColAttribute */
      39         640 :         CHKColAttribute(index, SQL_DESC_NAME, name, TDS_VECTOR_SIZE(name), &len, NULL, "S");
      40         640 :         if (odbc_db_is_microsoft())
      41         520 :                 NAME_TEST;
      42         640 :         CHKColAttribute(index, SQL_DESC_LABEL, name, TDS_VECTOR_SIZE(name), &len, NULL, "S");
      43         640 :         NAME_TEST;
      44         640 : }
      45             : 
      46             : static const char *catalog = NULL;
      47             : static const char *schema = NULL;
      48             : static const char *table = "sysobjects";
      49             : static const char *expect = NULL;
      50             : static int expect_col = 3;
      51             : static char expected_type[20] = "SYSTEM TABLE";
      52             : 
      53             : static void
      54         128 : DoTest(const char *type, int row_returned, int line)
      55             : {
      56         128 :         int table_len = SQL_NULL_DATA;
      57             :         char table_buf[80];
      58         128 :         int found = 0;
      59             : 
      60             : #define LEN(x) (x) ? strlen(x) : SQL_NULL_DATA
      61             : 
      62         128 :         if (table) {
      63         128 :                 strcpy(table_buf, table);
      64         128 :                 strcat(table_buf, "garbage");
      65         128 :                 table_len = strlen(table);
      66             :         }
      67             : 
      68         128 :         printf("Test type '%s' %s row at line %d\n", type ? type : "", row_returned ? "with" : "without", line);
      69         128 :         CHKTables(T(catalog), LEN(catalog), T(schema), LEN(schema), T(table_buf), table_len, T(type), LEN(type), "SI");
      70             : 
      71             :         /* test column name (for DBD::ODBC) */
      72         128 :         TestName(1, odbc_use_version3 || !odbc_driver_is_freetds() ? "TABLE_CAT" : "TABLE_QUALIFIER");
      73         128 :         TestName(2, odbc_use_version3 || !odbc_driver_is_freetds() ? "TABLE_SCHEM" : "TABLE_OWNER");
      74         128 :         TestName(3, "TABLE_NAME");
      75         128 :         TestName(4, "TABLE_TYPE");
      76         128 :         TestName(5, "REMARKS");
      77             : 
      78         128 :         if (row_returned) {
      79          98 :                 CHKFetch("SI");
      80             : 
      81          98 :                 if (!expect) {
      82          68 :                         ReadCol(1);
      83          68 :                         ReadCol(2);
      84          68 :                         ReadCol(3);
      85          68 :                         if (strcasecmp(output, "sysobjects") != 0) {
      86           0 :                                 printf("wrong table %s\n", output);
      87           0 :                                 exit(1);
      88             :                         }
      89             : 
      90          68 :                         ReadCol(4);
      91             :                         /* under mssql2k5 is a view */
      92          68 :                         if (strcmp(output, expected_type) != 0) {
      93           0 :                                 printf("wrong table type %s\n", output);
      94           0 :                                 exit(1);
      95             :                         }
      96          68 :                         ReadCol(5);
      97             :                 }
      98             :         }
      99             : 
     100         128 :         if (expect) {
     101          30 :                 ReadCol(expect_col);
     102          30 :                 if (strcmp(output, expect) == 0)
     103          18 :                         found = 1;
     104             :         }
     105        5428 :         while (CHKFetch("SNo") == SQL_SUCCESS && row_returned > 1) {
     106        5300 :                 if (expect) {
     107        5284 :                         ReadCol(expect_col);
     108        5284 :                         if (strcmp(output, expect) == 0)
     109         146 :                                 found = 1;
     110             :                 }
     111             :                 if (row_returned < 2)
     112             :                         break;
     113             :         }
     114             : 
     115         128 :         if (expect && !found) {
     116           0 :                 printf("expected row not found\n");
     117           0 :                 exit(1);
     118             :         }
     119             : 
     120         128 :         CHKCloseCursor("SI");
     121         128 :         expect = NULL;
     122         128 :         expect_col = 3;
     123         128 : }
     124             : 
     125             : #define DoTest(a,b) DoTest(a,b,__LINE__)
     126             : 
     127             : int
     128          10 : main(void)
     129             : {
     130             :         char type[32];
     131          10 :         int mssql2005 = 0;
     132             : 
     133          10 :         odbc_use_version3 = 0;
     134          10 :         odbc_connect();
     135             : 
     136          10 :         if (odbc_db_is_microsoft() && odbc_db_version_int() >= 0x09000000u) {
     137           6 :                 mssql2005 = 1;
     138           6 :                 strcpy(expected_type, "VIEW");
     139           6 :                 odbc_command_with_result(odbc_stmt, "USE master");
     140             :         }
     141             : 
     142          10 :         DoTest(NULL, 1);
     143          10 :         sprintf(type, "'%s'", expected_type);
     144          10 :         DoTest(type, 1);
     145          10 :         DoTest("'TABLE'", 0);
     146          10 :         DoTest(type, 1);
     147          10 :         DoTest("TABLE", 0);
     148          10 :         DoTest("TABLE,VIEW", mssql2005 ? 1 : 0);
     149          10 :         DoTest("SYSTEM TABLE,'TABLE'", mssql2005 ? 0 : 1);
     150          10 :         sprintf(type, "TABLE,'%s'", expected_type);
     151          10 :         DoTest(type, 1);
     152             : 
     153          10 :         odbc_disconnect();
     154             : 
     155             : 
     156          10 :         odbc_use_version3 = 1;
     157          10 :         odbc_connect();
     158             : 
     159          10 :         if (mssql2005)
     160           6 :                 odbc_command_with_result(odbc_stmt, "USE master");
     161             : 
     162          10 :         sprintf(type, "'%s'", expected_type);
     163          10 :         DoTest(type, 1);
     164             :         /* TODO this should work even for Sybase and mssql 2005 */
     165          10 :         if (odbc_db_is_microsoft()) {
     166             :                 /* here table is a name of table */
     167           8 :                 catalog = "%";
     168           8 :                 schema = NULL;
     169           8 :                 DoTest(NULL, 2);
     170             :         }
     171             : 
     172             :         /*
     173             :          * tests for Jdbc compatibility
     174             :          */
     175             : 
     176             :         /* enum tables */
     177          10 :         catalog = NULL;
     178          10 :         schema = NULL;
     179          10 :         table = "%";
     180          10 :         expect = "sysobjects";
     181          10 :         DoTest(NULL, 2);
     182             : 
     183             :         /* enum catalogs */
     184          10 :         catalog = "%";
     185          10 :         schema = "";
     186          10 :         table = "";
     187          10 :         expect = "master";
     188          10 :         expect_col = 1;
     189          10 :         DoTest(NULL, 2);
     190             : 
     191             :         /* enum schemas (owners) */
     192          10 :         catalog = "";
     193          10 :         schema = "%";
     194          10 :         table = "";
     195          10 :         expect = "dbo";
     196          10 :         expect_col = 2;
     197          10 :         DoTest(NULL, 2);
     198             : 
     199          10 :         odbc_disconnect();
     200             : 
     201          10 :         printf("Done.\n");
     202             :         return 0;
     203             : }

Generated by: LCOV version 1.13