NCBI C++ ToolKit
dataread.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.

1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 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 /*
21  * Test data from server
22  * The test do a select for a given type, convert to char and compare result
23  */
24 #include "common.h"
25 
26 #include <freetds/convert.h>
27 
28 #include <common/test_assert.h>
29 
30 static int g_result = 0;
31 static TDSLOGIN *login;
32 static TDSSOCKET *tds;
33 
34 static void test0(const char *type, ...);
35 
36 static void
37 test(const char *type, const char *value, const char *result)
38 {
40 }
41 
42 static void
43 exec_query(const char *query)
44 {
46  fprintf(stderr, "executing query failed\n");
47  exit(1);
48  }
49 }
50 
51 static void
52 test0(const char *type, ...)
53 {
54  char buf[512];
55  CONV_RESULT cr;
56  int rc;
57  TDS_INT result_type;
58  int done_flags;
59  va_list ap;
60  struct {
61  const char *value;
62  const char *result;
63  } data[10];
64  int num_data = 0, i_row;
65 
66  sprintf(buf, "CREATE TABLE #tmp(a %s)", type);
67  exec_query(buf);
68 
69  va_start(ap, type);
70  for (;;) {
71  const char * value = va_arg(ap, const char *);
72  const char * result;
73  if (!value)
74  break;
75  result = va_arg(ap, const char *);
76  if (!result)
77  result = value;
78  data[num_data].value = value;
79  data[num_data].result = result;
80  sprintf(buf, "INSERT INTO #tmp VALUES(CONVERT(%s,'%s'))", type, value);
81  exec_query(buf);
82  ++num_data;
83  }
84  va_end(ap);
85 
86  assert(num_data > 0);
87 
88  /* execute it */
89  rc = tds_submit_query(tds, "SELECT * FROM #tmp");
90  if (rc != TDS_SUCCESS) {
91  fprintf(stderr, "tds_submit_query() failed\n");
92  exit(1);
93  }
94 
95  if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) {
96  fprintf(stderr, "tds_process_tokens() failed\n");
97  exit(1);
98  }
99 
100  if (result_type != TDS_ROWFMT_RESULT) {
101  fprintf(stderr, "expected row fmt() failed\n");
102  exit(1);
103  }
104 
105  if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) {
106  fprintf(stderr, "tds_process_tokens() failed\n");
107  exit(1);
108  }
109 
110  if (result_type != TDS_ROW_RESULT) {
111  fprintf(stderr, "expected row result() failed\n");
112  exit(1);
113  }
114 
115  i_row = 0;
116  while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS && (result_type == TDS_ROW_RESULT || result_type == TDS_COMPUTE_RESULT)) {
117 
118  TDSCOLUMN *curcol = tds->current_results->columns[0];
119  TDS_CHAR *src = (TDS_CHAR *) curcol->column_data;
120  int conv_type = tds_get_conversion_type(curcol->column_type, curcol->column_size);
121 
122  assert(i_row < num_data);
123 
124  if (is_blob_col(curcol) && curcol->column_type != SYBVARIANT) {
125  TDSBLOB *blob = (TDSBLOB *) src;
126 
127  src = blob->textvalue;
128  }
129 
130  if (tds_convert(test_context, conv_type, src, curcol->column_cur_size, SYBVARCHAR, &cr) < 0) {
131  fprintf(stderr, "Error converting\n");
132  g_result = 1;
133  } else {
134  if (strcmp(data[i_row].result, cr.c) != 0) {
135  fprintf(stderr, "Failed! Is \n%s\nShould be\n%s\n", cr.c, data[i_row].result);
136  g_result = 1;
137  }
138  free(cr.c);
139  }
140  ++i_row;
141  }
142 
143  if (rc != TDS_NO_MORE_RESULTS && rc != TDS_SUCCESS) {
144  fprintf(stderr, "tds_process_tokens() unexpected return\n");
145  exit(1);
146  }
147 
148  while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
149  switch (result_type) {
150  case TDS_NO_MORE_RESULTS:
151  return;
152 
153  case TDS_DONE_RESULT:
154  case TDS_DONEPROC_RESULT:
156  if (!(done_flags & TDS_DONE_ERROR))
157  break;
158 
159  default:
160  fprintf(stderr, "tds_process_tokens() unexpected result_type\n");
161  exit(1);
162  break;
163  }
164  }
165 
166  exec_query("DROP TABLE #tmp");
167 }
168 
169 int
170 main(int argc, char **argv)
171 {
172  fprintf(stdout, "%s: Testing conversion from server\n", __FILE__);
173  if (try_tds_login(&login, &tds, __FILE__, 0) != TDS_SUCCESS) {
174  fprintf(stderr, "try_tds_login() failed\n");
175  return 1;
176  }
177 
178  /* bit */
179  test("BIT", "0", NULL);
180  test("BIT", "1", NULL);
181 
182  /* integers */
183  test("TINYINT", "234", NULL);
184  test("SMALLINT", "-31789", NULL);
185  test("INT", "16909060", NULL);
186 
187  /* floating point */
188  test("REAL", "1.25", NULL);
189  test("FLOAT", "-49586.345", "-49586.345000000001");
190 
191  /* money */
192  test("MONEY", "-123.3400", "-123.34");
193  test("MONEY", "-123.3450", "-123.35");
194  test("MONEY", "123.3450", "123.35");
195  /* very long money, this test int64 operations too */
196  test("MONEY", "123456789012345.67", NULL);
197  /* test smaller money */
198  test("MONEY", "-922337203685477.5808", "-922337203685477.58");
199  test("SMALLMONEY", "89123.12", NULL);
200  test("SMALLMONEY", "-123.3400", "-123.34");
201  test("SMALLMONEY", "-123.3450", "-123.35");
202  test("SMALLMONEY", "123.3450", "123.35");
203  /* test smallest smallmoney */
204  test("SMALLMONEY", "-214748.3648", "-214748.36");
205 
206  /* char */
207  test("CHAR(10)", "pippo", "pippo ");
208  test("VARCHAR(20)", "pippo", NULL);
209  test0("TEXT", "a", NULL, "foofoo", NULL, "try with a relatively long value, we hope for the best", NULL, NULL);
210 
211  /* binary */
212  test("VARBINARY(6)", "foo", "666f6f");
213  test("BINARY(6)", "foo", "666f6f000000");
214  test0("IMAGE", "foo", "666f6f", "foofoofoofoo", "666f6f666f6f666f6f666f6f", NULL);
215 
216  /* numeric */
217  test("NUMERIC(10,2)", "12765.76", NULL);
218  test("NUMERIC(18,4)", "12765.761234", "12765.7612");
219 
220  /* date */
222  test_context->locale->date_fmt = strdup("%Y-%m-%d %H:%M:%S");
223 
224  test("DATETIME", "2003-04-21 17:50:03", NULL);
225  test("SMALLDATETIME", "2003-04-21 17:50:03", "2003-04-21 17:50:00");
226 
227  if (IS_TDS7_PLUS(tds->conn)) {
228  test("UNIQUEIDENTIFIER", "12345678-1234-A234-9876-543298765432", NULL);
229  test("NVARCHAR(20)", "Excellent test", NULL);
230  test("NCHAR(20)", "Excellent test", "Excellent test ");
231  test("NTEXT", "Excellent test", NULL);
232  }
233 
234  if (IS_TDS71_PLUS(tds->conn)) {
235  test("SQL_VARIANT", "test123", NULL);
236  }
237 
238  try_tds_logout(login, tds, 0);
239  return g_result;
240 }
char value[7]
Definition: config.c:431
static void test(const char *type, const char *value, const char *result)
Definition: dataread.c:37
int main(int argc, char **argv)
Definition: dataread.c:170
static void exec_query(const char *query)
Definition: dataread.c:43
static TDSLOGIN * login
Definition: dataread.c:31
static void test0(const char *type,...)
Definition: dataread.c:52
static int g_result
Definition: dataread.c:30
static TDSSOCKET * tds
Definition: dataread.c:32
#define NULL
Definition: ncbistd.hpp:225
exit(2)
char * buf
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define strdup
Definition: ncbi_ansi_ext.h:70
@ SYBVARIANT
Definition: proto.h:200
#define tds_process_simple_query
#define tds_submit_query
#define tds_get_conversion_type
#define tds_convert
#define tds_process_tokens
#define assert(x)
Definition: srv_diag.hpp:58
static string query
Information about blobs (e.g.
Definition: tds.h:658
TDS_CHAR * textvalue
Definition: tds.h:659
Metadata about columns in regular and compute rows.
Definition: tds.h:761
TDS_INT column_size
maximun size of data.
Definition: tds.h:766
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
TDS_INT column_cur_size
size written in variable (ie: char, text, binary).
Definition: tds.h:811
TDSLOCALE * locale
Definition: tds.h:1099
char * date_fmt
Definition: tds.h:650
Definition: tds.h:584
TDSCOLUMN ** columns
Definition: tds.h:844
Information for a server connection.
Definition: tds.h:1211
TDSRESULTINFO * current_results
Current query information.
Definition: tds.h:1263
TDSCONNECTION conn[1]
Definition: tds.h:1215
Definition: type.c:6
#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
TDSCONTEXT * test_context
Definition: common.c:52
#define TDS_DONEPROC_RESULT
Definition: tds.h:228
#define IS_TDS71_PLUS(x)
Definition: tds.h:1709
#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 is_blob_col(x)
Definition: tds.h:445
#define TDS_COMPUTE_RESULT
Definition: tds.h:220
#define IS_TDS7_PLUS(x)
Definition: tds.h:1708
char TDS_CHAR
Definition: tds.h:144
#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
TDS_CHAR * c
Definition: convert.h:58
void free(voidpf ptr)
Modified on Tue Apr 09 08:00:27 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:35:48 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:21:37 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:23:41 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:50:47 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:29:41 2024 by modify_doxy.py rev. 669887