LCOV - code coverage report
Current view: top level - src/tds/unittests - t0002.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 24 38 63.2 %
Date: 2025-01-18 11:50:39 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 1998-1999  Brian Bruns
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : #include "common.h"
      20             : 
      21             : #include <freetds/utils/string.h>
      22             : #include <freetds/replacements.h>
      23             : 
      24             : static char *
      25             : value_as_string(TDSSOCKET * tds, int col_idx)
      26             : {
      27             :         static char result[256];
      28             :         const int type = tds->res_info->columns[col_idx]->column_type;
      29             :         const void *value = tds->res_info->columns[col_idx]->column_data;
      30             : 
      31             :         switch (type) {
      32             :         case SYBVARCHAR:
      33             :                 strlcpy(result, (const char *) value, sizeof(result));
      34             :                 break;
      35             :         case SYBINT4:
      36             :                 sprintf(result, "%d", *(const int *) value);
      37             :                 break;
      38             :         default:
      39             :                 sprintf(result, "Unexpected column_type %d", type);
      40             :                 break;
      41             :         }
      42             :         return result;
      43             : }                               /* value_as_string()  */
      44             : 
      45             : 
      46             : int
      47           8 : main(void)
      48             : {
      49             :         TDSLOGIN *login;
      50             :         TDSSOCKET *tds;
      51           8 :         int verbose = 0;
      52           8 :         int num_cols = 2;
      53             :         TDS_INT result_type;
      54             :         int rc;
      55             :         int i, done_flags;
      56             : 
      57           8 :         printf("%s: Test basic submit query, results\n", __FILE__);
      58           8 :         rc = try_tds_login(&login, &tds, __FILE__, verbose);
      59           8 :         if (rc != TDS_SUCCESS) {
      60           0 :                 fprintf(stderr, "try_tds_login() failed\n");
      61           0 :                 return 1;
      62             :         }
      63             : 
      64           8 :         rc = tds_submit_query(tds, "select db_name() dbname, user_name() username");
      65           8 :         if (rc != TDS_SUCCESS) {
      66           0 :                 fprintf(stderr, "tds_submit_query() failed\n");
      67           0 :                 return 1;
      68             :         }
      69             : 
      70          32 :         while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
      71          24 :                 switch (result_type) {
      72           8 :                 case TDS_ROWFMT_RESULT:
      73           8 :                         if (tds->res_info->num_cols != num_cols) {
      74           0 :                                 fprintf(stderr, "Error:  num_cols != %d in %s\n", num_cols, __FILE__);
      75           0 :                                 return 1;
      76             :                         }
      77           8 :                         if (tds->res_info->columns[0]->column_type != SYBVARCHAR
      78           8 :                             || tds->res_info->columns[1]->column_type != SYBVARCHAR) {
      79           0 :                                 fprintf(stderr, "Wrong column_type in %s\n", __FILE__);
      80           0 :                                 return 1;
      81             :                         }
      82          16 :                         if (strcmp(tds_dstr_cstr(&tds->res_info->columns[0]->column_name), "dbname")
      83          16 :                             || strcmp(tds_dstr_cstr(&tds->res_info->columns[1]->column_name), "username")) {
      84           0 :                                 fprintf(stderr, "Wrong column_name in %s\n", __FILE__);
      85           0 :                                 return 1;
      86             :                         }
      87             :                         break;
      88             : 
      89             :                 case TDS_ROW_RESULT:
      90             : 
      91           8 :                         while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) {
      92             :                                 if (result_type != TDS_ROW_RESULT || result_type != TDS_COMPUTE_RESULT)
      93             :                                         break;
      94             :                                 if (verbose) {
      95             :                                         for (i = 0; i < num_cols; i++) {
      96             :                                                 printf("col %i is %s\n", i, value_as_string(tds, i));
      97             :                                         }
      98             :                                 }
      99             :                         }
     100           8 :                         if (rc != TDS_SUCCESS) {
     101           0 :                                 fprintf(stderr, "tds_process_tokens() unexpected return\n");
     102             :                         }
     103             :                         break;
     104             : 
     105           8 :                 case TDS_DONE_RESULT:
     106             :                 case TDS_DONEPROC_RESULT:
     107             :                 case TDS_DONEINPROC_RESULT:
     108           8 :                         if (!(done_flags & TDS_DONE_ERROR))
     109             :                                 break;
     110             : 
     111             :                 default:
     112           0 :                         fprintf(stderr, "tds_process_tokens() unexpected result_type\n");
     113           0 :                         break;
     114             :                 }
     115           8 :         }
     116           8 :         if (rc != TDS_NO_MORE_RESULTS) {
     117           0 :                 fprintf(stderr, "tds_process_tokens() unexpected return\n");
     118             :         }
     119             : 
     120           8 :         try_tds_logout(login, tds, verbose);
     121           8 :         return 0;
     122             : }

Generated by: LCOV version 1.13