NCBI C++ ToolKit
flags.c
Go to the documentation of 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, 2000, 2001, 2002, 2003, 2004, 2005 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 
20 #include "common.h"
21 
22 #include <freetds/convert.h>
23 
24 #include <common/test_assert.h>
25 
26 static TDSLOGIN *login;
27 static TDSSOCKET *tds;
28 
29 static void
30 fatal_error(const char *msg)
31 {
32  fprintf(stderr, "%s\n", msg);
33  exit(1);
34 }
35 
36 static void
37 check_flags(TDSCOLUMN * curcol, int n, const char *possible_results)
38 {
39  char msg[256];
40  char flags[256];
41  size_t l;
42  char *all_res = strdup(possible_results);
43  char *res;
44  int correct = 0;
45 
46  flags[0] = 0;
47  if (curcol->column_nullable)
48  strcat(flags, "nullable ");
49  if (curcol->column_writeable)
50  strcat(flags, "writable ");
51  if (curcol->column_identity)
52  strcat(flags, "identity ");
53  if (curcol->column_key)
54  strcat(flags, "key ");
55  if (curcol->column_hidden)
56  strcat(flags, "hidden ");
57  l = strlen(flags);
58  if (l)
59  flags[l - 1] = 0;
60 
61  /* one result is valid ?? */
62  for (res = strtok(all_res, "-"); res; res = strtok(NULL, "-"))
63  if (strcmp(flags, res) == 0)
64  correct = 1;
65 
66  if (!correct) {
67  sprintf(msg, "flags:%s\nexpected: %s\nwrong column %d flags", flags, possible_results, n + 1);
68  fatal_error(msg);
69  }
70 }
71 
72 static void
73 test_begin(const char *cmd)
74 {
75  TDS_INT result_type;
76 
77  fprintf(stdout, "%s: Testing query\n", cmd);
79  fatal_error("tds_submit_query() failed");
80 
82  fatal_error("tds_process_tokens() failed");
83 
84  if (result_type != TDS_ROWFMT_RESULT)
85  fatal_error("expected row fmt() failed");
86 
87  /* test columns results */
89  fatal_error("wrong current_results");
90 }
91 
92 static void
93 test_end(void)
94 {
95  TDS_INT result_type;
96  int done_flags;
97 
98  if (tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS) != TDS_SUCCESS)
99  fatal_error("tds_process_tokens() failed");
100 
101  if (result_type != TDS_DONE_RESULT)
102  fatal_error("expected done failed");
103 
104  if (done_flags & TDS_DONE_ERROR)
105  fatal_error("query failed");
106 
108  fatal_error("tds_process_tokens() failed");
109 }
110 
111 int
112 main(int argc, char **argv)
113 {
115  char mymsg[256];
116 
117  fprintf(stdout, "%s: Testing flags from server\n", __FILE__);
118  if (try_tds_login(&login, &tds, __FILE__, 0) != TDS_SUCCESS) {
119  fprintf(stderr, "try_tds_login() failed\n");
120  return 1;
121  }
122 
123  if (run_query(tds, "create table #tmp1 (i numeric(10,0) identity primary key, b varchar(20) null, c int not null)") !=
124  TDS_SUCCESS)
125  fatal_error("creating table error");
126 
127  /* TDS 4.2 without FOR BROWSE clause seem to forget flags... */
128  if (!IS_TDS42(tds->conn)) {
129  /* check select of all fields */
130  test_begin("select * from #tmp1");
132 
133  if (info->num_cols != 3) {
134  sprintf(mymsg,"wrong number of columns returned expected 3 got %d", info->num_cols);
135  fatal_error(mymsg);
136  }
137 
138  check_flags(info->columns[0], 0, "identity");
139  check_flags(info->columns[1], 1, "nullable writable");
140  check_flags(info->columns[2], 2, "writable");
141 
142  test_end();
143  }
144 
145 
146  /* check select of 2 field */
147  test_begin("select c, b from #tmp1 for browse");
149 
150  if (info->num_cols != 3)
151  fatal_error("wrong number of columns returned");
152 
153  check_flags(info->columns[0], 0, "writable");
154 
155  if (!IS_TDS42(tds->conn)) {
156  check_flags(info->columns[1], 1, "nullable writable");
157  } else {
158  check_flags(info->columns[1], 1, "writable-nullable writable");
159  }
160  /* TDS5 return not identity information altough documented.. */
161  check_flags(info->columns[2], 2, "writable identity key hidden-writable key hidden");
162 
163  test_end();
164 
165  try_tds_logout(login, tds, 0);
166  return 0;
167 }
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
static uch flags
static void check_flags(TDSCOLUMN *curcol, int n, const char *possible_results)
Definition: flags.c:37
int main(int argc, char **argv)
Definition: flags.c:112
static void fatal_error(const char *msg)
Definition: flags.c:30
static TDSLOGIN * login
Definition: flags.c:26
static void test_end(void)
Definition: flags.c:93
static void test_begin(const char *cmd)
Definition: flags.c:73
static TDSSOCKET * tds
Definition: flags.c:27
#define NULL
Definition: ncbistd.hpp:225
exit(2)
yy_size_t n
static MDB_envinfo info
Definition: mdb_load.c:37
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define strdup
Definition: ncbi_ansi_ext.h:70
#define tds_submit_query
#define tds_process_tokens
#define strcat(s, k)
Metadata about columns in regular and compute rows.
Definition: tds.h:761
unsigned int column_writeable
Definition: tds.h:796
unsigned int column_identity
Definition: tds.h:797
unsigned int column_key
Definition: tds.h:798
unsigned int column_nullable
Definition: tds.h:795
unsigned int column_hidden
Definition: tds.h:799
Definition: tds.h:584
Hold information for any results.
Definition: tds.h:842
Information for a server connection.
Definition: tds.h:1211
TDSRESULTINFO * current_results
Current query information.
Definition: tds.h:1263
TDSRESULTINFO * res_info
Definition: tds.h:1264
TDSCONNECTION conn[1]
Definition: tds.h:1215
int run_query(TDSSOCKET *tds, const char *query)
Definition: common.c:143
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
#define TDS_ROWFMT_RESULT
Definition: tds.h:224
tds_sysdep_int32_type TDS_INT
Definition: tds.h:149
#define IS_TDS42(x)
Definition: tds.h:1699
@ TDS_TOKEN_RESULTS
Definition: tds.h:261
#define TDS_DONE_RESULT
Definition: tds.h:227
#define TDS_NO_MORE_RESULTS
Definition: tds.h:202
@ TDS_DONE_ERROR
error occurred
Definition: tds.h:272
#define TDS_SUCCESS
Definition: tds.h:203
Modified on Sat Dec 02 09:19:54 2023 by modify_doxy.py rev. 669887