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

Generated by: LCOV version 1.13