NCBI C++ ToolKit
t0002.c
Go to the documentation of this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

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/string.h>
22 #include <replacements.h>
23 
24 #include <common/test_assert.h>
25 
26 static char *
27 value_as_string(TDSSOCKET * tds, int col_idx)
28 {
29  static char result[256];
30  const int type = tds->res_info->columns[col_idx]->column_type;
31  const void *value = tds->res_info->columns[col_idx]->column_data;
32 
33  switch (type) {
34  case SYBVARCHAR:
35  strlcpy(result, (const char *) value, sizeof(result));
36  break;
37  case SYBINT4:
38  sprintf(result, "%d", *(const int *) value);
39  break;
40  default:
41  sprintf(result, "Unexpected column_type %d", type);
42  break;
43  }
44  return result;
45 } /* value_as_string() */
46 
47 
48 int
49 main(int argc, char **argv)
50 {
51  TDSLOGIN *login;
52  TDSSOCKET *tds;
53  int verbose = 0;
54  int num_cols = 2;
55  TDS_INT result_type;
56  int rc;
57  int i, done_flags;
58 
59  fprintf(stdout, "%s: Test basic submit query, results\n", __FILE__);
60  rc = try_tds_login(&login, &tds, __FILE__, verbose);
61  if (rc != TDS_SUCCESS) {
62  fprintf(stderr, "try_tds_login() failed\n");
63  return 1;
64  }
65 
66  rc = tds_submit_query(tds, "select db_name() dbname, user_name() username");
67  if (rc != TDS_SUCCESS) {
68  fprintf(stderr, "tds_submit_query() failed\n");
69  return 1;
70  }
71 
72  while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
73  switch (result_type) {
74  case TDS_ROWFMT_RESULT:
75  if (tds->res_info->num_cols != num_cols) {
76  fprintf(stderr, "Error: num_cols != %d in %s\n", num_cols, __FILE__);
77  return 1;
78  }
81  fprintf(stderr, "Wrong column_type in %s\n", __FILE__);
82  return 1;
83  }
84  if (strcmp(tds_dstr_cstr(&tds->res_info->columns[0]->column_name), "dbname")
85  || strcmp(tds_dstr_cstr(&tds->res_info->columns[1]->column_name), "username")) {
86  fprintf(stderr, "Wrong column_name in %s\n", __FILE__);
87  return 1;
88  }
89  break;
90 
91  case TDS_ROW_RESULT:
92 
94  if (result_type != TDS_ROW_RESULT || result_type != TDS_COMPUTE_RESULT)
95  break;
96  if (verbose) {
97  for (i = 0; i < num_cols; i++) {
98  printf("col %i is %s\n", i, value_as_string(tds, i));
99  }
100  }
101  }
102  if (rc != TDS_SUCCESS) {
103  fprintf(stderr, "tds_process_tokens() unexpected return\n");
104  }
105  break;
106 
107  case TDS_DONE_RESULT:
108  case TDS_DONEPROC_RESULT:
110  if (!(done_flags & TDS_DONE_ERROR))
111  break;
112 
113  default:
114  fprintf(stderr, "tds_process_tokens() unexpected result_type\n");
115  break;
116  }
117  }
118  if (rc != TDS_NO_MORE_RESULTS) {
119  fprintf(stderr, "tds_process_tokens() unexpected return\n");
120  }
121 
123  return 0;
124 }
static TDSSOCKET * tds
Definition: collations.c:37
char value[7]
Definition: config.c:431
int main(int argc, char **argv)
Definition: t0002.c:18
static TDSLOGIN * login
Definition: dataread.c:31
#define NULL
Definition: ncbistd.hpp:225
static const char * tds_dstr_cstr(DSTR *s)
Returns a C version (NUL terminated string) of dstr.
Definition: string.h:66
int i
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
true_type verbose
Definition: processing.cpp:890
#define tds_submit_query
#define tds_process_tokens
#define strlcpy(d, s, l)
Definition: replacements.h:80
DSTR column_name
Definition: tds.h:787
unsigned char * column_data
Definition: tds.h:793
TDS_SERVER_TYPE column_type
This type can be different from wire type because conversion (e.g.
Definition: tds.h:768
Definition: tds.h:584
TDS_USMALLINT num_cols
Definition: tds.h:845
TDSCOLUMN ** columns
Definition: tds.h:844
Information for a server connection.
Definition: tds.h:1211
TDSRESULTINFO * res_info
Definition: tds.h:1264
Definition: type.c:6
#define SYBINT4
Definition: sybdb.h:170
#define SYBVARCHAR
Definition: sybdb.h:162
int try_tds_logout(TDSLOGIN *login, TDSSOCKET *tds, int verbose)
Definition: common.c:128
int try_tds_login(TDSLOGIN **login, TDSSOCKET **tds, const char *appname, int verbose)
Definition: common.c:55
static char * value_as_string(TDSSOCKET *tds, int col_idx)
Definition: t0002.c:27
#define TDS_DONEPROC_RESULT
Definition: tds.h:228
#define TDS_ROWFMT_RESULT
Definition: tds.h:224
tds_sysdep_int32_type TDS_INT
Definition: tds.h:149
@ TDS_RETURN_ROW
Definition: tds.h:256
@ TDS_STOPAT_ROWFMT
Definition: tds.h:252
@ TDS_TOKEN_RESULTS
Definition: tds.h:261
@ TDS_RETURN_COMPUTE
Definition: tds.h:257
@ TDS_RETURN_DONE
Definition: tds.h:255
#define TDS_DONE_RESULT
Definition: tds.h:227
#define TDS_NO_MORE_RESULTS
Definition: tds.h:202
#define TDS_COMPUTE_RESULT
Definition: tds.h:220
#define TDS_ROW_RESULT
Definition: tds.h:216
@ TDS_DONE_ERROR
error occurred
Definition: tds.h:272
#define TDS_SUCCESS
Definition: tds.h:203
#define TDS_DONEINPROC_RESULT
Definition: tds.h:229
else result
Definition: token2.c:20
Modified on Tue Apr 09 08:00:28 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:35:49 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:21:41 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:23:44 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:50:49 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:29:42 2024 by modify_doxy.py rev. 669887
Modified on Tue Apr 16 20:15:20 2024 by modify_doxy.py rev. 669887