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: 2025-02-21 09:36:06 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.13