LCOV - code coverage report
Current view: top level - src/odbc/unittests - all_types.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 57 59 96.6 %
Date: 2024-04-18 20:40:06 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #undef NDEBUG
       2             : #include "common.h"
       3             : #include <assert.h>
       4             : #define TDS_DONT_DEFINE_DEFAULT_FUNCTIONS
       5             : #include "../../tds/unittests/common.h"
       6             : #include <freetds/odbc.h>
       7             : 
       8             : /* Check we support any possible types from the server */
       9             : 
      10             : static int sql_c_types[100];
      11             : static const char *sql_c_types_names[100];
      12             : static unsigned num_c_types = 0;
      13             : #ifdef _WIN32
      14             : HINSTANCE hinstFreeTDS;
      15             : #endif
      16             : 
      17             : static TDS_STMT *stmt;
      18             : 
      19         968 : static void test_type(TDSSOCKET *tds TDS_UNUSED, TDSCOLUMN *col)
      20             : {
      21             :         unsigned n;
      22             : 
      23             :         /* check that we can get type information from column */
      24             :         struct _drecord drec;
      25         968 :         memset(&drec, 0, sizeof(drec));
      26         968 :         odbc_set_sql_type_info(col, &drec, SQL_OV_ODBC3);
      27             : 
      28         968 :         assert(drec.sql_desc_literal_prefix);
      29         968 :         assert(drec.sql_desc_literal_suffix);
      30         968 :         assert(drec.sql_desc_type_name);
      31         968 :         assert(drec.sql_desc_type_name[0]);
      32             : 
      33             :         /* check we can attempt to convert from any type to any
      34             :          * SQL C type */
      35       24200 :         for (n = 0; n < num_c_types; ++n) {
      36             :                 TDS_CHAR buffer[256];
      37             :                 SQLLEN len;
      38       24200 :                 int sql_c_type = sql_c_types[n];
      39       24200 :                 const char *sql_c_type_name = sql_c_types_names[n];
      40             :                 struct _drecord drec_ixd;
      41       24200 :                 SQLLEN dest_len = sizeof(buffer);
      42             :                 TDSPARAMINFO *params;
      43             : 
      44       24200 :                 len = odbc_tds2sql_col(stmt, col, sql_c_type, buffer, sizeof(buffer), NULL);
      45       24200 :                 if (len == SQL_NULL_DATA) {
      46       12072 :                         printf("error converting to %3d (%s)\n", sql_c_type, sql_c_type_name);
      47       12072 :                         continue;
      48             :                 }
      49             : 
      50       12128 :                 params = tds_alloc_param_result(NULL);
      51       12128 :                 assert(params);
      52             : 
      53             :                 /* convert back to server */
      54       12128 :                 memset(&drec_ixd, 0, sizeof(drec_ixd));
      55       12128 :                 drec_ixd.sql_desc_concise_type = sql_c_type;
      56       12128 :                 drec_ixd.sql_desc_data_ptr = buffer;
      57       12128 :                 drec_ixd.sql_desc_octet_length_ptr = &dest_len;
      58       12128 :                 drec_ixd.sql_desc_precision = 18;
      59       12128 :                 drec_ixd.sql_desc_scale = 4;
      60       12128 :                 odbc_sql2tds(stmt, &drec_ixd, &drec, params->columns[0], true, stmt->ard, 0);
      61       12128 :                 tds_free_param_results(params);
      62             :         }
      63         968 : }
      64             : 
      65             : int
      66           8 : main(void)
      67             : {
      68             :         TDS_DBC *dbc;
      69             :         TDS_ENV *env;
      70             :         SQLULEN ulen;
      71             :         int i;
      72             : 
      73             : #ifdef _WIN32
      74             :         hinstFreeTDS = GetModuleHandle(NULL);
      75             : #endif
      76             : 
      77             :         /* extract all C types we support */
      78        3216 :         for (i = -200; i <= 200; ++i) {
      79        3208 :                 assert(num_c_types < 100);
      80        3208 :                 if (odbc_c_to_server_type(i) != TDS_INVALID_TYPE) {
      81         200 :                         sql_c_types[num_c_types] = i;
      82         200 :                         sql_c_types_names[num_c_types] = odbc_lookup_value(i, odbc_sql_c_types, NULL);
      83         200 :                         assert(sql_c_types_names[num_c_types] != NULL);
      84         200 :                         num_c_types++;
      85             :                 }
      86             :         }
      87             : 
      88             :         /* this specific test doesn't need a connection, so fake one */
      89           8 :         CHKAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &odbc_env, "S");
      90           8 :         SQLSetEnvAttr(odbc_env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER);
      91           8 :         CHKAllocHandle(SQL_HANDLE_DBC, odbc_env, &odbc_conn, "S");
      92             : 
      93           8 :         if (!odbc_driver_is_freetds())
      94             :                 return 0;
      95             : 
      96             :         /* get internal structures */
      97           8 :         CHKGetInfo(SQL_DRIVER_HDBC, &ulen, sizeof(ulen), NULL, "S");
      98           8 :         dbc = (TDS_DBC *) (TDS_UINTPTR) ulen;
      99           8 :         CHKGetInfo(SQL_DRIVER_HENV, &ulen, sizeof(ulen), NULL, "S");
     100           8 :         env = (TDS_ENV *) (TDS_UINTPTR) ulen;
     101           8 :         assert(dbc && env);
     102           8 :         assert(env->tds_ctx);
     103             : 
     104           8 :         assert(!dbc->tds_socket);
     105           8 :         dbc->tds_socket = tds_alloc_socket(env->tds_ctx, 512);
     106           8 :         assert(dbc->tds_socket);
     107           8 :         dbc->tds_socket->conn->use_iconv = 0;
     108           8 :         tds_set_parent(dbc->tds_socket, dbc);
     109           8 :         if (TDS_FAILED(tds_iconv_open(dbc->tds_socket->conn, "UTF-8", 1))) {
     110           0 :                 fprintf(stderr, "Failed to initialize iconv\n");
     111           0 :                 return 1;
     112             :         }
     113             : 
     114             :         /* get one statement to test with */
     115           8 :         assert(dbc->stmt_list == NULL);
     116           8 :         CHKAllocStmt(&odbc_stmt, "S");
     117           8 :         assert(dbc->stmt_list);
     118           8 :         stmt = dbc->stmt_list;
     119             : 
     120           8 :         tds_all_types(dbc->tds_socket, test_type);
     121             : 
     122           8 :         odbc_disconnect();
     123           8 :         return 0;
     124             : }

Generated by: LCOV version 1.13